Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
ib_stencil.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2021-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/ib_stencil.h>
5
6#include <deal.II/lac/full_matrix.h>
7#include <deal.II/lac/vector.h>
8
9template <int dim>
10unsigned int
12 const unsigned int degree)
13{
14 // The number of points used in the stencil excluding the DOF is equal to the
15 // degree.
16 unsigned int nb_points = degree;
17
18 // In the case where the cell is used directly to find the solution at
19 // the IB only one point is needed.
20 if (degree > 4)
21 nb_points = 1;
22 return nb_points;
23}
24
25template <int dim>
26void
27IBStencil<dim>::p_base(const unsigned int degree)
28{
29 AssertThrow(degree > 0,
30 ExcMessage("IB stencil degree must be greater than 0."));
31 using numbers::PI;
32 // Define the sampling point position of the stencil on the reference 1D
33 // stencil 0 to 1, 1 being the position of the DOF.
34 reference_points.resize(degree + 1);
35 for (unsigned int i = 0; i < degree + 1; ++i)
36 {
37 reference_points[i] = 0.5 * (1 - cos(PI * i / degree));
38 }
39}
40
41template <int dim>
42std::vector<double>
43IBStencil<dim>::coefficients(const unsigned int degree,
44 const double length_ratio)
45{
46 p_base(degree);
47 // Initialize the coefficient vector
48 std::vector<double> coef(degree + 1);
49
50 FullMatrix<double> vandermonde(degree + 1, degree + 1);
51 FullMatrix<double> stencil(degree + 1, degree + 1);
52 FullMatrix<double> inv_vandermonde(degree + 1, degree + 1);
53
54 Vector<double> rhs(degree + 1);
55 // Define the Vandermonde matrix
56 for (unsigned int i = 0; i < degree + 1; ++i)
57 {
58 for (unsigned int j = 0; j < degree + 1; ++j)
59 {
60 vandermonde[i][j] = std::pow(reference_points[i], j);
61 }
62 rhs[i] = std::pow(1 + length_ratio, i);
63 }
64 // Invert the Vandermonde matrix
65 inv_vandermonde.invert(vandermonde);
66
67 // Multiply each line of the inverted matrix by (1+length_ratio)^i
68 for (unsigned int i = 0; i < degree + 1; ++i)
69 {
70 for (unsigned int j = 0; j < degree + 1; ++j)
71 {
72 stencil[i][j] = inv_vandermonde[i][j] * rhs[i];
73 }
74 }
75 // Sum the columns to get the coefficients.
76 for (unsigned int i = 0; i < degree + 1; ++i)
77 {
78 for (unsigned int j = 0; j < degree + 1; ++j)
79 {
80 coef[degree - i] += stencil[j][i];
81 }
82 }
83
84 // Fill the coefficient vector based on the degree.
85 if (degree > 4)
86 {
87 // In this case the cell is directly used to find the solution at the IB
88 // position. In this case only one point is needed (the position of the
89 // point on the IB) and its coefficient is 1.
90 coef.resize(1);
91 coef[0] = 1;
92 }
93
94 return coef;
95}
96
97template <int dim>
98std::tuple<Point<dim>, std::vector<Point<dim>>>
100 const unsigned int degree,
101 const double length_ratio,
103 const Point<dim> &dof_point,
104 const typename DoFHandler<dim>::active_cell_iterator &cell_guess)
105{
106 // Create the vector of points used for the stencil based on the degree of the
107 // stencil. Also return the DOF position or the position of the point on the
108 // IB depending on if the cell is used directly.
109 p_base(degree);
110 Point<dim> point;
111 Point<dim> surface_point;
112 p.closest_surface_point(dof_point, surface_point, cell_guess);
113 std::vector<Point<dim>> interpolation_points;
114
115 if (degree > 4)
116 {
117 // In this case the cell is directly used to find the solution at the IB
118 // position. In this case only one point is needed (the position of the
119 // point on the IB).
120 Tensor<1, dim, double> vect_ib = dof_point - surface_point;
121 point = surface_point;
122
123 Point<dim, double> interpolation_point_1(dof_point +
124 vect_ib * 1. / length_ratio);
125
126 interpolation_points.resize(1);
127 interpolation_points[0] = interpolation_point_1;
128 }
129 else
130 {
131 interpolation_points.resize(degree);
132 Tensor<1, dim, double> vect_ib = dof_point - surface_point;
133 point = dof_point;
134 for (unsigned int i = 1; i < degree + 1; ++i)
135 {
136 interpolation_points[i - 1] =
137 dof_point +
138 vect_ib * (1 - reference_points[degree - i]) / (length_ratio);
139 }
140 }
141 return {point, interpolation_points};
142}
143
144template <int dim>
145std::tuple<Point<dim>, std::vector<Point<dim>>>
147 const double length_ratio,
149 const Point<dim> &dof_point)
150{
151 // Create the vector of points used for the stencil based on the degree of the
152 // stencil. Also return the DOF position or the position of the point on the
153 // IB depending on if the cell is used directly.
154 p_base(degree);
155 Point<dim> point;
156 Point<dim> surface_point;
157 p.closest_surface_point(dof_point, surface_point);
158 std::vector<Point<dim>> interpolation_points;
159
160 if (degree > 4)
161 {
162 // In this case the cell is directly used to find the solution at the IB
163 // position. In this case only one point is needed (the position of the
164 // point on the IB).
165 Tensor<1, dim, double> vect_ib = dof_point - surface_point;
166 point = surface_point;
167
168 Point<dim, double> interpolation_point_1(dof_point +
169 vect_ib * 1. / length_ratio);
170
171 interpolation_points.resize(1);
172 interpolation_points[0] = interpolation_point_1;
173 }
174 else
175 {
176 interpolation_points.resize(degree);
177 Tensor<1, dim, double> vect_ib = dof_point - surface_point;
178 point = dof_point;
179 for (unsigned int i = 1; i < degree + 1; ++i)
180 {
181 interpolation_points[i - 1] =
182 dof_point +
183 vect_ib * (1 - reference_points[degree - i]) / (length_ratio);
184 }
185 }
186 return {point, interpolation_points};
187}
188
189template <int dim>
190Point<dim>
193 const Point<dim> &dof_point,
194 const typename DoFHandler<dim>::active_cell_iterator &cell_guess)
195{
196 // Return the DOF position or the position of the point on the
197 // IB depending on if the cell is used directly.
198
199 Point<dim> surface_point;
200 p.closest_surface_point(dof_point, surface_point, cell_guess);
201 Tensor<1, dim, double> vect_ib = dof_point - surface_point;
202 Point<dim> point = dof_point + vect_ib / 16.;
203
204 return point;
205}
206
207template <int dim>
208Point<dim>
210 const Point<dim> &dof_point)
211{
212 // Create the vector of points used for the stencil based on the degree of the
213 // stencil. Also return the DOF position or the position of the point on the
214 // IB depending on if the cell is used directly
215
216 Point<dim> surface_point;
217 p.closest_surface_point(dof_point, surface_point);
218 Tensor<1, dim, double> vect_ib = dof_point - surface_point;
219 Point<dim> point = dof_point + vect_ib / 16.;
220
221 return point;
222}
223
224template <int dim>
225double
228 const Point<dim> &dof_point,
229 unsigned int component,
230 const typename DoFHandler<dim>::active_cell_iterator &cell_guess)
231{
232 double immersed_boundary_velocity = 0;
233
234 // Return the value of the IB condition for that specific stencil.
235 Tensor<1, 3, double> radial_vector;
236 Point<dim> closest_point;
237 p.closest_surface_point(dof_point, closest_point, cell_guess);
238 Point<dim> position_to_surface;
239 position_to_surface = closest_point - p.position;
240 radial_vector[0] = position_to_surface[0];
241 radial_vector[1] = position_to_surface[1];
242 // We have to do that conversion as there is no proper conversion from
243 // tensor of dim 2 to 3.
244 if constexpr (dim == 3)
245 radial_vector[2] = position_to_surface[2];
246
247 if constexpr (dim == 2)
248 {
249 Tensor<1, 3> relative_tangential_velocity =
250 cross_product_3d(p.omega, radial_vector);
251 if (component == 0)
252 {
253 // vx in 2D
254 immersed_boundary_velocity =
255 relative_tangential_velocity[0] + p.velocity[0];
256 }
257 if (component == 1)
258 {
259 // vy in 2D
260 immersed_boundary_velocity =
261 relative_tangential_velocity[1] + p.velocity[1];
262 }
263 }
264 if constexpr (dim == 3)
265 {
266 Tensor<1, 3> relative_tangential_velocity =
267 cross_product_3d(p.omega, radial_vector);
268 if (component == 0)
269 {
270 // vx in 3D
271 immersed_boundary_velocity =
272 relative_tangential_velocity[0] + p.velocity[0];
273 }
274 if (component == 1)
275 {
276 // vy in 3D
277 immersed_boundary_velocity =
278 relative_tangential_velocity[1] + p.velocity[1];
279 }
280 if (component == 2)
281 {
282 // vz in 3D
283 immersed_boundary_velocity =
284 relative_tangential_velocity[2] + p.velocity[2];
285 }
286 }
287
288 return immersed_boundary_velocity;
289}
290
291template <int dim>
292double
294 const Point<dim> &dof_point,
295 unsigned int component)
296{
297 double immersed_boundary_velocity = 0;
298
299 // Return the value of the IB condition for that specific stencil.
300 Tensor<1, 3, double> radial_vector;
301 Point<dim> closest_point;
302 p.closest_surface_point(dof_point, closest_point);
303 Point<dim> position_to_surface;
304 position_to_surface = closest_point - p.position;
305 radial_vector[0] = position_to_surface[0];
306 radial_vector[1] = position_to_surface[1];
307 // We have to do that conversion as there is no proper conversion from
308 // tensor of dim 2 to 3.
309 if constexpr (dim == 3)
310 radial_vector[2] = position_to_surface[2];
311
312 if constexpr (dim == 2)
313 {
314 Tensor<1, 3> relative_tangential_velocity =
315 cross_product_3d(p.omega, radial_vector);
316 if (component == 0)
317 {
318 // vx in 2D
319 immersed_boundary_velocity =
320 relative_tangential_velocity[0] + p.velocity[0];
321 }
322 if (component == 1)
323 {
324 // vy in 2D
325 immersed_boundary_velocity =
326 relative_tangential_velocity[1] + p.velocity[1];
327 }
328 }
329 if constexpr (dim == 3)
330 {
331 Tensor<1, 3> relative_tangential_velocity =
332 cross_product_3d(p.omega, radial_vector);
333 if (component == 0)
334 {
335 // vx in 3D
336 immersed_boundary_velocity =
337 relative_tangential_velocity[0] + p.velocity[0];
338 }
339 if (component == 1)
340 {
341 // vy in 3D
342 immersed_boundary_velocity =
343 relative_tangential_velocity[1] + p.velocity[1];
344 }
345 if (component == 2)
346 {
347 // vz in 3D
348 immersed_boundary_velocity =
349 relative_tangential_velocity[2] + p.velocity[2];
350 }
351 }
352
353 return immersed_boundary_velocity;
354}
355
356template class IBStencil<2>;
357template class IBStencil<3>;
Define the behavior of a specific particle for the sharp interface IB solver.
Definition ib_particle.h:26
Tensor< 1, 3 > omega
Angular velocity.
Tensor< 1, 3 > velocity
Translational velocity.
void closest_surface_point(const Point< dim > &p, Point< dim > &closest_point, const typename DoFHandler< dim >::active_cell_iterator &cell_guess)
Find the closest surface point on the shape.
Point< dim > position
Position, which corresponds to the centroid for most implemented shapes.
Define where the sharp immersed-boundaries are imposed on Eulerian grid.
Definition ib_stencil.h:21
void p_base(const unsigned int degree)
Return the points on the reference 1D element for the polynomial base.
Definition ib_stencil.cc:27
virtual std::tuple< Point< dim >, std::vector< Point< dim > > > support_points_for_interpolation(const unsigned int degree, const double length_ratio, IBParticle< dim > &p, const Point< dim > &dof_point, const typename DoFHandler< dim >::active_cell_iterator &cell_guess)
Find the DOF at which we try to apply the IB by interpolation and the location of the support points ...
Definition ib_stencil.cc:99
virtual double ib_velocity(IBParticle< dim > &p, const Point< dim > &dof_point, const unsigned int component, const typename DoFHandler< dim >::active_cell_iterator &cell_guess)
Return the velocity of the IB used in the RHS of the equation.
virtual std::vector< double > coefficients(const unsigned int degree, const double length_ratio)
Return the coefficients (weights) of the stencil.
Definition ib_stencil.cc:43
virtual Point< dim > point_for_cell_detection(IBParticle< dim > &p, const Point< dim > &dof_point, const typename DoFHandler< dim >::active_cell_iterator &cell_guess)
Define the point used to define the cell used for the stencil calculation.
virtual unsigned int number_of_interpolation_support_points(const unsigned int degree)
Return the number of points used by the interpolation stencil (excluding the dof itself) for a stenci...
Definition ib_stencil.cc:11
std::vector< double > reference_points
Points of the reference 1D stencil.
Definition ib_stencil.h:153