DOLFINx 0.10.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
utils.h
1// Copyright (C) 2012-2020 Chris Richardson
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 <array>
10#include <concepts>
11#include <cstdint>
12#include <dolfinx/common/MPI.h>
13#include <dolfinx/graph/AdjacencyList.h>
14#include <dolfinx/mesh/Mesh.h>
15#include <dolfinx/mesh/utils.h>
16#include <map>
17#include <memory>
18#include <set>
19#include <span>
20#include <tuple>
21#include <vector>
22
23namespace dolfinx::mesh
24{
25template <typename T>
27class Topology;
28enum class GhostMode;
29} // namespace dolfinx::mesh
30
31namespace dolfinx::common
32{
33class IndexMap;
34} // namespace dolfinx::common
35
36namespace dolfinx::refinement
37{
38namespace impl
39{
41std::int64_t local_to_global(std::int32_t local_index,
42 const common::IndexMap& map);
43
53template <std::floating_point T>
54std::pair<std::vector<T>, std::array<std::size_t, 2>> create_new_geometry(
55 const mesh::Mesh<T>& mesh,
56 const std::map<std::int32_t, std::int64_t>& local_edge_to_new_vertex)
57{
58 // Build map from vertex -> geometry dof
59 auto x_dofmap = mesh.geometry().dofmap();
60 const int tdim = mesh.topology()->dim();
61 auto c_to_v = mesh.topology()->connectivity(tdim, 0);
62 assert(c_to_v);
63 auto map_v = mesh.topology()->index_map(0);
64 assert(map_v);
65 std::vector<std::int32_t> vertex_to_x(map_v->size_local()
66 + map_v->num_ghosts());
67 auto map_c = mesh.topology()->index_map(tdim);
68
69 assert(map_c);
70 auto dof_layout = mesh.geometry().cmap().create_dof_layout();
71 auto entity_dofs_all = dof_layout.entity_dofs_all();
72 for (int c = 0; c < map_c->size_local() + map_c->num_ghosts(); ++c)
73 {
74 auto vertices = c_to_v->links(c);
75 auto dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
76 x_dofmap, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
77 for (std::size_t i = 0; i < vertices.size(); ++i)
78 {
79 auto vertex_pos = entity_dofs_all[0][i][0];
80 vertex_to_x[vertices[i]] = dofs[vertex_pos];
81 }
82 }
83
84 // Copy over existing mesh vertices
85 std::span<const T> x_g = mesh.geometry().x();
86 const std::size_t gdim = mesh.geometry().dim();
87 const std::size_t num_vertices = map_v->size_local();
88 const std::size_t num_new_vertices = local_edge_to_new_vertex.size();
89
90 std::array<std::size_t, 2> shape = {num_vertices + num_new_vertices, gdim};
91 std::vector<T> new_vertex_coords(shape[0] * shape[1]);
92 for (std::size_t v = 0; v < num_vertices; ++v)
93 {
94 std::size_t pos = 3 * vertex_to_x[v];
95 for (std::size_t j = 0; j < gdim; ++j)
96 new_vertex_coords[gdim * v + j] = x_g[pos + j];
97 }
98
99 // Compute new vertices
100 if (num_new_vertices > 0)
101 {
102 std::vector<int> edges(num_new_vertices);
103 int i = 0;
104 for (auto& e : local_edge_to_new_vertex)
105 edges[i++] = e.first;
106
107 // Compute midpoint of each edge (padded to 3D)
108 const std::vector<T> midpoints = mesh::compute_midpoints(mesh, 1, edges);
109 for (std::size_t i = 0; i < num_new_vertices; ++i)
110 for (std::size_t j = 0; j < gdim; ++j)
111 new_vertex_coords[gdim * (num_vertices + i) + j] = midpoints[3 * i + j];
112 }
113
114 return {std::move(new_vertex_coords), shape};
115}
116
117} // namespace impl
118
129 MPI_Comm comm,
130 const std::vector<std::vector<std::int32_t>>& marked_for_update,
131 std::span<std::int8_t> marked_edges, const common::IndexMap& map);
132
147template <std::floating_point T>
148std::tuple<std::map<std::int32_t, std::int64_t>, std::vector<T>,
149 std::array<std::size_t, 2>>
151 const graph::AdjacencyList<int>& shared_edges,
152 const mesh::Mesh<T>& mesh,
153 std::span<const std::int8_t> marked_edges)
154{
155 // Take marked_edges and use to create new vertices
156 std::shared_ptr<const common::IndexMap> edge_index_map
157 = mesh.topology()->index_map(1);
158
159 // Add new edge midpoints to list of vertices. The new vertex will be owned by
160 // the process owning the edge.
161 int n = 0;
162 std::map<std::int32_t, std::int64_t> local_edge_to_new_vertex;
163 for (int local_i = 0; local_i < edge_index_map->size_local(); ++local_i)
164 {
165 if (marked_edges[local_i])
166 {
167 [[maybe_unused]] auto it = local_edge_to_new_vertex.insert({local_i, n});
168 assert(it.second);
169 ++n;
170 }
171 }
172
173 const std::int64_t num_local = n;
174 std::int64_t global_offset = 0;
175 MPI_Exscan(&num_local, &global_offset, 1, MPI_INT64_T, MPI_SUM, mesh.comm());
176 global_offset += mesh.topology()->index_map(0)->local_range()[1];
177 std::for_each(local_edge_to_new_vertex.begin(),
178 local_edge_to_new_vertex.end(),
179 [global_offset](auto& e) { e.second += global_offset; });
180
181 // Create actual points
182 auto [new_vertex_coords, xshape]
183 = impl::create_new_geometry(mesh, local_edge_to_new_vertex);
184
185 // If they are shared, then the new global vertex index needs to be
186 // sent off-process.
187
188 // Get number of neighbors
189 int indegree(-1), outdegree(-2), weighted(-1);
190 MPI_Dist_graph_neighbors_count(comm, &indegree, &outdegree, &weighted);
191 assert(indegree == outdegree);
192 const int num_neighbors = indegree;
193
194 std::vector<std::vector<std::int64_t>> values_to_send(num_neighbors);
195 for (auto& local_edge : local_edge_to_new_vertex)
196 {
197 const std::size_t local_i = local_edge.first;
198 // shared, but locally owned : remote owned are not in list.
199
200 for (int remote_process : shared_edges.links(local_i))
201 {
202 // Send (global edge index) -> (new global vertex index) map
203 values_to_send[remote_process].push_back(
204 impl::local_to_global(local_i, *edge_index_map));
205 values_to_send[remote_process].push_back(local_edge.second);
206 }
207 }
208
209 // Send all shared edges marked for update and receive from other
210 // processes
211 std::vector<std::int64_t> received_values;
212 {
213 int indegree(-1), outdegree(-2), weighted(-1);
214 MPI_Dist_graph_neighbors_count(comm, &indegree, &outdegree, &weighted);
215 assert(indegree == outdegree);
216
217 std::vector<std::int64_t> send_buffer;
218 std::vector<int> send_sizes;
219 for (auto& x : values_to_send)
220 {
221 send_sizes.push_back(x.size());
222 send_buffer.insert(send_buffer.end(), x.begin(), x.end());
223 }
224 assert((int)send_sizes.size() == outdegree);
225
226 std::vector<int> recv_sizes(outdegree);
227 send_sizes.reserve(1);
228 recv_sizes.reserve(1);
229 MPI_Neighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
230 MPI_INT, comm);
231
232 // Build displacements
233 std::vector<int> send_disp = {0};
234 std::partial_sum(send_sizes.begin(), send_sizes.end(),
235 std::back_inserter(send_disp));
236 std::vector<int> recv_disp = {0};
237 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
238 std::back_inserter(recv_disp));
239
240 received_values.resize(recv_disp.back());
241 MPI_Neighbor_alltoallv(send_buffer.data(), send_sizes.data(),
242 send_disp.data(), MPI_INT64_T,
243 received_values.data(), recv_sizes.data(),
244 recv_disp.data(), MPI_INT64_T, comm);
245 }
246
247 // Add received remote global vertex indices to map
248 std::vector<std::int64_t> recv_global_edge;
249 assert(received_values.size() % 2 == 0);
250 for (std::size_t i = 0; i < received_values.size() / 2; ++i)
251 recv_global_edge.push_back(received_values[i * 2]);
252 std::vector<std::int32_t> recv_local_edge(recv_global_edge.size());
253 mesh.topology()->index_map(1)->global_to_local(recv_global_edge,
254 recv_local_edge);
255 for (std::size_t i = 0; i < received_values.size() / 2; ++i)
256 {
257 assert(recv_local_edge[i] != -1);
258 [[maybe_unused]] auto it = local_edge_to_new_vertex.insert(
259 {recv_local_edge[i], received_values[i * 2 + 1]});
260 assert(it.second);
261 }
262
263 return {std::move(local_edge_to_new_vertex), std::move(new_vertex_coords),
264 xshape};
265}
266
279std::vector<std::int64_t> adjust_indices(const common::IndexMap& map,
280 std::int32_t n);
281
294std::array<std::vector<std::int32_t>, 2> transfer_facet_meshtag(
295 const mesh::MeshTags<std::int32_t>& tags0, const mesh::Topology& topology1,
296 std::span<const std::int32_t> cell, std::span<const std::int8_t> facet);
297
309std::array<std::vector<std::int32_t>, 2>
311 const mesh::Topology& topology1,
312 std::span<const std::int32_t> parent_cell);
313
314} // namespace dolfinx::refinement
Definition IndexMap.h:94
Definition topologycomputation.h:24
std::span< T > links(std::size_t node)
Definition AdjacencyList.h:111
MeshTags associate values with mesh topology entities.
Definition utils.h:26
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:44
Functions supporting mesh operations.
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
GhostMode
Enum for different partitioning ghost modes.
Definition utils.h:36
std::vector< T > compute_midpoints(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Compute the midpoints for mesh entities of a given dimension.
Definition utils.h:381
Mesh refinement algorithms.
Definition dolfinx_refinement.h:8
std::array< std::vector< std::int32_t >, 2 > transfer_facet_meshtag(const mesh::MeshTags< std::int32_t > &tags0, const mesh::Topology &topology1, std::span< const std::int32_t > cell, std::span< const std::int8_t > facet)
Transfer facet MeshTags from coarse mesh to refined mesh.
Definition utils.cpp:153
std::tuple< std::map< std::int32_t, std::int64_t >, std::vector< T >, std::array< std::size_t, 2 > > create_new_vertices(MPI_Comm comm, const graph::AdjacencyList< int > &shared_edges, const mesh::Mesh< T > &mesh, std::span< const std::int8_t > marked_edges)
Add new vertex for each marked edge, and create new_vertex_coordinates and global_edge->new_vertex ma...
Definition utils.h:150
std::vector< std::int64_t > adjust_indices(const common::IndexMap &map, std::int32_t n)
Given an index map, add "n" extra indices at the end of local range.
Definition utils.cpp:103
std::array< std::vector< std::int32_t >, 2 > transfer_cell_meshtag(const mesh::MeshTags< std::int32_t > &tags0, const mesh::Topology &topology1, std::span< const std::int32_t > parent_cell)
Transfer cell MeshTags from coarse mesh to refined mesh.
Definition utils.cpp:274
void update_logical_edgefunction(MPI_Comm comm, const std::vector< std::vector< std::int32_t > > &marked_for_update, std::span< std::int8_t > marked_edges, const common::IndexMap &map)
Communicate edge markers between processes that share edges.
Definition utils.cpp:47