PolyFEM
Loading...
Searching...
No Matches
SplineBasis2d.cpp
Go to the documentation of this file.
1#include "SplineBasis2d.hpp"
2
3#include "LagrangeBasis2d.hpp"
5
8
10
11#include <polysolve/linear/Solver.hpp>
12
14
15#include <polyfem/Common.hpp>
17
18#include <Eigen/Sparse>
19
20#include <cassert>
21#include <iostream>
22#include <vector>
23#include <array>
24#include <map>
25#include <set>
26
27// TODO carefull with simplices
28
29namespace polyfem
30{
31 using namespace polysolve;
32 using namespace Eigen;
33 using namespace assembler;
34 using namespace mesh;
35 using namespace quadrature;
36
37 namespace basis
38 {
39 namespace
40 {
41 typedef Matrix<std::vector<int>, 3, 3> SpaceMatrix;
42 typedef Matrix<RowVectorNd, 3, 3> NodeMatrix;
43
44 void print_local_space(const SpaceMatrix &space)
45 {
46 std::stringstream ss;
47 ss << std::endl;
48 for (int j = 2; j >= 0; --j)
49 {
50 for (int i = 0; i < 3; ++i)
51 {
52 if (space(i, j).size() > 0)
53 {
54 for (std::size_t l = 0; l < space(i, j).size(); ++l)
55 ss << space(i, j)[l] << ",";
56
57 ss << "\t";
58 }
59 else
60 ss << "x\t";
61 }
62 ss << std::endl;
63 }
64
65 logger().trace("Local space:\n{}", ss.str());
66 }
67
68 int node_id_from_edge_index(const Mesh2D &mesh, MeshNodes &mesh_nodes, const Navigation::Index &index)
69 {
70 const int face_id = mesh.switch_face(index).face;
71 if (face_id >= 0 && mesh.is_cube(face_id))
72 return mesh_nodes.node_id_from_face(face_id);
73
74 return mesh_nodes.node_id_from_edge(index.edge);
75 }
76
77 void explore_direction(const Navigation::Index &index, const Mesh2D &mesh, MeshNodes &mesh_nodes, const int x, const int y, const bool is_x, const bool invert, LocalBoundary &lb, SpaceMatrix &space, NodeMatrix &node, std::map<int, InterfaceData> &poly_edge_to_data)
78 {
79 int node_id = node_id_from_edge_index(mesh, mesh_nodes, index);
80 // bool real_boundary = mesh.node_id_from_edge_index(index, node_id);
81
82 assert(std::find(space(x, y).begin(), space(x, y).end(), node_id) == space(x, y).end());
83 space(x, y).push_back(node_id);
84 node(x, y) = mesh_nodes.node_position(node_id);
85 assert(node(x, y).size() == 2);
86
87 const int x1 = is_x ? x : (invert ? 2 : 0);
88 const int y1 = !is_x ? y : (invert ? 2 : 0);
89
90 const int x2 = is_x ? x : (invert ? 0 : 2);
91 const int y2 = !is_x ? y : (invert ? 0 : 2);
92
93 const bool is_boundary = mesh_nodes.is_boundary(node_id);
94 const bool is_interface = mesh_nodes.is_interface(node_id);
95
96 if (is_boundary)
97 {
98 lb.add_boundary_primitive(index.edge, LagrangeBasis2d::quad_edge_local_nodes(2, mesh, index)[1] - 4);
99 // lb.add_boundary_primitive(index.edge, LagrangeBasis2d::quadr_quad_edge_local_nodes(mesh, index)[1]-4);
100 // bounday_nodes.push_back(node_id);
101 }
102 else if (is_interface)
103 {
104 InterfaceData &data = poly_edge_to_data[index.edge];
105 data.local_indices.push_back(y * 3 + x);
106 }
107 else
108 {
109 assert(!is_boundary && !is_interface);
110
111 Navigation::Index start_index = mesh.switch_face(index);
112 assert(start_index.vertex == index.vertex);
113 assert(start_index.face >= 0);
114
115 Navigation::Index edge1 = mesh.switch_edge(start_index);
116 node_id = node_id_from_edge_index(mesh, mesh_nodes, edge1);
117 // if(mesh_nodes.is_boundary(node_id))
118 // bounday_nodes.push_back(node_id);
119
120 if (std::find(space(x1, y1).begin(), space(x1, y1).end(), node_id) == space(x1, y1).end())
121 {
122 space(x1, y1).push_back(node_id);
123 node(x1, y1) = mesh_nodes.node_position(node_id);
124 }
125
126 Navigation::Index edge2 = mesh.switch_edge(mesh.switch_vertex(start_index));
127 node_id = node_id_from_edge_index(mesh, mesh_nodes, edge2);
128 // if(mesh_nodes.is_boundary(node_id))
129 // bounday_nodes.push_back(node_id);
130 if (std::find(space(x2, y2).begin(), space(x2, y2).end(), node_id) == space(x2, y2).end())
131 {
132 space(x2, y2).push_back(node_id);
133 node(x2, y2) = mesh_nodes.node_position(node_id);
134 // node(x2, y2).push_back(mesh.node_from_edge_index(edge2));
135 }
136 }
137 }
138
139 void add_id_for_poly(const Navigation::Index &index, const int x1, const int y1, const int x2, const int y2, const SpaceMatrix &space, std::map<int, InterfaceData> &poly_edge_to_data)
140 {
141 auto it = poly_edge_to_data.find(index.edge);
142 if (it != poly_edge_to_data.end())
143 {
144 InterfaceData &data = it->second;
145
146 assert(space(x1, y1).size() == 1);
147 data.local_indices.push_back(y1 * 3 + x1);
148
149 assert(space(x2, y2).size() == 1);
150 data.local_indices.push_back(y2 * 3 + x2);
151 }
152 }
153
154 void build_local_space(const Mesh2D &mesh, MeshNodes &mesh_nodes, const int el_index, SpaceMatrix &space, NodeMatrix &node, std::vector<LocalBoundary> &local_boundary, std::map<int, InterfaceData> &poly_edge_to_data)
155 {
156 assert(!mesh.is_volume());
157
158 Navigation::Index index;
159 // space.setConstant(-1);
160
161 const int el_node_id = mesh_nodes.node_id_from_face(el_index);
162 space(1, 1).push_back(el_node_id);
163 node(1, 1) = mesh_nodes.node_position(el_node_id);
164 // (mesh.node_from_face(el_index));
165
166 LocalBoundary lb(el_index, BoundaryType::QUAD_LINE);
167
169 index = mesh.get_index_from_face(el_index);
170 explore_direction(index, mesh, mesh_nodes, 1, 0, false, false, lb, space, node, poly_edge_to_data);
171
173 index = mesh.next_around_face(index);
174 explore_direction(index, mesh, mesh_nodes, 2, 1, true, false, lb, space, node, poly_edge_to_data);
175
177 index = mesh.next_around_face(index);
178 explore_direction(index, mesh, mesh_nodes, 1, 2, false, true, lb, space, node, poly_edge_to_data);
179
181 index = mesh.next_around_face(index);
182 explore_direction(index, mesh, mesh_nodes, 0, 1, true, true, lb, space, node, poly_edge_to_data);
183
185 if (mesh_nodes.is_boundary_or_interface(space(1, 2).front()) && mesh_nodes.is_boundary_or_interface(space(2, 1).front()))
186 {
187 assert(space(2, 2).empty());
188
189 Navigation::Index start_index = mesh.get_index_from_face(el_index);
190 start_index = mesh.next_around_face(start_index);
191 start_index = mesh.next_around_face(start_index);
192
193 const int node_id = mesh_nodes.node_id_from_vertex(start_index.vertex);
194 // mesh.vertex_node_id(start_index.vertex);
195 space(2, 2).push_back(node_id);
196 node(2, 2) = mesh_nodes.node_position(node_id);
197 // node(2,2).push_back(mesh.node_from_vertex(start_index.vertex));
198
199 // bounday_nodes.push_back(node_id);
200 }
201
202 if (mesh_nodes.is_boundary_or_interface(space(1, 0).front()) && mesh_nodes.is_boundary_or_interface(space(2, 1).front()))
203 {
204 assert(space(2, 0).empty());
205
206 Navigation::Index start_index = mesh.get_index_from_face(el_index);
207 start_index = mesh.next_around_face(start_index);
208
209 const int node_id = mesh_nodes.node_id_from_vertex(start_index.vertex);
210 // mesh.vertex_node_id(start_index.vertex);
211 space(2, 0).push_back(node_id);
212 node(2, 0) = mesh_nodes.node_position(node_id);
213 // .push_back(mesh.node_from_vertex(start_index.vertex));
214
215 // bounday_nodes.push_back(node_id);
216 }
217
218 if (mesh_nodes.is_boundary_or_interface(space(1, 2).front()) && mesh_nodes.is_boundary_or_interface(space(0, 1).front()))
219 {
220 assert(space(0, 2).empty());
221
222 Navigation::Index start_index = mesh.get_index_from_face(el_index);
223 start_index = mesh.next_around_face(start_index);
224 start_index = mesh.next_around_face(start_index);
225 start_index = mesh.next_around_face(start_index);
226
227 // const int node_id = mesh.vertex_node_id(start_index.vertex);
228 const int node_id = mesh_nodes.node_id_from_vertex(start_index.vertex);
229 space(0, 2).push_back(node_id);
230 node(0, 2) = mesh_nodes.node_position(node_id);
231 // .push_back(mesh.node_from_vertex(start_index.vertex));
232
233 // bounday_nodes.push_back(node_id);
234 }
235
236 if (mesh_nodes.is_boundary_or_interface(space(1, 0).front()) && mesh_nodes.is_boundary_or_interface(space(0, 1).front()))
237 {
238 Navigation::Index start_index = mesh.get_index_from_face(el_index);
239
240 // const int node_id = mesh.vertex_node_id(start_index.vertex);
241 const int node_id = mesh_nodes.node_id_from_vertex(start_index.vertex);
242 space(0, 0).push_back(node_id);
243 node(0, 0) = mesh_nodes.node_position(node_id);
244 //.push_back(mesh.node_from_vertex(start_index.vertex));
245
246 // bounday_nodes.push_back(node_id);
247 }
248
249 // print_local_space(space);
250
252 index = mesh.get_index_from_face(el_index);
253 add_id_for_poly(index, 0, 0, 2, 0, space, poly_edge_to_data);
254
255 index = mesh.next_around_face(index);
256 add_id_for_poly(index, 2, 0, 2, 2, space, poly_edge_to_data);
257
258 index = mesh.next_around_face(index);
259 add_id_for_poly(index, 2, 2, 0, 2, space, poly_edge_to_data);
260
261 index = mesh.next_around_face(index);
262 add_id_for_poly(index, 0, 2, 0, 0, space, poly_edge_to_data);
263
264 if (!lb.empty())
265 local_boundary.emplace_back(lb);
266 }
267
268 void setup_knots_vectors(MeshNodes &mesh_nodes, const SpaceMatrix &space, std::array<std::array<double, 4>, 3> &h_knots, std::array<std::array<double, 4>, 3> &v_knots)
269 {
270 // left and right neigh are absent
271 if (mesh_nodes.is_boundary_or_interface(space(0, 1).front()) && mesh_nodes.is_boundary_or_interface(space(2, 1).front()))
272 {
273 h_knots[0] = {{0, 0, 0, 1}};
274 h_knots[1] = {{0, 0, 1, 1}};
275 h_knots[2] = {{0, 1, 1, 1}};
276 }
277 // left neigh is absent
278 else if (mesh_nodes.is_boundary_or_interface(space(0, 1).front()))
279 {
280 h_knots[0] = {{0, 0, 0, 1}};
281 h_knots[1] = {{0, 0, 1, 2}};
282 h_knots[2] = {{0, 1, 2, 3}};
283 }
284 // right neigh is absent
285 else if (mesh_nodes.is_boundary_or_interface(space(2, 1).front()))
286 {
287 h_knots[0] = {{-2, -1, 0, 1}};
288 h_knots[1] = {{-1, 0, 1, 1}};
289 h_knots[2] = {{0, 1, 1, 1}};
290 }
291 else
292 {
293 h_knots[0] = {{-2, -1, 0, 1}};
294 h_knots[1] = {{-1, 0, 1, 2}};
295 h_knots[2] = {{0, 1, 2, 3}};
296 }
297
298 // top and bottom neigh are absent
299 if (mesh_nodes.is_boundary_or_interface(space(1, 0).front()) && mesh_nodes.is_boundary_or_interface(space(1, 2).front()))
300 {
301 v_knots[0] = {{0, 0, 0, 1}};
302 v_knots[1] = {{0, 0, 1, 1}};
303 v_knots[2] = {{0, 1, 1, 1}};
304 }
305 // bottom neigh is absent
306 else if (mesh_nodes.is_boundary_or_interface(space(1, 0).front()))
307 {
308 v_knots[0] = {{0, 0, 0, 1}};
309 v_knots[1] = {{0, 0, 1, 2}};
310 v_knots[2] = {{0, 1, 2, 3}};
311 }
312 // top neigh is absent
313 else if (mesh_nodes.is_boundary_or_interface(space(1, 2).front()))
314 {
315 v_knots[0] = {{-2, -1, 0, 1}};
316 v_knots[1] = {{-1, 0, 1, 1}};
317 v_knots[2] = {{0, 1, 1, 1}};
318 }
319 else
320 {
321 v_knots[0] = {{-2, -1, 0, 1}};
322 v_knots[1] = {{-1, 0, 1, 2}};
323 v_knots[2] = {{0, 1, 2, 3}};
324 }
325 }
326
327 void basis_for_regular_quad(const SpaceMatrix &space, const NodeMatrix &loc_nodes, const std::array<std::array<double, 4>, 3> &h_knots, const std::array<std::array<double, 4>, 3> &v_knots, ElementBases &b)
328 {
329 for (int y = 0; y < 3; ++y)
330 {
331 for (int x = 0; x < 3; ++x)
332 {
333 if (space(x, y).size() == 1)
334 {
335 const int global_index = space(x, y).front();
336 const Eigen::MatrixXd &node = loc_nodes(x, y);
337 assert(node.size() == 2);
338
339 const int local_index = y * 3 + x;
340 b.bases[local_index].init(2, global_index, local_index, node);
341
342 const QuadraticBSpline2d spline(h_knots[x], v_knots[y]);
343 b.bases[local_index].set_basis([spline](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { spline.interpolate(uv, val); });
344 b.bases[local_index].set_grad([spline](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { spline.derivative(uv, val); });
345 }
346 }
347 }
348 }
349
350 void basis_for_irregulard_quad(const int el_id, const Mesh2D &mesh, MeshNodes &mesh_nodes, const SpaceMatrix &space, const NodeMatrix &loc_nodes, const std::array<std::array<double, 4>, 3> &h_knots, const std::array<std::array<double, 4>, 3> &v_knots, ElementBases &b)
351 {
352 for (int y = 0; y < 3; ++y)
353 {
354 for (int x = 0; x < 3; ++x)
355 {
356 if (space(x, y).size() > 1)
357 {
358 const int mpx = 1;
359 const int mpy = y;
360
361 const int mmx = x;
362 const int mmy = 1;
363
364 std::vector<int> other_indices;
365 const auto &center = b.bases[1 * 3 + 1].global().front();
366
367 const auto &el1 = b.bases[mpy * 3 + mpx].global().front();
368 const auto &el2 = b.bases[mmy * 3 + mmx].global().front();
369
370 Navigation::Index start_index = mesh.get_index_from_face(el_id);
371 bool found = false;
372 for (int i = 0; i < 4; ++i)
373 {
374 other_indices.clear();
375 int n_neighs = 0;
376 Navigation::Index index = start_index;
377 do
378 {
379 const int f_index = mesh_nodes.node_id_from_face(index.face);
380 if (f_index != el1.index && f_index != el2.index && f_index != center.index)
381 other_indices.push_back(f_index);
382
383 ++n_neighs;
384 index = mesh.next_around_vertex(index);
385 } while (index.face != start_index.face);
386 if (n_neighs != 4)
387 {
388 found = true;
389 break;
390 }
391
392 start_index = mesh.next_around_face(start_index);
393 }
394 assert(found);
395
396 const int local_index = y * 3 + x;
397 auto &base = b.bases[local_index];
398
399 const int k = int(other_indices.size()) + 3;
400
401 base.global().resize(k);
402
403 base.global()[0].index = center.index;
404 base.global()[0].val = (4. - k) / k;
405 base.global()[0].node = center.node;
406
407 base.global()[1].index = el1.index;
408 base.global()[1].val = (4. - k) / k;
409 base.global()[1].node = el1.node;
410
411 base.global()[2].index = el2.index;
412 base.global()[2].val = (4. - k) / k;
413 base.global()[2].node = el2.node;
414
415 for (std::size_t n = 0; n < other_indices.size(); ++n)
416 {
417 base.global()[3 + n].index = other_indices[n];
418 base.global()[3 + n].val = 4. / k;
419 base.global()[3 + n].node = mesh_nodes.node_position(other_indices[n]);
420 }
421
422 const QuadraticBSpline2d spline(h_knots[x], v_knots[y]);
423 b.bases[local_index].set_basis([spline](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { spline.interpolate(uv, val); });
424 b.bases[local_index].set_grad([spline](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { spline.derivative(uv, val); });
425 }
426 }
427 }
428 }
429
430 void create_q2_nodes(const Mesh2D &mesh, const int el_index, std::set<int> &vertex_id, std::set<int> &edge_id, ElementBases &b, std::vector<LocalBoundary> &local_boundary, int &n_bases)
431 {
432 b.bases.resize(9);
433
434 LocalBoundary lb(el_index, BoundaryType::QUAD_LINE);
435
436 Navigation::Index index = mesh.get_index_from_face(el_index);
437 for (int j = 0; j < 4; ++j)
438 {
439 int current_vertex_node_id = -1;
440 int current_edge_node_id = -1;
441 Eigen::Matrix<double, 1, 2> current_edge_node;
442 Eigen::MatrixXd current_vertex_node;
443
444 // auto e2l = LagrangeBasis2d::quadr_quad_edge_local_nodes(mesh, index);
445 auto e2l = LagrangeBasis2d::quad_edge_local_nodes(2, mesh, index);
446
447 int vertex_basis_id = e2l[0];
448 int edge_basis_id = e2l[1];
449
450 const int opposite_face = mesh.switch_face(index).face;
451
452 // if the edge/vertex is boundary the it is a Q2 edge
453 bool is_vertex_q2 = true;
454 bool is_vertex_boundary = false;
455
456 Navigation::Index vindex = index;
457
458 do
459 {
460 if (vindex.face < 0)
461 {
462 is_vertex_boundary = true;
463 break;
464 }
465 if (mesh.is_spline_compatible(vindex.face))
466 {
467 is_vertex_q2 = false;
468 break;
469 }
470 vindex = mesh.next_around_vertex(vindex);
471 } while (vindex.edge != index.edge);
472
473 if (is_vertex_q2)
474 {
475 vindex = mesh.switch_face(index);
476 do
477 {
478 if (vindex.face < 0)
479 {
480 is_vertex_boundary = true;
481 break;
482 }
483
484 if (mesh.is_spline_compatible(vindex.face))
485 {
486 is_vertex_q2 = false;
487 break;
488 }
489 vindex = mesh.next_around_vertex(vindex);
490 } while (vindex.edge != index.edge);
491 }
492
493 const bool is_edge_q2 = opposite_face < 0 || !mesh.is_spline_compatible(opposite_face);
494
495 if (is_edge_q2)
496 {
497 const bool is_new_edge = edge_id.insert(index.edge).second;
498
499 if (is_new_edge)
500 {
501 current_edge_node_id = n_bases++;
502 current_edge_node = mesh.edge_barycenter(index.edge);
503
504 if (opposite_face < 0)
505 {
506 // bounday_nodes.push_back(current_edge_node_id);
507 lb.add_boundary_primitive(index.edge, edge_basis_id - 4);
508 }
509 }
510 }
511
512 if (is_vertex_q2)
513 {
514 assert(is_edge_q2);
515 const bool is_new_vertex = vertex_id.insert(index.vertex).second;
516
517 if (is_new_vertex)
518 {
519 current_vertex_node_id = n_bases++;
520 current_vertex_node = mesh.point(index.vertex);
521
522 // if(is_vertex_boundary)//mesh.is_vertex_boundary(index.vertex))
523 // bounday_nodes.push_back(current_vertex_node_id);
524 }
525 }
526
527 // init new Q2 nodes
528 if (current_vertex_node_id >= 0)
529 b.bases[vertex_basis_id].init(2, current_vertex_node_id, vertex_basis_id, current_vertex_node);
530
531 if (current_edge_node_id >= 0)
532 b.bases[edge_basis_id].init(2, current_edge_node_id, edge_basis_id, current_edge_node);
533
534 // set the basis functions
535 b.bases[vertex_basis_id].set_basis([vertex_basis_id](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { polyfem::autogen::q_basis_value_2d(2, vertex_basis_id, uv, val); });
536 b.bases[vertex_basis_id].set_grad([vertex_basis_id](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { polyfem::autogen::q_grad_basis_value_2d(2, vertex_basis_id, uv, val); });
537
538 b.bases[edge_basis_id].set_basis([edge_basis_id](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { polyfem::autogen::q_basis_value_2d(2, edge_basis_id, uv, val); });
539 b.bases[edge_basis_id].set_grad([edge_basis_id](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { polyfem::autogen::q_grad_basis_value_2d(2, edge_basis_id, uv, val); });
540
541 index = mesh.next_around_face(index);
542 }
543
544 // central node always present
545 const int face_basis_id = 8;
546 b.bases[face_basis_id].init(2, n_bases++, face_basis_id, mesh.face_barycenter(el_index));
547 b.bases[face_basis_id].set_basis([face_basis_id](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { polyfem::autogen::q_basis_value_2d(2, face_basis_id, uv, val); });
548 b.bases[face_basis_id].set_grad([face_basis_id](const Eigen::MatrixXd &uv, Eigen::MatrixXd &val) { polyfem::autogen::q_grad_basis_value_2d(2, face_basis_id, uv, val); });
549
550 if (!lb.empty())
551 local_boundary.emplace_back(lb);
552 }
553
554 void insert_into_global(const Local2Global &data, std::vector<Local2Global> &vec)
555 {
556 // ignore small weights
557 if (fabs(data.val) < 1e-10)
558 return;
559
560 bool found = false;
561
562 for (std::size_t i = 0; i < vec.size(); ++i)
563 {
564 if (vec[i].index == data.index)
565 {
566 // logger().trace("{} {} {}", vec[i].val, data.val, fabs(vec[i].val - data.val));
567 assert(fabs(vec[i].val - data.val) < 1e-10);
568 assert((vec[i].node - data.node).norm() < 1e-10);
569 found = true;
570 break;
571 }
572 }
573
574 if (!found)
575 vec.push_back(data);
576 }
577
578 void assign_q2_weights(const Mesh2D &mesh, const int el_index, std::vector<ElementBases> &bases)
579 {
580 // Eigen::MatrixXd eval_p;
581 std::vector<AssemblyValues> eval_p;
582 Navigation::Index index = mesh.get_index_from_face(el_index);
583 ElementBases &b = bases[el_index];
584
585 for (int j = 0; j < 4; ++j)
586 {
587 const int opposite_face = mesh.switch_face(index).face;
588
589 if (opposite_face < 0 || !mesh.is_cube(opposite_face))
590 {
591 index = mesh.next_around_face(index);
592 continue;
593 }
594
595 // const auto param_p = LagrangeBasis2d::quadr_quad_edge_local_nodes_coordinates(mesh, mesh.switch_face(index));
596 // const auto indices = LagrangeBasis2d::quadr_quad_edge_local_nodes(mesh, index);
597
598 const auto indices = LagrangeBasis2d::quad_edge_local_nodes(2, mesh, index);
599 Eigen::Matrix<double, 3, 2> param_p;
600
601 {
602 Eigen::MatrixXd quad_loc_nodes;
603 polyfem::autogen::q_nodes_2d(2, quad_loc_nodes);
604 const auto opposite_indices = LagrangeBasis2d::quad_edge_local_nodes(2, mesh, mesh.switch_face(index));
605 for (int k = 0; k < 3; ++k)
606 param_p.row(k) = quad_loc_nodes.row(opposite_indices[k]);
607 }
608
609 const int i0 = indices[0];
610 const int i1 = indices[1];
611 const int i2 = indices[2];
612
613 const auto &other_bases = bases[opposite_face];
614 other_bases.evaluate_bases(param_p, eval_p);
615
616 for (std::size_t i = 0; i < other_bases.bases.size(); ++i)
617 {
618 const auto &other_b = other_bases.bases[i];
619
620 if (other_b.global().empty())
621 continue;
622
623 assert(eval_p[i].val.size() == 3);
624
625 // basis i of element opposite face is zero on this elements
626 if (eval_p[i].val.cwiseAbs().maxCoeff() <= 1e-10)
627 continue;
628
629 for (std::size_t k = 0; k < other_b.global().size(); ++k)
630 {
631 // auto glob0 = other_b.global()[k]; glob0.val *= eval_p(0,i);
632 // auto glob1 = other_b.global()[k]; glob1.val *= eval_p(1,i);
633 // auto glob2 = other_b.global()[k]; glob2.val *= eval_p(2,i);
634
635 auto glob0 = other_b.global()[k];
636 glob0.val *= eval_p[i].val(0);
637 auto glob1 = other_b.global()[k];
638 glob1.val *= eval_p[i].val(1);
639 auto glob2 = other_b.global()[k];
640 glob2.val *= eval_p[i].val(2);
641
642 insert_into_global(glob0, b.bases[i0].global());
643 insert_into_global(glob1, b.bases[i1].global());
644 insert_into_global(glob2, b.bases[i2].global());
645 }
646 }
647
648 index = mesh.next_around_face(index);
649 }
650 }
651
652 void setup_data_for_polygons(const Mesh2D &mesh, const int el_index, const ElementBases &b, std::map<int, InterfaceData> &poly_edge_to_data)
653 {
654 Navigation::Index index = mesh.get_index_from_face(el_index);
655 for (int j = 0; j < 4; ++j)
656 {
657 const int opposite_face = mesh.switch_face(index).face;
658 const bool is_neigh_poly = opposite_face >= 0 && mesh.is_polytope(opposite_face);
659
660 if (is_neigh_poly)
661 {
662 // auto e2l = LagrangeBasis2d::quadr_quad_edge_local_nodes(mesh, index);
663 auto e2l = LagrangeBasis2d::quad_edge_local_nodes(2, mesh, index);
664 const int vertex_basis_id = e2l[0];
665 const int edge_basis_id = e2l[1];
666 const int vertex_basis_id2 = e2l[2];
667
668 InterfaceData &data = poly_edge_to_data[index.edge];
669
670 data.local_indices.push_back(edge_basis_id);
671 data.local_indices.push_back(vertex_basis_id);
672 data.local_indices.push_back(vertex_basis_id2);
673 }
674
675 index = mesh.next_around_face(index);
676 }
677 }
678 } // namespace
679
681 const std::string &assembler,
682 const int quadrature_order, const int mass_quadrature_order, std::vector<ElementBases> &bases, std::vector<LocalBoundary> &local_boundary, std::map<int, InterfaceData> &poly_edge_to_data)
683 {
684 using std::max;
685 assert(!mesh.is_volume());
686
687 MeshNodes mesh_nodes(mesh, true, true, 1, 1);
688
689 const int n_els = mesh.n_elements();
690 bases.resize(n_els);
691
692 local_boundary.clear();
693
694 // QuadQuadrature quad_quadrature;
695
696 for (int e = 0; e < n_els; ++e)
697 {
698 if (!mesh.is_spline_compatible(e))
699 continue;
700
701 SpaceMatrix space;
702 NodeMatrix loc_nodes;
703
704 // const int max_local_base =
705 build_local_space(mesh, mesh_nodes, e, space, loc_nodes, local_boundary, poly_edge_to_data);
706 // n_bases = max(n_bases, max_local_base);
707
708 ElementBases &b = bases[e];
709 // quad_quadrature.get_quadrature(quadrature_order, b.quadrature);
710 const int real_order = quadrature_order > 0 ? quadrature_order : AssemblerUtils::quadrature_order(assembler, 2, AssemblerUtils::BasisType::SPLINE, 2);
711 const int real_mass_order = mass_quadrature_order > 0 ? mass_quadrature_order : AssemblerUtils::quadrature_order("Mass", 2, AssemblerUtils::BasisType::SPLINE, 2);
712
713 b.set_quadrature([real_order](Quadrature &quad) {
714 QuadQuadrature quad_quadrature;
715 quad_quadrature.get_quadrature(real_order, quad);
716 });
717 b.set_mass_quadrature([real_mass_order](Quadrature &quad) {
718 QuadQuadrature quad_quadrature;
719 quad_quadrature.get_quadrature(real_mass_order, quad);
720 });
721 b.bases.resize(9);
722
723 b.set_local_node_from_primitive_func([e](const int primitive_id, const Mesh &mesh) {
724 Eigen::VectorXi res(3);
725 const auto &mesh2d = dynamic_cast<const Mesh2D &>(mesh);
726 auto index = mesh2d.get_index_from_face(e);
727 int le;
728 for (le = 0; le < mesh2d.n_face_vertices(e); ++le)
729 {
730 if (index.edge == primitive_id)
731 break;
732 index = mesh2d.next_around_face(index);
733 }
734 assert(index.edge == primitive_id);
735
736 switch (le)
737 {
738 case 3:
739 res << (3 * 0 + 0), (3 * 1 + 0), (3 * 2 + 0);
740 break;
741 case 0:
742 res << (3 * 0 + 0), (3 * 0 + 1), (3 * 0 + 2);
743 break;
744 case 1:
745 res << (3 * 0 + 2), (3 * 1 + 2), (3 * 2 + 2);
746 break;
747 case 2:
748 res << (3 * 2 + 0), (3 * 2 + 1), (3 * 2 + 2);
749 break;
750 default:
751 assert(false);
752 }
753
754 return res;
755 });
756
757 std::array<std::array<double, 4>, 3> h_knots;
758 std::array<std::array<double, 4>, 3> v_knots;
759
760 setup_knots_vectors(mesh_nodes, space, h_knots, v_knots);
761
762 // print_local_space(space);
763
764 basis_for_regular_quad(space, loc_nodes, h_knots, v_knots, b);
765 basis_for_irregulard_quad(e, mesh, mesh_nodes, space, loc_nodes, h_knots, v_knots, b);
766 }
767
768 std::set<int> edge_id;
769 std::set<int> vertex_id;
770
771 int n_bases = mesh_nodes.n_nodes();
772
773 for (int e = 0; e < n_els; ++e)
774 {
775 if (mesh.is_polytope(e) || mesh.is_spline_compatible(e))
776 continue;
777
778 ElementBases &b = bases[e];
779 // quad_quadrature.get_quadrature(quadrature_order, b.quadrature);
780 const int real_order = quadrature_order > 0 ? quadrature_order : AssemblerUtils::quadrature_order(assembler, 2, AssemblerUtils::BasisType::CUBE_LAGRANGE, 2);
781 const int real_mass_order = mass_quadrature_order > 0 ? mass_quadrature_order : AssemblerUtils::quadrature_order("Mass", 2, AssemblerUtils::BasisType::CUBE_LAGRANGE, 2);
782
783 b.set_quadrature([real_order](Quadrature &quad) {
784 QuadQuadrature quad_quadrature;
785 quad_quadrature.get_quadrature(real_order, quad);
786 });
787 b.set_mass_quadrature([real_mass_order](Quadrature &quad) {
788 QuadQuadrature quad_quadrature;
789 quad_quadrature.get_quadrature(real_mass_order, quad);
790 });
791
792 b.set_local_node_from_primitive_func([e](const int primitive_id, const Mesh &mesh) {
793 const auto &mesh2d = dynamic_cast<const Mesh2D &>(mesh);
794 auto index = mesh2d.get_index_from_face(e);
795
796 for (int le = 0; le < mesh2d.n_face_vertices(e); ++le)
797 {
798 if (index.edge == primitive_id)
799 break;
800 index = mesh2d.next_around_face(index);
801 }
802 assert(index.edge == primitive_id);
803
804 // const auto indices = LagrangeBasis2d::quadr_quad_edge_local_nodes(mesh2d, index);
805 const auto indices = LagrangeBasis2d::quad_edge_local_nodes(2, mesh2d, index);
806 Eigen::VectorXi res(indices.size());
807
808 for (size_t i = 0; i < indices.size(); ++i)
809 res(i) = indices[i];
810
811 return res;
812 });
813
814 create_q2_nodes(mesh, e, vertex_id, edge_id, b, local_boundary, n_bases);
815 }
816
817 bool missing_bases = false;
818 do
819 {
820 missing_bases = false;
821 for (int e = 0; e < n_els; ++e)
822 {
823 if (mesh.is_polytope(e) || mesh.is_spline_compatible(e))
824 continue;
825
826 auto &b = bases[e];
827 if (b.is_complete())
828 continue;
829
830 assign_q2_weights(mesh, e, bases);
831
832 missing_bases = missing_bases || b.is_complete();
833 }
834 } while (missing_bases);
835
836 for (int e = 0; e < n_els; ++e)
837 {
838 if (mesh.is_polytope(e) || mesh.is_spline_compatible(e))
839 continue;
840 const ElementBases &b = bases[e];
841 setup_data_for_polygons(mesh, e, b, poly_edge_to_data);
842 }
843
844 return n_bases;
845 }
846
847 void SplineBasis2d::fit_nodes(const Mesh2D &mesh, const int n_bases, std::vector<ElementBases> &gbases)
848 {
849 assert(false);
850 // const int dim = 2;
851 // const int n_constraints = 9;
852 // const int n_elements = mesh.n_elements();
853
854 // std::vector< Eigen::Triplet<double> > entries, entries_t;
855
856 // MeshNodes nodes(mesh, 1, 1, 0);
857 // // Eigen::MatrixXd tmp;
858 // std::vector<AssemblyValues> tmp_val;
859
860 // Eigen::MatrixXd node_rhs(n_constraints*n_elements, dim);
861 // Eigen::MatrixXd samples(n_constraints, dim);
862 // polyfem::autogen::q_nodes_2d(2, samples);
863 // // for(int i = 0; i < n_constraints; ++i)
864 // // samples.row(i) = LagrangeBasis2d::quadr_quad_local_node_coordinates(i);
865
866 // for(int i = 0; i < n_elements; ++i)
867 // {
868 // auto &base = gbases[i];
869
870 // if(!mesh.is_cube(i))
871 // continue;
872
873 // auto global_ids = LagrangeBasis2d::quadr_quad_local_to_global(mesh, i);
874 // assert(global_ids.size() == n_constraints);
875
876 // for(int j = 0; j < n_constraints; ++j)
877 // {
878 // auto n_id = nodes.node_id_from_primitive(global_ids[j]);
879 // auto n = nodes.node_position(n_id);
880 // for(int d = 0; d < dim; ++d)
881 // node_rhs(n_constraints*i + j, d) = n(d);
882 // }
883
884 // base.evaluate_bases(samples, tmp_val);
885 // const auto &lbs = base.bases;
886
887 // const int n_local_bases = int(lbs.size());
888 // for(int j = 0; j < n_local_bases; ++j)
889 // {
890 // const Basis &b = lbs[j];
891 // const auto &tmp = tmp_val[j].val;
892
893 // for(std::size_t ii = 0; ii < b.global().size(); ++ii)
894 // {
895 // for (long k = 0; k < tmp.size(); ++k)
896 // {
897 // entries.emplace_back(n_constraints*i + k, b.global()[ii].index, tmp(k)*b.global()[ii].val);
898 // entries_t.emplace_back(b.global()[ii].index, n_constraints*i + k, tmp(k)*b.global()[ii].val);
899 // }
900 // }
901 // }
902 // }
903
904 // Eigen::MatrixXd new_nodes(n_bases, dim);
905
906 // {
907 // StiffnessMatrix mat(n_constraints*n_elements, n_bases);
908 // StiffnessMatrix mat_t(n_bases, n_constraints*n_elements);
909
910 // mat.setFromTriplets(entries.begin(), entries.end());
911 // mat_t.setFromTriplets(entries_t.begin(), entries_t.end());
912
913 // StiffnessMatrix A = mat_t * mat;
914 // Eigen::MatrixXd b = mat_t * node_rhs;
915
916 // json params = {
917 // {"mtype", -2}, // matrix type for Pardiso (2 = SPD)
918 // // {"max_iter", 0}, // for iterative solvers
919 // // {"tolerance", 1e-9}, // for iterative solvers
920 // };
921 // auto solver = LinearSolver::create("", "");
922 // solver->setParameters(params);
923 // solver->analyzePattern(A);
924 // solver->factorize(A);
925
926 // for(int d = 0; d < dim; ++d)
927 // solver->solve(b.col(d), new_nodes.col(d));
928 // }
929
930 // for(int i = 0; i < n_elements; ++i)
931 // {
932 // auto &base = gbases[i];
933
934 // if(!mesh.is_cube(i))
935 // continue;
936
937 // auto &lbs = base.bases;
938 // const int n_local_bases = int(lbs.size());
939 // for(int j = 0; j < n_local_bases; ++j)
940 // {
941 // Basis &b = lbs[j];
942
943 // for(std::size_t ii = 0; ii < b.global().size(); ++ii)
944 // {
945 // // if(nodes.is_primitive_boundary(b.global()[ii].index))
946 // // continue;
947
948 // for(int d = 0; d < dim; ++d)
949 // {
950 // b.global()[ii].node(d) = new_nodes(b.global()[ii].index, d);
951 // }
952 // }
953 // }
954 // }
955 }
956 } // namespace basis
957} // namespace polyfem
Eigen::MatrixXd vec
double val
Quadrature quadrature
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 quad_edge_local_nodes(const int q, const mesh::Mesh2D &mesh, mesh::Navigation::Index index)
static void fit_nodes(const mesh::Mesh2D &mesh, const int n_bases, std::vector< ElementBases > &gbases)
static int build_bases(const mesh::Mesh2D &mesh, const std::string &assembler, const int quadrature_order, const int mass_quadrature_order, std::vector< ElementBases > &bases, std::vector< mesh::LocalBoundary > &local_boundary, std::map< int, InterfaceData > &poly_edge_to_data)
2D mesh interface: the elements are the faces (polygons) and the boundary elements are the edges.
Definition Mesh2D.hpp:18
bool is_volume() const override
checks if mesh is volume
Definition Mesh2D.hpp:27
virtual Navigation::Index get_index_from_face(int f, int lv=0) const =0
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
Definition Mesh.hpp:181
bool is_polytope(const int el_id) const
checks if element is polygon compatible
Definition Mesh.cpp:374
bool is_spline_compatible(const int el_id) const
checks if element is spline compatible
Definition Mesh.cpp:341
void get_quadrature(const int order, Quadrature &quad)
str base
Definition p_bases.py:438
list indices
Definition p_bases.py:259
void q_nodes_2d(const int q, Eigen::MatrixXd &val)
void q_grad_basis_value_2d(const int q, const int local_index, const Eigen::MatrixXd &uv, Eigen::MatrixXd &val)
void q_basis_value_2d(const int q, const int local_index, const Eigen::MatrixXd &uv, Eigen::MatrixXd &val)
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44