standalone.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <stdlib.h>
  27. #include <stdio.h>
  28. #include <time.h>
  29. #include <string.h>
  30. #include "ucomplex.h"
  31. #include "nmie.h"
  32. #define MAXLAYERS 1100
  33. #define MAXTHETA 800
  34. #define PI 3.14159
  35. //***********************************************************************************//
  36. // This is the main function of 'scattnlay', here we read the parameters as //
  37. // arguments passed to the program which should be executed with the following //
  38. // syntaxis: //
  39. // ./scattnlay -l Layers x1 m1.r m1.i [x2 m2.r m2.i ...] [-t ti tf nt] [-c comment] //
  40. // //
  41. // When all the parameters were correctly passed we setup the integer L (the //
  42. // number of layers) and the arrays x and m, containing the size parameters and //
  43. // refractive indexes of the layers, respectively and call the function nMie. //
  44. // If the calculation is successful the results are printed with the following //
  45. // format: //
  46. // //
  47. // * If no comment was passed: //
  48. // 'Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  49. // //
  50. // * If a comment was passed: //
  51. // 'comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo' //
  52. //***********************************************************************************//
  53. int main(int argc, char *argv[]) {
  54. char comment[200];
  55. int has_comment = 0;
  56. int i, j, L;
  57. double *x, *Theta;
  58. complex *m, *S1, *S2;
  59. double Qext, Qabs, Qsca, Qbk, Qpr, g, Albedo;
  60. double ti = 0.0, tf = 90.0;
  61. int nt = 0;
  62. if (argc < 5) {
  63. 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]);
  64. return -1;
  65. }
  66. strcpy(comment, "");
  67. for (i = 1; i < argc; i++) {
  68. if (strcmp(argv[i], "-l") == 0) {
  69. i++;
  70. L = atoi(argv[i]);
  71. x = (double *) malloc(L*sizeof(double));
  72. m = (complex *) malloc(L*sizeof(complex));
  73. if (argc < 3*(L + 1)) {
  74. 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]);
  75. return -1;
  76. } else {
  77. for (j = 0; j < L; j++) {
  78. i++;
  79. x[j] = atof(argv[i]);
  80. i++;
  81. m[j].r = atof(argv[i]);
  82. i++;
  83. m[j].i = atof(argv[i]);
  84. }
  85. }
  86. } else if (strcmp(argv[i], "-t") == 0) {
  87. i++;
  88. ti = atof(argv[i]);
  89. i++;
  90. tf = atof(argv[i]);
  91. i++;
  92. nt = atoi(argv[i]);
  93. Theta = (double *) malloc(nt*sizeof(double));
  94. S1 = (complex *) malloc(nt*sizeof(complex));
  95. S2 = (complex *) malloc(nt*sizeof(complex));
  96. } else if (strcmp(argv[i], "-c") == 0) {
  97. i++;
  98. strcpy(comment, argv[i]);
  99. has_comment = 1;
  100. } else { i++; }
  101. }
  102. if (nt < 0) {
  103. printf("Error reading Theta.\n");
  104. return -1;
  105. } else if (nt == 1) {
  106. Theta[0] = ti*PI/180.0;
  107. } else {
  108. for (i = 0; i < nt; i++) {
  109. Theta[i] = (ti + (double)i*(tf - ti)/(nt - 1))*PI/180.0;
  110. }
  111. }
  112. nMie(L, x, m, nt, Theta, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, S1, S2);
  113. if (has_comment) {
  114. printf("%6s, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", comment, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  115. } else {
  116. printf("%+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e, %+.5e\n", Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo);
  117. }
  118. if (nt > 0) {
  119. printf(" Theta, S1.r, S1.i, S2.r, S2.i\n");
  120. for (i = 0; i < nt; i++) {
  121. 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);
  122. }
  123. }
  124. return 0;
  125. }