Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
isothermal_compressible_navier_stokes_cls_assembler.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2023-2024, 2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/bdf.h>
6
9
10template <int dim>
11void
13 const NavierStokesScratchData<dim> &scratch_data,
15{
16 const std::vector<double> &viscosity_for_stabilization_vector =
18
19 // Loop and quadrature information
20 const auto &JxW_vec = scratch_data.JxW;
21 const unsigned int n_q_points = scratch_data.n_q_points;
22 const unsigned int n_dofs = scratch_data.n_dofs;
23 const double h = scratch_data.cell_size;
24
25 // Copy data elements
26 auto &strong_residual_vec = copy_data.strong_residual;
27 auto &strong_jacobian_vec = copy_data.strong_jacobian;
28 auto &local_matrix = copy_data.local_matrix;
29
30 // Time steps and inverse time steps which is used for stabilization constant
31 std::vector<double> time_steps_vector =
32 this->simulation_control->get_time_steps_vector();
33 const double dt = time_steps_vector[0];
34 const double sdt = 1. / dt;
35
36 // Loop over the quadrature points
37 for (unsigned int q = 0; q < n_q_points; ++q)
38 {
39 // Gather into local variables the relevant fields
40 const Tensor<1, dim> velocity = scratch_data.velocity_values[q];
41 const Tensor<2, dim> velocity_gradient =
42 scratch_data.velocity_gradients[q];
43 const Tensor<1, dim> velocity_laplacian =
44 scratch_data.velocity_laplacians[q];
45 const Tensor<3, dim> &velocity_hessian =
46 scratch_data.velocity_hessians[q];
47
48 // From hessian, calculate grad (div (u)) term needed for CLS problems
49 Tensor<1, dim> grad_div_velocity;
50 for (int d = 0; d < dim; ++d)
51 {
52 for (int k = 0; k < dim; ++k)
53 {
54 // hessian[c][i][j] is the (i,j)th component of the matrix of
55 // second derivatives of the cth vector component of the vector
56 // field at quadrature point q of the current cell
57 grad_div_velocity[d] += velocity_hessian[k][d][k];
58 }
59 }
60
61 const Tensor<1, dim> pressure_gradient =
62 scratch_data.pressure_gradients[q];
63
64 // Forcing term
65 Tensor<1, dim> force = scratch_data.force[q];
66
67 // Calculation of the magnitude of the velocity for the
68 // stabilization parameter
69 const double u_mag = std::max(velocity.norm(), 1e-12);
70
71 // Store JxW in local variable for faster access;
72 const double JxW = JxW_vec[q];
73
74 // Calculation of the equivalent properties at the quadrature point
75 double density_eq = scratch_data.density[q];
76 double density_0 = scratch_data.density_0[q];
77 const double dynamic_viscosity_eq = scratch_data.dynamic_viscosity[q];
78
79 const double dynamic_viscosity_for_stabilization =
80 viscosity_for_stabilization_vector[q];
81
82 double compressibility_multiplier =
83 scratch_data.compressibility_multiplier[q];
84
85 // Calculation of the GLS stabilization parameter. The
86 // stabilization parameter used is different if the simulation
87 // is steady or unsteady. In the unsteady case it includes the
88 // value of the time step
89 const double tau =
90 this->simulation_control->get_assembly_method() ==
93 u_mag, dynamic_viscosity_for_stabilization / density_eq, h) :
95 u_mag, dynamic_viscosity_for_stabilization / density_eq, h, sdt);
96
97 // Calculate the strong residual for GLS stabilization
98 auto strong_residual = density_eq * velocity_gradient * velocity +
99 pressure_gradient -
100 dynamic_viscosity_eq * velocity_laplacian -
101 dynamic_viscosity_eq * grad_div_velocity -
102 density_eq * force + strong_residual_vec[q];
103
104 std::vector<Tensor<1, dim>> grad_phi_u_j_x_velocity(n_dofs);
105 std::vector<Tensor<1, dim>> velocity_gradient_x_phi_u_j(n_dofs);
106
107 // The pressure scaling factor is not applied, since SimulationParameters
108 // rejects any factor other than 1 when the density is not constant.
109
110 // We loop over the column first to prevent recalculation
111 // of the strong jacobian in the inner loop
112 for (unsigned int j = 0; j < n_dofs; ++j)
113 {
114 const auto &phi_u_j = scratch_data.phi_u[q][j];
115 const auto &grad_phi_u_j = scratch_data.grad_phi_u[q][j];
116 const auto &laplacian_phi_u_j = scratch_data.laplacian_phi_u[q][j];
117 const auto &grad_phi_p_j = scratch_data.grad_phi_p[q][j];
118
119 strong_jacobian_vec[q][j] +=
120 (density_eq * velocity_gradient * phi_u_j +
121 density_eq * grad_phi_u_j * velocity + grad_phi_p_j -
122 dynamic_viscosity_eq * laplacian_phi_u_j);
123
124 // Store these temporary products in auxiliary variables for speed
125 grad_phi_u_j_x_velocity[j] = grad_phi_u_j * velocity;
126 velocity_gradient_x_phi_u_j[j] = velocity_gradient * phi_u_j;
127 }
128
129 for (unsigned int i = 0; i < n_dofs; ++i)
130 {
131 const auto &phi_u_i = scratch_data.phi_u[q][i];
132 const auto &grad_phi_u_i = scratch_data.grad_phi_u[q][i];
133 const auto &div_phi_u_i = scratch_data.div_phi_u[q][i];
134 const auto &phi_p_i = scratch_data.phi_p[q][i];
135 const auto &grad_phi_p_i = scratch_data.grad_phi_p[q][i];
136
137 // Store these temporary products in auxiliary variables for speed
138 const auto grad_phi_u_i_x_velocity = grad_phi_u_i * velocity;
139 const auto strong_residual_x_grad_phi_u_i =
140 strong_residual * grad_phi_u_i;
141
142 for (unsigned int j = 0; j < n_dofs; ++j)
143 {
144 const auto &phi_u_j = scratch_data.phi_u[q][j];
145 const auto &grad_phi_u_j = scratch_data.grad_phi_u[q][j];
146 const auto &div_phi_u_j = scratch_data.div_phi_u[q][j];
147 const auto &shear_rate_j = grad_phi_u_j + transpose(grad_phi_u_j);
148
149 const auto &phi_p_j = scratch_data.phi_p[q][j];
150 const auto &grad_phi_p_j = scratch_data.grad_phi_p[q][j];
151
152 const auto &strong_jac = strong_jacobian_vec[q][j];
153
154 double local_matrix_ij =
155 // Continuity
156 phi_p_i * compressibility_multiplier * phi_u_j *
157 pressure_gradient +
158 phi_p_i * compressibility_multiplier * velocity * grad_phi_p_j +
159 phi_p_i * density_0 * div_phi_u_j +
160 // Momentum
161 dynamic_viscosity_eq *
162 scalar_product(shear_rate_j, grad_phi_u_i) +
163 phi_u_i * density_eq * velocity_gradient_x_phi_u_j[j] +
164 phi_u_i * density_eq * grad_phi_u_j_x_velocity[j] -
165 div_phi_u_i * phi_p_j;
166
167 // PSPG GLS term
168 local_matrix_ij += tau / density_eq * (strong_jac * grad_phi_p_i);
169
170 // SUPG GLS term
171 local_matrix_ij +=
172 tau * (strong_jac * grad_phi_u_i_x_velocity +
173 strong_residual_x_grad_phi_u_i * phi_u_j);
174 local_matrix_ij *= JxW;
175 local_matrix(i, j) += local_matrix_ij;
176 }
177 }
178 }
179}
180
181template <int dim>
182void
184 const NavierStokesScratchData<dim> &scratch_data,
186{
187 const std::vector<double> &viscosity_for_stabilization_vector =
189
190 // Loop and quadrature information
191 const auto &JxW_vec = scratch_data.JxW;
192 const unsigned int n_q_points = scratch_data.n_q_points;
193 const unsigned int n_dofs = scratch_data.n_dofs;
194 const double h = scratch_data.cell_size;
195
196 // Copy data elements
197 auto &strong_residual_vec = copy_data.strong_residual;
198 auto &local_rhs = copy_data.local_rhs;
199
200 // Time steps and inverse time steps which is used for stabilization constant
201 std::vector<double> time_steps_vector =
202 this->simulation_control->get_time_steps_vector();
203 const double dt = time_steps_vector[0];
204 const double sdt = 1. / dt;
205
206 // Loop over the quadrature points
207 for (unsigned int q = 0; q < n_q_points; ++q)
208 {
209 // Velocity
210 const Tensor<1, dim> velocity = scratch_data.velocity_values[q];
211 const double velocity_divergence = scratch_data.velocity_divergences[q];
212 const Tensor<2, dim> velocity_gradient =
213 scratch_data.velocity_gradients[q];
214 const Tensor<1, dim> velocity_laplacian =
215 scratch_data.velocity_laplacians[q];
216
217 const Tensor<3, dim> &velocity_hessian =
218 scratch_data.velocity_hessians[q];
219 // From hessian, calculate grad (div (u)) term needed for CLS problems
220 Tensor<1, dim> grad_div_velocity;
221 for (int d = 0; d < dim; ++d)
222 {
223 for (int k = 0; k < dim; ++k)
224 {
225 // hessian[c][i][j] is the (i,j)th component of the matrix of
226 // second derivatives of the cth vector component of the vector
227 // field at quadrature point q of the current cell
228 grad_div_velocity[d] += velocity_hessian[k][d][k];
229 }
230 }
231
232 // Calculate shear rate
233 const Tensor<2, dim> shear_rate =
234 velocity_gradient + transpose(velocity_gradient);
235
236 // Pressure
237 const double pressure = scratch_data.pressure_values[q];
238 const Tensor<1, dim> pressure_gradient =
239 scratch_data.pressure_gradients[q];
240
241 // Forcing term
242 Tensor<1, dim> force = scratch_data.force[q];
243
244 // Calculation of the magnitude of the
245 // velocity for the stabilization parameter
246 const double u_mag = std::max(velocity.norm(), 1e-12);
247
248 // Store JxW in local variable for faster access;
249 const double JxW = JxW_vec[q];
250
251 // Calculation of the equivalent properties at the quadrature point
252 double density_eq = scratch_data.density[q];
253 double density_0 = scratch_data.density_0[q];
254 const double dynamic_viscosity_eq = scratch_data.dynamic_viscosity[q];
255
256 double compressibility_multiplier =
257 scratch_data.compressibility_multiplier[q];
258
259 // Calculation of the GLS stabilization parameter. The
260 // stabilization parameter used is different if the simulation
261 // is steady or unsteady. In the unsteady case it includes the
262 // value of the time step
263 const double tau =
264 this->simulation_control->get_assembly_method() ==
267 u_mag, viscosity_for_stabilization_vector[q] / density_eq, h) :
269 u_mag, viscosity_for_stabilization_vector[q] / density_eq, h, sdt);
270
271
272 // Calculate the strong residual for GLS stabilization
273 auto strong_residual = density_eq * velocity_gradient * velocity +
274 pressure_gradient -
275 dynamic_viscosity_eq * velocity_laplacian -
276 dynamic_viscosity_eq * grad_div_velocity -
277 density_eq * force + strong_residual_vec[q];
278
279 // Assembly of the right-hand side
280 for (unsigned int i = 0; i < n_dofs; ++i)
281 {
282 const auto phi_u_i = scratch_data.phi_u[q][i];
283 const auto grad_phi_u_i = scratch_data.grad_phi_u[q][i];
284 const auto phi_p_i = scratch_data.phi_p[q][i];
285 const auto grad_phi_p_i = scratch_data.grad_phi_p[q][i];
286 const auto div_phi_u_i = scratch_data.div_phi_u[q][i];
287
288 // Navier-Stokes Residual
289 local_rhs(i) +=
290 // Continuity
291 (-phi_p_i * compressibility_multiplier * velocity *
292 pressure_gradient -
293 phi_p_i * density_0 * velocity_divergence
294 // Momentum
295 - dynamic_viscosity_eq * scalar_product(shear_rate, grad_phi_u_i) -
296 density_eq * velocity_gradient * velocity * phi_u_i +
297 pressure * div_phi_u_i + density_eq * force * phi_u_i) *
298 JxW;
299
300 // PSPG GLS term
301 local_rhs(i) +=
302 -tau / density_eq * (strong_residual * grad_phi_p_i) * JxW;
303
304 // SUPG GLS term
305 local_rhs(i) +=
306 -tau * (strong_residual * (grad_phi_u_i * velocity)) * JxW;
307 }
308 }
309}
310
313
314template <int dim>
315void
317 const NavierStokesScratchData<dim> &scratch_data,
319{
320 // Loop and quadrature information
321 const auto &JxW = scratch_data.JxW;
322 const unsigned int n_q_points = scratch_data.n_q_points;
323 const unsigned int n_dofs = scratch_data.n_dofs;
324
325 // Copy data elements
326 auto &strong_residual = copy_data.strong_residual;
327 auto &strong_jacobian = copy_data.strong_jacobian;
328 auto &local_matrix = copy_data.local_matrix;
329
330 // Time stepping information
331 const auto method = this->simulation_control->get_assembly_method();
332 // Vector for the BDF coefficients
333 const Vector<double> &bdf_coefs =
334 this->simulation_control->get_bdf_coefficients();
335 std::vector<double> pressure(1 + number_of_previous_solutions(method));
336 std::vector<Tensor<1, dim>> velocity(1 +
338
339 // Loop over the quadrature points
340 for (unsigned int q = 0; q < n_q_points; ++q)
341 {
342 const double density = scratch_data.density[q];
343 const double compressibility_multiplier =
344 scratch_data.compressibility_multiplier[q];
345
346 velocity[0] = scratch_data.velocity_values[q];
347 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
348 velocity[p + 1] = scratch_data.previous_velocity_values[p][q];
349
350 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
351 ++p)
352 {
353 strong_residual[q] += density * bdf_coefs[p] * velocity[p];
354 }
355
356 for (unsigned int j = 0; j < n_dofs; ++j)
357 {
358 strong_jacobian[q][j] +=
359 density * bdf_coefs[0] * scratch_data.phi_u[q][j];
360 }
361
362 for (unsigned int i = 0; i < n_dofs; ++i)
363 {
364 const double phi_p_i = scratch_data.phi_p[q][i];
365 const Tensor<1, dim> &phi_u_i = scratch_data.phi_u[q][i];
366 for (unsigned int j = 0; j < n_dofs; ++j)
367 {
368 const double phi_p_j = scratch_data.phi_p[q][j];
369 const Tensor<1, dim> &phi_u_j = scratch_data.phi_u[q][j];
370
371 local_matrix(i, j) +=
372 (phi_p_i * compressibility_multiplier * phi_p_j +
373 phi_u_i * density * phi_u_j) *
374 bdf_coefs[0] * JxW[q];
375 }
376 }
377 }
378}
379
380template <int dim>
381void
383 const NavierStokesScratchData<dim> &scratch_data,
385{
386 // Loop and quadrature information
387 const auto &JxW = scratch_data.JxW;
388 const unsigned int n_q_points = scratch_data.n_q_points;
389 const unsigned int n_dofs = scratch_data.n_dofs;
390
391 // Copy data elements
392 auto &strong_residual = copy_data.strong_residual;
393 auto &local_rhs = copy_data.local_rhs;
394
395 // Time stepping information
396 const auto method = this->simulation_control->get_assembly_method();
397 // Vector for the BDF coefficients
398 const Vector<double> &bdf_coefs =
399 this->simulation_control->get_bdf_coefficients();
400 std::vector<double> pressure(1 + number_of_previous_solutions(method));
401 std::vector<Tensor<1, dim>> velocity(1 +
403
404 // Loop over the quadrature points
405 for (unsigned int q = 0; q < n_q_points; ++q)
406 {
407 const double density = scratch_data.density[q];
408 const double compressibility_multiplier =
409 scratch_data.compressibility_multiplier[q];
410
411 pressure[0] = scratch_data.pressure_values[q];
412 velocity[0] = scratch_data.velocity_values[q];
413 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
414 {
415 pressure[p + 1] = scratch_data.previous_pressure_values[p][q];
416 velocity[p + 1] = scratch_data.previous_velocity_values[p][q];
417 }
418
419 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
420 ++p)
421 {
422 strong_residual[q] += (density * bdf_coefs[p] * velocity[p]);
423 }
424
425 for (unsigned int i = 0; i < n_dofs; ++i)
426 {
427 const double phi_p_i = scratch_data.phi_p[q][i];
428 const auto phi_u_i = scratch_data.phi_u[q][i];
429 double local_rhs_i = 0;
430 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
431 ++p)
432 {
433 local_rhs_i -=
434 bdf_coefs[p] *
435 (phi_p_i * compressibility_multiplier * pressure[p] +
436 phi_u_i * density * velocity[p]);
437 }
438 local_rhs(i) += local_rhs_i * JxW[q];
439 }
440 }
441}
442
unsigned int number_of_previous_solutions(const Parameters::SimulationControl::TimeSteppingMethod method)
Get the number of previous time steps for a specific BDF scheme.
Definition bdf.h:97
virtual void assemble_rhs(const NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data) override
assemble_rhs Assembles the rhs
virtual void assemble_matrix(const NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data) override
assemble_matrix Assembles the matrix
Class that assembles the core of the Navier-Stokes equation with free surface using CLS modeling....
virtual void assemble_matrix(const NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data) override
assemble_matrix Assembles the matrix
virtual void assemble_rhs(const NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data) override
assemble_rhs Assembles the rhs
Class that stores the information required by the assembly procedure for a Navier-Stokes equation....
std::vector< Tensor< 1, dim > > force
std::vector< Tensor< 3, dim > > velocity_hessians
std::vector< std::vector< Tensor< 1, dim > > > previous_velocity_values
std::vector< std::vector< double > > previous_pressure_values
std::vector< double > compressibility_multiplier
std::vector< Tensor< 2, dim > > velocity_gradients
Table< 2, Tensor< 1, dim > > phi_u
std::vector< Tensor< 1, dim > > velocity_values
Table< 2, Tensor< 2, dim > > grad_phi_u
Table< 2, Tensor< 1, dim > > laplacian_phi_u
std::vector< double > velocity_divergences
std::vector< double > dynamic_viscosity_for_stabilization
Values of the dynamic viscosity used in the SUPG and PSPG stabilizations.
Table< 2, Tensor< 1, dim > > grad_phi_p
std::vector< double > dynamic_viscosity
std::vector< Tensor< 1, dim > > pressure_gradients
std::vector< Tensor< 1, dim > > velocity_laplacians
Class responsible for storing the information calculated using the assembly of stabilized Tensor<1,...
Definition copy_data.h:166
FullMatrix< double > local_matrix
Definition copy_data.h:204
std::vector< Tensor< 1, dim > > strong_residual
Definition copy_data.h:207
std::vector< std::vector< Tensor< 1, dim > > > strong_jacobian
Definition copy_data.h:208
@ velocity
Velocity vector field from fluid dynamics.
@ pressure
Pressure scalar field from fluid dynamics.
Simulation control classes for managing time-stepping and iteration control in Lethe.
double calculate_navier_stokes_gls_tau_transient(const double u_mag, const double kinematic_viscosity, const double h, const double sdt)
Calculate the stabilization parameter for the transient Navier-Stokes equations.
double calculate_navier_stokes_gls_tau_steady(const double u_mag, const double kinematic_viscosity, const double h)
Calculate the stabilization parameter for the Navier-Stokes equations in steady-state.