HArD::Core2D
Hybrid Arbitrary Degree::Core 2D - Library to implement 2D schemes with edge and cell polynomials as unknowns
Loading...
Searching...
No Matches
Mesh2D.hpp
Go to the documentation of this file.
1#include "Polytope2D.hpp"
2
3#ifndef _MESH2D_HPP
4#define _MESH2D_HPP
5
6namespace Mesh2D
7{
8 inline double signed_area(VectorRd A, VectorRd B, VectorRd C) // returns signed area of trangle ABC. Positive if anticlock, negative otherwise
9 {
10 return (A(0) * (B(1) - C(1)) + B(0) * (C(1) - A(1)) + C(0) * (A(1) - B(1))) / 2.0;
11 }
12
13
25 class Mesh
26 {
27 public:
28 Mesh() {}
30 {
31 for (auto &vertex : _vertices)
32 {
33 delete vertex;
34 }
35 for (auto &edge : _edges)
36 {
37 delete edge;
38 }
39 for (auto &cell : _cells)
40 {
41 delete cell;
42 }
43 }
44
45 void set_name(std::string name) { _mesh_name = name; }
46 inline std::string get_name() { return _mesh_name; }
47
48 double h_max() const
49 {
50 double val = 0.0;
51 for (auto &cell : _cells)
52 {
53 val = std::max(val, cell->diam());
54 }
55 return val;
56 }
57
58 inline std::size_t dim() const { return DIMENSION; }
59
60 inline std::size_t n_vertices() const { return _vertices.size(); }
61 inline std::size_t n_edges() const { return _edges.size(); }
62 inline std::size_t n_faces() const { return _edges.size(); }
63 inline std::size_t n_cells() const { return _cells.size(); }
64
65 inline std::size_t n_b_vertices() const { return _b_vertices.size(); }
66 inline std::size_t n_b_edges() const { return _b_edges.size(); }
67 inline std::size_t n_b_faces() const { return _b_edges.size(); }
68 inline std::size_t n_b_cells() const { return _b_cells.size(); }
69
70 inline std::size_t n_i_vertices() const { return _i_vertices.size(); }
71 inline std::size_t n_i_edges() const { return _i_edges.size(); }
72 inline std::size_t n_i_faces() const { return _i_edges.size(); }
73 inline std::size_t n_i_cells() const { return _i_cells.size(); }
74
75 inline std::vector<Vertex *> get_vertices() const { return _vertices; }
76 inline std::vector<Edge *> get_edges() const { return _edges; }
77 inline std::vector<Face *> get_faces() const { return _edges; }
78 inline std::vector<Cell *> get_cells() const { return _cells; }
79
80 inline std::vector<Vertex *> get_b_vertices() const { return _b_vertices; }
81 inline std::vector<Edge *> get_b_edges() const { return _b_edges; }
82 inline std::vector<Face *> get_b_faces() const { return _b_edges; }
83 inline std::vector<Cell *> get_b_cells() const { return _b_cells; }
84
85 inline std::vector<Vertex *> get_i_vertices() const { return _i_vertices; }
86 inline std::vector<Edge *> get_i_edges() const { return _i_edges; }
87 inline std::vector<Face *> get_i_faces() const { return _i_edges; }
88 inline std::vector<Cell *> get_i_cells() const { return _i_cells; }
89
91 {
92 assert(std::find(_vertices.begin(), _vertices.end(), vertex) == _vertices.end());
93 _vertices.push_back(vertex);
94 }
95
97 {
98 assert(std::find(_edges.begin(), _edges.end(), edge) == _edges.end());
99 _edges.push_back(edge);
100 }
101
103 {
104 assert(std::find(_edges.begin(), _edges.end(), face) == _edges.end());
105 _edges.push_back(face);
106 }
107
109 {
110 assert(std::find(_cells.begin(), _cells.end(), cell) == _cells.end());
111 _cells.push_back(cell);
112 }
113
115 {
116 assert(std::find(_b_vertices.begin(), _b_vertices.end(), vertex) == _b_vertices.end());
117 _b_vertices.push_back(vertex);
118 }
119
121 {
122 assert(std::find(_b_edges.begin(), _b_edges.end(), edge) == _b_edges.end());
123 _b_edges.push_back(edge);
124 }
125
127 {
128 assert(std::find(_b_edges.begin(), _b_edges.end(), face) == _b_edges.end());
129 _b_edges.push_back(face);
130 }
131
133 {
134 assert(std::find(_b_cells.begin(), _b_cells.end(), cell) == _b_cells.end());
135 _b_cells.push_back(cell);
136 }
137
139 {
140 assert(std::find(_i_vertices.begin(), _i_vertices.end(), vertex) == _i_vertices.end());
141 _i_vertices.push_back(vertex);
142 }
143
145 {
146 assert(std::find(_i_edges.begin(), _i_edges.end(), edge) == _i_edges.end());
147 _i_edges.push_back(edge);
148 }
149
151 {
152 assert(std::find(_i_edges.begin(), _i_edges.end(), face) == _i_edges.end());
153 _i_edges.push_back(face);
154 }
155
157 {
158 assert(std::find(_i_cells.begin(), _i_cells.end(), cell) == _i_cells.end());
159 _i_cells.push_back(cell);
160 }
161
162 // Note that all these assume that a MeshObject's index is equal to its position in the mesh!!
163 inline Vertex *vertex(std::size_t index) const
164 {
165 assert(index < _vertices.size());
166 return _vertices[index];
167 }
168 inline Edge *edge(std::size_t index) const
169 {
170 assert(index < _edges.size());
171 return _edges[index];
172 }
173 inline Face *face(std::size_t index) const
174 {
175 assert(index < _edges.size());
176 return _edges[index];
177 }
178 inline Cell *cell(std::size_t index) const
179 {
180 assert(index < _cells.size());
181 return _cells[index];
182 }
183
184 inline Vertex *b_vertex(std::size_t index) const
185 {
186 assert(index < _b_vertices.size());
187 return _b_vertices[index];
188 }
189 inline Edge *b_edge(std::size_t index) const
190 {
191 assert(index < _b_edges.size());
192 return _b_edges[index];
193 }
194 inline Face *b_face(std::size_t index) const
195 {
196 assert(index < _b_edges.size());
197 return _b_edges[index];
198 }
199 inline Cell *b_cell(std::size_t index) const
200 {
201 assert(index < _b_cells.size());
202 return _b_cells[index];
203 }
204
205 inline Vertex *i_vertex(std::size_t index) const
206 {
207 assert(index < _i_vertices.size());
208 return _i_vertices[index];
209 }
210 inline Edge *i_edge(std::size_t index) const
211 {
212 assert(index < _i_edges.size());
213 return _i_edges[index];
214 }
215 inline Face *i_face(std::size_t index) const
216 {
217 assert(index < _i_edges.size());
218 return _i_edges[index];
219 }
220 inline Cell *i_cell(std::size_t index) const
221 {
222 assert(index < _i_cells.size());
223 return _i_cells[index];
224 }
225
226 std::vector<double> regularity()
227 {
234
235 std::vector<std::vector<double>> reg_cell(n_cells(), {0.0, 0.0});
236 std::size_t count = 0;
237 for (auto &T : _cells)
238 {
239 double hT = T->diam();
240 VectorRd xT = T->center_mass();
241
242 reg_cell[count][0] = hT / pow(T->measure(), 1.0 / DIMENSION);
243
244 double rhoT = hT;
245 std::vector<Face *> faces = T->get_faces();
246 for (auto &F : faces)
247 {
248 double hF = F->diam();
249 VectorRd xF = F->center_mass();
250 VectorRd nTF = F->normal(); // sign does not matter
251
252 reg_cell[count][0] = std::max(reg_cell[count][0], hT / hF);
253
254 rhoT = std::min(rhoT, std::abs((xT - xF).dot(nTF))); // If xT is not in T, is this really a good measure?
255 }
256 reg_cell[count][1] = hT / rhoT;
257 ++count; // could just use iterators
258 }
259
260 std::vector<double> value(2, 0.0);
261 for (size_t iT = 0; iT < n_cells(); iT++)
262 {
263 value[0] = std::max(value[0], reg_cell[iT][0]);
264 value[1] = std::max(value[1], reg_cell[iT][1]);
265 }
266
267 return value;
268 }
269
270 void renum(const char B, const std::vector<size_t> new_to_old)
271 {
272
273 switch (B)
274 {
275 case 'C':
276 {
277 std::vector<Cell *> old_index = _cells;
278 for (size_t i = 0; i < _cells.size(); i++)
279 {
280 old_index[new_to_old[i]]->set_global_index(i);
281 _cells[i] = old_index[new_to_old[i]];
282 }
283 break;
284 }
285
286 case 'F':
287 {
288 std::vector<Face *> old_index = _edges;
289 for (size_t i = 0; i < _edges.size(); i++)
290 {
291 old_index[new_to_old[i]]->set_global_index(i);
292 _edges[i] = old_index[new_to_old[i]];
293 }
294 break;
295 }
296
297 case 'E':
298 {
299 std::vector<Edge *> old_index = _edges;
300 for (size_t i = 0; i < _edges.size(); i++)
301 {
302 old_index[new_to_old[i]]->set_global_index(i);
303 _edges[i] = old_index[new_to_old[i]];
304 }
305 break;
306 }
307
308 case 'V':
309 {
310 std::vector<Vertex *> old_index = _vertices;
311 for (size_t i = 0; i < _vertices.size(); i++)
312 {
313 old_index[new_to_old[i]]->set_global_index(i);
314 _vertices[i] = old_index[new_to_old[i]];
315 }
316 break;
317 }
318 }
319 }
320
321 size_t find_cell(const VectorRd x) const
322 {
323
324 // Locate neighbouring cells
325 std::vector<size_t> neighbouring_cells;
326 for (Cell *T : get_cells())
327 {
328 if ((T->center_mass() - x).norm() <= T->diam())
329 {
330 neighbouring_cells.push_back(T->global_index());
331 }
332 }
333
334 // In neighbouring cells, find one such that x is contained in one of the subtriangles
335 size_t i = 0;
336 bool found = false;
337 size_t iT = 0;
338 while (i < neighbouring_cells.size() && !found)
339 {
340 iT = neighbouring_cells[i];
341 Cell *T = cell(iT);
342
343 for(auto& simplex : T->get_simplices())
344 {
345 double area1 = signed_area(x, simplex[0], simplex[1]);
346 double area2 = signed_area(x, simplex[1], simplex[2]);
347 double area3 = signed_area(x, simplex[2], simplex[0]);
348
349 found = (area1 >= 0.0 && area2 >= 0.0 && area3 >= 0.0) || (area1 <= 0.0 && area2 <= 0.0 && area3 <= 0.0); // if all non neg or non pos must be in or on triangle
350 if(found) break;
351 }
352 ++i;
353 }
354 assert(found);
355
356 return iT;
357 }
358
359 void plot_simplices(std::ofstream *out)
360 {
361 for (auto &cell : _cells)
362 {
363 cell->plot_simplices(out);
364 }
365 }
366
367 void plot_mesh(std::ofstream *out)
368 {
369 for (auto &edge : _edges)
370 {
371 edge->plot(out);
372 }
373 }
374
375 private:
376 std::string _mesh_name;
377
378 std::vector<Vertex *> _vertices;
379 std::vector<Edge *> _edges;
380 // std::vector<Face*> _faces;
381 std::vector<Cell *> _cells;
382
383 std::vector<Vertex *> _b_vertices;
384 std::vector<Edge *> _b_edges;
385 // std::vector<Face*> _b_faces;
386 std::vector<Cell *> _b_cells;
387
388 std::vector<Vertex *> _i_vertices;
389 std::vector<Edge *> _i_edges;
390 // std::vector<Face*> _i_faces;
391 std::vector<Cell *> _i_cells;
392 };
393} // namespace MeshND
394
395#endif
Definition Mesh2D.hpp:26
A Vertex is a Polytope with object_dim = 0.
Definition Polytope2D.hpp:54
Compute max and min eigenvalues of all matrices for i
Definition compute_eigs.m:5
std::vector< Cell * > get_cells() const
lists the cells in the mesh.
Definition Mesh2D.hpp:78
std::vector< Face * > get_b_faces() const
lists the boundary faces in the mesh.
Definition Mesh2D.hpp:82
std::size_t n_b_edges() const
number of boundary edges in the mesh.
Definition Mesh2D.hpp:66
Cell * i_cell(std::size_t index) const
get a constant pointer to an internal cell using an index
Definition Mesh2D.hpp:220
double h_max() const
< max diameter of cells
Definition Mesh2D.hpp:48
std::vector< Face * > get_faces() const
lists the faces in the mesh.
Definition Mesh2D.hpp:77
std::size_t n_faces() const
number of faces in the mesh.
Definition Mesh2D.hpp:62
void add_face(Face *face)
Definition Mesh2D.hpp:102
std::string get_name()
getter for the mesh name
Definition Mesh2D.hpp:46
void plot(std::ofstream *out) const
Plot the polytope to out.
Definition Polytope2D.hpp:489
std::size_t n_vertices() const
number of vertices in the mesh.
Definition Mesh2D.hpp:60
std::size_t n_i_cells() const
number of internal cells in the mesh.
Definition Mesh2D.hpp:73
std::size_t dim() const
dimension of the mesh
Definition Mesh2D.hpp:58
Face * face(std::size_t index) const
get a constant pointer to a face using its global index
Definition Mesh2D.hpp:173
void add_vertex(Vertex *vertex)
Definition Mesh2D.hpp:90
Vertex * b_vertex(std::size_t index) const
get a constant pointer to a boundary vertex using an index
Definition Mesh2D.hpp:184
Cell * cell(std::size_t index) const
get a constant pointer to a cell using its global index
Definition Mesh2D.hpp:178
void add_b_cell(Cell *cell)
Definition Mesh2D.hpp:132
void add_i_cell(Cell *cell)
Definition Mesh2D.hpp:156
void add_b_vertex(Vertex *vertex)
Definition Mesh2D.hpp:114
std::vector< Edge * > get_i_edges() const
lists the internal edges in the mesh.
Definition Mesh2D.hpp:86
void plot_simplices(std::ofstream *out) const
Plot the simplices to out.
Definition Polytope2D.hpp:473
std::vector< double > regularity()
Definition Mesh2D.hpp:226
Edge * i_edge(std::size_t index) const
get a constant pointer to an internal edge using an index
Definition Mesh2D.hpp:210
std::vector< Edge * > get_edges() const
lists the edges in the mesh.
Definition Mesh2D.hpp:76
Edge * edge(std::size_t index) const
get a constant pointer to a edge using its global index
Definition Mesh2D.hpp:168
void add_i_vertex(Vertex *vertex)
Definition Mesh2D.hpp:138
void set_name(std::string name)
set the name of the mesh
Definition Mesh2D.hpp:45
std::vector< Edge * > get_b_edges() const
lists the boundary edges in the mesh.
Definition Mesh2D.hpp:81
void add_i_face(Face *face)
Definition Mesh2D.hpp:150
~Mesh()
Definition Mesh2D.hpp:29
void plot_simplices(std::ofstream *out)
Definition Mesh2D.hpp:359
std::vector< Face * > get_i_faces() const
lists the internal faces in the mesh.
Definition Mesh2D.hpp:87
Face * i_face(std::size_t index) const
get a constant pointer to an internal face using an index
Definition Mesh2D.hpp:215
double diam() const
Return the diameter of the Polytope.
Definition Polytope2D.hpp:74
void plot_mesh(std::ofstream *out)
Definition Mesh2D.hpp:367
Mesh()
Definition Mesh2D.hpp:28
std::size_t n_b_faces() const
number of boundary faces in the mesh.
Definition Mesh2D.hpp:67
std::size_t n_i_faces() const
number of internal faces in the mesh.
Definition Mesh2D.hpp:72
void add_i_edge(Edge *edge)
Definition Mesh2D.hpp:144
Edge * b_edge(std::size_t index) const
get a constant pointer to boundary a edge using an index
Definition Mesh2D.hpp:189
std::size_t n_cells() const
number of cells in the mesh.
Definition Mesh2D.hpp:63
std::vector< Cell * > get_i_cells() const
lists the internal cells in the mesh.
Definition Mesh2D.hpp:88
void renum(const char B, const std::vector< size_t > new_to_old)
Definition Mesh2D.hpp:270
std::size_t n_i_edges() const
number of internal edges in the mesh.
Definition Mesh2D.hpp:71
void add_b_face(Face *face)
Definition Mesh2D.hpp:126
Cell * b_cell(std::size_t index) const
get a constant pointer to boundary a cell using an index
Definition Mesh2D.hpp:199
size_t find_cell(const VectorRd x) const
< returns the index of the cell containing the point x
Definition Mesh2D.hpp:321
std::vector< Vertex * > get_b_vertices() const
lists the boundary vertices in the mesh.
Definition Mesh2D.hpp:80
std::vector< Vertex * > get_i_vertices() const
lists the internal vertices in the mesh.
Definition Mesh2D.hpp:85
Vertex * i_vertex(std::size_t index) const
get a constant pointer to an internal vertex using an index
Definition Mesh2D.hpp:205
std::size_t n_b_cells() const
number of boundary cells in the mesh.
Definition Mesh2D.hpp:68
Vertex * vertex(std::size_t index) const
get a constant pointer to a vertex using its global index
Definition Mesh2D.hpp:163
std::size_t n_i_vertices() const
number of internal vertices in the mesh.
Definition Mesh2D.hpp:70
Face * b_face(std::size_t index) const
get a constant pointer to boundary a face using an index
Definition Mesh2D.hpp:194
std::size_t n_edges() const
number of edges in the mesh.
Definition Mesh2D.hpp:61
std::vector< Cell * > get_b_cells() const
lists the boundary cells in the mesh.
Definition Mesh2D.hpp:83
void add_cell(Cell *cell)
Definition Mesh2D.hpp:108
void add_edge(Edge *edge)
Definition Mesh2D.hpp:96
void add_b_edge(Edge *edge)
Definition Mesh2D.hpp:120
std::size_t n_b_vertices() const
number of boundary vertices in the mesh.
Definition Mesh2D.hpp:65
std::vector< Vertex * > get_vertices() const
lists the vertices in the mesh.
Definition Mesh2D.hpp:75
A
Definition mmread.m:102
if(strcmp(field, 'real')) % real valued entries T
Definition mmread.m:93
end Read size then branch according to sparse or dense format count
Definition mmread.m:75
Definition Mesh2D.hpp:7
double signed_area(VectorRd A, VectorRd B, VectorRd C)
Definition Mesh2D.hpp:8
constexpr int DIMENSION
Definition Polytope2D.hpp:17
Eigen::Matrix< double, DIMENSION, 1 > VectorRd
Definition Polytope2D.hpp:19