Debian Codegraph @

dune/localfunctions/whitney/edges0.5/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_WHITNEY_EDGES0_5_BASIS_HH
7 #define DUNE_LOCALFUNCTIONS_WHITNEY_EDGES0_5_BASIS_HH
8
9 #include <cstddef>
10 #include <vector>
11
12 #include <dune/common/fmatrix.hh>
13 #include <dune/common/fvector.hh>
14
15 #include <dune/localfunctions/common/localtoglobaladaptors.hh>
16 #include <dune/localfunctions/lagrange/lagrangesimplex.hh>
17 #include <dune/localfunctions/whitney/edges0.5/common.hh>
18
19 namespace Dune {
20
21 //////////////////////////////////////////////////////////////////////
22 //
23 // Basis
24 //
25
26 //! Basis for order 0.5 (lowest order) edge elements on simplices
27 /**
28 * \ingroup WhitneyImpl
29 *
30 * \tparam Geometry Type of the local-to-global map.
31 * \tparam RF Type to represent the field in the range.
32 *
33 * \nosubgrouping
34 */
35 template<class Geometry, class RF>
36 class EdgeS0_5Basis :
37 private EdgeS0_5Common<Geometry::mydimension, typename Geometry::ctype>
38 {
39 public:
40 //! \brief export type traits for function signature
41 struct Traits {
42 typedef typename Geometry::ctype DomainField;
43 static const std::size_t dimDomainLocal = Geometry::mydimension;
44 static const std::size_t dimDomainGlobal = Geometry::coorddimension;
45 typedef FieldVector<DomainField, dimDomainLocal> DomainLocal;
46 typedef FieldVector<DomainField, dimDomainGlobal> DomainGlobal;
47
48 typedef RF RangeField;
49 static const std::size_t dimRange = dimDomainLocal;
50 typedef FieldVector<RangeField, dimRange> Range;
51
52 typedef FieldMatrix<RangeField, dimRange, dimDomainGlobal> Jacobian;
53 };
54
55 private:
56 typedef Dune::Impl::LagrangeSimplexLocalBasis<typename Traits::DomainField,
57 typename Traits::RangeField,
58 Traits::dimDomainLocal,
59 1 // Polynomial order
60 > P1LocalBasis;
61 typedef ScalarLocalToGlobalBasisAdaptor<P1LocalBasis, Geometry> P1Basis;
62
63 static const P1LocalBasis& p1LocalBasis;
64 static const std::size_t dim = Traits::dimDomainLocal;
65
66 typedef EdgeS0_5Common<dim, typename Geometry::ctype> Base;
67 using Base::refelem;
68 using Base::s;
69
70 // global values of the Jacobians (gradients) of the p1 basis
71 std::vector<typename P1Basis::Traits::Jacobian> p1j;
72 // edge sizes and orientations
73 std::vector<typename Traits::DomainField> edgel;
74
75 public:
76 //! Construct an EdgeS0_5Basis
77 /**
78 * \param geo Geometry of the element to construct a local basis
79 * for.
80 * \param vertexOrder Vertex ordering information. Only the vertex order
81 * on the dim=1 sub-entities (edges) is required.
82 */
83 template<typename VertexOrder>
84 EdgeS0_5Basis(const Geometry& geo, const VertexOrder& vertexOrder) :
85 p1j(s, typename P1Basis::Traits::Jacobian(0)), edgel(s)
86 {
87 // use some arbitrary position to evaluate jacobians, they are constant
88 static const typename Traits::DomainLocal xl(0);
89
90 // precompute Jacobian (gradients) of the p1 element
91 P1Basis(p1LocalBasis, geo).evaluateJacobian(xl, p1j);
92
93 // calculate edge sizes and orientations
94 for(std::size_t i = 0; i < s; ++i) {
95 edgel[i] = (geo.corner(refelem.subEntity(i,dim-1,0,dim))-
96 geo.corner(refelem.subEntity(i,dim-1,1,dim))
97 ).two_norm();
98 const typename VertexOrder::iterator& edgeVertexOrder =
99 vertexOrder.begin(dim-1, i);
100 if(edgeVertexOrder[0] > edgeVertexOrder[1])
101 edgel[i] *= -1;
102 }
103 }
104
105 //! number of shape functions
106 std::size_t size () const { return s; }
107
108 //! Evaluate all shape functions
109 void evaluateFunction(const typename Traits::DomainLocal& xl,
110 std::vector<typename Traits::Range>& out) const
111 {
112 out.assign(s, typename Traits::Range(0));
113
114 // compute p1 values -- use the local basis directly for that, local and
115 // global values are identical for scalars
116 std::vector<typename P1LocalBasis::Traits::RangeType> p1v;
117 p1LocalBasis.evaluateFunction(xl, p1v);
118
119 for(std::size_t i = 0; i < s; i++) {
120 const std::size_t i0 = refelem.subEntity(i,dim-1,0,dim);
121 const std::size_t i1 = refelem.subEntity(i,dim-1,1,dim);
122 out[i].axpy( p1v[i0], p1j[i1][0]);
123 out[i].axpy(-p1v[i1], p1j[i0][0]);
124 out[i] *= edgel[i];
125 }
126 }
127
128 //! Evaluate all Jacobians
129 void evaluateJacobian(const typename Traits::DomainLocal&,
130 std::vector<typename Traits::Jacobian>& out) const
131 {
132 out.resize(s);
133
134 for(std::size_t i = 0; i < s; i++) {
135 const std::size_t i0 = refelem.subEntity(i,dim-1,0,dim);
136 const std::size_t i1 = refelem.subEntity(i,dim-1,1,dim);
137 for(std::size_t j = 0; j < dim; j++)
138 for(std::size_t k = 0; k < dim; k++)
139 out[i][j][k] = edgel[i] *
140 (p1j[i0][0][k]*p1j[i1][0][j]-p1j[i1][0][k]*p1j[i0][0][j]);
141 }
142 }
143
144 //! \brief Evaluate partial derivatives of all shape functions
145 void partial (const std::array<unsigned int, dim>& order,
146 const typename Traits::DomainLocal& in, // position
147 std::vector<typename Traits::Range>& out) const // return value
148 {
149 auto totalOrder = std::accumulate(order.begin(), order.end(), 0);
150 if (totalOrder == 0) {
151 evaluateFunction(in, out);
152 } else if (totalOrder==1) {
153 auto const k = std::distance(order.begin(), std::find(order.begin(), order.end(), 1));
154 out.resize(size());
155
156 for (std::size_t i = 0; i < s; i++)
157 {
158 const std::size_t i0 = refelem.subEntity(i,dim-1,0,dim);
159 const std::size_t i1 = refelem.subEntity(i,dim-1,1,dim);
160 for(std::size_t j = 0; j < dim; j++)
161 out[i][j] = edgel[i] *
162 (p1j[i0][0][k]*p1j[i1][0][j] - p1j[i1][0][k]*p1j[i0][0][j]);
163 }
164 } else {
165 DUNE_THROW(NotImplemented, "Desired derivative order is not implemented");
166 }
167 }
168
169 //! Polynomial order of the shape functions
170 std::size_t order () const { return 1; }
171 };
172
173 template<class Geometry, class RF>
174 const typename EdgeS0_5Basis<Geometry, RF>::P1LocalBasis&
175 EdgeS0_5Basis<Geometry, RF>::p1LocalBasis = P1LocalBasis();
176
177 } // namespace Dune
178
179 #endif // DUNE_LOCALFUNCTIONS_WHITNEY_EDGES0_5_BASIS_HH