standalone.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "ucomplex.h"
  38. #include "nmie.h"
  39. #include "nmie-wrapper.h"
  40. //#define MAXLAYERS 1100
  41. const int MAXLAYERS = 1100;
  42. //#define MAXTHETA 800
  43. const int MAXTHETA = 800;
  44. //#define PI 3.14159
  45. const double PI=3.14159265358979323846;
  46. //***********************************************************************************//
  47. // This is the main function of 'scattnlay', here we read the parameters as //
  48. // arguments passed to the program which should be executed with the following //
  49. // syntaxis: //
  50. // ./scattnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment] //
  51. // //
  52. // When all the parameters were correctly passed we setup the integer L (the //
  53. // number of layers) and the arrays x and m, containing the size parameters and //
  54. // refractive indexes of the layers, respectively and call the function nMie. //
  55. // If the calculation is successful the results are printed with the following //
  56. // format: //
  57. // //
  58. // * If no comment was passed: //
  59. // 'Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  60. // //
  61. // * If a comment was passed: //
  62. // 'comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  63. //***********************************************************************************//
  64. int main(int argc, char *argv[]) {
  65. try {
  66. char comment[200];
  67. std::string comment_std;
  68. int has_comment = 0;
  69. int i, j, L;
  70. double *x, *Theta;
  71. std::vector<double> x_std, Theta_std;
  72. complex *m, *S1, *S2;
  73. std::vector<std::complex<double> > m_std, S1_std, S2_std;
  74. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  75. double ti = 0.0, tf = 90.0;
  76. int nt = 0;
  77. if (argc < 5) {
  78. printf("Insufficient parameters.\n");
  79. printf("Usage: %s -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment]\n", argv[0]);
  80. return -1;
  81. }
  82. strcpy(comment, "");
  83. for (i = 1; i < argc; i++) {
  84. if (strcmp(argv[i], "-l") == 0) {
  85. i++;
  86. L = atoi(argv[i]);
  87. x = (double *) malloc(L*sizeof(double));
  88. x_std.resize(L);
  89. m = (complex *) malloc(L*sizeof(complex));
  90. m_std.resize(L);
  91. if (argc < 3*(L + 1)) {
  92. printf("Insufficient parameters.\nUsage: %s -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment]\n", argv[0]);
  93. return -1;
  94. } else {
  95. for (j = 0; j < L; j++) {
  96. i++;
  97. x[j] = atof(argv[i]);
  98. x_std[j] = x[j];
  99. i++;
  100. m[j].r = atof(argv[i]);
  101. i++;
  102. m[j].i = atof(argv[i]);
  103. m_std[j] = std::complex<double>(m[j].r, m[j].i);
  104. }
  105. }
  106. } else if (strcmp(argv[i], "-t") == 0) {
  107. i++;
  108. ti = atof(argv[i]);
  109. i++;
  110. tf = atof(argv[i]);
  111. i++;
  112. nt = atoi(argv[i]);
  113. Theta = (double *) malloc(nt*sizeof(double));
  114. Theta_std.resize(nt);
  115. S1 = (complex *) malloc(nt*sizeof(complex));
  116. S2 = (complex *) malloc(nt*sizeof(complex));
  117. S1_std.resize(nt);
  118. S2_std.resize(nt);
  119. } else if (strcmp(argv[i], "-c") == 0) {
  120. i++;
  121. strcpy(comment, argv[i]);
  122. comment_std = std::string(comment);
  123. has_comment = 1;
  124. } else { i++; }
  125. }
  126. if (nt < 0) {
  127. printf("Error reading Theta.\n");
  128. return -1;
  129. } else if (nt == 1) {
  130. Theta[0] = ti*PI/180.0;
  131. Theta_std[0] = Theta[0];
  132. } else {
  133. for (i = 0; i < nt; i++) {
  134. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  135. Theta_std[i] = Theta[i];
  136. }
  137. }
  138. // printf("Debug run!\n");
  139. nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  140. std::vector<double> old_result({Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo});
  141. // if (has_comment) {
  142. // printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  143. // } else {
  144. // printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  145. // }
  146. // if (nt > 0) {
  147. // printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  148. // for (i = 0; i < nt; i++) {
  149. // printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e\n", Theta[i]*180.0/PI, S1[i].r, S1[i].i, S2[i].r, S2[i].i);
  150. // }
  151. // }
  152. /////////////////////////////////////////////////////////////////////////
  153. // After conversion
  154. nMie_std( x, m, Theta, S1, S2,
  155. L, x_std, m_std, nt, Theta_std, &Qext, &Qsca, &Qabs,
  156. &Qbk, &Qpr, &g, &Albedo, S1_std, S2_std);
  157. std::vector<double> new_result({Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo});
  158. if (has_comment) {
  159. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  160. } else {
  161. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  162. }
  163. if (nt > 0) {
  164. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  165. for (i = 0; i < nt; i++) {
  166. printf("%6.2f, %+.5e, %+.5e, %+.5e, %+.5e\n", Theta[i]*180.0/PI, S1[i].r, S1[i].i, S2[i].r, S2[i].i);
  167. }
  168. }
  169. for (int i =0; i < old_result.size(); ++i) {
  170. double diff = std::abs((new_result[i] - old_result[i])/new_result[i]);
  171. // printf("%g ", diff);
  172. if (std::abs(diff) > 1e-16) printf(" ********* WARNING!!! Final diff = %g ********* \n", diff);
  173. }
  174. // std::vector<double> diff_result(old_result.size(), 0.0);
  175. // std::transform(new_result.begin(), new_result.end(), old_result.begin(),
  176. // std::back_inserter(diff_result), std::minus<double>());
  177. // std::cout << "diff: " << diff_result << std::endl;
  178. } catch( const std::invalid_argument& ia ) {
  179. // Will catch if multi_layer_mie fails or other errors.
  180. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  181. return -1;
  182. }
  183. return 0;
  184. }