3.6-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
python/discretization/gridvariables.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_DISCRETIZATION_GRIDVARIABLES_HH
25#define DUMUX_PYTHON_DISCRETIZATION_GRIDVARIABLES_HH
26
27#include <memory>
28#include <dune/istl/bvector.hh>
29#include <dune/python/pybind11/pybind11.h>
30#include <dune/python/common/typeregistry.hh>
32
33namespace Dumux::Python {
34
35namespace Impl {
36
37template<class ElementSolution>
38void registerElementSolution(pybind11::handle scope)
39{
40 using namespace Dune::Python;
41
42 auto [cls, addedToRegistry] = insertClass<ElementSolution>(
43 scope, "ElementSolution",
44 GenerateTypeName(Dune::className<ElementSolution>()),
45 IncludeFiles{"dumux/discretization/elementsolution.hh"}
46 );
47
48 if (addedToRegistry)
49 {
50 using pybind11::operator""_a;
51
52 cls.def("__getitem__", [](const ElementSolution& self, std::size_t i){
53 if (i >= self.size())
54 throw pybind11::index_error();
55 return self[i];
56 });
57
58 cls.def_property_readonly("size", &ElementSolution::size);
59 }
60}
61
62} // end namespace Impl
63
64
65// see python/dumux/discretization/__init__.py for how this is used for JIT compilation
66template <class GV, class... Options>
67void registerGridVariables(pybind11::handle scope, pybind11::class_<GV, Options...> cls)
68{
69 using pybind11::operator""_a;
70
71 using Problem = typename GV::GridVolumeVariables::Problem;
72 using PrimaryVariables = typename GV::GridVolumeVariables::VolumeVariables::PrimaryVariables;
73 using GridGeometry = typename GV::GridGeometry;
74 using Element = typename GridGeometry::GridView::template Codim<0>::Entity;
75 using SolutionVector = Dune::BlockVector<PrimaryVariables>;
76
77 cls.def(pybind11::init([](std::shared_ptr<const Problem> problem,
78 std::shared_ptr<const GridGeometry> gridGeometry){
79 return std::make_shared<GV>(problem, gridGeometry);
80 }));
81
82 cls.def("init", [](GV& self, const SolutionVector& sol) { return self.init(sol); });
83 cls.def("advanceTimeStep", &GV::advanceTimeStep);
84 cls.def_property_readonly("curGridVolVars", [](GV& self) { return self.curGridVolVars(); });
85 cls.def_property_readonly("gridFluxVarsCache", [](GV& self) { return self.gridFluxVarsCache(); });
86 cls.def_property_readonly("prevGridVolVars", [](GV& self) { return self.prevGridVolVars(); });
87 cls.def_property_readonly("gridGeometry", &GV::gridGeometry);
88
89 cls.def("updateAfterGridAdaption", [](GV& self, const SolutionVector& sol){
90 return self.updateAfterGridAdaption(sol);
91 });
92
93 cls.def("resetTimeStep", [](GV& self, const SolutionVector& sol){
94 return self.resetTimeStep(sol);
95 });
96
97 cls.def("update", [](GV& self, const SolutionVector& sol, const bool forceFluxCacheUpdate = false){
98 return self.update(sol, forceFluxCacheUpdate);
99 });
100
101 using ElementSolution = std::decay_t<decltype(elementSolution(std::declval<Element>(),
102 std::declval<SolutionVector>(),
103 std::declval<GridGeometry>()))>;
104 Impl::registerElementSolution<ElementSolution>(scope);
105}
106
107} // namespace Dumux::Python
108
109#endif
Element solution classes and factory functions.
Definition: python/assembly/fvassembler.hh:30
void registerGridVariables(pybind11::handle scope, pybind11::class_< GV, Options... > cls)
Definition: python/discretization/gridvariables.hh:67