Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
shape.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2022-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/shape.h>
5
6#include <deal.II/grid/manifold.h>
7#include <deal.II/grid/manifold_lib.h>
8
9#include <algorithm>
10
11#ifdef DEAL_II_WITH_OPENCASCADE
12# include <deal.II/opencascade/manifold_lib.h>
13# include <deal.II/opencascade/utilities.h>
14
15# include <BRepBuilderAPI_MakeVertex.hxx>
16# include <BRepClass3d_SolidClassifier.hxx>
17# include <BRepExtrema_DistShapeShape.hxx>
18#endif
19
20
21
22template <int dim>
23double
25{
26 return 0.;
27}
28
29template <int dim>
30void
32{
33 // Prevent expensive cache clearing if the cache is empty.
34 if (value_cache.size() > 0)
35 {
36 value_cache.clear();
37 gradient_cache.clear();
38 closest_point_cache.clear();
39 }
40}
41
42template <int dim>
43Point<dim>
44Shape<dim>::align_and_center(const Point<dim> &evaluation_point) const
45{
46 // Translation and rotation to standard position and orientation for
47 // distance calculations
48 Point<dim> center_of_rotation = position;
49 Point<dim> rotated_point;
50 Point<dim> translated_point;
51
52 // Rotation from the solid orientation
53 // Angular position around x, y and z axis
54 Tensor<1, 3> theta = orientation;
55
56 // The centralized point is the one to be rotated, and it is updated after
57 // each rotation around one axis The centralized rotated point is the result
58 // of each rotation, and it is initialized in case no rotation is performed
59 Point<dim> centralized_point;
60 centralized_point = evaluation_point - center_of_rotation;
61 Point<dim> centralized_rotated = centralized_point;
62
63 // Selection of the first axis around which to rotate:
64 // x -> 0, y -> 1, z -> 2
65 // In 2D, only rotation around the z axis is possible
66 if constexpr (dim == 2)
67 {
68 if (std::abs(theta[2]) > 1e-10 * this->effective_radius)
69 {
70 Tensor<2, 2> rot_matrix =
71 Physics::Transformations::Rotations::rotation_matrix_2d(-theta[2]);
72
73 // Multiplication
74 centralized_rotated.clear();
75 for (int j = 0; j < dim; ++j)
76 {
77 for (int k = 0; k < dim; ++k)
78 {
79 centralized_rotated[j] +=
80 rot_matrix[j][k] * centralized_point[k];
81 }
82 }
83 }
84 }
85 else // (dim == 3)
86 {
87 for (unsigned int i = 0; i < 3; ++i)
88 {
89 if (std::abs(theta[i]) > 1e-10 * this->effective_radius)
90 {
91 Tensor<1, 3> axis;
92 axis[i] = 1.0;
93 Tensor<2, 3> rot_matrix =
94 Physics::Transformations::Rotations::rotation_matrix_3d(
95 axis, -theta[i]);
96
97 // Multiplication
98 centralized_rotated.clear();
99 for (int j = 0; j < dim; ++j)
100 {
101 for (int k = 0; k < dim; ++k)
102 {
103 centralized_rotated[j] +=
104 rot_matrix[j][k] * centralized_point[k];
105 }
106 }
107 centralized_point = centralized_rotated;
108 }
109 }
110 }
111
112 rotated_point = centralized_rotated + center_of_rotation;
113
114 // Translation from the solid position
115 translated_point = rotated_point - position;
116
117 return translated_point;
118}
119
120template <int dim>
121Point<dim>
122Shape<dim>::reverse_align_and_center(const Point<dim> &evaluation_point) const
123{
124 // Reverse translation and rotation from standard position and orientation to
125 // global referential
126 Point<dim> center_of_rotation = position;
127 Point<dim> rotated_point;
128 Point<dim> translated_point;
129
130 // Rotation from the solid orientation
131 // Angular position around x, y and z axis
132 Tensor<1, 3> theta = orientation;
133
134 // The centralized point is the one to be rotated, and it is updated after
135 // each rotation around one axis. The centralized rotated point is the result
136 // of each rotation, and it is initialized in case no rotation is performed
137 Point<dim> centralized_point;
138 centralized_point = evaluation_point;
139 Point<dim> centralized_rotated = centralized_point;
140
141 // Selection of the first axis around which to rotate:
142 // x -> 0, y -> 1, z -> 2
143 // In 2D, only rotation around the z axis is possible
144
145 if constexpr (dim == 2)
146 {
147 if (std::abs(theta[2]) > 1e-10 * this->effective_radius)
148 {
149 Tensor<2, 2> rot_matrix =
150 Physics::Transformations::Rotations::rotation_matrix_2d(theta[2]);
151
152 // Multiplication
153 centralized_rotated.clear();
154 for (int j = 0; j < dim; ++j)
155 {
156 for (int k = 0; k < dim; ++k)
157 {
158 centralized_rotated[j] +=
159 rot_matrix[j][k] * centralized_point[k];
160 }
161 }
162 }
163 }
164 else // (dim == 3)
165 {
166 for (int i = 0; i < dim; ++i)
167 {
168 if (std::abs(theta[2 - i]) > 1e-10 * this->effective_radius)
169 {
170 Tensor<1, 3> axis;
171 axis[2 - i] = 1.0;
172 Tensor<2, 3> rot_matrix =
173 Physics::Transformations::Rotations::rotation_matrix_3d(
174 axis, theta[2 - i]);
175
176 // Multiplication
177 centralized_rotated.clear();
178 for (int j = 0; j < dim; ++j)
179 {
180 for (int k = 0; k < dim; ++k)
181 {
182 centralized_rotated[j] +=
183 rot_matrix[j][k] * centralized_point[k];
184 }
185 }
186 centralized_point = centralized_rotated;
187 }
188 }
189 }
190 rotated_point = centralized_rotated + center_of_rotation;
191
192 // Translation from the solid position
193 translated_point = rotated_point;
194
195 return translated_point;
196}
197
198template <int dim>
199std::shared_ptr<Manifold<dim - 1, dim>>
201{
202 return std::make_shared<FlatManifold<dim - 1, dim>>();
203}
204
205template <int dim>
206std::string
207Shape<dim>::point_to_string(const Point<dim> &evaluation_point) const
208{
209 // This function transforms a point into a string.
210 // The point precision is conserve up to a precision of 1e-12.
211 std::string point_in_string = "";
212 for (int d = 0; d < dim; ++d)
213 {
214 point_in_string =
215 point_in_string + ";" +
216 std::to_string(std::round(evaluation_point[d] * 1e12) / 1e12);
217 }
218 return point_in_string;
219}
220
221
222template <int dim>
223void
225 const Point<dim> &p,
226 Point<dim> &closest_point,
227 const typename DoFHandler<dim>::active_cell_iterator &cell_guess)
228{
229 Tensor<1, dim> actual_gradient;
230 double distance_from_surface;
231 actual_gradient = this->gradient_with_cell_guess(p, cell_guess);
232 distance_from_surface = this->value_with_cell_guess(p, cell_guess);
233 // Check if the gradient is well-defined. If the point is on the surface,
234 // the gradient can be badly defined for some shapes. We return the point
235 // directly in these cases since it would be on the surface.
236 closest_point = p - (actual_gradient / (actual_gradient.norm() +
237 1e-16 * this->effective_radius)) *
238 distance_from_surface;
239}
240
241
242template <int dim>
243void
245 Point<dim> &closest_point) const
246{
247 Tensor<1, dim> actual_gradient;
248 double distance_from_surface;
249 actual_gradient = this->gradient(p);
250 distance_from_surface = this->value(p);
251
252 // Check if the gradient is well-defined. If the point is on the surface,
253 // the gradient can be badly defined for some shapes. We return the point
254 // directly in these cases since it would be on the surface.
255 closest_point = p - (actual_gradient / (actual_gradient.norm() +
256 1e-16 * this->effective_radius)) *
257 distance_from_surface;
258}
259
260template <int dim>
261double
263 const Point<dim> &evaluation_point,
264 const typename DoFHandler<dim>::active_cell_iterator & /*cell*/,
265 const unsigned int /*component*/)
266{
267 return this->value(evaluation_point);
268}
269
270template <int dim>
271Tensor<1, dim>
273 const Point<dim> &evaluation_point,
274 const typename DoFHandler<dim>::active_cell_iterator & /*cell*/,
275 const unsigned int /*component*/)
276{
277 return this->gradient(evaluation_point);
278}
279
280template <int dim>
281double
282Sphere<dim>::value(const Point<dim> &evaluation_point,
283 const unsigned int /*component*/) const
284{
285 return sphere_function->value(evaluation_point) - this->layer_thickening;
286}
287
288
289template <int dim>
290std::shared_ptr<Shape<dim>>
292{
293 std::shared_ptr<Shape<dim>> copy =
294 std::make_shared<Sphere<dim>>(this->effective_radius,
295 this->position,
296 this->orientation);
297 return copy;
298}
299
300template <int dim>
301Tensor<1, dim>
302Sphere<dim>::gradient(const Point<dim> &evaluation_point,
303 const unsigned int /*component*/) const
304{
305 // We make sure that the evaluation point and the sphere center are different,
306 // because if they are the same the analytical gradient is not defined: the
307 // function returns a NaN. We use the numerical gradient if the points are the
308 // same.
309 if ((evaluation_point - this->position).norm() <
310 1e-12 * this->effective_radius)
311 return AutoDerivativeFunction<dim>::gradient(evaluation_point);
312 return sphere_function->gradient(evaluation_point);
313}
314
315template <int dim>
316std::shared_ptr<Manifold<dim - 1, dim>>
318{
319 return std::make_shared<SphericalManifold<dim - 1, dim>>(this->position);
320}
321
322template <int dim>
323double
325{
326 double solid_volume;
327 using numbers::PI;
328 if (dim == 2)
329 solid_volume = this->effective_radius * this->effective_radius * PI;
330
331 else // dim = 3
332 solid_volume = 4.0 / 3.0 * this->effective_radius * this->effective_radius *
333 this->effective_radius * PI;
334 return solid_volume;
335}
336
337template <int dim>
338void
340{
341 this->Shape<dim>::set_position(position);
342 sphere_function = std::make_shared<Functions::SignedDistance::Sphere<dim>>(
344}
345
346template <int dim>
347std::tuple<double, Tensor<1, dim>, Point<dim>>
349 Shape<dim> &shape,
350 const typename DoFHandler<dim>::active_cell_iterator &cell,
351 std::vector<Point<dim>> &candidate_points,
352 double precision,
353 bool exact_distance_outside_of_contact)
354{
355 (void)candidate_points;
356 (void)precision;
357 (void)exact_distance_outside_of_contact;
358 double distance = DBL_MAX;
359 Tensor<1, dim> normal;
360 Point<dim> contact_point;
361 if (typeid(shape) == typeid(Sphere<dim>))
362 {
363 Tensor<1, dim> center_to_center_vector =
364 shape.get_position() - this->position;
365 distance = (center_to_center_vector.norm() - this->effective_radius -
366 shape.effective_radius) /
367 2;
368 normal = center_to_center_vector / center_to_center_vector.norm();
369 contact_point =
370 this->position + normal * (this->effective_radius + distance);
371 }
372 else
373 {
374 distance = (shape.value_with_cell_guess(this->position, cell) -
375 this->effective_radius) /
376 2;
377 normal = -shape.gradient_with_cell_guess(this->position, cell);
378 contact_point =
379 this->position + normal * (this->effective_radius + distance);
380 }
381
382 return std::make_tuple(distance, normal, contact_point);
383}
384
385template <int dim>
386std::tuple<double, Tensor<1, dim>, Point<dim>>
388 std::vector<Point<dim>> &candidate_points,
389 double precision,
390 bool exact_distance_outside_of_contact)
391{
392 (void)candidate_points;
393 (void)precision;
394 (void)exact_distance_outside_of_contact;
395 double distance = DBL_MAX;
396 Tensor<1, dim> normal;
397 Point<dim> contact_point;
398 if (typeid(shape) == typeid(Sphere<dim>))
399 {
400 Tensor<1, dim> center_to_center_vector =
401 shape.get_position() - this->position;
402 distance = (center_to_center_vector.norm() - this->effective_radius -
403 shape.effective_radius) /
404 2;
405 normal = center_to_center_vector / center_to_center_vector.norm();
406 contact_point =
407 this->position + normal * (this->effective_radius + distance);
408 }
409 else
410 {
411 distance = (shape.value(this->position) - this->effective_radius) / 2;
412 normal = -shape.gradient(this->position);
413 contact_point =
414 this->position + normal * (this->effective_radius + distance);
415 }
416
417 return std::make_tuple(distance, normal, contact_point);
418}
419
420template <int dim>
421double
423{
424 return (this->position - p).norm();
425}
426
427template <int dim>
428double
430 const Point<dim> &p,
431 const typename DoFHandler<dim>::active_cell_iterator &cell)
432{
433 (void)cell;
434 return (this->position - p).norm();
435}
436
437
438template <int dim>
439double
440Plane<dim>::value(const Point<dim> &evaluation_point,
441 const unsigned int /*component*/) const
442{
443 Point<dim> current_point = this->align_and_center(evaluation_point);
444 double dot_product = scalar_product((current_point), normal);
445 Point<dim> projected_point =
446 current_point - dot_product / normal.norm_square() * normal;
447 auto rotate_in_globalpoint = this->reverse_align_and_center(projected_point);
448 if (dot_product > 0)
449 return (rotate_in_globalpoint - evaluation_point).norm();
450 else
451 return -(rotate_in_globalpoint - evaluation_point).norm();
452}
453
454template <int dim>
455std::shared_ptr<Shape<dim>>
457{
458 std::shared_ptr<Shape<dim>> copy =
459 std::make_shared<Plane<dim>>(this->position, this->orientation);
460 return copy;
461}
462
463template <int dim>
464Tensor<1, dim>
465Plane<dim>::gradient(const Point<dim> &evaluation_point,
466 const unsigned int /*component*/) const
467{
468 (void)evaluation_point;
469 // We take the vector in z of the plane and rotate it in the world frame.
470 if constexpr (dim == 2)
471 {
473 }
474 else
475 {
476 return this->rotation_matrix * normal;
477 }
478}
479
480template <int dim>
481double
483{
484 return 0;
485}
486
487
488template <int dim>
489void
491 const Point<dim> &p,
492 Point<dim> &closest_point,
493 const typename DoFHandler<dim>::active_cell_iterator & /*cell_guess*/)
494{
495 auto point_in_string = this->point_to_string(p);
496 auto iterator = this->closest_point_cache.find(point_in_string);
497 if (iterator == this->closest_point_cache.end())
498 {
499 this->closest_surface_point(p, closest_point);
500
501 Point<dim> copy_closest_point{};
502 for (int d = 0; d < dim; d++)
503 copy_closest_point[d] = closest_point[d];
504 if (!this->part_of_a_composite)
505 {
506 this->closest_point_cache[point_in_string] = copy_closest_point;
507 this->value_cache[point_in_string] = this->value(p);
508 this->gradient_cache[point_in_string] = this->gradient(p);
509 }
510 }
511 else
512 closest_point = iterator->second;
513}
514
515template <int dim>
516void
518 Point<dim> &closest_point) const
519{
520 auto point_in_string = this->point_to_string(p);
521 auto iterator = this->closest_point_cache.find(point_in_string);
522 if (iterator == this->closest_point_cache.end())
523 {
524 // The initial guess is chosen as the centered point (i.e. evaluation
525 // point, in the superquadric referential). It should already be somewhat
526 // close to the closest point, and it is already in the right octant.
527 Point<dim> current_point = this->align_and_center(p);
528
529 unsigned int iteration = 0;
530 constexpr unsigned int iteration_max = 1e2;
531
532 Point<dim> dx{}, distance_gradient{};
533 double current_distance = superquadric(current_point);
534
535 while (iteration < iteration_max && abs(current_distance) > epsilon)
536 {
537 constexpr double relaxation = 1;
538 distance_gradient = superquadric_gradient(current_point);
539 // limit the step size
540
541 if (distance_gradient.norm() < epsilon)
542 // Gradient can be null if the evaluation point is exactly on the
543 // centroid of the shape. In this case it is also the closest point.
544 break;
545 // This is a modified Newton method that limits the step size when the
546 // gradient norm is smaller than 1.
547 dx = -relaxation * (current_distance * distance_gradient) /
548 distance_gradient.norm() /
549 std::max(1.0, distance_gradient.norm());
550
551 current_point = current_point + dx;
552 current_distance = superquadric(current_point);
553
554 iteration++;
555 }
556 closest_point = this->reverse_align_and_center(current_point);
557 }
558 else
559 closest_point = iterator->second;
560}
561
562template <int dim>
563double
564Superquadric<dim>::value(const Point<dim> &evaluation_point,
565 const unsigned int /*component*/) const
566{
567 using numbers::PI;
568
569 auto point_in_string = this->point_to_string(evaluation_point);
570 auto iterator = this->value_cache.find(point_in_string);
571 if (iterator == this->value_cache.end())
572 {
573 // The closest point has to be found first, because it is used in value
574 // calculation.
575 Point<dim> closest_point{};
576 this->closest_surface_point(evaluation_point, closest_point);
577
578 Point<dim> centered_point = this->align_and_center(evaluation_point);
579 if (superquadric(centered_point) > 0)
580 return (closest_point - evaluation_point).norm() -
581 this->layer_thickening;
582 else
583 return -(closest_point - evaluation_point).norm() -
584 this->layer_thickening;
585 }
586 else
587 return iterator->second;
588}
589
590template <int dim>
591double
593 const Point<dim> &evaluation_point,
594 const typename DoFHandler<dim>::active_cell_iterator & /*cell*/,
595 const unsigned int /*component = 0*/)
596{
597 auto point_in_string = this->point_to_string(evaluation_point);
598 auto iterator = this->value_cache.find(point_in_string);
599 if (iterator == this->value_cache.end())
600 {
601 // The closest point has to be found first, because it is used in value
602 // calculation.
603 Point<dim> closest_point{};
604 this->closest_surface_point(evaluation_point, closest_point);
605 Point<dim> copy_closest_point{};
606 for (int d = 0; d < dim; d++)
607 copy_closest_point[d] = closest_point[d];
608
609 double levelset = this->value(evaluation_point);
610 if (!this->part_of_a_composite)
611 {
612 this->closest_point_cache[point_in_string] = copy_closest_point;
613 this->value_cache[point_in_string] = levelset;
614 this->gradient_cache[point_in_string] =
615 this->gradient(evaluation_point);
616 }
617 return levelset;
618 }
619 else
620 return iterator->second;
621}
622
623template <int dim>
624std::shared_ptr<Shape<dim>>
626{
627 std::shared_ptr<Shape<dim>> copy =
628 std::make_shared<Superquadric<dim>>(this->half_lengths,
629 this->exponents,
630 this->epsilon,
631 this->position,
632 this->orientation);
633 return copy;
634}
635
636template <int dim>
637Tensor<1, dim>
638Superquadric<dim>::gradient(const Point<dim> &evaluation_point,
639 const unsigned int /*component*/) const
640{
641 auto point_in_string = this->point_to_string(evaluation_point);
642 auto iterator = this->gradient_cache.find(point_in_string);
643 if (iterator == this->gradient_cache.end())
644 {
645 Point<dim> closest_point{};
646 this->closest_surface_point(evaluation_point, closest_point);
647
648 Tensor<1, dim> gradient;
649 Point<dim> centered_point = this->align_and_center(evaluation_point);
650 if (superquadric(centered_point) > 0)
651 gradient = (evaluation_point - closest_point) /
652 ((evaluation_point - closest_point).norm() +
653 1e-16 * this->effective_radius);
654 else
655 gradient = -(evaluation_point - closest_point) /
656 ((evaluation_point - closest_point).norm() +
657 1e-16 * this->effective_radius);
658 return gradient;
659 }
660 else
661 return iterator->second;
662}
663
664template <int dim>
665Tensor<1, dim>
667 const Point<dim> &evaluation_point,
668 const typename DoFHandler<dim>::active_cell_iterator & /*cell*/,
669 const unsigned int /*component*/)
670{
671 auto point_in_string = this->point_to_string(evaluation_point);
672 auto iterator = this->gradient_cache.find(point_in_string);
673 if (iterator == this->gradient_cache.end())
674 {
675 // The closest point has to be found first, because it is used in value
676 // calculation
677 Point<dim> closest_point{};
678 this->closest_surface_point(evaluation_point, closest_point);
679 Point<dim> copy_closest_point{};
680 for (int d = 0; d < dim; d++)
681 copy_closest_point[d] = closest_point[d];
682
683 Tensor<1, dim> gradient = this->gradient(evaluation_point);
684 if (!this->part_of_a_composite)
685 {
686 this->closest_point_cache[point_in_string] = copy_closest_point;
687 this->value_cache[point_in_string] = this->value(evaluation_point);
688 this->gradient_cache[point_in_string] = gradient;
689 }
690 return gradient;
691 }
692 else
693 return iterator->second;
694}
695
696template <int dim>
697double
698OpenCascadeShape<dim>::value(const Point<dim> &evaluation_point,
699 const unsigned int /*component*/) const
700{
701#ifdef DEAL_II_WITH_OPENCASCADE
702 auto point_in_string = this->point_to_string(evaluation_point);
703 auto iterator = this->value_cache.find(point_in_string);
704 if (iterator != this->value_cache.end())
705 return iterator->second;
706
707 Point<dim> centered_point = this->align_and_center(evaluation_point);
708 Point<dim> projected_point;
709 auto pt = OpenCASCADE::point(centered_point);
710 TopoDS_Vertex vertex = BRepBuilderAPI_MakeVertex(pt);
711 BRepExtrema_DistShapeShape distancetool(shape, vertex);
712 distancetool.Perform();
713 gp_Pnt pt_on_surface = distancetool.PointOnShape1(1);
714 projected_point[0] = pt_on_surface.X();
715 projected_point[1] = pt_on_surface.Y();
716 if constexpr (dim == 3)
717 {
718 projected_point[2] = pt_on_surface.Z();
719 }
720
721 BRepClass3d_SolidClassifier point_classifier(shape);
722 point_classifier.Perform(pt, shape_tol);
723 TopAbs_State point_state = point_classifier.State();
724 // Check if the evaluation point is inside the shape (This check can return
725 // true only if the shape is a solid). By default, step file format will
726 // define a solid. This is necessary to sign the distance function evaluation.
727 if (point_state == TopAbs_State::TopAbs_IN)
728 {
729 // If the evaluation point is inside the shape and the shape is a solid,
730 // the distance to the shape will be 0. This is why we need to evaluate
731 // the distance of the point with the shell of the shape.
732 // Rotate the solution found to the global reference frame.
733 auto rotate_in_globalpoint =
734 this->reverse_align_and_center(projected_point);
735 return -(rotate_in_globalpoint - evaluation_point).norm() -
736 this->layer_thickening;
737 }
738 else
739 {
740 auto rotate_in_globalpoint =
741 this->reverse_align_and_center(projected_point);
742 return (rotate_in_globalpoint - evaluation_point).norm() -
743 this->layer_thickening;
744 }
745
746#else
747 (void)evaluation_point;
748 return 0;
749#endif
750}
751template <int dim>
752double
754 const Point<dim> &evaluation_point,
755 const typename DoFHandler<dim>::active_cell_iterator & /*cell*/,
756 const unsigned int /*component*/)
757{
758#ifdef DEAL_II_WITH_OPENCASCADE
759 auto point_in_string = this->point_to_string(evaluation_point);
760 auto iterator = this->value_cache.find(point_in_string);
761
762 if (iterator == this->value_cache.end())
763 {
764 // Transform the point to an OpenCascade point
765 Point<dim> centered_point = this->align_and_center(evaluation_point);
766 Point<dim> projected_point;
767 vertex_position = OpenCASCADE::point(centered_point);
768 // Perform the evaluation
769 vertex = BRepBuilderAPI_MakeVertex(vertex_position);
770 distancetool.LoadS2(vertex);
771 distancetool.Perform();
772
773 // Convert OpenCascade solution point to dealii Point.
774 gp_Pnt pt_on_surface = distancetool.PointOnShape1(1);
775 projected_point[0] = pt_on_surface.X();
776 projected_point[1] = pt_on_surface.Y();
777 if constexpr (dim == 3)
778 {
779 projected_point[2] = pt_on_surface.Z();
780 }
781
782 point_classifier.Perform(vertex_position, shape_tol);
783 TopAbs_State point_state = point_classifier.State();
784 // Check if the evaluation point is inside the shape (This check can
785 // return true only if the shape is a solid). By default, step file format
786 // will define a solid. This is necessary to sign the distance function
787 // evaluation.
788 if (point_state == TopAbs_State::TopAbs_IN)
789 {
790 // If the evaluation point is inside the shape and the shape is a
791 // solid, the distance to the shape will be 0. This is why we need to
792 // evaluate the distance of the point with the shell of the shape.
793 // Rotate the solution found to the global reference frame and cache
794 // the solution.
795 if (!this->part_of_a_composite)
796 {
797 this->value_cache[point_in_string] =
798 -(centered_point - projected_point).norm();
799 auto rotate_in_globalpoint =
800 this->reverse_align_and_center(projected_point);
801 this->gradient_cache[point_in_string] =
802 (rotate_in_globalpoint - evaluation_point) /
803 ((rotate_in_globalpoint - evaluation_point).norm() + 1.0e-16);
804 }
805 return -(centered_point - projected_point).norm() -
806 this->layer_thickening;
807 }
808 else
809 {
810 if (!this->part_of_a_composite)
811 {
812 this->value_cache[point_in_string] =
813 (centered_point - projected_point).norm();
814 auto rotate_in_globalpoint =
815 this->reverse_align_and_center(projected_point);
816 this->gradient_cache[point_in_string] =
817 -(rotate_in_globalpoint - evaluation_point) /
818 ((rotate_in_globalpoint - evaluation_point).norm() + 1.0e-16);
819 }
820 return (centered_point - projected_point).norm() -
821 this->layer_thickening;
822 }
823 }
824 else
825 {
826 return this->value_cache[point_in_string];
827 }
828#else
829 (void)evaluation_point;
830 return 0;
831#endif
832}
833
834template <int dim>
835std::shared_ptr<Shape<dim>>
837{
838 std::shared_ptr<Shape<dim>> copy =
839 std::make_shared<OpenCascadeShape<dim>>(local_file_name,
840 this->position,
841 this->orientation);
842 return copy;
843}
844
845template <int dim>
846Tensor<1, dim>
847OpenCascadeShape<dim>::gradient(const Point<dim> &evaluation_point,
848 const unsigned int /*component*/) const
849{
850#ifdef DEAL_II_WITH_OPENCASCADE
851 Point<dim> centered_point = this->align_and_center(evaluation_point);
852 Point<dim> projected_point;
853
854 // Transform the point to an OpenCascade point
855 auto pt = OpenCASCADE::point(centered_point);
856 TopoDS_Vertex vertex = BRepBuilderAPI_MakeVertex(pt);
857 // Perform the evaluation
858 BRepExtrema_DistShapeShape distancetool(shape, vertex);
859 distancetool.Perform();
860 gp_Pnt pt_on_surface = distancetool.PointOnShape1(1);
861
862 // Convert OpenCascade solution point to dealii Point.
863 projected_point[0] = pt_on_surface.X();
864 projected_point[1] = pt_on_surface.Y();
865 if constexpr (dim == 3)
866 {
867 projected_point[2] = pt_on_surface.Z();
868 }
869 BRepClass3d_SolidClassifier point_classifier(shape);
870 point_classifier.Perform(pt, shape_tol);
871 TopAbs_State point_state = point_classifier.State();
872 // Check the evaluation point is inside the shape (This check can return true
873 // only if the shape is a solid). By default, step file format will define a
874 // solid. This is necessary to sign the distance function evaluation.
875 if (point_state == TopAbs_State::TopAbs_IN)
876 {
877 // If the evaluation point is inside the shape and the shape is a solid,
878 // the distance to the shape will be 0. This is why we need to evaluate
879 // the distance of the point with the shell of the shape.
880 // Rotate the solution found to the global reference frame.
881 auto rotate_in_globalpoint =
882 this->reverse_align_and_center(projected_point);
883 return (rotate_in_globalpoint - evaluation_point) /
884 ((rotate_in_globalpoint - evaluation_point).norm() + 1.0e-16);
885 }
886 else
887 {
888 auto rotate_in_globalpoint =
889 this->reverse_align_and_center(projected_point);
890 return -(rotate_in_globalpoint - evaluation_point) /
891 ((rotate_in_globalpoint - evaluation_point).norm() + 1.0e-16);
892 }
893#else
894 // Empty return if Lethe is not compile with OpenCascade
895 (void)evaluation_point;
896 return Point<dim>();
897#endif
898}
899
900
901template <int dim>
902Tensor<1, dim>
904 const Point<dim> &evaluation_point,
905 const typename DoFHandler<dim>::active_cell_iterator & /*cell*/,
906 const unsigned int /*component*/)
907{
908#ifdef DEAL_II_WITH_OPENCASCADE
909 // This function is identical to the Gradient function but has the possibility
910 // to use the cache of the shape.
911
912 // First step is to convert the point into a string.
913 auto point_in_string = this->point_to_string(evaluation_point);
914 // We check the unordered map
915 auto iterator = this->gradient_cache.find(point_in_string);
916 // If we did not find the solution in the map we perform the calculation.
917 if (iterator == this->gradient_cache.end())
918 {
919 // Transform the point to an OpenCascade point
920 Point<dim> centered_point = this->align_and_center(evaluation_point);
921 Point<dim> projected_point;
922 vertex_position = OpenCASCADE::point(centered_point);
923 // Perform the evaluation
924 vertex = BRepBuilderAPI_MakeVertex(vertex_position);
925 distancetool.LoadS2(vertex);
926 distancetool.Perform();
927
928 // Convert OpenCascade solution point to dealii Point.
929 gp_Pnt pt_on_surface = distancetool.PointOnShape1(1);
930 projected_point[0] = pt_on_surface.X();
931 projected_point[1] = pt_on_surface.Y();
932 if constexpr (dim == 3)
933 {
934 projected_point[2] = pt_on_surface.Z();
935 }
936 point_classifier.Perform(vertex_position, shape_tol);
937 TopAbs_State point_state = point_classifier.State();
938 // Check the evaluation point is inside the shape (This check can return
939 // true only if the shape is a solid). By default, step file format will
940 // define a solid. This is necessary to sign the distance function
941 // evaluation.
942 if (point_state == TopAbs_State::TopAbs_IN)
943 {
944 // Rotate the solution found to the global reference frame and cache
945 // the solution.
946 auto rotate_in_globalpoint =
947 this->reverse_align_and_center(projected_point);
948 if (!this->part_of_a_composite)
949 {
950 this->value_cache[point_in_string] =
951 -(centered_point - projected_point).norm();
952 this->gradient_cache[point_in_string] =
953 (rotate_in_globalpoint - evaluation_point) /
954 ((rotate_in_globalpoint - evaluation_point).norm() + 1.0e-16);
955 return this->gradient_cache[point_in_string];
956 }
957 else
958 return (rotate_in_globalpoint - evaluation_point) /
959 ((rotate_in_globalpoint - evaluation_point).norm() +
960 1.0e-16);
961 }
962 else
963 {
964 auto rotate_in_globalpoint =
965 this->reverse_align_and_center(projected_point);
966 if (!this->part_of_a_composite)
967 {
968 this->value_cache[point_in_string] =
969 (centered_point - projected_point).norm();
970 this->gradient_cache[point_in_string] =
971 -(rotate_in_globalpoint - evaluation_point) /
972 ((rotate_in_globalpoint - evaluation_point).norm() + 1.0e-16);
973 return this->gradient_cache[point_in_string];
974 }
975 else
976 return -(rotate_in_globalpoint - evaluation_point) /
977 ((rotate_in_globalpoint - evaluation_point).norm() +
978 1.0e-16);
979 }
980 }
981 else
982 {
983 // If we are here it is that this point was already evaluated and the
984 // previous evaluation was cache.
985 return this->gradient_cache[point_in_string];
986 }
987#else
988
989 (void)evaluation_point;
990 return Tensor<1, dim>();
991#endif
992}
993
994
995template <int dim>
996double
1001
1002template <int dim>
1003void
1005{
1006 this->Shape<dim>::set_position(position);
1007}
1008
1009
1010template <int dim>
1011double
1012HyperRectangle<dim>::value(const Point<dim> &evaluation_point,
1013 const unsigned int /*component*/) const
1014{
1015 Point<dim> centered_point = this->align_and_center(evaluation_point);
1016
1017 Point<dim> abs_p;
1018 Point<dim> half_lengths_dim;
1019 for (int i = 0; i < dim; ++i)
1020 {
1021 abs_p[i] = std::abs(centered_point[i]);
1022 half_lengths_dim[i] = half_lengths[i];
1023 }
1024 Point<dim> q;
1025 q = abs_p - half_lengths_dim;
1026 Point<dim> max_q_0;
1027 for (int i = 0; i < dim; ++i)
1028 {
1029 max_q_0[i] = std::max(q[i], 0.0);
1030 }
1031 const double max_q = std::max(q[0], std::max(q[1], q[dim - 1]));
1032 return max_q_0.norm() + std::min(max_q, 0.0) - this->layer_thickening;
1033}
1034
1035template <int dim>
1036std::shared_ptr<Shape<dim>>
1038{
1039 std::shared_ptr<Shape<dim>> copy =
1040 std::make_shared<HyperRectangle<dim>>(this->half_lengths,
1041 this->position,
1042 this->orientation);
1043 return copy;
1044}
1045
1046template <int dim>
1047double
1049{
1050 double solid_volume = 1.0;
1051 for (int i = 0; i < dim; ++i)
1052 {
1053 solid_volume = solid_volume * 2.0 * half_lengths[i];
1054 }
1055 return solid_volume;
1056}
1057
1058template <int dim>
1059double
1060Ellipsoid<dim>::value(const Point<dim> &evaluation_point,
1061 const unsigned int /*component*/) const
1062{
1063 Point<dim> centered_point = this->align_and_center(evaluation_point);
1064
1065 Point<dim> v_k0;
1066 Point<dim> v_k1;
1067 for (int i = 0; i < dim; ++i)
1068 {
1069 // Ellipsoid parameters[0->2]:= radii x, y, z
1070 v_k0[i] = centered_point[i] / radii[i];
1071 v_k1[i] = centered_point[i] / (radii[i] * radii[i]);
1072 }
1073 double k0 = v_k0.norm();
1074 double k1 = v_k1.norm();
1075 return k0 * (k0 - 1.) / k1 - this->layer_thickening;
1076}
1077
1078template <int dim>
1079std::shared_ptr<Shape<dim>>
1081{
1082 std::shared_ptr<Shape<dim>> copy =
1083 std::make_shared<Ellipsoid<dim>>(this->radii,
1084 this->position,
1085 this->orientation);
1086 return copy;
1087}
1088
1089template <int dim>
1090double
1092{
1093 using numbers::PI;
1094 double solid_volume = PI * 4.0 / 3.0;
1095 for (int i = 0; i < dim; i++)
1096 {
1097 solid_volume = solid_volume * radii[i];
1098 }
1099 return solid_volume;
1100}
1101
1102template <int dim>
1103double
1104Torus<dim>::value(const Point<dim> &evaluation_point,
1105 const unsigned int /*component*/) const
1106{
1107 AssertDimension(dim, 3);
1108
1109 Point<dim> centered_point = this->align_and_center(evaluation_point);
1110
1111 Point<2> p_xz({centered_point[0], centered_point[2]});
1112 Point<2> q({p_xz.norm() - ring_radius, centered_point[1]});
1113 return q.norm() - ring_thickness - this->layer_thickening;
1114}
1115
1116template <int dim>
1117std::shared_ptr<Shape<dim>>
1119{
1120 std::shared_ptr<Shape<dim>> copy = std::make_shared<Torus<dim>>(
1121 this->ring_radius, this->ring_thickness, this->position, this->orientation);
1122 return copy;
1123}
1124
1125template <int dim>
1126double
1128{
1129 using numbers::PI;
1130 return 2.0 * PI * PI * ring_radius * ring_thickness * ring_thickness;
1131}
1132
1133template <int dim>
1134double
1135Cone<dim>::value(const Point<dim> &evaluation_point,
1136 const unsigned int /*component*/) const
1137{
1138 AssertDimension(dim, 3);
1139
1140 Point<dim> centered_point = this->align_and_center(evaluation_point);
1141
1142 // For a cone, the parameters are tan(base angle) and height
1143 Point<2> p_xz({centered_point[0], centered_point[2]});
1144 Point<2> w({p_xz.norm(), centered_point[1]});
1145 double dot_w_q = scalar_product<1, 2, double>(w, intermediate_q);
1146 double dot_q_q = scalar_product<1, 2, double>(intermediate_q, intermediate_q);
1147 Point<2> a;
1148 a = w - intermediate_q * std::clamp(dot_w_q / dot_q_q, 0., 1.0);
1149 Point<2> b_intermediate1({std::clamp(w[0] / intermediate_q[0], 0.0, 1.), 1.});
1150 Point<2> b_intermediate2({intermediate_q[0] * b_intermediate1[0],
1151 intermediate_q[1] * b_intermediate1[1]});
1152 Point<2> b;
1153 b = w - b_intermediate2;
1154 double k = (intermediate_q[1] > 0) ? 1 : ((intermediate_q[1] < 0) ? -1 : 0);
1155 double d = std::min(scalar_product<1, 2, double>(a, a),
1156 scalar_product<1, 2, double>(b, b));
1157 double s = std::max(k * (w[0] * intermediate_q[1] - w[1] * intermediate_q[0]),
1158 k * (w[1] - intermediate_q[1]));
1159
1160 return sqrt(d) * ((s > 0) ? 1 : ((s < 0) ? -1 : 0)) - this->layer_thickening;
1161}
1162
1163template <int dim>
1164std::shared_ptr<Shape<dim>>
1166{
1167 std::shared_ptr<Shape<dim>> copy = std::make_shared<Cone<dim>>(
1168 this->tan_base_angle, this->height, this->position, this->orientation);
1169 return copy;
1170}
1171
1172template <int dim>
1173double
1175{
1176 using numbers::PI;
1177 return PI / 3.0 * base_radius * base_radius * height;
1178}
1179
1180template <int dim>
1181double
1182CutHollowSphere<dim>::value(const Point<dim> &evaluation_point,
1183 const unsigned int /*component*/) const
1184{
1185 AssertDimension(dim, 3);
1186
1187 Point<dim> centered_point = this->align_and_center(evaluation_point);
1188
1189 Point<2> p_xz({centered_point[0], centered_point[2]});
1190 Point<2> q({p_xz.norm(), centered_point[1]});
1191
1192 if (cut_depth * q[0] < intermediate_w * q[1])
1193 {
1194 Point<2> wh({intermediate_w, cut_depth});
1195 return (q - wh).norm() - this->layer_thickening;
1196 }
1197 else
1198 {
1199 return std::abs(q.norm() - radius) - shell_thickness -
1200 this->layer_thickening;
1201 }
1202}
1203
1204template <int dim>
1205std::shared_ptr<Shape<dim>>
1207{
1208 std::shared_ptr<Shape<dim>> copy =
1209 std::make_shared<CutHollowSphere<dim>>(this->radius,
1210 this->cut_depth,
1211 this->shell_thickness,
1212 this->position,
1213 this->orientation);
1214 return copy;
1215}
1216
1217template <int dim>
1218double
1220{
1221 using numbers::PI;
1222 double small_radius = radius - shell_thickness;
1223 return 4.0 * PI / 3.0 *
1224 (radius * radius * radius -
1225 small_radius * small_radius * small_radius);
1226}
1227
1228template <int dim>
1229double
1230DeathStar<dim>::value(const Point<dim> &evaluation_point,
1231 const unsigned int /*component*/) const
1232{
1233 AssertDimension(dim, 3);
1234
1235 Point<dim> centered_point = this->align_and_center(evaluation_point);
1236
1237 Point<2> p_yz({centered_point[1], centered_point[2]});
1238 Point<2> corrected_p_2d({centered_point[0], p_yz.norm()});
1239 if (corrected_p_2d[0] * intermediate_b - corrected_p_2d[1] * intermediate_a >
1240 spheres_distance * std::max(intermediate_b - corrected_p_2d[1], 0.0))
1241 {
1242 Point<2> ab({intermediate_a, intermediate_b});
1243 return (corrected_p_2d - ab).norm() - this->layer_thickening;
1244 }
1245 else
1246 {
1247 Point<2> d0({spheres_distance, 0.});
1248 return std::max(corrected_p_2d.norm() - radius,
1249 -((corrected_p_2d - d0).norm() - hole_radius)) -
1250 this->layer_thickening;
1251 }
1252}
1253
1254template <int dim>
1255std::shared_ptr<Shape<dim>>
1257{
1258 std::shared_ptr<Shape<dim>> copy =
1259 std::make_shared<DeathStar<dim>>(this->radius,
1260 this->hole_radius,
1261 this->spheres_distance,
1262 this->position,
1263 this->orientation);
1264 return copy;
1265}
1266
1267template <int dim>
1268double
1270{
1271 using numbers::PI;
1272 return 4. * PI / 3. * radius * radius * radius;
1273}
1274
1275template <int dim>
1276double
1277CompositeShape<dim>::value(const Point<dim> &evaluation_point,
1278 const unsigned int /*component*/) const
1279{
1280 auto point_in_string = this->point_to_string(evaluation_point);
1281 auto iterator = this->value_cache.find(point_in_string);
1282 if (iterator == this->value_cache.end())
1283 {
1284 // We align and center the evaluation point according to the shape
1285 // referential
1286 Point<dim> centered_point = this->align_and_center(evaluation_point);
1287
1288 // The level set value of all component shapes is computed
1289 std::map<unsigned int, double> constituent_shapes_values;
1290 std::map<unsigned int, Tensor<1, dim>> constituent_shapes_gradients;
1291 for (auto const &[component_id, component] : constituents)
1292 {
1293 constituent_shapes_values[component_id] =
1294 component->value(centered_point);
1295 // A dummy gradient is used here because apply_boolean_operations
1296 // requires a gradient map as an argument. This design choice of not
1297 // duplicating apply_boolean_operations was made for brevity of the
1298 // code, at a negligible additional computing cost.
1299 constituent_shapes_gradients[component_id] = Tensor<1, dim>{};
1300 }
1301
1302 double levelset;
1303 std::tie(levelset, std::ignore) =
1304 apply_boolean_operations(constituent_shapes_values,
1305 constituent_shapes_gradients);
1306 return levelset;
1307 }
1308 else
1309 return iterator->second;
1310}
1311
1312template <int dim>
1313double
1315 const Point<dim> &evaluation_point,
1316 const typename DoFHandler<dim>::active_cell_iterator &cell,
1317 const unsigned int /*component*/)
1318{
1319 auto point_in_string = this->point_to_string(evaluation_point);
1320 auto iterator = this->value_cache.find(point_in_string);
1321 if (iterator == this->value_cache.end())
1322 {
1323 // We align and center the evaluation point according to the shape
1324 // referential
1325 Point<dim> centered_point = this->align_and_center(evaluation_point);
1326
1327 // The level set value of all component shapes is computed
1328 std::map<unsigned int, double> constituent_shapes_values;
1329 std::map<unsigned int, Tensor<1, dim>> constituent_shapes_gradients;
1330 for (auto const &[component_id, component] : constituents)
1331 {
1332 constituent_shapes_values[component_id] =
1333 component->value_with_cell_guess(centered_point, cell);
1334 // A dummy gradient is used here because apply_boolean_operations
1335 // requires a gradient map as an argument. This design choice of not
1336 // duplicating apply_boolean_operations was made for brevity of the
1337 // code, at a negligible additional computing cost.
1338 constituent_shapes_gradients[component_id] = Tensor<1, dim>{};
1339 }
1340
1341 double levelset;
1342 std::tie(levelset, std::ignore) =
1343 apply_boolean_operations(constituent_shapes_values,
1344 constituent_shapes_gradients);
1345 if (!this->part_of_a_composite)
1346 {
1347 this->value_cache[point_in_string] = levelset;
1348 }
1349 return levelset;
1350 }
1351 else
1352 return this->value_cache[point_in_string];
1353}
1354
1355template <int dim>
1356Tensor<1, dim>
1357CompositeShape<dim>::gradient(const Point<dim> &evaluation_point,
1358 const unsigned int /*component*/) const
1359{
1360 auto point_in_string = this->point_to_string(evaluation_point);
1361 auto iterator = this->gradient_cache.find(point_in_string);
1362 if (iterator == this->gradient_cache.end())
1363 {
1364 // We align and center the evaluation point according to the shape
1365 // referential
1366 Point<dim> centered_point = this->align_and_center(evaluation_point);
1367 // The level set value and gradient of all component shapes is computed
1368 std::map<unsigned int, double> constituent_shapes_values;
1369 std::map<unsigned int, Tensor<1, dim>> constituent_shapes_gradients;
1370 for (auto const &[component_id, component] : constituents)
1371 {
1372 constituent_shapes_values[component_id] =
1373 component->value(centered_point);
1374 constituent_shapes_gradients[component_id] =
1375 component->gradient(centered_point);
1376 }
1377
1378 double levelset;
1379 Tensor<1, dim> gradient;
1380 std::tie(levelset, gradient) =
1381 apply_boolean_operations(constituent_shapes_values,
1382 constituent_shapes_gradients);
1383
1384 // The gradient obtained is in the shape referential. It needs to be
1385 // returned to the global referential. We find the closest point (in the
1386 // shape referential), we return that point to its global referential
1387 // equivalent, then compute the gradient in the global referential.
1388 Point<dim> closest_point_shape_referential =
1389 centered_point -
1390 (gradient / (gradient.norm() + 1e-16 * this->effective_radius)) *
1391 levelset;
1392 Point<dim> closest_point_global_referential =
1393 this->reverse_align_and_center(closest_point_shape_referential);
1394 gradient = -(closest_point_global_referential - evaluation_point) /
1395 ((closest_point_global_referential - evaluation_point).norm() +
1396 1.0e-16);
1397
1398 return gradient;
1399 }
1400 else
1401 return iterator->second;
1402}
1403
1404template <int dim>
1405Tensor<1, dim>
1407 const Point<dim> &evaluation_point,
1408 const typename DoFHandler<dim>::active_cell_iterator &cell,
1409 const unsigned int /*component*/)
1410{
1411 auto point_in_string = this->point_to_string(evaluation_point);
1412 auto iterator = this->gradient_cache.find(point_in_string);
1413 if (iterator == this->gradient_cache.end())
1414 {
1415 // We align and center the evaluation point according to the shape
1416 // referential
1417 Point<dim> centered_point = this->align_and_center(evaluation_point);
1418 // The level set value and gradient of all component shapes is computed
1419 std::map<unsigned int, double> constituent_shapes_values;
1420 std::map<unsigned int, Tensor<1, dim>> constituent_shapes_gradients;
1421 for (auto const &[component_id, component] : constituents)
1422 {
1423 constituent_shapes_values[component_id] =
1424 component->value_with_cell_guess(centered_point, cell);
1425 constituent_shapes_gradients[component_id] =
1426 component->gradient_with_cell_guess(centered_point, cell);
1427 }
1428
1429 double levelset;
1430 Tensor<1, dim> gradient;
1431 std::tie(levelset, gradient) =
1432 apply_boolean_operations(constituent_shapes_values,
1433 constituent_shapes_gradients);
1434
1435 // The gradient obtained is in the shape referential. It needs to be
1436 // returned to the global referential. We find the closest point (in the
1437 // shape referential), we return that point to its global referential
1438 // equivalent, then compute the gradient in the global referential.
1439 Point<dim> closest_point_shape_referential =
1440 centered_point -
1441 (gradient / (gradient.norm() + 1e-16 * this->effective_radius)) *
1442 levelset;
1443 Point<dim> closest_point_global_referential =
1444 this->reverse_align_and_center(closest_point_shape_referential);
1445 gradient = -(closest_point_global_referential - evaluation_point) /
1446 ((closest_point_global_referential - evaluation_point).norm() +
1447 1.0e-16);
1448 if (!this->part_of_a_composite)
1449 {
1450 this->gradient_cache[point_in_string] = gradient;
1451 }
1452 return gradient;
1453 }
1454 else
1455 return this->gradient_cache[point_in_string];
1456}
1457
1458template <int dim>
1459std::shared_ptr<Shape<dim>>
1461{
1462 std::shared_ptr<Shape<dim>> copy = std::make_shared<CompositeShape<dim>>(
1464 return copy;
1465}
1466
1467template <int dim>
1468void
1470 DoFHandler<dim> &updated_dof_handler,
1471 const bool mesh_based_precalculations)
1472{
1473 if (!mesh_based_precalculations)
1474 return;
1475 for (auto const &constituent : constituents | boost::adaptors::map_values)
1476 if (typeid(*constituent) == typeid(RBFShape<dim>))
1477 std::static_pointer_cast<RBFShape<dim>>(constituent)
1478 ->update_precalculations(updated_dof_handler,
1479 mesh_based_precalculations);
1480 else if (typeid(*constituent) == typeid(CompositeShape<dim>))
1481 std::static_pointer_cast<CompositeShape<dim>>(constituent)
1482 ->update_precalculations(updated_dof_handler,
1483 mesh_based_precalculations);
1484}
1485
1486template <int dim>
1487void
1489{
1490 for (auto const &constituent : constituents | boost::adaptors::map_values)
1491 if (typeid(*constituent) == typeid(RBFShape<dim>))
1492 std::static_pointer_cast<RBFShape<dim>>(constituent)
1494 else if (typeid(*constituent) == typeid(CompositeShape<dim>))
1495 std::static_pointer_cast<CompositeShape<dim>>(constituent)
1497}
1498
1499template <int dim>
1500void
1502 DoFHandler<dim> &updated_dof_handler,
1503 const bool mesh_based_precalculations)
1504{
1505 for (auto const &constituent : constituents | boost::adaptors::map_values)
1506 if (typeid(*constituent) == typeid(RBFShape<dim>))
1507 std::static_pointer_cast<RBFShape<dim>>(constituent)
1508 ->remove_superfluous_data(updated_dof_handler,
1509 mesh_based_precalculations);
1510 else if (typeid(*constituent) == typeid(CompositeShape<dim>))
1511 std::static_pointer_cast<CompositeShape<dim>>(constituent)
1512 ->remove_superfluous_data(updated_dof_handler,
1513 mesh_based_precalculations);
1514}
1515
1516template <int dim>
1517std::pair<double, Tensor<1, dim>>
1519 std::map<unsigned int, double> constituent_shapes_values,
1520 std::map<unsigned int, Tensor<1, dim>> constituent_shapes_gradients) const
1521{
1522 // The boolean operations between the shapes are applied in order
1523 // The last computed values are considered to be the correct values to return
1524 double levelset = constituent_shapes_values[0];
1525 Tensor<1, dim> gradient = constituent_shapes_gradients[0];
1526 for (auto const &[operation_id, op_triplet] : operations)
1527 {
1528 BooleanOperation operation;
1529 unsigned int first_id;
1530 unsigned int second_id;
1531 std::tie(operation, first_id, second_id) = op_triplet;
1532
1533 double value_first_component, value_second_component;
1534 Tensor<1, dim> gradient_first_component{};
1535 Tensor<1, dim> gradient_second_component{};
1536 value_first_component = constituent_shapes_values.at(first_id);
1537 value_second_component = constituent_shapes_values.at(second_id);
1538 gradient_first_component = constituent_shapes_gradients.at(first_id);
1539 gradient_second_component = constituent_shapes_gradients.at(second_id);
1540 switch (operation)
1541 {
1543 if (value_first_component < value_second_component)
1544 {
1545 constituent_shapes_values[operation_id] = value_first_component;
1546 constituent_shapes_gradients[operation_id] =
1547 gradient_first_component;
1548 }
1549 else
1550 {
1551 constituent_shapes_values[operation_id] =
1552 value_second_component;
1553 constituent_shapes_gradients[operation_id] =
1554 gradient_second_component;
1555 }
1556 break;
1558 if (-value_first_component > value_second_component)
1559 {
1560 constituent_shapes_values[operation_id] =
1561 -value_first_component;
1562 constituent_shapes_gradients[operation_id] =
1563 -gradient_first_component;
1564 }
1565 else
1566 {
1567 constituent_shapes_values[operation_id] =
1568 value_second_component;
1569 constituent_shapes_gradients[operation_id] =
1570 gradient_second_component;
1571 }
1572 break;
1573 default: // BooleanOperation::Intersection
1574 if (value_first_component > value_second_component)
1575 {
1576 constituent_shapes_values[operation_id] = value_first_component;
1577 constituent_shapes_gradients[operation_id] =
1578 gradient_first_component;
1579 }
1580 else
1581 {
1582 constituent_shapes_values[operation_id] =
1583 value_second_component;
1584 constituent_shapes_gradients[operation_id] =
1585 gradient_second_component;
1586 }
1587 }
1588 levelset = constituent_shapes_values[operation_id];
1589 gradient = constituent_shapes_gradients[operation_id];
1590 }
1591 return {levelset, gradient};
1592}
1593
1594template <int dim>
1595void
1597{
1599 // The constituents themselves don't need to have their cache cleared, because
1600 // everytime the value or gradient functions are called the evaluation point
1601 // is modified (the relationships between the constituents don't change).
1602}
1603
1604template <int dim>
1605void
1607{
1608 this->Shape<dim>::set_layer_thickening(layer_thickening);
1609 for (auto const &constituent : constituents | boost::adaptors::map_values)
1610 constituent->set_layer_thickening(layer_thickening);
1611}
1612
1613template <int dim>
1614RBFShape<dim>::RBFShape(const std::string &shape_arguments_str,
1615 const Point<dim> &position,
1616 const Tensor<1, 3> &orientation)
1617 : Shape<dim>(1, position, orientation)
1618 , number_of_nodes(1)
1619 , iterable_nodes(1)
1623 , weights(1)
1624 , nodes_positions(1)
1625 , support_radii(1)
1626 , basis_functions(1)
1627 , useful_rbf_nodes(1)
1628{
1629 this->filename = shape_arguments_str;
1630 this->load_data_from_file();
1631}
1632
1633template <int dim>
1634double
1636 const Point<dim> &evaluation_point,
1637 const typename DoFHandler<dim>::active_cell_iterator &cell,
1638 const unsigned int /*component*/)
1639{
1640 double bounding_box_distance = bounding_box->value(evaluation_point);
1641 if (bounding_box_distance >= 0)
1642 return bounding_box_distance + this->effective_radius;
1643
1644 auto point_in_string = this->point_to_string(evaluation_point);
1645 auto iterator = this->value_cache.find(point_in_string);
1646 if (iterator == this->value_cache.end())
1647 {
1648 swap_iterable_nodes(cell);
1649 double value = this->value(evaluation_point);
1650 swap_iterable_nodes(cell);
1651 if (!this->part_of_a_composite)
1652 {
1653 this->value_cache[point_in_string] = value;
1654 }
1655 return value;
1656 }
1657 else
1658 {
1659 return this->value_cache[point_in_string];
1660 }
1661}
1662
1663template <int dim>
1664Tensor<1, dim>
1666 const Point<dim> &evaluation_point,
1667 const typename DoFHandler<dim>::active_cell_iterator &cell,
1668 const unsigned int /*component*/)
1669{
1670 Point<dim> centered_point = evaluation_point;
1671 double bounding_box_distance = bounding_box->value(centered_point);
1672 Tensor<1, dim> bounding_box_gradient = bounding_box->gradient(centered_point);
1673 if (bounding_box_distance > 0.)
1674 return bounding_box_gradient;
1675
1676 auto point_in_string = this->point_to_string(evaluation_point);
1677 auto iterator = this->gradient_cache.find(point_in_string);
1678 if (iterator == this->gradient_cache.end())
1679 {
1680 swap_iterable_nodes(cell);
1681 Tensor<1, dim> gradient = this->gradient(evaluation_point);
1682 swap_iterable_nodes(cell);
1683 if (!this->part_of_a_composite)
1684 {
1685 this->gradient_cache[point_in_string] = gradient;
1686 }
1687 return gradient;
1688 }
1689 else
1690 {
1691 return this->gradient_cache[point_in_string];
1692 }
1693}
1694
1695template <int dim>
1696double
1697RBFShape<dim>::value(const Point<dim> &evaluation_point,
1698 const unsigned int /*component*/) const
1699{
1700 double bounding_box_distance = bounding_box->value(evaluation_point);
1701 if (bounding_box_distance >= 0)
1702 return bounding_box_distance + this->effective_radius -
1703 this->layer_thickening;
1704
1705 auto point_in_string = this->point_to_string(evaluation_point);
1706 auto iterator = this->value_cache.find(point_in_string);
1707 if (iterator != this->value_cache.end())
1708 return iterator->second;
1709
1710 double value = std::max(bounding_box_distance, 0.0);
1711 double normalized_distance, basis;
1712 // Algorithm inspired by Optimad Bitpit. https://github.com/optimad/bitpit
1713 // Here we loop on every portion (of RBF nodes located in active cells close
1714 // to the evaluation point) and on every RBF node located in these active
1715 // cells.
1716 for (size_t portion_id = 0; portion_id < iterable_nodes.size(); portion_id++)
1717 {
1718 for (const size_t &node_id : *(std::get<2>(iterable_nodes[portion_id])))
1719 {
1720 normalized_distance =
1721 (evaluation_point - rotated_nodes_positions[node_id]).norm() /
1722 support_radii[node_id];
1723 // We cast the basis function value to the proper RBFBasisFunction
1725 static_cast<enum RBFShape<dim>::RBFBasisFunction>(
1726 round(basis_functions[node_id])),
1727 normalized_distance);
1728 value += basis * weights[node_id];
1729 }
1730 }
1731 return value - this->layer_thickening;
1732}
1733
1734template <int dim>
1735Tensor<1, dim>
1736RBFShape<dim>::gradient(const Point<dim> &evaluation_point,
1737 const unsigned int /*component*/) const
1738{
1739 Point<dim> centered_point = evaluation_point;
1740 double bounding_box_distance = bounding_box->value(centered_point);
1741 Tensor<1, dim> bounding_box_gradient = bounding_box->gradient(centered_point);
1742 if (bounding_box_distance > 0.)
1743 return bounding_box_gradient;
1744
1745 double distance, normalized_distance;
1746 Point<dim> relative_position{};
1747 // We use chain derivation to express the gradient of the basis in regards to
1748 // the position d(basis)/dx = d(basis) /d(normalized_distance)
1749 // * d(normalized_distance)/d(distance)
1750 // * d(distance) /dx
1751 double dbasis_drnorm_derivative;
1752 double drnorm_dr_derivative;
1753 Tensor<1, dim> dr_dx_derivative{};
1754 Tensor<1, dim> gradient{};
1755
1756 if (iterable_nodes.size() == 0)
1757 {
1758 throw std::logic_error(
1759 "Every location inside the bounding box should be covered by the radius "
1760 "of at least one RBF node. It this isn't the case, an error has been "
1761 "introduced in the code. ");
1762 }
1763 // Here we loop on every portion (of RBF nodes located in active cells close
1764 // to the evaluation point) and on every RBF node located in these active
1765 // cells.
1766 for (size_t portion_id = 0; portion_id < iterable_nodes.size(); portion_id++)
1767 {
1768 for (const size_t &node_id : *(std::get<2>(iterable_nodes[portion_id])))
1769 {
1770 // Calculation of dr/dx
1771 relative_position =
1772 (evaluation_point - rotated_nodes_positions[node_id]);
1773 distance = (relative_position).norm();
1774 normalized_distance = distance / support_radii[node_id];
1775 if (distance > 1e-16 * this->effective_radius)
1776 dr_dx_derivative = relative_position / distance;
1777 else
1778 {
1779 // If the evaluation point overlaps with the node position, we
1780 // assume that the contribution of this node is 0. This assumption
1781 // can be made because radial basis functions are symmetrical. If
1782 // can be made because radial basis functions are symmetrical. If
1783 // the basis function is not differentiable at its node (e.g.
1784 // linear function), this approximation will still hold since the
1785 // approximated distance field is already imperfect and
1786 // neighboring nodes will add their contribution at the evaluation
1787 // point.
1788 for (int d = 0; d < dim; d++)
1789 dr_dx_derivative[d] = 0;
1790 }
1791 // Calculation of dr_norm/dr
1792 drnorm_dr_derivative = 1.0 / support_radii[node_id];
1793 // Calculation of d(basis)/dr
1794 // We cast the basis function value to the proper RBFBasisFunction
1795 dbasis_drnorm_derivative = evaluate_basis_function_derivative(
1796 static_cast<enum RBFShape<dim>::RBFBasisFunction>(
1797 round(basis_functions[node_id])),
1798 normalized_distance);
1799 // Sum
1800 gradient += dbasis_drnorm_derivative * drnorm_dr_derivative *
1801 dr_dx_derivative * weights[node_id];
1802 }
1803 }
1804 return gradient;
1805}
1806
1807template <int dim>
1808std::shared_ptr<Shape<dim>>
1810{
1811 std::shared_ptr<Shape<dim>> copy =
1812 std::make_shared<RBFShape<dim>>(this->filename,
1813 this->position,
1814 this->orientation);
1815 return copy;
1816}
1817
1818template <int dim>
1819double
1821{
1822 return bounding_box->displaced_volume();
1823}
1824
1825template <int dim>
1826void
1828{
1829 Point<dim> high_bounding_point{};
1830 Point<dim> low_bounding_point{};
1831 Point<dim> bounding_box_center{};
1832 Tensor<1, dim> half_lengths = Tensor<1, dim>();
1833 for (int d = 0; d < dim; d++)
1834 {
1835 high_bounding_point[d] = std::numeric_limits<double>::lowest();
1836 low_bounding_point[d] = std::numeric_limits<double>::max();
1837 for (size_t i = 0; i < number_of_nodes; i++)
1838 {
1839 low_bounding_point[d] =
1840 std::min(low_bounding_point[d], nodes_positions[i][d]);
1841 high_bounding_point[d] =
1842 std::max(high_bounding_point[d], nodes_positions[i][d]);
1843 }
1845 0.5 * (low_bounding_point[d] + high_bounding_point[d]);
1846 half_lengths[d] = 0.5 * (high_bounding_point[d] - low_bounding_point[d]);
1847 }
1848 bounding_box =
1849 std::make_shared<HyperRectangle<dim>>(half_lengths,
1852 this->orientation);
1853 this->bounding_box_half_length = half_lengths;
1854 this->bounding_box_center =
1856}
1857
1858template <int dim>
1859void
1860RBFShape<dim>::update_precalculations(DoFHandler<dim> &updated_dof_handler,
1861 const bool mesh_based_precalculations)
1862{
1863 if (!mesh_based_precalculations)
1864 return;
1865 // We first reset the mapping, since the grid partitioning may change between
1866 // calls of this function. The precalculation cost is low enough that this
1867 // reset does not have a significant impact on global computational cost.
1868 likely_nodes_map.clear();
1869
1870 this->dof_handler = &updated_dof_handler;
1871 const unsigned int maximal_level =
1872 updated_dof_handler.get_triangulation().n_levels();
1873
1874 // We start by dividing the list of RBF nodes into manageable portions
1875 number_of_nodes = std::get<2>(iterable_nodes[0])->size();
1876 std::shared_ptr<std::vector<size_t>> temporary_node_numbers =
1877 std::make_shared<std::vector<size_t>>(number_of_nodes);
1878 std::iota(std::begin(*temporary_node_numbers),
1879 std::end(*temporary_node_numbers),
1880 0);
1881 std::map<const typename DoFHandler<dim>::cell_iterator,
1882 std::tuple<Point<dim>, double, std::shared_ptr<std::vector<size_t>>>>
1883 temporary_nodes_portions_map;
1884 temporary_nodes_portions_map.clear();
1885 for (unsigned int level = 0; level < maximal_level; level++)
1886 {
1888 const auto &cell_iterator =
1889 updated_dof_handler.cell_iterators_on_level(level);
1890 for (const auto &cell : cell_iterator)
1891 {
1892 if (cell->is_artificial_on_level())
1893 continue;
1894 if (cell->level() > 0)
1895 try
1896 {
1897 const auto cell_parent = cell->parent();
1898 const auto parent_iterator =
1899 temporary_nodes_portions_map.find(cell_parent);
1900 if (parent_iterator != temporary_nodes_portions_map.end())
1901 {
1902 temporary_node_numbers =
1903 std::get<2>(parent_iterator->second);
1904 }
1905 else
1906 // If the parent doesn't contain any node, the child will not
1907 // either
1908 continue;
1909 }
1910 catch (TriaAccessorExceptions::ExcCellHasNoParent())
1911 {}
1912
1913 std::shared_ptr<std::vector<size_t>> current_cell_nodes =
1914 std::make_shared<std::vector<size_t>>();
1915 current_cell_nodes->reserve(max_number_of_inside_nodes);
1916 for (unsigned int i = 0; i < temporary_node_numbers->size(); i++)
1917 {
1918 if (cell->point_inside(
1919 rotated_nodes_positions[(*temporary_node_numbers)[i]]))
1920 current_cell_nodes->push_back(temporary_node_numbers->at(i));
1921 }
1923 std::max(max_number_of_inside_nodes, current_cell_nodes->size());
1924 current_cell_nodes->shrink_to_fit();
1925 if (!current_cell_nodes->empty())
1926 temporary_nodes_portions_map[cell] =
1927 std::make_tuple(cell->barycenter(),
1928 cell->diameter(),
1929 current_cell_nodes);
1930
1931 // We currently ignore RBF nodes that are in no cells
1932 // A fix for this will be implemented shortly
1933 }
1934 // We need to remove all cells that are not needed anymore
1935 // from the map
1936 for (auto it = temporary_nodes_portions_map.cbegin();
1937 it != temporary_nodes_portions_map.cend();)
1938 {
1939 auto cell = it->first;
1940 const unsigned int cell_level = cell->level();
1941 bool cell_still_needed = cell->is_active() || cell_level == level;
1942
1943 auto previous_it = it;
1944 ++it;
1945 if (!cell_still_needed)
1946 temporary_nodes_portions_map.erase(previous_it);
1947 }
1948 }
1949
1950 // We give all the subsets to the 0 level, as an initial partitioning
1951 const auto &cell_iterator = updated_dof_handler.cell_iterators_on_level(0);
1952 typename DoFHandler<dim>::cell_iterator temp_cell;
1953 std::tuple<Point<dim>, double, std::shared_ptr<std::vector<size_t>>>
1954 temp_cell_tuple{};
1955 for (const auto &cell : cell_iterator)
1956 {
1957 if (cell->is_artificial_on_level())
1958 continue;
1959
1960 likely_nodes_map[cell] = std::make_shared<std::vector<
1961 std::
1962 tuple<Point<dim>, double, std::shared_ptr<std::vector<size_t>>>>>();
1963 for (auto it = temporary_nodes_portions_map.cbegin();
1964 it != temporary_nodes_portions_map.cend();
1965 ++it)
1966 {
1967 temp_cell = it->first;
1968 if (temp_cell->is_active())
1969 {
1970 temp_cell_tuple = std::make_tuple(temp_cell->barycenter(),
1971 temp_cell->diameter(),
1972 std::get<2>(it->second));
1973 likely_nodes_map[cell]->push_back(temp_cell_tuple);
1974 }
1975 }
1976 }
1977
1978 // We then treat all other levels
1979 for (unsigned int level = 1; level < maximal_level; level++)
1980 {
1981 const auto &cell_iterator_on_level =
1982 updated_dof_handler.cell_iterators_on_level(level);
1983 for (const auto &cell : cell_iterator_on_level)
1984 {
1985 if (cell->is_artificial_on_level())
1986 continue;
1987
1988 // We also check if the cell diameter is lower than the minimal
1989 // support radius. In that case, the likely nodes should stay more
1990 // or less the same.
1991 const bool cell_smaller_than_rbf_radius =
1992 (cell->diameter() < maximal_support_radius);
1993 if (cell_smaller_than_rbf_radius)
1994 likely_nodes_map[cell] = likely_nodes_map[cell->parent()];
1995 else
1996 determine_likely_nodes_for_one_cell(cell, cell->barycenter());
1997 }
1998 }
1999
2000 // We need to remove all cells that are not needed anymore
2001 // from the map
2002 for (auto it = likely_nodes_map.cbegin(); it != likely_nodes_map.cend();)
2003 {
2004 auto cell = it->first;
2005 bool cell_still_needed = cell->is_active() && !cell->is_artificial();
2006
2007 auto previous_it = it;
2008 ++it;
2009 if (!cell_still_needed)
2010 likely_nodes_map.erase(previous_it);
2011 }
2012
2013 // Here we loop on all (local or ghost) and active cells to define which RBF
2014 // nodes are useful to keep. We loop over the likely nodes map and take note
2015 // of the RBF nodes that are required in at least one active cell.
2016 number_of_nodes = weights.size();
2017 useful_rbf_nodes.clear();
2019 for (auto it = temporary_nodes_portions_map.cbegin();
2020 it != temporary_nodes_portions_map.cend();
2021 ++it)
2022 {
2023 auto cell = it->first;
2024 if (!cell->is_active())
2025 continue;
2026 if (std::get<2>(it->second).use_count() <= 1)
2027 continue;
2028 for (const size_t &node_id : *(std::get<2>(it->second)))
2029 useful_rbf_nodes[node_id] = true;
2030 }
2031}
2032
2033template <int dim>
2034void
2036 const typename DoFHandler<dim>::cell_iterator &cell,
2037 const Point<dim> support_point)
2038{
2039 // We exit the function immediately if the cell is already in the map
2040 if (likely_nodes_map.contains(cell))
2041 return;
2042
2043 bool parent_found = false;
2044 std::vector<
2045 std::tuple<Point<dim>, double, std::shared_ptr<std::vector<size_t>>>>
2046 temporary_iterable_nodes{
2047 {Point<dim>{}, 0, std::make_shared<std::vector<size_t>>()}};
2048 if (cell->level() > 0)
2049 try
2050 {
2051 const auto cell_parent = cell->parent();
2052 const auto parent_iterator = likely_nodes_map.find(cell_parent);
2053 if (parent_iterator != likely_nodes_map.end())
2054 {
2055 parent_found = true;
2056 temporary_iterable_nodes.swap(iterable_nodes);
2057 iterable_nodes.swap(*parent_iterator->second);
2058 }
2059 }
2060 catch (TriaAccessorExceptions::ExcCellHasNoParent())
2061 {}
2062
2063 double distance, max_distance;
2064 const double cell_diameter = cell->diameter();
2065 Point<dim> temp_cell_barycenter;
2066
2067 likely_nodes_map[cell] = std::make_shared<std::vector<
2068 std::tuple<Point<dim>, double, std::shared_ptr<std::vector<size_t>>>>>();
2069 const size_t number_of_portions = iterable_nodes.size();
2070 for (size_t portion_id = 0; portion_id < number_of_portions; portion_id++)
2071 {
2072 double temp_cell_diameter;
2073 std::tie(temp_cell_barycenter, temp_cell_diameter, std::ignore) =
2074 (iterable_nodes[portion_id]);
2075 // We only check for one support point, but we use a high security
2076 // factor. This allows not to loop over all support points.
2077 distance = (support_point - temp_cell_barycenter).norm();
2078 // We check if the distance is lower than 1 cell diagonal, since we
2079 // only check the distance with 1 support point, added to the support
2080 // radius
2081 max_distance =
2082 0.5 * cell_diameter + 0.5 * temp_cell_diameter + maximal_support_radius;
2083 if (distance < max_distance)
2084 {
2085 likely_nodes_map[cell]->push_back(iterable_nodes[portion_id]);
2086 }
2087 }
2088
2089 if (parent_found)
2090 {
2091 const auto cell_parent = cell->parent();
2092 const auto parent_iterator = likely_nodes_map.find(cell_parent);
2093 iterable_nodes.swap(*parent_iterator->second);
2094 temporary_iterable_nodes.swap(iterable_nodes);
2095 }
2096}
2097
2098template <int dim>
2099void
2101{
2102 weights.clear();
2103 support_radii.clear();
2104 basis_functions.clear();
2105 useful_rbf_nodes.clear();
2106 nodes_positions.clear();
2108
2109 // The following lines retrieve information regarding an RBF
2110 // with a given file name. Then, it converts the information
2111 // into a vector which is used to initialize the RBF shape.
2112 // All the information is concatenated into only one object so
2113 // that the usual initialization function can be called.
2114 std::map<std::string, std::vector<double>> rbf_data;
2115 fill_vectors_from_file(rbf_data, filename, " ");
2116 number_of_nodes = rbf_data["weight"].size();
2117
2118 weights.swap(rbf_data["weight"]);
2119 support_radii.swap(rbf_data["support_radius"]);
2120 basis_functions.swap(rbf_data["basis_function"]);
2122 Point<dim> temp_point;
2123 for (unsigned int i = 0; i < number_of_nodes; i++)
2124 {
2125 temp_point[0] = rbf_data["node_x"][i];
2126 temp_point[1] = rbf_data["node_y"][i];
2127 if constexpr (dim == 3)
2128 temp_point[2] = rbf_data["node_z"][i];
2129 nodes_positions[i] = temp_point;
2130 }
2131
2132 std::shared_ptr<std::vector<size_t>> temp_nodes_id =
2133 std::make_shared<std::vector<size_t>>(number_of_nodes);
2134 std::iota(std::begin(*temp_nodes_id), std::end(*temp_nodes_id), 0);
2135 iterable_nodes.resize(1);
2136 iterable_nodes[0] = {Point<dim>{},
2137 std::numeric_limits<double>::max(),
2138 temp_nodes_id};
2139
2140 maximal_support_radius = *std::ranges::max_element(support_radii);
2142 rotate_nodes();
2143 this->effective_radius = bounding_box->half_lengths.norm();
2144}
2145
2146template <int dim>
2147void
2148RBFShape<dim>::remove_superfluous_data(DoFHandler<dim> &updated_dof_handler,
2149 const bool mesh_based_precalculations)
2150{
2151 if (!mesh_based_precalculations)
2152 return;
2153
2154 if (likely_nodes_map.empty())
2155 this->update_precalculations(updated_dof_handler,
2156 mesh_based_precalculations);
2157
2159 size_t current_number_of_kept_rbf_nodes = 0;
2160 for (size_t i = 0; i < number_of_nodes; i++)
2161 if (useful_rbf_nodes[i])
2162 current_number_of_kept_rbf_nodes++;
2163
2164 std::vector<double> temp_weights;
2165 std::vector<Point<dim>> temp_rotated_nodes_positions;
2166 std::vector<double> temp_support_radii;
2167 std::vector<double> temp_basis_functions;
2168
2169 // Here we treat all vectors separately to keep the maximum memory low
2170 std::shared_ptr<std::vector<size_t>> temp_nodes_id =
2171 std::make_shared<std::vector<size_t>>(current_number_of_kept_rbf_nodes);
2172 std::iota(std::begin(*temp_nodes_id), std::end(*temp_nodes_id), 0);
2173 iterable_nodes.resize(1);
2174 iterable_nodes[0] = {Point<dim>{},
2175 std::numeric_limits<double>::max(),
2176 temp_nodes_id};
2177 std::get<2>(iterable_nodes[0])->shrink_to_fit();
2178
2179 // Weights treatment
2180 size_t counter = 0;
2181 temp_weights.resize(current_number_of_kept_rbf_nodes);
2182 for (size_t i = 0; i < number_of_nodes; i++)
2183 {
2184 if (useful_rbf_nodes[i])
2185 {
2186 // We put back the value to the real node id
2187 temp_weights[counter] = weights[i];
2188 counter++;
2189 }
2190 }
2191 weights.swap(temp_weights);
2192 temp_weights.clear();
2193
2194 // Nodes' positions treatment
2195 counter = 0;
2196 temp_rotated_nodes_positions.resize(current_number_of_kept_rbf_nodes);
2197 for (size_t i = 0; i < number_of_nodes; i++)
2198 {
2199 if (useful_rbf_nodes[i])
2200 {
2201 // We put back the value to the real node id
2202 temp_rotated_nodes_positions[counter] = rotated_nodes_positions[i];
2203 counter++;
2204 }
2205 }
2206 rotated_nodes_positions.swap(temp_rotated_nodes_positions);
2207 temp_rotated_nodes_positions.clear();
2208
2209 // Support radii treatment
2210 counter = 0;
2211 temp_support_radii.resize(current_number_of_kept_rbf_nodes);
2212 for (size_t i = 0; i < number_of_nodes; i++)
2213 {
2214 if (useful_rbf_nodes[i])
2215 {
2216 // We put back the value to the real node id
2217 temp_support_radii[counter] = support_radii[i];
2218 counter++;
2219 }
2220 }
2221 support_radii.swap(temp_support_radii);
2222 temp_support_radii.clear();
2223
2224 // Basis functions treatment
2225 counter = 0;
2226 temp_basis_functions.resize(current_number_of_kept_rbf_nodes);
2227 for (size_t i = 0; i < number_of_nodes; i++)
2228 {
2229 if (useful_rbf_nodes[i])
2230 {
2231 // We put back the value to the real node id
2232 temp_basis_functions[counter] = basis_functions[i];
2233 counter++;
2234 }
2235 }
2236 basis_functions.swap(temp_basis_functions);
2237 temp_basis_functions.clear();
2238
2239 this->update_precalculations(updated_dof_handler, mesh_based_precalculations);
2240}
2241
2242template <int dim>
2243void
2245{
2247 rotated_nodes_positions.resize(weights.size());
2248 for (unsigned int i = 0; i < weights.size(); ++i)
2251 nodes_positions.clear();
2252}
2253
2254
2255template <int dim>
2256double
2258 const double distance) const
2259{
2260 switch (basis_function)
2261 {
2263 return RBFShape<dim>::wendlandc2(distance);
2265 return RBFShape<dim>::linear(distance);
2267 return RBFShape<dim>::gauss90(distance);
2269 return RBFShape<dim>::gauss95(distance);
2271 return RBFShape<dim>::gauss99(distance);
2273 return RBFShape<dim>::c1c0(distance);
2275 return RBFShape<dim>::c2c0(distance);
2277 return RBFShape<dim>::c0c1(distance);
2279 return RBFShape<dim>::c1c1(distance);
2281 return RBFShape<dim>::c2c1(distance);
2283 return RBFShape<dim>::c0c2(distance);
2285 return RBFShape<dim>::c1c2(distance);
2287 return RBFShape<dim>::c2c2(distance);
2289 return RBFShape<dim>::cos(distance);
2290 default:
2291 return RBFShape<dim>::linear(distance);
2292 }
2293}
2294
2295template <int dim>
2296double
2298 const RBFBasisFunction basis_function,
2299 const double distance) const
2300{
2301 switch (basis_function)
2302 {
2304 return RBFShape<dim>::wendlandc2_derivative(distance);
2306 return RBFShape<dim>::linear_derivative(distance);
2308 return RBFShape<dim>::gauss90_derivative(distance);
2310 return RBFShape<dim>::gauss95_derivative(distance);
2312 return RBFShape<dim>::gauss99_derivative(distance);
2314 return RBFShape<dim>::c1c0_derivative(distance);
2316 return RBFShape<dim>::c2c0_derivative(distance);
2318 return RBFShape<dim>::c0c1_derivative(distance);
2320 return RBFShape<dim>::c1c1_derivative(distance);
2322 return RBFShape<dim>::c2c1_derivative(distance);
2324 return RBFShape<dim>::c0c2_derivative(distance);
2326 return RBFShape<dim>::c1c2_derivative(distance);
2328 return RBFShape<dim>::c2c2_derivative(distance);
2330 return RBFShape<dim>::cosinus_derivative(distance);
2331 default:
2332 return RBFShape<dim>::linear_derivative(distance);
2333 }
2334}
2335
2336template <int dim>
2337void
2339 const typename DoFHandler<dim>::active_cell_iterator &cell)
2340{
2341 // Here we check if the likely nodes have been identified
2342 auto iterator = likely_nodes_map.find(cell);
2343 if (iterator != likely_nodes_map.end())
2344 iterable_nodes.swap(*(likely_nodes_map[cell]));
2345}
2346
2347template <int dim>
2348double
2349Cylinder<dim>::value(const Point<dim> &evaluation_point,
2350 const unsigned int /*component*/) const
2351{
2352 Point<dim> centered_point = this->align_and_center(evaluation_point);
2353
2354
2355 double p_radius = std::pow(centered_point[0] * centered_point[0] +
2356 centered_point[1] * centered_point[1],
2357 0.5);
2358 double radius_diff = p_radius - radius;
2359 double h_diff = abs(centered_point[2]) - half_length;
2360
2361 if (radius_diff > 0 && h_diff > 0)
2362 return std::pow(radius_diff * radius_diff + h_diff * h_diff, 0.5) -
2363 this->layer_thickening;
2364 else if (radius_diff <= 0 && h_diff > 0)
2365 return h_diff - this->layer_thickening;
2366 else if (radius_diff > 0 && h_diff <= 0)
2367 return radius_diff - this->layer_thickening;
2368
2369 return std::max(radius_diff, h_diff) - this->layer_thickening;
2370}
2371
2372template <int dim>
2373std::shared_ptr<Shape<dim>>
2375{
2376 std::shared_ptr<Shape<dim>> copy = std::make_shared<Cylinder<dim>>(
2377 this->radius, this->half_length, this->position, this->orientation);
2378 return copy;
2379}
2380
2381template <int dim>
2382double
2384{
2385 using numbers::PI;
2386 double solid_volume = PI * radius * radius * half_length * 2;
2387
2388 return solid_volume;
2389}
2390
2391template <int dim>
2392double
2393CylindricalTube<dim>::value(const Point<dim> &evaluation_point,
2394 const unsigned int /*component*/) const
2395{
2396 Point<dim> centered_point = this->align_and_center(evaluation_point);
2397
2398
2399 // external cylinder
2400 double level_set_of_cylinder_hollow = 0;
2401 double p_radius = std::pow(centered_point[0] * centered_point[0] +
2402 centered_point[1] * centered_point[1],
2403 0.5);
2404 double radius_diff_o = p_radius - (radius + rectangular_base / 2);
2405 double radius_diff_i = p_radius - (radius - rectangular_base / 2);
2406 double h_diff_o = abs(centered_point[2] - height / 2) - height / 2;
2407
2408 if (radius_diff_o > 0 && h_diff_o > 0)
2409 level_set_of_cylinder_hollow =
2410 std::pow(radius_diff_o * radius_diff_o + h_diff_o * h_diff_o, 0.5);
2411 else if (radius_diff_o > 0 && h_diff_o <= 0)
2412 level_set_of_cylinder_hollow = radius_diff_o;
2413 else if (radius_diff_o <= 0 && radius_diff_i > 0 && h_diff_o > 0)
2414 level_set_of_cylinder_hollow = h_diff_o;
2415 else if (radius_diff_i <= 0 && h_diff_o > 0)
2416 level_set_of_cylinder_hollow =
2417 std::pow(radius_diff_i * radius_diff_i + h_diff_o * h_diff_o, 0.5);
2418 else if (radius_diff_i <= 0 && h_diff_o <= 0)
2419 level_set_of_cylinder_hollow = -radius_diff_i;
2420 else
2421 level_set_of_cylinder_hollow =
2422 std::max({radius_diff_o, h_diff_o, -radius_diff_i});
2423
2424 return level_set_of_cylinder_hollow - this->layer_thickening;
2425}
2426
2427template <int dim>
2428std::shared_ptr<Shape<dim>>
2430{
2431 std::shared_ptr<Shape<dim>> copy =
2432 std::make_shared<CylindricalTube<dim>>(this->radius + rectangular_base / 2,
2433 this->radius - rectangular_base / 2,
2434 this->height,
2435 this->position,
2436 this->orientation);
2437 return copy;
2438}
2439
2440template <int dim>
2441double
2443{
2444 using numbers::PI;
2445 double solid_volume = height * PI *
2446 ((this->radius + rectangular_base / 2) *
2447 (this->radius + rectangular_base / 2) -
2448 (this->radius - rectangular_base / 2) *
2449 (this->radius - rectangular_base / 2));
2450
2451 return solid_volume;
2452}
2453
2454
2455template <int dim>
2456double
2457CylindricalHelix<dim>::value(const Point<dim> &evaluation_point,
2458 const unsigned int /*component*/) const
2459{
2460 Point<dim> centered_point = this->align_and_center(evaluation_point);
2461
2462 // The evaluation proposed in this function is exact close to the helix but
2463 // far away above or bellow the helix some error may happen since the wrong
2464 // helix loop may be evaluated.
2465
2466 // Distance to the center of the helix
2467 // First we find a good initial guess for the non linear resolution.
2468 double phase = std::atan2(centered_point[1], centered_point[0]);
2469 if (std::isnan(phase))
2470 phase = 0;
2471 if (phase < 0)
2472 phase = phase + 2 * numbers::PI;
2473
2474 // Some calculation to find a good initial guess
2475 double nb_turns = height / pitch;
2476 double phase_at_top = (nb_turns - std::floor(nb_turns)) * 2 * numbers::PI;
2477 double x = centered_point[2] - phase * pitch / (2 * numbers::PI);
2478
2479 // Initialize the parametric variable t for the helix
2480 double t = 0;
2481 double t_initial_guess = 0;
2482
2483 // We select two initial guess for which we do the full computation. Both
2484 // point have the same phase as the point we are evaluating one is above on
2485 // the helix the other bellow. We keep the closest one.
2486 t_initial_guess = std::floor(x / pitch) * 2 * numbers::PI + phase;
2487
2488 // Solve the non-linear equation to find the point on the helix with the
2489 // first guess
2490 double level_set_tube = 0;
2491 t = t_initial_guess;
2492 double residual = -(radius * cos(t) - centered_point[0]) * sin(t) * radius +
2493 (radius * sin(t) - centered_point[1]) * cos(t) * radius +
2494 (pitch / (2 * numbers::PI) * t - centered_point[2]) *
2495 pitch / (2 * numbers::PI);
2496
2497 unsigned int i = 0;
2498
2499 // Newton iterations.
2500 while (abs(residual) > 1e-8 * this->effective_radius && i < 20)
2501 {
2502 residual = -(radius * cos(t) - centered_point[0]) * sin(t) * radius +
2503 (radius * sin(t) - centered_point[1]) * cos(t) * radius +
2504 (pitch / (2 * numbers::PI) * t - centered_point[2]) * pitch /
2505 (2 * numbers::PI);
2506 double dr_dt = centered_point[0] * cos(t) * radius +
2507 centered_point[1] * sin(t) * radius +
2508 (pitch / (2 * numbers::PI)) * (pitch / (2 * numbers::PI));
2509 t = t - residual / dr_dt;
2510 i += 1;
2511 }
2512 // Cap the value of the parametric variable t and find the point.
2513 if (t > nb_turns * numbers::PI * 2)
2514 {
2515 t = nb_turns * numbers::PI * 2;
2516 Point<dim> point_on_helix;
2517 point_on_helix[0] = radius * cos(t);
2518 point_on_helix[1] = radius * sin(t);
2519 point_on_helix[2] = t * pitch / (2 * numbers::PI);
2520 level_set_tube = (centered_point - point_on_helix).norm();
2521 }
2522 else if (t < 0)
2523 {
2524 t = 0;
2525 Point<dim> point_on_helix;
2526 point_on_helix[0] = radius * cos(t);
2527 point_on_helix[1] = radius * sin(t);
2528 point_on_helix[2] = t * pitch / (2 * numbers::PI);
2529
2530 level_set_tube = (centered_point - point_on_helix).norm();
2531 }
2532 else
2533 {
2534 Point<dim> point_on_helix;
2535 point_on_helix[0] = radius * cos(t);
2536 point_on_helix[1] = radius * sin(t);
2537 point_on_helix[2] = t * pitch / (2 * numbers::PI);
2538 level_set_tube = (centered_point - point_on_helix).norm() - radius_disk;
2539 }
2540
2541 // repeat for second guess
2542 double level_set_tube_2 = 0;
2543 t = t_initial_guess + 2 * numbers::PI;
2544 residual = -(radius * cos(t) - centered_point[0]) * sin(t) * radius +
2545 (radius * sin(t) - centered_point[1]) * cos(t) * radius +
2546 (pitch / (2 * numbers::PI) * t - centered_point[2]) * pitch /
2547 (2 * numbers::PI);
2548
2549 i = 0;
2550 while (abs(residual) > 1e-8 * this->effective_radius && i < 20)
2551 {
2552 residual = -(radius * cos(t) - centered_point[0]) * sin(t) * radius +
2553 (radius * sin(t) - centered_point[1]) * cos(t) * radius +
2554 (pitch / (2 * numbers::PI) * t - centered_point[2]) * pitch /
2555 (2 * numbers::PI);
2556 double dr_dt = centered_point[0] * cos(t) * radius +
2557 centered_point[1] * sin(t) * radius +
2558 (pitch / (2 * numbers::PI)) * (pitch / (2 * numbers::PI));
2559 t = t - residual / dr_dt;
2560 i += 1;
2561 }
2562
2563 if (t > nb_turns * numbers::PI * 2)
2564 {
2565 t = nb_turns * numbers::PI * 2;
2566 Point<dim> point_on_helix;
2567 point_on_helix[0] = radius * cos(t);
2568 point_on_helix[1] = radius * sin(t);
2569 point_on_helix[2] = t * pitch / (2 * numbers::PI);
2570 level_set_tube_2 = (centered_point - point_on_helix).norm();
2571 }
2572 else if (t < 0)
2573 {
2574 t = 0;
2575 Point<dim> point_on_helix;
2576 point_on_helix[0] = radius * cos(t);
2577 point_on_helix[1] = radius * sin(t);
2578 point_on_helix[2] = t * pitch / (2 * numbers::PI);
2579
2580 level_set_tube_2 = (centered_point - point_on_helix).norm();
2581 }
2582 else
2583 {
2584 Point<dim> point_on_helix;
2585 point_on_helix[0] = radius * cos(t);
2586 point_on_helix[1] = radius * sin(t);
2587 point_on_helix[2] = t * pitch / (2 * numbers::PI);
2588 level_set_tube_2 = (centered_point - point_on_helix).norm() - radius_disk;
2589 }
2590
2591 // Keep the best guess
2592 level_set_tube = std::min(level_set_tube, level_set_tube_2);
2593
2594 // Cap the helix with a plane at each end. Cap at the base.
2595 Point<dim> point_at_base;
2596 Tensor<1, dim> vector_at_base;
2597 point_at_base[0] = radius;
2598 point_at_base[1] = 0;
2599 point_at_base[2] = 0;
2600 vector_at_base[0] = 0;
2601 vector_at_base[1] = -radius;
2602 vector_at_base[2] = -pitch / (2 * numbers::PI);
2603
2604 double p_radius_cap_base =
2605 ((centered_point - point_at_base) -
2606 scalar_product((centered_point - point_at_base), vector_at_base) /
2607 vector_at_base.norm_square() * vector_at_base)
2608 .norm();
2609 double radial_distance_cap_base = p_radius_cap_base - radius_disk;
2610 double h_dist_from_cap_0 =
2611 (scalar_product((centered_point - point_at_base), vector_at_base) /
2612 vector_at_base.norm_square() * vector_at_base)
2613 .norm();
2614
2615 double dist_from_cap = 0;
2616 if (radial_distance_cap_base > 0)
2617 dist_from_cap =
2618 std::pow(h_dist_from_cap_0 * h_dist_from_cap_0 +
2619 radial_distance_cap_base * radial_distance_cap_base,
2620 0.5);
2621 else
2622 dist_from_cap = h_dist_from_cap_0;
2623
2624 // Cap the helix with a plane at each end. The cap at the top.
2625 Point<dim> point_at_top;
2626 Tensor<1, dim> vector_at_top;
2627 point_at_top[0] = std::cos(phase_at_top) * radius;
2628 point_at_top[1] = std::sin(phase_at_top) * radius;
2629 point_at_top[2] = height;
2630 vector_at_top[0] = -point_at_top[1];
2631 vector_at_top[1] = point_at_top[0];
2632 vector_at_top[2] = pitch / (2 * numbers::PI);
2633
2634 double p_radius_cap_top =
2635 ((centered_point - point_at_top) -
2636 scalar_product((centered_point - point_at_top), vector_at_top) /
2637 vector_at_top.norm_square() * vector_at_top)
2638 .norm();
2639 double radial_distance_cap_top = p_radius_cap_top - radius_disk;
2640 double h_dist_from_cap_top =
2641 (scalar_product((centered_point - point_at_top), vector_at_top) /
2642 vector_at_top.norm_square() * vector_at_top)
2643 .norm();
2644
2645 double dist_from_cap_top = 0;
2646 if (radial_distance_cap_top > 0)
2647 dist_from_cap_top =
2648 std::pow(h_dist_from_cap_top * h_dist_from_cap_top +
2649 radial_distance_cap_top * radial_distance_cap_top,
2650 0.5);
2651 else
2652 dist_from_cap_top = h_dist_from_cap_top;
2653
2654 // Do the union of the helix and the cap.
2655 double level_set = 0;
2656 if (level_set_tube > 0)
2657 level_set = std::min({level_set_tube, dist_from_cap, dist_from_cap_top});
2658 else
2659 level_set = std::max({level_set_tube, -dist_from_cap_top, -dist_from_cap});
2660
2661 return level_set - this->layer_thickening;
2662}
2663
2664template <int dim>
2665std::shared_ptr<Shape<dim>>
2667{
2668 std::shared_ptr<Shape<dim>> copy =
2669 std::make_shared<CylindricalHelix<dim>>(this->radius,
2670 this->radius_disk,
2671 this->height,
2672 this->pitch,
2673 this->position,
2674 this->orientation);
2675 return copy;
2676}
2677
2678template <int dim>
2679double
2681{
2682 using numbers::PI;
2683 constexpr double solid_volume = 0;
2684
2685 return solid_volume;
2686}
2687
2688template class Sphere<2>;
2689template class Sphere<3>;
2690template class HyperRectangle<2>;
2691template class HyperRectangle<3>;
2692template class Superquadric<3>;
2693template class Ellipsoid<2>;
2694template class Ellipsoid<3>;
2695template class Torus<3>;
2696template class Cone<3>;
2697template class Cylinder<3>;
2698template class CylindricalTube<3>;
2699template class CylindricalHelix<3>;
2700template class CutHollowSphere<3>;
2701template class DeathStar<3>;
2702template class CompositeShape<2>;
2703template class CompositeShape<3>;
2704template class RBFShape<2>;
2705template class RBFShape<3>;
2706template class OpenCascadeShape<2>;
2707template class OpenCascadeShape<3>;
2708template class Plane<2>;
2709template class Plane<3>;
This class was designed so that specific composite shapes could be defined through the parameter file...
Definition shape.h:2105
void update_precalculations(DoFHandler< dim > &updated_dof_handler, const bool mesh_based_precalculations)
Sets the proper dof handler, then computes/updates the map of cells and their likely non-null nodes.
Definition shape.cc:1469
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1460
void remove_superfluous_data(DoFHandler< dim > &updated_dof_handler, const bool mesh_based_precalculations)
Remove data that affects only artificial cells (not locally owned and not ghost). The data is removed...
Definition shape.cc:1501
double value_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1314
Tensor< 1, dim > gradient(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the gradient of the distance function.
Definition shape.cc:1357
std::map< unsigned int, std::tuple< BooleanOperation, unsigned int, unsigned int > > operations
Definition shape.h:2401
Tensor< 1, dim > gradient_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the gradient of the distance function.
Definition shape.cc:1406
std::map< unsigned int, std::shared_ptr< Shape< dim > > > constituents
Definition shape.h:2394
void set_layer_thickening(const double layer_thickening) override
See base.
Definition shape.cc:1606
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1277
std::pair< double, Tensor< 1, dim > > apply_boolean_operations(std::map< unsigned int, double > constituent_shapes_values, std::map< unsigned int, Tensor< 1, dim > > constituent_shapes_gradients) const
Computes the assigned boolean operations.
Definition shape.cc:1518
void load_data_from_file()
Load data from file. To be called at initialization, after repartitioning or when shape has moved....
Definition shape.cc:1488
virtual void clear_cache() override
Clear the cache of the shape.
Definition shape.cc:1596
CompositeShape(std::map< unsigned int, std::shared_ptr< Shape< dim > > > constituents, std::map< unsigned int, std::tuple< BooleanOperation, unsigned int, unsigned int > > operations, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs an assembly of shapes into a composite shape.
Definition shape.h:2121
Definition shape.h:1896
Tensor< 1, 2 > intermediate_q
Definition shape.h:1955
double tan_base_angle
Definition shape.h:1951
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:1174
double height
Definition shape.h:1952
double base_radius
Definition shape.h:1953
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1135
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1165
double shell_thickness
Definition shape.h:2020
double cut_depth
Definition shape.h:2019
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:1219
double radius
Definition shape.h:2018
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1206
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1182
double intermediate_w
Definition shape.h:2022
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:2383
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:2349
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:2374
double half_length
Definition shape.h:3254
double radius
Definition shape.h:3253
double radius_disk
Definition shape.h:3387
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:2457
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:2666
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:2680
double radius
Definition shape.h:3384
double height
Definition shape.h:3385
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:2393
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:2442
double radius
Definition shape.h:3316
double rectangular_base
Definition shape.h:3318
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:2429
double height
Definition shape.h:3317
double intermediate_b
Definition shape.h:2094
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1256
double intermediate_a
Definition shape.h:2093
double radius
Definition shape.h:2089
double hole_radius
Definition shape.h:2090
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1230
double spheres_distance
Definition shape.h:2091
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:1269
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1060
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:1091
Tensor< 1, dim > radii
Definition shape.h:1831
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1080
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1012
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:1048
Tensor< 1, dim > half_lengths
Definition shape.h:1779
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1037
double value_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:753
Tensor< 1, dim > gradient(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the gradient of the distance function.
Definition shape.cc:847
std::string local_file_name
Definition shape.h:2587
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:836
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:997
Tensor< 1, dim > gradient_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the gradient of the distance function.
Definition shape.cc:903
void set_position(const Point< dim > &position) override
Sets a new position for the shape.
Definition shape.cc:1004
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:698
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:456
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:440
Tensor< 1, dim > normal
Definition shape.h:1558
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:482
Tensor< 1, dim > gradient(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the analytical gradient of the distance.
Definition shape.cc:465
RBF Shapes express the signed distance function as a linear combination of Radial Basis Functions (RB...
Definition shape.h:2624
static double c2c0_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C2 continuity at distan...
Definition shape.h:3059
static double gauss95(const double distance)
Non-compact Gaussian function with 0.05 value at distance equal to 1.
Definition shape.h:2845
void remove_superfluous_data(DoFHandler< dim > &dof_handler, const bool mesh_based_precalculations)
Remove data that affects only artificial cells (not locally owned and not ghost). The data is removed...
Definition shape.cc:2148
static double c1c0(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C1 continuity at distance=0,...
Definition shape.h:2869
static double cosinus_derivative(const double distance)
Derivative of a compact cosinusoidal basis function. It preserves continuity at every point.
Definition shape.h:3148
std::vector< double > basis_functions
Definition shape.h:3196
static double c0c1(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C0 continuity at distance=0,...
Definition shape.h:2893
std::vector< std::tuple< Point< dim >, double, std::shared_ptr< std::vector< size_t > > > > iterable_nodes
Definition shape.h:3170
RBFShape(const std::string &shape_arguments_str, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
An RBFShape represents a physical object by describing its signed distance field with a linear combin...
Definition shape.cc:1614
static double linear(const double distance)
Compact linear function defined from 0 to 1.
Definition shape.h:2824
RBFBasisFunction
Enum class defining types of RBF kernel functions that could be used in the class.
Definition shape.h:2633
static double c0c1_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C0 continuity at distan...
Definition shape.h:3071
void determine_likely_nodes_for_one_cell(const typename DoFHandler< dim >::cell_iterator &cell, const Point< dim > support_point)
Establishes which nodes bring a non null contribution to the RBF.
Definition shape.cc:2035
static double c2c2_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C2 continuity at distan...
Definition shape.h:3135
static double c1c1_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C1 continuity at distan...
Definition shape.h:3083
double evaluate_basis_function(const RBFBasisFunction basis_function, const double distance) const
Returns the value of the basis function for a given distance. Inspired by Optimad Bitpit....
Definition shape.cc:2257
void rotate_nodes()
Rotate RBF nodes in the global reference frame (the reference frame of the triangulation).
Definition shape.cc:2244
void load_data_from_file()
Load RBF data from file. To be called at initialization, after repartitioning or when shape has moved...
Definition shape.cc:2100
static double gauss99(const double distance)
Non-compact Gaussian function with 0.01 value at distance equal to 1.
Definition shape.h:2856
static double c1c0_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C1 continuity at distan...
Definition shape.h:3047
Tensor< 1, dim > gradient_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the analytical gradient of the distance.
Definition shape.cc:1665
static double gauss90_derivative(const double distance)
Derivative of a non-compact Gaussian function with 0.1 value at distance equal to 1.
Definition shape.h:3009
double value_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1635
static double wendlandc2(const double distance)
Compact Wendland C2 function defined from 0 to 1.
Definition shape.h:2812
static double c0c2_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C0 continuity at distan...
Definition shape.h:3108
size_t max_number_of_inside_nodes
Definition shape.h:3186
static double c1c2(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C1 continuity at distance=0,...
Definition shape.h:2949
static double gauss99_derivative(const double distance)
Derivative of a non-compact Gaussian function with 0.01 value at distance equal to 1.
Definition shape.h:3033
std::vector< double > support_radii
Definition shape.h:3195
void swap_iterable_nodes(const typename DoFHandler< dim >::active_cell_iterator &cell)
Swap the vector of all nodes with a likely node vector.
Definition shape.cc:2338
static double c2c2(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C2 continuity at distance=0,...
Definition shape.h:2964
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1809
static double wendlandc2_derivative(const double distance)
Derivative of a compact Wendland C2 function defined from 0 to 1.
Definition shape.h:2988
std::map< const typename DoFHandler< dim >::cell_iterator, std::shared_ptr< std::vector< std::tuple< Point< dim >, double, std::shared_ptr< std::vector< size_t > > > > > > likely_nodes_map
Definition shape.h:3185
void initialize_bounding_box()
Initializes the bounding box around the nodes which enables distance calculation even if the RBF node...
Definition shape.cc:1827
static double c2c1(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C2 continuity at distance=0,...
Definition shape.h:2920
DoFHandler< dim > * dof_handler
Definition shape.h:3187
double evaluate_basis_function_derivative(const RBFBasisFunction basis_function, const double distance) const
Returns the derivative of the basis function for a given distance. Inspired by Optimad Bitpit....
Definition shape.cc:2297
std::string filename
Definition shape.h:3162
std::vector< double > weights
Definition shape.h:3192
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1697
double maximal_support_radius
Definition shape.h:3189
static double cos(const double distance)
Compact cosinusoidal basis function. It is null when r>1.
Definition shape.h:2977
static double c2c0(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C2 continuity at distance=0,...
Definition shape.h:2881
std::vector< Point< dim > > rotated_nodes_positions
Definition shape.h:3194
Tensor< 1, dim > gradient(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the analytical gradient of the distance for the current RBF.
Definition shape.cc:1736
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:1820
static double c1c1(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C1 continuity at distance=0,...
Definition shape.h:2906
std::vector< Point< dim > > nodes_positions
Definition shape.h:3193
static double c0c2(const double distance)
Compact polynomial function defined from 0 to 1. It preserves C0 continuity at distance=0,...
Definition shape.h:2934
size_t number_of_nodes
Definition shape.h:3163
void update_precalculations(DoFHandler< dim > &updated_dof_handler, const bool mesh_based_precalculations)
Sets the proper dof handler, then computes/updates the map of cells and their likely non-null nodes.
Definition shape.cc:1860
std::vector< bool > useful_rbf_nodes
Definition shape.h:3197
static double c1c2_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C1 continuity at distan...
Definition shape.h:3120
std::shared_ptr< HyperRectangle< dim > > bounding_box
Definition shape.h:3164
static double gauss95_derivative(const double distance)
Derivative of a non-compact Gaussian function with 0.05 value at distance equal to 1.
Definition shape.h:3021
static double c2c1_derivative(const double distance)
Derivative of a compact polynomial function defined from 0 to 1. It preserves C2 continuity at distan...
Definition shape.h:3095
static double gauss90(const double distance)
Non-compact Gaussian function with 0.1 value at distance equal to 1.
Definition shape.h:2834
static double linear_derivative(const double distance)
Derivative of a compact linear function defined from 0 to 1.
Definition shape.h:2999
virtual Point< dim > get_position()
Returns the position of the shape.
Definition shape.h:1187
Tensor< 2, 3 > rotation_matrix
Definition shape.h:1362
Tensor< 1, 3 > orientation
Definition shape.h:1351
double effective_radius
Definition shape.h:1333
virtual void clear_cache()
Clear the cache of the shape.
Definition shape.cc:31
virtual Tensor< 1, dim > gradient_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0)
Return the analytical gradient of the distance.
Definition shape.cc:272
virtual void closest_surface_point(const Point< dim > &p, Point< dim > &closest_point, const typename DoFHandler< dim >::active_cell_iterator &cell_guess)
Sets the closest_point parameter to be the point on the surface of the shape which has the minimal di...
Definition shape.cc:224
virtual double displaced_volume()
Return the volume displaced by the solid.
Definition shape.cc:24
std::string point_to_string(const Point< dim > &evaluation_point) const
This function returns a point in a string of text. This is used in the cache of the shape.
Definition shape.cc:207
std::unordered_map< std::string, Tensor< 1, dim > > gradient_cache
Definition shape.h:1356
double layer_thickening
Definition shape.h:1364
std::unordered_map< std::string, double > value_cache
Definition shape.h:1355
virtual std::shared_ptr< Manifold< dim - 1, dim > > get_shape_manifold()
Returns the default manifold of the shape. If not redefined, it is a flat manifold.
Definition shape.cc:200
Point< dim > bounding_box_center
Definition shape.h:1343
virtual void set_position(const Point< dim > &new_position)
Sets a new position for the shape.
Definition shape.h:1140
Point< dim > reverse_align_and_center(const Point< dim > &evaluation_point) const
This function applies the inverse operation of align_and_center.
Definition shape.cc:122
bool part_of_a_composite
Definition shape.h:1358
std::unordered_map< std::string, Point< dim > > closest_point_cache
Definition shape.h:1357
Tensor< 1, dim > bounding_box_half_length
Definition shape.h:1341
virtual void set_layer_thickening(const double the_layer_thickening)
Sets the layer thickening value (positive or negative) of the particle's shape.
Definition shape.h:1309
Point< dim > position
Definition shape.h:1348
virtual double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override=0
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Point< dim > align_and_center(const Point< dim > &evaluation_point) const
Most value functions assume that the particle's position is at the origin and that the shape is align...
Definition shape.cc:44
virtual double value_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0)
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:262
Shape(double radius, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
A general constructor for the Shapes.
Definition shape.h:86
virtual double local_curvature_radius_with_cell_guess(const Point< dim > &p, const typename DoFHandler< dim >::active_cell_iterator &cell) override
Return the curvature of the level set at a given point using the.
Definition shape.cc:429
virtual std::shared_ptr< Manifold< dim - 1, dim > > get_shape_manifold() override
Return the manifold of the sphere as a spherical manifold center at the position of the shape.
Definition shape.cc:317
Tensor< 1, dim > gradient(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the analytical gradient of the distance.
Definition shape.cc:302
Sphere(double radius, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructor for a sphere.
Definition shape.h:1378
void set_position(const Point< dim > &position) override
Sets a new position for the shape.
Definition shape.cc:339
std::shared_ptr< Functions::SignedDistance::Sphere< dim > > sphere_function
Definition shape.h:1488
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:282
std::tuple< double, Tensor< 1, dim >, Point< dim > > distance_to_shape(Shape< dim > &shape, std::vector< Point< dim > > &candidate_points, double precision=1e-6, bool exact_distance_outside_of_contact=false) override
Return the distance, the center point, and the normal between the current shape and the shape given i...
Definition shape.cc:387
virtual double local_curvature_radius(Point< dim > p) override
Return the curvature of the level set at a given point.
Definition shape.cc:422
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:291
std::tuple< double, Tensor< 1, dim >, Point< dim > > distance_to_shape_with_cell_guess(Shape< dim > &shape, const typename DoFHandler< dim >::active_cell_iterator &cell, std::vector< Point< dim > > &candidate_points, double precision=1e-6, bool exact_distance_outside_of_contact=false) override
Return the distance, the center point, and the normal between the current shape and the shape given i...
Definition shape.cc:348
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:324
This class defines superquadric shapes. Their signed distance function is: .
Definition shape.h:1570
Point< dim > superquadric_gradient(const Point< dim > &centered_point) const
Computes the gradient of the superquadric from its equation.
Definition shape.h:1703
double epsilon
Definition shape.h:1727
double value_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:592
Tensor< 1, dim > gradient_with_cell_guess(const Point< dim > &evaluation_point, const typename DoFHandler< dim >::active_cell_iterator &cell, const unsigned int component=0) override
Return the gradient of the distance function.
Definition shape.cc:666
void closest_surface_point(const Point< dim > &p, Point< dim > &closest_point, const typename DoFHandler< dim >::active_cell_iterator &cell_guess) override
Sets the closest_point parameter to be the point on the surface of the shape which has the minimal di...
Definition shape.cc:490
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:564
Tensor< 1, dim > gradient(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the analytical gradient of the distance.
Definition shape.cc:638
Tensor< 1, dim > exponents
Definition shape.h:1726
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:625
double superquadric(const Point< dim > &centered_point) const
Computes the value of the superquadric from its equation.
Definition shape.h:1691
Tensor< 1, dim > half_lengths
Definition shape.h:1725
double ring_thickness
Definition shape.h:1891
double ring_radius
Definition shape.h:1890
std::shared_ptr< Shape< dim > > static_copy() const override
Return a pointer to a copy of the Shape.
Definition shape.cc:1118
double value(const Point< dim > &evaluation_point, const unsigned int component=0) const override
Return the evaluation of the signed distance function of this solid at the given point evaluation poi...
Definition shape.cc:1104
double displaced_volume() override
Return the volume displaced by the solid.
Definition shape.cc:1127
void fill_vectors_from_file(std::map< std::string, std::vector< double > > &map, const std::string &file_name, const std::string &delimiter=" ")
function that read a file that was build from a dealii table and fill 2 vectors. The first vector con...
Definition utilities.cc:299
@ phase
Phase indicator scalar field from CLS.
Tensor< 1, 3 > tensor_nd_to_3d(const Tensor< 1, dim > &tensor)
Tensor< 1, 2 > tensor_nd_to_2d(const Tensor< 1, dim > &tensor)