Line data Source code
1 : // The libMesh Finite Element Library.
2 : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 :
4 : // This library is free software; you can redistribute it and/or
5 : // modify it under the terms of the GNU Lesser General Public
6 : // License as published by the Free Software Foundation; either
7 : // version 2.1 of the License, or (at your option) any later version.
8 :
9 : // This library is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : // Lesser General Public License for more details.
13 :
14 : // You should have received a copy of the GNU Lesser General Public
15 : // License along with this library; if not, write to the Free Software
16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 :
18 :
19 :
20 : // Local includes
21 : #include "libmesh/fe.h"
22 : #include "libmesh/inf_fe.h"
23 : #include "libmesh/libmesh_logging.h"
24 :
25 : // For projection code:
26 : #include "libmesh/boundary_info.h"
27 : #include "libmesh/mesh_base.h"
28 : #include "libmesh/dense_matrix.h"
29 : #include "libmesh/dense_vector.h"
30 : #include "libmesh/dof_map.h"
31 : #include "libmesh/elem.h"
32 : #include "libmesh/fe_interface.h"
33 : #include "libmesh/int_range.h"
34 : #include "libmesh/numeric_vector.h"
35 : #include "libmesh/periodic_boundary_base.h"
36 : #include "libmesh/periodic_boundaries.h"
37 : #include "libmesh/quadrature.h"
38 : #include "libmesh/quadrature_gauss.h"
39 : #include "libmesh/tensor_value.h"
40 : #include "libmesh/threads.h"
41 : #include "libmesh/fe_type.h"
42 : #include "libmesh/enum_to_string.h"
43 :
44 : // C++ Includes
45 : #include <memory>
46 :
47 : // Anonymous namespace, for a helper function for periodic boundary
48 : // constraint calculations
49 : namespace
50 : {
51 : using namespace libMesh;
52 :
53 : #ifdef LIBMESH_ENABLE_PERIODIC
54 :
55 : // Find the "primary" element around a boundary point:
56 62230 : const Elem * primary_boundary_point_neighbor(const Elem * elem,
57 : const Point & p,
58 : const BoundaryInfo & boundary_info,
59 : const std::set<boundary_id_type> & boundary_ids)
60 : {
61 : // If we don't find a better alternative, the user will have
62 : // provided the primary element
63 9699 : const Elem * primary = elem;
64 :
65 : // Container to catch boundary IDs passed back by BoundaryInfo.
66 19398 : std::vector<boundary_id_type> bc_ids;
67 :
68 : // Pull object out of the loop to reduce heap operations
69 62230 : std::unique_ptr<const Elem> periodic_side;
70 :
71 9699 : std::set<const Elem *> point_neighbors;
72 62230 : elem->find_point_neighbors(p, point_neighbors);
73 233630 : for (const auto & pt_neighbor : point_neighbors)
74 : {
75 : // If this point neighbor isn't at least
76 : // as coarse as the current primary elem, or if it is at
77 : // the same level but has a lower id, then
78 : // we won't defer to it.
79 339550 : if ((pt_neighbor->level() > primary->level()) ||
80 292556 : (pt_neighbor->level() == primary->level() &&
81 166728 : pt_neighbor->id() < primary->id()))
82 66000 : continue;
83 :
84 : // Otherwise, we will defer to the point neighbor, but only if
85 : // one of its sides is on a relevant boundary and that side
86 : // contains this vertex
87 14529 : bool vertex_on_periodic_side = false;
88 258476 : for (auto ns : pt_neighbor->side_index_range())
89 : {
90 243248 : boundary_info.boundary_ids (pt_neighbor, ns, bc_ids);
91 :
92 34535 : bool on_relevant_boundary = false;
93 486496 : for (const auto & id : boundary_ids)
94 243248 : if (std::find(bc_ids.begin(), bc_ids.end(), id) != bc_ids.end())
95 14483 : on_relevant_boundary = true;
96 :
97 243248 : if (!on_relevant_boundary)
98 118137 : continue;
99 :
100 104989 : pt_neighbor->build_side_ptr(periodic_side, ns);
101 104989 : if (!periodic_side->contains_point(p))
102 215 : continue;
103 :
104 14470 : vertex_on_periodic_side = true;
105 14470 : break;
106 : }
107 :
108 105400 : if (vertex_on_periodic_side)
109 104760 : primary = pt_neighbor;
110 : }
111 :
112 71929 : return primary;
113 42832 : }
114 :
115 : // Find the "primary" element around a boundary edge:
116 3360 : const Elem * primary_boundary_edge_neighbor(const Elem * elem,
117 : const Point & p1,
118 : const Point & p2,
119 : const BoundaryInfo & boundary_info,
120 : const std::set<boundary_id_type> & boundary_ids)
121 : {
122 : // If we don't find a better alternative, the user will have
123 : // provided the primary element
124 280 : const Elem * primary = elem;
125 :
126 560 : std::set<const Elem *> edge_neighbors;
127 3360 : elem->find_edge_neighbors(p1, p2, edge_neighbors);
128 :
129 : // Container to catch boundary IDs handed back by BoundaryInfo
130 560 : std::vector<boundary_id_type> bc_ids;
131 :
132 : // Pull object out of the loop to reduce heap operations
133 3080 : std::unique_ptr<const Elem> periodic_side;
134 :
135 6720 : for (const auto & e_neighbor : edge_neighbors)
136 : {
137 : // If this edge neighbor isn't at least
138 : // as coarse as the current primary elem, or if it is at
139 : // the same level but has a lower id, then
140 : // we won't defer to it.
141 6720 : if ((e_neighbor->level() > primary->level()) ||
142 6160 : (e_neighbor->level() == primary->level() &&
143 3360 : e_neighbor->id() < primary->id()))
144 0 : continue;
145 :
146 : // Otherwise, we will defer to the edge neighbor, but only if
147 : // one of its sides is on this periodic boundary and that
148 : // side contains this edge
149 280 : bool vertex_on_periodic_side = false;
150 6808 : for (auto ns : e_neighbor->side_index_range())
151 : {
152 6528 : boundary_info.boundary_ids (e_neighbor, ns, bc_ids);
153 :
154 544 : bool on_relevant_boundary = false;
155 13056 : for (const auto & id : boundary_ids)
156 6528 : if (std::find(bc_ids.begin(), bc_ids.end(), id) != bc_ids.end())
157 298 : on_relevant_boundary = true;
158 :
159 6528 : if (!on_relevant_boundary)
160 2706 : continue;
161 :
162 3576 : e_neighbor->build_side_ptr(periodic_side, ns);
163 7044 : if (!(periodic_side->contains_point(p1) &&
164 3468 : periodic_side->contains_point(p2)))
165 216 : continue;
166 :
167 280 : vertex_on_periodic_side = true;
168 280 : break;
169 : }
170 :
171 3360 : if (vertex_on_periodic_side)
172 3360 : primary = e_neighbor;
173 : }
174 :
175 3640 : return primary;
176 2800 : }
177 :
178 : #endif // LIBMESH_ENABLE_PERIODIC
179 :
180 : }
181 :
182 : namespace libMesh
183 : {
184 :
185 :
186 :
187 : // ------------------------------------------------------------
188 : // FEBase class members
189 : template <>
190 : std::unique_ptr<FEGenericBase<Real>>
191 5564559 : FEGenericBase<Real>::build (const unsigned int dim,
192 : const FEType & fet)
193 : {
194 5564559 : switch (dim)
195 : {
196 : // 0D
197 12 : case 0:
198 : {
199 12 : switch (fet.family)
200 : {
201 0 : case CLOUGH:
202 0 : return std::make_unique<FE<0,CLOUGH>>(fet);
203 :
204 0 : case HERMITE:
205 0 : return std::make_unique<FE<0,HERMITE>>(fet);
206 :
207 12 : case LAGRANGE:
208 12 : return std::make_unique<FE<0,LAGRANGE>>(fet);
209 :
210 0 : case L2_LAGRANGE:
211 0 : return std::make_unique<FE<0,L2_LAGRANGE>>(fet);
212 :
213 0 : case L2_LAGRANGE_GLL:
214 0 : return std::make_unique<FE<0,L2_LAGRANGE_GLL>>(fet);
215 :
216 0 : case LAGRANGE_GLL:
217 0 : return std::make_unique<FE<0,LAGRANGE_GLL>>(fet);
218 :
219 0 : case HIERARCHIC:
220 0 : return std::make_unique<FE<0,HIERARCHIC>>(fet);
221 :
222 0 : case L2_HIERARCHIC:
223 0 : return std::make_unique<FE<0,L2_HIERARCHIC>>(fet);
224 :
225 0 : case SIDE_HIERARCHIC:
226 0 : return std::make_unique<FE<0,SIDE_HIERARCHIC>>(fet);
227 :
228 0 : case MONOMIAL:
229 0 : return std::make_unique<FE<0,MONOMIAL>>(fet);
230 :
231 : #ifdef LIBMESH_ENABLE_HIGHER_ORDER_SHAPES
232 0 : case SZABAB:
233 0 : return std::make_unique<FE<0,SZABAB>>(fet);
234 :
235 0 : case BERNSTEIN:
236 0 : return std::make_unique<FE<0,BERNSTEIN>>(fet);
237 :
238 0 : case RATIONAL_BERNSTEIN:
239 0 : return std::make_unique<FE<0,RATIONAL_BERNSTEIN>>(fet);
240 : #endif
241 :
242 0 : case XYZ:
243 0 : return std::make_unique<FEXYZ<0>>(fet);
244 :
245 0 : case SCALAR:
246 0 : return std::make_unique<FEScalar<0>>(fet);
247 :
248 0 : default:
249 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
250 : }
251 : }
252 : // 1D
253 467301 : case 1:
254 : {
255 467301 : switch (fet.family)
256 : {
257 0 : case CLOUGH:
258 0 : return std::make_unique<FE<1,CLOUGH>>(fet);
259 :
260 399132 : case HERMITE:
261 399132 : return std::make_unique<FE<1,HERMITE>>(fet);
262 :
263 6114 : case LAGRANGE:
264 6114 : return std::make_unique<FE<1,LAGRANGE>>(fet);
265 :
266 3124 : case L2_LAGRANGE:
267 3124 : return std::make_unique<FE<1,L2_LAGRANGE>>(fet);
268 :
269 12354 : case L2_LAGRANGE_GLL:
270 12354 : return std::make_unique<FE<1,L2_LAGRANGE_GLL>>(fet);
271 :
272 5396 : case LAGRANGE_GLL:
273 5396 : return std::make_unique<FE<1,LAGRANGE_GLL>>(fet);
274 :
275 12359 : case HIERARCHIC:
276 12359 : return std::make_unique<FE<1,HIERARCHIC>>(fet);
277 :
278 4970 : case L2_HIERARCHIC:
279 4970 : return std::make_unique<FE<1,L2_HIERARCHIC>>(fet);
280 :
281 3550 : case SIDE_HIERARCHIC:
282 3550 : return std::make_unique<FE<1,SIDE_HIERARCHIC>>(fet);
283 :
284 5012 : case MONOMIAL:
285 5012 : return std::make_unique<FE<1,MONOMIAL>>(fet);
286 :
287 : #ifdef LIBMESH_ENABLE_HIGHER_ORDER_SHAPES
288 3976 : case SZABAB:
289 3976 : return std::make_unique<FE<1,SZABAB>>(fet);
290 :
291 4970 : case BERNSTEIN:
292 4970 : return std::make_unique<FE<1,BERNSTEIN>>(fet);
293 :
294 2368 : case RATIONAL_BERNSTEIN:
295 2368 : return std::make_unique<FE<1,RATIONAL_BERNSTEIN>>(fet);
296 : #endif
297 :
298 3976 : case XYZ:
299 3976 : return std::make_unique<FEXYZ<1>>(fet);
300 :
301 0 : case SCALAR:
302 0 : return std::make_unique<FEScalar<1>>(fet);
303 :
304 0 : default:
305 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
306 : }
307 : }
308 :
309 :
310 : // 2D
311 1720364 : case 2:
312 : {
313 1720364 : switch (fet.family)
314 : {
315 70708 : case CLOUGH:
316 70708 : return std::make_unique<FE<2,CLOUGH>>(fet);
317 :
318 36038 : case HERMITE:
319 36038 : return std::make_unique<FE<2,HERMITE>>(fet);
320 :
321 545970 : case LAGRANGE:
322 545970 : return std::make_unique<FE<2,LAGRANGE>>(fet);
323 :
324 57144 : case L2_LAGRANGE:
325 57144 : return std::make_unique<FE<2,L2_LAGRANGE>>(fet);
326 :
327 12496 : case L2_LAGRANGE_GLL:
328 12496 : return std::make_unique<FE<2,L2_LAGRANGE_GLL>>(fet);
329 :
330 5538 : case LAGRANGE_GLL:
331 5538 : return std::make_unique<FE<2,LAGRANGE_GLL>>(fet);
332 :
333 526520 : case HIERARCHIC:
334 526520 : return std::make_unique<FE<2,HIERARCHIC>>(fet);
335 :
336 103866 : case L2_HIERARCHIC:
337 103866 : return std::make_unique<FE<2,L2_HIERARCHIC>>(fet);
338 :
339 147652 : case SIDE_HIERARCHIC:
340 147652 : return std::make_unique<FE<2,SIDE_HIERARCHIC>>(fet);
341 :
342 23816 : case MONOMIAL:
343 23816 : return std::make_unique<FE<2,MONOMIAL>>(fet);
344 :
345 : #ifdef LIBMESH_ENABLE_HIGHER_ORDER_SHAPES
346 16934 : case SZABAB:
347 16934 : return std::make_unique<FE<2,SZABAB>>(fet);
348 :
349 16550 : case BERNSTEIN:
350 16550 : return std::make_unique<FE<2,BERNSTEIN>>(fet);
351 :
352 118705 : case RATIONAL_BERNSTEIN:
353 118705 : return std::make_unique<FE<2,RATIONAL_BERNSTEIN>>(fet);
354 : #endif
355 :
356 38356 : case XYZ:
357 38356 : return std::make_unique<FEXYZ<2>>(fet);
358 :
359 0 : case SCALAR:
360 0 : return std::make_unique<FEScalar<2>>(fet);
361 :
362 71 : case SUBDIVISION:
363 71 : return std::make_unique<FESubdivision>(fet);
364 :
365 0 : default:
366 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
367 : }
368 : }
369 :
370 :
371 : // 3D
372 3376882 : case 3:
373 : {
374 3376882 : switch (fet.family)
375 : {
376 0 : case CLOUGH:
377 0 : libmesh_error_msg("ERROR: Clough-Tocher elements currently only support 1D and 2D");
378 :
379 1306 : case HERMITE:
380 1306 : return std::make_unique<FE<3,HERMITE>>(fet);
381 :
382 2604541 : case LAGRANGE:
383 2604541 : return std::make_unique<FE<3,LAGRANGE>>(fet);
384 :
385 89277 : case L2_LAGRANGE:
386 89277 : return std::make_unique<FE<3,L2_LAGRANGE>>(fet);
387 :
388 12354 : case L2_LAGRANGE_GLL:
389 12354 : return std::make_unique<FE<3,L2_LAGRANGE_GLL>>(fet);
390 :
391 4402 : case LAGRANGE_GLL:
392 4402 : return std::make_unique<FE<3,LAGRANGE_GLL>>(fet);
393 :
394 85004 : case HIERARCHIC:
395 85004 : return std::make_unique<FE<3,HIERARCHIC>>(fet);
396 :
397 94460 : case L2_HIERARCHIC:
398 94460 : return std::make_unique<FE<3,L2_HIERARCHIC>>(fet);
399 :
400 305832 : case SIDE_HIERARCHIC:
401 305832 : return std::make_unique<FE<3,SIDE_HIERARCHIC>>(fet);
402 :
403 33839 : case MONOMIAL:
404 33839 : return std::make_unique<FE<3,MONOMIAL>>(fet);
405 :
406 : #ifdef LIBMESH_ENABLE_HIGHER_ORDER_SHAPES
407 0 : case SZABAB:
408 0 : return std::make_unique<FE<3,SZABAB>>(fet);
409 :
410 30670 : case BERNSTEIN:
411 30670 : return std::make_unique<FE<3,BERNSTEIN>>(fet);
412 :
413 15767 : case RATIONAL_BERNSTEIN:
414 15767 : return std::make_unique<FE<3,RATIONAL_BERNSTEIN>>(fet);
415 : #endif
416 :
417 99430 : case XYZ:
418 99430 : return std::make_unique<FEXYZ<3>>(fet);
419 :
420 0 : case SCALAR:
421 0 : return std::make_unique<FEScalar<3>>(fet);
422 :
423 0 : default:
424 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
425 : }
426 : }
427 :
428 0 : default:
429 0 : libmesh_error_msg("Invalid dimension dim = " << dim);
430 : }
431 : }
432 :
433 :
434 :
435 : template <>
436 : std::unique_ptr<FEGenericBase<RealGradient>>
437 574868 : FEGenericBase<RealGradient>::build (const unsigned int dim,
438 : const FEType & fet)
439 : {
440 574868 : switch (dim)
441 : {
442 : // 0D
443 0 : case 0:
444 : {
445 0 : switch (fet.family)
446 : {
447 0 : case HIERARCHIC_VEC:
448 0 : return std::make_unique<FEHierarchicVec<0>>(fet);
449 :
450 0 : case L2_HIERARCHIC_VEC:
451 0 : return std::make_unique<FEL2HierarchicVec<0>>(fet);
452 :
453 0 : case LAGRANGE_VEC:
454 0 : return std::make_unique<FELagrangeVec<0>>(fet);
455 :
456 0 : case L2_LAGRANGE_VEC:
457 0 : return std::make_unique<FEL2LagrangeVec<0>>(fet);
458 :
459 0 : case MONOMIAL_VEC:
460 0 : return std::make_unique<FEMonomialVec<0>>(fet);
461 :
462 0 : default:
463 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
464 : }
465 : }
466 0 : case 1:
467 : {
468 0 : switch (fet.family)
469 : {
470 0 : case HIERARCHIC_VEC:
471 0 : return std::make_unique<FEHierarchicVec<1>>(fet);
472 :
473 0 : case L2_HIERARCHIC_VEC:
474 0 : return std::make_unique<FEL2HierarchicVec<1>>(fet);
475 :
476 0 : case LAGRANGE_VEC:
477 0 : return std::make_unique<FELagrangeVec<1>>(fet);
478 :
479 0 : case L2_LAGRANGE_VEC:
480 0 : return std::make_unique<FEL2LagrangeVec<1>>(fet);
481 :
482 0 : case MONOMIAL_VEC:
483 0 : return std::make_unique<FEMonomialVec<1>>(fet);
484 :
485 0 : default:
486 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
487 : }
488 : }
489 212172 : case 2:
490 : {
491 212172 : switch (fet.family)
492 : {
493 16572 : case HIERARCHIC_VEC:
494 16572 : return std::make_unique<FEHierarchicVec<2>>(fet);
495 :
496 2840 : case L2_HIERARCHIC_VEC:
497 2840 : return std::make_unique<FEL2HierarchicVec<2>>(fet);
498 :
499 15977 : case LAGRANGE_VEC:
500 15977 : return std::make_unique<FELagrangeVec<2>>(fet);
501 :
502 3250 : case L2_LAGRANGE_VEC:
503 3250 : return std::make_unique<FEL2LagrangeVec<2>>(fet);
504 :
505 1470 : case MONOMIAL_VEC:
506 1470 : return std::make_unique<FEMonomialVec<2>>(fet);
507 :
508 56033 : case NEDELEC_ONE:
509 56033 : return std::make_unique<FENedelecOne<2>>(fet);
510 :
511 114610 : case RAVIART_THOMAS:
512 114610 : return std::make_unique<FERaviartThomas<2>>(fet);
513 :
514 1420 : case L2_RAVIART_THOMAS:
515 1420 : return std::make_unique<FEL2RaviartThomas<2>>(fet);
516 :
517 0 : default:
518 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
519 : }
520 : }
521 362696 : case 3:
522 : {
523 362696 : switch (fet.family)
524 : {
525 0 : case HIERARCHIC_VEC:
526 0 : return std::make_unique<FEHierarchicVec<3>>(fet);
527 :
528 710 : case L2_HIERARCHIC_VEC:
529 710 : return std::make_unique<FEL2HierarchicVec<3>>(fet);
530 :
531 22913 : case LAGRANGE_VEC:
532 22913 : return std::make_unique<FELagrangeVec<3>>(fet);
533 :
534 1420 : case L2_LAGRANGE_VEC:
535 1420 : return std::make_unique<FEL2LagrangeVec<3>>(fet);
536 :
537 0 : case MONOMIAL_VEC:
538 0 : return std::make_unique<FEMonomialVec<3>>(fet);
539 :
540 33874 : case NEDELEC_ONE:
541 33874 : return std::make_unique<FENedelecOne<3>>(fet);
542 :
543 303069 : case RAVIART_THOMAS:
544 303069 : return std::make_unique<FERaviartThomas<3>>(fet);
545 :
546 710 : case L2_RAVIART_THOMAS:
547 710 : return std::make_unique<FEL2RaviartThomas<3>>(fet);
548 :
549 0 : default:
550 0 : libmesh_error_msg("ERROR: Bad FEType.family == " << Utility::enum_to_string(fet.family));
551 : }
552 : }
553 :
554 0 : default:
555 0 : libmesh_error_msg("Invalid dimension dim = " << dim);
556 : } // switch(dim)
557 : }
558 :
559 :
560 :
561 :
562 :
563 :
564 :
565 : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
566 :
567 :
568 : template <>
569 : std::unique_ptr<FEGenericBase<Real>>
570 456 : FEGenericBase<Real>::build_InfFE (const unsigned int dim,
571 : const FEType & fet)
572 : {
573 456 : switch (dim)
574 : {
575 :
576 : // 1D
577 0 : case 1:
578 : {
579 0 : switch (fet.radial_family)
580 : {
581 0 : case INFINITE_MAP:
582 0 : libmesh_error_msg("ERROR: Can't build an infinite element with FEFamily = " << Utility::enum_to_string(fet.radial_family));
583 :
584 0 : case JACOBI_20_00:
585 : {
586 0 : switch (fet.inf_map)
587 : {
588 0 : case CARTESIAN:
589 0 : return std::make_unique<InfFE<1,JACOBI_20_00,CARTESIAN>>(fet);
590 :
591 0 : default:
592 0 : libmesh_error_msg("ERROR: Can't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
593 : }
594 : }
595 :
596 0 : case JACOBI_30_00:
597 : {
598 0 : switch (fet.inf_map)
599 : {
600 0 : case CARTESIAN:
601 0 : return std::make_unique<InfFE<1,JACOBI_30_00,CARTESIAN>>(fet);
602 :
603 0 : default:
604 0 : libmesh_error_msg("ERROR: Can't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
605 : }
606 : }
607 :
608 0 : case LEGENDRE:
609 : {
610 0 : switch (fet.inf_map)
611 : {
612 0 : case CARTESIAN:
613 0 : return std::make_unique<InfFE<1,LEGENDRE,CARTESIAN>>(fet);
614 :
615 0 : default:
616 0 : libmesh_error_msg("ERROR: Can't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
617 : }
618 : }
619 :
620 0 : case LAGRANGE:
621 : {
622 0 : switch (fet.inf_map)
623 : {
624 0 : case CARTESIAN:
625 0 : return std::make_unique<InfFE<1,LAGRANGE,CARTESIAN>>(fet);
626 :
627 0 : default:
628 0 : libmesh_error_msg("ERROR: Can't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
629 : }
630 : }
631 :
632 0 : default:
633 0 : libmesh_error_msg("ERROR: Bad FEType.radial_family= " << Utility::enum_to_string(fet.radial_family));
634 : }
635 : }
636 :
637 :
638 :
639 :
640 : // 2D
641 0 : case 2:
642 : {
643 0 : switch (fet.radial_family)
644 : {
645 0 : case INFINITE_MAP:
646 0 : libmesh_error_msg("ERROR: Can't build an infinite element with FEFamily = " << Utility::enum_to_string(fet.radial_family));
647 :
648 0 : case JACOBI_20_00:
649 : {
650 0 : switch (fet.inf_map)
651 : {
652 0 : case CARTESIAN:
653 0 : return std::make_unique<InfFE<2,JACOBI_20_00,CARTESIAN>>(fet);
654 :
655 0 : default:
656 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
657 : }
658 : }
659 :
660 0 : case JACOBI_30_00:
661 : {
662 0 : switch (fet.inf_map)
663 : {
664 0 : case CARTESIAN:
665 0 : return std::make_unique<InfFE<2,JACOBI_30_00,CARTESIAN>>(fet);
666 :
667 0 : default:
668 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
669 : }
670 : }
671 :
672 0 : case LEGENDRE:
673 : {
674 0 : switch (fet.inf_map)
675 : {
676 0 : case CARTESIAN:
677 0 : return std::make_unique<InfFE<2,LEGENDRE,CARTESIAN>>(fet);
678 :
679 0 : default:
680 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
681 : }
682 : }
683 :
684 0 : case LAGRANGE:
685 : {
686 0 : switch (fet.inf_map)
687 : {
688 0 : case CARTESIAN:
689 0 : return std::make_unique<InfFE<2,LAGRANGE,CARTESIAN>>(fet);
690 :
691 0 : default:
692 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
693 : }
694 : }
695 :
696 0 : default:
697 0 : libmesh_error_msg("ERROR: Bad FEType.radial_family= " << Utility::enum_to_string(fet.radial_family));
698 : }
699 : }
700 :
701 :
702 :
703 :
704 : // 3D
705 456 : case 3:
706 : {
707 456 : switch (fet.radial_family)
708 : {
709 0 : case INFINITE_MAP:
710 0 : libmesh_error_msg("ERROR: Don't build an infinite element with FEFamily = " << Utility::enum_to_string(fet.radial_family));
711 :
712 401 : case JACOBI_20_00:
713 : {
714 401 : switch (fet.inf_map)
715 : {
716 401 : case CARTESIAN:
717 401 : return std::make_unique<InfFE<3,JACOBI_20_00,CARTESIAN>>(fet);
718 :
719 0 : default:
720 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
721 : }
722 : }
723 :
724 20 : case JACOBI_30_00:
725 : {
726 20 : switch (fet.inf_map)
727 : {
728 20 : case CARTESIAN:
729 20 : return std::make_unique<InfFE<3,JACOBI_30_00,CARTESIAN>>(fet);
730 :
731 0 : default:
732 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
733 : }
734 : }
735 :
736 20 : case LEGENDRE:
737 : {
738 20 : switch (fet.inf_map)
739 : {
740 20 : case CARTESIAN:
741 20 : return std::make_unique<InfFE<3,LEGENDRE,CARTESIAN>>(fet);
742 :
743 0 : default:
744 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
745 : }
746 : }
747 :
748 15 : case LAGRANGE:
749 : {
750 15 : switch (fet.inf_map)
751 : {
752 15 : case CARTESIAN:
753 15 : return std::make_unique<InfFE<3,LAGRANGE,CARTESIAN>>(fet);
754 :
755 0 : default:
756 0 : libmesh_error_msg("ERROR: Don't build an infinite element with InfMapType = " << Utility::enum_to_string(fet.inf_map));
757 : }
758 : }
759 :
760 0 : default:
761 0 : libmesh_error_msg("ERROR: Bad FEType.radial_family= " << Utility::enum_to_string(fet.radial_family));
762 : }
763 : }
764 :
765 0 : default:
766 0 : libmesh_error_msg("Invalid dimension dim = " << dim);
767 : }
768 : }
769 :
770 :
771 :
772 : template <>
773 : std::unique_ptr<FEGenericBase<RealGradient>>
774 0 : FEGenericBase<RealGradient>::build_InfFE (const unsigned int,
775 : const FEType & )
776 : {
777 : // No vector types defined... YET.
778 0 : libmesh_not_implemented();
779 : return std::unique_ptr<FEVectorBase>();
780 : }
781 :
782 : #endif // ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
783 :
784 :
785 : template <typename OutputType>
786 124348299 : void FEGenericBase<OutputType>::compute_shape_functions (const Elem * elem,
787 : const std::vector<Point> & qp)
788 : {
789 : //-------------------------------------------------------------------------
790 : // Compute the shape function values (and derivatives)
791 : // at the Quadrature points. Note that the actual values
792 : // have already been computed via init_shape_functions
793 :
794 : // Start logging the shape function computation
795 33382798 : LOG_ONE_ENTITY_SCOPE("compute_shape_functions()", "FE");
796 :
797 124348299 : this->determine_calculations();
798 :
799 124348299 : if (calculate_phi)
800 112876178 : this->_fe_trans->map_phi(this->dim, elem, qp, (*this), this->phi, this->_add_p_level_in_reinit);
801 :
802 124348299 : if (calculate_dphi)
803 90042506 : this->_fe_trans->map_dphi(this->dim, elem, qp, (*this), this->dphi,
804 82476832 : this->dphidx, this->dphidy, this->dphidz);
805 :
806 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
807 124348299 : if (calculate_d2phi)
808 8705954 : this->_fe_trans->map_d2phi(this->dim, qp, (*this), this->d2phi,
809 8069562 : this->d2phidx2, this->d2phidxdy, this->d2phidxdz,
810 8069562 : this->d2phidy2, this->d2phidydz, this->d2phidz2);
811 : #endif //LIBMESH_ENABLE_SECOND_DERIVATIVES
812 :
813 : // Only compute curl for vector-valued elements
814 9954699 : if (calculate_curl_phi && TypesEqual<OutputType,RealGradient>::value)
815 859569 : this->_fe_trans->map_curl(this->dim, elem, qp, (*this), this->curl_phi);
816 :
817 : // Only compute div for vector-valued elements
818 9954699 : if (calculate_div_phi && TypesEqual<OutputType,RealGradient>::value)
819 1476125 : this->_fe_trans->map_div(this->dim, elem, qp, (*this), this->div_phi);
820 124348299 : }
821 :
822 :
823 : // Here, we rely on the input \p phi_vals for accurate integration of the mass matrix.
824 : // This is because in contact, we often have customized qrule for mortar segments
825 : // due to deformation of the element, and the size of \p phi_vals for the secondary
826 : // element changes accordingly.
827 : template <>
828 7166 : void FEGenericBase<Real>::compute_dual_shape_coeffs (const std::vector<Real> & JxW, const std::vector<std::vector<OutputShape>> & phi_vals)
829 : {
830 : // Start logging the dual coeff computation
831 1476 : LOG_SCOPE("compute_dual_shape_coeffs()", "FE");
832 :
833 7166 : const unsigned int sz=phi_vals.size();
834 7166 : libmesh_error_msg_if(!sz, "ERROR: cannot compute dual shape coefficients with empty phi values");
835 :
836 : //compute dual basis coefficient (dual_coeff)
837 6674 : dual_coeff.resize(sz, sz);
838 8150 : DenseMatrix<Real> A(sz, sz), D(sz, sz);
839 :
840 124716 : for (const auto i : index_range(phi_vals))
841 7118264 : for (const auto qp : index_range(phi_vals[i]))
842 : {
843 8519977 : D(i,i) += JxW[qp]*phi_vals[i][qp];
844 415939348 : for (const auto j : index_range(phi_vals))
845 504190671 : A(i,j) += JxW[qp]*phi_vals[i][qp]*phi_vals[j][qp];
846 : }
847 :
848 : // The integral positivity condition on the dual basis (Popp, Wohlmuth, Gee and Wall, SIAM J. Sci.
849 : // Comput. 34(4):B421-B446, 2012, Eq. (4.2)) fails on QUAD8 and TRI6: D(k,k) = \int N_k is -1/3 at a
850 : // QUAD8 corner and exactly 0 at a TRI6 vertex (their Eqs. (4.3) and (4.4)), the latter leaving those
851 : // dual shape functions identically zero. Biorthogonalize instead against the locally quadratic
852 : // transformed basis Ntilde = T N of their Sec. 4.4.1, in which each vertex absorbs a fraction alpha
853 : // of its adjacent mid-edge shapes. That replaces D by T^-1 diag(T d), which for this T is the sparse
854 : // update below. alpha = 1/5 is recommended there, and makes the weights strictly positive (QUAD8
855 : // 1/5 and 4/5, TRI6 1/15 and 1/10) while preserving the partition of unity. T = I elsewhere.
856 : // SECOND order on TRI7 is the TRI6 basis, with no degree of freedom at the centroid node, and
857 : // L2_LAGRANGE shares the LAGRANGE shape functions.
858 8892 : if (_elem && (get_family() == LAGRANGE || get_family() == L2_LAGRANGE) &&
859 4796 : (((_elem->type() == TRI6 || _elem->type() == TRI7) && sz == 6) ||
860 1583 : (_elem->type() == QUAD8 && sz == 8)))
861 : {
862 19 : const Real alpha = Real(1)/5;
863 : // Mid-edge nodes are the trailing indices, and only vertex entries are written, so D(m,m) here
864 : // is never a value an earlier iteration modified.
865 1058 : for (const auto m : make_range(_elem->n_vertices(), sz))
866 2568 : for (const auto v : make_range(_elem->n_second_order_adjacent_vertices(m)))
867 : {
868 1344 : const auto vertex = _elem->second_order_adjacent_vertex(m, v);
869 1344 : D(vertex, vertex) += alpha*D(m,m);
870 1464 : D(vertex, m) -= alpha*D(m,m);
871 : }
872 : }
873 :
874 : // dual_coeff = A^-1*D
875 124716 : for (const auto j : index_range(phi_vals))
876 : {
877 117550 : DenseVector<Real> Dcol(sz), coeffcol(sz);
878 4570692 : for (const auto i : index_range(phi_vals))
879 5125890 : Dcol(i) = D(i, j);
880 117550 : A.cholesky_solve(Dcol, coeffcol);
881 :
882 4570692 : for (const auto row : index_range(phi_vals))
883 5125890 : dual_coeff(row, j)=coeffcol(row);
884 : }
885 7166 : }
886 :
887 : template <>
888 7166 : void FEGenericBase<Real>::compute_dual_shape_functions ()
889 : {
890 : // Start logging the shape function computation
891 1476 : LOG_SCOPE("compute_dual_shape_functions()", "FE");
892 :
893 : // The dual coeffs matrix should have the same size as phi
894 492 : libmesh_assert(dual_coeff.m() == phi.size());
895 492 : libmesh_assert(dual_coeff.n() == phi.size());
896 :
897 : // initialize dual basis
898 124716 : for (const auto j : index_range(phi))
899 7118264 : for (const auto qp : index_range(phi[j]))
900 : {
901 7507135 : dual_phi[j][qp] = 0;
902 7000714 : if (calculate_dphi)
903 1517811 : dual_dphi[j][qp] = 0;
904 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
905 7000714 : if (calculate_d2phi)
906 1509279 : dual_d2phi[j][qp] = 0;
907 : #endif
908 : }
909 :
910 : // compute dual basis
911 124716 : for (const auto j : index_range(phi))
912 4570692 : for (const auto i : index_range(phi))
913 413391776 : for (const auto qp : index_range(phi[j]))
914 : {
915 535941350 : dual_phi[j][qp] += dual_coeff(i, j) * phi[i][qp];
916 408938634 : if (calculate_dphi)
917 190485018 : dual_dphi[j][qp] += dual_coeff(i, j) * dphi[i][qp];
918 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
919 408938634 : if (calculate_d2phi)
920 911913159 : dual_d2phi[j][qp] += dual_coeff(i, j) * d2phi[i][qp];
921 : #endif
922 : }
923 7166 : }
924 :
925 : template <typename OutputType>
926 0 : void FEGenericBase<OutputType>::print_phi(std::ostream & os) const
927 : {
928 0 : for (auto i : index_range(phi))
929 0 : for (auto j : index_range(phi[i]))
930 0 : os << " phi[" << i << "][" << j << "]=" << phi[i][j] << std::endl;
931 0 : }
932 :
933 : template <typename OutputType>
934 0 : void FEGenericBase<OutputType>::print_dual_phi(std::ostream & os) const
935 : {
936 0 : for (auto i : index_range(dual_phi))
937 0 : for (auto j : index_range(dual_phi[i]))
938 0 : os << " dual_phi[" << i << "][" << j << "]=" << dual_phi[i][j] << std::endl;
939 0 : }
940 :
941 :
942 :
943 :
944 : template <typename OutputType>
945 0 : void FEGenericBase<OutputType>::print_dphi(std::ostream & os) const
946 : {
947 0 : for (auto i : index_range(dphi))
948 0 : for (auto j : index_range(dphi[i]))
949 0 : os << " dphi[" << i << "][" << j << "]=" << dphi[i][j];
950 0 : }
951 :
952 : template <typename OutputType>
953 0 : void FEGenericBase<OutputType>::print_dual_dphi(std::ostream & os) const
954 : {
955 0 : for (auto i : index_range(dphi))
956 0 : for (auto j : index_range(dphi[i]))
957 0 : os << " dual_dphi[" << i << "][" << j << "]=" << dual_dphi[i][j];
958 0 : }
959 :
960 :
961 :
962 : template <typename OutputType>
963 347326360 : void FEGenericBase<OutputType>::determine_calculations()
964 : {
965 347326360 : this->calculations_started = true;
966 :
967 : // If the user did not explicitly pre-request something (or nothing)
968 : // to be computed, then we throw an error here.
969 30187673 : bool requested_ok =
970 337858336 : this->calculate_nothing || this->calculate_phi || this->calculate_dphi ||
971 379517965 : this->calculate_dphiref || this->calculate_curl_phi || this->calculate_div_phi ||
972 635208 : this->calculate_map;
973 :
974 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
975 30187673 : requested_ok = requested_ok || this->calculate_d2phi;
976 : #endif
977 :
978 30187673 : libmesh_error_msg_if(
979 : !requested_ok,
980 : "You must call one or more of the FE accessors "
981 : "(e.g. get_phi(), get_dphi(), get_nothing()) "
982 : "_before_ calling reinit()!");
983 :
984 : // Request whichever terms are necessary from the FEMap
985 347326360 : if (this->calculate_phi)
986 323543169 : this->_fe_trans->init_map_phi(*this);
987 :
988 347326360 : if (this->calculate_dphiref)
989 228344994 : this->_fe_trans->init_map_dphi(*this);
990 :
991 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
992 347326360 : if (this->calculate_d2phi)
993 61505085 : this->_fe_trans->init_map_d2phi(*this);
994 : #endif //LIBMESH_ENABLE_SECOND_DERIVATIVES
995 347326360 : }
996 :
997 :
998 :
999 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
1000 :
1001 :
1002 : template <typename OutputType>
1003 0 : void FEGenericBase<OutputType>::print_d2phi(std::ostream & os) const
1004 : {
1005 0 : for (auto i : index_range(dphi))
1006 0 : for (auto j : index_range(dphi[i]))
1007 0 : os << " d2phi[" << i << "][" << j << "]=" << d2phi[i][j];
1008 0 : }
1009 :
1010 : template <typename OutputType>
1011 0 : void FEGenericBase<OutputType>::print_dual_d2phi(std::ostream & os) const
1012 : {
1013 0 : for (auto i : index_range(dual_d2phi))
1014 0 : for (auto j : index_range(dual_d2phi[i]))
1015 0 : os << " dual_d2phi[" << i << "][" << j << "]=" << dual_d2phi[i][j];
1016 0 : }
1017 :
1018 : #endif
1019 :
1020 :
1021 :
1022 : #ifdef LIBMESH_ENABLE_AMR
1023 :
1024 : template <typename OutputType>
1025 : void
1026 0 : FEGenericBase<OutputType>::coarsened_dof_values(const NumericVector<Number> & old_vector,
1027 : const DofMap & dof_map,
1028 : const Elem * elem,
1029 : DenseVector<Number> & Ue,
1030 : const unsigned int var,
1031 : const bool use_old_dof_indices)
1032 : {
1033 : // Side/edge local DOF indices
1034 0 : std::vector<unsigned int> new_side_dofs, old_side_dofs;
1035 :
1036 : // FIXME: what about 2D shells in 3D space?
1037 0 : unsigned int dim = elem->dim();
1038 :
1039 : // Cache n_children(); it's a virtual call but it's const.
1040 0 : const unsigned int n_children = elem->n_children();
1041 :
1042 : // We use local FE objects for now
1043 : // FIXME: we should use more, external objects instead for efficiency
1044 0 : const FEType & base_fe_type = dof_map.variable_type(var);
1045 0 : std::unique_ptr<FEGenericBase<OutputShape>> fe
1046 : (FEGenericBase<OutputShape>::build(dim, base_fe_type));
1047 0 : std::unique_ptr<FEGenericBase<OutputShape>> fe_coarse
1048 : (FEGenericBase<OutputShape>::build(dim, base_fe_type));
1049 :
1050 0 : std::unique_ptr<QBase> qrule (base_fe_type.default_quadrature_rule(dim));
1051 0 : std::unique_ptr<QBase> qedgerule (base_fe_type.default_quadrature_rule(1));
1052 0 : std::unique_ptr<QBase> qsiderule (base_fe_type.default_quadrature_rule(dim-1));
1053 0 : std::vector<Point> coarse_qpoints;
1054 :
1055 : // The values of the shape functions at the quadrature
1056 : // points
1057 0 : const std::vector<std::vector<OutputShape>> & phi_values =
1058 : fe->get_phi();
1059 0 : const std::vector<std::vector<OutputShape>> & phi_coarse =
1060 : fe_coarse->get_phi();
1061 :
1062 : // The gradients of the shape functions at the quadrature
1063 : // points on the child element.
1064 0 : const std::vector<std::vector<OutputGradient>> * dphi_values =
1065 : nullptr;
1066 0 : const std::vector<std::vector<OutputGradient>> * dphi_coarse =
1067 : nullptr;
1068 :
1069 0 : const FEContinuity cont = fe->get_continuity();
1070 :
1071 0 : if (cont == C_ONE)
1072 : {
1073 : const std::vector<std::vector<OutputGradient>> &
1074 0 : ref_dphi_values = fe->get_dphi();
1075 0 : dphi_values = &ref_dphi_values;
1076 : const std::vector<std::vector<OutputGradient>> &
1077 0 : ref_dphi_coarse = fe_coarse->get_dphi();
1078 0 : dphi_coarse = &ref_dphi_coarse;
1079 : }
1080 :
1081 : // The Jacobian * quadrature weight at the quadrature points
1082 0 : const std::vector<Real> & JxW =
1083 0 : fe->get_JxW();
1084 :
1085 : // The XYZ locations of the quadrature points on the
1086 : // child element
1087 0 : const std::vector<Point> & xyz_values =
1088 0 : fe->get_xyz();
1089 :
1090 : // Number of nodes on parent element
1091 0 : const unsigned int n_nodes = elem->n_nodes();
1092 :
1093 : // Number of dofs on parent element
1094 : const unsigned int new_n_dofs =
1095 0 : FEInterface::n_dofs(base_fe_type, elem->max_descendant_p_level(), elem);
1096 :
1097 : // Fixed vs. free DoFs on edge/face projections
1098 0 : std::vector<char> dof_is_fixed(new_n_dofs, false); // bools
1099 0 : std::vector<int> free_dof(new_n_dofs, 0);
1100 :
1101 0 : DenseMatrix<Real> Ke;
1102 0 : DenseVector<Number> Fe;
1103 0 : Ue.resize(new_n_dofs); Ue.zero();
1104 :
1105 :
1106 : // When coarsening, in general, we need a series of
1107 : // projections to ensure a unique and continuous
1108 : // solution. We start by interpolating nodes, then
1109 : // hold those fixed and project edges, then
1110 : // hold those fixed and project faces, then
1111 : // hold those fixed and project interiors
1112 :
1113 : // Copy node values first
1114 : {
1115 0 : std::vector<dof_id_type> node_dof_indices;
1116 0 : if (use_old_dof_indices)
1117 0 : dof_map.old_dof_indices (elem, node_dof_indices, var);
1118 : else
1119 0 : dof_map.dof_indices (elem, node_dof_indices, var);
1120 :
1121 0 : unsigned int current_dof = 0;
1122 0 : for (unsigned int n=0; n!= n_nodes; ++n)
1123 : {
1124 : // FIXME: this should go through the DofMap,
1125 : // not duplicate dof_indices code badly!
1126 : const unsigned int my_nc =
1127 0 : FEInterface::n_dofs_at_node (base_fe_type, elem->max_descendant_p_level(), elem, n);
1128 0 : if (!elem->is_vertex(n))
1129 : {
1130 0 : current_dof += my_nc;
1131 0 : continue;
1132 : }
1133 :
1134 : // We're assuming here that child n shares vertex n,
1135 : // which is wrong on non-simplices right now
1136 : // ... but this code isn't necessary except on elements
1137 : // where p refinement creates more vertex dofs; we have
1138 : // no such elements yet.
1139 0 : int extra_order = 0;
1140 : // if (elem->child_ptr(n)->p_level() < elem->p_level())
1141 : // extra_order = elem->child_ptr(n)->p_level();
1142 : const unsigned int nc =
1143 0 : FEInterface::n_dofs_at_node (base_fe_type, extra_order, elem, n);
1144 0 : for (unsigned int i=0; i!= nc; ++i)
1145 : {
1146 0 : Ue(current_dof) =
1147 0 : old_vector(node_dof_indices[current_dof]);
1148 0 : dof_is_fixed[current_dof] = true;
1149 0 : current_dof++;
1150 : }
1151 : }
1152 : }
1153 :
1154 0 : FEType fe_type = base_fe_type, temp_fe_type;
1155 0 : fe_type.order = fe_type.order + elem->max_descendant_p_level();
1156 :
1157 : // In 3D, project any edge values next
1158 0 : if (dim > 2 && cont != DISCONTINUOUS)
1159 0 : for (auto e : elem->edge_index_range())
1160 : {
1161 0 : FEInterface::dofs_on_edge(elem, dim, fe_type,
1162 : e, new_side_dofs);
1163 :
1164 : const unsigned int n_new_side_dofs =
1165 0 : cast_int<unsigned int>(new_side_dofs.size());
1166 :
1167 : // Some edge dofs are on nodes and already
1168 : // fixed, others are free to calculate
1169 0 : unsigned int free_dofs = 0;
1170 0 : for (unsigned int i=0; i != n_new_side_dofs; ++i)
1171 0 : if (!dof_is_fixed[new_side_dofs[i]])
1172 0 : free_dof[free_dofs++] = i;
1173 0 : Ke.resize (free_dofs, free_dofs); Ke.zero();
1174 0 : Fe.resize (free_dofs); Fe.zero();
1175 : // The new edge coefficients
1176 0 : DenseVector<Number> Uedge(free_dofs);
1177 :
1178 : // Add projection terms from each child sharing
1179 : // this edge
1180 0 : for (unsigned int c=0; c != n_children; ++c)
1181 : {
1182 0 : if (!elem->is_child_on_edge(c,e))
1183 0 : continue;
1184 0 : const Elem * child = elem->child_ptr(c);
1185 :
1186 0 : std::vector<dof_id_type> child_dof_indices;
1187 0 : if (use_old_dof_indices)
1188 0 : dof_map.old_dof_indices (child,
1189 : child_dof_indices, var);
1190 : else
1191 0 : dof_map.dof_indices (child,
1192 : child_dof_indices, var);
1193 : const unsigned int child_n_dofs =
1194 : cast_int<unsigned int>
1195 0 : (child_dof_indices.size());
1196 :
1197 0 : temp_fe_type = base_fe_type;
1198 0 : temp_fe_type.order = temp_fe_type.order + child->p_level();
1199 :
1200 0 : FEInterface::dofs_on_edge(child, dim,
1201 : temp_fe_type, e, old_side_dofs);
1202 :
1203 : // Initialize both child and parent FE data
1204 : // on the child's edge
1205 0 : fe->attach_quadrature_rule (qedgerule.get());
1206 0 : fe->edge_reinit (child, e);
1207 0 : const unsigned int n_qp = qedgerule->n_points();
1208 :
1209 0 : FEMap::inverse_map (dim, elem, xyz_values,
1210 : coarse_qpoints);
1211 :
1212 0 : fe_coarse->reinit(elem, &coarse_qpoints);
1213 :
1214 : // Loop over the quadrature points
1215 0 : for (unsigned int qp=0; qp<n_qp; qp++)
1216 : {
1217 : // solution value at the quadrature point
1218 0 : OutputNumber fineval = libMesh::zero;
1219 : // solution grad at the quadrature point
1220 0 : OutputNumberGradient finegrad;
1221 :
1222 : // Sum the solution values * the DOF
1223 : // values at the quadrature point to
1224 : // get the solution value and gradient.
1225 0 : for (unsigned int i=0; i<child_n_dofs;
1226 : i++)
1227 : {
1228 0 : fineval +=
1229 0 : (old_vector(child_dof_indices[i])*
1230 0 : phi_values[i][qp]);
1231 0 : if (cont == C_ONE)
1232 0 : finegrad += (*dphi_values)[i][qp] *
1233 0 : old_vector(child_dof_indices[i]);
1234 : }
1235 :
1236 : // Form edge projection matrix
1237 0 : for (unsigned int sidei=0, freei=0; sidei != n_new_side_dofs; ++sidei)
1238 : {
1239 0 : unsigned int i = new_side_dofs[sidei];
1240 : // fixed DoFs aren't test functions
1241 0 : if (dof_is_fixed[i])
1242 0 : continue;
1243 0 : for (unsigned int sidej=0, freej=0; sidej != n_new_side_dofs; ++sidej)
1244 : {
1245 0 : unsigned int j =
1246 : new_side_dofs[sidej];
1247 0 : if (dof_is_fixed[j])
1248 0 : Fe(freei) -=
1249 0 : TensorTools::inner_product(phi_coarse[i][qp],
1250 0 : phi_coarse[j][qp]) *
1251 0 : JxW[qp] * Ue(j);
1252 : else
1253 0 : Ke(freei,freej) +=
1254 0 : TensorTools::inner_product(phi_coarse[i][qp],
1255 0 : phi_coarse[j][qp]) *
1256 : JxW[qp];
1257 0 : if (cont == C_ONE)
1258 : {
1259 0 : if (dof_is_fixed[j])
1260 0 : Fe(freei) -=
1261 0 : TensorTools::inner_product((*dphi_coarse)[i][qp],
1262 0 : (*dphi_coarse)[j][qp]) *
1263 0 : JxW[qp] * Ue(j);
1264 : else
1265 0 : Ke(freei,freej) +=
1266 0 : TensorTools::inner_product((*dphi_coarse)[i][qp],
1267 0 : (*dphi_coarse)[j][qp]) *
1268 : JxW[qp];
1269 : }
1270 0 : if (!dof_is_fixed[j])
1271 0 : freej++;
1272 : }
1273 0 : Fe(freei) += TensorTools::inner_product(phi_coarse[i][qp],
1274 0 : fineval) * JxW[qp];
1275 0 : if (cont == C_ONE)
1276 0 : Fe(freei) +=
1277 0 : TensorTools::inner_product(finegrad, (*dphi_coarse)[i][qp]) * JxW[qp];
1278 0 : freei++;
1279 : }
1280 : }
1281 : }
1282 0 : Ke.cholesky_solve(Fe, Uedge);
1283 :
1284 : // Transfer new edge solutions to element
1285 0 : for (unsigned int i=0; i != free_dofs; ++i)
1286 : {
1287 0 : Number & ui = Ue(new_side_dofs[free_dof[i]]);
1288 0 : libmesh_assert(std::abs(ui) < TOLERANCE ||
1289 : std::abs(ui - Uedge(i)) < TOLERANCE);
1290 0 : ui = Uedge(i);
1291 0 : dof_is_fixed[new_side_dofs[free_dof[i]]] = true;
1292 : }
1293 : }
1294 :
1295 : // Project any side values (edges in 2D, faces in 3D)
1296 0 : if (dim > 1 && cont != DISCONTINUOUS)
1297 0 : for (auto s : elem->side_index_range())
1298 : {
1299 0 : FEInterface::dofs_on_side(elem, dim, fe_type,
1300 : s, new_side_dofs);
1301 :
1302 : const unsigned int n_new_side_dofs =
1303 0 : cast_int<unsigned int>(new_side_dofs.size());
1304 :
1305 : // Some side dofs are on nodes/edges and already
1306 : // fixed, others are free to calculate
1307 0 : unsigned int free_dofs = 0;
1308 0 : for (unsigned int i=0; i != n_new_side_dofs; ++i)
1309 0 : if (!dof_is_fixed[new_side_dofs[i]])
1310 0 : free_dof[free_dofs++] = i;
1311 0 : Ke.resize (free_dofs, free_dofs); Ke.zero();
1312 0 : Fe.resize (free_dofs); Fe.zero();
1313 : // The new side coefficients
1314 0 : DenseVector<Number> Uside(free_dofs);
1315 :
1316 : // Add projection terms from each child sharing
1317 : // this side
1318 0 : for (unsigned int c=0; c != n_children; ++c)
1319 : {
1320 0 : if (!elem->is_child_on_side(c,s))
1321 0 : continue;
1322 0 : const Elem * child = elem->child_ptr(c);
1323 :
1324 0 : std::vector<dof_id_type> child_dof_indices;
1325 0 : if (use_old_dof_indices)
1326 0 : dof_map.old_dof_indices (child,
1327 : child_dof_indices, var);
1328 : else
1329 0 : dof_map.dof_indices (child,
1330 : child_dof_indices, var);
1331 : const unsigned int child_n_dofs =
1332 : cast_int<unsigned int>
1333 0 : (child_dof_indices.size());
1334 :
1335 0 : temp_fe_type = base_fe_type;
1336 0 : temp_fe_type.order = temp_fe_type.order + child->p_level();
1337 :
1338 0 : FEInterface::dofs_on_side(child, dim,
1339 : temp_fe_type, s, old_side_dofs);
1340 :
1341 : // Initialize both child and parent FE data
1342 : // on the child's side
1343 0 : fe->attach_quadrature_rule (qsiderule.get());
1344 0 : fe->reinit (child, s);
1345 0 : const unsigned int n_qp = qsiderule->n_points();
1346 :
1347 0 : FEMap::inverse_map (dim, elem, xyz_values,
1348 : coarse_qpoints);
1349 :
1350 0 : fe_coarse->reinit(elem, &coarse_qpoints);
1351 :
1352 : // Loop over the quadrature points
1353 0 : for (unsigned int qp=0; qp<n_qp; qp++)
1354 : {
1355 : // solution value at the quadrature point
1356 0 : OutputNumber fineval = libMesh::zero;
1357 : // solution grad at the quadrature point
1358 0 : OutputNumberGradient finegrad;
1359 :
1360 : // Sum the solution values * the DOF
1361 : // values at the quadrature point to
1362 : // get the solution value and gradient.
1363 0 : for (unsigned int i=0; i<child_n_dofs;
1364 : i++)
1365 : {
1366 0 : fineval +=
1367 0 : old_vector(child_dof_indices[i]) *
1368 0 : phi_values[i][qp];
1369 0 : if (cont == C_ONE)
1370 0 : finegrad += (*dphi_values)[i][qp] *
1371 0 : old_vector(child_dof_indices[i]);
1372 : }
1373 :
1374 : // Form side projection matrix
1375 0 : for (unsigned int sidei=0, freei=0; sidei != n_new_side_dofs; ++sidei)
1376 : {
1377 0 : unsigned int i = new_side_dofs[sidei];
1378 : // fixed DoFs aren't test functions
1379 0 : if (dof_is_fixed[i])
1380 0 : continue;
1381 0 : for (unsigned int sidej=0, freej=0; sidej != n_new_side_dofs; ++sidej)
1382 : {
1383 0 : unsigned int j =
1384 : new_side_dofs[sidej];
1385 0 : if (dof_is_fixed[j])
1386 0 : Fe(freei) -=
1387 0 : TensorTools::inner_product(phi_coarse[i][qp],
1388 0 : phi_coarse[j][qp]) *
1389 0 : JxW[qp] * Ue(j);
1390 : else
1391 0 : Ke(freei,freej) +=
1392 0 : TensorTools::inner_product(phi_coarse[i][qp],
1393 0 : phi_coarse[j][qp]) *
1394 : JxW[qp];
1395 0 : if (cont == C_ONE)
1396 : {
1397 0 : if (dof_is_fixed[j])
1398 0 : Fe(freei) -=
1399 0 : TensorTools::inner_product((*dphi_coarse)[i][qp],
1400 0 : (*dphi_coarse)[j][qp]) *
1401 0 : JxW[qp] * Ue(j);
1402 : else
1403 0 : Ke(freei,freej) +=
1404 0 : TensorTools::inner_product((*dphi_coarse)[i][qp],
1405 0 : (*dphi_coarse)[j][qp]) *
1406 : JxW[qp];
1407 : }
1408 0 : if (!dof_is_fixed[j])
1409 0 : freej++;
1410 : }
1411 0 : Fe(freei) += TensorTools::inner_product(fineval, phi_coarse[i][qp]) * JxW[qp];
1412 0 : if (cont == C_ONE)
1413 0 : Fe(freei) +=
1414 0 : TensorTools::inner_product(finegrad, (*dphi_coarse)[i][qp]) * JxW[qp];
1415 0 : freei++;
1416 : }
1417 : }
1418 : }
1419 0 : Ke.cholesky_solve(Fe, Uside);
1420 :
1421 : // Transfer new side solutions to element
1422 0 : for (unsigned int i=0; i != free_dofs; ++i)
1423 : {
1424 0 : Number & ui = Ue(new_side_dofs[free_dof[i]]);
1425 0 : libmesh_assert(std::abs(ui) < TOLERANCE ||
1426 : std::abs(ui - Uside(i)) < TOLERANCE);
1427 0 : ui = Uside(i);
1428 0 : dof_is_fixed[new_side_dofs[free_dof[i]]] = true;
1429 : }
1430 : }
1431 :
1432 : // Project the interior values, finally
1433 :
1434 : // Some interior dofs are on nodes/edges/sides and
1435 : // already fixed, others are free to calculate
1436 0 : unsigned int free_dofs = 0;
1437 0 : for (unsigned int i=0; i != new_n_dofs; ++i)
1438 0 : if (!dof_is_fixed[i])
1439 0 : free_dof[free_dofs++] = i;
1440 0 : Ke.resize (free_dofs, free_dofs); Ke.zero();
1441 0 : Fe.resize (free_dofs); Fe.zero();
1442 : // The new interior coefficients
1443 0 : DenseVector<Number> Uint(free_dofs);
1444 :
1445 : // Add projection terms from each child
1446 0 : for (auto & child : elem->child_ref_range())
1447 : {
1448 0 : std::vector<dof_id_type> child_dof_indices;
1449 0 : if (use_old_dof_indices)
1450 0 : dof_map.old_dof_indices (&child,
1451 : child_dof_indices, var);
1452 : else
1453 0 : dof_map.dof_indices (&child,
1454 : child_dof_indices, var);
1455 : const unsigned int child_n_dofs =
1456 : cast_int<unsigned int>
1457 0 : (child_dof_indices.size());
1458 :
1459 : // Initialize both child and parent FE data
1460 : // on the child's quadrature points
1461 0 : fe->attach_quadrature_rule (qrule.get());
1462 0 : fe->reinit (&child);
1463 0 : const unsigned int n_qp = qrule->n_points();
1464 :
1465 0 : FEMap::inverse_map (dim, elem, xyz_values, coarse_qpoints);
1466 :
1467 0 : fe_coarse->reinit(elem, &coarse_qpoints);
1468 :
1469 : // Loop over the quadrature points
1470 0 : for (unsigned int qp=0; qp<n_qp; qp++)
1471 : {
1472 : // solution value at the quadrature point
1473 0 : OutputNumber fineval = libMesh::zero;
1474 : // solution grad at the quadrature point
1475 0 : OutputNumberGradient finegrad;
1476 :
1477 : // Sum the solution values * the DOF
1478 : // values at the quadrature point to
1479 : // get the solution value and gradient.
1480 0 : for (unsigned int i=0; i<child_n_dofs; i++)
1481 : {
1482 0 : fineval +=
1483 0 : (old_vector(child_dof_indices[i]) *
1484 0 : phi_values[i][qp]);
1485 0 : if (cont == C_ONE)
1486 0 : finegrad += (*dphi_values)[i][qp] *
1487 0 : old_vector(child_dof_indices[i]);
1488 : }
1489 :
1490 : // Form interior projection matrix
1491 0 : for (unsigned int i=0, freei=0;
1492 0 : i != new_n_dofs; ++i)
1493 : {
1494 : // fixed DoFs aren't test functions
1495 0 : if (dof_is_fixed[i])
1496 0 : continue;
1497 0 : for (unsigned int j=0, freej=0; j !=
1498 : new_n_dofs; ++j)
1499 : {
1500 0 : if (dof_is_fixed[j])
1501 0 : Fe(freei) -=
1502 0 : TensorTools::inner_product(phi_coarse[i][qp],
1503 0 : phi_coarse[j][qp]) *
1504 0 : JxW[qp] * Ue(j);
1505 : else
1506 0 : Ke(freei,freej) +=
1507 0 : TensorTools::inner_product(phi_coarse[i][qp],
1508 0 : phi_coarse[j][qp]) *
1509 : JxW[qp];
1510 0 : if (cont == C_ONE)
1511 : {
1512 0 : if (dof_is_fixed[j])
1513 0 : Fe(freei) -=
1514 0 : TensorTools::inner_product((*dphi_coarse)[i][qp],
1515 0 : (*dphi_coarse)[j][qp]) *
1516 0 : JxW[qp] * Ue(j);
1517 : else
1518 0 : Ke(freei,freej) +=
1519 0 : TensorTools::inner_product((*dphi_coarse)[i][qp],
1520 0 : (*dphi_coarse)[j][qp]) *
1521 : JxW[qp];
1522 : }
1523 0 : if (!dof_is_fixed[j])
1524 0 : freej++;
1525 : }
1526 0 : Fe(freei) += TensorTools::inner_product(phi_coarse[i][qp], fineval) *
1527 : JxW[qp];
1528 0 : if (cont == C_ONE)
1529 0 : Fe(freei) += TensorTools::inner_product(finegrad, (*dphi_coarse)[i][qp]) * JxW[qp];
1530 0 : freei++;
1531 : }
1532 : }
1533 : }
1534 0 : Ke.cholesky_solve(Fe, Uint);
1535 :
1536 : // Transfer new interior solutions to element
1537 0 : for (unsigned int i=0; i != free_dofs; ++i)
1538 : {
1539 0 : Number & ui = Ue(free_dof[i]);
1540 0 : libmesh_assert(std::abs(ui) < TOLERANCE ||
1541 : std::abs(ui - Uint(i)) < TOLERANCE);
1542 0 : ui = Uint(i);
1543 : // We should be fixing all dofs by now; no need to keep track of
1544 : // that unless we're debugging
1545 : #ifndef NDEBUG
1546 0 : dof_is_fixed[free_dof[i]] = true;
1547 : #endif
1548 : }
1549 :
1550 : #ifndef NDEBUG
1551 : // Make sure every DoF got reached!
1552 0 : for (unsigned int i=0; i != new_n_dofs; ++i)
1553 0 : libmesh_assert(dof_is_fixed[i]);
1554 : #endif
1555 0 : }
1556 :
1557 :
1558 :
1559 : template <typename OutputType>
1560 : void
1561 0 : FEGenericBase<OutputType>::coarsened_dof_values(const NumericVector<Number> & old_vector,
1562 : const DofMap & dof_map,
1563 : const Elem * elem,
1564 : DenseVector<Number> & Ue,
1565 : const bool use_old_dof_indices)
1566 : {
1567 0 : Ue.resize(0);
1568 :
1569 0 : for (auto v : make_range(dof_map.n_variables()))
1570 : {
1571 0 : DenseVector<Number> Usub;
1572 :
1573 0 : coarsened_dof_values(old_vector, dof_map, elem, Usub,
1574 : v, use_old_dof_indices);
1575 :
1576 0 : Ue.append (Usub);
1577 : }
1578 0 : }
1579 :
1580 :
1581 :
1582 : template <typename OutputType>
1583 : void
1584 677998 : FEGenericBase<OutputType>::compute_proj_constraints (DofConstraints & constraints,
1585 : DofMap & dof_map,
1586 : const unsigned int variable_number,
1587 : const Elem * elem)
1588 : {
1589 58286 : libmesh_assert(elem);
1590 :
1591 677998 : const unsigned int Dim = elem->dim();
1592 :
1593 : // Only constrain elements in 2,3D.
1594 677998 : if (Dim == 1)
1595 103526 : return;
1596 :
1597 : // Only constrain active elements with this method
1598 58286 : if (!elem->active())
1599 9245 : return;
1600 :
1601 574472 : const Variable & var = dof_map.variable(variable_number);
1602 49041 : const FEType & base_fe_type = var.type();
1603 574472 : const bool add_p_level = base_fe_type.p_refinement;
1604 :
1605 : // Construct FE objects for this element and its neighbors.
1606 574472 : std::unique_ptr<FEGenericBase<OutputShape>> my_fe
1607 : (FEGenericBase<OutputShape>::build(Dim, base_fe_type));
1608 49041 : my_fe->add_p_level_in_reinit(add_p_level);
1609 574472 : const FEContinuity cont = my_fe->get_continuity();
1610 :
1611 : // We don't need to constrain discontinuous elements
1612 574472 : if (cont == DISCONTINUOUS)
1613 0 : return;
1614 49041 : libmesh_assert (cont == C_ZERO || cont == C_ONE ||
1615 : cont == SIDE_DISCONTINUOUS);
1616 :
1617 : // this would require some generalisation:
1618 : // - e.g. the 'my_fe'-object needs generalisation
1619 : // - due to lack of one-to-one correspondence of DOFs and nodes,
1620 : // this doesn't work easily.
1621 131556 : if (elem->infinite())
1622 0 : libmesh_not_implemented();
1623 :
1624 623513 : std::unique_ptr<FEGenericBase<OutputShape>> neigh_fe
1625 : (FEGenericBase<OutputShape>::build(Dim, base_fe_type));
1626 49041 : neigh_fe->add_p_level_in_reinit(add_p_level);
1627 :
1628 623513 : QGauss my_qface(Dim-1, base_fe_type.default_quadrature_order());
1629 574472 : my_fe->attach_quadrature_rule (&my_qface);
1630 98082 : std::vector<Point> neigh_qface;
1631 :
1632 131556 : const std::vector<Real> & JxW = my_fe->get_JxW();
1633 131556 : const std::vector<Point> & q_point = my_fe->get_xyz();
1634 49041 : const std::vector<std::vector<OutputShape>> & phi = my_fe->get_phi();
1635 49041 : const std::vector<std::vector<OutputShape>> & neigh_phi =
1636 : neigh_fe->get_phi();
1637 49041 : const std::vector<Point> * face_normals = nullptr;
1638 49041 : const std::vector<std::vector<OutputGradient>> * dphi = nullptr;
1639 49041 : const std::vector<std::vector<OutputGradient>> * neigh_dphi = nullptr;
1640 :
1641 98082 : std::vector<dof_id_type> my_dof_indices, neigh_dof_indices;
1642 98082 : std::vector<unsigned int> my_side_dofs, neigh_side_dofs;
1643 :
1644 574472 : if (cont == C_ONE)
1645 : {
1646 7274 : const std::vector<Point> & ref_face_normals =
1647 7490 : my_fe->get_normals();
1648 3637 : face_normals = &ref_face_normals;
1649 3637 : const std::vector<std::vector<OutputGradient>> & ref_dphi =
1650 : my_fe->get_dphi();
1651 3637 : dphi = &ref_dphi;
1652 3637 : const std::vector<std::vector<OutputGradient>> & ref_neigh_dphi =
1653 : neigh_fe->get_dphi();
1654 3637 : neigh_dphi = &ref_neigh_dphi;
1655 : }
1656 :
1657 672554 : DenseMatrix<Real> Ke;
1658 574472 : DenseVector<Real> Fe;
1659 147123 : std::vector<DenseVector<Real>> Ue;
1660 :
1661 : // Look at the element faces. Check to see if we need to
1662 : // build constraints.
1663 2746506 : for (auto s : elem->side_index_range())
1664 : {
1665 : // Get pointers to the element's neighbor.
1666 2172034 : const Elem * neigh = elem->neighbor_ptr(s);
1667 :
1668 2172034 : if (!neigh)
1669 231571 : continue;
1670 :
1671 1919275 : if (!var.active_on_subdomain(neigh->subdomain_id()))
1672 1636 : continue;
1673 :
1674 : // h refinement constraints:
1675 : // constrain dofs shared between
1676 : // this element and ones coarser
1677 : // than this element.
1678 1917495 : if (neigh->level() < elem->level())
1679 : {
1680 115192 : unsigned int s_neigh = neigh->which_neighbor_am_i(elem);
1681 10178 : libmesh_assert_less (s_neigh, neigh->n_neighbors());
1682 :
1683 : // Find the minimum p level; we build the h constraint
1684 : // matrix with this and then constrain away all higher p
1685 : // DoFs.
1686 10178 : libmesh_assert(neigh->active());
1687 135548 : const unsigned int min_p_level = add_p_level *
1688 135548 : std::min(elem->p_level(), neigh->p_level());
1689 : // we may need to make the FE objects reinit with the
1690 : // minimum shared p_level
1691 115192 : const unsigned int old_elem_level = add_p_level * elem->p_level();
1692 115192 : if (old_elem_level != min_p_level)
1693 360 : my_fe->set_fe_order(my_fe->get_fe_type().order.get_order() + min_p_level - old_elem_level);
1694 115192 : const unsigned int old_neigh_level = add_p_level * neigh->p_level();
1695 115192 : if (old_neigh_level != min_p_level)
1696 0 : neigh_fe->set_fe_order(neigh_fe->get_fe_type().order.get_order() + min_p_level - old_neigh_level);
1697 :
1698 115192 : my_fe->reinit(elem, s);
1699 :
1700 : // This function gets called element-by-element, so there
1701 : // will be a lot of memory allocation going on. We can
1702 : // at least minimize this for the case of the dof indices
1703 : // by efficiently preallocating the requisite storage.
1704 : // n_nodes is not necessarily n_dofs, but it is better
1705 : // than nothing!
1706 115192 : my_dof_indices.reserve (elem->n_nodes());
1707 115192 : neigh_dof_indices.reserve (neigh->n_nodes());
1708 :
1709 115192 : dof_map.dof_indices (elem, my_dof_indices,
1710 : variable_number,
1711 : min_p_level);
1712 115192 : dof_map.dof_indices (neigh, neigh_dof_indices,
1713 : variable_number,
1714 : min_p_level);
1715 :
1716 10178 : const unsigned int n_qp = my_qface.n_points();
1717 :
1718 115192 : FEMap::inverse_map (Dim, neigh, q_point, neigh_qface);
1719 :
1720 115192 : neigh_fe->reinit(neigh, &neigh_qface);
1721 :
1722 : // We're only concerned with DOFs whose values (and/or first
1723 : // derivatives for C1 elements) are supported on side nodes
1724 115192 : FEType elem_fe_type = base_fe_type;
1725 115192 : if (old_elem_level != min_p_level)
1726 360 : elem_fe_type.order = base_fe_type.order.get_order() + min_p_level - old_elem_level;
1727 115192 : FEType neigh_fe_type = base_fe_type;
1728 115192 : if (old_neigh_level != min_p_level)
1729 0 : neigh_fe_type.order = base_fe_type.order.get_order() + min_p_level - old_neigh_level;
1730 115192 : FEInterface::dofs_on_side(elem, Dim, elem_fe_type, s, my_side_dofs);
1731 115192 : FEInterface::dofs_on_side(neigh, Dim, neigh_fe_type, s_neigh, neigh_side_dofs);
1732 :
1733 10178 : const unsigned int n_side_dofs =
1734 20356 : cast_int<unsigned int>(my_side_dofs.size());
1735 10178 : libmesh_assert_equal_to (n_side_dofs, neigh_side_dofs.size());
1736 :
1737 : #ifndef NDEBUG
1738 56350 : for (auto i : my_side_dofs)
1739 46172 : libmesh_assert_less(i, my_dof_indices.size());
1740 56350 : for (auto i : neigh_side_dofs)
1741 46172 : libmesh_assert_less(i, neigh_dof_indices.size());
1742 : #endif
1743 :
1744 105014 : Ke.resize (n_side_dofs, n_side_dofs);
1745 115192 : Ue.resize(n_side_dofs);
1746 :
1747 : // Form the projection matrix, (inner product of fine basis
1748 : // functions against fine test functions)
1749 644442 : for (unsigned int is = 0; is != n_side_dofs; ++is)
1750 : {
1751 575422 : const unsigned int i = my_side_dofs[is];
1752 3364104 : for (unsigned int js = 0; js != n_side_dofs; ++js)
1753 : {
1754 3074962 : const unsigned int j = my_side_dofs[js];
1755 19665950 : for (unsigned int qp = 0; qp != n_qp; ++qp)
1756 : {
1757 24824344 : Ke(is,js) += JxW[qp] * TensorTools::inner_product(phi[i][qp], phi[j][qp]);
1758 16831096 : if (cont == C_ONE)
1759 4216280 : Ke(is,js) += JxW[qp] *
1760 646400 : TensorTools::inner_product((*dphi)[i][qp] *
1761 : (*face_normals)[qp],
1762 1616000 : (*dphi)[j][qp] *
1763 : (*face_normals)[qp]);
1764 : }
1765 : }
1766 : }
1767 :
1768 : // Form the right hand sides, (inner product of coarse basis
1769 : // functions against fine test functions)
1770 644442 : for (unsigned int is = 0; is != n_side_dofs; ++is)
1771 : {
1772 575422 : const unsigned int i = neigh_side_dofs[is];
1773 483078 : Fe.resize (n_side_dofs);
1774 3364104 : for (unsigned int js = 0; js != n_side_dofs; ++js)
1775 : {
1776 3074962 : const unsigned int j = my_side_dofs[js];
1777 19665950 : for (unsigned int qp = 0; qp != n_qp; ++qp)
1778 : {
1779 19495512 : Fe(js) += JxW[qp] *
1780 19495512 : TensorTools::inner_product(neigh_phi[i][qp],
1781 18163304 : phi[j][qp]);
1782 16831096 : if (cont == C_ONE)
1783 4216280 : Fe(js) += JxW[qp] *
1784 969600 : TensorTools::inner_product((*neigh_dphi)[i][qp] *
1785 : (*face_normals)[qp],
1786 1616000 : (*dphi)[j][qp] *
1787 : (*face_normals)[qp]);
1788 : }
1789 : }
1790 575422 : Ke.cholesky_solve(Fe, Ue[is]);
1791 : }
1792 :
1793 644442 : for (unsigned int js = 0; js != n_side_dofs; ++js)
1794 : {
1795 529250 : const unsigned int j = my_side_dofs[js];
1796 575422 : const dof_id_type my_dof_g = my_dof_indices[j];
1797 46172 : libmesh_assert_not_equal_to (my_dof_g, DofObject::invalid_id);
1798 :
1799 : // Hunt for "constraining against myself" cases before
1800 : // we bother creating a constraint row
1801 46172 : bool self_constraint = false;
1802 2742436 : for (unsigned int is = 0; is != n_side_dofs; ++is)
1803 : {
1804 2371590 : const unsigned int i = neigh_side_dofs[is];
1805 2371590 : const dof_id_type their_dof_g = neigh_dof_indices[i];
1806 200317 : libmesh_assert_not_equal_to (their_dof_g, DofObject::invalid_id);
1807 :
1808 2371590 : if (their_dof_g == my_dof_g)
1809 : {
1810 : #ifndef NDEBUG
1811 13766 : const Real their_dof_value = Ue[is](js);
1812 13766 : libmesh_assert_less (std::abs(their_dof_value-1.),
1813 : 10*TOLERANCE);
1814 :
1815 87298 : for (unsigned int k = 0; k != n_side_dofs; ++k)
1816 73532 : libmesh_assert(k == is ||
1817 : std::abs(Ue[k](js)) <
1818 : 10*TOLERANCE);
1819 : #endif
1820 :
1821 13766 : self_constraint = true;
1822 13766 : break;
1823 : }
1824 : }
1825 :
1826 529250 : if (self_constraint)
1827 235498 : continue;
1828 :
1829 : DofConstraintRow * constraint_row;
1830 :
1831 : // we may be running constraint methods concurrently
1832 : // on multiple threads, so we need a lock to
1833 : // ensure that this constraint is "ours"
1834 : {
1835 32406 : Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
1836 :
1837 370846 : if (dof_map.is_constrained_dof(my_dof_g))
1838 7084 : continue;
1839 :
1840 293752 : constraint_row = &(constraints[my_dof_g]);
1841 25322 : libmesh_assert(constraint_row->empty());
1842 : }
1843 :
1844 1804950 : for (unsigned int is = 0; is != n_side_dofs; ++is)
1845 : {
1846 1511198 : const unsigned int i = neigh_side_dofs[is];
1847 1511198 : const dof_id_type their_dof_g = neigh_dof_indices[i];
1848 124810 : libmesh_assert_not_equal_to (their_dof_g, DofObject::invalid_id);
1849 124810 : libmesh_assert_not_equal_to (their_dof_g, my_dof_g);
1850 :
1851 1636008 : const Real their_dof_value = Ue[is](js);
1852 :
1853 1511198 : if (std::abs(their_dof_value) < 10*TOLERANCE)
1854 872099 : continue;
1855 :
1856 53372 : constraint_row->emplace(their_dof_g, their_dof_value);
1857 : }
1858 : }
1859 :
1860 115192 : my_fe->set_fe_order(my_fe->get_fe_type().order.get_order() + old_elem_level - min_p_level);
1861 115192 : neigh_fe->set_fe_order(neigh_fe->get_fe_type().order.get_order() + old_neigh_level - min_p_level);
1862 : }
1863 :
1864 1917495 : if (add_p_level)
1865 : {
1866 : // p refinement constraints:
1867 : // constrain dofs shared between
1868 : // active elements and neighbors with
1869 : // lower polynomial degrees
1870 : const unsigned int min_p_level =
1871 2081367 : neigh->min_p_level_by_neighbor(elem, elem->p_level());
1872 2081367 : if (min_p_level < elem->p_level())
1873 : {
1874 : // Adaptive p refinement of non-hierarchic bases will
1875 : // require more coding
1876 48 : libmesh_assert(my_fe->is_hierarchic());
1877 576 : dof_map.constrain_p_dofs(variable_number, elem,
1878 : s, min_p_level);
1879 : }
1880 : }
1881 : }
1882 1429170 : }
1883 :
1884 : #endif // #ifdef LIBMESH_ENABLE_AMR
1885 :
1886 :
1887 :
1888 : #ifdef LIBMESH_ENABLE_PERIODIC
1889 : template <typename OutputType>
1890 : void
1891 269092 : FEGenericBase<OutputType>::
1892 : compute_periodic_constraints (DofConstraints & constraints,
1893 : DofMap & dof_map,
1894 : const PeriodicBoundaries & boundaries,
1895 : const MeshBase & mesh,
1896 : const PointLocatorBase * point_locator,
1897 : const unsigned int variable_number,
1898 : const Elem * elem)
1899 : {
1900 : // Only bother if we truly have periodic boundaries
1901 269092 : if (boundaries.empty())
1902 59049 : return;
1903 :
1904 67498 : libmesh_assert(elem);
1905 :
1906 : // Only constrain active elements with this method
1907 67498 : if (!elem->active())
1908 19539 : return;
1909 :
1910 139749 : if (elem->infinite())
1911 0 : libmesh_not_implemented();
1912 :
1913 210043 : const unsigned int Dim = elem->dim();
1914 :
1915 : // We need sys_number and variable_number for DofObject methods
1916 : // later
1917 95918 : const unsigned int sys_number = dof_map.sys_number();
1918 :
1919 47959 : const FEType & base_fe_type = dof_map.variable_type(variable_number);
1920 :
1921 : // Construct FE objects for this element and its pseudo-neighbors.
1922 210043 : std::unique_ptr<FEGenericBase<OutputShape>> my_fe
1923 : (FEGenericBase<OutputShape>::build(Dim, base_fe_type));
1924 210043 : const FEContinuity cont = my_fe->get_continuity();
1925 :
1926 : // We don't need to constrain discontinuous elements
1927 210043 : if (cont == DISCONTINUOUS)
1928 0 : return;
1929 47959 : libmesh_assert (cont == C_ZERO || cont == C_ONE);
1930 :
1931 : // We'll use element size to generate relative tolerances later
1932 210043 : const Real primary_hmin = elem->hmin();
1933 :
1934 258002 : std::unique_ptr<FEGenericBase<OutputShape>> neigh_fe
1935 : (FEGenericBase<OutputShape>::build(Dim, base_fe_type));
1936 :
1937 258002 : QGauss my_qface(Dim-1, base_fe_type.default_quadrature_order());
1938 210043 : my_fe->attach_quadrature_rule (&my_qface);
1939 95918 : std::vector<Point> neigh_qface;
1940 :
1941 139749 : const std::vector<Real> & JxW = my_fe->get_JxW();
1942 139749 : const std::vector<Point> & q_point = my_fe->get_xyz();
1943 47959 : const std::vector<std::vector<OutputShape>> & phi = my_fe->get_phi();
1944 47959 : const std::vector<std::vector<OutputShape>> & neigh_phi =
1945 : neigh_fe->get_phi();
1946 47959 : const std::vector<Point> * face_normals = nullptr;
1947 47959 : const std::vector<std::vector<OutputGradient>> * dphi = nullptr;
1948 47959 : const std::vector<std::vector<OutputGradient>> * neigh_dphi = nullptr;
1949 95918 : std::vector<dof_id_type> my_dof_indices, neigh_dof_indices;
1950 95918 : std::vector<unsigned int> my_side_dofs, neigh_side_dofs;
1951 :
1952 210043 : if (cont != C_ZERO)
1953 : {
1954 8192 : const std::vector<Point> & ref_face_normals =
1955 4096 : my_fe->get_normals();
1956 4096 : face_normals = &ref_face_normals;
1957 4096 : const std::vector<std::vector<OutputGradient>> & ref_dphi =
1958 : my_fe->get_dphi();
1959 4096 : dphi = &ref_dphi;
1960 4096 : const std::vector<std::vector<OutputGradient>> & ref_neigh_dphi =
1961 : neigh_fe->get_dphi();
1962 4096 : neigh_dphi = &ref_neigh_dphi;
1963 : }
1964 :
1965 305961 : DenseMatrix<Real> Ke;
1966 210043 : DenseVector<Real> Fe;
1967 143877 : std::vector<DenseVector<Real>> Ue;
1968 :
1969 : // Container to catch the boundary ids that BoundaryInfo hands us.
1970 95918 : std::vector<boundary_id_type> bc_ids;
1971 :
1972 : // Look at the element faces. Check to see if we need to
1973 : // build constraints.
1974 210043 : const unsigned short int max_ns = elem->n_sides();
1975 1039783 : for (unsigned short int s = 0; s != max_ns; ++s)
1976 : {
1977 1020024 : if (elem->neighbor_ptr(s))
1978 602010 : continue;
1979 :
1980 42832 : mesh.get_boundary_info().boundary_ids (elem, s, bc_ids);
1981 :
1982 66800 : for (const auto & boundary_id : bc_ids)
1983 : {
1984 23968 : const PeriodicBoundaryBase * periodic = boundaries.boundary(boundary_id);
1985 23968 : if (!periodic || !periodic->is_my_variable(variable_number))
1986 7672 : continue;
1987 :
1988 3172 : libmesh_assert(point_locator);
1989 :
1990 : // Get pointers to the element's neighbor.
1991 : unsigned int s_neigh;
1992 16296 : const Elem * neigh = boundaries.neighbor(boundary_id, *point_locator, elem, s, &s_neigh);
1993 :
1994 16296 : libmesh_error_msg_if(neigh == nullptr,
1995 : "PeriodicBoundaries point locator object returned nullptr!");
1996 :
1997 : // periodic (and possibly h refinement) constraints:
1998 : // constrain dofs shared between
1999 : // this element and ones as coarse
2000 : // as or coarser than this element.
2001 16296 : if (neigh->level() <= elem->level())
2002 : {
2003 : #ifdef LIBMESH_ENABLE_AMR
2004 : // Find the minimum p level; we build the h constraint
2005 : // matrix with this and then constrain away all higher p
2006 : // DoFs.
2007 2724 : libmesh_assert(neigh->active());
2008 14952 : const unsigned int min_p_level =
2009 20400 : std::min(elem->p_level(), neigh->p_level());
2010 :
2011 : // we may need to make the FE objects reinit with the
2012 : // minimum shared p_level
2013 : // FIXME - I hate using const_cast<> and avoiding
2014 : // accessor functions; there's got to be a
2015 : // better way to do this!
2016 2724 : const unsigned int old_elem_level = elem->p_level();
2017 14952 : if (old_elem_level != min_p_level)
2018 0 : (const_cast<Elem *>(elem))->hack_p_level(min_p_level);
2019 5448 : const unsigned int old_neigh_level = neigh->p_level();
2020 14952 : if (old_neigh_level != min_p_level)
2021 0 : (const_cast<Elem *>(neigh))->hack_p_level(min_p_level);
2022 : #endif // #ifdef LIBMESH_ENABLE_AMR
2023 :
2024 : // We can do a projection with a single integration,
2025 : // due to the assumption of nested finite element
2026 : // subspaces.
2027 : // FIXME: it might be more efficient to do nodes,
2028 : // then edges, then side, to reduce the size of the
2029 : // Cholesky factorization(s)
2030 14952 : my_fe->reinit(elem, s);
2031 :
2032 14952 : dof_map.dof_indices (elem, my_dof_indices,
2033 : variable_number);
2034 14952 : dof_map.dof_indices (neigh, neigh_dof_indices,
2035 : variable_number);
2036 :
2037 : // We use neigh_dof_indices_all_variables in the case that the
2038 : // periodic boundary condition involves mappings between multiple
2039 : // variables.
2040 8172 : std::vector<std::vector<dof_id_type>> neigh_dof_indices_all_variables;
2041 14952 : if(periodic->has_transformation_matrix())
2042 : {
2043 7200 : const std::set<unsigned int> & variables = periodic->get_variables();
2044 7200 : neigh_dof_indices_all_variables.resize(variables.size());
2045 600 : unsigned int index = 0;
2046 28800 : for(unsigned int var : variables)
2047 : {
2048 23400 : dof_map.dof_indices (neigh, neigh_dof_indices_all_variables[index],
2049 : var);
2050 21600 : index++;
2051 : }
2052 : }
2053 :
2054 2724 : const unsigned int n_qp = my_qface.n_points();
2055 :
2056 : // Translate the quadrature points over to the
2057 : // neighbor's boundary
2058 20400 : std::vector<Point> neigh_point(q_point.size());
2059 60848 : for (auto i : index_range(neigh_point))
2060 52676 : neigh_point[i] = periodic->get_corresponding_pos(q_point[i]);
2061 :
2062 14952 : FEMap::inverse_map (Dim, neigh, neigh_point,
2063 : neigh_qface);
2064 :
2065 14952 : neigh_fe->reinit(neigh, &neigh_qface);
2066 :
2067 : // We're only concerned with DOFs whose values (and/or first
2068 : // derivatives for C1 elements) are supported on side nodes
2069 14952 : FEInterface::dofs_on_side(elem, Dim, base_fe_type, s, my_side_dofs);
2070 14952 : FEInterface::dofs_on_side(neigh, Dim, base_fe_type, s_neigh, neigh_side_dofs);
2071 :
2072 : // We're done with functions that examine Elem::p_level(),
2073 : // so let's unhack those levels
2074 : #ifdef LIBMESH_ENABLE_AMR
2075 17676 : if (elem->p_level() != old_elem_level)
2076 0 : (const_cast<Elem *>(elem))->hack_p_level(old_elem_level);
2077 17676 : if (neigh->p_level() != old_neigh_level)
2078 0 : (const_cast<Elem *>(neigh))->hack_p_level(old_neigh_level);
2079 : #endif // #ifdef LIBMESH_ENABLE_AMR
2080 :
2081 2724 : const unsigned int n_side_dofs =
2082 : cast_int<unsigned int>
2083 5448 : (my_side_dofs.size());
2084 2724 : libmesh_assert_equal_to (n_side_dofs, neigh_side_dofs.size());
2085 :
2086 12228 : Ke.resize (n_side_dofs, n_side_dofs);
2087 14952 : Ue.resize(n_side_dofs);
2088 :
2089 : // Form the projection matrix, (inner product of fine basis
2090 : // functions against fine test functions)
2091 60936 : for (unsigned int is = 0; is != n_side_dofs; ++is)
2092 : {
2093 52772 : const unsigned int i = my_side_dofs[is];
2094 200592 : for (unsigned int js = 0; js != n_side_dofs; ++js)
2095 : {
2096 173404 : const unsigned int j = my_side_dofs[js];
2097 708992 : for (unsigned int qp = 0; qp != n_qp; ++qp)
2098 : {
2099 670424 : Ke(is,js) += JxW[qp] *
2100 670424 : TensorTools::inner_product(phi[i][qp],
2101 612404 : phi[j][qp]);
2102 554384 : if (cont != C_ZERO)
2103 384 : Ke(is,js) += JxW[qp] *
2104 64 : TensorTools::inner_product((*dphi)[i][qp] *
2105 : (*face_normals)[qp],
2106 160 : (*dphi)[j][qp] *
2107 : (*face_normals)[qp]);
2108 : }
2109 : }
2110 : }
2111 :
2112 : // Form the right hand sides, (inner product of coarse basis
2113 : // functions against fine test functions)
2114 60936 : for (unsigned int is = 0; is != n_side_dofs; ++is)
2115 : {
2116 52772 : const unsigned int i = neigh_side_dofs[is];
2117 39196 : Fe.resize (n_side_dofs);
2118 200592 : for (unsigned int js = 0; js != n_side_dofs; ++js)
2119 : {
2120 173404 : const unsigned int j = my_side_dofs[js];
2121 708992 : for (unsigned int qp = 0; qp != n_qp; ++qp)
2122 : {
2123 670424 : Fe(js) += JxW[qp] *
2124 670424 : TensorTools::inner_product(neigh_phi[i][qp],
2125 612404 : phi[j][qp]);
2126 554384 : if (cont != C_ZERO)
2127 384 : Fe(js) += JxW[qp] *
2128 96 : TensorTools::inner_product((*neigh_dphi)[i][qp] *
2129 : (*face_normals)[qp],
2130 160 : (*dphi)[j][qp] *
2131 : (*face_normals)[qp]);
2132 : }
2133 : }
2134 52772 : Ke.cholesky_solve(Fe, Ue[is]);
2135 : }
2136 :
2137 : // Make sure we're not adding recursive constraints
2138 : // due to the redundancy in the way we add periodic
2139 : // boundary constraints
2140 : //
2141 : // In order for this to work while threaded or on
2142 : // distributed meshes, we need a rigorous way to
2143 : // avoid recursive constraints. Here it is:
2144 : //
2145 : // For vertex DoFs, if there is a "prior" element
2146 : // (i.e. a coarser element or an equally refined
2147 : // element with a lower id) on this boundary which
2148 : // contains the vertex point, then we will avoid
2149 : // generating constraints; the prior element (or
2150 : // something prior to it) may do so. If we are the
2151 : // most prior (or "primary") element on this
2152 : // boundary sharing this point, then we look at the
2153 : // boundary periodic to us, we find the primary
2154 : // element there, and if that primary is coarser or
2155 : // equal-but-lower-id, then our vertex dofs are
2156 : // constrained in terms of that element.
2157 : //
2158 : // For edge DoFs, if there is a coarser element
2159 : // on this boundary sharing this edge, then we will
2160 : // avoid generating constraints (we will be
2161 : // constrained indirectly via AMR constraints
2162 : // connecting us to the coarser element's DoFs). If
2163 : // we are the coarsest element sharing this edge,
2164 : // then we generate constraints if and only if we
2165 : // are finer than the coarsest element on the
2166 : // boundary periodic to us sharing the corresponding
2167 : // periodic edge, or if we are at equal level but
2168 : // our edge nodes have higher ids than the periodic
2169 : // edge nodes (sorted from highest to lowest, then
2170 : // compared lexicographically)
2171 : //
2172 : // For face DoFs, we generate constraints if we are
2173 : // finer than our periodic neighbor, or if we are at
2174 : // equal level but our element id is higher than its
2175 : // element id.
2176 : //
2177 : // If the primary neighbor is also the current elem
2178 : // (a 1-element-thick mesh) then we choose which
2179 : // vertex dofs to constrain via lexicographic
2180 : // ordering on point locations
2181 :
2182 : // FIXME: This code doesn't yet properly handle
2183 : // cases where multiple different periodic BCs
2184 : // intersect.
2185 5448 : std::set<dof_id_type> my_constrained_dofs;
2186 :
2187 : // Container to catch boundary IDs handed back by BoundaryInfo.
2188 5448 : std::vector<boundary_id_type> new_bc_ids;
2189 :
2190 111064 : for (auto n : elem->node_index_range())
2191 : {
2192 96112 : if (!elem->is_node_on_side(n,s))
2193 43076 : continue;
2194 :
2195 6780 : const Node & my_node = elem->node_ref(n);
2196 :
2197 45896 : if (elem->is_vertex(n))
2198 : {
2199 : // Find all boundary ids that include this
2200 : // point and have periodic boundary
2201 : // conditions for this variable
2202 6640 : std::set<boundary_id_type> point_bcids;
2203 :
2204 271384 : for (unsigned int new_s = 0;
2205 278024 : new_s != max_ns; ++new_s)
2206 : {
2207 233808 : if (!elem->is_node_on_side(n,new_s))
2208 101032 : continue;
2209 :
2210 117144 : mesh.get_boundary_info().boundary_ids (elem, s, new_bc_ids);
2211 :
2212 234288 : for (const auto & new_boundary_id : new_bc_ids)
2213 : {
2214 117144 : const PeriodicBoundaryBase * new_periodic = boundaries.boundary(new_boundary_id);
2215 117144 : if (new_periodic && new_periodic->is_my_variable(variable_number))
2216 101472 : point_bcids.insert(new_boundary_id);
2217 : }
2218 : }
2219 :
2220 : // See if this vertex has point neighbors to
2221 : // defer to
2222 47797 : if (primary_boundary_point_neighbor
2223 44216 : (elem, my_node, mesh.get_boundary_info(), point_bcids)
2224 6640 : != elem)
2225 22621 : continue;
2226 :
2227 : // Find the complementary boundary id set
2228 3059 : std::set<boundary_id_type> point_pairedids;
2229 36028 : for (const auto & new_boundary_id : point_bcids)
2230 : {
2231 18014 : const PeriodicBoundaryBase * new_periodic = boundaries.boundary(new_boundary_id);
2232 18014 : point_pairedids.insert(new_periodic->pairedboundary);
2233 : }
2234 :
2235 : // What do we want to constrain against?
2236 3059 : const Elem * primary_elem = nullptr;
2237 3059 : const Elem * main_neigh = nullptr;
2238 18014 : Point main_pt = my_node,
2239 18014 : primary_pt = my_node;
2240 :
2241 36028 : for (const auto & new_boundary_id : point_bcids)
2242 : {
2243 : // Find the corresponding periodic point and
2244 : // its primary neighbor
2245 18014 : const PeriodicBoundaryBase * new_periodic = boundaries.boundary(new_boundary_id);
2246 :
2247 : const Point neigh_pt =
2248 18014 : new_periodic->get_corresponding_pos(my_node);
2249 :
2250 : // If the point is getting constrained
2251 : // to itself by this PBC then we don't
2252 : // generate any constraints
2253 18014 : if (neigh_pt.absolute_fuzzy_equals
2254 18014 : (my_node, primary_hmin*TOLERANCE))
2255 7742 : continue;
2256 :
2257 : // Otherwise we'll have a constraint in
2258 : // one direction or another
2259 18014 : if (!primary_elem)
2260 3059 : primary_elem = elem;
2261 :
2262 3059 : const Elem * primary_neigh =
2263 18014 : primary_boundary_point_neighbor(neigh, neigh_pt,
2264 : mesh.get_boundary_info(),
2265 : point_pairedids);
2266 :
2267 3059 : libmesh_assert(primary_neigh);
2268 :
2269 18014 : if (new_boundary_id == boundary_id)
2270 : {
2271 3059 : main_neigh = primary_neigh;
2272 18014 : main_pt = neigh_pt;
2273 : }
2274 :
2275 : // Finer elements will get constrained in
2276 : // terms of coarser neighbors, not the
2277 : // other way around
2278 32969 : if ((primary_neigh->level() > primary_elem->level()) ||
2279 :
2280 : // For equal-level elements, the one with
2281 : // higher id gets constrained in terms of
2282 : // the one with lower id
2283 28994 : (primary_neigh->level() == primary_elem->level() &&
2284 31531 : primary_neigh->id() > primary_elem->id()) ||
2285 :
2286 : // On a one-element-thick mesh, we compare
2287 : // points to see what side gets constrained
2288 1957 : (primary_neigh == primary_elem &&
2289 0 : (neigh_pt > primary_pt)))
2290 7742 : continue;
2291 :
2292 1957 : primary_elem = primary_neigh;
2293 10272 : primary_pt = neigh_pt;
2294 : }
2295 :
2296 14715 : if (!primary_elem ||
2297 21073 : primary_elem != main_neigh ||
2298 1957 : primary_pt != main_pt)
2299 1102 : continue;
2300 : }
2301 1680 : else if (elem->is_edge(n))
2302 : {
2303 : // Find which edge we're on
2304 1680 : unsigned int e=0, ne = elem->n_edges();
2305 3264 : for (; e != ne; ++e)
2306 : {
2307 3264 : if (elem->is_node_on_edge(n,e))
2308 140 : break;
2309 : }
2310 140 : libmesh_assert_less (e, elem->n_edges());
2311 :
2312 : // Find the edge end nodes
2313 : const Node
2314 140 : * e1 = nullptr,
2315 140 : * e2 = nullptr;
2316 4932 : for (auto nn : elem->node_index_range())
2317 : {
2318 4932 : if (nn == n)
2319 0 : continue;
2320 :
2321 4932 : if (elem->is_node_on_edge(nn, e))
2322 : {
2323 3360 : if (e1 == nullptr)
2324 : {
2325 280 : e1 = elem->node_ptr(nn);
2326 : }
2327 : else
2328 : {
2329 280 : e2 = elem->node_ptr(nn);
2330 1680 : break;
2331 : }
2332 : }
2333 : }
2334 140 : libmesh_assert (e1 && e2);
2335 :
2336 : // Find all boundary ids that include this
2337 : // edge and have periodic boundary
2338 : // conditions for this variable
2339 140 : std::set<boundary_id_type> edge_bcids;
2340 :
2341 8020 : for (unsigned int new_s = 0;
2342 8160 : new_s != max_ns; ++new_s)
2343 : {
2344 6480 : if (!elem->is_node_on_side(n,new_s))
2345 4400 : continue;
2346 :
2347 : // We're reusing the new_bc_ids vector created outside the loop over nodes.
2348 1680 : mesh.get_boundary_info().boundary_ids (elem, s, new_bc_ids);
2349 :
2350 3360 : for (const auto & new_boundary_id : new_bc_ids)
2351 : {
2352 1680 : const PeriodicBoundaryBase * new_periodic = boundaries.boundary(new_boundary_id);
2353 1680 : if (new_periodic && new_periodic->is_my_variable(variable_number))
2354 1540 : edge_bcids.insert(new_boundary_id);
2355 : }
2356 : }
2357 :
2358 :
2359 : // See if this edge has neighbors to defer to
2360 1680 : if (primary_boundary_edge_neighbor
2361 1680 : (elem, *e1, *e2, mesh.get_boundary_info(), edge_bcids)
2362 140 : != elem)
2363 0 : continue;
2364 :
2365 : // Find the complementary boundary id set
2366 140 : std::set<boundary_id_type> edge_pairedids;
2367 3360 : for (const auto & new_boundary_id : edge_bcids)
2368 : {
2369 1680 : const PeriodicBoundaryBase * new_periodic = boundaries.boundary(new_boundary_id);
2370 1680 : edge_pairedids.insert(new_periodic->pairedboundary);
2371 : }
2372 :
2373 : // What do we want to constrain against?
2374 140 : const Elem * primary_elem = nullptr;
2375 140 : const Elem * main_neigh = nullptr;
2376 1680 : Point main_pt1 = *e1,
2377 1680 : main_pt2 = *e2,
2378 1680 : primary_pt1 = *e1,
2379 1680 : primary_pt2 = *e2;
2380 :
2381 3360 : for (const auto & new_boundary_id : edge_bcids)
2382 : {
2383 : // Find the corresponding periodic edge and
2384 : // its primary neighbor
2385 1680 : const PeriodicBoundaryBase * new_periodic = boundaries.boundary(new_boundary_id);
2386 :
2387 1680 : Point neigh_pt1 = new_periodic->get_corresponding_pos(*e1),
2388 1680 : neigh_pt2 = new_periodic->get_corresponding_pos(*e2);
2389 :
2390 : // If the edge is getting constrained
2391 : // to itself by this PBC then we don't
2392 : // generate any constraints
2393 140 : if (neigh_pt1.absolute_fuzzy_equals
2394 1820 : (*e1, primary_hmin*TOLERANCE) &&
2395 : neigh_pt2.absolute_fuzzy_equals
2396 0 : (*e2, primary_hmin*TOLERANCE))
2397 840 : continue;
2398 :
2399 : // Otherwise we'll have a constraint in
2400 : // one direction or another
2401 1680 : if (!primary_elem)
2402 140 : primary_elem = elem;
2403 :
2404 140 : const Elem * primary_neigh = primary_boundary_edge_neighbor
2405 1680 : (neigh, neigh_pt1, neigh_pt2,
2406 : mesh.get_boundary_info(), edge_pairedids);
2407 :
2408 140 : libmesh_assert(primary_neigh);
2409 :
2410 1680 : if (new_boundary_id == boundary_id)
2411 : {
2412 140 : main_neigh = primary_neigh;
2413 1680 : main_pt1 = neigh_pt1;
2414 1680 : main_pt2 = neigh_pt2;
2415 : }
2416 :
2417 : // If we have a one-element thick mesh,
2418 : // we'll need to sort our points to get a
2419 : // consistent ordering rule
2420 : //
2421 : // Use >= in this test to make sure that,
2422 : // for angular constraints, no node gets
2423 : // constrained to itself.
2424 1680 : if (primary_neigh == primary_elem)
2425 : {
2426 0 : if (primary_pt1 > primary_pt2)
2427 0 : std::swap(primary_pt1, primary_pt2);
2428 0 : if (neigh_pt1 > neigh_pt2)
2429 0 : std::swap(neigh_pt1, neigh_pt2);
2430 :
2431 0 : if (neigh_pt2 >= primary_pt2)
2432 0 : continue;
2433 : }
2434 :
2435 : // Otherwise:
2436 : // Finer elements will get constrained in
2437 : // terms of coarser ones, not the other way
2438 : // around
2439 3360 : if ((primary_neigh->level() > primary_elem->level()) ||
2440 :
2441 : // For equal-level elements, the one with
2442 : // higher id gets constrained in terms of
2443 : // the one with lower id
2444 3080 : (primary_neigh->level() == primary_elem->level() &&
2445 280 : primary_neigh->id() > primary_elem->id()))
2446 840 : continue;
2447 :
2448 70 : primary_elem = primary_neigh;
2449 840 : primary_pt1 = neigh_pt1;
2450 840 : primary_pt2 = neigh_pt2;
2451 : }
2452 :
2453 1120 : if (!primary_elem ||
2454 1610 : primary_elem != main_neigh ||
2455 1890 : primary_pt1 != main_pt1 ||
2456 70 : primary_pt2 != main_pt2)
2457 70 : continue;
2458 : }
2459 0 : else if (elem->is_face(n))
2460 : {
2461 : // If we have a one-element thick mesh,
2462 : // use the ordering of the face node and its
2463 : // periodic counterpart to determine what
2464 : // gets constrained
2465 0 : if (neigh == elem)
2466 : {
2467 : const Point neigh_pt =
2468 0 : periodic->get_corresponding_pos(my_node);
2469 0 : if (neigh_pt > my_node)
2470 0 : continue;
2471 : }
2472 :
2473 : // Otherwise:
2474 : // Finer elements will get constrained in
2475 : // terms of coarser ones, not the other way
2476 : // around
2477 0 : if ((neigh->level() > elem->level()) ||
2478 :
2479 : // For equal-level elements, the one with
2480 : // higher id gets constrained in terms of
2481 : // the one with lower id
2482 0 : (neigh->level() == elem->level() &&
2483 0 : neigh->id() > elem->id()))
2484 0 : continue;
2485 : }
2486 :
2487 : // If we made it here without hitting a continue
2488 : // statement, then we're at a node whose dofs
2489 : // should be constrained by this element's
2490 : // calculations.
2491 : const unsigned int n_comp =
2492 11112 : my_node.n_comp(sys_number, variable_number);
2493 :
2494 22268 : for (unsigned int i=0; i != n_comp; ++i)
2495 : my_constrained_dofs.insert
2496 9125 : (my_node.dof_number
2497 11156 : (sys_number, variable_number, i));
2498 : }
2499 :
2500 : // FIXME: old code for disambiguating periodic BCs:
2501 : // this is not threadsafe nor safe to run on a
2502 : // non-serialized mesh.
2503 : /*
2504 : std::vector<bool> recursive_constraint(n_side_dofs, false);
2505 :
2506 : for (unsigned int is = 0; is != n_side_dofs; ++is)
2507 : {
2508 : const unsigned int i = neigh_side_dofs[is];
2509 : const dof_id_type their_dof_g = neigh_dof_indices[i];
2510 : libmesh_assert_not_equal_to (their_dof_g, DofObject::invalid_id);
2511 :
2512 : {
2513 : Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
2514 :
2515 : if (!dof_map.is_constrained_dof(their_dof_g))
2516 : continue;
2517 : }
2518 :
2519 : DofConstraintRow & their_constraint_row =
2520 : constraints[their_dof_g].first;
2521 :
2522 : for (unsigned int js = 0; js != n_side_dofs; ++js)
2523 : {
2524 : const unsigned int j = my_side_dofs[js];
2525 : const dof_id_type my_dof_g = my_dof_indices[j];
2526 : libmesh_assert_not_equal_to (my_dof_g, DofObject::invalid_id);
2527 :
2528 : if (their_constraint_row.count(my_dof_g))
2529 : recursive_constraint[js] = true;
2530 : }
2531 : }
2532 : */
2533 :
2534 60936 : for (unsigned int js = 0; js != n_side_dofs; ++js)
2535 : {
2536 : // FIXME: old code path
2537 : // if (recursive_constraint[js])
2538 : // continue;
2539 :
2540 45984 : const unsigned int j = my_side_dofs[js];
2541 52772 : const dof_id_type my_dof_g = my_dof_indices[j];
2542 6788 : libmesh_assert_not_equal_to (my_dof_g, DofObject::invalid_id);
2543 :
2544 : // FIXME: new code path
2545 32102 : if (!my_constrained_dofs.count(my_dof_g))
2546 35100 : continue;
2547 :
2548 : DofConstraintRow * constraint_row;
2549 :
2550 : // we may be running constraint methods concurrently
2551 : // on multiple threads, so we need a lock to
2552 : // ensure that this constraint is "ours"
2553 : {
2554 2031 : Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
2555 :
2556 11156 : if (dof_map.is_constrained_dof(my_dof_g))
2557 81 : continue;
2558 :
2559 10884 : constraint_row = &(constraints[my_dof_g]);
2560 1950 : libmesh_assert(constraint_row->empty());
2561 : }
2562 :
2563 43992 : for (unsigned int is = 0; is != n_side_dofs; ++is)
2564 : {
2565 33108 : const unsigned int i = neigh_side_dofs[is];
2566 33108 : const dof_id_type their_dof_g = neigh_dof_indices[i];
2567 4845 : libmesh_assert_not_equal_to (their_dof_g, DofObject::invalid_id);
2568 :
2569 : // Periodic constraints should never be
2570 : // self-constraints
2571 : // libmesh_assert_not_equal_to (their_dof_g, my_dof_g);
2572 :
2573 33108 : const Real their_dof_value = Ue[is](js);
2574 :
2575 33108 : if (their_dof_g == my_dof_g)
2576 : {
2577 0 : libmesh_assert_less (std::abs(their_dof_value-1.), 1.e-5);
2578 0 : for (unsigned int k = 0; k != n_side_dofs; ++k)
2579 0 : libmesh_assert(k == is || std::abs(Ue[k](js)) < 1.e-5);
2580 20880 : continue;
2581 0 : }
2582 :
2583 33108 : if (std::abs(their_dof_value) < 10*TOLERANCE)
2584 18433 : continue;
2585 :
2586 12228 : if(!periodic->has_transformation_matrix())
2587 : {
2588 2002 : constraint_row->emplace(their_dof_g, their_dof_value);
2589 : }
2590 : else
2591 : {
2592 : // In this case the current variable is constrained in terms of other variables.
2593 : // We assume that all variables in this constraint have the same FE type (this
2594 : // is asserted below), and hence we can create the constraint row contribution
2595 : // by multiplying their_dof_value by the corresponding row of the transformation
2596 : // matrix.
2597 :
2598 4752 : const std::set<unsigned int> & variables = periodic->get_variables();
2599 4752 : neigh_dof_indices_all_variables.resize(variables.size());
2600 396 : unsigned int index = 0;
2601 19008 : for(unsigned int other_var : variables)
2602 : {
2603 1188 : libmesh_assert_msg(base_fe_type == dof_map.variable_type(other_var), "FE types must match for all variables involved in constraint");
2604 :
2605 14256 : Real var_weighting = periodic->get_transformation_matrix()(variable_number, other_var);
2606 14256 : constraint_row->emplace(neigh_dof_indices_all_variables[index][i],
2607 14256 : var_weighting*their_dof_value);
2608 14256 : index++;
2609 : }
2610 : }
2611 :
2612 : }
2613 : }
2614 9504 : }
2615 : // p refinement constraints:
2616 : // constrain dofs shared between
2617 : // active elements and neighbors with
2618 : // lower polynomial degrees
2619 : #ifdef LIBMESH_ENABLE_AMR
2620 : const unsigned int min_p_level =
2621 19468 : neigh->min_p_level_by_neighbor(elem, elem->p_level());
2622 19468 : if (min_p_level < elem->p_level())
2623 : {
2624 : // Adaptive p refinement of non-hierarchic bases will
2625 : // require more coding
2626 0 : libmesh_assert(my_fe->is_hierarchic());
2627 0 : dof_map.constrain_p_dofs(variable_number, elem,
2628 : s, min_p_level);
2629 : }
2630 : #endif // #ifdef LIBMESH_ENABLE_AMR
2631 : }
2632 : }
2633 342375 : }
2634 :
2635 : #endif // LIBMESH_ENABLE_PERIODIC
2636 :
2637 : // ------------------------------------------------------------
2638 : // Explicit instantiations
2639 : template class LIBMESH_EXPORT FEGenericBase<Real>;
2640 : template class LIBMESH_EXPORT FEGenericBase<RealGradient>;
2641 :
2642 : } // namespace libMesh
|