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>
35template<
class Gr
idGeometry_,
class SpatialParams_,
class PrimaryVariables,
bool enableInternalDirichletConstra
ints_>
41 using Scalar =
typename GridGeometry::GridView::ctype;
42 using NumEqVector = Dune::FieldVector<Scalar, PrimaryVariables::dimension>;
43 using Element =
typename GridGeometry::GridView::template Codim<0>::Entity;
50 static constexpr std::size_t
numEq =
static_cast<std::size_t
>(PrimaryVariables::dimension);
55 std::vector<PointSource> >;
59 pybind11::object pyProblem)
61 , pyProblem_(pyProblem)
62 , name_(
"python_problem")
67 if (pybind11::hasattr(pyProblem_,
"name"))
68 name_ = pyProblem.attr(
"name")().
template cast<std::string>();
70 if (pybind11::hasattr(pyProblem_,
"paramGroup"))
71 paramGroup_ = pyProblem.attr(
"paramGroup")().
template cast<std::string>();
75 pybind11::object pyProblem)
79 const std::string&
name()
const
83 {
return paramGroup_; }
89 DUNE_THROW(Dune::InvalidStateException,
"boundaryTypes(..., scv) called for cell-centered method.");
92 if (pybind11::hasattr(pyProblem_,
"boundaryTypes"))
93 return pyProblem_.attr(
"boundaryTypes")(element, scv).
template cast<BoundaryTypes>();
95 return pyProblem_.attr(
"boundaryTypesAtPos")(scv.dofPosition()).
template cast<BoundaryTypes>();
103 DUNE_THROW(Dune::InvalidStateException,
"boundaryTypes(..., scvf) called for box method.");
106 if (pybind11::hasattr(pyProblem_,
"boundaryTypes"))
107 return pyProblem_.attr(
"boundaryTypes")(element, scvf).
template cast<BoundaryTypes>();
109 return pyProblem_.attr(
"boundaryTypesAtPos")(scvf.ipGlobal()).
template cast<BoundaryTypes>();
116 if constexpr (!
isBox)
117 DUNE_THROW(Dune::InvalidStateException,
"dirichlet(scv) called for cell-centered method.");
120 if (pybind11::hasattr(pyProblem_,
"dirichlet"))
121 return pyProblem_.attr(
"dirichlet")(element, scv).
template cast<PrimaryVariables>();
123 return pyProblem_.attr(
"dirichletAtPos")(scv.dofPosition()).
template cast<PrimaryVariables>();
131 DUNE_THROW(Dune::InvalidStateException,
"dirichlet(scvf) called for box method.");
134 if (pybind11::hasattr(pyProblem_,
"dirichlet"))
135 return pyProblem_.attr(
"dirichlet")(element, scvf).
template cast<PrimaryVariables>();
137 return pyProblem_.attr(
"dirichletAtPos")(scvf.ipGlobal()).
template cast<PrimaryVariables>();
141 template<
class ElementVolumeVariables,
class ElementFluxVariablesCache>
144 const ElementVolumeVariables& elemVolVars,
145 const ElementFluxVariablesCache& elemFluxVarsCache,
148 if (pybind11::hasattr(pyProblem_,
"neumann"))
149 return pyProblem_.attr(
"neumann")(element, fvGeometry, scvf).
template cast<NumEqVector>();
151 return pyProblem_.attr(
"neumannAtPos")(scvf.ipGlobal()).
template cast<NumEqVector>();
154 template<
class ElementVolumeVariables>
157 const ElementVolumeVariables& elemVolVars,
160 if (pybind11::hasattr(pyProblem_,
"source"))
161 return pyProblem_.attr(
"source")(element, fvGeometry, scv).
template cast<NumEqVector>();
163 return pyProblem_.attr(
"sourceAtPos")(scv.dofPosition()).
template cast<NumEqVector>();
168 return pyProblem_.attr(
"sourceAtPos")(globalPos).
template cast<NumEqVector>();
171 template<
class ElementVolumeVariables>
174 const ElementVolumeVariables& elemVolVars,
177 if (pybind11::hasattr(pyProblem_,
"scvPointSources"))
178 return pyProblem_.attr(
"scvPointSources")(element, fvGeometry, scv).
template cast<NumEqVector>();
185 return pointSourceMap_;
188 template<
class Entity>
189 PrimaryVariables
initial(
const Entity& entity)
const
191 return pyProblem_.attr(
"initial")(entity).
template cast<PrimaryVariables>();
195 {
return enableInternalDirichletConstraints_; }
201 template<
class MatrixBlock,
class VolumeVariables>
205 const VolumeVariables& volVars,
208 if (pybind11::hasattr(pyProblem_,
"addSourceDerivatives"))
209 pyProblem_.attr(
"addSourceDerivatives")(block, element, fvGeometry, scv);
213 {
return *gridGeometry_; }
217 {
return *spatialParams_; }
220 std::shared_ptr<const GridGeometry> gridGeometry_;
221 pybind11::object pyProblem_;
223 std::string paramGroup_;
224 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>);
Class to specify the type of a boundary.
Definition: common/boundarytypes.hh:26
A point source base class.
Definition: pointsource.hh:39
A C++ wrapper for a Python problem.
Definition: python/common/fvproblem.hh:37
NumEqVector source(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:155
static constexpr bool enableInternalDirichletConstraints()
Definition: python/common/fvproblem.hh:194
const SpatialParams & spatialParams() const
Return a reference to the underlying spatial parameters.
Definition: python/common/fvproblem.hh:216
typename GridGeometry::GridView::ctype Scalar
Definition: python/common/fvproblem.hh:41
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:85
PrimaryVariables dirichlet(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:113
typename GridGeometry::SubControlVolume SubControlVolume
Definition: python/common/fvproblem.hh:45
FVProblem(std::shared_ptr< const GridGeometry > gridGeometry, pybind11::object pyProblem)
Definition: python/common/fvproblem.hh:74
FVProblem(std::shared_ptr< const GridGeometry > gridGeometry, std::shared_ptr< const SpatialParams > spatialParams, pybind11::object pyProblem)
Definition: python/common/fvproblem.hh:57
static constexpr std::size_t numEq
Definition: python/common/fvproblem.hh:50
const std::string & name() const
Definition: python/common/fvproblem.hh:79
const GridGeometry & gridGeometry() const
Definition: python/common/fvproblem.hh:212
typename Element::Geometry::GlobalCoordinate GlobalPosition
Definition: python/common/fvproblem.hh:47
const std::string & paramGroup() const
Definition: python/common/fvproblem.hh:82
PrimaryVariables dirichlet(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:127
std::map< std::pair< std::size_t, std::size_t >, std::vector< PointSource > > PointSourceMap
Definition: python/common/fvproblem.hh:55
typename GridGeometry::SubControlVolumeFace SubControlVolumeFace
Definition: python/common/fvproblem.hh:46
NumEqVector scvPointSources(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:172
NumEqVector sourceAtPos(const GlobalPosition &globalPos) const
Definition: python/common/fvproblem.hh:166
NumEqVector neumann(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:142
Dune::FieldVector< Scalar, PrimaryVariables::dimension > NumEqVector
Definition: python/common/fvproblem.hh:42
const PointSourceMap & pointSourceMap() const
Definition: python/common/fvproblem.hh:183
typename GridGeometry::GridView::template Codim< 0 >::Entity Element
Definition: python/common/fvproblem.hh:43
static constexpr bool isBox
Definition: python/common/fvproblem.hh:49
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:99
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:202
GridGeometry_ GridGeometry
Definition: python/common/fvproblem.hh:39
SpatialParams_ SpatialParams
Definition: python/common/fvproblem.hh:40
typename GridGeometry::LocalView FVElementGeometry
Definition: python/common/fvproblem.hh:44
PrimaryVariables initial(const Entity &entity) const
Definition: python/common/fvproblem.hh:189
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:230
A point source class, i.e. sources located at a single point in space.
Basic spatial parameters to be used with finite-volume schemes.