]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add python wrappers for Quadrature 9428/head
authorAlexander Grayver <agrayver@erdw.ethz.ch>
Fri, 24 Jan 2020 10:23:14 +0000 (11:23 +0100)
committerAlexander Grayver <agrayver@erdw.ethz.ch>
Sun, 26 Jan 2020 12:51:50 +0000 (13:51 +0100)
contrib/python-bindings/include/quadrature_wrapper.h [new file with mode: 0644]
contrib/python-bindings/source/CMakeLists.txt
contrib/python-bindings/source/export_quadrature.cc [new file with mode: 0644]
contrib/python-bindings/source/quadrature_wrapper.cc [new file with mode: 0644]
contrib/python-bindings/source/wrappers.cc
contrib/python-bindings/tests/quadrature_wrapper.py [new file with mode: 0644]
doc/news/changes/minor/20200124AlexanderGrayver [new file with mode: 0644]

diff --git a/contrib/python-bindings/include/quadrature_wrapper.h b/contrib/python-bindings/include/quadrature_wrapper.h
new file mode 100644 (file)
index 0000000..2e1a893
--- /dev/null
@@ -0,0 +1,99 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 - 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+#ifndef dealii_quadrature_wrapper_h
+#define dealii_quadrature_wrapper_h
+
+#include <deal.II/base/config.h>
+
+#include <boost/python.hpp>
+
+#include <point_wrapper.h>
+
+DEAL_II_NAMESPACE_OPEN
+
+namespace python
+{
+  class QuadratureWrapper
+  {
+  public:
+    /**
+     * Copy constructor.
+     */
+    QuadratureWrapper(const QuadratureWrapper &other);
+
+    /**
+     * Constructor.
+     */
+    QuadratureWrapper(const int dim);
+
+    /**
+     * Destructor.
+     */
+    ~QuadratureWrapper();
+
+    /**
+     * Create QGauss.
+     */
+    void
+    create_gauss(const unsigned int n);
+
+    /**
+     * Create QGaussLobatto.
+     */
+    void
+    create_gauss_lobatto(const unsigned int n);
+
+    /**
+     * Return list of quadrature points.
+     */
+    boost::python::list
+    get_points() const;
+
+    /**
+     * Return list of quadrature weights.
+     */
+    boost::python::list
+    get_weights() const;
+
+    /**
+     * Return pointer to an underlying Quadrature object
+     */
+    void *
+    get_quadrature() const;
+
+    /**
+     * Return the dimension of the underlying object
+     */
+    int
+    get_dim() const;
+
+  private:
+    /**
+     * Dimension of the underlying Quadrature object.
+     */
+    int dim;
+
+    /**
+     * Pointer that can be casted to the underlying Quadrature object.
+     */
+    void *quadrature_ptr;
+  };
+
+} // namespace python
+
+DEAL_II_NAMESPACE_CLOSE
+
+#endif
index a76c202ee943b595bd5fe2a2a5c78df0f7bfe186..375bcb2dae5b5cb7e941fa9df67e2b570c07c0eb 100644 (file)
@@ -96,12 +96,14 @@ SET(_src
   export_triangulation.cc
   export_mapping.cc
   export_manifold.cc
+  export_quadrature.cc
   cell_accessor_wrapper.cc
   tria_accessor_wrapper.cc
   point_wrapper.cc
   triangulation_wrapper.cc
   mapping_wrapper.cc
   manifold_wrapper.cc
+  quadrature_wrapper.cc
   )
 
 FOREACH(_build ${DEAL_II_BUILD_TYPES})
