Debian Codegraph @

dune/localfunctions/common/virtualwrappers.hh

raw
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_LOCALFUNCTIONS_COMMON_VIRTUALWRAPPERS_HH
6 #define DUNE_LOCALFUNCTIONS_COMMON_VIRTUALWRAPPERS_HH
7
8 #include <array>
9
10 #include <dune/localfunctions/common/localbasis.hh>
11 #include <dune/localfunctions/common/localkey.hh>
12 #include <dune/localfunctions/common/virtualinterface.hh>
13
14 namespace Dune
15 {
16
17 // forward declaration needed by friend declarations
18 template<class Imp>
19 class LocalFiniteElementVirtualImp;
20
21 // default clone method is the copy constructor
22 template<class Imp, bool IsInterface>
23 struct LocalFiniteElementCloneFactoryHelper
24 {
25 static Imp* clone(const Imp& imp)
26 {
27 return new Imp(imp);
28 }
29 };
30
31 // if FE derives from virtual interface the clone method is used
32 template<class Imp>
33 struct LocalFiniteElementCloneFactoryHelper<Imp, true>
34 {
35 static Imp* clone(const Imp& imp)
36 {
37 return imp.clone();
38 }
39 };
40
41 // factory template to clone and create an objects
42 template<class Imp>
43 struct LocalFiniteElementCloneFactory
44 {
45 typedef LocalFiniteElementVirtualInterface<typename Imp::Traits::LocalBasisType::Traits> Interface;
46
47 static Imp* clone(const Imp& imp)
48 {
49 return LocalFiniteElementCloneFactoryHelper<Imp, std::is_base_of<Interface, Imp>::value>::clone(imp);
50 }
51
52 static Imp* create()
53 {
54 return new Imp;
55 }
56 };
57
58
59
60 // -----------------------------------------------------------------
61 // Basis
62 // -----------------------------------------------------------------
63
64
65
66 /**
67 * @brief class for wrapping a basis using the virtual interface
68 *
69 * @tparam T The LocalBasisTraits class
70 * @tparam Imp LocalBasisInterface implementation
71 */
72 template<class T , class Imp>
73 class LocalBasisVirtualImp
74 : public LocalBasisVirtualInterface<T>
75 {
76 template<class FEImp>
77 friend class LocalFiniteElementVirtualImp;
78
79 protected:
80
81 //! constructor taking an implementation of the interface
82 LocalBasisVirtualImp( const Imp &imp )
83 : impl_(imp)
84 {}
85
86 public:
87 using Traits = T;
88
89 //! @copydoc LocalBasisVirtualInterface::size
90 unsigned int size () const
91 {
92 return impl_.size();
93 }
94
95 //! @copydoc LocalBasisVirtualInterface::order
96 unsigned int order () const
97 {
98 return impl_.order();
99 }
100
101 //! @copydoc LocalBasisVirtualInterface::evaluateFunction
102 inline void evaluateFunction (const typename Traits::DomainType& in,
103 std::vector<typename Traits::RangeType>& out) const
104 {
105 impl_.evaluateFunction(in,out);
106 }
107
108 //! @copydoc LocalBasisVirtualInterface::evaluateJacobian
109 inline void evaluateJacobian(
110 const typename Traits::DomainType& in,
111 std::vector<typename Traits::JacobianType>& out) const
112 {
113 impl_.evaluateJacobian(in,out);
114 }
115
116 /** \brief Evaluate partial derivatives of any order of all shape functions
117 * \param order Order of the partial derivatives, in the classic multi-index notation
118 * \param in Position where to evaluate the derivatives
119 * \param[out] out Return value: the desired partial derivatives
120 */
121 void partial(const std::array<unsigned int,Traits::dimDomain>& order,
122 const typename Traits::DomainType& in,
123 std::vector<typename Traits::RangeType>& out) const
124 {
125 impl_.partial(order,in,out);
126 }
127
128 protected:
129 const Imp& impl_;
130 };
131
132
133
134 // -----------------------------------------------------------------
135 // Interpolation
136 // -----------------------------------------------------------------
137
138 /**
139 * @brief class for wrapping a local interpolation
140 * using the virtual interface
141 *
142 * @tparam DomainType domain type of the function to interpolate
143 * @tparam RangeType range type of the function to interpolate
144 * \tparam Imp LocalInterpolationVirtualInterface implementation
145 */
146 template<class DomainType, class RangeType, class Imp>
147 class LocalInterpolationVirtualImp
148 : public LocalInterpolationVirtualInterface< DomainType, RangeType >
149 {
150 template<class FEImp>
151 friend class LocalFiniteElementVirtualImp;
152
153 typedef LocalInterpolationVirtualInterface< DomainType, RangeType > Base;
154
155 protected:
156
157 //! constructor taking an implementation of the Dune::LocalInterpolationVirtualInterface
158 LocalInterpolationVirtualImp( const Imp &imp)
159 : impl_(imp) {}
160
161 public:
162
163 typedef typename Base::FunctionType FunctionType;
164
165 typedef typename Base::CoefficientType CoefficientType;
166
167 //! \copydoc LocalInterpolationVirtualInterface::interpolate
168 virtual void interpolate (const FunctionType& f, std::vector<CoefficientType>& out) const
169 {
170 impl_.interpolate(f,out);
171 }
172
173 protected:
174 const Imp& impl_;
175
176 };
177
178
179
180 // -----------------------------------------------------------------
181 // Coefficients
182 // -----------------------------------------------------------------
183
184 /**
185 * @brief class for wrapping local coefficients
186 * using the virtual interface
187 *
188 * @tparam Imp LocalCoefficientsInterface implementation
189 */
190 template<class Imp>
191 class LocalCoefficientsVirtualImp
192 : public LocalCoefficientsVirtualInterface
193 {
194 template<class FEImp>
195 friend class LocalFiniteElementVirtualImp;
196
197 protected:
198
199 //! constructor taking an implementation of the Dune::LocalCoefficientsVirtualInterface
200 LocalCoefficientsVirtualImp( const Imp &imp )
201 : impl_(imp)
202 {}
203
204 public:
205
206 //! @copydoc LocalCoefficientsVirtualInterface::size
207 std::size_t size () const
208 {
209 return impl_.size();
210 }
211
212 //! @copydoc LocalCoefficientsVirtualInterface::localKey
213 const LocalKey& localKey (std::size_t i) const
214 {
215 return impl_.localKey(i);
216 }
217
218 protected:
219 const Imp& impl_;
220
221 };
222
223
224
225 // -----------------------------------------------------------------
226 // Finite Element
227 // -----------------------------------------------------------------
228
229 /**
230 * @brief class for wrapping a finite element using the virtual interface
231 *
232 * This automatically inherits the differentiation order of the wrapped
233 * finite element and implements the corresponding interface
234 *
235 * @tparam Imp LocalBasisInterface implementation
236 */
237 template<class Imp>
238 class LocalFiniteElementVirtualImp
239 : public LocalFiniteElementVirtualInterface<typename Imp::Traits::LocalBasisType::Traits>
240 {
241 using LocalBasisTraits = typename Imp::Traits::LocalBasisType::Traits;
242 using Interface = LocalFiniteElementVirtualInterface<LocalBasisTraits>;
243
244 public:
245 typedef typename Interface::Traits Traits;
246
247 //! Constructor taking a LocalFiniteElementVirtualInterface implementation
248 LocalFiniteElementVirtualImp( const Imp &imp )
249 : impl_(LocalFiniteElementCloneFactory<Imp>::clone(imp)),
250 localBasisImp_(impl_->localBasis()),
251 localCoefficientsImp_(impl_->localCoefficients()),
252 localInterpolationImp_(impl_->localInterpolation())
253 {}
254
255 //! Default constructor. Assumes that the implementation class is default constructible as well.
256 LocalFiniteElementVirtualImp()
257 : impl_(LocalFiniteElementCloneFactory<Imp>::create()),
258 localBasisImp_(impl_->localBasis()),
259 localCoefficientsImp_(impl_->localCoefficients()),
260 localInterpolationImp_(impl_->localInterpolation())
261 {}
262
263 //! Copy constructor needed for deep copy
264 LocalFiniteElementVirtualImp(const LocalFiniteElementVirtualImp& other)
265 : impl_(LocalFiniteElementCloneFactory<Imp>::clone(*other.impl_)),
266 localBasisImp_(impl_->localBasis()),
267 localCoefficientsImp_(impl_->localCoefficients()),
268 localInterpolationImp_(impl_->localInterpolation())
269 {}
270
271 ~LocalFiniteElementVirtualImp()
272 {
273 delete impl_;
274 }
275
276 //! \copydoc LocalFiniteElementVirtualInterface::localBasis
277 const typename Traits::LocalBasisType& localBasis () const
278 {
279 return localBasisImp_;
280 }
281
282 //! \copydoc LocalFiniteElementVirtualInterface::localCoefficients
283 const typename Traits::LocalCoefficientsType& localCoefficients () const
284 {
285 return localCoefficientsImp_;
286 }
287
288 //! \copydoc LocalFiniteElementVirtualInterface::localInterpolation
289 const typename Traits::LocalInterpolationType& localInterpolation () const
290 {
291 return localInterpolationImp_;
292 }
293
294 /** \brief Number of shape functions in this finite element */
295 unsigned int size () const
296 {
297 return impl_->size();
298 }
299
300 //! \copydoc LocalFiniteElementVirtualInterface::type
301 const GeometryType type () const
302 {
303 return impl_->type();
304 }
305
306 /** \brief clone this wrapper
307 *
308 * This 'virtual copy constructor' is needed if you want to copy
309 * the wrapper through the virtual interface.
310 */
311 virtual LocalFiniteElementVirtualImp<Imp>* clone() const
312 {
313 return new LocalFiniteElementVirtualImp<Imp>(*this);
314 }
315
316 protected:
317 const Imp* impl_;
318
319 const LocalBasisVirtualImp<LocalBasisTraits, typename Imp::Traits::LocalBasisType> localBasisImp_;
320 const LocalCoefficientsVirtualImp<typename Imp::Traits::LocalCoefficientsType> localCoefficientsImp_;
321 const LocalInterpolationVirtualImp<typename LocalBasisTraits::DomainType,
322 typename LocalBasisTraits::RangeType,
323 typename Imp::Traits::LocalInterpolationType> localInterpolationImp_;
324 };
325 }
326 #endif