coating-flow.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2015 Ovidio Peña Rodríguez <ovidio@bytesfall.com>
  5. #
  6. # This file is part of python-scattnlay
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # The only additional remark is that we expect that all publications
  19. # describing work using this software, or all commercial products
  20. # using it, cite the following reference:
  21. # [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by
  22. # a multilayered sphere," Computer Physics Communications,
  23. # vol. 180, Nov. 2009, pp. 2348-2354.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. # This test case calculates the electric field in the
  28. # E-k plane, for an spherical Si-Ag-Si nanoparticle. Core radius is 17.74 nm,
  29. # inner layer 23.31nm, outer layer 22.95nm. Working wavelength is 800nm, we use
  30. # silicon epsilon=13.64+i0.047, silver epsilon= -28.05+i1.525
  31. import os, cmath
  32. import numpy as np
  33. from scattnlay import fieldnlay
  34. from fieldplot import fieldplot
  35. if __name__ == '__main__':
  36. import argparse
  37. parser = argparse.ArgumentParser()
  38. parser.add_argument("dirnames", nargs='*', default='.', help="read all data from DIR(S)")
  39. parser.add_argument("-f", "--filename", dest="fname", nargs='?', default=None,
  40. help="name of 'n' file")
  41. parser.add_argument("-w", "--wavelength", dest="wl", default=3.75, type=float,
  42. help="wavelength of electromagnetic wave")
  43. parser.add_argument("-r", "--radius", dest="rad", default=None, type=float,
  44. help="radius of PEC sphere")
  45. parser.add_argument("-t", "--thickness", dest="tc", default=0.8, type=float,
  46. help="thickness of cloaking layer")
  47. parser.add_argument("-n", "--npoints", dest="npts", default=101, type=int,
  48. help="number of points for the grid")
  49. args = parser.parse_args()
  50. for dirname in args.dirnames:
  51. print "Calculating spectra for data file(s) in dir '%s'..." % (dirname)
  52. wl = args.wl # cm
  53. if (args.rad is None):
  54. Rs = 0.75*wl # cm
  55. else:
  56. Rs = args.rad # cm
  57. tc = args.tc # cm
  58. if (args.fname is None):
  59. files = [x for x in os.listdir('%s/' % (dirname)) if x.endswith('.dat')]
  60. files.sort()
  61. else:
  62. files = [args.fname]
  63. npts = args.npts # cm
  64. if not os.path.exists('%s/flow-results/' % (dirname)):
  65. os.makedirs('%s/flow-results/' % (dirname))
  66. Rt = Rs + tc # cm
  67. print "Wl = %.2f, Rs = %.2f, tc = %.2f, Rt = %.2f" % (wl, Rs, tc, Rt)
  68. ms = 1.0 + 40.0j
  69. for i, fname in enumerate(files):
  70. print "Calculating spectra for file '%s'..." % (fname)
  71. basename = os.path.splitext(fname)[0]
  72. nvalues = np.loadtxt('%s/%s' % (dirname, fname))*1.0 + 1e-11j
  73. tl = tc/len(nvalues) # cm
  74. r = [Rs]
  75. for i in range(len(nvalues)):
  76. r += [r[i] + tl]
  77. x = np.ones((len(nvalues) + 1), dtype = np.float64)
  78. m = np.ones((len(nvalues) + 1), dtype = np.complex128)
  79. x = 2.0*np.pi*np.array(r, dtype = np.float64)/wl
  80. m = np.array([ms] + nvalues[:, 1].tolist(), dtype = np.complex128)
  81. print(x,m)
  82. factor = 2.91*x[0]/x[-1]
  83. print factor
  84. comment='PEC-'+basename
  85. WL_units=''
  86. #flow_total = 39
  87. # flow_total = 23 #SV False
  88. flow_total = 24
  89. #flow_total = 4
  90. #crossplane='XZ'
  91. crossplane='XYZ'
  92. #crossplane='YZ'
  93. #crossplane='XY'
  94. # Options to plot: Eabs, Habs, Pabs, angleEx, angleHy
  95. #field_to_plot='Pabs'
  96. #field_to_plot='Eabs'
  97. field_to_plot='angleEx'
  98. #field_to_plot='angleHy'
  99. print "x =", x
  100. print "m =", m
  101. import matplotlib.pyplot as plt
  102. plt.rcParams.update({'font.size': 16})
  103. fig, axs = plt.subplots(1,1)#, sharey=True, sharex=True)
  104. fig.tight_layout()
  105. fieldplot(fig, axs, x,m, wl, comment, WL_units, crossplane, field_to_plot, npts, factor, flow_total,
  106. subplot_label=' ',is_flow_extend=False
  107. , outline_width=1.5
  108. , pl=0 #PEC layer starts the design
  109. )
  110. # fieldplot(fig, axs, x[0],m[0], wl, comment, WL_units, crossplane, field_to_plot, npts, factor, flow_total,
  111. # subplot_label=' ' ,is_flow_extend=False
  112. # , outline_width=1.5
  113. # , pl=0 #PEC layer starts the design
  114. # )
  115. fig.subplots_adjust(hspace=0.3, wspace=-0.1)
  116. plt.savefig(comment+"-R"+str(int(round(x[-1]*wl/2.0/np.pi)))+"-"+crossplane+"-"
  117. +field_to_plot+".pdf",pad_inches=0.02, bbox_inches='tight')
  118. plt.draw()
  119. plt.clf()
  120. plt.close()
  121. print "Done!!"