version 0.4.2
flatassembler.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/pybind11/eigen.h>
13#include <dune/python/pybind11/pybind11.h>
14#include <dune/python/pybind11/stl.h>
15#include <dune/python/pybind11/stl_bind.h>
16
19#include <ikarus/utils/basis.hh>
20
21namespace Ikarus::Python {
22
23#define MAKE_ASSEMBLER_REGISTERY_FUNCTION(name) \
24 \
43 template <class Assembler, class... options> \
44 void register##name(pybind11::handle scope, pybind11::class_<Assembler, options...> cls) { \
45 using pybind11::operator""_a; \
46 using FEContainer = typename Assembler::FEContainer; \
47 using Basis = typename Assembler::Basis; \
48 using DirichletValuesType = typename Assembler::DirichletValuesType; \
49 using AffordanceCollectionType = typename Assembler::AffordanceCollectionType; \
50 using FERequirementType = typename Assembler::FERequirement; \
51 pybind11::module m = pybind11::module::import("ikarus"); \
52 cls.def(pybind11::init([](const pybind11::list& fes, const DirichletValuesType& dirichletValues) { \
53 /*here a copy of the whole vector of fes takes place! There is no way to prevent this if we want that \
54 * the user can pass native python lists here, see \
55 * https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html */ \
56 FEContainer fesV = fes.template cast<FEContainer>(); \
57 return new Assembler(std::move(fesV), dirichletValues); \
58 }), \
59 pybind11::keep_alive<1, 3>()); \
60 \
61 /* sparse matrices need to be copied to python therefore we remove the reference of the return type, see */ \
62 /* https://github.com/pybind/pybind11/blob/cbb876cc7b02c5f57e715cbc2c46ead3d1fbcf79/tests/test_eigen_matrix.cpp#L332-L341 \
63 */ \
64 cls.def( \
65 "matrix", \
66 [](Assembler& self, const FERequirementType& req, Ikarus::MatrixAffordance affordance, \
67 Ikarus::DBCOption dbcOption) -> std::remove_cvref_t<decltype(self.matrix(req, affordance))> { \
68 return self.matrix(req, affordance, dbcOption); \
69 }, \
70 pybind11::return_value_policy::copy); \
71 \
72 cls.def( \
73 "matrix", [](Assembler& self) -> std::remove_cvref_t<decltype(self.matrix())> { return self.matrix(); }, \
74 pybind11::return_value_policy::copy); \
75 \
76 cls.def( \
77 "matrix", \
78 [](Assembler& self, Ikarus::DBCOption dbcOption) -> std::remove_cvref_t<decltype(self.matrix(dbcOption))> { \
79 return self.matrix(dbcOption); \
80 }, \
81 pybind11::return_value_policy::copy); \
82 \
83 cls.def( \
84 "vector", \
85 [](Assembler& self, const FERequirementType& req, Ikarus::VectorAffordance affordance, \
86 Ikarus::DBCOption dbcOption) { return self.vector(req, affordance, dbcOption); }, \
87 pybind11::return_value_policy::reference); \
88 \
89 cls.def("vector", [](Assembler& self) { return self.vector(); }, pybind11::return_value_policy::reference); \
90 \
91 cls.def( \
92 "vector", [](Assembler& self, Ikarus::DBCOption dbcOption) { return self.vector(dbcOption); }, \
93 pybind11::return_value_policy::reference); \
94 \
95 cls.def( \
96 "scalar", \
97 [](Assembler& self, const FERequirementType& req, Ikarus::ScalarAffordance affordance) { \
98 return self.scalar(req, affordance); \
99 }, \
100 pybind11::return_value_policy::copy); \
101 \
102 cls.def("scalar", [](Assembler& self) { return self.scalar(); }, pybind11::return_value_policy::copy); \
104 cls.def( \
105 "createFullVector", \
106 [](Assembler& self, Eigen::Ref<const Eigen::VectorXd> redVec) { return self.createFullVector(redVec); }, \
107 pybind11::return_value_policy::move); \
108 cls.def("reducedSize", [](Assembler& self) { return self.reducedSize(); }, pybind11::return_value_policy::copy); \
109 cls.def("bind", [](Assembler& self, const FERequirementType& req, AffordanceCollectionType affordance, \
110 DBCOption dbcOption = DBCOption::Full) { return self.bind(req, affordance, dbcOption); }); \
111 cls.def("bind", [](Assembler& self, const FERequirementType& req) { return self.bind(req); }); \
112 cls.def("bind", [](Assembler& self, const AffordanceCollectionType affordance) { return self.bind(affordance); }); \
113 cls.def("bind", [](Assembler& self, const DBCOption dbcOption) { return self.bind(dbcOption); }); \
114 cls.def("bound", &Assembler::bound); \
115 cls.def("boundToRequirement", &Assembler::boundToRequirement); \
116 cls.def("boundToAffordanceCollection", &Assembler::boundToAffordanceCollection); \
117 cls.def("boundToDBCOption", &Assembler::boundToDBCOption); \
118 cls.def("requirement", &Assembler::requirement); \
119 cls.def("affordanceCollection", &Assembler::affordanceCollection); \
120 cls.def("dBCOption", &Assembler::dBCOption); \
121 }
122// TODO Alex add binding functions
125
126} // namespace Ikarus::Python
#define MAKE_ASSEMBLER_REGISTERY_FUNCTION(name)
Definition: flatassembler.hh:23
Definition of the LinearElastic class for finite element mechanics computations.
Definition: flatassembler.hh:21
SparseFlatAssembler assembles matrix quantities using a flat basis Indexing strategy....
Definition: simpleassemblers.hh:426
DenseFlatAssembler assembles matrix quantities using a flat basis Indexing strategy....
Definition: simpleassemblers.hh:553
Wrapper around Dune-functions global basis.