AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
DOFVector.hpp
1#pragma once
2
3#include <memory>
4#include <utility>
5
6#include <dune/common/concept.hh>
7#include <dune/common/shared_ptr.hh>
8#include <dune/functions/functionspacebases/concepts.hh>
9
10#include <amdis/DataTransfer.hpp>
11#include <amdis/LinearAlgebra.hpp>
12#include <amdis/Observer.hpp>
13#include <amdis/common/Concepts.hpp>
14#include <amdis/common/DerivativeTraits.hpp>
15#include <amdis/common/TypeTraits.hpp>
16#include <amdis/datatransfers/InterpolationDataTransfer.hpp>
17#include <amdis/functions/GlobalBasis.hpp>
18#include <amdis/typetree/TreePath.hpp>
19
20namespace AMDiS
21{
22 // forward declaration
23 template <class, class, class, class>
24 class DiscreteFunction;
25
26 // forward declaration
27 template <class, class>
28 class DerivativeGridFunction;
29
30
32
37 template <class GB, class T = double, class Traits = BackendTraits>
39 : public VectorFacade<T, Traits::template Vector<GB>::template Impl>
40 , private Observer<event::preAdapt>
41 , private Observer<event::adapt>
42 , private Observer<event::postAdapt>
43 {
44 using Self = DOFVector;
45
46 template <class, class, class, class>
47 friend class DiscreteFunction;
48
49 public:
51 using GlobalBasis = GB;
52
55
57 using size_type = typename GlobalBasis::size_type;
58
60 using value_type = T;
61
62 template <class Self>
63 static auto& coefficients(Self& self)
64 {
65 if constexpr(std::is_const_v<Self>)
66 return static_cast<Coefficients const&>(self);
67 else
68 return static_cast<Coefficients&>(self);
69 }
70
71 public:
74 template <class DataTransferTag = tag::interpolation_datatransfer>
75 DOFVector(std::shared_ptr<GB> const& basis, DataTransferTag = {})
77 , Observer<event::preAdapt>(basis->gridView().grid())
79 , Observer<event::postAdapt>(basis->gridView().grid())
80 , dataTransfer_(DataTransferFactory<DataTransferTag>::create(*basis, coefficients(*this)))
81 , basis_(basis)
82 {
83 resizeZero();
84 }
85
88 template <class GB_, class DataTransferTag = tag::interpolation_datatransfer,
89 REQUIRES(Concepts::Similar<GB_,GB>)>
90 DOFVector(GB_&& basis, DataTransferTag tag = {})
91 : DOFVector(Dune::wrap_or_move(FWD(basis)), tag)
92 {}
93
96 template <class GV, class PBF, class DataTransferTag = tag::interpolation_datatransfer,
97 REQUIRES(Concepts::PreBasisFactory<PBF, GV>)>
98 DOFVector(GV const& gridView, PBF const& preBasisFactory, DataTransferTag tag = {})
99 : DOFVector(std::make_shared<GB>(gridView, preBasisFactory), tag)
100 {}
101
103 GlobalBasis const& basis() const
104 {
105 return *basis_;
106 }
107
108 Coefficients const& coefficients() const
109 {
110 return coefficients(*this);
111 }
112
113 Coefficients& coefficients()
114 {
115 return coefficients(*this);
116 }
117
119 void backup(std::string const& filename);
120
122 void restore(std::string const& filename);
123
124 void resize()
125 {
126 Coefficients::resize(*basis_);
127 }
128
130
131 void resizeZero()
132 {
134 }
135
137
139 auto const& dataTransfer() const
140 {
141 return dataTransfer_;
142 }
143
146 {
147 return dataTransfer_;
148 }
149
152 {
153 dataTransfer_ = std::move(dataTransfer);
154 }
155
157 template <class DataTransferTag>
158 void setDataTransfer(DataTransferTag tag)
159 {
161 }
162
163
164 public: // functions to transform the DOFVector into a grid-function
165
167 template <class Range = void, class... Indices>
168 friend auto valueOf(DOFVector& dofVec, Indices... ii)
169 {
170 using TP = TYPEOF(makeTreePath(ii...));
172 return DF{dofVec.coefficients(), dofVec.basis_, makeTreePath(ii...)};
173 }
174
176 template <class Range = void, class... Indices>
177 friend auto valueOf(DOFVector const& dofVec, Indices... ii)
178 {
179 using TP = TYPEOF(makeTreePath(ii...));
181 return DF{dofVec.coefficients(), dofVec.basis_, makeTreePath(ii...)};
182 }
183
184 template <class Type>
185 auto makeDerivative(Type type) const
186 {
187 using TP = TYPEOF(makeTreePath());
190 DF{coefficients(), basis_, makeTreePath()}, type};
191 }
192
195 template <class Type>
196 friend auto derivativeOf(DOFVector const& dofVec, Type type)
197 {
198 return dofVec.makeDerivative(type);
199 }
200
202 friend auto gradientOf(DOFVector const& dofVec)
203 {
204 return dofVec.makeDerivative(tag::gradient{});
205 }
206
208 friend auto divergenceOf(DOFVector const& dofVec)
209 {
210 return dofVec.makeDerivative(tag::divergence{});
211 }
212
214 friend auto partialDerivativeOf(DOFVector const& dofVec, std::size_t comp)
215 {
216 return dofVec.makeDerivative(tag::partial{comp});
217 }
218
219 protected:
220
224 {
225 dataTransfer_.preAdapt(*basis_, coefficients(), e.value);
226 }
227
230 void updateImpl(event::adapt e) override
231 {
232 assert(e.value);
233 resize();
234 dataTransfer_.adapt(*basis_, coefficients());
235 }
236
240 {
241 dataTransfer_.postAdapt(coefficients());
242 }
243
244 private:
247 DataTransfer<GB,Coefficients> dataTransfer_;
248
249 std::shared_ptr<GlobalBasis const> basis_;
250 };
251
252
253 // deduction guides
254 template <class GB>
255 DOFVector(GB&& basis)
256 -> DOFVector<Underlying_t<GB>>;
257
258 template <class GB, class DataTransferTag,
259 REQUIRES(std::is_base_of_v<tag::datatransfer, DataTransferTag>)>
260 DOFVector(GB&& basis, DataTransferTag)
261 -> DOFVector<Underlying_t<GB>>;
262
263 template <class GV, class PBF,
264 REQUIRES(Concepts::PreBasisFactory<PBF, GV>)>
265 DOFVector(GV const& gridView, PBF const& pbf)
266 -> DOFVector<decltype(GlobalBasis{gridView,pbf})>;
267
268 template <class GV, class PBF, class DataTransferTag,
269 REQUIRES(Concepts::PreBasisFactory<PBF, GV>),
270 REQUIRES(std::is_base_of_v<tag::datatransfer, DataTransferTag>)>
271 DOFVector(GV const& gridView, PBF const& pbf, DataTransferTag)
272 -> DOFVector<decltype(GlobalBasis{gridView,pbf})>;
273
274
276
285 template <class ValueType = double, class GB,
286 class DataTransferTag = tag::interpolation_datatransfer>
287 DOFVector<Underlying_t<GB>, ValueType>
288 makeDOFVector(GB&& basis, DataTransferTag tag = {})
289 {
290 return {FWD(basis), tag};
291 }
292
293} // end namespace AMDiS
294
295#include <amdis/DOFVector.inc.hpp>
The basic container that stores a base vector and a corresponding basis.
Definition: DOFVector.hpp:43
void updateImpl(event::preAdapt e) override
Definition: DOFVector.hpp:223
friend auto derivativeOf(DOFVector const &dofVec, Type type)
Definition: DOFVector.hpp:196
friend auto gradientOf(DOFVector const &dofVec)
Return a derivative grid-function representing the gradient (jacobian) of the DOFVector.
Definition: DOFVector.hpp:202
void updateImpl(event::postAdapt) override
Definition: DOFVector.hpp:239
VectorFacade< T, Traits::template Vector< GB >::template Impl > Coefficients
The internal coefficient vector storage.
Definition: DOFVector.hpp:54
friend auto partialDerivativeOf(DOFVector const &dofVec, std::size_t comp)
Return a derivative grid-function representing the partial derivative wrt. the components comp of the...
Definition: DOFVector.hpp:214
void setDataTransfer(DataTransfer< GB, Coefficients > dataTransfer)
Assign the DataTransfer object.
Definition: DOFVector.hpp:151
friend auto valueOf(DOFVector const &dofVec, Indices... ii)
A Generator to transform a const DOFVector into a DiscreteFunction.
Definition: DOFVector.hpp:177
void restore(std::string const &filename)
Read backup data from file.
Definition: DOFVector.inc.hpp:40
void setDataTransfer(DataTransferTag tag)
Create a new DataTransfer object based on the operation type.
Definition: DOFVector.hpp:158
DOFVector(std::shared_ptr< GB > const &basis, DataTransferTag={})
Definition: DOFVector.hpp:75
GB GlobalBasis
A global basis associated to the coefficients.
Definition: DOFVector.hpp:51
typename GlobalBasis::size_type size_type
The index/size - type.
Definition: DOFVector.hpp:57
friend auto divergenceOf(DOFVector const &dofVec)
Return a derivative grid-function representing the divergence of the DOFVector.
Definition: DOFVector.hpp:208
void backup(std::string const &filename)
Write DOFVector to file.
Definition: DOFVector.inc.hpp:15
friend auto valueOf(DOFVector &dofVec, Indices... ii)
A Generator to transform a DOFVector into a DiscreteFunction.
Definition: DOFVector.hpp:168
auto & dataTransfer()
Return the associated DataTransfer object.
Definition: DOFVector.hpp:145
void updateImpl(event::adapt e) override
Definition: DOFVector.hpp:230
DOFVector(GV const &gridView, PBF const &preBasisFactory, DataTransferTag tag={})
Definition: DOFVector.hpp:98
auto const & dataTransfer() const
Return the associated DataTransfer object.
Definition: DOFVector.hpp:139
GlobalBasis const & basis() const
Return the global basis.
Definition: DOFVector.hpp:103
T value_type
The type of the elements of the DOFVector.
Definition: DOFVector.hpp:60
DOFVector(GB_ &&basis, DataTransferTag tag={})
Definition: DOFVector.hpp:90
The base class for data transfer classes.
Definition: DataTransfer.hpp:68
A Gridfunction that returns the derivative when calling localFunction.
Definition: DerivativeGridFunction.hpp:45
A mutable view on the subspace of a DOFVector,.
Definition: DiscreteFunction.hpp:45
Implementation of the ObserverInterface.
Definition: Observer.hpp:104
The basic container that stores a base vector and a corresponding basis.
Definition: VectorFacade.hpp:39
void resize(Basis const &basis)
Resize the vector to the size of the basis.
Definition: VectorFacade.hpp:103
void resizeZero(Basis const &basis)
Resize the vector to the size of the basis and set to zero.
Definition: VectorFacade.hpp:110
constexpr bool GlobalBasis
A Dune::Functions::GlobalBasis type.
Definition: Concepts.hpp:189
Definition: DataTransfer.hpp:133
Definition: Observer.hpp:25
Definition: Observer.hpp:30
Definition: Observer.hpp:19
Definition: DerivativeTraits.hpp:21
Definition: DerivativeTraits.hpp:19
Definition: DerivativeTraits.hpp:22