| 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_INTERPOLATION_HH |
| 7 |
#define DUNE_LOCALFUNCTIONS_META_POWER_INTERPOLATION_HH |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <cassert> |
| 11 |
#include <cstddef> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
namespace Dune { |
| 15 |
|
| 16 |
//! \brief Meta-interpolation turning a scalar interpolation into |
| 17 |
//! vector-valued interpolation |
| 18 |
/** |
| 19 |
* \tparam Backend Type of the scalar interpolation. |
| 20 |
* \tparam BasisTraits Traits type of the corresponding PowerBasis. |
| 21 |
* |
| 22 |
* \ingroup LocalFunctionsImpl |
| 23 |
* \nosubgrouping |
| 24 |
*/ |
| 25 |
template<class Backend, class BasisTraits> |
| 26 |
class PowerInterpolation { |
| 27 |
static_assert(Backend::Traits::dimRange == 1, |
| 28 |
"PowerInterpolation works only with scalar backends"); |
| 29 |
|
| 30 |
const Backend *backend; |
| 31 |
|
| 32 |
public: |
| 33 |
//! Export basis traits |
| 34 |
typedef BasisTraits Traits; |
| 35 |
|
| 36 |
//! Construct a PowerInterpolation |
| 37 |
/** |
| 38 |
* \param backend_ Backend interpolation object to construct this object |
| 39 |
* from. This object holds a reference to the backend |
| 40 |
* object. This reference is also copied when this object |
| 41 |
* is copied. |
| 42 |
*/ |
| 43 |
PowerInterpolation(const Backend &backend_) : backend(&backend_) { } |
| 44 |
|
| 45 |
private: |
| 46 |
template<class F> |
| 47 |
class ComponentEvaluator |
| 48 |
{ |
| 49 |
const F &f; |
| 50 |
std::size_t comp; |
| 51 |
|
| 52 |
public: |
| 53 |
ComponentEvaluator(const F &f_, std::size_t comp_) : |
| 54 |
f(f_), comp(comp_) |
| 55 |
{ } |
| 56 |
|
| 57 |
typename Backend::Traits::Range operator()(const typename Backend::Traits::DomainLocal &x) const |
| 58 |
{ |
| 59 |
typename Traits::Range fy = f(x); |
| 60 |
typename Backend::Traits::Range y; |
| 61 |
y[0] = fy[comp]; |
| 62 |
return y; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
public: |
| 67 |
//! Determine coefficients interpolating a given function |
| 68 |
/** |
| 69 |
* \param f An object supporting the expression \c y = f(x), |
| 70 |
* where \c x is of type \c Traits::DomainLocal and \c y of the |
| 71 |
* type \c Traits::Range. When \c y = f(x) is |
| 72 |
* evaluated, \c x will be a local coordinate, and the |
| 73 |
* expression should set \c y to the function value at that |
| 74 |
* position. The initial value of \c y should not be used. |
| 75 |
* \param out Vector where to store the interpolated coefficients. |
| 76 |
*/ |
| 77 |
template<typename F, typename C> |
| 78 |
void interpolate(const F& f, std::vector<C>& out) const { |
| 79 |
out.clear(); |
| 80 |
std::vector<C> cout; |
| 81 |
for(std::size_t d = 0; d < Traits::dimRange; ++d) { |
| 82 |
// When dropping support for `evaluate()` we can simply use a lambda |
| 83 |
// instead of ComponentEvaluator. But changing this now would break |
| 84 |
// PowerInterpolation for FE-implementation outside of dune-localfunctions |
| 85 |
// which may not have been adjusted so far. |
| 86 |
backend->interpolate(ComponentEvaluator<std::decay_t<decltype(f)>>(f, d), cout); |
| 87 |
if(d == 0) |
| 88 |
out.resize(cout.size()*Traits::dimRange); |
| 89 |
// make sure the size of cout does not change surprisingly |
| 90 |
assert(out.size() == cout.size()*Traits::dimRange); |
| 91 |
std::copy(cout.begin(), cout.end(), out.begin() + d*cout.size()); |
| 92 |
} |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
} // namespace Dune |
| 97 |
|
| 98 |
#endif // DUNE_LOCALFUNCTIONS_META_POWER_INTERPOLATION_HH |