3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
python/common/fvspatialparams.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 *****************************************************************************/
25#ifndef DUMUX_PYTHON_COMMON_FVSPATIALPARAMS_HH
26#define DUMUX_PYTHON_COMMON_FVSPATIALPARAMS_HH
27
28#include <memory>
29
30#include <dune/common/fvector.hh>
31#include <dune/common/exceptions.hh>
33#include <dune/python/pybind11/pybind11.h>
34
35namespace Dumux::Python {
36
42template<class GridGeometry_>
44{
45public:
46 using GridGeometry = GridGeometry_;
47 using GridView = typename GridGeometry::GridView;
48 using Scalar = typename GridView::ctype;
49 using Element = typename GridView::template Codim<0>::Entity;
50 using SubControlVolume = typename GridGeometry::SubControlVolume;
51
52 static constexpr bool isBox = GridGeometry::discMethod == DiscretizationMethods::box;
53 static constexpr int dimWorld = GridView::dimensionworld;
54
55 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
56 using GravityVector = Dune::FieldVector<Scalar, dimWorld>;
57
58 FVSpatialParams(std::shared_ptr<const GridGeometry> gridGeometry,
59 pybind11::object pySpatialParameters)
60 : gridGeometry_(gridGeometry)
61 , pySpatialParameters_(pySpatialParameters)
62 , gravity_(0.0)
63 {
64 if (getParam<bool>("Problem.EnableGravity"))
65 gravity_[dimWorld-1] = -9.81;
66 }
67
68 FVSpatialParams(std::shared_ptr<const GridGeometry> gridGeometry)
69 : gridGeometry_(gridGeometry)
70 , pySpatialParameters_{}
71 , gravity_(0.0)
72 {
73 if (getParam<bool>("Problem.EnableGravity"))
74 gravity_[dimWorld-1] = -9.81;
75 }
76
86 template<class ElementSolution>
88 const SubControlVolume& scv,
89 const ElementSolution& elemSol) const
90 {
91 if (pySpatialParameters_)
92 {
93 if (pybind11::hasattr(pySpatialParameters_, "extrusionFactor"))
94 return pySpatialParameters_.attr("extrusionFactor")(element, scv, elemSol).template cast<Scalar>();
95 else if (pybind11::hasattr(pySpatialParameters_, "extrusionFactorAtPos"))
96 return pySpatialParameters_.attr("extrusionFactorAtPos")(scv.dofPosition()).template cast<Scalar>();
97 }
98
99 // default
100 return 1.0;
101 }
102
106 template<class ElementSolution>
107 Scalar temperature(const Element& element,
108 const SubControlVolume& scv,
109 const ElementSolution& elemSol) const
110 {
111 if (pySpatialParameters_)
112 {
113 if (pybind11::hasattr(pySpatialParameters_, "temperature"))
114 return pySpatialParameters_.attr("temperature")(element, scv, elemSol).template cast<Scalar>();
115 else if (pybind11::hasattr(pySpatialParameters_, "temperatureAtPos"))
116 return pySpatialParameters_.attr("temperatureAtPos")(scv.dofPosition()).template cast<Scalar>();
117 }
118
119 // default
120 return 283.15;
121 }
122
133 const GravityVector& gravity(const GlobalPosition& pos) const
134 { return gravity_; }
135
138 { return *gridGeometry_; }
139
140private:
141 std::shared_ptr<const GridGeometry> gridGeometry_;
142 pybind11::object pySpatialParameters_;
143 GravityVector gravity_;
144};
145
146// Python wrapper for the above FVProblem C++ class
147template<class SpatialParams, class... options>
148void registerFVSpatialParams(pybind11::handle scope, pybind11::class_<SpatialParams, options...> cls)
149{
150 using pybind11::operator""_a;
151
152 using GridGeometry = typename SpatialParams::GridGeometry;
153
154 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry, pybind11::object p){
155 return std::make_shared<SpatialParams>(gridGeometry, p);
156 }));
157
158 cls.def("extrusionFactor", &SpatialParams::template extrusionFactor<decltype(std::ignore)>);
159 cls.def("temperature", &SpatialParams::template temperature<decltype(std::ignore)>);
160 cls.def("gravity", &SpatialParams::gravity);
161 cls.def_property_readonly("gridGeometry", &SpatialParams::gridGeometry);
162}
163
164} // namespace Dumux::Python
165
166#endif
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
constexpr Box box
Definition: method.hh:139
std::string temperature() noexcept
I/O name of temperature for equilibrium models.
Definition: name.hh:51
Definition: python/assembly/fvassembler.hh:30
void registerFVSpatialParams(pybind11::handle scope, pybind11::class_< SpatialParams, options... > cls)
Definition: python/common/fvspatialparams.hh:148
The base class for spatial parameters used with finite-volume schemes.
Definition: python/common/fvspatialparams.hh:44
typename GridGeometry::SubControlVolume SubControlVolume
Definition: python/common/fvspatialparams.hh:50
Dune::FieldVector< Scalar, dimWorld > GravityVector
Definition: python/common/fvspatialparams.hh:56
static constexpr bool isBox
Definition: python/common/fvspatialparams.hh:52
const GridGeometry & gridGeometry() const
The finite volume grid geometry.
Definition: python/common/fvspatialparams.hh:137
typename Element::Geometry::GlobalCoordinate GlobalPosition
Definition: python/common/fvspatialparams.hh:55
typename GridView::template Codim< 0 >::Entity Element
Definition: python/common/fvspatialparams.hh:49
GridGeometry_ GridGeometry
Definition: python/common/fvspatialparams.hh:46
FVSpatialParams(std::shared_ptr< const GridGeometry > gridGeometry)
Definition: python/common/fvspatialparams.hh:68
Scalar extrusionFactor(const Element &element, const SubControlVolume &scv, const ElementSolution &elemSol) const
Return how much the domain is extruded at a given sub-control volume.
Definition: python/common/fvspatialparams.hh:87
typename GridView::ctype Scalar
Definition: python/common/fvspatialparams.hh:48
FVSpatialParams(std::shared_ptr< const GridGeometry > gridGeometry, pybind11::object pySpatialParameters)
Definition: python/common/fvspatialparams.hh:58
const GravityVector & gravity(const GlobalPosition &pos) const
Returns the acceleration due to gravity .
Definition: python/common/fvspatialparams.hh:133
Scalar temperature(const Element &element, const SubControlVolume &scv, const ElementSolution &elemSol) const
Return the temperature in the given sub-control volume.
Definition: python/common/fvspatialparams.hh:107
typename GridGeometry::GridView GridView
Definition: python/common/fvspatialparams.hh:47
static constexpr int dimWorld
Definition: python/common/fvspatialparams.hh:53