diff --git a/contrib/python-bindings/source/export_quadrature.cc b/contrib/python-bindings/source/export_quadrature.cc
new file mode 100644 (file)
index 0000000..cfad93f
--- /dev/null
@@ -0,0 +1,66 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 - 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+#include <boost/python.hpp>
+
+#include <quadrature_wrapper.h>
+
+DEAL_II_NAMESPACE_OPEN
+
+namespace python
+{
+  const char create_gauss_docstring[] =
+    "Create Gauss quadrature with n points                     \n"
+    "in each space direction.                                  \n";
+
+
+  const char create_gauss_lobatto_docstring[] =
+    "Create Gauss-Lobatto quadrature with n points             \n"
+    "in each space direction.                                  \n";
+
+
+  const char get_points_docstring[] =
+    "Return the list of quadrature points.                     \n";
+
+
+  const char get_weights_docstring[] =
+    "Return the list of quadrature weights.                    \n";
+
+
+  void
+  export_quadrature()
+  {
+    boost::python::class_<QuadratureWrapper>(
+      "Quadrature", boost::python::init<const int>(boost::python::args("dim")))
+      .def("create_gauss",
+           &QuadratureWrapper::create_gauss,
+           create_gauss_docstring,
+           boost::python::args("self", "n"))
+      .def("create_gauss_lobatto",
+           &QuadratureWrapper::create_gauss_lobatto,
+           create_gauss_lobatto_docstring,
+           boost::python::args("self", "n"))
+      .def("points",
+           &QuadratureWrapper::get_points,
+           get_points_docstring,
+           boost::python::args("self"))
+      .def("weights",
+           &QuadratureWrapper::get_weights,
+           get_weights_docstring,
+           boost::python::args("self"));
+  }
+} // namespace python
+
+DEAL_II_NAMESPACE_CLOSE
diff --git a/contrib/python-bindings/source/quadrature_wrapper.cc b/contrib/python-bindings/source/quadrature_wrapper.cc
new file mode 100644 (file)
index 0000000..9d0fe32
--- /dev/null
@@ -0,0 +1,201 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 - 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+#include <deal.II/base/quadrature_lib.h>
+
+#include <quadrature_wrapper.h>
+
+DEAL_II_NAMESPACE_OPEN
+
+namespace python
+{
+  namespace internal
+  {
+    template <int dim>
+    boost::python::list
+    get_points(const void *quadrature_ptr)
+    {
+      const auto quadrature =
+        static_cast<const Quadrature<dim> *>(quadrature_ptr);
+
+      const auto points = quadrature->get_points();
+
+      boost::python::list points_list;
+      for (const auto &p : points)
+        {
+          boost::python::list python_p;
+          for (int d = 0; d < dim; ++d)
+            python_p.append(p[d]);
+
+          points_list.append(python_p);
+        }
+
+      return points_list;
+    }
+
+    template <int dim>
+    boost::python::list
+    get_weights(const void *quadrature_ptr)
+    {
+      const auto quadrature =
+        static_cast<const Quadrature<dim> *>(quadrature_ptr);
+
+      const auto weights = quadrature->get_weights();
+
+      boost::python::list weights_list;
+      for (const auto &w : weights)
+        weights_list.append(w);
+
+      return weights_list;
+    }
+  } // namespace internal
+
+
+
+  QuadratureWrapper::QuadratureWrapper(const int dim)
+    : dim(dim)
+    , quadrature_ptr(nullptr)
+  {
+    AssertThrow(dim == 2 || dim == 3, ExcMessage("Unsupported dimension."));
+  }
+
+
+
+  QuadratureWrapper::QuadratureWrapper(const QuadratureWrapper &other)
+  {
+    dim = other.dim;
+
+    AssertThrow(other.quadrature_ptr != nullptr,
+                ExcMessage("Underlying quadrature does not exist."));
+
+    if (dim == 2)
+      {
+        const auto quadrature =
+          static_cast<const Quadrature<2> *>(other.quadrature_ptr);
+        quadrature_ptr = new Quadrature<2>(*quadrature);
+      }
+    else if (dim == 3)
+      {
+        const auto quadrature =
+          static_cast<const Quadrature<3> *>(other.quadrature_ptr);
+        quadrature_ptr = new Quadrature<3>(*quadrature);
+      }
+    else
+      AssertThrow(false, ExcMessage("Given dimension is not implemented."));
+  }
+
+
+
+  QuadratureWrapper::~QuadratureWrapper()
+  {
+    if (dim != -1)
+      {
+        if (dim == 2)
+          {
+            // We cannot call delete on a void pointer so cast the void pointer
+            // back first.
+            Quadrature<2> *tmp = static_cast<Quadrature<2> *>(quadrature_ptr);
+            delete tmp;
+          }
+        else if (dim == 3)
+          {
+            Quadrature<3> *tmp = static_cast<Quadrature<3> *>(quadrature_ptr);
+            delete tmp;
+          }
+
+        dim            = -1;
+        quadrature_ptr = nullptr;
+      }
+  }
+
+
+
+  void
+  QuadratureWrapper::create_gauss(const unsigned int n)
+  {
+    if (dim == 2)
+      {
+        quadrature_ptr = new QGauss<2>(n);
+      }
+    else if (dim == 3)
+      {
+        quadrature_ptr = new QGauss<3>(n);
+      }
+    else
+      AssertThrow(false, ExcMessage("Given dimension is not implemented."));
+  }
+
+
+
+  void
+  QuadratureWrapper::create_gauss_lobatto(const unsigned int n)
+  {
+    if (dim == 2)
+      {
+        quadrature_ptr = new QGaussLobatto<2>(n);
+      }
+    else if (dim == 3)
+      {
+        quadrature_ptr = new QGaussLobatto<3>(n);
+      }
+    else
+      AssertThrow(false, ExcMessage("Given dimension is not implemented."));
+  }
+
+
+
+  boost::python::list
+  QuadratureWrapper::get_points() const
+  {
+    if (dim == 2)
+      return internal::get_points<2>(quadrature_ptr);
+    else if (dim == 3)
+      return internal::get_points<3>(quadrature_ptr);
+    else
+      AssertThrow(false, ExcMessage("Given dimension is not implemented."));
+  }
+
+
+
+  boost::python::list
+  QuadratureWrapper::get_weights() const
+  {
+    if (dim == 2)
+      return internal::get_weights<2>(quadrature_ptr);
+    else if (dim == 3)
+      return internal::get_weights<3>(quadrature_ptr);
+    else
+      AssertThrow(false, ExcMessage("Given dimension is not implemented."));
+  }
+
+
+
+  void *
+  QuadratureWrapper::get_quadrature() const
+  {
+    return quadrature_ptr;
+  }
+
+
+
+  int
+  QuadratureWrapper::get_dim() const
+  {
+    return dim;
+  }
+
+} // namespace python
+
+DEAL_II_NAMESPACE_CLOSE
index aa18628b452de32fba0f4bda4cc32c0e06b58c68..5eb2db61ebcef65b111cf9875ccc00698a78fbb8 100644 (file)
@@ -35,6 +35,8 @@ namespace python
   export_mapping();
   void
   export_manifold();
