Browse Source

Python wrapper working with new class.

Ovidio Peña Rodríguez 10 years ago
parent
commit
8dafd465ef
8 changed files with 40 additions and 30 deletions
  1. 10 0
      Makefile
  2. 16 16
      nmie.cc
  3. 6 6
      py_nmie.cc
  4. 4 4
      py_nmie.h
  5. 1 1
      scattnlay.cpp
  6. 1 1
      scattnlay.pyx
  7. 1 1
      setup.py
  8. 1 1
      setup_cython.py

+ 10 - 0
Makefile

@@ -7,6 +7,8 @@ VERSION=0.3.1
 all:
 	@echo "make source - Create source package"
 	@echo "make cython - Convert Cython code to c++"
+	@echo "make python_ext - Create Python extension using C++ code"
+	@echo "make cython_ext - Create Python extension using Cython code"
 	@echo "make install - Install on local system"
 	@echo "make buildrpm - Generate a rpm package"
 	@echo "make builddeb - Generate a deb package"
@@ -20,6 +22,14 @@ source:
 cython: scattnlay.pyx
 	cython --cplus scattnlay.pyx
 
+python_ext: nmie.cc py_nmie.cc scattnlay.cpp
+	export CFLAGS='-std=c++11'
+	python setup.py build_ext --inplace
+
+cython_ext: nmie.cc py_nmie.cc scattnlay.pyx
+	export CFLAGS='-std=c++11'
+	python setup_cython.py build_ext --inplace
+
 install:
 	$(PYTHON) setup.py install --root $(DESTDIR) $(COMPILE)
 

+ 16 - 16
nmie.cc

@@ -1342,8 +1342,8 @@ namespace nmie {
       for (int i = 0; i < 3; i++) {
         Es[i] = Es[i] + encap*(c_i*an_[n]*vn3e1n[i] - bn_[n]*vm3o1n[i]);
         Hs[i] = Hs[i] + encap*(c_i*bn_[n]*vn3o1n[i] + an_[n]*vm3e1n[i]);
-        //if (n<3) printf(" E[%d]=%g ", i,std::abs(Es[i]));
-        if (n<3) printf(" !!=%d=== %g ", i,std::abs(Es[i]));
+        //if (n < 3) printf(" E[%d]=%g ", i,std::abs(Es[i]));
+        if (n < 3) printf(" !!=%d=== %g ", i,std::abs(Es[i]));
       }
     }
     
