From 6cec9027b3c77dec157085feff23f5b200866802 Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Tue, 17 May 2016 16:53:11 -0400 Subject: [PATCH] Add python wrapper for Triangulation. --- source/CMakeLists.txt | 1 + source/python/CMakeLists.txt | 66 ++++++++++++++++++++++++++++++++++++ source/python/__init__.py | 20 +++++++++++ source/python/dummy.py | 29 ++++++++++++++++ source/python/wrappers.cc | 51 ++++++++++++++++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 source/python/CMakeLists.txt create mode 100644 source/python/__init__.py create mode 100644 source/python/dummy.py create mode 100644 source/python/wrappers.cc diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 4d3e8f7b4f..7075734e5f 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -48,6 +48,7 @@ ADD_SUBDIRECTORY(integrators) ADD_SUBDIRECTORY(matrix_free) ADD_SUBDIRECTORY(meshworker) ADD_SUBDIRECTORY(opencascade) +ADD_SUBDIRECTORY(python) FOREACH(build ${DEAL_II_BUILD_TYPES}) STRING(TOLOWER ${build} build_lowercase) diff --git a/source/python/CMakeLists.txt b/source/python/CMakeLists.txt new file mode 100644 index 0000000000..baf781342e --- /dev/null +++ b/source/python/CMakeLists.txt @@ -0,0 +1,66 @@ +## --------------------------------------------------------------------- +## +## 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 at +## the top level of the deal.II distribution. +## +## --------------------------------------------------------------------- + +FIND_LIBRARY(Boost_PYTHON_LIBRARY boost_python PATH ${Boost_LIBRARY_DIRS} NO_DEFAULT_PATH) +INCLUDE(FindPythonLibs) +INCLUDE(FindPythonInterp) +INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) +INCLUDE_DIRECTORIES(SYSTEM ${PYTHON_INCLUDE_DIRS}) + +SET(_src + wrappers.cc + ) + +FILE(GLOB _header + ${CMAKE_SOURCE_DIR}/include/deal.II/python/*.h + ) + + +PYTHON_ADD_MODULE(PyDealII ${_src}) + +SET(PYTHON_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages) + +TARGET_LINK_LIBRARIES(PyDealII deal_II.g) +TARGET_LINK_LIBRARIES(PyDealII ${Boost_PYTHON_LIBRARY}) +TARGET_LINK_LIBRARIES(PyDealII ${PYTHON_LIBRARIES}) + +SET_TARGET_PROPERTIES(PyDealII PROPERTIES + CXX_STANDARD 11 + ) + +INSTALL(TARGETS PyDealII DESTINATION ${PYTHON_INSTALL_PREFIX}/pydealii) + +SET(PYTHON_SOURCES + __init__.py + dummy.py + ) +FOREACH(PYTHON_SOURCE ${PYTHON_SOURCES}) + ADD_CUSTOM_COMMAND( + OUTPUT ${CMAKE_BINARY_DIR}/python/pydealii/${PYTHON_SOURCE} + DEPENDS ${CMAKE_SOURCE_DIR}/source/python/${PYTHON_SOURCE} + COMMAND ${CMAKE_COMMAND} + ARGS -E copy ${CMAKE_SOURCE_DIR}/source/python/${PYTHON_SOURCE} ${CMAKE_BINARY_DIR}/python/pydealii/${PYTHON_SOURCE} + COMMENT "Copying ${PYTHON_SOURCE}" + ) + ADD_CUSTOM_TARGET( + ${PYTHON_SOURCE} ALL + DEPENDS ${CMAKE_BINARY_DIR}/python/pydealii/${PYTHON_SOURCE} + ) +ENDFOREACH() +INSTALL( + DIRECTORY ${CMAKE_BINARY_DIR}/python/pydealii + DESTINATION ${PYTHON_INSTALL_PREFIX} + FILES_MATCHING PATTERN "*.py" + ) diff --git a/source/python/__init__.py b/source/python/__init__.py new file mode 100644 index 0000000000..f6326327e0 --- /dev/null +++ b/source/python/__init__.py @@ -0,0 +1,20 @@ +# --------------------------------------------------------------------- +# +# 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 at +# the top level of the deal.II distribution. +# +# --------------------------------------------------------------------- + +from .PyDealII import * +from .dummy import * + +__all__ = ['PyDealII', 'dummy'] +__doc__ = PyDealII.__doc__ diff --git a/source/python/dummy.py b/source/python/dummy.py new file mode 100644 index 0000000000..4523f75471 --- /dev/null +++ b/source/python/dummy.py @@ -0,0 +1,29 @@ +# --------------------------------------------------------------------- +# +# 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 at +# the top level of the deal.II distribution. +# +# --------------------------------------------------------------------- + +"""This a dummy module that contains a global function and a class.""" + +__all__ = ['Dummy', 'bar'] + +def bar(): + """Print bar.""" + print('bar') + +class Dummy: + """This a dummy class that has only one member function.""" + + def foo(self): + """Print foo.""" + print('foo') diff --git a/source/python/wrappers.cc b/source/python/wrappers.cc new file mode 100644 index 0000000000..271a03096c --- /dev/null +++ b/source/python/wrappers.cc @@ -0,0 +1,51 @@ +// --------------------------------------------------------------------- +// +// 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 at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + +#include +#include +#include + +char const *pydealii_docstring = + " \n" + "PyDealII \n" + "======== \n" + "Some interesting doc. \n" + ; + +unsigned int n_active_cells(const dealii::Triangulation<2> &triangulation) +{ + return triangulation.n_active_cells(); +} + +void generate_cube(dealii::Triangulation<2> &triangulation) +{ + dealii::GridGenerator::hyper_cube(triangulation); +} + +BOOST_PYTHON_MODULE(PyDealII) +{ + boost::python::scope().attr("__doc__") = pydealii_docstring; + + boost::python::docstring_options doc_options; + doc_options.enable_user_defined(); + doc_options.enable_py_signatures(); + doc_options.disable_cpp_signatures(); + + + boost::python::class_> ("Triangulation") + .def("n_active_cells", &n_active_cells) + .def("generate_cube", &generate_cube) + .def("refine_global", &dealii::Triangulation<2>::refine_global); +} -- 2.39.5