3.6-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>
32#include <dune/python/pybind11/pybind11.h>
33
36
37namespace Dumux::Python {
38
44template<class GridGeometry_>
46{
47public:
48 using GridGeometry = GridGeometry_;
49 using GridView = typename GridGeometry::GridView;
50 using Scalar = typename GridView::ctype;
51 using Element = typename GridView::template Codim<0>::Entity;
52 using SubControlVolume = typename GridGeometry::SubControlVolume;
53
54 static constexpr bool isBox = GridGeometry::discMethod == DiscretizationMethods::box;
55 static constexpr int dimWorld = GridView::dimensionworld;
56
57 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
58 using GravityVector = Dune::FieldVector<Scalar, dimWorld>;
59
60 FVSpatialParams(std::shared_ptr<const GridGeometry> gridGeometry,
61 pybind11::object pySpatialParameters)
62 : gridGeometry_(gridGeometry)
63 , pySpatialParameters_(pySpatialParameters)
64 , gravity_(0.0)
65 {
66 if (getParam<bool>("Problem.EnableGravity"))
67 gravity_[dimWorld-1] = -9.81;
68 }
69
70 FVSpatialParams(std::shared_ptr<const GridGeometry> gridGeometry)
71 : gridGeometry_(gridGeometry)
72 , pySpatialParameters_{}
73 , gravity_(0.0)
74 {
75 if (getParam<bool>("Problem.EnableGravity"))
76 gravity_[dimWorld-1] = -9.81;
77 }
78
88 template<class ElementSolution>
90 const SubControlVolume& scv,
91 const ElementSolution& elemSol) const
92 {
93 if (pySpatialParameters_)
94 {
95 if (pybind11::hasattr(pySpatialParameters_, "extrusionFactor"))
96 return pySpatialParameters_.attr("extrusionFactor")(element, scv, elemSol).template cast<Scalar>();
97 else if (pybind11::hasattr(pySpatialParameters_, "extrusionFactorAtPos"))
98 return pySpatialParameters_.attr("extrusionFactorAtPos")(scv.dofPosition()).template cast<Scalar>();
99 }
100
101 // default
102 return 1.0;
103 }
104
108 template<class ElementSolution>
109 Scalar temperature(const Element& element,
110 const SubControlVolume& scv,
111 const ElementSolution& elemSol) const
112 {
113 if (pySpatialParameters_)
114 {
115 if (pybind11::hasattr(pySpatialParameters_, "temperature"))
116 return pySpatialParameters_.attr("temperature")(element, scv, elemSol).template cast<Scalar>();
117 else if (pybind11::hasattr(pySpatialParameters_, "temperatureAtPos"))
118 return pySpatialParameters_.attr("temperatureAtPos")(scv.dofPosition()).template cast<Scalar>();
119 }
120
121 // default
122 return 283.15;
123 }
124
135 const GravityVector& gravity(const GlobalPosition& pos) const
136 { return gravity_; }
137
140 { return *gridGeometry_; }
141
142private:
143 std::shared_ptr<const GridGeometry> gridGeometry_;
144 pybind11::object pySpatialParameters_;
145 GravityVector gravity_;
146};
147
148// Python wrapper for the above FVProblem C++ class
149template<class SpatialParams, class... options>
150void registerFVSpatialParams(pybind11::handle scope, pybind11::class_<SpatialParams, options...> cls)
151{
152 using pybind11::operator""_a;
153
154 using GridGeometry = typename SpatialParams::GridGeometry;
155
156 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry, pybind11::object p){
157 return std::make_shared<SpatialParams>(gridGeometry, p);
158 }));
159
160 cls.def("extrusionFactor", &SpatialParams::template extrusionFactor<decltype(std::ignore)>);
161 cls.def("temperature", &SpatialParams::template temperature<decltype(std::ignore)>);
162 cls.def("gravity", &SpatialParams::gravity);
163 cls.def_property_readonly("gridGeometry", &SpatialParams::gridGeometry);
164}
165
166} // namespace Dumux::Python
167
168#endif
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
The available discretization methods in Dumux.
constexpr Box box
Definition: method.hh:136
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:150
The base class for spatial parameters used with finite-volume schemes.
Definition: python/common/fvspatialparams.hh:46
typename GridGeometry::SubControlVolume SubControlVolume
Definition: python/common/fvspatialparams.hh:52
Dune::FieldVector< Scalar, dimWorld > GravityVector
Definition: python/common/fvspatialparams.hh:58
static constexpr bool isBox
Definition: python/common/fvspatialparams.hh:54
const GridGeometry & gridGeometry() const
The finite volume grid geometry.
Definition: python/common/fvspatialparams.hh:139
typename Element::Geometry::GlobalCoordinate GlobalPosition
Definition: python/common/fvspatialparams.hh:57
typename GridView::template Codim< 0 >::Entity Element
Definition: python/common/fvspatialparams.hh:51
GridGeometry_ GridGeometry
Definition: python/common/fvspatialparams.hh:48
FVSpatialParams(std::shared_ptr< const GridGeometry > gridGeometry)
Definition: python/common/fvspatialparams.hh:70
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:89
typename GridView::ctype Scalar
Definition: python/common/fvspatialparams.hh:50
FVSpatialParams(std::shared_ptr< const GridGeometry > gridGeometry, pybind11::object pySpatialParameters)
Definition: python/common/fvspatialparams.hh:60
const GravityVector & gravity(const GlobalPosition &pos) const
Returns the acceleration due to gravity .
Definition: python/common/fvspatialparams.hh:135
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:109
typename GridGeometry::GridView GridView
Definition: python/common/fvspatialparams.hh:49
static constexpr int dimWorld
Definition: python/common/fvspatialparams.hh:55