nearfield.cc 9.5 KB

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