Skip to content
Snippets Groups Projects
Commit 2e25f728 authored by Ned Coltman's avatar Ned Coltman Committed by Timo Koch
Browse files

[python][material] remove old spatial params files

parent 8e464d6c
No related branches found
No related tags found
1 merge request!3069Spatial parameters for Python bindings
add_subdirectory(components)
add_subdirectory(fluidsystems)
add_subdirectory(spatialparams)
file(GLOB DUMUX_PYTHON_MATERIAL_SPATIALPARAMS_HEADERS *.hh *.inc)
install(FILES ${DUMUX_PYTHON_MATERIAL_SPATIALPARAMS_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dumux/python/material/spatialparams)
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/****************************************************************************
* See the file COPYING for full copying permissions. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
/*!
* \file
* \brief TODO: docme!
*/
#ifndef DUMUX_PYTHON_MATERIAL_SPATIAL_PARAMS_HH
#define DUMUX_PYTHON_MATERIAL_SPATIAL_PARAMS_HH
#include <dune/python/pybind11/pybind11.h>
#include <dune/python/pybind11/stl.h>
#include <dumux/material/spatialparams/fv1p.hh>
namespace Dumux::Python {
template<class GridGeometry, class Scalar, class PT>
class FVSpatialParamsOneP
: public Dumux::FVSpatialParamsOneP<GridGeometry, Scalar, FVSpatialParamsOneP<GridGeometry, Scalar, PT>>
{
using ThisType = FVSpatialParamsOneP<GridGeometry, Scalar, PT>;
using ParentType = Dumux::FVSpatialParamsOneP<GridGeometry, Scalar, ThisType>;
using Element = typename GridGeometry::GridView::template Codim<0>::Entity;
using SubControlVolume = typename GridGeometry::SubControlVolume;
using GlobalPosition = typename GridGeometry::GridView::template Codim<0>::Geometry::GlobalCoordinate;
public:
using PermeabilityType = PT;
FVSpatialParamsOneP(std::shared_ptr<const GridGeometry> gridGeometry,
pybind11::object pySpatialParams)
: ParentType(gridGeometry)
, pySpatialParams_(pySpatialParams)
{}
template<class ElementSolution>
PermeabilityType permeability(const Element& element,
const SubControlVolume& scv,
const ElementSolution& elemSol) const
{
if (pybind11::hasattr(pySpatialParams_, "permeability"))
return pySpatialParams_.attr("permeability")(element, scv, elemSol).template cast<PermeabilityType>();
else
return pySpatialParams_.attr("permeabilityAtPos")(scv.center()).template cast<PermeabilityType>();
}
template<class ElementSolution>
Scalar porosity(const Element& element,
const SubControlVolume& scv,
const ElementSolution& elemSol) const
{
if (pybind11::hasattr(pySpatialParams_, "porosity"))
return pySpatialParams_.attr("porosity")(element, scv, elemSol).template cast<Scalar>();
else
return pySpatialParams_.attr("porosityAtPos")(scv.center()).template cast<Scalar>();
}
private:
pybind11::object pySpatialParams_;
};
template <class SP, class... options>
void registerOnePSpatialParams(pybind11::handle scope,
pybind11::class_<SP, options...> cls)
{
using pybind11::operator""_a;
using GridGeometry = std::decay_t<decltype(std::declval<SP>().gridGeometry())>;
cls.def(pybind11::init([](std::shared_ptr<GridGeometry> gridGeometry, pybind11::object sp){
return std::make_shared<SP>(gridGeometry, sp);
}));
cls.def("gravity", &SP::gravity);
}
} // namespace Dumux::Python
#endif
add_subdirectory(components)
add_subdirectory(fluidsystems)
add_subdirectory(spatialparams)
add_python_targets(material
__init__
......
......@@ -2,4 +2,3 @@
from dumux.material.fluidsystems import FluidSystem
from dumux.material.components import Component
from dumux.material.spatialparams import OnePSpatialParams
add_python_targets(spatialparams
__init__
)
"""Spatial parameter classes"""
import numpy as np
from dune.generator.generator import SimpleGenerator
from dune.common.hashit import hashIt
from dumux.common import Property
from dumux.wrapping import cppWrapperCreator, cppWrapperClassAlias
@cppWrapperCreator
def _createOnePSpatialParamsDecorator(*, gridGeometry, scalar: Property = None):
"""Turn a Python spatial parameter class into an C++/Python hybrid class"""
dim = gridGeometry.gridView.dimensionworld
includes = gridGeometry._includes + [
"dumux/python/material/spatialparams/spatialparams.hh",
"dune/common/fmatrix.hh",
]
if scalar is None:
scalar = Property.fromCppType("double")
scalarType = scalar.cppType
permType = f"Dune::FieldMatrix<{scalarType}, {dim}, {dim}>"
typeName = (
f"Dumux::Python::FVSpatialParamsOneP<{gridGeometry._typeName}, {scalarType}, {permType}>"
)
moduleName = f"spatialparams_{hashIt(typeName)}"
def decorateOnePSpatialParams(cls):
generator = SimpleGenerator("OnePSpatialParams", "Dumux::Python")
module = generator.load(includes, typeName, moduleName, holder="std::shared_ptr")
def maybeConvertScalarToMatrix(permeabilityValue):
if isinstance(permeabilityValue, float):
matrix = np.zeros(shape=(dim, dim))
np.fill_diagonal(matrix, permeabilityValue)
return matrix.tolist()
return permeabilityValue
class Permeability:
"""Permeability decorator to make sure permeability has correct type"""
def __init__(self, permeabilityFunction):
self.permeabilityFunction = permeabilityFunction
def __call__(self, element, scv, elemSol):
result = self.permeabilityFunction(element, scv, elemSol)
return maybeConvertScalarToMatrix(result)
class PermeabilityAtPos:
"""PermeabilityAtPos decorator to make sure permeability has correct type"""
def __init__(self, permeabilityFunction):
self.permeabilityFunction = permeabilityFunction
def __call__(self, globalPos):
result = self.permeabilityFunction(globalPos)
return maybeConvertScalarToMatrix(result)
def createSpatialParams():
spatialParams = cls()
if hasattr(cls, "permeability"):
cls.permeability = Permeability(spatialParams.permeability)
if hasattr(cls, "permeabilityAtPos"):
cls.permeabilityAtPos = PermeabilityAtPos(spatialParams.permeabilityAtPos)
return module.OnePSpatialParams(gridGeometry, spatialParams)
return createSpatialParams
return decorateOnePSpatialParams
@cppWrapperClassAlias(creator=_createOnePSpatialParamsDecorator)
class OnePSpatialParams:
"""Class alias used to decorate Python spatial parameter implementations"""
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment