14#include "dune/common/classname.hh"
15#include <dune/functions/functionspacebases/lagrangebasis.hh>
16#include <dune/functions/functionspacebases/powerbasis.hh>
17#include <dune/grid/yaspgrid.hh>
18#include <dune/python/common/typeregistry.hh>
19#include <dune/python/functions/globalbasis.hh>
20#include <dune/python/functions/subspacebasis.hh>
21#include <dune/python/pybind11/eigen.h>
22#include <dune/python/pybind11/functional.h>
23#include <dune/python/pybind11/pybind11.h>
24#include <dune/python/pybind11/stl.h>
25#include <dune/python/pybind11/stl_bind.h>
32 using FixBoundaryDOFsWithGlobalIndexFunction = std::function<void(Eigen::Ref<Eigen::VectorX<bool>>,
int)>;
34 template <
typename LV>
35 using FixBoundaryDOFsWithLocalViewFunction = std::function<void(Eigen::Ref<Eigen::VectorX<bool>>,
int, LV&)>;
37 template <
typename LV,
typename IS>
38 using FixBoundaryDOFsWithIntersectionFunction =
39 std::function<void(Eigen::Ref<Eigen::VectorX<bool>>,
int, LV&,
const IS&)>;
41 template <
typename Basis>
42 auto registerSubSpaceLocalView() {
43 pybind11::module scopedf = pybind11::module::import(
"dune.functions");
44 using LocalViewWrapper = Dune::Python::LocalViewWrapper<Basis>;
46 auto includes = Dune::Python::IncludeFiles{
"dune/python/functions/globalbasis.hh"};
48 Dune::Python::insertClass<Basis>(scopedf,
"SubspaceBasis_" + Dune::className<typename Basis::PrefixPath>(),
49 Dune::Python::GenerateTypeName(Dune::className<Basis>()), includes);
51 auto [lv, isNew] = Dune::Python::insertClass<LocalViewWrapper>(
52 scopedf,
"LocalView_" + Dune::className<typename Basis::PrefixPath>(),
53 Dune::Python::GenerateTypeName(
"Dune::Python::LocalViewWrapper", Dune::MetaType<Basis>()), includes);
55 lv.def(
"bind", &LocalViewWrapper::bind);
56 lv.def(
"unbind", &LocalViewWrapper::unbind);
57 lv.def(
"index", [](
const LocalViewWrapper& localView,
int index) {
return localView.index(index); });
58 lv.def(
"__len__", [](LocalViewWrapper& self) ->
int {
return self.size(); });
60 Dune::Python::Functions::registerTree<typename LocalViewWrapper::Tree>(lv);
61 lv.def(
"tree", [](
const LocalViewWrapper& view) {
return view.tree(); });
66template <
class DirichletValues>
69 using Intersection =
typename Basis::GridView::Intersection;
71 using MultiIndex =
typename Basis::MultiIndex;
74 pybind11::module inspect_module = pybind11::module::import(
"inspect");
75 pybind11::object result = inspect_module.attr(
"signature")(functor).attr(
"parameters");
76 size_t numParams = pybind11::len(result);
79 auto function = functor.template cast<const Impl::FixBoundaryDOFsWithGlobalIndexFunction>();
80 auto lambda = [&](BackendType& vec,
const MultiIndex& indexGlobal) { function(vec.vector(), indexGlobal); };
83 }
else if (numParams == 3) {
84 auto lambda = [&](BackendType& vec,
int localIndex,
auto&& lv) {
85 using SubSpaceBasis =
typename std::remove_cvref_t<
decltype(lv)>::GlobalBasis;
86 Impl::registerSubSpaceLocalView<SubSpaceBasis>();
88 using SubSpaceLocalViewWrapper = Dune::Python::LocalViewWrapper<SubSpaceBasis>;
89 auto lvWrapper = SubSpaceLocalViewWrapper(lv);
92 functor.template cast<const Impl::FixBoundaryDOFsWithLocalViewFunction<SubSpaceLocalViewWrapper>>();
93 function(vec.vector(), localIndex, lvWrapper);
97 }
else if (numParams == 4) {
98 auto lambda = [&](BackendType& vec,
int localIndex,
auto&& lv,
const Intersection& intersection) {
99 using SubSpaceBasis =
typename std::remove_cvref_t<
decltype(lv)>::GlobalBasis;
100 Impl::registerSubSpaceLocalView<SubSpaceBasis>();
102 using SubSpaceLocalViewWrapper = Dune::Python::LocalViewWrapper<SubSpaceBasis>;
103 auto lvWrapper = SubSpaceLocalViewWrapper(lv);
105 auto function = functor.template cast<
106 const Impl::FixBoundaryDOFsWithIntersectionFunction<SubSpaceLocalViewWrapper, Intersection>>();
107 function(vec.vector(), localIndex, lvWrapper, intersection);
112 DUNE_THROW(Dune::NotImplemented,
"fixBoundaryDOFs: A function with this signature is not supported");
148 using pybind11::operator
""_a;
153 using MultiIndex =
typename Basis::MultiIndex;
154 using LocalView =
typename Basis::LocalView;
155 using Intersection =
typename Basis::GridView::Intersection;
157 pybind11::module scopedf = pybind11::module::import(
"dune.functions");
158 using LocalViewWrapper = Dune::Python::LocalViewWrapper<Basis>;
160 auto includes = Dune::Python::IncludeFiles{
"dune/python/functions/globalbasis.hh"};
161 auto [lv, isNew] = Dune::Python::insertClass<LocalViewWrapper>(
162 scopedf,
"LocalView", Dune::Python::GenerateTypeName(
"Dune::Python::LocalViewWrapper", Dune::MetaType<Basis>()),
166 lv.def(
"bind", &LocalViewWrapper::bind);
167 lv.def(
"unbind", &LocalViewWrapper::unbind);
168 lv.def(
"index", [](
const LocalViewWrapper& localView,
int index) {
return localView.index(index); });
169 lv.def(
"__len__", [](LocalViewWrapper& self) ->
int {
return self.size(); });
171 Dune::Python::Functions::registerTree<typename LocalViewWrapper::Tree>(lv);
172 lv.def(
"tree", [](
const LocalViewWrapper& view) {
return view.tree(); });
188 [](
DirichletValues& self,
const std::function<
void(
const Basis&, Eigen::Ref<Eigen::VectorX<bool>>)>& f) {
189 auto lambda = [&](
const Basis&
basis, BackendType& vec) { f(
basis, vec.vector()); };
Definition of the LinearElastic class for finite element mechanics computations.
void registerDirichletValues(pybind11::handle scope, pybind11::class_< DirichletValues, options... > cls)
Register Python bindings for a DirichletValues class.
Definition: python/dirichletvalues/dirichletvalues.hh:147
void init(int argc, char **argv, bool enableFileLogger=true)
Initializes the Ikarus framework.
Definition: init.hh:82
Definition: flatassembler.hh:21
void forwardCorrectFunction(DirichletValues &dirichletValues, const pybind11::function &functor, auto &&cppFunction)
Definition: python/dirichletvalues/dirichletvalues.hh:67
def basis(gv, tree)
Definition: basis.py:10
def dirichletValues(basis)
Definition: dirichlet_values.py:38
Class for handling Dirichlet boundary conditions in Ikarus.
Definition: utils/dirichletvalues.hh:67
auto size() const
Definition: utils/dirichletvalues.hh:181
void setSingleDOF(const MultiIndex i, bool flag)
Fixes and unfixes (set boolean value to true or false) a specific degree of freedom.
Definition: utils/dirichletvalues.hh:135
bool isConstrained(const MultiIndex &multiIndex) const
Definition: utils/dirichletvalues.hh:166
std::remove_cvref_t< B > Basis
Definition: utils/dirichletvalues.hh:69
decltype(Dune::Functions::istlVectorBackend(std::declval< FlagsType & >())) BackendType
Definition: utils/dirichletvalues.hh:72
void fixDOFs(F &&f)
Function to fix (set boolean values to true or false) degrees of freedom.
Definition: utils/dirichletvalues.hh:123
FC FlagsType
Definition: utils/dirichletvalues.hh:70
void reset()
Resets all degrees of freedom.
Definition: utils/dirichletvalues.hh:155
auto fixedDOFsize() const
Definition: utils/dirichletvalues.hh:178
auto & container() const
Definition: utils/dirichletvalues.hh:184