speed-test.cc 9.2 KB

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