| 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_HIERARCHICAL_SIMPLEX_P2_LOCALBASIS_HH |
| 6 |
#define DUNE_HIERARCHICAL_SIMPLEX_P2_LOCALBASIS_HH |
| 7 |
|
| 8 |
/** \file |
| 9 |
\brief Hierarchical p2 shape functions for the simplex |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <numeric> |
| 13 |
|
| 14 |
#include <dune/common/fvector.hh> |
| 15 |
#include <dune/common/fmatrix.hh> |
| 16 |
|
| 17 |
#include <dune/localfunctions/common/localbasis.hh> |
| 18 |
|
| 19 |
namespace Dune |
| 20 |
{ |
| 21 |
template<class D, class R, int dim> |
| 22 |
class HierarchicalSimplexP2LocalBasis |
| 23 |
{ |
| 24 |
public: |
| 25 |
HierarchicalSimplexP2LocalBasis() |
| 26 |
{ |
| 27 |
DUNE_THROW(Dune::NotImplemented,"HierarchicalSimplexP2LocalBasis not implemented for dim > 3."); |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
\brief Hierarchical P2 basis in 1d. |
| 33 |
|
| 34 |
The shape functions are associated to the following points: |
| 35 |
|
| 36 |
- \f$ f_0 \f$ ~ (0.0) // linear function |
| 37 |
- \f$ f_1 \f$ ~ (1.0) // linear function |
| 38 |
- \f$ f_2 \f$ ~ (0.5) // quadratic bubble |
| 39 |
|
| 40 |
\tparam D Type to represent the field in the domain. |
| 41 |
\tparam R Type to represent the field in the range. |
| 42 |
|
| 43 |
* \ingroup LocalFunctionsImpl |
| 44 |
\nosubgrouping |
| 45 |
*/ |
| 46 |
template<class D, class R> |
| 47 |
class HierarchicalSimplexP2LocalBasis<D,R,1> |
| 48 |
{ |
| 49 |
public: |
| 50 |
//! \brief export type traits for function signature |
| 51 |
typedef LocalBasisTraits<D,1,Dune::FieldVector<D,1>,R,1,Dune::FieldVector<R,1>, |
| 52 |
Dune::FieldMatrix<R,1,1> > Traits; |
| 53 |
|
| 54 |
//! \brief number of shape functions |
| 55 |
unsigned int size () const |
| 56 |
{ |
| 57 |
return 3; |
| 58 |
} |
| 59 |
|
| 60 |
//! \brief Evaluate all shape functions |
| 61 |
inline void evaluateFunction (const typename Traits::DomainType& in, |
| 62 |
std::vector<typename Traits::RangeType>& out) const |
| 63 |
{ |
| 64 |
out.resize(3); |
| 65 |
|
| 66 |
out[0] = 1-in[0]; |
| 67 |
out[1] = 1-4*(in[0]-0.5)*(in[0]-0.5); |
| 68 |
out[2] = in[0]; |
| 69 |
} |
| 70 |
|
| 71 |
//! \brief Evaluate Jacobian of all shape functions |
| 72 |
inline void |
| 73 |
evaluateJacobian (const typename Traits::DomainType& in, // position |
| 74 |
std::vector<typename Traits::JacobianType>& out) const // return value |
| 75 |
{ |
| 76 |
out.resize(3); |
| 77 |
|
| 78 |
out[0][0][0] = -1; |
| 79 |
out[1][0][0] = 4-8*in[0]; |
| 80 |
out[2][0][0] = 1; |
| 81 |
} |
| 82 |
|
| 83 |
//! \brief Evaluate partial derivatives of all shape functions |
| 84 |
void partial (const std::array<unsigned int, 1>& order, |
| 85 |
const typename Traits::DomainType& in, // position |
| 86 |
std::vector<typename Traits::RangeType>& out) const // return value |
| 87 |
{ |
| 88 |
auto totalOrder = order[0]; |
| 89 |
if (totalOrder == 0) { |
| 90 |
evaluateFunction(in, out); |
| 91 |
} else if (totalOrder == 1) { |
| 92 |
out.resize(size()); |
| 93 |
out[0] = -1; |
| 94 |
out[1] = 4-8*in[0]; |
| 95 |
out[2] = 1; |
| 96 |
} else if (totalOrder == 2) { |
| 97 |
out.resize(size()); |
| 98 |
out[0] = 0; |
| 99 |
out[1] = -8; |
| 100 |
out[2] = 0; |
| 101 |
} else { |
| 102 |
out.resize(size()); |
| 103 |
out[0] = out[1] = out[2] = 0; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** \brief Polynomial order of the shape functions (2, in this case) |
| 108 |
*/ |
| 109 |
unsigned int order () const |
| 110 |
{ |
| 111 |
return 2; |
| 112 |
} |
| 113 |
|
| 114 |
}; |
| 115 |
|
| 116 |
/** |
| 117 |
\brief Hierarchical P2 basis in 2d. |
| 118 |
|
| 119 |
The shape functions are associated to the following points: |
| 120 |
|
| 121 |
The functions are associated to points by: |
| 122 |
|
| 123 |
- \f$ f_0 \f$ ~ (0.0, 0.0) |
| 124 |
- \f$ f_1 \f$ ~ (0.5, 0.0) |
| 125 |
- \f$ f_2 \f$ ~ (1.0, 0.0) |
| 126 |
- \f$ f_3 \f$ ~ (0.0, 0.5) |
| 127 |
- \f$ f_4 \f$ ~ (0.5, 0.5) |
| 128 |
- \f$ f_5 \f$ ~ (0.0, 1.0) |
| 129 |
|
| 130 |
\tparam D Type to represent the field in the domain. |
| 131 |
\tparam R Type to represent the field in the range. |
| 132 |
|
| 133 |
* \ingroup LocalFunctionsImpl |
| 134 |
\nosubgrouping |
| 135 |
*/ |
| 136 |
template<class D, class R> |
| 137 |
class HierarchicalSimplexP2LocalBasis<D,R,2> |
| 138 |
{ |
| 139 |
public: |
| 140 |
//! \brief export type traits for function signature |
| 141 |
typedef LocalBasisTraits<D,2,Dune::FieldVector<D,2>,R,1,Dune::FieldVector<R,1>, |
| 142 |
Dune::FieldMatrix<R,1,2> > Traits; |
| 143 |
|
| 144 |
//! \brief number of shape functions |
| 145 |
unsigned int size () const |
| 146 |
{ |
| 147 |
return 6; |
| 148 |
} |
| 149 |
|
| 150 |
//! \brief Evaluate all shape functions |
| 151 |
inline void evaluateFunction (const typename Traits::DomainType& in, |
| 152 |
std::vector<typename Traits::RangeType>& out) const |
| 153 |
{ |
| 154 |
out.resize(6); |
| 155 |
|
| 156 |
out[0] = 1 - in[0] - in[1]; |
| 157 |
out[1] = 4*in[0]*(1-in[0]-in[1]); |
| 158 |
out[2] = in[0]; |
| 159 |
out[3] = 4*in[1]*(1-in[0]-in[1]); |
| 160 |
out[4] = 4*in[0]*in[1]; |
| 161 |
out[5] = in[1]; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
//! \brief Evaluate Jacobian of all shape functions |
| 166 |
inline void |
| 167 |
evaluateJacobian (const typename Traits::DomainType& in, // position |
| 168 |
std::vector<typename Traits::JacobianType>& out) const // return value |
| 169 |
{ |
| 170 |
out.resize(6); |
| 171 |
|
| 172 |
out[0][0][0] = -1; out[0][0][1] = -1; |
| 173 |
out[1][0][0] = 4-8*in[0]-4*in[1]; out[1][0][1] = -4*in[0]; |
| 174 |
out[2][0][0] = 1; out[2][0][1] = 0; |
| 175 |
out[3][0][0] = -4*in[1]; out[3][0][1] = 4-4*in[0]-8*in[1]; |
| 176 |
out[4][0][0] = 4*in[1]; out[4][0][1] = 4*in[0]; |
| 177 |
out[5][0][0] = 0; out[5][0][1] = 1; |
| 178 |
} |
| 179 |
|
| 180 |
//! \brief Evaluate partial derivatives of all shape functions |
| 181 |
void partial (const std::array<unsigned int, 2>& order, |
| 182 |
const typename Traits::DomainType& in, // position |
| 183 |
std::vector<typename Traits::RangeType>& out) const // return value |
| 184 |
{ |
| 185 |
auto totalOrder = std::accumulate(order.begin(), order.end(), 0); |
| 186 |
if (totalOrder == 0) { |
| 187 |
evaluateFunction(in, out); |
| 188 |
} else if (totalOrder == 1) { |
| 189 |
out.resize(size()); |
| 190 |
auto const direction = std::distance(order.begin(), std::find(order.begin(), order.end(), 1)); |
| 191 |
|
| 192 |
switch (direction) { |
| 193 |
case 0: |
| 194 |
out[0] = -1; |
| 195 |
out[1] = 4-8*in[0]-4*in[1]; |
| 196 |
out[2] = 1; |
| 197 |
out[3] = -4*in[1]; |
| 198 |
out[4] = 4*in[1]; |
| 199 |
out[5] = 0; |
| 200 |
break; |
| 201 |
case 1: |
| 202 |
out[0] = -1; |
| 203 |
out[1] = -4*in[0]; |
| 204 |
out[2] = 0; |
| 205 |
out[3] = 4-4*in[0]-8*in[1]; |
| 206 |
out[4] = 4*in[0]; |
| 207 |
out[5] = 1; |
| 208 |
break; |
| 209 |
default: |
| 210 |
DUNE_THROW(RangeError, "Component out of range."); |
| 211 |
} |
| 212 |
} else { |
| 213 |
DUNE_THROW(NotImplemented, "Desired derivative order is not implemented"); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** \brief Polynomial order of the shape functions (2 in this case) |
| 218 |
*/ |
| 219 |
unsigned int order () const |
| 220 |
{ |
| 221 |
return 2; |
| 222 |
} |
| 223 |
|
| 224 |
}; |
| 225 |
|
| 226 |
/** |
| 227 |
\brief Hierarchical P2 basis in 3d. |
| 228 |
|
| 229 |
The shape functions are associated to the following points: |
| 230 |
|
| 231 |
The functions are associated to points by: |
| 232 |
|
| 233 |
- \f$ f_0 \f$ ~ (0.0, 0.0, 0.0) |
| 234 |
- \f$ f_1 \f$ ~ (0.5, 0.0, 0.0) |
| 235 |
- \f$ f_2 \f$ ~ (1.0, 0.0, 0.0) |
| 236 |
- \f$ f_3 \f$ ~ (0.0, 0.5, 0.0) |
| 237 |
- \f$ f_4 \f$ ~ (0.5, 0.5, 0.0) |
| 238 |
- \f$ f_5 \f$ ~ (0.0, 1.0, 0.0) |
| 239 |
- \f$ f_6 \f$ ~ (0.0, 0.0, 0.5) |
| 240 |
- \f$ f_7 \f$ ~ (0.5, 0.0, 0.5) |
| 241 |
- \f$ f_8 \f$ ~ (0.0, 0.5, 0.5) |
| 242 |
- \f$ f_9 \f$ ~ (0.0, 0.0, 1.0) |
| 243 |
|
| 244 |
\tparam D Type to represent the field in the domain. |
| 245 |
\tparam R Type to represent the field in the range. |
| 246 |
|
| 247 |
* \ingroup LocalFunctionsImpl |
| 248 |
\nosubgrouping |
| 249 |
*/ |
| 250 |
template<class D, class R> |
| 251 |
class HierarchicalSimplexP2LocalBasis<D,R,3> |
| 252 |
{ |
| 253 |
public: |
| 254 |
//! \brief export type traits for function signature |
| 255 |
typedef LocalBasisTraits<D,3,Dune::FieldVector<D,3>,R,1,Dune::FieldVector<R,1>, |
| 256 |
Dune::FieldMatrix<R,1,3> > Traits; |
| 257 |
|
| 258 |
//! \brief number of shape functions |
| 259 |
unsigned int size () const |
| 260 |
{ |
| 261 |
return 10; |
| 262 |
} |
| 263 |
|
| 264 |
//! \brief Evaluate all shape functions |
| 265 |
void evaluateFunction (const typename Traits::DomainType& in, |
| 266 |
std::vector<typename Traits::RangeType>& out) const |
| 267 |
{ |
| 268 |
out.resize(10); |
| 269 |
|
| 270 |
out[0] = 1 - in[0] - in[1] - in[2]; |
| 271 |
out[1] = 4 * in[0] * (1 - in[0] - in[1] - in[2]); |
| 272 |
out[2] = in[0]; |
| 273 |
out[3] = 4 * in[1] * (1 - in[0] - in[1] - in[2]); |
| 274 |
out[4] = 4 * in[0] * in[1]; |
| 275 |
out[5] = in[1]; |
| 276 |
out[6] = 4 * in[2] * (1 - in[0] - in[1] - in[2]); |
| 277 |
out[7] = 4 * in[0] * in[2]; |
| 278 |
out[8] = 4 * in[1] * in[2]; |
| 279 |
out[9] = in[2]; |
| 280 |
} |
| 281 |
|
| 282 |
//! \brief Evaluate Jacobian of all shape functions |
| 283 |
void evaluateJacobian (const typename Traits::DomainType& in, // position |
| 284 |
std::vector<typename Traits::JacobianType>& out) const // return value |
| 285 |
{ |
| 286 |
out.resize(10); |
| 287 |
|
| 288 |
out[0][0][0] = -1; out[0][0][1] = -1; out[0][0][2] = -1; |
| 289 |
out[1][0][0] = 4-8*in[0]-4*in[1]-4*in[2]; out[1][0][1] = -4*in[0]; out[1][0][2] = -4*in[0]; |
| 290 |
out[2][0][0] = 1; out[2][0][1] = 0; out[2][0][2] = 0; |
| 291 |
out[3][0][0] = -4*in[1]; out[3][0][1] = 4-4*in[0]-8*in[1]-4*in[2]; out[3][0][2] = -4*in[1]; |
| 292 |
out[4][0][0] = 4*in[1]; out[4][0][1] = 4*in[0]; out[4][0][2] = 0; |
| 293 |
out[5][0][0] = 0; out[5][0][1] = 1; out[5][0][2] = 0; |
| 294 |
out[6][0][0] = -4*in[2]; out[6][0][1] = -4*in[2]; out[6][0][2] = 4-4*in[0]-4*in[1]-8*in[2]; |
| 295 |
out[7][0][0] = 4*in[2]; out[7][0][1] = 0; out[7][0][2] = 4*in[0]; |
| 296 |
out[8][0][0] = 0; out[8][0][1] = 4*in[2]; out[8][0][2] = 4*in[1]; |
| 297 |
out[9][0][0] = 0; out[9][0][1] = 0; out[9][0][2] = 1; |
| 298 |
} |
| 299 |
|
| 300 |
//! \brief Evaluate partial derivatives of all shape functions |
| 301 |
void partial (const std::array<unsigned int, 3>& order, |
| 302 |
const typename Traits::DomainType& in, // position |
| 303 |
std::vector<typename Traits::RangeType>& out) const // return value |
| 304 |
{ |
| 305 |
auto totalOrder = std::accumulate(order.begin(), order.end(), 0); |
| 306 |
if (totalOrder == 0) { |
| 307 |
evaluateFunction(in, out); |
| 308 |
} else if (totalOrder == 1) { |
| 309 |
out.resize(size()); |
| 310 |
auto const direction = std::distance(order.begin(), std::find(order.begin(), order.end(), 1)); |
| 311 |
|
| 312 |
switch (direction) { |
| 313 |
case 0: |
| 314 |
out[0] = -1; |
| 315 |
out[1] = 4-8*in[0]-4*in[1]-4*in[2]; |
| 316 |
out[2] = 1; |
| 317 |
out[3] = -4*in[1]; |
| 318 |
out[4] = 4*in[1]; |
| 319 |
out[5] = 0; |
| 320 |
out[6] = -4*in[2]; |
| 321 |
out[7] = 4*in[2]; |
| 322 |
out[8] = 0; |
| 323 |
out[9] = 0; |
| 324 |
break; |
| 325 |
case 1: |
| 326 |
out[0] = -1; |
| 327 |
out[1] = -4*in[0]; |
| 328 |
out[2] = 0; |
| 329 |
out[3] = 4-4*in[0]-8*in[1]-4*in[2]; |
| 330 |
out[4] = 4*in[0]; |
| 331 |
out[5] = 1; |
| 332 |
out[6] = -4*in[2]; |
| 333 |
out[7] = 0; |
| 334 |
out[8] = 4*in[2]; |
| 335 |
out[9] = 0; |
| 336 |
break; |
| 337 |
case 2: |
| 338 |
out[0] = -1; |
| 339 |
out[1] = -4*in[0]; |
| 340 |
out[2] = 0; |
| 341 |
out[3] = -4*in[1]; |
| 342 |
out[4] = 0; |
| 343 |
out[5] = 0; |
| 344 |
out[6] = 4-4*in[0]-4*in[1]-8*in[2]; |
| 345 |
out[7] = 4*in[0]; |
| 346 |
out[8] = 4*in[1]; |
| 347 |
out[9] = 1; |
| 348 |
break; |
| 349 |
default: |
| 350 |
DUNE_THROW(RangeError, "Component out of range."); |
| 351 |
} |
| 352 |
} else { |
| 353 |
DUNE_THROW(NotImplemented, "Desired derivative order is not implemented"); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/** \brief Polynomial order of the shape functions (2 in this case) |
| 358 |
*/ |
| 359 |
unsigned int order () const |
| 360 |
{ |
| 361 |
return 2; |
| 362 |
} |
| 363 |
|
| 364 |
}; |
| 365 |
} |
| 366 |
#endif |