speed-test.cc 8.3 KB

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