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
|
// 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_PYTHON_FUNCTIONS_DISCRETEFUNCTION_HH
#define DUNE_PYTHON_FUNCTIONS_DISCRETEFUNCTION_HH
#include <dune/common/ftraits.hh>
#include <dune/typetree/treepath.hh>
#include <dune/functions/gridfunctions/discreteglobalbasisfunction.hh>
#include <dune/python/common/pythonvector.hh>
#include <dune/python/grid/function.hh>
#include <dune/python/functions/hierarchicvectorwrapper.hh>
#include <dune/python/pybind11/pybind11.h>
namespace Dune
{
namespace Python
{
// HierarchicPythonVector
// ----------------------
template< class K >
using HierarchicPythonVector = HierarchicVectorWrapper< PythonVector< K >, K >;
namespace detail
{
// registerDiscreteFunctionConstructor
// -----------------------------------
template< class Basis, class K, class Range, class... options >
inline static void registerDiscreteFunctionConstructor ( pybind11::class_< Dune::Functions::DiscreteGlobalBasisFunction< Basis, HierarchicPythonVector< K >, Dune::Functions::HierarchicNodeToRangeMap, Range >, options... > &cls, PriorityTag< 1 > )
{
using pybind11::operator""_a;
typedef HierarchicPythonVector< K > Vector;
typedef Dune::Functions::HierarchicNodeToRangeMap NodeToRangeMap;
typedef Dune::Functions::DiscreteGlobalBasisFunction< Basis, Vector, NodeToRangeMap, Range > DiscreteFunction;
cls.def( pybind11::init( [] ( const Basis &basis, pybind11::buffer dofVector ) {
auto nodeToRangeMapPtr =
std::make_shared<const NodeToRangeMap>();
std::shared_ptr<const Basis> basisPtr = Dune::wrap_or_move( basis );
auto vectorPtr = std::make_shared< const Vector >( dofVector );
return new DiscreteFunction( basisPtr,
vectorPtr,
nodeToRangeMapPtr );
} ), pybind11::keep_alive< 1, 2 >(), pybind11::keep_alive< 1, 3 >(), "basis"_a, "dofVector"_a );
}
template< class DiscreteFunction, class... options >
inline static void registerDiscreteFunctionConstructor ( pybind11::class_< DiscreteFunction, options... > &cls, PriorityTag< 0 > )
{}
} // namespace detail
// registerDiscreteFunction
// ------------------------
template< class Basis, class Vector, class NodeToRangeMap, class Range, class... options >
inline static void registerDiscreteFunction ( pybind11::module module, pybind11::class_< Dune::Functions::DiscreteGlobalBasisFunction< Basis, Vector, NodeToRangeMap, Range >, options... > &cls )
{
typedef Dune::Functions::DiscreteGlobalBasisFunction< Basis, Vector, NodeToRangeMap, Range > DiscreteFunction;
registerGridFunction( module, cls );
detail::registerDiscreteFunctionConstructor( cls, PriorityTag< 42 >() );
cls.def_property_readonly( "basis", [] ( const DiscreteFunction &self ) -> const Basis & { return self.basis(); }, pybind11::keep_alive< 0, 1 >() );
cls.def_property_readonly( "grid", [] ( const DiscreteFunction &self ) -> const typename Basis::GridView & { return self.basis().gridView(); }, pybind11::keep_alive< 0, 1 >() );
}
} // namespace Python
} // namespace Dune
#endif // #ifndef DUNE_PYTHON_FUNCTIONS_DISCRETEFUNCTION_HH
|