compare.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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-wrapper.h"
  41. timespec diff(timespec start, timespec end);
  42. const double PI=3.14159265358979323846;
  43. //***********************************************************************************//
  44. // This is the main function of 'scattnlay', here we read the parameters as //
  45. // arguments passed to the program which should be executed with the following //
  46. // syntaxis: //
  47. // ./scattnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment] //
  48. // //
  49. // When all the parameters were correctly passed we setup the integer L (the //
  50. // number of layers) and the arrays x and m, containing the size parameters and //
  51. // refractive indexes of the layers, respectively and call the function nMie. //
  52. // If the calculation is successful the results are printed with the following //
  53. // format: //
  54. // //
  55. // * If no comment was passed: //
  56. // 'Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  57. // //
  58. // * If a comment was passed: //
  59. // 'comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  60. //***********************************************************************************//
  61. int main(int argc, char *argv[]) {
  62. try {
  63. std::vector<std::string> args;
  64. args.assign(argv, argv + argc);
  65. std::string error_msg(std::string("Insufficient parameters.\nUsage: ") + args[0]
  66. + " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] "
  67. + "[-t ti tf nt] [-c comment]\n");
  68. enum mode_states {read_L, read_x, read_mr, read_mi, read_ti, read_tf, read_nt, read_comment};
  69. // for (auto arg : args) std::cout<< arg <<std::endl;
  70. std::string comment;
  71. int has_comment = 0;
  72. int i, l, L = 0;
  73. std::vector<double> x, Theta;
  74. std::vector<std::complex<double> > m, S1, S2;
  75. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  76. std::vector<std::complex<double> > mw, S1w, S2w;
  77. double Qextw, Qabsw, Qscaw, Qbkw, Qprw, gw, Albedow;
  78. double ti = 0.0, tf = 90.0;
  79. int nt = 0;
  80. if (argc < 5) throw std::invalid_argument(error_msg);
  81. //strcpy(comment, "");
  82. // for (i = 1; i < argc; i++) {
  83. int mode = -1;
  84. double tmp_mr;
  85. for (auto arg : args) {
  86. // For each arg in args list we detect the change of the current
  87. // read mode or read the arg. The reading args algorithm works
  88. // as a finite-state machine.
  89. // Detecting new read mode (if it is a valid -key)
  90. if (arg == "-l") {
  91. mode = read_L;
  92. continue;
  93. }
  94. if (arg == "-t") {
  95. if ((mode != read_x) && (mode != read_comment))
  96. throw std::invalid_argument(std::string("Unfinished layer!\n")
  97. +error_msg);
  98. mode = read_ti;
  99. continue;
  100. }
  101. if (arg == "-c") {
  102. if ((mode != read_x) && (mode != read_nt))
  103. throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
  104. mode = read_comment;
  105. continue;
  106. }
  107. // Reading data. For invalid date the exception will be thrown
  108. // with the std:: and catched in the end.
  109. if (mode == read_L) {
  110. L = std::stoi(arg);
  111. mode = read_x;
  112. continue;
  113. }
  114. if (mode == read_x) {
  115. x.push_back(std::stod(arg));
  116. mode = read_mr;
  117. continue;
  118. }
  119. if (mode == read_mr) {
  120. tmp_mr = std::stod(arg);
  121. mode = read_mi;
  122. continue;
  123. }
  124. if (mode == read_mi) {
  125. m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  126. mode = read_x;
  127. continue;
  128. }
  129. // if (strcmp(argv[i], "-l") == 0) {
  130. // i++;
  131. // L = atoi(argv[i]);
  132. // x.resize(L);
  133. // m.resize(L);
  134. // if (argc < 3*(L + 1)) {
  135. // throw std::invalid_argument(error_msg);
  136. // } else {
  137. // for (l = 0; l < L; l++) {
  138. // i++;
  139. // x[l] = atof(argv[i]);
  140. // i++;
  141. // m[l] = std::complex<double>(atof(argv[i]), atof(argv[i + 1]));
  142. // i++;
  143. // }
  144. // }
  145. if (mode == read_ti) {
  146. ti = std::stod(arg);
  147. mode = read_tf;
  148. continue;
  149. }
  150. if (mode == read_tf) {
  151. tf = std::stod(arg);
  152. mode = read_nt;
  153. continue;
  154. }
  155. if (mode == read_nt) {
  156. nt = std::stoi(arg);
  157. Theta.resize(nt);
  158. S1.resize(nt);
  159. S2.resize(nt);
  160. S1w.resize(nt);
  161. S2w.resize(nt);
  162. continue;
  163. }
  164. //} else if (strcmp(argv[i], "-t") == 0) {
  165. // i++;
  166. // ti = atof(argv[i]);
  167. // i++;
  168. // tf = atof(argv[i]);
  169. // i++;
  170. // nt = atoi(argv[i]);
  171. // Theta.resize(nt);
  172. // S1.resize(nt);
  173. // S2.resize(nt);
  174. if (mode == read_comment) {
  175. comment = arg;
  176. has_comment = 1;
  177. continue;
  178. }
  179. // } else if (strcmp(argv[i], "-c") == 0) {
  180. // i++;
  181. // comment = args[i];
  182. // //strcpy(comment, argv[i]);
  183. // has_comment = 1;
  184. // } else { i++; }
  185. }
  186. if ( (x.size() != m.size()) || (L != x.size()) )
  187. throw std::invalid_argument(std::string("Broken structure!\n")
  188. +error_msg);
  189. if ( (0 == m.size()) || ( 0 == x.size()) )
  190. throw std::invalid_argument(std::string("Empty structure!\n")
  191. +error_msg);
  192. if (nt < 0) {
  193. printf("Error reading Theta.\n");
  194. return -1;
  195. } else if (nt == 1) {
  196. Theta[0] = ti*PI/180.0;
  197. } else {
  198. for (i = 0; i < nt; i++) {
  199. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  200. }
  201. }
  202. timespec time1, time2;
  203. // long cpptime_nsec, best_cpp;
  204. // long ctime_nsec, best_c;
  205. // long cpptime_sec, ctime_sec;
  206. // long repeats = 150;
  207. // //HeapProfilerStart("heapprof");
  208. // do {
  209. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  210. // for (int i = 0; i<repeats; ++i) {
  211. // nmie::nMie_wrapper(L, x, m, nt, Theta, &Qextw, &Qscaw,
  212. // &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  213. // }
  214. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  215. // cpptime_nsec = diff(time1,time2).tv_nsec;
  216. // cpptime_sec = diff(time1,time2).tv_sec;
  217. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  218. // // for (int i = 0; i<repeats; ++i) {
  219. // // nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  220. // // }
  221. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  222. // ctime_nsec = diff(time1,time2).tv_nsec;
  223. // ctime_sec = diff(time1,time2).tv_sec;
  224. // long double ratio = static_cast<long double>(ctime_nsec)
  225. // /static_cast<long double>(cpptime_nsec);
  226. // printf("-- C++ time consumed %lg sec\n", (cpptime_nsec/1e9));
  227. // if ( ratio > 0.01 ) {
  228. // if ( ctime_sec == 0 && cpptime_sec == 0) {
  229. // printf("-- C time consumed %lg sec\n", (ctime_nsec/1e9));
  230. // printf("-- total repeats: %ld\n", repeats);
  231. // printf("-- C/C++ time ratio: %Lg\n", ratio);
  232. // } else {
  233. // printf("==Test is too long!\n");
  234. // }
  235. // }
  236. // repeats *= 10;
  237. // } while (cpptime_nsec < 1e8 && ctime_nsec < 1e8);
  238. nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  239. nmie::nMie_wrapper(L, x, m, nt, Theta, &Qextw, &Qscaw, &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  240. printf("\n");
  241. if (has_comment) {
  242. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e old\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  243. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e wrapper\n", comment.c_str(), Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  244. } else {
  245. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e old\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  246. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e wrapper\n", Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  247. }
  248. if (nt > 0) {
  249. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  250. for (i = 0; i < nt; i++) {
  251. 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());
  252. printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e wrapper\n", Theta[i]*180.0/PI, S1w[i].real(), S1w[i].imag(), S2w[i].real(), S2w[i].imag());
  253. }
  254. }
  255. // Field testing
  256. double from_coord = -3.0, to_coord = 3.0;
  257. int samples = 11;
  258. std::vector<double> range, zero(samples, 0.0);
  259. for (int i = 0; i < samples; ++i)
  260. range.push_back( from_coord + (to_coord-from_coord)/static_cast<double>(samples) );
  261. std::vector<double> Xp, Yp, Zp;
  262. // X line
  263. Xp.insert(Xp.end(), range.begin(), range.end());
  264. Yp.insert(Yp.end(), zero.begin(), zero.end());
  265. Zp.insert(Zp.end(), zero.begin(), zero.end());
  266. // Y line
  267. Xp.insert(Xp.end(), zero.begin(), zero.end());
  268. Yp.insert(Yp.end(), range.begin(), range.end());
  269. Zp.insert(Zp.end(), zero.begin(), zero.end());
  270. // Z line
  271. Xp.insert(Xp.end(), zero.begin(), zero.end());
  272. Yp.insert(Yp.end(), zero.begin(), zero.end());
  273. Zp.insert(Zp.end(), range.begin(), range.end());
  274. int ncoord = Xp.size();
  275. x = {1.1, 2.1};
  276. m = {std::complex<double>(0.05/1.6,1.070), std::complex<double>(0.05/1.46,2.070)};
  277. L = x.size();
  278. int pl = 0;
  279. int nmax = 0;
  280. std::vector<std::vector<std::complex<double> > > E(ncoord), H(ncoord);
  281. for (auto& f:E) f.resize(3);
  282. for (auto& f:H) f.resize(3);
  283. nmax = nField( L, pl, x, m, nmax, ncoord, Xp, Yp, Zp, E, H);
  284. double sum_e = 0.0, sum_h = 0.0;
  285. for (auto f:E)
  286. for (auto c:f) sum_e+=std::abs(c);
  287. for (auto f:H)
  288. for (auto c:f) sum_h+=std::abs(c);
  289. printf ("Field total sum (old) \n\tE =%23.16f\n\tH*377=%23.16f\n", sum_e, sum_h*377.0);
  290. nmie::nField( L, pl, x, m, nmax, ncoord, Xp, Yp, Zp, E, H);
  291. sum_e = 0.0, sum_h = 0.0;
  292. for (auto f:E)
  293. for (auto c:f) sum_e+=std::abs(c);
  294. for (auto f:H)
  295. for (auto c:f) sum_h+=std::abs(c);
  296. printf ("Field total sum (wrapper) \n\tE =%23.16f\n\tH*377=%23.16f\n", sum_e, sum_h*377.0);
  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. }