dune-localfunctions 2.11
Loading...
Searching...
No Matches
python/localfunctions/localfiniteelement.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3#ifndef DUNE_PYTHON_LOCALFUNCTIONS_LOCALFINITEELEMENT_HH
4#define DUNE_PYTHON_LOCALFUNCTIONS_LOCALFINITEELEMENT_HH
5
6#include <dune/python/pybind11/pybind11.h>
7#include <dune/python/pybind11/gil_safe_call_once.h>
8
9#include <dune/common/visibility.hh>
12
13namespace Dune {
14namespace Python {
15
16namespace detail {
17
18template<typename LocalBasis>
19DUNE_EXPORT auto registerLocalBasis(pybind11::handle scope)
20{
21 // not a static pybind11::class_, see registerLocalKey
22 PYBIND11_CONSTINIT static pybind11::gil_safe_call_once_and_store<pybind11::class_<LocalBasis>> storage;
23 auto& cls = storage.call_once_and_store_result(
24 [scope]() { return pybind11::class_<LocalBasis>(scope, "LocalBasis"); }).get_stored();
25
26 cls.def("__len__", [](const LocalBasis& basis) { return basis.size(); });
27 cls.def_property_readonly("order", [](const LocalBasis& basis) { return basis.order(); });
28 cls.def("evaluateFunction",
29 [](const LocalBasis& basis, const typename LocalBasis::Traits::DomainType& in) {
30 std::vector<typename LocalBasis::Traits::RangeType> out;
31 basis.evaluateFunction(in, out);
32 return out;
33 });
34 cls.def("evaluateJacobian",
35 [](const LocalBasis& basis, const typename LocalBasis::Traits::DomainType& in) {
36 std::vector<typename LocalBasis::Traits::JacobianType> out;
37 basis.evaluateJacobian(in, out);
38 return out;
39 });
40 return cls;
41}
42
43DUNE_EXPORT auto registerLocalKey(pybind11::handle scope)
44{
45 // A static pybind11::class_ would be destroyed at exit, after the Python
46 // interpreter has been finalized, and drop its reference without the GIL;
47 // gil_safe_call_once_and_store never destroys the stored object.
48 PYBIND11_CONSTINIT static pybind11::gil_safe_call_once_and_store<pybind11::class_<LocalKey>> storage;
49 auto& cls = storage.call_once_and_store_result(
50 [scope]() { return pybind11::class_<LocalKey>(scope, "LocalKey"); }).get_stored();
51
52 cls.def_property_readonly("subEntity", &LocalKey::subEntity);
53 cls.def_property_readonly("codim", &LocalKey::codim);
54 cls.def_property("index",
55 [](const LocalKey& key) { return key.index(); },
56 [](LocalKey& key, unsigned int index) { key.index(index); });
57 cls.def("__lt__", &LocalKey::operator<);
58
59 return cls;
60}
61
62} /* namespace detail */
63
64template<typename LocalFiniteElement>
65DUNE_EXPORT auto registerLocalFiniteElement(pybind11::handle scope, const char* name = "LocalFiniteElement")
66{
67 // not a static pybind11::class_, see detail::registerLocalKey
68 PYBIND11_CONSTINIT static pybind11::gil_safe_call_once_and_store<pybind11::class_<LocalFiniteElement>> storage;
69 auto& cls = storage.call_once_and_store_result(
70 [scope, name]() { return pybind11::class_<LocalFiniteElement>(scope, name); }).get_stored();
71
72 detail::registerLocalBasis<typename LocalFiniteElement::Traits::LocalBasisType>(cls);
73
74 cls.def_property_readonly("localBasis", &LocalFiniteElement::localBasis, pybind11::return_value_policy::reference_internal);
75 // cls.def_property_readonly("localCoefficients", &LocalFiniteElement::localCoefficients);
76 // cls.def_property_readonly("localInterpolation", &LocalFiniteElement::localInterpolation);
77 cls.def("__len__", [](const LocalFiniteElement& lf) { return lf.size(); });
78 cls.def("size", [](const LocalFiniteElement& lf) { return lf.size(); });
79 cls.def_property_readonly("type", &LocalFiniteElement::type);
80
81 return cls;
82}
83
84
85} /* namespace Python */
86} /* namespace Dune */
87
88#endif
Definition bdfmcube.hh:18
Definition python/localfunctions/localfiniteelement.hh:14
DUNE_EXPORT auto registerLocalFiniteElement(pybind11::handle scope, const char *name="LocalFiniteElement")
Definition python/localfunctions/localfiniteelement.hh:65
Type erasure class storing a local finite element.
Definition localfunctions/common/localfiniteelement.hh:40
const Traits::LocalBasisType & localBasis() const
Access the LocalBasis of the stored local finite element.
Definition localfunctions/common/localfiniteelement.hh:156
unsigned int size() const
Get the number of basis functions of the stored local finite element.
Definition localfunctions/common/localfiniteelement.hh:188
const GeometryType & type() const
Get the GeometryType of the stored local finite element.
Definition localfunctions/common/localfiniteelement.hh:198
Describe position of one degree of freedom.
Definition localkey.hh:24
constexpr unsigned int index() const noexcept
Return offset within subentity.
Definition localkey.hh:70
constexpr unsigned int codim() const noexcept
Return codim of associated entity.
Definition localkey.hh:63
constexpr unsigned int subEntity() const noexcept
Return number of associated subentity.
Definition localkey.hh:56