DOLFINx 0.11.0.0
DOLFINx C++
Loading...
Searching...
No Matches
EntityMap.h
1// Copyright (C) 2025-2026 Jørgen S. Dokken and Joseph P. Dean
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 <concepts>
11#include <dolfinx/common/IndexMap.h>
12#include <ranges>
13#include <span>
14#include <vector>
15
16namespace dolfinx::mesh
17{
21{
22public:
35 template <typename U>
36 requires std::is_convertible_v<std::remove_cvref_t<U>,
37 std::vector<std::int32_t>>
38 EntityMap(std::shared_ptr<const Topology> topology,
39 std::shared_ptr<const Topology> sub_topology, int dim,
41 : _dim(dim), _topology(topology),
42 _sub_topology_to_topology(std::forward<U>(sub_topology_to_topology)),
43 _sub_topology(sub_topology)
44 {
45 auto e_imap = sub_topology->index_map(_dim);
46 if (!e_imap)
47 {
48 throw std::runtime_error(
49 "No index map for entities, call `Topology::create_entities("
50 + std::to_string(_dim) + ")");
51 }
52
53 std::size_t num_ents = e_imap->size_local() + e_imap->num_ghosts();
54 if (num_ents != _sub_topology_to_topology.size())
55 {
56 throw std::runtime_error(
57 "Size mismatch between `sub_topology_to_topology` and index map.");
58 }
59 }
60
62 EntityMap(const EntityMap& map) = default;
63
65 EntityMap(EntityMap&& map) = default;
66
67 // Destructor
68 ~EntityMap() = default;
69
73 std::size_t dim() const;
74
77 std::shared_ptr<const Topology> topology() const;
78
81 std::shared_ptr<const Topology> sub_topology() const;
82
103 std::vector<std::int32_t> sub_topology_to_topology(CellRange auto&& entities,
104 bool inverse) const
105 {
106 if (!inverse)
107 {
108 // In this case, we want to map from entity indices in
109 // `_sub_topology` to corresponding entities in `_topology`. Hence,
110 // for each index in `entities`, we get the corresponding index in
111 // `_topology` using `_sub_topology_to_topology`
112 auto mapped
113 = std::forward<decltype(entities)>(entities)
114 | std::views::transform([this](std::int32_t i)
115 { return _sub_topology_to_topology[i]; });
116 return std::vector<std::int32_t>(mapped.begin(), mapped.end());
117 }
118 else
119 {
120 // In this case, we are mapping from entity indices in `_topology`
121 // to entity indices in `_sub_topology`. Hence, we first need to
122 // construct the "inverse" of `_sub_topology_to_topology`
123 std::unordered_map<std::int32_t, std::int32_t> topology_to_sub_topology;
124 topology_to_sub_topology.reserve(_sub_topology_to_topology.size());
125 for (std::size_t i = 0; i < _sub_topology_to_topology.size(); ++i)
126 {
127 topology_to_sub_topology.insert(
128 {_sub_topology_to_topology[i], static_cast<std::int32_t>(i)});
129 }
130
131 // For each entity index in `entities` (which are indices in
132 // `_topology`), get the corresponding entity in `_sub_topology`.
133 // Since `_sub_topology` consists of a subset of entities in
134 // `_topology`, there are entities in topology that may not exist in
135 // `_sub_topology`. If this is the case, mark those entities with
136 // -1.
137
138 auto mapped = std::forward<decltype(entities)>(entities)
139 | std::views::transform(
140 [&topology_to_sub_topology](std::int32_t i)
141 {
142 // Map the entity if it exists. If it doesn't, mark
143 // with -1.
144 auto it = topology_to_sub_topology.find(i);
145 return (it != topology_to_sub_topology.end())
146 ? it->second
147 : -1;
148 });
149 return std::vector<std::int32_t>(mapped.begin(), mapped.end());
150 }
151 }
152
153private:
154 // Dimension of the entities
155 std::size_t _dim;
156
157 // A topology
158 std::shared_ptr<const Topology> _topology;
159
160 // A list of `_dim`-dimensional entities in _topology, where
161 // `_sub_topology_to_topology[i]` is the index in `_topology` of the
162 // `i`th entity in `_sub_topology`
163 std::vector<std::int32_t> _sub_topology_to_topology;
164
165 // A second topology, consisting of a subset of entities in
166 // `_topology`
167 std::shared_ptr<const Topology> _sub_topology;
168};
169} // namespace dolfinx::mesh
EntityMap(EntityMap &&map)=default
Move constructor.
std::size_t dim() const
Get the topological dimension of the entities related by this EntityMap.
Definition EntityMap.cpp:16
std::vector< std::int32_t > sub_topology_to_topology(CellRange auto &&entities, bool inverse) const
Map entities between the sub-topology and the parent topology.
Definition EntityMap.h:103
EntityMap(const EntityMap &map)=default
Copy constructor.
EntityMap(std::shared_ptr< const Topology > topology, std::shared_ptr< const Topology > sub_topology, int dim, U &&sub_topology_to_topology)
Constructor of a bidirectional map relating entities of dimension dim in topology and sub_topology.
Definition EntityMap.h:38
std::shared_ptr< const Topology > sub_topology() const
Get the sub-topology.
Definition EntityMap.cpp:23
std::shared_ptr< const Topology > topology() const
Get the (parent) topology.
Definition EntityMap.cpp:18
Requirement on range of cell indices.
Definition Topology.h:32
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32