Переглянути джерело

Updated some python code to run on Python 3.

Ovidio Peña Rodríguez 5 роки тому
батько
коміт
c5a0279b99

+ 16 - 13
examples/field-nanoshell.py

@@ -41,11 +41,14 @@ n1 = 1.53413
 n2 = 0.565838 + 7.23262j
 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
 
@@ -61,7 +64,7 @@ start_time = time.time()
 terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ)
 
 elapsed_time = time.time() - start_time
-print "Time: ", elapsed_time
+print("Time: ", elapsed_time)
 
 Er = np.absolute(E)
 
@@ -107,15 +110,15 @@ try:
     plt.ylabel('Y ( nm )')
 
     # 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
 
     plt.draw()
@@ -126,6 +129,6 @@ try:
     plt.close()
 finally:
     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/>.
 
 import numpy as np
-import sys
 
 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
     near- and far-field parameters.
@@ -55,15 +54,15 @@ def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
     """
 
     if mp:
-        import scattnlay_mp as snl
+        from scattnlay_mp import scattcoeffs as scattcoeffs_
     else:
-        import scattnlay_dp as snl
+        from scattnlay_dp import scattcoeffs as scattcoeffs_
 
     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 snl.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:
@@ -84,7 +83,7 @@ def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
         else:
             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:
             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):
     """
-    scattnlay(x, m[, theta, nmax, pl])
+    scattnlay(x, m[, theta, nmax, pl, mp])
 
     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:
-        import scattnlay_mp as snl
+        from scattnlay_mp import scattnlay as scattnlay_
     else:
-        import scattnlay_dp as snl
+        from scattnlay_dp import scattnlay as scattnlay_
 
     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 snl.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:
@@ -160,7 +159,7 @@ def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
         else:
             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
 #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):
     """
-    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.
 
@@ -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)
         xp: Array containing all X coordinates to calculate the complex
             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
               calculations. Only use it if you know what you are doing.
         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:
-        import scattnlay_mp as snl
+        from scattnlay_mp import fieldnlay as fieldnlay_
     else:
-        import scattnlay_dp as snl
+        from scattnlay_dp import fieldnlay as 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 snl.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:
@@ -212,7 +215,7 @@ def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
         else:
             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
 #fieldnlay()

+ 1 - 1
tests/python/test01.py

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

+ 1 - 1
tests/python/test02.py

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

+ 1 - 1
tests/python/test04.py

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