Browse Source

Completed change to pybind11. Everything seems to be working properly but more testing is needed.

Ovidio Peña Rodríguez 6 years ago
parent
commit
25540fd3b0
6 changed files with 55 additions and 53 deletions
  1. 10 8
      Makefile
  2. 4 6
      README.md
  3. 0 3
      scattnlay/__init__.py
  4. 33 28
      scattnlay/main.py
  5. 3 3
      setup.py
  6. 5 5
      src/pb11_wrapper.cc

+ 10 - 8
Makefile

@@ -8,29 +8,29 @@ MULTIPREC=100
 CXX_NMIE_HEADERS=$(SRCDIR)/nmie.hpp $(SRCDIR)/nmie-impl.hpp $(SRCDIR)/nmie-precision.hpp
 CXX_NMIE_HEADERS=$(SRCDIR)/nmie.hpp $(SRCDIR)/nmie-impl.hpp $(SRCDIR)/nmie-precision.hpp
 
 
 all:
 all:
-	@echo "make source - Create source package for Python extension"
-	@echo "make python_ext - Create Python extension in place"
+	@echo "make src - Create source package for Python extension"
+	@echo "make ext - Create Python extension in place"
 	@echo "make install - Install Python extension on local system"
 	@echo "make install - Install Python extension on local system"
-	@echo "make buildrpm - Generate a rpm package for Python extension"
-	@echo "make builddeb - Generate a deb package for Python extension"
+	@echo "make rpm - Generate a rpm package for Python extension"
+	@echo "make deb - Generate a deb package for Python extension"
 	@echo "make standalone - Create standalone programs (scattnlay and fieldnlay)"
 	@echo "make standalone - Create standalone programs (scattnlay and fieldnlay)"
 	@echo "make clean - Delete temporal files"
 	@echo "make clean - Delete temporal files"
 #	make standalone
 #	make standalone
 
 
-source:
+src:
 	$(PYTHON) setup.py sdist $(COMPILE) --dist-dir=../
 	$(PYTHON) setup.py sdist $(COMPILE) --dist-dir=../
 
 
-python_ext: $(SRCDIR)/nmie.cc $(SRCDIR)/nmie-pybind11.cc $(SRCDIR)/pb11_wrapper.cc
+ext: $(SRCDIR)/nmie.cc $(SRCDIR)/nmie-pybind11.cc $(SRCDIR)/pb11_wrapper.cc
 	$(PYTHON) setup.py build_ext --inplace
 	$(PYTHON) setup.py build_ext --inplace
 
 
 install:
 install:
 	$(PYTHON) setup.py install --root $(DESTDIR) $(COMPILE)
 	$(PYTHON) setup.py install --root $(DESTDIR) $(COMPILE)
 
 
-buildrpm:
+rpm:
 	#$(PYTHON) setup.py bdist_rpm --post-install=rpm/postinstall --pre-uninstall=rpm/preuninstall
 	#$(PYTHON) setup.py bdist_rpm --post-install=rpm/postinstall --pre-uninstall=rpm/preuninstall
 	$(PYTHON) setup.py bdist_rpm --dist-dir=../
 	$(PYTHON) setup.py bdist_rpm --dist-dir=../
 
 
-builddeb:
+deb:
 	# build the source package in the parent directory
 	# build the source package in the parent directory
 	# then rename it to project_version.orig.tar.gz
 	# then rename it to project_version.orig.tar.gz
 	$(PYTHON) setup.py sdist $(COMPILE) --dist-dir=../ --prune
 	$(PYTHON) setup.py sdist $(COMPILE) --dist-dir=../ --prune
@@ -43,12 +43,14 @@ standalone: scattnlay fieldnlay scattnlay-mp fieldnlay-mp
 # standalone programs with DP
 # standalone programs with DP
 scattnlay: $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 scattnlay: $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 	$(CXX) -DNDEBUG -O2 -Wall -std=c++11 $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc  -lm -o scattnlay $(CXXFLAGS) $(LDFLAGS)
 	$(CXX) -DNDEBUG -O2 -Wall -std=c++11 $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc  -lm -o scattnlay $(CXXFLAGS) $(LDFLAGS)
