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

[python|functions.constant] no header required

parent a1f4a37c
No related branches found
No related tags found
1 merge request!20Update bindings
...@@ -17,10 +17,75 @@ ...@@ -17,10 +17,75 @@
#include <dune/pybindxi/pybind11.h> #include <dune/pybindxi/pybind11.h>
#include <dune/pybindxi/stl.h> #include <dune/pybindxi/stl.h>
#include <dune/xt/common/string.hh>
#include <dune/xt/grid/gridprovider/provider.hh>
#include <dune/xt/grid/type_traits.hh>
#include <dune/xt/functions/constant.hh>
#include <python/dune/xt/common/fvector.hh>
#include <python/dune/xt/common/fmatrix.hh>
#include <python/dune/xt/common/bindings.hh> #include <python/dune/xt/common/bindings.hh>
#include <python/dune/xt/grid/traits.hh>
#include <python/dune/xt/common/exceptions.bindings.hh> #include <python/dune/xt/common/exceptions.bindings.hh>
#include "constant.hh" namespace Dune {
namespace XT {
namespace Functions {
namespace bindings {
template <size_t d, size_t r = 1, size_t rC = 1, class R = double>
class ConstantFunction
{
using type = Functions::ConstantFunction<d, r, rC, R>;
using base_type = Functions::FunctionInterface<d, r, rC, R>;
using bound_type = pybind11::class_<type, base_type>;
public:
static bound_type bind(pybind11::module& m, const std::string& class_id = "constant_function")
{
namespace py = pybind11;
using namespace pybind11::literals;
std::string class_name = class_id + "_" + Common::to_string(d) + "_to_" + Common::to_string(r);
if (rC > 1)
class_name += "x" + Common::to_string(rC);
class_name += "d";
const auto ClassName = Common::to_camel_case(class_name);
bound_type c(m, ClassName.c_str(), XT::Common::to_camel_case(class_id).c_str());
c.def(
py::init<const typename type::RangeReturnType&, const std::string&>(), "value"_a, "name"_a = type::static_id());
if (rC == 1)
m.def(XT::Common::to_camel_case(class_id).c_str(),
[](Grid::bindings::Dimension<d> /*dim_domain*/,
Grid::bindings::Dimension<r> /*dim_range*/,
const typename type::RangeReturnType& value,
const std::string& name) { return type(value, name); },
"dim_domain"_a,
"dim_range"_a,
"value"_a,
"name"_a = type::static_id());
else
m.def(XT::Common::to_camel_case(class_id).c_str(),
[](Grid::bindings::Dimension<d> /*dim_domain*/,
std::pair<Grid::bindings::Dimension<r>, Grid::bindings::Dimension<rC>> /*dim_range*/,
const typename type::RangeReturnType& value,
const std::string& name) { return type(value, name); },
"dim_domain"_a,
"dim_range"_a,
"value"_a,
"name"_a = type::static_id());
return c;
}
}; // class ConstantFunction
} // namespace bindings
} // namespace Functions
} // namespace XT
} // namespace Dune
PYBIND11_MODULE(_functions_constant, m) PYBIND11_MODULE(_functions_constant, m)
...@@ -33,23 +98,21 @@ PYBIND11_MODULE(_functions_constant, m) ...@@ -33,23 +98,21 @@ PYBIND11_MODULE(_functions_constant, m)
py::module::import("dune.xt.functions._functions_function_interface_2d"); py::module::import("dune.xt.functions._functions_function_interface_2d");
py::module::import("dune.xt.functions._functions_function_interface_3d"); py::module::import("dune.xt.functions._functions_function_interface_3d");
using namespace Dune::XT::Functions; Dune::XT::Functions::bindings::ConstantFunction<1, 1, 1>::bind(m);
Dune::XT::Functions::bindings::ConstantFunction<1, 2, 1>::bind(m);
bind_ConstantFunction<1, 1, 1>(m); Dune::XT::Functions::bindings::ConstantFunction<1, 2, 2>::bind(m);
bind_ConstantFunction<1, 2, 1>(m); Dune::XT::Functions::bindings::ConstantFunction<1, 3, 1>::bind(m);
bind_ConstantFunction<1, 2, 2>(m); Dune::XT::Functions::bindings::ConstantFunction<1, 3, 3>::bind(m);
bind_ConstantFunction<1, 3, 1>(m);
bind_ConstantFunction<1, 3, 3>(m); Dune::XT::Functions::bindings::ConstantFunction<2, 1, 1>::bind(m);
Dune::XT::Functions::bindings::ConstantFunction<2, 2, 1>::bind(m);
bind_ConstantFunction<2, 1, 1>(m); Dune::XT::Functions::bindings::ConstantFunction<2, 2, 2>::bind(m);
bind_ConstantFunction<2, 2, 1>(m); Dune::XT::Functions::bindings::ConstantFunction<2, 3, 1>::bind(m);
bind_ConstantFunction<2, 2, 2>(m); Dune::XT::Functions::bindings::ConstantFunction<2, 3, 3>::bind(m);
bind_ConstantFunction<2, 3, 1>(m);
bind_ConstantFunction<2, 3, 3>(m); Dune::XT::Functions::bindings::ConstantFunction<3, 1, 1>::bind(m);
Dune::XT::Functions::bindings::ConstantFunction<3, 2, 1>::bind(m);
bind_ConstantFunction<3, 1, 1>(m); Dune::XT::Functions::bindings::ConstantFunction<3, 2, 2>::bind(m);
bind_ConstantFunction<3, 2, 1>(m); Dune::XT::Functions::bindings::ConstantFunction<3, 3, 1>::bind(m);
bind_ConstantFunction<3, 2, 2>(m); Dune::XT::Functions::bindings::ConstantFunction<3, 3, 3>::bind(m);
bind_ConstantFunction<3, 3, 1>(m);
bind_ConstantFunction<3, 3, 3>(m);
} // PYBIND11_MODULE(...) } // PYBIND11_MODULE(...)
// This file is part of the dune-xt project:
// https://github.com/dune-community/dune-xt
// Copyright 2009-2018 dune-xt 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 - 2019)
// René Fritze (2018)
// Tim Keil (2018)
// Tobias Leibner (2018 - 2019)
#ifndef PYTHON_DUNE_XT_FUNCTIONS_CONSTANT_HH
#define PYTHON_DUNE_XT_FUNCTIONS_CONSTANT_HH
#include <dune/pybindxi/pybind11.h>
#include <dune/xt/common/string.hh>
#include <dune/xt/grid/gridprovider/provider.hh>
#include <dune/xt/grid/type_traits.hh>
#include <python/dune/xt/common/fvector.hh>
#include <python/dune/xt/common/fmatrix.hh>
#include <dune/xt/functions/constant.hh>
namespace Dune {
namespace XT {
namespace Functions {
/**
* \note We would like to drop the d template paremter and use either of
\code
static const size_t d = G::dimension;
static const constexpr size_t d = G::dimension;
\endcode
* but this triggers a bug in gcc-4.9, see e.g.: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59937
*/
template <size_t d, size_t r, size_t rC>
typename pybind11::class_<ConstantFunction<d, r, rC, double>> bind_ConstantFunction(pybind11::module& m)
{
namespace py = pybind11;
using namespace pybind11::literals;
using R = double;
using I = FunctionInterface<d, r, rC, R>;
using C = ConstantFunction<d, r, rC, R>;
py::class_<C, I> c(m,
std::string("ConstantFunction__" + Common::to_string(d) + "d_to_" + Common::to_string(r) + "x"
+ Common::to_string(rC))
.c_str(),
std::string("ConstantFunction__" + Common::to_string(d) + "d_to_" + Common::to_string(r) + "x"
+ Common::to_string(rC))
.c_str());
c.def(py::init<typename C::RangeReturnType, std::string>(), "value"_a, "name"_a = C::static_id());
c.def(py::init<typename C::RangeFieldType, std::string>(), "value"_a, "name"_a = C::static_id());
c.def_property_readonly("static_id", [](const C& /*self*/) { return C::static_id(); });
const std::string make_name = "make_constant_function_" + Common::to_string(r) + "x" + Common::to_string(rC);
m.def(std::string(make_name).c_str(),
[](const typename C::RangeReturnType& value, const std::string& name) { return C(value, name); },
"value"_a,
"name"_a = C::static_id());
// m.def(std::string(make_name).c_str(),
// [](const Grid::GridProvider<G>& /*grid*/, const typename C::RangeFieldType& value, const std::string& name)
// {
// return C(value, name);
// },
// "grid_provider"_a,
// "value"_a,
// "name"_a = C::static_id());
return c;
} // ... bind_ConstantFunction(...)
} // namespace Functions
} // namespace XT
} // namespace Dune
#endif // PYTHON_DUNE_XT_FUNCTIONS_CONSTANT_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