#pragma once #include #include #include #include #include #include #include #include #include namespace AMDiS { #ifndef DOXYGEN template class ConstantLocalFunction; #endif /// \brief LocalFunction of a Gridfunction returning a constant value. template class ConstantLocalFunction { public: /// The LocalDomain this LocalFunction can be evaluated in using Domain = D; /// The range type of the LocalFunction using Range = R; /// This LocalFunction has its own \ref derivativeOf() function enum { hasDerivative = true }; private: using Geometry = typename LC::Geometry; public: /// \brief Constructor. Stores the constant value. ConstantLocalFunction(T const& value) : value_(value) {} void bind(LC const& /*element*/) { /* do nothing */ } void unbind() { /* do nothing */ } /// Return the constant `value_`. Range const& operator()(Domain const& /*local*/) const { return value_; } /// \brief Create a \ref ConstantLocalFunction representing the derivative /// of a constant function, that ist, the value 0. template auto makeDerivative(Type const& /*type*/) const { using RawSignature = typename Dune::Functions::SignatureTraits::RawSignature; using DerivativeRange = typename DerivativeTraits::Range; DerivativeRange diff(0); return ConstantLocalFunction{diff}; } /// Return the constant polynomial order 0. int order() const { return 0; } private: T value_; }; /// \brief Gridfunction returning a constant value. /** * \ingroup GridFunctions * * A stored constant is return in global and local evaluation of this functor. * Maybe used with arithmetic types and vectors/matrices of arithmetic types. * It is also allowed to pass a \ref std::reference_wrapper to allow to * modify the value after construction. **/ template class ConstantGridFunction { public: using EntitySet = Dune::Functions::GridViewEntitySet; using Domain = typename EntitySet::GlobalCoordinate; using Range = Underlying_t; enum { hasDerivative = false }; private: using Element = typename EntitySet::Element; using LocalDomain = typename EntitySet::LocalCoordinate; using LocalFunction = ConstantLocalFunction; public: /// Constructor. Stores the function `fct` and creates an `EntitySet`. ConstantGridFunction(T const& value, GridView const& gridView) : value_(value) , entitySet_(gridView) {} /// Return the constant `value_` Range const& operator()(Domain const& /*x*/) const { return value_; } EntitySet const& entitySet() const { return entitySet_; } /// \brief Create an \ref ConstantLocalFunction with the stores `value_`. LocalFunction makeLocalFunction() const { return {value_}; } private: T value_; EntitySet entitySet_; }; namespace Concepts { /** \addtogroup Concepts * @{ **/ namespace Definition { template struct ConstantToGridFunction : std::is_arithmetic {}; template struct ConstantToGridFunction> : ConstantToGridFunction {}; template struct ConstantToGridFunction> : ConstantToGridFunction {}; template struct ConstantToGridFunction> : ConstantToGridFunction {}; template struct ConstantToGridFunction> : ConstantToGridFunction {}; } // end namespace Definition /// \brief Concepts that is true for all ''simple'' types that can be /// converted automatically to a GridFunction, e.g. arithmetic types, /// FieldVectors, and `std::reference_wrapper`. template constexpr bool ConstantToGridFunction = Definition::ConstantToGridFunction::value; /** @} **/ } // end namespace Concepts template struct GridFunctionCreator>> { template static auto create(Value const& value, GridView const& gridView) { return ConstantGridFunction{value, gridView}; } }; } // end namespace AMDiS