Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
shape.h
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#ifndef lethe_shape_h
5#define lethe_shape_h
6
8#include <core/utilities.h>
9
10#include <deal.II/base/auto_derivative_function.h>
11#include <deal.II/base/function.h>
12
13#include <deal.II/grid/manifold.h>
14#include <deal.II/grid/manifold_lib.h>
15
16#include <boost/range/adaptor/map.hpp>
17
18#ifdef DEAL_II_WITH_OPENCASCADE
19# include <deal.II/opencascade/manifold_lib.h>
20# include <deal.II/opencascade/utilities.h>
21
22# include <BRepBuilderAPI_MakeVertex.hxx>
23# include <BRepClass3d_SolidClassifier.hxx>
24# include <BRepExtrema_DistShapeShape.hxx>
25# include <BRepGProp.hxx>
26# include <GProp_GProps.hxx>
27#endif
28
29#include <deal.II/base/function_signed_distance.h>
30
31#include <deal.II/dofs/dof_handler.h>
32#include <deal.II/dofs/dof_tools.h>
33
34#include <deal.II/physics/transformations.h>
35
36#include <cfloat>
37#include <memory>
38#include <utility>
39
40using namespace dealii;
41
42/**
43 * @brief A base class used to represent geometrical entities. Its main uses
44 * are to return signed distance and its gradient. It inherits
45 * AutoDerivativeFunction so that it can evaluate gradients even when they are
46 * not implemented analytically.
47 *
48 * @tparam dim An integer that denotes the dimension of the space in which
49 * the flow is solved
50 */
51template <int dim>
52class Shape : public AutoDerivativeFunction<dim>
53{
54public:
55 /**
56 * @brief enum class that associate an integer index tp each type of shape.
57 * The enumerator names are the values of the "type" parameter of the
58 * particles.
59 */
78
79 /**
80 * @brief A general constructor for the Shapes
81 *
82 * @param radius The effective radius to be set for the shape. It's necessary for some calculations since not all shapes are spheres.
83 * @param position The position to set the shape at
84 * @param orientation The orientation to set the shape at
85 */
86 Shape(double radius,
87 const Point<dim> &position,
88 const Tensor<1, 3> &orientation)
89
90 : AutoDerivativeFunction<dim>(1e-8)
91 , effective_radius(radius)
94 , part_of_a_composite(false)
96 {}
97
98 /**
99 * @brief Return the evaluation of the signed distance function of this solid
100 * at the given point evaluation point
101 * Most level set functions implemented come from Inigo Quilez:
102 * iquilezles.org/articles/distfunctions
103 *
104 * @param evaluation_point The point at which the function will be evaluated
105 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
106 */
107 virtual double
108 value(const Point<dim> &evaluation_point,
109 const unsigned int component = 0) const override = 0;
110
111 /**
112 * @brief Return the evaluation of the signed distance function of this solid
113 * at the given point evaluation point with a guess for the cell containing
114 * the evaluation point
115 * @param evaluation_point The point at which the function will be evaluated
116 * @param cell The cell that is likely to contain the evaluation point
117 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
118 */
119 virtual double
121 const Point<dim> &evaluation_point,
122 const typename DoFHandler<dim>::active_cell_iterator &cell,
123 const unsigned int component = 0);
124
125 /**
126 * @brief Return the smoothed maximum of two variables used for shape contact calculation.
127 * @param a first variable
128 * @param b second variable
129 * @param smooth_factor
130 */
131 static double
132 smooth_max(const double a, const double b, const double smooth_factor = 10)
133 {
134 return (a * std::exp(a * smooth_factor) + b * std::exp(b * smooth_factor)) /
135 ((std::exp(a * smooth_factor) + std::exp(b * smooth_factor)));
136 }
137
138 /**
139 * @brief Return the distance, the center point, and the unit normal vector between the current shape and the shape given in the argument. The center point is the point where both shapes are at the same distance from each other. The unit normal vector is defined using the closest surface point of the two shapes. By default, the function does not calculate the distance between the two shapes if their bounding boxes are not in contact. This behavior can be modified by setting exact_distance_outside_of_contact to true.
140 * @param shape The shape with which the distance is evaluated
141 * @param cell The cell that is likely to contain the evaluation point
142 * @param candidate_points This is the initial guess points used in the calculation.
143 * @param precision This is the precision of the distance between the two shapes.
144 * @param exact_distance_outside_of_contact This is a boolean to force the exact distance evaluation if the shapes are not in contact.
145 */
146 virtual std::tuple<double, Tensor<1, dim>, Point<dim>>
148 Shape<dim> &shape,
149 const typename DoFHandler<dim>::active_cell_iterator &cell,
150 std::vector<Point<dim>> &candidate_points,
151 double precision = 1e-6,
152 bool exact_distance_outside_of_contact = false)
153 {
154 // Initialized the default values.
155 double distance = DBL_MAX;
156 Tensor<1, dim> normal;
157 Point<dim> contact_point;
158 bool bounding_box_contact = true;
159
160 // Check the bounding boxes. First we check the spherical bounding boxes
161 // then the rectangular boxes.
162 if (exact_distance_outside_of_contact == false)
163 {
164 Point<dim> bounding_box_center_one;
165 Point<dim> bounding_box_center_two;
166 double radius_one = this->bounding_box_half_length.norm();
167 double radius_two = shape.bounding_box_half_length.norm();
168 // In the case of the plane, the radius of the bounding box is zero, so
169 // we replace it with the radius of the other object.
170 if (radius_one == 0)
171 {
172 radius_one = radius_two;
173 }
174 if (radius_two == 0)
175 {
176 radius_two = radius_one;
177 }
178 if constexpr (dim == 3)
179 {
180 bounding_box_center_one =
181 this->position +
183 bounding_box_center_two =
184 shape.position +
186 }
187 else
188 {
189 bounding_box_center_one =
190 this->position +
193 bounding_box_center_two =
194 shape.position +
197 }
198 if (((bounding_box_center_one - bounding_box_center_two).norm() -
199 radius_one - radius_two) <= 0)
200 {
202 }
203 else
204 {
205 bounding_box_contact = false;
206 }
207 }
208
209 // The following algorithm does a minimization of the level set obtained by
210 // the intersection of two shapes. A usual gradient descent does not work as
211 // such. This algorithm uses a cartesian search combined with an additional
212 // guess based on the gradient of the level set and a numerical gradient
213 // descent.
215 {
216 std::vector<Tensor<1, dim>> search_direction(2 * dim);
217 // Define the cartesian direction search directions.
218 search_direction[0][0] = 1.;
219 search_direction[1][0] = -1.;
220 search_direction[2][1] = 1.;
221 search_direction[3][1] = -1.;
222 if constexpr (dim == 3)
223 {
224 search_direction[4][2] = 1.;
225 search_direction[5][2] = -1.;
226 }
227 // Loop over all the initial candidate points.
228 for (unsigned int i = 0; i < candidate_points.size(); ++i)
229 {
230 // Initialize variables used in the calculation.
231 Point<dim> current_point = candidate_points[i];
232 Point<dim> dx{}, distance_gradient{}, previous_position{};
233 // Initialize the iteration counter.
234 unsigned int iteration = 0;
235 constexpr unsigned int iteration_max = 2e2;
236
237 // The initial step size is set to 25% of the effective radius of
238 // the shape. Tests showed that this initial step size is generally
239 // adequate.
240 double max_step = shape.effective_radius * 0.25;
241 double previous_step_size = max_step;
242
243 // Initialize the value. In this minimization of the intersection of
244 // two level set we use the smooth max function for the union as it
245 // significantly smooths the minimization problem as we get close to
246 // the local minimum of the intersection.
247 double value_first_component =
248 this->value_with_cell_guess(current_point, cell);
249 double value_second_component =
250 shape.value_with_cell_guess(current_point, cell);
251 double current_distance =
252 smooth_max(value_first_component, value_second_component);
253
254 previous_position = current_point;
255 // The initial previous value and position
256 double previous_value = DBL_MAX;
257 unsigned int consecutive_center = 0;
258
259 // Iterate to find the minimum of the intersection.
260 while (iteration < iteration_max && previous_step_size > precision)
261 {
262 // Check the local gradient direction.
263 value_first_component =
264 this->value_with_cell_guess(current_point, cell);
265 value_second_component =
266 shape.value_with_cell_guess(current_point, cell);
267 if (value_first_component > value_second_component)
268 {
269 distance_gradient =
270 this->gradient_with_cell_guess(current_point, cell);
271 }
272 else
273 {
274 distance_gradient =
275 shape.gradient_with_cell_guess(current_point, cell);
276 }
277 Tensor<1, dim> direction = distance_gradient;
278 dx = -max_step * direction / direction.norm();
279 if (distance_gradient.norm() == 0)
280 {
281 dx = -max_step * direction;
282 }
283 // Check the first candidate point in the direction of the
284 // gradient.
285 Point<dim> new_point = current_point + dx;
286 value_first_component =
287 this->value_with_cell_guess(new_point, cell);
288 value_second_component =
289 shape.value_with_cell_guess(new_point, cell);
290 double new_distance =
291 smooth_max(value_first_component, value_second_component);
292 // Check if the guess is better than the previous guess. If it
293 // is not better, we do the cartesian directional search.
294 if (new_distance > current_distance - precision * precision)
295 {
296 // Initialize the container for the value.
297 std::vector<double> diff_results;
298 diff_results.resize(search_direction.size());
299 Point<dim> best_point;
300 double best_dist = current_distance;
301 for (unsigned int d = 0; d < search_direction.size(); ++d)
302 {
303 Tensor<1, dim> perturbation =
304 search_direction[d] * max_step;
305 value_first_component = this->value_with_cell_guess(
306 current_point + perturbation, cell);
307 value_second_component = shape.value_with_cell_guess(
308 current_point + perturbation, cell);
309 new_distance = smooth_max(value_first_component,
310 value_second_component);
311 diff_results[d] =
312 (new_distance - current_distance) / max_step;
313 // Check if the Cartesian search is better than the last
314 // candidate.
315 if (new_distance < best_dist - precision * precision)
316 {
317 best_dist = new_distance;
318 best_point = current_point + perturbation;
319 }
320 }
321 // Store the current results.
322 if (best_dist < current_distance - precision * precision)
323 {
324 current_distance = best_dist;
325 current_point = best_point;
326 }
327
328 // If none of the cartesian candidates are better, we use
329 // the value to find the point that minimizes the numerical
330 // gradient we evaluated with the cartesian guess.
331 if (current_distance >
332 previous_value - precision * precision)
333 {
334 // Define the guess point.
335 new_point[0] =
336 previous_position[0] -
337 ((diff_results[0] - diff_results[1]) / 2) /
338 ((diff_results[0] + diff_results[1]) / max_step);
339 new_point[1] =
340 previous_position[1] -
341 ((diff_results[2] - diff_results[3]) / 2) /
342 ((diff_results[2] + diff_results[3]) / max_step);
343 if constexpr (dim == 3)
344 {
345 new_point[2] =
346 previous_position[2] -
347 ((diff_results[4] - diff_results[5]) / 2) /
348 ((diff_results[4] + diff_results[5]) /
349 max_step);
350 }
351 value_first_component =
352 this->value_with_cell_guess(new_point, cell);
353 value_second_component =
354 shape.value_with_cell_guess(new_point, cell);
355 new_distance = smooth_max(value_first_component,
356 value_second_component);
357 // Check if it is better.
358 if (new_distance <
359 previous_value - precision * precision)
360 {
361 // If it is, we reduce the step size to the size of
362 // the step made by this guess.
363 max_step = (new_point - previous_position).norm();
364 current_distance = new_distance;
365 current_point = new_point;
366 }
367 }
368 // If the guess is not better, we reduce the search size and
369 // store the value. If one of the points is better, we reset
370 // the consecutive_center counter. This variable helps to
371 // converge faster when one of the guesses is very close to
372 // the optimal point.
373 if (current_distance >
374 previous_value - precision * precision)
375 {
376 consecutive_center += 1;
377 previous_step_size = max_step;
378 max_step *= 1.0 / std::pow(2.0, consecutive_center);
379 current_point = previous_position;
380 current_distance = previous_value;
381 }
382 else
383 {
384 max_step *= 1;
385 consecutive_center = 0;
386 }
387 }
388 else
389 {
390 current_point = new_point;
391 current_distance = new_distance;
392 max_step *= 1;
393 consecutive_center = 0;
394 }
395
396 // Store the previous values.
397 previous_value = current_distance;
398 previous_position = current_point;
399 iteration++;
400 }
401 // Store the minimum obtained with this initial guess and compare it
402 // with the results for other initial points.
403 if (distance > current_distance)
404 {
405 distance = current_distance;
406 contact_point = current_point;
407 // The normal is defined using the closest surface point on the
408 // two shapes.
409 Point<dim> closest_surface_point_on_this_shape;
410 Point<dim> closest_surface_point_on_the_input_shape;
411 this->closest_surface_point(current_point,
412 closest_surface_point_on_this_shape,
413 cell);
415 current_point,
416 closest_surface_point_on_the_input_shape,
417 cell);
418 if (distance > 0)
419 {
420 normal = (closest_surface_point_on_the_input_shape -
421 closest_surface_point_on_this_shape) /
422 ((closest_surface_point_on_the_input_shape -
423 closest_surface_point_on_this_shape)
424 .norm() +
425 DBL_MIN);
426 }
427 else
428 {
429 normal = (closest_surface_point_on_this_shape -
430 closest_surface_point_on_the_input_shape) /
431 ((closest_surface_point_on_this_shape -
432 closest_surface_point_on_the_input_shape)
433 .norm() +
434 DBL_MIN);
435 }
436 }
437 }
438 }
439 return std::make_tuple(distance, normal, contact_point);
440 }
441
442 /**
443 * @brief Return the distance, the center point, and the normal between the current shape and the shape given in the argument. The center point is the point where both shapes are at the same distance from each other. The normal is defined using the closest surface point on the two shapes. By default, the function does not calculate the distance between the two shapes if their bounding boxes are not in contact. This behavior can be modified using the appropriate parameter.
444 * @param shape The shape with which the distance is evaluated
445 * @param candidate_points This is the initial guess points used in the calculation.
446 * @param precision This is the precision of the distance between the two shapes.
447 * @param exact_distance_outside_of_contact This is a boolean to force the exact distance evaluation if the shapes are not in contact.
448 */
449 virtual std::tuple<double, Tensor<1, dim>, Point<dim>>
451 std::vector<Point<dim>> &candidate_points,
452 double precision = 1e-6,
453 bool exact_distance_outside_of_contact = false)
454 {
455 // Initialized the default values.
456 double distance = DBL_MAX;
457 Tensor<1, dim> normal;
458 Point<dim> contact_point;
459 bool bounding_box_contact = true;
460
461 // Check the bounding boxes. First we check the spherical bounding boxes
462 // then the rectangular boxes.
463 if (exact_distance_outside_of_contact == false)
464 {
465 Point<dim> bounding_box_center_one;
466 Point<dim> bounding_box_center_two;
467 double radius_one = this->bounding_box_half_length.norm();
468 double radius_two = shape.bounding_box_half_length.norm();
469 // In the case of the plane, the radius of the bounding box is zero, so
470 // we replace it with the radius of the other object.
471 if (radius_one == 0)
472 {
473 radius_one = radius_two;
474 }
475 if (radius_two == 0)
476 {
477 radius_two = radius_one;
478 }
479 if constexpr (dim == 3)
480 {
481 bounding_box_center_one =
482 this->position +
484 bounding_box_center_two =
485 shape.position +
487 }
488 else
489 {
490 bounding_box_center_one =
491 this->position +
494 bounding_box_center_two =
495 shape.position +
498 }
499 if (((bounding_box_center_one - bounding_box_center_two).norm() -
500 radius_one - radius_two) <= 0)
501 {
503 }
504 else
505 {
506 bounding_box_contact = false;
507 }
508 }
509
510 // The following algorithm does a minimization of the level_set obtained by
511 // the intersection of two shapes. A usual gradient descent does not work as
512 // such. This algorithm uses a cartesian search combined with an additional
513 // guess based on the gradient of the level set and a numerical gradient
514 // descent.
516 {
517 std::vector<Tensor<1, dim>> search_direction(2 * dim);
518 // Define the cartesian direction search directions.
519 search_direction[0][0] = 1.;
520 search_direction[1][0] = -1.;
521 search_direction[2][1] = 1.;
522 search_direction[3][1] = -1.;
523 if constexpr (dim == 3)
524 {
525 search_direction[4][2] = 1.;
526 search_direction[5][2] = -1.;
527 }
528 // Loop over all the initial candidate points.
529 for (unsigned int i = 0; i < candidate_points.size(); ++i)
530 {
531 // Initialize variable used in the calculation.
532 Point<dim> current_point = candidate_points[i];
533 Point<dim> dx{}, distance_gradient{}, previous_position{};
534 // Initialize the iteration counter. We limit the number of
535 // iterations to 200.
536 unsigned int iteration = 0;
537 constexpr unsigned int iteration_max = 2e2;
538
539 // The initial step size is set to 25% of the effective radius of
540 // the shape. Tests showed that this initial step size is generally
541 // adequate.
542 double max_step = shape.effective_radius * 0.25;
543 double previous_step_size = max_step;
544
545 // Initialize the value. In this minimisation of the intersection of
546 // two level set we use the smooth max function for the union as it
547 // significantly smooths the minimization problem as we get close to
548 // the local minimum of the intersection.
549 double value_first_component = this->value(current_point);
550 double value_second_component = shape.value(current_point);
551 double current_distance =
552 smooth_max(value_first_component, value_second_component);
553
554 previous_position = current_point;
555 // The initial previous value and position
556 double previous_value = DBL_MAX;
557 unsigned int consecutive_center = 0;
558
559 // Iterate to find the minimum of the intersection.
560 while (iteration < iteration_max &&
561 (previous_step_size) > precision)
562 {
563 // Check the local gradient direction.
564 value_first_component = this->value(current_point);
565 value_second_component = shape.value(current_point);
566 if (value_first_component > value_second_component)
567 {
568 distance_gradient = this->gradient(current_point);
569 }
570 else
571 {
572 distance_gradient = shape.gradient(current_point);
573 }
574 Tensor<1, dim> direction = distance_gradient;
575 dx = -max_step * direction / direction.norm();
576 if (distance_gradient.norm() == 0)
577 {
578 dx = -max_step * direction;
579 }
580 // Check the first candidate point in the direction of the
581 // gradient.
582 Point<dim> new_point = current_point + dx;
583 value_first_component = this->value(new_point);
584 value_second_component = shape.value(new_point);
585 double new_distance =
586 smooth_max(value_first_component, value_second_component);
587 // Check if the guess is better than the previous guess. If it
588 // is not better, we do the cartesian directional search.
589 if (new_distance > current_distance - precision * precision)
590 {
591 // Initialize the container for the value.
592 std::vector<double> diff_results;
593 diff_results.resize(search_direction.size());
594 Point<dim> best_point;
595 double best_dist = current_distance;
596 for (unsigned int d = 0; d < search_direction.size(); ++d)
597 {
598 Tensor<1, dim> perturbation =
599 search_direction[d] * max_step;
600 value_first_component =
601 this->value(current_point + perturbation);
602 value_second_component =
603 shape.value(current_point + perturbation);
604 new_distance = smooth_max(value_first_component,
605 value_second_component);
606 diff_results[d] =
607 (new_distance - current_distance) / max_step;
608 // Check if the Cartesian search is better than the last
609 // candidate.
610 if (new_distance < best_dist - precision * precision)
611 {
612 best_dist = new_distance;
613 best_point = current_point + perturbation;
614 }
615 }
616 // Store the current results.
617 if (best_dist < current_distance - precision * precision)
618 {
619 current_distance = best_dist;
620 current_point = best_point;
621 }
622
623 // If none of the cartesian candidates are better, we use
624 // the value to find the point that minimizes the numerical
625 // gradient we evaluated with the cartesian guess.
626 if (current_distance >
627 previous_value - precision * precision)
628 {
629 // Define the guess point.
630 new_point[0] =
631 previous_position[0] -
632 ((diff_results[0] - diff_results[1]) / 2) /
633 ((diff_results[0] + diff_results[1]) / max_step);
634 new_point[1] =
635 previous_position[1] -
636 ((diff_results[2] - diff_results[3]) / 2) /
637 ((diff_results[2] + diff_results[3]) / max_step);
638 if constexpr (dim == 3)
639 {
640 new_point[2] =
641 previous_position[2] -
642 ((diff_results[4] - diff_results[5]) / 2) /
643 ((diff_results[4] + diff_results[5]) /
644 max_step);
645 }
646 value_first_component = this->value(new_point);
647 value_second_component = shape.value(new_point);
648 new_distance = smooth_max(value_first_component,
649 value_second_component);
650 // Check if it is better.
651 if (new_distance <
652 previous_value - precision * precision)
653 {
654 // If it is, we reduce the step size to the size of
655 // the step made by this guess.
656 max_step = (new_point - previous_position).norm();
657 current_distance = new_distance;
658 current_point = new_point;
659 }
660 }
661 // If the guess is not better, we reduce the search size and
662 // store the value. If one of the points is better, we reset
663 // the consecutive_center counter. This variable helps to
664 // converge faster when one of the guesses is very close to
665 // the optimal point.
666 if (current_distance >
667 previous_value - precision * precision)
668 {
669 consecutive_center += 1;
670 previous_step_size = max_step;
671 max_step *= 1.0 / std::pow(2.0, consecutive_center);
672 current_point = previous_position;
673 current_distance = previous_value;
674 }
675 else
676 {
677 max_step *= 1;
678 consecutive_center = 0;
679 }
680 }
681 else
682 {
683 current_point = new_point;
684 current_distance = new_distance;
685 max_step *= 1;
686 consecutive_center = 0;
687 }
688
689 // Store the previous values.
690 previous_value = current_distance;
691 previous_position = current_point;
692 iteration++;
693 }
694 // Store the minimum obtained with this initial guess and compare it
695 // with the results for other initial points.
696 if (distance > current_distance)
697 {
698 distance = current_distance;
699 contact_point = current_point;
700 Point<dim> closest_surface_point_on_this_shape;
701 Point<dim> closest_surface_point_on_the_input_shape;
702 // The normal is defined using the closest surface point on the
703 // two shapes.
705 current_point, closest_surface_point_on_this_shape);
707 current_point, closest_surface_point_on_the_input_shape);
708 if (distance > 0)
709 {
710 normal = (closest_surface_point_on_the_input_shape -
711 closest_surface_point_on_this_shape) /
712 ((closest_surface_point_on_the_input_shape -
713 closest_surface_point_on_this_shape)
714 .norm() +
715 DBL_MIN);
716 }
717 else
718 {
719 normal = (closest_surface_point_on_this_shape -
720 closest_surface_point_on_the_input_shape) /
721 ((closest_surface_point_on_this_shape -
722 closest_surface_point_on_the_input_shape)
723 .norm() +
724 DBL_MIN);
725 }
726 }
727 }
728 }
729 return std::make_tuple(distance, normal, contact_point);
730 }
731
732
733 /**
734 * @brief Check if the bounding box of this shape and another one are overlapping.
735 * This function uses the Separating Axis Theorem (SAT) to perform the
736 * bounding box check.
737 * @param shape The shape with which the distance is evaluated
738 * @return This function returns false if the shapes are not overlapping.
739 */
740 bool
742 {
743 if constexpr (dim == 3)
744 {
745 // First, if one of the bounding boxes is a plane, then the size of the
746 // bounding box is set to zero. As such, we test the other box vertices
747 // directly.
748 if (this->bounding_box_half_length.norm() == 0)
749 {
750 // loop over the vertices;
751 for (int i = -1; i < 2; i += 2)
752 {
753 for (int j = -1; j < 2; j += 2)
754 {
755 for (int k = -1; k < 2; k += 2)
756 {
757 // create the vertex to test and check the value
758 Tensor<1, dim> vertex_position(
759 {(shape.bounding_box_half_length[0] +
760 shape.layer_thickening) *
761 i,
762 (shape.bounding_box_half_length[1] +
763 shape.layer_thickening) *
764 j,
765 (shape.bounding_box_half_length[2] +
766 shape.layer_thickening) *
767 k});
768 Point<3> vertex =
769 point_nd_to_3d(shape.position) +
770 shape.rotation_matrix *
772 vertex_position);
773 if (this->value(vertex) <= 0)
774 {
775 return true;
776 }
777 }
778 }
779 }
780 return false;
781 }
782 else if (shape.bounding_box_half_length.norm() == 0)
783 {
784 // loop over the vertices;
785 for (int i = -1; i < 2; i += 2)
786 {
787 for (int j = -1; j < 2; j += 2)
788 {
789 for (int k = -1; k < 2; k += 2)
790 {
791 // create the vertex to test and check the value
792 Tensor<1, dim> vertex_position(
793 {(this->bounding_box_half_length[0] +
794 this->layer_thickening) *
795 i,
796 (this->bounding_box_half_length[1] +
797 this->layer_thickening) *
798 j,
799 (this->bounding_box_half_length[2] +
800 this->layer_thickening) *
801 k});
802 Point<3> vertex =
803 point_nd_to_3d(this->position) +
804 this->rotation_matrix *
806 vertex_position);
807 if (shape.value(vertex) <= 0)
808 {
809 return true;
810 }
811 }
812 }
813 }
814 return false;
815 }
816
817 double ra, rb;
818 Tensor<2, dim> R, AbsR;
819 // Compute rotation matrix expressing each box in the other's coordinate
820 // frame
821 for (int i = 0; i < 3; i++)
822 {
823 for (int j = 0; j < 3; j++)
824 {
825 R[i][j] = scalar_product(this->rotation_matrix[i],
826 shape.rotation_matrix[j]);
827 }
828 }
829 // Compute translation vector t in this shape frame
830 Tensor<1, dim> t =
831 shape.position + shape.rotation_matrix * shape.bounding_box_center -
832 (this->position + this->rotation_matrix * this->bounding_box_center);
833 t = this->rotation_matrix * t;
834 // Compute common subexpressions. Add in an epsilon term to counteract
835 // arithmetic errors
836 for (int i = 0; i < 3; i++)
837 {
838 for (int j = 0; j < 3; j++)
839 {
840 AbsR[i][j] =
841 std::abs(R[i][j]) + std::numeric_limits<double>::epsilon();
842 }
843 }
844 // Test axes L = A0, L = A1, L = A2
845 for (int i = 0; i < 3; i++)
846 {
847 ra = (this->bounding_box_half_length[i] + this->layer_thickening);
848 rb = (shape.bounding_box_half_length[0] + shape.layer_thickening) *
849 AbsR[i][0] +
850 (shape.bounding_box_half_length[1] + shape.layer_thickening) *
851 AbsR[i][1] +
852 (shape.bounding_box_half_length[2] + shape.layer_thickening) *
853 AbsR[i][2];
854 if (std::abs(t[i]) > ra + rb)
855 return false;
856 }
857 // Test axes L = B0, L = B1, L = B2
858 for (int i = 0; i < 3; i++)
859 {
860 ra = (this->bounding_box_half_length[0] + this->layer_thickening) *
861 AbsR[0][i] +
862 (this->bounding_box_half_length[1] + this->layer_thickening) *
863 AbsR[1][i] +
864 (this->bounding_box_half_length[2] + this->layer_thickening) *
865 AbsR[2][i];
866 rb = shape.bounding_box_half_length[i] + shape.layer_thickening;
867 if (std::abs(t[0] * R[0][i] + t[1] * R[1][i] + t[2] * R[2][i]) >
868 ra + rb)
869 return false;
870 }
871 // Check the axis obtained from the cross product of all the bases axis.
872 Tensor<1, dim> L;
873 for (int i = 0; i < 3; i++)
874 {
875 for (int j = 0; j < 3; j++)
876 {
877 L = cross_product_3d(this->rotation_matrix[i],
878 shape.rotation_matrix[j]);
879 ra = (this->bounding_box_half_length[(i + 1) % 3] +
880 this->layer_thickening) *
881 AbsR[(i + 2) % 3][j] +
882 (this->bounding_box_half_length[(i + 2) % 3] +
883 this->layer_thickening) *
884 AbsR[(i + 1) % 3][j];
885 rb = (shape.bounding_box_half_length[(j + 1) % 3] +
886 shape.layer_thickening) *
887 AbsR[i][(j + 2) % 3] +
888 (shape.bounding_box_half_length[(j + 2) % 3] +
889 shape.layer_thickening) *
890 AbsR[i][(j + 1) % 3];
891 if (std::abs(scalar_product(t, L)) > ra + rb)
892 return false;
893 }
894 }
895 // No separating axis found, the boxes must be intersecting
896 return true;
897 }
898 else
899 {
900 // First, if one of the bounding boxes is a plane, then the size of the
901 // bounding box is zero. As such, we test the other box vertices
902 // directly.
903 if (this->bounding_box_half_length.norm() == 0)
904 {
905 // loop over the vertices;
906 for (int i = -1; i < 2; i += 2)
907 {
908 for (int j = -1; j < 2; j += 2)
909 {
910 // create the vertex to test and check the value
911 Tensor<1, dim> vertex_position(
912 {(shape.bounding_box_half_length[0] +
913 shape.layer_thickening) *
914 i,
915 (shape.bounding_box_half_length[1] +
916 shape.layer_thickening) *
917 j});
918 Point<3> vertex =
919 point_nd_to_3d(shape.position) +
920 shape.rotation_matrix *
922 vertex_position);
923 if (this->value(point_nd_to_2d(vertex)) <= 0)
924 {
925 return false;
926 }
927 }
928 }
929 return true;
930 }
931 else if (shape.bounding_box_half_length.norm() == 0)
932 {
933 // loop over the vertices;
934 for (int i = -1; i < 2; i += 2)
935 {
936 for (int j = -1; j < 2; j += 2)
937 {
938 // create the vertex to test and check the value
939 Tensor<1, 3> vertex_position(
940 {(this->bounding_box_half_length[0] +
941 this->layer_thickening) *
942 i,
943 (this->bounding_box_half_length[1] +
944 this->layer_thickening) *
945 j,
946 0});
947 Point<3> vertex =
948 point_nd_to_3d(this->position) +
949 this->rotation_matrix *
951 vertex_position);
952 if (shape.value(point_nd_to_2d(vertex)) <= 0)
953 {
954 return false;
955 }
956 }
957 }
958 return true;
959 }
960 double ra, rb;
961 Tensor<2, 3> R, AbsR;
962
963 // Compute rotation matrix expressing each box in the other's coordinate
964 // frame
965 for (int i = 0; i < 3; i++)
966 {
967 for (int j = 0; j < 3; j++)
968 {
969 R[i][j] = scalar_product(this->rotation_matrix[i],
970 shape.rotation_matrix[j]);
971 }
972 }
973
974 // Compute translation vector t in this shape frame
975 Tensor<1, 3> t =
976 point_nd_to_3d(shape.position) +
978 (point_nd_to_3d(this->position) +
980 t = this->rotation_matrix * t;
981
982 // Compute common subexpressions. Add in an epsilon term to counteract
983 // arithmetic errors
984 for (int i = 0; i < 3; i++)
985 {
986 for (int j = 0; j < 3; j++)
987 {
988 AbsR[i][j] =
989 std::abs(R[i][j]) + std::numeric_limits<double>::epsilon();
990 }
991 }
992
993 // Test axes L = A0, L = A1, L = A2
994 for (int i = 0; i < 2; i++)
995 {
996 ra = this->bounding_box_half_length[i] + this->layer_thickening;
997 rb = (shape.bounding_box_half_length[0] + shape.layer_thickening) *
998 AbsR[i][0] +
999 (shape.bounding_box_half_length[1] + shape.layer_thickening) *
1000 AbsR[i][1];
1001 if (std::abs(t[i]) > ra + rb)
1002 return false;
1003 }
1004
1005 // Test axes L = B0, L = B1, L = B2
1006 for (int i = 0; i < 2; i++)
1007 {
1008 ra = (this->bounding_box_half_length[0] + this->layer_thickening) *
1009 AbsR[0][i] +
1010 (this->bounding_box_half_length[1] + this->layer_thickening) *
1011 AbsR[1][i];
1012 rb = shape.bounding_box_half_length[i] + shape.layer_thickening;
1013 if (std::abs(t[0] * R[0][i] + t[1] * R[1][i]) > ra + rb)
1014 return false;
1015 }
1016
1017 // No separating axis found, the boxes must be intersecting
1018 return true;
1019 }
1020 }
1021
1022 /**
1023 * @brief Return the analytical gradient of the distance
1024 * @param evaluation_point The point at which the function will be evaluated
1025 * @param cell The cell that is likely to contain the evaluation point
1026 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1027 */
1028 virtual Tensor<1, dim>
1030 const Point<dim> &evaluation_point,
1031 const typename DoFHandler<dim>::active_cell_iterator &cell,
1032 const unsigned int component = 0);
1033
1034
1035 /**
1036 * @brief Return a pointer to a copy of the Shape
1037 */
1038 virtual std::shared_ptr<Shape<dim>>
1039 static_copy() const = 0;
1040
1041
1042 /**
1043 * @brief
1044 * Sets the closest_point parameter to be the point on the surface of the
1045 * shape which has the minimal distance from the given point p
1046 *
1047 * @param p The point at which the evaluation is performed
1048 * @param closest_point The reference to the closest point. This point will be modified by the function.
1049 * @param cell_guess A guess of the cell containing the evaluation point, which
1050 * is useful to reduce computation time
1051 */
1052 virtual void
1054 const Point<dim> &p,
1055 Point<dim> &closest_point,
1056 const typename DoFHandler<dim>::active_cell_iterator &cell_guess);
1057 virtual void
1058 closest_surface_point(const Point<dim> &p, Point<dim> &closest_point) const;
1059
1060 /**
1061 * @brief
1062 * Return the volume displaced by the solid.
1063 *
1064 * @return The displaced volume, or 0 for shapes whose volume is not
1065 * computed. The caller is responsible to handle this second case (0)
1066 * and estimate the volume differently.
1067 */
1068 virtual double
1070
1071
1072 /**
1073 * @brief
1074 * Return the curvature of the level set at a given point.
1075 */
1076 virtual double
1078 {
1079 double curvature = 0;
1080 double epsilone = 1e-6 * this->effective_radius;
1081 for (int d = 0; d < dim; ++d)
1082 {
1083 Tensor<1, dim> perturbation;
1084 perturbation[d] = epsilone;
1085 Tensor<1, dim> gradient_p = this->gradient(p + perturbation);
1086 gradient_p = gradient_p / (gradient_p.norm() + DBL_MIN);
1087 Tensor<1, dim> gradient_m = this->gradient(p - perturbation);
1088 gradient_m = gradient_m / (gradient_m.norm() + DBL_MIN);
1089 curvature += (gradient_p - gradient_m)[d] / (2 * epsilone);
1090 }
1091 // Here we bound the curvature radius obtained
1092 double radius_of_curvature = 1.0 / abs(curvature);
1093 return std::min(std::max(radius_of_curvature,
1094 this->effective_radius * 1e-2),
1095 this->effective_radius * 1e2);
1096 }
1097 /**
1098 * @brief
1099 * Return the curvature of the level at a given point using the cell guess
1100 * functions.
1101 * @param cell The cell that is likely to contain the evaluation point
1102 * @param p The point where the curvature is evaluated.
1103 */
1104 virtual double
1106 const Point<dim> &p,
1107 const typename DoFHandler<dim>::active_cell_iterator &cell)
1108 {
1109 double curvature = 0;
1110 double epsilone = 1e-6 * this->effective_radius;
1111 // Calculate the curvature using the divergence of the normal and define the
1112 // local curvature radius as the absolute value of the inverse of the
1113 // curvature.
1114 for (int d = 0; d < dim; ++d)
1115 {
1116 Tensor<1, dim> perturbation;
1117 perturbation[d] = epsilone;
1118 Tensor<1, dim> gradient_p =
1119 this->gradient_with_cell_guess(p + perturbation, cell);
1120 gradient_p = gradient_p / (gradient_p.norm() + DBL_MIN);
1121 Tensor<1, dim> gradient_m =
1122 this->gradient_with_cell_guess(p - perturbation, cell);
1123 gradient_m = gradient_m / (gradient_m.norm() + DBL_MIN);
1124 curvature += (gradient_p - gradient_m)[d] / (2 * epsilone);
1125 }
1126 // Here we bound the curvature radius obtained
1127 double radius_of_curvature = 1.0 / abs(curvature);
1128 return std::min(std::max(radius_of_curvature,
1129 this->effective_radius * 1e-2),
1130 this->effective_radius * 1e2);
1131 }
1132
1133 /**
1134 * @brief
1135 * Sets a new position for the shape
1136 *
1137 * @param new_position The new position the shape will be placed at
1138 */
1139 inline virtual void
1140 set_position(const Point<dim> &new_position)
1141 {
1142 clear_cache();
1143 position = new_position;
1144 }
1145
1146 /**
1147 * @brief
1148 * Sets a new orientation for the shape
1149 *
1150 * @param new_orientation The new orientation the shape will be set at
1151 */
1152 inline virtual void
1153 set_orientation(const Tensor<1, 3> &new_orientation)
1154 {
1155 clear_cache();
1156 this->rotation_matrix = 0;
1157 this->rotation_matrix[0][0] = 1.0;
1158 this->rotation_matrix[1][1] = 1.0;
1159 this->rotation_matrix[2][2] = 1.0;
1160 if constexpr (dim == 2)
1161 {
1162 Tensor<1, 3> axis;
1163 axis[2] = 1.0;
1164 this->rotation_matrix =
1165 Physics::Transformations::Rotations::rotation_matrix_3d(
1166 axis, new_orientation[2]);
1167 }
1168 if constexpr (dim == 3)
1169 {
1170 for (unsigned int i = 0; i < 3; ++i)
1171 {
1172 Tensor<1, 3> axis;
1173 axis[2 - i] = 1.0;
1174 this->rotation_matrix =
1175 Physics::Transformations::Rotations::rotation_matrix_3d(
1176 axis, new_orientation[2 - i]) *
1177 this->rotation_matrix;
1178 }
1179 }
1180 orientation = new_orientation;
1181 }
1182 /**
1183 * @brief Returns the position of the shape.
1184 *
1185 */
1186 inline virtual Point<dim>
1188 {
1189 return position;
1190 }
1191
1192 /**
1193 * @brief Returns the orientation of the shape
1194 *
1195 */
1196 inline virtual Tensor<1, 3>
1198 {
1199 return orientation;
1200 }
1201
1202 /**
1203 * @brief Returns the orientation of the shape
1204 *
1205 */
1206 inline virtual Tensor<2, 3>
1208 {
1209 return rotation_matrix;
1210 }
1211
1212 /**
1213 * @brief Returns the bounding box half length
1214 *
1215 */
1216 inline virtual Tensor<1, dim>
1221
1222 /**
1223 * @brief Returns the bounding box position
1224 *
1225 */
1226 inline virtual Point<dim>
1228 {
1229 return bounding_box_center;
1230 }
1231
1232 /**
1233 * @brief Returns the default manifold of the shape. If not redefined, it is a flat
1234 * manifold.
1235 */
1236 virtual std::shared_ptr<Manifold<dim - 1, dim>>
1238
1239
1240 /**
1241 * @brief Clear the cache of the shape
1242 *
1243 */
1244 virtual void
1245 clear_cache();
1246
1247 /**
1248 * @brief
1249 * Most value functions assume that the particle's position is at the origin
1250 * and that the shape is aligned with one of the main axes. This function
1251 * returns a point that is rotated and translated, in accordance with the
1252 * current shape position and orientation, so that subsequent calculations
1253 * for the value function are made more easily; it abstracts a step that is
1254 * required in the value function for most shapes.
1255 *
1256 * Returns the centered and aligned point used on the level set evaluation.
1257 *
1258 * @param evaluation_point The point that will be recentered and realigned
1259 * @return The aligned and centered point
1260 */
1261 Point<dim>
1262 align_and_center(const Point<dim> &evaluation_point) const;
1263
1264 /**
1265 * @brief
1266 * This function applies the inverse operation of align_and_center
1267 *
1268 * Returns the centered and aligned point used on the level set evaluation in
1269 * the global reference frame.
1270 *
1271 * @param evaluation_point The point that will be centered and aligned in the global reference frame.
1272 * @return The aligned and centered point
1273 */
1274 Point<dim>
1275 reverse_align_and_center(const Point<dim> &evaluation_point) const;
1276
1277 /**
1278 * @brief
1279 * This function returns a point in a string of text. This is used in the
1280 * cache of the shape.
1281 *
1282 * @param evaluation_point is the point that is transformed to its text form.
1283 */
1284 std::string
1285 point_to_string(const Point<dim> &evaluation_point) const;
1286
1287 /**
1288 * @brief Defines if this shape is part of a composite.
1289 * If true, cache management is deactivated and delegated to the upper level
1290 * shape, to avoid cache duplication.
1291 * @param is_part_of_a_composite is true if this shape is a constituent of a composite shape
1292 */
1293 void
1294 set_part_of_a_composite(const bool is_part_of_a_composite)
1295 {
1296 this->part_of_a_composite = is_part_of_a_composite;
1297 }
1298
1299 /**
1300 * @brief
1301 * Sets the layer thickening value (positive or negative) of the particle's
1302 * shape
1303 *
1304 * @param the_layer_thickening Thickness to be artificially added to the particle.
1305 * A negative value will decrease the particle's thickness by subtracting a
1306 * layer of specified width.
1307 */
1308 virtual void
1309 set_layer_thickening(const double the_layer_thickening)
1310 {
1311 this->layer_thickening = the_layer_thickening;
1312 }
1313
1314 /**
1315 * @brief
1316 * Function that return the XYZ rotation angles from a rotation matrix
1317 *
1318 * @param rotation_matrix_representation The rotation matrix.
1319 */
1320 static Tensor<1, 3>
1321 rotation_matrix_to_xyz_angles(Tensor<2, 3> &rotation_matrix_representation)
1322 {
1323 Tensor<1, 3> xyz_rotation;
1324 xyz_rotation[0] = std::atan2(-rotation_matrix_representation[1][2],
1325 rotation_matrix_representation[2][2]);
1326 xyz_rotation[1] = std::asin(rotation_matrix_representation[0][2]);
1327 xyz_rotation[2] = std::atan2(-rotation_matrix_representation[0][1],
1328 rotation_matrix_representation[0][0]);
1329 return xyz_rotation;
1330 };
1331
1332 // Effective radius used for crown refinement
1334
1335 // The string contains additional information on the shape. This may refer
1336 // to the file type used to define the shape or any other information
1337 // relative to how the shape was defined.
1339
1340 // The half lengths of bounding box of the shape
1342 // The center of the bounding box of the shape in the shape reference frame
1344
1345protected:
1346 // Position of the center of the Shape. It doesn't always correspond to the
1347 // center of mass
1348 Point<dim> position;
1349 // The solid orientation, which is defined as the sequential rotation around
1350 // the axes x->y->z by each of the tensor components, in radian
1351 Tensor<1, 3> orientation;
1352
1353 // The cache of the evaluation of the shape. This is used to avoid costly
1354 // reevaluation of the shape.
1355 std::unordered_map<std::string, double> value_cache;
1356 std::unordered_map<std::string, Tensor<1, dim>> gradient_cache;
1357 std::unordered_map<std::string, Point<dim>> closest_point_cache;
1359
1360
1361 // The rotation matrix that describes the solid orientation.
1362 Tensor<2, 3> rotation_matrix;
1363 // Layer thickening: used to artificially inflate/deflate the shape
1365};
1366
1367
1368template <int dim>
1369class Sphere : public Shape<dim>
1370{
1371public:
1372 /**
1373 * @brief Constructor for a sphere
1374 * @param radius The sphere radius
1375 * @param position The sphere center
1376 * @param orientation The sphere orientation
1377 */
1378 Sphere(double radius,
1379 const Point<dim> &position,
1380 const Tensor<1, 3> &orientation)
1381 : Shape<dim>(radius, position, orientation)
1382 {
1384 std::make_shared<Functions::SignedDistance::Sphere<dim>>(position,
1385 radius);
1386 for (int d = 0; d < dim; ++d)
1387 {
1388 this->bounding_box_half_length[d] = radius;
1389 this->bounding_box_center[d] = 0;
1390 }
1391 }
1392
1393 /**
1394 * @brief Return the evaluation of the signed distance function of this solid
1395 * at the given point evaluation point.
1396 *
1397 * @param evaluation_point The point at which the function will be evaluated
1398 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1399 */
1400 double
1401 value(const Point<dim> &evaluation_point,
1402 const unsigned int component = 0) const override;
1403
1404
1405 /**
1406 * @brief Return a pointer to a copy of the Shape
1407 */
1408 std::shared_ptr<Shape<dim>>
1409 static_copy() const override;
1410
1411 /**
1412 * @brief Return the analytical gradient of the distance
1413 * @param evaluation_point The point at which the function will be evaluated
1414 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1415 */
1416 Tensor<1, dim>
1417 gradient(const Point<dim> &evaluation_point,
1418 const unsigned int component = 0) const override;
1419
1420 /**
1421 * @brief
1422 * Return the volume displaced by the solid.
1423 */
1424 double
1425 displaced_volume() override;
1426
1427 void
1428 set_position(const Point<dim> &position) override;
1429
1430 /**
1431 * @brief Return the manifold of the sphere as a spherical manifold center at the position of the shape.
1432 */
1433 virtual std::shared_ptr<Manifold<dim - 1, dim>>
1434 get_shape_manifold() override;
1435
1436
1437 /**
1438 * @brief Return the distance, the center point, and the normal between the current shape and the shape given in the argument. The center point is the point that minimized the level set obtained from the intersection of the two shapes. The normal is defined using the closest surface point on the two shapes. This function is an optimized version of the general shape distance calculation for the case of a sphere.
1439 * @param shape The shape with which the distance is evaluated
1440 * @param cell The cell that is likely to contain the evaluation point
1441 * @param candidate_points This is the initial guess points used in the calculation. (In this implementation this parameter is void)
1442 * @param precision This is the precision of the distance between the two shapes. (In this implementation this parameter is void)
1443 * @param exact_distance_outside_of_contact This is a boolean to force the exact distance evaluation if the shapes are not in contact. (In this implementation this parameter is void)
1444 */
1445 std::tuple<double, Tensor<1, dim>, Point<dim>>
1447 Shape<dim> &shape,
1448 const typename DoFHandler<dim>::active_cell_iterator &cell,
1449 std::vector<Point<dim>> &candidate_points,
1450 double precision = 1e-6,
1451 bool exact_distance_outside_of_contact = false) override;
1452
1453 /**
1454 * @brief Return the distance, the center point, and the normal between the current shape and the shape given in the argument. The center point is the point that minimized the level set obtained from the intersection of the two shapes. The normal is defined using the closest surface point on the two shapes. This function is an optimized version of the general shape distance calculation for the case of a sphere.
1455 * @param shape The shape with which the distance is evaluated
1456 * @param candidate_points This is the initial guess points used in the calculation. (In this implementation, this parameter is void)
1457 * @param precision This is the precision of the distance between the two shapes. (In this implementation, this parameter is void)
1458 * @param exact_distance_outside_of_contact This is a boolean to force the exact distance evaluation if the shapes are not in contact. (In this implementation, this parameter is void)
1459 */
1460 std::tuple<double, Tensor<1, dim>, Point<dim>>
1462 std::vector<Point<dim>> &candidate_points,
1463 double precision = 1e-6,
1464 bool exact_distance_outside_of_contact = false) override;
1465
1466 /**
1467 * @brief
1468 * Return the curvature of the level set at a given point
1469 *
1470 * @param p The point where the curvature is evaluated.
1471 */
1472 virtual double
1473 local_curvature_radius(Point<dim> p) override;
1474
1475 /**
1476 * @brief
1477 * Return the curvature of the level set at a given point using the
1478 * @param cell The cell that is likely to contain the evaluation point
1479 * @param p The point where the curvature is evaluated.
1480 */
1481 virtual double
1483 const Point<dim> &p,
1484 const typename DoFHandler<dim>::active_cell_iterator &cell) override;
1485
1486
1487private:
1488 std::shared_ptr<Functions::SignedDistance::Sphere<dim>> sphere_function;
1489};
1490
1491
1492template <int dim>
1493class Plane : public Shape<dim>
1494{
1495public:
1496 /**
1497 * @brief Constructor for a infinite plane. The plane is normal to the Z axis in 3D and Y axis in 2D in the reference frame of the particle.
1498 * @param position The point on the plane.
1499 * @param orientation The orientation.
1500 */
1501 Plane(const Point<dim> &position, const Tensor<1, 3> &orientation)
1502 : Shape<dim>(1.0, position, orientation)
1503 {
1504 if constexpr (dim == 3)
1505 {
1506 normal = Tensor<1, dim>({0, 0, 1});
1507 }
1508 else
1509 {
1510 normal = Tensor<1, dim>({0, 1});
1511 }
1512 // This is a special case since the plane has no bounding box so here we
1513 // fill it with zeros.
1514 for (int d = 0; d < dim; ++d)
1515 {
1516 this->bounding_box_half_length[d] = 0;
1517 this->bounding_box_center[d] = 0;
1518 }
1519 }
1520
1521 /**
1522 * @brief Return the evaluation of the signed distance function of this solid
1523 * at the given point evaluation point.
1524 *
1525 * @param evaluation_point The point at which the function will be evaluated
1526 * @param component This parameter is not used, but it is necessary because
1527 * Shapes inherit from the Function class of deal.II.
1528 */
1529 double
1530 value(const Point<dim> &evaluation_point,
1531 const unsigned int component = 0) const override;
1532
1533
1534 /**
1535 * @brief Return a pointer to a copy of the Shape
1536 */
1537 std::shared_ptr<Shape<dim>>
1538 static_copy() const override;
1539
1540 /**
1541 * @brief Return the analytical gradient of the distance
1542 * @param evaluation_point The point at which the function will be evaluated
1543 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1544 */
1545 Tensor<1, dim>
1546 gradient(const Point<dim> &evaluation_point,
1547 const unsigned int component = 0) const override;
1548
1549 /**
1550 * @brief
1551 * Return the volume displaced by the solid.
1552 */
1553 double
1554 displaced_volume() override;
1555
1556
1557private:
1558 Tensor<1, dim> normal;
1559};
1560
1561
1562/**
1563 * @brief This class defines superquadric shapes. Their signed distance
1564 * function is: \f$\left|\frac{x}{a}\right|^r + \left|\frac{y}{b}\right|^s +
1565 * \left|\frac{z}{c}\right|^t - 1 = 0\f$
1566 * @tparam dim Dimension of the shape
1567 */
1568template <int dim>
1569class Superquadric : public Shape<dim>
1570{
1571public:
1572 /**
1573 * @brief Constructor for a superquadric shape
1574 * @param half_lengths The half-lengths of each direction
1575 * @param exponents The blockiness in each direction
1576 * @param epsilon The tolerance for surface representation
1577 * @param position The superquadric center
1578 * @param orientation The superquadric orientation
1579 */
1580 Superquadric(const Tensor<1, dim> half_lengths,
1581 const Tensor<1, dim> exponents,
1582 const double epsilon,
1583 const Point<dim> &position,
1584 const Tensor<1, 3> &orientation)
1585 : Shape<dim>(half_lengths.norm(), position, orientation)
1588 , epsilon(epsilon)
1589 {
1590 // Initialize the bounding box.
1592 for (int d = 0; d < dim; ++d)
1593 {
1594 this->bounding_box_center[d] = 0;
1595 }
1596 }
1597
1598 /**
1599 * @brief Return the evaluation of the signed distance function of this solid
1600 * at the given point evaluation point.
1601 *
1602 * @param evaluation_point The point at which the function will be evaluated
1603 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1604 */
1605 double
1606 value(const Point<dim> &evaluation_point,
1607 const unsigned int component = 0) const override;
1608
1609 /**
1610 * @brief Return the evaluation of the signed distance function of this solid
1611 * at the given point evaluation point with a guess for the cell containing
1612 * the evaluation point
1613 * @param evaluation_point The point at which the function will be evaluated
1614 * @param cell The cell that is likely to contain the evaluation point
1615 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1616 */
1617 double
1619 const Point<dim> &evaluation_point,
1620 const typename DoFHandler<dim>::active_cell_iterator &cell,
1621 [[maybe_unused]] const unsigned int component = 0) override;
1622
1623 /**
1624 * @brief Return a pointer to a copy of the Shape
1625 */
1626 std::shared_ptr<Shape<dim>>
1627 static_copy() const override;
1628
1629 /**
1630 * @brief Return the analytical gradient of the distance
1631 * @param evaluation_point The point at which the function will be evaluated
1632 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1633 */
1634 Tensor<1, dim>
1635 gradient(const Point<dim> &evaluation_point,
1636 const unsigned int component = 0) const override;
1637
1638 /**
1639 * @brief Return the gradient of the distance function
1640 * @param evaluation_point The point at which the function will be evaluated
1641 * @param cell The cell that is likely to contain the evaluation point
1642 * @param component Not applicable
1643 */
1644 Tensor<1, dim>
1646 const Point<dim> &evaluation_point,
1647 const typename DoFHandler<dim>::active_cell_iterator &cell,
1648 const unsigned int component = 0) override;
1649
1650 /**
1651 * @brief
1652 * Sets the closest_point parameter to be the point on the surface of the
1653 * shape which has the minimal distance from the given point p. Since this
1654 * function is at the core of the calculations for superquadrics, it is
1655 * redefined here.
1656 *
1657 * @param p The point at which the evaluation is performed
1658 * @param closest_point The reference to the closest point. This point will be modified by the function.
1659 * @param cell_guess A guess of the cell containing the evaluation point, which
1660 * is useful to reduce computation time
1661 */
1662 void
1664 const Point<dim> &p,
1665 Point<dim> &closest_point,
1666 const typename DoFHandler<dim>::active_cell_iterator &cell_guess) override;
1667 void
1668 closest_surface_point(const Point<dim> &p,
1669 Point<dim> &closest_point) const override;
1670
1671 /**
1672 * @brief Return the sign of the parameter. To be removed once PR#794 is merged.
1673 * @param prm parameter
1674 */
1675 static inline double
1676 sign(const double prm)
1677 {
1678 if (prm > 0)
1679 return 1;
1680 else if (prm < 0)
1681 return -1;
1682 else
1683 return 0;
1684 }
1685
1686 /**
1687 * @brief Computes the value of the superquadric from its equation
1688 * @param centered_point point at which we make the evaluation, in the shape referential
1689 */
1690 inline double
1691 superquadric(const Point<dim> &centered_point) const
1692 {
1693 return pow(abs(centered_point[0] / half_lengths[0]), exponents[0]) +
1694 pow(abs(centered_point[1] / half_lengths[1]), exponents[1]) +
1695 pow(abs(centered_point[2] / half_lengths[2]), exponents[2]) - 1.0;
1696 }
1697
1698 /**
1699 * @brief Computes the gradient of the superquadric from its equation
1700 * @param centered_point point at which we make the evaluation, in the shape referential
1701 */
1702 inline Point<dim>
1703 superquadric_gradient(const Point<dim> &centered_point) const
1704 {
1705 Point<dim> gradient{};
1706 for (int d = 0; d < dim; d++)
1707 {
1708 // For cases where the coordinate is of value 0, we avoid computing the
1709 // gradient. That is an issue when the exponent is lower than or equal
1710 // to 1
1711 if (abs(centered_point[d]) > epsilon)
1712 gradient[d] = exponents[d] *
1713 pow(abs(half_lengths[d]), -exponents[d]) *
1714 pow(abs(centered_point[d]), exponents[d]) /
1715 (centered_point[d] + DBL_MIN);
1716 else
1717 gradient[d] = exponents[d] *
1718 pow(abs(half_lengths[d]), -exponents[d]) *
1719 pow(abs(epsilon), exponents[d]) / (epsilon + DBL_MIN);
1720 }
1721 return gradient;
1722 }
1723
1724private:
1725 Tensor<1, dim> half_lengths;
1726 Tensor<1, dim> exponents;
1727 double epsilon;
1728};
1729
1730template <int dim>
1731class HyperRectangle : public Shape<dim>
1732{
1733public:
1734 /**
1735 * @brief Constructs a box with the given parameters
1736 * @param half_lengths The half lengths of each direction
1737 * @param position The hyper rectangle center
1738 * @param orientation The hyper rectangle orientation
1739 */
1740 HyperRectangle(const Tensor<1, dim> &half_lengths,
1741 const Point<dim> &position,
1742 const Tensor<1, 3> &orientation)
1743 : Shape<dim>(half_lengths.norm(), position, orientation)
1745 {
1747 for (int d = 0; d < dim; ++d)
1748 {
1749 this->bounding_box_center[d] = 0;
1750 }
1751 }
1752
1753
1754 /**
1755 * @brief Return the evaluation of the signed distance function of this solid
1756 * at the given point evaluation point.
1757 *
1758 * @param evaluation_point The point at which the function will be evaluated
1759 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1760 */
1761 double
1762 value(const Point<dim> &evaluation_point,
1763 const unsigned int component = 0) const override;
1764
1765 /**
1766 * @brief Return a pointer to a copy of the Shape
1767 */
1768 std::shared_ptr<Shape<dim>>
1769 static_copy() const override;
1770
1771 /**
1772 * @brief
1773 * Return the volume displaced by the solid.
1774 */
1775 double
1776 displaced_volume() override;
1777
1778 // Half-lengths of every side of the box
1779 Tensor<1, dim> half_lengths;
1780};
1781
1782template <int dim>
1783class Ellipsoid : public Shape<dim>
1784{
1785public:
1786 /**
1787 * @brief Constructs an ellipsoid with the given arguments
1788 * @param radii The radii of each direction
1789 * @param position The ellipsoid center
1790 * @param orientation The ellipsoid orientation
1791 */
1792 Ellipsoid(const Tensor<1, dim> &radii,
1793 const Point<dim> &position,
1794 const Tensor<1, 3> &orientation)
1795 : Shape<dim>(radii.norm(), position, orientation)
1796 , radii(radii)
1797 {
1799 for (int d = 0; d < dim; ++d)
1800 {
1801 this->bounding_box_center[d] = 0;
1802 }
1803 }
1804
1805 /**
1806 * @brief Return the evaluation of the signed distance function of this solid
1807 * at the given point evaluation point.
1808 *
1809 * @param evaluation_point The point at which the function will be evaluated
1810 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1811 */
1812 double
1813 value(const Point<dim> &evaluation_point,
1814 const unsigned int component = 0) const override;
1815
1816 /**
1817 * @brief Return a pointer to a copy of the Shape
1818 */
1819 std::shared_ptr<Shape<dim>>
1820 static_copy() const override;
1821
1822 /**
1823 * @brief
1824 * Return the volume displaced by the solid.
1825 */
1826 double
1827 displaced_volume() override;
1828
1829private:
1830 // The radii of all directions in which the ellipsoid is defined
1831 Tensor<1, dim> radii;
1832};
1833
1834template <int dim>
1835class Torus : public Shape<dim>
1836{
1837public:
1838 /**
1839 * @brief Constructs a torus with a given thickness and radius
1840 * @param ring_radius The ring radius
1841 * @param ring_thickness The ring thickness radius/half-thickness
1842 * @param position The torus center
1843 * @param orientation The orientation of the axis at the center of the torus
1844 */
1846 double ring_thickness,
1847 const Point<dim> &position,
1848 const Tensor<1, 3> &orientation)
1852 {
1853 if constexpr (dim == 3)
1854 {
1858 };
1859 for (int d = 0; d < dim; ++d)
1860 {
1861 this->bounding_box_center[d] = 0;
1862 }
1863 }
1864
1865 /**
1866 * @brief Return the evaluation of the signed distance function of this solid
1867 * at the given point evaluation point.
1868 *
1869 * @param evaluation_point The point at which the function will be evaluated
1870 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1871 */
1872 double
1873 value(const Point<dim> &evaluation_point,
1874 const unsigned int component = 0) const override;
1875
1876 /**
1877 * @brief Return a pointer to a copy of the Shape
1878 */
1879 std::shared_ptr<Shape<dim>>
1880 static_copy() const override;
1881
1882 /**
1883 * @brief
1884 * Return the volume displaced by the solid.
1885 */
1886 double
1887 displaced_volume() override;
1888
1889private:
1892};
1893
1894template <int dim>
1895class Cone : public Shape<dim>
1896{
1897public:
1898 /**
1899 * @brief Constructs a cone
1900 * @param tan_base_angle The tangent of the angle between the base of the cone and its curve side
1901 * @param height The height of the cone
1902 * @param position The position of the center of cone's base
1903 * @param orientation The orientation of the cone axis, from its base to its tip
1904 */
1906 double height,
1907 const Point<dim> &position,
1908 const Tensor<1, 3> &orientation)
1911 , height(height)
1914 {
1915 if constexpr (dim == 3)
1916 {
1918 this->bounding_box_half_length[1] = height / 2;
1920 this->bounding_box_center[0] = 0;
1921 this->bounding_box_center[1] = -height / 2;
1922 this->bounding_box_center[2] = 0;
1923 }
1924 }
1925
1926 /**
1927 * @brief Return the evaluation of the signed distance function of this solid
1928 * at the given point evaluation point.
1929 *
1930 * @param evaluation_point The point at which the function will be evaluated
1931 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1932 */
1933 double
1934 value(const Point<dim> &evaluation_point,
1935 const unsigned int component = 0) const override;
1936
1937 /**
1938 * @brief Return a pointer to a copy of the Shape
1939 */
1940 std::shared_ptr<Shape<dim>>
1941 static_copy() const override;
1942
1943 /**
1944 * @brief
1945 * Return the volume displaced by the solid.
1946 */
1947 double
1948 displaced_volume() override;
1949
1950private:
1952 double height;
1954
1955 Tensor<1, 2> intermediate_q;
1956};
1957
1958template <int dim>
1959class CutHollowSphere : public Shape<dim>
1960{
1961public:
1962 /**
1963 * @brief Constructs a hollow sphere that has a wall thickness and that is cut
1964 * by a given depth
1965 * @param radius The radius of the smallest sphere containing the cut hollow sphere
1966 * @param cut_depth The height of the slice removed from the sphere
1967 * @param shell_thickness The thickness of the hollow sphere shell
1968 * @param position The center of the sphere
1969 * @param orientation The orientation of the sphere, from it's center to the cut's center
1970 */
1972 double cut_depth,
1973 double shell_thickness,
1974 const Point<dim> &position,
1975 const Tensor<1, 3> &orientation)
1977 , radius(radius)
1981 {
1983 this->bounding_box_half_length[1] = radius / 2 + cut_depth / 2;
1984 this->bounding_box_center[0] = 0;
1985 this->bounding_box_center[1] = -radius / 2 + cut_depth / 2;
1986 if constexpr (dim == 3)
1987 {
1989 this->bounding_box_center[2] = 0;
1990 }
1991 }
1992
1993 /**
1994 * @brief Return the evaluation of the signed distance function of this solid
1995 * at the given point evaluation point.
1996 *
1997 * @param evaluation_point The point at which the function will be evaluated
1998 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
1999 */
2000 double
2001 value(const Point<dim> &evaluation_point,
2002 const unsigned int component = 0) const override;
2003
2004 /**
2005 * @brief Return a pointer to a copy of the Shape
2006 */
2007 std::shared_ptr<Shape<dim>>
2008 static_copy() const override;
2009
2010 /**
2011 * @brief
2012 * Return the volume displaced by the solid.
2013 */
2014 double
2015 displaced_volume() override;
2016
2017private:
2018 double radius;
2021
2023};
2024
2025template <int dim>
2026class DeathStar : public Shape<dim>
2027{
2028public:
2029 /**
2030 * @brief The Death Star is the result of a boolean subtraction of one sphere from
2031 * another
2032 * @param radius The main sphere radius
2033 * @param hole_radius The removed sphere radius
2034 * @param spheres_distance The distance between the centers of the sphere
2035 * @param position The main sphere's center
2036 * @param orientation The orientation from the main sphere's center to the removed sphere's center
2037 */
2039 double hole_radius,
2040 double spheres_distance,
2041 const Point<dim> &position,
2042 const Tensor<1, 3> &orientation)
2044 , radius(radius)
2049 (2. * spheres_distance))
2051 sqrt(std::max(radius * radius - intermediate_a * intermediate_a, 0.)))
2052 {
2054 this->bounding_box_half_length[0] = 0;
2056 this->bounding_box_half_length[1] = 0;
2057 if constexpr (dim == 3)
2058 {
2060 this->bounding_box_half_length[2] = 0;
2061 }
2062 }
2063
2064 /**
2065 * @brief Return the evaluation of the signed distance function of this solid
2066 * at the given point evaluation point.
2067 *
2068 * @param evaluation_point The point at which the function will be evaluated
2069 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
2070 */
2071 double
2072 value(const Point<dim> &evaluation_point,
2073 const unsigned int component = 0) const override;
2074
2075 /**
2076 * @brief Return a pointer to a copy of the Shape
2077 */
2078 std::shared_ptr<Shape<dim>>
2079 static_copy() const override;
2080
2081 /**
2082 * @brief
2083 * Return the volume displaced by the solid.
2084 */
2085 double
2086 displaced_volume() override;
2087
2088private:
2089 double radius;
2092
2095};
2096
2097/**
2098 * @brief This class was designed so that specific composite shapes could be
2099 * defined through the parameter file. Boolean operations such as union,
2100 * difference, and intersection are allowed.
2101 * @tparam dim Dimension of the shape
2102 */
2103template <int dim>
2104class CompositeShape : public Shape<dim>
2105{
2106public:
2113
2114 /**
2115 * @brief Constructs an assembly of shapes into a composite shape
2116 * @param constituents The shapes from which this composite shape will be composed
2117 * @param operations The list of operations to perform to construct the composite
2118 * @param position
2119 * @param orientation
2120 */
2122 std::map<unsigned int, std::shared_ptr<Shape<dim>>> constituents,
2123 std::map<unsigned int,
2124 std::tuple<BooleanOperation, unsigned int, unsigned int>>
2125 operations,
2126 const Point<dim> &position,
2127 const Tensor<1, 3> &orientation)
2128 : Shape<dim>(0., position, orientation)
2130 , operations(std::move(operations))
2131 {
2132 Point<dim> point_max;
2133 Point<dim> point_min;
2134 bool xyz_min_max_is_initialized = false;
2135
2136 // Calculation of the effective radius and setting of constituents' status
2137 for (auto const &constituent : constituents | boost::adaptors::map_values)
2138 {
2139 this->effective_radius =
2140 std::max(this->effective_radius, constituent->effective_radius);
2141 constituent->set_part_of_a_composite(true);
2142
2143 // For each of the components update the bounding box.
2144 if constexpr (dim == 2)
2145 {
2146 std::vector<Point<3>> component_bounding_box_vertex(4);
2147 unsigned int index = 0;
2148 for (int i = -1; i < 2; i += 2)
2149 {
2150 for (int j = -1; j < 2; j += 2)
2151 {
2152 component_bounding_box_vertex[index][0] =
2153 constituent->get_bounding_box_center()[0] +
2154 constituent->get_bounding_box_half_length()[0] * i;
2155 component_bounding_box_vertex[index][1] =
2156 constituent->get_bounding_box_center()[1] +
2157 constituent->get_bounding_box_half_length()[1] * j;
2158 index += 1;
2159 }
2160 }
2161 for (unsigned int i = 0; i < 4; ++i)
2162 {
2163 // Position the vertex point in the reference frame of the
2164 // composite
2165 constituent->set_position(constituent->get_position());
2166 constituent->set_orientation(constituent->get_orientation());
2167 auto new_point = constituent->get_rotation_matrix() *
2168 component_bounding_box_vertex[i] +
2169 point_nd_to_3d(constituent->get_position());
2170 if (xyz_min_max_is_initialized == false)
2171 {
2172 for (int d = 0; d < dim; ++d)
2173 {
2174 point_min[d] = new_point[d];
2175 point_max[d] = new_point[d];
2176 }
2177 xyz_min_max_is_initialized = true;
2178 }
2179 else
2180 {
2181 for (int d = 0; d < dim; ++d)
2182 {
2183 point_min[d] = std::min(point_min[d], new_point[d]);
2184 point_max[d] = std::max(point_max[d], new_point[d]);
2185 }
2186 }
2187 }
2188 }
2189 else
2190 {
2191 std::vector<Point<3>> component_bounding_box_vertex(8);
2192 unsigned int index = 0;
2193 for (int i = -1; i < 2; i += 2)
2194 {
2195 for (int j = -1; j < 2; j += 2)
2196 {
2197 for (int k = -1; k < 2; k += 2)
2198 {
2199 component_bounding_box_vertex[index][0] =
2200 constituent->get_bounding_box_center()[0] +
2201 constituent->get_bounding_box_half_length()[0] * i;
2202 component_bounding_box_vertex[index][1] =
2203 constituent->get_bounding_box_center()[1] +
2204 constituent->get_bounding_box_half_length()[1] * j;
2205 component_bounding_box_vertex[index][2] =
2206 constituent->get_bounding_box_center()[2] +
2207 constituent->get_bounding_box_half_length()[2] * k;
2208 index += 1;
2209 }
2210 }
2211 }
2212 for (unsigned int i = 0; i < 8; ++i)
2213 {
2214 // Position the vertex point in the reference frame of the
2215 // composite
2216 constituent->set_position(constituent->get_position());
2217 constituent->set_orientation(constituent->get_orientation());
2218 auto new_point = constituent->get_rotation_matrix() *
2219 component_bounding_box_vertex[i] +
2220 point_nd_to_3d(constituent->get_position());
2221
2222 if (xyz_min_max_is_initialized == false)
2223 {
2224 for (int d = 0; d < dim; ++d)
2225 {
2226 point_min[d] = new_point[d];
2227 point_max[d] = new_point[d];
2228 }
2229 xyz_min_max_is_initialized = true;
2230 }
2231 else
2232 {
2233 for (int d = 0; d < dim; ++d)
2234 {
2235 point_min[d] = std::min(point_min[d], new_point[d]);
2236 point_max[d] = std::max(point_max[d], new_point[d]);
2237 }
2238 }
2239 }
2240 }
2241 }
2242 this->bounding_box_half_length = (point_max - point_min) / 2;
2243 this->bounding_box_center = (point_max + point_min) / 2;
2244 }
2245
2246 /**
2247 * @brief Constructs an assembly of shapes into a composite shape from a vector of shapes.
2248 * This constructor is mainly used for outputting multiple shapes with a
2249 * global level set function defined as a union.
2250 * @param constituents_vector The shapes from which this composite sphere will be composed
2251 * @param position
2252 * @param orientation
2253 */
2254 CompositeShape(std::vector<std::shared_ptr<Shape<dim>>> constituents_vector,
2255 const Point<dim> &position,
2256 const Tensor<1, 3> &orientation)
2257 : Shape<dim>(0., position, orientation)
2258 {
2259 size_t number_of_constituents = constituents_vector.size();
2260
2261 for (size_t i = 0; i < number_of_constituents; i++)
2262 constituents[i] = constituents_vector[i];
2263 if (number_of_constituents > 1)
2264 {
2265 // If there are at least two components, the first operation should
2266 // always be a union of 0 and 1
2267 operations[number_of_constituents] =
2268 std::make_tuple(BooleanOperation::Union, 0, 1);
2269 // We make the union until the before last component
2270 for (size_t i = 1; i < number_of_constituents - 1; i++)
2271 {
2272 operations[i + number_of_constituents] =
2273 std::make_tuple(BooleanOperation::Union,
2274 i + 1,
2275 i + number_of_constituents - 1);
2276 }
2277 }
2278 // Calculation of the effective radius
2279 for (auto const &constituent : constituents | boost::adaptors::map_values)
2280 {
2281 this->effective_radius =
2282 std::max(this->effective_radius, constituent->effective_radius);
2283 }
2284 }
2285
2286 /**
2287 * @brief Return the evaluation of the signed distance function of this solid
2288 * at the given point evaluation point.
2289 *
2290 * @param evaluation_point The point at which the function will be evaluated
2291 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
2292 */
2293 double
2294 value(const Point<dim> &evaluation_point,
2295 const unsigned int component = 0) const override;
2296
2297 /**
2298 * @brief Return the evaluation of the signed distance function of this solid
2299 * at the given point evaluation point with a guess for the cell containing
2300 * the evaluation point
2301 * @param evaluation_point The point at which the function will be evaluated
2302 * @param cell The cell that is likely to contain the evaluation point
2303 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
2304 */
2305 double
2307 const Point<dim> &evaluation_point,
2308 const typename DoFHandler<dim>::active_cell_iterator &cell,
2309 [[maybe_unused]] const unsigned int component = 0) override;
2310
2311 /**
2312 * @brief Return the gradient of the distance function
2313 * @param evaluation_point The point at which the function will be evaluated
2314 * @param component Not applicable
2315 */
2316 Tensor<1, dim>
2317 gradient(const Point<dim> &evaluation_point,
2318 const unsigned int component = 0) const override;
2319
2320 /**
2321 * @brief Return the gradient of the distance function
2322 * @param evaluation_point The point at which the function will be evaluated
2323 * @param cell The cell that is likely to contain the evaluation point
2324 * @param component Not applicable
2325 */
2326 Tensor<1, dim>
2328 const Point<dim> &evaluation_point,
2329 const typename DoFHandler<dim>::active_cell_iterator &cell,
2330 const unsigned int component = 0) override;
2331
2332 /**
2333 * @brief Return a pointer to a copy of the Shape
2334 */
2335 std::shared_ptr<Shape<dim>>
2336 static_copy() const override;
2337
2338 /**
2339 * @brief Sets the proper dof handler, then computes/updates the map of cells
2340 * and their likely non-null nodes
2341 * @param updated_dof_handler the reference to the new dof_handler
2342 * @param mesh_based_precalculations mesh-based precalculations that can lead to slight shape misrepresentation (if RBF typed)
2343 */
2344 void
2345 update_precalculations(DoFHandler<dim> &updated_dof_handler,
2346 const bool mesh_based_precalculations);
2347
2348 /**
2349 * @brief Load data from file. To be called at initialization, after repartitioning or when shape has moved.
2350 * This function is used only for RBF shapes and its composites at the
2351 * moment.
2352 */
2353 void
2355
2356 /**
2357 * @brief Remove data that affects only artificial cells (not locally owned and not ghost).
2358 * The data is removed if it would never be accessed by the local process.
2359 * @param updated_dof_handler the reference to the new dof_handler
2360 * @param mesh_based_precalculations mesh-based precalculations that can lead to slight shape misrepresentation (if RBF typed)
2361 */
2362 void
2363 remove_superfluous_data(DoFHandler<dim> &updated_dof_handler,
2364 const bool mesh_based_precalculations);
2365
2366 /**
2367 * @brief Computes the assigned boolean operations
2368 * @param constituent_shapes_values map containing the computed values for the component shapes
2369 * @param constituent_shapes_gradients map containing the computed gradients for the component shapes
2370 */
2371 inline std::pair<double, Tensor<1, dim>>
2373 std::map<unsigned int, double> constituent_shapes_values,
2374 std::map<unsigned int, Tensor<1, dim>> constituent_shapes_gradients) const;
2375
2376 /**
2377 * @brief
2378 * Clear the cache of the shape
2379 */
2380 virtual void
2381 clear_cache() override;
2382
2383 /**
2384 * @brief
2385 * See base
2386 */
2387 void
2388 set_layer_thickening(const double layer_thickening) override;
2389
2390private:
2391 // The members of this class are all the constituent and operations that are
2392 // to be performed to construct the composite shape
2393 // This map link all primitive constituents of the composite shape to an id
2394 std::map<unsigned int, std::shared_ptr<Shape<dim>>> constituents;
2395 // This map links all operations between primitive constituents or
2396 // intermediate constituents (resulting from each operation) to an id. The
2397 // unsigned integers correspond to the first and second ids of the shapes
2398 // used for an operation
2399 std::map<unsigned int,
2400 std::tuple<BooleanOperation, unsigned int, unsigned int>>
2402};
2403
2404template <int dim>
2405class OpenCascadeShape : public Shape<dim>
2406{
2407public:
2408 /**
2409 * @brief Constructor for an OpenCascade shape
2410 * @param file_name The name of the file describing the shape
2411 * @param position The shape center
2412 * @param orientation The shape orientation
2413 */
2414 OpenCascadeShape(const std::string &file_name,
2415 const Point<dim> &position,
2416 const Tensor<1, 3> &orientation)
2417 : Shape<dim>(0.1, position, orientation)
2418 {
2419 // First, we read the shape file name
2420 local_file_name = file_name;
2421#ifdef DEAL_II_WITH_OPENCASCADE
2422 // Checks the file name extension to identify which type of OpenCascade
2423 // shape we are working with.
2424 std::vector<std::string> file_name_and_extension(
2425 Utilities::split_string_list(local_file_name, "."));
2426
2427 // Load the shape with the appropriate tool.
2428 if (file_name_and_extension[1] == "step" ||
2429 file_name_and_extension[1] == "stp")
2430 {
2431 shape = OpenCASCADE::read_STEP(local_file_name);
2432 this->additional_info_on_shape = "step";
2433 }
2434 else if (file_name_and_extension[1] == "iges" ||
2435 file_name_and_extension[1] == "igs")
2436 {
2437 shape = OpenCASCADE::read_IGES(local_file_name);
2438 this->additional_info_on_shape = "iges";
2439 }
2440 else if (file_name_and_extension[1] == "stl")
2441 {
2442 shape = OpenCASCADE::read_STL(local_file_name);
2443 this->additional_info_on_shape = "stl";
2444 }
2445 else
2446 {
2447 throw std::runtime_error(
2448 "Wrong file extension for an opencascade shape. The possible file type are: step, stp, iges, igs, stl.");
2449 }
2450
2451 // used this local variable as the shape tolerance in the calculations.
2452 shape_tol = OpenCASCADE::get_shape_tolerance(shape);
2453
2454 // Initialize some variables and the OpenCascade distance tool.
2455 OpenCASCADE::extract_compound_shapes(
2456 shape, compounds, compsolids, solids, shells, wires);
2457 vertex_position = OpenCASCADE::point(Point<dim>());
2458 vertex = BRepBuilderAPI_MakeVertex(vertex_position);
2459 distancetool = BRepExtrema_DistShapeShape(shape, vertex);
2460
2461 // Check if the shape has a shell. If it has a shell, we initialize a
2462 // distance tool with just the shell.
2463 if (shells.size() > 0)
2464 {
2465 // Check if the number of solids is precisely 1. If it is, we redefine
2466 // the shape as only the solid. If it is not the case and there are
2467 // multiple shells, we throw an error since we won't be able to
2468 // represent the shape correctly.
2469 if (solids.size() == 1)
2470 {
2471 // Extract the solid
2472 shape = solids[0];
2473 // Extract the shell 0.
2474 OpenCASCADE::extract_compound_shapes(
2475 shape, compounds, compsolids, solids, shells, wires);
2476 // Load the tools
2477 distancetool = BRepExtrema_DistShapeShape(shells[0], vertex);
2478 point_classifier.Load(shape);
2479 }
2480 else if (shells.size() > 1)
2481 {
2482 throw std::runtime_error(
2483 "Error!: The shape has more than one shell. The code does not support shapes with multiple shells or solids. If your shape has more than one shell or solid, it is usually possible to recombine them into one. Otherwise, it is possible to split the shape into sub-shells and sub-solids and then define one particle for each of them.");
2484 }
2485 distancetool = BRepExtrema_DistShapeShape(shells[0], vertex);
2486 point_classifier.Load(shape);
2487 }
2488 else
2489 {
2490 distancetool = BRepExtrema_DistShapeShape(shape, vertex);
2491 point_classifier.Load(shape);
2492 }
2493
2494 // Define the effective radius as the raidus of the sphere with the same
2495 // volume as the shape.
2496 GProp_GProps system;
2497 BRepGProp::LinearProperties(shape, system);
2498 BRepGProp::SurfaceProperties(shape, system);
2499 BRepGProp::VolumeProperties(shape, system);
2500 this->effective_radius =
2501 std::pow(system.Mass() * 3.0 / (4 * numbers::PI), 1.0 / dim);
2502
2503 // Define an "infinite" bounding box size
2504 this->bounding_box_half_length[0] = DBL_MAX;
2505 this->bounding_box_half_length[1] = DBL_MAX;
2506 this->bounding_box_center[0] = 0;
2507 this->bounding_box_center[1] = 0;
2508 if constexpr (dim == 3)
2509 {
2510 this->bounding_box_half_length[2] = DBL_MAX;
2511 this->bounding_box_center[2] = 0;
2512 }
2513
2514#endif
2515 }
2516
2517 /**
2518 * @brief Return the evaluation of the signed distance function of this solid
2519 * at the given point evaluation point.
2520 *
2521 * @param evaluation_point The point at which the function will be evaluate.
2522 * @param component Not applicable
2523 */
2524 double
2525 value(const Point<dim> &evaluation_point,
2526 const unsigned int component = 0) const override;
2527
2528 /**
2529 * @brief Return the evaluation of the signed distance function of this solid
2530 * at the given point evaluation point.
2531 *
2532 * @param evaluation_point The point at which the function will be evaluated
2533 * @param cell The cell that is likely to contain the evaluation point. Use
2534 * @param component Not applicable
2535 */
2536 double
2538 const Point<dim> &evaluation_point,
2539 const typename DoFHandler<dim>::active_cell_iterator &cell,
2540 const unsigned int component = 0) override;
2541
2542 /**
2543 * @brief Return a pointer to a copy of the Shape
2544 */
2545 std::shared_ptr<Shape<dim>>
2546 static_copy() const override;
2547
2548 /**
2549 * @brief Return the gradient of the distance function
2550 * @param evaluation_point The point at which the function will be evaluated
2551 * @param component Not applicable
2552 */
2553 Tensor<1, dim>
2554 gradient(const Point<dim> &evaluation_point,
2555 const unsigned int component = 0) const override;
2556
2557 /**
2558 * @brief Return the gradient of the distance function
2559 * @param evaluation_point The point at which the function will be evaluated
2560 * @param cell The cell that is likely to contain the evaluation point
2561 * @param component Not applicable
2562 */
2563 Tensor<1, dim>
2565 const Point<dim> &evaluation_point,
2566 const typename DoFHandler<dim>::active_cell_iterator &cell,
2567 const unsigned int component = 0) override;
2568
2569 /**
2570 * @brief
2571 * Return the volume displaced by the solid.
2572 */
2573 double
2574 displaced_volume() override;
2575
2576 /**
2577 * @brief
2578 * Sets a new position for the shape
2579 *
2580 * @param position The new position the shape will be placed at
2581 */
2582 void
2583 set_position(const Point<dim> &position) override;
2584
2585private:
2586 // Keep in memory the file name of the shape.
2587 std::string local_file_name;
2588#ifdef DEAL_II_WITH_OPENCASCADE
2589 // The shape define by the opencascade file.
2590 TopoDS_Shape shape;
2591
2592 // We split the shape into its components we store them in these containers.
2593 std::vector<TopoDS_Compound> compounds;
2594 std::vector<TopoDS_CompSolid> compsolids;
2595 std::vector<TopoDS_Solid> solids;
2596 std::vector<TopoDS_Shell> shells;
2597 std::vector<TopoDS_Wire> wires;
2598
2599 // The point at which we are going to evaluate the shape.
2600 gp_Pnt vertex_position;
2601
2602 // The shape object (vertex) used in the distance evaluation.
2603 TopoDS_Vertex vertex;
2604
2605 // The tool used for the distance evaluation.
2606 BRepClass3d_SolidClassifier point_classifier;
2607 BRepExtrema_DistShapeShape distancetool;
2608 BRepExtrema_DistShapeShape distancetool_shell;
2609 double shape_tol;
2610#endif
2611};
2612
2613
2614/**
2615 * @tparam dim Dimension of the shape
2616 * @brief RBF Shapes express the signed distance function as a linear
2617 * combination of Radial Basis Functions (RBF), which have a defined support
2618 * radius and basis function. A collection of nodes and weights compose the
2619 * object. Outside of the domain covered by the nodes, the distance is
2620 * computed by using the distance to a bounding box instead.
2621 */
2622template <int dim>
2623class RBFShape : public Shape<dim>
2624{
2625public:
2626 /**
2627 * Class taken from Optimad Bitpit. https://github.com/optimad/bitpit
2628 * @enum RBFBasisFunction
2629 * @brief Enum class defining types of RBF kernel functions that could be used
2630 * in the class
2631 */
2650
2651 /**
2652 * @brief An RBFShape represents a physical object by describing its signed
2653 * distance field with a linear combination of radial basis functions. Each
2654 * radial basis function has a location and properties that are used in the
2655 * sum.
2656 * @param shape_arguments_str the name of the file used to load the data
2657 * @param position the location of the RBF shape
2658 * @param orientation the orientation of the shape with respect to each main
2659 * axis
2660 */
2661 RBFShape(const std::string &shape_arguments_str,
2662 const Point<dim> &position,
2663 const Tensor<1, 3> &orientation);
2664
2665 /**
2666 * @brief Load RBF data from file. To be called at initialization, after repartitioning or when shape has moved.
2667 * This function is used only for RBF shapes and its composites at the
2668 * moment.
2669 */
2670 void
2672
2673 /**
2674 * @brief Remove data that affects only artificial cells (not locally owned and not ghost).
2675 * The data is removed if it would never be accessed by the local process.
2676 * @param dof_handler the reference to the new dof_handler
2677 * @param mesh_based_precalculations mesh-based precalculations that can lead to slight shape misrepresentation (if RBF typed)
2678 */
2679 void
2680 remove_superfluous_data(DoFHandler<dim> &dof_handler,
2681 const bool mesh_based_precalculations);
2682
2683 /**
2684 * @brief Return the evaluation of the signed distance function of this solid
2685 * at the given point evaluation point. The computation and addition of the
2686 * bounding box distance are necessary since the RBF nodes may not cover the
2687 * whole simulation domain. In that case, it is assumed that the distance
2688 * from the RBF object is approximately the same as the distance from the
2689 * corresponding bounding box.
2690 *
2691 * @param evaluation_point The point at which the function will be evaluated
2692 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
2693 */
2694 double
2695 value(const Point<dim> &evaluation_point,
2696 const unsigned int component = 0) const override;
2697
2698 /**
2699 * @brief Return the evaluation of the signed distance function of this solid
2700 * at the given point evaluation point with a guess for the cell containing
2701 * the evaluation point
2702 * @param evaluation_point The point at which the function will be evaluated
2703 * @param cell The cell that is likely to contain the evaluation point
2704 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
2705 */
2706 double
2708 const Point<dim> &evaluation_point,
2709 [[maybe_unused]] const typename DoFHandler<dim>::active_cell_iterator &cell,
2710 [[maybe_unused]] const unsigned int component = 0) override;
2711
2712 /**
2713 * @brief Return the analytical gradient of the distance
2714 * @param evaluation_point The point at which the function will be evaluated
2715 * @param cell The cell that is likely to contain the evaluation point
2716 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
2717 */
2718 Tensor<1, dim>
2720 const Point<dim> &evaluation_point,
2721 const typename DoFHandler<dim>::active_cell_iterator &cell,
2722 const unsigned int component = 0) override;
2723
2724 /**
2725 * @brief Return the analytical gradient of the distance for the current RBF
2726 * @param evaluation_point The point at which the function will be evaluated
2727 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
2728 */
2729 Tensor<1, dim>
2730 gradient(const Point<dim> &evaluation_point,
2731 const unsigned int component = 0) const override;
2732
2733 /**
2734 * @brief Return a pointer to a copy of the Shape
2735 */
2736 std::shared_ptr<Shape<dim>>
2737 static_copy() const override;
2738
2739 /**
2740 * @brief
2741 * Return the volume displaced by the solid.
2742 */
2743 double
2744 displaced_volume() override;
2745
2746 /**
2747 * A bounding box is constructed around the collection of nodes defining the
2748 * RBF. It solves an issue where the collection of nodes is located only
2749 * around the object itself, which would result in an undefined distance
2750 * when the value is evaluated outside of all support radii. The hyper
2751 * rectangle shape doesn't have this limitation, as its distance can be
2752 * evaluated anywhere. The distance computed by an RBF object will therefore
2753 * use an approximated distance when the evaluation point is too far.
2754 *
2755 * @brief Initializes the bounding box around the nodes which enables distance
2756 * calculation even if the RBF nodes don't cover the whole domain
2757 */
2758 void
2760
2761 /**
2762 * @brief Returns the value of the basis function for a given distance.
2763 * Inspired by Optimad Bitpit. https://github.com/optimad/bitpit
2764 * @param basis_function basis function to be used for calculation
2765 * @param distance distance to the node normalized by the support radius
2766 */
2767 inline double
2768 evaluate_basis_function(const RBFBasisFunction basis_function,
2769 const double distance) const;
2770
2771 /**
2772 * @brief Returns the derivative of the basis function for a given distance.
2773 * Inspired by Optimad Bitpit. https://github.com/optimad/bitpit
2774 * @param basis_function basis function to be used for calculation
2775 * @param distance distance to the node normalized by the support radius
2776 */
2777 inline double
2779 const double distance) const;
2780
2781 /**
2782 * @brief Establishes which nodes bring a non null contribution to the RBF
2783 * @param cell the cell for which the likely nodes are to be found
2784 * @param support_point one point that is located inside the cell
2785 */
2786 void
2788 const typename DoFHandler<dim>::cell_iterator &cell,
2789 const Point<dim> support_point);
2790
2791 /**
2792 * @brief Sets the proper dof handler, then computes/updates the map of cells
2793 * and their likely non-null nodes
2794 * @param updated_dof_handler the reference to the new dof_handler
2795 * @param mesh_based_precalculations mesh-based precalculations that can lead to slight shape misrepresentation (if RBF typed)
2796 * */
2797 void
2798 update_precalculations(DoFHandler<dim> &updated_dof_handler,
2799 const bool mesh_based_precalculations);
2800
2801 /**
2802 * @brief Rotate RBF nodes in the global reference frame (the reference frame of the triangulation).
2803 */
2804 void
2805 rotate_nodes();
2806
2807 /**
2808 * @brief Compact Wendland C2 function defined from 0 to 1.
2809 * @param distance distance to the node normalized by the support radius
2810 */
2811 static inline double
2812 wendlandc2(const double distance)
2813 {
2814 return distance > 1.0 ?
2815 0.0 :
2816 std::pow(1. - distance, 4.0) * (4.0 * distance + 1.0);
2817 }
2818
2819 /**
2820 * @brief Compact linear function defined from 0 to 1.
2821 * @param distance distance to the node normalized by the support radius
2822 */
2823 static inline double
2824 linear(const double distance)
2825 {
2826 return distance > 1.0 ? 0.0 : (1.0 - distance);
2827 }
2828
2829 /**
2830 * @brief Non-compact Gaussian function with 0.1 value at distance equal to 1.
2831 * @param distance distance to the node normalized by the support radius
2832 */
2833 static inline double
2834 gauss90(const double distance)
2835 {
2836 double eps = std::pow(-1.0 * std::log(0.1), 0.5);
2837 return std::exp(-1.0 * std::pow(distance * eps, 2.0));
2838 }
2839
2840 /**
2841 * @brief Non-compact Gaussian function with 0.05 value at distance equal to 1.
2842 * @param distance distance to the node normalized by the support radius
2843 */
2844 static inline double
2845 gauss95(const double distance)
2846 {
2847 double eps = std::pow(-1.0 * std::log(0.05), 0.5);
2848 return std::exp(-1.0 * std::pow(distance * eps, 2.0));
2849 }
2850
2851 /**
2852 * @brief Non-compact Gaussian function with 0.01 value at distance equal to 1.
2853 * @param distance distance to the node normalized by the support radius
2854 */
2855 static inline double
2856 gauss99(const double distance)
2857 {
2858 double eps = std::pow(-1.0 * std::log(0.01), 0.5);
2859 return std::exp(-1.0 * std::pow(distance * eps, 2.0));
2860 }
2861
2862 /**
2863 * @brief Compact polynomial function defined from 0 to 1.
2864 * It preserves C1 continuity at distance=0, and C0 continuity at
2865 * distance=1.
2866 * @param distance distance to the node normalized by the support radius
2867 */
2868 static inline double
2869 c1c0(const double distance)
2870 {
2871 return distance > 1.0 ? 0.0 : (1.0 - std::pow(distance, 2.0));
2872 }
2873
2874 /**
2875 * @brief Compact polynomial function defined from 0 to 1.
2876 * It preserves C2 continuity at distance=0, and C0 continuity at
2877 * distance=1.
2878 * @param distance distance to the node normalized by the support radius
2879 */
2880 static inline double
2881 c2c0(const double distance)
2882 {
2883 return distance > 1.0 ? 0.0 : (1.0 - std::pow(distance, 3.0));
2884 }
2885
2886 /**
2887 * @brief Compact polynomial function defined from 0 to 1.
2888 * It preserves C0 continuity at distance=0, and C1 continuity at
2889 * distance=1.
2890 * @param distance distance to the node normalized by the support radius
2891 */
2892 static inline double
2893 c0c1(const double distance)
2894 {
2895 return distance > 1.0 ? 0.0 :
2896 (1.0 - 2.0 * distance + std::pow(distance, 2.0));
2897 }
2898
2899 /**
2900 * @brief Compact polynomial function defined from 0 to 1.
2901 * It preserves C1 continuity at distance=0, and C1 continuity at
2902 * distance=1.
2903 * @param distance distance to the node normalized by the support radius
2904 */
2905 static inline double
2906 c1c1(const double distance)
2907 {
2908 return distance > 1.0 ? 0.0 :
2909 (1.0 - 3.0 * std::pow(distance, 2.0) +
2910 2.0 * std::pow(distance, 3.0));
2911 }
2912
2913 /**
2914 * @brief Compact polynomial function defined from 0 to 1.
2915 * It preserves C2 continuity at distance=0, and C1 continuity at
2916 * distance=1.
2917 * @param distance distance to the node normalized by the support radius
2918 */
2919 static inline double
2920 c2c1(const double distance)
2921 {
2922 return distance > 1.0 ? 0.0 :
2923 (1.0 - 4.0 * std::pow(distance, 3.0) +
2924 3.0 * std::pow(distance, 4.0));
2925 }
2926
2927 /**
2928 * @brief Compact polynomial function defined from 0 to 1.
2929 * It preserves C0 continuity at distance=0, and C2 continuity at
2930 * distance=1.
2931 * @param distance distance to the node normalized by the support radius
2932 */
2933 static inline double
2934 c0c2(const double distance)
2935 {
2936 return distance > 1.0 ?
2937 0.0 :
2938 (1.0 - 3.0 * distance + 3.0 * std::pow(distance, 2.0) -
2939 std::pow(distance, 3.0));
2940 }
2941
2942 /**
2943 * @brief Compact polynomial function defined from 0 to 1.
2944 * It preserves C1 continuity at distance=0, and C2 continuity at
2945 * distance=1.
2946 * @param distance distance to the node normalized by the support radius
2947 */
2948 static inline double
2949 c1c2(const double distance)
2950 {
2951 return distance > 1.0 ?
2952 0.0 :
2953 (1.0 - 6.0 * std::pow(distance, 2.0) +
2954 8.0 * std::pow(distance, 3.0) - 3.0 * std::pow(distance, 4.0));
2955 }
2956
2957 /**
2958 * @brief Compact polynomial function defined from 0 to 1.
2959 * It preserves C2 continuity at distance=0, and C2 continuity at
2960 * distance=1.
2961 * @param distance distance to the node normalized by the support radius
2962 */
2963 static inline double
2964 c2c2(const double distance)
2965 {
2966 return distance > 1.0 ?
2967 0.0 :
2968 (1.0 - 10.0 * std::pow(distance, 3.0) +
2969 15.0 * std::pow(distance, 4.0) - 6.0 * std::pow(distance, 5.0));
2970 }
2971
2972 /**
2973 * @brief Compact cosinusoidal basis function. It is null when r>1
2974 * @param distance distance to the node normalized by the support radius
2975 */
2976 static inline double
2977 cos(const double distance)
2978 {
2979 return distance > 1.0 ? 0.0 : 0.5 + 0.5 * std::cos(distance * M_PI);
2980 }
2981
2982
2983 /**
2984 * @brief Derivative of a compact Wendland C2 function defined from 0 to 1.
2985 * @param distance distance to the node normalized by the support radius
2986 */
2987 static inline double
2988 wendlandc2_derivative(const double distance)
2989 {
2990 return distance > 1.0 ? 0.0 :
2991 -20.0 * distance * std::pow(1.0 - distance, 3.0);
2992 }
2993
2994 /**
2995 * @brief Derivative of a compact linear function defined from 0 to 1.
2996 * @param distance distance to the node normalized by the support radius
2997 */
2998 static inline double
2999 linear_derivative(const double distance)
3000 {
3001 return distance > 1.0 ? 0.0 : -1.0;
3002 }
3003
3004 /**
3005 * @brief Derivative of a non-compact Gaussian function with 0.1 value at distance equal to 1.
3006 * @param distance distance to the node normalized by the support radius
3007 */
3008 static inline double
3009 gauss90_derivative(const double distance)
3010 {
3011 double eps = std::pow(-1.0 * std::log(0.1), 0.5);
3012 return -2.0 * std::pow(eps, 2.0) * distance *
3013 std::exp(-1.0 * std::pow(distance * eps, 2.0));
3014 }
3015
3016 /**
3017 * @brief Derivative of a non-compact Gaussian function with 0.05 value at distance equal to 1.
3018 * @param distance distance to the node normalized by the support radius
3019 */
3020 static inline double
3021 gauss95_derivative(const double distance)
3022 {
3023 double eps = std::pow(-1.0 * std::log(0.05), 0.5);
3024 return -2.0 * std::pow(eps, 2.0) * distance *
3025 std::exp(-1.0 * std::pow(distance * eps, 2.0));
3026 }
3027
3028 /**
3029 * @brief Derivative of a non-compact Gaussian function with 0.01 value at distance equal to 1.
3030 * @param distance distance to the node normalized by the support radius
3031 */
3032 static inline double
3033 gauss99_derivative(const double distance)
3034 {
3035 double eps = std::pow(-1.0 * std::log(0.01), 0.5);
3036 return -2.0 * std::pow(eps, 2.0) * distance *
3037 std::exp(-1.0 * std::pow(distance * eps, 2.0));
3038 }
3039
3040 /**
3041 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3042 * It preserves C1 continuity at distance=0, and C0 continuity at
3043 * distance=1.
3044 * @param distance distance to the node normalized by the support radius
3045 */
3046 static inline double
3047 c1c0_derivative(const double distance)
3048 {
3049 return distance > 1.0 ? 0.0 : -2.0 * distance;
3050 }
3051
3052 /**
3053 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3054 * It preserves C2 continuity at distance=0, and C0 continuity at
3055 * distance=1.
3056 * @param distance distance to the node normalized by the support radius
3057 */
3058 static inline double
3059 c2c0_derivative(const double distance)
3060 {
3061 return distance > 1.0 ? 0.0 : -3.0 * std::pow(distance, 2.0);
3062 }
3063
3064 /**
3065 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3066 * It preserves C0 continuity at distance=0, and C1 continuity at
3067 * distance=1.
3068 * @param distance distance to the node normalized by the support radius
3069 */
3070 static inline double
3071 c0c1_derivative(const double distance)
3072 {
3073 return distance > 1.0 ? 0.0 : 2.0 * (distance - 1.0);
3074 }
3075
3076 /**
3077 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3078 * It preserves C1 continuity at distance=0, and C1 continuity at
3079 * distance=1.
3080 * @param distance distance to the node normalized by the support radius
3081 */
3082 static inline double
3083 c1c1_derivative(const double distance)
3084 {
3085 return distance > 1.0 ? 0.0 : 6.0 * (distance - 1.0) * distance;
3086 }
3087
3088 /**
3089 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3090 * It preserves C2 continuity at distance=0, and C1 continuity at
3091 * distance=1.
3092 * @param distance distance to the node normalized by the support radius
3093 */
3094 static inline double
3095 c2c1_derivative(const double distance)
3096 {
3097 return distance > 1.0 ? 0.0 :
3098 12 * (distance - 1.0) * std::pow(distance, 2.0);
3099 }
3100
3101 /**
3102 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3103 * It preserves C0 continuity at distance=0, and C2 continuity at
3104 * distance=1.
3105 * @param distance distance to the node normalized by the support radius
3106 */
3107 static inline double
3108 c0c2_derivative(const double distance)
3109 {
3110 return distance > 1.0 ? 0.0 : -3.0 * std::pow(distance - 1.0, 2.0);
3111 }
3112
3113 /**
3114 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3115 * It preserves C1 continuity at distance=0, and C2 continuity at
3116 * distance=1.
3117 * @param distance distance to the node normalized by the support radius
3118 */
3119 static inline double
3120 c1c2_derivative(const double distance)
3121 {
3122 return distance > 1.0 ?
3123 0.0 :
3124 (1.0 - 6.0 * std::pow(distance, 2.0) +
3125 8.0 * std::pow(distance, 3.0) - 3.0 * std::pow(distance, 4.0));
3126 }
3127
3128 /**
3129 * @brief Derivative of a compact polynomial function defined from 0 to 1.
3130 * It preserves C2 continuity at distance=0, and C2 continuity at
3131 * distance=1.
3132 * @param distance distance to the node normalized by the support radius
3133 */
3134 static inline double
3135 c2c2_derivative(const double distance)
3136 {
3137 return distance > 1.0 ?
3138 0.0 :
3139 -30.0 * std::pow(distance - 1.0, 2.0) * std::pow(distance, 2.0);
3140 }
3141
3142 /**
3143 * @brief Derivative of a compact cosinusoidal basis function.
3144 * It preserves continuity at every point
3145 * @param distance distance to the node normalized by the support radius
3146 */
3147 static inline double
3148 cosinus_derivative(const double distance)
3149 {
3150 return distance > 1.0 ? 0.0 : -M_PI_2 * std::sin(M_PI * distance);
3151 }
3152
3153 /**
3154 * @brief Swap the vector of all nodes with a likely node vector
3155 * @param cell A likely one where the evaluation point is located
3156 */
3157 void
3159 const typename DoFHandler<dim>::active_cell_iterator &cell);
3160
3161private:
3162 std::string filename;
3164 std::shared_ptr<HyperRectangle<dim>> bounding_box;
3165
3166 // Elements of this vector are tuples containing: the cell barycenter, the
3167 // cell diameter, and the RBF nodes located inside the active cell
3168 std::vector<
3169 std::tuple<Point<dim>, double, std::shared_ptr<std::vector<size_t>>>>
3171 // Entries of this map contain vectors of tuples containing the cell
3172 // barycenter, diameter and likely nodes that are in that cell. The various
3173 // elements of the vector are the tuples which contain information on the
3174 // RBF nodes that affect the level set evaluation in this cell. Note: the
3175 // division of RBF nodes is made by separating nodes by which active cells
3176 // they are in, and then these RBF nodes groups are referenced by the cells
3177 // in a subsequent cell (by using a vector of pointers). This is required to
3178 // avoid repeating the nodes IDs multiple times (to keep memory requirements
3179 // low); they only appear once in a vector, then this vector is used by
3180 // passing its shared pointer.
3181 std::map<
3182 const typename DoFHandler<dim>::cell_iterator,
3183 std::shared_ptr<std::vector<
3184 std::tuple<Point<dim>, double, std::shared_ptr<std::vector<size_t>>>>>>
3187 DoFHandler<dim> *dof_handler;
3188
3190
3191public:
3192 std::vector<double> weights;
3193 std::vector<Point<dim>> nodes_positions;
3194 std::vector<Point<dim>> rotated_nodes_positions;
3195 std::vector<double> support_radii;
3196 std::vector<double> basis_functions;
3197 std::vector<bool> useful_rbf_nodes;
3198};
3199
3200
3201template <int dim>
3202class Cylinder : public Shape<dim>
3203{
3204public:
3205 /**
3206 * @brief Constructs a cylinder aligned with the z axis
3207 * @param radius the radius of the cylinder
3208 * @param half_length the half-length of the cylinder
3209 * @param position position of the barycenter of the cylinder
3210 * @param orientation orientation of the cylinder
3211 */
3213 double half_length,
3214 const Point<dim> &position,
3215 const Tensor<1, 3> &orientation)
3217 , radius(radius)
3219 {
3223 this->bounding_box_center[0] = 0;
3224 this->bounding_box_center[1] = 0;
3225 this->bounding_box_center[2] = 0;
3226 }
3227
3228 /**
3229 * @brief Return the evaluation of the signed distance function of this solid
3230 * at the given point evaluation point.
3231 *
3232 * @param evaluation_point The point at which the function will be evaluated
3233 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
3234 */
3235 double
3236 value(const Point<dim> &evaluation_point,
3237 const unsigned int component = 0) const override;
3238
3239 /**
3240 * @brief Return a pointer to a copy of the Shape
3241 */
3242 std::shared_ptr<Shape<dim>>
3243 static_copy() const override;
3244
3245 /**
3246 * @brief
3247 * Return the volume displaced by the solid.
3248 */
3249 double
3250 displaced_volume() override;
3251
3252private:
3253 double radius;
3255};
3256
3257template <int dim>
3258class CylindricalTube : public Shape<dim>
3259{
3260public:
3261 /**
3262 * @brief Constructs a tube by boolean difference of two tubes aligned with
3263 * the z axis when orientation is set to 0;0;0
3264 * @param radius_inside the radius of the negative (inside) cylinder
3265 * @param radius_outside the radius of the positive (outside) cylinder
3266 * @param half_length the half-length of the cylinders
3267 * @param position position of the barycenter of the cylinder
3268 * @param orientation orientation of the cylinder
3269 */
3270 CylindricalTube(double radius_inside,
3271 double radius_outside,
3272 double half_length,
3273 const Point<dim> &position,
3274 const Tensor<1, 3> &orientation)
3275 : Shape<dim>((radius_outside + radius_inside) / 2., position, orientation)
3276 , radius((radius_outside + radius_inside) / 2.)
3277 , height(half_length * 2.)
3278 , rectangular_base(radius_outside - radius_inside)
3279 {
3280 this->bounding_box_half_length[0] = radius_outside;
3281 this->bounding_box_half_length[1] = radius_outside;
3282 this->bounding_box_center[0] = 0;
3283 this->bounding_box_center[1] = 0;
3284 if constexpr (dim == 3)
3285 {
3286 this->bounding_box_half_length[2] = half_length;
3287 this->bounding_box_center[2] = 0;
3288 }
3289 }
3290
3291 /**
3292 * @brief Return the evaluation of the signed distance function of this solid
3293 * at the given point evaluation point.
3294 *
3295 * @param evaluation_point The point at which the function will be evaluated
3296 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
3297 */
3298 double
3299 value(const Point<dim> &evaluation_point,
3300 const unsigned int component = 0) const override;
3301
3302 /**
3303 * @brief Return a pointer to a copy of the Shape
3304 */
3305 std::shared_ptr<Shape<dim>>
3306 static_copy() const override;
3307
3308 /**
3309 * @brief
3310 * Return the volume displaced by the solid.
3311 */
3312 double
3313 displaced_volume() override;
3314
3315private:
3316 double radius;
3317 double height;
3319};
3320
3321template <int dim>
3322class CylindricalHelix : public Shape<dim>
3323{
3324public:
3325 /**
3326 * @brief Constructs a cylindrical helix by extruding a disk through a helicoidal path
3327 * aligned with the z axis when orientation is set to 0;0;0
3328 * @param radius_helix the radius of the helicoidal path
3329 * @param radius_disk the radius of the disk that is extruded along the helicoidal
3330 * path
3331 * @param height the total height of the helicoidal path
3332 * @param pitch the height difference between each helix loop around its axis
3333 * @param position the position of the helix base
3334 * @param orientation the orientation of the helix axis compared to the z axis
3335 */
3336 CylindricalHelix(double radius_helix,
3337 double radius_disk,
3338 double height,
3339 double pitch,
3340 const Point<dim> &position,
3341 const Tensor<1, 3> &orientation)
3343 , radius(radius_helix)
3344 , height(height)
3345 , pitch(pitch)
3347 {
3348 this->bounding_box_half_length[0] = radius_helix + radius_disk;
3349 this->bounding_box_half_length[1] = radius_helix + radius_disk;
3350 this->bounding_box_center[0] = 0;
3351 this->bounding_box_center[1] = 0;
3352 if constexpr (dim == 3)
3353 {
3355 this->bounding_box_center[2] = height / 2;
3356 }
3357 }
3358
3359 /**
3360 * @brief Return the evaluation of the signed distance function of this solid
3361 * at the given point evaluation point.
3362 *
3363 * @param evaluation_point The point at which the function will be evaluated
3364 * @param component This parameter is not used, but it is necessary because Shapes inherit from the Function class of deal.II.
3365 */
3366 double
3367 value(const Point<dim> &evaluation_point,
3368 const unsigned int component = 0) const override;
3369
3370 /**
3371 * @brief Return a pointer to a copy of the Shape
3372 */
3373 std::shared_ptr<Shape<dim>>
3374 static_copy() const override;
3375
3376 /**
3377 * @brief
3378 * Return the volume displaced by the solid.
3379 */
3380 double
3381 displaced_volume() override;
3382
3383private:
3384 double radius;
3385 double height;
3386 double pitch;
3388};
3389
3390#endif // lethe_shape_h
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
CompositeShape(std::vector< std::shared_ptr< Shape< dim > > > constituents_vector, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs an assembly of shapes into a composite shape from a vector of shapes. This constructor is ...
Definition shape.h:2254
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
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
Cone(double tan_base_angle, double height, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs a cone.
Definition shape.h:1905
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
CutHollowSphere(double radius, double cut_depth, double shell_thickness, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs a hollow sphere that has a wall thickness and that is cut by a given depth.
Definition shape.h:1971
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
Cylinder(double radius, double half_length, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs a cylinder aligned with the z axis.
Definition shape.h:3212
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
CylindricalHelix(double radius_helix, double radius_disk, double height, double pitch, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs a cylindrical helix by extruding a disk through a helicoidal path aligned with the z axis ...
Definition shape.h:3336
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
CylindricalTube(double radius_inside, double radius_outside, double half_length, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs a tube by boolean difference of two tubes aligned with the z axis when orientation is set ...
Definition shape.h:3270
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
DeathStar(double radius, double hole_radius, double spheres_distance, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
The Death Star is the result of a boolean subtraction of one sphere from another.
Definition shape.h:2038
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
Ellipsoid(const Tensor< 1, dim > &radii, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs an ellipsoid with the given arguments.
Definition shape.h:1792
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
HyperRectangle(const Tensor< 1, dim > &half_lengths, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs a box with the given parameters.
Definition shape.h:1740
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
OpenCascadeShape(const std::string &file_name, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructor for an OpenCascade shape.
Definition shape.h:2414
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
Plane(const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructor for a infinite plane. The plane is normal to the Z axis in 3D and Y axis in 2D in the ref...
Definition shape.h:1501
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
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
A base class used to represent geometrical entities. Its main uses are to return signed distance and ...
Definition shape.h:53
virtual Point< dim > get_position()
Returns the position of the shape.
Definition shape.h:1187
Tensor< 2, 3 > rotation_matrix
Definition shape.h:1362
virtual std::shared_ptr< Shape< dim > > static_copy() const =0
Return a pointer to a copy of the Shape.
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
ShapeType
enum class that associate an integer index tp each type of shape. The enumerator names are the values...
Definition shape.h:61
@ cut_hollow_sphere
Definition shape.h:70
@ cylindrical_helix
Definition shape.h:69
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
static Tensor< 1, 3 > rotation_matrix_to_xyz_angles(Tensor< 2, 3 > &rotation_matrix_representation)
Function that return the XYZ rotation angles from a rotation matrix.
Definition shape.h:1321
virtual 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)
Return the distance, the center point, and the unit normal vector between the current shape and the s...
Definition shape.h:147
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
virtual double local_curvature_radius(Point< dim > p)
Return the curvature of the level set at a given point.
Definition shape.h:1077
std::unordered_map< std::string, Tensor< 1, dim > > gradient_cache
Definition shape.h:1356
virtual 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)
Return the distance, the center point, and the normal between the current shape and the shape given i...
Definition shape.h:450
enum Shape::ShapeType type
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 double local_curvature_radius_with_cell_guess(const Point< dim > &p, const typename DoFHandler< dim >::active_cell_iterator &cell)
Return the curvature of the level at a given point using the cell guess functions.
Definition shape.h:1105
virtual void set_position(const Point< dim > &new_position)
Sets a new position for the shape.
Definition shape.h:1140
virtual Tensor< 1, 3 > get_orientation()
Returns the orientation of the shape.
Definition shape.h:1197
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
virtual Point< dim > get_bounding_box_center()
Returns the bounding box position.
Definition shape.h:1227
bool part_of_a_composite
Definition shape.h:1358
static double smooth_max(const double a, const double b, const double smooth_factor=10)
Return the smoothed maximum of two variables used for shape contact calculation.
Definition shape.h:132
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
virtual Tensor< 1, dim > get_bounding_box_half_length()
Returns the bounding box half length.
Definition shape.h:1217
Point< dim > position
Definition shape.h:1348
bool bounding_box_contact(Shape< dim > &shape)
Check if the bounding box of this shape and another one are overlapping. This function uses the Separ...
Definition shape.h:741
virtual void set_orientation(const Tensor< 1, 3 > &new_orientation)
Sets a new orientation for the shape.
Definition shape.h:1153
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 Tensor< 2, 3 > get_rotation_matrix()
Returns the orientation of the shape.
Definition shape.h:1207
std::string additional_info_on_shape
Definition shape.h:1338
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
void set_part_of_a_composite(const bool is_part_of_a_composite)
Defines if this shape is part of a composite. If true, cache management is deactivated and delegated ...
Definition shape.h:1294
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
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
Superquadric(const Tensor< 1, dim > half_lengths, const Tensor< 1, dim > exponents, const double epsilon, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructor for a superquadric shape.
Definition shape.h:1580
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
static double sign(const double prm)
Return the sign of the parameter. To be removed once PR#794 is merged.
Definition shape.h:1676
Tensor< 1, dim > half_lengths
Definition shape.h:1725
Torus(double ring_radius, double ring_thickness, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
Constructs a torus with a given thickness and radius.
Definition shape.h:1845
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
Point< 2 > point_nd_to_2d(const Point< dim > &point)
Tensor< 1, 2 > tensor_nd_to_2d(const Tensor< 1, dim > &tensor)
Point< 3 > point_nd_to_3d(const Point< dim > &point)