example-get-Mie.cc 9.9 KB

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