Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
isothermal_compressible_navier_stokes_assembler.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2023-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
10
11template <int dim>
12void
14 const NavierStokesScratchData<dim> &scratch_data,
16{
17 // Physical properties
18 const std::vector<double> &density_vector = scratch_data.density;
19 const std::vector<double> &dynamic_viscosity_vector =
20 scratch_data.dynamic_viscosity;
21 const double density_psi = scratch_data.density_psi;
22
23 const std::vector<double> &viscosity_for_stabilization_vector =
25
26 // Loop and quadrature information
27 const auto &JxW_vec = scratch_data.JxW;
28 const unsigned int n_q_points = scratch_data.n_q_points;
29 const unsigned int n_dofs = scratch_data.n_dofs;
30 const double h = scratch_data.cell_size;
31
32 // Copy data elements
33 auto &strong_residual_vec = copy_data.strong_residual;
34 auto &strong_jacobian_vec = copy_data.strong_jacobian;
35 auto &local_matrix = copy_data.local_matrix;
36
37 // Time steps and inverse time steps which is used for stabilization constant
38 std::vector<double> time_steps_vector =
39 this->simulation_control->get_time_steps_vector();
40 const double dt = time_steps_vector[0];
41 const double sdt = 1. / dt;
42
43 // The pressure scaling factor is not applied, since SimulationParameters
44 // rejects any factor other than 1 when the density is not constant.
45
46 // Loop over the quadrature points
47 for (unsigned int q = 0; q < n_q_points; ++q)
48 {
49 // Gather into local variables the relevant fields
50 const double density = density_vector[q];
51 const double dynamic_viscosity = dynamic_viscosity_vector[q];
52 const double dynamic_viscosity_for_stabilization =
53 viscosity_for_stabilization_vector[q];
54
55
56 const Tensor<1, dim> &velocity = scratch_data.velocity_values[q];
57 const Tensor<2, dim> &velocity_gradient =
58 scratch_data.velocity_gradients[q];
59 const Tensor<1, dim> &velocity_laplacian =
60 scratch_data.velocity_laplacians[q];
61
62 const Tensor<1, dim> &pressure_gradient =
63 scratch_data.pressure_gradients[q];
64
65 // Forcing term
66 const Tensor<1, dim> &force = scratch_data.force[q];
67 double mass_source = scratch_data.mass_source[q];
68
69 // Calculation of the magnitude of the velocity for the
70 // stabilization parameter
71 const double u_mag = std::max(velocity.norm(), 1e-12);
72
73 // Store JxW in local variable for faster access;
74 const double JxW = JxW_vec[q];
75
76 // Calculation of the GLS stabilization parameter. The
77 // stabilization parameter used is different if the simulation
78 // is steady or unsteady. In the unsteady case it includes the
79 // value of the time step
80 const double tau =
81 this->simulation_control->get_assembly_method() ==
84 u_mag, dynamic_viscosity_for_stabilization / density, h) :
86 u_mag, dynamic_viscosity_for_stabilization / density, h, sdt);
87
88 // Calculate the strong residual for GLS stabilization
89 auto strong_residual = density * velocity_gradient * velocity +
90 pressure_gradient -
91 dynamic_viscosity * velocity_laplacian - force +
92 mass_source * velocity + strong_residual_vec[q];
93
94 std::vector<Tensor<1, dim>> grad_phi_u_j_x_velocity(n_dofs);
95 std::vector<Tensor<1, dim>> velocity_gradient_x_phi_u_j(n_dofs);
96
97 // We loop over the column first to prevent recalculation
98 // of the strong jacobian in the inner loop
99 for (unsigned int j = 0; j < n_dofs; ++j)
100 {
101 const auto &phi_u_j = scratch_data.phi_u[q][j];
102 const auto &grad_phi_u_j = scratch_data.grad_phi_u[q][j];
103 const auto &laplacian_phi_u_j = scratch_data.laplacian_phi_u[q][j];
104 const auto &grad_phi_p_j = scratch_data.grad_phi_p[q][j];
105
106 strong_jacobian_vec[q][j] +=
107 (density * velocity_gradient * phi_u_j +
108 density * grad_phi_u_j * velocity + grad_phi_p_j -
109 dynamic_viscosity * laplacian_phi_u_j + mass_source * phi_u_j);
110
111 // Store these temporary products in auxiliary variables for speed
112 grad_phi_u_j_x_velocity[j] = grad_phi_u_j * velocity;
113 velocity_gradient_x_phi_u_j[j] = velocity_gradient * phi_u_j;
114 }
115
116 for (unsigned int i = 0; i < n_dofs; ++i)
117 {
118 const int component_i = scratch_data.components[i];
119
120 const auto &phi_u_i = scratch_data.phi_u[q][i];
121 const auto &grad_phi_u_i = scratch_data.grad_phi_u[q][i];
122 const auto &div_phi_u_i = scratch_data.div_phi_u[q][i];
123 const auto &phi_p_i = scratch_data.phi_p[q][i];
124 const auto &grad_phi_p_i = scratch_data.grad_phi_p[q][i];
125
126 // Store these temporary products in auxiliary variables for speed
127 const auto grad_phi_u_i_x_velocity = grad_phi_u_i * velocity;
128 const auto strong_residual_x_grad_phi_u_i =
129 strong_residual * grad_phi_u_i;
130
131 for (unsigned int j = 0; j < n_dofs; ++j)
132 {
133 const int component_j = scratch_data.components[j];
134
135 const auto &phi_u_j = scratch_data.phi_u[q][j];
136 const auto &phi_p_j = scratch_data.phi_p[q][j];
137 const auto &grad_phi_p_j = scratch_data.grad_phi_p[q][j];
138
139 const auto &strong_jac = strong_jacobian_vec[q][j];
140
141 // $$\nabla p$$ from the momentum equation
142 double local_matrix_ij =
143 component_j == dim ? -div_phi_u_i * phi_p_j : 0;
144
145 // continuity equation
146 if (component_i == dim)
147 {
148 const auto &div_phi_u_j = scratch_data.div_phi_u[q][j];
149
150 local_matrix_ij +=
151 // $$\psi \mathbf{u} \nabla p$$
152 phi_p_i * density_psi * phi_u_j * pressure_gradient +
153 phi_p_i * density_psi * velocity * grad_phi_p_j +
154
155 // $$\rho \nabla \cdot \mathbf{u}$$
156 phi_p_i * density * div_phi_u_j;
157
158 // PSPG GLS term
159 local_matrix_ij +=
160 tau / density * (strong_jac * grad_phi_p_i);
161 }
162
163 // momentum equation
164 if (component_i < dim && component_j < dim)
165 {
166 local_matrix_ij +=
167 // $$\rho (\mathbf{u} \cdot \nabla) \mathbf{u}$$
168 phi_u_i * density * velocity_gradient_x_phi_u_j[j] +
169 phi_u_i * density * grad_phi_u_j_x_velocity[j];
170
171 if (component_i == component_j)
172 {
173 const auto &grad_phi_u_j = scratch_data.grad_phi_u[q][j];
174
175 local_matrix_ij +=
176 // weak form of $$\mu \nabla^{2} \mathbf{u}$$
177 dynamic_viscosity * (grad_phi_u_j[component_j] *
178 grad_phi_u_i[component_i]) +
179 // volume source
180 phi_u_i * mass_source * phi_u_j;
181 }
182 }
183
184 // The jacobian matrix for the SUPG formulation
185 // currently does not include the jacobian of the
186 // stabilization parameter tau. Our experience has shown that
187 // does not alter the number of newton iteration for
188 // convergence, but greatly simplifies assembly.
189 local_matrix_ij +=
190 tau * (strong_jac * (grad_phi_u_i_x_velocity) +
191 strong_residual_x_grad_phi_u_i * phi_u_j);
192
193 local_matrix_ij *= JxW;
194 local_matrix(i, j) += local_matrix_ij;
195 }
196 }
197 }
198}
199
200template <int dim>
201void
203 const NavierStokesScratchData<dim> &scratch_data,
205{
206 // Physical properties
207 const std::vector<double> &density_vector = scratch_data.density;
208 const std::vector<double> &dynamic_viscosity_vector =
209 scratch_data.dynamic_viscosity;
210
211 const std::vector<double> &viscosity_for_stabilization_vector =
213
214 const double density_psi = scratch_data.density_psi;
215
216 // Loop and quadrature information
217 const auto &JxW_vec = scratch_data.JxW;
218 const unsigned int n_q_points = scratch_data.n_q_points;
219 const unsigned int n_dofs = scratch_data.n_dofs;
220 const double h = scratch_data.cell_size;
221
222 // Copy data elements
223 auto &strong_residual_vec = copy_data.strong_residual;
224 auto &local_rhs = copy_data.local_rhs;
225
226 // Time steps and inverse time steps which is used for stabilization constant
227 std::vector<double> time_steps_vector =
228 this->simulation_control->get_time_steps_vector();
229 const double dt = time_steps_vector[0];
230 const double sdt = 1. / dt;
231
232 // Loop over the quadrature points
233 for (unsigned int q = 0; q < n_q_points; ++q)
234 {
235 // Physical properties
236 const double density = density_vector[q];
237 const double dynamic_viscosity = dynamic_viscosity_vector[q];
238
239 // Velocity
240 const Tensor<1, dim> &velocity = scratch_data.velocity_values[q];
241 const double velocity_divergence = scratch_data.velocity_divergences[q];
242 const Tensor<2, dim> &velocity_gradient =
243 scratch_data.velocity_gradients[q];
244 const Tensor<1, dim> &velocity_laplacian =
245 scratch_data.velocity_laplacians[q];
246
247 // Pressure
248 const double pressure = scratch_data.pressure_values[q];
249 const Tensor<1, dim> &pressure_gradient =
250 scratch_data.pressure_gradients[q];
251
252 // Forcing term
253 const Tensor<1, dim> &force = scratch_data.force[q];
254 double mass_source = scratch_data.mass_source[q];
255 // Calculation of the magnitude of the
256 // velocity for the stabilization parameter
257 const double u_mag = std::max(velocity.norm(), 1e-12);
258
259 // Store JxW in local variable for faster access;
260 const double JxW = JxW_vec[q];
261
262 // Calculation of the GLS stabilization parameter. The
263 // stabilization parameter used is different if the simulation
264 // is steady or unsteady. In the unsteady case it includes the
265 // value of the time step
266 const double tau =
267 this->simulation_control->get_assembly_method() ==
270 u_mag, viscosity_for_stabilization_vector[q] / density, h) :
272 u_mag, viscosity_for_stabilization_vector[q] / density, h, sdt);
273
274 // Calculate the strong residual for GLS stabilization
275 auto strong_residual = density * velocity_gradient * velocity +
276 pressure_gradient -
277 dynamic_viscosity * velocity_laplacian - force +
278 mass_source * velocity + strong_residual_vec[q];
279
280 // Assembly of the right-hand side
281 for (unsigned int i = 0; i < n_dofs; ++i)
282 {
283 const auto &phi_u_i = scratch_data.phi_u[q][i];
284 const auto &grad_phi_u_i = scratch_data.grad_phi_u[q][i];
285 const auto &phi_p_i = scratch_data.phi_p[q][i];
286 const auto &grad_phi_p_i = scratch_data.grad_phi_p[q][i];
287 const auto &div_phi_u_i = scratch_data.div_phi_u[q][i];
288
289 double local_rhs_i = 0;
290
291 // Navier-Stokes Residual
292 local_rhs_i +=
293 (
294 // Continuity
295 -phi_p_i * density_psi * velocity * pressure_gradient -
296 phi_p_i * density * velocity_divergence - phi_p_i * mass_source -
297 // Momentum
298 phi_u_i * density * velocity_gradient * velocity +
299 div_phi_u_i * pressure -
300 dynamic_viscosity *
301 scalar_product(velocity_gradient, grad_phi_u_i) +
302 phi_u_i * force - mass_source * velocity * phi_u_i) *
303 JxW;
304
305 // PSPG GLS term
306 local_rhs_i +=
307 -tau / density * (strong_residual * grad_phi_p_i) * JxW;
308
309 // SUPG GLS term
310 local_rhs_i +=
311 -tau * (strong_residual * (grad_phi_u_i * velocity)) * JxW;
312
313 local_rhs(i) += local_rhs_i;
314 }
315 }
316}
317
320
321template <int dim>
322void
324 const NavierStokesScratchData<dim> &scratch_data,
326{
327 // Physical properties
328 const std::vector<double> &density_vector = scratch_data.density;
329 const double density_psi = scratch_data.density_psi;
330
331 // Loop and quadrature information
332 const auto &JxW = scratch_data.JxW;
333 const unsigned int n_q_points = scratch_data.n_q_points;
334 const unsigned int n_dofs = scratch_data.n_dofs;
335
336 // Copy data elements
337 auto &strong_residual = copy_data.strong_residual;
338 auto &strong_jacobian = copy_data.strong_jacobian;
339 auto &local_matrix = copy_data.local_matrix;
340
341 // Time stepping information
342 const auto method = this->simulation_control->get_assembly_method();
343 // Vector for the BDF coefficients
344 const Vector<double> &bdf_coefs =
345 this->simulation_control->get_bdf_coefficients();
346 std::vector<double> pressure(1 + number_of_previous_solutions(method));
347 std::vector<Tensor<1, dim>> velocity(1 +
349
350 // Loop over the quadrature points
351 for (unsigned int q = 0; q < n_q_points; ++q)
352 {
353 // Physical properties
354 const double density = density_vector[q];
355
356 velocity[0] = scratch_data.velocity_values[q];
357 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
358 velocity[p + 1] = scratch_data.previous_velocity_values[p][q];
359
360 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
361 ++p)
362 strong_residual[q] += bdf_coefs[p] * density * velocity[p];
363
364 for (unsigned int j = 0; j < n_dofs; ++j)
365 strong_jacobian[q][j] +=
366 bdf_coefs[0] * density * scratch_data.phi_u[q][j];
367
368 for (unsigned int i = 0; i < n_dofs; ++i)
369 {
370 const double phi_p_i = scratch_data.phi_p[q][i];
371 const Tensor<1, dim> &phi_u_i = scratch_data.phi_u[q][i];
372 for (unsigned int j = 0; j < n_dofs; ++j)
373 {
374 const double &phi_p_j = scratch_data.phi_p[q][j];
375 const Tensor<1, dim> &phi_u_j = scratch_data.phi_u[q][j];
376
377 local_matrix(i, j) +=
378 (phi_p_i * density_psi * phi_p_j * bdf_coefs[0] +
379 phi_u_i * density * phi_u_j * bdf_coefs[0]) *
380 JxW[q];
381 }
382 }
383 }
384}
385
386template <int dim>
387void
389 const NavierStokesScratchData<dim> &scratch_data,
391{
392 // Physical properties
393 const std::vector<double> &density_vector = scratch_data.density;
394 const double density_psi = scratch_data.density_psi;
395
396 // Loop and quadrature information
397 const auto &JxW = scratch_data.JxW;
398 const unsigned int n_q_points = scratch_data.n_q_points;
399 const unsigned int n_dofs = scratch_data.n_dofs;
400
401 // Copy data elements
402 auto &strong_residual = copy_data.strong_residual;
403 auto &local_rhs = copy_data.local_rhs;
404
405 // Time stepping information
406 const auto method = this->simulation_control->get_assembly_method();
407 // Vector for the BDF coefficients
408 const Vector<double> &bdf_coefs =
409 this->simulation_control->get_bdf_coefficients();
410 std::vector<double> pressure(1 + number_of_previous_solutions(method));
411 std::vector<Tensor<1, dim>> velocity(1 +
413
414 // Loop over the quadrature points
415 for (unsigned int q = 0; q < n_q_points; ++q)
416 {
417 // Physical properties
418 const double density = density_vector[q];
419
420 pressure[0] = scratch_data.pressure_values[q];
421 velocity[0] = scratch_data.velocity_values[q];
422 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
423 {
424 pressure[p + 1] = scratch_data.previous_pressure_values[p][q];
425 velocity[p + 1] = scratch_data.previous_velocity_values[p][q];
426 }
427
428 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
429 ++p)
430 strong_residual[q] += (bdf_coefs[p] * density * velocity[p]);
431
432
433 for (unsigned int i = 0; i < n_dofs; ++i)
434 {
435 const double phi_p_i = scratch_data.phi_p[q][i];
436 const Tensor<1, dim> &phi_u_i = scratch_data.phi_u[q][i];
437 double local_rhs_i = 0;
438 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
439 ++p)
440 {
441 local_rhs_i -=
442 bdf_coefs[p] * (phi_p_i * density_psi * pressure[p] +
443 phi_u_i * density * velocity[p]);
444 }
445 local_rhs(i) += local_rhs_i * JxW[q];
446 }
447 }
448}
449
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
Class that assembles the transient time arising from BDF time integration for the isothermal compress...
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 assembles the core of the isothermal compressible Navier-Stokes equations....
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< std::vector< Tensor< 1, dim > > > previous_velocity_values
std::vector< std::vector< double > > previous_pressure_values
std::vector< unsigned int > components
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.