| 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_CUBEQ1BUBBLE_LOCALBASIS_HH |
| 6 |
#define DUNE_LOCALFUNCTIONS_ENRICHED_CUBEQ1BUBBLE_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 Q1 basis in dim-d enriched by an (order 2) 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 edge bubble basis functions scaled by |
| 26 |
* the factor 2^(2^dim). The edge bubble functions are the product of the |
| 27 |
* corresponding vertex basis functions. |
| 28 |
* |
| 29 |
* \tparam D Type to represent the field in the domain. |
| 30 |
* \tparam R Type to represent the field in the range. |
| 31 |
* |
| 32 |
* \nosubgrouping |
| 33 |
**/ |
| 34 |
template<class D, class R, int dim> |
| 35 |
class CubeQ1BubbleLocalBasis |
| 36 |
{ |
| 37 |
template<class> friend class CubeQ1BubbleLocalInterpolation; |
| 38 |
|
| 39 |
//! Type of the local coordinates in the domain |
| 40 |
using DomainType = FieldVector<D,dim>; |
| 41 |
|
| 42 |
//! Range type of the basis functions |
| 43 |
using RangeType = FieldVector<R,1>; |
| 44 |
|
| 45 |
//! Type of the jacobians of the basis functions |
| 46 |
using JacobianType = FieldMatrix<R,1,dim>; |
| 47 |
|
| 48 |
static constexpr int dimension = dim; |
| 49 |
static constexpr int numVertices = power(2, dim); |
| 50 |
|
| 51 |
// scaling factor of bubble basis function for normalization |
| 52 |
static constexpr int scaling = power(2, 2*dim); |
| 53 |
|
| 54 |
public: |
| 55 |
//! Type traits for function signature |
| 56 |
using Traits = LocalBasisTraits<D,dim,DomainType,R,1,RangeType,JacobianType>; |
| 57 |
|
| 58 |
//! Returns number of shape functions |
| 59 |
static constexpr std::size_t size () noexcept |
| 60 |
{ |
| 61 |
return numVertices+1; |
| 62 |
} |
| 63 |
|
| 64 |
/// Evaluate all shape functions |
| 65 |
static constexpr void evaluateFunction (const DomainType& x, std::vector<RangeType>& out) |
| 66 |
{ |
| 67 |
out.resize(size()); |
| 68 |
|
| 69 |
for (int i = 0; i < numVertices; ++i) { |
| 70 |
out[i] = 1; |
| 71 |
for (int j = 0; j < dimension; ++j) { |
| 72 |
// if j-th bit of i is set multiply with x[j], else with 1-x[j] |
| 73 |
out[i] *= (i & (1<<j)) ? x[j] : 1-x[j]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
out.back() = scaling; |
| 78 |
for (int j = 0; j < dimension; ++j) { |
| 79 |
out.back() *= (1-x[j]) * x[j]; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/// Evaluate Jacobian of all shape functions |
| 84 |
static constexpr void evaluateJacobian (const DomainType& x, std::vector<JacobianType>& out) |
| 85 |
{ |
| 86 |
out.resize(size()); |
| 87 |
|
| 88 |
// Loop over all linear shape functions |
| 89 |
for (int i = 0; i < numVertices; ++i) { |
| 90 |
// Loop over all coordinate directions |
| 91 |
for (int j = 0; j < dimension; ++j) { |
| 92 |
// Initialize: the overall expression as a product |
| 93 |
// if j-th bit of i is set to 1, else -11 |
| 94 |
out[i][0][j] = (i & (1<<j)) ? 1 : -1; |
| 95 |
|
| 96 |
for (int k = 0; k < dimension; ++k) { |
| 97 |
if (j != k) |
| 98 |
// if k-th bit of i is set multiply with x[k], else with 1-x[k] |
| 99 |
out[i][0][j] *= (i & (1<<k)) ? x[k] : 1-x[k]; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
for (int j = 0; j < dimension; ++j) { |
| 105 |
out.back()[0][j] = scaling; |
| 106 |
for (int k = 0; k < dimension; ++k) |
| 107 |
out.back()[0][j] *= (j == k) ? (1-2*x[k]) : (1-x[k])*x[k]; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
//! Evaluate partial derivatives of all shape functions |
| 112 |
static constexpr void partial (const std::array<unsigned int, dim>& order, |
| 113 |
const DomainType& x, |
| 114 |
std::vector<RangeType>& out) |
| 115 |
{ |
| 116 |
unsigned int totalOrder = 0; |
| 117 |
for (int i = 0; i < dimension; ++i) |
| 118 |
totalOrder += order[i]; |
| 119 |
|
| 120 |
switch (totalOrder) { |
| 121 |
case 0: |
| 122 |
evaluateFunction(x,out); |
| 123 |
break; |
| 124 |
case 1: { |
| 125 |
out.resize(size()); |
| 126 |
int d = 0; // the direction of differentiation |
| 127 |
for (int i = 0; i < dimension; ++i) |
| 128 |
d += i * order[i]; |
| 129 |
|
| 130 |
if (d >= dimension) |
| 131 |
throw std::invalid_argument("Direction of partial derivative not found!"); |
| 132 |
|
| 133 |
// Loop over all shape functions |
| 134 |
for (int i = 0; i < numVertices; ++i) { |
| 135 |
// Initialize: the overall expression is a product |
| 136 |
// if j-th bit of i is set to 1, otherwise to -1 |
| 137 |
out[i] = (i & (1<<d)) ? 1 : -1; |
| 138 |
|
| 139 |
for (int j = 0; j < dimension; ++j) { |
| 140 |
if (d != j) |
| 141 |
// if j-th bit of i is set multiply with in[j], else with 1-in[j] |
| 142 |
out[i] *= (i & (1<<j)) ? x[j] : 1-x[j]; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
out.back() = scaling; |
| 147 |
for (int k = 0; k < dimension; ++k) |
| 148 |
out.back() *= (d == k) ? (1-2*x[k]) : (1-x[k])*x[k]; |
| 149 |
|
| 150 |
} break; |
| 151 |
default: |
| 152 |
throw std::runtime_error("Desired derivative order is not implemented"); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
//! Returns maximal polynomial order of the basis functions |
| 157 |
static constexpr unsigned int order () noexcept |
| 158 |
{ |
| 159 |
return 2; |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
} // end namespace Dune |
| 164 |
|
| 165 |
#endif // DUNE_LOCALFUNCTIONS_ENRICHED_CUBEQ1BUBBLE_LOCALBASIS_HH |