test_bulk_sphere.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, DISABLED_HandlesInput) {
  28. TEST(BulkSphere, HandlesInput) {
  29. nmie::MultiLayerMie<nmie::FloatType> nmie;
  30. // A list of tests for a bulk sphere from
  31. // Hong Du, "Mie-scattering calculation," Appl. Opt. 43, 1951-1956 (2004)
  32. // table 1: sphere size and refractive index
  33. // followed by resulting extinction and scattering efficiencies
  34. std::vector< std::tuple< double, std::complex<double>, double, double, char > >
  35. parameters_and_results
  36. {
  37. // x, {Re(m), Im(m)}, Qext, Qsca, test_name
  38. {0.099, {0.75,0}, 7.417859e-06, 7.417859e-06, 'a'},
  39. {0.101, {0.75,0}, 8.033538e-06, 8.033538e-06, 'b'},
  40. {10, {0.75,0}, 2.232265, 2.232265, 'c'},
  41. {100, {1.33,1e-5}, 2.101321, 2.096594, 'e'},
  42. {0.055, {1.5, 1}, 0.10149104, 1.131687e-05, 'g'},
  43. {0.056, {1.5, 1}, 0.1033467, 1.216311e-05, 'h'},
  44. {100, {1.5, 1}, 2.097502, 1.283697, 'i'},
  45. {1, {10, 10}, 2.532993, 2.049405, 'k'},
  46. {1000, {0.75,0}, 1.997908, 1.997908, 'd'},
  47. {100, {10, 10,}, 2.071124, 1.836785, 'l'},
  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. };
  52. for (const auto &data : parameters_and_results) {
  53. auto x = std::get<0>(data);
  54. auto m = std::get<1>(data);
  55. // auto Nstop = nmie::LeRu_near_field_cutoff(m*x)+1;
  56. nmie.SetLayersSize({x});
  57. nmie.SetLayersIndex({m});
  58. // nmie.SetMaxTerms(Nstop);
  59. nmie.RunMieCalculation();
  60. double Qext = static_cast<double>(nmie.GetQext());
  61. double Qsca = static_cast<double>(nmie.GetQsca());
  62. double Qext_Du = std::get<2>(data);
  63. double Qsca_Du = std::get<3>(data);
  64. EXPECT_FLOAT_EQ(Qext_Du, Qext)
  65. << "Extinction of the bulk sphere, test case:" << std::get<4>(data)
  66. << "\nnmax_ = " << nmie.GetMaxTerms();
  67. EXPECT_FLOAT_EQ(Qsca_Du, Qsca)
  68. << "Scattering of the bulk sphere, test case:" << std::get<4>(data);
  69. }
  70. }
  71. int main(int argc, char **argv) {
  72. testing::InitGoogleTest(&argc, argv);
  73. return RUN_ALL_TESTS();
  74. }