This source file includes following definitions.
- update_vertices
- dgf_find_vertex
- dgf_close_list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #ifndef GRID_GRIDANDTIME_IMPL_H
17 #define GRID_GRIDANDTIME_IMPL_H
18
19 #include <vector>
20 #include <array>
21 #include <string>
22 #include <unordered_map>
23 #include <algorithm>
24 #include <boost/functional/hash.hpp>
25 #include "grid.h"
26
27
28 namespace std{
29
30
31 template<> struct std::hash<Esfem::Grid::Deformation::Domain>{
32
33
34 std::size_t operator()(const Esfem::Grid::Deformation::Domain& n) const{
35 using std::hash;
36
37
38
39 using boost::hash_value;
40 using boost::hash_combine;
41 size_t seed {0};
42 hash_combine(seed, hash_value(n[0]));
43 hash_combine(seed, hash_value(n[1]));
44 hash_combine(seed, hash_value(n[2]));
45 return seed;
46 }
47 };
48
49 template<> struct std::hash<array<int, Esfem::Grid::world_dim()> >{
50
51 using Base = array<int, Esfem::Grid::world_dim()>;
52
53
54 std::size_t operator()(const Base& ai) const{
55
56
57
58
59 using boost::hash_value;
60 using boost::hash_combine;
61 size_t seed {0};
62 for(int i = 0; i < Esfem::Grid::world_dim(); ++i)
63 hash_combine(seed, hash_value(ai[i]));
64 return seed;
65 }
66 };
67
68 template<> struct std::equal_to<Esfem::Grid::Deformation::Domain>{
69
70
71 bool operator()(const Esfem::Grid::Deformation::Domain& lhs,
72 const Esfem::Grid::Deformation::Domain& rhs) const{
73 bool rv = true;
74 for(int i = 0; i < Esfem::Grid::Deformation::Domain::dimension; ++i)
75 rv = rv && lhs[i] == rhs[i];
76 return rv;
77 }
78 };
79
80 template<> struct std::equal_to<array<int, Esfem::Grid::world_dim()> >{
81
82 using Base = array<int, Esfem::Grid::world_dim()>;
83
84
85 bool operator()(const Base& lhs, const Base& rhs) const{
86 bool rv = true;
87 for(int i = 0; i < Esfem::Grid::world_dim(); ++i)
88 rv = rv && lhs[i] == rhs[i];
89 return rv;
90 }
91 };
92 }
93
94 namespace Esfem{
95 namespace Impl{
96
97
98 class Evolving_grid{
99 public:
100
101 using Node = Grid::Deformation::Domain;
102
103 Evolving_grid() = default;
104
105 explicit Evolving_grid(const std::string& filename);
106
107
108 const Node& operator[](const Node&) const;
109
110 Evolving_grid& operator=(const Grid::Vec_FEfun& new_nodes);
111
112
113 using Nodes_key = std::vector<std::string>;
114
115 using Map = std::unordered_map<std::string, Node>;
116 private:
117
118 const Nodes_key original_vertices;
119
120 std::size_t digit_precision {8};
121
122 Map vertices_map;
123
124
125
126 template<typename It, typename Ot>
127 void update_vertices(It first, It last, Ot out, const int dim);
128 };
129
130
131 namespace hash{
132
133 using range = Grid::Deformation::Domain;
134
135 using key = std::array<int, range::dimension>;
136
137
138
139 class grid{
140
141 static constexpr auto dim = range::dimension;
142
143 using map = std::unordered_map<key, range>;
144
145 using seq = std::vector<key>;
146
147 map m;
148
149 seq ol;
150 public:
151
152 struct bad : std::runtime_error{
153
154 explicit bad(const std::string& msg) :std::runtime_error {msg} {}
155 };
156
157 grid() = default;
158
159 explicit grid(const std::string& fname);
160
161
162 explicit grid(const Grid::Vec_FEfun& init_keys);
163
164 grid& operator=(const Grid::Vec_FEfun& value_list);
165
166
167 const range& operator[](const range&) const;
168 };
169
170 std::string to_string(const range&);
171
172 key to_key(const range&);
173 }
174
175
176
177
178 Evolving_grid::Nodes_key get_vertexList(const std::string& filename);
179
180
181 std::size_t get_relevant_precision(const Evolving_grid::Nodes_key& vertex_list);
182
183 Evolving_grid::Map
184 identity_map(const Evolving_grid::Nodes_key&, const std::size_t precision);
185
186 std::size_t get_relevant_precision(const std::string& number);
187
188
189 std::vector<std::size_t>
190 create_precision_vec(const std::vector<std::string>& vertex_list);
191
192
193
194 std::size_t max(const std::vector<std::size_t>&);
195
196 std::string node_to_string(const Evolving_grid::Node&, const int precision);
197 Evolving_grid::Node string_to_node(const std::string&);
198 std::string normalize_node(const std::string&, const int precision);
199
200 std::string clean_whitespace(const std::string&);
201
202 std::vector<std::string> dgfFile_to_vec(const std::string& filename);
203
204 template<typename It>
205 It dgf_find_vertex(It first, It last, const std::string& filename);
206 template<typename It>
207 It dgf_close_list(It first, It last, const std::string& filename);
208
209
210
211
212 template<typename It, typename Ot>
213 void Evolving_grid::update_vertices(It first, It last, Ot out, const int dim){
214 while(first != last){
215 Node new_vertex {};
216 for(int i = 0; i < dim; ++i){
217 new_vertex[i] = *first;
218 ++first;
219 }
220 try{
221 vertices_map.at(*out) = new_vertex;
222 }
223 catch(...){
224 const auto err_msg = "Bad node: " + *out;
225 throw std::logic_error {err_msg};
226 }
227 ++out;
228 }
229 }
230
231 template<typename It>
232 It dgf_find_vertex(It first, It last, const std::string& filename){
233 std::string starting_point {"VERTEX"};
234 It rv = std::find(first, last, "VERTEX");
235 if(rv == last)
236 throw std::runtime_error
237 {"Missing keyword \"" + starting_point
238 + "\" in file " + filename + "."};
239 ++rv;
240 return rv;
241 }
242 template<typename It>
243 It dgf_close_list(It first, It last, const std::string& filename){
244 std::string ending_point {"#"};
245 It rv = std::find(first, last, ending_point);
246 if(rv == last)
247 throw std::runtime_error
248 {"Missing ending symbol \"" + ending_point
249 + "\" in file " + filename + "."};
250 return rv;
251 }
252 }
253 }
254
255 #endif
256
257
258