Loading...
Searching...
No Matches
MeshTags.h
// Copyright (C) 2020-2022 Michal Habera and Garth N. Wells
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include "Topology.h"
#include <algorithm>
#include <dolfinx/common/IndexMap.h>
#include <dolfinx/common/log.h>
#include <dolfinx/common/utils.h>
#include <dolfinx/graph/AdjacencyList.h>
#include <dolfinx/io/cells.h>
#include <memory>
#include <span>
#include <utility>
#include <vector>
namespace dolfinx::mesh
{
template <typename T>
class MeshTags
{
public:
template <typename U, typename V>
requires std::is_convertible_v<std::remove_cvref_t<U>,
std::vector<std::int32_t>>
and std::is_convertible_v<std::remove_cvref_t<V>,
std::vector<T>>
MeshTags(std::shared_ptr<const Topology> topology, int dim, U&& indices,
V&& values)
: _topology(std::move(topology)), _dim(dim),
_indices(std::forward<U>(indices)), _values(std::forward<V>(values))
{
if (_indices.size() != _values.size())
{
throw std::runtime_error(
"Indices and values arrays must have same size.");
}
#ifndef NDEBUG
if (!std::ranges::is_sorted(_indices))
throw std::runtime_error("MeshTag data is not sorted");
if (std::adjacent_find(_indices.begin(), _indices.end()) != _indices.end())
throw std::runtime_error("MeshTag data has duplicates");
#endif
}
MeshTags(const MeshTags& tags) = default;
MeshTags(MeshTags&& tags) = default;
~MeshTags() = default;
MeshTags& operator=(const MeshTags& tags) = default;
MeshTags& operator=(MeshTags&& tags) = default;
std::vector<std::int32_t> find(const T value) const
{
std::size_t n = std::count(_values.begin(), _values.end(), value);
std::vector<std::int32_t> indices;
indices.reserve(n);
for (std::size_t i = 0; i < _values.size(); ++i)
{
if (_values[i] == value)
indices.push_back(_indices[i]);
}
return indices;
}
std::span<const std::int32_t> indices() const { return _indices; }
std::span<const T> values() const { return _values; }
int dim() const { return _dim; }
std::shared_ptr<const Topology> topology() const { return _topology; }
std::string name = "mesh_tags";
private:
// Associated topology
std::shared_ptr<const Topology> _topology;
// Topological dimension of tagged topology entities
int _dim;
// Local-to-process indices of tagged entities
std::vector<std::int32_t> _indices;
// Values attached to entities
std::vector<T> _values;
};
template <typename T>
MeshTags<T> create_meshtags(std::shared_ptr<const Topology> topology, int dim,
const graph::AdjacencyList<std::int32_t>& entities,
std::span<const T> values)
{
spdlog::info(
"Building MeshTags object from tagged entities (defined by vertices).");
// Compute the indices of the topology entities (index is set to -1 if
// it can't be found)
assert(topology);
const std::vector<std::int32_t> indices
= entities_to_index(*topology, dim, entities.array());
if (indices.size() != values.size())
{
throw std::runtime_error(
"Duplicate mesh entities when building MeshTags object.");
}
// Sort the indices and values by indices
auto [indices_sorted, values_sorted] = common::sort_unique(indices, values);
// Remove any entities that were not found (these have an index of -1)
auto it0 = std::ranges::lower_bound(indices_sorted, 0);
std::size_t pos0 = std::distance(indices_sorted.begin(), it0);
indices_sorted.erase(indices_sorted.begin(), it0);
values_sorted.erase(values_sorted.begin(),
std::next(values_sorted.begin(), pos0));
return MeshTags<T>(topology, dim, std::move(indices_sorted),
std::move(values_sorted));
}
} // namespace dolfinx::mesh
dolfinx::graph::AdjacencyList
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:38
dolfinx::graph::AdjacencyList::array
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:173
dolfinx::mesh::MeshTags
MeshTags associate values with mesh topology entities.
Definition MeshTags.h:33
dolfinx::mesh::MeshTags::indices
std::span< const std::int32_t > indices() const
Definition MeshTags.h:101
dolfinx::mesh::MeshTags::~MeshTags
~MeshTags()=default
Destructor.
dolfinx::mesh::MeshTags::MeshTags
MeshTags(const MeshTags &tags)=default
Copy constructor.
dolfinx::mesh::MeshTags::MeshTags
MeshTags(MeshTags &&tags)=default
Move constructor.
dolfinx::mesh::MeshTags::operator=
MeshTags & operator=(MeshTags &&tags)=default
Move assignment.
dolfinx::mesh::MeshTags::values
std::span< const T > values() const
Values attached to topology entities.
Definition MeshTags.h:104
dolfinx::mesh::MeshTags::name
std::string name
Name.
Definition MeshTags.h:113
dolfinx::mesh::MeshTags::MeshTags
MeshTags(std::shared_ptr< const Topology > topology, int dim, U &&indices, V &&values)
Create a MeshTag from entities of given dimension on a mesh.
Definition MeshTags.h:50
dolfinx::mesh::MeshTags::dim
int dim() const
Return topological dimension of tagged entities.
Definition MeshTags.h:107
dolfinx::mesh::MeshTags::operator=
MeshTags & operator=(const MeshTags &tags)=default
Move assignment.
dolfinx::mesh::MeshTags::topology
std::shared_ptr< const Topology > topology() const
Return topology.
Definition MeshTags.h:110
dolfinx::mesh::MeshTags::find
std::vector< std::int32_t > find(const T value) const
Find all entities with a given tag value.
Definition MeshTags.h:86
dolfinx::common::sort_unique
std::pair< std::vector< typename U::value_type >, std::vector< typename V::value_type > > sort_unique(const U &indices, const V &values)
Sort two arrays based on the values in array indices.
Definition utils.h:34
dolfinx::mesh
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
dolfinx::mesh::create_meshtags
MeshTags< T > create_meshtags(std::shared_ptr< const Topology > topology, int dim, const graph::AdjacencyList< std::int32_t > &entities, std::span< const T > values)
Create MeshTags from arrays.
Definition MeshTags.h:138
dolfinx::mesh::entities_to_index
std::vector< std::int32_t > entities_to_index(const Topology &topology, int dim, std::span< const std::int32_t > entities)
Get entity indices for entities defined by their vertices.
Definition Topology.cpp:1396