DOLFINx 0.10.0.0
DOLFINx C++ interface
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)), _index_map(index_map),
75 _cmaps(elements), _x(std::forward<V>(x)),
76 _input_global_indices(std::forward<W>(input_global_indices))
77 {
78 assert(_x.size() % 3 == 0);
79 if (_x.size() / 3 != _input_global_indices.size())
80 throw std::runtime_error("Geometry size mismatch.");
81
82 if (_dofmaps.size() != _cmaps.size())
83 {
84 throw std::runtime_error("Geometry number of dofmaps not equal to the "
85 "number of coordinate elements.");
86 }
87
88 // TODO: check that elements dim == number of dofmap columns
89 }
90
92 Geometry(const Geometry&) = default;
93
95 Geometry(Geometry&&) = default;
96
98 ~Geometry() = default;
99
101 Geometry& operator=(const Geometry&) = delete;
102
105
107 int dim() const { return _dim; }
108
111 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
112 const std::int32_t,
113 MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 2>>
114 dofmap() const
115 {
116 if (_dofmaps.size() != 1)
117 throw std::runtime_error("Multiple dofmaps");
118 return this->dofmap(0);
119 }
120
127 MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
128 const std::int32_t,
129 MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 2>>
130 dofmap(std::size_t i) const
131 {
132 std::size_t ndofs = _cmaps.at(i).dim();
133 return MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan<
134 const std::int32_t,
135 MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<std::size_t, 2>>(
136 _dofmaps.at(i).data(), _dofmaps.at(i).size() / ndofs, ndofs);
137 }
138
141 std::shared_ptr<const common::IndexMap> index_map() const
142 {
143 return _index_map;
144 }
145
150 std::span<const value_type> x() const { return _x; }
151
157 std::span<value_type> x() { return _x; }
158
165 const std::vector<fem::CoordinateElement<value_type>>& cmaps() const
166 {
167 return _cmaps;
168 }
169
174 {
175 if (_cmaps.size() > 1)
176 throw std::runtime_error("Multiple cmaps.");
177 return _cmaps.front();
178 }
179
181 const std::vector<std::int64_t>& input_global_indices() const
182 {
183 return _input_global_indices;
184 }
185
186private:
187 // Geometric dimension
188 int _dim;
189
190 // Map per cell for extracting coordinate data for each cmap
191 std::vector<std::vector<std::int32_t>> _dofmaps;
192
193 // IndexMap for geometry 'dofmap'
194 std::shared_ptr<const common::IndexMap> _index_map;
195
196 // The coordinate elements
197 std::vector<fem::CoordinateElement<value_type>> _cmaps;
198
199 // Coordinates for all points stored as a contiguous array (row-major,
200 // column size = 3)
201 std::vector<value_type> _x;
202
203 // Global indices as provided on Geometry creation
204 std::vector<std::int64_t> _input_global_indices;
205};
206
209template <typename U, typename V, typename W>
210Geometry(std::shared_ptr<const common::IndexMap>, U&&,
211 const std::vector<fem::CoordinateElement<
212 typename std::remove_reference_t<typename V::value_type>>>&,
213 V&&, int, W&&)
214 -> Geometry<typename std::remove_cvref_t<typename V::value_type>>;
216
241template <typename U>
242Geometry<typename std::remove_reference_t<typename U::value_type>>
244 const Topology& topology,
245 const std::vector<fem::CoordinateElement<
246 std::remove_reference_t<typename U::value_type>>>& elements,
247 std::span<const std::int64_t> nodes, std::span<const std::int64_t> xdofs,
248 const U& x, int dim,
249 std::function<std::vector<int>(const graph::AdjacencyList<std::int32_t>&)>
250 reorder_fn
251 = nullptr)
252{
253 spdlog::info("Create Geometry (multiple)");
254
255 assert(std::ranges::is_sorted(nodes));
256 using T = typename std::remove_reference_t<typename U::value_type>;
257
258 // Check elements match cell types in topology
259 const int tdim = topology.dim();
260 const std::size_t num_cell_types = topology.entity_types(tdim).size();
261 if (elements.size() != num_cell_types)
262 throw std::runtime_error("Mismatch between topology and geometry.");
263
264 std::vector<fem::ElementDofLayout> dof_layouts;
265 for (const auto& el : elements)
266 dof_layouts.push_back(el.create_dof_layout());
267
268 spdlog::info("Got {} dof layouts", dof_layouts.size());
269
270 // Build 'geometry' dofmap on the topology
271 auto [_dof_index_map, bs, dofmaps]
272 = fem::build_dofmap_data(topology.index_map(topology.dim())->comm(),
273 topology, dof_layouts, reorder_fn);
274 auto dof_index_map
275 = std::make_shared<common::IndexMap>(std::move(_dof_index_map));
276
277 // If the mesh has higher order geometry, permute the dofmap
278 if (elements.front().needs_dof_permutations())
279 {
280 const std::int32_t num_cells
281 = topology.connectivity(topology.dim(), 0)->num_nodes();
282 const std::vector<std::uint32_t>& cell_info
283 = topology.get_cell_permutation_info();
284 int d = elements.front().dim();
285 for (std::int32_t cell = 0; cell < num_cells; ++cell)
286 {
287 std::span dofs(dofmaps.front().data() + cell * d, d);
288 elements.front().permute_inv(dofs, cell_info[cell]);
289 }
290 }
291
292 spdlog::info("Calling compute_local_to_global");
293 // Compute local-to-global map from local indices in dofmap to the
294 // corresponding global indices in cells, and pass to function to
295 // compute local (dof) to local (position in coords) map from (i)
296 // local-to-global for dofs and (ii) local-to-global for entries in
297 // coords
298
299 spdlog::info("xdofs.size = {}", xdofs.size());
300 std::vector<std::int32_t> all_dofmaps;
301 std::stringstream s;
302 for (auto q : dofmaps)
303 {
304 s << q.size() << " ";
305 all_dofmaps.insert(all_dofmaps.end(), q.begin(), q.end());
306 }
307 spdlog::info("dofmap sizes = {}", s.str());
308 spdlog::info("all_dofmaps.size = {}", all_dofmaps.size());
309 spdlog::info("nodes.size = {}", nodes.size());
310
311 const std::vector<std::int32_t> l2l = graph::build::compute_local_to_local(
312 graph::build::compute_local_to_global(xdofs, all_dofmaps), nodes);
313
314 // Allocate space for input global indices and copy data
315 std::vector<std::int64_t> igi(nodes.size());
316 std::ranges::transform(l2l, igi.begin(),
317 [&nodes](auto index) { return nodes[index]; });
318
319 // Build coordinate dof array, copying coordinates to correct position
320 assert(x.size() % dim == 0);
321 const std::size_t shape0 = x.size() / dim;
322 const std::size_t shape1 = dim;
323 std::vector<T> xg(3 * shape0, 0);
324 for (std::size_t i = 0; i < shape0; ++i)
325 {
326 std::copy_n(std::next(x.begin(), shape1 * l2l[i]), shape1,
327 std::next(xg.begin(), 3 * i));
328 }
329
330 spdlog::info("Creating geometry with {} dofmaps", dof_layouts.size());
331
332 return Geometry(dof_index_map, std::move(dofmaps), elements, std::move(xg),
333 dim, std::move(igi));
334}
335
336} // namespace dolfinx::mesh
Definition XDMFFile.h:29
Definition topologycomputation.h:24
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:157
MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan< const std::int32_t, MDSPAN_IMPL_STANDARD_NAMESPACE::dextents< std::size_t, 2 > > dofmap(std::size_t i) const
Degree-of-freedom map associated with the ith coordinate map element in the geometry.
Definition Geometry.h:130
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:141
Geometry(Geometry &&)=default
Move constructor.
Geometry(const Geometry &)=default
Copy constructor.
const fem::CoordinateElement< value_type > & cmap() const
The element that describes the geometry map.
Definition Geometry.h:173
int dim() const
Return dimension of the Euclidean coordinate system.
Definition Geometry.h:107
Geometry & operator=(Geometry &&)=default
Move assignment.
MDSPAN_IMPL_STANDARD_NAMESPACE::mdspan< const std::int32_t, MDSPAN_IMPL_STANDARD_NAMESPACE::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:181
std::span< const value_type > x() const
Access geometry degrees-of-freedom data (const version).
Definition Geometry.h:150
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:165
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:46
std::shared_ptr< const common::IndexMap > index_map(int dim) const
Get the IndexMap that described the parallel distribution of the mesh entities.
Definition Topology.cpp:787
const std::vector< std::uint32_t > & get_cell_permutation_info() const
Returns the permutation information.
Definition Topology.cpp:807
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:793
const std::vector< CellType > & entity_types(int dim) const
Entity types in the topology for a given dimension.
Definition Topology.cpp:764
int dim() const noexcept
Topological dimension of the mesh.
Definition Topology.cpp:759
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:627
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:534
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:556
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, std::function< std::vector< int >(const graph::AdjacencyList< std::int32_t > &)> reorder_fn=nullptr)
Build Geometry from input data.
Definition Geometry.h:243