Debian Codegraph @

dune/localfunctions/meta/power/basis.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_META_POWER_BASIS_HH
7 #define DUNE_LOCALFUNCTIONS_META_POWER_BASIS_HH
8
9 #include <numeric>
10 #include <cstddef>
11 #include <vector>
12
13 #include <dune/common/fmatrix.hh>
14 #include <dune/common/fvector.hh>
15
16 namespace Dune {
17
18 //! Meta-basis turning a scalar basis into vector-valued basis
19 /**
20 * \ingroup LocalFunctionsImpl
21 *
22 * \tparam Backend Type of basis to take the power of.
23 * \tparam dimR Power to raise the basis to.
24 */
25 template<class Backend, std::size_t dimR>
26 class PowerBasis {
27 static_assert(Backend::Traits::dimRange == 1,
28 "PowerBasis works only with scalar backends");
29
30 // don't use a reference here so this class stays copyable
31 const Backend *backend;
32
33 public:
34 //! types of domain and range
35 struct Traits : public Backend::Traits
36 {
37 //! Dimension of the range values
38 static const std::size_t dimRange = dimR;
39 //! Type used for range values
40 typedef FieldVector<typename Traits::RangeField, dimR> Range;
41
42 //! Jacobian properties
43 /**
44 * \note The Jacobian should be some matrix type with \c dimRange x
45 * \c dimDomainGlobal components of type \c RangeField.
46 */
47 typedef FieldMatrix<typename Traits::RangeField, dimR,
48 Traits::dimDomainGlobal> Jacobian;
49 };
50
51 //! Construct a PowerBasis
52 /**
53 * \param backend_ Backend basis object to construct this object from.
54 * This object holds a reference to the backend object.
55 * This reference is also copied when this object is
56 * copied.
57 */
58 PowerBasis(const Backend &backend_) : backend(&backend_) { }
59
60 //! Number of shape functions
61 std::size_t size () const { return backend->size()*dimR; }
62 //! Polynomial order of the shape functions for quadrature
63 std::size_t order () const { return backend->order(); }
64
65 //! Evaluate all shape functions at given position
66 void evaluateFunction(const typename Traits::DomainLocal& in,
67 std::vector<typename Traits::Range>& out) const
68 {
69 std::vector<typename Backend::Traits::Range> backendValues;
70 backend->evaluateFunction(in, backendValues);
71 out.assign(size(), typename Traits::Range(0));
72 for(std::size_t d = 0; d < dimR; ++d)
73 for(std::size_t i = 0; i < backend->size(); ++i)
74 out[d*backend->size()+i][d] = backendValues[i][0];
75 }
76
77 //! Evaluate Jacobian of all shape functions at given position
78 void evaluateJacobian(const typename Traits::DomainLocal& in,
79 std::vector<typename Traits::Jacobian>& out) const
80 {
81 std::vector<typename Backend::Traits::Jacobian> backendValues;
82 backend->evaluateJacobian(in, backendValues);
83 out.assign(size(), typename Traits::Jacobian(0));
84 for(std::size_t d = 0; d < dimR; ++d)
85 for(std::size_t i = 0; i < backend->size(); ++i)
86 out[d*backend->size()+i][d] = backendValues[i][0];
87 }
88
89 //! \brief Evaluate partial derivatives of all shape functions
90 void partial (const std::array<unsigned int, Backend::Traits::dimDomainGlobal>& order,
91 const typename Traits::DomainLocal& in, // position
92 std::vector<typename Traits::Range>& out) const // return value
93 {
94 auto totalOrder = std::accumulate(order.begin(), order.end(), 0);
95 if (totalOrder == 0) {
96 evaluateFunction(in, out);
97 } else {
98 DUNE_THROW(NotImplemented, "Desired derivative order is not implemented");
99 }
100 }
101 };
102
103 } // namespace Dune
104
105 #endif // DUNE_LOCALFUNCTIONS_META_POWER_BASIS_HH