8#include <compile_time_options.h>
12#if DEAL_II_VERSION_GTE(9, 6, 0)
16#include <deal.II/base/config.h>
17#include <deal.II/distributed/tria.h>
18#include <deal.II/dofs/dof_accessor.h>
19#include <deal.II/dofs/dof_tools.h>
20#if DEAL_II_VERSION_GTE(9, 6, 0)
21#include <deal.II/grid/cell_status.h>
23#include <deal.II/grid/tria_accessor.h>
24#include <deal.II/grid/tria_iterator.h>
25#include <deal.II/lac/block_vector.h>
26#include <deal.II/lac/la_parallel_block_vector.h>
27#include <deal.II/lac/la_parallel_vector.h>
28#include <deal.II/lac/petsc_block_vector.h>
29#include <deal.II/lac/petsc_vector.h>
30#include <deal.II/lac/trilinos_parallel_block_vector.h>
31#include <deal.II/lac/trilinos_vector.h>
32#include <deal.II/lac/vector.h>
33#include <deal.II/matrix_free/fe_point_evaluation.h>
38 template <
typename Description,
int dim,
typename Number>
45 : mpi_ensemble_(mpi_ensemble)
46 , triangulation_(&triangulation)
47 , offline_data_(&offline_data)
48 , hyperbolic_system_(&hyperbolic_system)
49 , parabolic_system_(¶bolic_system)
50 , handle_(dealii::numbers::invalid_unsigned_int)
53 AssertThrow(have_distributed_triangulation<dim>,
55 "The SolutionTransfer class is not implemented for a "
56 "distributed::shared::Triangulation which we use in 1D"));
65 template <
typename state_type>
67 pack_state_values(
const std::vector<state_type> &state_values)
69 std::vector<char> buffer(
sizeof(state_type) * state_values.size());
70 std::memcpy(buffer.data(), state_values.data(), buffer.size());
78 template <
typename state_type>
79 std::vector<state_type> unpack_state_values(
80 const boost::iterator_range<std::vector<char>::const_iterator>
83 const std::size_t n_bytes = data_range.size();
84 Assert(n_bytes %
sizeof(state_type) == 0, dealii::ExcInternalError());
85 std::vector<state_type> state_values(n_bytes /
sizeof(state_type));
86 std::memcpy(state_values.data(),
88 state_values.size() *
sizeof(state_type));
94 template <
typename Description,
int dim,
typename Number>
95 inline DEAL_II_ALWAYS_INLINE
auto
96 SolutionTransfer<Description, dim, Number>::get_tensor(
97 const HyperbolicVector &U,
const dealii::types::global_dof_index global_i)
100 const auto &scalar_partitioner = offline_data_->scalar_partitioner();
101 const auto &affine_constraints = offline_data_->affine_constraints();
102 const auto local_i = scalar_partitioner->global_to_local(global_i);
103 if (affine_constraints.is_constrained(global_i)) {
105 const auto &line = *affine_constraints.get_constraint_entries(global_i);
106 for (
const auto &[global_k, c_k] : line) {
107 const auto local_k = scalar_partitioner->global_to_local(global_k);
108 result += c_k * U.get_tensor(local_k);
112 return U.get_tensor(local_i);
117 template <
typename Description,
int dim,
typename Number>
118 inline DEAL_II_ALWAYS_INLINE
void
119 SolutionTransfer<Description, dim, Number>::add_tensor(
121 const state_type &new_U_i,
122 const dealii::types::global_dof_index global_i)
124 const auto &scalar_partitioner = offline_data_->scalar_partitioner();
125 const auto local_i = scalar_partitioner->global_to_local(global_i);
126 U.add_tensor(new_U_i, local_i);
130 template <
typename Description,
int dim,
typename Number>
132 const StateVector &old_state_vector [[maybe_unused]])
136 <<
"SolutionTransfer<Description, dim, Number>::prepare_projection()"
140#if !DEAL_II_VERSION_GTE(9, 6, 0)
144 "The SolutionTransfer class needs deal.II version 9.6.0 or newer"));
147 AssertThrow(have_distributed_triangulation<dim>,
149 "The SolutionTransfer class is not implemented for a "
150 "distributed::shared::Triangulation which we use in 1D"));
152 const auto &discretization = offline_data_->discretization();
153 const auto &triangulation [[maybe_unused]] = discretization.triangulation();
154 Assert(triangulation_ == &triangulation,
156 "The attached triangulation object must be the same object that "
157 "is stored in Discretization/OfflineData"));
159 Assert(handle_ == dealii::numbers::invalid_unsigned_int,
161 "You can only add one solution per SolutionTransfer object."));
167 handle_ = triangulation_->register_data_attach(
168 [
this, &old_state_vector](
const auto cell,
169 const dealii::CellStatus status) {
170 const auto &dof_handler = offline_data_->dof_handler();
171 const auto dof_cell =
typename dealii::DoFHandler<dim>::cell_iterator(
172 &cell->get_triangulation(),
177 const auto &U = std::get<0>(old_state_vector);
183 const auto n_dofs_per_cell = dof_handler.get_fe().n_dofs_per_cell();
184 std::vector<state_type> state_values(n_dofs_per_cell);
187 case dealii::CellStatus::cell_will_persist:
189 case dealii::CellStatus::cell_will_be_refined: {
195 Assert(dof_cell->is_active(), dealii::ExcInternalError());
196 std::vector<dealii::types::global_dof_index> dof_indices(
198 dof_cell->get_dof_indices(dof_indices);
200 std::transform(std::begin(dof_indices),
201 std::end(dof_indices),
202 std::begin(state_values),
204 const auto U_i = get_tensor(U, i);
209 case dealii::CellStatus::children_will_be_coarsened: {
216 Assert(dof_cell->has_children(), dealii::ExcInternalError());
218 const auto &discretization = offline_data_->discretization();
219 const auto &finite_element = discretization.finite_element();
220 const auto &mapping = discretization.mapping();
221 const auto &quadrature = discretization.quadrature();
223 dealii::FEValues<dim> fe_values(
227 dealii::update_values | dealii::update_JxW_values |
228 dealii::update_quadrature_points);
230 const auto polynomial_space =
231 dealii::internal::FEPointEvaluation::get_polynomial_space(
234 std::vector<dealii::Point<dim, Number>> unit_points(
240 std::vector<dealii::Point<dim>> unit_points_temp(
241 std::is_same_v<Number, float> ? quadrature.size() : 0);
245 std::vector<state_type> state_values_quad(quadrature.size());
246 std::vector<state_type> local_rhs(n_dofs_per_cell);
248 std::vector<dealii::types::global_dof_index> dof_indices(
251 for (
unsigned int child = 0; child < dof_cell->n_children();
253 const auto child_cell = dof_cell->child(child);
254 Assert(child_cell->is_active(), dealii::ExcInternalError());
256 fe_values.reinit(child_cell);
258 if constexpr (std::is_same_v<Number, float>) {
259 mapping.transform_points_real_to_unit_cell(
261 fe_values.get_quadrature_points(),
263 std::transform(std::begin(unit_points_temp),
264 std::end(unit_points_temp),
265 std::begin(unit_points),
266 [](
const auto &x) {
return x; });
268 mapping.transform_points_real_to_unit_cell(
269 dof_cell, fe_values.get_quadrature_points(), unit_points);
272 child_cell->get_dof_indices(dof_indices);
274 for (
auto &it : state_values_quad)
277 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i) {
278 const auto U_i = get_tensor(U, dof_indices[i]);
279 for (
unsigned int q = 0; q < quadrature.size(); ++q) {
280 state_values_quad[q] += U_i * fe_values.shape_value(i, q);
284 for (
unsigned int q = 0; q < quadrature.size(); ++q)
285 state_values_quad[q] *= fe_values.JxW(q);
287 for (
unsigned int q = 0; q < quadrature.size(); ++q) {
288 const unsigned int n_shapes = polynomial_space.size();
289 AssertIndexRange(n_shapes, 10);
290 dealii::ndarray<Number, 10, 2, dim> shapes;
292 std::array<Number, dim> point;
293 for (
unsigned int d = 0; d < dim; ++d)
294 point[d] = unit_points[q][d];
295 for (
unsigned int i = 0; i < n_shapes; ++i)
296 polynomial_space[i].values_of_array(point, 1, &shapes[i][0]);
298 Assert(finite_element.degree == 1, dealii::ExcNotImplemented());
306 state_values_quad[q],
315 fe_values.reinit(dof_cell);
317 dealii::FullMatrix<double> mij(n_dofs_per_cell, n_dofs_per_cell);
318 dealii::Vector<double> mi(n_dofs_per_cell);
319 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i) {
320 for (
unsigned int j = 0; j < n_dofs_per_cell; ++j) {
322 for (
unsigned int q = 0; q < quadrature.size(); ++q)
323 sum += fe_values.shape_value(i, q) *
324 fe_values.shape_value(j, q) * fe_values.JxW(q);
332 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i) {
333 for (
unsigned int j = 0; j < n_dofs_per_cell; ++j) {
334 state_values[i] += mij(i, j) * local_rhs[j];
339 case dealii::CellStatus::cell_invalid:
340 Assert(
false, dealii::ExcInternalError());
345 return pack_state_values(state_values);
352 template <
typename Description,
int dim,
typename Number>
357 std::cout <<
"SolutionTransfer<Description, dim, Number>::project()"
361#if !DEAL_II_VERSION_GTE(9, 6, 0)
365 "The SolutionTransfer class needs deal.II version 9.6.0 or newer"));
369 AssertThrow(have_distributed_triangulation<dim>,
371 "The SolutionTransfer class is not implemented for a "
372 "distributed::shared::Triangulation which we use in 1D"));
374 const auto &scalar_partitioner = offline_data_->scalar_partitioner();
375 const auto &affine_constraints = offline_data_->affine_constraints();
376 const auto &discretization = offline_data_->discretization();
377 const auto &triangulation [[maybe_unused]] = discretization.triangulation();
378 Assert(triangulation_ == &triangulation,
380 "The attached triangulation object must be the same object that "
381 "is stored in Discretization/OfflineData"));
384 handle_ != dealii::numbers::invalid_unsigned_int,
386 "Cannot project() a state vector without valid handle. "
387 "prepare_projection() or set_handle() have to be called first."));
394 projected_mass.reinit(offline_data_->scalar_partitioner());
396 projected_state.reinit(offline_data_->hyperbolic_vector_partitioner());
398 triangulation_->notify_ready_to_unpack(
400 [
this, &projected_mass, &projected_state](
402 const dealii::CellStatus status,
403 const auto &data_range) {
404 const auto &dof_handler = offline_data_->dof_handler();
405 const auto dof_cell =
typename dealii::DoFHandler<dim>::cell_iterator(
406 &cell->get_triangulation(),
415 const auto n_dofs_per_cell = dof_handler.get_fe().n_dofs_per_cell();
416 std::vector<dealii::types::global_dof_index> dof_indices(
419 const auto state_values = unpack_state_values<state_type>(data_range);
422 case dealii::CellStatus::cell_will_persist:
424 case dealii::CellStatus::children_will_be_coarsened: {
430 Assert(dof_cell->is_active(), dealii::ExcInternalError());
431 dof_cell->get_dof_indices(dof_indices);
433 const auto &discretization = offline_data_->discretization();
434 const auto &finite_element = discretization.finite_element();
435 const auto &mapping = discretization.mapping();
436 const auto &quadrature = discretization.quadrature();
438 dealii::FEValues<dim> fe_values(mapping,
441 dealii::update_values |
442 dealii::update_JxW_values);
444 fe_values.reinit(dof_cell);
446 dealii::Vector<double> mi(n_dofs_per_cell);
447 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i) {
449 for (
unsigned int q = 0; q < quadrature.size(); ++q)
450 sum += fe_values.shape_value(i, q) * fe_values.JxW(q);
454 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i) {
455 const auto global_i = dof_indices[i];
456 add_tensor(projected_state, mi(i) * state_values[i], global_i);
457 projected_mass(global_i) += mi(i);
462 case dealii::CellStatus::cell_will_be_refined: {
468 Assert(dof_cell->has_children(), dealii::ExcInternalError());
470 const auto &discretization = offline_data_->discretization();
471 const auto &finite_element = discretization.finite_element();
472 const auto &mapping = discretization.mapping();
473 const auto &quadrature = discretization.quadrature();
475 dealii::FEValues<dim> fe_values(
479 dealii::update_values | dealii::update_JxW_values |
480 dealii::update_quadrature_points);
482 const auto polynomial_space =
483 dealii::internal::FEPointEvaluation::get_polynomial_space(
485 std::vector<dealii::Point<dim, Number>> unit_points(
491 std::vector<dealii::Point<dim>> unit_points_temp(
492 std::is_same_v<Number, float> ? quadrature.size() : 0);
494 dealii::FullMatrix<double> mij(n_dofs_per_cell, n_dofs_per_cell);
495 dealii::Vector<double> mi(n_dofs_per_cell);
496 std::vector<state_type> local_rhs(n_dofs_per_cell);
498 for (
unsigned int child = 0; child < dof_cell->n_children();
500 const auto child_cell = dof_cell->child(child);
502 Assert(child_cell->is_active(), dealii::ExcInternalError());
503 child_cell->get_dof_indices(dof_indices);
507 fe_values.reinit(child_cell);
509 if constexpr (std::is_same_v<Number, float>) {
510 mapping.transform_points_real_to_unit_cell(
512 fe_values.get_quadrature_points(),
514 std::transform(std::begin(unit_points_temp),
515 std::end(unit_points_temp),
516 std::begin(unit_points),
517 [](
const auto &x) {
return x; });
519 mapping.transform_points_real_to_unit_cell(
520 dof_cell, fe_values.get_quadrature_points(), unit_points);
523 for (
auto &it : local_rhs)
526 for (
unsigned int q = 0; q < quadrature.size(); ++q) {
527 Assert(finite_element.degree == 1, dealii::ExcNotImplemented());
529 dealii::internal::evaluate_tensor_product_value(
531 make_const_array_view(state_values),
534 coefficient *= fe_values.JxW(q);
536 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i)
537 local_rhs[i] += coefficient * fe_values.shape_value(i, q);
544 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i) {
545 for (
unsigned int j = 0; j < n_dofs_per_cell; ++j) {
547 for (
unsigned int q = 0; q < quadrature.size(); ++q)
548 sum += fe_values.shape_value(i, q) *
549 fe_values.shape_value(j, q) * fe_values.JxW(q);
557 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i) {
559 for (
unsigned int j = 0; j < n_dofs_per_cell; ++j) {
560 U_i += mij(i, j) * local_rhs[j];
563 const auto global_i = dof_indices[i];
564 add_tensor(projected_state, mi(i) * U_i, global_i);
565 projected_mass(global_i) += mi(i);
570 case dealii::CellStatus::cell_invalid:
571 Assert(
false, dealii::ExcInternalError());
582 auto &new_U = std::get<0>(new_state_vector);
583 const auto n_locally_owned = offline_data_->n_locally_owned();
587 const auto update_new_state_vector = [&]() {
591 for (
unsigned int local_i = 0; local_i < n_locally_owned; ++local_i) {
592 const auto global_i = scalar_partitioner->local_to_global(local_i);
593 if (affine_constraints.is_constrained(global_i))
596 const auto U_i = projected_state.get_tensor(local_i);
597 const auto m_i = projected_mass.local_element(local_i);
598 new_U.write_tensor(U_i / m_i, local_i);
600 new_U.update_ghost_values();
603 update_new_state_vector();
614 for (
const auto &line : affine_constraints.get_lines()) {
615 const auto global_i = line.index;
616 const auto local_i = scalar_partitioner->global_to_local(global_i);
619 if (local_i >= n_locally_owned)
623 const auto m_i_star = projected_mass.local_element(local_i);
624 const auto U_i_star = projected_state.get_tensor(local_i) / m_i_star;
628 for (
const auto &[global_k, c_k] : line.entries) {
629 const auto local_k = scalar_partitioner->global_to_local(global_k);
630 U_i_interp += c_k * new_U.get_tensor(local_k);
634 const auto defect = U_i_star - U_i_interp;
635 for (
const auto &[global_k, c_k] : line.entries) {
636 const auto local_k = scalar_partitioner->global_to_local(global_k);
637 const auto U_j = new_U.get_tensor(local_k);
639 projected_state.add_tensor(c_k * m_i_star * (U_j + defect), local_k);
640 projected_mass.local_element(local_k) += c_k * m_i_star;
644 update_new_state_vector();
650 const auto &lumped_mass_matrix = offline_data_->lumped_mass_matrix();
651 for (
unsigned int local_i = 0; local_i < n_locally_owned; ++local_i) {
652 const auto global_i = scalar_partitioner->local_to_global(local_i);
653 if (affine_constraints.is_constrained(global_i))
656 const auto m_i = projected_mass.local_element(local_i);
657 const auto m_i_reference = lumped_mass_matrix.local_element(local_i);
658 Assert(std::abs(m_i - m_i_reference) < 1.e-10,
660 "SolutionTransfer::projection(): something went wrong. Final "
661 "masses do not agree with those computed in OfflineData."));
typename Proxy< dim >::Triangulation Triangulation
typename View::HyperbolicVector HyperbolicVector
Vectors::ScalarVector< Number > ScalarVector
typename View::state_type state_type
typename Description::ParabolicSystem ParabolicSystem
void prepare_projection(const StateVector &old_state_vector)
SolutionTransfer(const MPIEnsemble &mpi_ensemble, typename Discretization< dim >::Triangulation &triangulation, const OfflineData< dim, Number > &offline_data, const HyperbolicSystem &hyperbolic_system, const ParabolicSystem ¶bolic_system)
typename Description::HyperbolicSystem HyperbolicSystem
void project(StateVector &new_state_vector)
typename View::StateVector StateVector
void integrate_tensor_product_value(const dealii::ndarray< Number, 2, dim > *shapes, const unsigned int n_shapes, const Number2 &value, Number2 *values, const dealii::Point< dim, Number > &p, const bool do_add)
DEAL_II_ALWAYS_INLINE FT add(const FT &flux_left_ij, const FT &flux_right_ij)