example-get-Mie.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2015 Ovidio Pena <ovidio@bytesfall.com> // Copyright
  3. // (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 // it
  8. // under the terms of the GNU General Public License as published by // the
  9. // Free Software Foundation, either version 3 of the License, or // (at your
  10. // option) any later version. //
  11. // //
  12. // This program is distributed in the hope that it will be useful, // but
  13. // WITHOUT ANY WARRANTY; without even the implied warranty of //
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU
  15. // 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 // using
  19. // it, cite the following reference: //
  20. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  21. // a multilayered sphere," Computer Physics Communications, // vol. 180,
  22. // Nov. 2009, pp. 2348-2354. //
  23. // //
  24. // You should have received a copy of the GNU General Public License // along
  25. // 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-impl.hpp"
  32. #include "../src/nmie-precision.hpp"
  33. #include "./read-spectra.h"
  34. // template<class T> inline T pow2(const T value) {return value*value;}
  35. int main(int argc, char* argv[]) {
  36. using namespace nmie;
  37. try {
  38. read_spectra::ReadSpectra Si_index, Ag_index;
  39. read_spectra::ReadSpectra plot_core_index_, plot_TiN_;
  40. std::string core_filename("Si-int.txt");
  41. // std::string core_filename("Ag.txt");
  42. // std::string TiN_filename("TiN.txt");
  43. std::string TiN_filename("Ag-int.txt");
  44. // std::string TiN_filename("Si.txt");
  45. std::string shell_filename(core_filename);
  46. nmie::MultiLayerMieApplied<nmie::FloatType> multi_layer_mie;
  47. const std::complex<double> epsilon_Si(18.4631066585, 0.6259727805);
  48. const std::complex<double> epsilon_Ag(-8.5014154589, 0.7585845411);
  49. const std::complex<double> index_Si = std::sqrt(epsilon_Si);
  50. const std::complex<double> index_Ag = std::sqrt(epsilon_Ag);
  51. double WL = 500; // nm
  52. double core_width = 5.27; // nm Si
  53. double inner_width = 8.22; // nm Ag
  54. double outer_width = 67.91; // nm Si
  55. bool isSiAgSi = true;
  56. double delta_width = 25.0;
  57. // bool isSiAgSi = false;
  58. if (isSiAgSi) {
  59. core_width = 5.27; // nm Si
  60. inner_width = 8.22; // nm Ag
  61. outer_width = 67.91; // nm Si
  62. multi_layer_mie.AddTargetLayer(core_width, index_Si);
  63. multi_layer_mie.AddTargetLayer(inner_width, index_Ag);
  64. multi_layer_mie.AddTargetLayer(outer_width + delta_width, index_Si);
  65. } else {
  66. inner_width = 31.93; // nm Ag
  67. outer_width = 4.06; // nm Si
  68. multi_layer_mie.AddTargetLayer(inner_width, index_Ag);
  69. multi_layer_mie.AddTargetLayer(outer_width + delta_width, index_Si);
  70. }
  71. for (int dd = 0; dd < 50; ++dd) {
  72. delta_width = dd;
  73. FILE* fp;
  74. std::string fname =
  75. "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,
  82. dln;
  83. multi_layer_mie.GetExpanCoeffs(aln, bln, cln, dln);
  84. std::vector<std::vector<std::complex<double> > > d_aln =
  85. nmie::ConvertComplexVectorVector<double>(aln);
  86. std::string str = std::string("#WL ");
  87. for (int l = 0; l < d_aln.size(); ++l) {
  88. for (int n = 0; n < 3; ++n) {
  89. str += "|a|^2+|d|^2_ln" + std::to_string(l) + std::to_string(n) +
  90. " " + "|b|^2+|c|^2_ln" + std::to_string(l) +
  91. std::to_string(n) + " ";
  92. }
  93. }
  94. str += "\n";
  95. fprintf(fp, "%s", str.c_str());
  96. fprintf(fp, "# |a|+|d|");
  97. str.clear();
  98. double from_WL = 400;
  99. double to_WL = 600;
  100. int total_points = 401;
  101. double delta_WL = std::abs(to_WL - from_WL) / (total_points - 1.0);
  102. Si_index.ReadFromFile(core_filename)
  103. .ResizeToComplex(from_WL, to_WL, total_points)
  104. .ToIndex();
  105. Ag_index.ReadFromFile(TiN_filename)
  106. .ResizeToComplex(from_WL, to_WL, total_points)
  107. .ToIndex();
  108. auto Si_data = Si_index.GetIndex();
  109. auto Ag_data = Ag_index.GetIndex();
  110. for (int i = 0; i < Si_data.size(); ++i) {
  111. const double& WL = Si_data[i].first;
  112. const std::complex<double>& Si = Si_data[i].second;
  113. const std::complex<double>& Ag = Ag_data[i].second;
  114. str += std::to_string(WL);
  115. multi_layer_mie.ClearTarget();
  116. if (isSiAgSi) {
  117. multi_layer_mie.AddTargetLayer(core_width, Si);
  118. multi_layer_mie.AddTargetLayer(inner_width, Ag);
  119. multi_layer_mie.AddTargetLayer(outer_width + delta_width, Si);
  120. } else {
  121. inner_width = 31.93; // nm Ag
  122. outer_width = 4.06; // nm Si
  123. multi_layer_mie.AddTargetLayer(inner_width, Ag);
  124. multi_layer_mie.AddTargetLayer(outer_width + delta_width, Si);
  125. }
  126. multi_layer_mie.SetWavelength(WL);
  127. multi_layer_mie.RunMieCalculation();
  128. multi_layer_mie.GetQabs();
  129. multi_layer_mie.GetExpanCoeffs(aln, bln, cln, dln);
  130. for (int l = 0; l < aln.size(); ++l) {
  131. for (int n = 0; n < 3; ++n) {
  132. str += " " +
  133. std::to_string(static_cast<double>(
  134. pow2(std::abs(aln[l][n])) + pow2(std::abs(dln[l][n])))) +
  135. " " +
  136. std::to_string(static_cast<double>(
  137. pow2(std::abs(bln[l][n])) + pow2(std::abs(cln[l][n]))));
  138. // str+=" "+std::to_string(aln[l][n].real() -
  139. // pow2(std::abs(aln[l][n])) +dln[l][n].real() -
  140. // pow2(std::abs(dln[l][n])))
  141. // + " "
  142. // + std::to_string(bln[l][n].real() - pow2(std::abs(bln[l][n]))
  143. // +cln[l][n].real() - pow2(std::abs(cln[l][n]))
  144. // );
  145. }
  146. }
  147. str += "\n";
  148. fprintf(fp, "%s", str.c_str());
  149. str.clear();
  150. }
  151. fclose(fp);
  152. }
  153. // WL = 500;
  154. // multi_layer_mie.SetWavelength(WL);
  155. // multi_layer_mie.RunMieCalculation();
  156. // multi_layer_mie.GetQabs();
  157. // multi_layer_mie.GetExpanCoeffs(aln, bln, cln, dln);
  158. // printf("\n Scattering");
  159. // for (int l = 0; l<aln.size(); ++l) {
  160. // int n = 0;
  161. // printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(),
  162. // aln[l][n].imag()); printf("bln[%i][%i] = %g, %gi\n", l, n+1,
  163. // bln[l][n].real(), bln[l][n].imag()); printf("cln[%i][%i] = %g, %gi\n",
  164. // l, n+1, cln[l][n].real(), cln[l][n].imag()); printf("dln[%i][%i] = %g,
  165. // %gi\n", l, n+1, dln[l][n].real(), dln[l][n].imag()); n = 1;
  166. // printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(),
  167. // aln[l][n].imag()); printf("bln[%i][%i] = %g, %gi\n", l, n+1,
  168. // bln[l][n].real(), bln[l][n].imag()); printf("cln[%i][%i] = %g, %gi\n",
  169. // l, n+1, cln[l][n].real(), cln[l][n].imag()); printf("dln[%i][%i] = %g,
  170. // %gi\n", l, n+1, dln[l][n].real(), dln[l][n].imag());
  171. // // n = 2;
  172. // // printf("aln[%i][%i] = %g, %gi\n", l, n+1, aln[l][n].real(),
  173. // aln[l][n].imag());
  174. // // printf("bln[%i][%i] = %g, %gi\n", l, n+1, bln[l][n].real(),
  175. // bln[l][n].imag());
  176. // // printf("cln[%i][%i] = %g, %gi\n", l, n+1, cln[l][n].real(),
  177. // cln[l][n].imag());
  178. // // printf("dln[%i][%i] = %g, %gi\n", l, n+1, dln[l][n].real(),
  179. // dln[l][n].imag());
  180. // }
  181. // printf("\n Absorbtion\n");
  182. // for (int l = 0; l<aln.size(); ++l) {
  183. // if (l == aln.size()-1) printf(" Total ");
  184. // printf("===== l=%i =====\n", l);
  185. // int n = 0;
  186. // printf("aln[%i][%i] = %g\n", l, n+1, aln[l][n].real() -
  187. // pow2(std::abs(aln[l][n]))); printf("bln[%i][%i] = %g\n", l, n+1,
  188. // bln[l][n].real() - pow2(std::abs(bln[l][n]))); printf("cln[%i][%i] =
  189. // %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() -
  191. // pow2(std::abs(dln[l][n]))); n = 1; printf("aln[%i][%i] = %g\n", l, n+1,
  192. // aln[l][n].real() - pow2(std::abs(aln[l][n]))); printf("bln[%i][%i] =
  193. // %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() -
  195. // pow2(std::abs(cln[l][n]))); printf("dln[%i][%i] = %g\n", l, n+1,
  196. // dln[l][n].real() - pow2(std::abs(dln[l][n])));
  197. // // n = 2;
  198. // // printf("aln[%i][%i] = %g\n", l, n+1, aln[l][n].real() -
  199. // pow2(std::abs(aln[l][n])));
  200. // // printf("bln[%i][%i] = %g\n", l, n+1, bln[l][n].real() -
  201. // pow2(std::abs(bln[l][n])));
  202. // // printf("cln[%i][%i] = %g\n", l, n+1, cln[l][n].real() -
  203. // pow2(std::abs(cln[l][n])));
  204. // // printf("dln[%i][%i] = %g\n", l, n+1, dln[l][n].real() -
  205. // pow2(std::abs(dln[l][n])));
  206. // }
  207. } catch (const std::invalid_argument& ia) {
  208. // Will catch if multi_layer_mie fails or other errors.
  209. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  210. return -1;
  211. }
  212. return 0;
  213. }