Skip to content
Snippets Groups Projects
Commit f1005805 authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[python|function-as-gridfunction] add bindings

parent 6b1e15d8
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,7 @@ dune_pybindxi_add_module(_gridfunction_interface_3d EXCLUDE_FROM_ALL gridfunctio
dune_pybindxi_add_module(_checkerboard EXCLUDE_FROM_ALL checkerboard.cc)
dune_pybindxi_add_module(_constant EXCLUDE_FROM_ALL constant.cc)
dune_pybindxi_add_module(_expression EXCLUDE_FROM_ALL expression.cc)
dune_pybindxi_add_module(_function_as_grid_function EXCLUDE_FROM_ALL function-as-grid-function.cc)
dune_pybindxi_add_module(_indicator EXCLUDE_FROM_ALL indicator.cc)
dune_pybindxi_add_module(_spe10 EXCLUDE_FROM_ALL spe10.cc)
......@@ -27,6 +27,7 @@ _modules = (
'_checkerboard',
'_constant',
'_expression',
'_function_as_grid_function',
'_indicator',
'_spe10',
)
......
// This file is part of the dune-xt-functions project:
// https://github.com/dune-community/dune-xt-functions
// Copyright 2009-2018 dune-xt-functions developers and contributors. All rights reserved.
// License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
// or GPL-2.0+ (http://opensource.org/licenses/gpl-license)
// with "runtime exception" (http://www.dune-project.org/license.html)
// Authors:
// Felix Schindler (2016 - 2018)
// René Fritze (2018)
// Tim Keil (2018)
#include "config.h"
#include <string>
#include <vector>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/pybindxi/pybind11.h>
#include <dune/pybindxi/stl.h>
#include <python/dune/xt/common/bindings.hh>
#include <python/dune/xt/common/exceptions.bindings.hh>
#include <python/dune/xt/grid/grids.bindings.hh>
#include "function-as-grid-function.hh"
template <class G>
void addbind_for_Grid(pybind11::module& m)
{
using namespace Dune::XT::Functions::bindings;
const auto grid_id = Dune::XT::Grid::bindings::grid_name<G>::value();
const auto g_dim = G::dimension;
bind_FunctionAsGridFunctionWrapper<G, g_dim, 1, 1>(m, grid_id);
bind_FunctionAsGridFunctionWrapper<G, g_dim, 2, 1>(m, grid_id);
bind_FunctionAsGridFunctionWrapper<G, g_dim, 2, 2>(m, grid_id);
bind_FunctionAsGridFunctionWrapper<G, g_dim, 3, 1>(m, grid_id);
bind_FunctionAsGridFunctionWrapper<G, g_dim, 3, 3>(m, grid_id);
} // ... addbind_for_Grid(...)
template <class Tuple = Dune::XT::Grid::AvailableGridTypes>
void all_grids(pybind11::module& m)
{
Dune::XT::Common::bindings::try_register(m, [](auto& m_) { // different grids but same entity
addbind_for_Grid<typename Tuple::head_type>(m_);
});
all_grids<typename Tuple::tail_type>(m);
} // ... addbind_for_Grid(...)
template <>
void all_grids<boost::tuples::null_type>(pybind11::module&)
{}
PYBIND11_MODULE(_function_as_grid_function, m)
{
namespace py = pybind11;
py::module::import("dune.xt.common");
py::module::import("dune.xt.la");
py::module::import("dune.xt.grid");
py::module::import("dune.xt.functions._gridfunction_interface_1d");
py::module::import("dune.xt.functions._gridfunction_interface_2d");
py::module::import("dune.xt.functions._gridfunction_interface_3d");
all_grids(m);
}
// This file is part of the dune-xt-functions project:
// https://github.com/dune-community/dune-xt-functions
// Copyright 2009-2018 dune-xt-functions developers and contributors. All rights reserved.
// License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
// or GPL-2.0+ (http://opensource.org/licenses/gpl-license)
// with "runtime exception" (http://www.dune-project.org/license.html)
// Authors:
// Felix Schindler (2019)
#ifndef PYTHON_DUNE_XT_FUNCTIONS_FUNCTION_AS_GRID_FUNCTION_HH
#define PYTHON_DUNE_XT_FUNCTIONS_FUNCTION_AS_GRID_FUNCTION_HH
#include <dune/pybindxi/pybind11.h>
#include <dune/xt/functions/base/function-as-grid-function.hh>
#include <dune/xt/functions/interfaces/function.hh>
#include <dune/xt/functions/interfaces/grid-function.hh>
namespace Dune {
namespace XT {
namespace Functions {
namespace bindings {
template <class G, size_t d, size_t r, size_t rC>
typename std::enable_if<
Grid::is_grid<G>::value,
pybind11::class_<FunctionAsGridFunctionWrapper<typename G::template Codim<0>::Entity, r, rC, double>>>::type
bind_FunctionAsGridFunctionWrapper(pybind11::module& m, const std::string& grid_id)
{
namespace py = pybind11;
using namespace pybind11::literals;
using E = typename G::template Codim<0>::Entity;
using D = typename G::ctype;
using R = double;
using I = GridFunctionInterface<E, r, rC, R>;
using C = FunctionAsGridFunctionWrapper<E, r, rC, R>;
const std::string classname = std::string("FunctionAsGridFunctionWrapper__" + grid_id + "_to_" + Common::to_string(r)
+ "x" + Common::to_string(rC));
py::class_<C, I> c(m, classname.c_str(), classname.c_str());
m.def("function_to_grid_function",
[](XT::Functions::FunctionInterface<d, r, rC, R>& func) { return std::make_unique<C>(func); },
py::keep_alive<0, 1>(),
"function"_a);
return c;
} // ... bind_FunctionAsGridFunctionWrapper(...)
} // namespace bindings
} // namespace Functions
} // namespace XT
} // namespace Dune
#endif // PYTHON_DUNE_XT_FUNCTIONS_FUNCTION_AS_GRID_FUNCTION_HH
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