Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
navier_stokes_base.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>
12#include <core/utilities.h>
13
20
21#include <deal.II/distributed/fully_distributed_tria.h>
22#include <deal.II/distributed/grid_refinement.h>
23
24#include <deal.II/dofs/dof_renumbering.h>
25
26#include <deal.II/fe/fe_q.h>
27#include <deal.II/fe/fe_q_bubbles.h>
28#include <deal.II/fe/fe_simplex_p.h>
29
30#include <deal.II/grid/tria_iterator.h>
31
32#include <deal.II/numerics/data_out.h>
33#include <deal.II/numerics/data_out_faces.h>
34#include <deal.II/numerics/data_out_resample.h>
35#include <deal.II/numerics/error_estimator.h>
36#include <deal.II/numerics/matrix_tools.h>
37#include <deal.II/numerics/solution_transfer.h>
38#include <deal.II/numerics/vector_tools.h>
39
40#include <deal.II/opencascade/manifold_lib.h>
41#include <deal.II/opencascade/utilities.h>
42
43#include <sys/stat.h>
44
45/*
46 * Constructor for the Navier-Stokes base class
47 */
48template <int dim, typename VectorType, typename DofsType>
51 const FluidDynamicsFormulation fluid_dynamics_formulation)
54 , mpi_communicator(MPI_COMM_WORLD)
57 , dof_handler()
59 this->pcout,
60 TimerOutput::never,
61 TimerOutput::wall_times)
62 , simulation_parameters(p_nsparam)
64 , velocity_fem_degree(p_nsparam.fem_parameters.velocity_degree)
65 , pressure_fem_degree(p_nsparam.fem_parameters.pressure_degree)
66 , number_quadrature_points(p_nsparam.fem_parameters.velocity_degree + 1)
67 , mesh_controller(p_nsparam.mesh_adaptation.maximum_number_elements)
68{
69 if (simulation_parameters.mesh.simplex)
70 {
71 // for simplex meshes
72 const FE_SimplexP<dim> velocity_fe(
73 p_nsparam.fem_parameters.velocity_degree);
74 const FE_SimplexP<dim> pressure_fe(
75 p_nsparam.fem_parameters.pressure_degree);
76 fe = std::make_shared<FESystem<dim>>(velocity_fe, dim, pressure_fe, 1);
77
78 AssertThrow(
79 !p_nsparam.fem_parameters.enable_bubble_function_velocity ||
80 !p_nsparam.fem_parameters.enable_bubble_function_pressure,
81 ExcMessage(
82 "Bubble enrichment functions are not compatible with simplex meshes."));
83
84 mapping = std::make_shared<MappingFE<dim>>(velocity_fe);
85 cell_quadrature =
86 std::make_shared<QGaussSimplex<dim>>(number_quadrature_points);
87 face_quadrature =
88 std::make_shared<QGaussSimplex<dim - 1>>(number_quadrature_points);
89 triangulation =
90 std::make_shared<parallel::fullydistributed::Triangulation<dim>>(
91 this->mpi_communicator);
92 dof_handler = std::make_shared<DoFHandler<dim>>(*this->triangulation);
93 }
94 else
95 {
96 // Usual case, for quad/hex meshes
97 std::shared_ptr<FiniteElement<dim>> fe_u;
98 std::shared_ptr<FiniteElement<dim>> fe_p;
99
100 if (p_nsparam.fem_parameters.enable_bubble_function_velocity)
101 fe_u = std::make_shared<FE_Q_Bubbles<dim>>(
102 p_nsparam.fem_parameters.velocity_degree);
103 else
104 fe_u =
105 std::make_shared<FE_Q<dim>>(p_nsparam.fem_parameters.velocity_degree);
106
107 if (p_nsparam.fem_parameters.enable_bubble_function_pressure)
108 fe_p = std::make_shared<FE_Q_Bubbles<dim>>(
109 p_nsparam.fem_parameters.pressure_degree);
110 else
111 fe_p =
112 std::make_shared<FE_Q<dim>>(p_nsparam.fem_parameters.pressure_degree);
113
114 fe = std::make_shared<FESystem<dim>>(*fe_u, dim, *fe_p, 1);
115 mapping = std::make_shared<MappingQ<dim>>(velocity_fem_degree);
116 cell_quadrature = std::make_shared<QGauss<dim>>(number_quadrature_points);
117 face_quadrature =
118 std::make_shared<QGauss<dim - 1>>(number_quadrature_points);
119 triangulation =
120 std::make_shared<parallel::distributed::Triangulation<dim>>(
121 this->mpi_communicator,
122 Triangulation<dim>::limit_level_difference_at_vertices,
123 (p_nsparam.linear_solver.at(fluid_dynamics).preconditioner ==
124 Parameters::LinearSolver::PreconditionerType::lsmg) ?
125 parallel::distributed::Triangulation<
126 dim>::construct_multigrid_hierarchy :
127 parallel::distributed::Triangulation<dim>::default_setting);
128 dof_handler = std::make_shared<DoFHandler<dim>>(*this->triangulation);
129 }
131 this->pcout.set_condition(
132 Utilities::MPI::this_mpi_process(this->mpi_communicator) == 0);
133
134 // Initialize solution shared_ptr
135 present_solution = std::make_shared<VectorType>();
136
137 // Check if the output directory exists
138 std::string output_dir_name =
139 simulation_parameters.simulation_control.output_folder;
140 struct stat buffer;
141
142 // If output directory does not exist, create it
143 if (Utilities::MPI::this_mpi_process(this->mpi_communicator) == 0)
144 {
145 if (stat(output_dir_name.c_str(), &buffer) != 0)
146 {
147 create_output_folder(output_dir_name);
148 }
149 }
150
151 if (simulation_parameters.simulation_control.method ==
153 simulation_control = std::make_shared<SimulationControlSteady>(
154 simulation_parameters.simulation_control);
155 else if (simulation_parameters.simulation_control.method ==
157 {
158 simulation_control = std::make_shared<SimulationControlAdjointSteady>(
159 simulation_parameters.simulation_control);
160 }
161 else
162 {
163 simulation_control = std::make_shared<SimulationControlTransient>(
164 simulation_parameters.simulation_control);
165 }
166
167 // Provide the simulation control object to the physical property manager
168 simulation_parameters.physical_properties_manager.provide_simulation_control(
170
175 this->pcout,
176 fluid_dynamics_formulation);
177
178 // If mortar is enabled, we need to change one default parameter of the
179 // function that computes normal vectors. In this case, we cannot use the
180 // manifold to compute them, since it leads to issues at the mortar interface.
181 // Hence we use the mapping instead.
182 this->use_manifold_for_normal = true;
183 if (this->simulation_parameters.mortar_parameters.enable)
184 this->use_manifold_for_normal = false;
185
186 // The rotor position of the mortar method is only updated once per time
187 // step, whereas the stages of an SDIRK method require it at the stage times.
188 AssertThrow(!(this->simulation_parameters.mortar_parameters.enable &&
189 this->simulation_control->is_sdirk()),
190 ExcMessage("SDIRK time-stepping methods are not supported with "
191 "the mortar method. Use a BDF method instead."));
192
193 if (this->simulation_control->is_sdirk())
194 {
195 // Pre-allocate memory for the previous k_j solutions using the number of
196 // stages of the method
197 sdirk_vectors.previous_k_j_solutions.resize(
198 SimulationControl::get_number_of_stages(
199 this->simulation_control->get_assembly_method()));
200 }
201 // Pre-allocate memory for the previous solutions using the information
202 // of the BDF schemes
203 previous_solutions = std::make_shared<std::vector<VectorType>>(
205
206 // Get the exact solution from the parser
207 exact_solution = &simulation_parameters.analytical_solution->uvwp;
208
209 // If there is a forcing function, get it from the parser
210 forcing_function = simulation_parameters.source_term.navier_stokes_source;
211
212 if (this->simulation_parameters.post_processing
213 .calculate_average_velocities ||
214 this->simulation_parameters.initial_condition->type ==
218 *dof_handler);
219
220 this->pcout << "Running on "
221 << Utilities::MPI::n_mpi_processes(this->mpi_communicator)
222 << " MPI rank(s)..." << std::endl;
223
224 this->pcout << std::setprecision(
225 simulation_parameters.simulation_control.log_precision)
226 << std::scientific;
227}
228
229template <int dim, typename VectorType, typename DofsType>
230void
232{
233 if (simulation_parameters.flow_control.enable_flow_control &&
234 simulation_parameters.simulation_control.method !=
236 {
237 // Calculate the average velocity
238 double average_velocity = calculate_average_velocity(
239 *this->dof_handler,
240 *this->present_solution,
241 simulation_parameters.flow_control.boundary_flow_id,
242 *this->face_quadrature,
243 *this->get_mapping());
244
245 this->flow_control.calculate_beta(
246 average_velocity,
247 simulation_control->get_time_step(),
248 simulation_control->get_iteration_number());
249
250 // Showing results
251 if (simulation_parameters.flow_control.verbosity ==
253 simulation_control->get_iteration_number() > 0 &&
254 this->this_mpi_process == 0)
255 {
256 announce_string(this->pcout, "Flow control summary");
257 this->pcout << "Space-average velocity: " << average_velocity
258 << std::endl;
259 this->pcout
260 << "Beta force: "
261 << flow_control
262 .get_beta()[simulation_parameters.flow_control.flow_direction]
263 << std::endl;
264 }
265 }
266}
267
268template <int dim, typename VectorType, typename DofsType>
269bool
272{
273 bool bc_exist = false;
274 // Loop over the boundary to check if they need assembler on their face.
275 for (auto const &[id, type] :
276 this->simulation_parameters.boundary_conditions.type)
277 {
278 if (type == bc)
279 bc_exist = true;
280 }
281 return bc_exist;
282}
283
284
285// This is a primitive first implementation that could be greatly improved by
286// doing a single pass instead of N boundary passes
287template <int dim, typename VectorType, typename DofsType>
288void
291{
292 TimerOutput::Scope t(this->computing_timer, "Calculate forces");
293
297 simulation_parameters.physical_properties_manager,
298 simulation_parameters.boundary_conditions,
299 *this->face_quadrature,
300 *this->get_mapping());
301
302 if (simulation_parameters.forces_parameters.verbosity ==
304 this->this_mpi_process == 0)
305 {
306 std::cout << std::endl;
307 std::string independent_column_names = "Boundary ID";
309 std::vector<std::string> dependent_column_names;
310 dependent_column_names.emplace_back("f_x");
311 dependent_column_names.emplace_back("f_y");
312 if (dim == 3)
313 dependent_column_names.emplace_back("f_z");
314 dependent_column_names.emplace_back("f_xv");
315 dependent_column_names.emplace_back("f_yv");
316 if (dim == 3)
317 dependent_column_names.emplace_back("f_zv");
318 dependent_column_names.emplace_back("f_xp");
319 dependent_column_names.emplace_back("f_yp");
320 if (dim == 3)
321 dependent_column_names.emplace_back("f_zp");
322
323 std::vector<unsigned int> boundary_ids =
324 extract_keys_from_map(simulation_parameters.boundary_conditions.type);
325
326 std::vector<Tensor<1, dim>> total_forces =
328
329 std::vector<Tensor<1, dim>> viscous_forces =
331
332 std::vector<Tensor<1, dim>> pressure_forces =
334
335 std::vector<std::vector<Tensor<1, dim>>> all_forces{total_forces,
336 viscous_forces,
337 pressure_forces};
338
339 TableHandler table = make_table_scalars_tensors(
340 boundary_ids,
341 independent_column_names,
342 all_forces,
343 dependent_column_names,
344 this->simulation_parameters.simulation_control.log_precision,
345 true);
346
347 std::cout << "+------------------------------------------+" << std::endl;
348 std::cout << "| Force summary |" << std::endl;
349 std::cout << "+------------------------------------------+" << std::endl;
350 table.write_text(std::cout);
351 }
352
353 for (auto const &[id, type] : simulation_parameters.boundary_conditions.type)
354 {
355 if (simulation_control->is_steady())
356 {
357 this->forces_tables[id].add_value(
358 "cells", this->triangulation->n_global_active_cells());
359 }
360 else
361 {
362 this->forces_tables[id].add_value(
363 "time", simulation_control->get_current_time());
364 this->forces_tables[id].set_precision(
365 "time", simulation_parameters.forces_parameters.output_precision);
367
368 this->forces_tables[id].add_value("f_x",
369 this->forces_on_boundaries[0][id][0]);
370 this->forces_tables[id].add_value("f_y",
371 this->forces_on_boundaries[0][id][1]);
372 if (dim == 3)
373 this->forces_tables[id].add_value("f_z",
374 this->forces_on_boundaries[0][id][2]);
375 else
376 this->forces_tables[id].add_value("f_z", 0.);
377
378 this->forces_tables[id].add_value("f_xv",
379 this->forces_on_boundaries[1][id][0]);
380 this->forces_tables[id].add_value("f_yv",
381 this->forces_on_boundaries[1][id][1]);
382 if (dim == 3)
383 this->forces_tables[id].add_value("f_zv",
384 this->forces_on_boundaries[1][id][2]);
385 else
386 this->forces_tables[id].add_value("f_zv", 0.);
387
388 this->forces_tables[id].add_value("f_xp",
389 this->forces_on_boundaries[2][id][0]);
390 this->forces_tables[id].add_value("f_yp",
391 this->forces_on_boundaries[2][id][1]);
392 if (dim == 3)
393 this->forces_tables[id].add_value("f_zp",
394 this->forces_on_boundaries[2][id][2]);
395 else
396 this->forces_tables[id].add_value("f_zp", 0.);
397
398 // Precision
399 this->forces_tables[id].set_precision(
400 "f_x", simulation_parameters.forces_parameters.output_precision);
401 this->forces_tables[id].set_precision(
402 "f_y", simulation_parameters.forces_parameters.output_precision);
403 this->forces_tables[id].set_precision(
404 "f_z", simulation_parameters.forces_parameters.output_precision);
405
406 this->forces_tables[id].set_precision(
407 "f_xv", simulation_parameters.forces_parameters.output_precision);
408 this->forces_tables[id].set_precision(
409 "f_yv", simulation_parameters.forces_parameters.output_precision);
410 this->forces_tables[id].set_precision(
411 "f_zv", simulation_parameters.forces_parameters.output_precision);
413 this->forces_tables[id].set_precision(
414 "f_xp", simulation_parameters.forces_parameters.output_precision);
415 this->forces_tables[id].set_precision(
416 "f_yp", simulation_parameters.forces_parameters.output_precision);
417 this->forces_tables[id].set_precision(
418 "f_zp", simulation_parameters.forces_parameters.output_precision);
419 }
420}
421
422
423template <int dim, typename VectorType, typename DofsType>
424void
427{
428 TimerOutput::Scope t(this->computing_timer, "Calculate torques");
429
430 std::map<types::boundary_id, Tensor<1, 3>> torques_on_boundaries =
433 simulation_parameters.physical_properties_manager,
434 simulation_parameters.boundary_conditions,
435 *this->face_quadrature,
436 *this->get_mapping());
437
438 if (simulation_parameters.forces_parameters.verbosity ==
440 this->this_mpi_process == 0)
442 this->pcout << std::endl;
443 std::string independent_column_names = "Boundary ID";
444
445 std::vector<std::string> dependent_column_names;
446 dependent_column_names.emplace_back("T_x");
447 dependent_column_names.emplace_back("T_y");
448 dependent_column_names.emplace_back("T_z");
449
450 std::vector<unsigned int> boundary_ids =
452 std::vector<Tensor<1, 3>> torques =
453 extract_values_from_map(torques_on_boundaries);
454
455 TableHandler table = make_table_scalars_tensors(
456 boundary_ids,
457 independent_column_names,
458 torques,
459 dependent_column_names,
460 this->simulation_parameters.simulation_control.log_precision,
461 true);
462
463 std::cout << "+------------------------------------------+" << std::endl;
464 std::cout << "| Torque summary |" << std::endl;
465 std::cout << "+------------------------------------------+" << std::endl;
466 table.write_text(std::cout);
467 }
468
469 for (auto const &[boundary_id, type] :
470 simulation_parameters.boundary_conditions.type)
471 {
472 if (simulation_control->is_steady())
473 {
474 this->torques_tables[boundary_id].add_value(
475 "cells", this->triangulation->n_global_active_cells());
476 }
477 else
478 {
479 this->torques_tables[boundary_id].add_value(
480 "time", simulation_control->get_current_time());
481 this->torques_tables[boundary_id].set_precision(
482 "time", simulation_parameters.forces_parameters.output_precision);
484 this->torques_tables[boundary_id].add_value(
485 "T_x", torques_on_boundaries[boundary_id][0]);
486 this->torques_tables[boundary_id].add_value(
487 "T_y", torques_on_boundaries[boundary_id][1]);
488 this->torques_tables[boundary_id].add_value(
489 "T_z", torques_on_boundaries[boundary_id][2]);
490
491 // Precision
492 this->torques_tables[boundary_id].set_precision(
493 "T_x", simulation_parameters.forces_parameters.output_precision);
494 this->torques_tables[boundary_id].set_precision(
495 "T_y", simulation_parameters.forces_parameters.output_precision);
496 this->torques_tables[boundary_id].set_precision(
497 "T_z", simulation_parameters.forces_parameters.output_precision);
498 }
499}
500
501
502template <int dim, typename VectorType, typename DofsType>
503void
505{
506 if (simulation_parameters.forces_parameters.calculate_force)
507 this->write_output_forces();
508
509 if (simulation_parameters.forces_parameters.calculate_torque)
510 this->write_output_torques();
511
512 if (simulation_parameters.analytical_solution->calculate_error())
513 {
514 if (simulation_parameters.simulation_control.method ==
516 {
517 error_table.set_scientific("error_pressure", true);
518 error_table.omit_column_from_convergence_rate_evaluation("cells");
519 error_table.evaluate_all_convergence_rates(
520 ConvergenceTable::reduction_rate_log2);
521 }
522 error_table.set_scientific("error_velocity", true);
524 if (this->this_mpi_process == 0)
525 {
526 std::string filename =
527 simulation_parameters.simulation_control.output_folder +
528 simulation_parameters.analytical_solution->get_filename() + ".dat";
529 std::ofstream output(filename.c_str());
530 error_table.write_text(output);
531 std::vector<std::string> sub_columns;
532 if (simulation_parameters.simulation_control.method ==
534 {
535 sub_columns.emplace_back("cells");
536 sub_columns.emplace_back("error_velocity");
537 sub_columns.emplace_back("error_pressure");
538 error_table.set_precision(
539 "error_pressure", simulation_control->get_log_precision());
540 error_table.set_column_order(sub_columns);
542 error_table.set_precision("error_velocity",
543 simulation_control->get_log_precision());
544
545 error_table.write_text(std::cout);
546 }
548
549 if (this->simulation_parameters.timer.type == Parameters::Timer::Type::end)
550 {
551 announce_string(this->pcout, "Fluid Dynamics");
552 this->pcout << std::defaultfloat;
553 this->computing_timer.print_summary();
554 this->pcout << std::scientific;
555 }
556}
557
558template <int dim, typename VectorType, typename DofsType>
559void
561{
562 for (unsigned int i = previous_solutions->size() - 1; i > 0; --i)
563 {
564 (*previous_solutions)[i] = (*previous_solutions)[i - 1];
565 }
566 (*previous_solutions)[0] = *this->present_solution;
567}
568
569
570template <int dim, typename VectorType, typename DofsType>
571void
573{
574 if (simulation_parameters.simulation_control.method !=
576 {
577 TimerOutput::Scope t(this->computing_timer,
578 "Calculate CFL and percolate time vectors");
581 const double CFL = calculate_CFL(*this->dof_handler,
582 *this->present_solution,
583 simulation_control->get_time_step(),
584 *this->cell_quadrature,
585 *this->get_mapping());
586 this->simulation_control->set_CFL(CFL);
588 if (this->simulation_parameters.restart_parameters.checkpoint &&
589 simulation_control->get_iteration_number() != 0 &&
590 (simulation_control->get_iteration_number() %
591 this->simulation_parameters.restart_parameters.frequency ==
592 0 ||
593 simulation_control->is_at_end()))
595 this->write_checkpoint();
596 }
597
598 if (this->simulation_parameters.timer.type ==
600 {
601 announce_string(this->pcout, "Fluid Dynamics");
602 this->pcout << std::defaultfloat;
603 this->computing_timer.print_summary();
604 this->pcout << std::scientific;
605 this->computing_timer.reset();
607 multiphysics->output_per_iteration_timer();
608 }
609}
610
611template <int dim, typename VectorType, typename DofsType>
612void
614 unsigned int,
616{}
617
618
619template <int dim, typename VectorType, typename DofsType>
620void
626
627template <int dim, typename VectorType, typename DofsType>
628void
631
632template <int dim, typename VectorType, typename DofsType>
633void
636{
637 if (this->simulation_parameters.initial_condition->type ==
639 {
640 // We get the solution via the average solution
642 *this->average_velocities->get_average_velocities();
645 else
646 {
647 // We get the solution via an initial condition
648 this->simulation_parameters.initial_condition->uvwp.set_time(
649 this->simulation_control->get_current_time());
650 set_initial_condition_fd(
651 this->simulation_parameters.initial_condition->type);
652 }
653}
654
655// Do an iteration with the NavierStokes Solver
656// Handles the fact that we may or may not be at a first
657// iteration with the solver and sets the initial condition
658template <int dim, typename VectorType, typename DofsType>
659void
661{
662 // Solve and percolate the auxiliary physics that should be treated
663 // BEFORE the fluid dynamics
664 multiphysics->solve(false, simulation_parameters.simulation_control.method);
665 multiphysics->percolate_time_vectors(false);
666
667 if (simulation_parameters.multiphysics.fluid_dynamics)
668 {
669 const auto method = this->simulation_control->get_assembly_method();
670 const auto time_step = this->simulation_control->get_time_step();
671 const unsigned int n_stages =
673
674 // For a multi-stage method, the Butcher tableau provides the time of
675 // each stage, and the sum of b_i * k_i is reset at each time step.
676 const SDIRKTable table =
677 (n_stages > 1) ? sdirk_table(method) : SDIRKTable();
678 if (n_stages > 1)
679 this->sdirk_vectors.local_sum_bi_ki = 0;
680
681 for (unsigned int stage = 0; stage < n_stages; ++stage)
682 {
683 if (simulation_parameters.physics_solving_strategy
685 .verbosity != Parameters::Verbosity::quiet ||
687 .verbosity != Parameters::Verbosity::quiet)
688 announce_string(this->pcout, "Fluid Dynamics");
689
690 if (n_stages > 1)
691 {
692 // The forcing and the boundary conditions of a stage are
693 // evaluated at the time of the stage t_n + c_i * dt.
694 this->simulation_control->set_stage_time(table.c[stage]);
696
697 // For SDIRK, some variables need to be updated before solving the
698 // non-linear system.
699 multi_stage_preresolution(stage, method);
700 }
701
703
704 if (n_stages > 1)
705 {
706 // For SDIRK, some variables need to be updated after solving the
707 // non-linear system.
708 multi_stage_postresolution(stage, method, time_step);
709 }
710 }
711
712 if (n_stages > 1)
713 {
714 // For SDIRK, the solution is not directly computed with the
715 // non-linear solver. Once we looped over all the stages, we restore
716 // the time to t_{n+1}, update the present_solution vector and
717 // re-impose the forcing and boundary conditions at t_{n+1}.
718 this->simulation_control->end_stages();
721 }
722
723 // If the physics need to be solved after the physics, the matrix free
724 // solver requires to update the value here. This is due to the
725 // different type of vectors.
726 if (this->multiphysics->get_active_physics().size() > 1)
727 {
729 }
730 }
731 else
732 {
733 // Fluid dynamics is not to be solved, but rather specified. Update
734 // condition and move on.
736 }
737
738 // Solve and percolate the auxiliary physics that should be treated
739 // AFTER the fluid dynamics
740 multiphysics->solve(true, simulation_parameters.simulation_control.method);
741 // Dear future Bruno, percolating auxiliary physics before fluid
742 // dynamics is necessary because of the checkpointing mechanism. You
743 // spent an evening debugging this, trust me.
744 multiphysics->percolate_time_vectors(true);
745}
746
747template <int dim, typename VectorType, typename DofsType>
748void
758
760template <int dim, typename VectorType, typename DofsType>
761void
764{
765 if (!this->simulation_parameters.constrain_solid_domain.enable)
766 return;
767
768 // Initialize StasisConstraintWithTemperature structs for each constraint
769 for (unsigned int c_id = 0;
770 c_id <
771 this->simulation_parameters.constrain_solid_domain.number_of_constraints;
772 c_id++)
773 {
774 StasisConstraintWithTemperature stasis_constraint_struct(
775 this->simulation_parameters.constrain_solid_domain.fluid_ids[c_id],
776 this->simulation_parameters.constrain_solid_domain
777 .temperature_min_values[c_id],
778 this->simulation_parameters.constrain_solid_domain
779 .temperature_max_values[c_id],
780 this->simulation_parameters.constrain_solid_domain
781 .filtered_phase_indicator_tolerance[c_id]);
782 this->stasis_constraint_structs.emplace_back(stasis_constraint_struct);
783 }
784
785 // For temperature-dependent constraints
786 const DoFHandler<dim> &dof_handler_ht =
787 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
790 std::make_shared<FEValues<dim>>(*this->get_mapping(),
791 dof_handler_ht.get_fe(),
792 *this->cell_quadrature,
793 update_values);
794
795 // For CLS simulations
796 if (this->simulation_parameters.multiphysics.CLS)
797 {
798 const DoFHandler<dim> &dof_handler_cls =
799 this->multiphysics->get_dof_handler(PhysicsID::heat_transfer);
801 this->fe_values_cls =
802 std::make_shared<FEValues<dim>>(*this->get_mapping(),
803 dof_handler_cls.get_fe(),
804 *this->cell_quadrature,
805 update_values);
807}
808
809template <int dim, typename VectorType, typename DofsType>
810void
812{
813 if (this->simulation_control->is_refinement_step(
814 this->simulation_parameters.mesh_adaptation))
815 {
816 if (this->simulation_parameters.mesh_adaptation.type ==
819
820 else if (this->simulation_parameters.mesh_adaptation.type ==
823 }
825
826template <int dim, typename VectorType, typename DofsType>
827void
829{
830 if (restart)
831 {
832 return;
833 }
834
835 for (unsigned int i_box = 0;
836 i_box < this->simulation_parameters.mesh_box_refinement
837 ->number_of_refinement_boxes;
838 ++i_box)
839 {
840 // Read the mesh that defines the box. Any mesh type supported by Lethe
841 // can be used, including the built-in 'lethe' grids.
842 Triangulation<dim> box_to_refine;
844 (*this->simulation_parameters.mesh_box_refinement
845 ->refinement_boxes_meshes)[i_box],
846 box_to_refine);
848 // Refine the number of time needed
849 for (unsigned int i = 0;
850 i < this->simulation_parameters.mesh_box_refinement
851 ->box_additional_refinements[i_box];
852 ++i)
854 if (dynamic_cast<parallel::distributed::Triangulation<dim> *>(
855 this->triangulation.get()) == nullptr)
856 return;
857
858 auto &tria =
859 *dynamic_cast<parallel::distributed::Triangulation<dim> *>(
860 this->triangulation.get());
861
862 // Time monitoring
863 TimerOutput::Scope t(this->computing_timer, "Box refine");
864 this->pcout << "Initial refinement in box " << i_box << " - Step "
865 << i + 1 << " of "
866 << this->simulation_parameters.mesh_box_refinement
867 ->box_additional_refinements[i_box]
868 << std::endl;
869
870
871 auto &present_solution = *this->present_solution;
872
873 // Find all the cells of the principal mesh that are partially
874 // contained inside the box mesh and set them up for refinement.
876 box_to_refine);
877
878 tria.prepare_coarsening_and_refinement();
879
880 // Solution transfer objects for all the solutions
881 SolutionTransfer<dim, VectorType> solution_transfer(
882 *this->dof_handler, true);
883 std::vector<SolutionTransfer<dim, VectorType>>
884 previous_solutions_transfer;
885 // Important to reserve to prevent pointer dangling
886 previous_solutions_transfer.reserve(previous_solutions->size());
887 for (unsigned int p = 0; p < previous_solutions->size(); ++p)
888 {
889 previous_solutions_transfer.emplace_back(
890 SolutionTransfer<dim, VectorType>(*this->dof_handler, true));
891 if constexpr (std::is_same_v<
893 LinearAlgebra::distributed::Vector<double>>)
894 (*previous_solutions)[p].update_ghost_values();
895 previous_solutions_transfer[p]
896 .prepare_for_coarsening_and_refinement(
897 (*previous_solutions)[p]);
898 }
899
900 if constexpr (std::is_same_v<
902 LinearAlgebra::distributed::Vector<double>>)
903 present_solution.update_ghost_values();
904 solution_transfer.prepare_for_coarsening_and_refinement(
905 present_solution);
906
907 multiphysics->prepare_for_mesh_adaptation();
908 if (this->average_velocities_are_enabled())
909 average_velocities->prepare_for_mesh_adaptation();
910
911 tria.execute_coarsening_and_refinement();
912 this->setup_dofs();
913
914 // Set up the vectors for the transfer
915 VectorType tmp = init_temporary_vector();
916
917 // Interpolate the solution at time and previous time
918 solution_transfer.interpolate(tmp);
919
920 // Distribute constraints
921 auto &nonzero_constraints = this->nonzero_constraints;
922 nonzero_constraints.distribute(tmp);
923
924 // Fix on the new mesh
925 present_solution = tmp;
926
927 for (unsigned int i = 0; i < previous_solutions->size(); ++i)
928 {
929 VectorType tmp_previous_solution = init_temporary_vector();
930 previous_solutions_transfer[i].interpolate(tmp_previous_solution);
931 nonzero_constraints.distribute(tmp_previous_solution);
932 (*previous_solutions)[i] = tmp_previous_solution;
933 }
934
935 multiphysics->post_mesh_adaptation();
936 if (this->average_velocities_are_enabled())
937 average_velocities->post_mesh_adaptation();
938 }
939 }
940}
941
942
943template <int dim, typename VectorType, typename DofsType>
944void
946{
947 if (dynamic_cast<parallel::distributed::Triangulation<dim> *>(
948 this->triangulation.get()) == nullptr)
949 return;
950
951 auto &tria = *dynamic_cast<parallel::distributed::Triangulation<dim> *>(
952 this->triangulation.get());
953
954 // If mortar is enabled, connect mortar cell weight signals for load balancing
956
957 // Time monitoring
958 TimerOutput::Scope t(this->computing_timer, "Refine");
959
960 Vector<float> estimated_error_per_cell(tria.n_active_cells());
961 const FEValuesExtractors::Vector velocity(0);
962 const FEValuesExtractors::Scalar pressure(dim);
963 auto &present_solution = *this->present_solution;
964 VectorType locally_relevant_solution;
965 locally_relevant_solution.reinit(this->locally_owned_dofs,
967 this->mpi_communicator);
968 locally_relevant_solution = *this->present_solution;
969 locally_relevant_solution.update_ghost_values();
970
971 // Global flags
972 // Their dimension is consistent with the dimension returned by
973 // save_refine_flags(), in order to be able to use load_refine_flags()
974 std::vector<bool> global_refine_flags(dim * tria.n_active_cells(), false);
975 std::vector<bool> global_coarsen_flags(dim * tria.n_active_cells(), false);
976
977 bool first_variable(true);
978 const double coarsening_factor = mesh_controller.calculate_coarsening_factor(
979 this->triangulation->n_global_active_cells());
980
981 unsigned int maximal_number_of_elements =
982 this->simulation_parameters.mesh_adaptation.maximum_number_elements;
983 // Override the maximal number of elements if the controller is used. The
984 // controller will find a coarsening_factor that respects the user-defined
985 // maximal_number_of_elements.
986 if (this->simulation_parameters.mesh_adaptation.mesh_controller_is_enabled)
987 maximal_number_of_elements = INT_MAX;
988
989 for (const std::pair<const Variable, Parameters::MultipleAdaptationParameters>
990 &ivar : this->simulation_parameters.mesh_adaptation.variables)
991 {
993 error_indicator_type = ivar.second.error_estimator;
994 double ivar_coarsening_factor = ivar.second.coarsening_fraction;
995 if (this->simulation_parameters.mesh_adaptation
996 .mesh_controller_is_enabled)
997 ivar_coarsening_factor = coarsening_factor;
998
999 if (ivar.first == Variable::pressure)
1000 {
1001 AssertThrow(
1002 error_indicator_type ==
1004 ExcMessage(
1005 "For the pressure variable, only the Kelly error estimator is "
1006 "available."));
1007
1008 KellyErrorEstimator<dim>::estimate(
1009 *this->get_mapping(),
1010 *this->dof_handler,
1011 *this->face_quadrature,
1012 typename std::map<types::boundary_id,
1013 const Function<dim, double> *>(),
1014 locally_relevant_solution,
1015 estimated_error_per_cell,
1016 this->fe->component_mask(pressure));
1017 }
1018 else if (ivar.first == Variable::velocity)
1019 {
1020 AssertThrow(
1021 error_indicator_type ==
1023 ExcMessage(
1024 "For the velocity variable, only the Kelly error estimator is "
1025 "available."));
1026
1027 KellyErrorEstimator<dim>::estimate(
1028 *this->get_mapping(),
1029 *this->dof_handler,
1030 *this->face_quadrature,
1031 typename std::map<types::boundary_id,
1032 const Function<dim, double> *>(),
1033 locally_relevant_solution,
1034 estimated_error_per_cell,
1035 this->fe->component_mask(velocity));
1036 }
1037 else
1038 {
1039 // refine_mesh on an auxiliary physic parameter
1040 multiphysics->compute_error_estimate(ivar, estimated_error_per_cell);
1041 }
1042
1043 if (this->simulation_parameters.mesh_adaptation.fractionType ==
1045 parallel::distributed::GridRefinement::refine_and_coarsen_fixed_number(
1046 tria,
1047 estimated_error_per_cell,
1048 ivar.second.refinement_fraction,
1049 ivar_coarsening_factor,
1050 maximal_number_of_elements);
1051
1052 else if (this->simulation_parameters.mesh_adaptation.fractionType ==
1054 parallel::distributed::GridRefinement::
1055 refine_and_coarsen_fixed_fraction(tria,
1056 estimated_error_per_cell,
1057 ivar.second.refinement_fraction,
1058 ivar_coarsening_factor);
1059
1060 // Remove the flags if the cell is at the boundary and is set as do not
1061 // touch in the parameter file
1062 if (this->simulation_parameters.mesh_adaptation
1063 .is_boundary_refinement_fixed)
1064 for (const auto &cell : tria.active_cell_iterators())
1065 if (cell->is_locally_owned())
1066 {
1067 if (cell->at_boundary())
1068 for (const auto &face : cell->face_iterators())
1069 {
1070 if (std::find(this->simulation_parameters.mesh_adaptation
1071 .boundaries_to_fix.begin(),
1072 this->simulation_parameters.mesh_adaptation
1073 .boundaries_to_fix.end(),
1074 face->boundary_id()) !=
1075 this->simulation_parameters.mesh_adaptation
1076 .boundaries_to_fix.end())
1077 {
1078 cell->clear_refine_flag();
1079 cell->clear_coarsen_flag();
1080 }
1081 }
1082 }
1083
1084 std::vector<bool> current_refine_flags;
1085 std::vector<bool> current_coarsen_flags;
1086
1087 tria.save_refine_flags(current_refine_flags);
1088 tria.save_coarsen_flags(current_coarsen_flags);
1089
1090 // Fill global flags
1091 if (first_variable)
1092 {
1093 // special case of the first refinement variable
1094 global_refine_flags = current_refine_flags;
1095 global_coarsen_flags = current_coarsen_flags;
1096
1097 first_variable = false;
1098 }
1099 else
1100 {
1101 // for subsequent refinement variables
1102 for (std::vector<bool>::size_type i = 0;
1103 i != global_refine_flags.size();
1104 ++i)
1105 {
1106 // refine if at least refinement flag on one variable
1107 global_refine_flags[i] =
1108 global_refine_flags[i] || current_refine_flags[i];
1109 }
1110
1111 // for subsequent coarsen variables
1112 for (std::vector<bool>::size_type i = 0;
1113 i != global_coarsen_flags.size();
1114 ++i)
1115 {
1116 // coarsen if refinement flag on all variables
1117 global_coarsen_flags[i] =
1118 global_coarsen_flags[i] && current_coarsen_flags[i];
1119 }
1120 }
1121
1122 // Clear flags in the triangulation
1123 for (const auto &cell : tria.active_cell_iterators())
1124 {
1125 cell->clear_coarsen_flag();
1126 cell->clear_refine_flag();
1127 }
1128 }
1129
1130 // Load global flags
1131 tria.load_refine_flags(global_refine_flags);
1132 tria.load_coarsen_flags(global_coarsen_flags);
1133
1134 if (tria.n_levels() >
1135 this->simulation_parameters.mesh_adaptation.maximum_refinement_level)
1136 for (typename Triangulation<dim>::active_cell_iterator cell =
1137 tria.begin_active(this->simulation_parameters.mesh_adaptation
1138 .maximum_refinement_level);
1139 cell != tria.end();
1140 ++cell)
1141 cell->clear_refine_flag();
1142 for (typename Triangulation<dim>::active_cell_iterator cell =
1143 tria.begin_active(this->simulation_parameters.mesh_adaptation
1144 .minimum_refinement_level);
1145 cell !=
1146 tria.end_active(
1147 this->simulation_parameters.mesh_adaptation.minimum_refinement_level);
1148 ++cell)
1149 cell->clear_coarsen_flag();
1150
1151 tria.prepare_coarsening_and_refinement();
1152
1153 // Solution transfer objects for all the solutions
1154 SolutionTransfer<dim, VectorType> solution_transfer(*this->dof_handler, true);
1155 std::vector<SolutionTransfer<dim, VectorType>> previous_solutions_transfer;
1156 // Important to reserve to prevent pointer dangling
1157 previous_solutions_transfer.reserve(previous_solutions->size());
1158 for (unsigned int i = 0; i < previous_solutions->size(); ++i)
1159 {
1160 previous_solutions_transfer.emplace_back(
1161 SolutionTransfer<dim, VectorType>(*this->dof_handler, true));
1162 if constexpr (std::is_same_v<VectorType,
1163 LinearAlgebra::distributed::Vector<double>>)
1164 (*previous_solutions)[i].update_ghost_values();
1165 previous_solutions_transfer[i].prepare_for_coarsening_and_refinement(
1166 (*previous_solutions)[i]);
1167 }
1168
1169 if constexpr (std::is_same_v<VectorType,
1170 LinearAlgebra::distributed::Vector<double>>)
1171 present_solution.update_ghost_values();
1172 solution_transfer.prepare_for_coarsening_and_refinement(present_solution);
1173
1174 multiphysics->prepare_for_mesh_adaptation();
1176 average_velocities->prepare_for_mesh_adaptation();
1177
1178 tria.execute_coarsening_and_refinement();
1179
1180 // If mortar is enabled, update mapping cache with refined triangulation
1181 if (this->simulation_parameters.mortar_parameters.enable)
1182 this->mapping_cache->initialize(*this->mapping, tria);
1183
1184 setup_dofs();
1185
1186 // Transfer solution
1187 transfer_solution(solution_transfer, previous_solutions_transfer);
1188}
1189
1190template <int dim, typename VectorType, typename DofsType>
1191void
1193{
1194 if (this->triangulation->n_global_levels() >
1195 this->simulation_parameters.mesh_adaptation.maximum_refinement_level)
1196 return;
1197 AssertThrow(this->triangulation->all_reference_cells_are_hyper_cube(),
1198 ExcMessage("Uniform refinement is not supported for "
1199 "simplex meshes."));
1200 TimerOutput::Scope t(this->computing_timer, "Refine");
1201
1202 // If mortar is enabled, connect mortar cell weight signals for load balancing
1204
1205 // Solution transfer objects for all the solutions
1206 SolutionTransfer<dim, VectorType> solution_transfer(*this->dof_handler, true);
1207 SolutionTransfer<dim, VectorType> solution_transfer_m2(*this->dof_handler,
1208 true);
1209 SolutionTransfer<dim, VectorType> solution_transfer_m3(*this->dof_handler,
1210 true);
1211
1212 if constexpr (std::is_same_v<VectorType,
1213 LinearAlgebra::distributed::Vector<double>>)
1214 present_solution->update_ghost_values();
1215
1216 solution_transfer.prepare_for_coarsening_and_refinement(
1217 *this->present_solution);
1218
1219 std::vector<SolutionTransfer<dim, VectorType>> previous_solutions_transfer;
1220 // Important to reserve to prevent pointer dangling
1221 previous_solutions_transfer.reserve(previous_solutions->size());
1222
1223 for (unsigned int i = 0; i < previous_solutions->size(); ++i)
1224 {
1225 previous_solutions_transfer.emplace_back(
1226 SolutionTransfer<dim, VectorType>(*this->dof_handler, true));
1227
1228 if constexpr (std::is_same_v<VectorType,
1229 LinearAlgebra::distributed::Vector<double>>)
1230 (*previous_solutions)[i].update_ghost_values();
1231
1232 previous_solutions_transfer[i].prepare_for_coarsening_and_refinement(
1233 (*previous_solutions)[i]);
1234 }
1235
1236 multiphysics->prepare_for_mesh_adaptation();
1238 average_velocities->prepare_for_mesh_adaptation();
1239
1240 // Refine
1241 this->triangulation->refine_global(1);
1242
1243 // If mortar is enabled, update mapping cache with refined triangulation
1244 if (this->simulation_parameters.mortar_parameters.enable)
1245 this->mapping_cache->initialize(*this->mapping, *this->triangulation);
1246
1247 setup_dofs();
1248
1249 // Transfer solution
1250 transfer_solution(solution_transfer, previous_solutions_transfer);
1251}
1252
1253template <int dim, typename VectorType, typename DofsType>
1254void
1256 SolutionTransfer<dim, VectorType> &solution_transfer,
1257 std::vector<SolutionTransfer<dim, VectorType>> &previous_solutions_transfer)
1258{
1259 // Set up the vectors for the transfer
1261
1262 // Interpolate the solution at time and previous time
1263 solution_transfer.interpolate(tmp);
1264
1265 // Distribute constraints
1267 nonzero_constraints.distribute(tmp);
1268
1269 // Fix on the new mesh
1270 *present_solution = tmp;
1271
1272 // Set up the vectors for the transfer
1273 for (unsigned int i = 0; i < previous_solutions->size(); ++i)
1274 {
1275 VectorType tmp_previous_solution = init_temporary_vector();
1276 previous_solutions_transfer[i].interpolate(tmp_previous_solution);
1277 nonzero_constraints.distribute(tmp_previous_solution);
1278 (*previous_solutions)[i] = tmp_previous_solution;
1279 }
1280
1281 multiphysics->post_mesh_adaptation();
1283 average_velocities->post_mesh_adaptation();
1284
1285 // Only needed if other physics apart from fluid dynamics are enabled.
1286 if (this->multiphysics->get_active_physics().size() > 1)
1288}
1289
1290template <int dim, typename VectorType, typename DofsType>
1291void
1293{
1294 this->multiphysics->prepare_for_postprocessing();
1295
1296 auto &present_solution = *this->present_solution;
1297
1298 // Enstrophy
1299 if (this->simulation_parameters.post_processing.calculate_enstrophy)
1300 {
1301 TimerOutput::Scope t(this->computing_timer, "Calculate enstrophy");
1302
1303 double enstrophy = calculate_enstrophy(*this->dof_handler,
1305 *this->cell_quadrature,
1306 *this->get_mapping());
1307
1308 this->enstrophy_table.add_value("time",
1309 simulation_control->get_current_time());
1310 this->enstrophy_table.add_value("enstrophy", enstrophy);
1311
1312 // Display Enstrophy to screen if verbosity is enabled
1313 if (this->simulation_parameters.post_processing.verbosity ==
1315 {
1316 this->pcout << "Enstrophy: " << enstrophy << std::endl;
1317 }
1318
1319 // Output Enstrophy to a text file from processor 0
1320 if (simulation_control->get_iteration_number() %
1321 this->simulation_parameters.post_processing.output_frequency ==
1322 0 &&
1323 this->this_mpi_process == 0)
1324 {
1325 std::string filename =
1326 simulation_parameters.simulation_control.output_folder +
1327 simulation_parameters.post_processing.enstrophy_output_name +
1328 ".dat";
1329 std::ofstream output(filename.c_str());
1330 enstrophy_table.set_precision("time", 12);
1331 enstrophy_table.set_precision("enstrophy", 12);
1332 this->enstrophy_table.write_text(output);
1333 }
1334 }
1335
1336 // Pressure power
1337 if (this->simulation_parameters.post_processing.calculate_pressure_power)
1338 {
1339 TimerOutput::Scope t(this->computing_timer, "Calculate pressure power");
1340
1341 const double pressure_power =
1344 *this->cell_quadrature,
1345 *this->get_mapping());
1346
1347 this->pressure_power_table.add_value(
1348 "time", simulation_control->get_current_time());
1349 this->pressure_power_table.add_value("pressure_power", pressure_power);
1350
1351 // Display pressure power to screen if verbosity is enabled
1352 if (this->simulation_parameters.post_processing.verbosity ==
1354 {
1355 this->pcout << "Pressure power: " << pressure_power << std::endl;
1356 }
1357
1358 // Output pressure power to a text file from processor 0
1359 if (simulation_control->get_iteration_number() %
1360 this->simulation_parameters.post_processing.output_frequency ==
1361 0 &&
1362 this->this_mpi_process == 0)
1363 {
1364 std::string filename =
1365 simulation_parameters.simulation_control.output_folder +
1366 simulation_parameters.post_processing.pressure_power_output_name +
1367 ".dat";
1368 std::ofstream output(filename.c_str());
1369 pressure_power_table.set_precision("time", 12);
1370 pressure_power_table.set_precision("pressure_power", 12);
1371 this->pressure_power_table.write_text(output);
1372 }
1373 }
1374
1375
1376 // Viscous dissipation
1377 if (this->simulation_parameters.post_processing.calculate_viscous_dissipation)
1378 {
1379 TimerOutput::Scope t(this->computing_timer,
1380 "Calculate viscous dissipation");
1381
1382 const double viscous_dissipation = calculate_viscous_dissipation(
1383 *this->dof_handler,
1385 *this->cell_quadrature,
1386 *this->get_mapping(),
1387 simulation_parameters.physical_properties_manager);
1388
1389 this->viscous_dissipation_table.add_value(
1390 "time", simulation_control->get_current_time());
1391 this->viscous_dissipation_table.add_value("viscous_dissipation",
1392 viscous_dissipation);
1393
1394 // Display pressure power to screen if verbosity is enabled
1395 if (this->simulation_parameters.post_processing.verbosity ==
1397 {
1398 this->pcout << "Viscous dissipation: " << viscous_dissipation
1399 << std::endl;
1400 }
1401
1402 // Output pressure power to a text file from processor 0
1403 if (simulation_control->get_iteration_number() %
1404 this->simulation_parameters.post_processing.output_frequency ==
1405 0 &&
1406 this->this_mpi_process == 0)
1407 {
1408 std::string filename =
1409 simulation_parameters.simulation_control.output_folder +
1410 simulation_parameters.post_processing
1411 .viscous_dissipation_output_name +
1412 ".dat";
1413 std::ofstream output(filename.c_str());
1414 viscous_dissipation_table.set_precision("time", 12);
1415 viscous_dissipation_table.set_precision("viscous_dissipation", 12);
1416 this->viscous_dissipation_table.write_text(output);
1417 }
1418 }
1419
1420 // The average velocities and reynolds stresses are calculated when the
1421 // time reaches the initial time. (time >= initial time) with 1e-6 as
1422 // tolerance.
1423 if (this->simulation_parameters.post_processing.calculate_average_velocities)
1424 {
1425 TimerOutput::Scope t(this->computing_timer,
1426 "Calculate average velocities");
1427 this->average_velocities->calculate_average_velocities(
1429 simulation_parameters.post_processing,
1430 simulation_control->get_current_time(),
1431 simulation_control->get_time_step());
1432 }
1433
1434 if (this->simulation_parameters.post_processing.calculate_kinetic_energy)
1435 {
1436 TimerOutput::Scope t(this->computing_timer, "Calculate kinetic energy");
1437
1438 double kE = calculate_kinetic_energy(*this->dof_handler,
1440 *this->cell_quadrature,
1441 *this->get_mapping());
1442 this->kinetic_energy_table.add_value(
1443 "time", simulation_control->get_current_time());
1444 this->kinetic_energy_table.add_value("kinetic-energy", kE);
1445 if (this->simulation_parameters.post_processing.verbosity ==
1447 {
1448 this->pcout << "Kinetic energy: " << kE << std::endl;
1449 }
1450
1451 // Output Kinetic Energy to a text file from processor 0
1452 if ((simulation_control->get_iteration_number() %
1453 this->simulation_parameters.post_processing.output_frequency ==
1454 0) &&
1455 this->this_mpi_process == 0)
1456 {
1457 std::string filename =
1458 simulation_parameters.simulation_control.output_folder +
1459 simulation_parameters.post_processing.kinetic_energy_output_name +
1460 ".dat";
1461 std::ofstream output(filename.c_str());
1462 kinetic_energy_table.set_precision("time", 12);
1463 kinetic_energy_table.set_precision("kinetic-energy", 12);
1464 this->kinetic_energy_table.write_text(output);
1465 }
1466 }
1467
1468 // Calculate apparent viscosity
1469 if (this->simulation_parameters.post_processing.calculate_apparent_viscosity)
1470 {
1471 TimerOutput::Scope t(this->computing_timer,
1472 "Calculate apparent viscosity");
1473
1474 double apparent_viscosity = calculate_apparent_viscosity(
1475 *this->dof_handler,
1476 *this->present_solution,
1477 *this->cell_quadrature,
1478 *this->get_mapping(),
1479 this->simulation_parameters.physical_properties_manager);
1480
1481 this->apparent_viscosity_table.add_value(
1482 "time", simulation_control->get_current_time());
1483 this->apparent_viscosity_table.add_value("apparent_viscosity",
1484 apparent_viscosity);
1485 if (this->simulation_parameters.post_processing.verbosity ==
1487 {
1488 this->pcout << "Apparent viscosity: " << apparent_viscosity
1489 << " m^2/s" << std::endl;
1490 }
1491
1492 // Output apparent viscosity to a text file from processor 0
1493 if (simulation_control->get_iteration_number() %
1494 this->simulation_parameters.post_processing.output_frequency ==
1495 0 &&
1496 this->this_mpi_process == 0)
1497 {
1498 std::string filename =
1499 simulation_parameters.simulation_control.output_folder +
1500 simulation_parameters.post_processing
1501 .apparent_viscosity_output_name +
1502 ".dat";
1503 std::ofstream output(filename.c_str());
1504 apparent_viscosity_table.set_precision("time", 12);
1505 apparent_viscosity_table.set_precision("apparent_viscosity", 12);
1506 this->apparent_viscosity_table.write_text(output);
1507 }
1508 }
1509
1510 // Calculate pressure drop between two boundaries
1511 if (this->simulation_parameters.post_processing.calculate_pressure_drop)
1512 {
1513 TimerOutput::Scope t(this->computing_timer, "Calculate pressure drop");
1514
1515 double pressure_drop, total_pressure_drop;
1516 std::tie(pressure_drop, total_pressure_drop) = calculate_pressure_drop(
1517 *this->dof_handler,
1518 *this->get_mapping(),
1519 this->evaluation_point,
1520 *this->cell_quadrature,
1521 *this->face_quadrature,
1522 this->simulation_parameters.post_processing.inlet_boundary_id,
1523 this->simulation_parameters.post_processing.outlet_boundary_id);
1524 this->pressure_drop_table.add_value(
1525 "time", simulation_control->get_current_time());
1526 this->pressure_drop_table.add_value("pressure-drop", pressure_drop);
1527 this->pressure_drop_table.add_value("total-pressure-drop",
1528 total_pressure_drop);
1529 if (this->simulation_parameters.post_processing.verbosity ==
1531 {
1532 this->pcout << "Pressure drop: "
1533 << this->simulation_parameters.physical_properties_manager
1534 .get_density_scale() *
1535 pressure_drop
1536 << " Pa" << std::endl;
1537 this->pcout << "Total pressure drop: "
1538 << this->simulation_parameters.physical_properties_manager
1539 .get_density_scale() *
1540 total_pressure_drop
1541 << " Pa" << std::endl;
1542 }
1543
1544 // Output pressure drop to a text file from processor 0
1545 if ((simulation_control->get_iteration_number() %
1546 this->simulation_parameters.post_processing.output_frequency ==
1547 0) &&
1548 this->this_mpi_process == 0)
1549 {
1550 std::string filename =
1551 simulation_parameters.simulation_control.output_folder +
1552 simulation_parameters.post_processing.pressure_drop_output_name +
1553 ".dat";
1554 std::ofstream output(filename.c_str());
1555 pressure_drop_table.set_precision("time", 12);
1556 pressure_drop_table.set_precision("pressure-drop", 12);
1557 pressure_drop_table.set_precision("total-pressure-drop", 12);
1558 this->pressure_drop_table.write_text(output);
1559 }
1560 }
1561
1562 // Calculate flow rate at every boundary
1563 if (this->simulation_parameters.post_processing.calculate_flow_rate)
1564 {
1565 TimerOutput::Scope t(this->computing_timer, "Calculate flow rate");
1566
1567 this->flow_rate_table.add_value("time",
1568 simulation_control->get_current_time());
1569 this->flow_rate_table.set_scientific("time", true);
1570
1571 if (this->simulation_parameters.post_processing.verbosity ==
1573 {
1574 announce_string(this->pcout, "Flow rates");
1575 }
1576
1577 for (auto const &[boundary_id, type] :
1578 simulation_parameters.boundary_conditions.type)
1579 {
1580 std::pair<double, double> boundary_flow_rate =
1582 *this->present_solution,
1583 boundary_id,
1584 *this->face_quadrature,
1585 *this->get_mapping());
1586
1587 this->flow_rate_table.add_value(
1588 "flow-rate-" + Utilities::int_to_string(boundary_id, 2),
1589 boundary_flow_rate.first);
1590 this->flow_rate_table.set_scientific(
1591 "flow-rate-" + Utilities::int_to_string(boundary_id, 2), true);
1592 if (this->simulation_parameters.post_processing.verbosity ==
1594 {
1595 this->pcout << "Flow rate at boundary " +
1596 std::to_string(boundary_id) + ": "
1597 << boundary_flow_rate.first << " m^3/s" << std::endl;
1598 }
1599 }
1600
1601 // Output flow rate to a text file from processor 0
1602 if ((simulation_control->get_iteration_number() %
1603 this->simulation_parameters.post_processing.output_frequency ==
1604 0) &&
1605 this->this_mpi_process == 0)
1606 {
1607 std::string filename =
1608 simulation_parameters.simulation_control.output_folder +
1609 simulation_parameters.post_processing.flow_rate_output_name +
1610 ".dat";
1611 std::ofstream output(filename.c_str());
1612 flow_rate_table.set_precision("time", 12);
1613 for (auto const &[boundary_id, type] :
1614 simulation_parameters.boundary_conditions.type)
1615 flow_rate_table.set_precision(
1616 "flow-rate-" + Utilities::int_to_string(boundary_id, 2), 12);
1617 this->flow_rate_table.write_text(output);
1618 }
1619 }
1620
1621 if (!firstIter)
1622 {
1623 // Calculate forces on the boundary conditions
1624 if (this->simulation_parameters.forces_parameters.calculate_force)
1625 {
1626 if (simulation_control->get_iteration_number() %
1627 this->simulation_parameters.forces_parameters
1628 .calculation_frequency ==
1629 0)
1631 if (simulation_control->get_iteration_number() %
1632 this->simulation_parameters.forces_parameters
1633 .output_frequency ==
1634 0)
1635 this->write_output_forces();
1636 }
1637
1638 // Calculate torques on the boundary conditions
1639 if (this->simulation_parameters.forces_parameters.calculate_torque)
1640 {
1641 if (simulation_control->get_iteration_number() %
1642 this->simulation_parameters.forces_parameters
1643 .calculation_frequency ==
1644 0)
1646 if (simulation_control->get_iteration_number() %
1647 this->simulation_parameters.forces_parameters
1648 .output_frequency ==
1649 0)
1650 this->write_output_torques();
1651 }
1652
1653 // Calculate error with respect to analytical solution
1654 if (this->simulation_parameters.analytical_solution->calculate_error())
1655 {
1656 // For the timer here, we manually enter and leave the subsection to
1657 // ensure that the timer is not monitoring time when we will output
1658 // the time later on.
1659 computing_timer.enter_subsection(
1660 "Calculate error w.r.t. analytical solution");
1661
1662 // Update the time of the exact solution to the actual time
1663 this->exact_solution->set_time(
1664 simulation_control->get_current_time());
1665
1666 present_solution.update_ghost_values();
1667
1668 const std::pair<double, double> errors =
1672 *this->cell_quadrature,
1673 *this->get_mapping());
1674 const double error_velocity = errors.first;
1675 const double error_pressure = errors.second;
1676
1677 // Manually close the subsection so we can use the timer output
1678 computing_timer.leave_subsection(
1679 "Calculate error w.r.t. analytical solution");
1680
1681 if (simulation_parameters.simulation_control.method ==
1683 {
1684 this->error_table.add_value(
1685 "cells", this->triangulation->n_global_active_cells());
1686 this->error_table.add_value("error_velocity", error_velocity);
1687 this->error_table.add_value("error_pressure", error_pressure);
1688 auto summary = computing_timer.get_summary_data(
1689 computing_timer.total_wall_time);
1690 double total_time = 0;
1691 for (auto it = summary.begin(); it != summary.end(); ++it)
1692 {
1693 total_time += summary[it->first];
1694 }
1695
1696 this->error_table.add_value("total_time", total_time);
1697 }
1698 else
1699 {
1700 this->error_table.add_value(
1701 "time", simulation_control->get_current_time());
1702 this->error_table.add_value("error_velocity", error_velocity);
1703
1704 if (this->simulation_parameters.timer.write_time_in_error_table)
1705 {
1706 auto summary = computing_timer.get_summary_data(
1707 computing_timer.total_wall_time);
1708 double total_time = 0;
1709 for (auto it = summary.begin(); it != summary.end(); ++it)
1710 {
1711 total_time += summary[it->first];
1712 }
1713 this->error_table.add_value("total_time", total_time);
1714 }
1715
1716 // Calculate error on pressure for CLS or Cahn-Hilliard
1717 // simulations
1718 if (this->simulation_parameters.multiphysics.CLS ||
1719 this->simulation_parameters.multiphysics.cahn_hilliard)
1720 this->error_table.add_value("error_pressure", error_pressure);
1721 }
1722 if (this->simulation_parameters.analytical_solution->verbosity ==
1724 {
1725 this->pcout << "L2 error velocity: " << error_velocity
1726 << std::endl;
1727 if (this->simulation_parameters.multiphysics.cahn_hilliard)
1728 {
1729 this->pcout << "L2 error pressure: " << error_pressure
1730 << std::endl;
1731 }
1732 }
1733 }
1734 }
1735
1736 // Postprocess probes
1737 if (this->simulation_parameters.post_processing.probing_points
1738 .probing_points_per_variable.contains(Variable::velocity))
1739 this->multiphysics->postprocess_probes(*mapping,
1740 *dof_handler,
1743 this->pcout);
1744 if (this->simulation_parameters.post_processing.probing_points
1745 .probing_points_per_variable.contains(Variable::pressure))
1746 this->multiphysics->postprocess_probes(*mapping,
1747 *dof_handler,
1750 this->pcout);
1751}
1752
1753template <int dim, typename VectorType, typename DofsType>
1754void
1756{
1757 const FEValuesExtractors::Vector velocities(0);
1758 const FEValuesExtractors::Scalar pressure(dim);
1759 VectorTools::interpolate(*this->get_mapping(),
1760 *this->dof_handler,
1761 this->simulation_parameters.initial_condition->uvwp,
1762 this->newton_update,
1763 this->fe->component_mask(velocities));
1764 VectorTools::interpolate(*this->get_mapping(),
1765 *this->dof_handler,
1766 this->simulation_parameters.initial_condition->uvwp,
1767 this->newton_update,
1768 this->fe->component_mask(pressure));
1769 this->nonzero_constraints.distribute(this->newton_update);
1770 *this->present_solution = this->newton_update;
1771 if (this->simulation_parameters.simulation_control.bdf_startup_method ==
1773 {
1774 for (unsigned int i = 1; i < this->previous_solutions->size(); ++i)
1775 {
1776 double previous_solution_time =
1777 -this->simulation_parameters.simulation_control.dt * i;
1778 this->simulation_parameters.initial_condition->uvwp.set_time(
1779 previous_solution_time);
1780 const FEValuesExtractors::Vector velocities(0);
1781 const FEValuesExtractors::Scalar pressure(dim);
1782 VectorTools::interpolate(
1783 *this->get_mapping(),
1784 *this->dof_handler,
1785 this->simulation_parameters.initial_condition->uvwp,
1786 this->newton_update,
1787 this->fe->component_mask(velocities));
1788 VectorTools::interpolate(
1789 *this->get_mapping(),
1790 *this->dof_handler,
1791 this->simulation_parameters.initial_condition->uvwp,
1792 this->newton_update,
1793 this->fe->component_mask(pressure));
1794 (*this->previous_solutions)[i - 1] = this->newton_update;
1795 }
1796 }
1797}
1798
1799template <int dim, typename VectorType, typename DofsType>
1800void
1802{
1803 double time = this->simulation_control->get_current_time();
1804 FEValuesExtractors::Vector velocities(0);
1805 FEValuesExtractors::Scalar pressure(dim);
1806
1807 // Non-zero constraints
1809 nonzero_constraints.clear();
1810
1811 if constexpr (std::is_same_v<VectorType, GlobalBlockVectorType>)
1812 {
1813 std::vector<unsigned int> block_component(dim + 1, 0);
1814 block_component[dim] = 1;
1815 DoFRenumbering::component_wise(*this->dof_handler, block_component);
1816 std::vector<types::global_dof_index> dofs_per_block =
1817 DoFTools::count_dofs_per_fe_block(*this->dof_handler, block_component);
1818
1819 unsigned int dof_u = dofs_per_block[0];
1820 unsigned int dof_p = dofs_per_block[1];
1821
1822 IndexSet locally_relevant_dofs_acquisition;
1823 locally_relevant_dofs_acquisition =
1824 DoFTools::extract_locally_relevant_dofs(*this->dof_handler);
1825 this->locally_relevant_dofs.resize(2);
1826 this->locally_relevant_dofs[0] =
1827 locally_relevant_dofs_acquisition.get_view(0, dof_u);
1828 this->locally_relevant_dofs[1] =
1829 locally_relevant_dofs_acquisition.get_view(dof_u, dof_u + dof_p);
1830 nonzero_constraints.reinit(this->dof_handler->locally_owned_dofs(),
1831 locally_relevant_dofs_acquisition);
1832 }
1833 else
1834 {
1835 nonzero_constraints.reinit(this->dof_handler->locally_owned_dofs(),
1836 this->locally_relevant_dofs);
1837 }
1838
1839 DoFTools::make_hanging_node_constraints(*this->dof_handler,
1841 for (auto const &[id, type] :
1842 this->simulation_parameters.boundary_conditions.type)
1843 {
1845 {
1846 VectorTools::interpolate_boundary_values(
1847 *this->get_mapping(),
1848 *this->dof_handler,
1849 id,
1850 dealii::Functions::ZeroFunction<dim>(dim + 1),
1852 this->fe->component_mask(velocities));
1853 }
1855 {
1856 std::set<types::boundary_id> no_normal_flux_boundaries;
1857 no_normal_flux_boundaries.insert(id);
1858 VectorTools::compute_no_normal_flux_constraints(
1859 *this->dof_handler,
1860 0,
1861 no_normal_flux_boundaries,
1863 *this->get_mapping(),
1865 }
1867 {
1868 this->simulation_parameters.boundary_conditions
1869 .navier_stokes_functions.at(id)
1870 ->u.set_time(time);
1871 this->simulation_parameters.boundary_conditions
1872 .navier_stokes_functions.at(id)
1873 ->v.set_time(time);
1874 this->simulation_parameters.boundary_conditions
1875 .navier_stokes_functions.at(id)
1876 ->w.set_time(time);
1877 VectorTools::interpolate_boundary_values(
1878 *this->get_mapping(),
1879 *this->dof_handler,
1880 id,
1882 &this->simulation_parameters.boundary_conditions
1883 .navier_stokes_functions.at(id)
1884 ->u,
1885 &this->simulation_parameters.boundary_conditions
1886 .navier_stokes_functions.at(id)
1887 ->v,
1888 &this->simulation_parameters.boundary_conditions
1889 .navier_stokes_functions.at(id)
1890 ->w),
1892 this->fe->component_mask(velocities));
1893 }
1895 {
1896 const auto &periodic_boundary =
1897 this->simulation_parameters.boundary_conditions.periodic_boundaries
1898 .at(id);
1899 DoFTools::make_periodicity_constraints(*this->dof_handler,
1900 id,
1901 periodic_boundary.neighbor_id,
1902 periodic_boundary.direction,
1904 }
1905 }
1906
1907 this->make_solid_domain(*this->dof_handler, true, this->nonzero_constraints);
1908
1909 nonzero_constraints.close();
1910}
1911
1912template <int dim, typename VectorType, typename DofsType>
1913void
1915{
1916 FEValuesExtractors::Vector velocities(0);
1917 FEValuesExtractors::Scalar pressure(dim);
1918 this->zero_constraints.clear();
1919
1920 if constexpr (std::is_same_v<VectorType, GlobalBlockVectorType>)
1921 {
1922 std::vector<unsigned int> block_component(dim + 1, 0);
1923 block_component[dim] = 1;
1924 DoFRenumbering::component_wise(*this->dof_handler, block_component);
1925 std::vector<types::global_dof_index> dofs_per_block =
1926 DoFTools::count_dofs_per_fe_block(*this->dof_handler, block_component);
1927
1928 unsigned int dof_u = dofs_per_block[0];
1929 unsigned int dof_p = dofs_per_block[1];
1930
1931 IndexSet locally_relevant_dofs_acquisition;
1932 locally_relevant_dofs_acquisition =
1933 DoFTools::extract_locally_relevant_dofs(*this->dof_handler);
1934 this->locally_relevant_dofs.resize(2);
1935 this->locally_relevant_dofs[0] =
1936 locally_relevant_dofs_acquisition.get_view(0, dof_u);
1937 this->locally_relevant_dofs[1] =
1938 locally_relevant_dofs_acquisition.get_view(dof_u, dof_u + dof_p);
1939 this->zero_constraints.reinit(this->dof_handler->locally_owned_dofs(),
1940 locally_relevant_dofs_acquisition);
1941 }
1942 else
1943 {
1944 this->locally_relevant_dofs =
1945 DoFTools::extract_locally_relevant_dofs(*this->dof_handler);
1946 this->zero_constraints.reinit(this->dof_handler->locally_owned_dofs(),
1947 this->locally_relevant_dofs);
1948 }
1949
1950 DoFTools::make_hanging_node_constraints(*this->dof_handler,
1951 this->zero_constraints);
1952
1953 for (auto const &[id, type] :
1954 this->simulation_parameters.boundary_conditions.type)
1955 {
1958 {
1959 VectorTools::interpolate_boundary_values(
1960 *this->get_mapping(),
1961 *this->dof_handler,
1962 id,
1963 dealii::Functions::ZeroFunction<dim>(dim + 1),
1964 this->zero_constraints,
1965 this->fe->component_mask(velocities));
1966 }
1968 {
1969 std::set<types::boundary_id> no_normal_flux_boundaries;
1970 no_normal_flux_boundaries.insert(id);
1971 VectorTools::compute_no_normal_flux_constraints(
1972 *this->dof_handler,
1973 0,
1974 no_normal_flux_boundaries,
1975 this->zero_constraints,
1976 *this->get_mapping(),
1978 }
1980 {
1981 const auto &periodic_boundary =
1982 this->simulation_parameters.boundary_conditions.periodic_boundaries
1983 .at(id);
1984 DoFTools::make_periodicity_constraints(*this->dof_handler,
1985 id,
1986 periodic_boundary.neighbor_id,
1987 periodic_boundary.direction,
1988 this->zero_constraints);
1989 }
1991 {
1992 /*The pressure boundary condition is implemented in the matrix-based
1993 * assemblers*/
1994 }
1995 else if (type ==
1997 {
1998 /*The function weak boundary condition is implemented in the
1999 * matrix-based assemblers and the matrix-free operators*/
2000 }
2001 else if (type ==
2003 {
2004 /*The partial slip boundary condition is implemented in the
2005 * matrix-based assemblers*/
2006 }
2008 {
2009 /*The directional do-nothing boundary condition is implemented
2010 * in the matrix-based assemblers and the matrix-free operators*/
2011 }
2012 else if (type ==
2014 {
2015 /*The Neumann traction boundary condition is implemented
2016 * in the matrix-based assemblers and the matrix-free operators*/
2017 }
2019 {
2020 /*Default boundary condition*/
2021 }
2022 /// TODO ADD ASSERTION for the default case here.
2023 }
2024
2025 this->make_solid_domain(*this->dof_handler, false, this->zero_constraints);
2026
2027 this->zero_constraints.close();
2028}
2029
2030template <int dim, typename VectorType, typename DofsType>
2031void
2033{
2034 if (!this->simulation_parameters.mortar_parameters.enable)
2035 return;
2036
2037 TimerOutput::Scope t(this->computing_timer, "Reinit mortar operators");
2038
2039 // Create mortar manager
2040 if (this->simulation_parameters.mortar_parameters.interface_type ==
2042 this->mortar_manager = std::make_shared<MortarManagerCircle<dim>>(
2043 *this->cell_quadrature,
2044 *this->get_mapping(),
2045 *this->dof_handler,
2046 this->simulation_parameters.mortar_parameters);
2047 else if (this->simulation_parameters.mortar_parameters.interface_type ==
2049 this->mortar_manager = std::make_shared<MortarManagerLinear<dim>>(
2050 *this->cell_quadrature,
2051 *this->get_mapping(),
2052 *this->dof_handler,
2053 this->simulation_parameters.mortar_parameters);
2054 else
2055 AssertThrow(false, ExcMessage("Invalid mortar interface type."));
2056
2057
2058 // Create mortar coupling evaluator
2060 std::make_shared<NavierStokesCouplingEvaluationSIPG<dim, double>>(
2061 *this->get_mapping(),
2062 *this->dof_handler,
2063 this->simulation_parameters.physical_properties_manager
2064 .get_kinematic_viscosity_scale());
2065
2067 std::make_shared<CouplingOperator<dim, double>>(
2068 *this->get_mapping(),
2069 *this->dof_handler,
2070 this->zero_constraints,
2072 this->mortar_manager,
2073 this->simulation_parameters.mortar_parameters.rotor_boundary_id,
2074 this->simulation_parameters.mortar_parameters.stator_boundary_id,
2075 this->simulation_parameters.mortar_parameters.sip_factor);
2076}
2077
2078template <int dim, typename VectorType, typename DofsType>
2079void
2081 const bool is_first)
2082{
2083 if (!this->simulation_parameters.mortar_parameters.enable)
2084 return;
2085
2086 if ((simulation_parameters.mortar_parameters.verbosity ==
2088 simulation_parameters.mortar_parameters.verbosity ==
2090 !is_first)
2091 {
2092 announce_string(this->pcout, "Mortar Interface");
2093 if (simulation_parameters.mortar_parameters.verbosity ==
2096 this->simulation_parameters.mortar_parameters,
2097 this->pcout);
2098 }
2099 TimerOutput::Scope t(this->computing_timer, "Rotate rotor mapping");
2100
2101 // Get updated rotation angle (radians)
2102 simulation_parameters.mortar_parameters.rotor_rotation_angle->set_time(
2103 this->simulation_control->get_current_time());
2104 const double rotation_angle =
2105 simulation_parameters.mortar_parameters.rotor_rotation_angle->value(
2106 Point<dim>());
2107
2108 // Get updated angular velocity (radians/time)
2109 simulation_parameters.mortar_parameters.rotor_angular_velocity->set_time(
2110 this->simulation_control->get_current_time());
2111 const double angular_velocity =
2112 simulation_parameters.mortar_parameters.rotor_angular_velocity->value(
2113 Point<dim>());
2114
2115 if ((simulation_parameters.mortar_parameters.verbosity ==
2117 simulation_parameters.mortar_parameters.verbosity ==
2119 !is_first)
2120 {
2121 this->pcout << "Rotor grid angle: " << rotation_angle << " rad \n"
2122 << "Rotor grid velocity: " << angular_velocity
2123 << " rad/time \n"
2124 << std::endl;
2125 }
2126
2127 // Rotate mapping only in the case of a circular mortar interface
2128 if (this->simulation_parameters.mortar_parameters.interface_type ==
2131 *this->dof_handler,
2132 *this->mapping_cache,
2133 *this->mapping,
2134 rotation_angle,
2135 this->simulation_parameters.mortar_parameters.center_of_rotation,
2136 this->simulation_parameters.mortar_parameters.rotation_axis_direction);
2137 else if (this->simulation_parameters.mortar_parameters.interface_type ==
2139 this->mapping_cache->initialize(*this->mapping,
2140 this->dof_handler->get_triangulation());
2141 else
2142 AssertThrow(false, ExcMessage("Invalid mortar interface type."));
2143}
2144
2145template <int dim, typename VectorType, typename DofsType>
2146void
2148{
2149 if (!this->simulation_parameters.boundary_conditions.time_dependent)
2150 return;
2151
2152 // We can never assume in the code anywhere that the local_evaluation_point is
2153 // at the right value its value must always be reinitialized from the present
2154 // solution. This may appear trivial, but this is extremely important when we
2155 // are checkpointing. Trust me future Bruno.
2157
2158 double time = this->simulation_control->get_current_time();
2159
2160 for (auto const &[id, type] :
2161 this->simulation_parameters.boundary_conditions.type)
2162 {
2163 // Only set the time if it actually makes sense to set the time.
2165 type ==
2169 type ==
2171 {
2172 this->simulation_parameters.boundary_conditions
2173 .navier_stokes_functions.at(id)
2174 ->u.set_time(time);
2175 this->simulation_parameters.boundary_conditions
2176 .navier_stokes_functions.at(id)
2177 ->v.set_time(time);
2178 this->simulation_parameters.boundary_conditions
2179 .navier_stokes_functions.at(id)
2180 ->w.set_time(time);
2181 this->simulation_parameters.boundary_conditions
2182 .navier_stokes_functions.at(id)
2183 ->p.set_time(time);
2184 this->simulation_parameters.boundary_conditions
2185 .navier_stokes_functions.at(id)
2186 ->traction_fn.set_time(time);
2187 }
2188 }
2190 // Distribute constraints
2194}
2195
2196template <int dim, typename VectorType, typename DofsType>
2197void
2199{
2200 TimerOutput::Scope timer(this->computing_timer, "Read checkpoint");
2201 std::string prefix =
2202 this->simulation_parameters.simulation_control.output_folder +
2203 this->simulation_parameters.restart_parameters.filename;
2204 this->simulation_control->read(prefix);
2205 this->pvdhandler.read(prefix);
2206
2207 if (this->simulation_control->get_output_boundaries())
2208 {
2209 std::string prefix_boundary =
2210 this->simulation_parameters.simulation_control.output_folder +
2211 this->simulation_parameters.restart_parameters.filename + ".boundary";
2212
2213 this->pvdhandler_boundary.read(prefix_boundary);
2214 }
2215
2216 this->set_solution_from_checkpoint(prefix);
2217
2218 if (simulation_parameters.flow_control.enable_flow_control)
2219 {
2220 this->flow_control.read(prefix);
2221 }
2222
2223 multiphysics->read_checkpoint();
2224
2225 // Deserialize all post-processing tables that are currently used
2226 std::vector<OutputStructTableHandler> table_output_structs =
2227 this->gather_tables();
2228 deserialize_tables_vector(table_output_structs, this->mpi_communicator);
2229}
2230
2231template <int dim, typename VectorType, typename DofsType>
2232void
2234 const std::string &checkpoint_file_prefix)
2235{
2236 const std::string filename = checkpoint_file_prefix + ".triangulation";
2237 check_file_exists(filename,
2238 "checkpoint file of the triangulation, given by "
2239 "'subsection restart' - 'set filename'");
2240
2241 try
2242 {
2243 if (auto tria = dynamic_cast<parallel::distributed::Triangulation<dim> *>(
2244 this->triangulation.get()))
2245 {
2246 tria->load(filename.c_str());
2247 }
2248 }
2249 catch (...)
2250 {
2251 AssertThrow(false,
2252 ExcMessage("Cannot open snapshot mesh file or read the "
2253 "triangulation stored there."));
2254 }
2255
2256
2257 setup_dofs();
2259
2260 // SolutionTransfer::deserialize writes into the target vectors. For Trilinos
2261 // vectors this is forbidden on ghosted vectors (debug-mode assertion), so we
2262 // build purely locally-owned temporaries and copy the result into the
2263 // ghosted solution vectors below. For deal.II's parallel distributed vector
2264 // SolutionTransfer expects the targets to have their ghosts set up, so in
2265 // that case we keep the ghosted construction. In both cases, the ghost values
2266 // of the solution vectors are updated after the copy.
2267 std::vector<VectorType *> x_system(1 + previous_solutions->size());
2268
2269 auto make_distributed = [this]() {
2270 if constexpr (std::is_same_v<VectorType,
2271 LinearAlgebra::distributed::Vector<double>>)
2272 return VectorType(this->locally_owned_dofs,
2274 this->mpi_communicator);
2275 else
2276 return VectorType(this->locally_owned_dofs, this->mpi_communicator);
2277 };
2278
2279 VectorType distributed_system = make_distributed();
2280 x_system[0] = &(distributed_system);
2281
2282 std::vector<VectorType> distributed_previous_solutions;
2283 distributed_previous_solutions.reserve(previous_solutions->size());
2284 for (unsigned int i = 0; i < previous_solutions->size(); ++i)
2285 {
2286 distributed_previous_solutions.emplace_back(make_distributed());
2287 x_system[i + 1] = &distributed_previous_solutions[i];
2288 }
2289 SolutionTransfer<dim, VectorType> system_trans_vectors(*this->dof_handler);
2290
2292 {
2293 std::vector<VectorType *> sum_vectors =
2294 this->average_velocities->read(checkpoint_file_prefix);
2295
2296 x_system.insert(x_system.end(), sum_vectors.begin(), sum_vectors.end());
2297 }
2298
2299 system_trans_vectors.deserialize(x_system);
2300 *this->present_solution = distributed_system;
2301 for (unsigned int i = 0; i < previous_solutions->size(); ++i)
2302 {
2303 (*previous_solutions)[i] = distributed_previous_solutions[i];
2304 }
2305
2306 // The deserialized vectors have no ghost values, so the copies above zero the
2307 // ghost entries of deal.II vectors. Update them, since the solution is read
2308 // before the first solve (e.g. by the dynamic flow control).
2309 this->present_solution->update_ghost_values();
2310 for (auto &previous_solution : *previous_solutions)
2311 previous_solution.update_ghost_values();
2312
2314}
2315
2316template <int dim, typename VectorType, typename DofsType>
2317void
2320{
2321 if (!this->average_velocities_are_enabled())
2322 return;
2323
2324 // Reset the average velocity profile if the initial time to average the
2325 // velocities has not been reached. Disabled if the initial condition is an
2326 // average velocity profile.
2327 if (simulation_parameters.post_processing.calculate_average_velocities &
2328 (this->simulation_parameters.initial_condition->type !=
2330 {
2331 if ((this->simulation_parameters.post_processing
2332 .initial_time_for_average_velocities +
2333 1e-6 * simulation_control->get_time_step()) >
2334 this->simulation_control->get_current_time())
2335 {
2336 this->pcout
2337 << "Warning: The checkpointed time-averaged velocity has been reinitialized because the initial averaging time has not yet been reached."
2338 << std::endl;
2339 this->average_velocities->zero_average_after_restart();
2340 }
2341 }
2342
2343 this->average_velocities->sanitize_after_restart(
2344 this->simulation_control->get_current_time());
2345}
2346
2347template <int dim, typename VectorType, typename DofsType>
2348void
2350 const DoFHandler<dim> &dof_handler,
2351 const bool non_zero_constraints,
2352 AffineConstraints<double> &constraints)
2353{
2354 // If there are no solid regions, there is no work to be done and we can
2355 // return.
2356 if (simulation_parameters.physical_properties_manager
2357 .get_number_of_solids() == 0)
2358 return;
2359
2360 // Otherwise we call the generic function to set the solid domain.
2363 non_zero_constraints,
2364 constraints);
2365}
2366
2367template <int dim, typename VectorType, typename DofsType>
2368void
2370 const DoFHandler<dim> *dof_handler_ht)
2371{
2372 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
2373 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2374
2375 // Get struct containing temperature range information and flag
2376 // containers for DOFs.
2377 StasisConstraintWithTemperature &stasis_constraint_struct =
2379
2380 // Get domain restriction with plane information
2381 bool restrain_domain_with_plane =
2382 this->simulation_parameters.constrain_solid_domain
2383 .enable_domain_restriction_with_plane;
2384 Point<dim> plane_point(
2385 this->simulation_parameters.constrain_solid_domain.restriction_plane_point);
2386 Tensor<1, dim> plane_normal_vector(
2387 this->simulation_parameters.constrain_solid_domain
2388 .restriction_plane_normal_vector);
2389
2390 // Get temperature solution
2391 const auto temperature_solution =
2392 this->multiphysics->get_solution(PhysicsID::heat_transfer);
2393 std::vector<double> local_temperature_values(this->cell_quadrature->size());
2394
2395 for (const auto &cell : dof_handler->active_cell_iterators())
2396 {
2397 if (cell->is_locally_owned() || cell->is_ghost())
2398 {
2399 cell->get_dof_indices(local_dof_indices);
2400
2401 // If a restriction plane is defined, check if the cell is in the
2402 // valid domain.
2403 if (!restrain_domain_with_plane ||
2405 plane_point,
2406 plane_normal_vector))
2407 {
2409 dof_handler_ht,
2410 temperature_solution,
2411 local_temperature_values);
2412 identify_cell_and_constrain_velocity(local_dof_indices,
2413 local_temperature_values,
2414 stasis_constraint_struct);
2415 }
2416 }
2417 }
2418}
2419
2420template <int dim, typename VectorType, typename DofsType>
2421void
2423 constrain_stasis_with_temperature_cls(const DoFHandler<dim> *dof_handler_cls,
2424 const DoFHandler<dim> *dof_handler_ht)
2425{
2426 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
2427 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2428
2429 // Get domain restriction with plane information
2430 bool restrain_domain_with_plane =
2431 this->simulation_parameters.constrain_solid_domain
2432 .enable_domain_restriction_with_plane;
2433 Point<dim> plane_point(
2434 this->simulation_parameters.constrain_solid_domain.restriction_plane_point);
2435 Tensor<1, dim> plane_normal_vector(
2436 this->simulation_parameters.constrain_solid_domain
2437 .restriction_plane_normal_vector);
2438
2439 // Get filtered phase indicator solution
2440 const auto filtered_phase_indicator_solution =
2441 this->multiphysics->get_filtered_solution(PhysicsID::CLS);
2442 std::vector<double> local_filtered_phase_indicator_values(
2443 this->cell_quadrature->size());
2444
2445 // Get temperature solution
2446 const auto temperature_solution =
2447 this->multiphysics->get_solution(PhysicsID::heat_transfer);
2448 std::vector<double> local_temperature_values(this->cell_quadrature->size());
2449
2450 // Loop over structs containing fluid id, temperature and phase indicator
2451 // range information, and flag containers for DOFs.
2452 for (StasisConstraintWithTemperature &stasis_constraint_struct :
2454 {
2455 for (const auto &cell : dof_handler->active_cell_iterators())
2456 {
2457 if (cell->is_locally_owned() || cell->is_ghost())
2458 {
2459 cell->get_dof_indices(local_dof_indices);
2460
2461 // If a restriction plane is defined, check if the cell is in the
2462 // valid domain.
2463 if (!restrain_domain_with_plane ||
2465 plane_point,
2466 plane_normal_vector))
2467 {
2468 bool cell_is_in_right_fluid = true;
2470 cell,
2471 dof_handler_cls,
2472 filtered_phase_indicator_solution,
2473 local_filtered_phase_indicator_values);
2474
2475 // Check if cell is only in the fluid of interest. As soon as
2476 // one filtered phase indicator value is outside the tolerated
2477 // range, the cell is perceived as being in the wrong fluid.
2478 for (const double &filtered_phase_indicator :
2479 local_filtered_phase_indicator_values)
2480 {
2481 if (abs(stasis_constraint_struct.fluid_id -
2482 filtered_phase_indicator) >=
2483 stasis_constraint_struct
2484 .filtered_phase_indicator_tolerance)
2485 {
2486 cell_is_in_right_fluid = false;
2487 break;
2488 }
2489 }
2490
2491 // If the cell is not in the right fluid, no solid constraint
2492 // will be applied on the cell's DOFs; we skip to the next
2493 // cell.
2494 if (!cell_is_in_right_fluid)
2495 continue;
2496
2498 dof_handler_ht,
2499 temperature_solution,
2500 local_temperature_values);
2502 local_dof_indices,
2503 local_temperature_values,
2504 stasis_constraint_struct);
2505 }
2506 }
2507 }
2508 }
2509}
2510
2511template <int dim, typename VectorType, typename DofsType>
2512void
2515 const std::vector<types::global_dof_index> &local_dof_indices,
2516 const std::vector<double> &local_temperature_values,
2517 StasisConstraintWithTemperature &stasis_constraint_struct)
2518{
2519 for (const double &temperature : local_temperature_values)
2520 {
2521 // Skip cells with at least 1 DOF that is outbound the temperature limits.
2522 if (temperature < stasis_constraint_struct.min_solid_temperature ||
2523 temperature > stasis_constraint_struct.max_solid_temperature)
2524 return;
2525 }
2527 local_dof_indices,
2528 false,
2530}
2531
2532template <int dim, typename VectorType, typename DofsType>
2533std::vector<OutputStruct<dim, VectorType>>
2535{
2536 return std::vector<OutputStruct<dim, VectorType>>();
2537}
2538
2539template <int dim, typename VectorType, typename DofsType>
2540void
2542 const VectorType &solution,
2543 std::vector<OutputStruct<dim, VectorType>> &solution_output_structs)
2544{
2545 // Add the interpretation of the solution. The dim first components are the
2546 // velocity vectors and the following one is the pressure.
2547 std::vector<std::string> solution_names(dim, "velocity");
2548 solution_names.emplace_back("pressure");
2549 std::vector<DataComponentInterpretation::DataComponentInterpretation>
2550 data_component_interpretation(
2551 dim, DataComponentInterpretation::component_is_part_of_vector);
2552 data_component_interpretation.emplace_back(
2553 DataComponentInterpretation::component_is_scalar);
2554
2555 OutputStructSolution<dim, VectorType> solution_fd_struct(
2556 *this->dof_handler,
2557 solution,
2558 solution_names,
2559 data_component_interpretation);
2560
2561 // Add the interpretation of the solution to the output structs
2562 // using in_place_type to save memory allocation and avoid unnecessary copies
2563 solution_output_structs.emplace_back(
2564 std::in_place_type<OutputStructSolution<dim, VectorType>>,
2565 *this->dof_handler,
2566 solution,
2567 solution_names,
2568 data_component_interpretation);
2569
2570 if (this->simulation_parameters.post_processing
2571 .calculate_average_velocities ||
2572 this->simulation_parameters.initial_condition->type ==
2574 {
2575 // Add the interpretation of the average solution. The dim first
2576 // components are the average velocity vectors and the following one is
2577 // the average pressure. (<u>, <v>, <w>, <p>)
2578 std::vector<std::string> average_solution_names(dim, "average_velocity");
2579 average_solution_names.emplace_back("average_pressure");
2580
2581 std::vector<DataComponentInterpretation::DataComponentInterpretation>
2582 average_data_component_interpretation(
2583 dim, DataComponentInterpretation::component_is_part_of_vector);
2584 average_data_component_interpretation.emplace_back(
2585 DataComponentInterpretation::component_is_scalar);
2586 solution_output_structs.emplace_back(
2587 std::in_place_type<OutputStructSolution<dim, VectorType>>,
2588 *this->dof_handler,
2589 *this->average_velocities->get_average_velocities(),
2590 average_solution_names,
2591 average_data_component_interpretation);
2592
2593 // Add the interpretation of the reynolds stresses of solution.
2594 // The dim first components are the normal reynolds stress vectors and
2595 // the following ones are others resolved reynolds stresses.
2596 std::vector<std::string> reynolds_normal_stress_names(
2597 dim, "reynolds_normal_stress");
2598 reynolds_normal_stress_names.emplace_back("turbulent_kinetic_energy");
2599 std::vector<DataComponentInterpretation::DataComponentInterpretation>
2600 reynolds_normal_stress_data_component_interpretation(
2601 dim, DataComponentInterpretation::component_is_part_of_vector);
2602 reynolds_normal_stress_data_component_interpretation.emplace_back(
2603 DataComponentInterpretation::component_is_scalar);
2604
2605 std::vector<std::string> reynolds_shear_stress_names = {
2606 "reynolds_shear_stress_uv"};
2607 if (dim == 2)
2608 {
2609 reynolds_shear_stress_names.emplace_back("dummy_rss_2d");
2610 }
2611 if (dim == 3)
2612 {
2613 reynolds_shear_stress_names.emplace_back("reynolds_shear_stress_vw");
2614 reynolds_shear_stress_names.emplace_back("reynolds_shear_stress_uw");
2615 }
2616 reynolds_shear_stress_names.emplace_back("dummy_rss");
2617
2618 std::vector<DataComponentInterpretation::DataComponentInterpretation>
2619 reynolds_shear_stress_data_component_interpretation(
2620 dim, DataComponentInterpretation::component_is_scalar);
2621 reynolds_shear_stress_data_component_interpretation.emplace_back(
2622 DataComponentInterpretation::component_is_scalar);
2623
2624 solution_output_structs.emplace_back(
2625 std::in_place_type<OutputStructSolution<dim, VectorType>>,
2626 *this->dof_handler,
2627 this->average_velocities->get_reynolds_normal_stresses(),
2628 reynolds_normal_stress_names,
2629 reynolds_normal_stress_data_component_interpretation);
2630
2631 solution_output_structs.emplace_back(
2632 std::in_place_type<OutputStructSolution<dim, VectorType>>,
2633 *this->dof_handler,
2634 this->average_velocities->get_reynolds_shear_stresses(),
2635 reynolds_shear_stress_names,
2636 reynolds_shear_stress_data_component_interpretation);
2637 }
2638
2639 // Create the post-processors to have derived information about the velocity
2640 // They are generated outside the if condition for smoothing to ensure
2641 // that the objects still exist when the write output of DataOut is called
2642 // Regular discontinuous postprocessors
2643 // They are created as shared pointers to outlive the function
2644 if (this->simulation_parameters.post_processing.output_q_criterion)
2645 {
2646 std::shared_ptr<QCriterionPostprocessor<dim>> qcriterion =
2647 std::make_shared<QCriterionPostprocessor<dim>>();
2648 solution_output_structs.emplace_back(
2650 *this->dof_handler,
2651 solution,
2652 qcriterion);
2653 }
2654
2655 std::shared_ptr<DivergencePostprocessor<dim>> divergence =
2656 std::make_shared<DivergencePostprocessor<dim>>();
2657 solution_output_structs.emplace_back(
2659 *this->dof_handler,
2660 solution,
2661 divergence);
2662
2663 if (this->simulation_parameters.post_processing.output_velocity_gradient)
2664 {
2665 std::shared_ptr<GradientPostprocessor<dim>> gradient =
2666 std::make_shared<GradientPostprocessor<dim>>();
2667 solution_output_structs.emplace_back(
2669 *this->dof_handler,
2670 solution,
2671 gradient);
2672 }
2673
2674 if (this->simulation_parameters.post_processing.output_vorticity)
2675 {
2676 std::shared_ptr<VorticityPostprocessor<dim>> vorticity =
2677 std::make_shared<VorticityPostprocessor<dim>>();
2678 solution_output_structs.emplace_back(
2680 *this->dof_handler,
2681 solution,
2682 vorticity);
2683 }
2684
2685 // Get physical properties models
2686 std::vector<std::shared_ptr<DensityModel>> density_models =
2687 this->simulation_parameters.physical_properties_manager
2688 .get_density_vector();
2689 std::vector<std::shared_ptr<RheologicalModel>> rheological_models =
2690 this->simulation_parameters.physical_properties_manager
2691 .get_rheology_vector();
2692
2693 const unsigned int n_fluids =
2694 this->simulation_parameters.physical_properties_manager
2695 .get_number_of_fluids();
2696
2697 std::vector<std::shared_ptr<DensityPostprocessor<dim>>>
2698 density_postprocessors;
2699 density_postprocessors.reserve(n_fluids);
2700 std::vector<std::shared_ptr<KinematicViscosityPostprocessor<dim>>>
2701 kinematic_viscosity_postprocessors;
2702 kinematic_viscosity_postprocessors.reserve(n_fluids);
2703 std::vector<std::shared_ptr<DynamicViscosityPostprocessor<dim>>>
2704 dynamic_viscosity_postprocessors;
2705 dynamic_viscosity_postprocessors.reserve(n_fluids);
2706
2707 for (unsigned int f_id = 0; f_id < n_fluids; ++f_id)
2708 {
2709 density_postprocessors.emplace_back(
2710 std::make_shared<DensityPostprocessor<dim>>(density_models[f_id],
2711 f_id));
2712 kinematic_viscosity_postprocessors.emplace_back(
2713 std::make_shared<KinematicViscosityPostprocessor<dim>>(
2714 rheological_models[f_id], f_id));
2715 dynamic_viscosity_postprocessors.emplace_back(
2716 std::make_shared<DynamicViscosityPostprocessor<dim>>(
2717 rheological_models[f_id],
2718 density_models[f_id]->get_density_ref(),
2719 f_id));
2720
2721 // Only output when density is not constant or if it is a multiphase
2722 // flow
2723 if (!density_models[f_id]->is_constant_density_model() ||
2724 this->simulation_parameters.multiphysics.CLS ||
2725 this->simulation_parameters.multiphysics.cahn_hilliard)
2726 solution_output_structs.emplace_back(
2728 *this->dof_handler,
2729 solution,
2730 density_postprocessors[f_id]);
2731
2732 // Only output the kinematic viscosity for non-newtonian rheology
2733 if (rheological_models[f_id]->is_non_newtonian_rheological_model())
2734 {
2735 solution_output_structs.emplace_back(
2737 *this->dof_handler,
2738 solution,
2739 kinematic_viscosity_postprocessors[f_id]);
2740
2741 // Only output the dynamic viscosity for multiphase flows
2742 if (this->simulation_parameters.multiphysics.CLS ||
2743 this->simulation_parameters.multiphysics.cahn_hilliard)
2744 solution_output_structs.emplace_back(
2746 *this->dof_handler,
2747 solution,
2748 kinematic_viscosity_postprocessors[f_id]);
2749 }
2750 }
2751
2752 if (this->simulation_parameters.physical_properties_manager
2753 .is_non_newtonian())
2754 {
2755 auto shear_rate_processor =
2756 std::make_shared<ShearRatePostprocessor<dim>>();
2757 solution_output_structs.emplace_back(
2759 *this->dof_handler,
2760 solution,
2761 shear_rate_processor);
2762 }
2763
2764 SRFPostprocessor<dim> srf(simulation_parameters.velocity_sources.omega_x,
2765 simulation_parameters.velocity_sources.omega_y,
2766 simulation_parameters.velocity_sources.omega_z);
2767
2768 if (simulation_parameters.velocity_sources.rotating_frame_type ==
2770 solution_output_structs.emplace_back(
2772 *this->dof_handler,
2773 solution,
2774 std::make_shared<SRFPostprocessor<dim>>(srf));
2775
2776 // Add subdomain id output
2777 Vector<float> subdomain(this->triangulation->n_active_cells());
2778 for (unsigned int i = 0; i < subdomain.size(); ++i)
2779 subdomain(i) = this->triangulation->locally_owned_subdomain();
2780
2781 solution_output_structs.emplace_back(
2782 std::in_place_type<OutputStructCellVector>, subdomain, "subdomain");
2783
2784 // Add fields specific to other physics
2785 std::vector<OutputStruct<dim, VectorType>> additional_output_structs =
2786 this->gather_output_hook();
2787
2788 for (const auto &output_struct : additional_output_structs)
2789 solution_output_structs.emplace_back(output_struct);
2790}
2791
2792template <int dim, typename VectorType, typename DofsType>
2793void
2795 const VectorType &solution)
2796{
2797 TimerOutput::Scope t(this->computing_timer, "Output results");
2798
2799 const std::string folder = simulation_control->get_output_path();
2800 const std::string solution_name = simulation_control->get_output_name();
2801 const unsigned int iter = simulation_control->get_iteration_number();
2802 const double time = simulation_control->get_current_time();
2803 const unsigned int subdivision = simulation_control->get_number_subdivision();
2804 const unsigned int group_files = simulation_control->get_group_files();
2805
2806 // Vector containing structs with vtu output information
2807 std::vector<OutputStruct<dim, VectorType>> solution_output_structs;
2808
2809 // Gather all results in vector container
2810 gather_output_results(*this->present_solution, solution_output_structs);
2811
2812 // Create data output object
2813 DataOut<dim> data_out;
2814
2815 // Additional flag to enable the output of high-order elements
2816 DataOutBase::VtkFlags flags;
2817 if (this->velocity_fem_degree > 1)
2818 flags.write_higher_order_cells = true;
2819 data_out.set_flags(flags);
2820
2821 // Attach DoF handler to data output object
2822 data_out.attach_dof_handler(*this->dof_handler);
2823
2824 // Fill data out object with solutions in structs
2825 for (const auto &solution_output_struct : solution_output_structs)
2826 {
2827 if (auto solution_struct =
2829 &solution_output_struct))
2830 {
2831 data_out.add_data_vector(
2832 solution_struct->dof_handler,
2833 solution_struct->solution,
2834 solution_struct->solution_names,
2835 solution_struct->data_component_interpretation);
2836 }
2837 else if (auto vector_struct =
2838 std::get_if<OutputStructCellVector>(&solution_output_struct))
2839 {
2840 data_out.add_data_vector(vector_struct->solution,
2841 vector_struct->solution_name);
2842 }
2843 else if (auto postprocessor_struct =
2845 &solution_output_struct))
2846 {
2847 data_out.add_data_vector(postprocessor_struct->dof_handler,
2848 postprocessor_struct->solution,
2849 *postprocessor_struct->data_postprocessor);
2850 }
2851 }
2852
2853 // Add multiphysics output
2854 std::vector<OutputStruct<dim, GlobalVectorType>> multiphysics_output_structs =
2855 multiphysics->gather_output_hook_global_vector();
2856
2857 for (const auto &output_struct : multiphysics_output_structs)
2858 if (auto solution_struct =
2860 &output_struct))
2861 {
2862 data_out.add_data_vector(
2863 solution_struct->dof_handler,
2864 solution_struct->solution,
2865 solution_struct->solution_names,
2866 solution_struct->data_component_interpretation);
2867 }
2868 else if (auto vector_struct =
2869 std::get_if<OutputStructCellVector>(&output_struct))
2870 {
2871 data_out.add_data_vector(vector_struct->solution,
2872 vector_struct->solution_name);
2873 }
2874 else if (auto postprocessor_struct =
2876 &output_struct))
2877 {
2878 data_out.add_data_vector(postprocessor_struct->dof_handler,
2879 postprocessor_struct->solution,
2880 *postprocessor_struct->data_postprocessor);
2881 }
2882
2883 // Add multiphysics block vector solution
2884 std::vector<OutputStruct<dim, GlobalBlockVectorType>>
2885 multiphysics_output_structs_block =
2886 multiphysics->gather_output_hook_global_block_vector();
2887 for (const auto &output_struct : multiphysics_output_structs_block)
2888 if (auto solution_struct =
2890 &output_struct))
2891 {
2892 data_out.add_data_vector(
2893 solution_struct->dof_handler,
2894 solution_struct->solution,
2895 solution_struct->solution_names,
2896 solution_struct->data_component_interpretation);
2897 }
2898 else if (auto vector_struct =
2899 std::get_if<OutputStructCellVector>(&output_struct))
2900 {
2901 data_out.add_data_vector(vector_struct->solution,
2902 vector_struct->solution_name);
2903 }
2904 else if (auto postprocessor_struct = std::get_if<
2906 &output_struct))
2907 {
2908 data_out.add_data_vector(postprocessor_struct->dof_handler,
2909 postprocessor_struct->solution,
2910 *postprocessor_struct->data_postprocessor);
2911 }
2912
2913
2914 // Build the patches and write the output
2915 data_out.build_patches(*this->get_mapping(),
2916 subdivision,
2917 DataOut<dim>::curved_inner_cells);
2918
2920 data_out,
2921 folder,
2922 solution_name,
2923 time,
2924 iter,
2925 group_files,
2926 this->mpi_communicator);
2927
2928 if (simulation_control->get_output_boundaries() &&
2929 (simulation_control->get_iteration_number() == 0 ||
2930 simulation_parameters.mortar_parameters.enable))
2931 {
2932 DataOutFaces<dim> data_out_faces;
2933
2934 // Add the additional flag to enable high-order cells output when the
2935 // velocity interpolation degree is larger than 1
2936 DataOutBase::VtkFlags flags;
2937 if (this->velocity_fem_degree > 1)
2938 flags.write_higher_order_cells = true;
2939 data_out_faces.set_flags(flags);
2940
2941 BoundaryPostprocessor<dim> boundary_id;
2942 data_out_faces.attach_dof_handler(*this->dof_handler);
2943 data_out_faces.add_data_vector(solution, boundary_id);
2944 data_out_faces.build_patches(*this->get_mapping(), subdivision);
2945
2947 data_out_faces,
2948 folder,
2949 time,
2950 iter,
2951 group_files,
2952 this->mpi_communicator);
2953 }
2954}
2955
2956template <int dim, typename VectorType, typename DofsType>
2957void
2959{
2960 TimerOutput::Scope t(this->computing_timer, "Output forces");
2961 if (this->this_mpi_process == 0)
2962 {
2963 for (auto const &[boundary_id, type] :
2964 simulation_parameters.boundary_conditions.type)
2965 {
2966 std::string filename =
2967 simulation_parameters.simulation_control.output_folder +
2968 simulation_parameters.forces_parameters.force_output_name + "." +
2969 Utilities::int_to_string(boundary_id, 2) + ".dat";
2970 std::ofstream output(filename.c_str());
2971
2972 forces_tables[boundary_id].write_text(output);
2973 }
2974 }
2975}
2976
2977template <int dim, typename VectorType, typename DofsType>
2978void
2980{
2981 TimerOutput::Scope t(this->computing_timer, "Output torques");
2982 if (this->this_mpi_process == 0)
2983 {
2984 for (auto const &[boundary_id, type] :
2985 simulation_parameters.boundary_conditions.type)
2986 {
2987 std::string filename =
2988 simulation_parameters.simulation_control.output_folder +
2989 simulation_parameters.forces_parameters.torque_output_name + "." +
2990 Utilities::int_to_string(boundary_id, 2) + ".dat";
2991 std::ofstream output(filename.c_str());
2992
2993 this->torques_tables[boundary_id].write_text(output);
2994 }
2995 }
2996}
2997
2998template <int dim, typename VectorType, typename DofsType>
2999std::vector<OutputStructTableHandler>
3001{
3002 std::vector<OutputStructTableHandler> table_output_structs;
3003 const Parameters::PostProcessing<dim> post_processing =
3004 this->simulation_parameters.post_processing;
3005 std::string prefix =
3006 this->simulation_parameters.simulation_control.output_folder;
3007 std::string suffix = ".checkpoint";
3008
3009 if (post_processing.calculate_enstrophy)
3010 table_output_structs.emplace_back(this->enstrophy_table,
3011 prefix +
3012 post_processing.enstrophy_output_name +
3013 suffix);
3014 if (post_processing.calculate_kinetic_energy)
3015 table_output_structs.emplace_back(
3017 prefix + post_processing.kinetic_energy_output_name + suffix);
3018 if (post_processing.calculate_pressure_power)
3019 table_output_structs.emplace_back(
3021 prefix + post_processing.pressure_power_output_name + suffix);
3022 if (post_processing.calculate_viscous_dissipation)
3023 table_output_structs.emplace_back(
3025 prefix + post_processing.viscous_dissipation_output_name + suffix);
3026 if (post_processing.calculate_apparent_viscosity)
3027 table_output_structs.emplace_back(
3029 prefix + post_processing.apparent_viscosity_output_name + suffix);
3030 if (post_processing.calculate_flow_rate)
3031 table_output_structs.emplace_back(this->flow_rate_table,
3032 prefix +
3033 post_processing.flow_rate_output_name +
3034 suffix);
3035 if (post_processing.calculate_pressure_drop)
3036 table_output_structs.emplace_back(
3037 this->pressure_drop_table,
3038 prefix + post_processing.pressure_drop_output_name + suffix);
3039 if (this->simulation_parameters.forces_parameters.calculate_force)
3040 for (auto const &[boundary_id, type] :
3041 this->simulation_parameters.boundary_conditions.type)
3042 {
3043 table_output_structs.emplace_back(
3044 this->forces_tables[boundary_id],
3045 prefix +
3046 this->simulation_parameters.forces_parameters.force_output_name +
3047 "_" + Utilities::int_to_string(boundary_id, 2) + suffix);
3048 }
3049 if (this->simulation_parameters.forces_parameters.calculate_torque)
3050 for (auto const &[boundary_id, type] :
3051 this->simulation_parameters.boundary_conditions.type)
3052 {
3053 table_output_structs.emplace_back(
3054 this->torques_tables[boundary_id],
3055 prefix +
3056 this->simulation_parameters.forces_parameters.torque_output_name +
3057 "_" + Utilities::int_to_string(boundary_id, 2) + suffix);
3058 }
3059 if (this->simulation_parameters.analytical_solution->calculate_error())
3060 table_output_structs.emplace_back(
3061 this->error_table,
3062 prefix + this->simulation_parameters.analytical_solution->get_filename() +
3063 "_FD" + suffix);
3064
3065 return table_output_structs;
3066}
3067
3068template <int dim, typename VectorType, typename DofsType>
3069void
3071{
3072 TimerOutput::Scope timer(this->computing_timer, "Write checkpoint");
3073 std::string prefix =
3074 this->simulation_parameters.simulation_control.output_folder +
3075 this->simulation_parameters.restart_parameters.filename;
3076 if (Utilities::MPI::this_mpi_process(this->mpi_communicator) == 0)
3077 {
3078 simulation_control->save(prefix);
3079 this->pvdhandler.save(prefix);
3080
3081 if (simulation_parameters.flow_control.enable_flow_control)
3082 this->flow_control.save(prefix);
3083
3084 if (this->simulation_control->get_output_boundaries())
3085 {
3086 std::string prefix_boundary =
3087 this->simulation_parameters.simulation_control.output_folder +
3088 this->simulation_parameters.restart_parameters.filename +
3089 ".boundary";
3090
3091 this->pvdhandler_boundary.save(prefix_boundary);
3092 }
3093 }
3094
3095 std::vector<const VectorType *> sol_set_transfer;
3096 sol_set_transfer.emplace_back(&(*this->present_solution));
3097 for (unsigned int i = 0; i < previous_solutions->size(); ++i)
3098 {
3099 sol_set_transfer.emplace_back(&(*previous_solutions)[i]);
3100 }
3101
3103 {
3104 std::vector<const VectorType *> av_set_transfer =
3105 this->average_velocities->save(prefix);
3106
3107 // Insert average velocities vectors into the set transfer vector
3108 sol_set_transfer.insert(sol_set_transfer.end(),
3109 av_set_transfer.begin(),
3110 av_set_transfer.end());
3111 }
3112
3113 SolutionTransfer<dim, VectorType> system_trans_vectors(*this->dof_handler);
3114 system_trans_vectors.prepare_for_serialization(sol_set_transfer);
3115
3116 multiphysics->write_checkpoint();
3117
3118 if (auto tria = dynamic_cast<parallel::distributed::Triangulation<dim> *>(
3119 this->triangulation.get()))
3120 {
3121 std::string triangulationName = prefix + ".triangulation";
3122 tria->save(prefix + ".triangulation");
3123 }
3124
3125 // Serialize all post-processing tables that are currently used
3126 const std::vector<OutputStructTableHandler> &table_output_structs =
3127 this->gather_tables();
3128 serialize_tables_vector(table_output_structs, this->mpi_communicator);
3129}
3130
3131template <int dim, typename VectorType, typename DofsType>
3132void
3135{
3136 const double pressure_scaling_factor =
3137 simulation_parameters.stabilization.pressure_scaling_factor;
3138
3139 // We skip the function if the factor has a value of 1
3140 if (abs(pressure_scaling_factor - 1) < 1e-8)
3141 return;
3142
3143 TimerOutput::Scope t(this->computing_timer, "Rescale pressure");
3144
3145 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
3146 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
3147 // Map used to keep track of which DOFs have been looped over
3148 std::unordered_set<unsigned int> rescaled_dofs_set;
3149 rescaled_dofs_set.clear();
3150 for (const auto &cell : dof_handler->active_cell_iterators())
3151 {
3152 if (cell->is_locally_owned() || cell->is_ghost())
3153 {
3154 cell->get_dof_indices(local_dof_indices);
3155 for (unsigned int j = 0; j < local_dof_indices.size(); ++j)
3156 {
3157 const unsigned int global_id = local_dof_indices[j];
3158 // We check if we have already checked this DOF
3159 auto iterator = rescaled_dofs_set.find(global_id);
3160 if (iterator == rescaled_dofs_set.end())
3161 {
3162 const int component_index =
3163 this->fe->system_to_component_index(j).first;
3164 if (this->dof_handler->locally_owned_dofs().is_element(
3165 global_id) &&
3166 component_index == dim)
3167 {
3168 this->newton_update(global_id) =
3169 this->newton_update(global_id) *
3170 pressure_scaling_factor;
3171 }
3172 rescaled_dofs_set.insert(global_id);
3173 }
3174 }
3175 }
3176 }
3177}
3178
3179template <int dim, typename VectorType, typename DofsType>
3180void
3182{
3183 TimerOutput::Scope t(this->computing_timer,
3184 "Calculate and output norms after Newton its");
3185
3186 if constexpr (std::is_same_v<VectorType, GlobalVectorType> ||
3187 std::is_same_v<VectorType,
3188 LinearAlgebra::distributed::Vector<double>>)
3189 {
3190 FEValuesExtractors::Vector velocities(0);
3191 FEValuesExtractors::Scalar pressure(dim);
3192
3193 ComponentMask velocity_mask = fe->component_mask(velocities);
3194 ComponentMask pressure_mask = fe->component_mask(pressure);
3195
3196 const std::vector<IndexSet> index_set_velocity =
3197 DoFTools::locally_owned_dofs_per_component(*dof_handler, velocity_mask);
3198 const std::vector<IndexSet> index_set_pressure =
3199 DoFTools::locally_owned_dofs_per_component(*dof_handler, pressure_mask);
3200
3201 double local_sum = 0.0;
3202 double local_max = std::numeric_limits<double>::lowest();
3203
3204 for (int d = 0; d < dim; ++d)
3205 {
3206 for (const auto &j : index_set_velocity[d])
3207 {
3208 double dof_newton_update = newton_update[j];
3209 local_sum += dof_newton_update * dof_newton_update;
3210 local_max = std::max(local_max, std::abs(dof_newton_update));
3211 }
3212 }
3213
3214 double global_velocity_l2_norm =
3215 std::sqrt(Utilities::MPI::sum(local_sum, this->mpi_communicator));
3216 double global_velocity_linfty_norm =
3217 Utilities::MPI::max(local_max, this->mpi_communicator);
3218
3219 local_sum = 0.0;
3220 local_max = std::numeric_limits<double>::lowest();
3221
3222 for (const auto &j : index_set_pressure[dim])
3223 {
3224 double dof_newton_update = newton_update[j];
3225 local_sum += dof_newton_update * dof_newton_update;
3226 local_max = std::max(local_max, std::abs(dof_newton_update));
3227 }
3228
3229 double global_pressure_l2_norm =
3230 std::sqrt(Utilities::MPI::sum(local_sum, this->mpi_communicator));
3231 double global_pressure_linfty_norm =
3232 Utilities::MPI::max(local_max, this->mpi_communicator);
3233
3234 this->pcout << "\n\t ||du||_L2 = " << std::setw(6)
3235 << global_velocity_l2_norm << std::setw(6)
3236 << "\t ||du||_Linfty = " << global_velocity_linfty_norm
3237 << std::endl;
3238 this->pcout << "\t ||dp||_L2 = " << std::setw(6)
3239 << global_pressure_l2_norm << std::setw(6)
3240 << "\t ||dp||_Linfty = " << global_pressure_linfty_norm
3241 << std::endl;
3242 }
3243 if constexpr (std::is_same_v<VectorType, GlobalBlockVectorType>)
3244 {
3245 this->pcout << "\t ||du||_L2 = " << std::setw(6)
3246 << newton_update.block(0).l2_norm() << std::setw(6)
3247 << "\t ||du||_Linfty = "
3248 << newton_update.block(0).linfty_norm() << std::endl;
3249 this->pcout << "\t ||dp||_L2 = " << std::setw(6)
3250 << newton_update.block(1).l2_norm() << std::setw(6)
3251 << "\t ||dp||_Linfty = "
3252 << newton_update.block(1).linfty_norm() << std::endl;
3253 }
3254}
3255
3256template <int dim, typename VectorType, typename DofsType>
3257inline VectorType
3259{
3260 VectorType tmp;
3261
3262 if constexpr (std::is_same_v<VectorType, GlobalVectorType> ||
3263 std::is_same_v<VectorType, GlobalBlockVectorType>)
3264 tmp.reinit(locally_owned_dofs, this->mpi_communicator);
3265
3266 else if constexpr (std::is_same_v<VectorType,
3267 LinearAlgebra::distributed::Vector<double>>)
3268 tmp.reinit(locally_owned_dofs,
3270 this->mpi_communicator);
3271
3272 return tmp;
3273}
3274
3275template <int dim, typename VectorType, typename DofsType>
3276void
3278{
3279 if (!this->simulation_parameters.mortar_parameters.enable)
3280 return;
3281
3282 if (dynamic_cast<parallel::distributed::Triangulation<dim> *>(
3283 this->triangulation.get()) == nullptr)
3284 return;
3285
3286 auto &tria = *dynamic_cast<parallel::distributed::Triangulation<dim> *>(
3287 this->triangulation.get());
3288
3289 tria.signals.weight.connect(
3290 [this](const typename parallel::distributed::Triangulation<
3291 dim>::cell_iterator &cell,
3292 const CellStatus /*status*/) -> unsigned int {
3293 // Default cell weight used for all cells
3294 const unsigned int base_weight = 1000;
3295
3296 // Check if this cell touches a mortar boundary
3297 const unsigned int rotor_bid =
3298 simulation_parameters.mortar_parameters.rotor_boundary_id;
3299 const unsigned int stator_bid =
3300 simulation_parameters.mortar_parameters.stator_boundary_id;
3301
3302 if (cell->is_locally_owned())
3303 {
3304 for (const auto face_no : cell->face_indices())
3305 {
3306 const auto face = cell->face(face_no);
3307 if (face->at_boundary() && (face->boundary_id() == rotor_bid ||
3308 face->boundary_id() == stator_bid))
3309 {
3310 // Return higher weight for mortar cells
3311 return simulation_parameters.mortar_parameters.cell_weight;
3312 }
3313 }
3314 }
3315
3316 return base_weight;
3317 });
3318}
3319
3320// Pre-compile the 2D and 3D version with the types that can occur
3323template class NavierStokesBase<2,
3325 std::vector<IndexSet>>;
3326template class NavierStokesBase<3,
3328 std::vector<IndexSet>>;
3329
3330#ifndef LETHE_USE_LDV
3331template class NavierStokesBase<2,
3332 LinearAlgebra::distributed::Vector<double>,
3333 IndexSet>;
3334template class NavierStokesBase<3,
3335 LinearAlgebra::distributed::Vector<double>,
3336 IndexSet>;
3337#endif
unsigned int maximum_number_of_previous_solutions()
Get the maximum number of previous time steps supposed by the BDF schemes implemented in Lethe.
Definition bdf.h:84
Calculates the time-averaged velocities and pressure and the independent components of the Reynolds...
BoundaryPostprocessor Post-processor class used to attach the boundary id to the faces when outputtin...
Definition manifolds.h:99
Compute the density scalar field of a material within a domain.
Compute the dynamic viscosity scalar field of a fluid within a domain.
This class implements a boundary conditions for the fluid dynamics solver where the velocity componen...
Compute the kinematic viscosity scalar field of a fluid within a domain.
std::vector< StasisConstraintWithTemperature > stasis_constraint_structs
virtual void output_newton_update_norms() override
Output the L2 and Linfty norms of the correction vector.
void constrain_stasis_with_temperature(const DoFHandler< dim > *dof_handler_ht)
Constrain a fluid's subdomains according to the temperature field to null velocity and pressure field...
void box_refine_mesh(const bool restart)
Allow the initial refinement of all cells of the principal mesh that are partially contained in one o...
ConvergenceTable error_table
PVDHandler pvdhandler_boundary
virtual void set_specified_fluid_dynamics_solution()
Specify the fluid dynamics solution instead of solving for it.
std::shared_ptr< MortarManagerBase< dim > > mortar_manager
std::shared_ptr< FESystem< dim > > fe
void connect_mortar_weight_signals()
Connect weight signals for load balancing of mortar cells. Cells touching mortar boundaries can be as...
void get_cell_temperature_values(const typename DoFHandler< dim >::active_cell_iterator &cell, const DoFHandler< dim > *dof_handler_ht, const GlobalVectorType &temperature_solution, std::vector< double > &local_temperature_values)
Get cell's local temperature values at quadrature points.
AffineConstraints< double > nonzero_constraints
void write_output_results(const VectorType &solution)
Generate post-processing parallel VTU files from vector of solution_output_struct.
const unsigned int pressure_fem_degree
virtual void update_multi_stage_solution(double time_step)
std::vector< std::map< types::boundary_id, Tensor< 1, dim > > > forces_on_boundaries
virtual std::vector< OutputStructTableHandler > gather_tables()
Returns a vector of references to TableHandler objects that needs to be serialized/ deserialized for ...
void define_zero_constraints()
Define the zero constraints used to solve the problem.
SDIRKVectors sdirk_vectors
Instance of SDIRK-related vectors.
const unsigned int n_mpi_processes
unsigned int number_quadrature_points
virtual void update_solutions_for_multiphysics()
Required only for the matrix-free solver to update solutions within iterate function.
void constrain_stasis_with_temperature_cls(const DoFHandler< dim > *dof_handler_cls, const DoFHandler< dim > *dof_handler_ht)
Constrain fluids' subdomains according to the temperature field to null velocity and pressure fields ...
void rescale_pressure_dofs_in_newton_update()
This function is used to rescale pressure DOFs in the newton correction.
void write_output_torques()
Writes the torques per boundary condition to a text file output.
std::shared_ptr< Quadrature< dim - 1 > > face_quadrature
void write_output_forces()
Writes the forces per boundary condition to a text file output.
void transfer_solution(SolutionTransfer< dim, VectorType > &solution_transfer, std::vector< SolutionTransfer< dim, VectorType > > &previous_solutions_transfer)
Transfer solution after mesh refinement.
virtual AffineConstraints< double > & get_nonzero_constraints() override
void make_solid_domain(const DoFHandler< dim > &dof_handler, const bool non_zero_constraints, AffineConstraints< double > &constraints)
Turn regions of the mesh where the material_id>0 into a solid block by injecting velocity and pressur...
virtual void refine_mesh()
Allow the refinement of the mesh according to one of the 2 methods proposed.
virtual void read_checkpoint()
Restart a previous simulation from a checkpoint file.
void define_non_zero_constraints()
Define the non-zero constraints used to solve the problem.
virtual void enable_dynamic_zero_constraints_fd()
Enable the use of dynamic zero constraints by initializing required FEValues objects.
void postprocessing_torques(const VectorType &evaluation_point)
Post-processing function Outputs the torque acting on each boundary condition.
virtual void finish_time_step()
Finishes the time step of the fluid dynamics. Post-processing and time stepping.
TableHandler apparent_viscosity_table
void rotate_rotor_mapping(const bool is_first)
Rotate rotor mapping in mortar method.
void gather_output_results(const VectorType &solution, std::vector< OutputStruct< dim, VectorType > > &solution_output_structs)
Gather solution information to generate output results.
AffineConstraints< double > dynamic_zero_constraints
virtual void dynamic_flow_control()
If set to enable, dynamic_flow_control allows to control the flow by executing space-average velocity...
std::shared_ptr< NavierStokesCouplingEvaluationSIPG< dim, double > > mortar_coupling_evaluator
virtual void update_multiphysics_time_average_solution()=0
Update the time average velocity field solution.
const unsigned int this_mpi_process
SimulationParameters< dim > simulation_parameters
TableHandler kinetic_energy_table
bool check_existance_of_bc(BoundaryConditions::FluidDynamicsBoundaryType bc)
Check if a specifique boundary condition exist.
bool average_velocities_are_enabled() const
Indicate whether the time-averaged velocity object holds vectors that must be preserved....
virtual void multi_stage_postresolution(unsigned int stage, Parameters::SimulationControl::TimeSteppingMethod method, double time_step)
Calculate from Update .
virtual void iterate()
Do a regular CFD iteration.
void finish_simulation_fd()
Finishes the simulation for fluid dynamics by calling the post-processing elements that are required.
void restore_average_velocities_after_checkpoint()
Restore the state of the time-averaged velocity object after its vectors have been deserialized.
std::shared_ptr< CouplingOperator< dim, double > > mortar_coupling_operator
TableHandler flow_rate_table
virtual void postprocess_fd(bool first_iteration)
Post-process fluid dynamics after an iteration.
TimerOutput computing_timer
std::shared_ptr< std::vector< GlobalBlockVectorType > > previous_solutions
TableHandler pressure_power_table
std::shared_ptr< MappingQCache< dim > > mapping_cache
VectorType init_temporary_vector()
This function initializes correctly a temporary vector depending on the vector type.
std::shared_ptr< SimulationControl > simulation_control
void update_boundary_conditions()
Update non-zero constraints if the boundary is time dependent. Note: not implemented for the block fl...
VectorType local_evaluation_point
virtual std::vector< OutputStruct< dim, VectorType > > gather_output_hook()
Gather and return vector of output structs that are particular to some applications.
virtual void multi_stage_preresolution(unsigned int stage, Parameters::SimulationControl::TimeSteppingMethod method)
Update the sum_over_previous_stages variable to use it in the solver.
FlowControl< dim > flow_control
TableHandler pressure_drop_table
std::shared_ptr< MultiphysicsInterface< dim > > multiphysics
virtual void set_solution_from_checkpoint(const std::string &checkpoint_file_prefix)
Read the solution from a checkpoint file and set it as the current solution.
NavierStokesBase(SimulationParameters< dim > &nsparam, const FluidDynamicsFormulation fluid_dynamics_formulation=FluidDynamicsFormulation::standard)
Construct the Navier-Stokes base and the multiphysics interface.
bool cell_in_constraining_domain(const typename DoFHandler< dim >::active_cell_iterator &cell, const Point< dim > &plane_point, const Tensor< 1, dim > &plane_normal_vector)
Checks and identifies if the cell is located in the constraining domain defined by a plane with its o...
AffineConstraints< double > zero_constraints
MeshController mesh_controller
virtual void percolate_time_vectors_fd()
finish_time_step Finishes the time step of the fluid dynamics Post-processing and time stepping
virtual void update_forcing_and_boundary_conditions()
Set the time of the forcing function and of the time-dependent boundary conditions to the current tim...
std::shared_ptr< AverageVelocities< dim, GlobalBlockVectorType, std::vector< IndexSet > > > average_velocities
virtual void setup_dofs()
Initialize the degree of freedom and the memory associated with them for fluid dynamics and enabled a...
TableHandler enstrophy_table
std::shared_ptr< Mapping< dim > > mapping
void identify_cell_and_constrain_velocity(const std::vector< types::global_dof_index > &local_dof_indices, const std::vector< double > &local_temperature_values, StasisConstraintWithTemperature &stasis_constraint_struct)
Identify if temperature DOFs of the cell are within the constraining range. If they are,...
TableHandler viscous_dissipation_table
void postprocessing_forces(const VectorType &evaluation_point)
Post-processing function Outputs the forces acting on each boundary condition.
void set_nodal_values()
Set the nodal values of velocity and pressure.
void get_cell_filtered_phase_indicator_values(const typename DoFHandler< dim >::active_cell_iterator &cell, const DoFHandler< dim > *dof_handler_cls, const GlobalVectorType &filtered_phase_indicator_solution, std::vector< double > &local_filtered_phase_indicator_values)
Get cell's local filtered phase indicator values at quadrature points.
virtual void write_checkpoint()
Write the checkpoint.
std::shared_ptr< DoFHandler< dim > > dof_handler
std::shared_ptr< parallel::DistributedTriangulationBase< dim > > triangulation
void reinit_mortar_operators()
Initialize mortar coupling manager, operator, and evaluator.
const unsigned int velocity_fem_degree
Postprocessing - Defines the parameters for the post-processing. In Lethe, post-processing implies th...
bool calculate_pressure_drop
Enable pressure drop post-processing.
std::string kinetic_energy_output_name
Prefix for kinetic energy output.
bool calculate_pressure_power
Enable pressure power post-processing.
std::string pressure_power_output_name
Prefix for the pressure power output.
bool calculate_kinetic_energy
Enable total kinetic energy post-processing.
bool calculate_flow_rate
Enable flow rate post-processing.
bool calculate_enstrophy
Enable total enstrophy post-processing.
std::string apparent_viscosity_output_name
Prefix for the apparent viscosity output.
bool calculate_viscous_dissipation
Enable viscous dissipation post-processing.
std::string enstrophy_output_name
Prefix for the enstrophy output.
std::string viscous_dissipation_output_name
Prefix for the viscous dissipation output.
std::string pressure_drop_output_name
Prefix for pressure drop output.
bool calculate_apparent_viscosity
Enable calculating apparent viscosity.
std::string flow_rate_output_name
Prefix for flow rate output.
PhysicsSolver(const Parameters::NonLinearSolver non_linear_solver_parameters)
Constructor for the non-linear physics.
ConditionalOStream pcout
void solve_governing_system()
Solve the global system of equations according to a given strategy, either linear or not.
PhysicsSolverStrategy< VectorType > * physics_solving_strategy
Compute the velocity field in the Eulerian frame of reference for simulations carried out in a single...
static unsigned int get_number_of_stages(const Parameters::SimulationControl::TimeSteppingMethod &method)
Return the number of stages associated with the time-stepping method. For SDIRK methods,...
void check_file_exists(const std::string &file_name, const std::string &file_description)
Verify that a file that Lethe must read exists and can be opened, and raise an error stated in the us...
Definition utilities.cc:225
TableHandler make_table_scalars_tensors(const std::vector< T > &independent_values, const std::string &independent_column_name, const std::vector< Tensor< 1, dim > > &dependent_vector, const std::vector< std::string > &dependent_column_names, const unsigned int display_precision, const bool display_scientific_notation=false)
Generate a table from a vector of scalars (independent variable) and a vector of Tensor<1,...
Definition utilities.cc:59
std::vector< val > extract_values_from_map(const std::map< key, val > &map)
Extract the values from map object.
Definition utilities.h:1138
void create_output_folder(const std::string &dirname)
Creates the simulation output folder.
Definition utilities.cc:401
void serialize_tables_vector(const std::vector< OutputStructTableHandler > &table_output_structs, const MPI_Comm mpi_communicator)
Serializes all the TableHandler objects in table_output_structs.
Definition utilities.h:602
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
std::vector< key > extract_keys_from_map(const std::map< key, val > &map)
Extract the keys from map object.
Definition utilities.h:1118
void deserialize_tables_vector(std::vector< OutputStructTableHandler > &table_output_structs, const MPI_Comm mpi_communicator)
Deserializes all the TableHandler objects in table_output_structs.
Definition utilities.h:624
void build_refinement_box_triangulation(const Parameters::Mesh< dim, spacedim > &box_mesh_parameters, Triangulation< dim, spacedim > &box_triangulation)
Build the triangulation which delimits a mesh refinement box.
Definition grids.cc:712
void mortar_workload_imbalance(const Triangulation< dim > &triangulation, const Parameters::Mortar< dim > &mortar_parameters, const ConditionalOStream &pcout)
Compute workload imbalance of mortar cells.
@ velocity
Velocity vector field from fluid dynamics.
@ pressure
Pressure scalar field from fluid dynamics.
@ temperature
Temperature scalar field from heat transfer.
FluidDynamicsFormulation
Formulation of the fluid dynamics equations solved by a Navier-Stokes solver.
PhysicsID
Unique identifiers for each physics module in Lethe.
@ fluid_dynamics
Fluid Dynamics (Velocity,Pressure).
@ CLS
@ heat_transfer
Heat transfer (Temperature).
void rotate_mapping(const DoFHandler< dim > &dof_handler, MappingQCache< dim > &mapping_cache, const Mapping< dim > &mapping, const double rotation_angle, const Point< dim > &center_of_rotation=Point< dim >(), const unsigned int rotation_axis_direction=2)
Rotate the mapping according to the rotation angle in a rotor-stator configuration.
void flag_cells_in_refinement_box(const DoFHandler< dim > &dof_handler, const Triangulation< dim > &box_triangulation)
Flag for refinement of all the cells of a triangulation which lie within a refinement box.
std::variant< OutputStructPostprocessor< dim, VectorType >, OutputStructSolution< dim, VectorType >, OutputStructCellVector > OutputStruct
Variant handler of the three output structs (OutputStructPostprocessor, OutputStructSolution and Outp...
double calculate_pressure_power(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping)
Calculate the average power done by pressure in the simulation domain. The average power is defined a...
std::pair< double, double > calculate_pressure_drop(const DoFHandler< dim > &dof_handler, const Mapping< dim > &mapping, const VectorType &evaluation_point, const Quadrature< dim > &cell_quadrature_formula, const Quadrature< dim - 1 > &face_quadrature_formula, const unsigned int inlet_boundary_id, const unsigned int outlet_boundary_id)
Calculate the pressure drop and total pressure drop between two boundaries. The pressure drop thus ca...
double calculate_apparent_viscosity(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping, const PhysicalPropertiesManager &properties_manager)
Calculates the apparent viscosity of the fluid for non Newtonian flows.
std::map< types::boundary_id, Tensor< 1, 3 > > calculate_torques(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const PhysicalPropertiesManager &properties_manager, const BoundaryConditions::FluidDynamicsBoundaryConditions< dim > &boundary_conditions, const Quadrature< dim - 1 > &face_quadrature_formula, const Mapping< dim > &mapping)
Calculates the torques due to the fluid motion on every boundary conditions.
std::pair< double, double > calculate_flow_rate(const DoFHandler< dim > &dof_handler, const VectorType &present_solution, const unsigned int &boundary_id, const Quadrature< dim - 1 > &face_quadrature_formula, const Mapping< dim > &mapping)
calculate_flow_rate. This function calculates the volumetric flow rate at the selected boundary....
double calculate_kinetic_energy(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping)
Calculate the average kinetic energy in the simulation domain.
double calculate_CFL(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const double time_step, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping)
Calculate the CFL condition on the simulation domain.
double calculate_average_velocity(const DoFHandler< dim > &dof_handler, const VectorType &present_solution, const unsigned int &boundary_id, const Quadrature< dim - 1 > &face_quadrature_formula, const Mapping< dim > &mapping)
calculate_average_velocity. This function calculates average velocity from the volumetric flow rate a...
std::pair< double, double > calculate_L2_error(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const Function< dim > *exact_solution, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping)
Calculates the L2 norm of the error on velocity and pressure.
std::vector< std::map< types::boundary_id, Tensor< 1, dim > > > calculate_forces(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const PhysicalPropertiesManager &properties_manager, const BoundaryConditions::FluidDynamicsBoundaryConditions< dim > &boundary_conditions, const Quadrature< dim - 1 > &face_quadrature_formula, const Mapping< dim > &mapping)
Calculates the force due to the fluid motion on every boundary conditions.
double calculate_enstrophy(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping)
Calculate the average enstrophy in the simulation domain.
double calculate_viscous_dissipation(const DoFHandler< dim > &dof_handler, const VectorType &evaluation_point, const Quadrature< dim > &quadrature_formula, const Mapping< dim > &mapping, const PhysicalPropertiesManager &properties_manager)
Calculate the viscous dissipation of kinetic energy which is defined as ∫∇u.τdΩ/∫1dΩ
SDIRKTable sdirk_table(const Parameters::SimulationControl::TimeSteppingMethod method)
Construct the Butcher tableau (A, b, c) for a given SDIRK method.
void establish_solid_domain(const dealii::DoFHandler< dim > &dof_handler, const DofsType &locally_owned_dofs, const bool non_zero_constraints, dealii::AffineConstraints< double > &constraints)
Turn regions of the mesh where the material_id>0 into a solid block by injecting velocity and pressur...
void constrain_solid_cell_velocity_dofs(const dealii::FiniteElement< dim > &fe, const std::vector< dealii::types::global_dof_index > &local_dof_indices, const bool &non_zero_constraints, dealii::AffineConstraints< double > &constraints)
Constrain velocity DOFs of a solid cell.
void write_boundaries_vtu_and_pvd(PVDHandler &pvd_handler_boundary, const DataOutFaces< dim > &data_out_faces, const std::string &folder, const double time, const unsigned int iter, const unsigned int group_files, const MPI_Comm &mpi_communicator, const std::string &file_prefix=std::string("boundaries"), const unsigned int digits=5)
Output the Data Out Faces as vtu files, with a pvtu file and a pvd to store the timing This function ...
void write_vtu_and_pvd(PVDHandler &pvd_handler, const DataOutInterface< dim, spacedim > &data_out, const std::string &folder, const std::string &file_prefix, const double time, const unsigned int iter, const unsigned int group_files, const MPI_Comm &mpi_communicator, const unsigned int digits=5)
Output the data out to "group_files" vtu files, with a pvtu file and a pvd to store the timing This f...
Struct containing information about the solution output. It is used to pass the information required ...
Struct containing information about the solution output. It is used to pass all the information requi...
ErrorEstimator
Error estimator for the variable.
Structure representing a Butcher tableau for a SDIRK method.
std::vector< double > c
Struct containing fluid id, temperature and phase indicator range information, and flag containers fo...
const double min_solid_temperature
Lower threshold values of the constraining field (temperature).
const double max_solid_temperature
Upper threshold values of the constraining field (temperature).
GlobalVectorType VectorType
dealii::TrilinosWrappers::MPI::BlockVector GlobalBlockVectorType
Definition vector.h:18
void write_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_objects, const ConditionalOStream &pcout, MPI_Comm &mpi_communicator, const CheckpointControl &checkpoint_controller)
Write_checkpoint Write a DEM simulation checkpointing to allow for DEM simulation restart.