| 1 |
/* ------------------------------------------------------------------------ |
| 2 |
* |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
* Copyright (C) 2023 - 2024 by the deal.II authors |
| 5 |
* |
| 6 |
* This file is part of the deal.II library. |
| 7 |
* |
| 8 |
* Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
* LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
* governing the source code and code contributions can be found in |
| 11 |
* LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
* |
| 13 |
* ------------------------------------------------------------------------ |
| 14 |
* |
| 15 |
* |
| 16 |
* Authors: Magdalena Schreter-Fleischhacker, Technical University of |
| 17 |
* Munich, 2023 |
| 18 |
* Peter Munch, University of Augsburg, 2023 |
| 19 |
*/ |
| 20 |
|
| 21 |
// @sect3{Include files} |
| 22 |
// |
| 23 |
// The program starts with including all the relevant header files. |
| 24 |
#include <deal.II/base/conditional_ostream.h> |
| 25 |
#include <deal.II/base/function_lib.h> |
| 26 |
#include <deal.II/base/function_signed_distance.h> |
| 27 |
#include <deal.II/base/mpi.h> |
| 28 |
#include <deal.II/base/mpi.templates.h> |
| 29 |
|
| 30 |
#include <deal.II/distributed/tria.h> |
| 31 |
|
| 32 |
#include <deal.II/dofs/dof_renumbering.h> |
| 33 |
#include <deal.II/dofs/dof_tools.h> |
| 34 |
|
| 35 |
#include <deal.II/fe/fe_nothing.h> |
| 36 |
#include <deal.II/fe/fe_q.h> |
| 37 |
#include <deal.II/fe/fe_system.h> |
| 38 |
#include <deal.II/fe/mapping_q1.h> |
| 39 |
#include <deal.II/fe/mapping_q_cache.h> |
| 40 |
|
| 41 |
#include <deal.II/grid/grid_generator.h> |
| 42 |
#include <deal.II/grid/grid_tools.h> |
| 43 |
|
| 44 |
#include <deal.II/lac/generic_linear_algebra.h> |
| 45 |
|
| 46 |
#include <deal.II/numerics/data_out.h> |
| 47 |
|
| 48 |
#include <iostream> |
| 49 |
#include <fstream> |
| 50 |
|
| 51 |
// The files most relevant for this tutorial are the ones that |
| 52 |
// contain Utilities::MPI::RemotePointEvaluation and the distributed evaluation |
| 53 |
// functions in the VectorTools namespace, which use |
| 54 |
// Utilities::MPI::RemotePointEvaluation. |
| 55 |
#include <deal.II/base/mpi_remote_point_evaluation.h> |
| 56 |
#include <deal.II/numerics/vector_tools.h> |
| 57 |
|
| 58 |
// The following header file provides the class FEPointEvaluation, which allows |
| 59 |
// us to evaluate values of a local solution vector at arbitrary unit points of |
| 60 |
// a cell. |
| 61 |
#include <deal.II/matrix_free/fe_point_evaluation.h> |
| 62 |
|
| 63 |
// We pack everything that is specific for this program into a namespace |
| 64 |
// of its own. |
| 65 |
|
| 66 |
namespace Step87 |
| 67 |
{ |
| 68 |
using namespace dealii; |
| 69 |
|
| 70 |
// @sect3{Utility functions (declaration)} |
| 71 |
// |
| 72 |
// In the following, we declare utility functions that are used |
| 73 |
// in the mini examples below. You find the definitions at the end |
| 74 |
// of the tutorial. |
| 75 |
// |
| 76 |
// The minimum requirement of this tutorial is MPI. If deal.II is built |
| 77 |
// with p4est, we use parallel::distributed::Triangulation as |
| 78 |
// distributed mesh. The class parallel::shared::Triangulation is |
| 79 |
// used if deal.II is built without p4est or if the dimension of the |
| 80 |
// triangulation is 1D, e.g., in the case of codim-1 meshes. |
| 81 |
#ifdef DEAL_II_WITH_P4EST |
| 82 |
template <int dim, int spacedim = dim> |
| 83 |
using DistributedTriangulation = typename std::conditional_t< |
| 84 |
dim == 1, |
| 85 |
parallel::shared::Triangulation<dim, spacedim>, |
| 86 |
parallel::distributed::Triangulation<dim, spacedim>>; |
| 87 |
#else |
| 88 |
template <int dim, int spacedim = dim> |
| 89 |
using DistributedTriangulation = |
| 90 |
parallel::shared::Triangulation<dim, spacedim>; |
| 91 |
#endif |
| 92 |
|
| 93 |
// A list of points along a line is created by definition of a |
| 94 |
// start point @p p0, an end point @p p1, and the number of subdivisions |
| 95 |
// @p n_subdivisions. |
| 96 |
template <int spacedim> |
| 97 |
std::vector<Point<spacedim>> |
| 98 |
create_points_along_line(const Point<spacedim> &p0, |
| 99 |
const Point<spacedim> &p1, |
| 100 |
const unsigned int n_subdivisions); |
| 101 |
|
| 102 |
// A given list of @p points and the corresponding values @p values_0 |
| 103 |
// and @p values_1 (optional) are printed column-wise to a file @p |
| 104 |
// file_name. In addition, the first column represents the distance |
| 105 |
// of the points from the first point. |
| 106 |
template <int spacedim, typename T0, typename T1 = int> |
| 107 |
void print_along_line(const std::string &file_name, |
| 108 |
const std::vector<Point<spacedim>> &points, |
| 109 |
const std::vector<T0> &values_0, |
| 110 |
const std::vector<T1> &values_1 = {}); |
| 111 |
|
| 112 |
// Create a unique list of the real coordinates of support points into |
| 113 |
// @p support_points from the provided Mapping @p mapping and the DoFHandler |
| 114 |
// @p dof_handler. |
| 115 |
template <int dim, int spacedim> |
| 116 |
void collect_support_points( |
| 117 |
const Mapping<dim, spacedim> &mapping, |
| 118 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 119 |
LinearAlgebra::distributed::Vector<double> &support_points); |
| 120 |
|
| 121 |
// From the provided Mapping @p mapping and the DoFHandler @p dof_handler collect |
| 122 |
// the global DoF indices and corresponding support points within a narrow |
| 123 |
// band around the zero-level-set isosurface. Thereto, |
| 124 |
// the value of the finite element function @p signed_distance corresponding to |
| 125 |
// the DoFHandler @p dof_handler_support_points is evaluated at each support point. |
| 126 |
// A support point is only collected if the absolute value is below the value |
| 127 |
// for |
| 128 |
// the @p narrow_band_threshold. |
| 129 |
template <int dim, int spacedim, typename T> |
| 130 |
std::tuple<std::vector<Point<spacedim>>, std::vector<types::global_dof_index>> |
| 131 |
collect_support_points_with_narrow_band( |
| 132 |
const Mapping<dim, spacedim> &mapping, |
| 133 |
const DoFHandler<dim, spacedim> &dof_handler_signed_distance, |
| 134 |
const LinearAlgebra::distributed::Vector<T> &signed_distance, |
| 135 |
const DoFHandler<dim, spacedim> &dof_handler_support_points, |
| 136 |
const double narrow_band_threshold); |
| 137 |
|
| 138 |
// Convert a distributed vector of support points (@p support_points_unrolled) |
| 139 |
// with a sequential order of the coordinates per point into a list of points. |
| 140 |
template <int spacedim> |
| 141 |
std::vector<Point<spacedim>> convert( |
| 142 |
const LinearAlgebra::distributed::Vector<double> &support_points_unrolled); |
| 143 |
|
| 144 |
// @sect3{Mini example 0: Evaluation at given points for a serial mesh} |
| 145 |
// |
| 146 |
// In this introductory example, we demonstrate basic functionalities |
| 147 |
// available in deal.II to evaluate solution quantities at arbitrary |
| 148 |
// points on a serial mesh. The same functionalities are used directly |
| 149 |
// or indirecly in the distributed case to evaluate solution on locally |
| 150 |
// owned cells. This, however, needs to be augmented by communication, |
| 151 |
// as presented in following examples. |
| 152 |
// |
| 153 |
// We first create the typical objects needed |
| 154 |
// for a finite element discretization (defined by mapping, triangulation, |
| 155 |
// and finite element) and a vector containing finite element solution |
| 156 |
// coefficients. |
| 157 |
void example_0() |
| 158 |
{ |
| 159 |
std::cout << "Running: example 0" << std::endl; |
| 160 |
|
| 161 |
constexpr unsigned int dim = 2; |
| 162 |
constexpr unsigned int fe_degree = 3; |
| 163 |
|
| 164 |
const MappingQ1<dim> mapping; |
| 165 |
Triangulation<dim> tria; |
| 166 |
GridGenerator::subdivided_hyper_cube(tria, 5); |
| 167 |
|
| 168 |
const FE_Q<dim> fe(fe_degree); |
| 169 |
DoFHandler<dim> dof_handler(tria); |
| 170 |
dof_handler.distribute_dofs(fe); |
| 171 |
|
| 172 |
Vector<double> vector(dof_handler.n_dofs()); |
| 173 |
VectorTools::interpolate(mapping, |
| 174 |
dof_handler, |
| 175 |
Functions::SignedDistance::Sphere<dim>( |
| 176 |
(dim == 1) ? Point<dim>(0.5) : |
| 177 |
(dim == 2) ? Point<dim>(0.5, 0.5) : |
| 178 |
Point<dim>(0.5, 0.5, 0.5), |
| 179 |
0.25), |
| 180 |
vector); |
| 181 |
|
| 182 |
// We create a list of points inside the domain at which we |
| 183 |
// would like to evaluate the finite element interpolant. |
| 184 |
const auto points_line = |
| 185 |
create_points_along_line((dim == 1) ? Point<dim>(0.0) : |
| 186 |
(dim == 2) ? Point<dim>(0.0, 0.5) : |
| 187 |
Point<dim>(0.0, 0.5, 0.5), |
| 188 |
(dim == 1) ? Point<dim>(1.0) : |
| 189 |
(dim == 2) ? Point<dim>(1.0, 0.5) : |
| 190 |
Point<dim>(1.0, 0.5, 0.5), |
| 191 |
20); |
| 192 |
|
| 193 |
// Now, we loop over all evaluation points. In the first step, we determine |
| 194 |
// via GridTools::find_active_cell_around_point() the cell $K$ that |
| 195 |
// surrounds the point and translate the given real coordinate |
| 196 |
// $\boldsymbol{x}$ to the corresponding coordinate on the unit cell |
| 197 |
// $\hat{\boldsymbol{x}}_K$ according to the provided mapping. |
| 198 |
// The resulting information is printed to the screen. |
| 199 |
std::vector<double> values_line; |
| 200 |
values_line.reserve(points_line.size()); |
| 201 |
|
| 202 |
for (const auto &p_real : points_line) |
| 203 |
{ |
| 204 |
const auto [cell, p_unit] = |
| 205 |
GridTools::find_active_cell_around_point(mapping, |
| 206 |
dof_handler, |
| 207 |
p_real); |
| 208 |
|
| 209 |
{ |
| 210 |
AssertThrow(cell != dof_handler.end(), ExcInternalError()); |
| 211 |
std::cout << " - Found point with real coordinates: " << p_real |
| 212 |
<< std::endl; |
| 213 |
std::cout << " - in cell with vertices:"; |
| 214 |
for (const auto &v : cell->vertex_indices()) |
| 215 |
std::cout << " (" << cell->vertex(v) << ")"; |
| 216 |
std::cout << std::endl; |
| 217 |
std::cout << " - with coordinates on the unit cell: (" << p_unit |
| 218 |
<< ")" << std::endl; |
| 219 |
} |
| 220 |
|
| 221 |
// Having determined $K$ and $\hat{\boldsymbol{x}}_K$, we can |
| 222 |
// perform the evaluation of the finite element solution at this |
| 223 |
// point. In the following, we show three approaches for this |
| 224 |
// purpose. In the first approach, we follow a traditional technique |
| 225 |
// by using FEValues based on a cell-specific quadrature rule |
| 226 |
// consisting of the unit point. |
| 227 |
std::cout << " - Values at point:" << std::endl; |
| 228 |
|
| 229 |
{ |
| 230 |
FEValues<dim> fe_values(mapping, |
| 231 |
fe, |
| 232 |
Quadrature<dim>(p_unit), |
| 233 |
update_values); |
| 234 |
fe_values.reinit(cell); |
| 235 |
|
| 236 |
std::vector<double> quad_values(1); |
| 237 |
fe_values.get_function_values(vector, quad_values); |
| 238 |
const double value_0 = quad_values[0]; |
| 239 |
std::cout << " - " << value_0 << " (w. FEValues)" << std::endl; |
| 240 |
values_line.push_back(value_0); |
| 241 |
} |
| 242 |
|
| 243 |
// The second approach considers FEPointEvaluation, which directly |
| 244 |
// takes a list of unit points for the subsequent evaluation. |
| 245 |
// The class FEPointEvaluation is a class optimized for the evaluation |
| 246 |
// on cell level at arbitrary points and should be favored for such |
| 247 |
// tasks. |
| 248 |
{ |
| 249 |
std::vector<double> cell_vector(fe.n_dofs_per_cell()); |
| 250 |
cell->get_dof_values(vector, cell_vector.begin(), cell_vector.end()); |
| 251 |
|
| 252 |
FEPointEvaluation<1, dim> fe_point(mapping, fe, update_values); |
| 253 |
fe_point.reinit(cell, ArrayView<const Point<dim>>(p_unit)); |
| 254 |
fe_point.evaluate(cell_vector, EvaluationFlags::values); |
| 255 |
const auto value_1 = fe_point.get_value(0); |
| 256 |
std::cout << " - " << value_1 << " (w. FEPointEvaluation)" |
| 257 |
<< std::endl; |
| 258 |
} |
| 259 |
|
| 260 |
// Finally, in the third approach, the function |
| 261 |
// VectorTools::point_value() is considered. It performs both |
| 262 |
// the search of the surrounding cell and the evaluation at the |
| 263 |
// requested point. However, its application is limited |
| 264 |
// to a serial run of the code. |
| 265 |
{ |
| 266 |
const auto value_2 = |
| 267 |
VectorTools::point_value(dof_handler, vector, p_real); |
| 268 |
std::cout << " - " << value_2 << " (w. VectorTools::point_value())" |
| 269 |
<< std::endl; |
| 270 |
std::cout << std::endl; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// We output the requested points together with the corresponding |
| 275 |
// evaluated solution to a CSV file. |
| 276 |
std::cout << " - writing csv file" << std::endl; |
| 277 |
print_along_line("example_0_profile.csv", points_line, values_line); |
| 278 |
} |
| 279 |
|
| 280 |
// Obviously, the code above cannot work for distributed meshes, since |
| 281 |
// the search (which might require communication) is called within a for-loop |
| 282 |
// with loop bounds possibly different on each process. In the following |
| 283 |
// code examples, we present the usage of arbitrary point evaluation |
| 284 |
// in a parallel computation. |
| 285 |
// |
| 286 |
// @sect3{Mini example 1: Evaluation at given points on a distributed mesh} |
| 287 |
// |
| 288 |
// Just like in the introductory example, we evaluate the solution |
| 289 |
// along a line, however, on a distributed mesh. We again start with |
| 290 |
// setting up the objects needed for a finite element discretization. |
| 291 |
void example_1() |
| 292 |
{ |
| 293 |
constexpr unsigned int dim = 2; |
| 294 |
constexpr unsigned int fe_degree = 3; |
| 295 |
|
| 296 |
ConditionalOStream pcout(std::cout, |
| 297 |
Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == |
| 298 |
0); |
| 299 |
|
| 300 |
pcout << "Running: example 1" << std::endl; |
| 301 |
|
| 302 |
const MappingQ1<dim> mapping; |
| 303 |
DistributedTriangulation<dim> tria(MPI_COMM_WORLD); |
| 304 |
GridGenerator::subdivided_hyper_cube(tria, 7); |
| 305 |
|
| 306 |
const FE_Q<dim> fe(fe_degree); |
| 307 |
DoFHandler<dim> dof_handler(tria); |
| 308 |
dof_handler.distribute_dofs(fe); |
| 309 |
|
| 310 |
// We determine a finite element solution representing implicitly |
| 311 |
// the geometry of a sphere with a radius of $r=0.25$ and the center at |
| 312 |
// $(0.5,0.5)$ via a signed distance function. |
| 313 |
LinearAlgebra::distributed::Vector<double> signed_distance; |
| 314 |
signed_distance.reinit(dof_handler.locally_owned_dofs(), |
| 315 |
DoFTools::extract_locally_active_dofs(dof_handler), |
| 316 |
MPI_COMM_WORLD); |
| 317 |
|
| 318 |
VectorTools::interpolate(mapping, |
| 319 |
dof_handler, |
| 320 |
Functions::SignedDistance::Sphere<dim>( |
| 321 |
(dim == 1) ? Point<dim>(0.5) : |
| 322 |
(dim == 2) ? Point<dim>(0.5, 0.5) : |
| 323 |
Point<dim>(0.5, 0.5, 0.5), |
| 324 |
0.25), |
| 325 |
signed_distance); |
| 326 |
|
| 327 |
// Next, we fill a vector from an arbitrary function that should represent |
| 328 |
// a possible finite element solution, which we would like to evaluate. |
| 329 |
LinearAlgebra::distributed::Vector<double> solution; |
| 330 |
solution.reinit(dof_handler.locally_owned_dofs(), |
| 331 |
DoFTools::extract_locally_active_dofs(dof_handler), |
| 332 |
MPI_COMM_WORLD); |
| 333 |
|
| 334 |
VectorTools::interpolate(mapping, |
| 335 |
dof_handler, |
| 336 |
Functions::SignedDistance::Plane<dim>( |
| 337 |
Point<dim>(), Point<dim>::unit_vector(0)), |
| 338 |
solution); |
| 339 |
|
| 340 |
// We create a list of arbitrary (evaluation) points along a horizontal |
| 341 |
// line, which intersects the center of the sphere. We do this only |
| 342 |
// on the root rank, since we intend to output the results to a CSV file |
| 343 |
// by the root rank. |
| 344 |
std::vector<Point<dim>> profile; |
| 345 |
if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0) |
| 346 |
profile = create_points_along_line((dim == 1) ? Point<dim>(0.0) : |
| 347 |
(dim == 2) ? Point<dim>(0.0, 0.5) : |
| 348 |
Point<dim>(0.0, 0.5, 0.5), |
| 349 |
(dim == 1) ? Point<dim>(1.0) : |
| 350 |
(dim == 2) ? Point<dim>(1.0, 0.5) : |
| 351 |
Point<dim>(1.0, 0.5, 0.5), |
| 352 |
20); |
| 353 |
|
| 354 |
// Now, we can evaluate the results, e.g., for the signed distance |
| 355 |
// function at all evaluation points in one go. First, we create a |
| 356 |
// modifiable Utilities::MPI::RemotePointEvaluation object. We use |
| 357 |
// VectorTools::point_values() by specifying the number of components of the |
| 358 |
// solution vector (1 for the present example) as a template parameter. |
| 359 |
// Within this function, the provided object for |
| 360 |
// Utilities::MPI::RemotePointEvaluation is automatically reinitialized with |
| 361 |
// the given points (profile). The ghost values of the solution vector |
| 362 |
// need to be updated from the user. |
| 363 |
Utilities::MPI::RemotePointEvaluation<dim, dim> rpe; |
| 364 |
|
| 365 |
signed_distance.update_ghost_values(); |
| 366 |
const std::vector<double> profile_signed_distance = |
| 367 |
VectorTools::point_values<1>( |
| 368 |
mapping, dof_handler, signed_distance, profile, rpe); |
| 369 |
|
| 370 |
// In addition to VectorTools::point_values(), function gradients can be |
| 371 |
// evaluated via VectorTools::point_gradient(). However, for the computation |
| 372 |
// of user-derived quantities, one might need to fall back to the direct |
| 373 |
// usage of Utilities::MPI::RemotePointEvaluation::evaluate_and_process() or |
| 374 |
// Utilities::MPI::RemotePointEvaluation::process_and_evaluate(). For the |
| 375 |
// sake of demonstration, we use the former to evaluate the values |
| 376 |
// of the solution vector at the requested points. First, we define a |
| 377 |
// lambda function for the operation on the surrounding cells. Using the |
| 378 |
// CellData object, we can create a FEPointEvaluation object to evaluate the |
| 379 |
// solution values at the cell-specific unit coordinates of the requested |
| 380 |
// points. Then, we assign the values to the result vector. |
| 381 |
const auto evaluate_function = [&](const ArrayView<double> &values, |
| 382 |
const auto &cell_data) { |
| 383 |
FEPointEvaluation<1, dim> fe_point(mapping, fe, update_values); |
| 384 |
|
| 385 |
std::vector<double> local_values; |
| 386 |
std::vector<types::global_dof_index> local_dof_indices; |
| 387 |
|
| 388 |
for (const auto cell : cell_data.cell_indices()) |
| 389 |
{ |
| 390 |
const auto cell_dofs = |
| 391 |
cell_data.get_active_cell_iterator(cell)->as_dof_handler_iterator( |
| 392 |
dof_handler); |
| 393 |
|
| 394 |
const auto unit_points = cell_data.get_unit_points(cell); |
| 395 |
const auto local_value = cell_data.get_data_view(cell, values); |
| 396 |
|
| 397 |
local_values.resize(cell_dofs->get_fe().n_dofs_per_cell()); |
| 398 |
cell_dofs->get_dof_values(solution, |
| 399 |
local_values.begin(), |
| 400 |
local_values.end()); |
| 401 |
|
| 402 |
fe_point.reinit(cell_dofs, unit_points); |
| 403 |
fe_point.evaluate(local_values, EvaluationFlags::values); |
| 404 |
|
| 405 |
for (unsigned int q = 0; q < unit_points.size(); ++q) |
| 406 |
local_value[q] = fe_point.get_value(q); |
| 407 |
} |
| 408 |
}; |
| 409 |
|
| 410 |
// The lambda function is passed to |
| 411 |
// Utilities::MPI::RemotePointEvaluation::evaluate_and_process(), where |
| 412 |
// the values are processed accordingly and stored within the created |
| 413 |
// output vector. Again, the ghost values of the vector to be read |
| 414 |
// need to be updated by the user. |
| 415 |
solution.update_ghost_values(); |
| 416 |
|
| 417 |
const std::vector<double> output = |
| 418 |
rpe.evaluate_and_process<double>(evaluate_function); |
| 419 |
|
| 420 |
// Finally, we output all results: the mesh as a VTU file and the |
| 421 |
// results along the line as a CSV file. You can import the CSV file in |
| 422 |
// ParaView and compare the output with the native line plot of |
| 423 |
// ParaView based on the VTU file. |
| 424 |
DataOut<dim> data_out; |
| 425 |
data_out.add_data_vector(dof_handler, signed_distance, "signed_distance"); |
| 426 |
data_out.build_patches(mapping); |
| 427 |
data_out.write_vtu_in_parallel("example_1.vtu", MPI_COMM_WORLD); |
| 428 |
|
| 429 |
if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0) |
| 430 |
{ |
| 431 |
std::cout << " - writing csv file" << std::endl; |
| 432 |
print_along_line("example_1_profile.csv", |
| 433 |
profile, |
| 434 |
profile_signed_distance, |
| 435 |
output); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
// @sect3{Mini example 2: Closest-point evaluation on a distributed mesh} |
| 440 |
// |
| 441 |
// In this mini example, we perform a closest-point projection for each |
| 442 |
// support point of a mesh within a narrow band by iteratively solving for |
| 443 |
// @f[ |
| 444 |
//\boldsymbol{x}^{(i+1)} = \boldsymbol{x}^{(i)} - |
| 445 |
//\boldsymbol{n}(\boldsymbol{x}^{(i)})\phi(\boldsymbol{x}^{(i)}). |
| 446 |
// @f] |
| 447 |
// Once the closest point is determined, we can compute the distance and |
| 448 |
// extrapolate the values from the interface. Note that the demonstrated |
| 449 |
// algorithm does not guarantee that the closest points are collinear |
| 450 |
// (see discussion in @cite coquerelle2016fourth). For the latter, one |
| 451 |
// might also need to perform a tangential correction, which we omit |
| 452 |
// here to keep the discussion concise. |
| 453 |
// |
| 454 |
// We start with creating the objects for the finite element representation |
| 455 |
// of the background mesh. |
| 456 |
void example_2() |
| 457 |
{ |
| 458 |
constexpr unsigned int dim = 2; |
| 459 |
constexpr unsigned int fe_degree = 3; |
| 460 |
|
| 461 |
ConditionalOStream pcout(std::cout, |
| 462 |
Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == |
| 463 |
0); |
| 464 |
|
| 465 |
pcout << "Running: example 2" << std::endl; |
| 466 |
pcout << " - create system" << std::endl; |
| 467 |
|
| 468 |
const FE_Q<dim> fe(fe_degree); |
| 469 |
const MappingQ1<dim> mapping; |
| 470 |
DistributedTriangulation<dim> tria(MPI_COMM_WORLD); |
| 471 |
GridGenerator::subdivided_hyper_cube(tria, 50); |
| 472 |
|
| 473 |
DoFHandler<dim> dof_handler(tria); |
| 474 |
dof_handler.distribute_dofs(fe); |
| 475 |
|
| 476 |
// We compute finite element solution vector, |
| 477 |
// based on an arbitrary function. In addition, a finite element |
| 478 |
// function computed from a signed distance function represents |
| 479 |
// the geometry of a sphere implicitly. |
| 480 |
LinearAlgebra::distributed::Vector<double> solution; |
| 481 |
solution.reinit(dof_handler.locally_owned_dofs(), |
| 482 |
DoFTools::extract_locally_active_dofs(dof_handler), |
| 483 |
MPI_COMM_WORLD); |
| 484 |
|
| 485 |
VectorTools::interpolate(mapping, |
| 486 |
dof_handler, |
| 487 |
Functions::SignedDistance::Plane<dim>( |
| 488 |
Point<dim>(), Point<dim>::unit_vector(0)), |
| 489 |
solution); |
| 490 |
|
| 491 |
LinearAlgebra::distributed::Vector<double> signed_distance; |
| 492 |
signed_distance.reinit(dof_handler.locally_owned_dofs(), |
| 493 |
DoFTools::extract_locally_active_dofs(dof_handler), |
| 494 |
MPI_COMM_WORLD); |
| 495 |
|
| 496 |
VectorTools::interpolate(mapping, |
| 497 |
dof_handler, |
| 498 |
Functions::SignedDistance::Sphere<dim>( |
| 499 |
(dim == 1) ? Point<dim>(0.5) : |
| 500 |
(dim == 2) ? Point<dim>(0.5, 0.5) : |
| 501 |
Point<dim>(0.5, 0.5, 0.5), |
| 502 |
0.25), |
| 503 |
signed_distance); |
| 504 |
signed_distance.update_ghost_values(); |
| 505 |
|
| 506 |
// In the next step, we collect the points in the narrow band around |
| 507 |
// the zero-level-set isosurface for which we would like to perform |
| 508 |
// a closest point projection. To this end, we loop over all support |
| 509 |
// points and collect the coordinates and the DoF indices of those |
| 510 |
// with a maximum distance of 0.1 from the zero-level-set isosurface. |
| 511 |
pcout << " - determine narrow band" << std::endl; |
| 512 |
|
| 513 |
const auto [support_points, support_points_idx] = |
| 514 |
collect_support_points_with_narrow_band(mapping, |
| 515 |
dof_handler, |
| 516 |
signed_distance, |
| 517 |
dof_handler, |
| 518 |
0.1 /*narrow_band_threshold*/); |
| 519 |
|
| 520 |
// For the iterative solution procedure of the closest-point projection, |
| 521 |
// the maximum number of iterations and the tolerance for the maximum |
| 522 |
// absolute acceptable change in the distance in one iteration are set. |
| 523 |
pcout << " - determine closest point iteratively" << std::endl; |
| 524 |
constexpr int max_iter = 30; |
| 525 |
constexpr double tol_distance = 1e-6; |
| 526 |
|
| 527 |
// Now, we are ready to perform the algorithm by setting an initial guess |
| 528 |
// for the projection points simply corresponding to the collected support |
| 529 |
// points. We collect the global indices of the support points and the |
| 530 |
// total number of points that need to be processed and do not |
| 531 |
// fulfill the required tolerance. Those will be gradually reduced |
| 532 |
// upon the iterative process. |
| 533 |
std::vector<Point<dim>> closest_points = support_points; // initial guess |
| 534 |
|
| 535 |
std::vector<unsigned int> unmatched_points_idx(closest_points.size()); |
| 536 |
std::iota(unmatched_points_idx.begin(), unmatched_points_idx.end(), 0); |
| 537 |
|
| 538 |
int n_unmatched_points = |
| 539 |
Utilities::MPI::sum(unmatched_points_idx.size(), MPI_COMM_WORLD); |
| 540 |
|
| 541 |
// Now, we create a Utilities::MPI::RemotePointEvaluation cache object and |
| 542 |
// start the loop for the fix-point iteration. We update the list of points |
| 543 |
// that still need to be processed and subsequently pass this information |
| 544 |
// to the Utilities::MPI::RemotePointEvaluation object. For the sake of |
| 545 |
// illustration, we export the coordinates of the points to be updated for |
| 546 |
// each iteration to a CSV file. Next, we can evaluate the signed distance |
| 547 |
// function and the gradient at those points to update the current solution |
| 548 |
// for the closest points. We perform the update only if the signed |
| 549 |
// distance of the closest point is not already within the tolerance |
| 550 |
// and register those points that still need to be processed. |
| 551 |
Utilities::MPI::RemotePointEvaluation<dim, dim> rpe; |
| 552 |
|
| 553 |
for (int it = 0; it < max_iter && n_unmatched_points > 0; ++it) |
| 554 |
{ |
| 555 |
pcout << " - iteration " << it << ": " << n_unmatched_points; |
| 556 |
|
| 557 |
std::vector<Point<dim>> unmatched_points(unmatched_points_idx.size()); |
| 558 |
for (unsigned int i = 0; i < unmatched_points_idx.size(); ++i) |
| 559 |
unmatched_points[i] = closest_points[unmatched_points_idx[i]]; |
| 560 |
|
| 561 |
const auto all_unmatched_points = |
| 562 |
Utilities::MPI::reduce<std::vector<Point<dim>>>( |
| 563 |
unmatched_points, MPI_COMM_WORLD, [](const auto &a, const auto &b) { |
| 564 |
auto result = a; |
| 565 |
result.insert(result.end(), b.begin(), b.end()); |
| 566 |
return result; |
| 567 |
}); |
| 568 |
|
| 569 |
if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0) |
| 570 |
{ |
| 571 |
std::ofstream file("example_2_" + std::to_string(it) + ".csv"); |
| 572 |
for (const auto &p : all_unmatched_points) |
| 573 |
file << p << std::endl; |
| 574 |
file.close(); |
| 575 |
} |
| 576 |
|
| 577 |
rpe.reinit(unmatched_points, tria, mapping); |
| 578 |
|
| 579 |
AssertThrow(rpe.all_points_found(), |
| 580 |
ExcMessage("Processed point is outside domain.")); |
| 581 |
|
| 582 |
const auto eval_values = |
| 583 |
VectorTools::point_values<1>(rpe, dof_handler, signed_distance); |
| 584 |
|
| 585 |
const auto eval_gradient = |
| 586 |
VectorTools::point_gradients<1>(rpe, dof_handler, signed_distance); |
| 587 |
|
| 588 |
std::vector<unsigned int> unmatched_points_idx_next; |
| 589 |
|
| 590 |
for (unsigned int i = 0; i < unmatched_points_idx.size(); ++i) |
| 591 |
if (std::abs(eval_values[i]) > tol_distance) |
| 592 |
{ |
| 593 |
closest_points[unmatched_points_idx[i]] -= |
| 594 |
eval_values[i] * eval_gradient[i]; |
| 595 |
|
| 596 |
unmatched_points_idx_next.emplace_back(unmatched_points_idx[i]); |
| 597 |
} |
| 598 |
|
| 599 |
unmatched_points_idx.swap(unmatched_points_idx_next); |
| 600 |
|
| 601 |
n_unmatched_points = |
| 602 |
Utilities::MPI::sum(unmatched_points_idx.size(), MPI_COMM_WORLD); |
| 603 |
|
| 604 |
pcout << " -> " << n_unmatched_points << std::endl; |
| 605 |
} |
| 606 |
|
| 607 |
// We print a warning message if we exceed the maximum number of allowed |
| 608 |
// iterations and if there are still projection points with a distance |
| 609 |
// value exceeding the tolerance. |
| 610 |
if (n_unmatched_points > 0) |
| 611 |
pcout << "WARNING: The tolerance of " << n_unmatched_points |
| 612 |
<< " points is not yet attained." << std::endl; |
| 613 |
|
| 614 |
// As a result, we obtain a list of support points and corresponding |
| 615 |
// closest points at the zero-isosurface level set. This information |
| 616 |
// can be used to update the signed distance function, i.e., the |
| 617 |
// reinitialization the values of the level-set function to maintain |
| 618 |
// the signed distance property @cite henri2022geometrical. |
| 619 |
pcout << " - determine distance in narrow band" << std::endl; |
| 620 |
LinearAlgebra::distributed::Vector<double> solution_distance; |
| 621 |
solution_distance.reinit(solution); |
| 622 |
|
| 623 |
for (unsigned int i = 0; i < closest_points.size(); ++i) |
| 624 |
solution_distance[support_points_idx[i]] = |
| 625 |
support_points[i].distance(closest_points[i]); |
| 626 |
|
| 627 |
// In addition, we use the information of the closest point to |
| 628 |
// extrapolate values from the interface, i.e., the zero-level |
| 629 |
// set isosurface, to the support points within the narrow band. |
| 630 |
// This might be helpful to improve accuracy, e.g., for |
| 631 |
// diffuse interface fluxes where certain quantities are only |
| 632 |
// accurately determined at the interface (e.g. curvature |
| 633 |
// for surface tension @cite coquerelle2016fourth). |
| 634 |
pcout << " - perform extrapolation in narrow band" << std::endl; |
| 635 |
rpe.reinit(closest_points, tria, mapping); |
| 636 |
solution.update_ghost_values(); |
| 637 |
const auto vals = VectorTools::point_values<1>(rpe, dof_handler, solution); |
| 638 |
|
| 639 |
LinearAlgebra::distributed::Vector<double> solution_extrapolated; |
| 640 |
solution_extrapolated.reinit(solution); |
| 641 |
|
| 642 |
for (unsigned int i = 0; i < closest_points.size(); ++i) |
| 643 |
solution_extrapolated[support_points_idx[i]] = vals[i]; |
| 644 |
|
| 645 |
// Finally, we output the results to a VTU file. |
| 646 |
pcout << " - output results" << std::endl; |
| 647 |
DataOut<dim> data_out; |
| 648 |
data_out.add_data_vector(dof_handler, signed_distance, "signed_distance"); |
| 649 |
data_out.add_data_vector(dof_handler, solution, "solution"); |
| 650 |
data_out.add_data_vector(dof_handler, |
| 651 |
solution_distance, |
| 652 |
"solution_distance"); |
| 653 |
data_out.add_data_vector(dof_handler, |
| 654 |
solution_extrapolated, |
| 655 |
"solution_extrapolated"); |
| 656 |
data_out.build_patches(mapping); |
| 657 |
data_out.write_vtu_in_parallel("example_2.vtu", MPI_COMM_WORLD); |
| 658 |
|
| 659 |
pcout << std::endl; |
| 660 |
} |
| 661 |
|
| 662 |
// @sect3{Mini example 3: Sharp interface method on the example of surface tension for front tracking} |
| 663 |
// |
| 664 |
// The final mini example presents a basic implementation of |
| 665 |
// front tracking @cite peskin1977numerical, @cite unverdi1992front |
| 666 |
// of a surface mesh $\mathbb{T}_\Gamma$ immersed |
| 667 |
// in a Eulerian background fluid mesh $\mathbb{T}_\Omega$. |
| 668 |
// |
| 669 |
// We assume that the immersed surface is transported according to a |
| 670 |
// prescribed velocity field from the background mesh. Subsequently, |
| 671 |
// we perform a sharp computation of the surface-tension force: |
| 672 |
// @f[ |
| 673 |
// (\boldsymbol v_i (\boldsymbol{x}), \boldsymbol F_S |
| 674 |
// (\boldsymbol{x}))_{\Omega} |
| 675 |
// = |
| 676 |
// \left( \boldsymbol{v}_i (\boldsymbol{x}), \sigma (\boldsymbol{x}) \kappa |
| 677 |
// (\boldsymbol{x}) \boldsymbol{n} (\boldsymbol{x}) \right)_\Gamma \approx |
| 678 |
// \sum_{q\in\mathbb{T}_\Gamma} \boldsymbol{v}_i^T (\boldsymbol{x}_q) |
| 679 |
// \sigma (\boldsymbol{x}_q) \kappa (\boldsymbol{x}_q) \boldsymbol{n} |
| 680 |
// (\boldsymbol{x}_q) |J(\boldsymbol{x}_q)| w(\boldsymbol{x}_q) \quad \forall |
| 681 |
// i\in\mathbb{T}_\Omega |
| 682 |
// . |
| 683 |
// @f] |
| 684 |
// We decompose this operation into two steps. In the first step, we evaluate |
| 685 |
// the force contributions $\sigma (\boldsymbol{x}_q) \kappa |
| 686 |
// (\boldsymbol{x}_q) \boldsymbol{n} |
| 687 |
// (\boldsymbol{x}_q)$ at the quadrature points defined on the immersed mesh |
| 688 |
// and multiply them with the mapped quadrature weight $|J(\boldsymbol{x}_q)| |
| 689 |
// w_q$: |
| 690 |
// @f[ |
| 691 |
// \boldsymbol{F}_S (\boldsymbol{x}_q) \gets \sigma (\boldsymbol{x}_q) \kappa |
| 692 |
// (\boldsymbol{x}_q) \boldsymbol{n} (\boldsymbol{x}_q) |J(\boldsymbol{x}_q)| |
| 693 |
// w_q \quad \forall q\in\mathbb{T}_\Gamma. |
| 694 |
// @f] |
| 695 |
// In the second step, we compute the discretized weak form by multiplying |
| 696 |
// with test functions on the background mesh: |
| 697 |
// @f[ |
| 698 |
// (\boldsymbol v_i (\boldsymbol{x}), \boldsymbol F_S |
| 699 |
// (\boldsymbol{x}))_{\Omega} \gets \sum_q \boldsymbol{v}_i^T |
| 700 |
// (\boldsymbol{x}_q) \boldsymbol{F}_S |
| 701 |
// (\boldsymbol{x}_q) |
| 702 |
// \quad \forall i\in\mathbb{T}_\Omega |
| 703 |
// . |
| 704 |
// @f] |
| 705 |
// Obviously, we need to communicate between the two steps. The second step |
| 706 |
// can be handled completely by Utilities::MPI::RemotePointEvaluation, which |
| 707 |
// provides the function |
| 708 |
// Utilities::MPI::RemotePointEvaluation::process_and_evaluate() for this |
| 709 |
// purpose. |
| 710 |
// |
| 711 |
// We start with setting the parameters consisting of the polynomial degree of |
| 712 |
// the shape functions, the dimension of the background mesh, the time-step |
| 713 |
// size to be considered for transporting the surface mesh and the number of |
| 714 |
// time steps. |
| 715 |
|
| 716 |
void example_3() |
| 717 |
{ |
| 718 |
constexpr unsigned int degree = 3; |
| 719 |
constexpr unsigned int dim = 2; |
| 720 |
const double dt = 0.01; |
| 721 |
const unsigned int n_time_steps = 200; |
| 722 |
|
| 723 |
// This program is intended to be executed in 2D or 3D. |
| 724 |
static_assert(dim == 2 || dim == 3, "Only implemented for 2D or 3D."); |
| 725 |
|
| 726 |
ConditionalOStream pcout(std::cout, |
| 727 |
Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == |
| 728 |
0); |
| 729 |
|
| 730 |
pcout << "Running: example 3" << std::endl; |
| 731 |
|
| 732 |
// Next, we create the standard objects necessary for the finite element |
| 733 |
// representation of the background mesh |
| 734 |
pcout << " - creating background mesh" << std::endl; |
| 735 |
DistributedTriangulation<dim> tria_background(MPI_COMM_WORLD); |
| 736 |
GridGenerator::hyper_cube(tria_background); |
| 737 |
tria_background.refine_global(5); |
| 738 |
|
| 739 |
const MappingQ1<dim> mapping_background; |
| 740 |
const FESystem<dim> fe_background(FE_Q<dim>(degree), dim); |
| 741 |
DoFHandler<dim> dof_handler_background(tria_background); |
| 742 |
dof_handler_background.distribute_dofs(fe_background); |
| 743 |
|
| 744 |
// and, similarly, for the immersed surface mesh. |
| 745 |
// We use a sphere with radius $r=0.75$ which is |
| 746 |
// placed in the center of the top half of the cubic background domain. |
| 747 |
pcout << " - creating immersed mesh" << std::endl; |
| 748 |
const Point<dim> center((dim == 2) ? Point<dim>(0.5, 0.75) : |
| 749 |
Point<dim>(0.5, 0.75, 0.5)); |
| 750 |
const double radius = 0.15; |
| 751 |
|
| 752 |
DistributedTriangulation<dim - 1, dim> tria_immersed(MPI_COMM_WORLD); |
| 753 |
GridGenerator::hyper_sphere(tria_immersed, center, radius); |
| 754 |
tria_immersed.refine_global(4); |
| 755 |
|
| 756 |
// Two different mappings are considered for the immersed |
| 757 |
// surface mesh: one valid for the initial configuration and one |
| 758 |
// that is updated in every time step according to the nodal |
| 759 |
// displacements. Two types of finite elements are used to |
| 760 |
// represent scalar and vector-valued DoF values. |
| 761 |
const MappingQ<dim - 1, dim> mapping_immersed_base(3); |
| 762 |
MappingQCache<dim - 1, dim> mapping_immersed(3); |
| 763 |
mapping_immersed.initialize(mapping_immersed_base, tria_immersed); |
| 764 |
const QGauss<dim - 1> quadrature_immersed(degree + 1); |
| 765 |
|
| 766 |
const FE_Q<dim - 1, dim> fe_scalar_immersed(degree); |
| 767 |
const FESystem<dim - 1, dim> fe_immersed(fe_scalar_immersed, dim); |
| 768 |
DoFHandler<dim - 1, dim> dof_handler_immersed(tria_immersed); |
| 769 |
dof_handler_immersed.distribute_dofs(fe_immersed); |
| 770 |
|
| 771 |
// We renumber the DoFs related to the vector-valued problem to |
| 772 |
// simplify access to the individual components. |
| 773 |
DoFRenumbering::support_point_wise(dof_handler_immersed); |
| 774 |
|
| 775 |
// We fill a DoF vector on the background mesh with an analytical |
| 776 |
// velocity field considering the Rayleigh-Kothe vortex flow and |
| 777 |
// initialize a DoF vector for the weak form of the surface-tension force. |
| 778 |
LinearAlgebra::distributed::Vector<double> velocity; |
| 779 |
velocity.reinit(dof_handler_background.locally_owned_dofs(), |
| 780 |
DoFTools::extract_locally_active_dofs( |
| 781 |
dof_handler_background), |
| 782 |
MPI_COMM_WORLD); |
| 783 |
Functions::RayleighKotheVortex<dim> vortex(2); |
| 784 |
|
| 785 |
LinearAlgebra::distributed::Vector<double> force_vector( |
| 786 |
dof_handler_background.locally_owned_dofs(), |
| 787 |
DoFTools::extract_locally_active_dofs(dof_handler_background), |
| 788 |
MPI_COMM_WORLD); |
| 789 |
|
| 790 |
// Next, we collect the real positions $\boldsymbol{x}_q$ of the quadrature |
| 791 |
// points of the surface mesh in a vector. |
| 792 |
LinearAlgebra::distributed::Vector<double> immersed_support_points; |
| 793 |
collect_support_points(mapping_immersed, |
| 794 |
dof_handler_immersed, |
| 795 |
immersed_support_points); |
| 796 |
|
| 797 |
// We initialize a Utilities::MPI::RemotePointEvaluation object and start |
| 798 |
// the time loop. For any other step than the initial one, we first move the |
| 799 |
// support points of the surface mesh according to the fluid velocity of the |
| 800 |
// background mesh. Thereto, we first update the time of the velocity |
| 801 |
// function. Then, we update the internal data structures of the |
| 802 |
// Utilities::MPI::RemotePointEvaluation object with the collected support |
| 803 |
// points of the immersed mesh. We throw an exception if one of the points |
| 804 |
// cannot be found within the domain of the background mesh. Next, we |
| 805 |
// evaluate the velocity at the surface-mesh support points and compute the |
| 806 |
// resulting update of the coordinates. Finally, we update the mapping of |
| 807 |
// the immersed surface mesh to the current position. |
| 808 |
Utilities::MPI::RemotePointEvaluation<dim> rpe; |
| 809 |
double time = 0.0; |
| 810 |
for (unsigned int it = 0; it <= n_time_steps; ++it, time += dt) |
| 811 |
{ |
| 812 |
pcout << "time: " << time << std::endl; |
| 813 |
if (it > 0) |
| 814 |
{ |
| 815 |
pcout << " - move support points (immersed mesh)" << std::endl; |
| 816 |
vortex.set_time(time); |
| 817 |
VectorTools::interpolate(mapping_background, |
| 818 |
dof_handler_background, |
| 819 |
vortex, |
| 820 |
velocity); |
| 821 |
rpe.reinit(convert<dim>(immersed_support_points), |
| 822 |
tria_background, |
| 823 |
mapping_background); |
| 824 |
|
| 825 |
AssertThrow(rpe.all_points_found(), |
| 826 |
ExcMessage( |
| 827 |
"Immersed domain leaves background domain!")); |
| 828 |
|
| 829 |
velocity.update_ghost_values(); |
| 830 |
const auto immersed_velocity = |
| 831 |
VectorTools::point_values<dim>(rpe, |
| 832 |
dof_handler_background, |
| 833 |
velocity); |
| 834 |
velocity.zero_out_ghost_values(); |
| 835 |
|
| 836 |
for (unsigned int i = 0, c = 0; |
| 837 |
i < immersed_support_points.locally_owned_size() / dim; |
| 838 |
++i) |
| 839 |
for (unsigned int d = 0; d < dim; ++d, ++c) |
| 840 |
immersed_support_points.local_element(c) += |
| 841 |
immersed_velocity[i][d] * dt; |
| 842 |
|
| 843 |
mapping_immersed.initialize(mapping_immersed_base, |
| 844 |
dof_handler_immersed, |
| 845 |
immersed_support_points, |
| 846 |
false); |
| 847 |
} |
| 848 |
|
| 849 |
// Next, we loop over all locally owned cells of the immersed mesh and |
| 850 |
// its quadrature points to compute the value for the local surface |
| 851 |
// tension force contribution $\boldsymbol{F}_S(\boldsymbol{x}_q)$. We |
| 852 |
// store the real coordinates of the quadrature points and the |
| 853 |
// corresponding force contributions in two individual vectors. For |
| 854 |
// computation of the latter, the normal vector |
| 855 |
// $\boldsymbol{n}(\boldsymbol{x}_q)$ can be directly extracted from the |
| 856 |
// surface mesh via FEValues and, for the curvature, we use the |
| 857 |
// following approximation: |
| 858 |
// @f[ |
| 859 |
// \kappa(\boldsymbol{x}_q) |
| 860 |
// = |
| 861 |
// \nabla \cdot \boldsymbol{n}(\boldsymbol{x}_q) |
| 862 |
// = |
| 863 |
// \text{tr}\left({\nabla \boldsymbol{n}(\boldsymbol{x}_q)}\right) |
| 864 |
// \approx |
| 865 |
// \text{tr}\left({\nabla \sum_i \boldsymbol{N}_i (\boldsymbol{x}_q) |
| 866 |
// \boldsymbol n_i}\right) |
| 867 |
// = |
| 868 |
// \sum_i\text{tr}\left({\nabla \boldsymbol{N}_i (\boldsymbol{x}_q) |
| 869 |
// \boldsymbol n_i}\right) |
| 870 |
// \;\text{with}\; i\in[0,n_{\text{dofs\_per\_cell}}), |
| 871 |
// @f] |
| 872 |
// which we can apply since the immersed mesh is consistently |
| 873 |
// orientated. The surface tension coefficient is set to 1 for the |
| 874 |
// sake of demonstration. |
| 875 |
pcout << " - compute to be tested values (immersed mesh)" << std::endl; |
| 876 |
using value_type = Tensor<1, dim, double>; |
| 877 |
|
| 878 |
std::vector<Point<dim>> integration_points; |
| 879 |
std::vector<value_type> integration_values; |
| 880 |
|
| 881 |
FEValues<dim - 1, dim> fe_values(mapping_immersed, |
| 882 |
fe_immersed, |
| 883 |
quadrature_immersed, |
| 884 |
update_JxW_values | update_gradients | |
| 885 |
update_normal_vectors | |
| 886 |
update_quadrature_points); |
| 887 |
|
| 888 |
FEValues<dim - 1, dim> fe_values_co( |
| 889 |
mapping_immersed, |
| 890 |
fe_scalar_immersed, |
| 891 |
fe_scalar_immersed.get_unit_support_points(), |
| 892 |
update_JxW_values | update_normal_vectors); |
| 893 |
|
| 894 |
std::vector<unsigned int> component_to_system_index( |
| 895 |
fe_immersed.n_dofs_per_cell()); |
| 896 |
|
| 897 |
for (unsigned int i = 0, c = 0; |
| 898 |
i < fe_scalar_immersed.n_dofs_per_cell(); |
| 899 |
++i) |
| 900 |
for (unsigned int d = 0; d < dim; ++d, ++c) |
| 901 |
component_to_system_index[c] = |
| 902 |
fe_immersed.component_to_system_index(d, i); |
| 903 |
|
| 904 |
for (const auto &cell : tria_immersed.active_cell_iterators() | |
| 905 |
IteratorFilters::LocallyOwnedCell()) |
| 906 |
{ |
| 907 |
fe_values.reinit(cell); |
| 908 |
fe_values_co.reinit(cell); |
| 909 |
|
| 910 |
for (const auto &q : fe_values.quadrature_point_indices()) |
| 911 |
{ |
| 912 |
integration_points.emplace_back(fe_values.quadrature_point(q)); |
| 913 |
|
| 914 |
const auto sigma = 1.0; // surface tension coefficient |
| 915 |
|
| 916 |
const auto normal = fe_values.normal_vector(q); |
| 917 |
double curvature = 0; |
| 918 |
for (unsigned int i = 0, c = 0; |
| 919 |
i < fe_scalar_immersed.n_dofs_per_cell(); |
| 920 |
++i) |
| 921 |
for (unsigned int d = 0; d < dim; ++d, ++c) |
| 922 |
curvature += fe_values.shape_grad_component( |
| 923 |
component_to_system_index[c], q, d)[d] * |
| 924 |
fe_values_co.normal_vector(i)[d]; |
| 925 |
|
| 926 |
const auto FxJxW = |
| 927 |
sigma * curvature * normal * fe_values.JxW(q); |
| 928 |
|
| 929 |
integration_values.emplace_back(FxJxW); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
// Before we evaluate the weak form of the surface-tension force, the |
| 934 |
// communication pattern of Utilities::MPI::RemotePointEvaluation is |
| 935 |
// set up from the quadrature points of the immersed mesh, determining |
| 936 |
// the surrounding cells on the background mesh. |
| 937 |
pcout << " - test values (background mesh)" << std::endl; |
| 938 |
|
| 939 |
rpe.reinit(integration_points, tria_background, mapping_background); |
| 940 |
|
| 941 |
// In preparation for utilizing |
| 942 |
// Utilities::MPI::RemotePointEvaluation::process_and_evaluate that |
| 943 |
// performs the |
| 944 |
// multiplication with the test function, we set up a callback function |
| 945 |
// that contains the operation on the intersected cells of the |
| 946 |
// background mesh. Within this function, we initialize a |
| 947 |
// FEPointEvaluation object that allows us to integrate values at |
| 948 |
// arbitrary points within a cell. We loop over the cells that surround |
| 949 |
// quadrature points of the immersed mesh -- provided by the callback |
| 950 |
// function. From the provided CellData object, we retrieve the unit |
| 951 |
// points, i.e., the quadrature points of the immersed mesh that lie |
| 952 |
// within the current cell and a pointer to the stored values on the |
| 953 |
// current cell (local surface-tension force) for convenience. We |
| 954 |
// reinitialize the data structure of FEPointEvaluation on every cell |
| 955 |
// according to the unit points. Next, we loop over the quadrature |
| 956 |
// points attributed to the cell and submit the local surface-tension |
| 957 |
// force to the FEPointEvaluation object. Via |
| 958 |
// FEPointEvaluation::test_and_sum(), the submitted values are |
| 959 |
// multiplied by the values of the test function and a summation over |
| 960 |
// all given points is performed. Subsequently, the contributions are |
| 961 |
// assembled into the global vector containing the weak form of the |
| 962 |
// surface-tension force. |
| 963 |
const auto integration_function = [&](const auto &values, |
| 964 |
const auto &cell_data) { |
| 965 |
FEPointEvaluation<dim, dim> phi_force(mapping_background, |
| 966 |
fe_background, |
| 967 |
update_values); |
| 968 |
|
| 969 |
std::vector<double> local_values; |
| 970 |
std::vector<types::global_dof_index> local_dof_indices; |
| 971 |
|
| 972 |
for (const auto cell : cell_data.cell_indices()) |
| 973 |
{ |
| 974 |
const auto cell_dofs = |
| 975 |
cell_data.get_active_cell_iterator(cell) |
| 976 |
->as_dof_handler_iterator(dof_handler_background); |
| 977 |
|
| 978 |
const auto unit_points = cell_data.get_unit_points(cell); |
| 979 |
const auto FxJxW = cell_data.get_data_view(cell, values); |
| 980 |
|
| 981 |
phi_force.reinit(cell_dofs, unit_points); |
| 982 |
|
| 983 |
for (const auto q : phi_force.quadrature_point_indices()) |
| 984 |
phi_force.submit_value(FxJxW[q], q); |
| 985 |
|
| 986 |
local_values.resize(cell_dofs->get_fe().n_dofs_per_cell()); |
| 987 |
phi_force.test_and_sum(local_values, EvaluationFlags::values); |
| 988 |
|
| 989 |
local_dof_indices.resize(cell_dofs->get_fe().n_dofs_per_cell()); |
| 990 |
cell_dofs->get_dof_indices(local_dof_indices); |
| 991 |
AffineConstraints<double>().distribute_local_to_global( |
| 992 |
local_values, local_dof_indices, force_vector); |
| 993 |
} |
| 994 |
}; |
| 995 |
|
| 996 |
// The callback function is passed together with the vector holding the |
| 997 |
// surface-tension force contribution at each quadrature point of the |
| 998 |
// immersed mesh to |
| 999 |
// Utilities::MPI::RemotePointEvaluation::process_and_evaluate. The only |
| 1000 |
// missing step is to compress the distributed force vector. |
| 1001 |
rpe.process_and_evaluate<value_type>(integration_values, |
| 1002 |
integration_function); |
| 1003 |
force_vector.compress(VectorOperation::add); |
| 1004 |
|
| 1005 |
// After every tenth step or at the beginning/end of the time loop, we |
| 1006 |
// output the force vector and the velocity of the background mesh to |
| 1007 |
// a VTU file. In addition, we also export the geometry of the |
| 1008 |
// (deformed) immersed surface mesh to a separate VTU file. |
| 1009 |
if (it % 10 == 0 || it == n_time_steps) |
| 1010 |
{ |
| 1011 |
std::vector< |
| 1012 |
DataComponentInterpretation::DataComponentInterpretation> |
| 1013 |
vector_component_interpretation( |
| 1014 |
dim, DataComponentInterpretation::component_is_part_of_vector); |
| 1015 |
pcout << " - write data (background mesh)" << std::endl; |
| 1016 |
DataOut<dim> data_out_background; |
| 1017 |
DataOutBase::VtkFlags flags_background; |
| 1018 |
flags_background.write_higher_order_cells = true; |
| 1019 |
data_out_background.set_flags(flags_background); |
| 1020 |
data_out_background.add_data_vector( |
| 1021 |
dof_handler_background, |
| 1022 |
force_vector, |
| 1023 |
std::vector<std::string>(dim, "force"), |
| 1024 |
vector_component_interpretation); |
| 1025 |
data_out_background.add_data_vector( |
| 1026 |
dof_handler_background, |
| 1027 |
velocity, |
| 1028 |
std::vector<std::string>(dim, "velocity"), |
| 1029 |
vector_component_interpretation); |
| 1030 |
data_out_background.build_patches(mapping_background, 3); |
| 1031 |
data_out_background.write_vtu_in_parallel("example_3_background_" + |
| 1032 |
std::to_string(it) + |
| 1033 |
".vtu", |
| 1034 |
MPI_COMM_WORLD); |
| 1035 |
|
| 1036 |
pcout << " - write mesh (immersed mesh)" << std::endl; |
| 1037 |
DataOut<dim - 1, dim> data_out_immersed; |
| 1038 |
data_out_immersed.attach_triangulation(tria_immersed); |
| 1039 |
data_out_immersed.build_patches(mapping_immersed, 3); |
| 1040 |
data_out_immersed.write_vtu_in_parallel("example_3_immersed_" + |
| 1041 |
std::to_string(it) + |
| 1042 |
".vtu", |
| 1043 |
MPI_COMM_WORLD); |
| 1044 |
} |
| 1045 |
pcout << std::endl; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
// @sect3{Utility functions (definition)} |
| 1050 |
template <int spacedim> |
| 1051 |
std::vector<Point<spacedim>> |
| 1052 |
create_points_along_line(const Point<spacedim> &p0, |
| 1053 |
const Point<spacedim> &p1, |
| 1054 |
const unsigned int n_subdivisions) |
| 1055 |
{ |
| 1056 |
Assert(n_subdivisions >= 1, ExcInternalError()); |
| 1057 |
|
| 1058 |
std::vector<Point<spacedim>> points; |
| 1059 |
points.reserve(n_subdivisions + 1); |
| 1060 |
|
| 1061 |
points.emplace_back(p0); |
| 1062 |
for (unsigned int i = 1; i < n_subdivisions; ++i) |
| 1063 |
points.emplace_back(p0 + (p1 - p0) * static_cast<double>(i) / |
| 1064 |
static_cast<double>(n_subdivisions)); |
| 1065 |
points.emplace_back(p1); |
| 1066 |
|
| 1067 |
return points; |
| 1068 |
} |
| 1069 |
|
| 1070 |
template <int spacedim, typename T0, typename T1> |
| 1071 |
void print_along_line(const std::string &file_name, |
| 1072 |
const std::vector<Point<spacedim>> &points, |
| 1073 |
const std::vector<T0> &values_0, |
| 1074 |
const std::vector<T1> &values_1) |
| 1075 |
{ |
| 1076 |
AssertThrow(points.size() == values_0.size() && |
| 1077 |
(values_1.size() == points.size() || values_1.empty()), |
| 1078 |
ExcMessage("The provided vectors must have the same length.")); |
| 1079 |
|
| 1080 |
std::ofstream file(file_name); |
| 1081 |
|
| 1082 |
for (unsigned int i = 0; i < points.size(); ++i) |
| 1083 |
{ |
| 1084 |
file << std::fixed << std::right << std::setw(5) << std::setprecision(3) |
| 1085 |
<< points[0].distance(points[i]); |
| 1086 |
|
| 1087 |
for (unsigned int d = 0; d < spacedim; ++d) |
| 1088 |
file << std::fixed << std::right << std::setw(10) |
| 1089 |
<< std::setprecision(3) << points[i][d]; |
| 1090 |
|
| 1091 |
file << std::fixed << std::right << std::setw(10) |
| 1092 |
<< std::setprecision(3) << values_0[i]; |
| 1093 |
|
| 1094 |
if (!values_1.empty()) |
| 1095 |
file << std::fixed << std::right << std::setw(10) |
| 1096 |
<< std::setprecision(3) << values_1[i]; |
| 1097 |
file << std::endl; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
template <int dim, int spacedim> |
| 1102 |
void collect_support_points( |
| 1103 |
const Mapping<dim, spacedim> &mapping, |
| 1104 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1105 |
LinearAlgebra::distributed::Vector<double> &support_points) |
| 1106 |
{ |
| 1107 |
support_points.reinit(dof_handler.locally_owned_dofs(), |
| 1108 |
DoFTools::extract_locally_active_dofs(dof_handler), |
| 1109 |
dof_handler.get_mpi_communicator()); |
| 1110 |
|
| 1111 |
const auto &fe = dof_handler.get_fe(); |
| 1112 |
|
| 1113 |
FEValues<dim, spacedim> fe_values( |
| 1114 |
mapping, |
| 1115 |
fe, |
| 1116 |
fe.base_element(0).get_unit_support_points(), |
| 1117 |
update_quadrature_points); |
| 1118 |
|
| 1119 |
std::vector<types::global_dof_index> local_dof_indices( |
| 1120 |
fe.n_dofs_per_cell()); |
| 1121 |
|
| 1122 |
std::vector<unsigned int> component_to_system_index( |
| 1123 |
fe_values.n_quadrature_points * spacedim); |
| 1124 |
|
| 1125 |
for (unsigned int q = 0, c = 0; q < fe_values.n_quadrature_points; ++q) |
| 1126 |
for (unsigned int d = 0; d < spacedim; ++d, ++c) |
| 1127 |
component_to_system_index[c] = fe.component_to_system_index(d, q); |
| 1128 |
|
| 1129 |
for (const auto &cell : dof_handler.active_cell_iterators() | |
| 1130 |
IteratorFilters::LocallyOwnedCell()) |
| 1131 |
{ |
| 1132 |
fe_values.reinit(cell); |
| 1133 |
cell->get_dof_indices(local_dof_indices); |
| 1134 |
|
| 1135 |
for (unsigned int q = 0, c = 0; q < fe_values.n_quadrature_points; ++q) |
| 1136 |
for (unsigned int d = 0; d < spacedim; ++d, ++c) |
| 1137 |
support_points[local_dof_indices[component_to_system_index[c]]] = |
| 1138 |
fe_values.quadrature_point(q)[d]; |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
template <int dim, int spacedim, typename T> |
| 1143 |
std::tuple<std::vector<Point<spacedim>>, std::vector<types::global_dof_index>> |
| 1144 |
collect_support_points_with_narrow_band( |
| 1145 |
const Mapping<dim, spacedim> &mapping, |
| 1146 |
const DoFHandler<dim, spacedim> &dof_handler_signed_distance, |
| 1147 |
const LinearAlgebra::distributed::Vector<T> &signed_distance, |
| 1148 |
const DoFHandler<dim, spacedim> &dof_handler_support_points, |
| 1149 |
const double narrow_band_threshold) |
| 1150 |
{ |
| 1151 |
AssertThrow(narrow_band_threshold >= 0, |
| 1152 |
ExcMessage("The narrow band threshold" |
| 1153 |
" must be larger than or equal to 0.")); |
| 1154 |
const auto &tria = dof_handler_signed_distance.get_triangulation(); |
| 1155 |
const Quadrature<dim> quad(dof_handler_support_points.get_fe() |
| 1156 |
.base_element(0) |
| 1157 |
.get_unit_support_points()); |
| 1158 |
|
| 1159 |
FEValues<dim> distance_values(mapping, |
| 1160 |
dof_handler_signed_distance.get_fe(), |
| 1161 |
quad, |
| 1162 |
update_values); |
| 1163 |
|
| 1164 |
FEValues<dim> req_values(mapping, |
| 1165 |
dof_handler_support_points.get_fe(), |
| 1166 |
quad, |
| 1167 |
update_quadrature_points); |
| 1168 |
|
| 1169 |
std::vector<T> temp_distance(quad.size()); |
| 1170 |
std::vector<types::global_dof_index> local_dof_indices( |
| 1171 |
dof_handler_support_points.get_fe().n_dofs_per_cell()); |
| 1172 |
|
| 1173 |
std::vector<Point<dim>> support_points; |
| 1174 |
std::vector<types::global_dof_index> support_points_idx; |
| 1175 |
|
| 1176 |
const bool has_ghost_elements = signed_distance.has_ghost_elements(); |
| 1177 |
|
| 1178 |
const auto &locally_owned_dofs_req = |
| 1179 |
dof_handler_support_points.locally_owned_dofs(); |
| 1180 |
std::vector<bool> flags(locally_owned_dofs_req.n_elements(), false); |
| 1181 |
|
| 1182 |
if (has_ghost_elements == false) |
| 1183 |
signed_distance.update_ghost_values(); |
| 1184 |
|
| 1185 |
for (const auto &cell : |
| 1186 |
tria.active_cell_iterators() | IteratorFilters::LocallyOwnedCell()) |
| 1187 |
{ |
| 1188 |
const auto cell_distance = |
| 1189 |
cell->as_dof_handler_iterator(dof_handler_signed_distance); |
| 1190 |
distance_values.reinit(cell_distance); |
| 1191 |
distance_values.get_function_values(signed_distance, temp_distance); |
| 1192 |
|
| 1193 |
const auto cell_req = |
| 1194 |
cell->as_dof_handler_iterator(dof_handler_support_points); |
| 1195 |
req_values.reinit(cell_req); |
| 1196 |
cell_req->get_dof_indices(local_dof_indices); |
| 1197 |
|
| 1198 |
for (const auto q : req_values.quadrature_point_indices()) |
| 1199 |
if (std::abs(temp_distance[q]) < narrow_band_threshold) |
| 1200 |
{ |
| 1201 |
const auto idx = local_dof_indices[q]; |
| 1202 |
|
| 1203 |
if (locally_owned_dofs_req.is_element(idx) == false || |
| 1204 |
flags[locally_owned_dofs_req.index_within_set(idx)]) |
| 1205 |
continue; |
| 1206 |
|
| 1207 |
flags[locally_owned_dofs_req.index_within_set(idx)] = true; |
| 1208 |
|
| 1209 |
support_points_idx.emplace_back(idx); |
| 1210 |
support_points.emplace_back(req_values.quadrature_point(q)); |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (has_ghost_elements == false) |
| 1215 |
signed_distance.zero_out_ghost_values(); |
| 1216 |
|
| 1217 |
return {support_points, support_points_idx}; |
| 1218 |
} |
| 1219 |
|
| 1220 |
template <int spacedim> |
| 1221 |
std::vector<Point<spacedim>> convert( |
| 1222 |
const LinearAlgebra::distributed::Vector<double> &support_points_unrolled) |
| 1223 |
{ |
| 1224 |
const unsigned int n_points = |
| 1225 |
support_points_unrolled.locally_owned_size() / spacedim; |
| 1226 |
|
| 1227 |
std::vector<Point<spacedim>> points(n_points); |
| 1228 |
|
| 1229 |
for (unsigned int i = 0, c = 0; i < n_points; ++i) |
| 1230 |
for (unsigned int d = 0; d < spacedim; ++d, ++c) |
| 1231 |
points[i][d] = support_points_unrolled.local_element(c); |
| 1232 |
|
| 1233 |
return points; |
| 1234 |
} |
| 1235 |
|
| 1236 |
} // namespace Step87 |
| 1237 |
|
| 1238 |
// @sect3{Driver} |
| 1239 |
// |
| 1240 |
// Finally, the driver of the program executes the four mini examples. |
| 1241 |
int main(int argc, char **argv) |
| 1242 |
{ |
| 1243 |
using namespace dealii; |
| 1244 |
Utilities::MPI::MPI_InitFinalize mpi(argc, argv, 1); |
| 1245 |
std::cout.precision(5); |
| 1246 |
|
| 1247 |
if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0) |
| 1248 |
Step87::example_0(); // only run on root process |
| 1249 |
|
| 1250 |
Step87::example_1(); |
| 1251 |
Step87::example_2(); |
| 1252 |
Step87::example_3(); |
| 1253 |
} |