test_bulk_sphere.cc 5.5 KB

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