test_bulk_sphere.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "gtest/gtest.h"
  2. #include "../src/nmie-basic.hpp"
  3. // TODO fails for MP with 100 digits. And 16 digits, which should be equal to double precision.
  4. #ifndef MULTI_PRECISION
  5. //TEST(BulkSphere, DISABLED_ArgPi) {
  6. TEST(BulkSphere, ArgPi) {
  7. std::vector<double> WLs{50, 80, 100,200, 400}; //nm
  8. double host_index = 2.;
  9. double core_radius = 100.; //nm
  10. double delta = 1e-5;
  11. nmie::MultiLayerMie<nmie::FloatType> nmie;
  12. nmie.SetLayersIndex({std::complex<double>(4,0)});
  13. for (auto WL:WLs) {
  14. nmie.SetLayersSize({2*nmie.PI_*host_index*core_radius/(WL+delta)});
  15. nmie.RunMieCalculation();
  16. double Qabs_p = std::abs(static_cast<double>(nmie.GetQabs()));
  17. nmie.SetLayersSize({2*nmie.PI_*host_index*core_radius/(WL-delta)});
  18. nmie.RunMieCalculation();
  19. double Qabs_m = std::abs(static_cast<double>(nmie.GetQabs()));
  20. nmie.SetLayersSize({2*nmie.PI_*host_index*core_radius/(WL)});
  21. nmie.RunMieCalculation();
  22. double Qabs = std::abs(static_cast<double>(nmie.GetQabs()));
  23. EXPECT_GT(Qabs_p+Qabs_m, Qabs);
  24. }
  25. }
  26. #endif
  27. TEST(BulkSphere, HandlesInput) {
  28. nmie::MultiLayerMie<nmie::FloatType> nmie;
  29. // A list of tests for a bulk sphere from
  30. // Hong Du, "Mie-scattering calculation," Appl. Opt. 43, 1951-1956 (2004)
  31. // table 1: sphere size and refractive index
  32. // followed by resulting extinction and scattering efficiencies
  33. std::vector< std::tuple< double, std::complex<double>, double, double, char > >
  34. parameters_and_results
  35. {
  36. // x, {Re(m), Im(m)}, Qext, Qsca, test_name
  37. {0.099, {0.75,0}, 7.417859e-06, 7.417859e-06, 'a'},
  38. {0.101, {0.75,0}, 8.033538e-06, 8.033538e-06, 'b'},
  39. {10, {0.75,0}, 2.232265, 2.232265, 'c'},
  40. {100, {1.33,1e-5}, 2.101321, 2.096594, 'e'},
  41. {0.055, {1.5, 1}, 0.10149104, 1.131687e-05, 'g'},
  42. {0.056, {1.5, 1}, 0.1033467, 1.216311e-05, 'h'},
  43. {100, {1.5, 1}, 2.097502, 1.283697, 'i'},
  44. {1, {10, 10}, 2.532993, 2.049405, 'k'},
  45. {1000, {0.75,0}, 1.997908, 1.997908, 'd'},
  46. {100, {10, 10,}, 2.071124, 1.836785, 'l'},
  47. #ifndef MULTI_PRECISION
  48. {10000, {1.33,1e-5}, 2.004089, 1.723857, 'f'},
  49. {10000, {1.5, 1}, 2.004368, 1.236574, 'j'},
  50. {10000, {10, 10}, 2.005914, 1.795393, 'm'},
  51. #endif
  52. };
  53. for (const auto &data : parameters_and_results) {
  54. auto x = std::get<0>(data);
  55. auto m = std::get<1>(data);
  56. // auto Nstop = nmie::LeRu_near_field_cutoff(m*x)+1;
  57. nmie.SetLayersSize({x});
  58. nmie.SetLayersIndex({m});
  59. // nmie.SetMaxTerms(Nstop);
  60. nmie.RunMieCalculation();
  61. double Qext = static_cast<double>(nmie.GetQext());
  62. double Qsca = static_cast<double>(nmie.GetQsca());
  63. double Qext_Du = std::get<2>(data);
  64. double Qsca_Du = std::get<3>(data);
  65. EXPECT_FLOAT_EQ(Qext_Du, Qext)
  66. << "Extinction of the bulk sphere, test case:" << std::get<4>(data)
  67. << "\nnmax_ = " << nmie.GetMaxTerms();
  68. EXPECT_FLOAT_EQ(Qsca_Du, Qsca)
  69. << "Scattering of the bulk sphere, test case:" << std::get<4>(data);
  70. }
  71. }
  72. int main(int argc, char **argv) {
  73. testing::InitGoogleTest(&argc, argv);
  74. return RUN_ALL_TESTS();
  75. }