compare.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. template <class VectorType, int dimensions> inline
  45. std::vector<VectorType> CrossProduct(std::vector<VectorType> &a, std::vector<VectorType> &b) {
  46. if (a.size() != 3 || b.size() != 3) throw std::invalid_argument("Cross product only for 3D vectors!");
  47. std::vector<VectorType> r (3);
  48. r[0] = a[1]*b[2]-a[2]*b[1];
  49. r[1] = a[2]*b[0]-a[0]*b[2];
  50. r[2] = a[0]*b[1]-a[1]*b[0];
  51. return r;
  52. }
  53. //***********************************************************************************//
  54. // This is the main function of 'scattnlay', here we read the parameters as //
  55. // arguments passed to the program which should be executed with the following //
  56. // syntaxis: //
  57. // ./scattnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment] //
  58. // //
  59. // When all the parameters were correctly passed we setup the integer L (the //
  60. // number of layers) and the arrays x and m, containing the size parameters and //
  61. // refractive indexes of the layers, respectively and call the function nMie. //
  62. // If the calculation is successful the results are printed with the following //
  63. // format: //
  64. // //
  65. // * If no comment was passed: //
  66. // 'Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  67. // //
  68. // * If a comment was passed: //
  69. // 'comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  70. //***********************************************************************************//
  71. int main(int argc, char *argv[]) {
  72. try {
  73. std::vector<std::string> args;
  74. args.assign(argv, argv + argc);
  75. std::string error_msg(std::string("Insufficient parameters.\nUsage: ") + args[0]
  76. + " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] "
  77. + "[-t ti tf nt] [-c comment]\n");
  78. enum mode_states {read_L, read_x, read_mr, read_mi, read_ti, read_tf, read_nt, read_comment};
  79. // for (auto arg : args) std::cout<< arg <<std::endl;
  80. std::string comment;
  81. int has_comment = 0;
  82. int i, l, L = 0;
  83. std::vector<double> x, Theta;
  84. std::vector<std::complex<double> > m, S1, S2;
  85. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  86. std::vector<std::complex<double> > mw, S1w, S2w;
  87. double Qextw, Qabsw, Qscaw, Qbkw, Qprw, gw, Albedow;
  88. double ti = 0.0, tf = 90.0;
  89. int nt = 0;
  90. if (argc < 5) throw std::invalid_argument(error_msg);
  91. //strcpy(comment, "");
  92. // for (i = 1; i < argc; i++) {
  93. int mode = -1;
  94. double tmp_mr;
  95. for (auto arg : args) {
  96. // For each arg in args list we detect the change of the current
  97. // read mode or read the arg. The reading args algorithm works
  98. // as a finite-state machine.
  99. // Detecting new read mode (if it is a valid -key)
  100. if (arg == "-l") {
  101. mode = read_L;
  102. continue;
  103. }
  104. if (arg == "-t") {
  105. if ((mode != read_x) && (mode != read_comment))
  106. throw std::invalid_argument(std::string("Unfinished layer!\n")
  107. +error_msg);
  108. mode = read_ti;
  109. continue;
  110. }
  111. if (arg == "-c") {
  112. if ((mode != read_x) && (mode != read_nt))
  113. throw std::invalid_argument(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.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  136. mode = read_x;
  137. continue;
  138. }
  139. if (mode == read_ti) {
  140. ti = std::stod(arg);
  141. mode = read_tf;
  142. continue;
  143. }
  144. if (mode == read_tf) {
  145. tf = std::stod(arg);
  146. mode = read_nt;
  147. continue;
  148. }
  149. if (mode == read_nt) {
  150. nt = std::stoi(arg);
  151. Theta.resize(nt);
  152. S1.resize(nt);
  153. S2.resize(nt);
  154. S1w.resize(nt);
  155. S2w.resize(nt);
  156. continue;
  157. }
  158. if (mode == read_comment) {
  159. comment = arg;
  160. has_comment = 1;
  161. continue;
  162. }
  163. }
  164. if ( (x.size() != m.size()) || (L != x.size()) )
  165. throw std::invalid_argument(std::string("Broken structure!\n")
  166. +error_msg);
  167. if ( (0 == m.size()) || ( 0 == x.size()) )
  168. throw std::invalid_argument(std::string("Empty structure!\n")
  169. +error_msg);
  170. if (nt < 0) {
  171. printf("Error reading Theta.\n");
  172. return -1;
  173. } else if (nt == 1) {
  174. Theta[0] = ti*PI/180.0;
  175. } else {
  176. for (i = 0; i < nt; i++) {
  177. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  178. }
  179. }
  180. // timespec time1, time2;
  181. // long cpptime_nsec, best_cpp;
  182. // long ctime_nsec, best_c;
  183. // long cpptime_sec, ctime_sec;
  184. // long repeats = 150;
  185. // //HeapProfilerStart("heapprof");
  186. // do {
  187. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  188. // for (int i = 0; i<repeats; ++i) {
  189. // nmie::nMie(L, x, m, nt, Theta, &Qextw, &Qscaw,
  190. // &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  191. // }
  192. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  193. // cpptime_nsec = diff(time1,time2).tv_nsec;
  194. // cpptime_sec = diff(time1,time2).tv_sec;
  195. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  196. // // for (int i = 0; i<repeats; ++i) {
  197. // // nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  198. // // }
  199. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  200. // ctime_nsec = diff(time1,time2).tv_nsec;
  201. // ctime_sec = diff(time1,time2).tv_sec;
  202. // long double ratio = static_cast<long double>(ctime_nsec)
  203. // /static_cast<long double>(cpptime_nsec);
  204. // printf("-- C++ time consumed %lg sec\n", (cpptime_nsec/1e9));
  205. // if ( ratio > 0.01 ) {
  206. // if ( ctime_sec == 0 && cpptime_sec == 0) {
  207. // printf("-- C time consumed %lg sec\n", (ctime_nsec/1e9));
  208. // printf("-- total repeats: %ld\n", repeats);
  209. // printf("-- C/C++ time ratio: %Lg\n", ratio);
  210. // } else {
  211. // printf("==Test is too long!\n");
  212. // }
  213. // }
  214. // repeats *= 10;
  215. // } while (cpptime_nsec < 1e8 && ctime_nsec < 1e8);
  216. nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  217. nmie::nMie(L, x, m, nt, Theta, &Qextw, &Qscaw, &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  218. printf("\n");
  219. if (has_comment) {
  220. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e old\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  221. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e \n", comment.c_str(), Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  222. } else {
  223. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e old\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  224. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e \n", Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  225. }
  226. if (nt > 0) {
  227. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  228. for (i = 0; i < nt; i++) {
  229. 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());
  230. 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());
  231. }
  232. }
  233. // Field testing
  234. //double size=2.0*PI*1.0/6.0;
  235. double WL=354; //nm
  236. double core_r = WL/20.0;
  237. double r_x = 2.0*PI*core_r/WL;
  238. double size=r_x;
  239. double R = size/(2.0*PI);
  240. double from_coord = -3.0*size, to_coord = 3.0*size;
  241. std::vector<double> range;
  242. int samples = 1251;
  243. for (int i = 0; i < samples; ++i) {
  244. range.push_back( from_coord + (to_coord-from_coord)/(static_cast<double>(samples)-1)*i );
  245. //range.push_back(size*0.01);
  246. //range.push_back(size*0.99999);
  247. //range.push_back(R/2.0);
  248. //range.push_back(size*1.00001);
  249. //range.push_back(3);
  250. //printf("r=%g ", range.back());
  251. }
  252. // range.push_back(size*0.99999999);
  253. // range.push_back(R/2.0);
  254. // range.push_back(size*1.00000001);
  255. //printf("r/2 = %g\n", R/2.0);
  256. //int samples = range.size();
  257. std::vector<double> zero(samples, 0.0);
  258. std::vector<double> Xp, Yp, Zp;
  259. // // X line
  260. // Xp.insert(Xp.end(), range.begin(), range.end());
  261. // Yp.insert(Yp.end(), zero.begin(), zero.end());
  262. // Zp.insert(Zp.end(), zero.begin(), zero.end());
  263. // //Y line
  264. // Xp.insert(Xp.end(), zero.begin(), zero.end());
  265. // Yp.insert(Yp.end(), range.begin(), range.end());
  266. // Zp.insert(Zp.end(), zero.begin(), zero.end());
  267. // Z line
  268. Xp.insert(Xp.end(), zero.begin(), zero.end());
  269. Yp.insert(Yp.end(), zero.begin(), zero.end());
  270. Zp.insert(Zp.end(), range.begin(), range.end());
  271. int ncoord = Xp.size();
  272. // Test solid sphere
  273. x = {size};
  274. std::complex<double> epsilon_Ag(-2.0, 0.28);
  275. m = {std::sqrt(epsilon_Ag)};
  276. //m = {std::complex<double>(2.000000,0.00)};
  277. //m = {std::complex<double>(1.414213562, 0.00)};
  278. L = x.size();
  279. int pl = 0;
  280. int nmax = 0;
  281. std::vector<std::vector<std::complex<double> > > E(ncoord), H(ncoord);
  282. for (auto &f:E) f.resize(3);
  283. for (auto &f:H) f.resize(3);
  284. double free_impedance = 376.73031;
  285. //double free_impedance = 1.0;
  286. nmie::nField( L, pl, x, m, nmax, ncoord, Xp, Yp, Zp, E, H);
  287. double sum_e = 0.0, sum_h = 0.0;
  288. printf ("Field total sum ()\n");
  289. double min_E, max_E;
  290. for (auto c:E[0]) {
  291. sum_e+=std::abs(pow2(c));
  292. }
  293. min_E = sum_e;
  294. max_E = sum_e;
  295. for (auto f:E) {
  296. sum_e = 0.0;
  297. for (auto c:f) {
  298. sum_e+=std::abs(pow2(c));
  299. //printf("component: %g + %g i\n", std::real(c), std::imag(c));
  300. }
  301. if (sum_e > max_E) max_E = sum_e;
  302. if (sum_e < min_E) min_E = sum_e;
  303. //printf("Field E=%g\n", std::sqrt(std::abs(sum_e)));
  304. }
  305. printf("Min E = %g; max E =%g", min_E, max_E);
  306. // for (auto f:H) {
  307. // sum_h = 0.0;
  308. // for (auto c:f) sum_h+=std::abs(pow2(c));
  309. // printf("Field H=%g\n", std::sqrt(std::abs(sum_h))*free_impedance);
  310. // }
  311. } catch( const std::invalid_argument &ia ) {
  312. // Will catch if multi_layer_mie fails or other errors.
  313. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. timespec diff(timespec start, timespec end)
  319. {
  320. timespec temp;
  321. if ((end.tv_nsec-start.tv_nsec)<0) {
  322. temp.tv_sec = end.tv_sec-start.tv_sec-1;
  323. temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  324. } else {
  325. temp.tv_sec = end.tv_sec-start.tv_sec;
  326. temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  327. }
  328. return temp;
  329. }