version 3.8
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// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#ifndef DUMUX_PYTHON_COMMON_FVSPATIALPARAMS_HH
14#define DUMUX_PYTHON_COMMON_FVSPATIALPARAMS_HH
15
16#include <memory>
17
18#include <dune/common/fvector.hh>
19#include <dune/common/exceptions.hh>
20#include <dune/python/pybind11/pybind11.h>
21
24
25namespace Dumux::Python {
26
32template<class GridGeometry_>
34{
35public:
36 using GridGeometry = GridGeometry_;
37 using GridView = typename GridGeometry::GridView;
38 using Scalar = typename GridView::ctype;
39 using Element = typename GridView::template Codim<0>::Entity;
40 using SubControlVolume = typename GridGeometry::SubControlVolume;
41
42 static constexpr bool isBox = GridGeometry::discMethod == DiscretizationMethods::box;
43 static constexpr int dimWorld = GridView::dimensionworld;
44
45 using GlobalPosition = typename Element::Geometry::GlobalCoordinate;
46 using GravityVector = Dune::FieldVector<Scalar, dimWorld>;
47
48 FVSpatialParams(std::shared_ptr<const GridGeometry> gridGeometry,
49 pybind11::object pySpatialParameters)
50 : gridGeometry_(gridGeometry)
51 , pySpatialParameters_(pySpatialParameters)
52 , gravity_(0.0)
53 {
54 if (getParam<bool>("Problem.EnableGravity"))
55 gravity_[dimWorld-1] = -9.81;
56 }
57
58 FVSpatialParams(std::shared_ptr<const GridGeometry> gridGeometry)
59 : gridGeometry_(gridGeometry)
60 , pySpatialParameters_{}
61 , gravity_(0.0)
62 {
63 if (getParam<bool>("Problem.EnableGravity"))
64 gravity_[dimWorld-1] = -9.81;
65 }
66
76 template<class ElementSolution>
78 const SubControlVolume& scv,
79 const ElementSolution& elemSol) const
80 {
81 if (pySpatialParameters_)
82 {
83 if (pybind11::hasattr(pySpatialParameters_, "extrusionFactor"))
84 return pySpatialParameters_.attr("extrusionFactor")(element, scv, elemSol).template cast<Scalar>();
85 else if (pybind11::hasattr(pySpatialParameters_, "extrusionFactorAtPos"))
86 return pySpatialParameters_.attr("extrusionFactorAtPos")(scv.dofPosition()).template cast<Scalar>();
87 }
88
89 // default
90 return 1.0;
91 }
92
96 template<class ElementSolution>
97 Scalar temperature(const Element& element,
98 const SubControlVolume& scv,
99 const ElementSolution& elemSol) const
100 {
101 if (pySpatialParameters_)
102 {
103 if (pybind11::hasattr(pySpatialParameters_, "temperature"))
104 return pySpatialParameters_.attr("temperature")(element, scv, elemSol).template cast<Scalar>();
105 else if (pybind11::hasattr(pySpatialParameters_, "temperatureAtPos"))
106 return pySpatialParameters_.attr("temperatureAtPos")(scv.dofPosition()).template cast<Scalar>();
107 }
108
109 // default
110 return 283.15;
111 }
112
123 const GravityVector& gravity(const GlobalPosition& pos) const
124 { return gravity_; }
125
128 { return *gridGeometry_; }
129
130private:
131 std::shared_ptr<const GridGeometry> gridGeometry_;
132 pybind11::object pySpatialParameters_;
133 GravityVector gravity_;
134};
135
136// Python wrapper for the above FVProblem C++ class
137template<class SpatialParams, class... options>
138void registerFVSpatialParams(pybind11::handle scope, pybind11::class_<SpatialParams, options...> cls)
139{
140 using pybind11::operator""_a;
141
142 using GridGeometry = typename SpatialParams::GridGeometry;
143
144 cls.def(pybind11::init([](std::shared_ptr<const GridGeometry> gridGeometry, pybind11::object p){
145 return std::make_shared<SpatialParams>(gridGeometry, p);
146 }));
147
148 cls.def("extrusionFactor", &SpatialParams::template extrusionFactor<decltype(std::ignore)>);
149 cls.def("temperature", &SpatialParams::template temperature<decltype(std::ignore)>);
150 cls.def("gravity", &SpatialParams::gravity);
151 cls.def_property_readonly("gridGeometry", &SpatialParams::gridGeometry);
152}
153
154} // namespace Dumux::Python
155
156#endif
The base class for spatial parameters used with finite-volume schemes.
Definition: python/common/fvspatialparams.hh:34
typename GridGeometry::SubControlVolume SubControlVolume
Definition: python/common/fvspatialparams.hh:40
Dune::FieldVector< Scalar, dimWorld > GravityVector
Definition: python/common/fvspatialparams.hh:46
static constexpr bool isBox
Definition: python/common/fvspatialparams.hh:42
const GridGeometry & gridGeometry() const
The finite volume grid geometry.
Definition: python/common/fvspatialparams.hh:127
typename Element::Geometry::GlobalCoordinate GlobalPosition
Definition: python/common/fvspatialparams.hh:45
typename GridView::template Codim< 0 >::Entity Element
Definition: python/common/fvspatialparams.hh:39
GridGeometry_ GridGeometry
Definition: python/common/fvspatialparams.hh:36
FVSpatialParams(std::shared_ptr< const GridGeometry > gridGeometry)
Definition: python/common/fvspatialparams.hh:58
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:77
typename GridView::ctype Scalar
Definition: python/common/fvspatialparams.hh:38
FVSpatialParams(std::shared_ptr< const GridGeometry > gridGeometry, pybind11::object pySpatialParameters)
Definition: python/common/fvspatialparams.hh:48
const GravityVector & gravity(const GlobalPosition &pos) const
Returns the acceleration due to gravity .
Definition: python/common/fvspatialparams.hh:123
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:97
typename GridGeometry::GridView GridView
Definition: python/common/fvspatialparams.hh:37
static constexpr int dimWorld
Definition: python/common/fvspatialparams.hh:43
The available discretization methods in Dumux.
constexpr Box box
Definition: method.hh:147
std::string temperature() noexcept
I/O name of temperature for equilibrium models.
Definition: name.hh:39
Definition: python/assembly/fvassembler.hh:18
void registerFVSpatialParams(pybind11::handle scope, pybind11::class_< SpatialParams, options... > cls)
Definition: python/common/fvspatialparams.hh:138
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.