3.6-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_PYTHON_COMMON_FVPROBLEM_HH
25#define DUMUX_PYTHON_COMMON_FVPROBLEM_HH
26
27#include <string>
28#include <memory>
29#include <tuple>
30
31#include <dune/common/fvector.hh>
32#include <dune/common/exceptions.hh>
33#include <dune/python/pybind11/pybind11.h>
34
39
40namespace Dumux::Python {
41
46template<class GridGeometry_, class SpatialParams_, class PrimaryVariables, bool enableInternalDirichletConstraints_>
48{
49public:
50 using GridGeometry = GridGeometry_;
51 using SpatialParams = SpatialParams_;
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;
55 using FVElementGeometry = typename GridGeometry::LocalView;
56 using SubControlVolume = typename GridGeometry::SubControlVolume;
57 using SubControlVolumeFace = typename GridGeometry::SubControlVolumeFace;
58 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
59
60 static constexpr bool isBox = GridGeometry::discMethod == DiscretizationMethods::box;
61 static constexpr std::size_t numEq = static_cast<std::size_t>(PrimaryVariables::dimension);
63
64 FVProblem(std::shared_ptr<const GridGeometry> gridGeometry,
65 std::shared_ptr<const SpatialParams> spatialParams,
66 pybind11::object pyProblem)
67 : gridGeometry_(gridGeometry)
68 , pyProblem_(pyProblem)
69 , name_("python_problem")
70 , paramGroup_("")
71 , spatialParams_(spatialParams)
72 {
73 if (pybind11::hasattr(pyProblem_, "name"))
74 name_ = pyProblem.attr("name")().template cast<std::string>();
75
76 if (pybind11::hasattr(pyProblem_, "paramGroup"))
77 paramGroup_ = pyProblem.attr("paramGroup")().template cast<std::string>();
78 }
79
80 FVProblem(std::shared_ptr<const GridGeometry> gridGeometry,
81 pybind11::object pyProblem)
82 : FVProblem(gridGeometry, std::make_shared<SpatialParams>(gridGeometry), pyProblem)
83 {}
84
85 const std::string& name() const
86 { return name_; }
87
88 const std::string& paramGroup() const
89 { return paramGroup_; }
90
92 const SubControlVolume &scv) const
93 {
94 if constexpr (!isBox)
95 DUNE_THROW(Dune::InvalidStateException, "boundaryTypes(..., scv) called for cell-centered method.");
96 else
97 {
98 if (pybind11::hasattr(pyProblem_, "boundaryTypes"))
99 return pyProblem_.attr("boundaryTypes")(element, scv).template cast<BoundaryTypes>();
100 else
101 return pyProblem_.attr("boundaryTypesAtPos")(scv.dofPosition()).template cast<BoundaryTypes>();
102 }
103 }
104
106 const SubControlVolumeFace &scvf) const
107 {
108 if constexpr (isBox)
109 DUNE_THROW(Dune::InvalidStateException, "boundaryTypes(..., scvf) called for box method.");
110 else
111 {
112 if (pybind11::hasattr(pyProblem_, "boundaryTypes"))
113 return pyProblem_.attr("boundaryTypes")(element, scvf).template cast<BoundaryTypes>();
114 else
115 return pyProblem_.attr("boundaryTypesAtPos")(scvf.ipGlobal()).template cast<BoundaryTypes>();
116 }
117 }
118
119 PrimaryVariables dirichlet(const Element &element,
120 const SubControlVolume &scv) const
121 {
122 if constexpr (!isBox)
123 DUNE_THROW(Dune::InvalidStateException, "dirichlet(scv) called for cell-centered method.");
124 else
125 {
126 if (pybind11::hasattr(pyProblem_, "dirichlet"))
127 return pyProblem_.attr("dirichlet")(element, scv).template cast<PrimaryVariables>();
128 else
129 return pyProblem_.attr("dirichletAtPos")(scv.dofPosition()).template cast<PrimaryVariables>();
130 }
131 }
132
133 PrimaryVariables dirichlet(const Element &element,
134 const SubControlVolumeFace &scvf) const
135 {
136 if constexpr (isBox)
137 DUNE_THROW(Dune::InvalidStateException, "dirichlet(scvf) called for box method.");
138 else
139 {
140 if (pybind11::hasattr(pyProblem_, "dirichlet"))
141 return pyProblem_.attr("dirichlet")(element, scvf).template cast<PrimaryVariables>();
142 else
143 return pyProblem_.attr("dirichletAtPos")(scvf.ipGlobal()).template cast<PrimaryVariables>();
144 }
145 }
146
147 template<class ElementVolumeVariables, class ElementFluxVariablesCache>
149 const FVElementGeometry& fvGeometry,
150 const ElementVolumeVariables& elemVolVars,
151 const ElementFluxVariablesCache& elemFluxVarsCache,
152 const SubControlVolumeFace& scvf) const
153 {
154 if (pybind11::hasattr(pyProblem_, "neumann"))
155 return pyProblem_.attr("neumann")(element, fvGeometry, scvf).template cast<NumEqVector>();
156 else
157 return pyProblem_.attr("neumannAtPos")(scvf.ipGlobal()).template cast<NumEqVector>();
158 }
159
160 template<class ElementVolumeVariables>
161 NumEqVector source(const Element &element,
162 const FVElementGeometry& fvGeometry,
163 const ElementVolumeVariables& elemVolVars,
164 const SubControlVolume &scv) const
165 {
166 if (pybind11::hasattr(pyProblem_, "source"))
167 return pyProblem_.attr("source")(element, fvGeometry, scv).template cast<NumEqVector>();
168 else
169 return pyProblem_.attr("sourceAtPos")(scv.dofPosition()).template cast<NumEqVector>();
170 }
171
172 NumEqVector sourceAtPos(const GlobalPosition &globalPos) const
173 {
174 return pyProblem_.attr("sourceAtPos")(globalPos).template cast<NumEqVector>();
175 }
176
177 template<class ElementVolumeVariables>
179 const FVElementGeometry& fvGeometry,
180 const ElementVolumeVariables& elemVolVars,
181 const SubControlVolume& scv) const
182 {
183 if (pybind11::hasattr(pyProblem_, "scvPointSources"))
184 return pyProblem_.attr("scvPointSources")(element, fvGeometry, scv).template cast<NumEqVector>();
185 else
186 return NumEqVector(0.0);
187 }
188
189 template<class Entity>
190 PrimaryVariables initial(const Entity& entity) const
191 {
192 return pyProblem_.attr("initial")(entity).template cast<PrimaryVariables>();
193 }
194
196 { return enableInternalDirichletConstraints_; }
197
202 template<class MatrixBlock, class VolumeVariables>
203 void addSourceDerivatives(MatrixBlock& block,
204 const Element& element,
205 const FVElementGeometry& fvGeometry,
206 const VolumeVariables& volVars,
207 const SubControlVolume& scv) const
208 {
209 if (pybind11::hasattr(pyProblem_, "addSourceDerivatives"))
210 pyProblem_.attr("addSourceDerivatives")(block, element, fvGeometry, scv);
211 }
212
214 { return *gridGeometry_; }
215
218 { return *spatialParams_; }
219
220private:
221 std::shared_ptr<const GridGeometry> gridGeometry_;
222 pybind11::object pyProblem_;
223 std::string name_;
224 std::string paramGroup_;
225 std::shared_ptr<const SpatialParams> spatialParams_;
226};
227
228// Python wrapper for the above FVProblem C++ class
229template<class Problem, class... options>
230void registerFVProblem(pybind11::handle scope, pybind11::class_<Problem, options...> cls)
231{
232 using pybind11::operator""_a;
233 using namespace Dune::Python;
234
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,
239 pybind11::object p){
240 return std::make_shared<Problem>(gridGeometry, spatialParams, p);
241 }));
242 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry,
243 pybind11::object p){
244 return std::make_shared<Problem>(gridGeometry, p);
245 }));
246
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);
250
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;
254
255 if constexpr (Problem::isBox)
256 {
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);
260 }
261 else
262 {
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);
266 }
267
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>);
273}
274
275} // end namespace Dumux::Python
276
277#endif
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.
TODO: docme!
Basic spatial parameters to be used with finite-volume schemes.