13 #include <dolfinx/common/IndexMap.h>
14 #include <dolfinx/common/UniqueIdGenerator.h>
15 #include <dolfinx/common/span.hpp>
16 #include <dolfinx/common/utils.h>
17 #include <dolfinx/graph/AdjacencyList.h>
18 #include <dolfinx/graph/partition.h>
19 #include <dolfinx/io/cells.h>
47 template <
typename U,
typename V>
51 _values(std::forward<V>(
values))
55 throw std::runtime_error(
56 "Indices and values arrays must have same size.");
59 if (!std::is_sorted(_indices.begin(), _indices.end()))
60 throw std::runtime_error(
"MeshTag data is not sorted");
61 if (std::adjacent_find(_indices.begin(), _indices.end()) != _indices.end())
62 throw std::runtime_error(
"MeshTag data has duplicates");
84 std::vector<std::int32_t>
find(
const T value)
const
86 int n = std::count(_values.begin(), _values.end(), value);
87 std::vector<std::int32_t>
indices(n);
89 for (std::int32_t i = 0; i < _values.size(); ++i)
91 if (_values[i] == value)
92 indices[counter++] = _indices[i];
99 const std::vector<std::int32_t>&
indices()
const {
return _indices; }
102 const std::vector<T>&
values()
const {
return _values; }
105 int dim()
const {
return _dim; }
108 std::shared_ptr<const Mesh>
mesh()
const {
return _mesh; }
111 std::string
name =
"mesh_tags";
114 std::size_t
id()
const {
return _unique_id; }
121 std::shared_ptr<const Mesh> _mesh;
127 std::vector<std::int32_t> _indices;
130 std::vector<T> _values;
139 template <
typename T>
143 const tcb::span<const T>& values)
146 if ((std::size_t)entities.
num_nodes() != values.size())
147 throw std::runtime_error(
"Number of entities and values must match");
150 const auto map_e = mesh->topology().index_map(dim);
153 auto e_to_v = mesh->topology().connectivity(dim, 0);
155 throw std::runtime_error(
"Missing entity-vertex connectivity.");
157 const int num_vertices_per_entity = e_to_v->num_links(0);
158 const int num_entities_mesh = map_e->size_local() + map_e->num_ghosts();
160 std::map<std::vector<std::int32_t>, std::int32_t> entity_key_to_index;
161 std::vector<std::int32_t> key(num_vertices_per_entity);
162 for (
int e = 0; e < num_entities_mesh; ++e)
167 auto vertices = e_to_v->links(e);
168 std::copy(vertices.begin(), vertices.end(), key.begin());
169 std::sort(key.begin(), key.end());
170 entity_key_to_index.insert({key, e});
175 std::vector<std::int32_t> indices_new;
176 std::vector<T> values_new;
177 std::vector<std::int32_t> entity(num_vertices_per_entity);
178 for (std::int32_t e = 0; e < entities.
num_nodes(); ++e)
181 assert(num_vertices_per_entity == entities.
num_links(e));
182 std::copy(entities.
links(e).begin(), entities.
links(e).end(),
184 std::sort(entity.begin(), entity.end());
186 if (
const auto it = entity_key_to_index.find(entity);
187 it != entity_key_to_index.end())
189 indices_new.push_back(it->second);
190 values_new.push_back(values[e]);
194 auto [indices_sorted, values_sorted]
197 std::move(values_sorted));
static std::size_t id()
Generate a unique ID.
Definition: UniqueIdGenerator.cpp:22
This class provides a static adjacency list data structure. It is commonly used to store directed gra...
Definition: AdjacencyList.h:68
tcb::span< T > links(int node)
Get the links (edges) for given node.
Definition: AdjacencyList.h:151
std::int32_t num_nodes() const
Get the number of nodes.
Definition: AdjacencyList.h:136
int num_links(int node) const
Number of connections for given node.
Definition: AdjacencyList.h:141
std::pair< U, V > sort_unique(const U &indices, const V &values)
Sort two arrays based on the values in array indices. Any duplicate indices and the corresponding val...
Definition: utils.h:29
mesh::MeshTags< T > create_meshtags(const std::shared_ptr< const mesh::Mesh > &mesh, const int dim, const graph::AdjacencyList< std::int32_t > &entities, const tcb::span< const T > &values)
Create MeshTags from arrays.
Definition: MeshTags.h:141