From 6e4bd483d74cb5da8eab78adddee3e085d982e23 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 16 Oct 2024 10:23:47 +0000 Subject: [PATCH 01/31] #20 Remove mesh from shapes example --- .github/workflows/build-and-test.yml | 4 +- README.md | 2 +- examples/shapes/CMakeLists.txt | 8 +-- examples/shapes/extern/meshgen/CMakeLists.txt | 6 -- examples/shapes/extern/meshgen/MeshGen.cpp | 16 ----- examples/shapes/extern/meshgen/MeshGen.hpp | 24 -------- examples/shapes/src/cpp/mesh/AbstractMesh.cpp | 31 ---------- examples/shapes/src/cpp/mesh/AbstractMesh.hpp | 50 ---------------- examples/shapes/src/cpp/mesh/ConcreteMesh.cpp | 19 ------ examples/shapes/src/cpp/mesh/ConcreteMesh.hpp | 29 --------- examples/shapes/src/cpp/mesh/MeshFactory.cpp | 16 ----- examples/shapes/src/cpp/mesh/MeshFactory.hpp | 27 --------- examples/shapes/src/py/test/test_classes.py | 13 ---- .../wrapper/mesh/AbstractMesh_2_2.cppwg.cpp | 45 -------------- .../wrapper/mesh/AbstractMesh_2_2.cppwg.hpp | 10 ---- .../wrapper/mesh/AbstractMesh_3_3.cppwg.cpp | 45 -------------- .../wrapper/mesh/AbstractMesh_3_3.cppwg.hpp | 10 ---- .../wrapper/mesh/ConcreteMesh_2.cppwg.cpp | 36 ----------- .../wrapper/mesh/ConcreteMesh_2.cppwg.hpp | 10 ---- .../wrapper/mesh/ConcreteMesh_3.cppwg.cpp | 36 ----------- .../wrapper/mesh/ConcreteMesh_3.cppwg.hpp | 10 ---- .../mesh/_pyshapes_mesh.main.cppwg.cpp | 19 ------ examples/shapes/wrapper/package_info.yaml | 6 -- .../wrapper_header_collection.cppwg.hpp | 11 ---- test/__init__.py | 0 {tests => test}/test_wrapper_generation.py | 59 ++++++++++--------- 26 files changed, 34 insertions(+), 508 deletions(-) delete mode 100644 examples/shapes/extern/meshgen/CMakeLists.txt delete mode 100644 examples/shapes/extern/meshgen/MeshGen.cpp delete mode 100644 examples/shapes/extern/meshgen/MeshGen.hpp delete mode 100644 examples/shapes/src/cpp/mesh/AbstractMesh.cpp delete mode 100644 examples/shapes/src/cpp/mesh/AbstractMesh.hpp delete mode 100644 examples/shapes/src/cpp/mesh/ConcreteMesh.cpp delete mode 100644 examples/shapes/src/cpp/mesh/ConcreteMesh.hpp delete mode 100644 examples/shapes/src/cpp/mesh/MeshFactory.cpp delete mode 100644 examples/shapes/src/cpp/mesh/MeshFactory.hpp delete mode 100644 examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.cpp delete mode 100644 examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.hpp delete mode 100644 examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.cpp delete mode 100644 examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.hpp delete mode 100644 examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.cpp delete mode 100644 examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.hpp delete mode 100644 examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.cpp delete mode 100644 examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.hpp delete mode 100644 examples/shapes/wrapper/mesh/_pyshapes_mesh.main.cppwg.cpp create mode 100644 test/__init__.py rename {tests => test}/test_wrapper_generation.py (56%) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 36af0e1..4db2767 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -42,7 +42,7 @@ jobs: run: python -m flake8 - name: Test wrapper generation - run: python -m unittest tests/test_wrapper_generation.py + run: python -m unittest test/test_wrapper_generation.py - name: Generate new wrappers run: | @@ -52,7 +52,7 @@ jobs: cppwg src/cpp \ --wrapper_root wrapper/ \ --package_info wrapper/package_info.yaml \ - --includes src/cpp/*/ extern/*/ \ + --includes src/cpp/*/ \ --std c++17 \ --logfile cppwg.log diff --git a/README.md b/README.md index 1da1836..29fa9d7 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ cd examples/shapes cppwg src/cpp \ --wrapper_root wrapper \ --package_info wrapper/package_info.yaml \ - --includes src/cpp/geometry src/cpp/math_funcs src/cpp/mesh src/cpp/primitives extern/meshgen + --includes src/cpp/geometry src/cpp/math_funcs src/cpp/mesh src/cpp/primitives ``` For the `Rectangle` class, this creates two files in diff --git a/examples/shapes/CMakeLists.txt b/examples/shapes/CMakeLists.txt index 3f72e81..d276e6e 100644 --- a/examples/shapes/CMakeLists.txt +++ b/examples/shapes/CMakeLists.txt @@ -16,9 +16,6 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(pybind11) -# Add external library -add_subdirectory(extern/meshgen meshgen_build) - # Add a shared library for the main C++ source file(GLOB_RECURSE SHAPES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp) add_library(shapes SHARED ${SHAPES_SOURCES}) @@ -28,10 +25,8 @@ target_include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/geometry ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/math_funcs - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/primitives ) -target_link_libraries(shapes PUBLIC meshgen::meshgen) # Copy the Python source and test trees to the build location file( @@ -44,7 +39,7 @@ file( ) # Create a shared library for each wrapper module -foreach(MODULE geometry math_funcs mesh primitives) +foreach(MODULE geometry math_funcs primitives) # Add the autogenerated wrappers to the module target file(GLOB MODULE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/${MODULE}/*.cpp) @@ -63,7 +58,6 @@ foreach(MODULE geometry math_funcs mesh primitives) ${CMAKE_CURRENT_SOURCE_DIR}/wrapper ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/geometry ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/math_funcs - ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/mesh ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/primitives ) diff --git a/examples/shapes/extern/meshgen/CMakeLists.txt b/examples/shapes/extern/meshgen/CMakeLists.txt deleted file mode 100644 index 672572f..0000000 --- a/examples/shapes/extern/meshgen/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.8) -project(meshgen) - -add_library(meshgen MeshGen.cpp MeshGen.hpp) -target_include_directories(meshgen PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -add_library(meshgen::meshgen ALIAS meshgen) diff --git a/examples/shapes/extern/meshgen/MeshGen.cpp b/examples/shapes/extern/meshgen/MeshGen.cpp deleted file mode 100644 index e411abc..0000000 --- a/examples/shapes/extern/meshgen/MeshGen.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "MeshGen.hpp" - -MeshGen::MeshGen() -{ - // Constructor -} - -MeshGen::~MeshGen() -{ - // Destructor -} - -void MeshGen::generateMesh() -{ - // Generate a mesh -} diff --git a/examples/shapes/extern/meshgen/MeshGen.hpp b/examples/shapes/extern/meshgen/MeshGen.hpp deleted file mode 100644 index 0e71bf0..0000000 --- a/examples/shapes/extern/meshgen/MeshGen.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _MESHGEN_HPP -#define _MESHGEN_HPP - -class MeshGen -{ - -public: - /** - * Default Constructor - */ - MeshGen(); - - /** - * Destructor - */ - ~MeshGen(); - - /** - * Generate a mesh - */ - void generateMesh(); -}; - -#endif // _MESHGEN_HPP diff --git a/examples/shapes/src/cpp/mesh/AbstractMesh.cpp b/examples/shapes/src/cpp/mesh/AbstractMesh.cpp deleted file mode 100644 index 7f6ec65..0000000 --- a/examples/shapes/src/cpp/mesh/AbstractMesh.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "AbstractMesh.hpp" - -template -AbstractMesh::AbstractMesh() : mIndex(0) -{ -} - -template -AbstractMesh::~AbstractMesh() -{ -} - -template -unsigned AbstractMesh::GetIndex() const -{ - return mIndex; -} - -template -void AbstractMesh::SetIndex(unsigned index) -{ - mIndex = index; -} - -template -void AbstractMesh::AddVertex(Point vertex) -{ -} - -template class AbstractMesh<2, 2>; -template class AbstractMesh<3, 3>; diff --git a/examples/shapes/src/cpp/mesh/AbstractMesh.hpp b/examples/shapes/src/cpp/mesh/AbstractMesh.hpp deleted file mode 100644 index 4ccbae6..0000000 --- a/examples/shapes/src/cpp/mesh/AbstractMesh.hpp +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef _ABSTRACT_MESH_HPP -#define _ABSTRACT_MESH_HPP - -#include "Point.hpp" - -/** - * A mesh in SPACE_DIM space with ELEMENT_DIM dimensional elements - */ -template -class AbstractMesh -{ -private: - /** - * AbstractMesh index - */ - unsigned mIndex; - -public: - /** - * Default Constructor - */ - AbstractMesh(); - - /** - * Destructor - */ - ~AbstractMesh(); - - /** - * Return the index - */ - unsigned GetIndex() const; - - /** - * Set the index - */ - void SetIndex(unsigned index); - - /** - * Add a vertex to the mesh - */ - void AddVertex(Point vertex); - - /** - * Scale the mesh by a factor - */ - virtual void Scale(const double factor) = 0; -}; - -#endif // _ABSTRACT_MESH_HPP diff --git a/examples/shapes/src/cpp/mesh/ConcreteMesh.cpp b/examples/shapes/src/cpp/mesh/ConcreteMesh.cpp deleted file mode 100644 index 65cee4f..0000000 --- a/examples/shapes/src/cpp/mesh/ConcreteMesh.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "AbstractMesh.hpp" -#include "ConcreteMesh.hpp" - -template -ConcreteMesh::ConcreteMesh() : AbstractMesh() -{ -} - -template -ConcreteMesh::~ConcreteMesh() -{ -} -template -void ConcreteMesh::Scale(const double factor){ - // Scale the mesh -}; - -template class ConcreteMesh<2>; -template class ConcreteMesh<3>; diff --git a/examples/shapes/src/cpp/mesh/ConcreteMesh.hpp b/examples/shapes/src/cpp/mesh/ConcreteMesh.hpp deleted file mode 100644 index 84d917e..0000000 --- a/examples/shapes/src/cpp/mesh/ConcreteMesh.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef _CONCRETE_MESH_HPP -#define _CONCRETE_MESH_HPP - -#include "AbstractMesh.hpp" - -/** - * A concrete mesh implementation - */ -template -class ConcreteMesh : public AbstractMesh -{ -public: - /** - * Default Constructor - */ - ConcreteMesh(); - - /** - * Destructor - */ - ~ConcreteMesh(); - - /** - * Scale the mesh by a factor - */ - void Scale(const double factor) override; -}; - -#endif // _CONCRETE_MESH_HPP diff --git a/examples/shapes/src/cpp/mesh/MeshFactory.cpp b/examples/shapes/src/cpp/mesh/MeshFactory.cpp deleted file mode 100644 index cc95b03..0000000 --- a/examples/shapes/src/cpp/mesh/MeshFactory.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "ConcreteMesh.hpp" - -#include "MeshFactory.hpp" - -template -MeshFactory::MeshFactory(): mMeshGen() -{ -} - -template -MeshFactory::~MeshFactory() -{ -} - -template class MeshFactory >; -template class MeshFactory >; diff --git a/examples/shapes/src/cpp/mesh/MeshFactory.hpp b/examples/shapes/src/cpp/mesh/MeshFactory.hpp deleted file mode 100644 index 1f414cd..0000000 --- a/examples/shapes/src/cpp/mesh/MeshFactory.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef _MESH_FACTORY_HPP -#define _MESH_FACTORY_HPP - -#include "MeshGen.hpp" - -/** - * A concrete mesh implementation - */ -template -class MeshFactory -{ -private: - MeshGen mMeshGen; - -public: - /** - * Default Constructor - */ - MeshFactory(); - - /** - * Destructor - */ - ~MeshFactory(); -}; - -#endif // _MESH_FACTORY_HPP diff --git a/examples/shapes/src/py/test/test_classes.py b/examples/shapes/src/py/test/test_classes.py index 23ebe0f..d723b25 100644 --- a/examples/shapes/src/py/test/test_classes.py +++ b/examples/shapes/src/py/test/test_classes.py @@ -1,7 +1,6 @@ import unittest import pyshapes.geometry -import pyshapes.mesh import pyshapes.primitives @@ -36,14 +35,6 @@ def testGeometry(self): cuboid = pyshapes.primitives.Cuboid(5.0, 10.0, 20.0) self.assertTrue(len(cuboid.rGetVertices()) == 8) - def testMesh(self): - - cmesh = pyshapes.mesh.ConcreteMesh_2() - self.assertTrue(cmesh.GetIndex() == 0) - - cmesh.SetIndex(1) - self.assertTrue(cmesh.GetIndex() == 1) - def testSyntax(self): self.assertEqual(pyshapes.geometry.Point[2], pyshapes.geometry.Point_2) @@ -51,10 +42,6 @@ def testSyntax(self): point = pyshapes.geometry.Point[3](0.0, 1.0, 2.0) self.assertTrue(point.GetLocation() == [0.0, 1.0, 2.0]) - self.assertEqual( - pyshapes.mesh.AbstractMesh[2, 2], pyshapes.mesh.AbstractMesh_2_2 - ) - if __name__ == "__main__": unittest.main() diff --git a/examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.cpp b/examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.cpp deleted file mode 100644 index b2a60a7..0000000 --- a/examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#include -#include -#include "wrapper_header_collection.cppwg.hpp" - -#include "AbstractMesh_2_2.cppwg.hpp" - -namespace py = pybind11; -typedef AbstractMesh<2, 2> AbstractMesh_2_2; -PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); - -class AbstractMesh_2_2_Overrides : public AbstractMesh_2_2 -{ -public: - using AbstractMesh_2_2::AbstractMesh; - void Scale(double const factor) override - { - PYBIND11_OVERRIDE_PURE( - void, - AbstractMesh_2_2, - Scale, - factor); - } -}; - -void register_AbstractMesh_2_2_class(py::module &m) -{ - py::class_>(m, "AbstractMesh_2_2") - .def(py::init<>()) - .def("GetIndex", - (unsigned int(AbstractMesh_2_2::*)() const) &AbstractMesh_2_2::GetIndex, - " ") - .def("SetIndex", - (void(AbstractMesh_2_2::*)(unsigned int)) &AbstractMesh_2_2::SetIndex, - " ", py::arg("index")) - .def("AddVertex", - (void(AbstractMesh_2_2::*)(::Point<2>)) &AbstractMesh_2_2::AddVertex, - " ", py::arg("vertex")) - .def("Scale", - (void(AbstractMesh_2_2::*)(double const)) &AbstractMesh_2_2::Scale, - " ", py::arg("factor")) - ; -} diff --git a/examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.hpp b/examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.hpp deleted file mode 100644 index e565615..0000000 --- a/examples/shapes/wrapper/mesh/AbstractMesh_2_2.cppwg.hpp +++ /dev/null @@ -1,10 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#ifndef AbstractMesh_2_2_hpp__cppwg_wrapper -#define AbstractMesh_2_2_hpp__cppwg_wrapper - -#include - -void register_AbstractMesh_2_2_class(pybind11::module &m); -#endif // AbstractMesh_2_2_hpp__cppwg_wrapper diff --git a/examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.cpp b/examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.cpp deleted file mode 100644 index f5b2b97..0000000 --- a/examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#include -#include -#include "wrapper_header_collection.cppwg.hpp" - -#include "AbstractMesh_3_3.cppwg.hpp" - -namespace py = pybind11; -typedef AbstractMesh<3, 3> AbstractMesh_3_3; -PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); - -class AbstractMesh_3_3_Overrides : public AbstractMesh_3_3 -{ -public: - using AbstractMesh_3_3::AbstractMesh; - void Scale(double const factor) override - { - PYBIND11_OVERRIDE_PURE( - void, - AbstractMesh_3_3, - Scale, - factor); - } -}; - -void register_AbstractMesh_3_3_class(py::module &m) -{ - py::class_>(m, "AbstractMesh_3_3") - .def(py::init<>()) - .def("GetIndex", - (unsigned int(AbstractMesh_3_3::*)() const) &AbstractMesh_3_3::GetIndex, - " ") - .def("SetIndex", - (void(AbstractMesh_3_3::*)(unsigned int)) &AbstractMesh_3_3::SetIndex, - " ", py::arg("index")) - .def("AddVertex", - (void(AbstractMesh_3_3::*)(::Point<3>)) &AbstractMesh_3_3::AddVertex, - " ", py::arg("vertex")) - .def("Scale", - (void(AbstractMesh_3_3::*)(double const)) &AbstractMesh_3_3::Scale, - " ", py::arg("factor")) - ; -} diff --git a/examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.hpp b/examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.hpp deleted file mode 100644 index f122f63..0000000 --- a/examples/shapes/wrapper/mesh/AbstractMesh_3_3.cppwg.hpp +++ /dev/null @@ -1,10 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#ifndef AbstractMesh_3_3_hpp__cppwg_wrapper -#define AbstractMesh_3_3_hpp__cppwg_wrapper - -#include - -void register_AbstractMesh_3_3_class(pybind11::module &m); -#endif // AbstractMesh_3_3_hpp__cppwg_wrapper diff --git a/examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.cpp b/examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.cpp deleted file mode 100644 index 8750693..0000000 --- a/examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#include -#include -#include "wrapper_header_collection.cppwg.hpp" - -#include "ConcreteMesh_2.cppwg.hpp" - -namespace py = pybind11; -typedef ConcreteMesh<2> ConcreteMesh_2; -PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); - -class ConcreteMesh_2_Overrides : public ConcreteMesh_2 -{ -public: - using ConcreteMesh_2::ConcreteMesh; - void Scale(double const factor) override - { - PYBIND11_OVERRIDE( - void, - ConcreteMesh_2, - Scale, - factor); - } -}; - -void register_ConcreteMesh_2_class(py::module &m) -{ - py::class_, AbstractMesh<2>>(m, "ConcreteMesh_2") - .def(py::init<>()) - .def("Scale", - (void(ConcreteMesh_2::*)(double const)) &ConcreteMesh_2::Scale, - " ", py::arg("factor")) - ; -} diff --git a/examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.hpp b/examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.hpp deleted file mode 100644 index fcf783a..0000000 --- a/examples/shapes/wrapper/mesh/ConcreteMesh_2.cppwg.hpp +++ /dev/null @@ -1,10 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#ifndef ConcreteMesh_2_hpp__cppwg_wrapper -#define ConcreteMesh_2_hpp__cppwg_wrapper - -#include - -void register_ConcreteMesh_2_class(pybind11::module &m); -#endif // ConcreteMesh_2_hpp__cppwg_wrapper diff --git a/examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.cpp b/examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.cpp deleted file mode 100644 index 3c96c97..0000000 --- a/examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#include -#include -#include "wrapper_header_collection.cppwg.hpp" - -#include "ConcreteMesh_3.cppwg.hpp" - -namespace py = pybind11; -typedef ConcreteMesh<3> ConcreteMesh_3; -PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); - -class ConcreteMesh_3_Overrides : public ConcreteMesh_3 -{ -public: - using ConcreteMesh_3::ConcreteMesh; - void Scale(double const factor) override - { - PYBIND11_OVERRIDE( - void, - ConcreteMesh_3, - Scale, - factor); - } -}; - -void register_ConcreteMesh_3_class(py::module &m) -{ - py::class_, AbstractMesh<3>>(m, "ConcreteMesh_3") - .def(py::init<>()) - .def("Scale", - (void(ConcreteMesh_3::*)(double const)) &ConcreteMesh_3::Scale, - " ", py::arg("factor")) - ; -} diff --git a/examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.hpp b/examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.hpp deleted file mode 100644 index 900cb8d..0000000 --- a/examples/shapes/wrapper/mesh/ConcreteMesh_3.cppwg.hpp +++ /dev/null @@ -1,10 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#ifndef ConcreteMesh_3_hpp__cppwg_wrapper -#define ConcreteMesh_3_hpp__cppwg_wrapper - -#include - -void register_ConcreteMesh_3_class(pybind11::module &m); -#endif // ConcreteMesh_3_hpp__cppwg_wrapper diff --git a/examples/shapes/wrapper/mesh/_pyshapes_mesh.main.cppwg.cpp b/examples/shapes/wrapper/mesh/_pyshapes_mesh.main.cppwg.cpp deleted file mode 100644 index 4361e15..0000000 --- a/examples/shapes/wrapper/mesh/_pyshapes_mesh.main.cppwg.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// This file is automatically generated by cppwg. -// Do not modify this file directly. - -#include -#include "wrapper_header_collection.cppwg.hpp" -#include "AbstractMesh_2_2.cppwg.hpp" -#include "AbstractMesh_3_3.cppwg.hpp" -#include "ConcreteMesh_2.cppwg.hpp" -#include "ConcreteMesh_3.cppwg.hpp" - -namespace py = pybind11; - -PYBIND11_MODULE(_pyshapes_mesh, m) -{ - register_AbstractMesh_2_2_class(m); - register_AbstractMesh_3_3_class(m); - register_ConcreteMesh_2_class(m); - register_ConcreteMesh_3_class(m); -} diff --git a/examples/shapes/wrapper/package_info.yaml b/examples/shapes/wrapper/package_info.yaml index 5b8d9d0..9a6c7e1 100644 --- a/examples/shapes/wrapper/package_info.yaml +++ b/examples/shapes/wrapper/package_info.yaml @@ -94,12 +94,6 @@ modules: - name: Triangle excluded: True # Exclude this class from wrapping. - - name: mesh - source_locations: - classes: - - name: ConcreteMesh - - name: AbstractMesh - # Text to add at the top of all wrappers prefix_text: | // This file is automatically generated by cppwg. diff --git a/examples/shapes/wrapper/wrapper_header_collection.cppwg.hpp b/examples/shapes/wrapper/wrapper_header_collection.cppwg.hpp index 8eb7eb1..b4694d9 100644 --- a/examples/shapes/wrapper/wrapper_header_collection.cppwg.hpp +++ b/examples/shapes/wrapper/wrapper_header_collection.cppwg.hpp @@ -5,10 +5,7 @@ #define pyshapes_HEADERS_HPP_ // Includes -#include "AbstractMesh.hpp" -#include "ConcreteMesh.hpp" #include "Cuboid.hpp" -#include "MeshFactory.hpp" #include "Point.hpp" #include "Rectangle.hpp" #include "Shape.hpp" @@ -21,10 +18,6 @@ template class Point<2>; template class Point<3>; template class Shape<2>; template class Shape<3>; -template class AbstractMesh<2, 2>; -template class AbstractMesh<3, 3>; -template class ConcreteMesh<2>; -template class ConcreteMesh<3>; // Typedefs for nicer naming namespace cppwg @@ -33,10 +26,6 @@ namespace cppwg typedef Point<3> Point_3; typedef Shape<2> Shape_2; typedef Shape<3> Shape_3; - typedef AbstractMesh<2, 2> AbstractMesh_2_2; - typedef AbstractMesh<3, 3> AbstractMesh_3_3; - typedef ConcreteMesh<2> ConcreteMesh_2; - typedef ConcreteMesh<3> ConcreteMesh_3; } // namespace cppwg #endif // pyshapes_HEADERS_HPP_ diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_wrapper_generation.py b/test/test_wrapper_generation.py similarity index 56% rename from tests/test_wrapper_generation.py rename to test/test_wrapper_generation.py index b91050f..9213b1b 100644 --- a/tests/test_wrapper_generation.py +++ b/test/test_wrapper_generation.py @@ -1,4 +1,5 @@ import os +import shutil import subprocess import unittest from difflib import context_diff @@ -33,60 +34,60 @@ def file_diff(file_a: str, file_b: str) -> bool: class TestWrapperGeneration(unittest.TestCase): - - def test_wrapper_generation(self) -> None: - """ - Generate wrappers and compare with the reference wrappers. - """ - + def setUp(self) -> None: # Set paths to the shapes code, reference and generated wrappers, etc. - shapes_root = os.path.abspath("examples/shapes") - shapes_src = os.path.join(shapes_root, "src/cpp") - extern_src = os.path.join(shapes_root, "extern") + root = os.path.abspath("examples/shapes") + extern = os.path.join(root, "extern") + self.src = os.path.join(root, "src/cpp") - wrapper_root_ref = os.path.join(shapes_root, "wrapper") - wrapper_root_gen = os.path.join(shapes_root, "gen_wrapper") + self.includes = glob(self.src + "/*/") + glob(extern + "/*/") - self.assertTrue(os.path.isdir(shapes_root)) - self.assertTrue(os.path.isdir(shapes_src)) - self.assertTrue(os.path.isdir(extern_src)) - self.assertTrue(os.path.isdir(wrapper_root_ref)) + self.wrapper = os.path.join(root, "wrapper") + self.wrapper_gen = os.path.join(root, "wrapper.gen") - generate_script = os.path.abspath("cppwg/__main__.py") - package_info_path = os.path.join(wrapper_root_ref, "package_info.yaml") - self.assertTrue(os.path.isfile(package_info_path)) + self.config = os.path.join(self.wrapper, "package_info.yaml") + self.script = os.path.abspath("cppwg/__main__.py") - includes = glob(shapes_src + "/*/") + glob(extern_src + "/*/") + def test_wrapper_generation(self) -> None: + """ + Generate wrappers and compare with the reference wrappers. + """ + self.assertTrue(os.path.isdir(self.src), self.src) + self.assertTrue(os.path.isdir(self.wrapper), self.wrapper) + self.assertTrue(os.path.isfile(self.config), self.config) # Generate the wrappers subprocess.call( [ "python", - generate_script, - shapes_src, + self.script, + self.src, "--wrapper_root", - wrapper_root_gen, + self.wrapper_gen, "--package_info", - package_info_path, + self.config, "--includes", ] - + includes + + self.includes ) - self.assertTrue(os.path.isdir(wrapper_root_gen)) + self.assertTrue(os.path.isdir(self.wrapper_gen), self.wrapper_gen) # Compare the generated files with reference files self.maxDiff = None - for dirpath, _, filenames in os.walk(wrapper_root_ref): + for dirpath, _, filenames in os.walk(self.wrapper): for filename in filenames: if filename.endswith(".cppwg.cpp") or filename.endswith(".cppwg.hpp"): file_ref = os.path.join(dirpath, filename) - file_gen = file_ref.replace(wrapper_root_ref, wrapper_root_gen, 1) + file_gen = file_ref.replace(self.wrapper, self.wrapper_gen, 1) - self.assertTrue(os.path.isfile(file_ref)) - self.assertTrue(os.path.isfile(file_gen)) + self.assertTrue(os.path.isfile(file_ref), file_ref) + self.assertTrue(os.path.isfile(file_gen), file_gen) self.assertEqual(file_diff(file_gen, file_ref), "", f"\n{file_ref}") + def tearDown(self) -> None: + shutil.rmtree(self.wrapper_gen) + if __name__ == "__main__": unittest.main() From 64d6158ac40b1a6840972cf754764b2b128cb955 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 16 Oct 2024 10:34:13 +0000 Subject: [PATCH 02/31] #20 Add cells example --- examples/cells/CMakeLists.txt | 80 ++++++++++++++++++++ examples/cells/dynamic/config.yaml | 37 +++++++++ examples/cells/extern/meshgen/CMakeLists.txt | 6 ++ examples/cells/extern/meshgen/MeshGen.cpp | 16 ++++ examples/cells/extern/meshgen/MeshGen.hpp | 24 ++++++ examples/cells/src/cpp/mesh/AbstractMesh.cpp | 31 ++++++++ examples/cells/src/cpp/mesh/AbstractMesh.hpp | 50 ++++++++++++ examples/cells/src/cpp/mesh/ConcreteMesh.cpp | 19 +++++ examples/cells/src/cpp/mesh/ConcreteMesh.hpp | 29 +++++++ examples/cells/src/cpp/mesh/MeshFactory.cpp | 16 ++++ examples/cells/src/cpp/mesh/MeshFactory.hpp | 27 +++++++ examples/cells/src/py/cells/__init__.py | 0 examples/cells/src/py/cells/_syntax.py | 25 ++++++ examples/cells/src/py/cells/mesh/__init__.py | 1 + examples/cells/src/py/test/test_cells.py | 10 +++ 15 files changed, 371 insertions(+) create mode 100644 examples/cells/CMakeLists.txt create mode 100644 examples/cells/dynamic/config.yaml create mode 100644 examples/cells/extern/meshgen/CMakeLists.txt create mode 100644 examples/cells/extern/meshgen/MeshGen.cpp create mode 100644 examples/cells/extern/meshgen/MeshGen.hpp create mode 100644 examples/cells/src/cpp/mesh/AbstractMesh.cpp create mode 100644 examples/cells/src/cpp/mesh/AbstractMesh.hpp create mode 100644 examples/cells/src/cpp/mesh/ConcreteMesh.cpp create mode 100644 examples/cells/src/cpp/mesh/ConcreteMesh.hpp create mode 100644 examples/cells/src/cpp/mesh/MeshFactory.cpp create mode 100644 examples/cells/src/cpp/mesh/MeshFactory.hpp create mode 100644 examples/cells/src/py/cells/__init__.py create mode 100644 examples/cells/src/py/cells/_syntax.py create mode 100644 examples/cells/src/py/cells/mesh/__init__.py create mode 100644 examples/cells/src/py/test/test_cells.py diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt new file mode 100644 index 0000000..3f72e81 --- /dev/null +++ b/examples/cells/CMakeLists.txt @@ -0,0 +1,80 @@ +cmake_minimum_required(VERSION 3.16...3.22) +project(shapes LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +# Find Python +find_package(Python3 REQUIRED COMPONENTS Interpreter Development) + +# Fetch pybind11 +include(FetchContent) +FetchContent_Declare( + pybind11 + GIT_REPOSITORY https://github.com/pybind/pybind11 + GIT_TAG v2.13.5 + GIT_SHALLOW 1 +) +FetchContent_MakeAvailable(pybind11) + +# Add external library +add_subdirectory(extern/meshgen meshgen_build) + +# Add a shared library for the main C++ source +file(GLOB_RECURSE SHAPES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp) +add_library(shapes SHARED ${SHAPES_SOURCES}) +target_include_directories( + shapes + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/geometry + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/math_funcs + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/primitives +) +target_link_libraries(shapes PUBLIC meshgen::meshgen) + +# Copy the Python source and test trees to the build location +file( + COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/pyshapes + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ +) +file( + COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/test/ + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ +) + +# Create a shared library for each wrapper module +foreach(MODULE geometry math_funcs mesh primitives) + # Add the autogenerated wrappers to the module target + file(GLOB MODULE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/${MODULE}/*.cpp) + + set(MODULE_LIB _pyshapes_${MODULE}) + add_library(${MODULE_LIB} SHARED ${MODULE_SOURCES}) + target_link_libraries( + ${MODULE_LIB} + PRIVATE + Python3::Python + pybind11::module + shapes + ) + target_include_directories( + ${MODULE_LIB} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/wrapper + ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/geometry + ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/math_funcs + ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/mesh + ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/primitives + ) + + # Set suitable extensions for the module and place the compiled + # shared library in a suitable location inside the python package + set_target_properties( + ${MODULE_LIB} + PROPERTIES + PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}" + LIBRARY_OUTPUT_DIRECTORY + ${CMAKE_CURRENT_BINARY_DIR}/pyshapes/${MODULE}/ + ) +endforeach() diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml new file mode 100644 index 0000000..3a17738 --- /dev/null +++ b/examples/cells/dynamic/config.yaml @@ -0,0 +1,37 @@ +# For docs on config options, see examples/shapes/wrapper/package_info.yaml +name: pycells + +smart_ptr_type: std::shared_ptr +pointer_call_policy: reference +reference_call_policy: reference_internal + +common_include_file: False +source_includes: + - + +exclude_default_args: False + +template_substitutions: + - signature: + replacement: [[2], [3]] + - signature: + replacement: [[2, 2], [3, 3]] + - signature: + replacement: [[2, 2], [3, 3]] + +modules: + - name: lib + source_locations: + - cycle + - mesh + - ode + - pde + - population + - visualization + + classes: + - name: AbstractCellPopulation + - name: MeshBasedCellPopulation + +prefix_text: | + // This file is auto-generated by cppwg; manual changes will be overwritten. diff --git a/examples/cells/extern/meshgen/CMakeLists.txt b/examples/cells/extern/meshgen/CMakeLists.txt new file mode 100644 index 0000000..672572f --- /dev/null +++ b/examples/cells/extern/meshgen/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.8) +project(meshgen) + +add_library(meshgen MeshGen.cpp MeshGen.hpp) +target_include_directories(meshgen PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +add_library(meshgen::meshgen ALIAS meshgen) diff --git a/examples/cells/extern/meshgen/MeshGen.cpp b/examples/cells/extern/meshgen/MeshGen.cpp new file mode 100644 index 0000000..e411abc --- /dev/null +++ b/examples/cells/extern/meshgen/MeshGen.cpp @@ -0,0 +1,16 @@ +#include "MeshGen.hpp" + +MeshGen::MeshGen() +{ + // Constructor +} + +MeshGen::~MeshGen() +{ + // Destructor +} + +void MeshGen::generateMesh() +{ + // Generate a mesh +} diff --git a/examples/cells/extern/meshgen/MeshGen.hpp b/examples/cells/extern/meshgen/MeshGen.hpp new file mode 100644 index 0000000..0e71bf0 --- /dev/null +++ b/examples/cells/extern/meshgen/MeshGen.hpp @@ -0,0 +1,24 @@ +#ifndef _MESHGEN_HPP +#define _MESHGEN_HPP + +class MeshGen +{ + +public: + /** + * Default Constructor + */ + MeshGen(); + + /** + * Destructor + */ + ~MeshGen(); + + /** + * Generate a mesh + */ + void generateMesh(); +}; + +#endif // _MESHGEN_HPP diff --git a/examples/cells/src/cpp/mesh/AbstractMesh.cpp b/examples/cells/src/cpp/mesh/AbstractMesh.cpp new file mode 100644 index 0000000..7f6ec65 --- /dev/null +++ b/examples/cells/src/cpp/mesh/AbstractMesh.cpp @@ -0,0 +1,31 @@ +#include "AbstractMesh.hpp" + +template +AbstractMesh::AbstractMesh() : mIndex(0) +{ +} + +template +AbstractMesh::~AbstractMesh() +{ +} + +template +unsigned AbstractMesh::GetIndex() const +{ + return mIndex; +} + +template +void AbstractMesh::SetIndex(unsigned index) +{ + mIndex = index; +} + +template +void AbstractMesh::AddVertex(Point vertex) +{ +} + +template class AbstractMesh<2, 2>; +template class AbstractMesh<3, 3>; diff --git a/examples/cells/src/cpp/mesh/AbstractMesh.hpp b/examples/cells/src/cpp/mesh/AbstractMesh.hpp new file mode 100644 index 0000000..4ccbae6 --- /dev/null +++ b/examples/cells/src/cpp/mesh/AbstractMesh.hpp @@ -0,0 +1,50 @@ +#ifndef _ABSTRACT_MESH_HPP +#define _ABSTRACT_MESH_HPP + +#include "Point.hpp" + +/** + * A mesh in SPACE_DIM space with ELEMENT_DIM dimensional elements + */ +template +class AbstractMesh +{ +private: + /** + * AbstractMesh index + */ + unsigned mIndex; + +public: + /** + * Default Constructor + */ + AbstractMesh(); + + /** + * Destructor + */ + ~AbstractMesh(); + + /** + * Return the index + */ + unsigned GetIndex() const; + + /** + * Set the index + */ + void SetIndex(unsigned index); + + /** + * Add a vertex to the mesh + */ + void AddVertex(Point vertex); + + /** + * Scale the mesh by a factor + */ + virtual void Scale(const double factor) = 0; +}; + +#endif // _ABSTRACT_MESH_HPP diff --git a/examples/cells/src/cpp/mesh/ConcreteMesh.cpp b/examples/cells/src/cpp/mesh/ConcreteMesh.cpp new file mode 100644 index 0000000..65cee4f --- /dev/null +++ b/examples/cells/src/cpp/mesh/ConcreteMesh.cpp @@ -0,0 +1,19 @@ +#include "AbstractMesh.hpp" +#include "ConcreteMesh.hpp" + +template +ConcreteMesh::ConcreteMesh() : AbstractMesh() +{ +} + +template +ConcreteMesh::~ConcreteMesh() +{ +} +template +void ConcreteMesh::Scale(const double factor){ + // Scale the mesh +}; + +template class ConcreteMesh<2>; +template class ConcreteMesh<3>; diff --git a/examples/cells/src/cpp/mesh/ConcreteMesh.hpp b/examples/cells/src/cpp/mesh/ConcreteMesh.hpp new file mode 100644 index 0000000..84d917e --- /dev/null +++ b/examples/cells/src/cpp/mesh/ConcreteMesh.hpp @@ -0,0 +1,29 @@ +#ifndef _CONCRETE_MESH_HPP +#define _CONCRETE_MESH_HPP + +#include "AbstractMesh.hpp" + +/** + * A concrete mesh implementation + */ +template +class ConcreteMesh : public AbstractMesh +{ +public: + /** + * Default Constructor + */ + ConcreteMesh(); + + /** + * Destructor + */ + ~ConcreteMesh(); + + /** + * Scale the mesh by a factor + */ + void Scale(const double factor) override; +}; + +#endif // _CONCRETE_MESH_HPP diff --git a/examples/cells/src/cpp/mesh/MeshFactory.cpp b/examples/cells/src/cpp/mesh/MeshFactory.cpp new file mode 100644 index 0000000..cc95b03 --- /dev/null +++ b/examples/cells/src/cpp/mesh/MeshFactory.cpp @@ -0,0 +1,16 @@ +#include "ConcreteMesh.hpp" + +#include "MeshFactory.hpp" + +template +MeshFactory::MeshFactory(): mMeshGen() +{ +} + +template +MeshFactory::~MeshFactory() +{ +} + +template class MeshFactory >; +template class MeshFactory >; diff --git a/examples/cells/src/cpp/mesh/MeshFactory.hpp b/examples/cells/src/cpp/mesh/MeshFactory.hpp new file mode 100644 index 0000000..1f414cd --- /dev/null +++ b/examples/cells/src/cpp/mesh/MeshFactory.hpp @@ -0,0 +1,27 @@ +#ifndef _MESH_FACTORY_HPP +#define _MESH_FACTORY_HPP + +#include "MeshGen.hpp" + +/** + * A concrete mesh implementation + */ +template +class MeshFactory +{ +private: + MeshGen mMeshGen; + +public: + /** + * Default Constructor + */ + MeshFactory(); + + /** + * Destructor + */ + ~MeshFactory(); +}; + +#endif // _MESH_FACTORY_HPP diff --git a/examples/cells/src/py/cells/__init__.py b/examples/cells/src/py/cells/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/cells/src/py/cells/_syntax.py b/examples/cells/src/py/cells/_syntax.py new file mode 100644 index 0000000..2afd7e6 --- /dev/null +++ b/examples/cells/src/py/cells/_syntax.py @@ -0,0 +1,25 @@ +import inspect +from collections.abc import Iterable + + +class TemplateClassDict: + + def __init__(self, template_dict): + self._dict = {} + for arg_tuple, cls in template_dict.items(): + if not inspect.isclass(cls): + raise TypeError("Expected class, got {}".format(type(cls))) + if not isinstance(arg_tuple, Iterable): + arg_tuple = (arg_tuple,) + key = tuple( + arg.__name__ if inspect.isclass(arg) else str(arg) for arg in arg_tuple + ) + self._dict[key] = cls + + def __getitem__(self, arg_tuple): + if not isinstance(arg_tuple, Iterable): + arg_tuple = (arg_tuple,) + key = tuple( + arg.__name__ if inspect.isclass(arg) else str(arg) for arg in arg_tuple + ) + return self._dict[key] diff --git a/examples/cells/src/py/cells/mesh/__init__.py b/examples/cells/src/py/cells/mesh/__init__.py new file mode 100644 index 0000000..7f9ab87 --- /dev/null +++ b/examples/cells/src/py/cells/mesh/__init__.py @@ -0,0 +1 @@ +from pycells._pycells_lib import ConcreteMesh_2 diff --git a/examples/cells/src/py/test/test_cells.py b/examples/cells/src/py/test/test_cells.py new file mode 100644 index 0000000..133cf84 --- /dev/null +++ b/examples/cells/src/py/test/test_cells.py @@ -0,0 +1,10 @@ +import unittest + + +class TestCells(unittest.TestCase): + + def testCells(self): + pass + +if __name__ == "__main__": + unittest.main() From 81a886eb2e2416b796fd6ee515b0df532968b3d5 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 16 Oct 2024 10:51:45 +0000 Subject: [PATCH 03/31] #20 Add Potts to cells example --- examples/cells/dynamic/config.yaml | 18 ++++++++++++++---- .../mesh/{ConcreteMesh.cpp => PottsMesh.cpp} | 0 .../mesh/{ConcreteMesh.hpp => PottsMesh.hpp} | 0 3 files changed, 14 insertions(+), 4 deletions(-) rename examples/cells/src/cpp/mesh/{ConcreteMesh.cpp => PottsMesh.cpp} (100%) rename examples/cells/src/cpp/mesh/{ConcreteMesh.hpp => PottsMesh.hpp} (100%) diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index 3a17738..7ac5afe 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -22,16 +22,26 @@ template_substitutions: modules: - name: lib source_locations: - - cycle + - cell - mesh - - ode - - pde - population - visualization classes: + # cell + - name: Cell + + # mesh + - name: AbstractMesh + - name: MeshFactory + - name: PottsMesh + + # population - name: AbstractCellPopulation - - name: MeshBasedCellPopulation + - name: PottsCellPopulation + + # visualization + - name: VtkScene prefix_text: | // This file is auto-generated by cppwg; manual changes will be overwritten. diff --git a/examples/cells/src/cpp/mesh/ConcreteMesh.cpp b/examples/cells/src/cpp/mesh/PottsMesh.cpp similarity index 100% rename from examples/cells/src/cpp/mesh/ConcreteMesh.cpp rename to examples/cells/src/cpp/mesh/PottsMesh.cpp diff --git a/examples/cells/src/cpp/mesh/ConcreteMesh.hpp b/examples/cells/src/cpp/mesh/PottsMesh.hpp similarity index 100% rename from examples/cells/src/cpp/mesh/ConcreteMesh.hpp rename to examples/cells/src/cpp/mesh/PottsMesh.hpp From c4fd42ab2fa3cef3d7093ef6b22c65f0ab1777f0 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Mon, 28 Oct 2024 12:40:17 +0000 Subject: [PATCH 04/31] #20 Build cells example correctly --- cppwg/info/class_info.py | 2 +- examples/cells/CMakeLists.txt | 104 +++++++++--------- examples/cells/dynamic/config.yaml | 13 ++- .../wrapper_header_collection.cppwg.hpp | 30 +++++ examples/cells/pyproject.toml | 10 ++ examples/cells/src/cpp/mesh/AbstractMesh.cpp | 8 +- examples/cells/src/cpp/mesh/AbstractMesh.hpp | 4 +- examples/cells/src/cpp/mesh/MeshFactory.cpp | 6 +- examples/cells/src/cpp/mesh/PottsMesh.cpp | 12 +- examples/cells/src/cpp/mesh/PottsMesh.hpp | 14 +-- .../src/py/{test => tests}/test_cells.py | 0 .../src/py/{test => tests}/test_classes.py | 0 .../src/py/{test => tests}/test_functions.py | 0 {test => tests}/__init__.py | 0 {test => tests}/test_wrapper_generation.py | 0 15 files changed, 125 insertions(+), 78 deletions(-) create mode 100644 examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp create mode 100644 examples/cells/pyproject.toml rename examples/cells/src/py/{test => tests}/test_cells.py (100%) rename examples/shapes/src/py/{test => tests}/test_classes.py (100%) rename examples/shapes/src/py/{test => tests}/test_functions.py (100%) rename {test => tests}/__init__.py (100%) rename {test => tests}/test_wrapper_generation.py (100%) diff --git a/cppwg/info/class_info.py b/cppwg/info/class_info.py index 1388ce3..d82e1c6 100644 --- a/cppwg/info/class_info.py +++ b/cppwg/info/class_info.py @@ -305,7 +305,7 @@ class instantiation. For example, class "Foo" with template arguments arg_str = arg_str.replace(name, replacement) # Remove special characters - arg_str = arg_str.translate(rm_table) + arg_str = arg_str.replace("<", "_").replace(",", "_").translate(rm_table) # Capitalize the first letter if len(arg_str) > 1: diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 3f72e81..645df95 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -1,11 +1,41 @@ cmake_minimum_required(VERSION 3.16...3.22) -project(shapes LANGUAGES CXX) +project(cells LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) # Find Python find_package(Python3 REQUIRED COMPONENTS Interpreter Development) +# Find VTK +find_package(VTK 7.1 + REQUIRED + COMPONENTS + vtkCommonCore + vtkCommonDataModel + vtkFiltersCore + vtkFiltersGeneral + vtkFiltersGeneric + vtkFiltersGeometry + vtkFiltersModeling + vtkFiltersProgrammable + vtkFiltersSources + vtkFiltersVerdict + vtkInteractionStyle + vtkIOCore + vtkIOGeometry + vtkIOImage + vtkIOLegacy + vtkIOMovie + vtkIOParallelXML + vtkIOXML + vtkRenderingAnnotation + vtkRenderingCore + vtkRenderingFreeType + vtkRenderingOpenGL2 + vtkWrappingPythonCore +) + # Fetch pybind11 include(FetchContent) FetchContent_Declare( @@ -20,61 +50,35 @@ FetchContent_MakeAvailable(pybind11) add_subdirectory(extern/meshgen meshgen_build) # Add a shared library for the main C++ source -file(GLOB_RECURSE SHAPES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp) -add_library(shapes SHARED ${SHAPES_SOURCES}) +file(GLOB_RECURSE CELLS_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.hpp +) +add_library(cells SHARED ${CELLS_SOURCES}) target_include_directories( - shapes + cells PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/geometry - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/math_funcs ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/primitives ) -target_link_libraries(shapes PUBLIC meshgen::meshgen) +target_link_libraries(cells PUBLIC meshgen::meshgen) -# Copy the Python source and test trees to the build location -file( - COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/pyshapes - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ +# Set up Pybind11 module +file(GLOB PYCELLS_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/lib/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/lib/*.hpp ) -file( - COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/test/ - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ +pybind11_add_module(_pycells_lib MODULE ${PYCELLS_SOURCES}) +target_link_libraries( + _pycells_lib + PRIVATE + cells +) +target_include_directories( + _pycells_lib + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/wrappers/lib ) -# Create a shared library for each wrapper module -foreach(MODULE geometry math_funcs mesh primitives) - # Add the autogenerated wrappers to the module target - file(GLOB MODULE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/${MODULE}/*.cpp) - - set(MODULE_LIB _pyshapes_${MODULE}) - add_library(${MODULE_LIB} SHARED ${MODULE_SOURCES}) - target_link_libraries( - ${MODULE_LIB} - PRIVATE - Python3::Python - pybind11::module - shapes - ) - target_include_directories( - ${MODULE_LIB} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/wrapper - ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/geometry - ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/math_funcs - ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/mesh - ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/primitives - ) - - # Set suitable extensions for the module and place the compiled - # shared library in a suitable location inside the python package - set_target_properties( - ${MODULE_LIB} - PROPERTIES - PREFIX "${PYTHON_MODULE_PREFIX}" - SUFFIX "${PYTHON_MODULE_EXTENSION}" - LIBRARY_OUTPUT_DIRECTORY - ${CMAKE_CURRENT_BINARY_DIR}/pyshapes/${MODULE}/ - ) -endforeach() +# Add install target for scikit-build +install(TARGETS cells DESTINATION .) diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index 7ac5afe..91b4633 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -29,19 +29,24 @@ modules: classes: # cell - - name: Cell + # - name: Cell # mesh - name: AbstractMesh - name: MeshFactory + source_includes: + - PottsMesh.hpp + template_substitutions: + - signature: + replacement: [['PottsMesh<2>'], ['PottsMesh<3>']] - name: PottsMesh # population - - name: AbstractCellPopulation - - name: PottsCellPopulation + # - name: AbstractCellPopulation + # - name: PottsCellPopulation # visualization - - name: VtkScene + # - name: VtkScene prefix_text: | // This file is auto-generated by cppwg; manual changes will be overwritten. diff --git a/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp new file mode 100644 index 0000000..14d443b --- /dev/null +++ b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp @@ -0,0 +1,30 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef pycells_HEADERS_HPP_ +#define pycells_HEADERS_HPP_ + +// Includes +#include "AbstractMesh.hpp" +#include "MeshFactory.hpp" +#include "PottsMesh.hpp" + +// Instantiate Template Classes +template class AbstractMesh<2, 2>; +template class AbstractMesh<3, 3>; +template class MeshFactory>; +template class MeshFactory>; +template class PottsMesh<2>; +template class PottsMesh<3>; + +// Typedefs for nicer naming +namespace cppwg +{ + typedef AbstractMesh<2, 2> AbstractMesh_2_2; + typedef AbstractMesh<3, 3> AbstractMesh_3_3; + typedef MeshFactory> MeshFactory_PottsMesh_2; + typedef MeshFactory> MeshFactory_PottsMesh_3; + typedef PottsMesh<2> PottsMesh_2; + typedef PottsMesh<3> PottsMesh_3; +} // namespace cppwg + +#endif // pycells_HEADERS_HPP_ diff --git a/examples/cells/pyproject.toml b/examples/cells/pyproject.toml new file mode 100644 index 0000000..8677070 --- /dev/null +++ b/examples/cells/pyproject.toml @@ -0,0 +1,10 @@ +[project] +name = "pycells" +version = "0.0.1" + +[build-system] +requires = ["scikit-build-core"] +build-backend = "scikit_build_core.build" + +[tool.scikit-build.cmake] +build-type = "Release" diff --git a/examples/cells/src/cpp/mesh/AbstractMesh.cpp b/examples/cells/src/cpp/mesh/AbstractMesh.cpp index 7f6ec65..776d784 100644 --- a/examples/cells/src/cpp/mesh/AbstractMesh.cpp +++ b/examples/cells/src/cpp/mesh/AbstractMesh.cpp @@ -22,10 +22,10 @@ void AbstractMesh::SetIndex(unsigned index) mIndex = index; } -template -void AbstractMesh::AddVertex(Point vertex) -{ -} +// template +// void AbstractMesh::AddVertex(Node vertex) +// { +// } template class AbstractMesh<2, 2>; template class AbstractMesh<3, 3>; diff --git a/examples/cells/src/cpp/mesh/AbstractMesh.hpp b/examples/cells/src/cpp/mesh/AbstractMesh.hpp index 4ccbae6..4867bc2 100644 --- a/examples/cells/src/cpp/mesh/AbstractMesh.hpp +++ b/examples/cells/src/cpp/mesh/AbstractMesh.hpp @@ -1,8 +1,6 @@ #ifndef _ABSTRACT_MESH_HPP #define _ABSTRACT_MESH_HPP -#include "Point.hpp" - /** * A mesh in SPACE_DIM space with ELEMENT_DIM dimensional elements */ @@ -39,7 +37,7 @@ class AbstractMesh /** * Add a vertex to the mesh */ - void AddVertex(Point vertex); + // void AddVertex(Node vertex); /** * Scale the mesh by a factor diff --git a/examples/cells/src/cpp/mesh/MeshFactory.cpp b/examples/cells/src/cpp/mesh/MeshFactory.cpp index cc95b03..92138d6 100644 --- a/examples/cells/src/cpp/mesh/MeshFactory.cpp +++ b/examples/cells/src/cpp/mesh/MeshFactory.cpp @@ -1,4 +1,4 @@ -#include "ConcreteMesh.hpp" +#include "PottsMesh.hpp" #include "MeshFactory.hpp" @@ -12,5 +12,5 @@ MeshFactory::~MeshFactory() { } -template class MeshFactory >; -template class MeshFactory >; +template class MeshFactory >; +template class MeshFactory >; diff --git a/examples/cells/src/cpp/mesh/PottsMesh.cpp b/examples/cells/src/cpp/mesh/PottsMesh.cpp index 65cee4f..ba676be 100644 --- a/examples/cells/src/cpp/mesh/PottsMesh.cpp +++ b/examples/cells/src/cpp/mesh/PottsMesh.cpp @@ -1,19 +1,19 @@ #include "AbstractMesh.hpp" -#include "ConcreteMesh.hpp" +#include "PottsMesh.hpp" template -ConcreteMesh::ConcreteMesh() : AbstractMesh() +PottsMesh::PottsMesh() : AbstractMesh() { } template -ConcreteMesh::~ConcreteMesh() +PottsMesh::~PottsMesh() { } template -void ConcreteMesh::Scale(const double factor){ +void PottsMesh::Scale(const double factor){ // Scale the mesh }; -template class ConcreteMesh<2>; -template class ConcreteMesh<3>; +template class PottsMesh<2>; +template class PottsMesh<3>; diff --git a/examples/cells/src/cpp/mesh/PottsMesh.hpp b/examples/cells/src/cpp/mesh/PottsMesh.hpp index 84d917e..684e7b3 100644 --- a/examples/cells/src/cpp/mesh/PottsMesh.hpp +++ b/examples/cells/src/cpp/mesh/PottsMesh.hpp @@ -1,24 +1,24 @@ -#ifndef _CONCRETE_MESH_HPP -#define _CONCRETE_MESH_HPP +#ifndef _POTTS_MESH_HPP +#define _POTTS_MESH_HPP #include "AbstractMesh.hpp" /** - * A concrete mesh implementation + * A Potts mesh implementation */ template -class ConcreteMesh : public AbstractMesh +class PottsMesh : public AbstractMesh { public: /** * Default Constructor */ - ConcreteMesh(); + PottsMesh(); /** * Destructor */ - ~ConcreteMesh(); + ~PottsMesh(); /** * Scale the mesh by a factor @@ -26,4 +26,4 @@ class ConcreteMesh : public AbstractMesh void Scale(const double factor) override; }; -#endif // _CONCRETE_MESH_HPP +#endif // _POTTS_MESH_HPP diff --git a/examples/cells/src/py/test/test_cells.py b/examples/cells/src/py/tests/test_cells.py similarity index 100% rename from examples/cells/src/py/test/test_cells.py rename to examples/cells/src/py/tests/test_cells.py diff --git a/examples/shapes/src/py/test/test_classes.py b/examples/shapes/src/py/tests/test_classes.py similarity index 100% rename from examples/shapes/src/py/test/test_classes.py rename to examples/shapes/src/py/tests/test_classes.py diff --git a/examples/shapes/src/py/test/test_functions.py b/examples/shapes/src/py/tests/test_functions.py similarity index 100% rename from examples/shapes/src/py/test/test_functions.py rename to examples/shapes/src/py/tests/test_functions.py diff --git a/test/__init__.py b/tests/__init__.py similarity index 100% rename from test/__init__.py rename to tests/__init__.py diff --git a/test/test_wrapper_generation.py b/tests/test_wrapper_generation.py similarity index 100% rename from test/test_wrapper_generation.py rename to tests/test_wrapper_generation.py From e5c5ff7bd75a5a63aca768edc713a3e2728b2ea1 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Mon, 28 Oct 2024 17:53:21 +0000 Subject: [PATCH 05/31] #20 Simplify cells example build --- .github/workflows/build-and-test.yml | 2 +- examples/cells/CMakeLists.txt | 54 ++++++------------- examples/cells/pyproject.toml | 16 +++--- examples/cells/setup.cfg | 21 ++++++++ examples/cells/setup.py | 3 ++ examples/cells/src/py/cells/__init__.py | 0 examples/cells/src/py/pycells/__init__.py | 1 + .../src/py/{cells => pycells}/_syntax.py | 0 .../py/{cells => pycells}/mesh/__init__.py | 0 .../cells/{src/py => }/tests/test_cells.py | 0 10 files changed, 50 insertions(+), 47 deletions(-) create mode 100644 examples/cells/setup.cfg create mode 100644 examples/cells/setup.py delete mode 100644 examples/cells/src/py/cells/__init__.py create mode 100644 examples/cells/src/py/pycells/__init__.py rename examples/cells/src/py/{cells => pycells}/_syntax.py (100%) rename examples/cells/src/py/{cells => pycells}/mesh/__init__.py (100%) rename examples/cells/{src/py => }/tests/test_cells.py (100%) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 4db2767..bbd4259 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -42,7 +42,7 @@ jobs: run: python -m flake8 - name: Test wrapper generation - run: python -m unittest test/test_wrapper_generation.py + run: python -m unittest tests/test_wrapper_generation.py - name: Generate new wrappers run: | diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 645df95..8772c28 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -1,11 +1,11 @@ cmake_minimum_required(VERSION 3.16...3.22) -project(cells LANGUAGES CXX) +project(pycells LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# Find Python -find_package(Python3 REQUIRED COMPONENTS Interpreter Development) +# Find pybind11 +find_package(pybind11 CONFIG REQUIRED) # Find VTK find_package(VTK 7.1 @@ -36,49 +36,27 @@ find_package(VTK 7.1 vtkWrappingPythonCore ) -# Fetch pybind11 -include(FetchContent) -FetchContent_Declare( - pybind11 - GIT_REPOSITORY https://github.com/pybind/pybind11 - GIT_TAG v2.13.5 - GIT_SHALLOW 1 -) -FetchContent_MakeAvailable(pybind11) - # Add external library add_subdirectory(extern/meshgen meshgen_build) -# Add a shared library for the main C++ source -file(GLOB_RECURSE CELLS_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.hpp +# Set up pycells module +file(GLOB SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/**/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/**/*.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/**/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/**/*.hpp ) -add_library(cells SHARED ${CELLS_SOURCES}) -target_include_directories( - cells - PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh -) -target_link_libraries(cells PUBLIC meshgen::meshgen) -# Set up Pybind11 module -file(GLOB PYCELLS_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/lib/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/lib/*.hpp -) -pybind11_add_module(_pycells_lib MODULE ${PYCELLS_SOURCES}) -target_link_libraries( - _pycells_lib - PRIVATE - cells -) +pybind11_add_module(_pycells_lib ${SOURCES}) + target_include_directories( _pycells_lib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/wrappers/lib + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh ) -# Add install target for scikit-build -install(TARGETS cells DESTINATION .) +target_link_libraries(_pycells_lib PUBLIC meshgen::meshgen) + +install(TARGETS _pycells_lib LIBRARY DESTINATION pycells) diff --git a/examples/cells/pyproject.toml b/examples/cells/pyproject.toml index 8677070..86fd8eb 100644 --- a/examples/cells/pyproject.toml +++ b/examples/cells/pyproject.toml @@ -1,10 +1,10 @@ -[project] -name = "pycells" -version = "0.0.1" - [build-system] -requires = ["scikit-build-core"] -build-backend = "scikit_build_core.build" +requires = [ + "scikit_build_core", + "setuptools", + "pybind11", +] +build-backend = "scikit_build_core.setuptools.build_meta" -[tool.scikit-build.cmake] -build-type = "Release" +[tool.scikit-build] +cmake.build-type = "Release" diff --git a/examples/cells/setup.cfg b/examples/cells/setup.cfg new file mode 100644 index 0000000..274ff13 --- /dev/null +++ b/examples/cells/setup.cfg @@ -0,0 +1,21 @@ +[metadata] +name = pycells +version = 0.0.1 + +[options] +zip_safe = False +python_requires = >=3.9 +packages = find: +package_dir = + =src/py +include_package_data = True + +[options.packages.find] +where = src/py + +[options.package_data] +mypkg = + *.so + +[build_cmake] +source_dir = . diff --git a/examples/cells/setup.py b/examples/cells/setup.py new file mode 100644 index 0000000..6068493 --- /dev/null +++ b/examples/cells/setup.py @@ -0,0 +1,3 @@ +from setuptools import setup + +setup() diff --git a/examples/cells/src/py/cells/__init__.py b/examples/cells/src/py/cells/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/examples/cells/src/py/pycells/__init__.py b/examples/cells/src/py/pycells/__init__.py new file mode 100644 index 0000000..266fdcd --- /dev/null +++ b/examples/cells/src/py/pycells/__init__.py @@ -0,0 +1 @@ +from ._pycells_lib import * diff --git a/examples/cells/src/py/cells/_syntax.py b/examples/cells/src/py/pycells/_syntax.py similarity index 100% rename from examples/cells/src/py/cells/_syntax.py rename to examples/cells/src/py/pycells/_syntax.py diff --git a/examples/cells/src/py/cells/mesh/__init__.py b/examples/cells/src/py/pycells/mesh/__init__.py similarity index 100% rename from examples/cells/src/py/cells/mesh/__init__.py rename to examples/cells/src/py/pycells/mesh/__init__.py diff --git a/examples/cells/src/py/tests/test_cells.py b/examples/cells/tests/test_cells.py similarity index 100% rename from examples/cells/src/py/tests/test_cells.py rename to examples/cells/tests/test_cells.py From 5d3e53adb575d0b129592e51b859c0ea76bcbd6b Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Tue, 29 Oct 2024 07:47:31 +0000 Subject: [PATCH 06/31] #20 Add Scene to cells example --- examples/cells/CMakeLists.txt | 4 +++- examples/cells/dynamic/config.yaml | 11 +-------- .../wrapper_header_collection.cppwg.hpp | 5 ++++ .../cells/src/cpp/visualization/Scene.cpp | 22 +++++++++++++++++ .../cells/src/cpp/visualization/Scene.hpp | 24 +++++++++++++++++++ examples/shapes/CMakeLists.txt | 2 +- 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 examples/cells/src/cpp/visualization/Scene.cpp create mode 100644 examples/cells/src/cpp/visualization/Scene.hpp diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 8772c28..f39c0f0 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -55,8 +55,10 @@ target_include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/wrappers/lib ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/visualization + ${VTK_INCLUDE_DIRS} ) -target_link_libraries(_pycells_lib PUBLIC meshgen::meshgen) +target_link_libraries(_pycells_lib PUBLIC meshgen::meshgen ${VTK_LIBRARIES}) install(TARGETS _pycells_lib LIBRARY DESTINATION pycells) diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index 91b4633..08cee60 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -22,15 +22,10 @@ template_substitutions: modules: - name: lib source_locations: - - cell - mesh - - population - visualization classes: - # cell - # - name: Cell - # mesh - name: AbstractMesh - name: MeshFactory @@ -41,12 +36,8 @@ modules: replacement: [['PottsMesh<2>'], ['PottsMesh<3>']] - name: PottsMesh - # population - # - name: AbstractCellPopulation - # - name: PottsCellPopulation - # visualization - # - name: VtkScene + - name: Scene prefix_text: | // This file is auto-generated by cppwg; manual changes will be overwritten. diff --git a/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp index 14d443b..67af8b1 100644 --- a/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp +++ b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp @@ -7,6 +7,7 @@ #include "AbstractMesh.hpp" #include "MeshFactory.hpp" #include "PottsMesh.hpp" +#include "Scene.hpp" // Instantiate Template Classes template class AbstractMesh<2, 2>; @@ -15,6 +16,8 @@ template class MeshFactory>; template class MeshFactory>; template class PottsMesh<2>; template class PottsMesh<3>; +template class Scene<2>; +template class Scene<3>; // Typedefs for nicer naming namespace cppwg @@ -25,6 +28,8 @@ namespace cppwg typedef MeshFactory> MeshFactory_PottsMesh_3; typedef PottsMesh<2> PottsMesh_2; typedef PottsMesh<3> PottsMesh_3; + typedef Scene<2> Scene_2; + typedef Scene<3> Scene_3; } // namespace cppwg #endif // pycells_HEADERS_HPP_ diff --git a/examples/cells/src/cpp/visualization/Scene.cpp b/examples/cells/src/cpp/visualization/Scene.cpp new file mode 100644 index 0000000..93b3d6e --- /dev/null +++ b/examples/cells/src/cpp/visualization/Scene.cpp @@ -0,0 +1,22 @@ +#include "Scene.hpp" + +template +Scene::Scene() + : mpRenderer(vtkSmartPointer::New()) +{ + mpRenderer->SetBackground(0.0, 0.0, 0.0); +} + +template +Scene::~Scene() +{ +} + +template +vtkSmartPointer Scene::GetRenderer() +{ + return mpRenderer; +} + +template class Scene<2>; +template class Scene<3>; diff --git a/examples/cells/src/cpp/visualization/Scene.hpp b/examples/cells/src/cpp/visualization/Scene.hpp new file mode 100644 index 0000000..fcd8d10 --- /dev/null +++ b/examples/cells/src/cpp/visualization/Scene.hpp @@ -0,0 +1,24 @@ +#ifndef SCENE_HPP_ +#define SCENE_HPP_ + +#include +#include +#include + +VTK_MODULE_INIT(vtkRenderingOpenGL2); +VTK_MODULE_INIT(vtkRenderingFreeType); + +template +class Scene +{ + vtkSmartPointer mpRenderer; + +public: + Scene(); + + virtual ~Scene(); + + vtkSmartPointer GetRenderer(); +}; + +#endif // SCENE_HPP_ diff --git a/examples/shapes/CMakeLists.txt b/examples/shapes/CMakeLists.txt index d276e6e..95d872f 100644 --- a/examples/shapes/CMakeLists.txt +++ b/examples/shapes/CMakeLists.txt @@ -34,7 +34,7 @@ file( DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ ) file( - COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/test/ + COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/tests/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ ) From 5577aa9e314da7dee1dfc54b1464e1a79c5d5dee Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 30 Oct 2024 14:16:03 +0000 Subject: [PATCH 07/31] #20 Add vtk typecaster to cell example --- cppwg/info/class_info.py | 4 +- examples/cells/CMakeLists.txt | 8 +- examples/cells/dynamic/config.yaml | 9 ++ .../wrapper_header_collection.cppwg.hpp | 6 + examples/cells/src/cpp/cell/Cell.cpp | 14 ++ examples/cells/src/cpp/cell/Cell.hpp | 29 ++++ examples/cells/src/cpp/mesh/AbstractMesh.cpp | 9 +- examples/cells/src/cpp/mesh/AbstractMesh.hpp | 6 +- examples/cells/src/cpp/mesh/MeshFactory.cpp | 3 +- examples/cells/src/cpp/mesh/Node.cpp | 34 +++++ examples/cells/src/cpp/mesh/Node.hpp | 46 ++++++ examples/cells/src/cpp/mesh/PottsMesh.cpp | 1 - .../cells/src/cpp/visualization/Scene.cpp | 12 +- .../cells/src/cpp/visualization/Scene.hpp | 4 +- examples/cells/src/py/pycells/__init__.py | 31 +++- .../cells/src/py/pycells/mesh/__init__.py | 1 - examples/cells/tests/test_cells.py | 10 +- .../meshgen/CMakeLists.txt | 0 .../meshgen/MeshGen.cpp | 0 .../meshgen/MeshGen.hpp | 0 examples/cells/thirdparty/smtk/LICENSE.txt | 38 +++++ .../thirdparty/smtk/PybindVTKTypeCaster.h | 139 ++++++++++++++++++ 22 files changed, 385 insertions(+), 19 deletions(-) create mode 100644 examples/cells/src/cpp/cell/Cell.cpp create mode 100644 examples/cells/src/cpp/cell/Cell.hpp create mode 100644 examples/cells/src/cpp/mesh/Node.cpp create mode 100644 examples/cells/src/cpp/mesh/Node.hpp delete mode 100644 examples/cells/src/py/pycells/mesh/__init__.py rename examples/cells/{extern => thirdparty}/meshgen/CMakeLists.txt (100%) rename examples/cells/{extern => thirdparty}/meshgen/MeshGen.cpp (100%) rename examples/cells/{extern => thirdparty}/meshgen/MeshGen.hpp (100%) create mode 100644 examples/cells/thirdparty/smtk/LICENSE.txt create mode 100644 examples/cells/thirdparty/smtk/PybindVTKTypeCaster.h diff --git a/cppwg/info/class_info.py b/cppwg/info/class_info.py index d82e1c6..ff1cf8e 100644 --- a/cppwg/info/class_info.py +++ b/cppwg/info/class_info.py @@ -305,7 +305,9 @@ class instantiation. For example, class "Foo" with template arguments arg_str = arg_str.replace(name, replacement) # Remove special characters - arg_str = arg_str.replace("<", "_").replace(",", "_").translate(rm_table) + arg_str = ( + arg_str.replace("<", "_").replace(",", "_").translate(rm_table) + ) # Capitalize the first letter if len(arg_str) > 1: diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index f39c0f0..b07b884 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -29,6 +29,7 @@ find_package(VTK 7.1 vtkIOMovie vtkIOParallelXML vtkIOXML + vtkPython vtkRenderingAnnotation vtkRenderingCore vtkRenderingFreeType @@ -37,7 +38,7 @@ find_package(VTK 7.1 ) # Add external library -add_subdirectory(extern/meshgen meshgen_build) +add_subdirectory(thirdparty/meshgen meshgen_build) # Set up pycells module file(GLOB SOURCES @@ -51,11 +52,14 @@ pybind11_add_module(_pycells_lib ${SOURCES}) target_include_directories( _pycells_lib - PRIVATE + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/wrappers/lib ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/cell ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/visualization + ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/meshgen + ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/smtk ${VTK_INCLUDE_DIRS} ) diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index 08cee60..b699112 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -14,6 +14,8 @@ exclude_default_args: False template_substitutions: - signature: replacement: [[2], [3]] + - signature: + replacement: [[2], [3]] - signature: replacement: [[2, 2], [3, 3]] - signature: @@ -22,10 +24,14 @@ template_substitutions: modules: - name: lib source_locations: + - cell - mesh - visualization classes: + # cell + - name: Cell + # mesh - name: AbstractMesh - name: MeshFactory @@ -34,10 +40,13 @@ modules: template_substitutions: - signature: replacement: [['PottsMesh<2>'], ['PottsMesh<3>']] + - name: Node - name: PottsMesh # visualization - name: Scene + source_includes: + - PybindVTKTypeCaster.h prefix_text: | // This file is auto-generated by cppwg; manual changes will be overwritten. diff --git a/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp index 67af8b1..dd18042 100644 --- a/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp +++ b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp @@ -5,7 +5,9 @@ // Includes #include "AbstractMesh.hpp" +#include "Cell.hpp" #include "MeshFactory.hpp" +#include "Node.hpp" #include "PottsMesh.hpp" #include "Scene.hpp" @@ -14,6 +16,8 @@ template class AbstractMesh<2, 2>; template class AbstractMesh<3, 3>; template class MeshFactory>; template class MeshFactory>; +template class Node<2>; +template class Node<3>; template class PottsMesh<2>; template class PottsMesh<3>; template class Scene<2>; @@ -26,6 +30,8 @@ namespace cppwg typedef AbstractMesh<3, 3> AbstractMesh_3_3; typedef MeshFactory> MeshFactory_PottsMesh_2; typedef MeshFactory> MeshFactory_PottsMesh_3; + typedef Node<2> Node_2; + typedef Node<3> Node_3; typedef PottsMesh<2> PottsMesh_2; typedef PottsMesh<3> PottsMesh_3; typedef Scene<2> Scene_2; diff --git a/examples/cells/src/cpp/cell/Cell.cpp b/examples/cells/src/cpp/cell/Cell.cpp new file mode 100644 index 0000000..c7d3a6a --- /dev/null +++ b/examples/cells/src/cpp/cell/Cell.cpp @@ -0,0 +1,14 @@ +#include "Cell.hpp" + +Cell::Cell() : mIndex(0) +{ +} + +Cell::~Cell() +{ +} + +unsigned Cell::GetCellId() const +{ + return mIndex; +} diff --git a/examples/cells/src/cpp/cell/Cell.hpp b/examples/cells/src/cpp/cell/Cell.hpp new file mode 100644 index 0000000..68b8b49 --- /dev/null +++ b/examples/cells/src/cpp/cell/Cell.hpp @@ -0,0 +1,29 @@ +#ifndef CELL_HPP_ +#define CELL_HPP_ + +class Cell +{ +private: + /** + * Cell index + */ + unsigned mIndex; + +public: + /** + * Default Constructor + */ + Cell(); + + /** + * Destructor + */ + virtual ~Cell(); + + /** + * Return the index + */ + unsigned GetCellId() const; +}; + +#endif // CELL_HPP_ diff --git a/examples/cells/src/cpp/mesh/AbstractMesh.cpp b/examples/cells/src/cpp/mesh/AbstractMesh.cpp index 776d784..5bffc2b 100644 --- a/examples/cells/src/cpp/mesh/AbstractMesh.cpp +++ b/examples/cells/src/cpp/mesh/AbstractMesh.cpp @@ -1,4 +1,5 @@ #include "AbstractMesh.hpp" +#include "Node.hpp" template AbstractMesh::AbstractMesh() : mIndex(0) @@ -22,10 +23,10 @@ void AbstractMesh::SetIndex(unsigned index) mIndex = index; } -// template -// void AbstractMesh::AddVertex(Node vertex) -// { -// } +template +void AbstractMesh::AddNode(Node node) +{ +} template class AbstractMesh<2, 2>; template class AbstractMesh<3, 3>; diff --git a/examples/cells/src/cpp/mesh/AbstractMesh.hpp b/examples/cells/src/cpp/mesh/AbstractMesh.hpp index 4867bc2..057a896 100644 --- a/examples/cells/src/cpp/mesh/AbstractMesh.hpp +++ b/examples/cells/src/cpp/mesh/AbstractMesh.hpp @@ -1,6 +1,8 @@ #ifndef _ABSTRACT_MESH_HPP #define _ABSTRACT_MESH_HPP +#include "Node.hpp" + /** * A mesh in SPACE_DIM space with ELEMENT_DIM dimensional elements */ @@ -35,9 +37,9 @@ class AbstractMesh void SetIndex(unsigned index); /** - * Add a vertex to the mesh + * Add a node to the mesh */ - // void AddVertex(Node vertex); + void AddNode(Node node); /** * Scale the mesh by a factor diff --git a/examples/cells/src/cpp/mesh/MeshFactory.cpp b/examples/cells/src/cpp/mesh/MeshFactory.cpp index 92138d6..06f6400 100644 --- a/examples/cells/src/cpp/mesh/MeshFactory.cpp +++ b/examples/cells/src/cpp/mesh/MeshFactory.cpp @@ -1,6 +1,5 @@ -#include "PottsMesh.hpp" - #include "MeshFactory.hpp" +#include "PottsMesh.hpp" template MeshFactory::MeshFactory(): mMeshGen() diff --git a/examples/cells/src/cpp/mesh/Node.cpp b/examples/cells/src/cpp/mesh/Node.cpp new file mode 100644 index 0000000..b2d7917 --- /dev/null +++ b/examples/cells/src/cpp/mesh/Node.cpp @@ -0,0 +1,34 @@ +#include "Node.hpp" + +#include +#include + +template +Node::Node() + : Node({0.0, 0.0, 0.0}) +{ +} + +template +Node::Node(std::vector coords) + : mIndex(0), mLocation() +{ + for (unsigned i = 0; i < SPACE_DIM; ++i) + { + mLocation[i] = coords[i]; + } +} + +template +Node::~Node() +{ +} + +template +unsigned Node::GetIndex() const +{ + return mIndex; +} + +template class Node<2>; +template class Node<3>; diff --git a/examples/cells/src/cpp/mesh/Node.hpp b/examples/cells/src/cpp/mesh/Node.hpp new file mode 100644 index 0000000..5c95820 --- /dev/null +++ b/examples/cells/src/cpp/mesh/Node.hpp @@ -0,0 +1,46 @@ +#ifndef _NODE_HPP_ +#define _NODE_HPP_ + +#include +#include + +/** + * A node in a mesh + */ +template +class Node +{ +private: + /** + * Node index + */ + unsigned mIndex; + + /** + * Node location + */ + std::array mLocation; + +public: + /** + * Default Constructor + */ + Node(); + + /** + * Constructor with coordinates + */ + Node(std::vector coords); + + /** + * Destructor + */ + ~Node(); + + /** + * Return the index + */ + unsigned GetIndex() const; +}; + +#endif //_NODE_HPP_ diff --git a/examples/cells/src/cpp/mesh/PottsMesh.cpp b/examples/cells/src/cpp/mesh/PottsMesh.cpp index ba676be..1c3c456 100644 --- a/examples/cells/src/cpp/mesh/PottsMesh.cpp +++ b/examples/cells/src/cpp/mesh/PottsMesh.cpp @@ -1,4 +1,3 @@ -#include "AbstractMesh.hpp" #include "PottsMesh.hpp" template diff --git a/examples/cells/src/cpp/visualization/Scene.cpp b/examples/cells/src/cpp/visualization/Scene.cpp index 93b3d6e..97a29aa 100644 --- a/examples/cells/src/cpp/visualization/Scene.cpp +++ b/examples/cells/src/cpp/visualization/Scene.cpp @@ -1,10 +1,18 @@ #include "Scene.hpp" +#include +#include +#include +#include + template Scene::Scene() - : mpRenderer(vtkSmartPointer::New()) + : mpRenderer(vtkSmartPointer::New()), + mpRenderWindow(vtkSmartPointer::New()) { - mpRenderer->SetBackground(0.0, 0.0, 0.0); + mpRenderer->SetBackground(1.0, 1.0, 1.0); + mpRenderWindow->AddRenderer(mpRenderer); + mpRenderWindow->SetSize(800.0, 600.0); } template diff --git a/examples/cells/src/cpp/visualization/Scene.hpp b/examples/cells/src/cpp/visualization/Scene.hpp index fcd8d10..e633960 100644 --- a/examples/cells/src/cpp/visualization/Scene.hpp +++ b/examples/cells/src/cpp/visualization/Scene.hpp @@ -2,7 +2,8 @@ #define SCENE_HPP_ #include -#include +#include +#include #include VTK_MODULE_INIT(vtkRenderingOpenGL2); @@ -12,6 +13,7 @@ template class Scene { vtkSmartPointer mpRenderer; + vtkSmartPointer mpRenderWindow; public: Scene(); diff --git a/examples/cells/src/py/pycells/__init__.py b/examples/cells/src/py/pycells/__init__.py index 266fdcd..00c0f38 100644 --- a/examples/cells/src/py/pycells/__init__.py +++ b/examples/cells/src/py/pycells/__init__.py @@ -1 +1,30 @@ -from ._pycells_lib import * +from ._pycells_lib import ( + MeshFactory_PottsMesh_2, + MeshFactory_PottsMesh_3, + PottsMesh_2, + PottsMesh_3, + Scene_2, + Scene_3, +) +from ._syntax import TemplateClassDict + +MeshFactory = TemplateClassDict( + { + ("PottsMesh", "2"): MeshFactory_PottsMesh_2, + ("PottsMesh", "3"): MeshFactory_PottsMesh_3, + } +) + +PottsMesh = TemplateClassDict( + { + ("2",): PottsMesh_2, + ("3",): PottsMesh_3, + } +) + +Scene = TemplateClassDict( + { + ("2",): Scene_2, + ("3",): Scene_3, + } +) diff --git a/examples/cells/src/py/pycells/mesh/__init__.py b/examples/cells/src/py/pycells/mesh/__init__.py deleted file mode 100644 index 7f9ab87..0000000 --- a/examples/cells/src/py/pycells/mesh/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from pycells._pycells_lib import ConcreteMesh_2 diff --git a/examples/cells/tests/test_cells.py b/examples/cells/tests/test_cells.py index 133cf84..12ebe44 100644 --- a/examples/cells/tests/test_cells.py +++ b/examples/cells/tests/test_cells.py @@ -1,10 +1,16 @@ import unittest +import vtk +from pycells import Scene_2 + class TestCells(unittest.TestCase): - def testCells(self): - pass + def testVtkCaster(self): + scene = Scene_2() + renderer = scene.GetRenderer() + self.assertIsNotNone(renderer) + if __name__ == "__main__": unittest.main() diff --git a/examples/cells/extern/meshgen/CMakeLists.txt b/examples/cells/thirdparty/meshgen/CMakeLists.txt similarity index 100% rename from examples/cells/extern/meshgen/CMakeLists.txt rename to examples/cells/thirdparty/meshgen/CMakeLists.txt diff --git a/examples/cells/extern/meshgen/MeshGen.cpp b/examples/cells/thirdparty/meshgen/MeshGen.cpp similarity index 100% rename from examples/cells/extern/meshgen/MeshGen.cpp rename to examples/cells/thirdparty/meshgen/MeshGen.cpp diff --git a/examples/cells/extern/meshgen/MeshGen.hpp b/examples/cells/thirdparty/meshgen/MeshGen.hpp similarity index 100% rename from examples/cells/extern/meshgen/MeshGen.hpp rename to examples/cells/thirdparty/meshgen/MeshGen.hpp diff --git a/examples/cells/thirdparty/smtk/LICENSE.txt b/examples/cells/thirdparty/smtk/LICENSE.txt new file mode 100644 index 0000000..0f0d7b8 --- /dev/null +++ b/examples/cells/thirdparty/smtk/LICENSE.txt @@ -0,0 +1,38 @@ +SMTK License Version 1.0 +======================================================================== +Copyright (c) 2012 Kitware Inc. 28 Corporate Drive +Clifton Park, NY, 12065, USA. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of Kitware nor the names of any contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The following files and directories come from third parties. Check the +contents of these for details on the specifics of their respective +licenses. +- - - - - - - - - - - - - - - - - - - - - - - - +Thirdparty/pugixml +Thirdparty/PyYaml +Thidpary/rtvl +CMake/FindDocutils.cmake diff --git a/examples/cells/thirdparty/smtk/PybindVTKTypeCaster.h b/examples/cells/thirdparty/smtk/PybindVTKTypeCaster.h new file mode 100644 index 0000000..62218dc --- /dev/null +++ b/examples/cells/thirdparty/smtk/PybindVTKTypeCaster.h @@ -0,0 +1,139 @@ +//========================================================================= +// Copyright (c) Kitware, Inc. +// All rights reserved. +// See LICENSE.txt for details. +// +// This software is distributed WITHOUT ANY WARRANTY; without even +// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the above copyright notice for more information. +//========================================================================= + +#ifndef pybind_extension_vtk_source_VTKTypeCaster_h +#define pybind_extension_vtk_source_VTKTypeCaster_h + +#include +#include + +#include + +#include "vtkNew.h" +#include "vtkObjectBase.h" +#include "vtkPythonUtil.h" +#include "vtkSmartPointer.h" + +namespace pybind11 +{ +namespace detail +{ + +/// Thanks to Eric Cosineau, who figured out a more general means of casting +/// to/from VTK's python wrappings: +/// https://github.com/EricCousineau-TRI/repro/blob/b9e02d6d5a71f6315b80759ba1628b4bb383c0b8/python/vtk_pybind/vtk_pybind.h + +/// Direct access to VTK class. +template +struct type_caster::value> > +{ +private: + Class* value; + +public: + static constexpr auto name = _(); + + static handle cast(const Class* src, return_value_policy policy, handle /*parent*/) + { + if (!src) + return none().release(); + if (policy == return_value_policy::take_ownership) + { + throw cast_error("vtk_pybind: `take_ownership` does not make sense in VTK?"); + } + if (policy == return_value_policy::copy) + { + throw cast_error("vtk_pybind: `copy` does not make sense in VTK?"); + } + return vtkPythonUtil::GetObjectFromPointer(const_cast(src)); + } + + static handle cast(const Class& src, return_value_policy policy, handle parent) + { + return cast(&src, policy, parent); + } + + operator Class*() { return value; } + operator Class&() { return *value; } + // Does this even make sense in VTK? + operator Class &&() && { return std::move(*value); } + + template + using cast_op_type = pybind11::detail::movable_cast_op_type; + + bool load(handle src, bool /* convert */) + { + value = dynamic_cast( + vtkPythonUtil::GetPointerFromObject(src.ptr(), type_id().c_str())); + return value != nullptr; + } +}; + +/// VTK Pointer-like object - may be non-copyable. +template +struct vtk_ptr_cast_only +{ +protected: + using Class = intrinsic_t())>; + using value_caster_type = type_caster; + +public: + static constexpr auto name = _(); + static handle cast(const Ptr& ptr, return_value_policy policy, handle parent) + { + return value_caster_type::cast(*ptr, policy, parent); + ; + } +}; + +/// VTK Pointer-like object - copyable / movable. +template +struct vtk_ptr_cast_and_load : public vtk_ptr_cast_only +{ +private: + Ptr value; + // N.B. Can't easily access base versions... + using Class = intrinsic_t())>; + using value_caster_type = type_caster; + +public: + operator Ptr&() { return value; } + // Does this even make sense in VTK? + operator Ptr &&() && { return std::move(value); } + + template + using cast_op_type = pybind11::detail::movable_cast_op_type; + + bool load(handle src, bool convert) + { + value_caster_type value_caster; + if (!value_caster.load(src, convert)) + { + return false; + } + value = Ptr(value_caster.operator Class*()); + return true; + } +}; + +template +struct type_caster > : public vtk_ptr_cast_and_load > +{ +}; + +template +struct type_caster > : public vtk_ptr_cast_only > +{ +}; + +} // namespace detail +} // namespace pybind11 + +#endif From aa3ea5a4f5eec795c66f27cf3dea758db579ce20 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 30 Oct 2024 15:56:41 +0000 Subject: [PATCH 08/31] #20 Test cells example --- .github/workflows/build-and-test.yml | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index bbd4259..5a405e7 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -23,10 +23,18 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - name: Install build dependencies + - name: Install system dependencies run: | - sudo apt-get update - sudo apt-get install cmake + sudo apt-get update && \ + sudo apt-get install \ + cmake \ + libpetsc-real3.15 \ + libpetsc-real3.15-dbg \ + libpetsc-real3.15-dev \ + libvtk9-dev \ + python3-petsc4py-real3.15 \ + python3-vtk9 \ + vtk9 - name: Setup Python ${{ matrix.python-version }} uses: actions/setup-python@v5 @@ -61,7 +69,7 @@ jobs: cd examples/shapes cat cppwg.log | grep "Unknown class" - - name: Build Python module + - name: Build shapes example run: | cd examples/shapes mkdir build @@ -69,8 +77,16 @@ jobs: cmake .. make -j $(nproc) - - name: Test built module + - name: Build cells example + run: | + pip install --user --no-cache -v examples/cells + + - name: Test shapes example run: | cd examples/shapes/build python -m unittest test_functions.py python -m unittest test_classes.py + + - name: Test cells example + run: | + python -m unittest discover examples/cells/tests From 986d91caec4c71ec95406d49d0c2512b9a7ca11e Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Thu, 31 Oct 2024 16:23:20 +0000 Subject: [PATCH 09/31] #20 Add PETSc utils to cell example --- .github/workflows/build-and-test.yml | 3 + examples/cells/CMakeLists.txt | 58 +++-- examples/cells/cmake/FindPETSc.cmake | 199 ++++++++++++++++++ examples/cells/dynamic/config.yaml | 6 +- .../wrapper_header_collection.cppwg.hpp | 1 + examples/cells/setup.cfg | 6 +- examples/cells/src/cpp/mesh/MeshFactory.cpp | 14 +- examples/cells/src/cpp/mesh/MeshFactory.hpp | 10 +- examples/cells/src/cpp/utils/PetscUtils.cpp | 57 +++++ examples/cells/src/cpp/utils/PetscUtils.hpp | 24 +++ .../cells/src/cpp/visualization/Scene.hpp | 1 - examples/cells/src/py/pycells/__init__.py | 1 + .../cells/thirdparty/meshgen/CMakeLists.txt | 6 - examples/cells/thirdparty/meshgen/MeshGen.cpp | 16 -- examples/cells/thirdparty/meshgen/MeshGen.hpp | 24 --- 15 files changed, 335 insertions(+), 91 deletions(-) create mode 100644 examples/cells/cmake/FindPETSc.cmake create mode 100644 examples/cells/src/cpp/utils/PetscUtils.cpp create mode 100644 examples/cells/src/cpp/utils/PetscUtils.hpp delete mode 100644 examples/cells/thirdparty/meshgen/CMakeLists.txt delete mode 100644 examples/cells/thirdparty/meshgen/MeshGen.cpp delete mode 100644 examples/cells/thirdparty/meshgen/MeshGen.hpp diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 5a405e7..ef76143 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -32,6 +32,9 @@ jobs: libpetsc-real3.15-dbg \ libpetsc-real3.15-dev \ libvtk9-dev \ + mpi-default-bin \ + mpi-default-dev \ + python3-mpi4py \ python3-petsc4py-real3.15 \ python3-vtk9 \ vtk9 diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index b07b884..3fc0585 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -4,41 +4,27 @@ project(pycells LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +# Find Python +find_package(Python3 REQUIRED COMPONENTS Interpreter Development) + # Find pybind11 find_package(pybind11 CONFIG REQUIRED) # Find VTK -find_package(VTK 7.1 - REQUIRED - COMPONENTS - vtkCommonCore - vtkCommonDataModel - vtkFiltersCore - vtkFiltersGeneral - vtkFiltersGeneric - vtkFiltersGeometry - vtkFiltersModeling - vtkFiltersProgrammable - vtkFiltersSources - vtkFiltersVerdict - vtkInteractionStyle - vtkIOCore - vtkIOGeometry - vtkIOImage - vtkIOLegacy - vtkIOMovie - vtkIOParallelXML - vtkIOXML - vtkPython - vtkRenderingAnnotation - vtkRenderingCore - vtkRenderingFreeType - vtkRenderingOpenGL2 - vtkWrappingPythonCore +find_package(VTK REQUIRED COMPONENTS + vtkCommonCore + vtkRenderingCore + vtkRenderingOpenGL2 + vtkWrappingPythonCore ) -# Add external library -add_subdirectory(thirdparty/meshgen meshgen_build) +# Find PETSc +find_package(PETSc REQUIRED) + +# Find MPI +find_package(MPI REQUIRED COMPONENTS CXX) # Set up pycells module file(GLOB SOURCES @@ -57,12 +43,22 @@ target_include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/cell ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/utils ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/visualization - ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/meshgen ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/smtk ${VTK_INCLUDE_DIRS} + ${PETSC_INCLUDES} + ${MPI_C_INCLUDE_PATH} + ${MPI_CXX_INCLUDE_PATH} ) -target_link_libraries(_pycells_lib PUBLIC meshgen::meshgen ${VTK_LIBRARIES}) +target_link_libraries( + _pycells_lib + PUBLIC + ${VTK_LIBRARIES} + ${PETSC_LIBRARIES} + ${MPI_C_LIBRARIES} + ${MPI_CXX_LIBRARIES} +) install(TARGETS _pycells_lib LIBRARY DESTINATION pycells) diff --git a/examples/cells/cmake/FindPETSc.cmake b/examples/cells/cmake/FindPETSc.cmake new file mode 100644 index 0000000..b2c7158 --- /dev/null +++ b/examples/cells/cmake/FindPETSc.cmake @@ -0,0 +1,199 @@ +# This file is modified from https://github.com/jedbrown/cmake-modules.git +# (BSD-2-Clause License) +# +# Find PETSc; once done, this will define: +# PETSC_FOUND - system has PETSc +# PETSC_INCLUDES - the PETSc include directories +# PETSC_LIBRARIES - link these to use PETSc +# PETSC_VERSION - version string (MAJOR.MINOR.SUBMINOR) +# +# Usage: +# find_package(PETSc) +# +# Setting these changes the behavior of the search +# PETSC_DIR - directory in which PETSc resides +# PETSC_ARCH - build architecture +# +# Redistribution and use is allowed according to the terms of the BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. +# + +# Ubuntu uses versioned paths e.g /usr/lib/petscdir/petsc3.15/x86_64-linux-gnu-real +file(GLOB ubuntu_paths "/usr/lib/petscdir/*") + +find_path( + PETSC_DIR + include/petsc.h + HINTS ENV PETSC_DIR + PATHS /usr/lib/petsc ${deb_paths} + DOC "PETSc Directory" +) + +if(PETSC_DIR AND NOT PETSC_ARCH) + foreach(_arch $ENV{PETSC_ARCH} x86_64-linux-gnu-real x86_64-linux-gnu-real-debug) + find_path( + petscconf + petscconf.h + HINTS ${PETSC_DIR} + PATH_SUFFIXES ${_arch}/include + NO_DEFAULT_PATH + ) + if(petscconf) + set(PETSC_ARCH "${_arch}" CACHE STRING "PETSc build architecture") + break() + endif() + endforeach() +endif() + +if(EXISTS "${PETSC_DIR}/${PETSC_ARCH}/lib/petsc/conf/petscvariables") + set(petsc_conf_rules "${PETSC_DIR}/lib/petsc/conf/rules") + set(petsc_conf_variables "${PETSC_DIR}/lib/petsc/conf/variables") +else() + message(SEND_ERROR "The pair PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} do not specify a valid PETSc installation") +endif() + +# Get the PETSc version +if(EXISTS "${PETSC_DIR}/include/petscversion.h") + file(STRINGS "${PETSC_DIR}/include/petscversion.h" vstrings REGEX "#define PETSC_VERSION_(RELEASE|MAJOR|MINOR|SUBMINOR|PATCH) ") + foreach(line ${vstrings}) + string(REGEX REPLACE " +" ";" fields ${line}) # break line into three fields (the first is always "#define") + list(GET fields 1 var) + list(GET fields 2 val) + set(${var} ${val}) + endforeach() + + set(vstring "${PETSC_VERSION_MAJOR}.${PETSC_VERSION_MINOR}.${PETSC_VERSION_SUBMINOR}") + + if(PETSC_VERSION_RELEASE) + if($(PETSC_VERSION_PATCH) GREATER 0) + set(vstring "${vstring}p${PETSC_VERSION_PATCH}") + endif() + else() + # make dev version compare higher than any patch level of a released version + set(vstring "${vstring}.99") + endif() + set(PETSC_VERSION "${vstring}" CACHE INTERNAL "PETSc version") +else() + message(SEND_ERROR "PETSC_DIR cannot be used, ${PETSC_DIR}/include/petscversion.h does not exist") +endif() + +# A temporary makefile to probe the PETSc configuration +set(ENV{PETSC_DIR} "${PETSC_DIR}") +set(ENV{PETSC_ARCH} "${PETSC_ARCH}") +set(petsc_config_makefile "${PROJECT_BINARY_DIR}/Makefile.petsc") +file(WRITE "${petsc_config_makefile}" +"## This file was autogenerated by FindPETSc.cmake +# PETSC_DIR = ${PETSC_DIR} +# PETSC_ARCH = ${PETSC_ARCH} +include ${petsc_conf_rules} +include ${petsc_conf_variables} +show : +\t-@echo -n \${\${VARIABLE}} +") + +find_program(MAKE_EXECUTABLE NAMES make gmake) + +macro(PETSC_GET_VARIABLE name var) + set(${var} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE) + execute_process( + COMMAND ${MAKE_EXECUTABLE} --no-print-directory -f ${petsc_config_makefile} show VARIABLE=${name} + OUTPUT_VARIABLE ${var} + ) +endmacro() + +# Extract include paths +petsc_get_variable(PETSC_CCPPFLAGS petsc_ccpp_flags) + +string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${petsc_ccpp_flags}") + set(_incs_found "") + foreach(token ${_all_tokens}) + string(REGEX REPLACE "^-I" "" token ${token}) + string(REGEX REPLACE "//" "/" token ${token}) + if(EXISTS ${token}) + list(APPEND _incs_found ${token}) + else() + message(STATUS "Include directory ${token} does not exist") + endif() + endforeach(token) + list(REMOVE_DUPLICATES _incs_found) +set(PETSC_INCLUDES_ALL "${_incs_found}") + +# Extract libraries +petsc_get_variable(PETSC_LIB_DIR petsc_lib_dir) +message(STATUS "petsc_lib_dir ${petsc_lib_dir}") + +macro(PETSC_FIND_LIBRARY suffix name) + set(PETSC_LIBRARY_${suffix} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE) # Clear any stale value, if we got here, we need to find it again + find_library(PETSC_LIBRARY_${suffix} NAMES ${name} HINTS ${petsc_lib_dir} NO_DEFAULT_PATH) + set(PETSC_LIBRARIES_${suffix} "${PETSC_LIBRARY_${suffix}}") + mark_as_advanced(PETSC_LIBRARY_${suffix}) +endmacro() + +# Look for petscvec first, if it doesn't exist, we must be using single-library +petsc_find_library(VEC petscvec) +if(PETSC_LIBRARY_VEC) + petsc_find_library(SYS petscsys) + petsc_find_library(MAT petscmat) + petsc_find_library(DM petscdm) + petsc_find_library(KSP petscksp) + petsc_find_library(SNES petscsnes) + petsc_find_library(TS petscts) + set(PETSC_LIBRARIES_ALL + ${PETSC_LIBRARY_SYS} + ${PETSC_LIBRARY_VEC} + ${PETSC_LIBRARY_MAT} + ${PETSC_LIBRARY_DM} + ${PETSC_LIBRARY_KSP} + ${PETSC_LIBRARY_SNES} + ${PETSC_LIBRARY_TS} + ) +else() + set(PETSC_LIBRARY_VEC "NOTFOUND" CACHE INTERNAL "Cleared" FORCE) + petsc_find_library(SINGLE petsc) + # Ubuntu uses _real and _complex extensions + if(NOT PETSC_LIBRARY_SINGLE) + petsc_find_library(SINGLE petsc_real) + endif() + if(NOT PETSC_LIBRARY_SINGLE) + petsc_find_library(SINGLE petsc_complex) + endif() + set(PETSC_LIBRARIES_SYS "${PETSC_LIBRARY_SINGLE}") + set(PETSC_LIBRARIES_VEC "${PETSC_LIBRARY_SINGLE}") + set(PETSC_LIBRARIES_MAT "${PETSC_LIBRARY_SINGLE}") + set(PETSC_LIBRARIES_DM "${PETSC_LIBRARY_SINGLE}") + set(PETSC_LIBRARIES_KSP "${PETSC_LIBRARY_SINGLE}") + set(PETSC_LIBRARIES_SNES "${PETSC_LIBRARY_SINGLE}") + set(PETSC_LIBRARIES_ALL "${PETSC_LIBRARY_SINGLE}") +endif() + +if(PETSC_LIBRARY_TS) + message(STATUS "Recognized PETSc install with separate libraries for each package") +else() + message(STATUS "Recognized PETSc install with single library for all packages") +endif() + +# We do an out-of-source build so __FILE__ will be an absolute path, hence __INSDIR__ is superfluous +set(PETSC_DEFINITIONS "-D__INSDIR__=" CACHE STRING "PETSc definitions" FORCE) + +# Sometimes this can be used to assist FindMPI.cmake +petsc_get_variable(PCC petsc_cc) +petsc_get_variable(MPIEXEC petsc_mpiexec) +set(PETSC_COMPILER ${petsc_cc} CACHE FILEPATH "PETSc compiler" FORCE) +set(PETSC_MPIEXEC ${petsc_mpiexec} CACHE FILEPATH "Executable for running PETSc MPI programs" FORCE) + +set(PETSC_INCLUDES ${PETSC_INCLUDES_ALL} CACHE STRING "PETSc include path" FORCE) +set(PETSC_LIBRARIES ${PETSC_LIBRARIES_ALL} CACHE STRING "PETSc libraries" FORCE) +# Note that we have forced values for all these choices. If you +# change these, you are telling the system to trust you that they +# work. It is likely that you will end up with a broken build. +mark_as_advanced(PETSC_INCLUDES PETSC_LIBRARIES PETSC_COMPILER PETSC_DEFINITIONS PETSC_MPIEXEC) + +file(REMOVE ${petsc_config_makefile}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + PETSc + REQUIRED_VARS PETSC_INCLUDES PETSC_LIBRARIES + VERSION_VAR PETSC_VERSION + FAIL_MESSAGE "PETSc could not be found. Be sure to set PETSC_DIR and PETSC_ARCH." +) diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index b699112..a4149fe 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -26,6 +26,7 @@ modules: source_locations: - cell - mesh + - utils - visualization classes: @@ -39,10 +40,13 @@ modules: - PottsMesh.hpp template_substitutions: - signature: - replacement: [['PottsMesh<2>'], ['PottsMesh<3>']] + replacement: [["PottsMesh<2>"], ["PottsMesh<3>"]] - name: Node - name: PottsMesh + # utils + - name: PetscUtils + # visualization - name: Scene source_includes: diff --git a/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp index dd18042..1db0a7d 100644 --- a/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp +++ b/examples/cells/dynamic/wrappers/wrapper_header_collection.cppwg.hpp @@ -8,6 +8,7 @@ #include "Cell.hpp" #include "MeshFactory.hpp" #include "Node.hpp" +#include "PetscUtils.hpp" #include "PottsMesh.hpp" #include "Scene.hpp" diff --git a/examples/cells/setup.cfg b/examples/cells/setup.cfg index 274ff13..7c102b8 100644 --- a/examples/cells/setup.cfg +++ b/examples/cells/setup.cfg @@ -4,7 +4,7 @@ version = 0.0.1 [options] zip_safe = False -python_requires = >=3.9 +python_requires = >=3.8 packages = find: package_dir = =src/py @@ -13,9 +13,5 @@ include_package_data = True [options.packages.find] where = src/py -[options.package_data] -mypkg = - *.so - [build_cmake] source_dir = . diff --git a/examples/cells/src/cpp/mesh/MeshFactory.cpp b/examples/cells/src/cpp/mesh/MeshFactory.cpp index 06f6400..4602d8a 100644 --- a/examples/cells/src/cpp/mesh/MeshFactory.cpp +++ b/examples/cells/src/cpp/mesh/MeshFactory.cpp @@ -1,8 +1,10 @@ #include "MeshFactory.hpp" #include "PottsMesh.hpp" +#include + template -MeshFactory::MeshFactory(): mMeshGen() +MeshFactory::MeshFactory() { } @@ -11,5 +13,11 @@ MeshFactory::~MeshFactory() { } -template class MeshFactory >; -template class MeshFactory >; +template +std::shared_ptr MeshFactory::generateMesh() +{ + return std::make_shared(); +} + +template class MeshFactory>; +template class MeshFactory>; diff --git a/examples/cells/src/cpp/mesh/MeshFactory.hpp b/examples/cells/src/cpp/mesh/MeshFactory.hpp index 1f414cd..14a20a2 100644 --- a/examples/cells/src/cpp/mesh/MeshFactory.hpp +++ b/examples/cells/src/cpp/mesh/MeshFactory.hpp @@ -1,7 +1,7 @@ #ifndef _MESH_FACTORY_HPP #define _MESH_FACTORY_HPP -#include "MeshGen.hpp" +#include /** * A concrete mesh implementation @@ -9,9 +9,6 @@ template class MeshFactory { -private: - MeshGen mMeshGen; - public: /** * Default Constructor @@ -22,6 +19,11 @@ class MeshFactory * Destructor */ ~MeshFactory(); + + /** + * Generate a mesh + */ + std::shared_ptr generateMesh(); }; #endif // _MESH_FACTORY_HPP diff --git a/examples/cells/src/cpp/utils/PetscUtils.cpp b/examples/cells/src/cpp/utils/PetscUtils.cpp new file mode 100644 index 0000000..4caf0e5 --- /dev/null +++ b/examples/cells/src/cpp/utils/PetscUtils.cpp @@ -0,0 +1,57 @@ +#include "PetscUtils.hpp" + +#include +#include +#include +#include +#include + +#include + +void PetscUtils::Initialise() +{ + if (!PetscUtils::IsInitialised()) + { + PetscInitialize(PETSC_NULL, PETSC_NULL, PETSC_NULL, PETSC_NULL); + } +} + +bool PetscUtils::IsInitialised() +{ + PetscBool isInitialised; + PetscInitialized(&isInitialised); + return (bool)isInitialised; +} + +int PetscUtils::GetSize() +{ + if (!PetscUtils::IsInitialised()) + { + return -1; + } + + PetscInt size; + MPI_Comm_size(PETSC_COMM_WORLD, &size); + return (unsigned)size; +} + +int PetscUtils::GetRank() +{ + if (!PetscUtils::IsInitialised()) + { + return -1; + } + + PetscInt rank; + MPI_Comm_rank(PETSC_COMM_WORLD, &rank); + return (unsigned)rank; +} + +Vec PetscUtils::CreateVec(int size) +{ + Vec vec; + VecCreate(PETSC_COMM_WORLD, &vec); + VecSetSizes(vec, PETSC_DECIDE, size); + VecSetType(vec, VECMPI); + return vec; +} diff --git a/examples/cells/src/cpp/utils/PetscUtils.hpp b/examples/cells/src/cpp/utils/PetscUtils.hpp new file mode 100644 index 0000000..d6e89bb --- /dev/null +++ b/examples/cells/src/cpp/utils/PetscUtils.hpp @@ -0,0 +1,24 @@ +#ifndef PETSCUTILS_HPP_ +#define PETSCUTILS_HPP_ + +#include +#include +#include +#include + +#include + +class PetscUtils +{ +public: + static void Initialise(); + + static bool IsInitialised(); + + static int GetSize(); + static int GetRank(); + + static Vec CreateVec(int size); +}; + +#endif // PETSCUTILS_HPP_ diff --git a/examples/cells/src/cpp/visualization/Scene.hpp b/examples/cells/src/cpp/visualization/Scene.hpp index e633960..6fe3296 100644 --- a/examples/cells/src/cpp/visualization/Scene.hpp +++ b/examples/cells/src/cpp/visualization/Scene.hpp @@ -7,7 +7,6 @@ #include VTK_MODULE_INIT(vtkRenderingOpenGL2); -VTK_MODULE_INIT(vtkRenderingFreeType); template class Scene diff --git a/examples/cells/src/py/pycells/__init__.py b/examples/cells/src/py/pycells/__init__.py index 00c0f38..c95aada 100644 --- a/examples/cells/src/py/pycells/__init__.py +++ b/examples/cells/src/py/pycells/__init__.py @@ -1,6 +1,7 @@ from ._pycells_lib import ( MeshFactory_PottsMesh_2, MeshFactory_PottsMesh_3, + PetscUtils, PottsMesh_2, PottsMesh_3, Scene_2, diff --git a/examples/cells/thirdparty/meshgen/CMakeLists.txt b/examples/cells/thirdparty/meshgen/CMakeLists.txt deleted file mode 100644 index 672572f..0000000 --- a/examples/cells/thirdparty/meshgen/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.8) -project(meshgen) - -add_library(meshgen MeshGen.cpp MeshGen.hpp) -target_include_directories(meshgen PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -add_library(meshgen::meshgen ALIAS meshgen) diff --git a/examples/cells/thirdparty/meshgen/MeshGen.cpp b/examples/cells/thirdparty/meshgen/MeshGen.cpp deleted file mode 100644 index e411abc..0000000 --- a/examples/cells/thirdparty/meshgen/MeshGen.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "MeshGen.hpp" - -MeshGen::MeshGen() -{ - // Constructor -} - -MeshGen::~MeshGen() -{ - // Destructor -} - -void MeshGen::generateMesh() -{ - // Generate a mesh -} diff --git a/examples/cells/thirdparty/meshgen/MeshGen.hpp b/examples/cells/thirdparty/meshgen/MeshGen.hpp deleted file mode 100644 index 0e71bf0..0000000 --- a/examples/cells/thirdparty/meshgen/MeshGen.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _MESHGEN_HPP -#define _MESHGEN_HPP - -class MeshGen -{ - -public: - /** - * Default Constructor - */ - MeshGen(); - - /** - * Destructor - */ - ~MeshGen(); - - /** - * Generate a mesh - */ - void generateMesh(); -}; - -#endif // _MESHGEN_HPP From dc71eb83de0da7b666226667503db8ea35b2aa31 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Thu, 31 Oct 2024 16:56:42 +0000 Subject: [PATCH 10/31] #20 Add PETSc typecaster to cell example --- .github/workflows/build-and-test.yml | 27 ++--------- .github/workflows/test-typecasters.yml | 43 ++++++++++++++++++ examples/cells/CMakeLists.txt | 9 ++-- examples/cells/cmake/FindPETSc.cmake | 6 --- examples/cells/dynamic/config.yaml | 4 +- .../wrappers/all/AbstractMesh_2_2.cppwg.cpp | 45 +++++++++++++++++++ .../wrappers/all/AbstractMesh_2_2.cppwg.hpp | 9 ++++ .../wrappers/all/AbstractMesh_3_3.cppwg.cpp | 45 +++++++++++++++++++ .../wrappers/all/AbstractMesh_3_3.cppwg.hpp | 9 ++++ .../cells/dynamic/wrappers/all/Cell.cppwg.cpp | 22 +++++++++ .../cells/dynamic/wrappers/all/Cell.cppwg.hpp | 9 ++++ .../all/MeshFactory_PottsMesh_2.cppwg.cpp | 23 ++++++++++ .../all/MeshFactory_PottsMesh_2.cppwg.hpp | 9 ++++ .../all/MeshFactory_PottsMesh_3.cppwg.cpp | 23 ++++++++++ .../all/MeshFactory_PottsMesh_3.cppwg.hpp | 9 ++++ .../dynamic/wrappers/all/Node_2.cppwg.cpp | 23 ++++++++++ .../dynamic/wrappers/all/Node_2.cppwg.hpp | 9 ++++ .../dynamic/wrappers/all/Node_3.cppwg.cpp | 23 ++++++++++ .../dynamic/wrappers/all/Node_3.cppwg.hpp | 9 ++++ .../dynamic/wrappers/all/PetscUtils.cppwg.cpp | 35 +++++++++++++++ .../dynamic/wrappers/all/PetscUtils.cppwg.hpp | 9 ++++ .../wrappers/all/PottsMesh_2.cppwg.cpp | 36 +++++++++++++++ .../wrappers/all/PottsMesh_2.cppwg.hpp | 9 ++++ .../wrappers/all/PottsMesh_3.cppwg.cpp | 36 +++++++++++++++ .../wrappers/all/PottsMesh_3.cppwg.hpp | 9 ++++ .../dynamic/wrappers/all/Scene_2.cppwg.cpp | 23 ++++++++++ .../dynamic/wrappers/all/Scene_2.cppwg.hpp | 9 ++++ .../dynamic/wrappers/all/Scene_3.cppwg.cpp | 23 ++++++++++ .../dynamic/wrappers/all/Scene_3.cppwg.hpp | 9 ++++ .../wrappers/all/_pycells_all.main.cppwg.cpp | 33 ++++++++++++++ .../cpp/typecasters/PybindPETScTypeCaster.hpp | 12 +++++ examples/cells/src/py/pycells/__init__.py | 2 +- 32 files changed, 566 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/test-typecasters.yml create mode 100644 examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/Cell.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/Cell.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/Node_2.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/Node_3.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/Scene_2.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/Scene_2.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/Scene_3.cppwg.cpp create mode 100644 examples/cells/dynamic/wrappers/all/Scene_3.cppwg.hpp create mode 100644 examples/cells/dynamic/wrappers/all/_pycells_all.main.cppwg.cpp create mode 100644 examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index ef76143..33972d3 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -26,18 +26,7 @@ jobs: - name: Install system dependencies run: | sudo apt-get update && \ - sudo apt-get install \ - cmake \ - libpetsc-real3.15 \ - libpetsc-real3.15-dbg \ - libpetsc-real3.15-dev \ - libvtk9-dev \ - mpi-default-bin \ - mpi-default-dev \ - python3-mpi4py \ - python3-petsc4py-real3.15 \ - python3-vtk9 \ - vtk9 + sudo apt-get install cmake - name: Setup Python ${{ matrix.python-version }} uses: actions/setup-python@v5 @@ -52,10 +41,10 @@ jobs: - name: Lint with flake8 run: python -m flake8 - - name: Test wrapper generation + - name: Test shapes example wrapper generation run: python -m unittest tests/test_wrapper_generation.py - - name: Generate new wrappers + - name: Regenerate shapes example wrappers run: | cd examples/shapes/wrapper rm -rf geometry math_funcs primitives @@ -67,7 +56,7 @@ jobs: --std c++17 \ --logfile cppwg.log - - name: Check for new classes + - name: Check shapes example for new classes run: | cd examples/shapes cat cppwg.log | grep "Unknown class" @@ -80,16 +69,8 @@ jobs: cmake .. make -j $(nproc) - - name: Build cells example - run: | - pip install --user --no-cache -v examples/cells - - name: Test shapes example run: | cd examples/shapes/build python -m unittest test_functions.py python -m unittest test_classes.py - - - name: Test cells example - run: | - python -m unittest discover examples/cells/tests diff --git a/.github/workflows/test-typecasters.yml b/.github/workflows/test-typecasters.yml new file mode 100644 index 0000000..96bb17b --- /dev/null +++ b/.github/workflows/test-typecasters.yml @@ -0,0 +1,43 @@ +name: test-typecasters + +on: + workflow_dispatch: + pull_request: + branches: + - "**" + +concurrency: + group: build-and-test-${{ github.ref }} + cancel-in-progress: true + +jobs: + build-and-test: + runs-on: ubuntu-22.04 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install system dependencies + run: | + sudo apt-get update && \ + sudo apt-get install \ + cmake \ + libpetsc-real3.15 \ + libpetsc-real3.15-dbg \ + libpetsc-real3.15-dev \ + libvtk9-dev \ + mpi-default-bin \ + mpi-default-dev \ + python3-mpi4py \ + python3-petsc4py-real3.15 \ + python3-vtk9 \ + vtk9 + + - name: Build cells example + run: | + pip install --user --no-cache -v examples/cells + + - name: Test cells example + run: | + python -m unittest discover examples/cells/tests diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 3fc0585..dd0081b 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -34,15 +34,16 @@ file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/**/*.hpp ) -pybind11_add_module(_pycells_lib ${SOURCES}) +pybind11_add_module(_pycells_all ${SOURCES}) target_include_directories( - _pycells_lib + _pycells_all PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/wrappers/lib ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/cell ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/typecasters ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/utils ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/visualization ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/smtk @@ -53,7 +54,7 @@ target_include_directories( ) target_link_libraries( - _pycells_lib + _pycells_all PUBLIC ${VTK_LIBRARIES} ${PETSC_LIBRARIES} @@ -61,4 +62,4 @@ target_link_libraries( ${MPI_CXX_LIBRARIES} ) -install(TARGETS _pycells_lib LIBRARY DESTINATION pycells) +install(TARGETS _pycells_all LIBRARY DESTINATION pycells) diff --git a/examples/cells/cmake/FindPETSc.cmake b/examples/cells/cmake/FindPETSc.cmake index b2c7158..857aed4 100644 --- a/examples/cells/cmake/FindPETSc.cmake +++ b/examples/cells/cmake/FindPETSc.cmake @@ -157,12 +157,6 @@ else() if(NOT PETSC_LIBRARY_SINGLE) petsc_find_library(SINGLE petsc_complex) endif() - set(PETSC_LIBRARIES_SYS "${PETSC_LIBRARY_SINGLE}") - set(PETSC_LIBRARIES_VEC "${PETSC_LIBRARY_SINGLE}") - set(PETSC_LIBRARIES_MAT "${PETSC_LIBRARY_SINGLE}") - set(PETSC_LIBRARIES_DM "${PETSC_LIBRARY_SINGLE}") - set(PETSC_LIBRARIES_KSP "${PETSC_LIBRARY_SINGLE}") - set(PETSC_LIBRARIES_SNES "${PETSC_LIBRARY_SINGLE}") set(PETSC_LIBRARIES_ALL "${PETSC_LIBRARY_SINGLE}") endif() diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index a4149fe..95ebcd3 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -22,7 +22,7 @@ template_substitutions: replacement: [[2, 2], [3, 3]] modules: - - name: lib + - name: all source_locations: - cell - mesh @@ -46,6 +46,8 @@ modules: # utils - name: PetscUtils + source_includes: + - PybindPETScTypeCaster.hpp # visualization - name: Scene diff --git a/examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.cpp b/examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.cpp new file mode 100644 index 0000000..ed81b5f --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.cpp @@ -0,0 +1,45 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include +#include "AbstractMesh.hpp" + +#include "AbstractMesh_2_2.cppwg.hpp" + +namespace py = pybind11; +typedef AbstractMesh<2, 2> AbstractMesh_2_2; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +class AbstractMesh_2_2_Overrides : public AbstractMesh_2_2 +{ +public: + using AbstractMesh_2_2::AbstractMesh; + void Scale(double const factor) override + { + PYBIND11_OVERRIDE_PURE( + void, + AbstractMesh_2_2, + Scale, + factor); + } +}; + +void register_AbstractMesh_2_2_class(py::module &m) +{ + py::class_>(m, "AbstractMesh_2_2") + .def(py::init<>()) + .def("GetIndex", + (unsigned int(AbstractMesh_2_2::*)() const) &AbstractMesh_2_2::GetIndex, + " ") + .def("SetIndex", + (void(AbstractMesh_2_2::*)(unsigned int)) &AbstractMesh_2_2::SetIndex, + " ", py::arg("index")) + .def("AddNode", + (void(AbstractMesh_2_2::*)(::Node<2>)) &AbstractMesh_2_2::AddNode, + " ", py::arg("node")) + .def("Scale", + (void(AbstractMesh_2_2::*)(double const)) &AbstractMesh_2_2::Scale, + " ", py::arg("factor")) + ; +} diff --git a/examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.hpp b/examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.hpp new file mode 100644 index 0000000..c1503c6 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/AbstractMesh_2_2.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef AbstractMesh_2_2_hpp__cppwg_wrapper +#define AbstractMesh_2_2_hpp__cppwg_wrapper + +#include + +void register_AbstractMesh_2_2_class(pybind11::module &m); +#endif // AbstractMesh_2_2_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.cpp b/examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.cpp new file mode 100644 index 0000000..c56583b --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.cpp @@ -0,0 +1,45 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include +#include "AbstractMesh.hpp" + +#include "AbstractMesh_3_3.cppwg.hpp" + +namespace py = pybind11; +typedef AbstractMesh<3, 3> AbstractMesh_3_3; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +class AbstractMesh_3_3_Overrides : public AbstractMesh_3_3 +{ +public: + using AbstractMesh_3_3::AbstractMesh; + void Scale(double const factor) override + { + PYBIND11_OVERRIDE_PURE( + void, + AbstractMesh_3_3, + Scale, + factor); + } +}; + +void register_AbstractMesh_3_3_class(py::module &m) +{ + py::class_>(m, "AbstractMesh_3_3") + .def(py::init<>()) + .def("GetIndex", + (unsigned int(AbstractMesh_3_3::*)() const) &AbstractMesh_3_3::GetIndex, + " ") + .def("SetIndex", + (void(AbstractMesh_3_3::*)(unsigned int)) &AbstractMesh_3_3::SetIndex, + " ", py::arg("index")) + .def("AddNode", + (void(AbstractMesh_3_3::*)(::Node<3>)) &AbstractMesh_3_3::AddNode, + " ", py::arg("node")) + .def("Scale", + (void(AbstractMesh_3_3::*)(double const)) &AbstractMesh_3_3::Scale, + " ", py::arg("factor")) + ; +} diff --git a/examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.hpp b/examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.hpp new file mode 100644 index 0000000..1b26ef7 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/AbstractMesh_3_3.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef AbstractMesh_3_3_hpp__cppwg_wrapper +#define AbstractMesh_3_3_hpp__cppwg_wrapper + +#include + +void register_AbstractMesh_3_3_class(pybind11::module &m); +#endif // AbstractMesh_3_3_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/Cell.cppwg.cpp b/examples/cells/dynamic/wrappers/all/Cell.cppwg.cpp new file mode 100644 index 0000000..f9ba7bb --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Cell.cppwg.cpp @@ -0,0 +1,22 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include +#include "Cell.hpp" + +#include "Cell.cppwg.hpp" + +namespace py = pybind11; +typedef Cell Cell; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_Cell_class(py::module &m) +{ + py::class_>(m, "Cell") + .def(py::init<>()) + .def("GetCellId", + (unsigned int(Cell::*)() const) &Cell::GetCellId, + " ") + ; +} diff --git a/examples/cells/dynamic/wrappers/all/Cell.cppwg.hpp b/examples/cells/dynamic/wrappers/all/Cell.cppwg.hpp new file mode 100644 index 0000000..ae3fe5b --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Cell.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef Cell_hpp__cppwg_wrapper +#define Cell_hpp__cppwg_wrapper + +#include + +void register_Cell_class(pybind11::module &m); +#endif // Cell_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.cpp b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.cpp new file mode 100644 index 0000000..3147611 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.cpp @@ -0,0 +1,23 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include "PottsMesh.hpp" +#include +#include "MeshFactory.hpp" + +#include "MeshFactory_PottsMesh_2.cppwg.hpp" + +namespace py = pybind11; +typedef MeshFactory> MeshFactory_PottsMesh_2; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_MeshFactory_PottsMesh_2_class(py::module &m) +{ + py::class_>(m, "MeshFactory_PottsMesh_2") + .def(py::init<>()) + .def("generateMesh", + (::std::shared_ptr>(MeshFactory_PottsMesh_2::*)()) &MeshFactory_PottsMesh_2::generateMesh, + " ") + ; +} diff --git a/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.hpp b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.hpp new file mode 100644 index 0000000..28dc4ad --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_2.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef MeshFactory_PottsMesh_2_hpp__cppwg_wrapper +#define MeshFactory_PottsMesh_2_hpp__cppwg_wrapper + +#include + +void register_MeshFactory_PottsMesh_2_class(pybind11::module &m); +#endif // MeshFactory_PottsMesh_2_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.cpp b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.cpp new file mode 100644 index 0000000..2a8b1dc --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.cpp @@ -0,0 +1,23 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include "PottsMesh.hpp" +#include +#include "MeshFactory.hpp" + +#include "MeshFactory_PottsMesh_3.cppwg.hpp" + +namespace py = pybind11; +typedef MeshFactory> MeshFactory_PottsMesh_3; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_MeshFactory_PottsMesh_3_class(py::module &m) +{ + py::class_>(m, "MeshFactory_PottsMesh_3") + .def(py::init<>()) + .def("generateMesh", + (::std::shared_ptr>(MeshFactory_PottsMesh_3::*)()) &MeshFactory_PottsMesh_3::generateMesh, + " ") + ; +} diff --git a/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.hpp b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.hpp new file mode 100644 index 0000000..eb7de7c --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/MeshFactory_PottsMesh_3.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef MeshFactory_PottsMesh_3_hpp__cppwg_wrapper +#define MeshFactory_PottsMesh_3_hpp__cppwg_wrapper + +#include + +void register_MeshFactory_PottsMesh_3_class(pybind11::module &m); +#endif // MeshFactory_PottsMesh_3_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp b/examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp new file mode 100644 index 0000000..074b342 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp @@ -0,0 +1,23 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include +#include "Node.hpp" + +#include "Node_2.cppwg.hpp" + +namespace py = pybind11; +typedef Node<2> Node_2; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_Node_2_class(py::module &m) +{ + py::class_>(m, "Node_2") + .def(py::init<>()) + .def(py::init<::std::vector>(), py::arg("coords")) + .def("GetIndex", + (unsigned int(Node_2::*)() const) &Node_2::GetIndex, + " ") + ; +} diff --git a/examples/cells/dynamic/wrappers/all/Node_2.cppwg.hpp b/examples/cells/dynamic/wrappers/all/Node_2.cppwg.hpp new file mode 100644 index 0000000..35b1aeb --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Node_2.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef Node_2_hpp__cppwg_wrapper +#define Node_2_hpp__cppwg_wrapper + +#include + +void register_Node_2_class(pybind11::module &m); +#endif // Node_2_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp b/examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp new file mode 100644 index 0000000..61b52b3 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp @@ -0,0 +1,23 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include +#include "Node.hpp" + +#include "Node_3.cppwg.hpp" + +namespace py = pybind11; +typedef Node<3> Node_3; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_Node_3_class(py::module &m) +{ + py::class_>(m, "Node_3") + .def(py::init<>()) + .def(py::init<::std::vector>(), py::arg("coords")) + .def("GetIndex", + (unsigned int(Node_3::*)() const) &Node_3::GetIndex, + " ") + ; +} diff --git a/examples/cells/dynamic/wrappers/all/Node_3.cppwg.hpp b/examples/cells/dynamic/wrappers/all/Node_3.cppwg.hpp new file mode 100644 index 0000000..0c2935d --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Node_3.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef Node_3_hpp__cppwg_wrapper +#define Node_3_hpp__cppwg_wrapper + +#include + +void register_Node_3_class(pybind11::module &m); +#endif // Node_3_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp b/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp new file mode 100644 index 0000000..ca9ef8b --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp @@ -0,0 +1,35 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include "PybindPETScTypeCaster.hpp" +#include +#include "PetscUtils.hpp" + +#include "PetscUtils.cppwg.hpp" + +namespace py = pybind11; +typedef PetscUtils PetscUtils; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_PetscUtils_class(py::module &m) +{ + py::class_>(m, "PetscUtils") + .def(py::init<>()) + .def_static("Initialise", + (void(*)()) &PetscUtils::Initialise, + " ") + .def_static("IsInitialised", + (bool(*)()) &PetscUtils::IsInitialised, + " ") + .def_static("GetSize", + (int(*)()) &PetscUtils::GetSize, + " ") + .def_static("GetRank", + (int(*)()) &PetscUtils::GetRank, + " ") + .def_static("CreateVec", + (::Vec(*)(int)) &PetscUtils::CreateVec, + " ", py::arg("size"), py::return_value_policy::reference) + ; +} diff --git a/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.hpp b/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.hpp new file mode 100644 index 0000000..3b30963 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef PetscUtils_hpp__cppwg_wrapper +#define PetscUtils_hpp__cppwg_wrapper + +#include + +void register_PetscUtils_class(pybind11::module &m); +#endif // PetscUtils_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.cpp b/examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.cpp new file mode 100644 index 0000000..a26f649 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.cpp @@ -0,0 +1,36 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include +#include "PottsMesh.hpp" + +#include "PottsMesh_2.cppwg.hpp" + +namespace py = pybind11; +typedef PottsMesh<2> PottsMesh_2; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +class PottsMesh_2_Overrides : public PottsMesh_2 +{ +public: + using PottsMesh_2::PottsMesh; + void Scale(double const factor) override + { + PYBIND11_OVERRIDE( + void, + PottsMesh_2, + Scale, + factor); + } +}; + +void register_PottsMesh_2_class(py::module &m) +{ + py::class_, AbstractMesh<2>>(m, "PottsMesh_2") + .def(py::init<>()) + .def("Scale", + (void(PottsMesh_2::*)(double const)) &PottsMesh_2::Scale, + " ", py::arg("factor")) + ; +} diff --git a/examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.hpp b/examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.hpp new file mode 100644 index 0000000..30615bd --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/PottsMesh_2.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef PottsMesh_2_hpp__cppwg_wrapper +#define PottsMesh_2_hpp__cppwg_wrapper + +#include + +void register_PottsMesh_2_class(pybind11::module &m); +#endif // PottsMesh_2_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.cpp b/examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.cpp new file mode 100644 index 0000000..9ac2287 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.cpp @@ -0,0 +1,36 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include +#include "PottsMesh.hpp" + +#include "PottsMesh_3.cppwg.hpp" + +namespace py = pybind11; +typedef PottsMesh<3> PottsMesh_3; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +class PottsMesh_3_Overrides : public PottsMesh_3 +{ +public: + using PottsMesh_3::PottsMesh; + void Scale(double const factor) override + { + PYBIND11_OVERRIDE( + void, + PottsMesh_3, + Scale, + factor); + } +}; + +void register_PottsMesh_3_class(py::module &m) +{ + py::class_, AbstractMesh<3>>(m, "PottsMesh_3") + .def(py::init<>()) + .def("Scale", + (void(PottsMesh_3::*)(double const)) &PottsMesh_3::Scale, + " ", py::arg("factor")) + ; +} diff --git a/examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.hpp b/examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.hpp new file mode 100644 index 0000000..51ee08f --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/PottsMesh_3.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef PottsMesh_3_hpp__cppwg_wrapper +#define PottsMesh_3_hpp__cppwg_wrapper + +#include + +void register_PottsMesh_3_class(pybind11::module &m); +#endif // PottsMesh_3_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/Scene_2.cppwg.cpp b/examples/cells/dynamic/wrappers/all/Scene_2.cppwg.cpp new file mode 100644 index 0000000..bccdd79 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Scene_2.cppwg.cpp @@ -0,0 +1,23 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include "PybindVTKTypeCaster.h" +#include +#include "Scene.hpp" + +#include "Scene_2.cppwg.hpp" + +namespace py = pybind11; +typedef Scene<2> Scene_2; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_Scene_2_class(py::module &m) +{ + py::class_>(m, "Scene_2") + .def(py::init<>()) + .def("GetRenderer", + (::vtkSmartPointer(Scene_2::*)()) &Scene_2::GetRenderer, + " ") + ; +} diff --git a/examples/cells/dynamic/wrappers/all/Scene_2.cppwg.hpp b/examples/cells/dynamic/wrappers/all/Scene_2.cppwg.hpp new file mode 100644 index 0000000..bfca3fc --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Scene_2.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef Scene_2_hpp__cppwg_wrapper +#define Scene_2_hpp__cppwg_wrapper + +#include + +void register_Scene_2_class(pybind11::module &m); +#endif // Scene_2_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/Scene_3.cppwg.cpp b/examples/cells/dynamic/wrappers/all/Scene_3.cppwg.cpp new file mode 100644 index 0000000..04b2d41 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Scene_3.cppwg.cpp @@ -0,0 +1,23 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include +#include "PybindVTKTypeCaster.h" +#include +#include "Scene.hpp" + +#include "Scene_3.cppwg.hpp" + +namespace py = pybind11; +typedef Scene<3> Scene_3; +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr); + +void register_Scene_3_class(py::module &m) +{ + py::class_>(m, "Scene_3") + .def(py::init<>()) + .def("GetRenderer", + (::vtkSmartPointer(Scene_3::*)()) &Scene_3::GetRenderer, + " ") + ; +} diff --git a/examples/cells/dynamic/wrappers/all/Scene_3.cppwg.hpp b/examples/cells/dynamic/wrappers/all/Scene_3.cppwg.hpp new file mode 100644 index 0000000..b9c477c --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/Scene_3.cppwg.hpp @@ -0,0 +1,9 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#ifndef Scene_3_hpp__cppwg_wrapper +#define Scene_3_hpp__cppwg_wrapper + +#include + +void register_Scene_3_class(pybind11::module &m); +#endif // Scene_3_hpp__cppwg_wrapper diff --git a/examples/cells/dynamic/wrappers/all/_pycells_all.main.cppwg.cpp b/examples/cells/dynamic/wrappers/all/_pycells_all.main.cppwg.cpp new file mode 100644 index 0000000..0bcd915 --- /dev/null +++ b/examples/cells/dynamic/wrappers/all/_pycells_all.main.cppwg.cpp @@ -0,0 +1,33 @@ +// This file is auto-generated by cppwg; manual changes will be overwritten. + +#include +#include "Cell.cppwg.hpp" +#include "Node_2.cppwg.hpp" +#include "Node_3.cppwg.hpp" +#include "AbstractMesh_2_2.cppwg.hpp" +#include "AbstractMesh_3_3.cppwg.hpp" +#include "PottsMesh_2.cppwg.hpp" +#include "PottsMesh_3.cppwg.hpp" +#include "MeshFactory_PottsMesh_2.cppwg.hpp" +#include "MeshFactory_PottsMesh_3.cppwg.hpp" +#include "PetscUtils.cppwg.hpp" +#include "Scene_2.cppwg.hpp" +#include "Scene_3.cppwg.hpp" + +namespace py = pybind11; + +PYBIND11_MODULE(_pycells_all, m) +{ + register_Cell_class(m); + register_Node_2_class(m); + register_Node_3_class(m); + register_AbstractMesh_2_2_class(m); + register_AbstractMesh_3_3_class(m); + register_PottsMesh_2_class(m); + register_PottsMesh_3_class(m); + register_MeshFactory_PottsMesh_2_class(m); + register_MeshFactory_PottsMesh_3_class(m); + register_PetscUtils_class(m); + register_Scene_2_class(m); + register_Scene_3_class(m); +} diff --git a/examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp b/examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp new file mode 100644 index 0000000..ad1b664 --- /dev/null +++ b/examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp @@ -0,0 +1,12 @@ +#ifndef PYBINDPETSCTYPECASTER_HPP_ +#define PYBINDPETSCTYPECASTER_HPP_ + +#include +#include + +#include + +PYBIND11_MAKE_OPAQUE(Mat); +PYBIND11_MAKE_OPAQUE(Vec); + +#endif // PYBINDPETSCTYPECASTER_HPP_ diff --git a/examples/cells/src/py/pycells/__init__.py b/examples/cells/src/py/pycells/__init__.py index c95aada..b092a21 100644 --- a/examples/cells/src/py/pycells/__init__.py +++ b/examples/cells/src/py/pycells/__init__.py @@ -1,4 +1,4 @@ -from ._pycells_lib import ( +from ._pycells_all import ( MeshFactory_PottsMesh_2, MeshFactory_PottsMesh_3, PetscUtils, From d6fc9c71f51469cd8bd1efb3584dc79c6e372adc Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Thu, 31 Oct 2024 17:23:27 +0000 Subject: [PATCH 11/31] #20 Test PETSc typecaster --- examples/cells/src/py/pycells/__init__.py | 7 +++++++ examples/cells/tests/test_cells.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/cells/src/py/pycells/__init__.py b/examples/cells/src/py/pycells/__init__.py index b092a21..cde2da7 100644 --- a/examples/cells/src/py/pycells/__init__.py +++ b/examples/cells/src/py/pycells/__init__.py @@ -29,3 +29,10 @@ ("3",): Scene_3, } ) + +__all__ = [ + "MeshFactory", + "PetscUtils", + "PottsMesh", + "Scene", +] diff --git a/examples/cells/tests/test_cells.py b/examples/cells/tests/test_cells.py index 12ebe44..484f86e 100644 --- a/examples/cells/tests/test_cells.py +++ b/examples/cells/tests/test_cells.py @@ -1,16 +1,22 @@ import unittest +import petsc4py import vtk -from pycells import Scene_2 +from pycells import PetscUtils, Scene class TestCells(unittest.TestCase): def testVtkCaster(self): - scene = Scene_2() + scene = Scene[2]() renderer = scene.GetRenderer() self.assertIsNotNone(renderer) + def testPetscCaster(self): + petsc4py.init() + vec = PetscUtils.CreateVec(10) + self.assertIsNotNone(vec) + if __name__ == "__main__": unittest.main() From e7a3e2aa922bde402a4e513663c764d547ed74b6 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Thu, 31 Oct 2024 22:46:14 +0000 Subject: [PATCH 12/31] #20 Add thirdparty caster_petsc --- examples/cells/CMakeLists.txt | 8 +- examples/cells/cmake/FindPETSc.cmake | 53 +- examples/cells/cmake/FindPETSc4py.cmake | 29 + examples/cells/dynamic/config.yaml | 2 +- .../dynamic/wrappers/all/PetscUtils.cppwg.cpp | 2 +- .../cpp/typecasters/PybindPETScTypeCaster.hpp | 12 - examples/cells/tests/test_cells.py | 7 +- examples/cells/thirdparty/dolfinx/COPYING | 680 ++++++++++++++++++ .../cells/thirdparty/dolfinx/COPYING.LESSER | 165 +++++ .../cells/thirdparty/dolfinx/caster_petsc.h | 82 +++ 10 files changed, 1006 insertions(+), 34 deletions(-) create mode 100644 examples/cells/cmake/FindPETSc4py.cmake delete mode 100644 examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp create mode 100644 examples/cells/thirdparty/dolfinx/COPYING create mode 100644 examples/cells/thirdparty/dolfinx/COPYING.LESSER create mode 100644 examples/cells/thirdparty/dolfinx/caster_petsc.h diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index dd0081b..c1fcb01 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16...3.22) -project(pycells LANGUAGES CXX) +project(pycells LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -23,6 +23,9 @@ find_package(VTK REQUIRED COMPONENTS # Find PETSc find_package(PETSc REQUIRED) +# Find PETSc4py +find_package(PETSc4py REQUIRED) + # Find MPI find_package(MPI REQUIRED COMPONENTS CXX) @@ -43,12 +46,13 @@ target_include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/cell ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/typecasters ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/utils ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/visualization + ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/dolfinx ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/smtk ${VTK_INCLUDE_DIRS} ${PETSC_INCLUDES} + ${PETSC4PY_INCLUDES} ${MPI_C_INCLUDE_PATH} ${MPI_CXX_INCLUDE_PATH} ) diff --git a/examples/cells/cmake/FindPETSc.cmake b/examples/cells/cmake/FindPETSc.cmake index 857aed4..456d5c0 100644 --- a/examples/cells/cmake/FindPETSc.cmake +++ b/examples/cells/cmake/FindPETSc.cmake @@ -1,22 +1,45 @@ -# This file is modified from https://github.com/jedbrown/cmake-modules.git -# (BSD-2-Clause License) +# - Try to find PETSc +# Once done this will define # -# Find PETSc; once done, this will define: -# PETSC_FOUND - system has PETSc -# PETSC_INCLUDES - the PETSc include directories -# PETSC_LIBRARIES - link these to use PETSc -# PETSC_VERSION - version string (MAJOR.MINOR.SUBMINOR) -# -# Usage: -# find_package(PETSc) +# PETSC_FOUND - system has PETSc +# PETSC_INCLUDES - the PETSc include directories +# PETSC_LIBRARIES - link these to use PETSc +# PETSC_VERSION - version string (MAJOR.MINOR.SUBMINOR) # # Setting these changes the behavior of the search -# PETSC_DIR - directory in which PETSc resides -# PETSC_ARCH - build architecture +# PETSC_DIR - directory in which PETSc resides +# PETSC_ARCH - build architecture +# +# Usage: find_package(PETSc) +# +#============================================================================= +# This file is based on https://github.com/jedbrown/cmake-modules.git +# (BSD 2-clause License) +# +# Copyright the authors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. # -# Redistribution and use is allowed according to the terms of the BSD license. -# For details see the accompanying COPYING-CMAKE-SCRIPTS file. +# * Redistributions in binary form must reproduce the above copyright notice, this +# list of conditions and the following disclaimer in the documentation and/or +# other materials provided with the distribution. # +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#============================================================================= # Ubuntu uses versioned paths e.g /usr/lib/petscdir/petsc3.15/x86_64-linux-gnu-real file(GLOB ubuntu_paths "/usr/lib/petscdir/*") @@ -189,5 +212,5 @@ find_package_handle_standard_args( PETSc REQUIRED_VARS PETSC_INCLUDES PETSC_LIBRARIES VERSION_VAR PETSC_VERSION - FAIL_MESSAGE "PETSc could not be found. Be sure to set PETSC_DIR and PETSC_ARCH." + FAIL_MESSAGE "PETSc could not be found. Be sure to set PETSC_DIR and PETSC_ARCH." ) diff --git a/examples/cells/cmake/FindPETSc4py.cmake b/examples/cells/cmake/FindPETSc4py.cmake new file mode 100644 index 0000000..82f9524 --- /dev/null +++ b/examples/cells/cmake/FindPETSc4py.cmake @@ -0,0 +1,29 @@ +# - Try to find petsc4py +# Once done this will define +# +# PETSC4PY_FOUND - system has petsc4py +# PETSC4PY_INCLUDES - the petsc4py include directories +# PETSC4PY_VERSION - version string (MAJOR.MINOR.SUBMINOR) +# +# Usage: find_package(PETSc4py) + +execute_process( + COMMAND ${Python3_EXECUTABLE} -c "import petsc4py; print(petsc4py.get_include(), end='')" + OUTPUT_VARIABLE PETSC4PY_INCLUDES + RESULT_VARIABLE PETSC4PY_NOT_FOUND +) + +execute_process( + COMMAND ${Python3_EXECUTABLE} -c "import petsc4py; print(petsc4py.__version__, end='')" + OUTPUT_VARIABLE PETSC4PY_VERSION +) + +mark_as_advanced(PETSC4PY_INCLUDES, PETSC4PY_VERSION) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args( + PETSc4py + REQUIRED_VARS PETSC4PY_INCLUDES + VERSION_VAR PETSC4PY_VERSION + FAIL_MESSAGE "PETSc4py could not be found." +) diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index 95ebcd3..00c15f7 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -47,7 +47,7 @@ modules: # utils - name: PetscUtils source_includes: - - PybindPETScTypeCaster.hpp + - caster_petsc.h # visualization - name: Scene diff --git a/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp b/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp index ca9ef8b..c7de3f9 100644 --- a/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp +++ b/examples/cells/dynamic/wrappers/all/PetscUtils.cppwg.cpp @@ -2,7 +2,7 @@ #include #include -#include "PybindPETScTypeCaster.hpp" +#include "caster_petsc.h" #include #include "PetscUtils.hpp" diff --git a/examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp b/examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp deleted file mode 100644 index ad1b664..0000000 --- a/examples/cells/src/cpp/typecasters/PybindPETScTypeCaster.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef PYBINDPETSCTYPECASTER_HPP_ -#define PYBINDPETSCTYPECASTER_HPP_ - -#include -#include - -#include - -PYBIND11_MAKE_OPAQUE(Mat); -PYBIND11_MAKE_OPAQUE(Vec); - -#endif // PYBINDPETSCTYPECASTER_HPP_ diff --git a/examples/cells/tests/test_cells.py b/examples/cells/tests/test_cells.py index 484f86e..42c3390 100644 --- a/examples/cells/tests/test_cells.py +++ b/examples/cells/tests/test_cells.py @@ -1,4 +1,5 @@ import unittest +import sys import petsc4py import vtk @@ -10,12 +11,12 @@ class TestCells(unittest.TestCase): def testVtkCaster(self): scene = Scene[2]() renderer = scene.GetRenderer() - self.assertIsNotNone(renderer) + self.assertIsInstance(renderer, vtk.vtkRenderingOpenGL2Python.vtkOpenGLRenderer) def testPetscCaster(self): - petsc4py.init() + petsc4py.init(sys.argv) vec = PetscUtils.CreateVec(10) - self.assertIsNotNone(vec) + self.assertIsInstance(vec, petsc4py.PETSc.Vec) if __name__ == "__main__": diff --git a/examples/cells/thirdparty/dolfinx/COPYING b/examples/cells/thirdparty/dolfinx/COPYING new file mode 100644 index 0000000..85f73fd --- /dev/null +++ b/examples/cells/thirdparty/dolfinx/COPYING @@ -0,0 +1,680 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. + + +Note: Individual files contain the following tag instead of the full license text. + +SPDX-License-Identifier: LGPL-3.0-or-later +This enables machine processing of license information based on the SPDX License Identifiers that are here available: http://spdx.org/licenses/ \ No newline at end of file diff --git a/examples/cells/thirdparty/dolfinx/COPYING.LESSER b/examples/cells/thirdparty/dolfinx/COPYING.LESSER new file mode 100644 index 0000000..491226c --- /dev/null +++ b/examples/cells/thirdparty/dolfinx/COPYING.LESSER @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/examples/cells/thirdparty/dolfinx/caster_petsc.h b/examples/cells/thirdparty/dolfinx/caster_petsc.h new file mode 100644 index 0000000..eaba0ff --- /dev/null +++ b/examples/cells/thirdparty/dolfinx/caster_petsc.h @@ -0,0 +1,82 @@ +// Copyright (C) 2017-2023 Chris Richardson and Garth N. Wells +// +// This file is part of DOLFINx (https://www.fenicsproject.org) +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +#pragma once + +#include +#include + +#include +#include +#include + +// pybind11 casters for PETSc/petsc4py objects + +namespace py = pybind11; + +// Import petsc4py on demand +#define VERIFY_PETSC4PY_FROMPY(func) \ + if (!func) \ + { \ + if (import_petsc4py() != 0) \ + return false; \ + } + +#define VERIFY_PETSC4PY_FROMCPP(func) \ + if (!func) \ + { \ + if (import_petsc4py() != 0) \ + return {}; \ + } + +// Macro for casting between PETSc and petsc4py objects +#define PETSC_CASTER_MACRO(TYPE, P4PYTYPE, NAME) \ + template <> \ + class type_caster<_p_##TYPE> \ + { \ + public: \ + PYBIND11_TYPE_CASTER(TYPE, const_name(#NAME)); \ + bool load(handle src, bool) \ + { \ + VERIFY_PETSC4PY_FROMPY(PyPetsc##P4PYTYPE##_Get); \ + if (PyObject_TypeCheck(src.ptr(), &PyPetsc##P4PYTYPE##_Type) != 0) \ + { \ + value = PyPetsc##P4PYTYPE##_Get(src.ptr()); \ + return true; \ + } \ + else \ + return false; \ + } \ + \ + static handle cast(TYPE src, return_value_policy policy, handle) \ + { \ + VERIFY_PETSC4PY_FROMCPP(PyPetsc##P4PYTYPE##_New); \ + if (policy == return_value_policy::take_ownership) \ + { \ + PyObject *obj = PyPetsc##P4PYTYPE##_New(src); \ + PetscObjectDereference((PetscObject)src); \ + return py::handle(obj); \ + } \ + else if (policy == return_value_policy::automatic_reference or \ + policy == return_value_policy::reference) \ + { \ + PyObject *obj = PyPetsc##P4PYTYPE##_New(src); \ + return py::handle(obj); \ + } \ + else \ + { \ + return {}; \ + } \ + } \ + \ + operator TYPE() { return value; } \ + } + +namespace pybind11::detail +{ + PETSC_CASTER_MACRO(Mat, Mat, mat); + PETSC_CASTER_MACRO(Vec, Vec, vec); +} // namespace pybind11::detail From e56e4b07c40f7d05025a2e0f7ba67460087ad65b Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Thu, 31 Oct 2024 22:56:14 +0000 Subject: [PATCH 13/31] #20 Fix test errors --- .github/workflows/build-and-test.yml | 4 +++- cppwg/info/base_info.py | 1 - examples/cells/tests/test_cells.py | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 33972d3..502291d 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -39,7 +39,9 @@ jobs: pip install .[dev] - name: Lint with flake8 - run: python -m flake8 + run: | + python -m flake8 --version + python -m flake8 . - name: Test shapes example wrapper generation run: python -m unittest tests/test_wrapper_generation.py diff --git a/cppwg/info/base_info.py b/cppwg/info/base_info.py index f777ace..1c35d86 100644 --- a/cppwg/info/base_info.py +++ b/cppwg/info/base_info.py @@ -106,7 +106,6 @@ def __init__(self, name: str, info_config: Optional[Dict[str, Any]] = None) -> N "unsigned int": "Unsigned", "Unsigned int": "Unsigned", "unsigned": "Unsigned", - "double": "Double", "std::vector": "Vector", "std::pair": "Pair", "std::map": "Map", diff --git a/examples/cells/tests/test_cells.py b/examples/cells/tests/test_cells.py index 42c3390..bb268f9 100644 --- a/examples/cells/tests/test_cells.py +++ b/examples/cells/tests/test_cells.py @@ -1,5 +1,5 @@ -import unittest import sys +import unittest import petsc4py import vtk @@ -11,12 +11,12 @@ class TestCells(unittest.TestCase): def testVtkCaster(self): scene = Scene[2]() renderer = scene.GetRenderer() - self.assertIsInstance(renderer, vtk.vtkRenderingOpenGL2Python.vtkOpenGLRenderer) + self.assertIsNotNone(renderer) def testPetscCaster(self): petsc4py.init(sys.argv) vec = PetscUtils.CreateVec(10) - self.assertIsInstance(vec, petsc4py.PETSc.Vec) + self.assertIsNotNone(vec) if __name__ == "__main__": From 49386fb2497569d7a2ccc7caba32905b429326df Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Fri, 1 Nov 2024 14:27:17 +0000 Subject: [PATCH 14/31] #20 Use skbuild --- .github/workflows/test-typecasters.yml | 2 +- .gitignore | 1 + examples/cells/CMakeLists.txt | 50 +++++++------ examples/cells/cmake/FindPETSc.cmake | 75 ++++++++++--------- examples/cells/cmake/FindPETSc4py.cmake | 11 +-- .../{ => dynamic}/thirdparty/dolfinx/COPYING | 0 .../thirdparty/dolfinx/COPYING.LESSER | 0 .../thirdparty/dolfinx/caster_petsc.h | 6 +- .../{ => dynamic}/thirdparty/smtk/LICENSE.txt | 0 .../thirdparty/smtk/PybindVTKTypeCaster.h | 0 examples/cells/pyproject.toml | 8 +- examples/cells/setup.cfg | 17 ----- examples/cells/setup.py | 15 +++- 13 files changed, 93 insertions(+), 92 deletions(-) rename examples/cells/{ => dynamic}/thirdparty/dolfinx/COPYING (100%) rename examples/cells/{ => dynamic}/thirdparty/dolfinx/COPYING.LESSER (100%) rename examples/cells/{ => dynamic}/thirdparty/dolfinx/caster_petsc.h (96%) rename examples/cells/{ => dynamic}/thirdparty/smtk/LICENSE.txt (100%) rename examples/cells/{ => dynamic}/thirdparty/smtk/PybindVTKTypeCaster.h (100%) delete mode 100644 examples/cells/setup.cfg diff --git a/.github/workflows/test-typecasters.yml b/.github/workflows/test-typecasters.yml index 96bb17b..10caf2b 100644 --- a/.github/workflows/test-typecasters.yml +++ b/.github/workflows/test-typecasters.yml @@ -40,4 +40,4 @@ jobs: - name: Test cells example run: | - python -m unittest discover examples/cells/tests + python -m unittest discover -s examples/cells/tests diff --git a/.gitignore b/.gitignore index a40bdea..a3c89df 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ __pycache__/ .Python env/ build/ +_skbuild/ develop-eggs/ dist/ downloads/ diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index c1fcb01..15d028d 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -29,41 +29,43 @@ find_package(PETSc4py REQUIRED) # Find MPI find_package(MPI REQUIRED COMPONENTS CXX) -# Set up pycells module -file(GLOB SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/**/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/**/*.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/**/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/**/*.hpp +# Add a shared library target for the main C++ source +file(GLOB_RECURSE MAIN_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.hpp ) - -pybind11_add_module(_pycells_all ${SOURCES}) - -target_include_directories( - _pycells_all - PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/wrappers/lib +add_library(cells SHARED ${MAIN_SOURCES}) +target_link_libraries(cells PUBLIC + ${VTK_LIBRARIES} + ${PETSC_LIBRARIES} + ${MPI_C_LIBRARIES} + ${MPI_CXX_LIBRARIES} +) +target_include_directories(cells PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/cell ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/utils ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/visualization - ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/dolfinx - ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/smtk ${VTK_INCLUDE_DIRS} ${PETSC_INCLUDES} - ${PETSC4PY_INCLUDES} ${MPI_C_INCLUDE_PATH} ${MPI_CXX_INCLUDE_PATH} ) -target_link_libraries( - _pycells_all - PUBLIC - ${VTK_LIBRARIES} - ${PETSC_LIBRARIES} - ${MPI_C_LIBRARIES} - ${MPI_CXX_LIBRARIES} +# Set up pycells module +file(GLOB WRAPPER_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all/*.hpp +) +pybind11_add_module(_pycells_all MODULE ${WRAPPER_SOURCES}) +target_link_libraries(_pycells_all PUBLIC cells) +target_include_directories(_pycells_all PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/thirdparty/dolfinx + ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/thirdparty/smtk + ${PETSC4PY_INCLUDES} ) -install(TARGETS _pycells_all LIBRARY DESTINATION pycells) +# Install target for scikit-build +install(TARGETS _pycells_all LIBRARY DESTINATION .) diff --git a/examples/cells/cmake/FindPETSc.cmake b/examples/cells/cmake/FindPETSc.cmake index 456d5c0..d15d3f5 100644 --- a/examples/cells/cmake/FindPETSc.cmake +++ b/examples/cells/cmake/FindPETSc.cmake @@ -41,6 +41,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #============================================================================= +# Set PETSC_DIR # Ubuntu uses versioned paths e.g /usr/lib/petscdir/petsc3.15/x86_64-linux-gnu-real file(GLOB ubuntu_paths "/usr/lib/petscdir/*") @@ -48,10 +49,11 @@ find_path( PETSC_DIR include/petsc.h HINTS ENV PETSC_DIR - PATHS /usr/lib/petsc ${deb_paths} + PATHS /usr/lib/petsc ${ubuntu_paths} DOC "PETSc Directory" ) +# Set PETSC_ARCH if(PETSC_DIR AND NOT PETSC_ARCH) foreach(_arch $ENV{PETSC_ARCH} x86_64-linux-gnu-real x86_64-linux-gnu-real-debug) find_path( @@ -68,7 +70,11 @@ if(PETSC_DIR AND NOT PETSC_ARCH) endforeach() endif() -if(EXISTS "${PETSC_DIR}/${PETSC_ARCH}/lib/petsc/conf/petscvariables") +# Check for PETSc config files +if(EXISTS "${PETSC_DIR}/include/petscversion.h" AND + EXISTS "${PETSC_DIR}/${PETSC_ARCH}/lib/petsc/conf/rules" AND + EXISTS "${PETSC_DIR}/${PETSC_ARCH}/lib/petsc/conf/petscvariables") + set(petsc_version_h "${PETSC_DIR}/include/petscversion.h") set(petsc_conf_rules "${PETSC_DIR}/lib/petsc/conf/rules") set(petsc_conf_variables "${PETSC_DIR}/lib/petsc/conf/variables") else() @@ -76,30 +82,26 @@ else() endif() # Get the PETSc version -if(EXISTS "${PETSC_DIR}/include/petscversion.h") - file(STRINGS "${PETSC_DIR}/include/petscversion.h" vstrings REGEX "#define PETSC_VERSION_(RELEASE|MAJOR|MINOR|SUBMINOR|PATCH) ") - foreach(line ${vstrings}) - string(REGEX REPLACE " +" ";" fields ${line}) # break line into three fields (the first is always "#define") - list(GET fields 1 var) - list(GET fields 2 val) - set(${var} ${val}) - endforeach() - - set(vstring "${PETSC_VERSION_MAJOR}.${PETSC_VERSION_MINOR}.${PETSC_VERSION_SUBMINOR}") - - if(PETSC_VERSION_RELEASE) - if($(PETSC_VERSION_PATCH) GREATER 0) - set(vstring "${vstring}p${PETSC_VERSION_PATCH}") - endif() - else() - # make dev version compare higher than any patch level of a released version - set(vstring "${vstring}.99") +file(STRINGS "${petsc_version_h}" _vstrings REGEX "#define PETSC_VERSION_(RELEASE|MAJOR|MINOR|SUBMINOR|PATCH) ") +foreach(_line ${_vstrings}) + string(REGEX REPLACE " +" ";" _fields ${_line}) # break line into three fields (the first is always "#define") + list(GET _fields 1 _var) + list(GET _fields 2 _val) + set(${_var} ${_val}) +endforeach() + +set(_version "${PETSC_VERSION_MAJOR}.${PETSC_VERSION_MINOR}.${PETSC_VERSION_SUBMINOR}") + +if(PETSC_VERSION_RELEASE) + if($(PETSC_VERSION_PATCH) GREATER 0) + set(_version "${_version}p${PETSC_VERSION_PATCH}") endif() - set(PETSC_VERSION "${vstring}" CACHE INTERNAL "PETSc version") else() - message(SEND_ERROR "PETSC_DIR cannot be used, ${PETSC_DIR}/include/petscversion.h does not exist") + # make dev version compare higher than any patch level of a released version + set(_version "${_version}.99") endif() - +set(PETSC_VERSION "${_version}" CACHE INTERNAL "PETSc version") + # A temporary makefile to probe the PETSc configuration set(ENV{PETSC_DIR} "${PETSC_DIR}") set(ENV{PETSC_ARCH} "${PETSC_ARCH}") @@ -124,24 +126,24 @@ macro(PETSC_GET_VARIABLE name var) ) endmacro() -# Extract include paths +# Extract include paths from compile command line petsc_get_variable(PETSC_CCPPFLAGS petsc_ccpp_flags) string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${petsc_ccpp_flags}") set(_incs_found "") - foreach(token ${_all_tokens}) - string(REGEX REPLACE "^-I" "" token ${token}) - string(REGEX REPLACE "//" "/" token ${token}) - if(EXISTS ${token}) - list(APPEND _incs_found ${token}) + foreach(_token ${_all_tokens}) + string(REGEX REPLACE "^-I" "" _token ${_token}) + string(REGEX REPLACE "//" "/" _token ${_token}) + if(EXISTS ${_token}) + list(APPEND _incs_found ${_token}) else() - message(STATUS "Include directory ${token} does not exist") + message(STATUS "Include directory ${_token} does not exist") endif() - endforeach(token) + endforeach() list(REMOVE_DUPLICATES _incs_found) set(PETSC_INCLUDES_ALL "${_incs_found}") -# Extract libraries +# Find PETSc libraries petsc_get_variable(PETSC_LIB_DIR petsc_lib_dir) message(STATUS "petsc_lib_dir ${petsc_lib_dir}") @@ -192,17 +194,18 @@ endif() # We do an out-of-source build so __FILE__ will be an absolute path, hence __INSDIR__ is superfluous set(PETSC_DEFINITIONS "-D__INSDIR__=" CACHE STRING "PETSc definitions" FORCE) +set(PETSC_INCLUDES ${PETSC_INCLUDES_ALL} CACHE STRING "PETSc include path" FORCE) +set(PETSC_LIBRARIES ${PETSC_LIBRARIES_ALL} CACHE STRING "PETSc libraries" FORCE) + # Sometimes this can be used to assist FindMPI.cmake petsc_get_variable(PCC petsc_cc) petsc_get_variable(MPIEXEC petsc_mpiexec) set(PETSC_COMPILER ${petsc_cc} CACHE FILEPATH "PETSc compiler" FORCE) set(PETSC_MPIEXEC ${petsc_mpiexec} CACHE FILEPATH "Executable for running PETSc MPI programs" FORCE) -set(PETSC_INCLUDES ${PETSC_INCLUDES_ALL} CACHE STRING "PETSc include path" FORCE) -set(PETSC_LIBRARIES ${PETSC_LIBRARIES_ALL} CACHE STRING "PETSc libraries" FORCE) -# Note that we have forced values for all these choices. If you +# Note that we have forced values for all these choices. If you # change these, you are telling the system to trust you that they -# work. It is likely that you will end up with a broken build. +# work. It is likely that you will end up with a broken build. mark_as_advanced(PETSC_INCLUDES PETSC_LIBRARIES PETSC_COMPILER PETSC_DEFINITIONS PETSC_MPIEXEC) file(REMOVE ${petsc_config_makefile}) diff --git a/examples/cells/cmake/FindPETSc4py.cmake b/examples/cells/cmake/FindPETSc4py.cmake index 82f9524..42a0792 100644 --- a/examples/cells/cmake/FindPETSc4py.cmake +++ b/examples/cells/cmake/FindPETSc4py.cmake @@ -10,13 +10,14 @@ execute_process( COMMAND ${Python3_EXECUTABLE} -c "import petsc4py; print(petsc4py.get_include(), end='')" OUTPUT_VARIABLE PETSC4PY_INCLUDES - RESULT_VARIABLE PETSC4PY_NOT_FOUND ) -execute_process( - COMMAND ${Python3_EXECUTABLE} -c "import petsc4py; print(petsc4py.__version__, end='')" - OUTPUT_VARIABLE PETSC4PY_VERSION -) +if(PETSC4PY_INCLUDES) + execute_process( + COMMAND ${Python3_EXECUTABLE} -c "import petsc4py; print(petsc4py.__version__, end='')" + OUTPUT_VARIABLE PETSC4PY_VERSION + ) +endif() mark_as_advanced(PETSC4PY_INCLUDES, PETSC4PY_VERSION) diff --git a/examples/cells/thirdparty/dolfinx/COPYING b/examples/cells/dynamic/thirdparty/dolfinx/COPYING similarity index 100% rename from examples/cells/thirdparty/dolfinx/COPYING rename to examples/cells/dynamic/thirdparty/dolfinx/COPYING diff --git a/examples/cells/thirdparty/dolfinx/COPYING.LESSER b/examples/cells/dynamic/thirdparty/dolfinx/COPYING.LESSER similarity index 100% rename from examples/cells/thirdparty/dolfinx/COPYING.LESSER rename to examples/cells/dynamic/thirdparty/dolfinx/COPYING.LESSER diff --git a/examples/cells/thirdparty/dolfinx/caster_petsc.h b/examples/cells/dynamic/thirdparty/dolfinx/caster_petsc.h similarity index 96% rename from examples/cells/thirdparty/dolfinx/caster_petsc.h rename to examples/cells/dynamic/thirdparty/dolfinx/caster_petsc.h index eaba0ff..0faa0dc 100644 --- a/examples/cells/thirdparty/dolfinx/caster_petsc.h +++ b/examples/cells/dynamic/thirdparty/dolfinx/caster_petsc.h @@ -15,8 +15,6 @@ // pybind11 casters for PETSc/petsc4py objects -namespace py = pybind11; - // Import petsc4py on demand #define VERIFY_PETSC4PY_FROMPY(func) \ if (!func) \ @@ -58,13 +56,13 @@ namespace py = pybind11; { \ PyObject *obj = PyPetsc##P4PYTYPE##_New(src); \ PetscObjectDereference((PetscObject)src); \ - return py::handle(obj); \ + return pybind11::handle(obj); \ } \ else if (policy == return_value_policy::automatic_reference or \ policy == return_value_policy::reference) \ { \ PyObject *obj = PyPetsc##P4PYTYPE##_New(src); \ - return py::handle(obj); \ + return pybind11::handle(obj); \ } \ else \ { \ diff --git a/examples/cells/thirdparty/smtk/LICENSE.txt b/examples/cells/dynamic/thirdparty/smtk/LICENSE.txt similarity index 100% rename from examples/cells/thirdparty/smtk/LICENSE.txt rename to examples/cells/dynamic/thirdparty/smtk/LICENSE.txt diff --git a/examples/cells/thirdparty/smtk/PybindVTKTypeCaster.h b/examples/cells/dynamic/thirdparty/smtk/PybindVTKTypeCaster.h similarity index 100% rename from examples/cells/thirdparty/smtk/PybindVTKTypeCaster.h rename to examples/cells/dynamic/thirdparty/smtk/PybindVTKTypeCaster.h diff --git a/examples/cells/pyproject.toml b/examples/cells/pyproject.toml index 86fd8eb..bae971c 100644 --- a/examples/cells/pyproject.toml +++ b/examples/cells/pyproject.toml @@ -1,10 +1,12 @@ [build-system] requires = [ - "scikit_build_core", - "setuptools", + "cmake", + "ninja", "pybind11", + "scikit-build", + "setuptools", ] -build-backend = "scikit_build_core.setuptools.build_meta" +build-backend = "setuptools.build_meta" [tool.scikit-build] cmake.build-type = "Release" diff --git a/examples/cells/setup.cfg b/examples/cells/setup.cfg deleted file mode 100644 index 7c102b8..0000000 --- a/examples/cells/setup.cfg +++ /dev/null @@ -1,17 +0,0 @@ -[metadata] -name = pycells -version = 0.0.1 - -[options] -zip_safe = False -python_requires = >=3.8 -packages = find: -package_dir = - =src/py -include_package_data = True - -[options.packages.find] -where = src/py - -[build_cmake] -source_dir = . diff --git a/examples/cells/setup.py b/examples/cells/setup.py index 6068493..fde2f34 100644 --- a/examples/cells/setup.py +++ b/examples/cells/setup.py @@ -1,3 +1,14 @@ -from setuptools import setup +from skbuild import setup + +setup( + name="pycells", + version="0.0.1", + description="pycells", + author="kna", + license="BSD-3-Clause", + packages=["pycells"], + package_dir={"": "src/py"}, + cmake_install_dir="src/py/pycells", + python_requires=">=3.8", +) -setup() From 909ad0186265b2e10a98d4f0289a0f56efc156a7 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Fri, 1 Nov 2024 21:22:20 +0000 Subject: [PATCH 15/31] #20 Split main and wrapper libs in cells example --- .flake8 | 3 ++- examples/cells/CMakeLists.txt | 31 +++++++++++++++++++++++-------- examples/cells/setup.py | 1 - pyproject.toml | 3 ++- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/.flake8 b/.flake8 index 95b9d39..b3432bc 100644 --- a/.flake8 +++ b/.flake8 @@ -6,9 +6,10 @@ exclude = .git, .github, build, + cppwg/templates, doc, examples, - cppwg/templates, + _skbuild, tests, venv, diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 15d028d..7aba5db 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -9,9 +9,6 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) # Find Python find_package(Python3 REQUIRED COMPONENTS Interpreter Development) -# Find pybind11 -find_package(pybind11 CONFIG REQUIRED) - # Find VTK find_package(VTK REQUIRED COMPONENTS vtkCommonCore @@ -29,6 +26,15 @@ find_package(PETSc4py REQUIRED) # Find MPI find_package(MPI REQUIRED COMPONENTS CXX) +# Fetch pybind11 +include(FetchContent) +FetchContent_Declare( + pybind11 + URL https://github.com/pybind/pybind11/archive/refs/tags/v2.10.4.tar.gz + URL_HASH SHA256=832e2f309c57da9c1e6d4542dedd34b24e4192ecb4d62f6f4866a737454c9970 +) +FetchContent_MakeAvailable(pybind11) + # Add a shared library target for the main C++ source file(GLOB_RECURSE MAIN_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp @@ -53,19 +59,28 @@ target_include_directories(cells PUBLIC ${MPI_CXX_INCLUDE_PATH} ) -# Set up pycells module +# Set up the pycells module with pybind11 file(GLOB WRAPPER_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all/*.hpp ) pybind11_add_module(_pycells_all MODULE ${WRAPPER_SOURCES}) -target_link_libraries(_pycells_all PUBLIC cells) -target_include_directories(_pycells_all PUBLIC +target_link_libraries(_pycells_all PRIVATE cells) +target_include_directories(_pycells_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/thirdparty/dolfinx ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/thirdparty/smtk ${PETSC4PY_INCLUDES} ) -# Install target for scikit-build -install(TARGETS _pycells_all LIBRARY DESTINATION .) +# Add install targets for scikit-build +if(SKBUILD) + set_target_properties(_pycells_all PROPERTIES + SKIP_BUILD_RPATH FALSE + BUILD_WITH_INSTALL_RPATH FALSE + INSTALL_RPATH_USE_LINK_PATH TRUE + INSTALL_RPATH $ORIGIN + ) + install(TARGETS cells LIBRARY DESTINATION .) + install(TARGETS _pycells_all LIBRARY DESTINATION .) +endif() diff --git a/examples/cells/setup.py b/examples/cells/setup.py index fde2f34..c79cca2 100644 --- a/examples/cells/setup.py +++ b/examples/cells/setup.py @@ -11,4 +11,3 @@ cmake_install_dir="src/py/pycells", python_requires=">=3.8", ) - diff --git a/pyproject.toml b/pyproject.toml index fcdd3ae..e1f451c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,9 +49,10 @@ target-version = ["py38", "py39", "py310", "py311", "py312"] extend-exclude = """ ( ^/cppwg/templates/ + | _skbuild/ ) """ [tool.isort] profile = "black" -extend_skip_glob = ["examples/shapes/wrapper/pybind11/*"] +extend_skip = ["_skbuild"] From 9f5aa68916966fcca24f78ca1f4b65aff3346f03 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Fri, 1 Nov 2024 23:05:15 +0000 Subject: [PATCH 16/31] #20 Add cells target for making wrappers --- examples/cells/CMakeLists.txt | 37 ++++++++++++++++--------- examples/cells/pyproject.toml | 1 - pyproject.toml | 43 ----------------------------- setup.cfg | 52 +++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 57 deletions(-) create mode 100644 setup.cfg diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 7aba5db..c6f1932 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -4,7 +4,7 @@ project(pycells LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) # Find Python find_package(Python3 REQUIRED COMPONENTS Interpreter Development) @@ -37,8 +37,8 @@ FetchContent_MakeAvailable(pybind11) # Add a shared library target for the main C++ source file(GLOB_RECURSE MAIN_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.hpp + ${CMAKE_SOURCE_DIR}/src/cpp/*.cpp + ${CMAKE_SOURCE_DIR}/src/cpp/*.hpp ) add_library(cells SHARED ${MAIN_SOURCES}) target_link_libraries(cells PUBLIC @@ -48,11 +48,11 @@ target_link_libraries(cells PUBLIC ${MPI_CXX_LIBRARIES} ) target_include_directories(cells PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/cell - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/utils - ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/visualization + ${CMAKE_SOURCE_DIR}/src/cpp + ${CMAKE_SOURCE_DIR}/src/cpp/cell + ${CMAKE_SOURCE_DIR}/src/cpp/mesh + ${CMAKE_SOURCE_DIR}/src/cpp/utils + ${CMAKE_SOURCE_DIR}/src/cpp/visualization ${VTK_INCLUDE_DIRS} ${PETSC_INCLUDES} ${MPI_C_INCLUDE_PATH} @@ -61,15 +61,15 @@ target_include_directories(cells PUBLIC # Set up the pycells module with pybind11 file(GLOB WRAPPER_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all/*.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all/*.hpp + ${CMAKE_SOURCE_DIR}/dynamic/wrappers/all/*.cpp + ${CMAKE_SOURCE_DIR}/dynamic/wrappers/all/*.hpp ) pybind11_add_module(_pycells_all MODULE ${WRAPPER_SOURCES}) target_link_libraries(_pycells_all PRIVATE cells) target_include_directories(_pycells_all PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/wrappers/all - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/thirdparty/dolfinx - ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/thirdparty/smtk + ${CMAKE_SOURCE_DIR}/dynamic/wrappers/all + ${CMAKE_SOURCE_DIR}/dynamic/thirdparty/dolfinx + ${CMAKE_SOURCE_DIR}/dynamic/thirdparty/smtk ${PETSC4PY_INCLUDES} ) @@ -84,3 +84,14 @@ if(SKBUILD) install(TARGETS cells LIBRARY DESTINATION .) install(TARGETS _pycells_all LIBRARY DESTINATION .) endif() + +# Target for re-generating wrappers +add_custom_target(wrap + COMMAND ${Python3_EXECUTABLE} -m cppwg ${CMAKE_SOURCE_DIR}/src/cpp + -w ${CMAKE_SOURCE_DIR}/dynamic/wrappers + -p ${CMAKE_SOURCE_DIR}/dynamic/config.yaml + -i "$,;>" + -l ${CMAKE_BINARY_DIR}/cppwg.log + --std c++17 + COMMAND_EXPAND_LISTS +) diff --git a/examples/cells/pyproject.toml b/examples/cells/pyproject.toml index bae971c..0ed003f 100644 --- a/examples/cells/pyproject.toml +++ b/examples/cells/pyproject.toml @@ -2,7 +2,6 @@ requires = [ "cmake", "ninja", - "pybind11", "scikit-build", "setuptools", ] diff --git a/pyproject.toml b/pyproject.toml index e1f451c..b874398 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,49 +1,6 @@ [build-system] requires = ["setuptools", "wheel"] -[project] -name = "cppwg" -description = "An automatic Python wrapper generator for C++" -authors = [ - { name = "Chaste Developers", email = "chaste-users@maillist.ox.ac.uk" }, - { name = "James Grogan", email = "grogan@maths.ox.ac.uk" }, - { name = "Kwabena Amponsah", email = "kwabenantim@gmail.com" }, -] -license = { file = "LICENSE" } -keywords = ["C++", "Python", "Pybind11"] -readme = "README.md" -version = "0.3.1" - -classifiers = [ - "Development Status :: 4 - Beta", - "Environment :: Console", - "Intended Audience :: Developers", - "Operating System :: MacOS :: MacOS X", - "Operating System :: Microsoft :: Windows", - "Operating System :: POSIX", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Programming Language :: Python :: Implementation :: CPython", - "Topic :: Software Development", -] - -dependencies = ["pyyaml >=6.0", "pygccxml >=2.2", "castxml >=0.4"] - -requires-python = ">=3.8" - -[project.optional-dependencies] -dev = ["black", "flake8", "flake8-bugbear", "flake8-docstrings", "isort"] -docs = ["sphinx", "sphinx-rtd-theme", "numpydoc"] - -[project.scripts] -cppwg = "cppwg.__main__:main" - -[project.urls] -Repository = "https://github.com/Chaste/cppwg/" - [tool.black] target-version = ["py38", "py39", "py310", "py311", "py312"] extend-exclude = """ diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..45b5176 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,52 @@ +[metadata] +name = cppwg +version = 0.3.1 +author = Chaste Developers +author_email = chaste-users@maillist.ox.ac.uk +description = An automatic Python wrapper generator for C++ code +long_description = file: README.md +keywords = C++, Python, pybind11 +license = BSD-3-Clause +classifiers = + Development Status :: 4 - Beta + Environment :: Console + Intended Audience :: Developers + Operating System :: MacOS :: MacOS X + Operating System :: Microsoft :: Windows + Operating System :: POSIX + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 + Programming Language :: Python :: 3.12 + Programming Language :: Python :: Implementation :: CPython + Topic :: Software Development + +project_urls = + Source Code = https://github.com/Chaste/cppwg/ + +[options] +zip_safe = False +packages = find: +python_requires = >=3.8 +install_requires = + pyyaml>=6.0 + pygccxml>=2.2 + castxml>=0.4 + +[options.entry_points] +console_scripts = + executable-name = cppwg.__main__:main + +[options.extras_require] +dev = + black + flake8 + flake8-bugbear + flake8-docstrings + isort + +docs = + sphinx + sphinx-rtd-theme + numpydoc From 0d8f6af0cde8ec1a2100d90b7e9c8f04da6f8167 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 3 Nov 2024 18:32:45 +0000 Subject: [PATCH 17/31] #20 Update example tests --- .github/workflows/test-cells.yml | 78 ++++++++++++++++ .../{build-and-test.yml => test-shapes.yml} | 50 +++++----- .github/workflows/test-style.yml | 34 +++++++ .github/workflows/test-typecasters.yml | 43 --------- examples/cells/CMakeLists.txt | 2 +- examples/cells/requirements.txt | 5 + setup.cfg | 2 +- setup.py | 3 + tests/__init__.py | 0 tests/test_wrapper_generation.py | 93 ------------------- 10 files changed, 144 insertions(+), 166 deletions(-) create mode 100644 .github/workflows/test-cells.yml rename .github/workflows/{build-and-test.yml => test-shapes.yml} (51%) create mode 100644 .github/workflows/test-style.yml delete mode 100644 .github/workflows/test-typecasters.yml create mode 100644 examples/cells/requirements.txt create mode 100644 setup.py delete mode 100644 tests/__init__.py delete mode 100644 tests/test_wrapper_generation.py diff --git a/.github/workflows/test-cells.yml b/.github/workflows/test-cells.yml new file mode 100644 index 0000000..07e08a5 --- /dev/null +++ b/.github/workflows/test-cells.yml @@ -0,0 +1,78 @@ +name: test-cells + +on: + workflow_dispatch: + pull_request: + branches: + - "**" + +jobs: + test-cells: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] + + concurrency: + group: test-cells-${{ github.ref }}-${{ matrix.python-version }} + cancel-in-progress: true + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get install cmake + + - name: Setup Miniconda Python ${{ matrix.python-version }} + uses: conda-incubator/setup-miniconda@v3 + with: + auto-update-conda: true + use-mamba: true + miniforge-variant: Mambaforge + miniforge-version: latest + python-version: ${{ matrix.python-version }} + activate-environment: test_cells + channels: conda-forge + + - name: Install cppwg + run: | + conda develop . + + - name: Install requirements + run: | + mamba install --yes --file requirements.txt + working-directory: examples/cells + + - name: Configure + run: | + export PETSC_DIR=$CONDA_PREFIX + export PETSC_ARCH= + mkdir build && cd build + cmake .. + working-directory: examples/cells + + - name: Regenerate wrappers + run: | + rm -rf dynamic/wrappers + cd build + make pycells_wrappers + working-directory: examples/cells + + - name: Check for changes + run: | + git diff --exit-code dynamic/wrappers + grep "Unknown class" build/cppwg.log + working-directory: examples/cells + + - name: Build + run: pip install --user --no-cache-dir -v . + working-directory: examples/cells + + - name: Test + run: python -m unittest discover -s tests + working-directory: examples/cells diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/test-shapes.yml similarity index 51% rename from .github/workflows/build-and-test.yml rename to .github/workflows/test-shapes.yml index 502291d..4c78676 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/test-shapes.yml @@ -1,4 +1,4 @@ -name: build-and-test +name: test-shapes on: workflow_dispatch: @@ -7,16 +7,16 @@ on: - "**" jobs: - build-and-test: + test-shapes: runs-on: ubuntu-latest strategy: fail-fast: false matrix: - python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] concurrency: - group: build-and-test-${{ github.ref }}-${{ matrix.python-version }} + group: test-shapes-${{ github.ref }}-${{ matrix.python-version }} cancel-in-progress: true steps: @@ -25,7 +25,7 @@ jobs: - name: Install system dependencies run: | - sudo apt-get update && \ + sudo apt-get update sudo apt-get install cmake - name: Setup Python ${{ matrix.python-version }} @@ -36,43 +36,37 @@ jobs: - name: Install cppwg run: | python -m pip install --upgrade pip - pip install .[dev] + python -m pip install . - - name: Lint with flake8 + - name: Regenerate wrappers run: | - python -m flake8 --version - python -m flake8 . - - - name: Test shapes example wrapper generation - run: python -m unittest tests/test_wrapper_generation.py - - - name: Regenerate shapes example wrappers - run: | - cd examples/shapes/wrapper - rm -rf geometry math_funcs primitives - cd .. + rm -rf wrapper/*/ cppwg src/cpp \ --wrapper_root wrapper/ \ --package_info wrapper/package_info.yaml \ --includes src/cpp/*/ \ --std c++17 \ --logfile cppwg.log + working-directory: examples/shapes - - name: Check shapes example for new classes + - name: Check for changes run: | - cd examples/shapes - cat cppwg.log | grep "Unknown class" + git diff --exit-code wrapper + grep "Unknown class" cppwg.log + working-directory: examples/shapes - - name: Build shapes example + - name: Configure run: | - cd examples/shapes - mkdir build - cd build + mkdir build && cd build cmake .. - make -j $(nproc) + working-directory: examples/shapes + + - name: Build + run: make -j $(nproc) + working-directory: examples/shapes/build - - name: Test shapes example + - name: Test run: | - cd examples/shapes/build python -m unittest test_functions.py python -m unittest test_classes.py + working-directory: examples/shapes/build diff --git a/.github/workflows/test-style.yml b/.github/workflows/test-style.yml new file mode 100644 index 0000000..7a9038d --- /dev/null +++ b/.github/workflows/test-style.yml @@ -0,0 +1,34 @@ +name: test-style + +on: + workflow_dispatch: + pull_request: + branches: + - "**" + +concurrency: + group: test-style-${{ github.ref }} + cancel-in-progress: true + +jobs: + test-style: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + sudo apt-get update && \ + sudo apt-get install cmake + + - name: Install cppwg + run: | + python -m pip install --upgrade pip + python -m pip install .[dev] + + - name: Lint + run: | + python -m flake8 --version + python -m flake8 . diff --git a/.github/workflows/test-typecasters.yml b/.github/workflows/test-typecasters.yml deleted file mode 100644 index 10caf2b..0000000 --- a/.github/workflows/test-typecasters.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: test-typecasters - -on: - workflow_dispatch: - pull_request: - branches: - - "**" - -concurrency: - group: build-and-test-${{ github.ref }} - cancel-in-progress: true - -jobs: - build-and-test: - runs-on: ubuntu-22.04 - - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Install system dependencies - run: | - sudo apt-get update && \ - sudo apt-get install \ - cmake \ - libpetsc-real3.15 \ - libpetsc-real3.15-dbg \ - libpetsc-real3.15-dev \ - libvtk9-dev \ - mpi-default-bin \ - mpi-default-dev \ - python3-mpi4py \ - python3-petsc4py-real3.15 \ - python3-vtk9 \ - vtk9 - - - name: Build cells example - run: | - pip install --user --no-cache -v examples/cells - - - name: Test cells example - run: | - python -m unittest discover -s examples/cells/tests diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index c6f1932..35b0c0b 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -86,7 +86,7 @@ if(SKBUILD) endif() # Target for re-generating wrappers -add_custom_target(wrap +add_custom_target(pycells_wrappers COMMAND ${Python3_EXECUTABLE} -m cppwg ${CMAKE_SOURCE_DIR}/src/cpp -w ${CMAKE_SOURCE_DIR}/dynamic/wrappers -p ${CMAKE_SOURCE_DIR}/dynamic/config.yaml diff --git a/examples/cells/requirements.txt b/examples/cells/requirements.txt new file mode 100644 index 0000000..70c676a --- /dev/null +++ b/examples/cells/requirements.txt @@ -0,0 +1,5 @@ +mpich +mpi4py +petsc +petsc4py +vtk diff --git a/setup.cfg b/setup.cfg index 45b5176..4bafa3a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,7 +36,7 @@ install_requires = [options.entry_points] console_scripts = - executable-name = cppwg.__main__:main + cppwg = cppwg.__main__:main [options.extras_require] dev = diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6068493 --- /dev/null +++ b/setup.py @@ -0,0 +1,3 @@ +from setuptools import setup + +setup() diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_wrapper_generation.py b/tests/test_wrapper_generation.py deleted file mode 100644 index 9213b1b..0000000 --- a/tests/test_wrapper_generation.py +++ /dev/null @@ -1,93 +0,0 @@ -import os -import shutil -import subprocess -import unittest -from difflib import context_diff -from glob import glob - - -def file_diff(file_a: str, file_b: str) -> bool: - """Check if two files have the same content. - - Parameters - __________ - file_a: str - The path to the first file - file__b: str - The path to the second file - - Returns - __________ - str - A diff of the two files - """ - # Read files and remove excess whitespace - with open(file_a, "r") as fa: - a = [line.strip() for line in fa] - a = [line for line in a if line] - - with open(file_b, "r") as fb: - b = [line.strip() for line in fb] - b = [line for line in b if line] - - return "\n".join(context_diff(a, b)) - - -class TestWrapperGeneration(unittest.TestCase): - def setUp(self) -> None: - # Set paths to the shapes code, reference and generated wrappers, etc. - root = os.path.abspath("examples/shapes") - extern = os.path.join(root, "extern") - self.src = os.path.join(root, "src/cpp") - - self.includes = glob(self.src + "/*/") + glob(extern + "/*/") - - self.wrapper = os.path.join(root, "wrapper") - self.wrapper_gen = os.path.join(root, "wrapper.gen") - - self.config = os.path.join(self.wrapper, "package_info.yaml") - self.script = os.path.abspath("cppwg/__main__.py") - - def test_wrapper_generation(self) -> None: - """ - Generate wrappers and compare with the reference wrappers. - """ - self.assertTrue(os.path.isdir(self.src), self.src) - self.assertTrue(os.path.isdir(self.wrapper), self.wrapper) - self.assertTrue(os.path.isfile(self.config), self.config) - - # Generate the wrappers - subprocess.call( - [ - "python", - self.script, - self.src, - "--wrapper_root", - self.wrapper_gen, - "--package_info", - self.config, - "--includes", - ] - + self.includes - ) - - self.assertTrue(os.path.isdir(self.wrapper_gen), self.wrapper_gen) - - # Compare the generated files with reference files - self.maxDiff = None - for dirpath, _, filenames in os.walk(self.wrapper): - for filename in filenames: - if filename.endswith(".cppwg.cpp") or filename.endswith(".cppwg.hpp"): - file_ref = os.path.join(dirpath, filename) - file_gen = file_ref.replace(self.wrapper, self.wrapper_gen, 1) - - self.assertTrue(os.path.isfile(file_ref), file_ref) - self.assertTrue(os.path.isfile(file_gen), file_gen) - self.assertEqual(file_diff(file_gen, file_ref), "", f"\n{file_ref}") - - def tearDown(self) -> None: - shutil.rmtree(self.wrapper_gen) - - -if __name__ == "__main__": - unittest.main() From f7ecccbf04ea3257fd25481cce910eb737b72a31 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 3 Nov 2024 18:39:05 +0000 Subject: [PATCH 18/31] #20 Fix conda testing --- .github/workflows/test-cells.yml | 3 ++- setup.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test-cells.yml b/.github/workflows/test-cells.yml index 07e08a5..f4e91dc 100644 --- a/.github/workflows/test-cells.yml +++ b/.github/workflows/test-cells.yml @@ -41,7 +41,8 @@ jobs: - name: Install cppwg run: | - conda develop . + python -m pip install --upgrade pip + python -m pip install . - name: Install requirements run: | diff --git a/setup.py b/setup.py index 6068493..685c789 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +"""Setup.""" + from setuptools import setup setup() From 9ae603bce25d7ff89c019c7ee33d1db595d3a1c3 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 3 Nov 2024 18:51:08 +0000 Subject: [PATCH 19/31] #20 Fix vtk conda test --- .github/workflows/test-cells.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test-cells.yml b/.github/workflows/test-cells.yml index f4e91dc..e1bc6f8 100644 --- a/.github/workflows/test-cells.yml +++ b/.github/workflows/test-cells.yml @@ -51,8 +51,10 @@ jobs: - name: Configure run: | + export CMAKE_PREFIX_PATH="$CONDA_PREFIX;$CMAKE_PREFIX_PATH" export PETSC_DIR=$CONDA_PREFIX export PETSC_ARCH= + export VTK_DIR=$CONDA_PREFIX mkdir build && cd build cmake .. working-directory: examples/cells From 14701754d6b5cfbb49404164dc37474ffaff73e4 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 3 Nov 2024 23:07:13 +0000 Subject: [PATCH 20/31] #20 Allow conda activation in tests --- .github/workflows/test-cells.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/test-cells.yml b/.github/workflows/test-cells.yml index e1bc6f8..662f2ae 100644 --- a/.github/workflows/test-cells.yml +++ b/.github/workflows/test-cells.yml @@ -19,6 +19,10 @@ jobs: group: test-cells-${{ github.ref }}-${{ matrix.python-version }} cancel-in-progress: true + defaults: + run: + shell: bash -el {0} # -l needed to activate conda + steps: - name: Checkout uses: actions/checkout@v4 From 2e8484fe24ef666c748e4f7565fad80ef338ce95 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 3 Nov 2024 23:18:52 +0000 Subject: [PATCH 21/31] #20 Set config paths for conda tests --- .github/workflows/test-cells.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-cells.yml b/.github/workflows/test-cells.yml index 662f2ae..6914926 100644 --- a/.github/workflows/test-cells.yml +++ b/.github/workflows/test-cells.yml @@ -53,12 +53,15 @@ jobs: mamba install --yes --file requirements.txt working-directory: examples/cells + - name: Set config paths + run: | + echo "CMAKE_PREFIX_PATH=$CONDA_PREFIX;$CMAKE_PREFIX_PATH" >> $GITHUB_ENV + echo "PETSC_DIR=$CONDA_PREFIX" >> $GITHUB_ENV + echo "PETSC_ARCH=" >> $GITHUB_ENV + echo "VTK_DIR=$CONDA_PREFIX" >> $GITHUB_ENV + - name: Configure run: | - export CMAKE_PREFIX_PATH="$CONDA_PREFIX;$CMAKE_PREFIX_PATH" - export PETSC_DIR=$CONDA_PREFIX - export PETSC_ARCH= - export VTK_DIR=$CONDA_PREFIX mkdir build && cd build cmake .. working-directory: examples/cells From 254430a8ac024ecff5558207df165d4083ac5204 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 3 Nov 2024 23:51:33 +0000 Subject: [PATCH 22/31] #20 Fix conda test issues --- .github/workflows/test-cells.yml | 3 +-- README.md | 3 ++- examples/cells/CMakeLists.txt | 20 +++++++++++--------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/test-cells.yml b/.github/workflows/test-cells.yml index 6914926..13a5f81 100644 --- a/.github/workflows/test-cells.yml +++ b/.github/workflows/test-cells.yml @@ -37,7 +37,6 @@ jobs: with: auto-update-conda: true use-mamba: true - miniforge-variant: Mambaforge miniforge-version: latest python-version: ${{ matrix.python-version }} activate-environment: test_cells @@ -80,7 +79,7 @@ jobs: working-directory: examples/cells - name: Build - run: pip install --user --no-cache-dir -v . + run: python -m pip install -v . working-directory: examples/cells - name: Test diff --git a/README.md b/README.md index 29fa9d7..850f794 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -![build](https://github.com/Chaste/cppwg/actions/workflows/build-and-test.yml/badge.svg) +![shapes-example](https://github.com/Chaste/cppwg/actions/workflows/test-shapes.yml/badge.svg) +![cells-example](https://github.com/Chaste/cppwg/actions/workflows/test-cells.yml/badge.svg) # cppwg diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 35b0c0b..4e6a0d1 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -10,12 +10,13 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) find_package(Python3 REQUIRED COMPONENTS Interpreter Development) # Find VTK -find_package(VTK REQUIRED COMPONENTS - vtkCommonCore - vtkRenderingCore - vtkRenderingOpenGL2 - vtkWrappingPythonCore -) +find_package(VTK COMPONENTS CommonCore QUIET) + if(VTK_FOUND) + find_package(VTK REQUIRED COMPONENTS CommonCore RenderingCore RenderingOpenGL2 WrappingPythonCore) + else() + find_package(VTK REQUIRED COMPONENTS vtkCommonCore vtkRenderingCore vtkRenderingOpenGL2 vtkWrappingPythonCore) + endif() +endforeach() # Find PETSc find_package(PETSc REQUIRED) @@ -29,9 +30,10 @@ find_package(MPI REQUIRED COMPONENTS CXX) # Fetch pybind11 include(FetchContent) FetchContent_Declare( - pybind11 - URL https://github.com/pybind/pybind11/archive/refs/tags/v2.10.4.tar.gz - URL_HASH SHA256=832e2f309c57da9c1e6d4542dedd34b24e4192ecb4d62f6f4866a737454c9970 + pybind11 + GIT_REPOSITORY https://github.com/pybind/pybind11 + GIT_TAG v2.10.4 + GIT_SHALLOW 1 ) FetchContent_MakeAvailable(pybind11) From c7172ffe6157e3ffe5993b8e6839277e7a93ab7c Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sun, 3 Nov 2024 23:54:47 +0000 Subject: [PATCH 23/31] #20 Fix cells cmake --- examples/cells/CMakeLists.txt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 4e6a0d1..2275d27 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -11,12 +11,11 @@ find_package(Python3 REQUIRED COMPONENTS Interpreter Development) # Find VTK find_package(VTK COMPONENTS CommonCore QUIET) - if(VTK_FOUND) - find_package(VTK REQUIRED COMPONENTS CommonCore RenderingCore RenderingOpenGL2 WrappingPythonCore) - else() - find_package(VTK REQUIRED COMPONENTS vtkCommonCore vtkRenderingCore vtkRenderingOpenGL2 vtkWrappingPythonCore) - endif() -endforeach() +if(VTK_FOUND) + find_package(VTK REQUIRED COMPONENTS CommonCore RenderingCore RenderingOpenGL2 WrappingPythonCore) +else() + find_package(VTK REQUIRED COMPONENTS vtkCommonCore vtkRenderingCore vtkRenderingOpenGL2 vtkWrappingPythonCore) +endif() # Find PETSc find_package(PETSc REQUIRED) From e6e82c87f22191cbd4aeec1bdc87218b8c46d608 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Mon, 4 Nov 2024 18:18:36 +0000 Subject: [PATCH 24/31] #20 Add conda recipe --- examples/cells/CMakeLists.txt | 16 +++++++++--- examples/cells/conda-recipe/build.sh | 3 +++ examples/cells/conda-recipe/meta.yaml | 36 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 examples/cells/conda-recipe/build.sh create mode 100644 examples/cells/conda-recipe/meta.yaml diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 2275d27..22be15e 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -10,13 +10,21 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) find_package(Python3 REQUIRED COMPONENTS Interpreter Development) # Find VTK -find_package(VTK COMPONENTS CommonCore QUIET) -if(VTK_FOUND) - find_package(VTK REQUIRED COMPONENTS CommonCore RenderingCore RenderingOpenGL2 WrappingPythonCore) -else() +foreach(_core_module vtkCommonCore CommonCore) + find_package(VTK COMPONENTS ${_core_module} QUIET) + if(VTK_FOUND) + break() + endif() +endforeach() + +if(VTK_VERSION VERSION_LESS 9.0.0) find_package(VTK REQUIRED COMPONENTS vtkCommonCore vtkRenderingCore vtkRenderingOpenGL2 vtkWrappingPythonCore) +else() + find_package(VTK REQUIRED COMPONENTS CommonCore RenderingCore RenderingOpenGL2 WrappingPythonCore) endif() +message(STATUS "VTK version: ${VTK_VERSION}") + # Find PETSc find_package(PETSc REQUIRED) diff --git a/examples/cells/conda-recipe/build.sh b/examples/cells/conda-recipe/build.sh new file mode 100644 index 0000000..a1c55e8 --- /dev/null +++ b/examples/cells/conda-recipe/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash -ex + +$PYTHON -m pip install . diff --git a/examples/cells/conda-recipe/meta.yaml b/examples/cells/conda-recipe/meta.yaml new file mode 100644 index 0000000..d686744 --- /dev/null +++ b/examples/cells/conda-recipe/meta.yaml @@ -0,0 +1,36 @@ +{% set version = "0.0.1" %} +{% set build = 0 %} + +package: + name: pycells + version: {{ version }} + +source: + path: .. + +build: + number: {{ build }} + +requirements: + build: + - cmake + - ninja + - pip + - python + - scikit-build + - setuptools + + host: + - mpi4py + - mpich + - petsc + - petsc4py + - python + - vtk + +test: + imports: + - pycells + +about: + license: BSD-3-Clause From 7bff31e68fa75d931aca4990e600a06719de71a5 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Tue, 5 Nov 2024 09:55:56 +0000 Subject: [PATCH 25/31] #20 Rework cells example tests --- .../{test-cells.yml => test-cells-conda.yml} | 8 ++- .github/workflows/test-cells-ubuntu.yml | 68 +++++++++++++++++++ .../{test-shapes.yml => test-shapes-pip.yml} | 8 +-- README.md | 5 +- 4 files changed, 81 insertions(+), 8 deletions(-) rename .github/workflows/{test-cells.yml => test-cells-conda.yml} (92%) create mode 100644 .github/workflows/test-cells-ubuntu.yml rename .github/workflows/{test-shapes.yml => test-shapes-pip.yml} (91%) diff --git a/.github/workflows/test-cells.yml b/.github/workflows/test-cells-conda.yml similarity index 92% rename from .github/workflows/test-cells.yml rename to .github/workflows/test-cells-conda.yml index 13a5f81..ec34de4 100644 --- a/.github/workflows/test-cells.yml +++ b/.github/workflows/test-cells-conda.yml @@ -30,7 +30,7 @@ jobs: - name: Install system dependencies run: | sudo apt-get update - sudo apt-get install cmake + sudo apt-get install cmake git - name: Setup Miniconda Python ${{ matrix.python-version }} uses: conda-incubator/setup-miniconda@v3 @@ -79,7 +79,11 @@ jobs: working-directory: examples/cells - name: Build - run: python -m pip install -v . + run: conda build conda-recipe + working-directory: examples/cells + + - name: Install + run: conda install --use-local pycells working-directory: examples/cells - name: Test diff --git a/.github/workflows/test-cells-ubuntu.yml b/.github/workflows/test-cells-ubuntu.yml new file mode 100644 index 0000000..6adde86 --- /dev/null +++ b/.github/workflows/test-cells-ubuntu.yml @@ -0,0 +1,68 @@ +name: test-cells-ubuntu + +on: + workflow_dispatch: + pull_request: + branches: + - "**" + +concurrency: + group: test-cells-ubuntu-${{ github.ref }} + cancel-in-progress: true + +jobs: + test-cells-ubuntu: + runs-on: ubuntu-22.04 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get install \ + cmake \ + git \ + libpetsc-real3.15 \ + libpetsc-real3.15-dbg \ + libpetsc-real3.15-dev \ + libvtk9-dev \ + mpi-default-bin \ + mpi-default-dev \ + python3-mpi4py \ + python3-petsc4py-real3.15 \ + python3-vtk9 \ + vtk9 + + - name: Install cppwg + run: | + python -m pip install --upgrade pip + python -m pip install . + + - name: Configure + run: | + mkdir build && cd build + cmake .. + working-directory: examples/cells + + - name: Regenerate wrappers + run: | + rm -rf dynamic/wrappers + cd build + make pycells_wrappers + working-directory: examples/cells + + - name: Check for changes + run: | + git diff --exit-code dynamic/wrappers + grep "Unknown class" build/cppwg.log + working-directory: examples/cells + + - name: Build + run: python -m pip install -v . + working-directory: examples/cells + + - name: Test + run: python -m unittest discover -s tests + working-directory: examples/cells diff --git a/.github/workflows/test-shapes.yml b/.github/workflows/test-shapes-pip.yml similarity index 91% rename from .github/workflows/test-shapes.yml rename to .github/workflows/test-shapes-pip.yml index 4c78676..bea7477 100644 --- a/.github/workflows/test-shapes.yml +++ b/.github/workflows/test-shapes-pip.yml @@ -1,4 +1,4 @@ -name: test-shapes +name: test-shapes-pip on: workflow_dispatch: @@ -7,7 +7,7 @@ on: - "**" jobs: - test-shapes: + test-shapes-pip: runs-on: ubuntu-latest strategy: @@ -16,7 +16,7 @@ jobs: python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] concurrency: - group: test-shapes-${{ github.ref }}-${{ matrix.python-version }} + group: test-shapes-pip-${{ github.ref }}-${{ matrix.python-version }} cancel-in-progress: true steps: @@ -26,7 +26,7 @@ jobs: - name: Install system dependencies run: | sudo apt-get update - sudo apt-get install cmake + sudo apt-get install cmake git - name: Setup Python ${{ matrix.python-version }} uses: actions/setup-python@v5 diff --git a/README.md b/README.md index 850f794..6bfddc8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ -![shapes-example](https://github.com/Chaste/cppwg/actions/workflows/test-shapes.yml/badge.svg) -![cells-example](https://github.com/Chaste/cppwg/actions/workflows/test-cells.yml/badge.svg) +![conda](https://github.com/Chaste/cppwg/actions/workflows/test-cells-conda.yml/badge.svg) +![pip](https://github.com/Chaste/cppwg/actions/workflows/test-shapes-pip.yml/badge.svg) +![ubuntu](https://github.com/Chaste/cppwg/actions/workflows/test-cells-ubuntu.yml/badge.svg) # cppwg From 1e82dc242c63dac60127575a485daaf69e55eb52 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 6 Nov 2024 14:50:59 +0000 Subject: [PATCH 26/31] #20 Switch to scikit-build-core --- examples/cells/CMakeLists.txt | 2 +- examples/cells/pyproject.toml | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index 22be15e..e9db84b 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -39,7 +39,7 @@ include(FetchContent) FetchContent_Declare( pybind11 GIT_REPOSITORY https://github.com/pybind/pybind11 - GIT_TAG v2.10.4 + GIT_TAG v2.13.6 GIT_SHALLOW 1 ) FetchContent_MakeAvailable(pybind11) diff --git a/examples/cells/pyproject.toml b/examples/cells/pyproject.toml index 0ed003f..3b7db8c 100644 --- a/examples/cells/pyproject.toml +++ b/examples/cells/pyproject.toml @@ -1,11 +1,14 @@ [build-system] -requires = [ - "cmake", - "ninja", - "scikit-build", - "setuptools", -] -build-backend = "setuptools.build_meta" +requires = ["scikit-build-core >= 0.10.0"] +build-backend = "scikit_build_core.build" + +[project] +name = "pycells" +version = "0.0.1" +license = { text = "BSD-3-Clause License" } +requires-python = ">=3.8" [tool.scikit-build] cmake.build-type = "Release" +wheel.packages = ["src/py/pycells"] +wheel.install-dir = "pycells" From 659fca198395bf2f07dde01cf311a34f93db5be4 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 6 Nov 2024 19:06:01 +0000 Subject: [PATCH 27/31] #20 Add conda variants for cells example --- .github/workflows/test-cells-conda.yml | 22 +++++++++--------- .github/workflows/test-cells-ubuntu.yml | 2 +- examples/cells/conda-recipe/build.sh | 3 --- examples/cells/conda/recipe/build.sh | 8 +++++++ .../{conda-recipe => conda/recipe}/meta.yaml | 14 ++++------- examples/cells/conda/variants/python3.10.yaml | 2 ++ examples/cells/conda/variants/python3.11.yaml | 2 ++ examples/cells/conda/variants/python3.12.yaml | 2 ++ examples/cells/conda/variants/python3.13.yaml | 2 ++ examples/cells/conda/variants/python3.8.yaml | 2 ++ examples/cells/conda/variants/python3.9.yaml | 2 ++ examples/cells/requirements.txt | 2 +- examples/cells/src/cpp/utils/PetscUtils.cpp | 23 +++++++++++++------ 13 files changed, 53 insertions(+), 33 deletions(-) delete mode 100644 examples/cells/conda-recipe/build.sh create mode 100644 examples/cells/conda/recipe/build.sh rename examples/cells/{conda-recipe => conda/recipe}/meta.yaml (73%) create mode 100644 examples/cells/conda/variants/python3.10.yaml create mode 100644 examples/cells/conda/variants/python3.11.yaml create mode 100644 examples/cells/conda/variants/python3.12.yaml create mode 100644 examples/cells/conda/variants/python3.13.yaml create mode 100644 examples/cells/conda/variants/python3.8.yaml create mode 100644 examples/cells/conda/variants/python3.9.yaml diff --git a/.github/workflows/test-cells-conda.yml b/.github/workflows/test-cells-conda.yml index ec34de4..3047c8c 100644 --- a/.github/workflows/test-cells-conda.yml +++ b/.github/workflows/test-cells-conda.yml @@ -1,4 +1,4 @@ -name: test-cells +name: test-cells-conda on: workflow_dispatch: @@ -7,7 +7,7 @@ on: - "**" jobs: - test-cells: + test-cells-conda: runs-on: ubuntu-latest strategy: @@ -16,7 +16,7 @@ jobs: python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] concurrency: - group: test-cells-${{ github.ref }}-${{ matrix.python-version }} + group: test-cells-conda-${{ github.ref }}-${{ matrix.python-version }} cancel-in-progress: true defaults: @@ -39,7 +39,6 @@ jobs: use-mamba: true miniforge-version: latest python-version: ${{ matrix.python-version }} - activate-environment: test_cells channels: conda-forge - name: Install cppwg @@ -48,8 +47,7 @@ jobs: python -m pip install . - name: Install requirements - run: | - mamba install --yes --file requirements.txt + run: mamba install --yes --file requirements.txt working-directory: examples/cells - name: Set config paths @@ -78,14 +76,16 @@ jobs: grep "Unknown class" build/cppwg.log working-directory: examples/cells + - name: Install conda-build tools + run: mamba install boa conda-build conda-verify + - name: Build - run: conda build conda-recipe - working-directory: examples/cells + run: conda mambabuild recipe -m variants/python${{ matrix.python-version }}.yaml + working-directory: examples/cells/conda - name: Install - run: conda install --use-local pycells - working-directory: examples/cells + run: mamba install --use-local pycells - name: Test - run: python -m unittest discover -s tests + run: python -m unittest discover tests working-directory: examples/cells diff --git a/.github/workflows/test-cells-ubuntu.yml b/.github/workflows/test-cells-ubuntu.yml index 6adde86..5b36558 100644 --- a/.github/workflows/test-cells-ubuntu.yml +++ b/.github/workflows/test-cells-ubuntu.yml @@ -64,5 +64,5 @@ jobs: working-directory: examples/cells - name: Test - run: python -m unittest discover -s tests + run: python -m unittest discover tests working-directory: examples/cells diff --git a/examples/cells/conda-recipe/build.sh b/examples/cells/conda-recipe/build.sh deleted file mode 100644 index a1c55e8..0000000 --- a/examples/cells/conda-recipe/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -ex - -$PYTHON -m pip install . diff --git a/examples/cells/conda/recipe/build.sh b/examples/cells/conda/recipe/build.sh new file mode 100644 index 0000000..7dd38b3 --- /dev/null +++ b/examples/cells/conda/recipe/build.sh @@ -0,0 +1,8 @@ +#!/bin/bash -ex + +export CMAKE_PREFIX_PATH="$PREFIX;$CMAKE_PREFIX_PATH" +export PETSC_DIR="$PREFIX" +export PETSC_ARCH= +export VTK_DIR="$PREFIX" + +$PYTHON -m pip install -v --no-build-isolation . diff --git a/examples/cells/conda-recipe/meta.yaml b/examples/cells/conda/recipe/meta.yaml similarity index 73% rename from examples/cells/conda-recipe/meta.yaml rename to examples/cells/conda/recipe/meta.yaml index d686744..0e982f6 100644 --- a/examples/cells/conda-recipe/meta.yaml +++ b/examples/cells/conda/recipe/meta.yaml @@ -6,26 +6,20 @@ package: version: {{ version }} source: - path: .. + path: ../.. build: number: {{ build }} requirements: - build: - - cmake - - ninja - - pip - - python - - scikit-build - - setuptools - host: - mpi4py - - mpich + - openmpi - petsc - petsc4py + - pip - python + - scikit-build-core - vtk test: diff --git a/examples/cells/conda/variants/python3.10.yaml b/examples/cells/conda/variants/python3.10.yaml new file mode 100644 index 0000000..49527e1 --- /dev/null +++ b/examples/cells/conda/variants/python3.10.yaml @@ -0,0 +1,2 @@ +python: + - 3.10.* diff --git a/examples/cells/conda/variants/python3.11.yaml b/examples/cells/conda/variants/python3.11.yaml new file mode 100644 index 0000000..b2289ba --- /dev/null +++ b/examples/cells/conda/variants/python3.11.yaml @@ -0,0 +1,2 @@ +python: + - 3.11.* diff --git a/examples/cells/conda/variants/python3.12.yaml b/examples/cells/conda/variants/python3.12.yaml new file mode 100644 index 0000000..1df82ad --- /dev/null +++ b/examples/cells/conda/variants/python3.12.yaml @@ -0,0 +1,2 @@ +python: + - 3.12.* diff --git a/examples/cells/conda/variants/python3.13.yaml b/examples/cells/conda/variants/python3.13.yaml new file mode 100644 index 0000000..13edda7 --- /dev/null +++ b/examples/cells/conda/variants/python3.13.yaml @@ -0,0 +1,2 @@ +python: + - 3.13.* diff --git a/examples/cells/conda/variants/python3.8.yaml b/examples/cells/conda/variants/python3.8.yaml new file mode 100644 index 0000000..b34e9cc --- /dev/null +++ b/examples/cells/conda/variants/python3.8.yaml @@ -0,0 +1,2 @@ +python: + - 3.8.* diff --git a/examples/cells/conda/variants/python3.9.yaml b/examples/cells/conda/variants/python3.9.yaml new file mode 100644 index 0000000..f305cf2 --- /dev/null +++ b/examples/cells/conda/variants/python3.9.yaml @@ -0,0 +1,2 @@ +python: + - 3.9.* diff --git a/examples/cells/requirements.txt b/examples/cells/requirements.txt index 70c676a..6f5e733 100644 --- a/examples/cells/requirements.txt +++ b/examples/cells/requirements.txt @@ -1,4 +1,4 @@ -mpich +openmpi mpi4py petsc petsc4py diff --git a/examples/cells/src/cpp/utils/PetscUtils.cpp b/examples/cells/src/cpp/utils/PetscUtils.cpp index 4caf0e5..eb16b08 100644 --- a/examples/cells/src/cpp/utils/PetscUtils.cpp +++ b/examples/cells/src/cpp/utils/PetscUtils.cpp @@ -12,7 +12,11 @@ void PetscUtils::Initialise() { if (!PetscUtils::IsInitialised()) { +#if PETSC_VERSION_GE(3, 19, 0) + PetscInitialize(PETSC_NULLPTR, PETSC_NULLPTR, PETSC_NULLPTR, PETSC_NULLPTR); +#else PetscInitialize(PETSC_NULL, PETSC_NULL, PETSC_NULL, PETSC_NULL); +#endif } } @@ -27,7 +31,7 @@ int PetscUtils::GetSize() { if (!PetscUtils::IsInitialised()) { - return -1; + PetscUtils::Initialise(); } PetscInt size; @@ -39,7 +43,7 @@ int PetscUtils::GetRank() { if (!PetscUtils::IsInitialised()) { - return -1; + PetscUtils::Initialise(); } PetscInt rank; @@ -49,9 +53,14 @@ int PetscUtils::GetRank() Vec PetscUtils::CreateVec(int size) { - Vec vec; - VecCreate(PETSC_COMM_WORLD, &vec); - VecSetSizes(vec, PETSC_DECIDE, size); - VecSetType(vec, VECMPI); - return vec; + if (!PetscUtils::IsInitialised()) + { + PetscUtils::Initialise(); + } + + Vec v; + VecCreate(PETSC_COMM_WORLD, &v); + VecSetSizes(v, PETSC_DECIDE, size); + VecSetFromOptions(v); + return v; } From 83d1b1853cbb3ad29e3121deed9d266cd32771aa Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 6 Nov 2024 19:15:24 +0000 Subject: [PATCH 28/31] #20 Update conda build example for python 3.13 --- .github/workflows/test-cells-conda.yml | 2 +- examples/cells/requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-cells-conda.yml b/.github/workflows/test-cells-conda.yml index 3047c8c..1dbbdb0 100644 --- a/.github/workflows/test-cells-conda.yml +++ b/.github/workflows/test-cells-conda.yml @@ -77,7 +77,7 @@ jobs: working-directory: examples/cells - name: Install conda-build tools - run: mamba install boa conda-build conda-verify + run: mamba install boa conda-verify - name: Build run: conda mambabuild recipe -m variants/python${{ matrix.python-version }}.yaml diff --git a/examples/cells/requirements.txt b/examples/cells/requirements.txt index 6f5e733..61dc56c 100644 --- a/examples/cells/requirements.txt +++ b/examples/cells/requirements.txt @@ -1,5 +1,5 @@ -openmpi mpi4py +openmpi petsc petsc4py vtk From 71ae42573855393f615c85b07eaa84b1bab5a567 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Wed, 6 Nov 2024 22:51:32 +0000 Subject: [PATCH 29/31] #20 Remove missing conda-build for python 3.13 from CI --- .github/workflows/test-cells-conda.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-cells-conda.yml b/.github/workflows/test-cells-conda.yml index 1dbbdb0..eee41ab 100644 --- a/.github/workflows/test-cells-conda.yml +++ b/.github/workflows/test-cells-conda.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] concurrency: group: test-cells-conda-${{ github.ref }}-${{ matrix.python-version }} @@ -77,7 +77,7 @@ jobs: working-directory: examples/cells - name: Install conda-build tools - run: mamba install boa conda-verify + run: mamba install boa conda-build conda-verify - name: Build run: conda mambabuild recipe -m variants/python${{ matrix.python-version }}.yaml From 471b701b6569f137bbe359d731f6fa74684b4f03 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Thu, 7 Nov 2024 09:37:50 +0000 Subject: [PATCH 30/31] #20 Remove cells setup.py --- README.md | 2 +- examples/cells/setup.py | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) delete mode 100644 examples/cells/setup.py diff --git a/README.md b/README.md index 6bfddc8..be70715 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -![conda](https://github.com/Chaste/cppwg/actions/workflows/test-cells-conda.yml/badge.svg) ![pip](https://github.com/Chaste/cppwg/actions/workflows/test-shapes-pip.yml/badge.svg) ![ubuntu](https://github.com/Chaste/cppwg/actions/workflows/test-cells-ubuntu.yml/badge.svg) +![conda](https://github.com/Chaste/cppwg/actions/workflows/test-cells-conda.yml/badge.svg) # cppwg diff --git a/examples/cells/setup.py b/examples/cells/setup.py deleted file mode 100644 index c79cca2..0000000 --- a/examples/cells/setup.py +++ /dev/null @@ -1,13 +0,0 @@ -from skbuild import setup - -setup( - name="pycells", - version="0.0.1", - description="pycells", - author="kna", - license="BSD-3-Clause", - packages=["pycells"], - package_dir={"": "src/py"}, - cmake_install_dir="src/py/pycells", - python_requires=">=3.8", -) From 5f507f7c6ba998b8f2ad653ccc945e778508bd9a Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Thu, 7 Nov 2024 12:29:32 +0000 Subject: [PATCH 31/31] #20 Add ublas typecaster to cells --- .github/workflows/test-cells-conda.yml | 2 +- .github/workflows/test-cells-ubuntu.yml | 1 + examples/cells/CMakeLists.txt | 10 +- examples/cells/conda/recipe/meta.yaml | 1 + examples/cells/{ => conda}/requirements.txt | 1 + examples/cells/dynamic/config.yaml | 5 + .../typecasters/PybindUblasTypeCaster.hpp | 97 +++++++++++++++++++ .../thirdparty/dolfinx/COPYING | 0 .../thirdparty/dolfinx/COPYING.LESSER | 0 .../thirdparty/dolfinx/caster_petsc.h | 5 +- .../thirdparty/smtk/LICENSE.txt | 0 .../thirdparty/smtk/PybindVTKTypeCaster.h | 0 .../dynamic/wrappers/all/Node_2.cppwg.cpp | 8 ++ .../dynamic/wrappers/all/Node_3.cppwg.cpp | 8 ++ examples/cells/src/cpp/mesh/Node.cpp | 27 ++++++ examples/cells/src/cpp/mesh/Node.hpp | 21 +++- examples/cells/src/py/pycells/__init__.py | 14 +++ examples/cells/src/py/pycells/_syntax.py | 2 + examples/cells/tests/test_cells.py | 8 +- 19 files changed, 203 insertions(+), 7 deletions(-) rename examples/cells/{ => conda}/requirements.txt (77%) create mode 100644 examples/cells/dynamic/typecasters/PybindUblasTypeCaster.hpp rename examples/cells/dynamic/{ => typecasters}/thirdparty/dolfinx/COPYING (100%) rename examples/cells/dynamic/{ => typecasters}/thirdparty/dolfinx/COPYING.LESSER (100%) rename examples/cells/dynamic/{ => typecasters}/thirdparty/dolfinx/caster_petsc.h (98%) rename examples/cells/dynamic/{ => typecasters}/thirdparty/smtk/LICENSE.txt (100%) rename examples/cells/dynamic/{ => typecasters}/thirdparty/smtk/PybindVTKTypeCaster.h (100%) diff --git a/.github/workflows/test-cells-conda.yml b/.github/workflows/test-cells-conda.yml index eee41ab..5dfb488 100644 --- a/.github/workflows/test-cells-conda.yml +++ b/.github/workflows/test-cells-conda.yml @@ -47,7 +47,7 @@ jobs: python -m pip install . - name: Install requirements - run: mamba install --yes --file requirements.txt + run: mamba install --yes --file conda/requirements.txt working-directory: examples/cells - name: Set config paths diff --git a/.github/workflows/test-cells-ubuntu.yml b/.github/workflows/test-cells-ubuntu.yml index 5b36558..0e8545a 100644 --- a/.github/workflows/test-cells-ubuntu.yml +++ b/.github/workflows/test-cells-ubuntu.yml @@ -24,6 +24,7 @@ jobs: sudo apt-get install \ cmake \ git \ + libboost-all-dev \ libpetsc-real3.15 \ libpetsc-real3.15-dbg \ libpetsc-real3.15-dev \ diff --git a/examples/cells/CMakeLists.txt b/examples/cells/CMakeLists.txt index e9db84b..e67d6d3 100644 --- a/examples/cells/CMakeLists.txt +++ b/examples/cells/CMakeLists.txt @@ -9,6 +9,9 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) # Find Python find_package(Python3 REQUIRED COMPONENTS Interpreter Development) +# Find Boost +find_package (Boost REQUIRED) + # Find VTK foreach(_core_module vtkCommonCore CommonCore) find_package(VTK COMPONENTS ${_core_module} QUIET) @@ -51,6 +54,7 @@ file(GLOB_RECURSE MAIN_SOURCES ) add_library(cells SHARED ${MAIN_SOURCES}) target_link_libraries(cells PUBLIC + ${Boost_LIBRARIES} ${VTK_LIBRARIES} ${PETSC_LIBRARIES} ${MPI_C_LIBRARIES} @@ -62,6 +66,7 @@ target_include_directories(cells PUBLIC ${CMAKE_SOURCE_DIR}/src/cpp/mesh ${CMAKE_SOURCE_DIR}/src/cpp/utils ${CMAKE_SOURCE_DIR}/src/cpp/visualization + ${Boost_INCLUDE_DIR} ${VTK_INCLUDE_DIRS} ${PETSC_INCLUDES} ${MPI_C_INCLUDE_PATH} @@ -77,8 +82,9 @@ pybind11_add_module(_pycells_all MODULE ${WRAPPER_SOURCES}) target_link_libraries(_pycells_all PRIVATE cells) target_include_directories(_pycells_all PRIVATE ${CMAKE_SOURCE_DIR}/dynamic/wrappers/all - ${CMAKE_SOURCE_DIR}/dynamic/thirdparty/dolfinx - ${CMAKE_SOURCE_DIR}/dynamic/thirdparty/smtk + ${CMAKE_SOURCE_DIR}/dynamic/typecasters/ + ${CMAKE_SOURCE_DIR}/dynamic/typecasters/thirdparty/dolfinx + ${CMAKE_SOURCE_DIR}/dynamic/typecasters/thirdparty/smtk ${PETSC4PY_INCLUDES} ) diff --git a/examples/cells/conda/recipe/meta.yaml b/examples/cells/conda/recipe/meta.yaml index 0e982f6..9095ded 100644 --- a/examples/cells/conda/recipe/meta.yaml +++ b/examples/cells/conda/recipe/meta.yaml @@ -13,6 +13,7 @@ build: requirements: host: + - boost-cpp - mpi4py - openmpi - petsc diff --git a/examples/cells/requirements.txt b/examples/cells/conda/requirements.txt similarity index 77% rename from examples/cells/requirements.txt rename to examples/cells/conda/requirements.txt index 61dc56c..6d4615c 100644 --- a/examples/cells/requirements.txt +++ b/examples/cells/conda/requirements.txt @@ -1,3 +1,4 @@ +boost-cpp mpi4py openmpi petsc diff --git a/examples/cells/dynamic/config.yaml b/examples/cells/dynamic/config.yaml index 00c15f7..044db50 100644 --- a/examples/cells/dynamic/config.yaml +++ b/examples/cells/dynamic/config.yaml @@ -35,13 +35,18 @@ modules: # mesh - name: AbstractMesh + - name: MeshFactory source_includes: - PottsMesh.hpp template_substitutions: - signature: replacement: [["PottsMesh<2>"], ["PottsMesh<3>"]] + - name: Node + source_includes: + - PybindUblasTypeCaster.hpp + - name: PottsMesh # utils diff --git a/examples/cells/dynamic/typecasters/PybindUblasTypeCaster.hpp b/examples/cells/dynamic/typecasters/PybindUblasTypeCaster.hpp new file mode 100644 index 0000000..64ccc6d --- /dev/null +++ b/examples/cells/dynamic/typecasters/PybindUblasTypeCaster.hpp @@ -0,0 +1,97 @@ +/* + +Copyright (c) 2005-2024, University of Oxford. +All rights reserved. + +University of Oxford means the Chancellor, Masters and Scholars of the +University of Oxford, having an administrative office at Wellington +Square, Oxford OX1 2JD, UK. + +This file is part of Chaste. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the University of Oxford nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef PYBINDUBLASTYPECASTER_HPP_ +#define PYBINDUBLASTYPECASTER_HPP_ + +#include + +#include +#include +#include + +#define PYBIND11_CVECTOR_TYPECASTER(T, N) \ + namespace pybind11 \ + { \ + namespace detail \ + { \ + template <> \ + struct type_caster > \ + { \ + public: \ + typedef boost::numeric::ublas::c_vector c_vector_##T##_##N##_t; \ + PYBIND11_TYPE_CASTER(c_vector_##T##_##N##_t, const_name("c_vector_" #T "_" #N "_t")); \ + bool load(handle src, bool convert) \ + { \ + if (!convert && !array_t::check_(src)) \ + { \ + return false; \ + } \ + auto buf = array_t::ensure(src); \ + if (!buf) \ + { \ + return false; \ + } \ + if (buf.ndim() != 1 or buf.shape()[0] != N) \ + { \ + return false; \ + } \ + value.resize(N); \ + for (unsigned i = 0; i < N; ++i) \ + { \ + value[i] = buf.data()[i]; \ + } \ + return true; \ + } \ + static handle cast(const boost::numeric::ublas::c_vector& src, \ + return_value_policy, \ + handle) \ + { \ + std::vector shape(1, N); \ + std::vector strides(1, sizeof(T)); \ + T* data = src.size() ? const_cast(&src[0]) : static_cast(NULL); \ + array a(std::move(shape), std::move(strides), data); \ + return a.release(); \ + } \ + }; \ + } \ + } + +PYBIND11_CVECTOR_TYPECASTER(double, 2); +PYBIND11_CVECTOR_TYPECASTER(double, 3); + +#undef PYBIND11_CVECTOR_TYPECASTER + +#endif // PYBINDUBLASTYPECASTER_HPP_ diff --git a/examples/cells/dynamic/thirdparty/dolfinx/COPYING b/examples/cells/dynamic/typecasters/thirdparty/dolfinx/COPYING similarity index 100% rename from examples/cells/dynamic/thirdparty/dolfinx/COPYING rename to examples/cells/dynamic/typecasters/thirdparty/dolfinx/COPYING diff --git a/examples/cells/dynamic/thirdparty/dolfinx/COPYING.LESSER b/examples/cells/dynamic/typecasters/thirdparty/dolfinx/COPYING.LESSER similarity index 100% rename from examples/cells/dynamic/thirdparty/dolfinx/COPYING.LESSER rename to examples/cells/dynamic/typecasters/thirdparty/dolfinx/COPYING.LESSER diff --git a/examples/cells/dynamic/thirdparty/dolfinx/caster_petsc.h b/examples/cells/dynamic/typecasters/thirdparty/dolfinx/caster_petsc.h similarity index 98% rename from examples/cells/dynamic/thirdparty/dolfinx/caster_petsc.h rename to examples/cells/dynamic/typecasters/thirdparty/dolfinx/caster_petsc.h index 0faa0dc..0b926f1 100644 --- a/examples/cells/dynamic/thirdparty/dolfinx/caster_petsc.h +++ b/examples/cells/dynamic/typecasters/thirdparty/dolfinx/caster_petsc.h @@ -4,7 +4,8 @@ // // SPDX-License-Identifier: LGPL-3.0-or-later -#pragma once +#ifndef CASTER_PETSC_H_ +#define CASTER_PETSC_H_ #include #include @@ -78,3 +79,5 @@ namespace pybind11::detail PETSC_CASTER_MACRO(Mat, Mat, mat); PETSC_CASTER_MACRO(Vec, Vec, vec); } // namespace pybind11::detail + +#endif // CASTER_PETSC_H_ diff --git a/examples/cells/dynamic/thirdparty/smtk/LICENSE.txt b/examples/cells/dynamic/typecasters/thirdparty/smtk/LICENSE.txt similarity index 100% rename from examples/cells/dynamic/thirdparty/smtk/LICENSE.txt rename to examples/cells/dynamic/typecasters/thirdparty/smtk/LICENSE.txt diff --git a/examples/cells/dynamic/thirdparty/smtk/PybindVTKTypeCaster.h b/examples/cells/dynamic/typecasters/thirdparty/smtk/PybindVTKTypeCaster.h similarity index 100% rename from examples/cells/dynamic/thirdparty/smtk/PybindVTKTypeCaster.h rename to examples/cells/dynamic/typecasters/thirdparty/smtk/PybindVTKTypeCaster.h diff --git a/examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp b/examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp index 074b342..3db7b00 100644 --- a/examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp +++ b/examples/cells/dynamic/wrappers/all/Node_2.cppwg.cpp @@ -2,6 +2,7 @@ #include #include +#include "PybindUblasTypeCaster.hpp" #include #include "Node.hpp" @@ -16,8 +17,15 @@ void register_Node_2_class(py::module &m) py::class_>(m, "Node_2") .def(py::init<>()) .def(py::init<::std::vector>(), py::arg("coords")) + .def(py::init<::boost::numeric::ublas::c_vector>(), py::arg("coords")) .def("GetIndex", (unsigned int(Node_2::*)() const) &Node_2::GetIndex, " ") + .def("GetLocation", + (::boost::numeric::ublas::c_vector(Node_2::*)()) &Node_2::GetLocation, + " ") + .def("Translate", + (void(Node_2::*)(::boost::numeric::ublas::c_vector const &)) &Node_2::Translate, + " ", py::arg("rDisplacement")) ; } diff --git a/examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp b/examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp index 61b52b3..d6149b9 100644 --- a/examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp +++ b/examples/cells/dynamic/wrappers/all/Node_3.cppwg.cpp @@ -2,6 +2,7 @@ #include #include +#include "PybindUblasTypeCaster.hpp" #include #include "Node.hpp" @@ -16,8 +17,15 @@ void register_Node_3_class(py::module &m) py::class_>(m, "Node_3") .def(py::init<>()) .def(py::init<::std::vector>(), py::arg("coords")) + .def(py::init<::boost::numeric::ublas::c_vector>(), py::arg("coords")) .def("GetIndex", (unsigned int(Node_3::*)() const) &Node_3::GetIndex, " ") + .def("GetLocation", + (::boost::numeric::ublas::c_vector(Node_3::*)()) &Node_3::GetLocation, + " ") + .def("Translate", + (void(Node_3::*)(::boost::numeric::ublas::c_vector const &)) &Node_3::Translate, + " ", py::arg("rDisplacement")) ; } diff --git a/examples/cells/src/cpp/mesh/Node.cpp b/examples/cells/src/cpp/mesh/Node.cpp index b2d7917..8a2f675 100644 --- a/examples/cells/src/cpp/mesh/Node.cpp +++ b/examples/cells/src/cpp/mesh/Node.cpp @@ -1,5 +1,7 @@ #include "Node.hpp" +#include + #include #include @@ -19,6 +21,16 @@ Node::Node(std::vector coords) } } +template +Node::Node(boost::numeric::ublas::c_vector coords) + : mIndex(0), mLocation() +{ + for (unsigned i = 0; i < SPACE_DIM; ++i) + { + mLocation[i] = coords[i]; + } +} + template Node::~Node() { @@ -30,5 +42,20 @@ unsigned Node::GetIndex() const return mIndex; } +template +boost::numeric::ublas::c_vector Node::GetLocation() +{ + return mLocation; +} + +template +void Node::Translate(const boost::numeric::ublas::c_vector &rDisplacement) +{ + for (unsigned i = 0; i < SPACE_DIM; ++i) + { + mLocation[i] += rDisplacement[i]; + } +} + template class Node<2>; template class Node<3>; diff --git a/examples/cells/src/cpp/mesh/Node.hpp b/examples/cells/src/cpp/mesh/Node.hpp index 5c95820..e995d15 100644 --- a/examples/cells/src/cpp/mesh/Node.hpp +++ b/examples/cells/src/cpp/mesh/Node.hpp @@ -1,6 +1,8 @@ #ifndef _NODE_HPP_ #define _NODE_HPP_ +#include + #include #include @@ -19,7 +21,7 @@ class Node /** * Node location */ - std::array mLocation; + boost::numeric::ublas::c_vector mLocation; public: /** @@ -28,10 +30,15 @@ class Node Node(); /** - * Constructor with coordinates + * Constructor with std::vector coordinates */ Node(std::vector coords); + /** + * Constructor with c_vector coordinates + */ + Node(boost::numeric::ublas::c_vector coords); + /** * Destructor */ @@ -41,6 +48,16 @@ class Node * Return the index */ unsigned GetIndex() const; + + /** + * Return the location + */ + boost::numeric::ublas::c_vector GetLocation(); + + /** + * Translate with the given displacement vector + */ + void Translate(const boost::numeric::ublas::c_vector &rDisplacement); }; #endif //_NODE_HPP_ diff --git a/examples/cells/src/py/pycells/__init__.py b/examples/cells/src/py/pycells/__init__.py index cde2da7..2825225 100644 --- a/examples/cells/src/py/pycells/__init__.py +++ b/examples/cells/src/py/pycells/__init__.py @@ -1,6 +1,11 @@ +"""Main pycells module.""" + from ._pycells_all import ( + Cell, MeshFactory_PottsMesh_2, MeshFactory_PottsMesh_3, + Node_2, + Node_3, PetscUtils, PottsMesh_2, PottsMesh_3, @@ -16,6 +21,13 @@ } ) +Node = TemplateClassDict( + { + ("2",): Node_2, + ("3",): Node_3, + } +) + PottsMesh = TemplateClassDict( { ("2",): PottsMesh_2, @@ -31,7 +43,9 @@ ) __all__ = [ + "Cell", "MeshFactory", + "Node", "PetscUtils", "PottsMesh", "Scene", diff --git a/examples/cells/src/py/pycells/_syntax.py b/examples/cells/src/py/pycells/_syntax.py index 2afd7e6..a6733ea 100644 --- a/examples/cells/src/py/pycells/_syntax.py +++ b/examples/cells/src/py/pycells/_syntax.py @@ -1,3 +1,5 @@ +"""Syntax module.""" + import inspect from collections.abc import Iterable diff --git a/examples/cells/tests/test_cells.py b/examples/cells/tests/test_cells.py index bb268f9..6b4390d 100644 --- a/examples/cells/tests/test_cells.py +++ b/examples/cells/tests/test_cells.py @@ -3,7 +3,7 @@ import petsc4py import vtk -from pycells import PetscUtils, Scene +from pycells import Node, PetscUtils, Scene class TestCells(unittest.TestCase): @@ -18,6 +18,12 @@ def testPetscCaster(self): vec = PetscUtils.CreateVec(10) self.assertIsNotNone(vec) + def testUblasCaster(self): + node = Node[2]() + self.assertEqual(list(node.GetLocation()), [0, 0]) + node.Translate([1, 1]) + self.assertEqual(list(node.GetLocation()), [1, 1]) + if __name__ == "__main__": unittest.main()