test_bulk_sphere.cc 4.5 KB

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