24#ifndef DUMUX_PYTHON_COMMON_FVPROBLEM_HH
25#define DUMUX_PYTHON_COMMON_FVPROBLEM_HH
31#include <dune/common/fvector.hh>
32#include <dune/common/exceptions.hh>
33#include <dune/python/pybind11/pybind11.h>
46template<
class Gr
idGeometry_,
class SpatialParams_,
class PrimaryVariables,
bool enableInternalDirichletConstra
ints_>
52 using Scalar =
typename GridGeometry::GridView::ctype;
53 using NumEqVector = Dune::FieldVector<Scalar, PrimaryVariables::dimension>;
54 using Element =
typename GridGeometry::GridView::template Codim<0>::Entity;
61 static constexpr std::size_t
numEq =
static_cast<std::size_t
>(PrimaryVariables::dimension);
66 pybind11::object pyProblem)
68 , pyProblem_(pyProblem)
69 , name_(
"python_problem")
73 if (pybind11::hasattr(pyProblem_,
"name"))
74 name_ = pyProblem.attr(
"name")().
template cast<std::string>();
76 if (pybind11::hasattr(pyProblem_,
"paramGroup"))
77 paramGroup_ = pyProblem.attr(
"paramGroup")().
template cast<std::string>();
81 pybind11::object pyProblem)
85 const std::string&
name()
const
89 {
return paramGroup_; }
95 DUNE_THROW(Dune::InvalidStateException,
"boundaryTypes(..., scv) called for cell-centered method.");
98 if (pybind11::hasattr(pyProblem_,
"boundaryTypes"))
99 return pyProblem_.attr(
"boundaryTypes")(element, scv).
template cast<BoundaryTypes>();
101 return pyProblem_.attr(
"boundaryTypesAtPos")(scv.dofPosition()).
template cast<BoundaryTypes>();
109 DUNE_THROW(Dune::InvalidStateException,
"boundaryTypes(..., scvf) called for box method.");
112 if (pybind11::hasattr(pyProblem_,
"boundaryTypes"))
113 return pyProblem_.attr(
"boundaryTypes")(element, scvf).
template cast<BoundaryTypes>();
115 return pyProblem_.attr(
"boundaryTypesAtPos")(scvf.ipGlobal()).
template cast<BoundaryTypes>();
122 if constexpr (!
isBox)
123 DUNE_THROW(Dune::InvalidStateException,
"dirichlet(scv) called for cell-centered method.");
126 if (pybind11::hasattr(pyProblem_,
"dirichlet"))
127 return pyProblem_.attr(
"dirichlet")(element, scv).
template cast<PrimaryVariables>();
129 return pyProblem_.attr(
"dirichletAtPos")(scv.dofPosition()).
template cast<PrimaryVariables>();
137 DUNE_THROW(Dune::InvalidStateException,
"dirichlet(scvf) called for box method.");
140 if (pybind11::hasattr(pyProblem_,
"dirichlet"))
141 return pyProblem_.attr(
"dirichlet")(element, scvf).
template cast<PrimaryVariables>();
143 return pyProblem_.attr(
"dirichletAtPos")(scvf.ipGlobal()).
template cast<PrimaryVariables>();
147 template<
class ElementVolumeVariables,
class ElementFluxVariablesCache>
150 const ElementVolumeVariables& elemVolVars,
151 const ElementFluxVariablesCache& elemFluxVarsCache,
154 if (pybind11::hasattr(pyProblem_,
"neumann"))
155 return pyProblem_.attr(
"neumann")(element, fvGeometry, scvf).
template cast<NumEqVector>();
157 return pyProblem_.attr(
"neumannAtPos")(scvf.ipGlobal()).
template cast<NumEqVector>();
160 template<
class ElementVolumeVariables>
163 const ElementVolumeVariables& elemVolVars,
166 if (pybind11::hasattr(pyProblem_,
"source"))
167 return pyProblem_.attr(
"source")(element, fvGeometry, scv).
template cast<NumEqVector>();
169 return pyProblem_.attr(
"sourceAtPos")(scv.dofPosition()).
template cast<NumEqVector>();
174 return pyProblem_.attr(
"sourceAtPos")(globalPos).
template cast<NumEqVector>();
177 template<
class ElementVolumeVariables>
180 const ElementVolumeVariables& elemVolVars,
183 if (pybind11::hasattr(pyProblem_,
"scvPointSources"))
184 return pyProblem_.attr(
"scvPointSources")(element, fvGeometry, scv).
template cast<NumEqVector>();
189 template<
class Entity>
190 PrimaryVariables
initial(
const Entity& entity)
const
192 return pyProblem_.attr(
"initial")(entity).
template cast<PrimaryVariables>();
196 {
return enableInternalDirichletConstraints_; }
202 template<
class MatrixBlock,
class VolumeVariables>
206 const VolumeVariables& volVars,
209 if (pybind11::hasattr(pyProblem_,
"addSourceDerivatives"))
210 pyProblem_.attr(
"addSourceDerivatives")(block, element, fvGeometry, scv);
214 {
return *gridGeometry_; }
218 {
return *spatialParams_; }
221 std::shared_ptr<const GridGeometry> gridGeometry_;
222 pybind11::object pyProblem_;
224 std::string paramGroup_;
225 std::shared_ptr<const SpatialParams> spatialParams_;
229template<
class Problem,
class... options>
232 using pybind11::operator
""_a;
233 using namespace Dune::Python;
235 using GridGeometry =
typename Problem::GridGeometry;
236 using SpatialParams =
typename Problem::SpatialParams;
237 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
238 std::shared_ptr<const SpatialParams> spatialParams,
240 return std::make_shared<Problem>(gridGeometry, spatialParams, p);
242 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
244 return std::make_shared<Problem>(gridGeometry, p);
247 cls.def_property_readonly(
"name", &Problem::name);
248 cls.def_property_readonly(
"numEq", [](Problem&){
return Problem::numEq; });
249 cls.def_property_readonly(
"gridGeometry", &Problem::gridGeometry);
251 using GridView =
typename GridGeometry::GridView;
252 using Element =
typename GridView::template Codim<0>::Entity;
253 using Vertex =
typename GridView::template Codim<GridView::dimension>::Entity;
255 if constexpr (Problem::isBox)
257 using SCV =
typename Problem::SubControlVolume;
258 cls.def(
"boundaryTypes", pybind11::overload_cast<const Element&, const SCV&>(&Problem::boundaryTypes, pybind11::const_),
"element"_a,
"scv"_a);
259 cls.def(
"dirichlet", pybind11::overload_cast<const Element&, const SCV&>(&Problem::dirichlet, pybind11::const_),
"element"_a,
"scv"_a);
263 using SCVF =
typename Problem::SubControlVolumeFace;
264 cls.def(
"boundaryTypes", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::boundaryTypes, pybind11::const_),
"element"_a,
"scvf"_a);
265 cls.def(
"dirichlet", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::dirichlet, pybind11::const_),
"element"_a,
"scvf"_a);
268 cls.def(
"neumann", &Problem::template neumann<
decltype(std::ignore),
decltype(std::ignore)>);
269 cls.def(
"source", &Problem::template source<
decltype(std::ignore)>);
270 cls.def(
"sourceAtPos", &Problem::sourceAtPos);
271 cls.def(
"initial", &Problem::template initial<Element>);
272 cls.def(
"initial", &Problem::template initial<Vertex>);
The available discretization methods in Dumux.
constexpr Box box
Definition: method.hh:136
Definition: python/assembly/fvassembler.hh:30
void registerFVProblem(pybind11::handle scope, pybind11::class_< Problem, options... > cls)
Definition: python/common/fvproblem.hh:230
Class to specify the type of a boundary.
Definition: common/boundarytypes.hh:38
A C++ wrapper for a Python problem.
Definition: python/common/fvproblem.hh:48
NumEqVector source(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:161
static constexpr bool enableInternalDirichletConstraints()
Definition: python/common/fvproblem.hh:195
const SpatialParams & spatialParams() const
Return a reference to the underlying spatial parameters.
Definition: python/common/fvproblem.hh:217
typename GridGeometry::GridView::ctype Scalar
Definition: python/common/fvproblem.hh:52
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:91
PrimaryVariables dirichlet(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:119
typename GridGeometry::SubControlVolume SubControlVolume
Definition: python/common/fvproblem.hh:56
FVProblem(std::shared_ptr< const GridGeometry > gridGeometry, pybind11::object pyProblem)
Definition: python/common/fvproblem.hh:80
FVProblem(std::shared_ptr< const GridGeometry > gridGeometry, std::shared_ptr< const SpatialParams > spatialParams, pybind11::object pyProblem)
Definition: python/common/fvproblem.hh:64
static constexpr std::size_t numEq
Definition: python/common/fvproblem.hh:61
const std::string & name() const
Definition: python/common/fvproblem.hh:85
const GridGeometry & gridGeometry() const
Definition: python/common/fvproblem.hh:213
typename Element::Geometry::GlobalCoordinate GlobalPosition
Definition: python/common/fvproblem.hh:58
const std::string & paramGroup() const
Definition: python/common/fvproblem.hh:88
PrimaryVariables dirichlet(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:133
typename GridGeometry::SubControlVolumeFace SubControlVolumeFace
Definition: python/common/fvproblem.hh:57
NumEqVector scvPointSources(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:178
NumEqVector sourceAtPos(const GlobalPosition &globalPos) const
Definition: python/common/fvproblem.hh:172
NumEqVector neumann(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:148
Dune::FieldVector< Scalar, PrimaryVariables::dimension > NumEqVector
Definition: python/common/fvproblem.hh:53
typename GridGeometry::GridView::template Codim< 0 >::Entity Element
Definition: python/common/fvproblem.hh:54
static constexpr bool isBox
Definition: python/common/fvproblem.hh:60
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:105
void addSourceDerivatives(MatrixBlock &block, const Element &element, const FVElementGeometry &fvGeometry, const VolumeVariables &volVars, const SubControlVolume &scv) const
Add source term derivative to the Jacobian.
Definition: python/common/fvproblem.hh:203
GridGeometry_ GridGeometry
Definition: python/common/fvproblem.hh:50
SpatialParams_ SpatialParams
Definition: python/common/fvproblem.hh:51
typename GridGeometry::LocalView FVElementGeometry
Definition: python/common/fvproblem.hh:55
PrimaryVariables initial(const Entity &entity) const
Definition: python/common/fvproblem.hh:190
Class to specify the type of a boundary.
Basic spatial parameters to be used with finite-volume schemes.