Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
fluid_dynamics_matrix_based.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2019-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>
5#include <core/grids.h>
6#include <core/manifolds.h>
7#include <core/multiphysics.h>
9#include <core/utilities.h>
10
16
17#include <deal.II/base/exceptions.h>
18#include <deal.II/base/work_stream.h>
19
20#include <deal.II/dofs/dof_renumbering.h>
21#include <deal.II/dofs/dof_tools.h>
22
23#include <deal.II/grid/grid_tools.h>
24
25#include <deal.II/lac/full_matrix.h>
26#include <deal.II/lac/solver_bicgstab.h>
27#include <deal.II/lac/solver_cg.h>
28#include <deal.II/lac/solver_control.h>
29#include <deal.II/lac/solver_gmres.h>
30#include <deal.II/lac/sparse_direct.h>
31#include <deal.II/lac/sparse_ilu.h>
32
33#include <deal.II/numerics/vector_tools.h>
34
35#include <cmath>
36#include <exception>
37
38
39// Constructor for class FluidDynamicsMatrixBased
40template <int dim>
43 const FluidDynamicsFormulation fluid_dynamics_formulation)
44 : NavierStokesBase<dim, GlobalVectorType, IndexSet>(
45 p_nsparam,
46 fluid_dynamics_formulation)
47{
49 ((this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
52 .amg_precond_ilu_fill :
53 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
54 .ilu_precond_fill);
55}
56
57template <int dim>
62
63template <int dim>
64void
66{
67 TimerOutput::Scope t(this->computing_timer, "Setup DOFs");
68
69 // Clear the preconditioner before the matrix they are associated with is
70 // cleared
71 amg_preconditioner.reset();
72 ilu_preconditioner.reset();
74
75
76 // Now reset system matrix
77 system_matrix.clear();
78
79 this->dof_handler->distribute_dofs(*this->fe);
80 DoFRenumbering::Cuthill_McKee(*this->dof_handler);
81
82 this->locally_owned_dofs = this->dof_handler->locally_owned_dofs();
84 DoFTools::extract_locally_relevant_dofs(*this->dof_handler);
85
86 // If enabled, rotate rotor mapping
87 this->rotate_rotor_mapping(false);
88
89 // Non Zero constraints
91
92 // Zero constraints
94
95 // If enabled, fix pressure constant
97
98 // If enabled, create mortar operators
100
101 // Operations on the following vectors (addition, multiplication, etc.) can
102 // only be done if these are reinitialized WITHOUT locally_relevant_dofs (i.e.
103 // without ghost DoFs). This is why most of them are reinitialized both with
104 // and without locally_relevant_dofs.
105 this->present_solution->reinit(this->locally_owned_dofs,
107 this->mpi_communicator);
109 this->mpi_communicator);
110 this->evaluation_point.reinit(this->locally_owned_dofs,
112 this->mpi_communicator);
113
114 // Initialize vector of previous solutions
115 for (auto &solution : *this->previous_solutions)
116 {
117 solution.reinit(this->locally_owned_dofs,
119 this->mpi_communicator);
120 }
121
122 if (this->simulation_control->is_sdirk())
123 {
124 // Reinitialize vectors used for the SDIRK methods
125 this->sdirk_vectors.sum_bi_ki.reinit(this->locally_owned_dofs,
127 this->mpi_communicator);
128 this->sdirk_vectors.local_sum_bi_ki.reinit(this->locally_owned_dofs,
129 this->mpi_communicator);
130 this->sdirk_vectors.sum_over_previous_stages.reinit(
131 this->locally_owned_dofs,
133 this->mpi_communicator);
134 this->sdirk_vectors.local_sum_over_previous_stages.reinit(
136 this->sdirk_vectors.locally_owned_for_calculation.reinit(
138
139 for (auto &solution : this->sdirk_vectors.previous_k_j_solutions)
140 {
141 solution.reinit(this->locally_owned_dofs,
143 this->mpi_communicator);
144 }
145 }
146
147 this->newton_update.reinit(this->locally_owned_dofs, this->mpi_communicator);
148 this->system_rhs.reinit(this->locally_owned_dofs, this->mpi_communicator);
149
150
152 DynamicSparsityPattern dsp(this->locally_relevant_dofs);
153 DoFTools::make_sparsity_pattern(*this->dof_handler,
154 dsp,
156 false);
157
158 // Add sparsity pattern entries
159 if (this->simulation_parameters.mortar_parameters.enable)
160 this->mortar_coupling_operator->add_sparsity_pattern_entries(dsp);
161
162 SparsityTools::distribute_sparsity_pattern(
163 dsp,
164 this->dof_handler->locally_owned_dofs(),
165 this->mpi_communicator,
166 this->locally_relevant_dofs);
167
169 this->locally_owned_dofs,
170 dsp,
171 this->mpi_communicator);
172
173 if (this->simulation_parameters.post_processing
174 .calculate_average_velocities ||
175 this->simulation_parameters.initial_condition->type ==
177 {
178 this->average_velocities->initialize_vectors(this->locally_owned_dofs,
180 this->mpi_communicator);
181 }
182
183 double global_volume =
184 GridTools::volume(*this->triangulation, *this->get_mapping());
185
186 this->pcout << " Number of active cells: "
187 << this->triangulation->n_global_active_cells() << std::endl
188 << " Number of degrees of freedom: "
189 << this->dof_handler->n_dofs() << std::endl;
190 this->pcout << " Volume of triangulation: " << global_volume
191 << std::endl;
192
193
194 // Provide the fluid dynamics dof_handler and present solution to the
195 // multiphysics interface
196 this->multiphysics->set_dof_handler(PhysicsID::fluid_dynamics,
197 this->dof_handler);
198 this->multiphysics->set_mapping(PhysicsID::fluid_dynamics, this->mapping);
199 this->multiphysics->set_solution(PhysicsID::fluid_dynamics,
200 this->present_solution);
201 this->multiphysics->set_previous_solutions(PhysicsID::fluid_dynamics,
202 this->previous_solutions);
203}
204
205template <int dim>
206void
208{
209 if (!this->simulation_parameters.mortar_parameters.enable)
210 return;
211
212 TimerOutput::Scope t(this->computing_timer, "Update mortar configuration");
213
214 // We need to update the mortar operator/evaluator, as well as the sparsity
215 // pattern, at every iteration. Since this is already done within
216 // setup_dofs(), which is called in refine_mesh(), here we make sure that,
217 // when there is no mesh refinement, the mortar information is still updated
218 if (this->simulation_control->is_at_start() ||
219 !this->simulation_control->is_refinement_step(
220 this->simulation_parameters.mesh_adaptation) ||
221 this->simulation_parameters.mesh_adaptation.type ==
223 {
224 // Clear the preconditioner before the matrix they are associated with is
225 // cleared
226 amg_preconditioner.reset();
227 ilu_preconditioner.reset();
229
230 // Now reset system matrix
231 system_matrix.clear();
232
233 // Rotate mapping
234 this->rotate_rotor_mapping(false);
235
236 // Non Zero constraints
238
239 // Zero constraints
241
242 // If enabled, fix pressure constant
244
245 // Create mortar manager, operator, and evaluator
247
248 // Create dynamic sparsity pattern
249 DynamicSparsityPattern dsp(this->locally_relevant_dofs);
250 DoFTools::make_sparsity_pattern(*this->dof_handler,
251 dsp,
253 false);
254
255 // Add mortar sparsity pattern entries
256 this->mortar_coupling_operator->add_sparsity_pattern_entries(dsp);
257
258 SparsityTools::distribute_sparsity_pattern(
259 dsp,
260 this->dof_handler->locally_owned_dofs(),
261 this->mpi_communicator,
262 this->locally_relevant_dofs);
263
265 this->locally_owned_dofs,
266 dsp,
267 this->mpi_communicator);
268 }
269}
270
271template <int dim>
272void
274{
275 if (this->simulation_parameters.post_processing
276 .calculate_average_velocities ||
277 this->simulation_parameters.initial_condition->type ==
279 {
280 this->multiphysics->set_time_average_solution(
282 this->average_velocities->get_average_velocities());
283 }
284}
285
286template <int dim>
287void
289{
290 if (!this->simulation_parameters.constrain_solid_domain.enable)
291 return;
292
293 this->dynamic_zero_constraints.clear();
296
297 DoFTools::make_hanging_node_constraints(*this->dof_handler,
299
300 const DoFHandler<dim> &dof_handler_ht =
301 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
302
303 if (!this->simulation_parameters.multiphysics.CLS)
304 this->constrain_stasis_with_temperature(&dof_handler_ht);
305 else
306 {
307 const DoFHandler<dim> &dof_handler_cls =
308 this->multiphysics->get_dof_handler(PhysicsID::CLS);
309 this->constrain_stasis_with_temperature_cls(&dof_handler_cls,
310 &dof_handler_ht);
311 }
312
313 this->dynamic_zero_constraints.merge(
314 this->zero_constraints,
315 AffineConstraints<double>::MergeConflictBehavior::right_object_wins);
316
317 this->dynamic_zero_constraints.close();
318
319 // Clear sets for next time step
320 for (StasisConstraintWithTemperature &stasis_constraint_struct :
322 {
323 stasis_constraint_struct.dofs_are_in_solid.clear();
324 stasis_constraint_struct.dofs_are_connected_to_fluid.clear();
325 }
326}
327
328template <int dim>
329void
331{
332 if (!this->simulation_parameters.boundary_conditions.fix_pressure_constant)
333 return;
334
335 // Create auxiliary constraints object to store pressure constraint, since at
336 // this point both zero_constraints and nonzero_constraints objects have been
337 // closed
338 AffineConstraints<double> constraints_extended;
339 constraints_extended.reinit(this->dof_handler->locally_owned_dofs(),
340 this->locally_relevant_dofs);
341
342 types::global_dof_index min_index = numbers::invalid_unsigned_int;
343
344 std::vector<types::global_dof_index> dof_indices;
345
346 // Loop over the cells to identify the min index
347 for (const auto &cell : this->dof_handler->active_cell_iterators())
348 {
349 if (cell->is_locally_owned())
350 {
351 const auto &fe = cell->get_fe();
352
353 dof_indices.resize(fe.n_dofs_per_cell());
354 cell->get_dof_indices(dof_indices);
355
356 for (unsigned int i = 0; i < dof_indices.size(); ++i)
357 if (fe.system_to_component_index(i).first == dim)
358 min_index = std::min(min_index, dof_indices[i]);
359 }
360 }
361
362 // Necessary to find the min across all cores.
363 min_index =
364 Utilities::MPI::min(min_index, this->dof_handler->get_mpi_communicator());
365
366 if (this->locally_relevant_dofs.is_element(min_index))
367 constraints_extended.add_line(min_index);
368
369 // Merge auxiliary constraints object with existent constraints
370 this->nonzero_constraints.merge(
371 constraints_extended,
372 AffineConstraints<double>::MergeConflictBehavior::no_conflicts_allowed,
373 true);
374 this->zero_constraints.merge(
375 constraints_extended,
376 AffineConstraints<double>::MergeConflictBehavior::no_conflicts_allowed,
377 true);
378}
379
380template <int dim>
381void
383{
384 // The SDIRK time integration is only implemented for the single-phase
385 // Navier-Stokes equations with a constant density.
386 if (this->simulation_control->is_sdirk())
387 {
388 AssertThrow(!this->simulation_parameters.multiphysics.CLS &&
389 !this->simulation_parameters.multiphysics.cahn_hilliard,
390 ExcMessage("SDIRK time-stepping methods are not supported "
391 "for multiphase flows (CLS or Cahn-Hilliard). "
392 "Use a BDF method instead."));
393 AssertThrow(this->simulation_parameters.physical_properties_manager
394 .density_is_constant(),
395 ExcMessage("SDIRK time-stepping methods are not supported "
396 "for flows with a variable density. Use a BDF "
397 "method instead."));
398 }
399
400 this->assemblers.clear();
401
402 if (this->check_existance_of_bc(
404 {
405 this->assemblers.emplace_back(
406 std::make_shared<WeakDirichletBoundaryCondition<dim>>(
407 this->simulation_control,
408 this->simulation_parameters.boundary_conditions));
409 }
410 if (this->check_existance_of_bc(
412 {
413 this->assemblers.emplace_back(
415 this->simulation_control,
416 this->simulation_parameters.boundary_conditions));
417 }
418 if (this->check_existance_of_bc(
420 {
421 this->assemblers.emplace_back(
422 std::make_shared<OutletBoundaryCondition<dim>>(
423 this->simulation_control,
424 this->simulation_parameters.boundary_conditions));
425 }
426 if (this->check_existance_of_bc(
428 {
429 this->assemblers.emplace_back(
430 std::make_shared<PressureBoundaryCondition<dim>>(
431 this->simulation_control,
432 this->simulation_parameters.boundary_conditions));
433 }
434
435 if (this->check_existance_of_bc(
437 {
438 this->assemblers.emplace_back(
440 this->simulation_control,
441 this->simulation_parameters.boundary_conditions));
442 }
443
444
445 // Thermal buoyancy force
446 if (this->simulation_parameters.multiphysics.thermal_buoyancy_force)
447 {
448 if (this->simulation_parameters.multiphysics.CLS)
449 {
450 // CLS formulation includes density explicitly in the momentum
451 // equation
452 this->assemblers.emplace_back(
453 std::make_shared<ThermalBuoyancyAssemblyCLS<dim>>(
454 this->simulation_control,
455 this->simulation_parameters.physical_properties_manager
456 .get_reference_temperature()));
457 }
458 else
459 {
460 this->assemblers.emplace_back(
461 std::make_shared<ThermalBuoyancyAssembly<dim>>(
462 this->simulation_control,
463 this->simulation_parameters.physical_properties_manager
464 .get_reference_temperature()));
465 }
466 }
467
468 if (this->simulation_parameters.multiphysics.cahn_hilliard)
469 {
470 // Time-stepping schemes
471 if (time_stepping_is_bdf(this->simulation_control->get_assembly_method()))
472 {
473 this->assemblers.emplace_back(
475 this->simulation_control));
476 }
477
478 AssertThrow(
479 this->simulation_parameters.velocity_sources.permeability_model ==
481 PhaseChangePermeabilityModelDoesNotSupportCHN());
482
483 this->assemblers.emplace_back(
486 }
487
488 if (this->simulation_parameters.multiphysics.CLS)
489 {
490 // Time-stepping schemes
492 this->simulation_control->get_assembly_method()) &&
493 this->simulation_parameters.physical_properties_manager
494 .density_is_constant())
495 {
496 this->assemblers.emplace_back(
497 std::make_shared<GLSNavierStokesCLSAssemblerBDF<dim>>(
498 this->simulation_control));
499 }
500 else if (time_stepping_is_bdf(
501 this->simulation_control->get_assembly_method()))
502 {
503 this->assemblers.emplace_back(
504 std::make_shared<
506 this->simulation_control));
507 }
508
509 // Surface tension force (STF)
510 if (this->simulation_parameters.multiphysics.cls_parameters
511 .surface_tension_force.enable)
512 {
513 if (this->simulation_parameters.multiphysics.cls_parameters
514 .surface_tension_force.enable_marangoni_effect)
515 this->assemblers.emplace_back(
517 this->simulation_control,
518 this->simulation_parameters.multiphysics.cls_parameters
519 .surface_tension_force));
520 else
521 {
522 this->assemblers.emplace_back(
523 std::make_shared<GLSNavierStokesCLSAssemblerSTF<dim>>(
525 }
526 }
527
528 // Recoil pressure
529 if (this->simulation_parameters.evaporation.enable_recoil_pressure)
530 {
531 this->assemblers.emplace_back(
533 this->simulation_control,
534 this->simulation_parameters.evaporation));
535 }
536
537 // Darcy force for phase change simulations
538 if (this->simulation_parameters.velocity_sources.permeability_model ==
540 {
541 AssertThrow(this->simulation_parameters.multiphysics.heat_transfer,
542 PhaseChangePermeabilityModelRequiresTemperature());
543 this->assemblers.emplace_back(
544 std::make_shared<PhaseChangeDarcyCLSAssembler<dim>>(
545 this->simulation_parameters.physical_properties_manager
546 .get_phase_change_parameters_vector(),
547 this->simulation_parameters.velocity_sources
548 .enable_darcy_multiply_by_density));
549 }
550 // Carman-Kozeny force for phase change simulations
551 else if (this->simulation_parameters.velocity_sources
552 .permeability_model ==
554 carman_kozeny_phase_change)
555 {
556 AssertThrow(this->simulation_parameters.multiphysics.heat_transfer,
557 PhaseChangePermeabilityModelRequiresTemperature());
558 this->assemblers.emplace_back(
560 this->simulation_parameters.velocity_sources
561 .fluid_with_phase_change,
562 this->simulation_parameters.velocity_sources
563 .carman_kozeny_permeability_area,
564 this->simulation_parameters.velocity_sources
565 .carman_kozeny_tolerance,
566 this->simulation_parameters.physical_properties_manager
567 .get_phase_change_parameters_vector()));
568 }
569
570 if (this->simulation_parameters.physical_properties_manager
571 .is_non_newtonian())
572 {
573 // Core assembler with Non newtonian viscosity
574 this->assemblers.emplace_back(
577 }
578 else if (!this->simulation_parameters.physical_properties_manager
579 .density_is_constant())
580 {
581 this->assemblers.emplace_back(
582 std::make_shared<
585 }
586 else
587 {
588 // Core assembler
589 this->assemblers.emplace_back(
590 std::make_shared<GLSNavierStokesCLSAssemblerCore<dim>>(
592 }
593 }
594
595 if (!this->simulation_parameters.multiphysics.CLS &&
596 !this->simulation_parameters.multiphysics.cahn_hilliard)
597 {
598 // Time-stepping schemes
599 if (time_stepping_is_bdf(this->simulation_control->get_assembly_method()))
600 {
601 if (!this->simulation_parameters.physical_properties_manager
602 .density_is_constant())
603 {
604 this->assemblers.emplace_back(
605 std::make_shared<
607 this->simulation_control));
608 }
609 else
610 {
611 this->assemblers.emplace_back(
612 std::make_shared<GLSNavierStokesAssemblerBDF<dim>>(
613 this->simulation_control));
614 }
615 }
616
617 // sdirk methods
618 if (this->simulation_control->is_sdirk())
619 {
620 this->assemblers.emplace_back(
621 std::make_shared<GLSNavierStokesAssemblerSDIRK<dim>>(
622 this->simulation_control));
623 }
624
625 // Rotating frame sources term
626 if (this->simulation_parameters.velocity_sources.rotating_frame_type ==
628 {
629 this->assemblers.emplace_back(
630 std::make_shared<GLSNavierStokesAssemblerSRF<dim>>(
631 this->simulation_parameters.velocity_sources));
632 }
633
634 // Darcy force for phase change simulations in single phase
635 if (this->simulation_parameters.velocity_sources.permeability_model ==
637 {
638 AssertThrow(this->simulation_parameters.multiphysics.heat_transfer,
639 PhaseChangePermeabilityModelRequiresTemperature());
640 AssertThrow(
641 !this->simulation_parameters.velocity_sources
642 .enable_darcy_multiply_by_density,
643 SingleFluidPhaseChangeDarcyModelDoesNotSupportDensityMultiplication());
644
645 this->assemblers.emplace_back(
646 std::make_shared<PhaseChangeDarcyAssembly<dim>>(
647 this->simulation_parameters.physical_properties_manager
648 .get_physical_properties_parameters()
649 .fluids[0]
650 .phase_change_parameters));
651 }
652 // Carman-Kozeny force for phase change simulations in single phase
653 else if (this->simulation_parameters.velocity_sources
654 .permeability_model ==
656 carman_kozeny_phase_change)
657 {
658 AssertThrow(this->simulation_parameters.multiphysics.heat_transfer,
659 PhaseChangePermeabilityModelRequiresTemperature());
660 this->assemblers.emplace_back(
662 this->simulation_parameters.velocity_sources
663 .carman_kozeny_permeability_area[0],
664 this->simulation_parameters.velocity_sources
665 .carman_kozeny_tolerance[0],
666 this->simulation_parameters.physical_properties_manager
667 .get_physical_properties_parameters()
668 .fluids[0]
669 .phase_change_parameters.T_liquidus,
670
671 this->simulation_parameters.physical_properties_manager
672 .get_physical_properties_parameters()
673 .fluids[0]
674 .phase_change_parameters.T_solidus));
675 }
676
677 if (this->simulation_parameters.physical_properties_manager
678 .is_non_newtonian())
679 {
680 // Core assembler with Non newtonian viscosity
681 this->assemblers.emplace_back(
683 this->simulation_control));
684 }
685 else
686 {
687 // Core default assembler
688 if ((this->simulation_parameters.stabilization
689 .use_default_stabilization == true) ||
690 this->simulation_parameters.stabilization.stabilization ==
692 {
693 if (!this->simulation_parameters.physical_properties_manager
694 .density_is_constant())
695 {
696 this->assemblers.emplace_back(
697 std::make_shared<
699 this->simulation_control));
700 }
701 else
702 {
703 this->assemblers.emplace_back(
705 this->simulation_control));
706 }
707 }
708 else if (this->simulation_parameters.stabilization.stabilization ==
710 {
711 if (!this->simulation_parameters.physical_properties_manager
712 .density_is_constant())
713 {
714 this->assemblers.emplace_back(
715 std::make_shared<
717 this->simulation_control));
718 }
719 else
720 {
721 this->assemblers.emplace_back(
722 std::make_shared<GLSNavierStokesAssemblerCore<dim>>(
723 this->simulation_control));
724 }
725 }
726 else
727 throw std::runtime_error(
728 "Using the GLS solver with a stabilization other than the pspg_supg or gls "
729 "stabilization will lead to an unstable solver that is unable to converge");
730 }
731 }
732}
733
734template <int dim>
735void
737{
738 TimerOutput::Scope t(this->computing_timer, "Assemble matrix");
739
740 this->system_matrix = 0;
742
743 auto scratch_data = NavierStokesScratchData<dim>(
744 this->simulation_control,
745 this->simulation_parameters.physical_properties_manager,
746 *this->fe,
747 *this->cell_quadrature,
748 *this->get_mapping(),
749 *this->face_quadrature);
750
751 if (this->simulation_parameters.multiphysics.CLS)
752 {
753 const DoFHandler<dim> &dof_handler_cls =
754 this->multiphysics->get_dof_handler(PhysicsID::CLS);
755 scratch_data.enable_cls(
756 dof_handler_cls.get_fe(),
757 *this->cell_quadrature,
758 *this->get_mapping(),
759 this->simulation_parameters.multiphysics.cls_parameters.phase_filter);
760
761 if (this->simulation_parameters.multiphysics.cls_parameters
762 .surface_tension_force.enable)
763 {
764 const DoFHandler<dim>
765 &projected_phase_indicator_gradient_dof_handler =
766 this->multiphysics
767 ->get_projected_phase_indicator_gradient_dof_handler();
768 const DoFHandler<dim> &curvature_dof_handler =
769 this->multiphysics->get_curvature_dof_handler();
770 scratch_data.enable_projected_phase_indicator_gradient(
771 projected_phase_indicator_gradient_dof_handler.get_fe(),
772 *this->cell_quadrature,
773 *this->get_mapping());
774 scratch_data.enable_curvature(curvature_dof_handler.get_fe(),
775 *this->cell_quadrature,
776 *this->get_mapping());
777 }
778 }
779 else if (this->simulation_parameters.multiphysics.cahn_hilliard)
780 {
781 const DoFHandler<dim> &dof_handler_cahn_hilliard =
782 this->multiphysics->get_dof_handler(PhysicsID::cahn_hilliard);
783 scratch_data.enable_cahn_hilliard(
784 dof_handler_cahn_hilliard.get_fe(),
785 *this->cell_quadrature,
786 *this->get_mapping(),
787 this->simulation_parameters.multiphysics.cahn_hilliard_parameters);
788 }
789
790 if (this->simulation_parameters.multiphysics.heat_transfer)
791 {
792 const DoFHandler<dim> &dof_handler_ht =
793 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
794 scratch_data.enable_heat_transfer(dof_handler_ht.get_fe(),
795 *this->cell_quadrature,
796 *this->get_mapping());
797 }
798
799 if (this->simulation_parameters.mortar_parameters.enable)
800 scratch_data.enable_mortar();
801
802 WorkStream::run(
803 this->dof_handler->begin_active(),
804 this->dof_handler->end(),
805 *this,
808 scratch_data,
809 StabilizedMethodsTensorCopyData<dim>(this->fe->n_dofs_per_cell(),
810 this->cell_quadrature->size()));
811
812 // Add mortar entries
813 if (this->simulation_parameters.mortar_parameters.enable)
814 {
815 this->computing_timer.enter_subsection("Assemble matrix (mortar)");
816 this->mortar_coupling_operator->add_system_matrix_entries(
817 this->system_matrix);
818 this->computing_timer.leave_subsection("Assemble matrix (mortar)");
819 }
820
821 system_matrix.compress(VectorOperation::add);
822}
823
824template <int dim>
825void
827 const typename DoFHandler<dim>::active_cell_iterator &cell,
828 NavierStokesScratchData<dim> &scratch_data,
830{
831 copy_data.cell_is_local = cell->is_locally_owned();
832 if (!cell->is_locally_owned())
833 return;
834
835 scratch_data.reinit(
836 cell,
837 this->evaluation_point,
838 *this->previous_solutions,
839 this->sdirk_vectors.sum_over_previous_stages,
840 this->forcing_function,
841 this->flow_control.get_beta(),
842 this->simulation_parameters.stabilization.pressure_scaling_factor);
843
844 if (this->simulation_parameters.multiphysics.CLS)
845 {
846 const DoFHandler<dim> &dof_handler_cls =
847 this->multiphysics->get_dof_handler(PhysicsID::CLS);
848 typename DoFHandler<dim>::active_cell_iterator phase_cell(
849 &(*(this->triangulation)),
850 cell->level(),
851 cell->index(),
852 &dof_handler_cls);
853
854 scratch_data.reinit_cls(
855 phase_cell,
856 this->multiphysics->get_solution(PhysicsID::CLS),
857 this->multiphysics->get_filtered_solution(PhysicsID::CLS),
858 this->multiphysics->get_previous_solutions(PhysicsID::CLS));
859
860 if (this->simulation_parameters.multiphysics.cls_parameters
861 .surface_tension_force.enable)
862 {
863 const DoFHandler<dim>
864 &projected_phase_indicator_gradient_dof_handler =
865 this->multiphysics
866 ->get_projected_phase_indicator_gradient_dof_handler();
867
868 typename DoFHandler<dim>::active_cell_iterator
869 projected_phase_indicator_gradient_cell(
870 &(*(this->triangulation)),
871 cell->level(),
872 cell->index(),
873 &projected_phase_indicator_gradient_dof_handler);
874
876 projected_phase_indicator_gradient_cell,
877 this->multiphysics
878 ->get_projected_phase_indicator_gradient_solution());
879
880
881
882 const DoFHandler<dim> &curvature_dof_handler =
883 this->multiphysics->get_curvature_dof_handler();
884 typename DoFHandler<dim>::active_cell_iterator curvature_cell(
885 &(*(this->triangulation)),
886 cell->level(),
887 cell->index(),
888 &curvature_dof_handler);
889 scratch_data.reinit_curvature(
890 curvature_cell, this->multiphysics->get_curvature_solution());
891 }
892 }
893 else if (this->simulation_parameters.multiphysics.cahn_hilliard)
894 {
895 const DoFHandler<dim> &dof_handler_cahn_hilliard =
896 this->multiphysics->get_dof_handler(PhysicsID::cahn_hilliard);
897 typename DoFHandler<dim>::active_cell_iterator phase_cell(
898 &(*(this->triangulation)),
899 cell->level(),
900 cell->index(),
901 &dof_handler_cahn_hilliard);
902
903 scratch_data.reinit_cahn_hilliard(
904 phase_cell,
905 this->multiphysics->get_solution(PhysicsID::cahn_hilliard),
906 this->multiphysics->get_filtered_solution(PhysicsID::cahn_hilliard));
907 }
908
909 if (this->simulation_parameters.multiphysics.heat_transfer)
910 {
911 const DoFHandler<dim> &dof_handler_ht =
912 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
913
914 typename DoFHandler<dim>::active_cell_iterator temperature_cell(
915 &(*(this->triangulation)),
916 cell->level(),
917 cell->index(),
918 &dof_handler_ht);
919
920 scratch_data.reinit_heat_transfer(
921 temperature_cell,
922 this->multiphysics->get_solution(PhysicsID::heat_transfer),
923 this->multiphysics->get_previous_solutions(PhysicsID::heat_transfer));
924 }
925
926 if (this->simulation_parameters.ale.enabled())
927 scratch_data.reinit_ale(this->simulation_parameters.ale);
928
929 if (this->simulation_parameters.mortar_parameters.enable)
930 scratch_data.reinit_mortar(cell,
931 this->simulation_parameters.mortar_parameters);
932
933 scratch_data.calculate_physical_properties();
934
935 copy_data.reset();
936
937 if (this->simulation_parameters.physical_properties_manager
938 .get_number_of_solids() < 1 ||
939 cell->material_id() == 0)
940 {
941 for (auto &assembler : this->assemblers)
942 {
943 assembler->assemble_matrix(scratch_data, copy_data);
944 }
945 }
946
947 cell->get_dof_indices(copy_data.local_dof_indices);
948}
949
950template <int dim>
951void
954{
955 if (!copy_data.cell_is_local)
956 return;
957
958 const AffineConstraints<double> &zero_constraints_used =
959 (!this->simulation_parameters.constrain_solid_domain.enable) ?
960 this->zero_constraints :
962 zero_constraints_used.distribute_local_to_global(copy_data.local_matrix,
963 copy_data.local_dof_indices,
964 this->system_matrix);
965}
966
967
968template <int dim>
969void
971{
972 TimerOutput::Scope t(this->computing_timer, "Assemble RHS");
973
974 this->system_rhs = 0;
976
977 auto scratch_data = NavierStokesScratchData<dim>(
978 this->simulation_control,
979 this->simulation_parameters.physical_properties_manager,
980 *this->fe,
981 *this->cell_quadrature,
982 *this->get_mapping(),
983 *this->face_quadrature);
984
985 if (this->simulation_parameters.multiphysics.CLS)
986 {
987 const DoFHandler<dim> &dof_handler_cls =
988 this->multiphysics->get_dof_handler(PhysicsID::CLS);
989 scratch_data.enable_cls(
990 dof_handler_cls.get_fe(),
991 *this->cell_quadrature,
992 *this->get_mapping(),
993 this->simulation_parameters.multiphysics.cls_parameters.phase_filter);
994
995 if (this->simulation_parameters.multiphysics.cls_parameters
996 .surface_tension_force.enable)
997 {
998 const DoFHandler<dim>
999 &projected_phase_indicator_gradient_dof_handler =
1000 this->multiphysics
1001 ->get_projected_phase_indicator_gradient_dof_handler();
1002 const DoFHandler<dim> &curvature_dof_handler =
1003 this->multiphysics->get_curvature_dof_handler();
1004
1005 scratch_data.enable_projected_phase_indicator_gradient(
1006 projected_phase_indicator_gradient_dof_handler.get_fe(),
1007 *this->cell_quadrature,
1008 *this->get_mapping());
1009 scratch_data.enable_curvature(curvature_dof_handler.get_fe(),
1010 *this->cell_quadrature,
1011 *this->get_mapping());
1012 }
1013 }
1014
1015 else if (this->simulation_parameters.multiphysics.cahn_hilliard)
1016 {
1017 const DoFHandler<dim> &dof_handler_cahn_hilliard =
1018 this->multiphysics->get_dof_handler(PhysicsID::cahn_hilliard);
1019 scratch_data.enable_cahn_hilliard(
1020 dof_handler_cahn_hilliard.get_fe(),
1021 *this->cell_quadrature,
1022 *this->get_mapping(),
1023 this->simulation_parameters.multiphysics.cahn_hilliard_parameters);
1024 }
1025
1026 if (this->simulation_parameters.multiphysics.heat_transfer)
1027 {
1028 const DoFHandler<dim> &dof_handler_ht =
1029 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
1030 scratch_data.enable_heat_transfer(dof_handler_ht.get_fe(),
1031 *this->cell_quadrature,
1032 *this->get_mapping());
1033 }
1034
1035 if (this->simulation_parameters.mortar_parameters.enable)
1036 scratch_data.enable_mortar();
1037
1038 WorkStream::run(
1039 this->dof_handler->begin_active(),
1040 this->dof_handler->end(),
1041 *this,
1044 scratch_data,
1045 StabilizedMethodsTensorCopyData<dim>(this->fe->n_dofs_per_cell(),
1046 this->cell_quadrature->size()));
1047
1048 // Add mortar entries
1049 if (this->simulation_parameters.mortar_parameters.enable)
1050 {
1051 this->computing_timer.enter_subsection("Assemble RHS (mortar)");
1052 // Change sign of RHS to be compatible with mortar coupling terms
1053 this->system_rhs.compress(VectorOperation::add);
1054 this->system_rhs *= -1.0;
1055 this->mortar_coupling_operator->vmult_add(this->system_rhs,
1056 this->evaluation_point);
1057 // Return RHS to original sign
1058 this->system_rhs *= -1.0;
1059
1060 this->computing_timer.leave_subsection("Assemble RHS (mortar)");
1061 }
1062
1063 this->system_rhs.compress(VectorOperation::add);
1064
1065 if (this->simulation_control->is_first_assembly())
1066 this->simulation_control->provide_residual(this->system_rhs.l2_norm());
1067}
1068
1069
1070template <int dim>
1071void
1073 const typename DoFHandler<dim>::active_cell_iterator &cell,
1074 NavierStokesScratchData<dim> &scratch_data,
1076{
1077 copy_data.cell_is_local = cell->is_locally_owned();
1078 if (!cell->is_locally_owned())
1079 return;
1080
1081 scratch_data.reinit(
1082 cell,
1083 this->evaluation_point,
1084 *this->previous_solutions,
1085 this->sdirk_vectors.sum_over_previous_stages,
1086 this->forcing_function,
1087 this->flow_control.get_beta(),
1088 this->simulation_parameters.stabilization.pressure_scaling_factor);
1089
1090 if (this->simulation_parameters.multiphysics.CLS)
1091 {
1092 const DoFHandler<dim> &dof_handler_cls =
1093 this->multiphysics->get_dof_handler(PhysicsID::CLS);
1094 typename DoFHandler<dim>::active_cell_iterator phase_cell(
1095 &(*(this->triangulation)),
1096 cell->level(),
1097 cell->index(),
1098 &dof_handler_cls);
1099
1100 scratch_data.reinit_cls(
1101 phase_cell,
1102 this->multiphysics->get_solution(PhysicsID::CLS),
1103 this->multiphysics->get_filtered_solution(PhysicsID::CLS),
1104 this->multiphysics->get_previous_solutions(PhysicsID::CLS));
1105
1106 if (this->simulation_parameters.multiphysics.cls_parameters
1107 .surface_tension_force.enable)
1108 {
1109 const DoFHandler<dim>
1110 &projected_phase_indicator_gradient_dof_handler =
1111 this->multiphysics
1112 ->get_projected_phase_indicator_gradient_dof_handler();
1113 typename DoFHandler<dim>::active_cell_iterator
1114 projected_phase_indicator_gradient_cell(
1115 &(*(this->triangulation)),
1116 cell->level(),
1117 cell->index(),
1118 &projected_phase_indicator_gradient_dof_handler);
1120 projected_phase_indicator_gradient_cell,
1121 this->multiphysics
1122 ->get_projected_phase_indicator_gradient_solution());
1123
1124 const DoFHandler<dim> &curvature_dof_handler =
1125 this->multiphysics->get_curvature_dof_handler();
1126 typename DoFHandler<dim>::active_cell_iterator curvature_cell(
1127 &(*(this->triangulation)),
1128 cell->level(),
1129 cell->index(),
1130 &curvature_dof_handler);
1131 scratch_data.reinit_curvature(
1132 curvature_cell, this->multiphysics->get_curvature_solution());
1133 }
1134 }
1135 else if (this->simulation_parameters.multiphysics.cahn_hilliard)
1136 {
1137 const DoFHandler<dim> &dof_handler_cahn_hilliard =
1138 this->multiphysics->get_dof_handler(PhysicsID::cahn_hilliard);
1139 typename DoFHandler<dim>::active_cell_iterator phase_cell(
1140 &(*(this->triangulation)),
1141 cell->level(),
1142 cell->index(),
1143 &dof_handler_cahn_hilliard);
1144
1145 scratch_data.reinit_cahn_hilliard(
1146 phase_cell,
1147 this->multiphysics->get_solution(PhysicsID::cahn_hilliard),
1148 this->multiphysics->get_filtered_solution(PhysicsID::cahn_hilliard));
1149 }
1150
1151 if (this->simulation_parameters.multiphysics.heat_transfer)
1152 {
1153 const DoFHandler<dim> &dof_handler_ht =
1154 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
1155
1156 typename DoFHandler<dim>::active_cell_iterator temperature_cell(
1157 &(*(this->triangulation)),
1158 cell->level(),
1159 cell->index(),
1160 &dof_handler_ht);
1161
1162 scratch_data.reinit_heat_transfer(
1163 temperature_cell,
1164 this->multiphysics->get_solution(PhysicsID::heat_transfer),
1165 this->multiphysics->get_previous_solutions(PhysicsID::heat_transfer));
1166 }
1167
1168 if (this->simulation_parameters.ale.enabled())
1169 scratch_data.reinit_ale(this->simulation_parameters.ale);
1170
1171 if (this->simulation_parameters.mortar_parameters.enable)
1172 scratch_data.reinit_mortar(cell,
1173 this->simulation_parameters.mortar_parameters);
1174
1175 scratch_data.calculate_physical_properties();
1176
1177 copy_data.reset();
1178
1179 if (this->simulation_parameters.physical_properties_manager
1180 .get_number_of_solids() < 1 ||
1181 cell->material_id() == 0)
1182 {
1183 for (auto &assembler : this->assemblers)
1184 {
1185 assembler->assemble_rhs(scratch_data, copy_data);
1186 }
1187 }
1188
1189
1190 cell->get_dof_indices(copy_data.local_dof_indices);
1191}
1192
1193
1194
1195template <int dim>
1196void
1198 const StabilizedMethodsTensorCopyData<dim> &copy_data)
1199{
1200 if (!copy_data.cell_is_local)
1201 return;
1202
1203 const AffineConstraints<double> &zero_constraints_used =
1204 (!this->simulation_parameters.constrain_solid_domain.enable) ?
1205 this->zero_constraints :
1207 zero_constraints_used.distribute_local_to_global(copy_data.local_rhs,
1208 copy_data.local_dof_indices,
1209 this->system_rhs);
1210}
1211
1212/**
1213 * Set the initial condition using a L2 or a viscous solver
1214 **/
1215template <int dim>
1216void
1218 Parameters::FluidDynamicsInitialConditionType initial_condition_type,
1219 bool restart)
1220{
1221 if (restart)
1222 {
1223 this->pcout << "************************" << std::endl;
1224 this->pcout << "---> Simulation Restart " << std::endl;
1225 this->pcout << "************************" << std::endl;
1226 this->read_checkpoint();
1227 }
1228 else if (initial_condition_type ==
1230 {
1232 solve_L2_system(1e-15, 1e-15);
1233
1234 *this->present_solution = this->newton_update;
1235 this->finish_time_step();
1236 }
1237 else if (initial_condition_type ==
1239 {
1240 this->set_nodal_values();
1241 this->finish_time_step();
1242 }
1243 else if (initial_condition_type ==
1245 {
1246 this->set_nodal_values();
1247 std::shared_ptr<RheologicalModel> original_viscosity_model =
1248 this->simulation_parameters.physical_properties_manager.get_rheology();
1249
1250 // Temporarily set the rheology to be newtonian with predefined viscosity
1251 std::shared_ptr<Newtonian> temporary_rheology =
1252 std::make_shared<Newtonian>(
1253 this->simulation_parameters.initial_condition->kinematic_viscosity);
1254
1255 this->simulation_parameters.physical_properties_manager.set_rheology(
1256 temporary_rheology);
1257
1258 // Temporarily solve a steady problem. The assembly method is restored
1259 // afterwards, otherwise the time-stepping scheme of the simulation would
1260 // remain steady for the rest of the simulation.
1261 const auto original_assembly_method =
1262 this->simulation_control->get_assembly_method();
1263
1264 this->simulation_control->set_assembly_method(
1267 this->finish_time_step();
1268
1269 // Reset the assembly method to the one of the simulation
1270 this->simulation_control->set_assembly_method(original_assembly_method);
1271
1272 this->simulation_parameters.physical_properties_manager.set_rheology(
1273 original_viscosity_model);
1274 }
1275 else if (initial_condition_type ==
1277 {
1278 this->pcout << "*********************************" << std::endl;
1279 this->pcout << " Initial condition using ramp " << std::endl;
1280 this->pcout << "*********************************" << std::endl;
1281
1282 Timer timer(this->mpi_communicator);
1283
1284 this->set_nodal_values();
1285
1286 // The ramp solves a sequence of steady problems. The assembly method is
1287 // stored here and restored at the end of the ramp, otherwise the
1288 // time-stepping scheme of the simulation would remain steady for the rest
1289 // of the simulation.
1290 const auto original_assembly_method =
1291 this->simulation_control->get_assembly_method();
1292
1293 // Create a pointer to the current viscosity model
1294 std::shared_ptr<RheologicalModel> viscosity_model =
1295 this->simulation_parameters.physical_properties_manager.get_rheology();
1296
1297 // Gather n and viscosity final paramerters
1298 const double n_end = viscosity_model->get_n();
1299 const double viscosity_end = viscosity_model->get_kinematic_viscosity();
1300
1301 // n ramp parameters
1302 double n =
1303 this->simulation_parameters.initial_condition->ramp.ramp_n.n_init;
1304 const int n_iter_n =
1305 this->simulation_parameters.initial_condition->ramp.ramp_n.n_iter;
1306 const double alpha_n =
1307 this->simulation_parameters.initial_condition->ramp.ramp_n.alpha;
1308
1309 // Kinematic viscosity ramp parameters
1310 const int n_iter_viscosity =
1311 this->simulation_parameters.initial_condition->ramp.ramp_viscosity
1312 .n_iter;
1313 double kinematic_viscosity =
1314 n_iter_viscosity > 0 ?
1315 this->simulation_parameters.initial_condition->ramp.ramp_viscosity
1316 .kinematic_viscosity_init :
1317 viscosity_end;
1318 const double alpha_viscosity =
1319 this->simulation_parameters.initial_condition->ramp.ramp_viscosity
1320 .alpha;
1321
1322 viscosity_model->set_kinematic_viscosity(kinematic_viscosity);
1323
1324 // Ramp on n
1325 for (int i = 0; i < n_iter_n; ++i)
1326 {
1327 this->pcout << "********* Solution for n = " + std::to_string(n) +
1328 " and viscosity = " +
1329 std::to_string(kinematic_viscosity) + " *********"
1330 << std::endl;
1331
1332 viscosity_model->set_n(n);
1333
1334 this->simulation_control->set_assembly_method(
1337 this->finish_time_step();
1338
1339 n += alpha_n * (n_end - n);
1340 }
1341
1342 // Reset n to simulation parameters
1343 viscosity_model->set_n(n_end);
1344
1345 // Ramp on kinematic viscosity
1346 for (int i = 0; i < n_iter_viscosity; ++i)
1347 {
1348 this->pcout << "********* Solution for n = " + std::to_string(n_end) +
1349 " and viscosity = " +
1350 std::to_string(kinematic_viscosity) + " *********"
1351 << std::endl;
1352
1353 viscosity_model->set_kinematic_viscosity(kinematic_viscosity);
1354
1355 this->simulation_control->set_assembly_method(
1358 this->finish_time_step();
1359
1360 kinematic_viscosity +=
1361 alpha_viscosity * (viscosity_end - kinematic_viscosity);
1362 }
1363
1364 // Reset kinematic viscosity to simulation parameters
1365 viscosity_model->set_kinematic_viscosity(viscosity_end);
1366
1367 // Reset the assembly method to the one of the simulation
1368 this->simulation_control->set_assembly_method(original_assembly_method);
1369
1370 timer.stop();
1371
1372 if (this->simulation_parameters.timer.type !=
1374 {
1375 this->pcout << "*********************************" << std::endl;
1376 this->pcout << " Time spent in ramp: " << timer.wall_time() << "s"
1377 << std::endl;
1378 this->pcout << "*********************************" << std::endl;
1379 }
1380 }
1381 else if (initial_condition_type ==
1383 average_velocity_profile)
1384 {
1385 announce_string(this->pcout,
1386 "Initial condition using average velocity profile");
1387
1388 std::string checkpoint_file_name =
1389 this->simulation_parameters.initial_condition->average_velocity_folder +
1390 this->simulation_parameters.initial_condition
1391 ->average_velocity_file_name;
1392
1393 // The average velocity profile needs to come from the results of a
1394 // simulation. The checkpoint/restart mechanism is used to obtain all the
1395 // simulations information necessary to restart the simulation. The same
1396 // mesh needs to be used, but new physics can be added to the simulation.
1397 this->set_solution_from_checkpoint(checkpoint_file_name);
1398
1399 auto simulation_control_info =
1400 this->simulation_control->get_checkpointed_simulation_control_info(
1401 checkpoint_file_name);
1402
1403 // Calculate the the average velocities
1404 this->average_velocities->calculate_average_velocities(
1406 this->simulation_parameters.post_processing,
1407 simulation_control_info[0], // Checkpointed end time
1408 simulation_control_info[1]); // Checkpointed time step
1409
1411 *this->average_velocities->get_average_velocities();
1413 }
1414 else
1415 {
1416 throw std::runtime_error("GLSNS - Initial condition could not be set");
1417 }
1418}
1419
1420template <int dim>
1421void
1423{
1424 system_matrix = 0;
1425 this->system_rhs = 0;
1426 FEValues<dim> fe_values(*this->get_mapping(),
1427 *this->fe,
1428 *this->cell_quadrature,
1429 update_values | update_quadrature_points |
1430 update_JxW_values);
1431 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
1432 const unsigned int n_q_points = this->cell_quadrature->size();
1433 FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1434 Vector<double> local_rhs(dofs_per_cell);
1435 std::vector<Vector<double>> initial_velocity(n_q_points,
1436 Vector<double>(dim + 1));
1437 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1438 const FEValuesExtractors::Vector velocities(0);
1439 const FEValuesExtractors::Scalar pressure(dim);
1440
1441 Tensor<1, dim> rhs_initial_velocity_pressure;
1442 double rhs_initial_pressure;
1443
1444 std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
1445 std::vector<double> phi_p(dofs_per_cell);
1446
1447 for (const auto &cell : this->dof_handler->active_cell_iterators())
1448 {
1449 if (cell->is_locally_owned())
1450 {
1451 fe_values.reinit(cell);
1452 local_matrix = 0;
1453 local_rhs = 0;
1454 this->simulation_parameters.initial_condition->uvwp.vector_value_list(
1455 fe_values.get_quadrature_points(), initial_velocity);
1456 for (unsigned int q = 0; q < n_q_points; ++q)
1457 {
1458 for (unsigned int k = 0; k < dofs_per_cell; ++k)
1459 {
1460 phi_p[k] = fe_values[pressure].value(k, q);
1461 phi_u[k] = fe_values[velocities].value(k, q);
1462 }
1463
1464 // Establish the rhs tensor operator
1465 for (int i = 0; i < dim; ++i)
1466 {
1467 const unsigned int component_i =
1468 this->fe->system_to_component_index(i).first;
1469 rhs_initial_velocity_pressure[i] =
1470 initial_velocity[q](component_i);
1471 }
1472 rhs_initial_pressure = initial_velocity[q](dim);
1473
1474 for (unsigned int i = 0; i < dofs_per_cell; ++i)
1475 {
1476 // Matrix assembly
1477 for (unsigned int j = 0; j < dofs_per_cell; ++j)
1478 {
1479 local_matrix(i, j) +=
1480 (phi_u[j] * phi_u[i]) * fe_values.JxW(q);
1481 local_matrix(i, j) +=
1482 (phi_p[j] * phi_p[i]) * fe_values.JxW(q);
1483 }
1484 local_rhs(i) += (phi_u[i] * rhs_initial_velocity_pressure +
1485 phi_p[i] * rhs_initial_pressure) *
1486 fe_values.JxW(q);
1487 }
1488 }
1489
1490 cell->get_dof_indices(local_dof_indices);
1491 this->nonzero_constraints.distribute_local_to_global(
1492 local_matrix,
1493 local_rhs,
1494 local_dof_indices,
1496 this->system_rhs);
1497 }
1498 }
1499 system_matrix.compress(VectorOperation::add);
1500 this->system_rhs.compress(VectorOperation::add);
1501}
1502
1503template <int dim>
1504void
1506{
1507 const double absolute_residual =
1508 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1509 .minimum_residual;
1510 const double relative_residual =
1511 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1512 .relative_residual;
1513
1514 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1516 solve_system_GMRES(absolute_residual, relative_residual);
1517 else if (this->simulation_parameters.linear_solver
1520 solve_system_BiCGStab(absolute_residual, relative_residual);
1521 else if (this->simulation_parameters.linear_solver
1524 solve_system_direct(absolute_residual, relative_residual);
1525 else
1526 AssertThrow(
1527 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1529 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1531 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1533 ExcMessage(
1534 "This linear solver is not supported. Only <gmres>, <bicgstab> and <direct> linear solvers are supported."));
1535
1537}
1538
1539template <int dim>
1540void
1542{
1543 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1545 setup_ILU();
1546 else if (this->simulation_parameters.linear_solver
1548 .preconditioner ==
1550 setup_AMG();
1551 else
1552 AssertThrow(
1553 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1554 .preconditioner ==
1556 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1557 .preconditioner ==
1559 ExcMessage(
1560 "This linear solver does not support this preconditioner. Only <ilu> and <amg> preconditioners are supported."));
1561}
1562
1563
1564template <int dim>
1565void
1567{
1568 TimerOutput::Scope t(this->computing_timer, "Setup ILU");
1569
1570 const double ilu_atol =
1571 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1572 .ilu_precond_atol;
1573 const double ilu_rtol =
1574 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1575 .ilu_precond_rtol;
1576 TrilinosWrappers::PreconditionILU::AdditionalData preconditionerOptions(
1577 current_preconditioner_fill_level, ilu_atol, ilu_rtol, 0);
1578
1579 ilu_preconditioner = std::make_shared<TrilinosWrappers::PreconditionILU>();
1580
1581 ilu_preconditioner->initialize(system_matrix, preconditionerOptions);
1582}
1583
1584template <int dim>
1585void
1587{
1588 TimerOutput::Scope t(this->computing_timer, "Setup AMG");
1589
1590 // Constant modes for velocity
1591 std::vector<std::vector<bool>> constant_modes;
1592
1593 // Constant modes include pressure since everything is in the same matrix
1594 ComponentMask components(dim + 1, true);
1595 constant_modes =
1596 DoFTools::extract_constant_modes(*this->dof_handler, components);
1597
1598 const bool elliptic = false;
1599 bool higher_order_elements = false;
1600 if (this->velocity_fem_degree > 1)
1601 higher_order_elements = true;
1602 const unsigned int n_cycles =
1603 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1604 .amg_n_cycles;
1605 const bool w_cycle =
1606 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1607 .amg_w_cycles;
1608 const double aggregation_threshold =
1609 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1610 .amg_aggregation_threshold;
1611 const unsigned int smoother_sweeps =
1612 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1613 .amg_smoother_sweeps;
1614 const unsigned int smoother_overlap =
1615 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1616 .amg_smoother_overlap;
1617 const bool output_details = false;
1618 const char *smoother_type = "ILU";
1619 const char *coarse_type = "ILU";
1620 TrilinosWrappers::PreconditionAMG::AdditionalData preconditionerOptions(
1621 elliptic,
1622 higher_order_elements,
1623 n_cycles,
1624 w_cycle,
1625 aggregation_threshold,
1626 constant_modes,
1627 smoother_sweeps,
1628 smoother_overlap,
1629 output_details,
1630 smoother_type,
1631 coarse_type);
1632
1633 Teuchos::ParameterList parameter_ml;
1634 std::unique_ptr<Epetra_MultiVector> distributed_constant_modes;
1635 preconditionerOptions.set_parameters(parameter_ml,
1636 distributed_constant_modes,
1638 const double ilu_fill = current_preconditioner_fill_level;
1639 const double ilu_atol =
1640 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1641 .amg_precond_ilu_atol;
1642 const double ilu_rtol =
1643 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1644 .amg_precond_ilu_rtol;
1645 parameter_ml.set("smoother: ifpack level-of-fill", ilu_fill);
1646 parameter_ml.set("smoother: ifpack absolute threshold", ilu_atol);
1647 parameter_ml.set("smoother: ifpack relative threshold", ilu_rtol);
1648
1649 parameter_ml.set("coarse: ifpack level-of-fill", ilu_fill);
1650 parameter_ml.set("coarse: ifpack absolute threshold", ilu_atol);
1651 parameter_ml.set("coarse: ifpack relative threshold", ilu_rtol);
1652 amg_preconditioner = std::make_shared<TrilinosWrappers::PreconditionAMG>();
1653 amg_preconditioner->initialize(system_matrix, parameter_ml);
1654}
1655
1656template <int dim>
1657void
1659 const double absolute_residual,
1660 const double relative_residual)
1661{
1662 const unsigned int max_iter = 3;
1663 unsigned int iter = 0;
1664 bool success = false;
1665
1666 auto &system_rhs = this->system_rhs;
1667
1668 const AffineConstraints<double> &zero_constraints_used =
1669 (!this->simulation_parameters.constrain_solid_domain.enable) ?
1670 this->zero_constraints :
1672
1673 const double rescale_metric = this->get_residual_rescale_metric();
1674 const double current_residual = this->system_rhs.l2_norm() / rescale_metric;
1675 const double linear_solver_tolerance =
1676 std::max(relative_residual * current_residual, absolute_residual);
1677 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1678 .verbosity != Parameters::Verbosity::quiet)
1679 {
1680 this->pcout << " -Tolerance of iterative solver is : "
1681 << linear_solver_tolerance << std::endl;
1682 }
1683 const double non_rescaled_linear_solver_tolerance =
1684 linear_solver_tolerance * rescale_metric;
1685 GlobalVectorType completely_distributed_solution(this->locally_owned_dofs,
1686 this->mpi_communicator);
1687
1688 SolverControl solver_control(this->simulation_parameters.linear_solver
1690 .max_iterations,
1691 non_rescaled_linear_solver_tolerance,
1692 true,
1693 true);
1694 bool extra_verbose = false;
1695 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1697 extra_verbose = true;
1698
1699 TrilinosWrappers::SolverGMRES::AdditionalData solver_parameters(
1700 extra_verbose,
1701 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1702 .max_krylov_vectors);
1703
1704 AssertThrow(
1705 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1706 .preconditioner ==
1708 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1710 ExcMessage(
1711 "This linear solver does not support this preconditioner. Only <ilu> and <amg> preconditioners are supported."));
1712
1713 // The solver starts from the initial fill level for the ILU(n) or the ILU
1714 // smoother provided in the parameter file. If for any reason the linear
1715 // solver crashes, it will restart with a fill level increased by 1. This
1716 // restart happens up to a maximum of 3 times, after which it will let the
1717 // solver crash. If a change happened on the fill level, it will go back
1718 // to its original value at the end of the restart process.
1719 while (success == false and iter < max_iter)
1720 {
1721 try
1722 {
1725
1726 TrilinosWrappers::SolverGMRES solver(solver_control,
1727 solver_parameters);
1728
1729 {
1730 TimerOutput::Scope t(this->computing_timer, "Solve linear system");
1731
1732 if (this->simulation_parameters.linear_solver
1734 .preconditioner ==
1736 solver.solve(system_matrix,
1737 completely_distributed_solution,
1738 system_rhs,
1740 else if (this->simulation_parameters.linear_solver
1742 .preconditioner ==
1744 solver.solve(system_matrix,
1745 completely_distributed_solution,
1746 system_rhs,
1748
1749 if (this->simulation_parameters.linear_solver
1751 .verbosity != Parameters::Verbosity::quiet)
1752 {
1753 this->pcout
1754 << " -Iterative solver took : " << solver_control.last_step()
1755 << " steps to reach a residual norm of "
1756 << solver_control.last_value() / rescale_metric << std::endl;
1757 }
1758 }
1759
1760 this->computing_timer.enter_subsection(
1761 "Distribute constraints after linear solver");
1762
1763 zero_constraints_used.distribute(completely_distributed_solution);
1764
1765 this->computing_timer.leave_subsection(
1766 "Distribute constraints after linear solver");
1767
1768 auto &newton_update = this->newton_update;
1769 newton_update = completely_distributed_solution;
1770 success = true;
1771 }
1772 catch (const std::exception &e)
1773 {
1774 const bool last_attempt = (iter == max_iter - 1);
1775
1776 if (last_attempt && !this->simulation_parameters.linear_solver
1778 .force_linear_solver_continuation)
1779 {
1780 this->pcout << " GMRES solver failed after " << max_iter
1781 << " attempts." << std::endl;
1782 if (const auto *nc =
1783 dynamic_cast<const SolverControl::NoConvergence *>(&e))
1784 this->pcout << " Last attempt stopped at iteration "
1785 << nc->last_step << " with a residual of "
1786 << nc->last_residual / rescale_metric
1787 << " (tolerance " << linear_solver_tolerance << ")."
1788 << std::endl;
1789 throw;
1790 }
1791
1793 this->pcout
1794 << " GMRES solver failed! Trying with a higher preconditioner fill level. New fill = "
1795 << current_preconditioner_fill_level << std::endl;
1797
1798 if (iter == max_iter - 1)
1799 {
1800 if (!this->simulation_parameters.linear_solver
1802 .force_linear_solver_continuation)
1803 throw;
1804
1805 else
1806 {
1807 this->pcout
1808 << " GMRES solver failed! Continuing the Newton step as requested by the <force linear solver continuation> parameter."
1809 << std::endl;
1810
1811 // A failed linear solve may leave the Newton update with
1812 // non-finite entries (e.g. a preconditioner or operator
1813 // breakdown). Continuing with such a Newton update would
1814 // propagate NaNs/Infs through the whole Newton iteration. To
1815 // stay defensive, we reset the Newton update to zero in that
1816 // case so that this Newton step makes no progress instead of
1817 // poisoning the solution. The l2_norm() call is collective
1818 // and triggers the MPI reduction required for a global check
1819 // across all processes.
1820 if (!std::isfinite(this->newton_update.l2_norm()))
1821 {
1822 this->pcout
1823 << " The Newton update contains non-finite values; resetting it to zero."
1824 << std::endl;
1825 this->newton_update = 0.0;
1826 }
1827 }
1828 }
1829 }
1830 iter += 1;
1831 }
1833}
1834
1835
1836template <int dim>
1837void
1839 const double relative_residual)
1840{
1841 const unsigned int max_iter = 3;
1842 unsigned int iter = 0;
1843 bool success = false;
1844
1845 const AffineConstraints<double> &nonzero_constraints =
1846 this->nonzero_constraints;
1847 const double rescale_metric = this->get_residual_rescale_metric();
1848 const double current_residual = this->system_rhs.l2_norm() / rescale_metric;
1849 const double linear_solver_tolerance =
1850 std::max(relative_residual * current_residual, absolute_residual);
1851 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1852 .verbosity != Parameters::Verbosity::quiet)
1853 {
1854 this->pcout << " -Tolerance of iterative solver is : "
1855 << linear_solver_tolerance << std::endl;
1856 }
1857 const double non_rescaled_linear_solver_tolerance =
1858 linear_solver_tolerance * rescale_metric;
1859
1860 GlobalVectorType completely_distributed_solution(this->locally_owned_dofs,
1861 this->mpi_communicator);
1862
1863 SolverControl solver_control(this->simulation_parameters.linear_solver
1865 .max_iterations,
1866 non_rescaled_linear_solver_tolerance,
1867 true,
1868 true);
1869 bool extra_verbose = false;
1870 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1872 extra_verbose = true;
1873
1874 TrilinosWrappers::SolverGMRES::AdditionalData solver_parameters(
1875 extra_verbose,
1876 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1877 .max_krylov_vectors);
1878
1879 AssertThrow(
1880 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1881 .preconditioner ==
1883 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
1885 ExcMessage(
1886 "This linear solver does not support this preconditioner. Only <ilu> and <amg> preconditioners are supported."));
1887
1888 // The solver starts from the initial fill level for the ILU(n) or the ILU
1889 // smoother provided in the parameter file. If for any reason the linear
1890 // solver crashes, it will restart with a fill level increased by 1. This
1891 // restart happens up to a maximum of 3 times, after which it will let the
1892 // solver crash. If a change happened on the fill level, it will go back
1893 // to its original value at the end of the restart process.
1894 while (success == false and iter < max_iter)
1895 {
1896 try
1897 {
1900
1901 TrilinosWrappers::SolverGMRES solver(solver_control,
1902 solver_parameters);
1903
1904 {
1905 TimerOutput::Scope t(this->computing_timer, "Solve linear system");
1906
1907 if (this->simulation_parameters.linear_solver
1909 .preconditioner ==
1911 solver.solve(system_matrix,
1912 completely_distributed_solution,
1913 this->system_rhs,
1915 else if (this->simulation_parameters.linear_solver
1917 .preconditioner ==
1919 solver.solve(system_matrix,
1920 completely_distributed_solution,
1921 this->system_rhs,
1923
1924 if (this->simulation_parameters.linear_solver
1926 .verbosity != Parameters::Verbosity::quiet)
1927 {
1928 this->pcout
1929 << " -Iterative solver took : " << solver_control.last_step()
1930 << " steps to reach a residual norm of "
1931 << solver_control.last_value() / rescale_metric << std::endl;
1932 }
1933 }
1934
1935 this->computing_timer.enter_subsection(
1936 "Distribute constraints after linear solver");
1937
1938 nonzero_constraints.distribute(completely_distributed_solution);
1939
1940 this->computing_timer.leave_subsection(
1941 "Distribute constraints after linear solver");
1942
1943 auto &newton_update = this->newton_update;
1944 newton_update = completely_distributed_solution;
1945 success = true;
1946 }
1947 catch (const std::exception &e)
1948 {
1949 const bool last_attempt = (iter == max_iter - 1);
1950
1951 if (last_attempt && !this->simulation_parameters.linear_solver
1953 .force_linear_solver_continuation)
1954 {
1955 this->pcout
1956 << " GMRES solver failed while solving the L2 projection problem after "
1957 << max_iter << " attempts." << std::endl;
1958 if (const auto *nc =
1959 dynamic_cast<const SolverControl::NoConvergence *>(&e))
1960 this->pcout << " Last attempt stopped at iteration "
1961 << nc->last_step << " with a residual of "
1962 << nc->last_residual / rescale_metric
1963 << " (tolerance " << linear_solver_tolerance << ")."
1964 << std::endl;
1965 throw;
1966 }
1967
1969 this->pcout
1970 << " GMRES solver failed while solving the L2 projection problem! Trying with a higher preconditioner fill level. New fill = "
1971 << current_preconditioner_fill_level << std::endl;
1973 }
1974 iter += 1;
1975 }
1977}
1978
1979// The solver starts from the initial fill level provided in the parameter file.
1980// If for any reason the linear solver crashes, it will restart with a fill
1981// level increased by 1. This restart happens up to a maximum of 3 times, after
1982// which it will let the solver crash. If a change happened on the fill level,
1983// it will go back to its original value at the end of the restart process.
1984template <int dim>
1985void
1987 const double absolute_residual,
1988 const double relative_residual)
1989{
1990 TimerOutput::Scope t(this->computing_timer, "solve");
1991 const unsigned int max_iter = 3;
1992 unsigned int iter = 0;
1993 bool success = false;
1994
1995
1996 auto &system_rhs = this->system_rhs;
1997
1998 const AffineConstraints<double> &zero_constraints_used =
1999 (!this->simulation_parameters.constrain_solid_domain.enable) ?
2000 this->zero_constraints :
2002
2003 const double rescale_metric = this->get_residual_rescale_metric();
2004 const double current_residual = this->system_rhs.l2_norm() / rescale_metric;
2005 const double linear_solver_tolerance =
2006 std::max(relative_residual * current_residual, absolute_residual);
2007 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2008 .verbosity != Parameters::Verbosity::quiet)
2009 {
2010 this->pcout << " -Tolerance of iterative solver is : "
2011 << linear_solver_tolerance << std::endl;
2012 }
2013 const double non_rescaled_linear_solver_tolerance =
2014 linear_solver_tolerance * rescale_metric;
2015 GlobalVectorType completely_distributed_solution(this->locally_owned_dofs,
2016 this->mpi_communicator);
2017
2018 bool extra_verbose = false;
2019 if (this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2021 extra_verbose = true;
2022 TrilinosWrappers::SolverBicgstab::AdditionalData solver_parameters(
2023 extra_verbose);
2024
2025 SolverControl solver_control(this->simulation_parameters.linear_solver
2027 .max_iterations,
2028 non_rescaled_linear_solver_tolerance,
2029 true,
2030 true);
2031 TrilinosWrappers::SolverBicgstab solver(solver_control, solver_parameters);
2032
2033 AssertThrow(
2034 this->simulation_parameters.linear_solver.at(PhysicsID::fluid_dynamics)
2036 ExcMessage(
2037 "This linear solver does not support this preconditioner. Only <ilu> preconditioner is supported."));
2038
2039 while (success == false and iter < max_iter)
2040 {
2041 try
2042 {
2043 if (!ilu_preconditioner)
2045
2046 {
2047 TimerOutput::Scope t(this->computing_timer, "Solve linear system");
2048
2049 if (this->simulation_parameters.linear_solver
2051 .preconditioner ==
2053 solver.solve(system_matrix,
2054 completely_distributed_solution,
2055 system_rhs,
2057
2058 if (this->simulation_parameters.linear_solver
2060 .verbosity != Parameters::Verbosity::quiet)
2061 {
2062 this->pcout
2063 << " -Iterative solver took : " << solver_control.last_step()
2064 << " steps to reach a residual norm of "
2065 << solver_control.last_value() / rescale_metric << std::endl;
2066 }
2067 zero_constraints_used.distribute(completely_distributed_solution);
2068 this->newton_update = completely_distributed_solution;
2069 }
2070 success = true;
2071 }
2072 catch (const std::exception &e)
2073 {
2074 const bool last_attempt = (iter == max_iter - 1);
2075
2076 if (last_attempt && !this->simulation_parameters.linear_solver
2078 .force_linear_solver_continuation)
2079 {
2080 this->pcout << " BiCGStab solver failed after " << max_iter
2081 << " attempts." << std::endl;
2082 if (const auto *nc =
2083 dynamic_cast<const SolverControl::NoConvergence *>(&e))
2084 {
2085 this->pcout << " Last attempt stopped at iteration "
2086 << nc->last_step << " with a residual of "
2087 << nc->last_residual / rescale_metric
2088 << " (tolerance " << linear_solver_tolerance
2089 << ")." << std::endl;
2090
2091 if (!std::isfinite(nc->last_residual))
2092 this->pcout
2093 << " The residual is not finite, which indicates a breakdown of the BiCGStab iteration or of the preconditioner."
2094 << std::endl;
2095 else if (nc->last_step < solver_control.max_steps())
2096 this->pcout
2097 << " The iteration budget was not exhausted, which points to a BiCGStab breakdown rather than slow convergence."
2098 << std::endl;
2099 }
2100 throw;
2101 }
2103 this->pcout
2104 << " BiCGStab solver failed! Trying with a higher preconditioner fill level. New fill = "
2105 << current_preconditioner_fill_level << std::endl;
2107 }
2108 iter += 1;
2109 }
2111}
2112
2113template <int dim>
2114void
2116 const double absolute_residual,
2117 const double relative_residual)
2118{
2119 auto &system_rhs = this->system_rhs;
2120
2121 const AffineConstraints<double> &zero_constraints_used =
2122 (!this->simulation_parameters.constrain_solid_domain.enable) ?
2123 this->zero_constraints :
2125
2126 const double rescale_metric = this->get_residual_rescale_metric();
2127 const double current_residual = this->system_rhs.l2_norm() / rescale_metric;
2128 const double linear_solver_tolerance =
2129 std::max(relative_residual * current_residual, absolute_residual);
2130 const double non_rescaled_linear_solver_tolerance =
2131 linear_solver_tolerance * rescale_metric;
2132
2133 GlobalVectorType completely_distributed_solution(this->locally_owned_dofs,
2134 this->mpi_communicator);
2135
2136 SolverControl solver_control(this->simulation_parameters.linear_solver
2138 .max_iterations,
2139 non_rescaled_linear_solver_tolerance,
2140 true,
2141 true);
2142 TrilinosWrappers::SolverDirect solver(solver_control);
2143
2144 solver.initialize(system_matrix);
2145 solver.solve(completely_distributed_solution, system_rhs);
2146 zero_constraints_used.distribute(completely_distributed_solution);
2147 auto &newton_update = this->newton_update;
2148 newton_update = completely_distributed_solution;
2149}
2150
2151template <int dim>
2152void
2154 unsigned int stage,
2156{
2157 // Copy the reference to some of the vectors to enhance readability below
2158 auto &previous_k_j_solutions = this->sdirk_vectors.previous_k_j_solutions;
2159 auto &sum_over_previous_stages = this->sdirk_vectors.sum_over_previous_stages;
2160 auto &local_sum_over_previous_stages =
2161 this->sdirk_vectors.local_sum_over_previous_stages;
2162 auto &locally_owned_for_calculation =
2163 this->sdirk_vectors.locally_owned_for_calculation;
2164
2165 // If a SDIRK method is selected, we need to solve as many
2166 // nonlinear systems as the number of stages.
2167
2168 // At each stage, the value of the (a_ij), (b_i) and (c_i)
2169 // coefficients are differents
2170
2171 SDIRKTable table = sdirk_table(method);
2172 SDIRKStageData stage_data(table, stage + 1);
2173 const double a_ii = stage_data.a_ij[stage];
2174
2175 // At each stage, we need to recompute sum(a_{ij} * k_j)
2176 // = sum_over_previous_stages
2177 local_sum_over_previous_stages = 0;
2178
2179 if (stage > 0)
2180 {
2181 // At the first stage, the sum_over_previous_stages is set to
2182 // 0. But for the next stages, we need to update this
2183 // sum_over_previous_stages
2184 for (unsigned int p = 0; p < stage; ++p)
2185 {
2186 locally_owned_for_calculation = previous_k_j_solutions[p];
2187 local_sum_over_previous_stages.add(stage_data.a_ij[p] / a_ii,
2188 locally_owned_for_calculation);
2189 }
2190 }
2191
2192 sum_over_previous_stages = local_sum_over_previous_stages;
2193}
2194
2195template <int dim>
2196void
2198 unsigned int stage,
2200 double time_step)
2201{
2202 auto &present_solution = *this->present_solution;
2205
2206 auto &local_sum_over_previous_stages =
2207 this->sdirk_vectors.local_sum_over_previous_stages;
2208
2209 auto &locally_owned_for_calculation =
2210 this->sdirk_vectors.locally_owned_for_calculation;
2211
2212 auto &previous_k_j_solutions = this->sdirk_vectors.previous_k_j_solutions;
2213
2214 auto &sum_bi_ki = this->sdirk_vectors.sum_bi_ki;
2215 auto &local_sum_bi_ki = this->sdirk_vectors.local_sum_bi_ki;
2216
2217 SDIRKTable table = sdirk_table(method);
2218 SDIRKStageData stage_data(table, stage + 1);
2219 const double a_ii = stage_data.a_ij[stage];
2220
2221 // Once we have solved the nonlinear system for the velocity, we
2222 // want to store the value of the coefficient k_i for the final
2223 // sum b_i*k_i with k_i = (u*_{i} - u_{n})/(time_step*a_ii) -
2224 // sum_over_previous_stages
2225 locally_owned_for_calculation = previous_solutions[0];
2227 local_evaluation_point.add(-1.0, locally_owned_for_calculation);
2228 local_evaluation_point *= 1.0 / (time_step * a_ii);
2229 local_evaluation_point.add(-1.0, local_sum_over_previous_stages);
2230
2231 // We store the value of the present_k_i_solution in the
2232 // previous_k_j_solutions vector.
2233 previous_k_j_solutions[stage] = local_evaluation_point;
2234
2235 // We update the sum of b_i*k_i
2236 const double b_i = stage_data.b_i;
2237 local_sum_bi_ki.add(b_i, local_evaluation_point);
2238 sum_bi_ki = local_sum_bi_ki;
2239}
2240
2241template <int dim>
2242void
2244{
2245 auto &present_solution = *this->present_solution;
2248
2249 // The following variables are used for the SDIRK method
2250 // u_{n+1} = u_n + sum(b_i * k_i)
2251 auto &sum_bi_ki = this->sdirk_vectors.sum_bi_ki;
2252 auto &local_sum_bi_ki = this->sdirk_vectors.local_sum_bi_ki;
2253
2254 // At each time iteration, we update the value of present_solution
2255 local_sum_bi_ki = sum_bi_ki;
2257 local_evaluation_point.add(time_step, local_sum_bi_ki);
2259}
2260
2261template <int dim>
2262void
2264{
2265 this->computing_timer.enter_subsection("Read mesh and manifolds");
2266
2267 if (this->simulation_parameters.mortar_parameters.enable)
2268 {
2269 // Mortar load balancing
2271
2273 *this->triangulation,
2274 this->simulation_parameters.mesh,
2275 this->simulation_parameters.manifolds_parameters,
2276 this->simulation_parameters.restart_parameters.restart,
2277 this->simulation_parameters.boundary_conditions.periodic_boundaries,
2278 this->simulation_parameters.mortar_parameters);
2279 // Create and initialize mapping cache
2280 this->mapping_cache =
2281 std::make_shared<MappingQCache<dim>>(this->velocity_fem_degree);
2282 this->rotate_rotor_mapping(true);
2283 }
2284 else
2286 *this->triangulation,
2287 this->simulation_parameters.mesh,
2288 this->simulation_parameters.manifolds_parameters,
2289 this->simulation_parameters.restart_parameters.restart,
2290 this->simulation_parameters.boundary_conditions.periodic_boundaries);
2291
2292 this->computing_timer.leave_subsection("Read mesh and manifolds");
2293
2294 this->setup_dofs();
2296 this->box_refine_mesh(this->simulation_parameters.restart_parameters.restart);
2298 this->simulation_parameters.initial_condition->type,
2299 this->simulation_parameters.restart_parameters.restart);
2301
2302 while (this->simulation_control->integrate())
2303 {
2304 // We allow the physics to update their forcing and boundary conditions
2305 // according to their own parameters
2307 this->multiphysics->update_boundary_conditions();
2308
2309 this->simulation_control->print_progression(this->pcout);
2310 this->dynamic_flow_control();
2312
2313 if (!this->simulation_control->is_at_start())
2315
2317 this->iterate();
2318 this->postprocess(false);
2319 this->finish_time_step();
2320 }
2321
2322
2323 this->finish_simulation();
2324}
2325
2326
2327// Pre-compile the 2D and 3D Navier-Stokes solver to ensure that the library is
2328// valid before we actually compile the solver This greatly helps with debugging
2329template class FluidDynamicsMatrixBased<2>;
2330template class FluidDynamicsMatrixBased<3>;
A solver class for the Navier-Stokes equation using GLS stabilization.
void define_dynamic_zero_constraints()
Define the zero constraints used to solved the problem that change with other physics' solutions.
virtual void solve()
solve Solves the Navier-Stokes problem
TrilinosWrappers::SparseMatrix system_matrix
void assemble_L2_projection()
Assembles an L2_projection matrix for the velocity and the pressure. This L2 projection matrix can be...
virtual void copy_local_rhs_to_global_rhs(const StabilizedMethodsTensorCopyData< dim > &copy_data)
Copy local cell rhs information to global rhs.
virtual void update_multiphysics_time_average_solution() override
Updates the average velocity field solution in the multiphyscics interface.
std::shared_ptr< TrilinosWrappers::PreconditionAMG > amg_preconditioner
void solve_linear_system() override
Call for the assembly of the linear system of equation.
void setup_AMG()
Set-up AMG preconditioner.
virtual void setup_assemblers()
sets up the vector of assembler functions
void define_pressure_constraints()
Fix a constant pressure on the minimum dof index. This can be enabled in the simulation control param...
virtual void set_initial_condition_fd(Parameters::FluidDynamicsInitialConditionType initial_condition_type, bool restart=false) override
Sets the initial condition for the solver.
std::shared_ptr< TrilinosWrappers::PreconditionILU > ilu_preconditioner
virtual void multi_stage_postresolution(unsigned int stage, Parameters::SimulationControl::TimeSteppingMethod method, double time_step) override
Calculate from Update .
virtual void assemble_local_system_rhs(const typename DoFHandler< dim >::active_cell_iterator &cell, NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data)
Assemble the local rhs for a given cell.
virtual void update_mortar_configuration() override
Update mortar configuration.
void solve_system_BiCGStab(const double absolute_residual, const double relative_residual)
BiCGStab solver with ILU(N) preconditioning.
void solve_system_direct(const double absolute_residual, const double relative_residual)
Direct solver using TrilinosWrappers::SolverDirect The use of this solver should be avoided for 3D pr...
virtual void assemble_system_matrix() override
Assembles the matrix associated with the solver.
void setup_preconditioner() override
Set-up the appropriate preconditioner.
void multi_stage_preresolution(unsigned int stage, Parameters::SimulationControl::TimeSteppingMethod method) override
Update the sum_over_previous_stages variable to use it in the solver.
void setup_ILU()
Set-up ILU preconditioner.
virtual void setup_dofs_fd() override
setup_dofs_fd Setup the degree of freedom, system matrix and solution vectors Navier-Stokes problem
void solve_L2_system(const double absolute_residual, const double relative_residual)
Solver for the L2 Projection linear system at the initial time step to apply this initial condition....
void solve_system_GMRES(const double absolute_residual, const double relative_residual)
GMRES solver with ILU(N) preconditioning or AMG preconditioning.
virtual void copy_local_matrix_to_global_matrix(const StabilizedMethodsTensorCopyData< dim > &copy_data)
Copy local cell information to global matrix.
virtual void update_multi_stage_solution(double time_step) override
virtual void assemble_system_rhs() override
Assemble the rhs associated with the solver.
virtual void assemble_local_system_matrix(const typename DoFHandler< dim >::active_cell_iterator &cell, NavierStokesScratchData< dim > &scratch_data, StabilizedMethodsTensorCopyData< dim > &copy_data)
Assemble the local matrix for a given cell.
FluidDynamicsMatrixBased(SimulationParameters< dim > &nsparam, const FluidDynamicsFormulation fluid_dynamics_formulation=FluidDynamicsFormulation::standard)
Construct the matrix-based Navier-Stokes solver.
Class that assembles the transient time arising from BDF time integration for the isothermal compress...
Class that assembles the core of the isothermal compressible Navier-Stokes equations....
Class that assembles the core of the Navier-Stokes equation with free surface using CLS modeling....
Class that assembles the transient time arising from BDF time integration for the Navier Stokes equat...
Class that assembles the core of the Navier-Stokes equation. According to the following weak form: ...
Class that assembles the core of the Navier-Stokes equation using a Rheological model to predict non-...
Class that assembles the transient term arising from SDIRK time integration for the Navier Stokes equ...
Class that assembles the coriolis and the centrifugal if a simulation is carried out in a rotating fr...
Assembles the transient time arising from BDF time integration for the Navier-Stokes equation with fr...
Assembles the core of the Navier-Stokes equation with free surface using CLS modeling....
Assembles the Marangoni effect for the Navier-Stokes equations according to the following equation:
Assembles the core of the Navier-Stokes equation using a Rheological model to predict non-Newtonian b...
Assembles the Surface Tension Force (STF) for the Navier-Stokes equations according to the following ...
Class that assembles the transient time arising from BDF time integration for the Navier-Stokes equat...
Class that assembles the core of the Navier-Stokes equation with free surface using Cahn-Hilliard mod...
std::vector< StasisConstraintWithTemperature > stasis_constraint_structs
void constrain_stasis_with_temperature(const DoFHandler< dim > *dof_handler_ht)
std::shared_ptr< Quadrature< dim > > cell_quadrature
virtual void set_initial_condition(const Parameters::FluidDynamicsInitialConditionType initial_condition_type, const bool restart=false)
void constrain_stasis_with_temperature_cls(const DoFHandler< dim > *dof_handler_cls, const DoFHandler< dim > *dof_handler_ht)
std::shared_ptr< GlobalVectorType > present_solution
virtual AffineConstraints< double > & get_nonzero_constraints() override
virtual void refine_mesh()
Allow the refinement of the mesh according to one of the 2 methods proposed.
bool check_existance_of_bc(BoundaryConditions::FluidDynamicsBoundaryType bc)
std::shared_ptr< CouplingOperator< dim, double > > mortar_coupling_operator
std::shared_ptr< std::vector< GlobalVectorType > > previous_solutions
std::shared_ptr< MappingQCache< dim > > mapping_cache
std::shared_ptr< SimulationControl > simulation_control
std::vector< std::shared_ptr< NavierStokesAssemblerBase< dim > > > assemblers
std::shared_ptr< MultiphysicsInterface< dim > > multiphysics
virtual void set_solution_from_checkpoint(const std::string &checkpoint_file_prefix)
NavierStokesBase(SimulationParameters< dim > &nsparam, const FluidDynamicsFormulation fluid_dynamics_formulation=FluidDynamicsFormulation::standard)
std::shared_ptr< AverageVelocities< dim, GlobalVectorType, IndexSet > > average_velocities
std::shared_ptr< DoFHandler< dim > > dof_handler
std::shared_ptr< parallel::DistributedTriangulationBase< dim > > triangulation
Assembles the momentum source due to evaporation for the Navier-Stokes equations.
Class that stores the information required by the assembly procedure for a Navier-Stokes equation....
void reinit_mortar(const typename DoFHandler< dim >::active_cell_iterator &cell, const Parameters::Mortar< dim > &mortar_parameters)
Renitialize the content of the scratch data for mortar.
void reinit_projected_phase_indicator_gradient(const typename DoFHandler< dim >::active_cell_iterator &projected_phase_indicator_gradient_cell, const VectorType &current_projected_phase_indicator_gradient_solution)
void calculate_physical_properties()
Calculates the physical properties. This function calculates the physical properties that may be requ...
void reinit(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const std::vector< VectorType > &previous_solutions, const VectorType &sum_over_previous_stages, const std::shared_ptr< Function< dim > > &forcing_function, const Tensor< 1, dim > &beta_force, const double pressure_scaling_factor)
Reinitializes the content of the scratch.
void reinit_heat_transfer(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const std::vector< VectorType > &previous_solutions)
Reinitialize the content of the scratch for the heat transfer auxiliary physic.
void reinit_ale(const Parameters::ALE< dim > &ale)
Renitialize the content of the scratch data for ALE.
void reinit_cahn_hilliard(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const VectorType &current_filtered_solution)
Reinitialize the content of the scratch for CH.
void reinit_cls(const typename DoFHandler< dim >::active_cell_iterator &cell, const VectorType &current_solution, const VectorType &current_filtered_solution, const std::vector< VectorType > &previous_solutions)
Reinitialize the content of the scratch for the cls.
void reinit_curvature(const typename DoFHandler< dim >::active_cell_iterator &curvature_cell, const VectorType &current_curvature_solution)
Class that assembles a Neumann traction boundary condition. According to the following weak form: a...
Class that assembles the weak formulation of an outlet boundary condition according to the following ...
Class that assembles the core of the Navier-Stokes equation. According to the following weak form: ...
Class that assembles the special case of partial slip condition using the weak formulation of a Diric...
Assembler of the non-linear Carman-Kozeny permeability model for simulations with phase change.
Assembler of the non-linear Carman-Kozeny permeability model for CLS simulations with phase change.
Class that assembles a phase change Darcy forcing term. This term adds to the right hand-side of the...
Assembles a phase change Darcy forcing term for a CLS two-fluid simulation. The term is added to the...
void solve_governing_system()
Solve the global system of equations according to a given strategy, either linear or not.
Class that assembles a Neumann boundary condition. According to the following weak form: (p-mu*grad_u...
std::vector< double > a_ij
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
void reset()
Resets the cell_matrix, cell_rhs, strong_residual and strong_jacobian to zero.
Definition copy_data.h:190
std::vector< types::global_dof_index > local_dof_indices
Definition copy_data.h:206
Assembles the thermal buoyancy source term for CLS simulations. This assembler includes density expli...
Class that assembles a thermal buoyancy forcing term using the Boussinesq approximation....
Class that assembles the weak formulation of a Dirichlet boundary condition using the Nitsche method....
void announce_string(const ConditionalOStream &pcout, const std::string &expression, const char delimiter='-')
Prints a string and then adds a line above and below made with dashes containing as many dashes as th...
Definition utilities.h:500
void read_mesh_and_manifolds_for_stator_and_rotor(parallel::DistributedTriangulationBase< dim, spacedim > &triangulation, const Parameters::Mesh< dim, spacedim > &mesh_parameters, const Parameters::Manifolds &manifolds_parameters, bool restart, const Parameters::PeriodicBoundaries &periodic_boundaries, const Parameters::Mortar< dim > &mortar_parameters)
Completely sets up a mesh and its manifolds for rotor-stator domains.
Definition grids.cc:501
void read_mesh_and_manifolds(parallel::DistributedTriangulationBase< dim, spacedim > &triangulation, const Parameters::Mesh< dim, spacedim > &mesh_parameters, const Parameters::Manifolds &manifolds_parameters, bool restart, const Parameters::PeriodicBoundaries &periodic_boundaries)
Completely sets up a mesh and its manifolds.
Definition grids.cc:447
Identifiers for multiphysics modules and their solution fields.
@ pressure
Pressure scalar field from fluid dynamics.
FluidDynamicsFormulation
Formulation of the fluid dynamics equations solved by a Navier-Stokes solver.
@ fluid_dynamics
Fluid Dynamics (Velocity,Pressure).
@ CLS
@ heat_transfer
Heat transfer (Temperature).
@ cahn_hilliard
Cahn-Hilliard (Phase indicator, Potential).
void read_checkpoint(TimerOutput &computing_timer, const DEMSolverParameters< dim > &parameters, std::shared_ptr< SimulationControl > &simulation_control, PVDHandler &particles_pvdhandler, PVDHandler &grid_pvdhandler, parallel::distributed::Triangulation< dim > &triangulation, Particles::ParticleHandler< dim > &particle_handler, std::shared_ptr< Insertion< dim, PropertiesIndex > > &insertion_object, std::vector< std::shared_ptr< SerialSolid< dim - 1, dim > > > &solid_surfaces, CheckpointControl &checkpoint_controller)
Read a DEM simulation checkpoint, allowing the simulation to restart from where it stopped.
SDIRKTable sdirk_table(const Parameters::SimulationControl::TimeSteppingMethod method)
Construct the Butcher tableau (A, b, c) for a given SDIRK method.
@ ilu
Incomplete LU Factorization.
Structure representing a Butcher tableau for a SDIRK method.
Struct containing fluid id, temperature and phase indicator range information, and flag containers fo...
bool time_stepping_is_bdf(const Parameters::SimulationControl::TimeSteppingMethod method)
Determines if the time integration method is within the bdf family.
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17