compare.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2013 Ovidio Pena <ovidio@bytesfall.com> //
  3. // //
  4. // This file is part of scattnlay //
  5. // //
  6. // This program is free software: you can redistribute it and/or modify //
  7. // it under the terms of the GNU General Public License as published by //
  8. // the Free Software Foundation, either version 3 of the License, or //
  9. // (at your option) any later version. //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  14. // GNU General Public License for more details. //
  15. // //
  16. // The only additional remark is that we expect that all publications //
  17. // describing work using this software, or all commercial products //
  18. // using it, cite the following reference: //
  19. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  20. // a multilayered sphere," Computer Physics Communications, //
  21. // vol. 180, Nov. 2009, pp. 2348-2354. //
  22. // //
  23. // You should have received a copy of the GNU General Public License //
  24. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  25. //**********************************************************************************//
  26. #include <algorithm>
  27. #include <complex>
  28. #include <functional>
  29. #include <iostream>
  30. #include <stdexcept>
  31. #include <string>
  32. #include <vector>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <time.h>
  36. #include <string.h>
  37. //sudo aptitude install libgoogle-perftools-dev
  38. #include <google/heap-profiler.h>
  39. #include "nmie.h"
  40. #include "nmie-old.h"
  41. timespec diff(timespec start, timespec end);
  42. const double PI=3.14159265358979323846;
  43. template<class T> inline T pow2(const T value) {return value*value;}
  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. + "[-t ti tf nt] [-c comment]\n");
  69. enum mode_states {read_L, read_x, read_mr, read_mi, read_ti, read_tf, read_nt, read_comment};
  70. // for (auto arg : args) std::cout<< arg <<std::endl;
  71. std::string comment;
  72. int has_comment = 0;
  73. int i, l, L = 0;
  74. std::vector<double> x, Theta;
  75. std::vector<std::complex<double> > m, S1, S2;
  76. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  77. std::vector<std::complex<double> > mw, S1w, S2w;
  78. double Qextw, Qabsw, Qscaw, Qbkw, Qprw, gw, Albedow;
  79. double ti = 0.0, tf = 90.0;
  80. int nt = 0;
  81. if (argc < 5) throw std::invalid_argument(error_msg);
  82. //strcpy(comment, "");
  83. // for (i = 1; i < argc; i++) {
  84. int mode = -1;
  85. double tmp_mr;
  86. for (auto arg : args) {
  87. // For each arg in args list we detect the change of the current
  88. // read mode or read the arg. The reading args algorithm works
  89. // as a finite-state machine.
  90. // Detecting new read mode (if it is a valid -key)
  91. if (arg == "-l") {
  92. mode = read_L;
  93. continue;
  94. }
  95. if (arg == "-t") {
  96. if ((mode != read_x) && (mode != read_comment))
  97. throw std::invalid_argument(std::string("Unfinished layer!\n")
  98. +error_msg);
  99. mode = read_ti;
  100. continue;
  101. }
  102. if (arg == "-c") {
  103. if ((mode != read_x) && (mode != read_nt))
  104. throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
  105. mode = read_comment;
  106. continue;
  107. }
  108. // Reading data. For invalid date the exception will be thrown
  109. // with the std:: and catched in the end.
  110. if (mode == read_L) {
  111. L = std::stoi(arg);
  112. mode = read_x;
  113. continue;
  114. }
  115. if (mode == read_x) {
  116. x.push_back(std::stod(arg));
  117. mode = read_mr;
  118. continue;
  119. }
  120. if (mode == read_mr) {
  121. tmp_mr = std::stod(arg);
  122. mode = read_mi;
  123. continue;
  124. }
  125. if (mode == read_mi) {
  126. m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  127. mode = read_x;
  128. continue;
  129. }
  130. if (mode == read_ti) {
  131. ti = std::stod(arg);
  132. mode = read_tf;
  133. continue;
  134. }
  135. if (mode == read_tf) {
  136. tf = std::stod(arg);
  137. mode = read_nt;
  138. continue;
  139. }
  140. if (mode == read_nt) {
  141. nt = std::stoi(arg);
  142. Theta.resize(nt);
  143. S1.resize(nt);
  144. S2.resize(nt);
  145. S1w.resize(nt);
  146. S2w.resize(nt);
  147. continue;
  148. }
  149. if (mode == read_comment) {
  150. comment = arg;
  151. has_comment = 1;
  152. continue;
  153. }
  154. }
  155. if ( (x.size() != m.size()) || (L != x.size()) )
  156. throw std::invalid_argument(std::string("Broken structure!\n")
  157. +error_msg);
  158. if ( (0 == m.size()) || ( 0 == x.size()) )
  159. throw std::invalid_argument(std::string("Empty structure!\n")
  160. +error_msg);
  161. if (nt < 0) {
  162. printf("Error reading Theta.\n");
  163. return -1;
  164. } else if (nt == 1) {
  165. Theta[0] = ti*PI/180.0;
  166. } else {
  167. for (i = 0; i < nt; i++) {
  168. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  169. }
  170. }
  171. // timespec time1, time2;
  172. // long cpptime_nsec, best_cpp;
  173. // long ctime_nsec, best_c;
  174. // long cpptime_sec, ctime_sec;
  175. // long repeats = 150;
  176. // //HeapProfilerStart("heapprof");
  177. // do {
  178. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  179. // for (int i = 0; i<repeats; ++i) {
  180. // nmie::nMie(L, x, m, nt, Theta, &Qextw, &Qscaw,
  181. // &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  182. // }
  183. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  184. // cpptime_nsec = diff(time1,time2).tv_nsec;
  185. // cpptime_sec = diff(time1,time2).tv_sec;
  186. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  187. // // for (int i = 0; i<repeats; ++i) {
  188. // // nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  189. // // }
  190. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  191. // ctime_nsec = diff(time1,time2).tv_nsec;
  192. // ctime_sec = diff(time1,time2).tv_sec;
  193. // long double ratio = static_cast<long double>(ctime_nsec)
  194. // /static_cast<long double>(cpptime_nsec);
  195. // printf("-- C++ time consumed %lg sec\n", (cpptime_nsec/1e9));
  196. // if ( ratio > 0.01 ) {
  197. // if ( ctime_sec == 0 && cpptime_sec == 0) {
  198. // printf("-- C time consumed %lg sec\n", (ctime_nsec/1e9));
  199. // printf("-- total repeats: %ld\n", repeats);
  200. // printf("-- C/C++ time ratio: %Lg\n", ratio);
  201. // } else {
  202. // printf("==Test is too long!\n");
  203. // }
  204. // }
  205. // repeats *= 10;
  206. // } while (cpptime_nsec < 1e8 && ctime_nsec < 1e8);
  207. nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  208. nmie::nMie(L, x, m, nt, Theta, &Qextw, &Qscaw, &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  209. printf("\n");
  210. if (has_comment) {
  211. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e old\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  212. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e \n", comment.c_str(), Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  213. } else {
  214. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e old\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  215. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e \n", Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  216. }
  217. if (nt > 0) {
  218. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  219. for (i = 0; i < nt; i++) {
  220. printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e old\n", Theta[i]*180.0/PI, S1[i].real(), S1[i].imag(), S2[i].real(), S2[i].imag());
  221. printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e \n", Theta[i]*180.0/PI, S1w[i].real(), S1w[i].imag(), S2w[i].real(), S2w[i].imag());
  222. }
  223. }
  224. // Field testing
  225. //double size=2.0*PI*1.0/6.0;
  226. double size=0.001;
  227. double R = size/(2.0*PI);
  228. double from_coord = -3.0*size, to_coord = 3.0*size;
  229. std::vector<double> range;
  230. int samples = 1251;
  231. for (int i = 0; i < samples; ++i) {
  232. range.push_back( from_coord + (to_coord-from_coord)/(static_cast<double>(samples)-1)*i );
  233. //range.push_back(size*0.01);
  234. //range.push_back(size*0.99999);
  235. //range.push_back(R/2.0);
  236. //range.push_back(size*1.00001);
  237. //range.push_back(3);
  238. //printf("r=%g ", range.back());
  239. }
  240. // range.push_back(size*0.99999999);
  241. // range.push_back(R/2.0);
  242. // range.push_back(size*1.00000001);
  243. //printf("r/2 = %g\n", R/2.0);
  244. //int samples = range.size();
  245. std::vector<double> zero(samples, 0.0);
  246. std::vector<double> Xp, Yp, Zp;
  247. // X line
  248. Xp.insert(Xp.end(), range.begin(), range.end());
  249. Yp.insert(Yp.end(), zero.begin(), zero.end());
  250. Zp.insert(Zp.end(), zero.begin(), zero.end());
  251. //Y line
  252. Xp.insert(Xp.end(), zero.begin(), zero.end());
  253. Yp.insert(Yp.end(), range.begin(), range.end());
  254. Zp.insert(Zp.end(), zero.begin(), zero.end());
  255. // Z line
  256. Xp.insert(Xp.end(), zero.begin(), zero.end());
  257. Yp.insert(Yp.end(), zero.begin(), zero.end());
  258. Zp.insert(Zp.end(), range.begin(), range.end());
  259. int ncoord = Xp.size();
  260. // Test solid sphere
  261. x = {size};
  262. m = {std::complex<double>(2.000000,0.00)};
  263. //m = {std::complex<double>(1.414213562, 0.00)};
  264. L = x.size();
  265. int pl = 0;
  266. int nmax = 0;
  267. std::vector<std::vector<std::complex<double> > > E(ncoord), H(ncoord);
  268. for (auto& f:E) f.resize(3);
  269. for (auto& f:H) f.resize(3);
  270. double free_impedance = 376.73031;
  271. //double free_impedance = 1.0;
  272. nmie::nField( L, pl, x, m, nmax, ncoord, Xp, Yp, Zp, E, H);
  273. double sum_e = 0.0, sum_h = 0.0;
  274. printf ("Field total sum ()\n");
  275. double min_E, max_E;
  276. for (auto c:E[0]) {
  277. sum_e+=std::abs(pow2(c));
  278. }
  279. min_E = sum_e;
  280. max_E = sum_e;
  281. for (auto f:E) {
  282. sum_e = 0.0;
  283. for (auto c:f) {
  284. sum_e+=std::abs(pow2(c));
  285. //printf("component: %g + %g i\n", std::real(c), std::imag(c));
  286. }
  287. if (sum_e > max_E) max_E = sum_e;
  288. if (sum_e < min_E) min_E = sum_e;
  289. //printf("Field E=%g\n", std::sqrt(std::abs(sum_e)));
  290. }
  291. printf("Min E = %g; max E =%g", min_E, max_E);
  292. // for (auto f:H) {
  293. // sum_h = 0.0;
  294. // for (auto c:f) sum_h+=std::abs(pow2(c));
  295. // printf("Field H=%g\n", std::sqrt(std::abs(sum_h))*free_impedance);
  296. // }
  297. } catch( const std::invalid_argument& ia ) {
  298. // Will catch if multi_layer_mie fails or other errors.
  299. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  300. return -1;
  301. }
  302. return 0;
  303. }
  304. timespec diff(timespec start, timespec end)
  305. {
  306. timespec temp;
  307. if ((end.tv_nsec-start.tv_nsec)<0) {
  308. temp.tv_sec = end.tv_sec-start.tv_sec-1;
  309. temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  310. } else {
  311. temp.tv_sec = end.tv_sec-start.tv_sec;
  312. temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  313. }
  314. return temp;
  315. }