Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
fluid_dynamics_nitsche.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2020-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/bdf.h>
5#include <core/grids.h>
6#include <core/manifolds.h>
9#include <core/utilities.h>
10
12
13#include <deal.II/base/multithread_info.h>
14
15#include <deal.II/numerics/fe_field_function.h>
16
17#include <deal.II/particles/data_out.h>
18
19#include <boost/archive/text_iarchive.hpp>
20#include <boost/archive/text_oarchive.hpp>
21
22// Constructor for class FluidDynamicsNitsche
23template <int dim, int spacedim>
26 : FluidDynamicsMatrixBased<spacedim>(p_nsparam)
27{
28 const unsigned int n_solids =
29 this->simulation_parameters.nitsche->number_solids;
30
31 // Initialize shared pointer with an empty vector
32 solids =
33 std::make_shared<std::vector<std::shared_ptr<SolidBase<dim, spacedim>>>>();
34
35 // Reserve memory
36 solids->reserve(n_solids);
37
38 // Fill with solids
39 for (unsigned int i_solid = 0; i_solid < n_solids; ++i_solid)
40 {
41 solids->emplace_back(std::make_shared<SolidBase<dim, spacedim>>(
42 this->simulation_parameters.nitsche->nitsche_solids[i_solid],
43 this->triangulation,
44 this->mapping));
45 }
46
47 pvdhandler_solid_particles.resize(n_solids);
48
49 pvdhandler_solid_triangulation.resize(n_solids);
50
51 solid_forces_table.resize(n_solids);
52 solid_torques_table.resize(n_solids);
53}
54
55template <int dim, int spacedim>
56template <bool assemble_matrix>
57void
59{
60 TimerOutput::Scope t(this->computing_timer, "Nitsche assemble restriction");
61
62 Assert(
63 !this->simulation_parameters.physical_properties_manager.is_non_newtonian(),
64 RequiresConstantViscosity("assemble_nitsche_restriction"));
65
66 // Viscosity for stabilization constant
67 const double kinematic_viscosity =
68 this->simulation_parameters.physical_properties_manager
69 .get_kinematic_viscosity_scale();
70
71 // Time steps and inverse time steps which is used for stabilization constant
72 std::vector<double> time_steps_vector =
73 this->simulation_control->get_time_steps_vector();
74 const double dt = time_steps_vector[0];
75 const double sdt = 1. / dt;
76
77 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
78 {
79 std::shared_ptr<Particles::ParticleHandler<spacedim>> &solid_ph =
80 (*solids)[i_solid]->get_solid_particle_handler();
81
82
83 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
84
85 std::vector<types::global_dof_index> fluid_dof_indices(dofs_per_cell);
86
87 FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
88 dealii::Vector<double> local_rhs(dofs_per_cell);
89
90 Tensor<1, spacedim> velocity;
91 Function<spacedim> *solid_velocity =
92 (*solids)[i_solid]->get_solid_velocity();
93
94 solid_velocity->set_time(this->simulation_control->get_current_time());
95
96 // Penalization terms
97 const double beta =
98 this->simulation_parameters.nitsche->nitsche_solids[i_solid]->beta;
99
100 // Loop over all local particles
101 auto particle = solid_ph->begin();
102 while (particle != solid_ph->end())
103 {
104 local_matrix = 0;
105 local_rhs = 0;
106
107 const auto &cell = particle->get_surrounding_cell();
108 double h_cell = compute_cell_diameter<dim>(cell->measure(),
109 this->velocity_fem_degree);
110 const double penalty_parameter =
111 1. / std::pow(h_cell * h_cell, double(dim) / double(spacedim));
112 const auto &dh_cell =
113 typename DoFHandler<spacedim>::cell_iterator(*cell,
114 &(*this->dof_handler));
115 dh_cell->get_dof_indices(fluid_dof_indices);
116
117 const auto pic = solid_ph->particles_in_cell(cell);
118 Assert(pic.begin() == particle, ExcInternalError());
119 for (const auto &p : pic)
120 {
121 const ArrayView<const double> properties = p.get_properties();
122
123 velocity = 0;
124 const auto &ref_q = p.get_reference_location();
125 const auto &real_q = p.get_location();
126 const auto &JxW = properties[0];
127
128 for (unsigned int k = 0; k < dofs_per_cell; ++k)
129 {
130 const auto comp_k =
131 this->fe->system_to_component_index(k).first;
132 if (comp_k < spacedim)
133 {
134 // Get the velocity at non-quadrature point (particle in
135 // fluid)
137 velocity[comp_k] +=
138 evaluation_point[fluid_dof_indices[k]] *
139 this->fe->shape_value(k, ref_q);
140 }
141 }
142
143 const double u_mag = velocity.norm();
144
145
146 const double tau =
147 this->simulation_control->get_assembly_method() ==
149 1. / std::sqrt(std::pow(2. * u_mag / h_cell, 2) +
150 9 * std::pow(4 * kinematic_viscosity /
151 (h_cell * h_cell),
152 2)) :
153 1. /
154 std::sqrt(
155 std::pow(sdt, 2) + std::pow(2. * u_mag / h_cell, 2) +
156 9 * std::pow(4 * kinematic_viscosity / (h_cell * h_cell),
157 2));
158
159 for (unsigned int i = 0; i < dofs_per_cell; ++i)
160 {
161 const auto comp_i =
162 this->fe->system_to_component_index(i).first;
163 if (comp_i < spacedim)
164 {
165 if (assemble_matrix)
166 {
167 for (unsigned int j = 0; j < dofs_per_cell; ++j)
168 {
169 const auto comp_j =
170 this->fe->system_to_component_index(j).first;
171 if (comp_i == comp_j)
172 {
173 local_matrix(i, j) +=
174 penalty_parameter * beta *
175 this->fe->shape_value(i, ref_q) *
176 this->fe->shape_value(j, ref_q) * JxW;
177
178 // Partial jacobian of the strong residual
179 // term so that Nitsche's IB is taken into
180 // account in the stabilization
181 local_matrix(i, j) +=
182 penalty_parameter * beta *
183 this->fe->shape_value(j, ref_q) *
184 (this->fe->shape_grad(i, ref_q) *
185 velocity) *
186 tau * JxW;
187 }
188 }
189 }
190 // Regular residual
191 local_rhs(i) += -penalty_parameter * beta *
192 velocity[comp_i] *
193 this->fe->shape_value(i, ref_q) * JxW +
194 penalty_parameter * beta *
195 solid_velocity->value(real_q, comp_i) *
196 this->fe->shape_value(i, ref_q) * JxW;
197
198 // Residual due taking into account Nitsche IB in the
199 // strong residual used for GLS stabilization
200 const double strong_residual =
201 -penalty_parameter * beta *
202 (solid_velocity->value(real_q, comp_i) -
203 velocity[comp_i]);
204
205 const double test_function =
206 this->fe->shape_grad(i, ref_q) * velocity;
207
208 local_rhs(i) +=
209 -tau * (strong_residual)*test_function * JxW;
210 }
211 }
212 }
213 const AffineConstraints<double> &constraints_used =
214 this->zero_constraints;
215 auto &system_rhs = this->system_rhs;
216 constraints_used.distribute_local_to_global(local_matrix,
217 local_rhs,
218 fluid_dof_indices,
219 this->system_matrix,
220 system_rhs);
221 particle = pic.end();
222 }
223 this->system_matrix.compress(VectorOperation::add);
224 this->system_rhs.compress(VectorOperation::add);
225 }
226}
227
228template <>
229Tensor<1, 3>
231 const unsigned int i_solid)
232{
233 std::shared_ptr<Particles::ParticleHandler<3>> &solid_ph =
234 (*solids)[i_solid]->get_solid_particle_handler();
235
236 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
237
238 std::vector<types::global_dof_index> fluid_dof_indices(dofs_per_cell);
239
240 Tensor<2, 3> velocity_gradient;
241 double pressure;
242 Tensor<1, 3> normal_vector;
243 Tensor<2, 3> fluid_stress;
244 Tensor<2, 3> fluid_pressure;
245 Tensor<1, 3> force; // to be changed for a vector of tensors when
246 // allowing multiple solids
247
248
249 Assert(
250 !this->simulation_parameters.physical_properties_manager.is_non_newtonian(),
251 RequiresConstantViscosity("assemble_nitsche_restriction"));
252
253 const double kinematic_viscosity =
254 this->simulation_parameters.physical_properties_manager
255 .get_kinematic_viscosity_scale();
256
257 // Loop over all local particles
258 auto particle = solid_ph->begin();
259 while (particle != solid_ph->end())
260 {
261 const auto &cell = particle->get_surrounding_cell();
262 const auto &dh_cell =
263 typename DoFHandler<3>::cell_iterator(*cell, &(*this->dof_handler));
264 dh_cell->get_dof_indices(fluid_dof_indices);
265
266 const auto pic = solid_ph->particles_in_cell(cell);
267 Assert(pic.begin() == particle, ExcInternalError());
268
269 // Generate FEField function to evaluate values and gradients
270 // at the particle location
272
273 Functions::FEFieldFunction<3, GlobalVectorType> fe_field(
275
276
277 fe_field.set_active_cell(dh_cell);
278
279 for (const auto &p : pic)
280 {
281 const ArrayView<const double> properties = p.get_properties();
282
283 velocity_gradient = 0;
284 pressure = 0;
285 const auto &q = p.get_location();
286 const auto &JxW = properties[0];
287 normal_vector[0] = -properties[1];
288 normal_vector[1] = -properties[2];
289 normal_vector[2] = -properties[3];
290
291 for (int k = 0; k < 3; ++k)
292 {
293 velocity_gradient[k] = fe_field.gradient(q, k);
294 }
295
296 pressure = fe_field.value(q, 3);
297
298 for (int d = 0; d < 2; ++d)
299 {
300 fluid_pressure[d][d] = pressure;
301 }
302
303 fluid_stress = kinematic_viscosity *
304 (velocity_gradient + transpose(velocity_gradient)) -
305 fluid_pressure;
306 force += fluid_stress * normal_vector * JxW;
307 }
308
309 particle = pic.end();
310 }
311 force = Utilities::MPI::sum(force, this->mpi_communicator);
312 return force;
313}
314
315template <int dim, int spacedim>
316Tensor<1, spacedim>
318 const unsigned int i_solid)
319{
320 std::shared_ptr<Particles::ParticleHandler<dim, spacedim>> &solid_ph =
321 (*solids)[i_solid]->get_solid_particle_handler();
322
323 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
324
325 std::vector<types::global_dof_index> fluid_dof_indices(dofs_per_cell);
326
327 // Penalization terms
328 const double beta =
329 this->simulation_parameters.nitsche->nitsche_solids[i_solid]->beta;
330 Tensor<1, spacedim> velocity;
331 Function<spacedim> *solid_velocity = (*solids)[i_solid]->get_solid_velocity();
332 Tensor<1, spacedim> force;
333 for (int i = 0; i < spacedim; ++i)
334 force[i] = 0;
335
336 // Loop over all local particles
337 auto particle = solid_ph->begin();
338 while (particle != solid_ph->end())
339 {
340 const auto &cell = particle->get_surrounding_cell();
341 double h_cell =
342 compute_cell_diameter<dim>(cell->measure(), this->velocity_fem_degree);
343 const double penalty_parameter =
344 1. / std::pow(h_cell * h_cell, double(dim) / double(spacedim));
345 const auto &dh_cell =
346 typename DoFHandler<spacedim>::cell_iterator(*cell,
347 &(*this->dof_handler));
348 dh_cell->get_dof_indices(fluid_dof_indices);
349
350 const auto pic = solid_ph->particles_in_cell(cell);
351 Assert(pic.begin() == particle, ExcInternalError());
352 for (const auto &p : pic)
353 {
354 const ArrayView<const double> properties = p.get_properties();
355
356 velocity = 0;
357 const auto &ref_q = p.get_reference_location();
358 const auto &real_q = p.get_location();
359 const auto &JxW = properties[0];
360
361 for (unsigned int k = 0; k < dofs_per_cell; ++k)
362 {
363 const auto comp_k = this->fe->system_to_component_index(k).first;
364 if (comp_k < spacedim)
365 {
366 // Get the velocity at non-quadrature point (particle in
367 // fluid)
369 velocity[comp_k] += evaluation_point[fluid_dof_indices[k]] *
370 this->fe->shape_value(k, ref_q);
371 }
372 }
373 for (unsigned int i = 0; i < dofs_per_cell; ++i)
374 {
375 const auto comp_i = this->fe->system_to_component_index(i).first;
376 if (comp_i < spacedim)
377 {
378 force[comp_i] +=
379 penalty_parameter * beta * this->fe->shape_value(i, ref_q) *
380 JxW *
381 (solid_velocity->value(real_q, comp_i) - velocity[comp_i]);
382 }
383 }
384 }
385 particle = pic.end();
386 }
387 force = Utilities::MPI::sum(force, this->mpi_communicator);
388 return force;
389}
390
391template <int dim, int spacedim>
392Tensor<1, 3>
394 const unsigned int i_solid)
395{
396 std::shared_ptr<Particles::ParticleHandler<spacedim>> &solid_ph =
397 (*solids)[i_solid]->get_solid_particle_handler();
398
399 const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
400
401 std::vector<types::global_dof_index> fluid_dof_indices(dofs_per_cell);
402
403 // Penalization terms
404 const double beta =
405 this->simulation_parameters.nitsche->nitsche_solids[i_solid]->beta;
406 Tensor<1, spacedim> velocity;
407 Function<spacedim> *solid_velocity = (*solids)[i_solid]->get_solid_velocity();
408
409
410 Tensor<1, 3> torque;
411 torque = 0;
412
413 // Todo center of rotation should be parameter passed.
414 Point<spacedim> center_of_rotation =
415 this->simulation_parameters.nitsche->nitsche_solids[i_solid]
416 ->center_of_rotation;
417
418 // Loop over all local particles
419 auto particle = solid_ph->begin();
420 while (particle != solid_ph->end())
421 {
422 const auto &cell = particle->get_surrounding_cell();
423 double h_cell =
424 compute_cell_diameter<dim>(cell->measure(), this->velocity_fem_degree);
425 const double penalty_parameter =
426 1. / std::pow(h_cell * h_cell, double(dim) / double(spacedim));
427 const auto &dh_cell =
428 typename DoFHandler<spacedim>::cell_iterator(*cell,
429 &(*this->dof_handler));
430 dh_cell->get_dof_indices(fluid_dof_indices);
431
432 const auto pic = solid_ph->particles_in_cell(cell);
433 Assert(pic.begin() == particle, ExcInternalError());
434 for (const auto &p : pic)
435 {
436 const ArrayView<const double> properties = p.get_properties();
437
438 Tensor<1, spacedim> force;
439 force = 0;
440 velocity = 0;
441 const auto &ref_q = p.get_reference_location();
442 const auto &real_q = p.get_location();
443 const auto &JxW = properties[0];
444
445 for (unsigned int k = 0; k < dofs_per_cell; ++k)
446 {
447 const auto comp_k = this->fe->system_to_component_index(k).first;
448 if (comp_k < spacedim)
449 {
450 // Get the velocity at non-quadrature point (particle in
451 // fluid)
453 velocity[comp_k] += evaluation_point[fluid_dof_indices[k]] *
454 this->fe->shape_value(k, ref_q);
455 }
456 }
457 for (unsigned int i = 0; i < dofs_per_cell; ++i)
458 {
459 const auto comp_i = this->fe->system_to_component_index(i).first;
460 if (comp_i < spacedim)
461 {
462 force[comp_i] -=
463 penalty_parameter * beta * this->fe->shape_value(i, ref_q) *
464 JxW *
465 (solid_velocity->value(real_q, comp_i) - velocity[comp_i]);
466 }
467 }
468 // Calculate torque on particle location
469 auto distance = real_q - center_of_rotation;
470
471 if (dim == 2)
472 {
473 torque[0] = 0.;
474 torque[1] = 0.;
475 torque[2] += distance[0] * force[1] - distance[1] * force[0];
476 }
477 else if (dim == 3)
478 {
479 torque[0] += distance[1] * force[2] - distance[2] * force[1];
480 torque[1] += distance[2] * force[0] - distance[0] * force[2];
481 torque[2] += distance[0] * force[1] - distance[1] * force[0];
482 }
483 }
484 particle = pic.end();
485 }
486 torque = Utilities::MPI::sum(torque, this->mpi_communicator);
487 return torque;
488}
489
490template <int dim, int spacedim>
491void
493 const unsigned int i_solid,
494 bool first_solid_forces)
495{
496 TimerOutput::Scope t(this->computing_timer, "Calculate force on solid");
497
498 std::vector<Tensor<1, spacedim>> force;
499 std::vector<unsigned int> solid_indices;
500
501 force.emplace_back(this->calculate_forces_on_solid(i_solid));
502 solid_indices.emplace_back(i_solid);
503
504 if (this->simulation_parameters.nitsche->verbosity ==
506 this->this_mpi_process == 0)
507 {
508 std::string independent_column_names = "Solid ID";
509 std::vector<std::string> dependent_column_names;
510 dependent_column_names.emplace_back("f_x");
511 dependent_column_names.emplace_back("f_y");
512 if (spacedim == 3)
513 dependent_column_names.emplace_back("f_z");
514
515 TableHandler table = make_table_scalars_tensors(
516 solid_indices,
517 independent_column_names,
518 force,
519 dependent_column_names,
520 this->simulation_parameters.simulation_control.log_precision,
521 true);
522
523 // printed only for the first i_solid for which calculate_forces is true
524 // to be improved so that the column_names are not repeated
525 if (first_solid_forces)
526 {
527 std::cout << std::endl;
528 std::cout << "+------------------------------------------+"
529 << std::endl;
530 std::cout << "| Force on solid summary |"
531 << std::endl;
532 std::cout << "+------------------------------------------+"
533 << std::endl;
534 }
535 table.write_text(std::cout);
536 }
537
538 if (this->simulation_control->is_steady())
539 {
540 solid_forces_table[i_solid].add_value(
541 "cells", this->triangulation->n_global_active_cells());
542 }
543 else
544 {
545 solid_forces_table[i_solid].add_value(
546 "time", this->simulation_control->get_current_time());
547 solid_forces_table[i_solid].set_precision(
548 "time", this->simulation_parameters.forces_parameters.output_precision);
549 }
550 solid_forces_table[i_solid].add_value("f_x", force[0][0]);
551 solid_forces_table[i_solid].add_value("f_y", force[0][1]);
552 if (dim == 3)
553 solid_forces_table[i_solid].add_value("f_z", force[0][2]);
554 else
555 solid_forces_table[i_solid].add_value("f_z", 0.);
556
557 // Precision
558 solid_forces_table[i_solid].set_precision(
559 "f_x", this->simulation_parameters.forces_parameters.output_precision);
560 solid_forces_table[i_solid].set_precision(
561 "f_y", this->simulation_parameters.forces_parameters.output_precision);
562 solid_forces_table[i_solid].set_precision(
563 "f_z", this->simulation_parameters.forces_parameters.output_precision);
564
565 std::string filename_force =
566 this->simulation_parameters.simulation_control.output_folder +
567 this->simulation_parameters.nitsche->nitsche_solids[i_solid]
568 ->force_output_name +
569 "_" + Utilities::int_to_string(i_solid, 2) + ".dat";
570 std::ofstream output_force(filename_force.c_str());
571
572 solid_forces_table[i_solid].write_text(output_force);
573}
574
575template <int dim, int spacedim>
576void
578 const unsigned int i_solid,
579 bool first_solid_torques)
580{
581 TimerOutput::Scope t(this->computing_timer, "Calculate torque on solid");
582
583 std::vector<Tensor<1, 3>> torque;
584 std::vector<unsigned int> solid_indices;
585
586 torque.emplace_back(this->calculate_torque_on_solid(i_solid));
587 solid_indices.emplace_back(i_solid);
588
589 if (this->simulation_parameters.nitsche->verbosity ==
591 this->this_mpi_process == 0)
592 {
593 std::string independent_column_names = "Solid ID";
594 std::vector<std::string> dependent_column_names;
595 dependent_column_names.emplace_back("T_x");
596 dependent_column_names.emplace_back("T_y");
597 dependent_column_names.emplace_back("T_z");
598
599 TableHandler table = make_table_scalars_tensors(
600 solid_indices,
601 independent_column_names,
602 torque,
603 dependent_column_names,
604 this->simulation_parameters.simulation_control.log_precision,
605 true);
606
607 // printed only for the first i_solid for which calculate_torques is true
608 // to be improved so that the column_names are not repeated
609 if (first_solid_torques)
610 {
611 std::cout << std::endl;
612 std::cout << "+------------------------------------------+"
613 << std::endl;
614 std::cout << "| Torque on solids summary |"
615 << std::endl;
616 std::cout << "+------------------------------------------+"
617 << std::endl;
618 }
619 table.write_text(std::cout);
620 }
621
622 if (this->simulation_control->is_steady())
623 {
624 solid_torques_table[i_solid].add_value(
625 "cells", this->triangulation->n_global_active_cells());
626 }
627 else
628 {
629 solid_torques_table[i_solid].add_value(
630 "time", this->simulation_control->get_current_time());
631 solid_torques_table[i_solid].set_precision(
632 "time", this->simulation_parameters.forces_parameters.output_precision);
633 }
634 solid_torques_table[i_solid].add_value("T_x", torque[0][0]);
635 solid_torques_table[i_solid].add_value("T_y", torque[0][1]);
636 solid_torques_table[i_solid].add_value("T_z", torque[0][2]);
637
638 // Precision
639 solid_torques_table[i_solid].set_precision(
640 "T_x", this->simulation_parameters.forces_parameters.output_precision);
641 solid_torques_table[i_solid].set_precision(
642 "T_y", this->simulation_parameters.forces_parameters.output_precision);
643 solid_torques_table[i_solid].set_precision(
644 "T_z", this->simulation_parameters.forces_parameters.output_precision);
645
646 if (this->this_mpi_process == 0)
647 {
648 std::string filename_torque =
649 this->simulation_parameters.simulation_control.output_folder +
650 this->simulation_parameters.nitsche->nitsche_solids[i_solid]
651 ->torque_output_name +
652 "_" + Utilities::int_to_string(i_solid, 2) + ".dat";
653 std::ofstream output_torque(filename_torque.c_str());
654
655 solid_torques_table[i_solid].write_text(output_torque);
656 }
657}
658
659template <int dim, int spacedim>
660void
662{
663 // Fluid setup
665 *this->triangulation,
666 this->simulation_parameters.mesh,
667 this->simulation_parameters.manifolds_parameters,
668 this->simulation_parameters.restart_parameters.restart,
669 this->simulation_parameters.boundary_conditions.periodic_boundaries);
670
671 this->setup_dofs();
672
673 // Solid setup
674 if (!this->simulation_parameters.restart_parameters.restart)
675 {
676 TimerOutput::Scope t(this->computing_timer,
677 "Nitsche setup solid mesh and particles");
678 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
679 {
680 (*solids)[i_solid]->initial_setup();
681
682 // Output initial configuration, if output_frequency!=0
683 if (this->simulation_control->is_output_iteration())
684 {
685 output_solid_particles(i_solid);
687 }
688 }
689 if constexpr (dim == spacedim)
690 {
691 // Share the Nitsche solids to the multiphysics interface
692 this->multiphysics->set_solid(solids);
693 }
694 }
695
697 this->simulation_parameters.initial_condition->type,
698 this->simulation_parameters.restart_parameters.restart);
699
700
701
702 // Time integration
703 while (this->simulation_control->integrate())
704 {
705 this->simulation_control->print_progression(this->pcout);
706
707 {
708 TimerOutput::Scope t(this->computing_timer, "Nitsche move particles");
709 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
710 {
711 if (this->simulation_parameters.nitsche->nitsche_solids[i_solid]
712 ->enable_particles_motion)
713 {
714 // Particle and solid displacement is explicit, thus it must go
715 // from t to t+dt
716 const double time_step =
717 this->simulation_control->get_time_step();
718 const double initial_time =
719 this->simulation_control->get_current_time() - time_step;
720
721 (*solids)[i_solid]->update_temperature_time(time_step);
722 (*solids)[i_solid]->integrate_velocity(time_step, initial_time);
723 (*solids)[i_solid]->move_solid_triangulation(time_step,
724 initial_time);
725 }
726 }
727 if constexpr (dim == spacedim)
728 {
729 // Share the Nitsche solids to the multiphysics interface
730 this->multiphysics->set_solid(solids);
731 }
732 }
733 if (this->simulation_control->is_at_start())
734 this->iterate();
735 else
736 {
737 this->refine_mesh();
738 this->iterate();
739 }
740
741 this->postprocess(false);
742 bool first_solid_forces(true);
743 bool first_solid_torques(true);
744
745 for (unsigned int i_solid = 0;
746 i_solid < this->simulation_parameters.nitsche->number_solids;
747 ++i_solid)
748 {
749 if (this->simulation_parameters.nitsche->nitsche_solids[i_solid]
750 ->calculate_force_on_solid)
751 {
752 postprocess_solid_forces(i_solid, first_solid_forces);
753 first_solid_forces = false;
754 }
755 if (this->simulation_parameters.nitsche->nitsche_solids[i_solid]
756 ->calculate_torque_on_solid)
757 {
758 // for 22 and 33 simulations
759 //(to be implemented for 23 simulations)
760 if (dim == spacedim)
761 {
762 postprocess_solid_torques(i_solid, first_solid_torques);
763 first_solid_torques = false;
764 }
765 }
766
767 if (this->simulation_control->is_output_iteration())
768 {
769 output_solid_particles(i_solid);
771 }
772 }
773 this->finish_time_step();
774 }
775 if (this->simulation_parameters.test.enabled)
776 {
777 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
778 {
779 (*solids)[i_solid]->print_particle_positions();
780 }
781 }
782 this->finish_simulation();
783}
784
785template <int dim, int spacedim>
786void
788 const unsigned int i_solid)
789{
790 std::shared_ptr<Particles::ParticleHandler<spacedim>> &particle_handler =
791 (*solids)[i_solid]->get_solid_particle_handler();
792 Particles::DataOut<spacedim, spacedim> particles_out;
793 particles_out.build_patches(*particle_handler);
794
795 const std::string folder = this->simulation_control->get_output_path();
796 const std::string solution_name =
797 this->simulation_control->get_output_name() + "_solid_particles_" +
798 Utilities::int_to_string(i_solid, 2);
799 const unsigned int iter = this->simulation_control->get_iteration_number();
800 const double time = this->simulation_control->get_current_time();
801 const unsigned int group_files = this->simulation_control->get_group_files();
802
804 *(&particles_out),
805 folder,
806 solution_name,
807 time,
808 iter,
809 group_files,
810 this->mpi_communicator);
811}
812
813template <int dim, int spacedim>
814void
816 const unsigned int i_solid)
817{
818 DataOut<dim, spacedim> data_out;
819 DoFHandler<dim, spacedim> &solid_dh =
820 (*solids)[i_solid]->get_solid_dof_handler();
821 data_out.attach_dof_handler(solid_dh);
822
823 DoFHandler<dim, spacedim> &displacement_dh =
824 (*solids)[i_solid]->get_displacement_dof_handler();
825 data_out.attach_dof_handler(displacement_dh);
826
827 std::vector<std::string> solution_names(spacedim, "displacement");
828 std::vector<DataComponentInterpretation::DataComponentInterpretation>
829 data_component_interpretation(
830 spacedim, DataComponentInterpretation::component_is_part_of_vector);
831 GlobalVectorType &displacement_vector =
832 (*solids)[i_solid]->get_displacement_vector();
833
834 data_out.add_data_vector(displacement_vector,
835 solution_names,
836 DataOut<dim, spacedim>::type_dof_data,
837 data_component_interpretation);
838
839
840
841 data_out.build_patches();
842
843 const std::string folder = this->simulation_control->get_output_path();
844 const std::string solution_name =
845 this->simulation_control->get_output_name() + "_solid_triangulation_" +
846 Utilities::int_to_string(i_solid, 2);
847 const unsigned int iter = this->simulation_control->get_iteration_number();
848 const double time = this->simulation_control->get_current_time();
849 const unsigned int group_files = this->simulation_control->get_group_files();
850
852 data_out,
853 folder,
854 solution_name,
855 time,
856 iter,
857 group_files,
858 this->mpi_communicator);
859}
860
861template <int dim, int spacedim>
862void
864{
865 if (this->simulation_control->is_refinement_step(
866 this->simulation_parameters.mesh_adaptation))
867 {
868 // If no adaptation is to be carried out, get out of the function
869 if (this->simulation_parameters.mesh_adaptation.type ==
871 return;
872
873 // Prepare the solid particle handlers for the mesh refinement
874 // All type of refinement except none will require that the particle
875 // handler be prepared for refinement
876 for (const auto &solid : *solids)
877 solid->get_solid_particle_handler()
878 ->prepare_for_coarsening_and_refinement();
879
880
881 if (this->simulation_parameters.mesh_adaptation.type ==
883 this->refine_mesh_adaptive();
884
885 else if (this->simulation_parameters.mesh_adaptation.type ==
887 this->refine_mesh_uniform();
888
889 // Unpact them after refinement has occured
890 for (const auto &solid : *solids)
891 solid->get_solid_particle_handler()
892 ->unpack_after_coarsening_and_refinement();
893 }
894}
895
896template <int dim, int spacedim>
897void
906
907template <int dim, int spacedim>
908void
915
916template <int dim, int spacedim>
917std::vector<OutputStructTableHandler>
919{
920 // Initilization of the structure that will contain the references to the
921 // tables that are additional in this solver and their associated filenames
922 std::vector<OutputStructTableHandler> table_output_structs;
923
924 // Prefix and suffix for the table checkpoint file name
925 std::string prefix =
926 this->simulation_parameters.simulation_control.output_folder;
927 std::string suffix = ".checkpoint";
928
929 // Add the tables of the forces and torques calculated on each solid, one
930 // table per solid
931 for (unsigned int i_solid = 0;
932 i_solid < this->simulation_parameters.nitsche->number_solids;
933 ++i_solid)
934 {
935 if (this->simulation_parameters.nitsche->nitsche_solids[i_solid]
936 ->calculate_force_on_solid)
937 {
938 table_output_structs.emplace_back(
939 this->solid_forces_table[i_solid],
940 prefix +
941 this->simulation_parameters.nitsche->nitsche_solids[i_solid]
942 ->force_output_name +
943 "_" + Utilities::int_to_string(i_solid, 2) + suffix);
944 }
945 if (this->simulation_parameters.nitsche->nitsche_solids[i_solid]
946 ->calculate_torque_on_solid)
947 {
948 table_output_structs.emplace_back(
949 this->solid_torques_table[i_solid],
950 prefix +
951 this->simulation_parameters.nitsche->nitsche_solids[i_solid]
952 ->torque_output_name +
953 "_" + Utilities::int_to_string(i_solid, 2) + suffix);
954 }
955 }
956 return table_output_structs;
957}
958
959template <int dim, int spacedim>
960void
962{
963 // Write solid base checkpoint
964 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
965 {
966 std::string filename =
967 this->simulation_parameters.simulation_control.output_folder +
968 this->simulation_parameters.restart_parameters.filename + "_solid_" +
969 Utilities::int_to_string(i_solid, 2);
970
971 (*this->solids)[i_solid]->write_checkpoint(filename);
972 }
973
974 // Call regular checkpointing routine
976
977 std::string prefix =
978 this->simulation_parameters.simulation_control.output_folder +
979 this->simulation_parameters.restart_parameters.filename;
980
981 // Write data about paraview format (pvd)
982 if (Utilities::MPI::this_mpi_process(this->mpi_communicator) == 0)
983 {
984 this->simulation_control->save(prefix);
985 // Navier-Stokes
986 this->pvdhandler.save(prefix);
987 // Nitche
988 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
989 {
990 pvdhandler_solid_particles[i_solid].save(
991 prefix + "_solid_particles_" +
992 Utilities::int_to_string(i_solid, 2));
993 pvdhandler_solid_triangulation[i_solid].save(
994 prefix + "_solid_triangulation_" +
995 Utilities::int_to_string(i_solid, 2));
996 }
997 }
998
999 // Serialize the default post-processing tables that are members of
1000 // NavierStokesBase. Only the default tables need to be serialized here, since
1001 // the additional tables (specific to this solver) are serialized above in the
1002 // previous call this->FluidDynamicsMatrixBased<spacedim>::
1003 // write_checkpoint(), which calls this->gather_tables() and then
1004 // serialize_tables_vector in NavierStokesBase.
1005 const std::vector<OutputStructTableHandler> &table_output_structs =
1007 serialize_tables_vector(table_output_structs, this->mpi_communicator);
1008}
1009
1010template <int dim, int spacedim>
1011void
1013{
1015
1016 TimerOutput::Scope t(this->computing_timer,
1017 "Nitsche reload solid mesh and particles");
1018
1019 // Reload initial configurations
1020 // This must be done after the background triangulation is read
1021 // because the solid particle_handler is deserialized physically
1022 // after the triangulation is read.
1023 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
1024 {
1025 std::string prefix =
1026 this->simulation_parameters.simulation_control.output_folder +
1027 this->simulation_parameters.restart_parameters.filename + "_solid_" +
1028 Utilities::int_to_string(i_solid, 2);
1029
1030 (*solids)[i_solid]->read_checkpoint(prefix);
1031 }
1032
1033
1034 // Reload particle and solid pvd handlers
1035 std::string prefix =
1036 this->simulation_parameters.simulation_control.output_folder +
1037 this->simulation_parameters.restart_parameters.filename;
1038 for (unsigned int i_solid = 0; i_solid < solids->size(); ++i_solid)
1039 {
1040 // Load Paraview pvd handler for solid triangulation and particles
1041 pvdhandler_solid_particles[i_solid].read(
1042 prefix + "_solid_particles_" + Utilities::int_to_string(i_solid, 2));
1043 pvdhandler_solid_triangulation[i_solid].read(
1044 prefix + "_solid_triangulation_" +
1045 Utilities::int_to_string(i_solid, 2));
1046 }
1047
1048 // Deserialize the default post-processing tables that are members of
1049 // NavierStokesBase. Only the default tables need to be deserialized here,
1050 // since the additional tables (specific to this solver) are deserialized
1051 // above in the previous call this->FluidDynamicsMatrixBased<spacedim>::
1052 // read_checkpoint(), which calls this->gather_tables() and then
1053 // deserialize_tables_vector in NavierStokesBase.
1054 std::vector<OutputStructTableHandler> table_output_structs =
1056 deserialize_tables_vector(table_output_structs, this->mpi_communicator);
1057}
1058
1059
1060// Pre-compile the 2D and 3D Navier-Stokes solver to ensure that the library
1061// is valid before we actually compile the solver This greatly helps with
1062// debugging
1063template class FluidDynamicsNitsche<2>;
1064template class FluidDynamicsNitsche<2, 3>;
1065template class FluidDynamicsNitsche<3>;
TrilinosWrappers::SparseMatrix system_matrix
virtual void assemble_system_matrix() override
Assembles the matrix associated with the solver.
virtual void assemble_system_rhs() override
Assemble the rhs associated with the solver.
FluidDynamicsMatrixBased(SimulationParameters< dim > &nsparam, const FluidDynamicsFormulation fluid_dynamics_formulation=FluidDynamicsFormulation::standard)
Construct the matrix-based Navier-Stokes solver.
FluidDynamicsNitsche(SimulationParameters< spacedim > &nsparam)
void output_solid_triangulation(const unsigned int i_solid)
Outputs a vtu file for each output frequency of the solid triangulation.
void assemble_nitsche_restriction()
Adds the nitsche restriction to the global matrix and global rhs on the cells surrounding the immerse...
void postprocess_solid_torques(const unsigned int i_solid, bool first_solid_torques)
Post-process for torques on solid after an iteration.
std::vector< PVDHandler > pvdhandler_solid_triangulation
std::vector< OutputStructTableHandler > gather_tables() override
Returns a vector of references to TableHandler objects that needs to be serialized/deserialized for a...
Tensor< 1, 3 > calculate_torque_on_solid()
Calculates the torque due to the fluid motion on the solid.
virtual void read_checkpoint() override
Read a gls_nitsche simulation checkpoint and initiate simulation restart.
void output_solid_particles(const unsigned int i_solid)
Outputs a vtu file for each output frequency of the particles.
std::vector< TableHandler > solid_forces_table
void assemble_matrix_and_rhs()
Same as in fluid_dynamics_matrix_based, but calls assemble_nitsche_restriction() when global matrix a...
Tensor< 1, spacedim > calculate_forces_on_solid(const unsigned int i_solid)
Calculates the force due to the fluid motion on the solid.
std::shared_ptr< std::vector< std::shared_ptr< SolidBase< dim, spacedim > > > > solids
virtual void solve() override
solve Solves the Navier-Stokes problem
Tensor< 1, 3 > calculate_torque_on_solid(const unsigned int i_solid)
Calculates the torque due to the fluid motion on the solid.
std::vector< PVDHandler > pvdhandler_solid_particles
virtual void refine_mesh() override
Allow the refinement of the mesh according to one of the 2 methods proposed. Overrides the regular me...
virtual void write_checkpoint() override
Write a gls_nitsche simulation checkpointing to allow for gls_nitsche simulation restart.
void postprocess_solid_forces(const unsigned int i_solid, bool first_solid_forces)
Post-process for forces on solid after an iteration.
void assemble_rhs()
Same as in fluid_dynamics_matrix_based, but calls assemble_nitsche_restriction() when rhs is assemble...
std::vector< TableHandler > solid_torques_table
virtual void set_initial_condition(const Parameters::FluidDynamicsInitialConditionType initial_condition_type, const bool restart=false)
virtual std::vector< OutputStructTableHandler > gather_tables()
Returns a vector of references to TableHandler objects that needs to be serialized/ deserialized for ...
std::shared_ptr< SimulationControl > simulation_control
std::shared_ptr< MultiphysicsInterface< dim > > multiphysics
std::shared_ptr< DoFHandler< dim > > dof_handler
std::shared_ptr< parallel::DistributedTriangulationBase< dim > > triangulation
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
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
double compute_cell_diameter(const double cell_measure, const unsigned int fe_degree)
Computes equivalent cell diameter by comparing the area to a disk (2D) or the volume to a sphere (3D)...
Definition utilities.h:989
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 read_mesh_and_manifolds(parallel::DistributedTriangulationBase< dim, spacedim > &triangulation, const Parameters::Mesh< dim, spacedim > &mesh_parameters, const Parameters::Manifolds &manifolds_parameters, bool restart, const Parameters::PeriodicBoundaries &periodic_boundaries)
Completely sets up a mesh and its manifolds.
Definition grids.cc:447
@ velocity
Velocity vector field from fluid dynamics.
@ pressure
Pressure scalar field from fluid dynamics.
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...
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17
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.