nearfield.cc 9.7 KB

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