PolyFEM
Loading...
Searching...
No Matches
LagrangeBasis2d.cpp
Go to the documentation of this file.
1
2#include "LagrangeBasis2d.hpp"
3
8
10
12
13#include <cassert>
14#include <array>
16
17using namespace polyfem;
18using namespace polyfem::assembler;
19using namespace polyfem::basis;
20using namespace polyfem::mesh;
21using namespace polyfem::quadrature;
22using namespace polyfem::utils;
23
24/*
25
26Axes:
27 y
28 |
29 o──x
30
31Boundaries:
32X axis: left/right
33Y axis: bottom/top
34
35
36
37
38
39Corner nodes:
40 v2
41 │ \
42 │ \
43 │ \
44 x x
45 │ \
46 │ \
47 │ \
48v0──────x─────v1
49
50v0 = (0, 0)
51v1 = (1, 0)
52v2 = (0, 1)
53
54Edge nodes:
55 x
56 │ \
57 │ \
58 │ \
59e2 e1
60 │ \
61 │ \
62 │ \
63 x──────e0───x
64
65e0 = (0.5, 0)
66e1 = (0.5, 0.5)
67e2 = ( 0, 0.5)
68
69
70
71
72
73
74
75
76
77Corner nodes:
78v3──────x─────v2
79 │ ┆ │
80 │ ┆ │
81 │ ┆ │
82 x┄┄┄┄┄┄x┄┄┄┄┄┄x
83 │ ┆ │
84 │ ┆ │
85 │ ┆ │
86v0──────x─────v1
87
88v0 = (0, 0)
89v1 = (1, 0)
90v2 = (1, 1)
91v3 = (0, 1)
92
93Edge nodes:
94 x─────e2──────x
95 │ ┆ │
96 │ ┆ │
97 │ ┆ │
98e3┄┄┄┄┄┄x┄┄┄┄┄e1
99 │ ┆ │
100 │ ┆ │
101 │ ┆ │
102 x─────e0──────x
103
104e0 = (0.5, 0)
105e1 = ( 1, 0.5)
106e2 = (0.5, 1)
107e3 = ( 0, 0.5)
108
109*/
110
111namespace
112{
113
114 void sort(std::array<int, 2> &array)
115 {
116 if (array[0] > array[1])
117 {
118 std::swap(array[0], array[1]);
119 }
120
121 assert(array[0] <= array[1]);
122 }
123
124 template <class InputIterator, class T>
125 int find_index(InputIterator first, InputIterator last, const T &val)
126 {
127 return std::distance(first, std::find(first, last, val));
128 }
129
130 Navigation::Index find_edge(const Mesh2D &mesh, int f, int v1, int v2)
131 {
132 std::array<int, 2> v = {{v1, v2}};
133 std::array<int, 2> u;
134 // std::sort(v.begin(), v.end());
135 sort(v);
136 auto idx = mesh.get_index_from_face(f, 0);
137 for (int lv = 0; lv < mesh.n_face_vertices(idx.face); ++lv)
138 {
139 u[0] = idx.vertex;
140 u[1] = mesh.switch_vertex(idx).vertex;
141 // std::sort(u.begin(), u.end());
142 sort(u);
143 if (u == v)
144 {
145 if (idx.vertex != v1)
146 idx = mesh.switch_vertex(idx);
147 assert(idx.vertex == v1);
148 return idx;
149 }
150 idx = mesh.next_around_face(idx);
151 }
152 throw std::runtime_error("Edge not found");
153 }
154
155 std::array<int, 3> tri_vertices_local_to_global(const Mesh2D &mesh, int f)
156 {
157 assert(mesh.is_simplex(f));
158
159 // Vertex nodes
160 std::array<int, 3> l2g;
161 for (int lv = 0; lv < 3; ++lv)
162 {
163 l2g[lv] = mesh.face_vertex(f, lv);
164 }
165
166 return l2g;
167 }
168
169 std::array<int, 4> quad_vertices_local_to_global(const Mesh2D &mesh, int f)
170 {
171 assert(mesh.is_cube(f));
172
173 // Vertex nodes
174 std::array<int, 4> l2g;
175 for (int lv = 0; lv < 4; ++lv)
176 {
177 l2g[lv] = mesh.face_vertex(f, lv);
178 }
179
180 return l2g;
181 }
182
183 void tri_local_to_global(const bool is_geom_bases, const int p, const Mesh2D &mesh, int f, const Eigen::VectorXi &discr_order, const Eigen::VectorXi &edge_orders, std::vector<int> &res, MeshNodes &nodes, std::vector<std::vector<int>> &edge_virtual_nodes)
184 {
185 const int n_edge_nodes = p > 1 ? ((p - 1) * 3) : 0;
186 const int nn = p > 2 ? (p - 2) : 0;
187 const int n_face_nodes = nn * (nn + 1) / 2;
188
189 if (p == 0)
190 {
191 res.push_back(nodes.node_id_from_face(f));
192 return;
193 }
194
195 // std::vector<int> res;
196 res.reserve(3 + n_edge_nodes + n_face_nodes);
197
198 // Vertex nodes
199 auto v = tri_vertices_local_to_global(mesh, f);
200
201 // Edge nodes
202 Eigen::Matrix<Navigation::Index, 3, 1> e;
203 Eigen::Matrix<int, 3, 2> ev;
204 ev.row(0) << v[0], v[1];
205 ev.row(1) << v[1], v[2];
206 ev.row(2) << v[2], v[0];
207 for (int le = 0; le < e.rows(); ++le)
208 {
209 const auto index = find_edge(mesh, f, ev(le, 0), ev(le, 1));
210 e[le] = index;
211 }
212
213 for (size_t lv = 0; lv < v.size(); ++lv)
214 {
215 const auto index = e[lv];
216 const auto other_face = mesh.switch_face(index).face;
217
218 if (is_geom_bases)
219 res.push_back(nodes.node_id_from_primitive(v[lv]));
220 else
221 {
222 if (!mesh.is_conforming())
223 {
224 const auto &ncmesh = dynamic_cast<const NCMesh2D &>(mesh);
225 // hanging vertex
226 if (ncmesh.leader_edge_of_vertex(ncmesh.face_vertex(f, lv)) >= 0)
227 res.push_back(-lv - 1);
228 else
229 res.push_back(nodes.node_id_from_primitive(v[lv]));
230 }
231 else
232 {
233 if (discr_order.size() > 0 && other_face >= 0 && discr_order(f) > discr_order(other_face))
234 res.push_back(-lv - 1);
235 else
236 res.push_back(nodes.node_id_from_primitive(v[lv]));
237 }
238 }
239 }
240
241 for (int le = 0; le < e.rows(); ++le)
242 {
243 const auto index = e[le];
244 const auto other_face = mesh.switch_face(index).face;
245
246 if (is_geom_bases)
247 {
248 auto node_ids = nodes.node_ids_from_edge(index, p - 1);
249 res.insert(res.end(), node_ids.begin(), node_ids.end());
250 }
251 else
252 {
253 if (!mesh.is_conforming())
254 {
255 const auto &ncmesh = dynamic_cast<const NCMesh2D &>(mesh);
256 const int edge_id = ncmesh.face_edge(f, le);
257 // follower edge
258 if (ncmesh.leader_edge_of_edge(edge_id) >= 0)
259 {
260 for (int tmp = 0; tmp < p - 1; ++tmp)
261 res.push_back(-le - 1);
262 }
263 // leader or conforming edge with constrained order
264 else if (edge_orders[edge_id] < discr_order[f])
265 {
266 for (int tmp = 0; tmp < p - 1; ++tmp)
267 res.push_back(-le - 1);
268 // leader edge
269 if (ncmesh.n_follower_edges(edge_id) > 0)
270 edge_virtual_nodes[edge_id] = nodes.node_ids_from_edge(index, edge_orders[edge_id] - 1);
271 }
272 // leader or conforming edge with unconstrained order
273 else
274 {
275 auto node_ids = nodes.node_ids_from_edge(index, p - 1);
276 res.insert(res.end(), node_ids.begin(), node_ids.end());
277 }
278 }
279 else
280 {
281 bool skip_other = discr_order.size() > 0 && other_face >= 0 && discr_order(f) > discr_order(other_face);
282
283 if (discr_order.size() > 0 && other_face >= 0 && discr_order(f) > discr_order(other_face))
284 {
285 for (int tmp = 0; tmp < p - 1; ++tmp)
286 res.push_back(-le - 1);
287 }
288 else
289 {
290 auto node_ids = nodes.node_ids_from_edge(index, p - 1);
291 res.insert(res.end(), node_ids.begin(), node_ids.end());
292 }
293 }
294 }
295 }
296
297 if (n_face_nodes > 0)
298 {
299 const auto index = e[0];
300
301 auto node_ids = nodes.node_ids_from_face(index, p - 2);
302 res.insert(res.end(), node_ids.begin(), node_ids.end());
303 }
304
305 assert(res.size() == size_t(3 + n_edge_nodes + n_face_nodes));
306 // return res;
307 }
308
309 void quad_local_to_global(const bool serendipity, const int q, const Mesh2D &mesh, int f, const Eigen::VectorXi &discr_order, std::vector<int> &res, MeshNodes &nodes)
310 {
311 const int nn = q > 1 ? (q - 1) : 0;
312 const int n_edge_nodes = nn * 4;
313 const int n_face_nodes = serendipity ? 0 : nn * nn;
314
315 if (q == 0)
316 {
317 res.push_back(nodes.node_id_from_face(f));
318 return;
319 }
320
321 // std::vector<int> res;
322 res.reserve(4 + n_edge_nodes + n_face_nodes);
323
324 // Vertex nodes
325 auto v = quad_vertices_local_to_global(mesh, f);
326
327 // Edge nodes
328 Eigen::Matrix<Navigation::Index, 4, 1> e;
329 Eigen::Matrix<int, 4, 2> ev;
330 ev.row(0) << v[0], v[1];
331 ev.row(1) << v[1], v[2];
332 ev.row(2) << v[2], v[3];
333 ev.row(3) << v[3], v[0];
334 for (int le = 0; le < e.rows(); ++le)
335 {
336 const auto index = find_edge(mesh, f, ev(le, 0), ev(le, 1));
337 e[le] = index;
338 }
339
340 for (size_t lv = 0; lv < v.size(); ++lv)
341 {
342 const auto index = e[lv];
343 const auto other_face = mesh.switch_face(index).face;
344
345 if (discr_order.size() > 0 && other_face >= 0 && discr_order(f) > discr_order(other_face))
346 res.push_back(-lv - 1);
347 else
348 res.push_back(nodes.node_id_from_primitive(v[lv]));
349 }
350
351 for (int le = 0; le < e.rows(); ++le)
352 {
353 const auto index = e[le];
354 const auto other_face = mesh.switch_face(index).face;
355
356 bool skip_other = discr_order.size() > 0 && other_face >= 0 && discr_order(f) > discr_order(other_face);
357
358 if (discr_order.size() > 0 && other_face >= 0 && discr_order(f) > discr_order(other_face))
359 {
360 for (int tmp = 0; tmp < q - 1; ++tmp)
361 res.push_back(-le - 1);
362 }
363 else
364 {
365 auto node_ids = nodes.node_ids_from_edge(index, q - 1);
366 res.insert(res.end(), node_ids.begin(), node_ids.end());
367 }
368 }
369
370 if (n_face_nodes > 0)
371 {
372 const auto index = e[0];
373
374 auto node_ids = nodes.node_ids_from_face(index, q - 1);
375 res.insert(res.end(), node_ids.begin(), node_ids.end());
376 }
377
378 assert(res.size() == size_t(4 + n_edge_nodes + n_face_nodes));
379 // return res;
380 }
381
382 // -----------------------------------------------------------------------------
383
384 // -----------------------------------------------------------------------------
385
399 void compute_nodes(
400 const Mesh2D &mesh,
401 const Eigen::VectorXi &discr_orders,
402 const Eigen::VectorXi &edge_orders,
403 const bool serendipity,
404 const bool has_polys,
405 const bool is_geom_bases,
406 MeshNodes &nodes,
407 std::vector<std::vector<int>> &edge_virtual_nodes,
408 std::vector<std::vector<int>> &element_nodes_id,
409 std::vector<LocalBoundary> &local_boundary,
410 std::map<int, InterfaceData> &poly_edge_to_data)
411 {
412 // Step 1: Assign global node ids for each quads
413 local_boundary.clear();
414
415 element_nodes_id.resize(mesh.n_faces());
416
417 if (!mesh.is_conforming())
418 {
419 const auto &ncmesh = dynamic_cast<const NCMesh2D &>(mesh);
420 edge_virtual_nodes.resize(ncmesh.n_edges());
421 }
422
423 for (int f = 0; f < mesh.n_faces(); ++f)
424 {
425 const int discr_order = discr_orders(f);
426 if (mesh.is_cube(f))
427 {
428 quad_local_to_global(serendipity, discr_order, mesh, f, discr_orders, element_nodes_id[f], nodes);
429
430 LocalBoundary lb(f, BoundaryType::QUAD_LINE);
431
432 auto v = quad_vertices_local_to_global(mesh, f);
433 Eigen::Matrix<int, 4, 2> ev;
434 ev.row(0) << v[0], v[1];
435 ev.row(1) << v[1], v[2];
436 ev.row(2) << v[2], v[3];
437 ev.row(3) << v[3], v[0];
438
439 for (int i = 0; i < int(ev.rows()); ++i)
440 {
441 const auto index = find_edge(mesh, f, ev(i, 0), ev(i, 1));
442 const int edge = index.edge;
443
444 if (mesh.is_boundary_edge(edge) || mesh.get_boundary_id(edge) > 0)
445 {
446 lb.add_boundary_primitive(edge, i);
447 }
448 }
449
450 if (!lb.empty())
451 local_boundary.emplace_back(lb);
452 }
453 else if (mesh.is_simplex(f))
454 {
455 tri_local_to_global(is_geom_bases, discr_order, mesh, f, discr_orders, edge_orders, element_nodes_id[f], nodes, edge_virtual_nodes);
456
457 auto v = tri_vertices_local_to_global(mesh, f);
458
459 Eigen::Matrix<int, 3, 2> ev;
460 ev.row(0) << v[0], v[1];
461 ev.row(1) << v[1], v[2];
462 ev.row(2) << v[2], v[0];
463
464 LocalBoundary lb(f, BoundaryType::TRI_LINE);
465
466 for (int i = 0; i < int(ev.rows()); ++i)
467 {
468 const auto index = find_edge(mesh, f, ev(i, 0), ev(i, 1));
469 const int edge = index.edge;
470
471 if (mesh.is_boundary_edge(edge) || mesh.get_boundary_id(edge) > 0)
472 {
473 lb.add_boundary_primitive(edge, i);
474 }
475 }
476
477 if (!lb.empty())
478 local_boundary.emplace_back(lb);
479 }
480 }
481
482 if (!has_polys)
483 return;
484
485 // Step 2: Iterate over edges of polygons and compute interface weights
486 Eigen::VectorXi indices;
487 for (int f = 0; f < mesh.n_faces(); ++f)
488 {
489 // Skip non-polytopes
490 if (!mesh.is_polytope(f))
491 {
492 continue;
493 }
494
495 auto index = mesh.get_index_from_face(f, 0);
496 for (int lv = 0; lv < mesh.n_face_vertices(f); ++lv)
497 {
498 auto index2 = mesh.switch_face(index);
499 if (index2.face >= 0)
500 {
501 // Opposite face is a quad, we need to set interface data
502 int f2 = index2.face;
503 const int discr_order = discr_orders(f2);
504
505 if (mesh.is_cube(f2))
506 {
507 indices = LagrangeBasis2d::quad_edge_local_nodes(discr_order, mesh, index2);
508 }
509 else if (mesh.is_simplex(f2))
510 {
511 indices = LagrangeBasis2d::tri_edge_local_nodes(discr_order, mesh, index2);
512 }
513 else
514 {
515 // assert(false);
516 continue;
517 }
518 InterfaceData data;
519 assert(indices.size() == 2 + discr_order - 1);
520 data.local_indices.insert(data.local_indices.begin(), indices.data(), indices.data() + indices.size());
521 assert(indices.size() == data.local_indices.size());
522 poly_edge_to_data[index2.edge] = data;
523 }
524 index = mesh.next_around_face(index);
525 }
526 }
527 }
528
535 void local_to_global(const Eigen::MatrixXd &verts, const Eigen::MatrixXd &uv, Eigen::MatrixXd &pts)
536 {
537 const int dim = verts.cols();
538 const int N = uv.rows();
539 assert(dim == 2);
540 assert(uv.cols() == dim);
541 assert(verts.rows() == dim + 1);
542
543 pts.setZero(N, dim);
544 for (int i = 0; i < N; i++)
545 pts.row(i) = uv(i, 0) * verts.row(1) + uv(i, 1) * verts.row(2) + (1.0 - uv(i, 0) - uv(i, 1)) * verts.row(0);
546 }
547
554 void global_to_local(const Eigen::MatrixXd &verts, const Eigen::MatrixXd &pts, Eigen::MatrixXd &uv)
555 {
556 const int dim = verts.cols();
557 const int N = pts.rows();
558 assert(dim == 2);
559 assert(verts.rows() == dim + 1);
560 assert(pts.cols() == dim);
561
562 Eigen::Matrix2d J;
563 for (int i = 0; i < dim; i++)
564 J.col(i) = verts.row(i + 1) - verts.row(0);
565
566 double detJ = J(0, 0) * J(1, 1) - J(0, 1) * J(1, 0);
567 J /= detJ;
568
569 uv.setZero(N, dim);
570 maybe_parallel_for(N, [&](int start, int end, int thread_id) {
571 for (int i = start; i < end; i++)
572 {
573 auto point = pts.row(i) - verts.row(0);
574 uv(i, 0) = J(1, 1) * point(0) - J(0, 1) * point(1);
575 uv(i, 1) = J(0, 0) * point(1) - J(1, 0) * point(0);
576 }
577 });
578 }
579
586 void compute_edge_orders(const NCMesh2D &mesh, const Eigen::VectorXi &elem_orders, Eigen::VectorXi &edge_orders)
587 {
588 const int max_order = elem_orders.maxCoeff();
589 edge_orders.setConstant(mesh.n_edges(), max_order);
590
591 for (int i = 0; i < mesh.n_faces(); i++)
592 for (int j = 0; j < mesh.n_face_vertices(i); j++)
593 edge_orders[mesh.face_edge(i, j)] = std::min(edge_orders[mesh.face_edge(i, j)], elem_orders[i]);
594
595 for (int i = 0; i < mesh.n_edges(); i++)
596 if (mesh.leader_edge_of_edge(i) >= 0)
597 edge_orders[mesh.leader_edge_of_edge(i)] = std::min(edge_orders[mesh.leader_edge_of_edge(i)], edge_orders[i]);
598
599 for (int i = 0; i < mesh.n_edges(); i++)
600 if (mesh.leader_edge_of_edge(i) >= 0)
601 edge_orders[i] = std::min(edge_orders[mesh.leader_edge_of_edge(i)], edge_orders[i]);
602 }
603} // anonymous namespace
604
605Eigen::VectorXi LagrangeBasis2d::tri_edge_local_nodes(const int p, const Mesh2D &mesh, Navigation::Index index)
606{
607 int f = index.face;
608 assert(mesh.is_simplex(f));
609
610 // Local to global mapping of node indices
611 auto l2g = tri_vertices_local_to_global(mesh, f);
612
613 // Extract requested interface
614 Eigen::VectorXi result(2 + (p - 1));
615 result[0] = find_index(l2g.begin(), l2g.end(), index.vertex);
616 result[result.size() - 1] = find_index(l2g.begin(), l2g.end(), mesh.switch_vertex(index).vertex);
617
618 if ((result[0] == 0 && result[result.size() - 1] == 1) || (result[0] == 1 && result[result.size() - 1] == 2) || (result[0] == 2 && result[result.size() - 1] == 0))
619 {
620 for (int i = 0; i < p - 1; ++i)
621 {
622 result[i + 1] = 3 + result[0] * (p - 1) + i;
623 }
624 }
625 else
626 {
627 for (int i = 0; i < p - 1; ++i)
628 {
629 result[i + 1] = 3 + (result[0] + (result[0] == 0 ? 3 : 0)) * (p - 1) - i - 1;
630 }
631 }
632
633 return result;
634}
635
636Eigen::VectorXi LagrangeBasis2d::quad_edge_local_nodes(const int q, const Mesh2D &mesh, Navigation::Index index)
637{
638 int f = index.face;
639 assert(mesh.is_cube(f));
640
641 // Local to global mapping of node indices
642 auto l2g = quad_vertices_local_to_global(mesh, f);
643
644 // Extract requested interface
645 Eigen::VectorXi result(2 + (q - 1));
646 result[0] = find_index(l2g.begin(), l2g.end(), index.vertex);
647 result[result.size() - 1] = find_index(l2g.begin(), l2g.end(), mesh.switch_vertex(index).vertex);
648
649 if ((result[0] == 0 && result[result.size() - 1] == 1) || (result[0] == 1 && result[result.size() - 1] == 2) || (result[0] == 2 && result[result.size() - 1] == 3) || (result[0] == 3 && result[result.size() - 1] == 0))
650 {
651 for (int i = 0; i < q - 1; ++i)
652 {
653 result[i + 1] = 4 + result[0] * (q - 1) + i;
654 }
655 }
656 else
657 {
658 for (int i = 0; i < q - 1; ++i)
659 {
660 result[i + 1] = 4 + (result[0] + (result[0] == 0 ? 4 : 0)) * (q - 1) - i - 1;
661 }
662 }
663
664 return result;
665}
666
667// -----------------------------------------------------------------------------
668
670 const Mesh2D &mesh,
671 const std::string &assembler,
672 const int quadrature_order,
673 const int mass_quadrature_order,
674 const int discr_order,
675 const bool bernstein,
676 const bool serendipity,
677 const bool has_polys,
678 const bool is_geom_bases,
679 const bool use_corner_quadrature,
680 std::vector<ElementBases> &bases,
681 std::vector<LocalBoundary> &local_boundary,
682 std::map<int, InterfaceData> &poly_edge_to_data,
683 std::shared_ptr<MeshNodes> &mesh_nodes)
684{
685
686 Eigen::VectorXi discr_orders(mesh.n_faces());
687 discr_orders.setConstant(discr_order);
688
689 return build_bases(mesh, assembler, quadrature_order, mass_quadrature_order, discr_orders, bernstein, serendipity, has_polys, is_geom_bases, use_corner_quadrature, bases, local_boundary, poly_edge_to_data, mesh_nodes);
690}
691
693 const Mesh2D &mesh,
694 const std::string &assembler,
695 const int quadrature_order,
696 const int mass_quadrature_order,
697 const Eigen::VectorXi &discr_orders,
698 const bool bernstein,
699 const bool serendipity,
700 const bool has_polys,
701 const bool is_geom_bases,
702 const bool use_corner_quadrature,
703 std::vector<ElementBases> &bases,
704 std::vector<LocalBoundary> &local_boundary,
705 std::map<int, InterfaceData> &poly_edge_to_data,
706 std::shared_ptr<MeshNodes> &mesh_nodes)
707{
708 assert(!mesh.is_volume());
709 assert(discr_orders.size() == mesh.n_faces());
710
711 const int max_p = discr_orders.maxCoeff();
712 const int nn = max_p > 1 ? (max_p - 1) : 0;
713 const int n_face_nodes = std::max(nn * nn, max_p == 1 ? 1 : 0);
714
715 Eigen::VectorXi edge_orders;
716 if (!mesh.is_conforming())
717 {
718 const auto &ncmesh = dynamic_cast<const NCMesh2D &>(mesh);
719 compute_edge_orders(ncmesh, discr_orders, edge_orders);
720 }
721
722 mesh_nodes = std::make_shared<MeshNodes>(mesh, has_polys, !is_geom_bases, (max_p > 1 ? (max_p - 1) : 0) * (is_geom_bases ? 2 : 1), max_p == 0 ? 1 : n_face_nodes);
723 MeshNodes &nodes = *mesh_nodes;
724 std::vector<std::vector<int>> element_nodes_id, edge_virtual_nodes;
725 compute_nodes(mesh, discr_orders, edge_orders, serendipity, has_polys, is_geom_bases, nodes, edge_virtual_nodes, element_nodes_id, local_boundary, poly_edge_to_data);
726 // boundary_nodes = nodes.boundary_nodes();
727
728 bases.resize(mesh.n_faces());
729 std::vector<int> interface_elements;
730 interface_elements.reserve(mesh.n_faces());
731
732 for (int e = 0; e < mesh.n_faces(); ++e)
733 {
734 ElementBases &b = bases[e];
735 const int discr_order = discr_orders(e);
736 const int n_el_bases = element_nodes_id[e].size();
737 b.bases.resize(n_el_bases);
738
739 bool skip_interface_element = false;
740
741 for (int j = 0; j < n_el_bases; ++j)
742 {
743 // mark interface between elements of different order
744 const int global_index = element_nodes_id[e][j];
745 if (global_index < 0)
746 {
747 skip_interface_element = true;
748 break;
749 }
750 }
751
752 if (skip_interface_element)
753 {
754 interface_elements.push_back(e);
755 }
756
757 if (mesh.is_cube(e))
758 {
759 const int real_order = quadrature_order > 0 ? quadrature_order : AssemblerUtils::quadrature_order(assembler, discr_order, AssemblerUtils::BasisType::CUBE_LAGRANGE, 2);
760 const int real_mass_order = mass_quadrature_order > 0 ? mass_quadrature_order : AssemblerUtils::quadrature_order("Mass", discr_order, AssemblerUtils::BasisType::CUBE_LAGRANGE, 2);
761 b.set_quadrature([real_order](Quadrature &quad) {
762 QuadQuadrature quad_quadrature;
763 quad_quadrature.get_quadrature(real_order, quad);
764 });
765 b.set_mass_quadrature([real_mass_order](Quadrature &quad) {
766 QuadQuadrature quad_quadrature;
767 quad_quadrature.get_quadrature(real_mass_order, quad);
768 });
769 // quad_quadrature.get_quadrature(real_order, b.quadrature);
770
771 b.set_local_node_from_primitive_func([discr_order, e](const int primitive_id, const Mesh &mesh) {
772 const auto &mesh2d = dynamic_cast<const Mesh2D &>(mesh);
773 auto index = mesh2d.get_index_from_face(e);
774
775 for (int le = 0; le < mesh2d.n_face_vertices(e); ++le)
776 {
777 if (index.edge == primitive_id)
778 break;
779 index = mesh2d.next_around_face(index);
780 }
781 assert(index.edge == primitive_id);
782 return quad_edge_local_nodes(discr_order, mesh2d, index);
783 });
784
785 for (int j = 0; j < n_el_bases; ++j)
786 {
787 const int global_index = element_nodes_id[e][j];
788
789 // if(!skip_interface_element)
790 b.bases[j].init(discr_order, global_index, j, nodes.node_position(global_index));
791
792 const int dtmp = serendipity ? -2 : discr_order;
793
794 b.bases[j].set_basis([dtmp, j](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { autogen::q_basis_value_2d(dtmp, j, uv, val); });
795 b.bases[j].set_grad([dtmp, j](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { autogen::q_grad_basis_value_2d(dtmp, j, uv, val); });
796 }
797 }
798 else if (mesh.is_simplex(e))
799 {
800 const int real_order = quadrature_order > 0 ? quadrature_order : AssemblerUtils::quadrature_order(assembler, discr_order, AssemblerUtils::BasisType::SIMPLEX_LAGRANGE, 2);
801 const int real_mass_order = mass_quadrature_order > 0 ? mass_quadrature_order : AssemblerUtils::quadrature_order("Mass", discr_order, AssemblerUtils::BasisType::SIMPLEX_LAGRANGE, 2);
802 b.set_quadrature([real_order, use_corner_quadrature](Quadrature &quad) {
803 TriQuadrature tri_quadrature(use_corner_quadrature);
804 tri_quadrature.get_quadrature(real_order, quad);
805 });
806 b.set_mass_quadrature([real_mass_order, use_corner_quadrature](Quadrature &quad) {
807 TriQuadrature tri_quadrature(use_corner_quadrature);
808 tri_quadrature.get_quadrature(real_mass_order, quad);
809 });
810
811 b.set_local_node_from_primitive_func([discr_order, e](const int primitive_id, const Mesh &mesh) {
812 const auto &mesh2d = dynamic_cast<const Mesh2D &>(mesh);
813 auto index = mesh2d.get_index_from_face(e);
814
815 for (int le = 0; le < mesh2d.n_face_vertices(e); ++le)
816 {
817 if (index.edge == primitive_id)
818 break;
819 index = mesh2d.next_around_face(index);
820 }
821 assert(index.edge == primitive_id);
822 return tri_edge_local_nodes(discr_order, mesh2d, index);
823 });
824
825 const bool rational = is_geom_bases && mesh.is_rational() && !mesh.cell_weights(e).empty();
826
827 for (int j = 0; j < n_el_bases; ++j)
828 {
829 const int global_index = element_nodes_id[e][j];
830
831 if (!skip_interface_element)
832 {
833 b.bases[j].init(discr_order, global_index, j, nodes.node_position(global_index));
834 }
835
836 if (rational)
837 {
838 const auto &w = mesh.cell_weights(e);
839 assert(discr_order == 2);
840 assert(w.size() == 6);
841
842 b.bases[j].set_basis([bernstein, discr_order, j, w](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) {
843 autogen::p_basis_value_2d(bernstein, discr_order, j, uv, val);
844 Eigen::MatrixXd denom = val;
845 denom.setZero();
846 Eigen::MatrixXd tmp;
847
848 for (int k = 0; k < 6; ++k)
849 {
850 autogen::p_basis_value_2d(bernstein, discr_order, k, uv, tmp);
851 denom += w[k] * tmp;
852 }
853
854 val = (w[j] * val.array() / denom.array()).eval();
855 });
856
857 b.bases[j].set_grad([bernstein, discr_order, j, w](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) {
858 Eigen::MatrixXd b;
859 autogen::p_basis_value_2d(bernstein, discr_order, j, uv, b);
860 autogen::p_grad_basis_value_2d(bernstein, discr_order, j, uv, val);
861 Eigen::MatrixXd denom = b;
862 denom.setZero();
863 Eigen::MatrixXd denom_prime = val;
864 denom_prime.setZero();
865 Eigen::MatrixXd tmp;
866
867 for (int k = 0; k < 6; ++k)
868 {
869 autogen::p_basis_value_2d(bernstein, discr_order, k, uv, tmp);
870 denom += w[k] * tmp;
871
872 autogen::p_grad_basis_value_2d(bernstein, discr_order, k, uv, tmp);
873 denom_prime += w[k] * tmp;
874 }
875
876 val.col(0) = ((w[j] * val.col(0).array() * denom.array() - w[j] * b.array() * denom_prime.col(0).array()) / (denom.array() * denom.array())).eval();
877 val.col(1) = ((w[j] * val.col(1).array() * denom.array() - w[j] * b.array() * denom_prime.col(1).array()) / (denom.array() * denom.array())).eval();
878 });
879 }
880 else
881 {
882 // pick out basis functions using autogenerated code
883 b.bases[j].set_basis([bernstein, discr_order, j](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { autogen::p_basis_value_2d(bernstein, discr_order, j, uv, val); });
884 b.bases[j].set_grad([bernstein, discr_order, j](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { autogen::p_grad_basis_value_2d(bernstein, discr_order, j, uv, val); });
885 }
886 }
887 }
888 else
889 {
890 // Polygon bases are built later on
891 }
892
893#ifndef NDEBUG
894 if (mesh.is_conforming())
895 {
896 Eigen::MatrixXd uv(4, 2);
897 uv << 0.1, 0.1, 0.3, 0.3, 0.9, 0.01, 0.01, 0.9;
898 Eigen::MatrixXd dx(4, 1);
899 dx.setConstant(1e-6);
900 Eigen::MatrixXd uvdx = uv;
901 uvdx.col(0) += dx;
902 Eigen::MatrixXd uvdy = uv;
903 uvdy.col(1) += dx;
904 Eigen::MatrixXd grad, val, vdx, vdy;
905
906 for (int j = 0; j < n_el_bases; ++j)
907 {
908 b.bases[j].eval_grad(uv, grad);
909
910 b.bases[j].eval_basis(uv, val);
911 b.bases[j].eval_basis(uvdx, vdx);
912 b.bases[j].eval_basis(uvdy, vdy);
913
914 assert((grad.col(0) - (vdx - val) / 1e-6).norm() < 1e-4);
915 assert((grad.col(1) - (vdy - val) / 1e-6).norm() < 1e-4);
916 }
917 }
918#endif
919 }
920
921 if (!is_geom_bases)
922 {
923 if (!mesh.is_conforming())
924 {
925 const auto &ncmesh = dynamic_cast<const NCMesh2D &>(mesh);
926
927 std::vector<std::vector<int>> elementOrder;
928 {
929 const int max_order = discr_orders.maxCoeff(), min_order = discr_orders.minCoeff();
930 int max_level = 0;
931 for (int e = 0; e < ncmesh.n_faces(); e++)
932 if (max_level < ncmesh.face_ref_level(e))
933 max_level = ncmesh.face_ref_level(e);
934
935 elementOrder.resize((max_level + 1) * (max_order - min_order + 1));
936 int N = 0;
937 int cur_level = 0;
938 while (cur_level <= max_level)
939 {
940 int order = min_order;
941 while (order <= max_order)
942 {
943 int cur_bucket = (max_order - min_order + 1) * cur_level + (order - min_order);
944 for (int i = 0; i < ncmesh.n_faces(); i++)
945 {
946 if (ncmesh.face_ref_level(i) != cur_level || discr_orders[i] != order)
947 continue;
948
949 N++;
950 elementOrder[cur_bucket].push_back(i);
951 }
952 order++;
953 }
954 cur_level++;
955 }
956 }
957
958 for (const auto &bucket : elementOrder)
959 {
960 if (bucket.size() == 0)
961 continue;
962 for (const int e : bucket)
963 {
964 ElementBases &b = bases[e];
965 const int discr_order = discr_orders(e);
966 const int n_el_bases = element_nodes_id[e].size();
967
968 for (int j = 0; j < n_el_bases; ++j)
969 {
970 const int global_index = element_nodes_id[e][j];
971
972 if (global_index >= 0)
973 b.bases[j].init(discr_order, global_index, j, nodes.node_position(global_index));
974 else
975 {
976 int large_edge = -1, local_edge = -1;
977 int opposite_element = -1;
978 bool is_on_leader_edge = false;
979
980 if (j < 3 + 3 * (discr_order - 1) && j >= 3)
981 {
982 local_edge = (j - 3) / (discr_order - 1);
983 large_edge = ncmesh.face_edge(e, local_edge);
984 if (ncmesh.n_follower_edges(large_edge) > 0)
985 is_on_leader_edge = true;
986 }
987
988 // this node is on a leader edge, but its order is constrained
989 if (is_on_leader_edge)
990 {
991 const int edge_order = edge_orders[large_edge];
992
993 // indices of fake nodes on this edge in the opposite element
994 Eigen::VectorXi indices;
995 indices.resize(discr_order + 1);
996 for (int i = 0; i < discr_order - 1; i++)
997 {
998 indices[i + 1] = 3 + local_edge * (discr_order - 1) + i;
999 }
1000 int index = indices[(j - 3) % (discr_order - 1) + 1];
1001
1002 // the position of node j in the opposite element
1003 Eigen::MatrixXd lnodes;
1004 autogen::p_nodes_2d(discr_order, lnodes);
1005 Eigen::Vector2d node_position = lnodes.row(index);
1006
1007 std::function<double(const int, const int, const double)> basis_1d = [](const int order, const int id, const double x) -> double {
1008 assert(id <= order && id >= 0);
1009 double y = 1;
1010 for (int o = 0; o <= order; o++)
1011 {
1012 if (o != id)
1013 y *= (x * order - o) / (id - o);
1014 }
1015 return y;
1016 };
1017
1018 // apply basis projection
1019 double x = NCMesh2D::element_weight_to_edge_weight(local_edge, node_position);
1020 for (int i = 0; i < edge_virtual_nodes[large_edge].size(); i++)
1021 {
1022 const int global_index = edge_virtual_nodes[large_edge][i];
1023 const double weight = basis_1d(edge_order, i + 1, x);
1024 if (std::abs(weight) < 1e-12)
1025 continue;
1026 b.bases[j].global().emplace_back(global_index, nodes.node_position(global_index), weight);
1027 }
1028
1029 const auto &global_1 = b.bases[local_edge].global();
1030 const auto &global_2 = b.bases[(local_edge + 1) % 3].global();
1031 double weight = basis_1d(edge_order, 0, x);
1032 if (std::abs(weight) > 1e-12)
1033 for (size_t ii = 0; ii < global_1.size(); ++ii)
1034 b.bases[j].global().emplace_back(global_1[ii].index, global_1[ii].node, weight * global_1[ii].val);
1035 weight = basis_1d(edge_order, edge_order, x);
1036 if (std::abs(weight) > 1e-12)
1037 for (size_t ii = 0; ii < global_2.size(); ++ii)
1038 b.bases[j].global().emplace_back(global_2[ii].index, global_2[ii].node, weight * global_2[ii].val);
1039 }
1040 else
1041 {
1042 Eigen::MatrixXd global_position;
1043 // this node is a hanging vertex, and it's not on a follower edge
1044 if (j < 3)
1045 {
1046 const int v_id = ncmesh.face_vertex(e, j);
1047 large_edge = ncmesh.leader_edge_of_vertex(v_id);
1048 opposite_element = ncmesh.face_neighbor(large_edge);
1049 global_position = ncmesh.point(v_id);
1050 }
1051 // this node is on a follower edge
1052 else
1053 {
1054 // find the local id of small edge
1055 int small_edge = ncmesh.face_edge(e, local_edge);
1056 large_edge = ncmesh.leader_edge_of_edge(small_edge);
1057
1058 // this edge is non-conforming
1059 if (ncmesh.n_face_neighbors(small_edge) == 1)
1060 opposite_element = ncmesh.face_neighbor(large_edge);
1061 // this edge is conforming, but the order on two sides is different
1062 else
1063 {
1065 idx.edge = small_edge;
1066 idx.vertex = ncmesh.edge_vertex(small_edge, 0);
1067 idx.face_corner = -1;
1068 idx.face = e;
1069 opposite_element = ncmesh.switch_face(idx).face;
1070 }
1071
1072 // the position of node j in the opposite element
1073 Eigen::MatrixXd lnodes;
1074 autogen::p_nodes_2d(discr_order, lnodes);
1075 global_position = lnodes.row(j);
1076
1077 Eigen::MatrixXd verts(ncmesh.n_face_vertices(e), 2);
1078 for (int i = 0; i < verts.rows(); i++)
1079 verts.row(i) = ncmesh.point(ncmesh.face_vertex(e, i));
1080
1081 Eigen::MatrixXd local_position = lnodes.row(j);
1082 local_to_global(verts, local_position, global_position);
1083 }
1084
1085 Eigen::MatrixXd verts(ncmesh.n_face_vertices(e), 2);
1086 for (int i = 0; i < verts.rows(); i++)
1087 verts.row(i) = ncmesh.point(ncmesh.face_vertex(opposite_element, i));
1088
1089 Eigen::MatrixXd node_position;
1090 global_to_local(verts, global_position, node_position);
1091
1092 // evaluate the basis of the opposite element at this node
1093 const auto &other_bases = bases[opposite_element];
1094 std::vector<AssemblyValues> w;
1095 other_bases.evaluate_bases(node_position, w);
1096
1097 // apply basis projection
1098 for (long i = 0; i < w.size(); ++i)
1099 {
1100 assert(w[i].val.size() == 1);
1101 if (std::abs(w[i].val(0)) < 1e-12)
1102 continue;
1103
1104 for (size_t ii = 0; ii < other_bases.bases[i].global().size(); ++ii)
1105 {
1106 const auto &other_global = other_bases.bases[i].global()[ii];
1107 b.bases[j].global().emplace_back(other_global.index, other_global.node, w[i].val(0) * other_global.val);
1108 }
1109 }
1110 }
1111
1112 auto &global_ = b.bases[j].global();
1113 if (global_.size() <= 1)
1114 continue;
1115
1116 std::map<int, Local2Global> list;
1117 for (size_t ii = 0; ii < global_.size(); ii++)
1118 {
1119 auto pair = list.insert({global_[ii].index, global_[ii]});
1120 if (!pair.second && pair.first != list.end())
1121 {
1122 assert((pair.first->second.node - global_[ii].node).norm() < 1e-12);
1123 pair.first->second.val += global_[ii].val;
1124 }
1125 }
1126
1127 global_.clear();
1128 for (auto it = list.begin(); it != list.end(); ++it)
1129 {
1130 if (std::abs(it->second.val) > 1e-12)
1131 global_.push_back(it->second);
1132 }
1133 }
1134 }
1135 }
1136 }
1137 }
1138 else
1139 {
1140 // loop over all potential element orders (since multiple loops may be needed to constrain interfaces between more than two elements)
1141 for (int pp = 2; pp <= autogen::MAX_P_BASES; ++pp)
1142 {
1143 // loop again over interface elements to address mismatched polynomial orders by constraining the higher order nodes
1144 for (int e : interface_elements)
1145 {
1146 ElementBases &b = bases[e];
1147 const int discr_order = discr_orders(e);
1148 const int n_el_bases = element_nodes_id[e].size();
1149
1150 assert(discr_order > 1);
1151 if (discr_order != pp)
1152 continue;
1153
1154 if (mesh.is_cube(e))
1155 {
1156 // TODO
1157 assert(false);
1158 }
1159 else if (mesh.is_simplex(e))
1160 {
1161 for (int j = 0; j < n_el_bases; ++j)
1162 {
1163 const int global_index = element_nodes_id[e][j];
1164
1165 if (global_index >= 0)
1166 b.bases[j].init(discr_order, global_index, j, nodes.node_position(global_index));
1167 else
1168 {
1169 const auto le = -(global_index + 1);
1170
1171 auto v = tri_vertices_local_to_global(mesh, e);
1172 Eigen::Matrix<int, 3, 2> ev;
1173 ev.row(0) << v[0], v[1];
1174 ev.row(1) << v[1], v[2];
1175 ev.row(2) << v[2], v[0];
1176
1177 const auto index = mesh.switch_face(find_edge(mesh, e, ev(le, 0), ev(le, 1)));
1178 const auto other_face = index.face;
1179
1180 Eigen::RowVector2d node_position;
1181 assert(discr_order > 1);
1182
1183 auto indices = tri_edge_local_nodes(discr_order, mesh, index);
1184 Eigen::MatrixXd lnodes;
1185 autogen::p_nodes_2d(discr_order, lnodes);
1186
1187 if (j < 3)
1188 node_position = lnodes.row(indices(0));
1189 else if (j < 3 + 3 * (discr_order - 1))
1190 node_position = lnodes.row(indices(((j - 3) % (discr_order - 1)) + 1));
1191 else
1192 assert(false);
1193
1194 const auto &other_bases = bases[other_face];
1195 // Eigen::MatrixXd w;
1196 std::vector<AssemblyValues> w;
1197 other_bases.evaluate_bases(node_position, w);
1198
1199 for (long i = 0; i < w.size(); ++i)
1200 {
1201 assert(w[i].val.size() == 1);
1202 if (std::abs(w[i].val(0)) < 1e-8)
1203 continue;
1204
1205 for (size_t ii = 0; ii < other_bases.bases[i].global().size(); ++ii)
1206 {
1207 const auto &other_global = other_bases.bases[i].global()[ii];
1208 // logger().trace("e {} j {} gid {}", e, j, other_global.index);
1209 b.bases[j].global().emplace_back(other_global.index, other_global.node, w[i].val(0) * other_global.val);
1210 }
1211 }
1212 }
1213 }
1214 }
1215 else
1216 {
1217 // Polygon bases are built later on
1218 }
1219 }
1220 }
1221 }
1222 }
1223
1224 return nodes.n_nodes();
1225}
double val
Eigen::RowVectorXd point
double J
int y
int edge_id
int x
static int quadrature_order(const std::string &assembler, const int basis_degree, const BasisType &b_type, const int dim)
utility for retrieving the needed quadrature order to precisely integrate the given form on the given...
Stores the basis functions for a given element in a mesh (facet in 2d, cell in 3d).
static Eigen::VectorXi tri_edge_local_nodes(const int p, const mesh::Mesh2D &mesh, mesh::Navigation::Index index)
static Eigen::VectorXi quad_edge_local_nodes(const int q, const mesh::Mesh2D &mesh, mesh::Navigation::Index index)
static int build_bases(const mesh::Mesh2D &mesh, const std::string &assembler, const int quadrature_order, const int mass_quadrature_order, const int discr_order, const bool bernstein, const bool serendipity, const bool has_polys, const bool is_geom_bases, const bool use_corner_quadrature, std::vector< ElementBases > &bases, std::vector< mesh::LocalBoundary > &local_boundary, std::map< int, InterfaceData > &poly_edge_to_data, std::shared_ptr< mesh::MeshNodes > &mesh_nodes)
Builds FE basis functions over the entire mesh (P1, P2 over triangles, Q1, Q2 over quads).
Boundary primitive IDs for a single element.
2D mesh interface: the elements are the faces (polygons) and the boundary elements are the edges.
Definition Mesh2D.hpp:18
Navigation::Index next_around_face(Navigation::Index idx) const
Definition Mesh2D.hpp:63
bool is_volume() const override
checks if mesh is volume
Definition Mesh2D.hpp:27
virtual Navigation::Index switch_vertex(Navigation::Index idx) const =0
virtual Navigation::Index get_index_from_face(int f, int lv=0) const =0
virtual Navigation::Index switch_face(Navigation::Index idx) const =0
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
bool is_polytope(const int el_id) const
checks if element is polygon compatible
Definition Mesh.cpp:374
bool is_rational() const
check if curved mesh has rational polynomials elements
Definition Mesh.hpp:305
virtual bool is_conforming() const =0
if the mesh is conforming
bool is_cube(const int el_id) const
checks if element is cube compatible
Definition Mesh.cpp:361
virtual int get_boundary_id(const int primitive) const
Get the boundary selection of an element (face in 3d, edge in 2d)
Definition Mesh.hpp:499
bool is_simplex(const int el_id) const
checks if element is simplex
Definition Mesh.cpp:392
const std::vector< double > & cell_weights(const int cell_index) const
weights for rational polynomial meshes
Definition Mesh.hpp:586
virtual bool is_boundary_edge(const int edge_global_id) const =0
is edge boundary
virtual int n_faces() const =0
number of faces
virtual int n_face_vertices(const int f_id) const =0
number of vertices of a face
virtual int face_vertex(const int f_id, const int lv_id) const =0
id of the face vertex
Non-conforming 2D triangle mesh with hanging nodes, for adaptive h-refinement: refine_element() split...
Definition NCMesh2D.hpp:33
int face_edge(const int f_id, const int le_id) const
Definition NCMesh2D.hpp:221
static double element_weight_to_edge_weight(const int l, const Eigen::Vector2d &pos)
Definition NCMesh2D.cpp:507
int n_faces() const override
number of faces
Definition NCMesh2D.hpp:188
int leader_edge_of_edge(const int e_id) const
Definition NCMesh2D.hpp:228
int n_face_vertices(const int f_id) const override
number of vertices of a face
Definition NCMesh2D.hpp:213
int n_edges() const override
number of edges
Definition NCMesh2D.hpp:191
void get_quadrature(const int order, Quadrature &quad)
enable_if_t< t==0 > sort(Eigen::Matrix< T, 3, 3 > &U, Eigen::Matrix< T, 3, 1 > &sigma, Eigen::Matrix< T, 3, 3 > &V)
Helper function of 3X3 SVD for sorting singular values.
list tmp
Definition p_bases.py:366
str nodes
Definition p_bases.py:399
list indices
Definition p_bases.py:259
Used for test only.
void p_grad_basis_value_2d(const bool bernstein, const int p, const int local_index, const Eigen::MatrixXd &uv, Eigen::MatrixXd &val)
void q_grad_basis_value_2d(const int q, const int local_index, const Eigen::MatrixXd &uv, Eigen::MatrixXd &val)
void p_nodes_2d(const int p, Eigen::MatrixXd &val)
void q_basis_value_2d(const int q, const int local_index, const Eigen::MatrixXd &uv, Eigen::MatrixXd &val)
void p_basis_value_2d(const bool bernstein, const int p, const int local_index, const Eigen::MatrixXd &uv, Eigen::MatrixXd &val)
void maybe_parallel_for(int size, const std::function< void(int, int, int)> &partial_for)
std::vector< int > local_indices