+
 fieldnlay: $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 fieldnlay: $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 	$(CXX) -DNDEBUG -O2 -Wall -std=c++11 $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc  -lm -o fieldnlay $(CXXFLAGS) $(LDFLAGS)
 	$(CXX) -DNDEBUG -O2 -Wall -std=c++11 $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc  -lm -o fieldnlay $(CXXFLAGS) $(LDFLAGS)
 
 
 # standalone programs with MP
 # standalone programs with MP
 scattnlay-mp: $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 scattnlay-mp: $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 	$(CXX) -DNDEBUG -DMULTI_PRECISION=$(MULTIPREC) -O2 -Wall -std=c++11 $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc  -lm -o scattnlay-mp $(CXXFLAGS) $(LDFLAGS)
 	$(CXX) -DNDEBUG -DMULTI_PRECISION=$(MULTIPREC) -O2 -Wall -std=c++11 $(SRCDIR)/farfield.cc $(SRCDIR)/nmie.cc  -lm -o scattnlay-mp $(CXXFLAGS) $(LDFLAGS)
+
 fieldnlay-mp: $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 fieldnlay-mp: $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc $(CXX_NMIE_HEADERS)
 	$(CXX) -DNDEBUG -DMULTI_PRECISION=$(MULTIPREC) -O2 -Wall -std=c++11 $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc  -lm -o fieldnlay-mp $(CXXFLAGS) $(LDFLAGS)
 	$(CXX) -DNDEBUG -DMULTI_PRECISION=$(MULTIPREC) -O2 -Wall -std=c++11 $(SRCDIR)/nearfield.cc $(SRCDIR)/nmie.cc  -lm -o fieldnlay-mp $(CXXFLAGS) $(LDFLAGS)
 
 

+ 4 - 6
README.md

@@ -51,13 +51,11 @@ And to compile the Debian package you need some tools:
 
 
 Compilation options
 Compilation options
 
 
- - **make source** - Create source package for Python extension
- - **make cython** - Convert Cython code to C++
- - **make python_ext** - Create Python extension using C++ code
- - **make cython_ext** - Create Python extension using Cython code
+ - **make src** - Create source package for Python extension
+ - **make ext** - Create Python extension using C++ code
  - **make install** - Install Python extension on local system
  - **make install** - Install Python extension on local system
- - **make buildrpm** - Generate a rpm package for Python extension
- - **make builddeb** - Generate a deb package for Python extension
+ - **make rpm** - Generate a rpm package for Python extension
+ - **make deb** - Generate a deb package for Python extension
  - **make standalone** - Create standalone programs (scattnlay and fieldnlay)
  - **make standalone** - Create standalone programs (scattnlay and fieldnlay)
  - **make clean** - Delete temporal files
  - **make clean** - Delete temporal files
 
 

+ 0 - 3
scattnlay/__init__.py

@@ -30,7 +30,4 @@
 #    You should have received a copy of the GNU General Public License
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from scattnlay.main import switch_to_double_precision
-from scattnlay.main import switch_to_multiple_precision
-switch_to_double_precision()
 from scattnlay.main import scattcoeffs, scattnlay, fieldnlay
 from scattnlay.main import scattcoeffs, scattnlay, fieldnlay

+ 33 - 28
scattnlay/main.py

@@ -30,26 +30,10 @@
 #    You should have received a copy of the GNU General Public License
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from scattnlay_ import scattcoeffs_, scattnlay_,  fieldnlay_
 import numpy as np
 import numpy as np
 import sys
 import sys
 
 
