deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
manifold.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2014 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#include <deal.II/base/table.h>
14#include <deal.II/base/tensor.h>
16
17#include <deal.II/fe/fe_q.h>
18
21#include <deal.II/grid/tria.h>
24
25#include <boost/container/small_vector.hpp>
26
27#include <algorithm>
28#include <cmath>
29#include <limits>
30#include <memory>
31#include <numeric>
32
33
35
36/* -------------------------- Manifold --------------------- */
37#ifndef DOXYGEN
38
39template <int dim, int spacedim>
42 const ArrayView<const Point<spacedim>> &,
43 const Point<spacedim> &) const
44{
46 return {};
47}
48
49
50
51template <int dim, int spacedim>
54 const Point<spacedim> &p2,
55 const double w) const
56{
57 const std::array<Point<spacedim>, 2> vertices{{p1, p2}};
58 return project_to_manifold(make_array_view(vertices), w * p2 + (1 - w) * p1);
59}
60
61
62
63template <int dim, int spacedim>
66 const ArrayView<const Point<spacedim>> &surrounding_points,
67 const ArrayView<const double> &weights) const
68{
69 const double tol = 1e-10;
70 const unsigned int n_points = surrounding_points.size();
71
72 Assert(n_points > 0, ExcMessage("There should be at least one point."));
73
74 Assert(n_points == weights.size(),
76 "There should be as many surrounding points as weights given."));
77
78 Assert(std::abs(std::accumulate(weights.begin(), weights.end(), 0.0) - 1.0) <
79 tol,
80 ExcMessage("The weights for the individual points should sum to 1!"));
81
82 // First sort points in the order of their weights. This is done to
83 // produce unique points even if get_intermediate_points is not
84 // associative (as for the SphericalManifold).
85 boost::container::small_vector<unsigned int, 100> permutation(n_points);
86 std::iota(permutation.begin(), permutation.end(), 0u);
87 std::sort(permutation.begin(),
88 permutation.end(),
89 [&weights](const std::size_t a, const std::size_t b) {
90 return weights[a] < weights[b];
91 });
92
93 // Now loop over points in the order of their associated weight
95 Point<spacedim> p = surrounding_points[permutation[0]];
97 double w = weights[permutation[0]];
98 for (unsigned int i = 1; i < n_points; ++i)
99 {
100 double weight = 0.0;
101 if (std::abs(weights[permutation[i]] + w) < tol)
102 weight = 0.0;
103 else
104 weight = w / (weights[permutation[i]] + w);
105
106 if (std::abs(weight) > 1e-14)
107 {
108 p = get_intermediate_point(p,
109 surrounding_points[permutation[i]],
110 1.0 - weight);
111 }
112 else
113 {
114 p = surrounding_points[permutation[i]];
115 }
116 w += weights[permutation[i]];
117 }
118
119 return p;
120}
121
122
123
124template <int dim, int spacedim>
125void
127 const ArrayView<const Point<spacedim>> &surrounding_points,
128 const Table<2, double> &weights,
129 ArrayView<Point<spacedim>> new_points) const
130{
131 AssertDimension(surrounding_points.size(), weights.size(1));
132
133 for (unsigned int row = 0; row < weights.size(0); ++row)
134 {
135 new_points[row] =
136 get_new_point(surrounding_points, make_array_view(weights, row));
137 }
138}
139
140
141
142template <>
145 const Point<2> &p) const
146{
147 const int spacedim = 2;
148
149 // get the tangent vector from the point 'p' in the direction of the further
150 // one of the two vertices that make up the face of this 2d cell
151 const Tensor<1, spacedim> tangent =
152 ((p - face->vertex(0)).norm_square() > (p - face->vertex(1)).norm_square() ?
153 -get_tangent_vector(p, face->vertex(0)) :
154 get_tangent_vector(p, face->vertex(1)));
155
156 // then rotate it by 90 degrees
157 const Tensor<1, spacedim> normal = cross_product_2d(tangent);
158 return normal / normal.norm();
159}
160
161
162
163template <>
166 const Point<3> &p) const
167{
168 const int spacedim = 3;
169
170 const std::array<Point<spacedim>, 4> vertices{
171 {face->vertex(0), face->vertex(1), face->vertex(2), face->vertex(3)}};
172 const std::array<double, 4> distances{{vertices[0].distance(p),
173 vertices[1].distance(p),
174 vertices[2].distance(p),
175 vertices[3].distance(p)}};
176 const double max_distance =
177 std::max({distances[0], distances[1], distances[2], distances[3]});
178
179 // We need to find two tangential vectors to the given point p, but we do
180 // not know how the point is oriented against the face. We guess the two
181 // directions by assuming a flat topology and take the two directions that
182 // indicate the angle closest to a perpendicular one (i.e., cos(theta) close
183 // to zero). We start with an invalid value but the loops should always find
184 // a value.
185 double abs_cos_angle = std::numeric_limits<double>::max();
186 unsigned int first_index = numbers::invalid_unsigned_int,
187 second_index = numbers::invalid_unsigned_int;
188 for (unsigned int i = 0; i < 3; ++i)
189 if (distances[i] > 1e-8 * max_distance)
190 for (unsigned int j = i + 1; j < 4; ++j)
191 if (distances[j] > 1e-8 * max_distance)
192 {
193 const double new_angle = (p - vertices[i]) * (p - vertices[j]) /
194 (distances[i] * distances[j]);
195 // multiply by factor 0.999 to bias the search in a way that
196 // avoids trouble with roundoff
197 if (std::abs(new_angle) < 0.999 * abs_cos_angle)
198 {
199 abs_cos_angle = std::abs(new_angle);
200 first_index = i;
201 second_index = j;
202 }
203 }
205 ExcMessage("The search for possible directions did not succeed."));
206
207 // Compute tangents and normal for selected vertices
210 Tensor<1, spacedim> normal;
211
212 bool done = false;
213 std::vector<bool> tested_vertices(vertices.size(), false);
214 tested_vertices[first_index] = true;
215 tested_vertices[second_index] = true;
216
217 do
218 {
219 // Compute tangents and normal for selected vertices
220 t1 = get_tangent_vector(p, vertices[first_index]);
221 t2 = get_tangent_vector(p, vertices[second_index]);
222 normal = cross_product_3d(t1, t2);
223
224 // if normal is zero, try some other combination of vertices
225 if (normal.norm_square() < 1e4 * std::numeric_limits<double>::epsilon() *
226 t1.norm_square() * t2.norm_square())
227 {
228 // See if we have tested all vertices already
229 auto first_false =
230 std::find(tested_vertices.begin(), tested_vertices.end(), false);
231 if (first_false == tested_vertices.end())
232 {
233 done = true;
234 }
235 else
236 {
237 *first_false = true;
238 second_index = first_false - tested_vertices.begin();
239 }
240 }
241 else
242 {
243 done = true;
244 }
245 }
246 while (!done);
247
248 Assert(
249 normal.norm_square() > 1e4 * std::numeric_limits<double>::epsilon() *
250 t1.norm_square() * t2.norm_square(),
252 "Manifold::normal_vector was unable to find a suitable combination "
253 "of vertices to compute a normal on this face. We chose the secants "
254 "that are as orthogonal as possible, but tangents appear to be "
255 "linearly dependent. Check for distorted faces in your triangulation."));
256
257 // Now figure out if we need to flip the direction, we do this by comparing
258 // to a reference normal that would be the correct result if all vertices
259 // would lie in a plane
260 const Tensor<1, spacedim> rt1 = vertices[3] - vertices[0];
261 const Tensor<1, spacedim> rt2 = vertices[2] - vertices[1];
262 const Tensor<1, spacedim> reference_normal = cross_product_3d(rt1, rt2);
263
264 if (reference_normal * normal < 0.0)
265 normal *= -1.0;
266
267 return normal / normal.norm();
268}
269
270
271
272template <int dim, int spacedim>
275 const typename Triangulation<dim, spacedim>::face_iterator & /*face*/,
276 const Point<spacedim> & /*p*/) const
277{
279 return Tensor<1, spacedim>();
280}
281
282
283
284template <>
285void
288 FaceVertexNormals &n) const
289{
290 n[0] = cross_product_2d(get_tangent_vector(face->vertex(0), face->vertex(1)));
291 n[1] =
292 -cross_product_2d(get_tangent_vector(face->vertex(1), face->vertex(0)));
293
294 for (unsigned int i = 0; i < 2; ++i)
295 {
296 Assert(n[i].norm() != 0,
297 ExcInternalError("Something went wrong. The "
298 "computed normals have "
299 "zero length."));
300 n[i] /= n[i].norm();
301 }
302}
303
304
305
306template <>
307void
310 FaceVertexNormals &n) const
311{
312 n[0] = cross_product_3d(get_tangent_vector(face->vertex(0), face->vertex(1)),
313 get_tangent_vector(face->vertex(0), face->vertex(2)));
314
315 n[1] = cross_product_3d(get_tangent_vector(face->vertex(1), face->vertex(3)),
316 get_tangent_vector(face->vertex(1), face->vertex(0)));
317
318 n[2] = cross_product_3d(get_tangent_vector(face->vertex(2), face->vertex(0)),
319 get_tangent_vector(face->vertex(2), face->vertex(3)));
320
321 n[3] = cross_product_3d(get_tangent_vector(face->vertex(3), face->vertex(2)),
322 get_tangent_vector(face->vertex(3), face->vertex(1)));
323
324 for (unsigned int i = 0; i < 4; ++i)
325 {
326 Assert(n[i].norm() != 0,
327 ExcInternalError("Something went wrong. The "
328 "computed normals have "
329 "zero length."));
330 n[i] /= n[i].norm();
331 }
332}
333
334
335
336template <int dim, int spacedim>
337void
340 FaceVertexNormals &n) const
341{
342 for (unsigned int v = 0; v < face->reference_cell().n_vertices(); ++v)
343 {
344 n[v] = normal_vector(face, face->vertex(v));
345 n[v] /= n[v].norm();
346 }
347}
348
349
350
351template <int dim, int spacedim>
354 const typename Triangulation<dim, spacedim>::line_iterator &line) const
355{
356 const auto points_weights = Manifolds::get_default_points_and_weights(line);
357 return get_new_point(make_array_view(points_weights.first),
358 make_array_view(points_weights.second));
359}
360
361
362
363template <int dim, int spacedim>
366 const typename Triangulation<dim, spacedim>::quad_iterator &quad) const
367{
368 const auto points_weights = Manifolds::get_default_points_and_weights(quad);
369 return get_new_point(make_array_view(points_weights.first),
370 make_array_view(points_weights.second));
371}
372
373
374
375template <int dim, int spacedim>
378 const typename Triangulation<dim, spacedim>::face_iterator &face) const
379{
380 Assert(dim > 1, ExcImpossibleInDim(dim));
381
382 switch (dim)
383 {
384 case 2:
385 return get_new_point_on_line(face);
386 case 3:
387 return get_new_point_on_quad(face);
388 }
389
390 return {};
391}
392
393
394
395template <int dim, int spacedim>
398 const typename Triangulation<dim, spacedim>::cell_iterator &cell) const
399{
400 switch (dim)
401 {
402 case 1:
403 return get_new_point_on_line(cell);
404 case 2:
405 return get_new_point_on_quad(cell);
406 case 3:
407 return get_new_point_on_hex(cell);
408 }
409
410 return {};
411}
412
413
414
415template <>
419{
420 Assert(false, ExcImpossibleInDim(1));
421 return {};
422}
423
424
425
426template <>
430{
431 Assert(false, ExcImpossibleInDim(1));
432 return {};
433}
434
435
436
437template <>
441{
442 Assert(false, ExcImpossibleInDim(1));
443 return {};
444}
445
446
447
448template <>
452{
453 Assert(false, ExcImpossibleInDim(1));
454 return {};
455}
456
457
458
459template <>
463{
464 Assert(false, ExcImpossibleInDim(1));
465 return {};
466}
467
468
469
470template <>
474{
475 Assert(false, ExcImpossibleInDim(1));
476 return {};
477}
478
479
480
481template <int dim, int spacedim>
484 const typename Triangulation<dim, spacedim>::hex_iterator & /*hex*/) const
485{
486 Assert(false, ExcImpossibleInDim(dim));
487 return {};
488}
489
490
491
492template <>
495 const Triangulation<3, 3>::hex_iterator &hex) const
496{
497 const auto points_weights =
499 return get_new_point(make_array_view(points_weights.first),
500 make_array_view(points_weights.second));
501}
502
503
504
505template <int dim, int spacedim>
508 const Point<spacedim> &x2) const
509{
510 const double epsilon = 1e-8;
511
512 const std::array<Point<spacedim>, 2> points{{x1, x2}};
513 const std::array<double, 2> weights{{epsilon, 1.0 - epsilon}};
514 const Point<spacedim> neighbor_point =
515 get_new_point(make_array_view(points), make_array_view(weights));
516 return (neighbor_point - x1) / epsilon;
517}
518#endif
519/* -------------------------- FlatManifold --------------------- */
520
521namespace internal
522{
523 namespace
524 {
526 normalized_alternating_product(const Tensor<1, 3> (&)[1])
527 {
528 // we get here from FlatManifold<2,3>::normal_vector, but
529 // the implementation below is bogus for this case anyway
530 // (see the assert at the beginning of that function).
532 return {};
533 }
534
535
536
538 normalized_alternating_product(const Tensor<1, 3> (&basis_vectors)[2])
539 {
540 Tensor<1, 3> tmp = cross_product_3d(basis_vectors[0], basis_vectors[1]);
541 return tmp / tmp.norm();
542 }
543
544 } // namespace
545} // namespace internal
546
547#ifndef DOXYGEN
548
549template <int dim, int spacedim>
551 const Tensor<1, spacedim> &periodicity,
552 const double tolerance)
553 : periodicity(periodicity)
554 , tolerance(tolerance)
555{}
556
557
558
559template <int dim, int spacedim>
560std::unique_ptr<Manifold<dim, spacedim>>
562{
563 return std::make_unique<FlatManifold<dim, spacedim>>(periodicity, tolerance);
564}
565
566
567
568template <int dim, int spacedim>
571 const ArrayView<const Point<spacedim>> &surrounding_points,
572 const ArrayView<const double> &weights) const
573{
574 Assert(std::abs(std::accumulate(weights.begin(), weights.end(), 0.0) - 1.0) <
575 1e-10,
576 ExcMessage("The weights for the new point should sum to 1!"));
577
579
580 // if there is no periodicity, use a shortcut
581 if (periodicity == Tensor<1, spacedim>())
582 {
583 for (unsigned int i = 0; i < surrounding_points.size(); ++i)
584 p += surrounding_points[i] * weights[i];
585 }
586 else
587 {
588 Tensor<1, spacedim> minP = periodicity;
589
590 for (unsigned int d = 0; d < spacedim; ++d)
591 if (periodicity[d] > 0)
592 for (unsigned int i = 0; i < surrounding_points.size(); ++i)
593 {
594 minP[d] = std::min(minP[d], surrounding_points[i][d]);
595 Assert((surrounding_points[i][d] <
596 periodicity[d] + tolerance * periodicity[d]) ||
597 (surrounding_points[i][d] >=
598 -tolerance * periodicity[d]),
599 ExcPeriodicBox(d, surrounding_points[i], periodicity[d]));
600 }
601
602 // compute the weighted average point, possibly taking into account
603 // periodicity
604 for (unsigned int i = 0; i < surrounding_points.size(); ++i)
605 {
607 for (unsigned int d = 0; d < spacedim; ++d)
608 if (periodicity[d] > 0)
609 dp[d] =
610 ((surrounding_points[i][d] - minP[d]) > periodicity[d] / 2.0 ?
611 -periodicity[d] :
612 0.0);
613
614 p += (surrounding_points[i] + dp) * weights[i];
615 }
616
617 // if necessary, also adjust the weighted point by the periodicity
618 for (unsigned int d = 0; d < spacedim; ++d)
619 if (periodicity[d] > 0)
620 if (p[d] < 0)
621 p[d] += periodicity[d];
622 }
623
624 return project_to_manifold(surrounding_points, p);
625}
626
627
628
629template <int dim, int spacedim>
630void
632 const ArrayView<const Point<spacedim>> &surrounding_points,
633 const Table<2, double> &weights,
634 ArrayView<Point<spacedim>> new_points) const
635{
636 AssertDimension(surrounding_points.size(), weights.size(1));
637 if (weights.size(0) == 0)
638 return;
639 AssertDimension(new_points.size(), weights.size(0));
640
641 const std::size_t n_points = surrounding_points.size();
642
643 // if there is no periodicity, use an optimized implementation with
644 // VectorizedArray, otherwise go to the get_new_point function for adjusting
645 // the domain
646 if (periodicity == Tensor<1, spacedim>())
647 {
648 for (unsigned int row = 0; row < weights.size(0); ++row)
649 Assert(std::abs(std::accumulate(&weights(row, 0),
650 &weights(row, 0) + n_points,
651 0.0) -
652 1.0) < 1e-10,
653 ExcMessage("The weights for each of the points should sum to "
654 "1!"));
655
656 constexpr std::size_t n_lanes =
657 std::min<std::size_t>(VectorizedArray<double>::size(), 4);
658 using VectorizedArrayType = VectorizedArray<double, n_lanes>;
659 const std::size_t n_regular_cols = (n_points / n_lanes) * n_lanes;
660 for (unsigned int row = 0; row < weights.size(0); row += n_lanes)
661 {
662 std::array<unsigned int, n_lanes> offsets;
663 // ensure to not access out of bounds, possibly duplicating some
664 // entries
665 for (std::size_t i = 0; i < n_lanes; ++i)
666 offsets[i] =
667 std::min<unsigned int>((row + i) * n_points,
668 (weights.size(0) - 1) * n_points);
670 for (std::size_t col = 0; col < n_regular_cols; col += n_lanes)
671 {
672 std::array<VectorizedArrayType, n_lanes> vectorized_weights;
674 &weights(0, 0) + col,
675 offsets.data(),
676 vectorized_weights.data());
677 for (std::size_t i = 0; i < n_lanes; ++i)
678 point += vectorized_weights[i] * surrounding_points[col + i];
679 }
680 for (std::size_t col = n_regular_cols; col < n_points; ++col)
681 {
682 VectorizedArrayType vectorized_weights;
683 vectorized_weights.gather(&weights(0, 0) + col, offsets.data());
684 point += vectorized_weights * surrounding_points[col];
685 }
686 for (unsigned int r = row;
687 r < std::min<unsigned int>(weights.size(0), row + n_lanes);
688 ++r)
689 {
690 // unpack and project to manifold
691 for (unsigned int d = 0; d < spacedim; ++d)
692 new_points[r][d] = point[d][r - row];
693 new_points[r] =
694 project_to_manifold(surrounding_points, new_points[r]);
695 }
696 }
697 }
698 else
699 for (unsigned int row = 0; row < weights.size(0); ++row)
700 new_points[row] =
701 get_new_point(surrounding_points,
702 ArrayView<const double>(&weights(row, 0), n_points));
703}
704
705
706
707template <int dim, int spacedim>
710 const ArrayView<const Point<spacedim>> & /*vertices*/,
711 const Point<spacedim> &candidate) const
712{
713 return candidate;
714}
715
716
717
718template <int dim, int spacedim>
721{
722 return periodicity;
723}
724
725
726
727template <int dim, int spacedim>
730 const Point<spacedim> &x2) const
731{
732 Tensor<1, spacedim> direction = x2 - x1;
733
734 // see if we have to take into account periodicity. if so, we need
735 // to make sure that if a distance in one coordinate direction
736 // is larger than half of the box length, then go the other way
737 // around (i.e., via the periodic box)
738 for (unsigned int d = 0; d < spacedim; ++d)
739 if (periodicity[d] > tolerance)
740 {
741 if (direction[d] < -periodicity[d] / 2)
742 direction[d] += periodicity[d];
743 else if (direction[d] > periodicity[d] / 2)
744 direction[d] -= periodicity[d];
745 }
746
747 return direction;
748}
749
750
751
752template <>
753void
757{
758 Assert(false, ExcImpossibleInDim(1));
759}
760
761
762
763template <>
764void
768{
770}
771
772
773
774template <>
775void
779{
781}
782
783
784
785template <>
786void
789 Manifold<2, 2>::FaceVertexNormals &face_vertex_normals) const
790{
791 const Tensor<1, 2> tangent = face->vertex(1) - face->vertex(0);
792 // We're in 2d. Faces are edges:
793 for (const unsigned int vertex : ReferenceCells::Line.vertex_indices())
794 // compute normals from tangent
795 face_vertex_normals[vertex] = Tensor<1, 2>({tangent[1], -tangent[0]});
796}
797
798
799
800template <>
801void
803 const Triangulation<2, 3>::face_iterator & /*face*/,
804 Manifold<2, 3>::FaceVertexNormals & /*face_vertex_normals*/) const
805{
807}
808
809
810
811template <>
812void
815 Manifold<3, 3>::FaceVertexNormals &face_vertex_normals) const
816{
817 if (face->reference_cell() == ReferenceCells::Quadrilateral)
818 {
819 static const unsigned int neighboring_vertices[4][2] = {{1, 2},
820 {3, 0},
821 {0, 3},
822 {2, 1}};
823 for (unsigned int vertex = 0; vertex < face->n_vertices(); ++vertex)
824 {
825 // first define the two tangent vectors at the vertex by using the
826 // two lines radiating away from this vertex
827 const Tensor<1, 3> tangents[2] = {
828 face->vertex(neighboring_vertices[vertex][0]) -
829 face->vertex(vertex),
830 face->vertex(neighboring_vertices[vertex][1]) -
831 face->vertex(vertex)};
832
833 // then compute the normal by taking the cross product. since the
834 // normal is not required to be normalized, no problem here
835 face_vertex_normals[vertex] =
836 cross_product_3d(tangents[0], tangents[1]);
837 }
838 }
839 else
841}
842
843
844
845template <>
848 const Point<1> &) const
849{
851 return {};
852}
853
854
855
856template <>
859 const Point<2> &) const
860{
862 return {};
863}
864
865
866
867template <>
870 const Point<3> &) const
871{
873 return {};
874}
875
876
877
878template <>
882 const Point<2> &p) const
883{
884 // In 2d, a face is just a straight line and
885 // we can use the 'standard' implementation.
886 return Manifold<2, 2>::normal_vector(face, p);
887}
888
889
890
891template <int dim, int spacedim>
895 const Point<spacedim> &p) const
896{
897 // I don't think the implementation below will work when dim!=spacedim;
898 // in fact, I believe that we don't even have enough information here,
899 // because we would need to know not only about the tangent vectors
900 // of the face, but also of the cell, to compute the normal vector.
901 // Someone will have to think about this some more.
902 Assert(dim == spacedim, ExcNotImplemented());
903
904 // in order to find out what the normal vector is, we first need to
905 // find the reference coordinates of the point p on the given face,
906 // or at least the reference coordinates of the closest point on the
907 // face
908 //
909 // in other words, we need to find a point xi so that f(xi)=||F(xi)-p||^2->min
910 // where F(xi) is the mapping. this algorithm is implemented in
911 // MappingQ1<dim,spacedim>::transform_real_to_unit_cell but only for cells,
912 // while we need it for faces here. it's also implemented in somewhat
913 // more generality there using the machinery of the MappingQ1 class
914 // while we really only need it for a specific case here
915 //
916 // in any case, the iteration we use here is a Gauss-Newton's iteration with
917 // xi^{n+1} = xi^n - H(xi^n)^{-1} J(xi^n)
918 // where
919 // J(xi) = (grad F(xi))^T (F(xi)-p)
920 // and
921 // H(xi) = [grad F(xi)]^T [grad F(xi)]
922 // In all this,
923 // F(xi) = sum_v vertex[v] phi_v(xi)
924 // We get the shape functions phi_v from an object of type FE_Q<dim-1>(1)
925
926 // We start at the center of the cell. If the face is a line or
927 // square, then the center is at 0.5 or (0.5,0.5). If the face is
928 // a triangle, then we start at the point (1/3,1/3).
929 const unsigned int facedim = dim - 1;
930
932
933 const auto face_reference_cell = face->reference_cell();
934
935 if ((dim <= 2) ||
936 (face_reference_cell == ReferenceCells::get_hypercube<facedim>()))
937 {
938 for (unsigned int i = 0; i < facedim; ++i)
939 xi[i] = 1. / 2;
941 else
942 {
943 for (unsigned int i = 0; i < facedim; ++i)
944 xi[i] = 1. / 3;
945 }
946
947 const double eps = 1e-12;
948 Tensor<1, spacedim> grad_F[facedim];
949 unsigned int iteration = 0;
950 while (true)
951 {
953 for (const unsigned int v : face->vertex_indices())
954 F +=
955 face->vertex(v) * face_reference_cell.d_linear_shape_function(xi, v);
956
957 for (unsigned int i = 0; i < facedim; ++i)
958 {
959 grad_F[i] = 0;
960 for (const unsigned int v : face->vertex_indices())
961 grad_F[i] +=
962 face->vertex(v) *
963 face_reference_cell.d_linear_shape_function_gradient(xi, v)[i];
964 }
965
967 for (unsigned int i = 0; i < facedim; ++i)
968 for (unsigned int j = 0; j < spacedim; ++j)
969 J[i] += grad_F[i][j] * (F - p)[j];
970
972 for (unsigned int i = 0; i < facedim; ++i)
973 for (unsigned int j = 0; j < facedim; ++j)
974 for (unsigned int k = 0; k < spacedim; ++k)
975 H[i][j] += grad_F[i][k] * grad_F[j][k];
976
977 const Tensor<1, facedim> delta_xi = -invert(H) * J;
978 xi += delta_xi;
979 ++iteration;
980
981 AssertThrow(iteration < 10,
983 "The Newton iteration to find the reference point "
984 "did not converge in 10 iterations. Do you have a "
985 "deformed cell? (See the glossary for a definition "
986 "of what a deformed cell is. You may want to output "
987 "the vertices of your cell."));
988
989 // It turns out that the check in reference coordinates with an absolute
990 // tolerance can cause a convergence failure of the Newton method as
991 // seen in tests/manifold/flat_manifold_09.cc. To work around this, also
992 // use a convergence check in world coordinates. This check has to be
993 // relative to the size of the face of course. Here we decided to use
994 // diameter because it works for non-planar faces and is cheap to
995 // compute:
996 const double normalized_delta_world = (F - p).norm() / face->diameter();
997
998 if (delta_xi.norm() < eps || normalized_delta_world < eps)
999 break;
1000 }
1001
1002 // so now we have the reference coordinates xi of the point p.
1003 // we then have to compute the normal vector, which we can do
1004 // by taking the (normalize) alternating product of all the tangent
1005 // vectors given by grad_F
1006 return internal::normalized_alternating_product(grad_F);
1007}
1008#endif
1009
1010/* -------------------------- ChartManifold --------------------- */
1011template <int dim, int spacedim, int chartdim>
1013 const Tensor<1, chartdim> &periodicity)
1014 : sub_manifold(periodicity)
1015{}
1016
1017
1018
1019template <int dim, int spacedim, int chartdim>
1022 const Point<spacedim> &p1,
1023 const Point<spacedim> &p2,
1024 const double w) const
1025{
1026 const std::array<Point<spacedim>, 2> points{{p1, p2}};
1027 const std::array<double, 2> weights{{1. - w, w}};
1028 return get_new_point(make_array_view(points), make_array_view(weights));
1029}
1030
1031
1032
1033template <int dim, int spacedim, int chartdim>
1036 const ArrayView<const Point<spacedim>> &surrounding_points,
1037 const ArrayView<const double> &weights) const
1038{
1039 const std::size_t n_points = surrounding_points.size();
1040
1041 boost::container::small_vector<Point<chartdim>, 200> chart_points(n_points);
1042
1043 for (unsigned int i = 0; i < n_points; ++i)
1044 chart_points[i] = pull_back(surrounding_points[i]);
1045
1046 const Point<chartdim> p_chart =
1047 sub_manifold.get_new_point(chart_points, weights);
1048
1049 return push_forward(p_chart);
1050}
1051
1052
1053
1054template <int dim, int spacedim, int chartdim>
1055void
1057 const ArrayView<const Point<spacedim>> &surrounding_points,
1058 const Table<2, double> &weights,
1059 ArrayView<Point<spacedim>> new_points) const
1060{
1061 Assert(weights.size(0) > 0, ExcEmptyObject());
1062 AssertDimension(surrounding_points.size(), weights.size(1));
1063
1064 const std::size_t n_points = surrounding_points.size();
1065
1066 boost::container::small_vector<Point<chartdim>, 200> chart_points(n_points);
1067 for (std::size_t i = 0; i < n_points; ++i)
1068 chart_points[i] = pull_back(surrounding_points[i]);
1069
1070 boost::container::small_vector<Point<chartdim>, 200> new_points_on_chart(
1071 weights.size(0));
1072 sub_manifold.get_new_points(chart_points, weights, new_points_on_chart);
1073
1074 for (std::size_t row = 0; row < weights.size(0); ++row)
1075 new_points[row] = push_forward(new_points_on_chart[row]);
1076}
1078
1079
1080template <int dim, int spacedim, int chartdim>
1083 const Point<chartdim> &) const
1084{
1085 // function must be implemented in a derived class to be usable,
1086 // as discussed in this function's documentation
1087 Assert(false, ExcPureFunctionCalled());
1089}
1090
1091
1092
1093template <int dim, int spacedim, int chartdim>
1096 const Point<spacedim> &x1,
1097 const Point<spacedim> &x2) const
1098{
1100 push_forward_gradient(pull_back(x1));
1101
1102 // ensure that the chart is not singular by asserting that its
1103 // derivative has a positive determinant. we need to make this
1104 // comparison relative to the size of the derivative. since the
1105 // determinant is the product of chartdim factors, take the
1106 // chartdim-th root of it in comparing against the size of the
1107 // derivative
1108 Assert(std::pow(std::abs(F_prime.determinant()), 1. / chartdim) >=
1109 1e-12 * F_prime.norm(),
1110 ExcMessage(
1111 "The derivative of a chart function must not be singular."));
1112
1113 const Tensor<1, chartdim> delta =
1114 sub_manifold.get_tangent_vector(pull_back(x1), pull_back(x2));
1115
1116 Tensor<1, spacedim> result;
1117 for (unsigned int i = 0; i < spacedim; ++i)
1118 result[i] += F_prime[i] * delta;
1119
1120 return result;
1121}
1122
1123
1124
1125template <int dim, int spacedim, int chartdim>
1126const Tensor<1, chartdim> &
1128{
1129 return sub_manifold.get_periodicity();
1130}
1131
1132// explicit instantiations
1133#include "grid/manifold.inst"
1134
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
iterator begin() const
Definition array_view.h:755
iterator end() const
Definition array_view.h:764
std::size_t size() const
Definition array_view.h:737
ChartManifold(const Tensor< 1, chartdim > &periodicity=Tensor< 1, chartdim >())
Definition manifold.cc:1012
virtual void get_new_points(const ArrayView< const Point< spacedim > > &surrounding_points, const Table< 2, double > &weights, ArrayView< Point< spacedim > > new_points) const override
Definition manifold.cc:1056
virtual Tensor< 1, spacedim > get_tangent_vector(const Point< spacedim > &x1, const Point< spacedim > &x2) const override
Definition manifold.cc:1095
virtual DerivativeForm< 1, chartdim, spacedim > push_forward_gradient(const Point< chartdim > &chart_point) const
Definition manifold.cc:1082
const Tensor< 1, chartdim > & get_periodicity() const
Definition manifold.cc:1127
virtual Point< spacedim > get_intermediate_point(const Point< spacedim > &p1, const Point< spacedim > &p2, const double w) const override
Definition manifold.cc:1021
virtual Point< spacedim > get_new_point(const ArrayView< const Point< spacedim > > &surrounding_points, const ArrayView< const double > &weights) const override
Definition manifold.cc:1035
Number determinant() const
numbers::NumberTraits< Number >::real_type norm() const
virtual Tensor< 1, spacedim > normal_vector(const typename Triangulation< dim, spacedim >::face_iterator &face, const Point< spacedim > &p) const override
virtual void get_normals_at_vertices(const typename Triangulation< dim, spacedim >::face_iterator &face, typename Manifold< dim, spacedim >::FaceVertexNormals &face_vertex_normals) const override
virtual Tensor< 1, spacedim > get_tangent_vector(const Point< spacedim > &x1, const Point< spacedim > &x2) const override
virtual Point< spacedim > project_to_manifold(const ArrayView< const Point< spacedim > > &points, const Point< spacedim > &candidate) const override
const Tensor< 1, spacedim > & get_periodicity() const
virtual std::unique_ptr< Manifold< dim, spacedim > > clone() const override
virtual Point< spacedim > get_new_point(const ArrayView< const Point< spacedim > > &surrounding_points, const ArrayView< const double > &weights) const override
FlatManifold(const Tensor< 1, spacedim > &periodicity=Tensor< 1, spacedim >(), const double tolerance=1e-10)
virtual void get_new_points(const ArrayView< const Point< spacedim > > &surrounding_points, const Table< 2, double > &weights, ArrayView< Point< spacedim > > new_points) const override
virtual Point< spacedim > get_new_point_on_hex(const typename Triangulation< dim, spacedim >::hex_iterator &hex) const
virtual Point< spacedim > get_new_point_on_line(const typename Triangulation< dim, spacedim >::line_iterator &line) const
virtual Point< spacedim > project_to_manifold(const ArrayView< const Point< spacedim > > &surrounding_points, const Point< spacedim > &candidate) const
virtual void get_new_points(const ArrayView< const Point< spacedim > > &surrounding_points, const Table< 2, double > &weights, ArrayView< Point< spacedim > > new_points) const
virtual void get_normals_at_vertices(const typename Triangulation< dim, spacedim >::face_iterator &face, FaceVertexNormals &face_vertex_normals) const
virtual Tensor< 1, spacedim > get_tangent_vector(const Point< spacedim > &x1, const Point< spacedim > &x2) const
std::array< Tensor< 1, spacedim >, GeometryInfo< dim >::vertices_per_face > FaceVertexNormals
Definition manifold.h:304
virtual Point< spacedim > get_intermediate_point(const Point< spacedim > &p1, const Point< spacedim > &p2, const double w) const
Point< spacedim > get_new_point_on_face(const typename Triangulation< dim, spacedim >::face_iterator &face) const
virtual Point< spacedim > get_new_point_on_quad(const typename Triangulation< dim, spacedim >::quad_iterator &quad) const
virtual Tensor< 1, spacedim > normal_vector(const typename Triangulation< dim, spacedim >::face_iterator &face, const Point< spacedim > &p) const
Point< spacedim > get_new_point_on_cell(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const
virtual Point< spacedim > get_new_point(const ArrayView< const Point< spacedim > > &surrounding_points, const ArrayView< const double > &weights) const
Definition point.h:111
numbers::NumberTraits< Number >::real_type norm() const
constexpr numbers::NumberTraits< Number >::real_type norm_square() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
Definition config.h:636
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
Definition config.h:680
#define DEAL_II_NOT_IMPLEMENTED()
unsigned int vertex_indices[2]
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcEmptyObject()
#define Assert(cond, exc)
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcPureFunctionCalled()
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
typename IteratorSelector::hex_iterator hex_iterator
Definition tria.h:1756
typename IteratorSelector::quad_iterator quad_iterator
Definition tria.h:1732
typename IteratorSelector::line_iterator line_iterator
Definition tria.h:1708
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Definition divergence.h:469
std::pair< std::array< Point< MeshIteratorType::AccessorType::space_dimension >, n_default_points_per_cell< MeshIteratorType >()>, std::array< double, n_default_points_per_cell< MeshIteratorType >()> > get_default_points_and_weights(const MeshIteratorType &iterator, const bool with_interpolation=false)
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition utilities.cc:210
Tensor< 2, dim, Number > w(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
SymmetricTensor< 2, dim, Number > epsilon(const Tensor< 2, dim, Number > &Grad_u)
constexpr ReferenceCell< 2 > Quadrilateral
constexpr ReferenceCell< 1 > Line
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
constexpr SymmetricTensor< 2, dim, Number > invert(const SymmetricTensor< 2, dim, Number > &)
void vectorized_load_and_transpose(const unsigned int n_entries, const Number *in, const unsigned int *offsets, VectorizedArray< Number, width > *out)