Debian Codegraph @

dune/localfunctions/common/interfaceswitch.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
6 #ifndef DUNE_LOCALFUNCTIONS_COMMON_INTERFACESWITCH_HH
7 #define DUNE_LOCALFUNCTIONS_COMMON_INTERFACESWITCH_HH
8
9 #include <cstddef>
10 #include <memory>
11 #include <vector>
12
13 #include <dune/common/fmatrix.hh>
14 #include <dune/common/typetraits.hh>
15 #include <dune/common/shared_ptr.hh>
16
17 namespace Dune {
18
19 //! \brief Switch for uniform treatment of finite element with either the
20 //! local or the global interface
21 /**
22 * \tparam FiniteElement Type of the finite element to handle.
23 * \tparam Dummy Dummy parameter for enable_if. This must be left
24 * at the default value of \c void.
25 *
26 * \note The local interface is detected by the presence of the type
27 * FiniteElement::Traits::LocalBasisType.
28 */
29 template<class FiniteElement, class Dummy = void>
30 struct FiniteElementInterfaceSwitch {
31 //! export the type of the basis
32 typedef typename FiniteElement::Traits::Basis Basis;
33 //! export the type of the interpolation
34 typedef typename FiniteElement::Traits::Interpolation Interpolation;
35 //! export the type of the coefficients
36 typedef typename FiniteElement::Traits::Coefficients Coefficients;
37
38 //! access basis
39 static const Basis &basis(const FiniteElement& fe)
40 { return fe.basis(); }
41 //! access interpolation
42 static const Interpolation &interpolation(const FiniteElement& fe)
43 { return fe.interpolation(); }
44 //! access coefficients
45 static const Coefficients &coefficients(const FiniteElement& fe)
46 { return fe.coefficients(); }
47
48 //! Type for storing finite elements
49 /**
50 * Some algorithms use one variable to store (as a shared pointer)
51 * a finite element and update that pointer while iterating
52 * through the grid. This works well as long as there is only a
53 * moderate number of different finite elements, which can be
54 * stored somewhere and don't change for the duration of the
55 * algorithm. This is the case for most local finite elements,
56 * since they exists in a finite number of variants.
57 *
58 * If the number of possible finite element realizations grows to
59 * big, e.g. for global finite elements or also for p-adaptive
60 * local finite elements, these are only created on the
61 * fly. Therefore we need to store a copy. Since finite elements
62 * in general are not assignable, we either copy-construct or
63 * move-construt them for each grid element we visit.
64 *
65 * To accommodate both interfaces, we store in a `shared_ptr`.
66 * Different ways to initialize a possible, from an l-value
67 * reference, an r-value reference or from a shared_ptr.
68 *
69 * For backwards compatibility we assume that an l-value reference
70 * to a local finite element is persistent and that we can simply
71 * store the pointer using `stackobject_to_shared_ptr`, while in
72 * the case of global finite elements we always need to copy
73 * construct using `make_shared`. If a local finite element is not
74 * persistent, it should be passed in as an r-value
75 * reference. Access to the finite element is done by simply
76 * dereferencing the store in both cases.
77 */
78 typedef std::shared_ptr<const FiniteElement> Store;
79 //! Store a finite element in the store.
80 /**
81 * For local finite elements this means storing the address of the passed
82 * reference, for global finite element this means creating a new object
83 * with allocation and copy-construction and storing that.
84 */
85 static void setStore(Store& store, const FiniteElement& fe)
86 { store = std::make_shared<const FiniteElement>(fe); }
87 //! Store a finite element in the store.
88 static void setStore(Store& store, FiniteElement&& fe)
89 { store = std::make_shared<const FiniteElement>(std::move(fe)); }
90 //! Store a finite element in the store.
91 static void setStore(Store& store, const Store& fe)
92 { store = fe; }
93 };
94
95 #ifndef DOXYGEN
96 //! \brief Switch for uniform treatment of finite element with either the
97 //! local or the global interface
98 template<class FiniteElement>
99 struct FiniteElementInterfaceSwitch<
100 FiniteElement,
101 typename std::enable_if<AlwaysTrue<typename FiniteElement::Traits::
102 LocalBasisType>::value>::type
103 >
104 {
105 //! export the type of the basis
106 typedef typename FiniteElement::Traits::LocalBasisType Basis;
107 //! export the type of the interpolation
108 typedef typename FiniteElement::Traits::LocalInterpolationType
109 Interpolation;
110 //! export the type of the coefficients
111 typedef typename FiniteElement::Traits::LocalCoefficientsType Coefficients;
112
113 //! access basis
114 static const Basis &basis(const FiniteElement& fe)
115 { return fe.localBasis(); }
116 //! access interpolation
117 static const Interpolation &interpolation(const FiniteElement& fe)
118 { return fe.localInterpolation(); }
119 //! access coefficients
120 static const Coefficients &coefficients(const FiniteElement& fe)
121 { return fe.localCoefficients(); }
122
123 //! Type for storing finite elements
124 typedef std::shared_ptr<const FiniteElement> Store;
125 //! Store a finite element in the store.
126 static void setStore(Store& store, const FiniteElement& fe)
127 { store = stackobject_to_shared_ptr<const FiniteElement>(fe); }
128 //! Store a finite element in the store.
129 static void setStore(Store& store, FiniteElement&& fe)
130 { store = std::make_shared<const FiniteElement>(std::move(fe)); }
131 //! Store a finite element in the store.
132 static void setStore(Store& store, const Store& fe)
133 { store = fe; }
134 };
135 #endif // !DOXYGEN
136
137 //! Switch for uniform treatment of local and global basis classes
138 /**
139 * \tparam Basis Type of the basis to handle.
140
141 * \tparam Dummy Dummy parameter for enable_if. This must be left at the
142 * default value of \c void.
143 *
144 * We don't provide any uniform access to the types and constants pertaining
145 * to the global domain. Providing this would require the Geometry as
146 * template parameter as well, and the user code can build them itself if it
147 * needs them with the help of the geometry. The omitted types are \c
148 * DomainGlobal and \c Jacobian, the omitted constant is \c dimDomainGlobal.
149 *
150 * \note The local interface is assumed if the constant
151 * Basis::Traits::dimDomain exists and has a value greater than 0.
152 */
153 template<class Basis, class Dummy = void>
154 struct BasisInterfaceSwitch {
155 //! export field types of the coordinates
156 typedef typename Basis::Traits::DomainField DomainField;
157 //! export dimension of local coordinates
158 static const std::size_t dimDomainLocal = Basis::Traits::dimDomainLocal;
159 //! export vector type of the local coordinates
160 typedef typename Basis::Traits::DomainLocal DomainLocal;
161
162 //! export field type of the values
163 typedef typename Basis::Traits::RangeField RangeField;
164 //! export dimension of the values
165 static const std::size_t dimRange = Basis::Traits::dimRange;
166 //! export vector type of the values
167 typedef typename Basis::Traits::Range Range;
168
169 //! Compute global gradient for scalar valued bases
170 /**
171 * \param basis The basis to get the derivatives from.
172 * \param geometry The geometry to use to transform the derivatives (for a
173 * local basis, unused in the case of a global basis).
174 * \param xl The local coordinates where to evaluate the gradient.
175 * \param grad The result (will be resized to the appropriate number
176 * of entries.
177 *
178 * \note This make sense only for a scalar valued basis.
179 */
180 template<typename Geometry>
181 static void gradient(const Basis& basis, const Geometry& geometry,
182 const DomainLocal& xl,
183 std::vector<FieldMatrix<RangeField, 1,
184 Geometry::coorddimension> >& grad)
185 {
186 grad.resize(basis.size());
187 basis.evaluateJacobian(xl, grad);
188 }
189 };
190
191 #ifndef DOXYGEN
192 //! Switch for uniform treatment of local and global basis classes
193 template<class Basis>
194 struct BasisInterfaceSwitch<Basis,
195 typename std::enable_if<
196 AlwaysTrue<
197 std::integral_constant<
198 std::size_t,
199 Basis::Traits::dimDomain
200 >
201 >::value
202 >::type
203 >
204 {
205 //! export field types of the coordinates
206 typedef typename Basis::Traits::DomainFieldType DomainField;
207 //! export dimension of local coordinates
208 static const std::size_t dimDomainLocal = Basis::Traits::dimDomain;
209 //! export vector type of the local coordinates
210 typedef typename Basis::Traits::DomainType DomainLocal;
211
212 //! export field type of the values
213 typedef typename Basis::Traits::RangeFieldType RangeField;
214 //! export dimension of the values
215 static const std::size_t dimRange = Basis::Traits::dimRange;
216 //! export vector type of the values
217 typedef typename Basis::Traits::RangeType Range;
218
219 //! Compute global gradient for scalar valued bases
220 template<typename Geometry>
221 static void gradient(const Basis& basis, const Geometry& geometry,
222 const DomainLocal& xl,
223 std::vector<FieldMatrix<RangeField, 1,
224 Geometry::coorddimension> >& grad)
225 {
226 std::vector<typename Basis::Traits::JacobianType> lgrad(basis.size());
227 basis.evaluateJacobian(xl, lgrad);
228
229 const typename Geometry::JacobianInverseTransposed& jac =
230 geometry.jacobianInverseTransposed(xl);
231
232 grad.resize(basis.size());
233 for(std::size_t i = 0; i < basis.size(); ++i)
234 jac.mv(lgrad[i][0], grad[i][0]);
235 }
236 };
237 #endif // !DOXYGEN
238
239 } // namespace Dune
240
241 #endif // DUNE_LOCALFUNCTIONS_COMMON_INTERFACESWITCH_HH