-
-def switch_to_double_precision():
-    from scattnlay_ import scattcoeffs_, scattnlay_,  fieldnlay_
-    sys.modules['scattnlay.main'].scattnlay_ = scattnlay_
-    sys.modules['scattnlay.main'].scattcoeffs_ = scattcoeffs_
-    sys.modules['scattnlay.main'].fieldnlay_ = fieldnlay_
-
-
-def switch_to_multiple_precision():
-    from scattnlay_mp_ import scattcoeffs_, scattnlay_,  fieldnlay_
-    sys.modules['scattnlay.main'].scattnlay_ = scattnlay_
-    sys.modules['scattnlay.main'].scattcoeffs_ = scattcoeffs_
-    sys.modules['scattnlay.main'].fieldnlay_ = fieldnlay_
-
-
-def scattcoeffs(x, m, nmax=-1, pl=-1):
+def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
     """
     """
     scattcoeffs(x, m[, nmax, pl])
     scattcoeffs(x, m[, nmax, pl])
 
 
@@ -61,18 +45,25 @@ def scattcoeffs(x, m, nmax=-1, pl=-1):
         nmax: Maximum number of multipolar expansion terms to be used for the
         nmax: Maximum number of multipolar expansion terms to be used for the
               calculations. Only use it if you know what you are doing, otherwise
               calculations. Only use it if you know what you are doing, otherwise
               set this parameter to -1 and the function will calculate it.
               set this parameter to -1 and the function will calculate it.
-        pl: Index of PEC layer. If there is none just send -1
+        pl: Index of PEC layer. If there is none just send -1.
+        mp: Use multiple (True) or double (False) precision.
 
 
     Returns: (terms, an, bn)
     Returns: (terms, an, bn)
     with
     with
         terms: Number of multipolar expansion terms used for the calculations
         terms: Number of multipolar expansion terms used for the calculations
         an, bn: Complex scattering coefficients
         an, bn: Complex scattering coefficients
     """
     """
+
+    if mp:
+        from scattnlay_mp import scattcoeffs, scattnlay,  fieldnlay
+    else:
+        from scattnlay_dp import scattcoeffs, scattnlay,  fieldnlay
+
     if len(m.shape) != 1 and len(m.shape) != 2:
     if len(m.shape) != 1 and len(m.shape) != 2:
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
     if len(x.shape) == 1:
     if len(x.shape) == 1:
         if len(m.shape) == 1:
         if len(m.shape) == 1:
-            return scattcoeffs_(x, m, nmax=nmax, pl=pl)
+            return scattcoeffs(x, m, nmax=nmax, pl=pl)
         else:
         else:
             raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
             raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
     elif len(x.shape) != 2:
     elif len(x.shape) != 2:
@@ -93,7 +84,7 @@ def scattcoeffs(x, m, nmax=-1, pl=-1):
         else:
         else:
             mi = m[i]
             mi = m[i]
 
 
-        terms[i], a, b = scattcoeffs_(xi, mi, nmax=nmax, pl=pl)
+        terms[i], a, b = scattcoeffs(xi, mi, nmax=nmax, pl=pl)
 
 
         if terms[i] > nstore:
         if terms[i] > nstore:
             nstore = terms[i]
             nstore = terms[i]
@@ -107,7 +98,7 @@ def scattcoeffs(x, m, nmax=-1, pl=-1):
 #scattcoeffs()
 #scattcoeffs()
 
 
 
 
-def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1):
+def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
     """
     """
     scattnlay(x, m[, theta, nmax, pl])
     scattnlay(x, m[, theta, nmax, pl])
 
 
@@ -119,7 +110,8 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1):
                calculated (optional, 1D ndarray)
                calculated (optional, 1D ndarray)
         nmax: Maximum number of multipolar expansion terms to be used for the
         nmax: Maximum number of multipolar expansion terms to be used for the
               calculations. Only use it if you know what you are doing.
               calculations. Only use it if you know what you are doing.
-        pl: Index of PEC layer.
+        pl: Index of PEC layer. If there is none just send -1.
+        mp: Use multiple (True) or double (False) precision.
 
 
     Returns: (terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2)
     Returns: (terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2)
     with
     with
@@ -133,11 +125,17 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1):
         Albedo: Single scattering albedo (Albedo = Qsca/Qext)
         Albedo: Single scattering albedo (Albedo = Qsca/Qext)
         S1, S2: Complex scattering amplitudes
         S1, S2: Complex scattering amplitudes
     """
     """
