Browse Source

Updated some python code to run on Python 3.

Ovidio Peña Rodríguez 5 years ago
parent
commit
c5a0279b99
5 changed files with 38 additions and 32 deletions
  1. 16 13
      examples/field-nanoshell.py
  2. 19 16
      scattnlay/main.py
  3. 1 1
      tests/python/test01.py
  4. 1 1
      tests/python/test02.py
  5. 1 1
      tests/python/test04.py

+ 16 - 13
examples/field-nanoshell.py

@@ -41,11 +41,14 @@ n1 = 1.53413
 n2 = 0.565838 + 7.23262j
 n2 = 0.565838 + 7.23262j
 nm = 1.3205
 nm = 1.3205
 
 
-x = 2.0*np.pi*nm*np.array([[0.05, 0.06]], dtype = np.float64)/1.064
-m = np.array([[n1/nm, n2/nm]], dtype = np.complex128)
+r1 = 0.05
+r2 = 0.06
 
 
-print "x =", x
-print "m =", m
+x = 2.0*np.pi*nm*np.array([[r1, r2]], dtype = np.float64)/1.064
+m = np.array((n1/nm, n2/nm), dtype = np.complex128)
+
+print("x =", x)
+print("m =", m)
 
 
 npts = 501
 npts = 501
 
 
@@ -61,7 +64,7 @@ start_time = time.time()
 terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ)
 terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ)
 
 
 elapsed_time = time.time() - start_time
 elapsed_time = time.time() - start_time
-print "Time: ", elapsed_time
+print("Time: ", elapsed_time)
 
 
 Er = np.absolute(E)
 Er = np.absolute(E)
 
 
@@ -107,15 +110,15 @@ try:
     plt.ylabel('Y ( nm )')
     plt.ylabel('Y ( nm )')
 
 
     # This part draws the nanoshell
     # This part draws the nanoshell
-#    from matplotlib import patches
+    from matplotlib import patches
 
 
-#    s1 = patches.Arc((0, 0), 2.0*x[0, 0], 2.0*x[0, 0], angle=0.0, zorder=2,
-#                      theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
-#    ax.add_patch(s1)
+    s1 = patches.Arc((0, 0), 2000.0*r1, 2000.0*r1, angle=0.0, zorder=2,
+                      theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
+    ax.add_patch(s1)
 
 
-#    s2 = patches.Arc((0, 0), 2.0*x[0, 1], 2.0*x[0, 1], angle=0.0, zorder=2,
-#                      theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
-#    ax.add_patch(s2)
+    s2 = patches.Arc((0, 0), 2000.0*r2, 2000.0*r2, angle=0.0, zorder=2,
+                      theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
+    ax.add_patch(s2)
     # End of drawing
     # End of drawing
 
 
     plt.draw()
     plt.draw()
@@ -126,6 +129,6 @@ try:
     plt.close()
     plt.close()
 finally:
 finally:
     np.savetxt("field-nanoshell.txt", result, fmt = "%.5f")
     np.savetxt("field-nanoshell.txt", result, fmt = "%.5f")
-    print result
+    print(result)
 
 
 
 

+ 19 - 16
scattnlay/main.py

@@ -31,11 +31,10 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 import numpy as np
 import numpy as np
-import sys
 
 
 def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
 def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
     """
     """
-    scattcoeffs(x, m[, nmax, pl])
+    scattcoeffs(x, m[, nmax, pl, mp])
 
 
     Calculate the scattering coefficients required to calculate both the
     Calculate the scattering coefficients required to calculate both the
     near- and far-field parameters.
     near- and far-field parameters.
@@ -55,15 +54,15 @@ def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
     """
     """
 
 
     if mp:
     if mp:
-        import scattnlay_mp as snl
+        from scattnlay_mp import scattcoeffs as scattcoeffs_
     else:
     else:
-        import scattnlay_dp as snl
+        from scattnlay_dp import scattcoeffs as scattcoeffs_
 
 
     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 snl.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:
@@ -84,7 +83,7 @@ def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
         else:
         else:
             mi = m[i]
             mi = m[i]
 
 
-        terms[i], a, b = snl.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]
@@ -100,7 +99,7 @@ def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
 
 
 def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
 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, mp])
 
 
     Calculate the actual scattering parameters and amplitudes.
     Calculate the actual scattering parameters and amplitudes.
 
 
@@ -127,15 +126,15 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
     """
     """
 
 
     if mp:
     if mp:
-        import scattnlay_mp as snl
+        from scattnlay_mp import scattnlay as scattnlay_
     else:
     else:
-        import scattnlay_dp as snl
+        from scattnlay_dp import scattnlay as scattnlay_
 
 
     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 snl.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:
@@ -160,7 +159,7 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
         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] = snl.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()
@@ -168,7 +167,7 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
 
 
 def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
 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[, nmax, pl, mp])
 
 
     Calculate the actual scattering parameters and amplitudes.
     Calculate the actual scattering parameters and amplitudes.
 
 
@@ -176,6 +175,10 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
         m: Relative refractive indices (1D or 2D ndarray)
         m: Relative refractive indices (1D or 2D ndarray)
         xp: Array containing all X coordinates to calculate the complex
         xp: Array containing all X coordinates to calculate the complex
             electric and magnetic fields (1D ndarray)
             electric and magnetic fields (1D ndarray)
+        yp: Array containing all Y coordinates to calculate the complex
+            electric and magnetic fields (1D ndarray)
+        zp: Array containing all Z coordinates to calculate the complex
+            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. If there is none just send -1.
         pl: Index of PEC layer. If there is none just send -1.
@@ -188,15 +191,15 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
     """
     """
 
 
     if mp:
     if mp:
-        import scattnlay_mp as snl
+        from scattnlay_mp import fieldnlay as fieldnlay_
     else:
     else:
-        import scattnlay_dp as snl
+        from scattnlay_dp import fieldnlay as 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 snl.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:
@@ -212,7 +215,7 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
         else:
         else:
             mi = m[i]
             mi = m[i]
 
 
-        terms[i], E[i], H[i] = snl.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()

+ 1 - 1
tests/python/test01.py

@@ -94,5 +94,5 @@ try:
     plt.show()
     plt.show()
 finally:
 finally:
     np.savetxt("test01.txt", result, fmt = "%.5f")
     np.savetxt("test01.txt", result, fmt = "%.5f")
-    print result
+    print(result)
 
 

+ 1 - 1
tests/python/test02.py

@@ -78,5 +78,5 @@ try:
     plt.show()
     plt.show()
 finally:
 finally:
     np.savetxt("test02.txt", result, fmt = "%.5f")
     np.savetxt("test02.txt", result, fmt = "%.5f")
-    print result
+    print(result)
 
 

+ 1 - 1
tests/python/test04.py

@@ -81,6 +81,6 @@ try:
     plt.show()
     plt.show()
 finally:
 finally:
     np.savetxt("test04.txt", result, fmt = "%.5f")
     np.savetxt("test04.txt", result, fmt = "%.5f")
-    print result
+    print(result)