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);
213 [[deprecated(
"extrusionFactorAtPos() should now be defined in the spatial params. This interface will be removed after 3.5.")]]
217 template<
class ElementSolution>
218 [[deprecated(
"extrusionFactor() should now be defined in the spatial params. This interface will be removed after 3.5.")]]
221 const ElementSolution& elemSol,
222 double defaultValue = 1.0)
const
225 [[deprecated(
"temperatureAtPos() should now be defined in the spatial params. This interface will be removed after 3.5.")]]
229 [[deprecated(
"temperature() should now be defined in the spatial params. This interface will be removed after 3.5.")]]
234 {
return *gridGeometry_; }
238 {
return *spatialParams_; }
241 std::shared_ptr<const GridGeometry> gridGeometry_;
242 pybind11::object pyProblem_;
244 std::string paramGroup_;
245 std::shared_ptr<const SpatialParams> spatialParams_;
249template<
class Problem,
class... options>
252 using pybind11::operator
""_a;
253 using namespace Dune::Python;
255 using GridGeometry =
typename Problem::GridGeometry;
256 using SpatialParams =
typename Problem::SpatialParams;
257 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
258 std::shared_ptr<const SpatialParams> spatialParams,
260 return std::make_shared<Problem>(gridGeometry, spatialParams, p);
262 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
264 return std::make_shared<Problem>(gridGeometry, p);
267 cls.def_property_readonly(
"name", &Problem::name);
268 cls.def_property_readonly(
"numEq", [](Problem&){
return Problem::numEq; });
269 cls.def_property_readonly(
"gridGeometry", &Problem::gridGeometry);
271 using GridView =
typename GridGeometry::GridView;
272 using Element =
typename GridView::template Codim<0>::Entity;
273 using Vertex =
typename GridView::template Codim<GridView::dimension>::Entity;
275 if constexpr (Problem::isBox)
277 using SCV =
typename Problem::SubControlVolume;
278 cls.def(
"boundaryTypes", pybind11::overload_cast<const Element&, const SCV&>(&Problem::boundaryTypes, pybind11::const_),
"element"_a,
"scv"_a);
279 cls.def(
"dirichlet", pybind11::overload_cast<const Element&, const SCV&>(&Problem::dirichlet, pybind11::const_),
"element"_a,
"scv"_a);
283 using SCVF =
typename Problem::SubControlVolumeFace;
284 cls.def(
"boundaryTypes", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::boundaryTypes, pybind11::const_),
"element"_a,
"scvf"_a);
285 cls.def(
"dirichlet", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::dirichlet, pybind11::const_),
"element"_a,
"scvf"_a);
288 cls.def(
"neumann", &Problem::template neumann<
decltype(std::ignore),
decltype(std::ignore)>);
289 cls.def(
"source", &Problem::template source<
decltype(std::ignore)>);
290 cls.def(
"sourceAtPos", &Problem::sourceAtPos);
291 cls.def(
"initial", &Problem::template initial<Element>);
292 cls.def(
"initial", &Problem::template initial<Vertex>);
The available discretization methods in Dumux.
constexpr Box box
Definition: method.hh:139
Definition: python/assembly/fvassembler.hh:30
void registerFVProblem(pybind11::handle scope, pybind11::class_< Problem, options... > cls)
Definition: python/common/fvproblem.hh:250
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:237
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
Scalar temperature(int defaultValue=1) const
Definition: python/common/fvproblem.hh:230
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:233
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
Scalar extrusionFactorAtPos(const GlobalPosition &globalPos, double defaultValue=1.0) const
Definition: python/common/fvproblem.hh:214
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
Scalar extrusionFactor(const Element &element, const SubControlVolume &scv, const ElementSolution &elemSol, double defaultValue=1.0) const
Definition: python/common/fvproblem.hh:219
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
Scalar temperatureAtPos(const GlobalPosition &globalPos, int defaultValue=1) const
Definition: python/common/fvproblem.hh:226
Class to specify the type of a boundary.
Basic spatial parameters to be used with finite-volume schemes.