test_bulk_sphere.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. {10000, {1.33,1e-5}, 2.004089, 1.723857, 'f'},
  48. {10000, {1.5, 1}, 2.004368, 1.236574, 'j'},
  49. // {10000, {10, 10}, 2.005914, 1.791, 'm'},
  50. {10000, {10, 10}, 2.005914, 1.795393, 'm'},
  51. #ifndef MULTI_PRECISION
  52. {10000, {1.33,1e-5}, 2.004089, 1.723857, 'f'},
  53. {10000, {1.5, 1}, 2.004368, 1.236574, 'j'},
  54. {10000, {10, 10}, 2.005914, 1.795393, 'm'},
  55. #endif
  56. };
  57. for (const auto &data : parameters_and_results) {
  58. auto x = std::get<0>(data);
  59. auto m = std::get<1>(data);
  60. // auto Nstop = nmie::LeRu_near_field_cutoff(m*x)+1;
  61. nmie.SetLayersSize({x});
  62. nmie.SetLayersIndex({m});
  63. // nmie.SetMaxTerms(Nstop);
  64. nmie.RunMieCalculation();
  65. double Qext = static_cast<double>(nmie.GetQext());
  66. double Qsca = static_cast<double>(nmie.GetQsca());
  67. double Qext_Du = std::get<2>(data);
  68. double Qsca_Du = std::get<3>(data);
  69. EXPECT_FLOAT_EQ(Qext_Du, Qext)
  70. << "Extinction of the bulk sphere, test case:" << std::get<4>(data)
  71. << "\nnmax_ = " << nmie.GetMaxTerms();
  72. EXPECT_FLOAT_EQ(Qsca_Du, Qsca)
  73. << "Scattering of the bulk sphere, test case:" << std::get<4>(data);
  74. }
  75. }
  76. int main(int argc, char **argv) {
  77. testing::InitGoogleTest(&argc, argv);
  78. return RUN_ALL_TESTS();
  79. }