1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_PYTHON_FUNCTION_SIMPLEGRIDFUNCTION_HH
#define DUNE_PYTHON_FUNCTION_SIMPLEGRIDFUNCTION_HH
#include <cassert>
#include <type_traits>
#include <utility>
#include <dune/common/typeutilities.hh>
#include <dune/python/pybind11/pybind11.h>
namespace Dune
{
namespace Python
{
// SimpleLocalFunction
// -------------------
template< class GridView, class LocalEvaluator >
class SimpleLocalFunction
{
typedef SimpleLocalFunction< GridView, LocalEvaluator > This;
public:
typedef typename GridView::template Codim< 0 >::Entity Element;
typedef typename Element::Geometry::LocalCoordinate LocalCoordinate;
typedef std::decay_t< std::invoke_result_t< LocalEvaluator, Element, LocalCoordinate > > Value;
explicit SimpleLocalFunction ( LocalEvaluator localEvaluator ) : localEvaluator_( std::move( localEvaluator ) ) {}
~SimpleLocalFunction() { unbind(); }
template <class GF>
explicit SimpleLocalFunction ( const GF &gf ) : SimpleLocalFunction(gf.localEvaluator()) {}
void bind ( const Element &element ) { element_ = element; }
void unbind () { element_.reset(); }
template< class X >
auto operator() ( const X &x ) const
-> decltype( std::declval< const LocalEvaluator & >()( std::declval< const Element & >(), x ) )
{
return localEvaluator_( element(), x );
}
const Element &element () const { assert( element_ ); return *element_; }
private:
std::optional<Element> element_;
LocalEvaluator localEvaluator_;
};
// SimpleGridFunction
// ------------------
template< class GV, class LocalEval >
class SimpleGridFunction
{
typedef SimpleGridFunction< GV, LocalEval > This;
public:
typedef LocalEval LocalEvaluator;
static const unsigned int dimRange = LocalEvaluator::dimRange;
typedef GV GridView;
typedef SimpleLocalFunction< GridView, LocalEvaluator > LocalFunction;
typedef typename LocalFunction::Element Element;
typedef typename LocalFunction::Value Value;
SimpleGridFunction ( const GridView &gridView, LocalEvaluator localEvaluator )
: gridView_( gridView ), localEvaluator_( std::move( localEvaluator ) )
{}
const GridView &gridView () const { return gridView_; }
const LocalEvaluator &localEvaluator () const { return localEvaluator_; }
friend LocalFunction localFunction ( const This &gf ) { return LocalFunction( gf.localEvaluator_ ); }
protected:
const GridView &gridView_;
LocalEvaluator localEvaluator_;
};
// LocalEvaluatorAdapter
// ---------------------
template< class Element, class Evaluator >
struct LocalEvaluatorAdapter
{
static const unsigned int dimRange = Evaluator::dimRange;
typedef typename Element::Geometry::GlobalCoordinate GlobalCoordinate;
typedef typename Element::Geometry::LocalCoordinate LocalCoordinate;
typedef decltype( std::declval< Evaluator >()( std::declval< GlobalCoordinate >() ) ) Value;
LocalEvaluatorAdapter ( Evaluator evaluator ) : evaluator_( std::move( evaluator ) ) {}
Value operator () ( const GlobalCoordinate &x ) const { return evaluator_( x ); }
Value operator () ( const Element &element, const LocalCoordinate &x ) const { return evaluator_( element.geometry().global( x ) ); }
private:
Evaluator evaluator_;
};
// SimpleGlobalGridFunction
// ------------------------
template< class GV, class Evaluator >
class SimpleGlobalGridFunction
: public SimpleGridFunction< GV, LocalEvaluatorAdapter< typename GV::template Codim< 0 >::Entity, Evaluator > >
{
typedef SimpleGlobalGridFunction< GV, Evaluator > This;
typedef SimpleGridFunction< GV, LocalEvaluatorAdapter< typename GV::template Codim< 0 >::Entity, Evaluator > > Base;
public:
typedef typename Base::GridView GridView;
typedef typename Base::Value Value;
typedef typename GridView::template Codim< 0 >::Geometry::GlobalCoordinate GlobalCoordinate;
SimpleGlobalGridFunction ( const GridView &gridView, Evaluator evaluator )
: Base( gridView, std::move( evaluator ) )
{}
Value operator() ( const GlobalCoordinate &x ) const { return localEvaluator_( x ); }
protected:
using Base::localEvaluator_;
};
// simpleGridFunction
// ------------------
template< class GV, class LE,
class = std::invoke_result_t< std::decay_t< LE >, typename GV::template Codim< 0 >::Entity, typename GV::template Codim< 0 >::Geometry::LocalCoordinate > >
inline static auto simpleGridFunction ( const GV &gridView, LE &&localEvaluator, PriorityTag< 1 > )
-> SimpleGridFunction< GV, std::decay_t< LE > >
{
return SimpleGridFunction< GV, std::decay_t< LE > >( gridView, std::forward< LE >( localEvaluator ) );
}
template< class GV, class E,
class = std::invoke_result_t< std::decay_t< E >, typename GV::template Codim< 0 >::Geometry::GlobalCoordinate > >
inline static auto simpleGridFunction ( const GV &gridView, E &&evaluator, PriorityTag< 0 > )
-> SimpleGlobalGridFunction< GV, std::decay_t< E > >
{
return SimpleGlobalGridFunction< GV, std::decay_t< E > >( gridView, std::forward< E >( evaluator ) );
}
template< class GridView, class Evaluator >
inline static auto simpleGridFunction ( const GridView &gridView, Evaluator &&evaluator )
-> decltype( simpleGridFunction( gridView, std::forward< Evaluator >( evaluator ), PriorityTag< 42 >() ) )
{
return simpleGridFunction( gridView, std::forward< Evaluator >( evaluator ), PriorityTag< 42 >() );
}
} // namespace Python
} // namespace Dune
#endif // #ifndef DUNE_PYTHON_FUNCTION_SIMPLEGRIDFUNCTION_HH
|