+
+    if mp:
+        from scattnlay_mp import scattcoeffs, scattnlay,  fieldnlay
+    else:
+        from scattnlay_dp import scattcoeffs, scattnlay,  fieldnlay
+
     if len(m.shape) != 1 and len(m.shape) != 2:
     if len(m.shape) != 1 and len(m.shape) != 2:
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
     if len(x.shape) == 1:
     if len(x.shape) == 1:
         if len(m.shape) == 1:
         if len(m.shape) == 1:
-            return scattnlay_(x, m, theta, nmax=nmax, pl=pl)
+            return scattnlay(x, m, theta, nmax=nmax, pl=pl)
         else:
         else:
             raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
             raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
     elif len(x.shape) != 2:
     elif len(x.shape) != 2:
@@ -162,13 +160,13 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1):
         else:
         else:
             mi = m[i]
             mi = m[i]
 
 
-        terms[i], Qext[i], Qsca[i], Qabs[i], Qbk[i], Qpr[i], g[i], Albedo[i], S1[i], S2[i] = scattnlay_(xi, mi, theta, nmax=nmax, pl=pl)
+        terms[i], Qext[i], Qsca[i], Qabs[i], Qbk[i], Qpr[i], g[i], Albedo[i], S1[i], S2[i] = scattnlay(xi, mi, theta, nmax=nmax, pl=pl)
 
 
     return terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2
     return terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2
 #scattnlay()
 #scattnlay()
 
 
 
 
-def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1):
+def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
     """
     """
     fieldnlay(x, m, xp, yp, zp[, theta, nmax, pl])
     fieldnlay(x, m, xp, yp, zp[, theta, nmax, pl])
 
 
@@ -180,18 +178,25 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1):
             electric and magnetic fields (1D ndarray)
             electric and magnetic fields (1D ndarray)
         nmax: Maximum number of multipolar expansion terms to be used for the
         nmax: Maximum number of multipolar expansion terms to be used for the
               calculations. Only use it if you know what you are doing.
               calculations. Only use it if you know what you are doing.
-        pl: Index of PEC layer.
+        pl: Index of PEC layer. If there is none just send -1.
+        mp: Use multiple (True) or double (False) precision.
 
 
     Returns: (terms, E, H)
     Returns: (terms, E, H)
     with
     with
         terms: Number of multipolar expansion terms used for the calculations
         terms: Number of multipolar expansion terms used for the calculations
         E, H: Complex electric and magnetic field at the provided coordinates
         E, H: Complex electric and magnetic field at the provided coordinates
     """
     """
+
+    if mp:
+        from scattnlay_mp import scattcoeffs, scattnlay,  fieldnlay
+    else:
+        from scattnlay_dp import scattcoeffs, scattnlay,  fieldnlay
+
     if len(m.shape) != 1 and len(m.shape) != 2:
     if len(m.shape) != 1 and len(m.shape) != 2:
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
     if len(x.shape) == 1:
     if len(x.shape) == 1:
         if len(m.shape) == 1:
         if len(m.shape) == 1:
-            return fieldnlay_(x, m, xp, yp, zp, nmax=nmax, pl=pl)
+            return fieldnlay(x, m, xp, yp, zp, nmax=nmax, pl=pl)
         else:
         else:
             raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
             raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
     elif len(x.shape) != 2:
     elif len(x.shape) != 2:
@@ -207,7 +212,7 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1):
         else:
         else:
             mi = m[i]
             mi = m[i]
 
 
-        terms[i], E[i], H[i] = fieldnlay_(xi, mi, xp, yp, zp, nmax=nmax, pl=pl)
+        terms[i], E[i], H[i] = fieldnlay(xi, mi, xp, yp, zp, nmax=nmax, pl=pl)
 
 
     return terms, E, H
     return terms, E, H
 #fieldnlay()
 #fieldnlay()

+ 3 - 3
setup.py

