AMDiS  2.10
The Adaptive Multi-Dimensional Simulation Toolbox
ElementVector.hpp
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 #include <optional>
6 #include <utility>
7 #include <vector>
8 
9 #include <dune/common/concept.hh>
10 #include <dune/common/shared_ptr.hh>
11 #include <amdis/Observer.hpp>
12 
13 namespace AMDiS
14 {
16 
20  template <class G, class T = double>
22  : private Observer<event::preAdapt>
23  , private Observer<event::adapt>
24  , private Observer<event::postAdapt>
25  {
26  using Self = ElementVector;
27 
28  public:
30  using Grid = G;
31 
33  using size_type = std::size_t;
34 
36  using value_type = T;
37 
39  using Data = std::vector<T>;
40 
41  public:
43  ElementVector(std::shared_ptr<G const> const& grid, T value = 0)
44  : Observer<event::preAdapt>(*grid)
45  , Observer<event::adapt>(*grid)
46  , Observer<event::postAdapt>(*grid)
47  , grid_(grid)
48  , data_(grid->size(0), value)
49  {}
50 
52  ElementVector(G const& grid, T value = 0)
53  : ElementVector(Dune::wrap_or_move(FWD(grid)), value)
54  {}
55 
57  auto gridView() const
58  {
59  return grid_->leafGridView();
60  }
61 
63  Data const& data() const
64  {
65  return data_;
66  }
67 
70  {
71  return data_;
72  }
73 
75  void resize()
76  {
77  data_.resize(grid_->size(0));
78  }
79 
81  void resizeZero()
82  {
83  data_.resize(grid_->size(0));
84  std::fill(data_.begin(), data_.end(), T(0));
85  }
86 
87  protected:
88 
90  void updateImpl(event::preAdapt e) override
91  {
92  persistentData_.emplace();
93 
94  // Store the data in a persistent container
95  auto const& indexSet = grid_->leafIndexSet();
96  auto const& idSet = grid_->localIdSet();
97  for (auto const& e : elements(grid_->leafGridView())) {
98  (*persistentData_)[idSet.id(e)] = data_[indexSet.index(e)];
99 
100  // store also the same value on all father elements
101  if (e.mightVanish() && e.hasFather()) {
102  T value = data_[indexSet.index(e)];
103  auto father = e.father();
104  for (; ; father = father.father()) {
105  (*persistentData_)[idSet.id(father)] = value;
106  if (!father.hasFather())
107  break;
108  }
109  }
110  }
111  }
112 
114  void updateImpl(event::adapt e) override
115  {
116  resize();
117 
118  // interpolate the data
119  auto const& indexSet = grid_->leafIndexSet();
120  auto const& idSet = grid_->localIdSet();
121  for (auto const& e : elements(grid_->leafGridView()))
122  {
123  T value = 0;
124  auto father = e;
125  if (e.isNew() && e.hasFather())
126  father = e.father();
127 
128  for (; ; father = father.father()) {
129  auto it = persistentData_->find(idSet.id(father));
130  if (it != persistentData_->end()) {
131  value = it->second;
132  break;
133  }
134  if (!father.hasFather())
135  break;
136  }
137 
138  data_[indexSet.index(e)] = value;
139  }
140  }
141 
144  {
145  persistentData_.reset();
146  }
147 
148  private:
149  std::shared_ptr<Grid const> grid_;
150  Data data_;
151 
152  std::optional<std::map<typename Grid::LocalIdSet::IdType, T>> persistentData_;
153  };
154 
155 } // end namespace AMDiS
ElementVector(std::shared_ptr< G const > const &grid, T value=0)
(1) Constructor. Stores the shared_ptr of the grid.
Definition: ElementVector.hpp:43
T value_type
The type of the elements of the ElementVector.
Definition: ElementVector.hpp:36
auto gridView() const
Return the GridView the data is defined on.
Definition: ElementVector.hpp:57
void resizeZero()
Resize the internal data without interpolating and set all values to 0.
Definition: ElementVector.hpp:81
void resize()
Resize the internal data without interpolating.
Definition: ElementVector.hpp:75
Definition: AdaptiveGrid.hpp:393
ElementVector(G const &grid, T value=0)
(2) Constructor. Forwards to (1) by wrapping into a shared_ptr.
Definition: ElementVector.hpp:52
Data const & data() const
Get a const-ref to the internal data vector.
Definition: ElementVector.hpp:63
std::vector< T > Data
The data container for the ElementVector.
Definition: ElementVector.hpp:39
Definition: AdaptBase.hpp:6
G Grid
Type of the grid.
Definition: ElementVector.hpp:30
An adaptive container that stores a value per grid element.
Definition: ElementVector.hpp:21
Implementation of the ObserverInterface.
Definition: Observer.hpp:102
Definition: Observer.hpp:25
Definition: Observer.hpp:30
void updateImpl(event::postAdapt) override
Implementation of Observer update(event::postAdapt) method.
Definition: ElementVector.hpp:143
std::size_t size_type
The index/size - type.
Definition: ElementVector.hpp:33
Definition: Observer.hpp:19
void updateImpl(event::preAdapt e) override
Implementation of Observer update(event::preAdapt) method.
Definition: ElementVector.hpp:90
void updateImpl(event::adapt e) override
Implementation of Observer update(event::adapt) method.
Definition: ElementVector.hpp:114
Data & data()
Get a ref to the internal data vector.
Definition: ElementVector.hpp:69