@@ -1422,17 +1422,17 @@ namespace nmie {
       double rn = static_cast<double>(n + 1);
       std::complex<double> znm1 = bj[n] + c_i*by[n];
       std::complex<double> zn = bj[n + 1] + c_i*by[n + 1];
-      //if (n<3) printf("\nbesselh = %g,%g", zn.real(), zn.imag()); //!
+      //if (n < 3) printf("\nbesselh = %g,%g", zn.real(), zn.imag()); //!
       // using BH 4.12 and 4.50
       std::complex<double> xxip = Rho*(bj[n] + c_i*by[n]) - rn*zn;
-      //if (n<3) printf("\nxxip = %g,%g", xxip.real(), xxip.imag()); //!
+      //if (n < 3) printf("\nxxip = %g,%g", xxip.real(), xxip.imag()); //!
       
       using std::sin;
       using std::cos;
       vm3o1n[0] = c_zero;
       vm3o1n[1] = cos(Phi)*Pi[n]*zn;
       vm3o1n[2] = -sin(Phi)*Tau[n]*zn;
-      // if (n<3)  printf("\nRE  vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g   \nIM vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g",
+      // if (n < 3)  printf("\nRE  vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g   \nIM vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g",
       //              vm3o1n[0].real(), vm3o1n[1].real(), vm3o1n[2].real(),
       //              vm3o1n[0].imag(), vm3o1n[1].imag(), vm3o1n[2].imag());
       vm3e1n[0] = c_zero;
@@ -1444,7 +1444,7 @@ namespace nmie {
       vn3e1n[0] = cos(Phi)*rn*(rn + 1.0)*sin(Theta)*Pi[n]*zn/Rho;
       vn3e1n[1] = cos(Phi)*Tau[n]*xxip/Rho;
       vn3e1n[2] = -sin(Phi)*Pi[n]*xxip/Rho;
-      // if (n<3)  printf("\nRE  vn3e1n[0]%g   vn3e1n[1]%g    vn3e1n[2]%g   \nIM vn3e1n[0]%g   vn3e1n[1]%g    vn3e1n[2]%g",
+      // if (n < 3)  printf("\nRE  vn3e1n[0]%g   vn3e1n[1]%g    vn3e1n[2]%g   \nIM vn3e1n[0]%g   vn3e1n[1]%g    vn3e1n[2]%g",
       //              vn3e1n[0].real(), vn3e1n[1].real(), vn3e1n[2].real(),
       //              vn3e1n[0].imag(), vn3e1n[1].imag(), vn3e1n[2].imag());
       
@@ -1453,7 +1453,7 @@ namespace nmie {
       // znm1 = (bj[n] + c_i*by[n]).real();
       // zn = (bj[n + 1] + c_i*by[n + 1]).real();
       xxip = Rho*(bj[n]) - rn*zn;
-      if (n<3)printf("\nbesselj = %g,%g", zn.real(), zn.imag()); //!
+      if (n < 3)printf("\nbesselj = %g,%g", zn.real(), zn.imag()); //!
       vm1o1n[0] = c_zero;
       vm1o1n[1] = cos(Phi)*Pi[n]*zn;
       vm1o1n[2] = -sin(Phi)*Tau[n]*zn;
@@ -1463,21 +1463,21 @@ namespace nmie {
       vn1o1n[0] = sin(Phi)*rn*(rn + 1.0)*sin(Theta)*Pi[n]*zn/Rho;
       vn1o1n[1] = sin(Phi)*Tau[n]*xxip/Rho;
       vn1o1n[2] = cos(Phi)*Pi[n]*xxip/Rho;
-      // if (n<3) printf("\nvn1o1n[2](%g) = cos(Phi)(%g)*Pi[n](%g)*xxip(%g)/Rho(%g)",
+      // if (n < 3) printf("\nvn1o1n[2](%g) = cos(Phi)(%g)*Pi[n](%g)*xxip(%g)/Rho(%g)",
       //                       std::abs(vn1o1n[2]), cos(Phi),Pi[n],std::abs(xxip),Rho);
       vn1e1n[0] = cos(Phi)*rn*(rn + 1.0)*sin(Theta)*Pi[n]*zn/Rho;
       vn1e1n[1] = cos(Phi)*Tau[n]*xxip/Rho;
       vn1e1n[2] = -sin(Phi)*Pi[n]*xxip/Rho;
-      // if (n<3)  printf("\nRE  vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g   \nIM vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g",
+      // if (n < 3)  printf("\nRE  vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g   \nIM vm3o1n[0]%g   vm3o1n[1]%g    vm3o1n[2]%g",
       //              vm3o1n[0].real(), vm3o1n[1].real(), vm3o1n[2].real(),
       //              vm3o1n[0].imag(), vm3o1n[1].imag(), vm3o1n[2].imag());
       
       // scattered field: BH p.94 (4.45)
       std::complex<double> encap = std::pow(c_i, rn)*(2.0*rn + 1.0)/(rn*rn + rn);
-      // if (n<3) printf("\n===== n=%d ======\n",n);
+      // if (n < 3) printf("\n===== n=%d ======\n",n);
       for (int i = 0; i < 3; i++) {
-        // if (n<3 && i==0) printf("\nn=%d",n);
-        // if (n<3) printf("\nbefore !El[%d]=%g,%g! ", i, El[i].real(), El[i].imag());
+        // if (n < 3 && i==0) printf("\nn=%d",n);
+        // if (n < 3) printf("\nbefore !El[%d]=%g,%g! ", i, El[i].real(), El[i].imag());
         Ei[i] = encap*(cl_n_[l][n]*vm1o1n[i] - c_i*dl_n_[l][n]*vn1e1n[i]
                        + c_i*al_n_[l][n]*vn3e1n[i] - bl_n_[l][n]*vm3o1n[i]);
         El[i] = El[i] + encap*(cl_n_[l][n]*vm1o1n[i] - c_i*dl_n_[l][n]*vn1e1n[i]
@@ -1485,18 +1485,18 @@ namespace nmie {
         Hl[i] = Hl[i] + encap*(-dl_n_[l][n]*vm1e1n[i] - c_i*cl_n_[l][n]*vn1o1n[i]
                                + c_i*bl_n_[l][n]*vn3o1n[i] + al_n_[l][n]*vm3e1n[i]);
         // printf("\n !Ei[%d]=%g,%g! ", i, Ei[i].real(), Ei[i].imag());
-        // if (n<3) printf("\n !El[%d]=%g,%g! ", i, El[i].real(), El[i].imag());
+        // if (n < 3) printf("\n !El[%d]=%g,%g! ", i, El[i].real(), El[i].imag());
         // //printf(" ===%d=== %g ", i,std::abs(cl_n_[l][n]*vm1o1n[i] - c_i*dl_n_[l][n]*vn1e1n[i]));
-        // if (n<3) printf(" ===%d=== %g ", i,std::abs(//-dl_n_[l][n]*vm1e1n[i] 
+        // if (n < 3) printf(" ===%d=== %g ", i,std::abs(//-dl_n_[l][n]*vm1e1n[i] 
         //                                             //- c_i*cl_n_[l][n]*
         //                                             vn1o1n[i]
         //                                             // + c_i*bl_n_[l][n]*vn3o1n[i]
         //                                             // + al_n_[l][n]*vm3e1n[i]
         //                      ));
-        // if (n<3) printf(" --- Ei[%d]=%g! ", i,std::abs(encap*(vm1o1n[i] - c_i*vn1e1n[i])));
+        // if (n < 3) printf(" --- Ei[%d]=%g! ", i,std::abs(encap*(vm1o1n[i] - c_i*vn1e1n[i])));
 
       }
-      //if (n<3) printf(" bj=%g \n", std::abs(bj[n]));
+      //if (n < 3) printf(" bj=%g \n", std::abs(bj[n]));
     }  // end of for all n
     
     // magnetic field

+ 6 - 6
py_nmie.cc

@@ -33,8 +33,8 @@
 // Same as nMie in 'nmie.h' but uses double arrays to return the results (useful for python).
 // This is a workaround because I have not been able to return the results using 
 // std::vector<std::complex<double> >
-int nMie(int L, int pl, std::vector<double> x, std::vector<std::complex<double> > m,
-         int nTheta, std::vector<double> Theta, int nmax,
+int nMie(const int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m,
+         const int nTheta, std::vector<double>& Theta, const int nmax,
          double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo,
 		 double S1r[], double S1i[], double S2r[], double S2i[]) {
 
@@ -43,7 +43,7 @@ int nMie(int L, int pl, std::vector<double> x, std::vector<std::complex<double>
   S1.resize(nTheta);
   S2.resize(nTheta);
 
-  result = nMie(L, pl, x, m, nTheta, Theta, nmax, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
+  result = nmie::nMie(L, pl, x, m, nTheta, Theta, nmax, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
 
   for (i = 0; i < nTheta; i++) {
     S1r[i] = S1[i].real();
@@ -58,8 +58,8 @@ int nMie(int L, int pl, std::vector<double> x, std::vector<std::complex<double>
 // Same as nField in 'nmie.h' but uses double arrays to return the results (useful for python).
 // This is a workaround because I have not been able to return the results using 
 // std::vector<std::complex<double> >
-int nField(int L, int pl, std::vector<double> x, std::vector<std::complex<double> > m, int nmax,
-           int nCoords, std::vector<double> Xp, std::vector<double> Yp, std::vector<double> Zp,
+int nField(const int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const int nmax,
+           const int nCoords, std::vector<double>& Xp, std::vector<double>& Yp, std::vector<double>& Zp,
            double Erx[], double Ery[], double Erz[], double Eix[], double Eiy[], double Eiz[],
            double Hrx[], double Hry[], double Hrz[], double Hix[], double Hiy[], double Hiz[]) {
 
@@ -72,7 +72,7 @@ int nField(int L, int pl, std::vector<double> x, std::vector<std::complex<double
     H[i].resize(3);
   }
 
-  result = nField(L, pl, x, m, nmax, nCoords, Xp, Yp, Zp, E, H);
+  result = nmie::nField(L, pl, x, m, nmax, nCoords, Xp, Yp, Zp, E, H);
 
   for (i = 0; i < nCoords; i++) {
     Erx[i] = E[i][0].real();

+ 4 - 4
py_nmie.h

@@ -27,13 +27,13 @@
 #include <complex>
 #include <vector>
 
-int nMie(int L, int pl, std::vector<double> x, std::vector<std::complex<double> > m,
-         int nTheta, std::vector<double> Theta, int nmax,
+int nMie(const int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m,
+         const int nTheta, std::vector<double>& Theta, const int nmax,
          double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo,
 		 double S1r[], double S1i[], double S2r[], double S2i[]);
 
-int nField(int L, int pl, std::vector<double> x, std::vector<std::complex<double> > m, int nmax,
-           int nCoords, std::vector<double> Xp, std::vector<double> Yp, std::vector<double> Zp,
+int nField(const int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const int nmax,
+           const int nCoords, std::vector<double>& Xp, std::vector<double>& Yp, std::vector<double>& Zp,
            double Erx[], double Ery[], double Erz[], double Eix[], double Eiy[], double Eiz[],
            double Hrx[], double Hry[], double Hrz[], double Hix[], double Hiy[], double Hiz[]);
 

+ 1 - 1
scattnlay.cpp

@@ -1,4 +1,4 @@
-/* Generated by Cython 0.20.1post0 (Debian 0.20.1+git90-g0e6e38e-1ubuntu2) on Fri Apr 10 01:44:22 2015 */
+/* Generated by Cython 0.20.1post0 (Debian 0.20.1+git90-g0e6e38e-1ubuntu2) on Fri Apr 10 17:29:52 2015 */
 
 #define PY_SSIZE_T_CLEAN
 #ifndef CYTHON_USE_PYLONG_INTERNALS

+ 1 - 1
scattnlay.pyx

@@ -23,7 +23,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # distutils: language = c++
-# distutils: sources = nmie-wrapper.cc
+# distutils: sources = nmie.cc
 
 from __future__ import division
 import numpy as np

+ 1 - 1
setup.py

@@ -53,7 +53,7 @@ O. Pena, U. Pal, Comput. Phys. Commun. 180 (2009) 2348-2354.""",
       license = 'GPL',
       platforms = 'any',
       ext_modules = [Extension("scattnlay",
-                               ["nmie.cc", "nmie-wrapper.cc", "py_nmie.cc", "scattnlay.cpp"],
+                               ["nmie.cc", "py_nmie.cc", "scattnlay.cpp"],
                                language = "c++",
                                include_dirs = [np.get_include()])], 
       extra_compile_args=['-std=c++11']

+ 1 - 1
setup_cython.py

@@ -54,7 +54,7 @@ O. Pena, U. Pal, Comput. Phys. Commun. 180 (2009) 2348-2354.""",
       license = 'GPL',
       platforms = 'any',
       ext_modules = cythonize("scattnlay.pyx",                       # our Cython source
-                              sources = ["nmie-core.cc"],            # additional source file(s)
+                              sources = ["nmie.cc"],                 # additional source file(s)
                               language = "c++",                      # generate C++ code
                               extra_compile_args = ['-std=c++11'],
       )