+  void
+  export_quadrature();
 } // namespace python
 
 DEAL_II_NAMESPACE_CLOSE
@@ -69,6 +71,7 @@ BOOST_PYTHON_MODULE(Debug)
   dealii::python::export_triangulation();
   dealii::python::export_mapping();
   dealii::python::export_manifold();
+  dealii::python::export_quadrature();
 }
 
 #else
@@ -88,6 +91,7 @@ BOOST_PYTHON_MODULE(Release)
   dealii::python::export_triangulation();
   dealii::python::export_mapping();
   dealii::python::export_manifold();
+  dealii::python::export_quadrature();
 }
 
 #endif
diff --git a/contrib/python-bindings/tests/quadrature_wrapper.py b/contrib/python-bindings/tests/quadrature_wrapper.py
new file mode 100644 (file)
index 0000000..2a7cb28
--- /dev/null
@@ -0,0 +1,42 @@
+# ---------------------------------------------------------------------
+#
+# Copyright (C) 2016 by the deal.II authors
+#
+# This file is part of the deal.II library.
+#
+# The deal.II library is free software; you can use it, redistribute
+# it, and/or modify it under the terms of the GNU Lesser General
+# Public License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+# The full text of the license can be found in the file LICENSE.md at
+# the top level directory of deal.II.
+#
+# ---------------------------------------------------------------------
+
+import unittest
+from PyDealII.Debug import *
+
+class TestQuadratureWrapper(unittest.TestCase):
+
+    def test_gauss(self):
+        quadrature = Quadrature(dim = 3)
+        quadrature.create_gauss(n = 1);
+        
+        w_sum = 0.
+        for weight in quadrature.weights():
+            w_sum += weight
+            
+        self.assertTrue(abs(1. - w_sum) < 1e-10)
+
+    def test_gauss_lobatto(self):
+        quadrature = Quadrature(dim = 3)
+        quadrature.create_gauss_lobatto(n = 2);
+        
+        w_sum = 0.
+        for weight in quadrature.weights():
+            w_sum += weight
+            
+        self.assertTrue(abs(1. - w_sum) < 1e-10)
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/doc/news/changes/minor/20200124AlexanderGrayver b/doc/news/changes/minor/20200124AlexanderGrayver
new file mode 100644 (file)
index 0000000..3715854
--- /dev/null
@@ -0,0 +1,3 @@
+New: Python bindings for the Quadrature class.
+<br>
+(Alexander Grayver, 2020/01/20)

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.