Browse Source

pybind11 working for RectilinearGrid

iss7
Trevor Irons 5 years ago
parent
commit
65034cb932

+ 23
- 0
CMake/SuperBuild.cmake View File

@@ -68,6 +68,29 @@ if ( LEMMA_VTK8_SUPPORT )
68 68
 
69 69
 endif()
70 70
 
71
+if (LEMMA_PYTHON3_BINDINGS)
72
+    if (pybind11_FOUND)
73
+        message( STATUS "pybind11 was found" )
74
+    else()
75
+        message( STATUS "pybind11 was NOT found, please build or remove LEMMA_PYTHON3_BINDINGS" )
76
+	    ExternalProject_Add(pybind11
77
+		    GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
78
+		    GIT_TAG "master"
79
+		    UPDATE_COMMAND ""
80
+		    PATCH_COMMAND ""
81
+    	    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/external/pybind11
82
+            CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} 
83
+                       -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
84
+                       -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} 
85
+                       -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
86
+                       -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} 
87
+                       -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} 
88
+		    #CONFIGURE_COMMAND ""
89
+		    #BUILD_COMMAND     ""
90
+		    #INSTALL_COMMAND   ""
91
+	    )
92
+    endif()
93
+endif()
71 94
 
72 95
 if (LEMMA_ENABLE_TESTING)
73 96
     if (CXXTEST_FOUND)

+ 9
- 5
CMakeLists.txt View File

@@ -72,6 +72,11 @@ if (CMAKE_CROSSCOMPILING)
72 72
 
73 73
     find_package (yaml-cpp 0.6         PATHS ${CMAKE_INSTALL_PREFIX}  NO_DEFAULT_PATH) # Serialisation of classes 
74 74
     find_package (Eigen3   3.3         PATHS ${CMAKE_INSTALL_PREFIX}  NO_DEFAULT_PATH) # Matrix/Vector & Math
75
+
76
+    if (LEMMA_PYTHON3_BINDINGS)
77
+        find_package(pybind11 PATHS ${CMAKE_INSTALL_PREFIX} NO_DEFAULT_PATH)           # Python 3 bindings
78
+    endif()
79
+
75 80
 #${CMAKE_INSTALL_PREFIX}  NO_DEFAULT_PATH 
76 81
     if (LEMMA_VTK8_SUPPORT)                                                            # Visualisation 
