1#ifndef DUNE_COPASI_GRID_CELL_DATA_HH
2#define DUNE_COPASI_GRID_CELL_DATA_HH
7#include <dune/grid/common/mcmgmapper.hh>
9#include <dune/grid/concepts/gridview.hh>
23template<Dune::Concept::Gr
idView GV,
class T>
28 using IndexSet =
typename GV::IndexSet;
37 using Entity =
typename GridView::template Codim<0>::Entity;
43 if (std::same_as<GridView, typename GridView::Grid::LevelGridView>) {
46 _grid_view_level.emplace(0);
60 if constexpr (std::same_as<GV, LeafGridView>) {
61 return std::make_unique<CellData<LeafGridView, T>>(*this);
63 if (_grid_view_level
and _grid_view_level.value() != 0)
66 "Leaf grid view cell data can only be made out of other leaf grid view (copy) or a 0-th level grid view (projection)");
69 auto leaf_gv = _index_mapper.gridView().grid().leafGridView();
70 auto leaf_cd = std::make_unique<CellData<LeafGridView, T>>(
leaf_gv);
78 for (std::size_t
id = 0;
id !=
sz; ++
id) {
119 template<std::ranges::input_range
R,
120 class Proj = std::identity,
121 std::indirect_unary_predicate<std::projected<std::ranges::iterator_t<R>,
Proj>>
Pred>
122 requires std::assignable_from<T&, std::ranges::range_value_t<R>>
130 for (std::size_t
i = 0;
i != _index_mapper.size(); ++
i, ++
it) {
132 _cell_data[
i *
cap +
ei] =
mask ? *
it : std::numeric_limits<T>::quiet_NaN();
137 "Range has a size of '{}', but there are '{}' entities in the container",
138 std::ranges::size(
r),
139 _index_mapper.size());
142 template<std::ranges::input_range R>
143 requires std::assignable_from<T&, std::ranges::range_value_t<R>>
147 key, std::forward<R>(
r), [](
auto&&) {
return std::true_type{}; }, std::identity{});
151 template<std::ranges::input_range
R,
152 class Proj = std::identity,
153 std::indirect_unary_predicate<std::projected<std::ranges::iterator_t<R>,
Proj>>
Pred>
154 requires std::assignable_from<T&, std::tuple_element_t<1, std::ranges::range_value_t<R>>>
161 auto imsz = _index_mapper.size();
162 for (
const auto& [
i, val] :
r) {
163 if(
i >= _index_mapper.size())
166 _cell_data[
i *
cap +
ei] =
mask ? val : std::numeric_limits<T>::quiet_NaN();
171 template<std::ranges::input_range R>
172 requires std::assignable_from<T&, std::tuple_element_t<1, std::ranges::range_value_t<R>>>
176 key, std::forward<R>(
r), [](
auto&&) {
return std::true_type{}; }, std::identity{});
182 std::vector<bool>& cell_mask)
const
188 cell_mask.resize(
sz);
190 for (std::size_t
id = 0;
id !=
sz; ++
id) {
202 return _cell_mask[
data_id] ? std::make_optional<T>(_cell_data[
data_id]) : std::nullopt;
206 std::optional<T>
getData(std::string_view key,
209 auto key_it = std::find(_keys.begin(), _keys.end(), key);
210 if (
key_it == _keys.end())
212 auto id = std::distance(_keys.begin(),
key_it);
217 template<Concept::IndexableEntity<IndexSet> E>
218 auto index(
const E&
entity)
const
220 if (_grid_view_level
and (*_grid_view_level !=
entity.level())) {
222 while (
_entity.level() > *_grid_view_level) {
225 if (
_entity.level() != *_grid_view_level)
227 "Invoking a cell data manager with an entity of lower level than "
228 "the grid view attached to it is an invalid operation!");
229 return _index_mapper.index(
_entity);
231 return _index_mapper.index(
entity);
235 void pushKey(std::string_view key)
237 auto key_it = std::find(_keys.begin(), _keys.end(), key);
238 if (
key_it != _keys.end())
241 _keys.push_back(std::string{ key });
254 void realloc(std::size_t
new_cap)
258 throw std::length_error{
"Requested raw capacity is smaller than current size" };
260 throw std::length_error{
"Requested capacity is bigger than maximum size" };
270 for (std::size_t
i = 0;
i != _index_mapper.size(); ++
i) {
290 IndexMapper _index_mapper;
293 std::optional<int> _grid_view_level;
295 std::vector<std::string> _keys;
297 std::vector<T> _cell_data;
298 std::vector<bool> _cell_mask;
Container for cell data of a grid view.
Definition cell_data.hh:25
void addData(std::string_view key, R &&r, Pred pred, Proj proj={})
Definition cell_data.hh:155
GV GridView
Definition cell_data.hh:34
CellData(GridView grid_view)
Definition cell_data.hh:39
void addData(std::string_view key, R &&r)
Definition cell_data.hh:144
void addData(std::string_view key, R &&r)
Definition cell_data.hh:173
std::size_t size() const noexcept
Number of elements per entity in the container.
Definition cell_data.hh:92
typename GridView::template Codim< 0 >::Entity Entity
Definition cell_data.hh:37
void getData(const Concept::IndexableEntity< IndexSet > auto &entity, std::vector< T > &cell_data, std::vector< bool > &cell_mask) const
Definition cell_data.hh:180
std::size_t max_size() const noexcept
Number of elements per entity that the container can ever hold container.
Definition cell_data.hh:95
std::optional< T > getData(std::string_view key, const Concept::IndexableEntity< IndexSet > auto &entity) const
Definition cell_data.hh:206
std::unique_ptr< CellData< typename GV::Grid::LeafGridView, T > > makeLeafGridViewCellData() const
creates a cell data object with a copy data for the leaf grid view
Definition cell_data.hh:58
std::size_t gridview_size() const noexcept
The size of the gridview - This gives the number of elements in the grid view.
Definition cell_data.hh:98
void addData(std::string_view key, R &&r, Pred pred, Proj proj={})
Definition cell_data.hh:123
const std::vector< std::string > & keys() const noexcept
Return the keys of the cell data.
Definition cell_data.hh:101
std::optional< T > getData(std::size_t id, const Concept::IndexableEntity< IndexSet > auto &entity) const
Definition cell_data.hh:197
void reserve(std::size_t new_cap)
Reservers new_cap elements per entity in the container.
Definition cell_data.hh:106
std::size_t capacity() const noexcept
Number of elements per entity that the container has currently allocated space for.
Definition cell_data.hh:89
void shrink_to_fit()
Definition cell_data.hh:112
Definition axis_names.hh:7
auto format_exception(Exception &&e, fmt::format_string< Args... > format, Args &&... args)
Definition exceptions.hh:23
constexpr bool is_bitflags_v
Alias for Bitflag indicator.
Definition bit_flags.hh:24