compare.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "nmie.h"
  38. #include "nmie-wrapper.h"
  39. timespec diff(timespec start, timespec end);
  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. std::vector<std::string> args;
  62. args.assign(argv, argv + argc);
  63. std::string error_msg(std::string("Insufficient parameters.\nUsage: ") + args[0]
  64. + " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] "
  65. + "[-t ti tf nt] [-c comment]\n");
  66. enum mode_states {read_L, read_x, read_mr, read_mi, read_ti, read_tf, read_nt, read_comment};
  67. // for (auto arg : args) std::cout<< arg <<std::endl;
  68. std::string comment;
  69. int has_comment = 0;
  70. int i, l, L = 0;
  71. std::vector<double> x, Theta;
  72. std::vector<std::complex<double> > m, S1, S2;
  73. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  74. std::vector<std::complex<double> > mw, S1w, S2w;
  75. double Qextw, Qabsw, Qscaw, Qbkw, Qprw, gw, Albedow;
  76. double ti = 0.0, tf = 90.0;
  77. int nt = 0;
  78. if (argc < 5) throw std::invalid_argument(error_msg);
  79. //strcpy(comment, "");
  80. // for (i = 1; i < argc; i++) {
  81. int mode = -1;
  82. double tmp_mr;
  83. for (auto arg : args) {
  84. // For each arg in args list we detect the change of the current
  85. // read mode or read the arg. The reading args algorithm works
  86. // as a finite-state machine.
  87. // Detecting new read mode (if it is a valid -key)
  88. if (arg == "-l") {
  89. mode = read_L;
  90. continue;
  91. }
  92. if (arg == "-t") {
  93. if ((mode != read_x) && (mode != read_comment))
  94. throw std::invalid_argument(std::string("Unfinished layer!\n")
  95. +error_msg);
  96. mode = read_ti;
  97. continue;
  98. }
  99. if (arg == "-c") {
  100. if ((mode != read_x) && (mode != read_nt))
  101. throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
  102. mode = read_comment;
  103. continue;
  104. }
  105. // Reading data. For invalid date the exception will be thrown
  106. // with the std:: and catched in the end.
  107. if (mode == read_L) {
  108. L = std::stoi(arg);
  109. mode = read_x;
  110. continue;
  111. }
  112. if (mode == read_x) {
  113. x.push_back(std::stod(arg));
  114. mode = read_mr;
  115. continue;
  116. }
  117. if (mode == read_mr) {
  118. tmp_mr = std::stod(arg);
  119. mode = read_mi;
  120. continue;
  121. }
  122. if (mode == read_mi) {
  123. m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  124. mode = read_x;
  125. continue;
  126. }
  127. // if (strcmp(argv[i], "-l") == 0) {
  128. // i++;
  129. // L = atoi(argv[i]);
  130. // x.resize(L);
  131. // m.resize(L);
  132. // if (argc < 3*(L + 1)) {
  133. // throw std::invalid_argument(error_msg);
  134. // } else {
  135. // for (l = 0; l < L; l++) {
  136. // i++;
  137. // x[l] = atof(argv[i]);
  138. // i++;
  139. // m[l] = std::complex<double>(atof(argv[i]), atof(argv[i + 1]));
  140. // i++;
  141. // }
  142. // }
  143. if (mode == read_ti) {
  144. ti = std::stod(arg);
  145. mode = read_tf;
  146. continue;
  147. }
  148. if (mode == read_tf) {
  149. tf = std::stod(arg);
  150. mode = read_nt;
  151. continue;
  152. }
  153. if (mode == read_nt) {
  154. nt = std::stoi(arg);
  155. Theta.resize(nt);
  156. S1.resize(nt);
  157. S2.resize(nt);
  158. S1w.resize(nt);
  159. S2w.resize(nt);
  160. continue;
  161. }
  162. //} else if (strcmp(argv[i], "-t") == 0) {
  163. // i++;
  164. // ti = atof(argv[i]);
  165. // i++;
  166. // tf = atof(argv[i]);
  167. // i++;
  168. // nt = atoi(argv[i]);
  169. // Theta.resize(nt);
  170. // S1.resize(nt);
  171. // S2.resize(nt);
  172. if (mode == read_comment) {
  173. comment = arg;
  174. has_comment = 1;
  175. continue;
  176. }
  177. // } else if (strcmp(argv[i], "-c") == 0) {
  178. // i++;
  179. // comment = args[i];
  180. // //strcpy(comment, argv[i]);
  181. // has_comment = 1;
  182. // } else { i++; }
  183. }
  184. if ( (x.size() != m.size()) || (L != x.size()) )
  185. throw std::invalid_argument(std::string("Broken structure!\n")
  186. +error_msg);
  187. if ( (0 == m.size()) || ( 0 == x.size()) )
  188. throw std::invalid_argument(std::string("Empty structure!\n")
  189. +error_msg);
  190. if (nt < 0) {
  191. printf("Error reading Theta.\n");
  192. return -1;
  193. } else if (nt == 1) {
  194. Theta[0] = ti*PI/180.0;
  195. } else {
  196. for (i = 0; i < nt; i++) {
  197. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  198. }
  199. }
  200. timespec time1, time2;
  201. long cpptime_nsec, best_cpp;
  202. long ctime_nsec, best_c;
  203. for (int i = 0; i<150000; ++i) {
  204. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  205. // nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  206. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  207. // //ctime_nsec = std::min(ctime_nsec,diff(time1,time2).tv_nsec);
  208. // ctime_nsec = diff(time1,time2).tv_nsec;
  209. // if (ctime_nsec < best_c) best_c = ctime_nsec;
  210. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  211. nmie::nMie_wrapper(L, x, m, nt, Theta, &Qextw, &Qscaw, &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  212. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  213. // //cpptime_nsec = std::min(cpptime_nsec, diff(time1,time2).tv_nsec);
  214. // cpptime_nsec = diff(time1,time2).tv_nsec;
  215. // if (cpptime_nsec < best_cpp) best_cpp = cpptime_nsec;
  216. //printf("-- C++ time consumed %ld sec : %ld nsec\n",diff(time1,time2).tv_sec,cpptime_nsec);
  217. //printf("-- C/C++ time ratio: %Lg\n", static_cast<long double>(ctime_nsec)/static_cast<long double>(cpptime_nsec));
  218. //printf("--best C/C++ time ratio: %Lg\n", static_cast<long double>(best_c)/static_cast<long double>(best_cpp));
  219. }
  220. //printf("--best C/C++ time ratio: %Lg\n", static_cast<long double>(best_c)/static_cast<long double>(best_cpp));
  221. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  222. nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  223. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  224. // ctime_nsec = diff(time1,time2).tv_nsec;
  225. // printf("-- C time consumed %ld sec : %ld nsec\n",diff(time1,time2).tv_sec, ctime_nsec);
  226. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  227. // nmie::nMie_wrapper(L, x, m, nt, Theta, &Qextw, &Qscaw, &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  228. // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  229. // cpptime_nsec = diff(time1,time2).tv_nsec;
  230. // printf("-- C++ time consumed %ld sec : %ld nsec\n",diff(time1,time2).tv_sec,cpptime_nsec);
  231. // printf("-- C/C++ time ratio: %Lg\n", static_cast<long double>(ctime_nsec)/static_cast<long double>(cpptime_nsec));
  232. if (has_comment) {
  233. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  234. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e wrapper\n", comment.c_str(), Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  235. } else {
  236. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  237. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e wrapper\n", Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  238. }
  239. if (nt > 0) {
  240. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  241. for (i = 0; i < nt; i++) {
  242. printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e\n", Theta[i]*180.0/PI, S1[i].real(), S1[i].imag(), S2[i].real(), S2[i].imag());
  243. 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());
  244. }
  245. }
  246. } catch( const std::invalid_argument& ia ) {
  247. // Will catch if multi_layer_mie fails or other errors.
  248. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. timespec diff(timespec start, timespec end)
  254. {
  255. timespec temp;
  256. if ((end.tv_nsec-start.tv_nsec)<0) {
  257. temp.tv_sec = end.tv_sec-start.tv_sec-1;
  258. temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  259. } else {
  260. temp.tv_sec = end.tv_sec-start.tv_sec;
  261. temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  262. }
  263. return temp;
  264. }