version 3.8
python/common/fvproblem.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_PYTHON_COMMON_FVPROBLEM_HH
13#define DUMUX_PYTHON_COMMON_FVPROBLEM_HH
14
15#include <string>
16#include <memory>
17#include <tuple>
18
19#include <dune/common/fvector.hh>
20#include <dune/common/exceptions.hh>
21#include <dune/python/pybind11/pybind11.h>
22
27
28namespace Dumux::Python {
29
34template<class GridGeometry_, class SpatialParams_, class PrimaryVariables, bool enableInternalDirichletConstraints_>
36{
37public:
38 using GridGeometry = GridGeometry_;
39 using SpatialParams = SpatialParams_;
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;
43 using FVElementGeometry = typename GridGeometry::LocalView;
44 using SubControlVolume = typename GridGeometry::SubControlVolume;
45 using SubControlVolumeFace = typename GridGeometry::SubControlVolumeFace;
46 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
47
48 static constexpr bool isBox = GridGeometry::discMethod == DiscretizationMethods::box;
49 static constexpr std::size_t numEq = static_cast<std::size_t>(PrimaryVariables::dimension);
51
52 FVProblem(std::shared_ptr<const GridGeometry> gridGeometry,
53 std::shared_ptr<const SpatialParams> spatialParams,
54 pybind11::object pyProblem)
55 : gridGeometry_(gridGeometry)
56 , pyProblem_(pyProblem)
57 , name_("python_problem")
58 , paramGroup_("")
59 , spatialParams_(spatialParams)
60 {
61 if (pybind11::hasattr(pyProblem_, "name"))
62 name_ = pyProblem.attr("name")().template cast<std::string>();
63
64 if (pybind11::hasattr(pyProblem_, "paramGroup"))
65 paramGroup_ = pyProblem.attr("paramGroup")().template cast<std::string>();
66 }
67
68 FVProblem(std::shared_ptr<const GridGeometry> gridGeometry,
69 pybind11::object pyProblem)
70 : FVProblem(gridGeometry, std::make_shared<SpatialParams>(gridGeometry), pyProblem)
71 {}
72
73 const std::string& name() const
74 { return name_; }
75
76 const std::string& paramGroup() const
77 { return paramGroup_; }
78
80 const SubControlVolume &scv) const
81 {
82 if constexpr (!isBox)
83 DUNE_THROW(Dune::InvalidStateException, "boundaryTypes(..., scv) called for cell-centered method.");
84 else
85 {
86 if (pybind11::hasattr(pyProblem_, "boundaryTypes"))
87 return pyProblem_.attr("boundaryTypes")(element, scv).template cast<BoundaryTypes>();
88 else
89 return pyProblem_.attr("boundaryTypesAtPos")(scv.dofPosition()).template cast<BoundaryTypes>();
90 }
91 }
92
94 const SubControlVolumeFace &scvf) const
95 {
96 if constexpr (isBox)
97 DUNE_THROW(Dune::InvalidStateException, "boundaryTypes(..., scvf) called for box method.");
98 else
99 {
100 if (pybind11::hasattr(pyProblem_, "boundaryTypes"))
101 return pyProblem_.attr("boundaryTypes")(element, scvf).template cast<BoundaryTypes>();
102 else
103 return pyProblem_.attr("boundaryTypesAtPos")(scvf.ipGlobal()).template cast<BoundaryTypes>();
104 }
105 }
106
107 PrimaryVariables dirichlet(const Element &element,
108 const SubControlVolume &scv) const
109 {
110 if constexpr (!isBox)
111 DUNE_THROW(Dune::InvalidStateException, "dirichlet(scv) called for cell-centered method.");
112 else
113 {
114 if (pybind11::hasattr(pyProblem_, "dirichlet"))
115 return pyProblem_.attr("dirichlet")(element, scv).template cast<PrimaryVariables>();
116 else
117 return pyProblem_.attr("dirichletAtPos")(scv.dofPosition()).template cast<PrimaryVariables>();
118 }
119 }
120
121 PrimaryVariables dirichlet(const Element &element,
122 const SubControlVolumeFace &scvf) const
123 {
124 if constexpr (isBox)
125 DUNE_THROW(Dune::InvalidStateException, "dirichlet(scvf) called for box method.");
126 else
127 {
128 if (pybind11::hasattr(pyProblem_, "dirichlet"))
129 return pyProblem_.attr("dirichlet")(element, scvf).template cast<PrimaryVariables>();
130 else
131 return pyProblem_.attr("dirichletAtPos")(scvf.ipGlobal()).template cast<PrimaryVariables>();
132 }
133 }
134
135 template<class ElementVolumeVariables, class ElementFluxVariablesCache>
137 const FVElementGeometry& fvGeometry,
138 const ElementVolumeVariables& elemVolVars,
139 const ElementFluxVariablesCache& elemFluxVarsCache,
140 const SubControlVolumeFace& scvf) const
141 {
142 if (pybind11::hasattr(pyProblem_, "neumann"))
143 return pyProblem_.attr("neumann")(element, fvGeometry, scvf).template cast<NumEqVector>();
144 else
145 return pyProblem_.attr("neumannAtPos")(scvf.ipGlobal()).template cast<NumEqVector>();
146 }
147
148 template<class ElementVolumeVariables>
149 NumEqVector source(const Element &element,
150 const FVElementGeometry& fvGeometry,
151 const ElementVolumeVariables& elemVolVars,
152 const SubControlVolume &scv) const
153 {
154 if (pybind11::hasattr(pyProblem_, "source"))
155 return pyProblem_.attr("source")(element, fvGeometry, scv).template cast<NumEqVector>();
156 else
157 return pyProblem_.attr("sourceAtPos")(scv.dofPosition()).template cast<NumEqVector>();
158 }
159
160 NumEqVector sourceAtPos(const GlobalPosition &globalPos) const
161 {
162 return pyProblem_.attr("sourceAtPos")(globalPos).template cast<NumEqVector>();
163 }
164
165 template<class ElementVolumeVariables>
167 const FVElementGeometry& fvGeometry,
168 const ElementVolumeVariables& elemVolVars,
169 const SubControlVolume& scv) const
170 {
171 if (pybind11::hasattr(pyProblem_, "scvPointSources"))
172 return pyProblem_.attr("scvPointSources")(element, fvGeometry, scv).template cast<NumEqVector>();
173 else
174 return NumEqVector(0.0);
175 }
176
177 template<class Entity>
178 PrimaryVariables initial(const Entity& entity) const
179 {
180 return pyProblem_.attr("initial")(entity).template cast<PrimaryVariables>();
181 }
182
184 { return enableInternalDirichletConstraints_; }
185
190 template<class MatrixBlock, class VolumeVariables>
191 void addSourceDerivatives(MatrixBlock& block,
192 const Element& element,
193 const FVElementGeometry& fvGeometry,
194 const VolumeVariables& volVars,
195 const SubControlVolume& scv) const
196 {
197 if (pybind11::hasattr(pyProblem_, "addSourceDerivatives"))
198 pyProblem_.attr("addSourceDerivatives")(block, element, fvGeometry, scv);
199 }
200
202 { return *gridGeometry_; }
203
206 { return *spatialParams_; }
207
208private:
209 std::shared_ptr<const GridGeometry> gridGeometry_;
210 pybind11::object pyProblem_;
211 std::string name_;
212 std::string paramGroup_;
213 std::shared_ptr<const SpatialParams> spatialParams_;
214};
215
216// Python wrapper for the above FVProblem C++ class
217template<class Problem, class... options>
218void registerFVProblem(pybind11::handle scope, pybind11::class_<Problem, options...> cls)
219{
220 using pybind11::operator""_a;
221 using namespace Dune::Python;
222
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,
227 pybind11::object p){
228 return std::make_shared<Problem>(gridGeometry, spatialParams, p);
229 }));
230 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
231 pybind11::object p){
232 return std::make_shared<Problem>(gridGeometry, p);
233 }));
234
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);
238
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;
242
243 if constexpr (Problem::isBox)
244 {
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);
248 }
249 else
250 {
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);
254 }
255
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>);
261}
262
263} // end namespace Dumux::Python
264
265#endif
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
TODO: docme!
Basic spatial parameters to be used with finite-volume schemes.