standalone.cc 8.7 KB

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