standalone.cc 9.4 KB

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