diff --git a/dumux/python/common/fvproblem.hh b/dumux/python/common/fvproblem.hh index 04206878423305e459ae1ccc812be470afabb430..1b634b519a8eccc9d280e4fde022b3610ffeac02 100644 --- a/dumux/python/common/fvproblem.hh +++ b/dumux/python/common/fvproblem.hh @@ -77,6 +77,11 @@ public: paramGroup_ = pyProblem.attr("paramGroup")().template cast<std::string>(); } + FVProblem(std::shared_ptr<const GridGeometry> gridGeometry, + pybind11::object pyProblem) + : FVProblem(gridGeometry, std::make_shared<SpatialParams>(gridGeometry), pyProblem) + {} + const std::string& name() const { return name_; } @@ -258,6 +263,10 @@ void registerFVProblem(pybind11::handle scope, pybind11::class_<Problem, options pybind11::object p){ return std::make_shared<Problem>(gridGeometry, spatialParams, p); })); + cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry, + pybind11::object p){ + return std::make_shared<Problem>(gridGeometry, p); + })); cls.def_property_readonly("name", &Problem::name); cls.def_property_readonly("numEq", [](Problem&){ return Problem::numEq; }); diff --git a/dumux/python/common/fvspatialparams.hh b/dumux/python/common/fvspatialparams.hh index 357d114e36e46ee17287a7156b3686b57b5adeae..9b69fe339b44d9597438fc4aafb717627212a2dd 100644 --- a/dumux/python/common/fvspatialparams.hh +++ b/dumux/python/common/fvspatialparams.hh @@ -65,6 +65,15 @@ public: gravity_[dimWorld-1] = -9.81; } + FVSpatialParams(std::shared_ptr<const GridGeometry> gridGeometry) + : gridGeometry_(gridGeometry) + , pySpatialParameters_{} + , gravity_(0.0) + { + if (getParam<bool>("Problem.EnableGravity")) + gravity_[dimWorld-1] = -9.81; + } + /*! * \brief Return how much the domain is extruded at a given sub-control volume. * @@ -79,12 +88,16 @@ public: const SubControlVolume& scv, const ElementSolution& elemSol) const { - if (pybind11::hasattr(pySpatialParameters_, "extrusionFactor")) - return pySpatialParameters_.attr("extrusionFactor")(element, scv, elemSol).template cast<Scalar>(); - else if (pybind11::hasattr(pySpatialParameters_, "extrusionFactorAtPos")) - return pySpatialParameters_.attr("extrusionFactorAtPos")(scv.dofPosition()).template cast<Scalar>(); - else - return 1.0; + if (pySpatialParameters_) + { + if (pybind11::hasattr(pySpatialParameters_, "extrusionFactor")) + return pySpatialParameters_.attr("extrusionFactor")(element, scv, elemSol).template cast<Scalar>(); + else if (pybind11::hasattr(pySpatialParameters_, "extrusionFactorAtPos")) + return pySpatialParameters_.attr("extrusionFactorAtPos")(scv.dofPosition()).template cast<Scalar>(); + } + + // default + return 1.0; } /*! @@ -95,12 +108,16 @@ public: const SubControlVolume& scv, const ElementSolution& elemSol) const { - if (pybind11::hasattr(pySpatialParameters_, "temperature")) - return pySpatialParameters_.attr("temperature")(element, scv, elemSol).template cast<Scalar>(); - else if (pybind11::hasattr(pySpatialParameters_, "temperatureAtPos")) - return pySpatialParameters_.attr("temperatureAtPos")(scv.dofPosition()).template cast<Scalar>(); - else - return 283.15; + if (pySpatialParameters_) + { + if (pybind11::hasattr(pySpatialParameters_, "temperature")) + return pySpatialParameters_.attr("temperature")(element, scv, elemSol).template cast<Scalar>(); + else if (pybind11::hasattr(pySpatialParameters_, "temperatureAtPos")) + return pySpatialParameters_.attr("temperatureAtPos")(scv.dofPosition()).template cast<Scalar>(); + } + + // default + return 283.15; } /*! diff --git a/python/dumux/common/CMakeLists.txt b/python/dumux/common/CMakeLists.txt index 21bad44ccf825f9063e6fdfac6fc8c2d58acfe09..31a5167f3d6615696e7439654851e9bb6f441092 100644 --- a/python/dumux/common/CMakeLists.txt +++ b/python/dumux/common/CMakeLists.txt @@ -1,5 +1,8 @@ add_python_targets(common __init__ + boundarytypes + fvproblem + fvspatialparams properties ) diff --git a/python/dumux/common/__init__.py b/python/dumux/common/__init__.py index 4bbfd02d98a2f1193e457be3a35fe3302f7b9189..f2fa10c16174a44718e47fb2f25c4a4dac6faab9 100644 --- a/python/dumux/common/__init__.py +++ b/python/dumux/common/__init__.py @@ -7,108 +7,9 @@ from dune.generator.generator import SimpleGenerator from dune.common.hashit import hashIt from dumux.common.properties import Model, Property +from dumux.common.boundarytypes import BoundaryTypes +from dumux.common.fvproblem import FVProblem +from dumux.common.fvspatialparams import FVSpatialParams from dumux.wrapping import cppWrapperCreator, cppWrapperClassAlias from ._common import * - - -@cppWrapperCreator -def _createFVProblemDecorator( - gridGeometry, spatialParams=None, enableInternalDirichletConstraints=False -): - """A problem decorator generator for Python problems - - from dumux.common import FVProblem - @FVProblem(gridGeometry) - class MyProblem: - ... - """ - - def createModule(numEq): - ggType = gridGeometry._typeName - spType = spatialParams._typeName - priVarType = f"Dune::FieldVector<double, {numEq}>" - enableIntDirConstraint = "true" if enableInternalDirichletConstraints else "false" - problemType = ( - f"Dumux::Python::FVProblem<{ggType}, {spType}, {priVarType}, {enableIntDirConstraint}>" - ) - includes = gridGeometry._includes + ["dumux/python/common/fvproblem.hh"] - moduleName = "fvproblem_" + hashIt(problemType) - holderType = f"std::shared_ptr<{problemType}>" - generator = SimpleGenerator("FVProblem", "Dumux::Python") - module = generator.load(includes, problemType, moduleName, options=[holderType]) - return module - - def decorateFVProblem(cls): - module = createModule(cls.numEq) - - def createFVProblem(): - return module.FVProblem(gridGeometry, spatialParams, cls()) - - return createFVProblem - - return decorateFVProblem - - -@cppWrapperClassAlias(creator=_createFVProblemDecorator) -class FVProblem: - """Class alias used to decorate a Python finite volume problem""" - - -@cppWrapperCreator -def _createBoundaryTypes(numEq=1): - """Create BoundaryTypes instances""" - - # only compile this once per numEq - cacheKey = f"BoundaryTypes_{numEq}" - try: - return globals()[cacheKey]() - except KeyError: - includes = ["dumux/python/common/boundarytypes.hh"] - typeName = f"Dumux::BoundaryTypes<{numEq}>" - moduleName = "boundarytypes_" + hashIt(typeName) - generator = SimpleGenerator("BoundaryTypes", "Dumux::Python") - module = generator.load(includes, typeName, moduleName) - globals().update({cacheKey: module.BoundaryTypes}) - return globals()[cacheKey]() - - -@cppWrapperClassAlias(creator=_createBoundaryTypes) -class BoundaryTypes: - """Class alias used to create a BoundaryTypes instance""" - - -@cppWrapperCreator -def _createFVSpatialParamsDecorator(gridGeometry): - """A spatial params decorator generator for Python spatial params - - from dumux.common import FVSpatialParams - @FVSpatialParams(gridGeometry) - class MySpatialParams: - ... - """ - - def createModule(): - ggType = gridGeometry._typeName - spatialParamsType = f"Dumux::Python::FVSpatialParams<{ggType}>" - includes = gridGeometry._includes + ["dumux/python/common/fvspatialparams.hh"] - moduleName = "fvspatialparams_" + hashIt(spatialParamsType) - holderType = f"std::shared_ptr<{spatialParamsType}>" - generator = SimpleGenerator("FVSpatialParams", "Dumux::Python") - module = generator.load(includes, spatialParamsType, moduleName, options=[holderType]) - return module - - def decorateFVSpatialParams(cls): - module = createModule() - - def createFVSpatialParams(): - return module.FVSpatialParams(gridGeometry, cls()) - - return createFVSpatialParams - - return decorateFVSpatialParams - - -@cppWrapperClassAlias(creator=_createFVSpatialParamsDecorator) -class FVSpatialParams: - """Class alias used to decorate a Python Finite Volume Spatial Params""" diff --git a/python/dumux/common/boundarytypes.py b/python/dumux/common/boundarytypes.py new file mode 100644 index 0000000000000000000000000000000000000000..7029b5ca2cf139fe383db9eca26b885a809fddcb --- /dev/null +++ b/python/dumux/common/boundarytypes.py @@ -0,0 +1,31 @@ +""" +Boundary types generator +""" + +from dune.generator.generator import SimpleGenerator +from dune.common.hashit import hashIt + +from dumux.wrapping import cppWrapperCreator, cppWrapperClassAlias + + +@cppWrapperCreator +def _createBoundaryTypes(numEq=1): + """Create BoundaryTypes instances""" + + # only compile this once per numEq + cacheKey = f"BoundaryTypes_{numEq}" + try: + return globals()[cacheKey]() + except KeyError: + includes = ["dumux/python/common/boundarytypes.hh"] + typeName = f"Dumux::BoundaryTypes<{numEq}>" + moduleName = "boundarytypes_" + hashIt(typeName) + generator = SimpleGenerator("BoundaryTypes", "Dumux::Python") + module = generator.load(includes, typeName, moduleName) + globals().update({cacheKey: module.BoundaryTypes}) + return globals()[cacheKey]() + + +@cppWrapperClassAlias(creator=_createBoundaryTypes) +class BoundaryTypes: + """Class alias used to create a BoundaryTypes instance""" diff --git a/python/dumux/common/fvproblem.py b/python/dumux/common/fvproblem.py new file mode 100644 index 0000000000000000000000000000000000000000..416500c3164bfb75b7f6db8348c976e4599bccb2 --- /dev/null +++ b/python/dumux/common/fvproblem.py @@ -0,0 +1,58 @@ +""" +Finite volume problem generator +""" + +from dune.generator.generator import SimpleGenerator +from dune.common.hashit import hashIt + +from dumux.wrapping import cppWrapperCreator, cppWrapperClassAlias + + +@cppWrapperCreator +def _createFVProblemDecorator( + gridGeometry, spatialParams=None, enableInternalDirichletConstraints=False +): + """A problem decorator generator for Python problems + + from dumux.common import FVProblem + @FVProblem(gridGeometry) + class MyProblem: + ... + """ + + def createModule(numEq): + ggType = gridGeometry._typeName + if spatialParams is not None: + spType = spatialParams._typeName + else: + spType = f"Dumux::Python::FVSpatialParams<{ggType}>" + + priVarType = f"Dune::FieldVector<double, {numEq}>" + enableIntDirConstraint = "true" if enableInternalDirichletConstraints else "false" + problemType = ( + f"Dumux::Python::FVProblem<{ggType}, {spType}, {priVarType}, {enableIntDirConstraint}>" + ) + includes = gridGeometry._includes + ["dumux/python/common/fvproblem.hh"] + moduleName = "fvproblem_" + hashIt(problemType) + holderType = f"std::shared_ptr<{problemType}>" + generator = SimpleGenerator("FVProblem", "Dumux::Python") + module = generator.load(includes, problemType, moduleName, options=[holderType]) + return module + + def decorateFVProblem(cls): + module = createModule(cls.numEq) + + def createFVProblem(): + if spatialParams is not None: + return module.FVProblem(gridGeometry, spatialParams, cls()) + + return module.FVProblem(gridGeometry, cls()) + + return createFVProblem + + return decorateFVProblem + + +@cppWrapperClassAlias(creator=_createFVProblemDecorator) +class FVProblem: + """Class alias used to decorate a Python finite volume problem""" diff --git a/python/dumux/common/fvspatialparams.py b/python/dumux/common/fvspatialparams.py new file mode 100644 index 0000000000000000000000000000000000000000..812df3f3b6bd855180e0b2320e4af02ac03d5291 --- /dev/null +++ b/python/dumux/common/fvspatialparams.py @@ -0,0 +1,44 @@ +""" +Finite volume spatial params generator +""" + +from dune.generator.generator import SimpleGenerator +from dune.common.hashit import hashIt + +from dumux.wrapping import cppWrapperCreator, cppWrapperClassAlias + + +@cppWrapperCreator +def _createFVSpatialParamsDecorator(gridGeometry): + """A spatial params decorator generator for Python spatial params + + from dumux.common import FVSpatialParams + @FVSpatialParams(gridGeometry) + class MySpatialParams: + ... + """ + + def createModule(): + ggType = gridGeometry._typeName + spatialParamsType = f"Dumux::Python::FVSpatialParams<{ggType}>" + includes = gridGeometry._includes + ["dumux/python/common/fvspatialparams.hh"] + moduleName = "fvspatialparams_" + hashIt(spatialParamsType) + holderType = f"std::shared_ptr<{spatialParamsType}>" + generator = SimpleGenerator("FVSpatialParams", "Dumux::Python") + module = generator.load(includes, spatialParamsType, moduleName, options=[holderType]) + return module + + def decorateFVSpatialParams(cls): + module = createModule() + + def createFVSpatialParams(): + return module.FVSpatialParams(gridGeometry, cls()) + + return createFVSpatialParams + + return decorateFVSpatialParams + + +@cppWrapperClassAlias(creator=_createFVSpatialParamsDecorator) +class FVSpatialParams: + """Class alias used to decorate a Python Finite Volume Spatial Params""" diff --git a/test/python/test_explicit_transport_cctpfa.py b/test/python/test_explicit_transport_cctpfa.py index 6e1297bbe4d928031bf00c5112c1af9f68c52cfe..670716406a7b6ef60a8c2a1db79c026527bac8d3 100755 --- a/test/python/test_explicit_transport_cctpfa.py +++ b/test/python/test_explicit_transport_cctpfa.py @@ -2,7 +2,7 @@ from dune.common import FieldVector from dune.grid import structuredGrid, gridFunction, OutputType -from dumux.common import FVProblem, BoundaryTypes, TimeLoop, FVSpatialParams +from dumux.common import FVProblem, BoundaryTypes, TimeLoop from dumux.discretization import GridGeometry import numpy as np @@ -29,19 +29,12 @@ gridGeometry = GridGeometry(gridView, discMethod="cctpfa") elementMapper = gridView.indexSet -############################################## -# Define problem (inital/boundary condtions) # -############################################## -# Define the spatial parameters -@FVSpatialParams(gridGeometry=gridGeometry) -class SpatialParams: - pass +################################################ +# Define problem (initial/boundary conditions) # +################################################ -spatialParams = SpatialParams() - - -@FVProblem(gridGeometry=gridGeometry, spatialParams=spatialParams) +@FVProblem(gridGeometry=gridGeometry) class Problem: numEq = 1 diff --git a/test/python/test_fvproblem.py b/test/python/test_fvproblem.py index b4fbd99001c3329e935c3b1c29528b0c20e51974..526a23aab23609c52b86366f1656ef86450f949a 100755 --- a/test/python/test_fvproblem.py +++ b/test/python/test_fvproblem.py @@ -20,22 +20,14 @@ def PrintProblemTest(problem): ############################################################ from dune.grid import structuredGrid from dumux.discretization import GridGeometry -from dumux.common import BoundaryTypes, FVProblem, FVSpatialParams +from dumux.common import BoundaryTypes, FVProblem gridView = structuredGrid([0, 0, 0], [1, 1, 1], [3, 3, 3]) gridGeometry = GridGeometry(gridView, discMethod="box") -# Define the spatial parameters -@FVSpatialParams(gridGeometry=gridGeometry) -class SpatialParams: - pass - -spatialParams = SpatialParams() - - -@FVProblem(gridGeometry=gridGeometry, spatialParams=spatialParams) +@FVProblem(gridGeometry=gridGeometry) class Problem: numEq = 2