Loading...
Searching...
No Matches
composedgridfunction.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
#ifndef DUNE_FUNCTIONS_GRIDFUNCTIONS_COMPOSEDGRIDFUNCTION_HH
#define DUNE_FUNCTIONS_GRIDFUNCTIONS_COMPOSEDGRIDFUNCTION_HH
#include <type_traits>
#include <tuple>
#include <dune/common/referencehelper.hh>
#include <dune/common/typeutilities.hh>
#include <dune/functions/common/defaultderivativetraits.hh>
#include <dune/functions/common/differentiablefunction.hh>
#include <dune/functions/gridfunctions/gridfunction.hh>
namespace Dune {
namespace Functions {
template<class OF, class... IF>
class ComposedGridFunction
{
using InnerFunctions = std::tuple<IF...>;
using InnerLocalFunctions = std::tuple<decltype(localFunction(resolveRef(std::declval<const IF&>())))...>;
template<std::size_t i>
using InnerFunction = std::decay_t<ResolveRef_t<std::tuple_element_t<i, InnerFunctions>>>;
using OuterFunction = OF;
public:
using EntitySet = typename InnerFunction<0>::EntitySet;
using Element = typename EntitySet::Element;
using Domain = typename EntitySet::GlobalCoordinate;
using LocalDomain = typename EntitySet::LocalCoordinate;
using Range = decltype(std::declval<OF>()(localFunction(std::declval<IF>())(std::declval<LocalDomain>())...));
private:
using Traits = Imp::GridFunctionTraits<Range(Domain), EntitySet, DefaultDerivativeTraits, 16>;
class LocalFunction
{
public:
LocalFunction(const ComposedGridFunction& globalFunction) :
globalFunction_(globalFunction),
innerLocalFunctions_(globalFunction.innerLocalFunctions())
{}
void bind(const Element& element)
{
std::apply([&](auto&... innerFunction) {
(innerFunction.bind(element),...);
}, innerLocalFunctions_);
}
void unbind()
{
std::apply([&](auto&... innerFunction) {
(innerFunction.unbind(),...);
}, innerLocalFunctions_);
}
bool bound() const
{
return std::apply([](const auto&... innerFunction) {
return (innerFunction.bound() && ...);
}, innerLocalFunctions_);
}
Range operator()(const LocalDomain& x) const
{
return std::apply([&](const auto&... innerFunction) {
return globalFunction_.outerFunction_(innerFunction(x)...);
}, innerLocalFunctions_);
}
const Element& localContext() const
{
return std::get<0>(innerLocalFunctions_).localContext();
}
friend typename Traits::LocalFunctionTraits::DerivativeInterface derivative(const LocalFunction& t)
{
DUNE_THROW(NotImplemented,"not implemented");
}
private:
const ComposedGridFunction& globalFunction_;
InnerLocalFunctions innerLocalFunctions_;
};
public:
template<class OFT, class... IFT,
disableCopyMove<ComposedGridFunction, OFT> = 0,
std::enable_if_t<(sizeof...(IFT) > 0), int> = 0>
ComposedGridFunction(OFT&& outerFunction, IFT&&... innerFunctions) :
outerFunction_(std::forward<OFT>(outerFunction)),
innerFunctions_(std::forward<IFT>(innerFunctions)...)
{}
Range operator()(const Domain& x) const
{
return std::apply([&](const auto&... innerFunction) {
return outerFunction_(innerFunction(x)...);
}, innerFunctions_);
}
friend typename Traits::DerivativeInterface derivative(const ComposedGridFunction& t)
{
DUNE_THROW(NotImplemented,"not implemented");
}
friend LocalFunction localFunction(const ComposedGridFunction& cgf)
{
return LocalFunction(cgf);
}
const EntitySet& entitySet() const
{
return resolveRef(std::get<0>(innerFunctions_)).entitySet();
}
protected:
InnerLocalFunctions innerLocalFunctions() const
{
return std::apply([&](const auto&... innerFunction) {
return std::make_tuple(localFunction(resolveRef(innerFunction))...);
}, innerFunctions_);
}
OuterFunction outerFunction_;
InnerFunctions innerFunctions_;
};
// deduction guides
template<class OF, class... IF>
ComposedGridFunction(const OF&, const IF&...)
-> ComposedGridFunction<OF,IF...>;
template<class OF, class... IF>
auto makeComposedGridFunction(OF&& outerFunction, IF&&... innerFunction)
{
using ComposedGridFunctionType = ComposedGridFunction<std::decay_t<OF>, std::decay_t<IF>...>;
return ComposedGridFunctionType(std::forward<OF>(outerFunction), std::forward<IF>(innerFunction)...);
}
}} // namespace Dune::Functions
#endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_COMPOSEDGRIDFUNCTION_HH
gridfunction.hh
differentiablefunction.hh
defaultderivativetraits.hh
Dune::Functions::ComposedGridFunction::makeComposedGridFunction
auto makeComposedGridFunction(OF &&outerFunction, IF &&... innerFunction)
Create a ComposedGridFunction that composes grid-functions with another function.
Definition composedgridfunction.hh:258
Dune
Definition monomialset.hh:19
Dune::Functions
Definition monomialset.hh:19
Dune::Functions::ComposedGridFunction
ComposedGridFunction(const OF &, const IF &...) -> ComposedGridFunction< OF, IF... >
Dune::Functions::DefaultDerivativeTraits
Default implementation for derivative traits.
Definition defaultderivativetraits.hh:41
Dune::Functions::ComposedGridFunction
Composition of grid functions with another function.
Definition composedgridfunction.hh:46
Dune::Functions::ComposedGridFunction::outerFunction_
OuterFunction outerFunction_
Definition composedgridfunction.hh:225
Dune::Functions::ComposedGridFunction::innerFunctions_
InnerFunctions innerFunctions_
Definition composedgridfunction.hh:226
Dune::Functions::ComposedGridFunction::ComposedGridFunction
ComposedGridFunction(OFT &&outerFunction, IFT &&... innerFunctions)
Create ComposedGridFunction.
Definition composedgridfunction.hh:171
Dune::Functions::ComposedGridFunction::entitySet
const EntitySet & entitySet() const
Return the EntitySet associated to this composed grid-function.
Definition composedgridfunction.hh:211
Dune::Functions::ComposedGridFunction::EntitySet
typename InnerFunction< 0 >::EntitySet EntitySet
Definition composedgridfunction.hh:57
Dune::Functions::ComposedGridFunction::LocalDomain
typename EntitySet::LocalCoordinate LocalDomain
Definition composedgridfunction.hh:61
Dune::Functions::ComposedGridFunction::Element
typename EntitySet::Element Element
Definition composedgridfunction.hh:58
Dune::Functions::ComposedGridFunction::innerLocalFunctions
InnerLocalFunctions innerLocalFunctions() const
Definition composedgridfunction.hh:218
Dune::Functions::ComposedGridFunction::localFunction
friend LocalFunction localFunction(const ComposedGridFunction &cgf)
Create a local-function of this composed grid-function.
Definition composedgridfunction.hh:196
Dune::Functions::ComposedGridFunction::Domain
typename EntitySet::GlobalCoordinate Domain
Definition composedgridfunction.hh:60
Dune::Functions::ComposedGridFunction::operator()
Range operator()(const Domain &x) const
Evaluation of the composed grid function in coordinates x.
Definition composedgridfunction.hh:177
Dune::Functions::ComposedGridFunction::derivative
friend Traits::DerivativeInterface derivative(const ComposedGridFunction &t)
Not implemented.
Definition composedgridfunction.hh:185
Dune::Functions::ComposedGridFunction::Range
decltype(std::declval< OF >()(localFunction(std::declval< IF >())(std::declval< LocalDomain >())...)) Range
Definition composedgridfunction.hh:63