From c7460b14afae1e6071fb36550ca96acf8bb3de14 Mon Sep 17 00:00:00 2001
From: Felix Schindler <felix.schindler@wwu.de>
Date: Fri, 14 Aug 2020 06:50:52 +0200
Subject: [PATCH] [P|functions] add divergence

---
 python/dune/xt/functions/CMakeLists.txt |   1 +
 python/dune/xt/functions/__init__.py    |   1 +
 python/dune/xt/functions/divergence.cc  | 132 ++++++++++++++++++++++++
 3 files changed, 134 insertions(+)
 create mode 100644 python/dune/xt/functions/divergence.cc

diff --git a/python/dune/xt/functions/CMakeLists.txt b/python/dune/xt/functions/CMakeLists.txt
index 15a883a6e..582af31a0 100644
--- a/python/dune/xt/functions/CMakeLists.txt
+++ b/python/dune/xt/functions/CMakeLists.txt
@@ -15,6 +15,7 @@
 dune_pybindxi_add_module(_functions_checkerboard EXCLUDE_FROM_ALL checkerboard.cc)
 dune_pybindxi_add_module(_functions_constant EXCLUDE_FROM_ALL constant.cc)
 dune_pybindxi_add_module(_functions_expression EXCLUDE_FROM_ALL expression.cc)
+dune_pybindxi_add_module(_functions_divergence EXCLUDE_FROM_ALL divergence.cc)
 dune_pybindxi_add_module(_functions_function_as_grid_function EXCLUDE_FROM_ALL function-as-grid-function.cc)
 dune_pybindxi_add_module(_functions_function_interface_1d EXCLUDE_FROM_ALL function-interface-1d.cc)
 dune_pybindxi_add_module(_functions_function_interface_2d EXCLUDE_FROM_ALL function-interface-2d.cc)
diff --git a/python/dune/xt/functions/__init__.py b/python/dune/xt/functions/__init__.py
index e4b3cf114..cf3e15224 100644
--- a/python/dune/xt/functions/__init__.py
+++ b/python/dune/xt/functions/__init__.py
@@ -23,6 +23,7 @@ for mod_name in (
         '_functions_checkerboard',
         '_functions_constant',
         '_functions_expression',
+        '_functions_divergence',
         '_functions_function_as_grid_function',
         '_functions_function_interface_3d',
         '_functions_gridfunction',
diff --git a/python/dune/xt/functions/divergence.cc b/python/dune/xt/functions/divergence.cc
new file mode 100644
index 000000000..175eba19e
--- /dev/null
+++ b/python/dune/xt/functions/divergence.cc
@@ -0,0 +1,132 @@
+// 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 (2019)
+
+#include "config.h"
+
+#include <string>
+#include <vector>
+
+#include <dune/common/parallel/mpihelper.hh>
+
+#include <dune/pybindxi/pybind11.h>
+#include <dune/pybindxi/functional.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/base/derivatives-of-grid-functions.hh>
+
+#include <python/dune/xt/common/parameter.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/grid/traits.hh>
+#include <python/dune/xt/common/exceptions.bindings.hh>
+
+namespace Dune {
+namespace XT {
+namespace Functions {
+namespace bindings {
+
+
+template <class G, class E>
+class DivergenceGridFunction
+{
+  using GP = XT::Grid::GridProvider<G>;
+  static const size_t d = G::dimension;
+  using GF = Functions::GridFunction<E, d>;
+
+public:
+  using type = Functions::DivergenceGridFunction<GF>;
+  using base_type = Functions::GridFunctionInterface<E>;
+  using bound_type = pybind11::class_<type, base_type>;
+
+public:
+  static bound_type bind(pybind11::module& m,
+                         const std::string& grid_id = Grid::bindings::grid_name<G>::value(),
+                         const std::string& layer_id = "",
+                         const std::string& class_id = "divergence_grid_function")
+  {
+    namespace py = pybind11;
+    using namespace pybind11::literals;
+
+    std::string class_name = class_id;
+    class_name += "_" + grid_id;
+    if (!layer_id.empty())
+      class_name += "_" + layer_id;
+    class_name += "_to_1d";
+    const auto ClassName = Common::to_camel_case(class_name);
+    bound_type c(m, ClassName.c_str(), Common::to_camel_case(class_id).c_str());
+    c.def(py::init<GF, const std::string&>(), "grid_function"_a, "name"_a = "DivergenceGridFunction");
+
+    m.def(
+        "divergence",
+        [](const GridFunctionInterface<E, d>& grid_function, const std::string& name) {
+          return new type(grid_function, name);
+        },
+        "grid_function"_a,
+        "name"_a = "DivergenceGridFunction",
+        py::keep_alive<0, 1>());
+    m.def(
+        "divergence",
+        [](GF grid_function, const std::string& name) { return new type(grid_function, name); },
+        "grid_function"_a,
+        "name"_a = "DivergenceGridFunction");
+
+    return c;
+  }
+}; // class GridFunction
+
+
+} // namespace bindings
+} // namespace Functions
+} // namespace XT
+} // namespace Dune
+
+
+template <class GridTypes = Dune::XT::Grid::AvailableGridTypes>
+struct DivergenceGridFunction_for_all_grids
+{
+  using G = typename GridTypes::head_type;
+  using GV = typename G::LeafGridView;
+  using E = Dune::XT::Grid::extract_entity_t<GV>;
+
+  static void bind(pybind11::module& m)
+  {
+    using Dune::XT::Functions::bindings::DivergenceGridFunction;
+    using Dune::XT::Grid::bindings::grid_name;
+
+    DivergenceGridFunction<G, E>::bind(m, grid_name<G>::value());
+
+    DivergenceGridFunction_for_all_grids<typename GridTypes::tail_type>::bind(m);
+  }
+};
+
+template <>
+struct DivergenceGridFunction_for_all_grids<boost::tuples::null_type>
+{
+  static void bind(pybind11::module& /*m*/) {}
+};
+
+
+PYBIND11_MODULE(_functions_divergence, m)
+{
+  namespace py = pybind11;
+
+  py::module::import("dune.xt.common");
+  py::module::import("dune.xt.grid");
+  py::module::import("dune.xt.la");
+  py::module::import("dune.xt.functions._functions_interfaces_grid_function_1d");
+  py::module::import("dune.xt.functions._functions_interfaces_grid_function_2d");
+  py::module::import("dune.xt.functions._functions_interfaces_grid_function_3d");
+  py::module::import("dune.xt.functions._functions_gridfunction");
+
+  DivergenceGridFunction_for_all_grids<>::bind(m);
+} // PYBIND11_MODULE(...)
-- 
GitLab