3.3.0
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
38
39namespace Dumux::Python {
40
45template<class GridGeometry_, class PrimaryVariables>
47{
48public:
49 using GridGeometry = GridGeometry_;
50 using Scalar = typename PrimaryVariables::value_type;
51 using NumEqVector = Dune::FieldVector<Scalar, PrimaryVariables::dimension>;
52 using Element = typename GridGeometry::GridView::template Codim<0>::Entity;
53 using FVElementGeometry = typename GridGeometry::LocalView;
54 using SubControlVolume = typename GridGeometry::SubControlVolume;
55 using SubControlVolumeFace = typename GridGeometry::SubControlVolumeFace;
56 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
57
58 static constexpr bool isBox = GridGeometry::discMethod == DiscretizationMethod::box;
59 static constexpr std::size_t numEq = static_cast<std::size_t>(PrimaryVariables::dimension);
61
62 FVProblem(std::shared_ptr<const GridGeometry> gridGeometry, pybind11::object pyProblem)
63 : gridGeometry_(gridGeometry), pyProblem_(pyProblem)
64 {}
65
66 std::string name() const
67 {
68 return pyProblem_.attr("name").template cast<std::string>();
69 }
70
72 const SubControlVolume &scv) const
73 {
74 if constexpr (!isBox)
75 DUNE_THROW(Dune::InvalidStateException, "boundaryTypes(..., scv) called for cell-centered method.");
76 else
77 return pyProblem_.attr("boundaryTypes")(element, scv).template cast<BoundaryTypes>();
78 }
79
81 const SubControlVolumeFace &scvf) const
82 {
83 if constexpr (isBox)
84 DUNE_THROW(Dune::InvalidStateException, "boundaryTypes(..., scvf) called for box method.");
85 else
86 return pyProblem_.attr("boundaryTypes")(element, scvf).template cast<BoundaryTypes>();
87 }
88
89 PrimaryVariables dirichlet(const Element &element,
90 const SubControlVolume &scv) const
91 {
92 if constexpr (!isBox)
93 DUNE_THROW(Dune::InvalidStateException, "dirichlet(scvf) called for cell-centered method.");
94 else
95 return pyProblem_.attr("dirichlet")(element, scv).template cast<PrimaryVariables>();
96 }
97
98 PrimaryVariables dirichlet(const Element &element,
99 const SubControlVolumeFace &scvf) const
100 {
101 if constexpr (isBox)
102 DUNE_THROW(Dune::InvalidStateException, "dirichlet(scvf) called for box method.");
103 else
104 return pyProblem_.attr("dirichlet")(element, scvf).template cast<PrimaryVariables>();
105 }
106
107 template<class ElementVolumeVariables, class ElementFluxVariablesCache>
109 const FVElementGeometry& fvGeometry,
110 const ElementVolumeVariables& elemVolVars,
111 const ElementFluxVariablesCache& elemFluxVarsCache,
112 const SubControlVolumeFace& scvf) const
113 {
114 return pyProblem_.attr("neumann")(element, fvGeometry, scvf).template cast<NumEqVector>();
115 }
116
117 template<class ElementVolumeVariables>
118 NumEqVector source(const Element &element,
119 const FVElementGeometry& fvGeometry,
120 const ElementVolumeVariables& elemVolVars,
121 const SubControlVolume &scv) const
122 {
123 return pyProblem_.attr("source")(element, fvGeometry, scv).template cast<NumEqVector>();
124 }
125
126 NumEqVector sourceAtPos(const GlobalPosition &globalPos) const
127 {
128 return pyProblem_.attr("sourceAtPos")(globalPos).template cast<NumEqVector>();
129 }
130
131 template<class Entity>
132 PrimaryVariables initial(const Entity& entity) const
133 {
134 return pyProblem_.attr("intial")(entity).template cast<PrimaryVariables>();
135 }
136
137 template<class ElementSolution>
139 const SubControlVolume& scv,
140 const ElementSolution& elemSol) const
141 {
142 return pyProblem_.attr("extrusionFactor")(element, scv).template cast<Scalar>();
143 }
144
146 { return *gridGeometry_; }
147
148private:
149 std::shared_ptr<const GridGeometry> gridGeometry_;
150 pybind11::object pyProblem_;
151};
152
153// Python wrapper for the above FVProblem C++ class
154template<class Problem, class... options>
155void registerFVProblem(pybind11::handle scope, pybind11::class_<Problem, options...> cls)
156{
157 using pybind11::operator""_a;
158 using namespace Dune::Python;
159
160 using GridGeometry = typename Problem::GridGeometry;
161 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry, pybind11::object p){
162 return std::make_shared<Problem>(gridGeometry, p);
163 }));
164
165 cls.def_property_readonly("name", &Problem::name);
166 cls.def_property_readonly("numEq", [](Problem&){ return Problem::numEq; });
167
168 using GridView = typename GridGeometry::GridView;
169 using Element = typename GridView::template Codim<0>::Entity;
170 using Vertex = typename GridView::template Codim<GridView::dimension>::Entity;
171
172 if constexpr (Problem::isBox)
173 {
174 using SCV = typename Problem::SubControlVolume;
175 cls.def("boundaryTypes", pybind11::overload_cast<const Element&, const SCV&>(&Problem::boundaryTypes, pybind11::const_), "element"_a, "scv"_a);
176 cls.def("dirichlet", pybind11::overload_cast<const Element&, const SCV&>(&Problem::dirichlet, pybind11::const_), "element"_a, "scv"_a);
177 }
178 else
179 {
180 using SCVF = typename Problem::SubControlVolumeFace;
181 cls.def("boundaryTypes", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::boundaryTypes, pybind11::const_), "element"_a, "scvf"_a);
182 cls.def("dirichlet", pybind11::overload_cast<const Element&, const SCVF&>(&Problem::dirichlet, pybind11::const_), "element"_a, "scvf"_a);
183 }
184
185 cls.def("neumann", &Problem::template neumann<decltype(std::ignore), decltype(std::ignore)>);
186 cls.def("source", &Problem::template source<decltype(std::ignore)>);
187 cls.def("sourceAtPos", &Problem::sourceAtPos);
188 cls.def("initial", &Problem::template initial<Element>);
189 cls.def("initial", &Problem::template initial<Vertex>);
190 cls.def("extrusionFactor", &Problem::template extrusionFactor<decltype(std::ignore)>);
191 cls.def("gridGeometry", &Problem::gridGeometry);
192}
193
194} // end namespace Dumux::Python
195
196#endif
The available discretization methods in Dumux.
Definition: python/common/boundarytypes.hh:33
void registerFVProblem(pybind11::handle scope, pybind11::class_< Problem, options... > cls)
Definition: python/common/fvproblem.hh:155
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:47
typename GridGeometry::GridView::template Codim< 0 >::Entity Element
Definition: python/common/fvproblem.hh:52
static constexpr bool isBox
Definition: python/common/fvproblem.hh:58
PrimaryVariables initial(const Entity &entity) const
Definition: python/common/fvproblem.hh:132
static constexpr std::size_t numEq
Definition: python/common/fvproblem.hh:59
FVProblem(std::shared_ptr< const GridGeometry > gridGeometry, pybind11::object pyProblem)
Definition: python/common/fvproblem.hh:62
Scalar extrusionFactor(const Element &element, const SubControlVolume &scv, const ElementSolution &elemSol) const
Definition: python/common/fvproblem.hh:138
PrimaryVariables dirichlet(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:89
NumEqVector sourceAtPos(const GlobalPosition &globalPos) const
Definition: python/common/fvproblem.hh:126
PrimaryVariables dirichlet(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:98
typename GridGeometry::SubControlVolumeFace SubControlVolumeFace
Definition: python/common/fvproblem.hh:55
typename Element::Geometry::GlobalCoordinate GlobalPosition
Definition: python/common/fvproblem.hh:56
typename GridGeometry::LocalView FVElementGeometry
Definition: python/common/fvproblem.hh:53
const GridGeometry & gridGeometry() const
Definition: python/common/fvproblem.hh:145
NumEqVector source(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:118
Dune::FieldVector< Scalar, PrimaryVariables::dimension > NumEqVector
Definition: python/common/fvproblem.hh:51
GridGeometry_ GridGeometry
Definition: python/common/fvproblem.hh:49
typename PrimaryVariables::value_type Scalar
Definition: python/common/fvproblem.hh:50
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolume &scv) const
Definition: python/common/fvproblem.hh:71
typename GridGeometry::SubControlVolume SubControlVolume
Definition: python/common/fvproblem.hh:54
NumEqVector neumann(const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:108
std::string name() const
Definition: python/common/fvproblem.hh:66
BoundaryTypes boundaryTypes(const Element &element, const SubControlVolumeFace &scvf) const
Definition: python/common/fvproblem.hh:80
Class to specify the type of a boundary.
TODO: docme!