DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
refine.h
1// Copyright (C) 2010-2024 Garth N. Wells and Paul T. Kühner
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include "dolfinx/common/MPI.h"
10#include "dolfinx/graph/AdjacencyList.h"
11#include "dolfinx/graph/partitioners.h"
12#include "dolfinx/mesh/Mesh.h"
13#include "dolfinx/mesh/Topology.h"
14#include "dolfinx/mesh/cell_types.h"
15#include "dolfinx/mesh/graphbuild.h"
16#include "dolfinx/mesh/utils.h"
17#include "interval.h"
18#include "plaza.h"
19#include <algorithm>
20#include <concepts>
21#include <mpi.h>
22#include <optional>
23#include <spdlog/spdlog.h>
24#include <stdexcept>
25#include <utility>
26#include <variant>
27
28namespace dolfinx::refinement
29{
30
42template <std::floating_point T>
45 std::span<std::int32_t> parent_cell)
46{
47 // TODO: optimize for non ghosted mesh?
48
49 return [&parent_mesh,
50 parent_cell](MPI_Comm comm, int /*nparts*/,
51 std::vector<mesh::CellType> cell_types,
52 std::vector<std::span<const std::int64_t>> cells)
54 {
55 const auto parent_cell_im
56 = parent_mesh.topology()->index_map(parent_mesh.topology()->dim());
57 std::int32_t parent_num_cells = parent_cell_im->size_local();
58 std::span parent_cell_owners = parent_cell_im->owners();
59
60 std::int32_t num_cells
61 = cells.front().size() / mesh::num_cell_vertices(cell_types.front());
62 std::vector<std::int32_t> destinations(num_cells);
63
64 int rank = dolfinx::MPI::rank(comm);
65 for (std::size_t i = 0; i < destinations.size(); i++)
66 {
67 bool parent_is_ghost_cell = parent_cell[i] > parent_num_cells;
68 if (parent_is_ghost_cell)
69 destinations[i] = parent_cell_owners[parent_cell[i] - parent_num_cells];
70 else
71 destinations[i] = rank;
72 }
73
74 if (comm == MPI_COMM_NULL)
75 return graph::regular_adjacency_list(std::move(destinations), 1);
76
77 auto dual_graph = mesh::build_dual_graph(comm, cell_types, cells);
78 std::vector<std::int32_t> node_disp(MPI::size(comm) + 1, 0);
79 std::int32_t local_size = dual_graph.num_nodes();
80 MPI_Allgather(&local_size, 1, dolfinx::MPI::mpi_t<std::int32_t>,
81 node_disp.data() + 1, 1, dolfinx::MPI::mpi_t<std::int32_t>,
82 comm);
83 std::partial_sum(node_disp.begin(), node_disp.end(), node_disp.begin());
84 return compute_destination_ranks(comm, dual_graph, node_disp, destinations);
85 };
86}
87
92
120template <std::floating_point T>
121std::tuple<mesh::Mesh<T>, std::optional<std::vector<std::int32_t>>,
122 std::optional<std::vector<std::int8_t>>>
124 std::optional<std::span<const std::int32_t>> edges,
125 std::variant<IdentityPartitionerPlaceholder, mesh::CellPartitionFunction>
126 partitioner
129{
130 auto topology = mesh.topology();
131 assert(topology);
132 if (!mesh::is_simplex(topology->cell_type()))
133 throw std::runtime_error("Refinement only defined for simplices");
134
135 auto [cell_adj, new_vertex_coords, xshape, parent_cell, parent_facet]
136 = (topology->cell_type() == mesh::CellType::interval)
137 ? interval::compute_refinement_data(mesh, edges, option)
138 : plaza::compute_refinement_data(mesh, edges, option);
139
140 if (std::holds_alternative<IdentityPartitionerPlaceholder>(partitioner))
141 {
142 if (!parent_cell)
143 {
144 throw std::runtime_error(
145 "Identity partitioner relies on parent cell computation");
146 }
147 assert(parent_cell);
148 partitioner = create_identity_partitioner(mesh, parent_cell.value());
149 }
150
151 assert(std::holds_alternative<mesh::CellPartitionFunction>(partitioner));
152
154 mesh.comm(), mesh.comm(), cell_adj.array(), mesh.geometry().cmap(),
155 mesh.comm(), new_vertex_coords, xshape,
156 std::get<mesh::CellPartitionFunction>(partitioner));
157
158 // Report the number of refined cells
159 const int D = topology->dim();
160 const std::int64_t n0 = topology->index_map(D)->size_global();
161 const std::int64_t n1 = mesh1.topology()->index_map(D)->size_global();
162 spdlog::info(
163 "Number of cells increased from {} to {} ({}% increase).", n0, n1,
164 100.0 * (static_cast<double>(n1) / static_cast<double>(n0) - 1.0));
165
166 return {std::move(mesh1), std::move(parent_cell), std::move(parent_facet)};
167}
168
169} // namespace dolfinx::refinement
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:38
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
std::shared_ptr< Topology > topology()
Get mesh topology.
Definition Mesh.h:69
Functions supporting mesh operations.
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:280
int size(MPI_Comm comm)
Definition MPI.cpp:72
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
AdjacencyList< typename std::decay_t< U >::value_type, V > regular_adjacency_list(U &&data, int degree)
Construct a constant degree (valency) adjacency list.
Definition AdjacencyList.h:248
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
int num_cell_vertices(CellType type)
Definition cell_types.cpp:147
bool is_simplex(CellType type)
Definition cell_types.cpp:145
std::function< graph::AdjacencyList< std::int32_t >( MPI_Comm comm, int nparts, const std::vector< CellType > &cell_types, const std::vector< std::span< const std::int64_t > > &cells)> CellPartitionFunction
Signature for the cell partitioning function. Function that implement this interface compute the dest...
Definition utils.h:208
Mesh< typename std::remove_reference_t< typename U::value_type > > create_mesh(MPI_Comm comm, MPI_Comm commt, std::vector< std::span< const std::int64_t > > cells, const std::vector< fem::CoordinateElement< typename std::remove_reference_t< typename U::value_type > > > &elements, MPI_Comm commg, const U &x, std::array< std::size_t, 2 > xshape, const CellPartitionFunction &partitioner, const CellReorderFunction &reorder_fn=graph::reorder_gps)
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1009
graph::AdjacencyList< std::int64_t > build_dual_graph(MPI_Comm comm, std::span< const CellType > celltypes, const std::vector< std::span< const std::int64_t > > &cells, std::optional< std::int32_t > max_facet_to_cell_links=2)
Build distributed mesh dual graph (cell-cell connections via facets) from minimal mesh data.
Definition graphbuild.cpp:613
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< T >, std::array< std::size_t, 2 >, std::optional< std::vector< std::int32_t > >, std::optional< std::vector< std::int8_t > > > compute_refinement_data(const mesh::Mesh< T > &mesh, std::optional< std::span< const std::int32_t > > edges, Option option)
Definition plaza.h:462
Mesh refinement algorithms.
Definition dolfinx_refinement.h:8
std::tuple< mesh::Mesh< T >, std::optional< std::vector< std::int32_t > >, std::optional< std::vector< std::int8_t > > > refine(const mesh::Mesh< T > &mesh, std::optional< std::span< const std::int32_t > > edges, std::variant< IdentityPartitionerPlaceholder, mesh::CellPartitionFunction > partitioner=IdentityPartitionerPlaceholder(), Option option=Option::parent_cell)
Refine a mesh with markers.
Definition refine.h:123
mesh::CellPartitionFunction create_identity_partitioner(const mesh::Mesh< T > &parent_mesh, std::span< std::int32_t > parent_cell)
Create a cell partitioner which maintains the partition of a coarse mesh.
Definition refine.h:44
Option
Options for data to compute during mesh refinement.
Definition option.h:16
@ parent_cell
Definition option.h:20
@ parent_facet
Definition option.h:18
Placeholder for the creation of an identity partitioner in refine.
Definition refine.h:90