AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
BasisMapping.hpp
1#pragma once
2
3#include <cassert>
4#include <map>
5#include <vector>
6
7#include <amdis/Observer.hpp>
8#include <amdis/typetree/MultiIndex.hpp>
9#include <dune/common/shared_ptr.hh>
10
11namespace AMDiS
12{
14
19 template <class SourceBasis, class TargetBasis,
20 class Storage = std::map<typename SourceBasis::MultiIndex, typename TargetBasis::MultiIndex>>
22 : private ObserverSequence<event::adapt,2>
23 {
24 public:
25 using SourceBasisType = SourceBasis;
26 using TargetBasisType = TargetBasis;
27
28 BasisMapping(std::shared_ptr<SourceBasis> const& source, std::shared_ptr<TargetBasis> const& target)
29 : ObserverSequence<event::adapt,2>(*source, *target)
30 , source_(source)
31 , target_(target)
32 {
33 // require the grids to be the same
34 assert(&source_->gridView().grid() == &target_->gridView().grid());
35
36 // calculate an initial mapping
37 updateImpl2();
38 }
39
40 BasisMapping(SourceBasis& source, TargetBasis& target)
41 : BasisMapping(Dune::stackobject_to_shared_ptr(source), Dune::stackobject_to_shared_ptr(target))
42 {}
43
44 protected:
45
46 void updateImpl(event::adapt e, index_t<0> i) override { if (update_) updateImpl2(); else update_ = true; }
47 void updateImpl(event::adapt e, index_t<1> i) override { if (update_) updateImpl2(); else update_ = true; }
48
49 private:
50
51 // Construct a mapping from the indices of the source basis to the indices of the target basis
52 void updateImpl2()
53 {
54 mapping_.clear();
55 resize(mapping_);
56
57 auto sourceLocalView = source_->localView();
58 auto targetLocalView = target_->localView();
59 for (auto const& e : elements(source_->gridView())) {
60 sourceLocalView.bind(e);
61 targetLocalView.bind(e);
62
63 assert(sourceLocalView.size() == targetLocalView.size());
64 for (std::size_t i = 0, s = sourceLocalView.size(); i < s; ++i)
65 mapping_[sourceLocalView.index(i)] = targetLocalView.index(i);
66 }
67
68 update_ = false;
69 }
70
71 public:
73
77 typename TargetBasis::MultiIndex const& operator()(typename SourceBasis::MultiIndex const& mi) const
78 {
79 return mapping_.at(mi);
80 }
81
83 typename TargetBasis::MultiIndex const& operator()(std::size_t idx) const
84 {
86 typename SourceBasis::MultiIndex mi{idx};
87 return mapping_.at(mi);
88 }
89
91 TargetBasis const& sizeInfo() const
92 {
93 return *target_;
94 }
95
96
98 std::shared_ptr<SourceBasis> const& source() const
99 {
100 return source_;
101 }
102
104 std::shared_ptr<TargetBasis> const& target() const
105 {
106 return target_;
107 }
108
109 private:
110 // vector must be resized before assigning indices
111 template <class T, class A>
112 void resize(std::vector<T,A>& mapping) const
113 {
114 mapping.resize(source_->dimension());
115 }
116
117 // map or unordered_map do not need a resize
118 template <class Container>
119 void resize(Container& mapping) const
120 {
121 /* do nothing */
122 }
123
124 private:
125 std::shared_ptr<SourceBasis> source_;
126 std::shared_ptr<TargetBasis> target_;
127 Storage mapping_;
128
129 bool update_ = false;
130 };
131
132} // end namespace AMDiS
Provide mapping from MultiIndex of source basis to MultiIndex of target basis.
Definition BasisMapping.hpp:23
std::shared_ptr< SourceBasis > const & source() const
Return the source basis.
Definition BasisMapping.hpp:98
TargetBasis::MultiIndex const & operator()(std::size_t idx) const
Map a flat source into to a target index.
Definition BasisMapping.hpp:83
TargetBasis const & sizeInfo() const
Return the target basis as a size info object.
Definition BasisMapping.hpp:91
std::shared_ptr< TargetBasis > const & target() const
Return the target basis.
Definition BasisMapping.hpp:104
TargetBasis::MultiIndex const & operator()(typename SourceBasis::MultiIndex const &mi) const
Map a source index to a target index.
Definition BasisMapping.hpp:77
Definition MultiIndex.hpp:13
Definition Observer.hpp:25