| 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_ENRICHED_SIMPLEXP1BUBBLE_LOCALBASIS_HH |
| 6 |
#define DUNE_LOCALFUNCTIONS_ENRICHED_SIMPLEXP1BUBBLE_LOCALBASIS_HH |
| 7 |
|
| 8 |
#include <numeric> |
| 9 |
#include <stdexcept> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include <dune/common/fvector.hh> |
| 13 |
#include <dune/common/fmatrix.hh> |
| 14 |
#include <dune/common/math.hh> |
| 15 |
|
| 16 |
#include <dune/localfunctions/common/localbasis.hh> |
| 17 |
|
| 18 |
namespace Dune |
| 19 |
{ |
| 20 |
/** |
| 21 |
* \ingroup LocalFunctionsImpl |
| 22 |
* \brief P1 basis in dim-d enriched by an (order dim+1) element bubble function. |
| 23 |
* |
| 24 |
* The shape functions are associated to the vertices and to the barycenter. The |
| 25 |
* bubble function is the product of the linear vertex basis functions scaled by |
| 26 |
* the factor (dim+1)^(dim+1). |
| 27 |
* |
| 28 |
* \tparam D Type to represent the field in the domain. |
| 29 |
* \tparam R Type to represent the field in the range. |
| 30 |
* |
| 31 |
* \nosubgrouping |
| 32 |
**/ |
| 33 |
template<class D, class R, int dim> |
| 34 |
class SimplexP1BubbleLocalBasis |
| 35 |
{ |
| 36 |
template<class> friend class SimplexP1BubbleLocalInterpolation; |
| 37 |
|
| 38 |
//! Type of the local coordinates in the domain |
| 39 |
using DomainType = FieldVector<D,dim>; |
| 40 |
|
| 41 |
//! Range type of the basis functions |
| 42 |
using RangeType = FieldVector<R,1>; |
| 43 |
|
| 44 |
//! Type of the jacobians of the basis functions |
| 45 |
using JacobianType = FieldMatrix<R,1,dim>; |
| 46 |
|
| 47 |
static constexpr int dimension = dim; |
| 48 |
static constexpr int numVertices = dim+1; |
| 49 |
|
| 50 |
public: |
| 51 |
//! Type traits for function signature |
| 52 |
using Traits = LocalBasisTraits<D,dim,DomainType,R,1,RangeType,JacobianType>; |
| 53 |
|
| 54 |
//! Returns number of shape functions |
| 55 |
static constexpr std::size_t size () noexcept |
| 56 |
{ |
| 57 |
return numVertices+1; |
| 58 |
} |
| 59 |
|
| 60 |
//! Evaluate all shape functions |
| 61 |
static constexpr void evaluateFunction (const DomainType& in, |
| 62 |
std::vector<RangeType>& out) |
| 63 |
{ |
| 64 |
out.resize(size()); |
| 65 |
out[0] = 1; |
| 66 |
out.back() = power(dim+1, dim+1); // normalization of the bubble function |
| 67 |
for (int i = 0; i < dim; ++i) { |
| 68 |
out[0] -= in[i]; |
| 69 |
out[i+1] = in[i]; |
| 70 |
out.back() *= in[i]; |
| 71 |
} |
| 72 |
out.back() *= out[0]; |
| 73 |
} |
| 74 |
|
| 75 |
//! Evaluate Jacobian of all shape functions |
| 76 |
static constexpr void evaluateJacobian (const DomainType& in, |
| 77 |
std::vector<JacobianType>& out) |
| 78 |
{ |
| 79 |
out.resize(size()); |
| 80 |
RangeType tmp = 1; |
| 81 |
for (int i = 0; i < dim; ++i) { |
| 82 |
out[0][0][i] = -1; |
| 83 |
for (int j = 0; j < dim; ++j) |
| 84 |
out[j+1][0][i] = (i == j); |
| 85 |
tmp -= in[i]; |
| 86 |
} |
| 87 |
|
| 88 |
for (int i = 0; i < dim; ++i) { |
| 89 |
out.back()[0][i] = power(dim+1, dim+1) * (tmp - in[i]); |
| 90 |
for (int j = i+1; j < dim+i; ++j) |
| 91 |
out.back()[0][i] *= in[j % dim]; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
//! Evaluate partial derivatives of all shape functions |
| 96 |
static constexpr void partial (const std::array<unsigned int, dim>& order, |
| 97 |
const DomainType& in, |
| 98 |
std::vector<RangeType>& out) |
| 99 |
{ |
| 100 |
unsigned int totalOrder = 0; |
| 101 |
for (int i = 0; i < dim; ++i) |
| 102 |
totalOrder += order[i]; |
| 103 |
|
| 104 |
switch (totalOrder) { |
| 105 |
case 0: |
| 106 |
evaluateFunction(in,out); |
| 107 |
break; |
| 108 |
case 1: { |
| 109 |
out.resize(size()); |
| 110 |
int d = 0; // the direction of differentiation |
| 111 |
for (int i = 0; i < dim; ++i) |
| 112 |
d += i * order[i]; |
| 113 |
|
| 114 |
out[0] = -1; |
| 115 |
RangeType tmp = 1; |
| 116 |
for (int j = 0; j < dim; ++j) { |
| 117 |
out[j+1] = (d == j); |
| 118 |
tmp -= in[j]; |
| 119 |
} |
| 120 |
out.back() = power(dim+1, dim+1) * (tmp - in[d]); |
| 121 |
for (int j = d+1; j < dim+d; ++j) |
| 122 |
out.back() *= in[j % dim]; |
| 123 |
} break; |
| 124 |
default: |
| 125 |
throw std::runtime_error("Desired derivative order is not implemented"); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
//! Returns maximal polynomial order of the basis functions |
| 130 |
static constexpr unsigned int order () noexcept |
| 131 |
{ |
| 132 |
return dim+1; |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
} // end namespace Dune |
| 137 |
|
| 138 |
#endif // DUNE_LOCALFUNCTIONS_ENRICHED_SIMPLEXP1BUBBLE_LOCALBASIS_HH |