12#ifndef DUMUX_PYTHON_COMMON_FVPROBLEM_HH
13#define DUMUX_PYTHON_COMMON_FVPROBLEM_HH
19#include <dune/common/fvector.hh>
20#include <dune/common/exceptions.hh>
21#include <dune/python/pybind11/pybind11.h>
34template<
class Gr
idGeometry_,
class SpatialParams_,
class PrimaryVariables,
bool enableInternalDirichletConstra
ints_>
40 using Scalar =
typename GridGeometry::GridView::ctype;
41 using NumEqVector = Dune::FieldVector<Scalar, PrimaryVariables::dimension>;
42 using Element =
typename GridGeometry::GridView::template Codim<0>::Entity;
49 static constexpr std::size_t
numEq =
static_cast<std::size_t
>(PrimaryVariables::dimension);
54 pybind11::object pyProblem)
56 , pyProblem_(pyProblem)
57 , name_(
"python_problem")
61 if (pybind11::hasattr(pyProblem_,
"name"))
62 name_ = pyProblem.attr(
"name")().
template cast<std::string>();
64 if (pybind11::hasattr(pyProblem_,
"paramGroup"))
65 paramGroup_ = pyProblem.attr(
"paramGroup")().
template cast<std::string>();
69 pybind11::object pyProblem)
73 const std::string&
name()
const
77 {
return paramGroup_; }
83 DUNE_THROW(Dune::InvalidStateException,
"boundaryTypes(..., scv) called for cell-centered method.");
86 if (pybind11::hasattr(pyProblem_,
"boundaryTypes"))
87 return pyProblem_.attr(
"boundaryTypes")(element, scv).
template cast<BoundaryTypes>();
89 return pyProblem_.attr(
"boundaryTypesAtPos")(scv.dofPosition()).
template cast<BoundaryTypes>();
97 DUNE_THROW(Dune::InvalidStateException,
"boundaryTypes(..., scvf) called for box method.");
100 if (pybind11::hasattr(pyProblem_,
"boundaryTypes"))
101 return pyProblem_.attr(
"boundaryTypes")(element, scvf).
template cast<BoundaryTypes>();
103 return pyProblem_.attr(
"boundaryTypesAtPos")(scvf.ipGlobal()).
template cast<BoundaryTypes>();
110 if constexpr (!
isBox)
111 DUNE_THROW(Dune::InvalidStateException,
"dirichlet(scv) called for cell-centered method.");
114 if (pybind11::hasattr(pyProblem_,
"dirichlet"))
115 return pyProblem_.attr(
"dirichlet")(element, scv).
template cast<PrimaryVariables>();
117 return pyProblem_.attr(
"dirichletAtPos")(scv.dofPosition()).
template cast<PrimaryVariables>();
125 DUNE_THROW(Dune::InvalidStateException,
"dirichlet(scvf) called for box method.");
128 if (pybind11::hasattr(pyProblem_,
"dirichlet"))
129 return pyProblem_.attr(
"dirichlet")(element, scvf).
template cast<PrimaryVariables>();
131 return pyProblem_.attr(
"dirichletAtPos")(scvf.ipGlobal()).
template cast<PrimaryVariables>();
135 template<
class ElementVolumeVariables,
class ElementFluxVariablesCache>
138 const ElementVolumeVariables& elemVolVars,
139 const ElementFluxVariablesCache& elemFluxVarsCache,
142 if (pybind11::hasattr(pyProblem_,
"neumann"))
143 return pyProblem_.attr(
"neumann")(element, fvGeometry, scvf).
template cast<NumEqVector>();
145 return pyProblem_.attr(
"neumannAtPos")(scvf.ipGlobal()).
template cast<NumEqVector>();
148 template<
class ElementVolumeVariables>
151 const ElementVolumeVariables& elemVolVars,
154 if (pybind11::hasattr(pyProblem_,
"source"))
155 return pyProblem_.attr(
"source")(element, fvGeometry, scv).
template cast<NumEqVector>();
157 return pyProblem_.attr(
"sourceAtPos")(scv.dofPosition()).
template cast<NumEqVector>();
162 return pyProblem_.attr(
"sourceAtPos")(globalPos).
template cast<NumEqVector>();
165 template<
class ElementVolumeVariables>
168 const ElementVolumeVariables& elemVolVars,
171 if (pybind11::hasattr(pyProblem_,
"scvPointSources"))
172 return pyProblem_.attr(
"scvPointSources")(element, fvGeometry, scv).
template cast<NumEqVector>();
177 template<
class Entity>
178 PrimaryVariables
initial(
const Entity& entity)
const
180 return pyProblem_.attr(
"initial")(entity).
template cast<PrimaryVariables>();
184 {
return enableInternalDirichletConstraints_; }
190 template<
class MatrixBlock,
class VolumeVariables>
194 const VolumeVariables& volVars,
197 if (pybind11::hasattr(pyProblem_,
"addSourceDerivatives"))
198 pyProblem_.attr(
"addSourceDerivatives")(block, element, fvGeometry, scv);
202 {
return *gridGeometry_; }
206 {
return *spatialParams_; }
209 std::shared_ptr<const GridGeometry> gridGeometry_;
210 pybind11::object pyProblem_;
212 std::string paramGroup_;
213 std::shared_ptr<const SpatialParams> spatialParams_;
217template<
class Problem,
class... options>
220 using pybind11::operator
""_a;
221 using namespace Dune::Python;
223 using GridGeometry =
typename Problem::GridGeometry;
224 using SpatialParams =
typename Problem::SpatialParams;
225 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
226 std::shared_ptr<const SpatialParams> spatialParams,
228 return std::make_shared<Problem>(gridGeometry, spatialParams, p);
230 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
232 return std::make_shared<Problem>(gridGeometry, p);
235 cls.def_property_readonly(
"name", &Problem::name);
236 cls.def_property_readonly(
"numEq", [](Problem&){
return Problem::numEq; });
237 cls.def_property_readonly(
"gridGeometry", &Problem::gridGeometry);
239 using GridView =
typename GridGeometry::GridView;
240 using Element =
typename GridView::template Codim<0>::Entity;
241 using Vertex =
typename GridView::template Codim<GridView::dimension>::Entity;
243 if constexpr (Problem::isBox)
245 using SCV =
typename Problem::SubControlVolume;
246 cls.def(
"boundaryTypes", pybind11::overload_cast<const Element&, const SCV&>(&Problem::boundaryTypes, pybind11::const_),
"element"_a,
"scv"_a);
247 cls.def(
"dirichlet", pybind11::overload_cast<const Element&, const SCV&>(&Problem::dirichlet, pybind11::const_),
"element"_a,
"scv"_a);
251 using SCVF =
typename Problem::SubControlVolumeFace;
252 cls.def(
"boundaryTypes", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::boundaryTypes, pybind11::const_),
"element"_a,
"scvf"_a);
253 cls.def(
"dirichlet", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::dirichlet, pybind11::const_),
"element"_a,
"scvf"_a);
256 cls.def(
"neumann", &Problem::template neumann<
decltype(std::ignore),
decltype(std::ignore)>);
257 cls.def(
"source", &Problem::template source<
decltype(std::ignore)>);
258 cls.def(
"sourceAtPos", &Problem::sourceAtPos);
259 cls.def(
"initial", &Problem::template initial<Element>);
260 cls.def(
"initial", &Problem::template initial<Vertex>);
Class to specify the type of a boundary.
Definition: common/boundarytypes.hh:26
A C++ wrapper for a Python problem.
Definition: python/common/fvproblem.hh:36
NumEqVector source(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:149
static constexpr bool enableInternalDirichletConstraints()
Definition: python/common/fvproblem.hh:183
const SpatialParams & spatialParams() const
Return a reference to the underlying spatial parameters.
Definition: python/common/fvproblem.hh:205
typename GridGeometry::GridView::ctype Scalar
Definition: python/common/fvproblem.hh:40
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:79
PrimaryVariables dirichlet(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:107
typename GridGeometry::SubControlVolume SubControlVolume
Definition: python/common/fvproblem.hh:44
FVProblem(std::shared_ptr< const GridGeometry > gridGeometry, pybind11::object pyProblem)
Definition: python/common/fvproblem.hh:68
FVProblem(std::shared_ptr< const GridGeometry > gridGeometry, std::shared_ptr< const SpatialParams > spatialParams, pybind11::object pyProblem)
Definition: python/common/fvproblem.hh:52
static constexpr std::size_t numEq
Definition: python/common/fvproblem.hh:49
const std::string & name() const
Definition: python/common/fvproblem.hh:73
const GridGeometry & gridGeometry() const
Definition: python/common/fvproblem.hh:201
typename Element::Geometry::GlobalCoordinate GlobalPosition
Definition: python/common/fvproblem.hh:46
const std::string & paramGroup() const
Definition: python/common/fvproblem.hh:76
PrimaryVariables dirichlet(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:121
typename GridGeometry::SubControlVolumeFace SubControlVolumeFace
Definition: python/common/fvproblem.hh:45
NumEqVector scvPointSources(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:166
NumEqVector sourceAtPos(const GlobalPosition &globalPos) const
Definition: python/common/fvproblem.hh:160
NumEqVector neumann(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:136
Dune::FieldVector< Scalar, PrimaryVariables::dimension > NumEqVector
Definition: python/common/fvproblem.hh:41
typename GridGeometry::GridView::template Codim< 0 >::Entity Element
Definition: python/common/fvproblem.hh:42
static constexpr bool isBox
Definition: python/common/fvproblem.hh:48
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:93
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:191
GridGeometry_ GridGeometry
Definition: python/common/fvproblem.hh:38
SpatialParams_ SpatialParams
Definition: python/common/fvproblem.hh:39
typename GridGeometry::LocalView FVElementGeometry
Definition: python/common/fvproblem.hh:43
PrimaryVariables initial(const Entity &entity) const
Definition: python/common/fvproblem.hh:178
Class to specify the type of a boundary.
The available discretization methods in Dumux.
constexpr Box box
Definition: method.hh:147
Definition: python/assembly/fvassembler.hh:18
void registerFVProblem(pybind11::handle scope, pybind11::class_< Problem, options... > cls)
Definition: python/common/fvproblem.hh:218
Basic spatial parameters to be used with finite-volume schemes.