nearfield.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //******************************************************************************
  2. // Copyright (C) 2009-2022 Ovidio Pena <ovidio@bytesfall.com>
  3. // Copyright (C) 2013-2022 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 <complex>
  32. #include <cstdio>
  33. #include <iostream>
  34. #include <stdexcept>
  35. #include <string>
  36. #include <vector>
  37. #include "nmie.hpp"
  38. //******************************************************************************
  39. // This is the main function of 'scattnlay', here we read the parameters as
  40. // arguments passed to the program which should be executed with the following
  41. // syntaxis:
  42. // ./fieldnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...]
  43. // -p xi xf nx yi yf ny zi zf nz [-c comment]
  44. //
  45. // When all the parameters were correctly passed we setup the integer L (the
  46. // number of layers) and the arrays x and m, containing the size parameters and
  47. // refractive indexes of the layers, respectively and call the function nMie.
  48. // If the calculation is successful the results are printed with the following
  49. // format:
  50. //
  51. // '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,
  52. // Hz.i'
  53. //
  54. //******************************************************************************
  55. int main(int argc, char* argv[]) {
  56. try {
  57. std::vector<std::string> args;
  58. args.assign(argv, argv + argc);
  59. std::string error_msg(std::string("Insufficient parameters.\nUsage: ") +
  60. args[0] +
  61. " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] " +
  62. " -p xi xf nx yi yf ny zi zf nz [-c comment]\n");
  63. enum mode_states {
  64. read_L,
  65. read_x,
  66. read_mr,
  67. read_mi,
  68. read_xi,
  69. read_xf,
  70. read_nx,
  71. read_yi,
  72. read_yf,
  73. read_ny,
  74. read_zi,
  75. read_zf,
  76. read_nz,
  77. read_comment
  78. };
  79. // for (auto arg : args) std::cout<< arg <<std::endl;
  80. std::string comment;
  81. int has_comment = 0;
  82. unsigned int L = 0;
  83. std::vector<double> x, Xp, Yp, Zp;
  84. std::vector<std::complex<double> > m;
  85. std::vector<std::vector<std::complex<double> > > E, H;
  86. double xi = 0.0, xf = 0.0, yi = 0.0, yf = 0.0, zi = 0.0, zf = 0.0;
  87. double dx = 0.0, dy = 0.0, dz = 0.0;
  88. int nx = 0, ny = 0, nz = 0;
  89. long total_points = 0;
  90. if (argc < 5)
  91. throw std::invalid_argument(error_msg);
  92. int mode = -1;
  93. double tmp_mr;
  94. for (const auto& arg : args) {
  95. // For each arg in args list we detect the change of the current
  96. // read mode or read the arg. The reading args algorithm works
  97. // as a finite-state machine.
  98. // Detecting new read mode (if it is a valid -key)
  99. if (arg == "-l") {
  100. mode = read_L;
  101. continue;
  102. }
  103. if (arg == "-p") {
  104. if ((mode != read_x) && (mode != read_comment))
  105. throw std::invalid_argument(std::string("Unfinished layer!\n") +
  106. error_msg);
  107. mode = read_xi;
  108. continue;
  109. }
  110. if (arg == "-c") {
  111. if ((mode != read_x) && (mode != read_nz))
  112. throw std::invalid_argument(
  113. std::string("Unfinished layer or theta!\n") + error_msg);
  114. mode = read_comment;
  115. continue;
  116. }
  117. // Reading data. For invalid date the exception will be thrown
  118. // with the std:: and catched in the end.
  119. if (mode == read_L) {
  120. L = std::stoi(arg);
  121. mode = read_x;
  122. continue;
  123. }
  124. if (mode == read_x) {
  125. x.push_back(std::stod(arg));
  126. mode = read_mr;
  127. continue;
  128. }
  129. if (mode == read_mr) {
  130. tmp_mr = std::stod(arg);
  131. mode = read_mi;
  132. continue;
  133. }
  134. if (mode == read_mi) {
  135. m.emplace_back(tmp_mr, std::stod(arg));
  136. mode = read_x;
  137. continue;
  138. }
  139. if (mode == read_xi) {
  140. xi = std::stod(arg);
  141. mode = read_xf;
  142. continue;
  143. }
  144. if (mode == read_xf) {
  145. xf = std::stod(arg);
  146. mode = read_nx;
  147. continue;
  148. }
  149. if (mode == read_nx) {
  150. nx = std::stoi(arg);
  151. mode = read_yi;
  152. continue;
  153. }
  154. if (mode == read_yi) {
  155. yi = std::stod(arg);
  156. mode = read_yf;
  157. continue;
  158. }
  159. if (mode == read_yf) {
  160. yf = std::stod(arg);
  161. mode = read_ny;
  162. continue;
  163. }
  164. if (mode == read_ny) {
  165. ny = std::stoi(arg);
  166. mode = read_zi;
  167. continue;
  168. }
  169. if (mode == read_zi) {
  170. zi = std::stod(arg);
  171. mode = read_zf;
  172. continue;
  173. }
  174. if (mode == read_zf) {
  175. zf = std::stod(arg);
  176. mode = read_nz;
  177. continue;
  178. }
  179. if (mode == read_nz) {
  180. nz = std::stoi(arg);
  181. total_points = nx * ny * nz;
  182. if (total_points <= 0)
  183. throw std::invalid_argument(
  184. std::string("Nothing to do! You must define the grid to "
  185. "calculate the fields.\n") +
  186. error_msg);
  187. Xp.resize(total_points);
  188. Yp.resize(total_points);
  189. Zp.resize(total_points);
  190. E.resize(total_points);
  191. H.resize(total_points);
  192. for (long i = 0; i < total_points; i++) {
  193. E[i].resize(3);
  194. H[i].resize(3);
  195. }
  196. continue;
  197. }
  198. if (mode == read_comment) {
  199. comment = arg;
  200. has_comment = 1;
  201. continue;
  202. }
  203. }
  204. if ((x.size() != m.size()) || (L != x.size()))
  205. throw std::invalid_argument(std::string("Broken structure!\n") +
  206. error_msg);
  207. if ((m.empty()) || (x.empty()))
  208. throw std::invalid_argument(std::string("Empty structure!\n") +
  209. error_msg);
  210. if (nx == 1)
  211. dx = 0.0;
  212. else
  213. dx = (xf - xi) / (nx - 1);
  214. if (ny == 1)
  215. dy = 0.0;
  216. else
  217. dy = (yf - yi) / (ny - 1);
  218. if (nz == 1)
  219. dz = 0.0;
  220. else
  221. dz = (zf - zi) / (nz - 1);
  222. for (int i = 0; i < nx; i++) {
  223. for (int j = 0; j < ny; j++) {
  224. for (int k = 0; k < nz; k++) {
  225. Xp[i * ny * nz + j * nz + k] = xi + (double)i * dx;
  226. Yp[i * ny * nz + j * nz + k] = yi + (double)j * dy;
  227. Zp[i * ny * nz + j * nz + k] = zi + (double)k * dz;
  228. }
  229. }
  230. }
  231. int nmax = nmie::nField(L, -1, x, m, -1, nmie::Modes::kAll,
  232. nmie::Modes::kAll, total_points, Xp, Yp, Zp, E, H);
  233. printf("Number of multipoles used in Mie series nmax=%i\n", nmax);
  234. if (has_comment)
  235. printf("%6s\n", comment.c_str());
  236. if (total_points > 0) {
  237. printf(
  238. " X, Y, Z, Ex.r, Ex.i, "
  239. " Ey.r, Ey.i, Ez.r, Ez.i, Hx.r, "
  240. " Hx.i, Hy.r, Hy.i, Hz.r, "
  241. "Hz.i\n");
  242. for (long i = 0; i < total_points; i++) {
  243. printf(
  244. "%10.7f, %10.7f, %10.7f, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, "
  245. "%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n",
  246. Xp[i], Yp[i], Zp[i], E[i][0].real(), E[i][0].imag(), E[i][1].real(),
  247. E[i][1].imag(), E[i][2].real(), E[i][2].imag(), H[i][0].real(),
  248. H[i][0].imag(), H[i][1].real(), H[i][1].imag(), H[i][2].real(),
  249. H[i][2].imag());
  250. }
  251. }
  252. } catch (const std::invalid_argument& ia) {
  253. // Will catch if multi_layer_mie fails or other errors.
  254. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  255. return -1;
  256. }
  257. return 0;
  258. }