example-eval-force.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2016 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2016 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 evaluates forces acting on the nanoparticle under irradiaton.
  28. #include <complex>
  29. #include <cstdio>
  30. #include <string>
  31. #include <iostream>
  32. #include "../src/nmie.hpp"
  33. #include "../src/nmie-impl.hpp"
  34. #include "../src/nmie-applied.hpp"
  35. #include "../src/nmie-applied-impl.hpp"
  36. #include "../src/shell-generator.hpp"
  37. double scale_ = 1.0;
  38. const double pi = 3.1415926535897932384626433832795;
  39. //const double pi = 3.1415926535897932384626433832795;
  40. double WL=545; //nm
  41. // ********************************************************************** //
  42. // ********************************************************************** //
  43. // ********************************************************************** //
  44. std::vector<double>
  45. EvaluateDiffForce (const std::vector< std::complex<double> > &E,
  46. const std::vector< std::complex<double> > &H,
  47. const std::vector<std::complex<double> > unit) {
  48. using namespace shell_generator;
  49. std::vector<double> P = (1/(2.0))
  50. *real(
  51. dot(unit,E)*vconj(E) +
  52. dot(unit,H)*vconj(H) +
  53. (-1.0/2.0)*(dot(E,vconj(E))
  54. +dot(H,vconj(H))
  55. )*unit
  56. );
  57. return P;
  58. }
  59. // ********************************************************************** //
  60. // ********************************************************************** //
  61. // ********************************************************************** //
  62. std::vector<double> GetChargeField (std::vector<double> point) {
  63. using namespace shell_generator;
  64. std::vector< std::complex<double> > zero (3,std::complex<double>(0.0,0.0));
  65. std::vector< std::complex<double> > E = zero;
  66. std::vector< std::complex<double> > H = zero;
  67. //double charge = 3.14;
  68. double charge = 1.0;
  69. double shift = 10;//*(2.0*pi)/WL;
  70. if (norm(point) < shift) std::cout<<"<";
  71. std::vector<double> v_shift = {shift, 0.0, 0.0};
  72. double r = norm(point-v_shift);
  73. std::vector<std::complex<double> > sph_unit = { point[0]/r, point[1]/r, point[2]/r};
  74. std::vector<std::complex<double> > unit = { (point[0]-shift)/r, point[1]/r, point[2]/r};
  75. const double pi = 3.1415926535897932384626433832795;
  76. //const double pi = 3.1415926535897932384626433832795;
  77. double ampl = charge/(4.0*pi*pow2(r));
  78. E = ampl*unit;
  79. std::cout << "%% " << real(E[0]) << " "
  80. << real(E[1]) << " "
  81. << real(E[2]) << " "
  82. << std::endl;
  83. //return EvaluateDiffForce(E,H,sph_unit);
  84. std::vector< double > gauss (3, 0.0);
  85. std::complex< double > gauss_value = dot(sph_unit,E);
  86. gauss[0] = real(gauss_value);
  87. return gauss;
  88. // // Test Poynting vector integration
  89. // std::vector<double> unit = { vert[0]/r, vert[1]/r, vert[2]/r};
  90. // std::vector<double> P = (1/(2.0))
  91. // *real(cross(E,vconj(H)));
  92. //integral[0] = integral[0] + per_face_area_[i]*dot(P,unit);
  93. }
  94. // ********************************************************************** //
  95. // ********************************************************************** //
  96. // ********************************************************************** //
  97. int main(int argc, char *argv[]) {
  98. try {
  99. nmie::MultiLayerMieApplied<double> multi_layer_mie;
  100. const std::complex<double> epsilon_Si(18.4631066585, 0.6259727805);
  101. // const std::complex<double> epsilon_Ag(-8.5014154589, 0.7585845411);
  102. const std::complex<double> index_Si = std::sqrt(epsilon_Si);
  103. //const std::complex<double> index_Si(3.1,0.00);
  104. // const std::complex<double> index_Ag = std::sqrt(epsilon_Ag);
  105. //double WL=400; //nm
  106. //double outer_width = 67.91; //nm Si
  107. //double outer_width = 40; //nm Si
  108. double outer_width = 1; //nm Si
  109. //auto shift = 0.0;
  110. for (int refines=0; refines<1; ++refines) {
  111. //shell.Refine();
  112. //for (int i=1; i<165; ++i) {
  113. for (int i=4; i<5; ++i) {
  114. //outer_width = 40 + 5*i;
  115. auto integration_radius = outer_width + 5*i ;
  116. //auto integration_radius = 1.0 ;
  117. //outer_width = 10; //+10*i; //nm Si
  118. multi_layer_mie.ClearAllDesign();
  119. multi_layer_mie.AddTargetLayer(outer_width, index_Si);
  120. multi_layer_mie.SetWavelength(WL);
  121. multi_layer_mie.RunMieCalculation();
  122. //double Qsca = multi_layer_mie.GetQsca();
  123. //printf("Qsca = %g\t", Qsca);
  124. scale_ = // 2.0*pi*
  125. (integration_radius);///WL;//*1.00001; //Integration sphere radius.
  126. shell_generator::ShellGenerator shell;
  127. shell.Init();
  128. shell.Refine(); // shell.Refine(); shell.Refine();
  129. shell.Rescale(scale_);
  130. auto points = shell.GetVerticesT();
  131. //auto points = shell.GetFaceCentersT();
  132. multi_layer_mie.SetFieldPointsSP(points);
  133. multi_layer_mie.RunFieldCalculation();
  134. auto E = nmie::ConvertComplexVectorVector<double>(multi_layer_mie.GetFieldE());
  135. auto H = nmie::ConvertComplexVectorVector<double>(multi_layer_mie.GetFieldH());
  136. // auto Es = nmie::ConvertComplexVectorVector<double>(multi_layer_mie.GetFieldEs());
  137. // auto Hs = nmie::ConvertComplexVectorVector<double>(multi_layer_mie.GetFieldHs());
  138. shell.SetField(E,H);
  139. //shell.SetFieldSph(Es,Hs);
  140. //auto F = shell.Integrate();
  141. shell.ValueAtPoint = &GetChargeField;
  142. auto F = shell.IntegrateByFacesQuadrature2();
  143. //auto F = shell.IntegrateByComp();
  144. std::cout //<< "integrate_R:\t"
  145. <<std::setprecision(16)
  146. << (scale_)//*WL/(2.0*pi)
  147. // << " $$ "<< shell_generator::norm(points[0])
  148. ;
  149. std::cout<<"\t"
  150. <<F[0]<<"\t"<< F[1] <<"\t" <<F[2] << std::endl;
  151. // auto F1 = shell.IntegrateByComp();
  152. // std::cout<<"F: " <<F1[0]<<", "<< F1[1] <<", "<<F1[2] << std::endl;
  153. // auto F = shell.IntegrateGaussSimple(3.14, 2*pi*outer_width/WL);
  154. // std::cout<<"\tr: "<<outer_width/2.0<<"\tF: " <<F<< std::endl;
  155. }
  156. } // end for refines
  157. } catch( const std::invalid_argument& ia ) {
  158. // Will catch if multi_layer_mie fails or other errors.
  159. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  160. return -1;
  161. }
  162. return 0;
  163. }