Commit c8e90280 authored by Timo Koch's avatar Timo Koch
Browse files

[python][cpp] Unify style in Python binding code

parent f1d0eec8
Pipeline #6311 waiting for manual action with stages
......@@ -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);
});
}
......
......@@ -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>
......
......@@ -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);
......
......@@ -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>(),
......
......@@ -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);
});
};
......
......@@ -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);
}));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment