From aef9f254e668bdeee4ada51e87d1cf970882d6c2 Mon Sep 17 00:00:00 2001 From: Timo Koch <timo.koch@iws.uni-stuttgart.de> Date: Sat, 24 Jul 2021 13:45:31 +0200 Subject: [PATCH] [python][cpp] Unify style in Python binding code --- dumux/python/assembly/fvassembler.hh | 6 +-- dumux/python/common/parameters.hh | 37 +++++++++---------- dumux/python/discretization/gridgeometry.hh | 4 +- dumux/python/discretization/gridvariables.hh | 31 +++++++--------- dumux/python/io/vtkoutputmodule.hh | 16 ++++---- .../python/porousmediumflow/velocityoutput.hh | 4 +- 6 files changed, 48 insertions(+), 50 deletions(-) diff --git a/dumux/python/assembly/fvassembler.hh b/dumux/python/assembly/fvassembler.hh index 5359b88e3a..1ede28dc75 100644 --- a/dumux/python/assembly/fvassembler.hh +++ b/dumux/python/assembly/fvassembler.hh @@ -59,15 +59,15 @@ void registerFVAssembler(pybind11::handle scope, pybind11::class_<FVAssembler, o cls.def_property_readonly("isStationaryProblem", &FVAssembler::isStationaryProblem); cls.def_property_readonly("gridVariables", [](FVAssembler& self) { return self.gridVariables(); }); - cls.def("updateGridVariables", [](FVAssembler& self, const SolutionVector& curSol) { + cls.def("updateGridVariables", [](FVAssembler& self, const SolutionVector& curSol){ self.updateGridVariables(curSol); }); - cls.def("assembleResidual", [](FVAssembler& self, const SolutionVector& curSol) { + cls.def("assembleResidual", [](FVAssembler& self, const SolutionVector& curSol){ self.assembleResidual(curSol); }); - cls.def("assembleJacobianAndResidual", [](FVAssembler& self, const SolutionVector& curSol) { + cls.def("assembleJacobianAndResidual", [](FVAssembler& self, const SolutionVector& curSol){ self.assembleJacobianAndResidual(curSol); }); } diff --git a/dumux/python/common/parameters.hh b/dumux/python/common/parameters.hh index d02aceaa21..d743c69727 100644 --- a/dumux/python/common/parameters.hh +++ b/dumux/python/common/parameters.hh @@ -35,10 +35,10 @@ template <class... options> void registerParameters(pybind11::handle scope, pybind11::class_<Parameters, options...> cls) { - auto setParams = [](const std::unordered_map<std::string, std::string>& params) - { - return [&](Dune::ParameterTree& tree) - { + using pybind11::operator""_a; + + auto setParams = [](const std::unordered_map<std::string, std::string>& params){ + return [&](Dune::ParameterTree& tree){ for (const auto& p : params) tree[p.first] = p.second; }; @@ -46,22 +46,21 @@ void registerParameters(pybind11::handle scope, cls.def(pybind11::init([setParams](const std::string& parameterFileName, const std::unordered_map<std::string, std::string>& params, - bool inputFileOverwritesParams) - { - auto p = new Parameters(); - p->init(parameterFileName, setParams(params), inputFileOverwritesParams); - return p; - }), - pybind11::arg("parameterFileName"), - pybind11::arg("params") = std::unordered_map<std::string, std::string>{}, - pybind11::arg("inputFileOverwritesParams") = false); + bool inputFileOverwritesParams){ + auto p = new Parameters(); + p->init(parameterFileName, setParams(params), inputFileOverwritesParams); + return p; + }) + , "parameterFileName"_a + , "params"_a = std::unordered_map<std::string, std::string>{} + , "inputFileOverwritesParams"_a = false + ); - cls.def(pybind11::init([setParams](const std::unordered_map<std::string, std::string>& params) - { - auto p = new Parameters(); - p->init(setParams(params)); - return p; - })); + cls.def(pybind11::init([setParams](const std::unordered_map<std::string, std::string>& params){ + auto p = new Parameters(); + p->init(setParams(params)); + return p; + })); } template<class Scalar> diff --git a/dumux/python/discretization/gridgeometry.hh b/dumux/python/discretization/gridgeometry.hh index 6b523fa6ec..f153f9caa3 100644 --- a/dumux/python/discretization/gridgeometry.hh +++ b/dumux/python/discretization/gridgeometry.hh @@ -147,7 +147,9 @@ void registerGridGeometry(pybind11::handle scope, pybind11::class_<GG, Options.. return toString(GG::discMethod); }); - cls.def_property_readonly("localView", [](GG& self){ return localView(self); }); + cls.def_property_readonly("localView", [](GG& self){ + return localView(self); + }); using SubControlVolume = typename GG::SubControlVolume; Impl::registerSubControlVolume<SubControlVolume>(scope); diff --git a/dumux/python/discretization/gridvariables.hh b/dumux/python/discretization/gridvariables.hh index 2973015288..f2edf6039a 100644 --- a/dumux/python/discretization/gridvariables.hh +++ b/dumux/python/discretization/gridvariables.hh @@ -49,13 +49,11 @@ void registerElementSolution(pybind11::handle scope) { using pybind11::operator""_a; - cls.def("__getitem__", - [](const ElementSolution& self, std::size_t i) - { - if (i >= self.size()) - throw pybind11::index_error(); - return self[i]; - }); + cls.def("__getitem__", [](const ElementSolution& self, std::size_t i){ + if (i >= self.size()) + throw pybind11::index_error(); + return self[i]; + }); cls.def_property_readonly("size", &ElementSolution::size); } @@ -88,18 +86,17 @@ void registerGridVariables(pybind11::handle scope, pybind11::class_<GV, Options. cls.def_property_readonly("prevGridVolVars", [](GV& self) { return self.prevGridVolVars(); }); cls.def_property_readonly("gridGeometry", &GV::gridGeometry); - cls.def("updateAfterGridAdaption", [](GV& self, const SolutionVector& sol) - { return self.updateAfterGridAdaption(sol); } - ); + cls.def("updateAfterGridAdaption", [](GV& self, const SolutionVector& sol){ + return self.updateAfterGridAdaption(sol); + }); - cls.def("resetTimeStep", [](GV& self, const SolutionVector& sol) - { return self.resetTimeStep(sol); } - ); + cls.def("resetTimeStep", [](GV& self, const SolutionVector& sol){ + return self.resetTimeStep(sol); + }); - cls.def("update", [](GV& self, const SolutionVector& sol, - const bool forceFluxCacheUpdate = false) - { return self.update(sol, forceFluxCacheUpdate); } - ); + cls.def("update", [](GV& self, const SolutionVector& sol, const bool forceFluxCacheUpdate = false){ + return self.update(sol, forceFluxCacheUpdate); + }); using ElementSolution = std::decay_t<decltype(elementSolution(std::declval<Element>(), std::declval<SolutionVector>(), diff --git a/dumux/python/io/vtkoutputmodule.hh b/dumux/python/io/vtkoutputmodule.hh index 319146c551..7272fc4e5a 100644 --- a/dumux/python/io/vtkoutputmodule.hh +++ b/dumux/python/io/vtkoutputmodule.hh @@ -53,22 +53,22 @@ void registerVtkOutputModule(pybind11::handle scope, using Scalar = double; - cls.def("addField", [](VtkOutputModule& self, const SolutionVector& sol, const std::string& name) - { self.addField(sol, name); } - ); + cls.def("addField", [](VtkOutputModule& self, const SolutionVector& sol, const std::string& name){ + self.addField(sol, name); + }); - cls.def("write", [](VtkOutputModule& self, Scalar time){ self.write(time); }); + cls.def("write", [](VtkOutputModule& self, Scalar time){ + self.write(time); + }); cls.def("addVolumeVariable", [](VtkOutputModule& self, std::function<Scalar(const VolumeVariables&)>&& f, - const std::string& name) { + const std::string& name){ self.addVolumeVariable(std::move(f), name); }); using VelocityOutputType = Dumux::VelocityOutput<GridVariables>; - cls.def("addVelocityOutput", [](VtkOutputModule& self, - std::shared_ptr<VelocityOutputType> velocityOutput) - { + cls.def("addVelocityOutput", [](VtkOutputModule& self, std::shared_ptr<VelocityOutputType> velocityOutput){ self.addVelocityOutput(velocityOutput); }); }; diff --git a/dumux/python/porousmediumflow/velocityoutput.hh b/dumux/python/porousmediumflow/velocityoutput.hh index 1d83ce2fd4..1b53c62d7c 100644 --- a/dumux/python/porousmediumflow/velocityoutput.hh +++ b/dumux/python/porousmediumflow/velocityoutput.hh @@ -37,8 +37,8 @@ void registerPorousMediumFlowVelocityOutput(pybind11::handle scope, using pybind11::operator""_a; using namespace Dune::Python; using VelocityOutput = Dumux::PorousMediumFlowVelocityOutput<GridVariables, FluxVariables>; - cls.def(pybind11::init([](const GridVariables& gridVariables) - { + + cls.def(pybind11::init([](const GridVariables& gridVariables){ return std::make_shared<VelocityOutput>(gridVariables); })); } -- GitLab