77 82
         find_package (VTK  8.2.0      
@@ -154,6 +159,7 @@ message ( STATUS "VTK Found? " ${VTK_FOUND} )
154 159
 ## Optional Dependencies
155 160
 if ( NOT Eigen3_FOUND OR 
156 161
      NOT yaml-cpp_FOUND OR 
162
+     (LEMMA_PYTHON3_BINDINGS AND NOT pybind11_FOUND) OR 
157 163
      (LEMMA_VTK8_SUPPORT AND NOT VTK_FOUND) OR
158 164
      (LEMMA_ENABLE_TESTING AND NOT CxxTest_FOUND) )
159 165
     message ( STATUS "Missing hard dependencies have been found, these will be downloaded any compiled." )
@@ -245,14 +251,12 @@ if (LEMMA_USE_BOOST)
245 251
 endif()
246 252
 
247 253
 if (LEMMA_PYTHON3_BINDINGS)
248
-
249 254
     find_package(PythonLibs 3.0 REQUIRED)
250
-    find_package(Boost 1.64.0   REQUIRED COMPONENTS system python3 numpy3)
251
-
255
+    find_package(pybind11 REQUIRED)
256
+    #    find_package(Boost 1.64.0   REQUIRED COMPONENTS system python3 numpy3)
252 257
     # Is this OK for portability of non-python libraries
253 258
     include_directories(${PYTHON_INCLUDE_DIRS})
254
-    include_directories(${Boost_INCLUDE_DIRS})
255
-
259
+#    include_directories(${Boost_INCLUDE_DIRS})
256 260
 endif()
257 261
 
258 262
 ####################

+ 1
- 0
Modules/LemmaCore/CMakeLists.txt View File

@@ -46,6 +46,7 @@ endif()
46 46
 # Python Bindings
47 47
 if (LEMMA_PYTHON3_BINDINGS)
48 48
 	add_subdirectory(python)
49
+	#pybind11_add_module(Lemma pyLemmaCore.cpp)
49 50
 endif()
50 51
 
51 52
 # Install

+ 10
- 4
Modules/LemmaCore/include/RectilinearGrid.h View File

@@ -72,6 +72,12 @@ namespace Lemma {
72 72
             virtual YAML::Node Serialize() const;
73 73
 
74 74
             /**
75
+             *  Uses YAML to serialize this object.
76
+             *  @return a YAML::Node
77
+             */
78
+            virtual std::string Print() const;
79
+
80
+            /**
75 81
              *  Factory method for generating concrete class.
76 82
              *  @return a std::shared_ptr of type RectilinearGrid
77 83
              */
@@ -155,7 +161,7 @@ namespace Lemma {
155 161
             /** Returns a spacing
156 162
              *  @return dx[ix]
157 163
              */
158
-            Real GetDx(const int& ix);
164
+            Real GetDx1(const int& ix);
159 165
 
160 166
             /** Returns Spacing Vector
161 167
              *  @return dy
@@ -165,7 +171,7 @@ namespace Lemma {
165 171
             /** Returns a spacing
166 172
              *  @return dy[iy]
167 173
              */
168
-            Real GetDy(const int& iy);
174
+            Real GetDy1(const int& iy);
169 175
 
170 176
             /** Returns Spacing Vector
171 177
              *  @return dz
@@ -175,7 +181,7 @@ namespace Lemma {
175 181
             /** Returns Spacing Vector
176 182
              *  @return dz[iz]
177 183
              */
178
-            Real GetDz(const int& iz);
184
+            Real GetDz1(const int& iz);
179 185
 
180 186
             // ====================  INQUIRY       =======================
181 187
 
@@ -227,4 +233,4 @@ namespace Lemma {
227 233
 #endif   // ----- #ifndef RECTILINEARGRID_INC  -----
228 234
 
229 235
 /* vim: set tabstop=4 expandtab: */
230
-/* vim: set filetype=cpp syntax=cpp.doxygen: */
236
+/* vim: set filetype=cpp syntax=cpp.doxygen */

+ 15
- 5
Modules/LemmaCore/python/CMakeLists.txt View File

@@ -1,16 +1,26 @@
1 1
 # include directories
2
-include_directories(${CMAKE_CURRENT_SOURCE_DIR}/python)
2
+#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/python)
3 3
 
4 4
 # create the lib
5
-add_library(pyLemmaCore SHARED ${CMAKE_CURRENT_SOURCE_DIR}/pyLemmaCore.cpp)
5
+#add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR} )
6
+#pybind11_add_module(pyLemmaCore pyLemmaCore.cpp)
7
+
8
+add_library(pyLemmaCore MODULE pyLemmaCore.cpp)
9
+target_link_libraries(pyLemmaCore PRIVATE pybind11::module lemmacore )
10
+set_target_properties(pyLemmaCore PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
11
+                                         SUFFIX "${PYTHON_MODULE_EXTENSION}")
12
+
13
+#target_include_directories( lemmacore PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include" )
14
+
15
+#add_library(pyLemmaCore SHARED ${CMAKE_CURRENT_SOURCE_DIR}/pyLemmaCore.cpp)
6 16
 
7 17
 # link
8
-target_link_libraries(pyLemmaCore ${Boost_LIBRARIES} lemmacore)
18
+#target_link_libraries(pyLemmaCore lemmacore)
9 19
 
10 20
 # Copy the __init__.py file
11
-configure_file(__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py COPYONLY)
21
+#configure_file(__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py COPYONLY)
12 22
 
13 23
 # Suppress prefix "lib" because Python does not allow this prefix
14
-set_target_properties(pyLemmaCore PROPERTIES PREFIX "")
24
+#set_target_properties(pyLemmaCore PROPERTIES PREFIX "")
15 25
 
16 26
 #install(TARGETS pyLemmaCore __init__.py DESTINATION "${PYTHON_INSTALL_PATH}")

+ 39
- 21
Modules/LemmaCore/python/pyLemmaCore.cpp View File

@@ -17,18 +17,47 @@
17 17
  * @copyright Copyright (c) 2019, Lemma Software, LLC
18 18
  */
19 19
 
20
+#include <pybind11/pybind11.h>
21
+#include <pybind11/iostream.h>
22
+#include <pybind11/eigen.h>
20 23
 #include "LemmaCore"
21
-#include <string>
22 24
 
23
-#include <boost/python.hpp>
24
-#include <boost/python/numpy.hpp>
25
-// Include the headers of MyLib
25
+namespace py = pybind11;
26 26
 
27
-//using namespace boost::python;
27
+PYBIND11_MODULE(pyLemmaCore, m) {
28 28
 
29
-namespace bp = boost::python;
30
-namespace np = boost::python::numpy;
29
+    py::add_ostream_redirect(m, "ostream_redirect");
31 30
 
31
+    m.doc() = "Python binding of LemmaCore, additional details can be found at https://lemmasoftware.org";
32
+
33
+    py::class_<Lemma::RectilinearGrid, std::shared_ptr<Lemma::RectilinearGrid> >(m, "RectilinearGrid")
34
+
35
+        // lifecycle
36
+        .def(py::init(&Lemma::RectilinearGrid::NewSP))
37
+
38
+        // print
39
+        .def("__repr__", &Lemma::RectilinearGrid::Print)
40
+        .def("Serialize", &Lemma::RectilinearGrid::Print)
41
+
42
+        // accessors
43
+        .def("GetName", &Lemma::RectilinearGrid::GetName, "Returns the name of the class")
44
+        .def("GetNx", &Lemma::RectilinearGrid::GetNx, "Returns the number of cells in the x direction")
45
+        .def("GetNy", &Lemma::RectilinearGrid::GetNy, "Returns the number of cells in the y direction")
46
+        .def("GetNz", &Lemma::RectilinearGrid::GetNz, "Returns the number of cells in the z direction")
47
+        .def("GetOx", &Lemma::RectilinearGrid::GetOx, "Returns the grid origin offset in the x direction")
48
+        .def("GetOy", &Lemma::RectilinearGrid::GetOy, "Returns the grid origin offset in the y direction")
49
+        .def("GetOz", &Lemma::RectilinearGrid::GetOz, "Returns the grid origin offset in the z direction")
50
+        .def("GetDx", &Lemma::RectilinearGrid::GetDx, "Returns the grid spacing in the x direction")
51
+        .def("GetDy", &Lemma::RectilinearGrid::GetDy, "Returns the grid spacing in the y direction")
52
+        .def("GetDz", &Lemma::RectilinearGrid::GetDz, "Returns the grid spacing in the z direction")
53
+
54
+        .def("SetDimensions", &Lemma::RectilinearGrid::SetDimensions, "Sets the number of cells in x, y, and z")
55
+        .def("SetOffset", &Lemma::RectilinearGrid::SetOffset, "Sets the origin offset in x, y, and z")
56
+        .def("SetSpacing", &Lemma::RectilinearGrid::SetSpacing, "Sets the grid spacing in x, y, and z");
57
+}
58
+
59
+
60
+/*
32 61
 BOOST_PYTHON_MODULE(pyLemmaCore)
33 62
 {
34 63
     Py_Initialize();
@@ -45,21 +74,10 @@ BOOST_PYTHON_MODULE(pyLemmaCore)
45 74
         //.def("DeSerialize", &Lemma::RectilinearGrid::DeSerialize)
46 75
 
47 76
         // Accessors
48
-        .def("GetName", &Lemma::RectilinearGrid::GetName)
49
-        .def("GetNx", &Lemma::RectilinearGrid::GetNx)
50
-        .def("GetNy", &Lemma::RectilinearGrid::GetNy)
51
-        .def("GetNz", &Lemma::RectilinearGrid::GetNz)
52
-        .def("GetOx", &Lemma::RectilinearGrid::GetOx)
53
-        .def("GetOy", &Lemma::RectilinearGrid::GetOy)
54
-        .def("GetOz", &Lemma::RectilinearGrid::GetOz)
55
-        //.def("GetDx", &Lemma::RectilinearGrid::GetDx)
56
-        //.def("GetDy", &Lemma::RectilinearGrid::GetDy)
57
-        //.def("GetDz", &Lemma::RectilinearGrid::GetDz)
58
-
59
-        .def("SetDimensions", &Lemma::RectilinearGrid::SetDimensions)
60
-        .def("SetOffset", &Lemma::RectilinearGrid::SetOffset)
61
-        .def("SetSpacing", &Lemma::RectilinearGrid::SetSpacing)
77
+
78
+
62 79
 
63 80
     ;
64 81
 
65 82
 }
83
+*/

+ 9
- 3
Modules/LemmaCore/src/RectilinearGrid.cpp View File

@@ -69,6 +69,12 @@ namespace Lemma {
69 69
         return node;
70 70
     }
71 71
 
72
+    std::string RectilinearGrid::Print() const {
73
+        YAML::Emitter out;
74
+        out << this->Serialize();
75
+        return out.c_str();
76
+    }
77
+
72 78
 
73 79
     //--------------------------------------------------------------------------------------
74 80
     //       Class:  RectilinearGrid
@@ -183,7 +189,7 @@ namespace Lemma {
183 189
     //       Class:  RectilinearGrid
184 190
     //      Method:  GetDx
185 191
     //--------------------------------------------------------------------------------------
186
-    Real RectilinearGrid::GetDx ( const int& ix  ) {
192
+    Real RectilinearGrid::GetDx1 ( const int& ix  ) {
187 193
         return dx[ix];
188 194
     }		// -----  end of method RectilinearGrid::GetDx  -----
189 195
 
@@ -199,7 +205,7 @@ namespace Lemma {
199 205
     //       Class:  RectilinearGrid
200 206
     //      Method:  GetDy
201 207
     //--------------------------------------------------------------------------------------
202
-    Real RectilinearGrid::GetDy ( const int& iy  ) {
208
+    Real RectilinearGrid::GetDy1 ( const int& iy  ) {
203 209
         return dy[iy];
204 210
     }		// -----  end of method RectilinearGrid::GetDy  -----
205 211
 
@@ -215,7 +221,7 @@ namespace Lemma {
215 221
     //       Class:  RectilinearGrid
216 222
     //      Method:  GetDz
217 223
     //--------------------------------------------------------------------------------------
218
-    Real RectilinearGrid::GetDz ( const int& iz  ) {
224
+    Real RectilinearGrid::GetDz1 ( const int& iz  ) {
219 225
         return dz[iz];
220 226
     }		// -----  end of method RectilinearGrid::GetDz  -----
221 227
 

+ 12
- 12
Modules/LemmaCore/src/RectilinearGridVTKExporter.cpp View File

@@ -142,27 +142,27 @@ void RectilinearGridVTKExporter::BuildVTKRectilinearGrid (  ) {
142 142
 
143 143
     // Set Coordinate>s
144 144
     vtkDoubleArray *xCoords = vtkDoubleArray::New();
145
-    xCoords->InsertNextValue(Grid->GetOx()-Grid->GetDx(0)/2.);
146
-    double xm1 = Grid->GetOx() - Grid->GetDx(0)/2.;
145
+    xCoords->InsertNextValue(Grid->GetOx()-Grid->GetDx1(0)/2.);
146
+    double xm1 = Grid->GetOx() - Grid->GetDx1(0)/2.;
147 147
     for (int ix=0; ix<Grid->GetNx(); ix++) {
148
-        xCoords->InsertNextValue(xm1 + Grid->GetDx(ix));
149
-        xm1 += Grid->GetDx(ix);
148
+        xCoords->InsertNextValue(xm1 + Grid->GetDx1(ix));
149
+        xm1 += Grid->GetDx1(ix);
150 150
     }
151 151
 
152 152
     vtkDoubleArray *yCoords = vtkDoubleArray::New();
153
-    yCoords->InsertNextValue(Grid->GetOy()-Grid->GetDy(0)/2.);
154
-    double ym1 = Grid->GetOy()-Grid->GetDy(0)/2.;
153
+    yCoords->InsertNextValue(Grid->GetOy()-Grid->GetDy1(0)/2.);
154
+    double ym1 = Grid->GetOy()-Grid->GetDy1(0)/2.;
155 155
     for (int iy=0; iy<Grid->GetNy(); iy++) {
156
-        yCoords->InsertNextValue(ym1 + Grid->GetDy(iy));
157
-        ym1 += Grid->GetDy(iy);
156
+        yCoords->InsertNextValue(ym1 + Grid->GetDy1(iy));
157
+        ym1 += Grid->GetDy1(iy);
158 158
     }
159 159
 
160 160
     vtkDoubleArray *zCoords = vtkDoubleArray::New();
161
-    zCoords->InsertNextValue(Grid->GetOz()-Grid->GetDz(0)/2.);
162
-    double zm1 = Grid->GetOz()-Grid->GetDz(0)/2.;
161
+    zCoords->InsertNextValue(Grid->GetOz()-Grid->GetDz1(0)/2.);
162
+    double zm1 = Grid->GetOz()-Grid->GetDz1(0)/2.;
163 163
     for (int iz=0; iz<Grid->GetNz(); iz++) {
164
-        zCoords->InsertNextValue(zm1 + Grid->GetDz(iz));
165
-        zm1 += Grid->GetDz(iz);
164
+        zCoords->InsertNextValue(zm1 + Grid->GetDz1(iz));
165
+        zm1 += Grid->GetDz1(iz);
166 166
     }
167 167
 
168 168
     VTKGrid = vtkSmartPointer<vtkRectilinearGrid>::New();

Loading…
Cancel
Save