From: Peter Munch Date: Fri, 15 Jul 2022 07:19:57 +0000 (+0200) Subject: Generalized VT::point_gradients() for multiple components X-Git-Tag: v9.5.0-rc1~1021^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F14140%2Fhead;p=dealii.git Generalized VT::point_gradients() for multiple components Co-authored-by: Peter Munch Co-authored-by: Martin Kronbichler rename file --- diff --git a/include/deal.II/numerics/vector_tools_evaluate.h b/include/deal.II/numerics/vector_tools_evaluate.h index 8660498930..fbb44a6ed7 100644 --- a/include/deal.II/numerics/vector_tools_evaluate.h +++ b/include/deal.II/numerics/vector_tools_evaluate.h @@ -343,6 +343,42 @@ namespace VectorTools + /** + * Perform reduction for tensors of tensors (e.g., gradient of + * vectorial quantities). + */ + template + Tensor<1, n_components, Tensor> + reduce( + const EvaluationFlags::EvaluationFlags &flags, + const ArrayView>> + &values) + { + switch (flags) + { + case EvaluationFlags::avg: + { + Tensor<1, n_components, Tensor> temp; + + for (unsigned int j = 0; j < values.size(); ++j) + for (unsigned int i = 0; i < n_components; ++i) + temp[i] = temp[i] + values[j][i]; + + for (unsigned int i = 0; i < n_components; ++i) + temp[i] /= Number(values.size()); + + return temp; + } + case EvaluationFlags::insert: + return values[0]; + default: + Assert(false, ExcNotImplemented()); + return values[0]; + } + } + + + template + void + set_value(Number &dst, const Number2 &src) + { + dst = src; + } + + + + template + void + set_value(Tensor &, const Number2 &) + { + Assert(false, + ExcMessage( + "A cell-data vector can only have a single component.")); + } + + + template + +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "../tests.h" + +using namespace dealii; + +template +MPI_Comm +get_mpi_comm(const MeshType &mesh) +{ + const auto *tria_parallel = dynamic_cast< + const parallel::TriangulationBase *>( + &(mesh.get_triangulation())); + + return tria_parallel != nullptr ? tria_parallel->get_communicator() : + MPI_COMM_SELF; +} + +template +std::shared_ptr +create_partitioner(const DoFHandler &dof_handler) +{ + IndexSet locally_relevant_dofs; + + DoFTools::extract_locally_relevant_dofs(dof_handler, locally_relevant_dofs); + + return std::make_shared( + dof_handler.locally_owned_dofs(), + locally_relevant_dofs, + get_mpi_comm(dof_handler)); +} + +template +void +print(const Mapping & mapping, + const DoFHandler & dof_handler, + const LinearAlgebra::distributed::Vector &result, + const unsigned int counter) +{ + result.update_ghost_values(); + + DataOutBase::VtkFlags flags; + flags.write_higher_order_cells = true; + + DataOut data_out; + data_out.set_flags(flags); + data_out.attach_dof_handler(dof_handler); + + const auto &tria = dof_handler.get_triangulation(); + + Vector ranks(tria.n_active_cells()); + for (const auto &cell : + tria.active_cell_iterators() | IteratorFilters::LocallyOwnedCell()) + ranks(cell->active_cell_index()) = cell->subdomain_id(); + data_out.add_data_vector(ranks, "rank"); + data_out.add_data_vector(result, "result"); + + data_out.build_patches(mapping, dof_handler.get_fe().tensor_degree() + 1); + data_out.write_vtu_with_pvtu_record( + "./", "example-7", counter, MPI_COMM_WORLD, 1, 1); + + result.zero_out_ghost_values(); +} + +template +class AnalyticalFunction : public Function +{ +public: + AnalyticalFunction(const unsigned int n_components) + : Function(n_components) + {} + + virtual double + value(const Point &p, const unsigned int component) const + { + return p[component] * p[component]; + } +}; + +void +test() +{ + const unsigned int dim = 2; + const unsigned int degree = 2; + const unsigned int n_components = dim; + const unsigned int n_refinements_1 = 3; + + parallel::distributed::Triangulation tria_1(MPI_COMM_WORLD); + GridGenerator::hyper_cube(tria_1, -1.0, +1.0); + tria_1.refine_global(n_refinements_1); + DoFHandler dof_handler_1(tria_1); + dealii::FESystem fe_1(dealii::FE_Q(degree), n_components); + dof_handler_1.distribute_dofs(fe_1); + + LinearAlgebra::distributed::Vector vector_1( + create_partitioner(dof_handler_1)); + + const MappingQ1 mapping_1; + VectorTools::interpolate(mapping_1, + dof_handler_1, + AnalyticalFunction(n_components), + vector_1); + + std::vector> evaluation_points; + + const unsigned int n_intervals = 100; + + if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0) + for (unsigned int i = 0; i <= n_intervals; ++i) + evaluation_points.emplace_back(-1.0 + 2.0 / n_intervals * i, 0.0); + + vector_1.update_ghost_values(); + Utilities::MPI::RemotePointEvaluation evaluation_cache; + const auto evaluation_point_gradient_results = + VectorTools::point_gradients( + mapping_1, dof_handler_1, vector_1, evaluation_points, evaluation_cache); + vector_1.zero_out_ghost_values(); + + if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0) + for (unsigned int i = 0; i <= n_intervals; ++i) + for (unsigned int c1 = 0; c1 < n_components; ++c1) + for (unsigned int c2 = 0; c2 < n_components; ++c2) + deallog << evaluation_point_gradient_results[i][c1][c2] << std::endl; +} + +int +main(int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi(argc, argv, 1); + initlog(); + deallog << std::setprecision(8) << std::fixed; + + test(); +} diff --git a/tests/remote_point_evaluation/vector_tools_evaluate_at_points_07.with_p4est=true.output b/tests/remote_point_evaluation/vector_tools_evaluate_at_points_07.with_p4est=true.output new file mode 100644 index 0000000000..b7bd8d724a --- /dev/null +++ b/tests/remote_point_evaluation/vector_tools_evaluate_at_points_07.with_p4est=true.output @@ -0,0 +1,404 @@ +-2.00000000 +0.00000000 +0.00000000 +0.00000000 +-1.96000000 +0.00000000 +0.00000000 +0.00000000 +-1.92000000 +-0.00000000 +0.00000000 +0.00000000 +-1.88000000 +0.00000000 +0.00000000 +0.00000000 +-1.84000000 +0.00000000 +0.00000000 +0.00000000 +-1.80000000 +0.00000000 +0.00000000 +0.00000000 +-1.76000000 +0.00000000 +0.00000000 +0.00000000 +-1.72000000 +-0.00000000 +0.00000000 +0.00000000 +-1.68000000 +0.00000000 +0.00000000 +0.00000000 +-1.64000000 +0.00000000 +0.00000000 +0.00000000 +-1.60000000 +0.00000000 +0.00000000 +0.00000000 +-1.56000000 +0.00000000 +0.00000000 +0.00000000 +-1.52000000 +-0.00000000 +0.00000000 +0.00000000 +-1.48000000 +0.00000000 +0.00000000 +0.00000000 +-1.44000000 +0.00000000 +0.00000000 +0.00000000 +-1.40000000 +0.00000000 +0.00000000 +0.00000000 +-1.36000000 +0.00000000 +0.00000000 +0.00000000 +-1.32000000 +0.00000000 +0.00000000 +0.00000000 +-1.28000000 +-0.00000000 +0.00000000 +0.00000000 +-1.24000000 +-0.00000000 +0.00000000 +0.00000000 +-1.20000000 +-0.00000000 +0.00000000 +0.00000000 +-1.16000000 +0.00000000 +0.00000000 +0.00000000 +-1.12000000 +0.00000000 +0.00000000 +0.00000000 +-1.08000000 +0.00000000 +0.00000000 +0.00000000 +-1.04000000 +-0.00000000 +0.00000000 +0.00000000 +-1.00000000 +0.00000000 +0.00000000 +0.00000000 +-0.96000000 +-0.00000000 +0.00000000 +0.00000000 +-0.92000000 +0.00000000 +0.00000000 +0.00000000 +-0.88000000 +0.00000000 +0.00000000 +0.00000000 +-0.84000000 +-0.00000000 +0.00000000 +0.00000000 +-0.80000000 +0.00000000 +0.00000000 +0.00000000 +-0.76000000 +-0.00000000 +0.00000000 +0.00000000 +-0.72000000 +0.00000000 +0.00000000 +0.00000000 +-0.68000000 +-0.00000000 +0.00000000 +0.00000000 +-0.64000000 +0.00000000 +0.00000000 +0.00000000 +-0.60000000 +0.00000000 +0.00000000 +0.00000000 +-0.56000000 +0.00000000 +0.00000000 +0.00000000 +-0.52000000 +-0.00000000 +0.00000000 +0.00000000 +-0.48000000 +-0.00000000 +0.00000000 +0.00000000 +-0.44000000 +0.00000000 +0.00000000 +0.00000000 +-0.40000000 +0.00000000 +0.00000000 +0.00000000 +-0.36000000 +0.00000000 +0.00000000 +0.00000000 +-0.32000000 +0.00000000 +0.00000000 +0.00000000 +-0.28000000 +0.00000000 +0.00000000 +0.00000000 +-0.24000000 +-0.00000000 +0.00000000 +0.00000000 +-0.20000000 +0.00000000 +0.00000000 +0.00000000 +-0.16000000 +-0.00000000 +0.00000000 +0.00000000 +-0.12000000 +0.00000000 +0.00000000 +0.00000000 +-0.08000000 +0.00000000 +0.00000000 +0.00000000 +-0.04000000 +0.00000000 +0.00000000 +0.00000000 +0.00000000 +0.00000000 +0.00000000 +0.00000000 +0.04000000 +0.00000000 +0.00000000 +0.00000000 +0.08000000 +0.00000000 +0.00000000 +0.00000000 +0.12000000 +-0.00000000 +0.00000000 +0.00000000 +0.16000000 +-0.00000000 +0.00000000 +0.00000000 +0.20000000 +-0.00000000 +0.00000000 +0.00000000 +0.24000000 +-0.00000000 +0.00000000 +0.00000000 +0.28000000 +0.00000000 +0.00000000 +0.00000000 +0.32000000 +-0.00000000 +0.00000000 +0.00000000 +0.36000000 +0.00000000 +0.00000000 +0.00000000 +0.40000000 +0.00000000 +0.00000000 +0.00000000 +0.44000000 +0.00000000 +0.00000000 +0.00000000 +0.48000000 +-0.00000000 +0.00000000 +0.00000000 +0.52000000 +0.00000000 +0.00000000 +0.00000000 +0.56000000 +0.00000000 +0.00000000 +0.00000000 +0.60000000 +0.00000000 +0.00000000 +0.00000000 +0.64000000 +0.00000000 +0.00000000 +0.00000000 +0.68000000 +0.00000000 +0.00000000 +0.00000000 +0.72000000 +-0.00000000 +0.00000000 +0.00000000 +0.76000000 +0.00000000 +0.00000000 +0.00000000 +0.80000000 +0.00000000 +0.00000000 +0.00000000 +0.84000000 +-0.00000000 +0.00000000 +0.00000000 +0.88000000 +0.00000000 +0.00000000 +0.00000000 +0.92000000 +0.00000000 +0.00000000 +0.00000000 +0.96000000 +-0.00000000 +0.00000000 +0.00000000 +1.00000000 +0.00000000 +0.00000000 +0.00000000 +1.04000000 +0.00000000 +0.00000000 +0.00000000 +1.08000000 +0.00000000 +0.00000000 +0.00000000 +1.12000000 +0.00000000 +0.00000000 +0.00000000 +1.16000000 +0.00000000 +0.00000000 +0.00000000 +1.20000000 +0.00000000 +0.00000000 +0.00000000 +1.24000000 +-0.00000000 +0.00000000 +0.00000000 +1.28000000 +0.00000000 +0.00000000 +0.00000000 +1.32000000 +0.00000000 +0.00000000 +0.00000000 +1.36000000 +0.00000000 +0.00000000 +0.00000000 +1.40000000 +0.00000000 +0.00000000 +0.00000000 +1.44000000 +0.00000000 +0.00000000 +0.00000000 +1.48000000 +0.00000000 +0.00000000 +0.00000000 +1.52000000 +-0.00000000 +0.00000000 +0.00000000 +1.56000000 +0.00000000 +0.00000000 +0.00000000 +1.60000000 +0.00000000 +0.00000000 +0.00000000 +1.64000000 +0.00000000 +0.00000000 +0.00000000 +1.68000000 +-0.00000000 +0.00000000 +0.00000000 +1.72000000 +0.00000000 +0.00000000 +0.00000000 +1.76000000 +-0.00000000 +0.00000000 +0.00000000 +1.80000000 +0.00000000 +0.00000000 +0.00000000 +1.84000000 +0.00000000 +0.00000000 +0.00000000 +1.88000000 +0.00000000 +0.00000000 +0.00000000 +1.92000000 +-0.00000000 +0.00000000 +0.00000000 +1.96000000 +0.00000000 +0.00000000 +0.00000000 +2.00000000 +0.00000000 +0.00000000 +0.00000000