farfield.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2018 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2018 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 at least one of the following references: //
  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. // [2] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  24. // calculation of electromagnetic near-field for a multilayered //
  25. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  26. // pp. 225-230. //
  27. // //
  28. // You should have received a copy of the GNU General Public License //
  29. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  30. //**********************************************************************************//
  31. #include <algorithm>
  32. #include <complex>
  33. #include <functional>
  34. #include <iostream>
  35. #include <stdexcept>
  36. #include <string>
  37. #include <vector>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <time.h>
  41. #include <string.h>
  42. #include "nmie.hpp"
  43. const double PI=3.14159265358979323846;
  44. //***********************************************************************************//
  45. // This is the main function of 'scattnlay', here we read the parameters as //
  46. // arguments passed to the program which should be executed with the following //
  47. // syntaxis: //
  48. // ./scattnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment] //
  49. // //
  50. // When all the parameters were correctly passed we setup the integer L (the //
  51. // number of layers) and the arrays x and m, containing the size parameters and //
  52. // refractive indexes of the layers, respectively and call the function nMie. //
  53. // If the calculation is successful the results are printed with the following //
  54. // format: //
  55. // //
  56. // * If no comment was passed: //
  57. // 'Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  58. // //
  59. // * If a comment was passed: //
  60. // 'comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  61. //***********************************************************************************//
  62. int main(int argc, char *argv[]) {
  63. try {
  64. std::vector<std::string> args;
  65. args.assign(argv, argv + argc);
  66. std::string error_msg(std::string("Insufficient parameters.\nUsage: ") + args[0]
  67. + " -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] "
  68. + "[-t ti tf nt] [-c comment]\n");
  69. enum mode_states {read_L, read_x, read_mr, read_mi, read_ti, read_tf, read_nt, read_comment};
  70. // for (auto arg : args) std::cout<< arg <<std::endl;
  71. std::string comment;
  72. int has_comment = 0;
  73. unsigned int L = 0;
  74. std::vector<double> x, Theta;
  75. std::vector<std::complex<double> > m, S1, S2;
  76. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  77. double ti = 0.0, tf = 90.0;
  78. int nt = 0;
  79. if (argc < 5) throw std::invalid_argument(error_msg);
  80. int mode = -1;
  81. double tmp_mr;
  82. for (auto arg : args) {
  83. // For each arg in args list we detect the change of the current
  84. // read mode or read the arg. The reading args algorithm works
  85. // as a finite-state machine.
  86. // Detecting new read mode (if it is a valid -key)
  87. if (arg == "-l") {
  88. mode = read_L;
  89. continue;
  90. }
  91. if (arg == "-t") {
  92. if ((mode != read_x) && (mode != read_comment))
  93. throw std::invalid_argument(std::string("Unfinished layer!\n") + error_msg);
  94. mode = read_ti;
  95. continue;
  96. }
  97. if (arg == "-c") {
  98. if ((mode != read_x) && (mode != read_nt))
  99. throw std::invalid_argument(std::string("Unfinished layer or theta!\n") + error_msg);
  100. mode = read_comment;
  101. continue;
  102. }
  103. // Reading data. For invalid date the exception will be thrown
  104. // with the std:: and catched in the end.
  105. if (mode == read_L) {
  106. L = std::stoi(arg);
  107. mode = read_x;
  108. continue;
  109. }
  110. if (mode == read_x) {
  111. x.push_back(std::stod(arg));
  112. mode = read_mr;
  113. continue;
  114. }
  115. if (mode == read_mr) {
  116. tmp_mr = std::stod(arg);
  117. mode = read_mi;
  118. continue;
  119. }
  120. if (mode == read_mi) {
  121. m.push_back(std::complex<double>( tmp_mr,std::stod(arg) ));
  122. mode = read_x;
  123. continue;
  124. }
  125. if (mode == read_ti) {
  126. ti = std::stod(arg);
  127. mode = read_tf;
  128. continue;
  129. }
  130. if (mode == read_tf) {
  131. tf = std::stod(arg);
  132. mode = read_nt;
  133. continue;
  134. }
  135. if (mode == read_nt) {
  136. nt = std::stoi(arg);
  137. Theta.resize(nt);
  138. S1.resize(nt);
  139. S2.resize(nt);
  140. continue;
  141. }
  142. if (mode == read_comment) {
  143. comment = arg;
  144. has_comment = 1;
  145. continue;
  146. }
  147. }
  148. if ( (x.size() != m.size()) || (L != x.size()) )
  149. throw std::invalid_argument(std::string("Broken structure!\n") + error_msg);
  150. if ( (0 == m.size()) || ( 0 == x.size()) )
  151. throw std::invalid_argument(std::string("Empty structure!\n") + error_msg);
  152. if (nt < 0) {
  153. printf("Error reading Theta.\n");
  154. return -1;
  155. } else if (nt == 1) {
  156. Theta[0] = ti*PI/180.0;
  157. } else {
  158. for (int i = 0; i < nt; i++) {
  159. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  160. }
  161. }
  162. nmie::nMie(L, -1, x, m, nt, Theta, -1, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  163. if (has_comment) {
  164. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", comment.c_str(), Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  165. } else {
  166. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  167. }
  168. if (nt > 0) {
  169. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  170. for (int i = 0; i < nt; i++) {
  171. 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());
  172. }
  173. }
  174. } catch( const std::invalid_argument& ia ) {
  175. // Will catch if multi_layer_mie fails or other errors.
  176. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  177. return -1;
  178. }
  179. return 0;
  180. }