Skip to content
Snippets Groups Projects
Commit 9bd121e6 authored by Kilian Weishaupt's avatar Kilian Weishaupt Committed by Timo Koch
Browse files

[python] Add params

parent 153040b7
No related branches found
No related tags found
1 merge request!2681Feature/python main file
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
/*!
* \file
* \brief TODO: docme!
*/
#ifndef DUMUX_PYTHON_COMMON_PARAMETERS_HH
#define DUMUX_PYTHON_COMMON_PARAMETERS_HH
#include <dumux/common/parameters.hh>
#include <dune/python/pybind11/pybind11.h>
#include <dune/python/pybind11/stl.h>
namespace Dumux::Python {
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)
{
for (const auto& p : params)
tree[p.first] = p.second;
};
};
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);
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>
void registerParameters(pybind11::handle scope, const char *clsName = "Parameters")
{
pybind11::class_<Parameters> cls(scope, clsName);
registerParameters(scope, cls);
}
} // namespace Dumux::Python
#endif
from ._common import *
from dumux.common.properties import Model, Property
from dune.generator.generator import SimpleGenerator
from dune.common.hashit import hashIt
# A problem decorator generator for Python problems
#
# from dumux.common import FVProblem
......@@ -25,6 +28,7 @@ def FVProblem(gridGeometry):
def FVProblemDecorator(Cls):
module = createModule(Cls.numEq)
def createFVProblem():
return module.FVProblem(gridGeometry, Cls())
return createFVProblem
......@@ -34,7 +38,7 @@ def FVProblem(gridGeometry):
# Function for JIT copmilation of Dumux::BoundaryTypes
def BoundaryTypes(numEq=1):
# only copmile this once per numEq
# only compile this once per numEq
cacheKey = "BoundaryTypes_{}".format(numEq)
try:
return globals()[cacheKey]()
......@@ -46,3 +50,21 @@ def BoundaryTypes(numEq=1):
module = generator.load(includes, typeName, moduleName)
globals().update({cacheKey : module.BoundaryTypes})
return globals()[cacheKey]()
def Parameters(*, file=None, dict={}):
parametersType = "Dumux::Parameters"
includes = ["dumux/common/parameters.hh", "dumux/python/common/parameters.hh"]
moduleName = "parameters_" + hashIt(parametersType)
generator = SimpleGenerator("Parameters", "Dumux::Python")
module = generator.load(includes, parametersType, moduleName)
# make sure all dict keys are strings
for key in dict:
if not isinstance(dict[key], str):
dict[key] = str(dict[key])
if file is not None:
return module.Parameters(file, dict)
else:
return module.Parameters(dict)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment