Преглед на файлове

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

Ovidio Peña Rodríguez преди 6 години
родител
ревизия
25540fd3b0
променени са 6 файла, в които са добавени 55 реда и са изтрити 53 реда
  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
 
 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 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 clean - Delete temporal files"
 #	make standalone
 
-source:
+src:
 	$(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
 
 install:
 	$(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 --dist-dir=../
 
-builddeb:
+deb:
 	# build the source package in the parent directory
 	# then rename it to project_version.orig.tar.gz
 	$(PYTHON) setup.py sdist $(COMPILE) --dist-dir=../ --prune
@@ -43,12 +43,14 @@ standalone: scattnlay fieldnlay scattnlay-mp fieldnlay-mp
 # standalone programs with DP
 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)
+
 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)
 
 # standalone programs with MP
 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)
+
 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)
 

+ 4 - 6
README.md

@@ -51,13 +51,11 @@ And to compile the Debian package you need some tools:
 
 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 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 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
 #    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

+ 33 - 28
scattnlay/main.py

@@ -30,26 +30,10 @@
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from scattnlay_ import scattcoeffs_, scattnlay_,  fieldnlay_
 import numpy as np
 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])
 
@@ -61,18 +45,25 @@ def scattcoeffs(x, m, nmax=-1, pl=-1):
         nmax: Maximum number of multipolar expansion terms to be used for the
               calculations. Only use it if you know what you are doing, otherwise
               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)
     with
         terms: Number of multipolar expansion terms used for the calculations
         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:
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
     if len(x.shape) == 1:
         if len(m.shape) == 1:
-            return scattcoeffs_(x, m, nmax=nmax, pl=pl)
+            return scattcoeffs(x, m, nmax=nmax, pl=pl)
         else:
             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:
@@ -93,7 +84,7 @@ def scattcoeffs(x, m, nmax=-1, pl=-1):
         else:
             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:
             nstore = terms[i]
@@ -107,7 +98,7 @@ def scattcoeffs(x, m, nmax=-1, pl=-1):
 #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])
 
@@ -119,7 +110,8 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1):
                calculated (optional, 1D ndarray)
         nmax: Maximum number of multipolar expansion terms to be used for the
               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)
     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)
         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:
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
     if len(x.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:
             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:
@@ -162,13 +160,13 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1):
         else:
             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
 #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])
 
@@ -180,18 +178,25 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1):
             electric and magnetic fields (1D ndarray)
         nmax: Maximum number of multipolar expansion terms to be used for the
               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)
     with
         terms: Number of multipolar expansion terms used for the calculations
         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:
         raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
     if len(x.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:
             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:
@@ -207,7 +212,7 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1):
         else:
             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
 #fieldnlay()

+ 3 - 3
setup.py

@@ -60,13 +60,13 @@ O. Pena, U. Pal, Comput. Phys. Commun. 180 (2009) 2348-2354.""",
       download_url = __download_url__,
       license = 'GPL',
       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"],
                                language = "c++",
                                include_dirs = [np.get_include(), pb.get_include()], 
                                extra_compile_args=['-std=c++11']),
-                     Extension("scattnlay_mp_",
+                     Extension("scattnlay_mp",
                                ["src/nmie.cc", "src/nmie-pybind11.cc", "src/pb11_wrapper.cc"],
                                language = "c++",
                                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
 #ifdef MULTI_PRECISION
-PYBIND11_MODULE(scattnlay_mp_, m)
+PYBIND11_MODULE(scattnlay_mp, m)
 #else
-PYBIND11_MODULE(scattnlay_, m)
+PYBIND11_MODULE(scattnlay_dp, m)
 #endif  // MULTI_PRECISION
 {
   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.",
         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.",
         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.",
         py::arg("x"), py::arg("m"), py::arg("xp"), py::arg("yp"), py::arg("zp"), py::arg("nmax") = -1, py::arg("pl") = -1);
 }