DOLFINx 0.11.0
DOLFINx C++
Loading...
Searching...
No Matches
Geometry.h
1// Copyright (C) 2006-2022 Anders Logg and Garth N. Wells
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 "Topology.h"
10#include <algorithm>
11#include <basix/mdspan.hpp>
12#include <concepts>
13#include <cstdint>
14#include <dolfinx/common/IndexMap.h>
15#include <dolfinx/common/MPI.h>
16#include <dolfinx/common/sort.h>
17#include <dolfinx/fem/CoordinateElement.h>
18#include <dolfinx/fem/ElementDofLayout.h>
19#include <dolfinx/fem/dofmapbuilder.h>
20#include <dolfinx/graph/AdjacencyList.h>
21#include <dolfinx/graph/partition.h>
22#include <functional>
23#include <memory>
24#include <span>
25#include <utility>
26#include <vector>
27
28namespace dolfinx::mesh
29{
30
32template <std::floating_point T>
34{
35public:
37 using value_type = T;
38
62 template <typename U, typename V, typename W>
63 requires std::is_convertible_v<std::remove_cvref_t<U>,
64 std::vector<std::vector<std::int32_t>>>
65 and std::is_convertible_v<std::remove_cvref_t<V>,
66 std::vector<T>>
67 and std::is_convertible_v<std::remove_cvref_t<W>,
68 std::vector<std::int64_t>>
70 std::shared_ptr<const common::IndexMap> index_map, U&& dofmaps,
71 const std::vector<fem::CoordinateElement<
72 typename std::remove_reference_t<typename V::value_type>>>& elements,
73 V&& x, int dim, W&& input_global_indices)
74 : _dim(dim), _dofmaps(std::forward<U>(dofmaps)),
75 _index_map(std::move(index_map)), _cmaps(elements),
76 _x(std::forward<V>(x)),
77 _input_global_indices(std::forward<W>(input_global_indices))
78 {
79 assert(_x.size() % 3 == 0);
80 if (_x.size() / 3 != _input_global_indices.size())
81 throw std::runtime_error("Geometry size mismatch.");
82
83 if (_dofmaps.size() != _cmaps.size())
84 {
85 throw std::runtime_error("Geometry number of dofmaps not equal to the "
86 "number of coordinate elements.");
87 }
88
89 // TODO: check that elements dim == number of dofmap columns
90 }
91
93 Geometry(const Geometry&) = default;
94
96 Geometry(Geometry&&) = default;
97
99 ~Geometry() = default;
100
102 Geometry& operator=(const Geometry&) = delete;
103
106
108 int dim() const { return _dim; }
109
113 [[deprecated("Use dofmaps().front() instead.")]]
114 md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>> dofmap() const
115 {
116 if (_dofmaps.size() != 1)
117 throw std::runtime_error("Multiple dofmaps");
118 std::size_t ndofs = _cmaps.front().dim();
119 return md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>(
120 _dofmaps.front().data(), _dofmaps.front().size() / ndofs, ndofs);
121 }
122
127 std::vector<md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>>
128 dofmaps() const
129 {
130 std::vector<md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>>
131 dms(_dofmaps.size());
132 for (std::size_t i = 0; i < _dofmaps.size(); ++i)
133 {
134 std::size_t ndofs = _cmaps.at(i).dim();
135 dms[i] = md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>(
136 _dofmaps.at(i).data(), _dofmaps.at(i).size() / ndofs, ndofs);
137 }
138 return dms;
139 }
140
143 std::shared_ptr<const common::IndexMap> index_map() const
144 {
145 return _index_map;
146 }
147
152 std::span<const value_type> x() const { return _x; }
153
159 std::span<value_type> x() { return _x; }
160
163 const std::vector<fem::CoordinateElement<value_type>>& cmaps() const
164 {
165 return _cmaps;
166 }
167
169 const std::vector<std::int64_t>& input_global_indices() const
170 {
171 return _input_global_indices;
172 }
173
174private:
175 // Geometric dimension
176 int _dim;
177
178 // Map per cell for extracting coordinate data for each cmap
179 std::vector<std::vector<std::int32_t>> _dofmaps;
180
181 // IndexMap for geometry 'dofmap'
182 std::shared_ptr<const common::IndexMap> _index_map;
183
184 // The coordinate elements
185 std::vector<fem::CoordinateElement<value_type>> _cmaps;
186
187 // Coordinates for all points stored as a contiguous array (row-major,
188 // column size = 3)
189 std::vector<value_type> _x;
190
191 // Global indices as provided on Geometry creation
192 std::vector<std::int64_t> _input_global_indices;
193};
194
197template <typename U, typename V, typename W>
198Geometry(std::shared_ptr<const common::IndexMap>, U&&,
199 const std::vector<fem::CoordinateElement<
200 typename std::remove_reference_t<typename V::value_type>>>&,
201 V&&, int, W&&)
202 -> Geometry<typename std::remove_cvref_t<typename V::value_type>>;
204
229template <typename U>
230Geometry<typename std::remove_reference_t<typename U::value_type>>
231create_geometry(const Topology& topology,
232 const std::vector<fem::CoordinateElement<
233 std::remove_reference_t<typename U::value_type>>>& elements,
234 std::span<const std::int64_t> nodes,
235 std::span<const std::int64_t> xdofs, const U& x, int dim,
236 const std::function<std::vector<int>(
237 const graph::AdjacencyList<std::int32_t>&)>& reorder_fn
238 = nullptr)
239{
240 spdlog::info("Create Geometry (multiple)");
241
242 assert(std::ranges::is_sorted(nodes));
243 using T = typename std::remove_reference_t<typename U::value_type>;
244
245 // Check elements match cell types in topology
246 const int tdim = topology.dim();
247 const std::size_t num_cell_types = topology.entity_types(tdim).size();
248 if (elements.size() != num_cell_types)
249 throw std::runtime_error("Mismatch between topology and geometry.");
250
251 std::vector<fem::ElementDofLayout> dof_layouts;
252 dof_layouts.reserve(elements.size());
253 for (auto& el : elements)
254 dof_layouts.push_back(el.create_dof_layout());
255
256 spdlog::info("Got {} dof layouts", dof_layouts.size());
257
258 // Build 'geometry' dofmap on the topology
259 auto [_dof_index_map, bs, dofmaps]
260 = fem::build_dofmap_data(topology.index_maps(topology.dim())[0]->comm(),
261 topology, dof_layouts, reorder_fn);
262 auto dof_index_map
263 = std::make_shared<common::IndexMap>(std::move(_dof_index_map));
264
265 // If the mesh has higher order geometry, permute the dofmap
266 if (elements.front().needs_dof_permutations())
267 {
268 const std::int32_t num_cells
269 = topology.connectivity(topology.dim(), 0)->num_nodes();
270 const std::vector<std::uint32_t>& cell_info
271 = topology.get_cell_permutation_info();
272 int d = elements.front().dim();
273 for (std::int32_t cell = 0; cell < num_cells; ++cell)
274 {
275 std::span dofs(dofmaps.front().data() + cell * d, d);
276 elements.front().permute_inv(dofs, cell_info[cell]);
277 }
278 }
279
280 spdlog::info("Calling compute_local_to_global");
281 // Compute local-to-global map from local indices in dofmap to the
282 // corresponding global indices in cells, and pass to function to
283 // compute local (dof) to local (position in coords) map from (i)
284 // local-to-global for dofs and (ii) local-to-global for entries in
285 // coords
286
287 spdlog::info("xdofs.size = {}", xdofs.size());
288 std::vector<std::int32_t> all_dofmaps;
289 std::stringstream s;
290 for (auto q : dofmaps)
291 {
292 s << q.size() << " ";
293 all_dofmaps.insert(all_dofmaps.end(), q.begin(), q.end());
294 }
295 spdlog::info("dofmap sizes = {}", s.str());
296 spdlog::info("all_dofmaps.size = {}", all_dofmaps.size());
297 spdlog::info("nodes.size = {}", nodes.size());
298
299 const std::vector<std::int32_t> l2l = graph::build::compute_local_to_local(
300 graph::build::compute_local_to_global(xdofs, all_dofmaps), nodes);
301
302 // Allocate space for input global indices and copy data
303 std::vector<std::int64_t> igi(nodes.size());
304 std::ranges::transform(l2l, igi.begin(),
305 [&nodes](auto index) { return nodes[index]; });
306
307 // Build coordinate dof array, copying coordinates to correct position
308 assert(x.size() % dim == 0);
309 const std::size_t shape0 = x.size() / dim;
310 const std::size_t shape1 = dim;
311 std::vector<T> xg(3 * shape0, 0);
312 for (std::size_t i = 0; i < shape0; ++i)
313 {
314 std::copy_n(std::next(x.begin(), shape1 * l2l[i]), shape1,
315 std::next(xg.begin(), 3 * i));
316 }
317
318 spdlog::info("Creating geometry with {} dofmaps", dof_layouts.size());
319
320 return Geometry(dof_index_map, std::move(dofmaps), elements, std::move(xg),
321 dim, std::move(igi));
322}
323
324} // namespace dolfinx::mesh
Definition CoordinateElement.h:38
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:38
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:34
~Geometry()=default
Destructor.
std::span< value_type > x()
Access geometry degrees-of-freedom data (non-const version).
Definition Geometry.h:159
Geometry(std::shared_ptr< const common::IndexMap > index_map, U &&dofmaps, const std::vector< fem::CoordinateElement< typename std::remove_reference_t< typename V::value_type > > > &elements, V &&x, int dim, W &&input_global_indices)
Constructor of object that holds mesh geometry data.
Definition Geometry.h:69
std::shared_ptr< const common::IndexMap > index_map() const
Index map for the geometry 'degrees-of-freedom'.
Definition Geometry.h:143
Geometry(Geometry &&)=default
Move constructor.
Geometry(const Geometry &)=default
Copy constructor.
std::vector< md::mdspan< const std::int32_t, md::dextents< std::size_t, 2 > > > dofmaps() const
Degree-of-freedom map associated with each coordinate map element in the geometry.
Definition Geometry.h:128
int dim() const
Return dimension of the Euclidean coordinate system.
Definition Geometry.h:108
Geometry & operator=(Geometry &&)=default
Move assignment.
md::mdspan< const std::int32_t, md::dextents< std::size_t, 2 > > dofmap() const
DofMap for the geometry.
Definition Geometry.h:114
const std::vector< std::int64_t > & input_global_indices() const
Global user indices.
Definition Geometry.h:169
std::span< const value_type > x() const
Access geometry degrees-of-freedom data (const version).
Definition Geometry.h:152
Geometry & operator=(const Geometry &)=delete
Copy assignment.
const std::vector< fem::CoordinateElement< value_type > > & cmaps() const
The elements that describes the geometry map.
Definition Geometry.h:163
T value_type
Value type.
Definition Geometry.h:37
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:49
const std::vector< std::uint32_t > & get_cell_permutation_info() const
Returns the permutation information.
Definition Topology.cpp:885
std::shared_ptr< const graph::AdjacencyList< std::int32_t > > connectivity(std::array< int, 2 > d0, std::array< int, 2 > d1) const
Get the connectivity from entities of topological dimension d0 to dimension d1.
Definition Topology.cpp:865
const std::vector< CellType > & entity_types(int dim) const
Entity types in the topology for a given dimension.
Definition Topology.cpp:808
int dim() const noexcept
Topological dimension of the mesh.
Definition Topology.cpp:803
std::vector< std::shared_ptr< const common::IndexMap > > index_maps(int dim) const
Get the index maps that described the parallel distribution of the mesh entities of a given topologic...
Definition Topology.cpp:831
std::tuple< common::IndexMap, int, std::vector< std::vector< std::int32_t > > > build_dofmap_data(MPI_Comm comm, const mesh::Topology &topology, const std::vector< ElementDofLayout > &element_dof_layouts, const std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> &reorder_fn)
Definition dofmapbuilder.cpp:643
std::vector< std::int64_t > compute_local_to_global(std::span< const std::int64_t > global, std::span< const std::int32_t > local)
Definition partition.cpp:542
std::vector< std::int32_t > compute_local_to_local(std::span< const std::int64_t > local0_to_global, std::span< const std::int64_t > local1_to_global)
Compute a local0-to-local1 map from two local-to-global maps with common global indices.
Definition partition.cpp:564
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
Geometry< typename std::remove_reference_t< typename U::value_type > > create_geometry(const Topology &topology, const std::vector< fem::CoordinateElement< std::remove_reference_t< typename U::value_type > > > &elements, std::span< const std::int64_t > nodes, std::span< const std::int64_t > xdofs, const U &x, int dim, const std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> &reorder_fn=nullptr)
Build Geometry from input data.
Definition Geometry.h:231