speed-test.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2016 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2016 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. //sudo aptitude install libgoogle-perftools-dev
  39. //#include <google/heap-profiler.h>
  40. #include "../../src/nmie.hpp"
  41. //#include "../../src/nmie-precision.hpp"
  42. //#include "../../src/nmie-impl.hpp"
  43. timespec diff(timespec start, timespec end);
  44. const double PI=3.14159265358979323846;
  45. template<class T> inline T pow2(const T value) {return value*value;}
  46. //***********************************************************************************//
  47. // This is the main function of 'scattnlay', here we read the parameters as //
  48. // arguments passed to the program which should be executed with the following //
  49. // syntaxis: //
  50. // ./scattnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment] //
  51. // //
  52. // When all the parameters were correctly passed we setup the integer L (the //
  53. // number of layers) and the arrays x and m, containing the size parameters and //
  54. // refractive indexes of the layers, respectively and call the function nMie. //
  55. // If the calculation is successful the results are printed with the following //
  56. // format: //
  57. // //
  58. // * If no comment was passed: //
  59. // 'Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  60. // //
  61. // * If a comment was passed: //
  62. // 'comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  63. //***********************************************************************************//
  64. int main(int argc, char *argv[]) {
  65. try {
  66. std::vector<std::string> args;
  67. args.assign(argv, argv + argc);
  68. std::string error_msg(std::string("Insufficient parameters.\nUsage: ") + args[0]
  69. + " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] "
  70. + "[-t ti tf nt] [-c comment]\n");
  71. enum mode_states {read_L, read_x, read_mr, read_mi, read_ti, read_tf, read_nt, read_comment};
  72. // for (auto arg : args) std::cout<< arg <<std::endl;
  73. std::string comment;
  74. int has_comment = 0;
  75. int i, l, L = 0;
  76. std::vector<double> x, Theta;
  77. std::vector<std::complex<double> > m, S1, S2;
  78. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  79. std::vector<std::complex<double> > mw, S1w, S2w;
  80. double Qextw, Qabsw, Qscaw, Qbkw, Qprw, gw, Albedow;
  81. double ti = 0.0, tf = 90.0;
  82. int nt = 0;
  83. if (argc < 5) throw std::invalid_argument(error_msg);
  84. //strcpy(comment, "");
  85. // for (i = 1; i < argc; i++) {
  86. int mode = -1;
  87. double tmp_mr;
  88. for (auto arg : args) {
  89. // For each arg in args list we detect the change of the current
  90. // read mode or read the arg. The reading args algorithm works
  91. // as a finite-state machine.
  92. // Detecting new read mode (if it is a valid -key)
  93. if (arg == "-l") {
  94. mode = read_L;
  95. continue;
  96. }
  97. if (arg == "-t") {
  98. if ((mode != read_x) && (mode != read_comment))
  99. throw std::invalid_argument(std::string("Unfinished layer!\n")
  100. +error_msg);
  101. mode = read_ti;
  102. continue;
  103. }
  104. if (arg == "-c") {
  105. if ((mode != read_x) && (mode != read_nt))
  106. throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
  107. mode = read_comment;
  108. continue;
  109. }
  110. // Reading data. For invalid date the exception will be thrown
  111. // with the std:: and catched in the end.
  112. if (mode == read_L) {
  113. L = std::stoi(arg);
  114. mode = read_x;
  115. continue;
  116. }
  117. if (mode == read_x) {
  118. x.push_back(std::stod(arg));
  119. mode = read_mr;
  120. continue;
  121. }
  122. if (mode == read_mr) {
  123. tmp_mr = std::stod(arg);
  124. mode = read_mi;
  125. continue;
  126. }
  127. if (mode == read_mi) {
  128. m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  129. mode = read_x;
  130. continue;
  131. }
  132. if (mode == read_ti) {
  133. ti = std::stod(arg);
  134. mode = read_tf;
  135. continue;
  136. }
  137. if (mode == read_tf) {
  138. tf = std::stod(arg);
  139. mode = read_nt;
  140. continue;
  141. }
  142. if (mode == read_nt) {
  143. nt = std::stoi(arg);
  144. Theta.resize(nt);
  145. S1.resize(nt);
  146. S2.resize(nt);
  147. S1w.resize(nt);
  148. S2w.resize(nt);
  149. continue;
  150. }
  151. if (mode == read_comment) {
  152. comment = arg;
  153. has_comment = 1;
  154. continue;
  155. }
  156. }
  157. if ( (x.size() != m.size()) || (L != x.size()) )
  158. throw std::invalid_argument(std::string("Broken structure!\n")
  159. +error_msg);
  160. if ( (0 == m.size()) || ( 0 == x.size()) )
  161. throw std::invalid_argument(std::string("Empty structure!\n")
  162. +error_msg);
  163. if (nt < 0) {
  164. printf("Error reading Theta.\n");
  165. return -1;
  166. } else if (nt == 1) {
  167. Theta[0] = ti*PI/180.0;
  168. } else {
  169. for (i = 0; i < nt; i++) {
  170. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  171. }
  172. }
  173. timespec time1, time2;
  174. long cpptime_nsec, best_cpp;
  175. long ctime_nsec, best_c;
  176. long cpptime_sec, ctime_sec;
  177. long repeats = 150;
  178. //HeapProfilerStart("heapprof");
  179. printf("Start\n");
  180. do {
  181. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
  182. for (int i = 0; i<repeats; ++i) {
  183. nmie::nMie(L, x, m, nt, Theta, &Qextw, &Qscaw,
  184. &Qabsw, &Qbkw, &Qprw, &gw, &Albedow, S1w, S2w);
  185. // break;
  186. }
  187. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
  188. cpptime_nsec = diff(time1,time2).tv_nsec;
  189. cpptime_sec = diff(time1,time2).tv_sec;
  190. printf("-- C++ time consumed %lg sec\n", cpptime_sec+(cpptime_nsec/1e9));
  191. repeats *= 10;
  192. // break;
  193. } while (cpptime_sec < 3 && ctime_sec < 3);
  194. printf("Finish\n");
  195. // if (has_comment) {
  196. // printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e \n", comment.c_str(), Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  197. // } else {
  198. // printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e \n", Qextw, Qscaw, Qabsw, Qbkw, Qprw, gw, Albedow);
  199. // }
  200. // if (nt > 0) {
  201. // printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  202. // for (i = 0; i < nt; i++) {
  203. // 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());
  204. // }
  205. // }
  206. } catch( const std::invalid_argument& ia ) {
  207. // Will catch if multi_layer_mie fails or other errors.
  208. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  209. return -1;
  210. }
  211. return 0;
  212. }
  213. timespec diff(timespec start, timespec end)
  214. {
  215. timespec temp;
  216. if ((end.tv_nsec-start.tv_nsec)<0) {
  217. temp.tv_sec = end.tv_sec-start.tv_sec-1;
  218. temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  219. } else {
  220. temp.tv_sec = end.tv_sec-start.tv_sec;
  221. temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  222. }
  223. return temp;
  224. }