example-get-Mie.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // This program returns expansion coefficents of Mie series
  28. #include "../src/nmie.hpp"
  29. #include "../src/nmie.hpp"
  30. #include "../src/nmie-precision.hpp"
  31. #include <complex>
  32. #include <cstdio>
  33. #include <string>
  34. #include "../src/nmie-applied.hpp"
  35. #include "../src/nmie-applied-impl.hpp"
  36. #include "../src/nmie-precision.hpp"
  37. #include "./read-spectra.h"
  38. // template<class T> inline T pow2(const T value) {return value*value;}
  39. int main(int argc, char *argv[]) {
  40. using namespace nmie ;
  41. try {
  42. //read_spectra::ReadSpectra Si_index;
  43. //read_spectra::ReadSpectra plot_core_index_, plot_TiN_;
  44. // std::string core_filename("Si-int.txt");
  45. //std::string core_filename("Ag.txt");
  46. //std::string TiN_filename("TiN.txt");
  47. //std::string TiN_filename("Ag-int.txt");
  48. //std::string TiN_filename("Si.txt");
  49. //std::string shell_filename(core_filename);
  50. nmie::MultiLayerMieApplied<nmie::FloatType> multi_layer_mie;
  51. const std::complex<double> epsilon_Si(16, 0);
  52. //const std::complex<double> epsilon_Ag(-8.5014154589, 0.7585845411);
  53. const std::complex<double> index_Si = std::sqrt(epsilon_Si);
  54. //const std::complex<double> index_Ag = std::sqrt(epsilon_Ag);
  55. double WL=550; //nm
  56. //double core_width = 102; //nm Si // radius
  57. double core_width = 196/2.0; //nm Si // radius
  58. //double inner_width = 8.22; //nm Ag
  59. //double outer_width = 67.91; //nm Si
  60. //bool isSiAgSi = true;
  61. //double delta_width = 25.0;
  62. // //bool isSiAgSi = false;
  63. // if (isSiAgSi) {
  64. // core_width = 5.27; //nm Si
  65. // inner_width = 8.22; //nm Ag
  66. // outer_width = 67.91; //nm Si
  67. multi_layer_mie.AddTargetLayer(core_width, index_Si);
  68. // multi_layer_mie.AddTargetLayer(inner_width, index_Ag);
  69. // multi_layer_mie.AddTargetLayer(outer_width+delta_width, index_Si);
  70. // } else {
  71. // inner_width = 31.93; //nm Ag
  72. // outer_width = 4.06; //nm Si
  73. // multi_layer_mie.AddTargetLayer(inner_width, index_Ag);
  74. // multi_layer_mie.AddTargetLayer(outer_width+delta_width, index_Si);
  75. // }
  76. // for (int dd = 0; dd<50; ++dd) {
  77. // delta_width = dd;
  78. // FILE *fp;
  79. // std::string fname = "absorb-layered-spectra-d"+std::to_string(dd)+".dat";
  80. // fp = fopen(fname.c_str(), "w");
  81. multi_layer_mie.SetWavelength(WL);
  82. multi_layer_mie.RunMieCalculation();
  83. double Qsca = static_cast<double>(multi_layer_mie.GetQsca());
  84. printf("Qsca = %g\n", Qsca);//*3.14159*core_width*core_width*1e-6);
  85. std::vector< std::vector<std::complex<nmie::FloatType> > > aln, bln, cln, dln;
  86. multi_layer_mie.GetExpanCoeffs(aln, bln, cln, dln);
  87. // nmie::ConvertComplexVectorVector<double>(aln);
  88. // std::string str = std::string("#WL ");
  89. // for (int l = 0; l<d_aln.size(); ++l) {
  90. // for (int n = 0; n<3; ++n) {
  91. // str+="|a|^2+|d|^2_ln"+std::to_string(l)+std::to_string(n)+" "
  92. // + "|b|^2+|c|^2_ln"+std::to_string(l)+std::to_string(n)+" ";
  93. // }
  94. // // }
  95. // str+="\n";
  96. // fprintf(fp, "%s", str.c_str());
  97. // fprintf(fp, "# |a|+|d|");
  98. // str.clear();
  99. // double from_WL = 400;
  100. // double to_WL = 600;
  101. // int total_points = 401;
  102. // double delta_WL = std::abs(to_WL - from_WL)/(total_points-1.0);
  103. // Si_index.ReadFromFile(core_filename).ResizeToComplex(from_WL, to_WL, total_points)
  104. // .ToIndex();
  105. // Ag_index.ReadFromFile(TiN_filename).ResizeToComplex(from_WL, to_WL, total_points)
  106. // .ToIndex();
  107. // auto Si_data = Si_index.GetIndex();
  108. // auto Ag_data = Ag_index.GetIndex();
  109. // for (int i=0; i < Si_data.size(); ++i) {
  110. // const double& WL = Si_data[i].first;
  111. // const std::complex<double>& Si = Si_data[i].second;
  112. // const std::complex<double>& Ag = Ag_data[i].second;
  113. // str+=std::to_string(WL);
  114. // multi_layer_mie.ClearTarget();
  115. // if (isSiAgSi) {
  116. // multi_layer_mie.AddTargetLayer(core_width, Si);
  117. // multi_layer_mie.AddTargetLayer(inner_width, Ag);
  118. // multi_layer_mie.AddTargetLayer(outer_width+delta_width, Si);
  119. // } else {
  120. // inner_width = 31.93; //nm Ag
  121. // outer_width = 4.06; //nm Si
  122. // multi_layer_mie.AddTargetLayer(inner_width, Ag);
  123. // multi_layer_mie.AddTargetLayer(outer_width+delta_width, Si);
  124. // }
  125. // multi_layer_mie.SetWavelength(WL);
  126. // multi_layer_mie.RunMieCalculation();
  127. // multi_layer_mie.GetQabs();
  128. // multi_layer_mie.GetExpanCoeffs(aln, bln, cln, dln);
  129. // for (int l = 0; l<aln.size(); ++l) {
  130. // for (int n = 0; n<3; ++n) {
  131. // str+=" "+std::to_string(static_cast<double>(pow2(std::abs(aln[l][n]))+
  132. // pow2(std::abs(dln[l][n]))))
  133. // + " "
  134. // + std::to_string(static_cast<double>(pow2(std::abs(bln[l][n]))
  135. // + pow2(std::abs(cln[l][n])) ));
  136. // // str+=" "+std::to_string(aln[l][n].real() - pow2(std::abs(aln[l][n]))
  137. // // +dln[l][n].real() - pow2(std::abs(dln[l][n])))
  138. // // + " "
  139. // // + std::to_string(bln[l][n].real() - pow2(std::abs(bln[l][n]))
  140. // // +cln[l][n].real() - pow2(std::abs(cln[l][n])) );
  141. // }
  142. // }
  143. // str+="\n";
  144. // fprintf(fp, "%s", str.c_str());
  145. // str.clear();
  146. // }
  147. // fclose(fp);
  148. // }
  149. // WL = 500;
  150. // multi_layer_mie.SetWavelength(WL);
  151. // multi_layer_mie.RunMieCalculation();
  152. // multi_layer_mie.GetQabs();
  153. // multi_layer_mie.GetExpanCoeffs(aln, bln, cln, dln);
  154. printf("\n Scattering");
  155. for (int l = 0; l<aln.size(); ++l) {
  156. int n = 0;
  157. printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(), aln[l][n].imag());
  158. printf("bln[%i][%i] = %g, %gi\n", l, n+1, bln[l][n].real(), bln[l][n].imag());
  159. printf("cln[%i][%i] = %g, %gi\n", l, n+1, cln[l][n].real(), cln[l][n].imag());
  160. printf("dln[%i][%i] = %g, %gi\n", l, n+1, dln[l][n].real(), dln[l][n].imag());
  161. n = 1;
  162. printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(), aln[l][n].imag());
  163. printf("bln[%i][%i] = %g, %gi\n", l, n+1, bln[l][n].real(), bln[l][n].imag());
  164. printf("cln[%i][%i] = %g, %gi\n", l, n+1, cln[l][n].real(), cln[l][n].imag());
  165. printf("dln[%i][%i] = %g, %gi\n", l, n+1, dln[l][n].real(), dln[l][n].imag());
  166. n = 2;
  167. printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(), aln[l][n].imag());
  168. printf("bln[%i][%i] = %g, %gi\n", l, n+1, bln[l][n].real(), bln[l][n].imag());
  169. printf("cln[%i][%i] = %g, %gi\n", l, n+1, cln[l][n].real(), cln[l][n].imag());
  170. printf("dln[%i][%i] = %g, %gi\n", l, n+1, dln[l][n].real(), dln[l][n].imag());
  171. n = 3;
  172. printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(), aln[l][n].imag());
  173. printf("bln[%i][%i] = %g, %gi\n", l, n+1, bln[l][n].real(), bln[l][n].imag());
  174. printf("cln[%i][%i] = %g, %gi\n", l, n+1, cln[l][n].real(), cln[l][n].imag());
  175. printf("dln[%i][%i] = %g, %gi\n", l, n+1, dln[l][n].real(), dln[l][n].imag());
  176. n = 4;
  177. printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(), aln[l][n].imag());
  178. printf("bln[%i][%i] = %g, %gi\n", l, n+1, bln[l][n].real(), bln[l][n].imag());
  179. printf("cln[%i][%i] = %g, %gi\n", l, n+1, cln[l][n].real(), cln[l][n].imag());
  180. printf("dln[%i][%i] = %g, %gi\n", l, n+1, dln[l][n].real(), dln[l][n].imag());
  181. }
  182. // printf("\n Absorbtion\n");
  183. // for (int l = 0; l<aln.size(); ++l) {
  184. // if (l == aln.size()-1) printf(" Total ");
  185. // printf("===== l=%i =====\n", l);
  186. // int n = 0;
  187. // printf("aln[%i][%i] = %g\n", l, n+1, aln[l][n].real() - pow2(std::abs(aln[l][n])));
  188. // printf("bln[%i][%i] = %g\n", l, n+1, bln[l][n].real() - pow2(std::abs(bln[l][n])));
  189. // printf("cln[%i][%i] = %g\n", l, n+1, cln[l][n].real() - pow2(std::abs(cln[l][n])));
  190. // printf("dln[%i][%i] = %g\n", l, n+1, dln[l][n].real() - pow2(std::abs(dln[l][n])));
  191. // n = 1;
  192. // printf("aln[%i][%i] = %g\n", l, n+1, aln[l][n].real() - pow2(std::abs(aln[l][n])));
  193. // printf("bln[%i][%i] = %g\n", l, n+1, bln[l][n].real() - pow2(std::abs(bln[l][n])));
  194. // printf("cln[%i][%i] = %g\n", l, n+1, cln[l][n].real() - pow2(std::abs(cln[l][n])));
  195. // printf("dln[%i][%i] = %g\n", l, n+1, dln[l][n].real() - pow2(std::abs(dln[l][n])));
  196. // // n = 2;
  197. // // printf("aln[%i][%i] = %g\n", l, n+1, aln[l][n].real() - pow2(std::abs(aln[l][n])));
  198. // // printf("bln[%i][%i] = %g\n", l, n+1, bln[l][n].real() - pow2(std::abs(bln[l][n])));
  199. // // printf("cln[%i][%i] = %g\n", l, n+1, cln[l][n].real() - pow2(std::abs(cln[l][n])));
  200. // // printf("dln[%i][%i] = %g\n", l, n+1, dln[l][n].real() - pow2(std::abs(dln[l][n])));
  201. // }
  202. } catch( const std::invalid_argument& ia ) {
  203. // Will catch if multi_layer_mie fails or other errors.
  204. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  205. return -1;
  206. }
  207. return 0;
  208. }