include/meltpooldg/utilities/amr_regions.hpp Source File

Developer Documentation: include/meltpooldg/utilities/amr_regions.hpp Source File
Developer Documentation
amr_regions.hpp
Go to the documentation of this file.
1
16#pragma once
17
18#include <deal.II/base/exceptions.h>
19#include <deal.II/base/point.h>
20#include <deal.II/base/tensor.h>
21
22#include <deal.II/grid/tria.h>
23
24#include <boost/serialization/access.hpp>
25
26#include <cmath>
27#include <memory>
28#include <utility>
29#include <vector>
30
31namespace MeltPoolDG::AMR
32{
47 template <int dim, typename number>
49 {
50 public:
56 template <typename T>
58 : region(std::make_unique<Model<T>>(std::forward<T>(region)))
59 {}
60
67 bool
68 point_inside(const dealii::Point<dim, number> &p) const
69 {
70 return region->point_inside(p);
71 }
72
73 private:
79 struct Concept
80 {
81 virtual ~Concept() = default;
82
83 virtual bool
84 point_inside(const dealii::Point<dim, number> &p) const = 0;
85 };
86
96 template <typename T>
97 struct Model : public Concept
98 {
99 Model(const T &t)
100 : model(t)
101 {}
102
103 bool
104 point_inside(const dealii::Point<dim, number> &p) const override
105 {
106 return model.point_inside(p);
107 }
108
109 private:
112 };
113
116 std::unique_ptr<Concept> region;
117 };
118
122 template <int dim, typename number>
124 {
125 public:
134 SphereAMRRegion(const dealii::Point<dim, number> &center, const number radius)
135 : center(center)
136 , radius(radius)
137 {
138 AssertThrow(radius > 0, dealii::ExcMessage("The radius cannot be negative."));
139 }
140
147 bool
148 point_inside(const dealii::Point<dim, number> &p) const
149 {
150 return p.distance(center) <= radius;
151 }
152
153 private:
154 const dealii::Point<dim, number> center;
155 const number radius;
156 };
157
161 template <int dim, typename number>
163 {
164 public:
172
182 SphericalShellAMRRegion(const dealii::Point<dim, number> &center,
183 const number inner_radius,
184 const number outer_radius)
185 : center(center)
188 {
189 AssertThrow(inner_radius >= 0, dealii::ExcMessage("The radius cannot be negative."));
190 AssertThrow(inner_radius < outer_radius,
191 dealii::ExcMessage("The inner radius must be smaller than the outer radius"));
192 }
193
200 bool
201 point_inside(const dealii::Point<dim, number> &p) const
202 {
203 const number distance = p.distance(center);
204 return (distance <= outer_radius and distance >= inner_radius);
205 }
206
213 template <class Archive>
214 void
215 serialize(Archive &ar, const unsigned int /*version*/)
216 {
217 ar &center;
218 ar &inner_radius;
219 ar &outer_radius;
220 }
221
222 private:
223 dealii::Point<dim, number> center = dealii::Point<dim, number>();
224 number inner_radius = 0.;
225 number outer_radius = 0.;
226 };
227
228
245 template <int dim, typename number>
246 bool
247 set_refinement_flags_in_regions(dealii::Triangulation<dim> &tria,
248 const std::vector<AMRRegion<dim, number>> &regions = {},
249 bool do_coarsening = false)
250 {
251 if (regions.empty())
252 return false;
253
254 bool any_flag_set = false;
255 for (auto &cell : tria.active_cell_iterators())
256 if (not cell->refine_flag_set())
257 for (const auto &region : regions)
258 {
259 if (region.point_inside(cell->center()))
260 {
261 cell->clear_coarsen_flag();
262 cell->set_refine_flag();
263 any_flag_set = true;
264 break;
265 }
266 else if (do_coarsening)
267 {
268 cell->set_coarsen_flag();
269 any_flag_set = true;
270 }
271 }
272
273 return any_flag_set;
274 }
275} // namespace MeltPoolDG::AMR
Type-erased wrapper representing a geometric region for adaptive mesh refinement.
Definition amr_regions.hpp:49
std::unique_ptr< Concept > region
Definition amr_regions.hpp:116
bool point_inside(const dealii::Point< dim, number > &p) const
Checks whether a given point lies inside this region.
Definition amr_regions.hpp:68
AMRRegion(T &&region)
Constructs an AMRRegion from a user-defined region object.
Definition amr_regions.hpp:57
Represents a spherical region for adaptive mesh refinement.
Definition amr_regions.hpp:124
SphereAMRRegion(const dealii::Point< dim, number > &center, const number radius)
Construct a spherical AMR region.
Definition amr_regions.hpp:134
bool point_inside(const dealii::Point< dim, number > &p) const
Check whether a point lies inside the sphere.
Definition amr_regions.hpp:148
const dealii::Point< dim, number > center
Definition amr_regions.hpp:154
const number radius
Definition amr_regions.hpp:155
Represents a spherical shell region for adaptive mesh refinement (AMR).
Definition amr_regions.hpp:163
void serialize(Archive &ar, const unsigned int)
Serialize or deserialize the object's data members.
Definition amr_regions.hpp:215
number outer_radius
Definition amr_regions.hpp:225
SphericalShellAMRRegion()=default
Default constructor (required for boost serialization). Sets all members to 0.
dealii::Point< dim, number > center
Definition amr_regions.hpp:223
bool point_inside(const dealii::Point< dim, number > &p) const
Check whether a point lies inside the spherical shell.
Definition amr_regions.hpp:201
number inner_radius
Definition amr_regions.hpp:224
SphericalShellAMRRegion(const dealii::Point< dim, number > &center, const number inner_radius, const number outer_radius)
Construct a spherical shell AMR region.
Definition amr_regions.hpp:182
Definition amr.hpp:15
bool set_refinement_flags_in_regions(dealii::Triangulation< dim > &tria, const std::vector< AMRRegion< dim, number > > &regions={}, bool do_coarsening=false)
Marks cells in a triangulation for refinement if their centers fall inside specified regions.
Definition amr_regions.hpp:247
Abstract base interface for all region types.
Definition amr_regions.hpp:80
virtual bool point_inside(const dealii::Point< dim, number > &p) const =0
Concrete wrapper around a user-defined region type.
Definition amr_regions.hpp:98
bool point_inside(const dealii::Point< dim, number > &p) const override
Definition amr_regions.hpp:104
T model
The actual object for describing the behavior of the region.
Definition amr_regions.hpp:111
Model(const T &t)
Definition amr_regions.hpp:99