@@ -60,13 +60,13 @@ O. Pena, U. Pal, Comput. Phys. Commun. 180 (2009) 2348-2354.""",
       download_url = __download_url__,
       download_url = __download_url__,
       license = 'GPL',
       license = 'GPL',
       platforms = 'any',
       platforms = 'any',
-      packages = ['scattnlay', 'scattnlay_mp'],
-      ext_modules = [Extension("scattnlay_",
+      packages = ['scattnlay', 'scattnlay_dp', 'scattnlay_mp'],
+      ext_modules = [Extension("scattnlay_dp",
                                ["src/nmie.cc", "src/nmie-pybind11.cc", "src/pb11_wrapper.cc"],
                                ["src/nmie.cc", "src/nmie-pybind11.cc", "src/pb11_wrapper.cc"],
                                language = "c++",
                                language = "c++",
                                include_dirs = [np.get_include(), pb.get_include()], 
                                include_dirs = [np.get_include(), pb.get_include()], 
                                extra_compile_args=['-std=c++11']),
                                extra_compile_args=['-std=c++11']),
-                     Extension("scattnlay_mp_",
+                     Extension("scattnlay_mp",
                                ["src/nmie.cc", "src/nmie-pybind11.cc", "src/pb11_wrapper.cc"],
                                ["src/nmie.cc", "src/nmie-pybind11.cc", "src/pb11_wrapper.cc"],
                                language = "c++",
                                language = "c++",
                                include_dirs = [np.get_include(), pb.get_include()], 
                                include_dirs = [np.get_include(), pb.get_include()], 

+ 5 - 5
src/pb11_wrapper.cc

@@ -5,22 +5,22 @@ namespace py = pybind11;
 
 
 // wrap as Python module
 // wrap as Python module
 #ifdef MULTI_PRECISION
 #ifdef MULTI_PRECISION
-PYBIND11_MODULE(scattnlay_mp_, m)
+PYBIND11_MODULE(scattnlay_mp, m)
 #else
 #else
-PYBIND11_MODULE(scattnlay_, m)
+PYBIND11_MODULE(scattnlay_dp, m)
 #endif  // MULTI_PRECISION
 #endif  // MULTI_PRECISION
 {
 {
   m.doc() = "The Python version of scattnlay";
   m.doc() = "The Python version of scattnlay";
 
 
-  m.def("scattcoeffs_", &py_ScattCoeffs,
+  m.def("scattcoeffs", &py_ScattCoeffs,
         "Calculate the scattering coefficients, required to calculate both the near- and far-field parameters.",
         "Calculate the scattering coefficients, required to calculate both the near- and far-field parameters.",
         py::arg("x"), py::arg("m"), py::arg("nmax") = -1, py::arg("pl") = -1);
         py::arg("x"), py::arg("m"), py::arg("nmax") = -1, py::arg("pl") = -1);
 
 
-  m.def("scattnlay_", &py_scattnlay,
+  m.def("scattnlay", &py_scattnlay,
         "Calculate the scattering parameters and amplitudes.",
         "Calculate the scattering parameters and amplitudes.",
         py::arg("x"), py::arg("m"), py::arg("theta") = py::array_t<double>(0), py::arg("nmax") = -1, py::arg("pl") = -1);
         py::arg("x"), py::arg("m"), py::arg("theta") = py::array_t<double>(0), py::arg("nmax") = -1, py::arg("pl") = -1);
 
 
-  m.def("fieldnlay_", &py_fieldnlay,
+  m.def("fieldnlay", &py_fieldnlay,
         "Calculate the complex electric and magnetic field in the surroundings and inside the particle.",
         "Calculate the complex electric and magnetic field in the surroundings and inside the particle.",
         py::arg("x"), py::arg("m"), py::arg("xp"), py::arg("yp"), py::arg("zp"), py::arg("nmax") = -1, py::arg("pl") = -1);
         py::arg("x"), py::arg("m"), py::arg("xp"), py::arg("yp"), py::arg("zp"), py::arg("nmax") = -1, py::arg("pl") = -1);
 }
 }