Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
navier_stokes_cahn_hilliard_assemblers.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>
7#include <core/utilities.h>
8
11
12template <int dim>
13void
15 const NavierStokesScratchData<dim> &scratch_data,
17{
18 const std::vector<double> &viscosity_for_stabilization_vector =
20
21 // Loop and quadrature information
22 const auto &JxW_vec = scratch_data.JxW;
23 const unsigned int n_q_points = scratch_data.n_q_points;
24 const unsigned int n_dofs = scratch_data.n_dofs;
25 const double h = scratch_data.cell_size;
26
27 // Copy data elements
28 auto &strong_residual_vec = copy_data.strong_residual;
29 auto &strong_jacobian_vec = copy_data.strong_jacobian;
30 auto &local_matrix = copy_data.local_matrix;
31
32 // Time steps and inverse time steps which is used for stabilization constant
33 std::vector<double> time_steps_vector =
34 this->simulation_control->get_time_steps_vector();
35 const double dt = time_steps_vector[0];
36 const double sdt = 1. / dt;
37
38 // Equations are formulated in incompressible form (density must be constant)
39 Assert(scratch_data.properties_manager.density_is_constant(),
40 RequiresConstantDensity(
41 "GLSNavierStokesCahnHilliardAssemblerCore<dim>::assemble_matrix"));
42
43 // Loop over the quadrature points
44 for (unsigned int q = 0; q < n_q_points; ++q)
45 {
46 // Gather into local variables the relevant fields
47 const Tensor<1, dim> velocity = scratch_data.velocity_values[q];
48 const Tensor<2, dim> velocity_gradient =
49 scratch_data.velocity_gradients[q];
50 const Tensor<1, dim> velocity_laplacian =
51 scratch_data.velocity_laplacians[q];
52 const Tensor<3, dim> &velocity_hessian =
53 scratch_data.velocity_hessians[q];
54
55 // From hessian, calculate grad (div (u)) term needed for the
56 // stabilization
57 Tensor<1, dim> grad_div_velocity;
58 for (int d = 0; d < dim; ++d)
59 {
60 for (int k = 0; k < dim; ++k)
61 {
62 // hessian[c][i][j] is the (i,j)th component of the matrix of
63 // second derivatives of the cth vector component of the
64 // vector field at quadrature point q of the current cell
65 grad_div_velocity[d] += velocity_hessian[k][d][k];
66 }
67 }
68
69 const Tensor<1, dim> phase_order_gradient =
71 const double potential_value =
73
74 // Forcing term
75 Tensor<1, dim> force = scratch_data.force[q];
76
77 const Tensor<1, dim> pressure_gradient =
78 scratch_data.pressure_gradients[q];
79
80 // Calculation of the magnitude of the velocity for the
81 // stabilization parameter
82 const double u_mag = std::max(velocity.norm(), 1e-12);
83
84 // Store JxW in local variable for faster access;
85 const double JxW = JxW_vec[q];
86
87 // Calculation of the equivalent properties at the quadrature point
88 double density_eq = scratch_data.density[q];
89 const double dynamic_viscosity_eq = scratch_data.dynamic_viscosity[q];
90
91 // Calculation of the GLS stabilization parameter. The
92 // stabilization parameter used is different if the simulation
93 // is steady or unsteady. In the unsteady case it includes the
94 // value of the time step
95 const double tau =
96 this->simulation_control->get_assembly_method() ==
99 u_mag, viscosity_for_stabilization_vector[q] / density_eq, h) :
101 u_mag, viscosity_for_stabilization_vector[q] / density_eq, h, sdt);
102
103 // Calculate the strong residual for GLS stabilization
104 auto strong_residual =
105 density_eq * velocity_gradient * velocity + pressure_gradient -
106 dynamic_viscosity_eq * velocity_laplacian -
107 dynamic_viscosity_eq * grad_div_velocity - density_eq * force -
108 potential_value * phase_order_gradient + strong_residual_vec[q];
109
110 std::vector<Tensor<1, dim>> grad_phi_u_j_x_velocity(n_dofs);
111 std::vector<Tensor<1, dim>> velocity_gradient_x_phi_u_j(n_dofs);
112
113 // Pressure scaling factor
114 const double pressure_scaling_factor =
115 scratch_data.pressure_scaling_factor;
116
117 // We loop over the column first to prevent recalculation
118 // of the strong jacobian in the inner loop
119 for (unsigned int j = 0; j < n_dofs; ++j)
120 {
121 const auto &phi_u_j = scratch_data.phi_u[q][j];
122 const auto &grad_phi_u_j = scratch_data.grad_phi_u[q][j];
123 const auto &laplacian_phi_u_j = scratch_data.laplacian_phi_u[q][j];
124
125 const auto &grad_phi_p_j =
126 scratch_data.grad_phi_p[q][j] * pressure_scaling_factor;
127
128 strong_jacobian_vec[q][j] +=
129 (density_eq * velocity_gradient * phi_u_j +
130 density_eq * grad_phi_u_j * velocity + grad_phi_p_j -
131 dynamic_viscosity_eq * laplacian_phi_u_j);
132
133 // Store these temporary products in auxiliary variables for speed
134 grad_phi_u_j_x_velocity[j] = grad_phi_u_j * velocity;
135 velocity_gradient_x_phi_u_j[j] = velocity_gradient * phi_u_j;
136 }
137
138 for (unsigned int i = 0; i < n_dofs; ++i)
139 {
140 const auto &phi_u_i = scratch_data.phi_u[q][i];
141 const auto &grad_phi_u_i = scratch_data.grad_phi_u[q][i];
142 const auto &div_phi_u_i = scratch_data.div_phi_u[q][i];
143 const auto &phi_p_i = scratch_data.phi_p[q][i];
144 const auto &grad_phi_p_i = scratch_data.grad_phi_p[q][i];
145
146 // Store these temporary products in auxiliary variables for speed
147 const auto grad_phi_u_i_x_velocity = grad_phi_u_i * velocity;
148 const auto strong_residual_x_grad_phi_u_i =
149 strong_residual * grad_phi_u_i;
150
151 for (unsigned int j = 0; j < n_dofs; ++j)
152 {
153 const auto &phi_u_j = scratch_data.phi_u[q][j];
154 const auto &grad_phi_u_j = scratch_data.grad_phi_u[q][j];
155 const auto &div_phi_u_j = scratch_data.div_phi_u[q][j];
156 const auto &shear_rate_j = grad_phi_u_j + transpose(grad_phi_u_j);
157
158 const auto &phi_p_j =
159 scratch_data.phi_p[q][j] * pressure_scaling_factor;
160
161 const auto &strong_jac = strong_jacobian_vec[q][j];
162
163 double local_matrix_ij =
164 // Momentum terms
165 dynamic_viscosity_eq *
166 scalar_product(shear_rate_j, grad_phi_u_i) +
167 density_eq * velocity_gradient_x_phi_u_j[j] * phi_u_i +
168 density_eq * grad_phi_u_j_x_velocity[j] * phi_u_i -
169 div_phi_u_i * phi_p_j +
170 // Continuity terms
171 phi_p_i * div_phi_u_j;
172
173 // PSPG GLS Term
174 local_matrix_ij += tau / density_eq * (strong_jac * grad_phi_p_i);
175
176 // SUPG stabilization
177 local_matrix_ij +=
178 tau * (strong_jac * grad_phi_u_i_x_velocity +
179 strong_residual_x_grad_phi_u_i * phi_u_j);
180
181
182 local_matrix_ij *= JxW;
183 local_matrix(i, j) += local_matrix_ij;
184 }
185 }
186 }
187}
188
189
190template <int dim>
191void
193 const NavierStokesScratchData<dim> &scratch_data,
195{
196 const std::vector<double> &viscosity_for_stabilization_vector =
198
199 const double h = scratch_data.cell_size;
200
201 // Loop and quadrature information
202 const auto &JxW_vec = scratch_data.JxW;
203 const unsigned int n_q_points = scratch_data.n_q_points;
204 const unsigned int n_dofs = scratch_data.n_dofs;
205
206 // Copy data elements
207 auto &strong_residual_vec = copy_data.strong_residual;
208 auto &local_rhs = copy_data.local_rhs;
209
210 // Time steps and inverse time steps which is used for stabilization constant
211 std::vector<double> time_steps_vector =
212 this->simulation_control->get_time_steps_vector();
213 const double dt = time_steps_vector[0];
214 const double sdt = 1. / dt;
215
216 Assert(scratch_data.properties_manager.density_is_constant(),
217 RequiresConstantDensity(
218 "GLSNavierStokesCahnHilliardAssemblerCore<dim>::assemble_rhs"));
219
220 // Loop over the quadrature points
221 for (unsigned int q = 0; q < n_q_points; ++q)
222 {
223 // Gather into local variables the fields for Cahn-Hilliard terms
224 const Tensor<1, dim> phase_order_gradient =
226 const double potential_value =
228
229
230 // Gather into local variables the relevant fields for velocity
231 const Tensor<1, dim> velocity = scratch_data.velocity_values[q];
232 const double velocity_divergence = scratch_data.velocity_divergences[q];
233 const Tensor<2, dim> velocity_gradient =
234 scratch_data.velocity_gradients[q];
235 const Tensor<1, dim> velocity_laplacian =
236 scratch_data.velocity_laplacians[q];
237 const Tensor<3, dim> &velocity_hessian =
238 scratch_data.velocity_hessians[q];
239 // From hessian, calculate grad (div (u)) term needed for CH problems
240 Tensor<1, dim> grad_div_velocity;
241 for (int d = 0; d < dim; ++d)
242 {
243 for (int k = 0; k < dim; ++k)
244 {
245 // hessian[c][i][j] is the (i,j)th component of the matrix of
246 // second derivatives of the cth vector component of the vector
247 // field at quadrature point q of the current cell
248 grad_div_velocity[d] += velocity_hessian[k][d][k];
249 }
250 }
251
252 // Calculate shear rate
253 const Tensor<2, dim> shear_rate =
254 velocity_gradient + transpose(velocity_gradient);
255
256 // Pressure
257 const double pressure = scratch_data.pressure_values[q];
258 const Tensor<1, dim> pressure_gradient =
259 scratch_data.pressure_gradients[q];
260
261 // Forcing term
262 Tensor<1, dim> force = scratch_data.force[q];
263
264 // Calculation of the magnitude of the
265 // velocity for the stabilization parameter
266 const double u_mag = std::max(velocity.norm(), 1e-12);
267
268 // Store JxW in local variable for faster access;
269 const double JxW = JxW_vec[q];
270
271 // Calculation of the equivalent properties at the quadrature point
272 double density_eq = scratch_data.density[q];
273 const double dynamic_viscosity_eq = scratch_data.dynamic_viscosity[q];
274
275 // Calculation of the GLS stabilization parameter. The
276 // stabilization parameter used is different if the simulation
277 // is steady or unsteady. In the unsteady case it includes the
278 // value of the time step
279 const double tau =
280 this->simulation_control->get_assembly_method() ==
283 u_mag, viscosity_for_stabilization_vector[q] / density_eq, h) :
285 u_mag, viscosity_for_stabilization_vector[q] / density_eq, h, sdt);
286
287 // Calculate the strong residual for GLS stabilization
288
289 auto strong_residual =
290 density_eq * velocity_gradient * velocity + pressure_gradient -
291 dynamic_viscosity_eq * velocity_laplacian -
292 dynamic_viscosity_eq * grad_div_velocity - density_eq * force -
293 potential_value * phase_order_gradient + strong_residual_vec[q];
294
295 // Assembly of the right-hand side
296 for (unsigned int i = 0; i < n_dofs; ++i)
297 {
298 const auto phi_u_i = scratch_data.phi_u[q][i];
299 const auto grad_phi_u_i = scratch_data.grad_phi_u[q][i];
300 const auto phi_p_i = scratch_data.phi_p[q][i];
301 const auto grad_phi_p_i = scratch_data.grad_phi_p[q][i];
302 const auto div_phi_u_i = scratch_data.div_phi_u[q][i];
303
304
305 // Navier-Stokes Residual
306 // Momentum
307 local_rhs(i) +=
308 // Momentum terms
309 (-dynamic_viscosity_eq * scalar_product(shear_rate, grad_phi_u_i) -
310 density_eq * velocity_gradient * velocity * phi_u_i +
311 pressure * div_phi_u_i +
312 density_eq * force * phi_u_i
313 // Continuity equation
314 - velocity_divergence * phi_p_i
315 // Surface tension term
316 + potential_value * phase_order_gradient * phi_u_i) *
317 JxW;
318
319 // PSPG GLS term
320 local_rhs(i) +=
321 -tau / density_eq * (strong_residual * grad_phi_p_i) * JxW;
322
323 // SUPG GLS term
324 local_rhs(i) +=
325 -tau * (strong_residual * (grad_phi_u_i * velocity)) * JxW;
326 }
327 }
328}
329
330
331
334
335template <int dim>
336void
338 const NavierStokesScratchData<dim> &scratch_data,
340{
341 // Loop and quadrature information
342 const auto &JxW = scratch_data.JxW;
343 const unsigned int n_q_points = scratch_data.n_q_points;
344 const unsigned int n_dofs = scratch_data.n_dofs;
345
346 // Copy data elements
347 auto &strong_residual = copy_data.strong_residual;
348 auto &strong_jacobian = copy_data.strong_jacobian;
349 auto &local_matrix = copy_data.local_matrix;
350
351 // Time stepping information
352 const auto method = this->simulation_control->get_assembly_method();
353 // Vector for the BDF coefficients
354 const Vector<double> &bdf_coefs =
355 this->simulation_control->get_bdf_coefficients();
356 std::vector<Tensor<1, dim>> velocity(1 +
358
359 // Loop over the quadrature points
360 for (unsigned int q = 0; q < n_q_points; ++q)
361 {
362 velocity[0] = scratch_data.velocity_values[q];
363 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
364 velocity[p + 1] = scratch_data.previous_velocity_values[p][q];
365
366 const double density = scratch_data.density[q];
367
368 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
369 ++p)
370 {
371 strong_residual[q] += density * bdf_coefs[p] * velocity[p];
372 }
373
374 for (unsigned int j = 0; j < n_dofs; ++j)
375 {
376 strong_jacobian[q][j] +=
377 density * bdf_coefs[0] * scratch_data.phi_u[q][j];
378 }
379
380 for (unsigned int i = 0; i < n_dofs; ++i)
381 {
382 const Tensor<1, dim> &phi_u_i = scratch_data.phi_u[q][i];
383 for (unsigned int j = 0; j < n_dofs; ++j)
384 {
385 const Tensor<1, dim> &phi_u_j = scratch_data.phi_u[q][j];
386
387 local_matrix(i, j) +=
388 phi_u_j * phi_u_i * density * bdf_coefs[0] * JxW[q];
389 }
390 }
391 }
392}
393
394template <int dim>
395void
397 const NavierStokesScratchData<dim> &scratch_data,
399{
400 // Loop and quadrature information
401 const auto &JxW = scratch_data.JxW;
402 const unsigned int n_q_points = scratch_data.n_q_points;
403 const unsigned int n_dofs = scratch_data.n_dofs;
404
405 // Copy data elements
406 auto &strong_residual = copy_data.strong_residual;
407 auto &local_rhs = copy_data.local_rhs;
408
409 // Time stepping information
410 const auto method = this->simulation_control->get_assembly_method();
411 // Vector for the BDF coefficients
412 const Vector<double> &bdf_coefs =
413 this->simulation_control->get_bdf_coefficients();
414 std::vector<Tensor<1, dim>> velocity(1 +
416
417 // Loop over the quadrature points
418 for (unsigned int q = 0; q < n_q_points; ++q)
419 {
420 velocity[0] = scratch_data.velocity_values[q];
421 for (unsigned int p = 0; p < number_of_previous_solutions(method); ++p)
422 velocity[p + 1] = scratch_data.previous_velocity_values[p][q];
423
424 const double density = scratch_data.density[q];
425
426 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
427 ++p)
428 {
429 strong_residual[q] += (density * bdf_coefs[p] * velocity[p]);
430 }
431
432 for (unsigned int i = 0; i < n_dofs; ++i)
433 {
434 const auto phi_u_i = scratch_data.phi_u[q][i];
435 double local_rhs_i = 0;
436 for (unsigned int p = 0; p < number_of_previous_solutions(method) + 1;
437 ++p)
438 {
439 local_rhs_i -= density * bdf_coefs[p] * (velocity[p] * phi_u_i);
440 }
441 local_rhs(i) += local_rhs_i * JxW[q];
442 }
443 }
444}
445
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 Navier-Stokes equat...
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
const std::shared_ptr< SimulationControl > simulation_control
Class that assembles the core of the Navier-Stokes equation with free surface using Cahn-Hilliard mod...
const std::shared_ptr< SimulationControl > simulation_control
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 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< Tensor< 1, dim > > phase_order_cahn_hilliard_gradients
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
const PhysicalPropertiesManager properties_manager
std::vector< double > chemical_potential_cahn_hilliard_values
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.