version 0.4.1
fe.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10
11#include <dune/python/common/typeregistry.hh>
12#include <dune/python/functions/globalbasis.hh>
13#include <dune/python/pybind11/eigen.h>
14#include <dune/python/pybind11/functional.h>
15#include <dune/python/pybind11/pybind11.h>
16#include <dune/python/pybind11/stl.h>
17
19
20namespace Ikarus::Python {
21
49template <class FE, class... options>
50void registerCalculateAt(pybind11::handle scope, pybind11::class_<FE, options...> cls, auto restultTypesTuple) {
51 using Traits = typename FE::Traits;
53 cls.def(
54 "calculateAt",
55 [&](FE& self, const FERequirements& req, const Dune::FieldVector<double, Traits::mydim>& local,
56 std::string resType) {
57 Eigen::VectorXd result;
58 bool success = false;
59 Dune::Hybrid::forEach(restultTypesTuple, [&]<typename RT>(RT i) {
60 if (resType == toString(i)) {
61 success = true;
62 result = self.template calculateAt<RT::template Rebind>(req, local).asVec();
63 }
64 });
65 if (success)
66 return result;
67 DUNE_THROW(Dune::NotImplemented, "Element " + Dune::className<FE>() + " doesn't support ResultType " + resType);
68 },
69 pybind11::arg("feRequirements"), pybind11::arg("local"), pybind11::arg("resultType"));
70}
71
85template <class FE, class... options>
86void registerFE(pybind11::handle scope, pybind11::class_<FE, options...> cls) {
87 using BH = typename FE::BasisHandler;
88 using GridElement = typename FE::GridElement;
90 using FlatBasis = typename FE::Traits::FlatBasis;
91
92 int index = 0;
93 cls.def(pybind11::init([](const BH& basisHandler, typename FE::PreTuple argsTuple) {
94 auto unpackTuple = [&]<typename... Arg>(Arg&&... args) {
95 return new FE(basisHandler, std::forward<Arg>(args)...);
96 };
97 return std::apply(unpackTuple, argsTuple);
98 }),
99 pybind11::keep_alive<1, 2>());
100
101 cls.def("bind", [](FE& self, const GridElement& e) { self.bind(e); });
102 cls.def("calculateScalar", [](FE& self, const FERequirements& req) { return calculateScalar(self, req); });
103 cls.def("calculateVector", [](FE& self, const FERequirements& req, Eigen::Ref<Eigen::VectorXd> vec) {
104 calculateVector(self, req, vec);
105 });
106 cls.def(
107 "calculateMatrix",
108 [](FE& self, const FERequirements& req, Eigen::Ref<Eigen::MatrixXd> mat) { calculateMatrix(self, req, mat); },
109 pybind11::arg("FERequirements"), pybind11::arg("elementMatrix").noconvert());
110
111 pybind11::module scopedf = pybind11::module::import("dune.functions");
112
113 typedef Dune::Python::LocalViewWrapper<FlatBasis> LocalViewWrapper;
114 auto includes = Dune::Python::IncludeFiles{"dune/python/functions/globalbasis.hh"};
115 auto lv = Dune::Python::insertClass<LocalViewWrapper>(
116 scopedf, "LocalViewWrapper",
117 Dune::Python::GenerateTypeName("Dune::Python::LocalViewWrapperWrapper", Dune::MetaType<FlatBasis>()),
118 includes)
119 .first;
120 lv.def("bind", &LocalViewWrapper::bind);
121 lv.def("unbind", &LocalViewWrapper::unbind);
122 lv.def("index", [](const LocalViewWrapper& localView, int index) { return localView.index(index); });
123 lv.def("__len__", [](LocalViewWrapper& self) -> int { return self.size(); });
124
125 Dune::Python::Functions::registerTree<typename LocalViewWrapper::Tree>(lv);
126 lv.def("tree", [](const LocalViewWrapper& view) { return view.tree(); });
127
128 cls.def(
129 "localView",
130 [](FE& self) -> LocalViewWrapper {
131 auto lvWrapped = LocalViewWrapper(self.localView().globalBasis());
132 // this can be simplified when https://gitlab.dune-project.org/staging/dune-functions/-/merge_requests/418
133 // becomes available
134 // pybind11::object obj = pybind11::cast(self.localView().element());
135 lvWrapped.base().bind(self.localView().element());
136 return lvWrapped;
137 },
138 pybind11::keep_alive<0, 1>());
139
140 registerCalculateAt(scope, cls, typename FE::SupportedResultTypes());
141}
142
143} // namespace Ikarus::Python
Contains the definition of the FEFactory class.
void registerFE(pybind11::handle scope, pybind11::class_< FE, options... > cls)
Register Python bindings for the FE class.
Definition: fe.hh:86
constexpr std::string toString(ScalarAffordances _e)
Definition: ferequirements.hh:37
void init(int argc, char **argv, bool enableFileLogger=true)
Initializes the Ikarus framework.
Definition: init.hh:82
Definition: flatassembler.hh:20
void registerCalculateAt(pybind11::handle scope, pybind11::class_< FE, options... > cls, auto restultTypesTuple)
Registers the calculateAt method for a finite element class in Python.
Definition: fe.hh:50
FE class is a base class for all finite elements.
Definition: febase.hh:81
const LocalView & localView() const
Get the const reference to the local view.
Definition: febase.hh:136
constexpr size_t size() const
Get the size of the local view.
Definition: febase.hh:124
Traits::BasisHandler BasisHandler
Type of the basisHandler.
Definition: febase.hh:90
std::tuple< typename Skills< PreFE, FE >::Pre... > PreTuple
Definition: febase.hh:99
void bind(const GridElement &element)
Convenient function to bind the local view to the element.
Definition: febase.hh:115
PreFE::Traits Traits
Type traits.
Definition: febase.hh:89
typename Traits::FERequirementType FERequirementType
Definition: febase.hh:95
typename Traits::Element GridElement
Type of the grid element.
Definition: febase.hh:94
Class representing the requirements for finite element calculations.
Definition: ferequirements.hh:157
typename BasisHandler::FlatBasis FlatBasis
Type of the flat basis.
Definition: fetraits.hh:37
decltype(std::tuple_cat(computeSupportedResultTypes< Skills...< PreFE, typename PreFE::template FE< Skills... ... > > >()...)) SupportedResultTypes
Type alias for the supported result types by the mixin.
Definition: mixin.hh:64
Definition: utils/dirichletvalues.hh:30