farfield.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2015 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2015 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. #include "nmie.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. unsigned int 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. int mode = -1;
  77. double tmp_mr;
  78. for (auto arg : args) {
  79. // For each arg in args list we detect the change of the current
  80. // read mode or read the arg. The reading args algorithm works
  81. // as a finite-state machine.
  82. // Detecting new read mode (if it is a valid -key)
  83. if (arg == "-l") {
  84. mode = read_L;
  85. continue;
  86. }
  87. if (arg == "-t") {
  88. if ((mode != read_x) && (mode != read_comment))
  89. throw std::invalid_argument(std::string("Unfinished layer!\n") + error_msg);
  90. mode = read_ti;
  91. continue;
  92. }
  93. if (arg == "-c") {
  94. if ((mode != read_x) && (mode != read_nt))
  95. throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
  96. mode = read_comment;
  97. continue;
  98. }
  99. // Reading data. For invalid date the exception will be thrown
  100. // with the std:: and catched in the end.
  101. if (mode == read_L) {
  102. L = std::stoi(arg);
  103. mode = read_x;
  104. continue;
  105. }
  106. if (mode == read_x) {
  107. x.push_back(std::stod(arg));
  108. mode = read_mr;
  109. continue;
  110. }
  111. if (mode == read_mr) {
  112. tmp_mr = std::stod(arg);
  113. mode = read_mi;
  114. continue;
  115. }
  116. if (mode == read_mi) {
  117. m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  118. mode = read_x;
  119. continue;
  120. }
  121. if (mode == read_ti) {
  122. ti = std::stod(arg);
  123. mode = read_tf;
  124. continue;
  125. }
  126. if (mode == read_tf) {
  127. tf = std::stod(arg);
  128. mode = read_nt;
  129. continue;
  130. }
  131. if (mode == read_nt) {
  132. nt = std::stoi(arg);
  133. Theta.resize(nt);
  134. S1.resize(nt);
  135. S2.resize(nt);
  136. continue;
  137. }
  138. if (mode == read_comment) {
  139. comment = arg;
  140. has_comment = 1;
  141. continue;
  142. }
  143. }
  144. if ( (x.size() != m.size()) || (L != x.size()) )
  145. throw std::invalid_argument(std::string("Broken structure!\n") + error_msg);
  146. if ( (0 == m.size()) || ( 0 == x.size()) )
  147. throw std::invalid_argument(std::string("Empty structure!\n") + error_msg);
  148. if (nt < 0) {
  149. printf("Error reading Theta.\n");
  150. return -1;
  151. } else if (nt == 1) {
  152. Theta[0] = ti*PI/180.0;
  153. } else {
  154. for (int i = 0; i < nt; i++) {
  155. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  156. }
  157. }
  158. nmie::nMie(L, -1, x, m, nt, Theta, -1, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  159. if (has_comment) {
  160. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  161. } else {
  162. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  163. }
  164. if (nt > 0) {
  165. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  166. for (int i = 0; i < nt; i++) {
  167. 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());
  168. }
  169. }
  170. } catch( const std::invalid_argument& ia ) {
  171. // Will catch if multi_layer_mie fails or other errors.
  172. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  173. return -1;
  174. }
  175. return 0;
  176. }