nearfield.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2018 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2018 Konstantin Ladutenko <kostyfisik@gmail.com> //
  4. // //
  5. // This file is part of scattnlay //
  6. // //
  7. // This program is free software: you can redistribute it and/or modify //
  8. // it under the terms of the GNU General Public License as published by //
  9. // the Free Software Foundation, either version 3 of the License, or //
  10. // (at your option) any later version. //
  11. // //
  12. // This program is distributed in the hope that it will be useful, //
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  15. // GNU General Public License for more details. //
  16. // //
  17. // The only additional remark is that we expect that all publications //
  18. // describing work using this software, or all commercial products //
  19. // using it, cite at least one of the following references: //
  20. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  21. // a multilayered sphere," Computer Physics Communications, //
  22. // vol. 180, Nov. 2009, pp. 2348-2354. //
  23. // [2] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  24. // calculation of electromagnetic near-field for a multilayered //
  25. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  26. // pp. 225-230. //
  27. // //
  28. // You should have received a copy of the GNU General Public License //
  29. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  30. //**********************************************************************************//
  31. #include <algorithm>
  32. #include <complex>
  33. #include <functional>
  34. #include <iostream>
  35. #include <stdexcept>
  36. #include <string>
  37. #include <vector>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <time.h>
  41. #include <string.h>
  42. #include "nmie.hpp"
  43. const double PI=3.14159265358979323846;
  44. //***********************************************************************************//
  45. // This is the main function of 'scattnlay', here we read the parameters as //
  46. // arguments passed to the program which should be executed with the following //
  47. // syntaxis: //
  48. // ./scattnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment] //
  49. // //
  50. // When all the parameters were correctly passed we setup the integer L (the //
  51. // number of layers) and the arrays x and m, containing the size parameters and //
  52. // refractive indexes of the layers, respectively and call the function nMie. //
  53. // If the calculation is successful the results are printed with the following //
  54. // format: //
  55. // //
  56. // * If no comment was passed: //
  57. // 'Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  58. // //
  59. // * If a comment was passed: //
  60. // 'comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  61. //***********************************************************************************//
  62. int main(int argc, char *argv[]) {
  63. try {
  64. std::vector<std::string> args;
  65. args.assign(argv, argv + argc);
  66. std::string error_msg(std::string("Insufficient parameters.\nUsage: ") + args[0]
  67. + " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] "
  68. + " -p xi xf nx yi yf ny zi zf nz [-c comment]\n");
  69. enum mode_states {read_L, read_x, read_mr, read_mi, read_xi, read_xf, read_nx, read_yi, read_yf, read_ny, read_zi, read_zf, read_nz, read_comment};
  70. // for (auto arg : args) std::cout<< arg <<std::endl;
  71. std::string comment;
  72. int has_comment = 0;
  73. unsigned int L = 0;
  74. std::vector<double> x, Xp, Yp, Zp;
  75. std::vector<std::complex<double> > m;
  76. std::vector<std::vector<std::complex<double> > > E, H;
  77. double xi = 0.0, xf = 0.0, yi = 0.0, yf = 0.0, zi = 0.0, zf = 0.0;
  78. double dx = 0.0, dy = 0.0, dz = 0.0;
  79. int nx = 0, ny = 0, nz = 0;
  80. long total_points = 0;
  81. if (argc < 5) throw std::invalid_argument(error_msg);
  82. int mode = -1;
  83. double tmp_mr;
  84. for (auto arg : args) {
  85. // For each arg in args list we detect the change of the current
  86. // read mode or read the arg. The reading args algorithm works
  87. // as a finite-state machine.
  88. // Detecting new read mode (if it is a valid -key)
  89. if (arg == "-l") {
  90. mode = read_L;
  91. continue;
  92. }
  93. if (arg == "-p") {
  94. if ((mode != read_x) && (mode != read_comment))
  95. throw std::invalid_argument(std::string("Unfinished layer!\n") + error_msg);
  96. mode = read_xi;
  97. continue;
  98. }
  99. if (arg == "-c") {
  100. if ((mode != read_x) && (mode != read_nz))
  101. throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
  102. mode = read_comment;
  103. continue;
  104. }
  105. // Reading data. For invalid date the exception will be thrown
  106. // with the std:: and catched in the end.
  107. if (mode == read_L) {
  108. L = std::stoi(arg);
  109. mode = read_x;
  110. continue;
  111. }
  112. if (mode == read_x) {
  113. x.push_back(std::stod(arg));
  114. mode = read_mr;
  115. continue;
  116. }
  117. if (mode == read_mr) {
  118. tmp_mr = std::stod(arg);
  119. mode = read_mi;
  120. continue;
  121. }
  122. if (mode == read_mi) {
  123. m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  124. mode = read_x;
  125. continue;
  126. }
  127. if (mode == read_xi) {
  128. xi = std::stod(arg);
  129. mode = read_xf;
  130. continue;
  131. }
  132. if (mode == read_xf) {
  133. xf = std::stod(arg);
  134. mode = read_nx;
  135. continue;
  136. }
  137. if (mode == read_nx) {
  138. nx = std::stoi(arg);
  139. mode = read_yi;
  140. continue;
  141. }
  142. if (mode == read_yi) {
  143. yi = std::stod(arg);
  144. mode = read_yf;
  145. continue;
  146. }
  147. if (mode == read_yf) {
  148. yf = std::stod(arg);
  149. mode = read_ny;
  150. continue;
  151. }
  152. if (mode == read_ny) {
  153. ny = std::stoi(arg);
  154. mode = read_zi;
  155. continue;
  156. }
  157. if (mode == read_zi) {
  158. zi = std::stod(arg);
  159. mode = read_zf;
  160. continue;
  161. }
  162. if (mode == read_zf) {
  163. zf = std::stod(arg);
  164. mode = read_nz;
  165. continue;
  166. }
  167. if (mode == read_nz) {
  168. nz = std::stoi(arg);
  169. total_points = nx*ny*nz;
  170. if (total_points <= 0)
  171. throw std::invalid_argument(std::string("Nothing to do! You must define the grid to calculate the fields.\n") + error_msg);
  172. Xp.resize(total_points);
  173. Yp.resize(total_points);
  174. Zp.resize(total_points);
  175. E.resize(total_points);
  176. H.resize(total_points);
  177. for (long i = 0; i < total_points; i++) {
  178. E[i].resize(3);
  179. H[i].resize(3);
  180. }
  181. continue;
  182. }
  183. if (mode == read_comment) {
  184. comment = arg;
  185. has_comment = 1;
  186. continue;
  187. }
  188. }
  189. if ( (x.size() != m.size()) || (L != x.size()) )
  190. throw std::invalid_argument(std::string("Broken structure!\n") + error_msg);
  191. if ( (0 == m.size()) || ( 0 == x.size()) )
  192. throw std::invalid_argument(std::string("Empty structure!\n") + error_msg);
  193. if (nx == 1)
  194. dx = 0.0;
  195. else
  196. dx = (xf - xi)/(nx - 1);
  197. if (ny == 1)
  198. dy = 0.0;
  199. else
  200. dy = (yf - yi)/(ny - 1);
  201. if (nz == 1)
  202. dz = 0.0;
  203. else
  204. dz = (zf - zi)/(nz - 1);
  205. for (int i = 0; i < nx; i++) {
  206. for (int j = 0; j < ny; j++) {
  207. for (int k = 0; k < nz; k++) {
  208. Xp[i*ny*nz + j*nz + k] = xi + (double)i*dx;
  209. Yp[i*ny*nz + j*nz + k] = yi + (double)j*dy;
  210. Zp[i*ny*nz + j*nz + k] = zi + (double)k*dz;
  211. }
  212. }
  213. }
  214. nmie::nField(L, -1, x, m, -1, total_points, Xp, Yp, Zp, E, H);
  215. if (has_comment)
  216. printf("%6s\n", comment.c_str());
  217. if (total_points > 0) {
  218. printf(" X, Y, Z, Ex.r, Ex.i, Ey.r, Ey.i, Ez.r, Ez.i, Hx.r, Hx.i, Hy.r, Hy.i, Hz.r, Hz.i\n");
  219. for (long i = 0; i < total_points; i++) {
  220. printf("%10.7f, %10.7f, %10.7f, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n",
  221. Xp[i], Yp[i], Zp[i],
  222. E[i][0].real(), E[i][0].imag(), E[i][1].real(), E[i][1].imag(), E[i][2].real(), E[i][2].imag(),
  223. H[i][0].real(), H[i][0].imag(), H[i][1].real(), H[i][1].imag(), H[i][2].real(), H[i][2].imag());
  224. }
  225. }
  226. } catch( const std::invalid_argument& ia ) {
  227. // Will catch if multi_layer_mie fails or other errors.
  228. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  229. return -1;
  230. }
  231. return 0;
  232. }