dune-mmesh (unstable)

persistentcontainer.hh
1#ifndef DUNE_MMESH_MISC_PERSISTENTCONTAINER_HH
2#define DUNE_MMESH_MISC_PERSISTENTCONTAINER_HH
3
4#include <dune/grid/utility/persistentcontainer.hh>
5#include <dune/grid/utility/persistentcontainermap.hh>
6#include <unordered_map>
7
8namespace Dune {
9
10template <class HostGrid, int dim, class T>
11class PersistentContainer<MMesh<HostGrid, dim>, T>
12 : public Dune::PersistentContainerMap<
13 MMesh<HostGrid, dim>, typename MMesh<HostGrid, dim>::LocalIdSet,
14 std::unordered_map<typename MMesh<HostGrid, dim>::LocalIdSet::IdType,
15 T> > {
16 typedef MMesh<HostGrid, dim> G;
17 typedef PersistentContainerMap<
18 G, typename G::LocalIdSet,
19 std::unordered_map<typename G::LocalIdSet::IdType, T> >
20 Base;
21 typedef typename MMesh<HostGrid, dim>::LocalIdSet IdSet;
22 typedef std::unordered_map<typename MMesh<HostGrid, dim>::LocalIdSet::IdType,
23 T>
24 Map;
25
26 public:
27 typedef typename Base::Grid Grid;
28 typedef typename Base::Value Value;
29
30 PersistentContainer(const Grid &grid, int codim, const Value &value = Value())
31 : Base(grid, codim, grid.localIdSet(), value) {}
32
33 using Base::codimension;
34 using Base::migrateLevel;
35
36 void resize(const Value &value = Value()) {
37 if (sequence_ != this->grid_->sequence()) {
38 Hybrid::forEach(std::make_index_sequence<Grid::dimension + 1>{},
39 [&](auto i) {
40 if (i == this->codimension())
41 this->template resize<i>(value);
42 });
43 sequence_ = this->grid_->sequence();
44 }
45 }
46
47 template <int codim>
48 void resize(const Value &value) {
49 std::integral_constant<bool, Capabilities::hasEntity<Grid, codim>::v>
50 hasEntity;
51 assert(codim == codimension());
52
53 // add one id for caching entity during adaptation
54 if constexpr (codim == 0) {
55 MMeshImpl::MultiId id(
56 {std::size_t(-4), std::size_t(-3), std::size_t(-2)});
57 this->data_.insert(std::make_pair(id, value));
58 }
59
60 // add one id for a every caching codim 1 entity during adaptation
61 if constexpr (codim == 1) {
62 MMeshImpl::MultiId eid0({std::size_t(-4), std::size_t(-3)});
63 this->data_.insert(std::make_pair(eid0, value));
64 MMeshImpl::MultiId eid1({std::size_t(-4), std::size_t(-2)});
65 this->data_.insert(std::make_pair(eid1, value));
66 MMeshImpl::MultiId eid2({std::size_t(-3), std::size_t(-2)});
67 this->data_.insert(std::make_pair(eid2, value));
68 }
69
70 // add one id for every caching vertex during adaptation
71 if constexpr (codim == dim) {
72 MMeshImpl::MultiId vid0(std::size_t(-4));
73 this->data_.insert(std::make_pair(vid0, value));
74 MMeshImpl::MultiId vid1(std::size_t(-3));
75 this->data_.insert(std::make_pair(vid1, value));
76 MMeshImpl::MultiId vid2(std::size_t(-2));
77 this->data_.insert(std::make_pair(vid2, value));
78 }
79
80 // create empty map, but keep old data
81 Map data;
82
83 // add new entries
84 this->template migrateLevel<codim>(0, value, data, hasEntity);
85 }
86
87 private:
88 int sequence_ = -1;
89};
90
91template <class MMesh, class T>
92class PersistentContainer<MMeshInterfaceGrid<MMesh>, T>
93 : public Dune::PersistentContainerMap<
94 MMeshInterfaceGrid<MMesh>,
95 typename MMeshInterfaceGrid<MMesh>::LocalIdSet,
96 std::unordered_map<
97 typename MMeshInterfaceGrid<MMesh>::LocalIdSet::IdType, T> > {
98 typedef MMeshInterfaceGrid<MMesh> G;
99 typedef PersistentContainerMap<
100 G, typename G::LocalIdSet,
101 std::unordered_map<typename G::LocalIdSet::IdType, T> >
102 Base;
103 typedef typename MMeshInterfaceGrid<MMesh>::LocalIdSet IdSet;
104 typedef std::unordered_map<
105 typename MMeshInterfaceGrid<MMesh>::LocalIdSet::IdType, T>
106 Map;
107 static constexpr int dim = MMesh::dimensionworld - 1;
108
109 public:
110 typedef typename Base::Grid Grid;
111 typedef typename Base::Value Value;
112
113 PersistentContainer(const Grid &grid, int codim, const Value &value = Value())
114 : Base(grid, codim, grid.localIdSet(), value) {}
115
116 using Base::codimension;
117 using Base::migrateLevel;
118
119 void resize(const Value &value = Value()) {
120 if (sequence_ != this->grid_->sequence()) {
121 Hybrid::forEach(std::make_index_sequence<Grid::dimension + 1>{},
122 [&](auto i) {
123 if (i == this->codimension())
124 this->template resize<i>(value);
125 });
126 sequence_ = this->grid_->sequence();
127 }
128 }
129
130 template <int codim>
131 void resize(const Value &value) {
132 std::integral_constant<bool, Capabilities::hasEntity<Grid, codim>::v>
133 hasEntity;
134 assert(codim == codimension());
135
136 // add one id for caching entity during adaptation
137 if constexpr (codim == 0) {
138 MMeshImpl::MultiId id({std::size_t(-3), std::size_t(-2)});
139 this->data_.insert(std::make_pair(id, value));
140 }
141
142 // add one id for every caching vertex during adaptation
143 if constexpr (codim == dim) {
144 MMeshImpl::MultiId vid0(std::size_t(-3));
145 this->data_.insert(std::make_pair(vid0, value));
146 MMeshImpl::MultiId vid1(std::size_t(-2));
147 this->data_.insert(std::make_pair(vid1, value));
148 }
149
150 // create empty map, but keep old data
151 Map data;
152
153 // add new entries
154 this->template migrateLevel<codim>(0, value, data, hasEntity);
155 }
156
157 private:
158 int sequence_ = -1;
159};
160
161} // namespace Dune
162
163#endif // #ifndef DUNE_MMESH_MISC_PERSISTENTCONTAINER_HH
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 5, 22:35, 2025)