From a81414438920389ea65ae75e7afe63ca0848da63 Mon Sep 17 00:00:00 2001 From: henrij22 <96132706+henrij22@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:21:45 +0200 Subject: [PATCH] Fix subspacelocalview --- .../python/dirichletvalues/dirichletvalues.hh | 68 ++++++++++--------- ikarus/python/finiteelements/fe.hh | 29 ++++---- .../finiteelements/registerferequirements.hh | 20 +++--- ikarus/python/test/testdirichletvalues.py | 1 + 4 files changed, 63 insertions(+), 55 deletions(-) diff --git a/ikarus/python/dirichletvalues/dirichletvalues.hh b/ikarus/python/dirichletvalues/dirichletvalues.hh index 3082f7d2a..7ddc76a55 100644 --- a/ikarus/python/dirichletvalues/dirichletvalues.hh +++ b/ikarus/python/dirichletvalues/dirichletvalues.hh @@ -39,39 +39,27 @@ namespace Impl { using FixBoundaryDOFsWithIntersectionFunction = std::function>, int, LV&, const IS&)>; - template - auto registerLocalView() { + template + auto registerSubSpaceLocalView() { pybind11::module scopedf = pybind11::module::import("dune.functions"); - using LocalView = Dune::Python::LocalViewWrapper; + using LocalViewWrapper = Dune::Python::LocalViewWrapper; auto includes = Dune::Python::IncludeFiles{"dune/python/functions/globalbasis.hh"}; - // also register subspace basis - if constexpr (registerBasis) { - auto construct = [](const Basis& basis) { return new Basis(basis); }; - - // This if statement does absolutly nothing - if (Dune::Python::findInTypeRegistry().second) { - auto [basisCls, isNotRegistered] = Dune::Python::insertClass( - scopedf, "SubspaceBasis", Dune::Python::GenerateTypeName(Dune::className()), includes); - if (isNotRegistered) - Dune::Python::registerSubspaceBasis(scopedf, basisCls); - } - // Dune::Python::registerBasisType(scopedf, basisCls, construct, std::false_type{}); - } else { - // auto [lv, isNotRegistered] = Dune::Python::insertClass( - // scopedf, "LocalView", - // Dune::Python::GenerateTypeName("Dune::Python::LocalViewWrapper", Dune::MetaType()), includes); - - // if (isNotRegistered) { - // lv.def("bind", &LocalView::bind); - // lv.def("unbind", &LocalView::unbind); - // lv.def("index", [](const LocalView& localView, int index) { return localView.index(index); }); - // lv.def("__len__", [](LocalView& self) -> int { return self.size(); }); - - // Dune::Python::Functions::registerTree(lv); - // lv.def("tree", [](const LocalView& view) { return view.tree(); }); - // } + Dune::Python::insertClass(scopedf, "SubspaceBasis_" + Dune::className(), + Dune::Python::GenerateTypeName(Dune::className()), includes); + + auto [lv, isNew] = Dune::Python::insertClass( + scopedf, "LocalView_" + Dune::className(), + Dune::Python::GenerateTypeName("Dune::Python::LocalViewWrapper", Dune::MetaType()), includes); + if (isNew) { + lv.def("bind", &LocalViewWrapper::bind); + lv.def("unbind", &LocalViewWrapper::unbind); + lv.def("index", [](const LocalViewWrapper& localView, int index) { return localView.index(index); }); + lv.def("__len__", [](LocalViewWrapper& self) -> int { return self.size(); }); + + Dune::Python::Functions::registerTree(lv); + lv.def("tree", [](const LocalViewWrapper& view) { return view.tree(); }); } } } // namespace Impl @@ -98,7 +86,7 @@ void forwardCorrectFunction(DirichletValues& dirichletValues, const pybind11::fu } else if (numParams == 3) { auto lambda = [&](BackendType& vec, int localIndex, auto&& lv) { using SubSpaceBasis = typename std::remove_cvref_t::GlobalBasis; - Impl::registerLocalView(); + Impl::registerSubSpaceLocalView(); using SubSpaceLocalViewWrapper = Dune::Python::LocalViewWrapper; auto lvWrapper = SubSpaceLocalViewWrapper(lv); @@ -112,7 +100,7 @@ void forwardCorrectFunction(DirichletValues& dirichletValues, const pybind11::fu } else if (numParams == 4) { auto lambda = [&](BackendType& vec, int localIndex, auto&& lv, const Intersection& intersection) { using SubSpaceBasis = typename std::remove_cvref_t::GlobalBasis; - Impl::registerLocalView(); + Impl::registerSubSpaceLocalView(); using SubSpaceLocalViewWrapper = Dune::Python::LocalViewWrapper; auto lvWrapper = SubSpaceLocalViewWrapper(lv); @@ -169,7 +157,23 @@ void registerDirichletValues(pybind11::handle scope, pybind11::class_(); + pybind11::module scopedf = pybind11::module::import("dune.functions"); + using LocalViewWrapper = Dune::Python::LocalViewWrapper; + + auto includes = Dune::Python::IncludeFiles{"dune/python/functions/globalbasis.hh"}; + auto [lv, isNew] = Dune::Python::insertClass( + scopedf, "LocalView", Dune::Python::GenerateTypeName("Dune::Python::LocalViewWrapper", Dune::MetaType()), + includes); + + if (isNew) { + lv.def("bind", &LocalViewWrapper::bind); + lv.def("unbind", &LocalViewWrapper::unbind); + lv.def("index", [](const LocalViewWrapper& localView, int index) { return localView.index(index); }); + lv.def("__len__", [](LocalViewWrapper& self) -> int { return self.size(); }); + + Dune::Python::Functions::registerTree(lv); + lv.def("tree", [](const LocalViewWrapper& view) { return view.tree(); }); + } cls.def(pybind11::init([](const Basis& basis) { return new DirichletValues(basis); }), pybind11::keep_alive<1, 2>()); diff --git a/ikarus/python/finiteelements/fe.hh b/ikarus/python/finiteelements/fe.hh index 6f0e6a136..34eb02e49 100644 --- a/ikarus/python/finiteelements/fe.hh +++ b/ikarus/python/finiteelements/fe.hh @@ -113,21 +113,22 @@ void registerFE(pybind11::handle scope, pybind11::class_ cls) { pybind11::arg("Requirement"), pybind11::arg("MatrixAffordance"), pybind11::arg("elementMatrix").noconvert()); pybind11::module scopedf = pybind11::module::import("dune.functions"); + using LocalViewWrapper = Dune::Python::LocalViewWrapper; - typedef Dune::Python::LocalViewWrapper LocalViewWrapper; - auto includes = Dune::Python::IncludeFiles{"dune/python/functions/globalbasis.hh"}; - auto lv = Dune::Python::insertClass( - scopedf, "LocalViewWrapper", - Dune::Python::GenerateTypeName("Dune::Python::LocalViewWrapperWrapper", Dune::MetaType()), - includes) - .first; - lv.def("bind", &LocalViewWrapper::bind); - lv.def("unbind", &LocalViewWrapper::unbind); - lv.def("index", [](const LocalViewWrapper& localView, int index) { return localView.index(index); }); - lv.def("__len__", [](LocalViewWrapper& self) -> int { return self.size(); }); - - Dune::Python::Functions::registerTree(lv); - lv.def("tree", [](const LocalViewWrapper& view) { return view.tree(); }); + auto includes = Dune::Python::IncludeFiles{"dune/python/functions/globalbasis.hh"}; + auto [lv, isNew] = Dune::Python::insertClass( + scopedf, "LocalViewWrapper", + Dune::Python::GenerateTypeName("Dune::Python::LocalViewWrapperWrapper", Dune::MetaType()), includes); + + if (isNew) { + lv.def("bind", &LocalViewWrapper::bind); + lv.def("unbind", &LocalViewWrapper::unbind); + lv.def("index", [](const LocalViewWrapper& localView, int index) { return localView.index(index); }); + lv.def("__len__", [](LocalViewWrapper& self) -> int { return self.size(); }); + + Dune::Python::Functions::registerTree(lv); + lv.def("tree", [](const LocalViewWrapper& view) { return view.tree(); }); + } cls.def( "localView", diff --git a/ikarus/python/finiteelements/registerferequirements.hh b/ikarus/python/finiteelements/registerferequirements.hh index 0d021d8a1..10c2f7125 100644 --- a/ikarus/python/finiteelements/registerferequirements.hh +++ b/ikarus/python/finiteelements/registerferequirements.hh @@ -3,6 +3,7 @@ #pragma once +#include #include #include @@ -22,25 +23,26 @@ void registerFERequirement(pybind11::handle scope, pybind11::class_( + auto includes = Dune::Python::IncludeFiles{"ikarus/finiteelements/ferequirements.hh"}; + auto [req, isNew] = Dune::Python::insertClass( scope, "FERequirements", Dune::Python::GenerateTypeName(Dune::className()), includes); - if (isNotRegistered) { - lv.def(pybind11::init()); - lv.def(pybind11::init()); - lv.def( + if (isNew) { + req.def(pybind11::init()); + req.def(pybind11::init()); + + req.def( "insertGlobalSolution", [](FERequirements& self, SolutionVectorType solVec) { self.insertGlobalSolution(solVec); }, "solutionVector"_a.noconvert()); - lv.def( + req.def( "globalSolution", [](FERequirements& self) { return self.globalSolution(); }, pybind11::return_value_policy::reference_internal); - lv.def( + req.def( "insertParameter", [](FERequirements& self, ValueWrapper& parVal) { self.insertParameter(parVal.val); }, pybind11::keep_alive<1, 2>(), "parameterValue"_a.noconvert()); - lv.def("parameter", [](const FERequirements& self) { return self.parameter(); }); + req.def("parameter", [](const FERequirements& self) { return self.parameter(); }); } } } // namespace Ikarus::Python \ No newline at end of file diff --git a/ikarus/python/test/testdirichletvalues.py b/ikarus/python/test/testdirichletvalues.py index 63f3ccb00..22fb46649 100644 --- a/ikarus/python/test/testdirichletvalues.py +++ b/ikarus/python/test/testdirichletvalues.py @@ -100,5 +100,6 @@ def fixTopSide(vec, localIndex, localView, intersection): assert dirichletValues2.fixedDOFsize == 0 assert sum(dirichletValues2.container) == 0 + if __name__ == "__main__": testDirichletValues()