DOLFINx 0.12.0.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 <cassert>
13#include <concepts>
14#include <cstdint>
15#include <dolfinx/common/IndexMap.h>
16#include <dolfinx/common/MPI.h>
17#include <dolfinx/common/sort.h>
18#include <dolfinx/fem/CoordinateElement.h>
19#include <dolfinx/fem/ElementDofLayout.h>
20#include <dolfinx/fem/dofmapbuilder.h>
21#include <dolfinx/graph/AdjacencyList.h>
22#include <dolfinx/graph/partition.h>
23#include <functional>
24#include <iterator>
25#include <memory>
26#include <span>
27#include <stdexcept>
28#include <utility>
29#include <vector>
30
31namespace dolfinx::mesh
32{
33
35template <std::floating_point T>
37{
38public:
40 using value_type = T;
41
65 template <typename U, typename V, typename W>
66 requires std::is_convertible_v<std::remove_cvref_t<U>,
67 std::vector<std::vector<std::int32_t>>>
68 and std::is_convertible_v<std::remove_cvref_t<V>,
69 std::vector<T>>
70 and std::is_convertible_v<std::remove_cvref_t<W>,
71 std::vector<std::int64_t>>
73 std::shared_ptr<const common::IndexMap> index_map, U&& dofmaps,
74 const std::vector<fem::CoordinateElement<
75 typename std::remove_reference_t<typename V::value_type>>>& elements,
76 V&& x, int dim, W&& input_global_indices)
77 : _dim(dim), _dofmaps(std::forward<U>(dofmaps)),
78 _index_map(std::move(index_map)), _cmaps(elements),
79 _x(std::forward<V>(x)),
80 _input_global_indices(std::forward<W>(input_global_indices))
81 {
82 assert(_x.size() % 3 == 0);
83 if (_x.size() / 3 != _input_global_indices.size())
84 throw std::runtime_error("Geometry size mismatch.");
85
86 if (_dofmaps.size() != _cmaps.size())
87 {
88 throw std::runtime_error("Geometry number of dofmaps not equal to the "
89 "number of coordinate elements.");
90 }
91
92 // TODO: check that elements dim == number of dofmap columns
93 }
94
96 Geometry(const Geometry&) = default;
97
99 Geometry(Geometry&&) = default;
100
102 ~Geometry() = default;
103
104 // Copy assignment (deleted)
105 Geometry& operator=(const Geometry&) = delete;
106
109
111 int dim() const { return _dim; }
112
116 [[deprecated("Use dofmaps().front() instead.")]]
117 md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>> dofmap() const
118 {
119 if (_dofmaps.size() != 1)
120 throw std::runtime_error("Multiple dofmaps");
121 std::size_t ndofs = _cmaps.front().dim();
122 return md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>(
123 _dofmaps.front().data(), _dofmaps.front().size() / ndofs, ndofs);
124 }
125
130 std::vector<md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>>
131 dofmaps() const
132 {
133 std::vector<md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>>
134 dms(_dofmaps.size());
135 for (std::size_t i = 0; i < _dofmaps.size(); ++i)
136 {
137 std::size_t ndofs = _cmaps.at(i).dim();
138 dms[i] = md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>>(
139 _dofmaps.at(i).data(), _dofmaps.at(i).size() / ndofs, ndofs);
140 }
141 return dms;
142 }
143
146 std::shared_ptr<const common::IndexMap> index_map() const
147 {
148 return _index_map;
149 }
150
155 std::span<const value_type> x() const { return _x; }
156
162 std::span<value_type> x() { return _x; }
163
166 const std::vector<fem::CoordinateElement<value_type>>& cmaps() const
167 {
168 return _cmaps;
169 }
170
172 const std::vector<std::int64_t>& input_global_indices() const
173 {
174 return _input_global_indices;
175 }
176
177private:
178 // Geometric dimension
179 int _dim;
180
181 // Map per cell for extracting coordinate data for each cmap
182 std::vector<std::vector<std::int32_t>> _dofmaps;
183
184 // IndexMap for geometry 'dofmap'
185 std::shared_ptr<const common::IndexMap> _index_map;
186
187 // The coordinate elements
188 std::vector<fem::CoordinateElement<value_type>> _cmaps;
189
190 // Coordinates for all points stored as a contiguous array (row-major,
191 // column size = 3)
192 std::vector<value_type> _x;
193
194 // Global indices as provided on Geometry creation
195 std::vector<std::int64_t> _input_global_indices;
196};
197
200template <typename U, typename V, typename W>
201Geometry(std::shared_ptr<const common::IndexMap>, U&&,
202 const std::vector<fem::CoordinateElement<
203 typename std::remove_reference_t<typename V::value_type>>>&,
204 V&&, int, W&&)
205 -> Geometry<typename std::remove_cvref_t<typename V::value_type>>;
207
233template <typename U>
234Geometry<typename std::remove_reference_t<typename U::value_type>>
235create_geometry(const Topology& topology,
236 const std::vector<fem::CoordinateElement<
237 std::remove_reference_t<typename U::value_type>>>& elements,
238 std::span<const std::int64_t> nodes,
239 std::span<const std::int64_t> xdofs, const U& x, int dim,
240 const std::function<std::vector<int>(
241 const graph::AdjacencyList<std::int32_t>&)>& reorder_fn
242 = nullptr)
243{
244 spdlog::info("Create Geometry (multiple)");
245
246 assert(std::ranges::is_sorted(nodes));
247 using T = typename std::remove_reference_t<typename U::value_type>;
248
249 // Check elements match cell types in topology
250 const int tdim = topology.dim();
251 const std::size_t num_cell_types = topology.entity_types(tdim).size();
252 if (elements.size() != num_cell_types)
253 throw std::runtime_error("Mismatch between topology and geometry.");
254
255 std::vector<fem::ElementDofLayout> dof_layouts;
256 dof_layouts.reserve(elements.size());
257 for (auto& el : elements)
258 dof_layouts.push_back(el.create_dof_layout());
259
260 // Build 'geometry' dofmap on the topology
261 auto [_dof_index_map, bs, dofmaps]
262 = fem::build_dofmap_data(topology.index_maps(topology.dim())[0]->comm(),
263 topology, dof_layouts, reorder_fn);
264 auto dof_index_map
265 = std::make_shared<common::IndexMap>(std::move(_dof_index_map));
266
267 // If the mesh has higher order geometry, permute the dofmap
268 if (elements.front().needs_dof_permutations())
269 {
270 const std::int32_t num_cells
271 = topology.connectivity(topology.dim(), 0)->num_nodes();
272 const std::vector<std::uint32_t>& cell_info
273 = topology.get_cell_permutation_info();
274 int d = elements.front().dim();
275 for (std::int32_t cell = 0; cell < num_cells; ++cell)
276 {
277 std::span dofs(dofmaps.front().data() + cell * d, d);
278 elements.front().permute_inv(dofs, cell_info[cell]);
279 }
280 }
281
282 // Compute local-to-global map from local indices in dofmap to the
283 // corresponding global indices in cells, and pass to function to
284 // compute local (dof) to local (position in coords) map from (i)
285 // local-to-global for dofs and (ii) local-to-global for entries in
286 // coords
287
288 std::vector<std::int32_t> all_dofmaps;
289 for (auto q : dofmaps)
290 all_dofmaps.insert(all_dofmaps.end(), q.begin(), q.end());
291
292 const std::vector<std::int32_t> l2l = graph::build::compute_local_to_local(
293 graph::build::compute_local_to_global(xdofs, all_dofmaps), nodes);
294
295 // Allocate space for input global indices and copy data
296 std::vector<std::int64_t> igi(nodes.size());
297 std::ranges::transform(l2l, igi.begin(),
298 [&nodes](auto index) { return nodes[index]; });
299
300 // Build coordinate dof array, copying coordinates to correct position
301 assert(x.size() % dim == 0);
302 const std::size_t shape0 = x.size() / dim;
303 const std::size_t shape1 = dim;
304 std::vector<T> xg(3 * shape0, 0);
305 for (std::size_t i = 0; i < shape0; ++i)
306 {
307 std::copy_n(std::next(x.begin(), shape1 * l2l[i]), shape1,
308 std::next(xg.begin(), 3 * i));
309 }
310
311 return Geometry(dof_index_map, std::move(dofmaps), elements, std::move(xg),
312 dim, std::move(igi));
313}
314
315} // namespace dolfinx::mesh
Definition CoordinateElement.h:38
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:41
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:37
~Geometry()=default
Destructor.
std::span< value_type > x()
Access geometry degrees-of-freedom data (non-const version).
Definition Geometry.h:162
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:72
std::shared_ptr< const common::IndexMap > index_map() const
Index map for the geometry 'degrees-of-freedom'.
Definition Geometry.h:146
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:131
int dim() const
Return dimension of the Euclidean coordinate system.
Definition Geometry.h:111
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:117
const std::vector< std::int64_t > & input_global_indices() const
Global user indices.
Definition Geometry.h:172
std::span< const value_type > x() const
Access geometry degrees-of-freedom data (const version).
Definition Geometry.h:155
const std::vector< fem::CoordinateElement< value_type > > & cmaps() const
The elements that describes the geometry map.
Definition Geometry.h:166
T value_type
Value type.
Definition Geometry.h:40
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
Get the cell permutation information.
Definition Topology.cpp:970
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:950
const std::vector< CellType > & entity_types(int dim) const
Entity types in the topology for a given dimension.
Definition Topology.cpp:893
int dim() const noexcept
Topological dimension of the mesh.
Definition Topology.cpp:888
std::vector< std::shared_ptr< const common::IndexMap > > index_maps(int dim) const
Get the index maps that describe the parallel distribution of the mesh entities of a given topologica...
Definition Topology.cpp:916
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:660
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:578
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:600
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:235