version 0.4.1
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
40template <class Assembler, class... options>
41void registerFlatAssembler(pybind11::handle scope, pybind11::class_<Assembler, options...> cls) {
42 using pybind11::operator""_a;
43 using FEContainer = typename Assembler::FEContainer;
44 using Basis = typename Assembler::Basis;
45 using DirichletValuesType = typename Assembler::DirichletValuesType;
46 using AffordanceCollectionType = typename Assembler::AffordanceCollectionType;
47 using FERequirementType = typename Assembler::FERequirement;
48 using SizeType = typename Assembler::SizeType;
49
50 cls.def(pybind11::init([](const pybind11::list& fes, const DirichletValuesType& dirichletValues) {
51 /*here a copy of the whole vector of fes takes place! There is no way to prevent this if we want that
52 * the user can pass native python lists here, see
53 * https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html */
54 FEContainer fesV = fes.template cast<FEContainer>();
55 return new Assembler(std::move(fesV), dirichletValues);
56 }),
57 pybind11::keep_alive<1, 3>());
58
59 /* sparse matrices need to be copied to python therefore we remove the reference of the return type, see */
60 /* https://github.com/pybind/pybind11/blob/cbb876cc7b02c5f57e715cbc2c46ead3d1fbcf79/tests/test_eigen_matrix.cpp#L332-L341
61 */
62 cls.def(
63 "matrix",
64 [](Assembler& self, const FERequirementType& req, Ikarus::MatrixAffordance affordance,
65 Ikarus::DBCOption dbcOption) -> std::remove_cvref_t<decltype(self.matrix(req, affordance))> {
66 return self.matrix(req, affordance, dbcOption);
67 },
68 pybind11::return_value_policy::copy);
69
70 cls.def(
71 "matrix", [](Assembler& self) -> std::remove_cvref_t<decltype(self.matrix())> { return self.matrix(); },
72 pybind11::return_value_policy::copy);
73
74 cls.def(
75 "matrix",
76 [](Assembler& self, Ikarus::DBCOption dbcOption) -> std::remove_cvref_t<decltype(self.matrix(dbcOption))> {
77 return self.matrix(dbcOption);
78 },
79 pybind11::return_value_policy::copy);
80
81 cls.def(
82 "vector",
83 [](Assembler& self, const FERequirementType& req, Ikarus::VectorAffordance affordance,
84 Ikarus::DBCOption dbcOption) { return self.vector(req, affordance, dbcOption); },
85 pybind11::return_value_policy::reference);
86
87 cls.def("vector", [](Assembler& self) { return self.vector(); }, pybind11::return_value_policy::reference);
88
89 cls.def(
90 "vector", [](Assembler& self, Ikarus::DBCOption dbcOption) { return self.vector(dbcOption); },
91 pybind11::return_value_policy::reference);
92
93 cls.def(
94 "scalar",
95 [](Assembler& self, const FERequirementType& req, Ikarus::ScalarAffordance affordance) {
96 return self.scalar(req, affordance);
97 },
98 pybind11::return_value_policy::copy);
99
100 cls.def("scalar", [](Assembler& self) { return self.scalar(); }, pybind11::return_value_policy::copy);
101
102 cls.def(
103 "createFullVector",
104 [](Assembler& self, Eigen::Ref<const Eigen::VectorXd> redVec) { return self.createFullVector(redVec); },
105 pybind11::return_value_policy::move);
106 cls.def("reducedSize", [](Assembler& self) { return self.reducedSize(); }, pybind11::return_value_policy::copy);
107 cls.def("bind", [](Assembler& self, const FERequirementType& req, AffordanceCollectionType affordance,
108 DBCOption dbcOption = DBCOption::Full) { return self.bind(req, affordance, dbcOption); });
109 cls.def("bind", [](Assembler& self, const FERequirementType& req) { return self.bind(req); });
110 cls.def("bind", [](Assembler& self, const AffordanceCollectionType affordance) { return self.bind(affordance); });
111 cls.def("bind", [](Assembler& self, const DBCOption dbcOption) { return self.bind(dbcOption); });
112 cls.def("bound", [](Assembler& self) { return self.bound(); });
113 cls.def("boundToRequirement", [](Assembler& self) { return self.boundToRequirement(); });
114 cls.def("boundToAffordanceCollection", [](Assembler& self) { return self.boundToAffordanceCollection(); });
115 cls.def("boundToDBCOption", [](Assembler& self) { return self.boundToDBCOption(); });
116 cls.def("requirement", [](Assembler& self) { return self.requirement(); });
117 cls.def("affordanceCollection", [](Assembler& self) { return self.affordanceCollection(); });
118 cls.def("dBCOption", [](Assembler& self) { return self.dBCOption(); });
119 cls.def_property_readonly("size", [](Assembler& self) { return self.size(); });
120 cls.def("__len__", [](Assembler& self) { return self.size(); });
121 cls.def("constraintsBelow", [](Assembler& self, SizeType i) { return self.constraintsBelow(i); });
122 cls.def("isConstrained", [](Assembler& self, SizeType i) { return self.isConstrained(i); });
123 cls.def_property_readonly("gridView", [](Assembler& self) { return self.gridView(); });
124}
125
126#define MAKE_ASSEMBLER_REGISTERY_FUNCTION(name) \
127 template <class Assembler, class... options> \
128 void register##name(pybind11::handle scope, pybind11::class_<Assembler, options...> cls) { \
129 registerFlatAssembler(scope, cls); \
130 }
131
134
135} // namespace Ikarus::Python
#define MAKE_ASSEMBLER_REGISTERY_FUNCTION(name)
Definition: flatassembler.hh:126
Definition of the LinearElastic class for finite element mechanics computations.
void registerFlatAssembler(pybind11::handle scope, pybind11::class_< Assembler, options... > cls)
Register Python bindings for a assembler class. .
Definition: flatassembler.hh:41
MatrixAffordance
A strongly typed enum class representing the matrix affordance.
Definition: ferequirements.hh:63
DBCOption
Definition: dirichletbcenforcement.hh:7
VectorAffordance
A strongly typed enum class representing the vector affordance.
Definition: ferequirements.hh:48
void init(int argc, char **argv, bool enableFileLogger=true)
Initializes the Ikarus framework.
Definition: init.hh:82
ScalarAffordance
A strongly typed enum class representing the scalar affordance.
Definition: ferequirements.hh:37
Definition: flatassembler.hh:21
def dirichletValues(basis)
Definition: dirichlet_values.py:38
SparseFlatAssembler assembles matrix quantities using a flat basis Indexing strategy....
Definition: simpleassemblers.hh:111
DenseFlatAssembler assembles matrix quantities using a flat basis Indexing strategy....
Definition: simpleassemblers.hh:191
Wrapper around Dune-functions global basis.