Debian Codegraph @

include/deal.II/numerics/vector_tools_constraints.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15
16 #ifndef dealii_vector_tools_constraints_templates_h
17 #define dealii_vector_tools_constraints_templates_h
18
19 #include <deal.II/grid/manifold.h>
20
21 #include <deal.II/hp/fe_values.h>
22
23 #include <deal.II/numerics/vector_tools_constraints.h>
24
25 #include <boost/container/small_vector.hpp>
26
27 #include <limits>
28 #include <list>
29 #include <set>
30
31
32 DEAL_II_NAMESPACE_OPEN
33
34 namespace VectorTools
35 {
36 namespace internal
37 {
38 /**
39 * A structure that stores the dim DoF indices that correspond to a
40 * vector-valued quantity at a single support point.
41 */
42 template <int dim>
43 struct VectorDoFTuple
44 {
45 std::array<types::global_dof_index, dim> dof_indices;
46
47 VectorDoFTuple()
48 {
49 for (unsigned int i = 0; i < dim; ++i)
50 dof_indices[i] = numbers::invalid_dof_index;
51 }
52
53
54 bool
55 operator<(const VectorDoFTuple<dim> &other) const
56 {
57 return (dof_indices < other.dof_indices);
58 }
59
60 bool
61 operator==(const VectorDoFTuple<dim> &other) const
62 {
63 return (dof_indices == other.dof_indices);
64 }
65
66 bool
67 operator!=(const VectorDoFTuple<dim> &other) const
68 {
69 return (dof_indices != other.dof_indices);
70 }
71 };
72
73
74 template <int dim>
75 std::ostream &
76 operator<<(std::ostream &out, const VectorDoFTuple<dim> &vdt)
77 {
78 for (unsigned int d = 0; d < dim; ++d)
79 out << vdt.dof_indices[d] << (d < dim - 1 ? " " : "");
80 return out;
81 }
82
83
84
85 /**
86 * Copied from VectorDoFTuple but only stores
87 * active fe index, face number, DoF index.
88 */
89 struct FaceDoFInfo
90 {
91 static const unsigned int size = 3;
92 // store active fe indeX, face number, DoF index
93 std::array<unsigned int, size> indices;
94
95 bool
96 operator<(const FaceDoFInfo &other) const
97 {
98 return (indices < other.indices);
99 }
100
101 bool
102 operator==(const FaceDoFInfo &other) const
103 {
104 return (indices == other.indices);
105 }
106
107 bool
108 operator!=(const FaceDoFInfo &other) const
109 {
110 return (indices != other.indices);
111 }
112 };
113
114
115
116 /**
117 * Add the constraint $\vec n \cdot \vec u = inhom$ to the list of
118 * constraints.
119 *
120 * Here, $\vec u$ is represented by the set of given DoF indices, and
121 * $\vec n$ by the vector specified as the second argument.
122 *
123 * The function does not add constraints if a degree of freedom is already
124 * constrained in the constraints object.
125 */
126 template <int dim, typename number>
127 void
128 add_constraint(const VectorDoFTuple<dim> &dof_indices,
129 const Tensor<1, dim> &constraining_vector,
130 AffineConstraints<number> &constraints,
131 const number inhomogeneity = 0)
132 {
133 // choose the DoF that has the largest component in the
134 // constraining_vector as the one to be constrained as this makes the
135 // process stable in cases where the constraining_vector has the form
136 // n=(1,0) or n=(0,1)
137 //
138 // we get constraints of the form x0 = a_1*x1 + a2*x2 + ... if one of
139 // the weights is essentially zero then skip this part. the
140 // AffineConstraints can also deal with cases like x0 = 0 if
141 // necessary
142 //
143 // there is a problem if we have a normal vector of the form
144 // (a,a,small) or (a,a,a). Depending on round-off we may choose the
145 // first or second component (or third, in the latter case) as the
146 // largest one, and depending on our choice one or another degree of
147 // freedom will be constrained. On a single processor this is not
148 // much of a problem, but it's a nightmare when we run in parallel
149 // and two processors disagree on which DoF should be constrained.
150 // This led to an incredibly difficult to find bug in step-32 when
151 // running in parallel with 9 or more processors.
152 //
153 // in practice, such normal vectors of the form (a,a,small) or
154 // (a,a,a) happen not infrequently since they lie on the diagonals
155 // where vertices frequently happen to land upon mesh refinement if
156 // one starts from a symmetric and regular body. we work around this
157 // problem in the following way: if we have a normal vector of the
158 // form (a,b) (similarly algorithm in 3d), we choose 'a' as the
159 // largest coefficient not if a>b but if a>b+1e-10. this shifts the
160 // problem away from the frequently visited diagonal to a line that
161 // is off the diagonal. there will of course be problems where the
162 // exact values of a and b differ by exactly 1e-10 and we get into
163 // the same instability, but from a practical viewpoint such problems
164 // should be much rarer. in particular, meshes have to be very fine
165 // for a vertex to land on this line if the original body had a
166 // vertex on the diagonal as well
167 switch (dim)
168 {
169 case 2:
170 {
171 if (std::fabs(constraining_vector[0]) >
172 std::fabs(constraining_vector[1]) + 1e-10)
173 {
174 if (!constraints.is_constrained(dof_indices.dof_indices[0]) &&
175 constraints.can_store_line(dof_indices.dof_indices[0]))
176 {
177 const number normalized_inhomogeneity =
178 (std::fabs(inhomogeneity / constraining_vector[0]) >
179 std::numeric_limits<number>::epsilon() ?
180 inhomogeneity / constraining_vector[0] :
181 0);
182
183 if (std::fabs(constraining_vector[1] /
184 constraining_vector[0]) >
185 std::numeric_limits<number>::epsilon())
186 constraints.add_constraint(dof_indices.dof_indices[0],
187 {{dof_indices.dof_indices[1],
188 -constraining_vector[1] /
189 constraining_vector[0]}},
190 normalized_inhomogeneity);
191 else
192 constraints.add_constraint(dof_indices.dof_indices[0],
193 {},
194 normalized_inhomogeneity);
195 }
196 }
197 else
198 {
199 if (!constraints.is_constrained(dof_indices.dof_indices[1]) &&
200 constraints.can_store_line(dof_indices.dof_indices[1]))
201 {
202 const number normalized_inhomogeneity =
203 (std::fabs(inhomogeneity / constraining_vector[1]) >
204 std::numeric_limits<number>::epsilon() ?
205 inhomogeneity / constraining_vector[1] :
206 0);
207
208 if (std::fabs(constraining_vector[0] /
209 constraining_vector[1]) >
210 std::numeric_limits<number>::epsilon())
211 constraints.add_constraint(dof_indices.dof_indices[1],
212 {{dof_indices.dof_indices[0],
213 -constraining_vector[0] /
214 constraining_vector[1]}},
215 normalized_inhomogeneity);
216 else
217 constraints.add_constraint(dof_indices.dof_indices[1],
218 {},
219 normalized_inhomogeneity);
220 }
221 }
222 break;
223 }
224
225 case 3:
226 {
227 // Store constraints. There are at most 2 entries per
228 // constraint, so a boost::container::small_vector with
229 // static size 2 will do just fine.
230 //
231 // TODO: This could use std::inplace_vector once available (in
232 // C++26?)
233 boost::container::
234 small_vector<std::pair<types::global_dof_index, number>, 2>
235 constraint_entries;
236
237 if ((std::fabs(constraining_vector[0]) >=
238 std::fabs(constraining_vector[1]) + 1e-10) &&
239 (std::fabs(constraining_vector[0]) >=
240 std::fabs(constraining_vector[2]) + 2e-10))
241 {
242 if (!constraints.is_constrained(dof_indices.dof_indices[0]) &&
243 constraints.can_store_line(dof_indices.dof_indices[0]))
244 {
245 if (std::fabs(constraining_vector[1] /
246 constraining_vector[0]) >
247 std::numeric_limits<number>::epsilon())
248 constraint_entries.emplace_back(
249 std::make_pair(dof_indices.dof_indices[1],
250 -constraining_vector[1] /
251 constraining_vector[0]));
252
253 if (std::fabs(constraining_vector[2] /
254 constraining_vector[0]) >
255 std::numeric_limits<number>::epsilon())
256 constraint_entries.emplace_back(
257 std::make_pair(dof_indices.dof_indices[2],
258 -constraining_vector[2] /
259 constraining_vector[0]));
260
261 const number normalized_inhomogeneity =
262 (std::fabs(inhomogeneity / constraining_vector[0]) >
263 std::numeric_limits<number>::epsilon() ?
264 inhomogeneity / constraining_vector[0] :
265 0);
266
267 constraints.add_constraint(dof_indices.dof_indices[0],
268 constraint_entries,
269 normalized_inhomogeneity);
270 }
271 }
272 else if ((std::fabs(constraining_vector[1]) + 1e-10 >=
273 std::fabs(constraining_vector[0])) &&
274 (std::fabs(constraining_vector[1]) >=
275 std::fabs(constraining_vector[2]) + 1e-10))
276 {
277 if (!constraints.is_constrained(dof_indices.dof_indices[1]) &&
278 constraints.can_store_line(dof_indices.dof_indices[1]))
279 {
280 if (std::fabs(constraining_vector[0] /
281 constraining_vector[1]) >
282 std::numeric_limits<number>::epsilon())
283 constraint_entries.emplace_back(
284 std::make_pair(dof_indices.dof_indices[0],
285 -constraining_vector[0] /
286 constraining_vector[1]));
287
288 if (std::fabs(constraining_vector[2] /
289 constraining_vector[1]) >
290 std::numeric_limits<number>::epsilon())
291 constraint_entries.emplace_back(
292 std::make_pair(dof_indices.dof_indices[2],
293 -constraining_vector[2] /
294 constraining_vector[1]));
295
296 const number normalized_inhomogeneity =
297 (std::fabs(inhomogeneity / constraining_vector[1]) >
298 std::numeric_limits<number>::epsilon() ?
299 inhomogeneity / constraining_vector[1] :
300 0);
301
302 constraints.add_constraint(dof_indices.dof_indices[1],
303 constraint_entries,
304 normalized_inhomogeneity);
305 }
306 }
307 else
308 {
309 if (!constraints.is_constrained(dof_indices.dof_indices[2]) &&
310 constraints.can_store_line(dof_indices.dof_indices[2]))
311 {
312 if (std::fabs(constraining_vector[0] /
313 constraining_vector[2]) >
314 std::numeric_limits<number>::epsilon())
315 constraint_entries.emplace_back(
316 std::make_pair(dof_indices.dof_indices[0],
317 -constraining_vector[0] /
318 constraining_vector[2]));
319
320 if (std::fabs(constraining_vector[1] /
321 constraining_vector[2]) >
322 std::numeric_limits<number>::epsilon())
323 constraint_entries.emplace_back(
324 std::make_pair(dof_indices.dof_indices[1],
325 -constraining_vector[1] /
326 constraining_vector[2]));
327
328 const number normalized_inhomogeneity =
329 (std::fabs(inhomogeneity / constraining_vector[2]) >
330 std::numeric_limits<number>::epsilon() ?
331 inhomogeneity / constraining_vector[2] :
332 0);
333
334 constraints.add_constraint(dof_indices.dof_indices[2],
335 constraint_entries,
336 normalized_inhomogeneity);
337 }
338 }
339
340 break;
341 }
342
343 default:
344 DEAL_II_NOT_IMPLEMENTED();
345 }
346 }
347
348
349 /**
350 * Add the constraint $(\vec u-\vec u_\Gamma) \| \vec t$ to the list of
351 * constraints. In 2d, this is a single constraint, in 3d these are two
352 * constraints.
353 *
354 * Here, $\vec u$ is represented by the set of given DoF indices, and
355 * $\vec t$ by the vector specified as the second argument.
356 *
357 * The function does not add constraints if a degree of freedom is already
358 * constrained in the constraints object.
359 */
360 template <int dim, typename number>
361 void
362 add_tangentiality_constraints(
363 const VectorDoFTuple<dim> &dof_indices,
364 const Tensor<1, dim> &tangent_vector,
365 AffineConstraints<number> &constraints,
366 const Vector<number> &b_values = Vector<number>(dim))
367 {
368 // choose the DoF that has the
369 // largest component in the
370 // tangent_vector as the
371 // independent component, and
372 // then constrain the others to
373 // it. specifically, if, say,
374 // component 0 of the tangent
375 // vector t is largest by
376 // magnitude, then
377 // x1=(b[1]*t[0]-b[0]*t[1])/t[0]+t[1]/t[0]*x_0, etc.
378 unsigned int largest_component = 0;
379 for (unsigned int d = 1; d < dim; ++d)
380 if (std::fabs(tangent_vector[d]) >
381 std::fabs(tangent_vector[largest_component]) + 1e-10)
382 largest_component = d;
383
384 // then constrain all of the
385 // other degrees of freedom in
386 // terms of the one just found
387 for (unsigned int d = 0; d < dim; ++d)
388 if (d != largest_component)
389 if (!constraints.is_constrained(dof_indices.dof_indices[d]) &&
390 constraints.can_store_line(dof_indices.dof_indices[d]))
391 {
392 const number inhomogeneity =
393 (b_values(d) * tangent_vector[largest_component] -
394 b_values(largest_component) * tangent_vector[d]) /
395 tangent_vector[largest_component];
396
397 const number normalized_inhomogeneity =
398 (std::fabs(inhomogeneity) >
399 std::numeric_limits<number>::epsilon() ?
400 inhomogeneity :
401 0);
402
403 if (std::fabs(tangent_vector[d] /
404 tangent_vector[largest_component]) >
405 std::numeric_limits<number>::epsilon())
406 constraints.add_constraint(
407 dof_indices.dof_indices[d],
408 {{dof_indices.dof_indices[largest_component],
409 tangent_vector[d] / tangent_vector[largest_component]}},
410 normalized_inhomogeneity);
411 else
412 constraints.add_constraint(dof_indices.dof_indices[d],
413 {},
414 normalized_inhomogeneity);
415 }
416 }
417
418
419
420 /**
421 * Given a vector, compute a set of dim-1 vectors that are orthogonal to
422 * the first one and mutually orthonormal as well.
423 */
424 template <int dim>
425 void
426 compute_orthonormal_vectors(const Tensor<1, dim> &vector,
427 Tensor<1, dim> (&orthonormals)[dim - 1])
428 {
429 switch (dim)
430 {
431 case 3:
432 {
433 // to do this in 3d, take
434 // one vector that is
435 // guaranteed to be not
436 // aligned with the
437 // average tangent and
438 // form the cross
439 // product. this yields
440 // one vector that is
441 // certainly
442 // perpendicular to the
443 // tangent; then take the
444 // cross product between
445 // this vector and the
446 // tangent and get one
447 // vector that is
448 // perpendicular to both
449
450 // construct a
451 // temporary vector
452 // by swapping the
453 // larger two
454 // components and
455 // flipping one
456 // sign; this can
457 // not be collinear
458 // with the average
459 // tangent
460 Tensor<1, dim> tmp = vector;
461 if ((std::fabs(tmp[0]) > std::fabs(tmp[1])) &&
462 (std::fabs(tmp[0]) > std::fabs(tmp[2])))
463 {
464 // entry zero
465 // is the
466 // largest
467 if ((std::fabs(tmp[1]) > std::fabs(tmp[2])))
468 std::swap(tmp[0], tmp[1]);
469 else
470 std::swap(tmp[0], tmp[2]);
471
472 tmp[0] *= -1;
473 }
474 else if ((std::fabs(tmp[1]) > std::fabs(tmp[0])) &&
475 (std::fabs(tmp[1]) > std::fabs(tmp[2])))
476 {
477 // entry one
478 // is the
479 // largest
480 if ((std::fabs(tmp[0]) > std::fabs(tmp[2])))
481 std::swap(tmp[1], tmp[0]);
482 else
483 std::swap(tmp[1], tmp[2]);
484
485 tmp[1] *= -1;
486 }
487 else
488 {
489 // entry two
490 // is the
491 // largest
492 if ((std::fabs(tmp[0]) > std::fabs(tmp[1])))
493 std::swap(tmp[2], tmp[0]);
494 else
495 std::swap(tmp[2], tmp[1]);
496
497 tmp[2] *= -1;
498 }
499
500 // make sure the two vectors
501 // are indeed not collinear
502 Assert(std::fabs(vector * tmp / vector.norm() / tmp.norm()) <
503 (1 - 1e-12),
504 ExcInternalError());
505
506 // now compute the
507 // two normals
508 orthonormals[0] = cross_product_3d(vector, tmp);
509 orthonormals[1] = cross_product_3d(vector, orthonormals[0]);
510
511 break;
512 }
513
514 default:
515 DEAL_II_NOT_IMPLEMENTED();
516 }
517 }
518
519
520
521 /**
522 * Map active fe index, face number and
523 * local degrees of freedom index to the dim degrees of freedom indices that
524 * correspond to a vector-valued quantity at the same support point.
525 */
526 template <int dim, int spacedim>
527 void
528 map_face_dof_to_vector_dof(
529 const typename DoFHandler<dim, spacedim>::cell_iterator &cell,
530 const unsigned int first_vector_component,
531 const std::set<types::boundary_id> &boundary_ids,
532 hp::FEFaceValues<dim, spacedim> &x_fe_face_values,
533 const IndexSet &refinement_edge_indices,
534 const unsigned int level,
535 std::map<FaceDoFInfo, std::array<unsigned int, dim>> &dof_to_vector_dof)
536 {
537 std::set<types::boundary_id>::iterator b_id;
538 for (const unsigned int face_no : cell->face_indices())
539 if ((b_id = boundary_ids.find(cell->face(face_no)->boundary_id())) !=
540 boundary_ids.end())
541 {
542 const FiniteElement<dim> &fe = cell->get_fe();
543 typename DoFHandler<dim, spacedim>::level_face_iterator face =
544 cell->face(face_no);
545
546 std::vector<types::global_dof_index> face_dofs;
547 // get the indices of the dofs on this cell...
548 face_dofs.resize(fe.n_dofs_per_face(face_no));
549
550 if (level != numbers::invalid_unsigned_int)
551 face->get_mg_dof_indices(level,
552 face_dofs,
553 cell->active_fe_index());
554 else
555 face->get_dof_indices(face_dofs, cell->active_fe_index());
556
557 x_fe_face_values.reinit(cell, face_no);
558
559 const auto &face_quadrature_collection =
560 x_fe_face_values.get_quadrature_collection();
561
562 std::array<unsigned int, dim> local_vector_indices;
563 for (unsigned int i = 0; i < face_dofs.size(); ++i)
564 {
565 if (fe.face_system_to_component_index(i, face_no).first ==
566 first_vector_component)
567 if (level == numbers::invalid_unsigned_int ||
568 !refinement_edge_indices.is_element(face_dofs[i]))
569 {
570 local_vector_indices[0] = i;
571 const FaceDoFInfo local_face_dof_info = {
572 {{cell->active_fe_index(), face_no, i}}};
573 for (unsigned int k = 0; k < fe.n_dofs_per_face(face_no);
574 ++k)
575 if ((k != i) &&
576 (face_quadrature_collection[cell->active_fe_index()]
577 .point(k) ==
578 face_quadrature_collection[cell->active_fe_index()]
579 .point(i)) &&
580 (fe.face_system_to_component_index(k, face_no)
581 .first >= first_vector_component) &&
582 (fe.face_system_to_component_index(k, face_no)
583 .first < first_vector_component + dim))
584 {
585 local_vector_indices
586 [fe.face_system_to_component_index(k, face_no)
587 .first -
588 first_vector_component] = k;
589 }
590 dof_to_vector_dof.insert(
591 std::make_pair(local_face_dof_info,
592 local_vector_indices));
593 }
594 }
595 }
596 }
597
598
599
600 /**
601 * Compute the mappings from vector degrees of freedom to normal vectors @p dof_to_normals_map
602 * and vector degrees of freedom to prescribed normal fluxes @p dof_vector_to_b_values.
603 */
604 template <int dim, int spacedim, typename number>
605 void
606 map_dofs_to_normal_vectors_and_normal_fluxes(
607 const typename DoFHandler<dim, spacedim>::cell_iterator &cell,
608 const unsigned int first_vector_component,
609 const std::set<types::boundary_id> &boundary_ids,
610 const std::map<types::boundary_id, const Function<spacedim, number> *>
611 &function_map,
612 hp::FEFaceValues<dim, spacedim> &x_fe_face_values,
613 const unsigned int n_dofs,
614 const IndexSet &refinement_edge_indices,
615 const unsigned int level,
616 std::multimap<
617 VectorDoFTuple<dim>,
618 std::pair<Tensor<1, dim>,
619 typename DoFHandler<dim, spacedim>::cell_iterator>>
620 &dof_to_normals_map,
621 std::map<VectorDoFTuple<dim>, Vector<number>> &dof_vector_to_b_values,
622 const bool use_manifold_for_normal)
623 {
624 // mapping from (active_fe_index, face_no and local
625 // dof index) to dim vector indices of the same
626 // supporting point.
627 std::map<FaceDoFInfo, std::array<unsigned int, dim>> dof_to_vector_dof;
628 map_face_dof_to_vector_dof<dim, spacedim>(cell,
629 first_vector_component,
630 boundary_ids,
631 x_fe_face_values,
632 refinement_edge_indices,
633 level,
634 dof_to_vector_dof);
635
636
637 std::set<types::boundary_id>::iterator b_id;
638 for (const unsigned int face_no : cell->face_indices())
639 if ((b_id = boundary_ids.find(cell->face(face_no)->boundary_id())) !=
640 boundary_ids.end())
641 {
642 const FiniteElement<dim> &fe = cell->get_fe();
643 typename DoFHandler<dim, spacedim>::level_face_iterator face =
644 cell->face(face_no);
645
646 std::vector<types::global_dof_index> face_dofs;
647 // get the indices of the dofs on this cell...
648 face_dofs.resize(fe.n_dofs_per_face(face_no));
649
650 if (level != numbers::invalid_unsigned_int)
651 face->get_mg_dof_indices(level,
652 face_dofs,
653 cell->active_fe_index());
654 else
655 face->get_dof_indices(face_dofs, cell->active_fe_index());
656
657 x_fe_face_values.reinit(cell, face_no);
658 const FEFaceValues<dim> &fe_values =
659 x_fe_face_values.get_present_fe_values();
660
661 // then identify which of them correspond to the selected set of
662 // vector components
663 for (unsigned int i = 0; i < face_dofs.size(); ++i)
664 if (fe.face_system_to_component_index(i, face_no).first ==
665 first_vector_component)
666 // Refinement edge indices are going to be constrained to 0
667 // during a multigrid cycle and do not need no-normal-flux
668 // constraints, so skip them:
669 if (level == numbers::invalid_unsigned_int ||
670 !refinement_edge_indices.is_element(face_dofs[i]))
671 {
672 const FaceDoFInfo face_dof_info = {
673 {{cell->active_fe_index(), face_no, i}}};
674 const auto it = dof_to_vector_dof.find(face_dof_info);
675 Assert(it != dof_to_vector_dof.end(), ExcInternalError());
676 const std::array<unsigned int, dim> local_vector_indices =
677 it->second;
678
679 Assert(
680 first_vector_component + dim <= fe.n_components(),
681 ExcMessage(
682 "Error: the finite element does not have enough components "
683 "to define a normal direction."));
684
685 VectorDoFTuple<dim> vector_dofs;
686 for (unsigned int d = 0; d < dim; ++d)
687 {
688 vector_dofs.dof_indices[d] =
689 face_dofs[local_vector_indices[d]];
690 }
691
692 for (unsigned int d = 0; d < dim; ++d)
693 Assert(vector_dofs.dof_indices[d] < n_dofs,
694 ExcInternalError());
695
696 // we need the normal vector on this face. we know that
697 // it is a vector of length 1 but at least with higher
698 // order mappings it isn't always possible to guarantee
699 // that each component is exact up to zero tolerance. in
700 // particular, as shown in the deal.II/no_flux_06 test,
701 // if we just take the normal vector as given by the
702 // fe_values object, we can get entries in the normal
703 // vectors of the unit cube that have entries up to
704 // several times 1e-14.
705 //
706 // the problem with this is that this later yields
707 // constraints that are circular (e.g., in the testcase,
708 // we get constraints of the form
709 //
710 // x22 = 2.93099e-14*x21 + 2.93099e-14*x23
711 // x21 = -2.93099e-14*x22 + 2.93099e-14*x21
712 //
713 // in both of these constraints, the small numbers
714 // should be zero and the constraints should simply be
715 // x22 = x21 = 0
716 //
717 // to achieve this, we utilize that we know that the
718 // normal vector has (or should have) length 1 and that
719 // we can simply set small elements to zero (without
720 // having to check that they are small *relative to
721 // something else*). we do this and then normalize the
722 // length of the vector back to one, just to be on the
723 // safe side
724 //
725 // one more point: we would like to use the "real"
726 // normal vector here, as provided by the boundary
727 // description and as opposed to what we get from the
728 // FEValues object. we do this in the immediately next
729 // line, but as is obvious, the boundary only has a
730 // vague idea which side of a cell it is on -- indicated
731 // by the face number. in other words, it may provide
732 // the inner or outer normal. by and large, there is no
733 // harm from this, since the tangential vector we
734 // compute is still the same. however, we do average
735 // over normal vectors from adjacent cells and if they
736 // have recorded normal vectors from the inside once and
737 // from the outside the other time, then this averaging
738 // is going to run into trouble. as a consequence we ask
739 // the mapping after all for its normal vector, but we
740 // only ask it so that we can possibly correct the sign
741 // of the normal vector provided by the boundary if they
742 // should point in different directions. this is the
743 // case in tests/deal.II/no_flux_11.
744 Tensor<1, dim> normal_vector;
745
746 if (use_manifold_for_normal)
747 {
748 // if we use high-order manifold ask it for the normal
749 normal_vector =
750 cell->face(face_no)->get_manifold().normal_vector(
751 cell->face(face_no), fe_values.quadrature_point(i));
752
753 if (normal_vector * fe_values.normal_vector(i) < 0)
754 normal_vector *= -1;
755 }
756 else
757 {
758 // else ask mapping via FEFaceValues
759 normal_vector = fe_values.normal_vector(i);
760 }
761
762 Assert(std::fabs(normal_vector.norm() - 1) < 1e-14,
763 ExcInternalError());
764 for (unsigned int d = 0; d < dim; ++d)
765 if (std::fabs(normal_vector[d]) < 1e-13)
766 normal_vector[d] = 0;
767 normal_vector /= normal_vector.norm();
768
769 const Point<dim> &point = fe_values.quadrature_point(i);
770 Vector<number> b_values(dim);
771 function_map.at(*b_id)->vector_value(point, b_values);
772
773 // now enter the (dofs,(normal_vector,cell)) entry into
774 // the map
775 dof_to_normals_map.insert(
776 std::make_pair(vector_dofs,
777 std::make_pair(normal_vector, cell)));
778 dof_vector_to_b_values.insert(
779 std::make_pair(vector_dofs, b_values));
780
781 #ifdef DEBUG_NO_NORMAL_FLUX
782 std::cout << "Adding normal vector:" << std::endl
783 << " dofs=" << vector_dofs << std::endl
784 << " cell=" << cell << " at " << cell->center()
785 << std::endl
786 << " normal=" << normal_vector << std::endl;
787 #endif
788 }
789 }
790 }
791
792
793
794 /**
795 * This is the internal function that computes the nonzero normal
796 * flux constraints on active cells
797 * if @p level is an invalid unsigned integer or level cells if the cell level is provided.
798 * It's called by compute_nonzero_normal_flux_constraints() and
799 * compute_nonzero_normal_flux_constraints_on_level() so as to have
800 * separate interfaces for the active and level cells.
801 */
802 template <int dim, int spacedim, typename number>
803 void
804 compute_nonzero_normal_flux_constraints_active_or_level(
805 const DoFHandler<dim, spacedim> &dof_handler,
806 const unsigned int first_vector_component,
807 const std::set<types::boundary_id> &boundary_ids,
808 const std::map<types::boundary_id, const Function<spacedim, number> *>
809 &function_map,
810 AffineConstraints<number> &constraints,
811 const Mapping<dim, spacedim> &mapping,
812 const bool use_manifold_for_normal,
813 const IndexSet &refinement_edge_indices = IndexSet(),
814 const unsigned int level = numbers::invalid_unsigned_int)
815 {
816 Assert(dim > 1,
817 ExcMessage("This function is not useful in 1d because it amounts "
818 "to imposing Dirichlet values on the vector-valued "
819 "quantity."));
820
821 // create FE and mapping collections for all elements in use by this
822 // DoFHandler
823 const hp::FECollection<dim, spacedim> &fe_collection =
824 dof_handler.get_fe_collection();
825 hp::MappingCollection<dim, spacedim> mapping_collection;
826 for (unsigned int i = 0; i < fe_collection.size(); ++i)
827 mapping_collection.push_back(mapping);
828
829 // TODO: the implementation makes the assumption that all faces have the
830 // same number of dofs
831 AssertDimension(dof_handler.get_fe().n_unique_faces(), 1);
832 const unsigned int face_no = 0;
833
834 // now also create a quadrature collection for the faces of a cell. fill
835 // it with a quadrature formula with the support points on faces for each
836 // FE
837 hp::QCollection<dim - 1> face_quadrature_collection;
838 for (unsigned int i = 0; i < fe_collection.size(); ++i)
839 {
840 const std::vector<Point<dim - 1>> &unit_support_points =
841 fe_collection[i].get_unit_face_support_points(face_no);
842
843 Assert(unit_support_points.size() ==
844 fe_collection[i].n_dofs_per_face(face_no),
845 ExcInternalError());
846
847 face_quadrature_collection.push_back(
848 Quadrature<dim - 1>(unit_support_points));
849 }
850
851 // now create the object with which we will generate the normal vectors
852 hp::FEFaceValues<dim, spacedim> x_fe_face_values(
853 mapping_collection,
854 fe_collection,
855 face_quadrature_collection,
856 update_quadrature_points | update_normal_vectors);
857
858 // have a map that stores normal vectors for each vector-dof tuple we want
859 // to constrain. since we can get at the same vector dof tuple more than
860 // once (for example if it is located at a vertex that we visit from all
861 // adjacent cells), we will want to average later on the normal vectors
862 // computed on different cells as described in the documentation of this
863 // function. however, we can only average if the contributions came from
864 // different cells, whereas we want to constrain twice or more in case the
865 // contributions came from different faces of the same cell
866 // (i.e. constrain not just the *average normal direction* but *all normal
867 // directions* we find). consequently, we also have to store which cell a
868 // normal vector was computed on
869 using DoFToNormalsMap = std::multimap<
870 VectorDoFTuple<dim>,
871 std::pair<Tensor<1, dim>,
872 typename DoFHandler<dim, spacedim>::cell_iterator>>;
873 std::map<VectorDoFTuple<dim>, Vector<number>> dof_vector_to_b_values;
874
875 DoFToNormalsMap dof_to_normals_map;
876
877 const unsigned int n_dof = dof_handler.n_dofs();
878
879 if (level == numbers::invalid_unsigned_int)
880 {
881 // active cells
882 for (const auto &cell : dof_handler.active_cell_iterators())
883 if (!cell->is_artificial())
884 {
885 map_dofs_to_normal_vectors_and_normal_fluxes(
886 cell,
887 first_vector_component,
888 boundary_ids,
889 function_map,
890 x_fe_face_values,
891 n_dof,
892 refinement_edge_indices,
893 level,
894 dof_to_normals_map,
895 dof_vector_to_b_values,
896 use_manifold_for_normal);
897 }
898 }
899 else
900 {
901 // level cells
902 for (const auto &cell : dof_handler.cell_iterators_on_level(level))
903 if (cell->level_subdomain_id() !=
904 numbers::artificial_subdomain_id &&
905 cell->level_subdomain_id() != numbers::invalid_subdomain_id)
906 {
907 map_dofs_to_normal_vectors_and_normal_fluxes(
908 cell,
909 first_vector_component,
910 boundary_ids,
911 function_map,
912 x_fe_face_values,
913 n_dof,
914 refinement_edge_indices,
915 level,
916 dof_to_normals_map,
917 dof_vector_to_b_values,
918 use_manifold_for_normal);
919 }
920 }
921
922 // Now do something with the collected information. To this end, loop
923 // through all sets of pairs (dofs,normal_vector) and identify which
924 // entries belong to the same set of dofs and then do as described in the
925 // documentation, i.e. either average the normal vector or don't for this
926 // particular set of dofs
927 typename DoFToNormalsMap::const_iterator p = dof_to_normals_map.begin();
928
929 while (p != dof_to_normals_map.end())
930 {
931 // first find the range of entries in the multimap that corresponds to
932 // the same vector-dof tuple. as usual, we define the range
933 // half-open. the first entry of course is 'p'
934 typename DoFToNormalsMap::const_iterator same_dof_range[2] = {p};
935 for (++p; p != dof_to_normals_map.end(); ++p)
936 if (p->first != same_dof_range[0]->first)
937 {
938 same_dof_range[1] = p;
939 break;
940 }
941 if (p == dof_to_normals_map.end())
942 same_dof_range[1] = dof_to_normals_map.end();
943
944 #ifdef DEBUG_NO_NORMAL_FLUX
945 std::cout << "For dof indices <" << p->first
946 << ">, found the following normals" << std::endl;
947 for (typename DoFToNormalsMap::const_iterator q = same_dof_range[0];
948 q != same_dof_range[1];
949 ++q)
950 std::cout << " " << q->second.first << " from cell "
951 << q->second.second << std::endl;
952 #endif
953
954
955 // now compute the reverse mapping: for each of the cells that
956 // contributed to the current set of vector dofs, add up the normal
957 // vectors. the values of the map are pairs of normal vectors and
958 // number of cells that have contributed
959 using CellToNormalsMap =
960 std::map<typename DoFHandler<dim, spacedim>::cell_iterator,
961 std::pair<Tensor<1, dim>, unsigned int>>;
962
963 CellToNormalsMap cell_to_normals_map;
964 for (typename DoFToNormalsMap::const_iterator q = same_dof_range[0];
965 q != same_dof_range[1];
966 ++q)
967 if (cell_to_normals_map.find(q->second.second) ==
968 cell_to_normals_map.end())
969 cell_to_normals_map[q->second.second] =
970 std::make_pair(q->second.first, 1U);
971 else
972 {
973 const Tensor<1, dim> old_normal =
974 cell_to_normals_map[q->second.second].first;
975 const unsigned int old_count =
976 cell_to_normals_map[q->second.second].second;
977
978 Assert(old_count > 0, ExcInternalError());
979
980 // in the same entry, store again the now averaged normal vector
981 // and the new count
982 cell_to_normals_map[q->second.second] =
983 std::make_pair((old_normal * old_count + q->second.first) /
984 (old_count + 1),
985 old_count + 1);
986 }
987 Assert(cell_to_normals_map.size() >= 1, ExcInternalError());
988
989 #ifdef DEBUG_NO_NORMAL_FLUX
990 std::cout << " cell_to_normals_map:" << std::endl;
991 for (typename CellToNormalsMap::const_iterator x =
992 cell_to_normals_map.begin();
993 x != cell_to_normals_map.end();
994 ++x)
995 std::cout << " " << x->first << " -> (" << x->second.first
996 << ',' << x->second.second << ')' << std::endl;
997 #endif
998
999 // count the maximum number of contributions from each cell
1000 unsigned int max_n_contributions_per_cell = 1;
1001 for (typename CellToNormalsMap::const_iterator x =
1002 cell_to_normals_map.begin();
1003 x != cell_to_normals_map.end();
1004 ++x)
1005 max_n_contributions_per_cell =
1006 std::max(max_n_contributions_per_cell, x->second.second);
1007
1008 // verify that each cell can have only contributed at most dim times,
1009 // since that is the maximum number of faces that come together at a
1010 // single place
1011 Assert(max_n_contributions_per_cell <= dim, ExcInternalError());
1012
1013 switch (max_n_contributions_per_cell)
1014 {
1015 // first deal with the case that a number of cells all have
1016 // registered that they have a normal vector defined at the
1017 // location of a given vector dof, and that each of them have
1018 // encountered this vector dof exactly once while looping over all
1019 // their faces. as stated in the documentation, this is the case
1020 // where we want to simply average over all normal vectors
1021 //
1022 // the typical case is in 2d where multiple cells meet at one
1023 // vertex sitting on the boundary. same in 3d for a vertex that
1024 // is associated with only one of the boundary indicators passed
1025 // to this function
1026 case 1:
1027 {
1028 // compute the average normal vector from all the ones that
1029 // have the same set of dofs. we could add them up and divide
1030 // them by the number of additions, or simply normalize them
1031 // right away since we want them to have unit length anyway
1032 Tensor<1, dim> normal;
1033 for (typename CellToNormalsMap::const_iterator x =
1034 cell_to_normals_map.begin();
1035 x != cell_to_normals_map.end();
1036 ++x)
1037 normal += x->second.first;
1038 normal /= normal.norm();
1039
1040 // normalize again
1041 for (unsigned int d = 0; d < dim; ++d)
1042 if (std::fabs(normal[d]) < 1e-13)
1043 normal[d] = 0;
1044 normal /= normal.norm();
1045
1046 // then construct constraints from this:
1047 const VectorDoFTuple<dim> &dof_indices =
1048 same_dof_range[0]->first;
1049 number normal_value = 0.;
1050 const Vector<number> b_values =
1051 dof_vector_to_b_values[dof_indices];
1052 for (unsigned int i = 0; i < dim; ++i)
1053 normal_value += b_values[i] * normal[i];
1054 add_constraint(dof_indices,
1055 normal,
1056 constraints,
1057 normal_value);
1058
1059 break;
1060 }
1061
1062 // this is the slightly more complicated case that a single cell
1063 // has contributed with exactly DIM normal vectors to the same set
1064 // of vector dofs. this is what happens in a corner in 2d and 3d
1065 // (but not on an edge in 3d, where we have only 2, i.e. <DIM,
1066 // contributions. Here we do not want to average the normal
1067 // vectors. Since we have DIM contributions, let's assume (and
1068 // verify) that they are in fact all linearly independent; in that
1069 // case, all vector components are constrained and we need to set
1070 // all of them to the corresponding boundary values
1071 case dim:
1072 {
1073 // assert that indeed only a single cell has contributed
1074 Assert(cell_to_normals_map.size() == 1, ExcInternalError());
1075
1076 // check linear independence by computing the determinant of
1077 // the matrix created from all the normal vectors. if they are
1078 // linearly independent, then the determinant is nonzero. if
1079 // they are orthogonal, then the matrix is in fact equal to 1
1080 // (since they are all unit vectors); make sure the
1081 // determinant is larger than 1e-3 to avoid cases where cells
1082 // are degenerate
1083 {
1084 Tensor<2, dim> t;
1085
1086 typename DoFToNormalsMap::const_iterator x =
1087 same_dof_range[0];
1088 for (unsigned int i = 0; i < dim; ++i, ++x)
1089 for (unsigned int j = 0; j < dim; ++j)
1090 t[i][j] = x->second.first[j];
1091
1092 Assert(
1093 std::fabs(determinant(t)) > 1e-3,
1094 ExcMessage(
1095 "Found a set of normal vectors that are nearly collinear."));
1096 }
1097
1098 // so all components of this vector dof are constrained. enter
1099 // this into the AffineConstraints object
1100 //
1101 // ignore dofs already constrained
1102 const VectorDoFTuple<dim> &dof_indices =
1103 same_dof_range[0]->first;
1104 const Vector<number> b_values =
1105 dof_vector_to_b_values[dof_indices];
1106 for (unsigned int i = 0; i < dim; ++i)
1107 if (!constraints.is_constrained(
1108 same_dof_range[0]->first.dof_indices[i]) &&
1109 constraints.can_store_line(
1110 same_dof_range[0]->first.dof_indices[i]))
1111 {
1112 constraints.add_constraint(
1113 dof_indices.dof_indices[i],
1114 {},
1115 (std::fabs(b_values[i]) >
1116 std::numeric_limits<number>::epsilon() ?
1117 b_values[i] :
1118 0));
1119 }
1120
1121 break;
1122 }
1123
1124 // this is the case of an edge contribution in 3d, i.e. the vector
1125 // is constrained in two directions but not the third.
1126 default:
1127 {
1128 Assert(dim >= 3, ExcNotImplemented());
1129 Assert(max_n_contributions_per_cell == 2, ExcInternalError());
1130
1131 // as described in the documentation, let us first collect
1132 // what each of the cells contributed at the current point. we
1133 // use a std::list instead of a std::set (which would be more
1134 // natural) because std::set requires that the stored elements
1135 // are comparable with operator<
1136 using CellContributions =
1137 std::map<typename DoFHandler<dim, spacedim>::cell_iterator,
1138 std::list<Tensor<1, dim>>>;
1139 CellContributions cell_contributions;
1140
1141 for (typename DoFToNormalsMap::const_iterator q =
1142 same_dof_range[0];
1143 q != same_dof_range[1];
1144 ++q)
1145 cell_contributions[q->second.second].push_back(
1146 q->second.first);
1147 Assert(cell_contributions.size() >= 1, ExcInternalError());
1148
1149 // now for each cell that has contributed determine the number
1150 // of normal vectors it has contributed. we currently only
1151 // implement if this is dim-1 for all cells (if a single cell
1152 // has contributed dim, or if all adjacent cells have
1153 // contributed 1 normal vector, this is already handled
1154 // above).
1155 //
1156 // we only implement the case that all cells contribute
1157 // dim-1 because we assume that we are following an edge
1158 // of the domain (think: we are looking at a vertex
1159 // located on one of the edges of a refined cube where the
1160 // boundary indicators of the two adjacent faces of the
1161 // cube are both listed in the set of boundary indicators
1162 // passed to this function). in that case, all cells along
1163 // that edge of the domain are assumed to have contributed
1164 // dim-1 normal vectors. however, there are cases where
1165 // this assumption is not justified (see the lengthy
1166 // explanation in test no_flux_12.cc) and in those cases
1167 // we simply ignore the cell that contributes only
1168 // once. this is also discussed at length in the
1169 // documentation of this function.
1170 //
1171 // for each contributing cell compute the tangential vector
1172 // that remains unconstrained
1173 std::list<Tensor<1, dim>> tangential_vectors;
1174 for (typename CellContributions::const_iterator contribution =
1175 cell_contributions.begin();
1176 contribution != cell_contributions.end();
1177 ++contribution)
1178 {
1179 #ifdef DEBUG_NO_NORMAL_FLUX
1180 std::cout
1181 << " Treating edge case with dim-1 contributions."
1182 << std::endl
1183 << " Looking at cell " << contribution->first
1184 << " which has contributed these normal vectors:"
1185 << std::endl;
1186 for (typename std::list<Tensor<1, dim>>::const_iterator
1187 t = contribution->second.begin();
1188 t != contribution->second.end();
1189 ++t)
1190 std::cout << " " << *t << std::endl;
1191 #endif
1192
1193 // as mentioned above, simply ignore cells that only
1194 // contribute once
1195 if (contribution->second.size() < dim - 1)
1196 continue;
1197
1198 Tensor<1, dim> normals[dim - 1];
1199 {
1200 unsigned int index = 0;
1201 for (typename std::list<Tensor<1, dim>>::const_iterator
1202 t = contribution->second.begin();
1203 t != contribution->second.end();
1204 ++t, ++index)
1205 normals[index] = *t;
1206 Assert(index == dim - 1, ExcInternalError());
1207 }
1208
1209 // calculate the tangent as the outer product of the
1210 // normal vectors. since these vectors do not need to be
1211 // orthogonal (think, for example, the case of the
1212 // deal.II/no_flux_07 test: a sheared cube in 3d, with Q2
1213 // elements, where we have constraints from the two normal
1214 // vectors of two faces of the sheared cube that are not
1215 // perpendicular to each other), we have to normalize the
1216 // outer product
1217 Tensor<1, dim> tangent;
1218 switch (dim)
1219 {
1220 case 3:
1221 // take cross product between normals[0] and
1222 // normals[1]. write it in the current form (with
1223 // [dim-2]) to make sure that compilers don't warn
1224 // about out-of-bounds accesses -- the warnings are
1225 // bogus since we get here only for dim==3, but at
1226 // least one isn't quite smart enough to notice this
1227 // and warns when compiling the function in 2d
1228 tangent =
1229 cross_product_3d(normals[0], normals[dim - 2]);
1230 break;
1231 default:
1232 DEAL_II_NOT_IMPLEMENTED();
1233 }
1234
1235 Assert(
1236 std::fabs(tangent.norm()) > 1e-12,
1237 ExcMessage(
1238 "Two normal vectors from adjacent faces are almost "
1239 "parallel."));
1240 tangent /= tangent.norm();
1241
1242 tangential_vectors.push_back(tangent);
1243 }
1244
1245 // go through the list of tangents and make sure that they all
1246 // roughly point in the same direction as the first one (i.e.
1247 // have an angle less than 90 degrees); if they don't then
1248 // flip their sign
1249 {
1250 const Tensor<1, dim> first_tangent =
1251 tangential_vectors.front();
1252 typename std::list<Tensor<1, dim>>::iterator t =
1253 tangential_vectors.begin();
1254 ++t;
1255 for (; t != tangential_vectors.end(); ++t)
1256 if (*t * first_tangent < 0)
1257 *t *= -1;
1258 }
1259
1260 // now compute the average tangent and normalize it
1261 Tensor<1, dim> average_tangent;
1262 for (typename std::list<Tensor<1, dim>>::const_iterator t =
1263 tangential_vectors.begin();
1264 t != tangential_vectors.end();
1265 ++t)
1266 average_tangent += *t;
1267 average_tangent /= average_tangent.norm();
1268
1269 // now all that is left is that we add the constraints that
1270 // the vector is parallel to the tangent
1271 const VectorDoFTuple<dim> &dof_indices =
1272 same_dof_range[0]->first;
1273 const Vector<number> b_values =
1274 dof_vector_to_b_values[dof_indices];
1275 add_tangentiality_constraints(dof_indices,
1276 average_tangent,
1277 constraints,
1278 b_values);
1279 }
1280 }
1281 }
1282 }
1283
1284 } // namespace internal
1285
1286
1287
1288 template <int dim, int spacedim>
1289 void
1290 compute_nonzero_normal_flux_constraints(
1291 const DoFHandler<dim, spacedim> &dof_handler,
1292 const unsigned int first_vector_component,
1293 const std::set<types::boundary_id> &boundary_ids,
1294 const std::map<types::boundary_id, const Function<spacedim> *>
1295 &function_map,
1296 AffineConstraints<double> &constraints,
1297 const Mapping<dim, spacedim> &mapping,
1298 const bool use_manifold_for_normal)
1299 {
1300 internal::compute_nonzero_normal_flux_constraints_active_or_level(
1301 dof_handler,
1302 first_vector_component,
1303 boundary_ids,
1304 function_map,
1305 constraints,
1306 mapping,
1307 use_manifold_for_normal);
1308 }
1309
1310
1311
1312 template <int dim, int spacedim>
1313 void
1314 compute_nonzero_normal_flux_constraints_on_level(
1315 const DoFHandler<dim, spacedim> &dof_handler,
1316 const unsigned int first_vector_component,
1317 const std::set<types::boundary_id> &boundary_ids,
1318 const std::map<types::boundary_id, const Function<spacedim> *>
1319 &function_map,
1320 AffineConstraints<double> &constraints,
1321 const Mapping<dim, spacedim> &mapping,
1322 const IndexSet &refinement_edge_indices,
1323 const unsigned int level,
1324 const bool use_manifold_for_normal)
1325 {
1326 internal::compute_nonzero_normal_flux_constraints_active_or_level(
1327 dof_handler,
1328 first_vector_component,
1329 boundary_ids,
1330 function_map,
1331 constraints,
1332 mapping,
1333 use_manifold_for_normal,
1334 refinement_edge_indices,
1335 level);
1336 }
1337
1338 namespace internal
1339 {
1340 template <int dim>
1341 struct PointComparator
1342 {
1343 bool
1344 operator()(const std::array<types::global_dof_index, dim> &p1,
1345 const std::array<types::global_dof_index, dim> &p2) const
1346 {
1347 for (unsigned int d = 0; d < dim; ++d)
1348 if (p1[d] < p2[d])
1349 return true;
1350 return false;
1351 }
1352 };
1353 } // namespace internal
1354
1355 template <int dim, int spacedim>
1356 void
1357 compute_nonzero_tangential_flux_constraints(
1358 const DoFHandler<dim, spacedim> &dof_handler,
1359 const unsigned int first_vector_component,
1360 const std::set<types::boundary_id> &boundary_ids,
1361 const std::map<types::boundary_id, const Function<spacedim> *>
1362 &function_map,
1363 AffineConstraints<double> &constraints,
1364 const Mapping<dim, spacedim> &mapping,
1365 const bool use_manifold_for_normal)
1366 {
1367 AffineConstraints<double> no_normal_flux_constraints(
1368 constraints.get_locally_owned_indices(), constraints.get_local_lines());
1369 compute_nonzero_normal_flux_constraints(dof_handler,
1370 first_vector_component,
1371 boundary_ids,
1372 function_map,
1373 no_normal_flux_constraints,
1374 mapping,
1375 use_manifold_for_normal);
1376
1377 const hp::FECollection<dim, spacedim> &fe_collection =
1378 dof_handler.get_fe_collection();
1379 hp::MappingCollection<dim, spacedim> mapping_collection;
1380 for (unsigned int i = 0; i < fe_collection.size(); ++i)
1381 mapping_collection.push_back(mapping);
1382
1383 // TODO: the implementation makes the assumption that all faces have the
1384 // same number of dofs
1385 AssertDimension(dof_handler.get_fe().n_unique_faces(), 1);
1386 const unsigned int face_no = 0;
1387
1388 // now also create a quadrature collection for the faces of a cell. fill
1389 // it with a quadrature formula with the support points on faces for each
1390 // FE
1391 hp::QCollection<dim - 1> face_quadrature_collection;
1392 for (unsigned int i = 0; i < fe_collection.size(); ++i)
1393 {
1394 const std::vector<Point<dim - 1>> &unit_support_points =
1395 fe_collection[i].get_unit_face_support_points(face_no);
1396
1397 Assert(unit_support_points.size() ==
1398 fe_collection[i].n_dofs_per_face(face_no),
1399 ExcInternalError());
1400
1401 face_quadrature_collection.push_back(
1402 Quadrature<dim - 1>(unit_support_points));
1403 }
1404
1405 // now create the object with which we will generate the normal vectors
1406 hp::FEFaceValues<dim, spacedim> x_fe_face_values(mapping_collection,
1407 fe_collection,
1408 face_quadrature_collection,
1409 update_quadrature_points |
1410 update_normal_vectors);
1411
1412 // Extract a list that collects all vector components that belong to the
1413 // same node (scalar basis function). When creating that list, we use an
1414 // array of dim components that stores the global degree of freedom.
1415 std::set<std::array<types::global_dof_index, dim>,
1416 internal::PointComparator<dim>>
1417 vector_dofs;
1418 std::vector<types::global_dof_index> face_dofs;
1419
1420 std::map<std::array<types::global_dof_index, dim>, Vector<double>>
1421 dof_vector_to_b_values;
1422
1423 std::set<types::boundary_id>::iterator b_id;
1424 std::vector<std::array<types::global_dof_index, dim>> cell_vector_dofs;
1425 for (const auto &cell : dof_handler.active_cell_iterators())
1426 if (!cell->is_artificial())
1427 for (const unsigned int face_no : cell->face_indices())
1428 if ((b_id = boundary_ids.find(cell->face(face_no)->boundary_id())) !=
1429 boundary_ids.end())
1430 {
1431 const FiniteElement<dim> &fe = cell->get_fe();
1432 typename DoFHandler<dim, spacedim>::face_iterator face =
1433 cell->face(face_no);
1434
1435 // get the indices of the dofs on this cell...
1436 face_dofs.resize(fe.n_dofs_per_face(face_no));
1437 face->get_dof_indices(face_dofs, cell->active_fe_index());
1438
1439 x_fe_face_values.reinit(cell, face_no);
1440 const FEFaceValues<dim> &fe_values =
1441 x_fe_face_values.get_present_fe_values();
1442
1443 std::map<types::global_dof_index, double> dof_to_b_value;
1444
1445 unsigned int n_scalar_indices = 0;
1446 cell_vector_dofs.resize(fe.n_dofs_per_face(face_no));
1447 for (unsigned int i = 0; i < fe.n_dofs_per_face(face_no); ++i)
1448 {
1449 if (fe.face_system_to_component_index(i, face_no).first >=
1450 first_vector_component &&
1451 fe.face_system_to_component_index(i, face_no).first <
1452 first_vector_component + dim)
1453 {
1454 const unsigned int component =
1455 fe.face_system_to_component_index(i, face_no).first -
1456 first_vector_component;
1457 n_scalar_indices = std::max(
1458 n_scalar_indices,
1459 fe.face_system_to_component_index(i, face_no).second +
1460 1);
1461 cell_vector_dofs[fe.face_system_to_component_index(
1462 i, face_no)
1463 .second][component] = face_dofs[i];
1464
1465 const Point<dim> &point = fe_values.quadrature_point(i);
1466 const double b_value =
1467 function_map.at(*b_id)->value(point, component);
1468 dof_to_b_value.insert(
1469 std::make_pair(face_dofs[i], b_value));
1470 }
1471 }
1472
1473 // now we identified the vector indices on the cell, so next
1474 // insert them into the set (it would be expensive to directly
1475 // insert incomplete points into the set)
1476 for (unsigned int i = 0; i < n_scalar_indices; ++i)
1477 {
1478 vector_dofs.insert(cell_vector_dofs[i]);
1479 Vector<double> b_values(dim);
1480 for (unsigned int j = 0; j < dim; ++j)
1481 b_values[j] = dof_to_b_value[cell_vector_dofs[i][j]];
1482 dof_vector_to_b_values.insert(
1483 std::make_pair(cell_vector_dofs[i], b_values));
1484 }
1485 }
1486
1487 // iterate over the list of all vector components we found and see if we
1488 // can find constrained ones
1489 unsigned int n_total_constraints_found = 0;
1490 for (const auto &dofs : vector_dofs)
1491 {
1492 unsigned int n_constraints = 0;
1493 bool is_constrained[dim];
1494 for (unsigned int d = 0; d < dim; ++d)
1495 if (no_normal_flux_constraints.is_constrained(dofs[d]))
1496 {
1497 is_constrained[d] = true;
1498 ++n_constraints;
1499 ++n_total_constraints_found;
1500 }
1501 else
1502 is_constrained[d] = false;
1503 if (n_constraints > 0)
1504 {
1505 // if more than one no-flux constraint is present, we need to
1506 // constrain all vector degrees of freedom (we are in a corner
1507 // where several faces meet and to get a continuous FE solution we
1508 // need to set all conditions corresponding to the boundary
1509 // function.).
1510 if (n_constraints > 1)
1511 {
1512 const Vector<double> b_value = dof_vector_to_b_values[dofs];
1513 for (unsigned int d = 0; d < dim; ++d)
1514 constraints.add_constraint(dofs[d], {}, b_value(d));
1515 continue;
1516 }
1517
1518 // ok, this is a no-flux constraint, so get the index of the dof
1519 // that is currently constrained and make it unconstrained. The
1520 // constraint indices will get the normal that contain the other
1521 // indices.
1522 Tensor<1, dim> normal;
1523 unsigned int constrained_index = numbers::invalid_unsigned_int;
1524 for (unsigned int d = 0; d < dim; ++d)
1525 if (is_constrained[d])
1526 {
1527 constrained_index = d;
1528 normal[d] = 1.;
1529 }
1530 AssertIndexRange(constrained_index, dim);
1531 const std::vector<std::pair<types::global_dof_index, double>>
1532 *constrained = no_normal_flux_constraints.get_constraint_entries(
1533 dofs[constrained_index]);
1534 // find components to which this index is constrained to
1535 Assert(constrained != nullptr, ExcInternalError());
1536 Assert(constrained->size() < dim, ExcInternalError());
1537 for (const auto &entry : *constrained)
1538 {
1539 int index = -1;
1540 for (unsigned int d = 0; d < dim; ++d)
1541 if (entry.first == dofs[d])
1542 index = d;
1543 Assert(index != -1, ExcInternalError());
1544 normal[index] = entry.second;
1545 }
1546 Vector<double> boundary_value = dof_vector_to_b_values[dofs];
1547 for (unsigned int d = 0; d < dim; ++d)
1548 {
1549 if (is_constrained[d])
1550 continue;
1551 const unsigned int new_index = dofs[d];
1552 if (!constraints.is_constrained(new_index))
1553 {
1554 if (std::abs(normal[d]) > 1e-13)
1555 constraints.add_constraint(new_index,
1556 {{dofs[constrained_index],
1557 -normal[d]}},
1558 boundary_value[d]);
1559 else
1560 constraints.add_constraint(new_index,
1561 {},
1562 boundary_value[d]);
1563 }
1564 }
1565 }
1566 }
1567 AssertDimension(n_total_constraints_found,
1568 no_normal_flux_constraints.n_constraints());
1569 }
1570
1571
1572 template <int dim, int spacedim, typename number>
1573 void
1574 compute_no_normal_flux_constraints(
1575 const DoFHandler<dim, spacedim> &dof_handler,
1576 const unsigned int first_vector_component,
1577 const std::set<types::boundary_id> &boundary_ids,
1578 AffineConstraints<number> &constraints,
1579 const Mapping<dim, spacedim> &mapping,
1580 const bool use_manifold_for_normal)
1581 {
1582 Functions::ZeroFunction<dim, number> zero_function(dim);
1583 std::map<types::boundary_id, const Function<spacedim, number> *>
1584 function_map;
1585 for (const types::boundary_id boundary_id : boundary_ids)
1586 function_map[boundary_id] = &zero_function;
1587 internal::compute_nonzero_normal_flux_constraints_active_or_level(
1588 dof_handler,
1589 first_vector_component,
1590 boundary_ids,
1591 function_map,
1592 constraints,
1593 mapping,
1594 use_manifold_for_normal);
1595 }
1596
1597
1598
1599 template <int dim, int spacedim>
1600 void
1601 compute_no_normal_flux_constraints_on_level(
1602 const DoFHandler<dim, spacedim> &dof_handler,
1603 const unsigned int first_vector_component,
1604 const std::set<types::boundary_id> &boundary_ids,
1605 AffineConstraints<double> &constraints,
1606 const Mapping<dim, spacedim> &mapping,
1607 const IndexSet &refinement_edge_indices,
1608 const unsigned int level,
1609 const bool use_manifold_for_normal)
1610 {
1611 Functions::ZeroFunction<dim> zero_function(dim);
1612 std::map<types::boundary_id, const Function<spacedim> *> function_map;
1613 for (const types::boundary_id boundary_id : boundary_ids)
1614 function_map[boundary_id] = &zero_function;
1615 internal::compute_nonzero_normal_flux_constraints_active_or_level(
1616 dof_handler,
1617 first_vector_component,
1618 boundary_ids,
1619 function_map,
1620 constraints,
1621 mapping,
1622 use_manifold_for_normal,
1623 refinement_edge_indices,
1624 level);
1625 }
1626
1627
1628
1629 template <int dim, int spacedim>
1630 void
1631 compute_normal_flux_constraints(
1632 const DoFHandler<dim, spacedim> &dof_handler,
1633 const unsigned int first_vector_component,
1634 const std::set<types::boundary_id> &boundary_ids,
1635 AffineConstraints<double> &constraints,
1636 const Mapping<dim, spacedim> &mapping,
1637 const bool use_manifold_for_normal)
1638 {
1639 Functions::ZeroFunction<dim> zero_function(dim);
1640 std::map<types::boundary_id, const Function<spacedim> *> function_map;
1641 for (const types::boundary_id boundary_id : boundary_ids)
1642 function_map[boundary_id] = &zero_function;
1643 compute_nonzero_tangential_flux_constraints(dof_handler,
1644 first_vector_component,
1645 boundary_ids,
1646 function_map,
1647 constraints,
1648 mapping,
1649 use_manifold_for_normal);
1650 }
1651 } // namespace VectorTools
1652
1653 DEAL_II_NAMESPACE_CLOSE
1654
1655 #endif // dealii_vector_tools_constraints_templates_h