test_near_field.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "gtest/gtest.h"
  2. #include "../src/nmie-basic.hpp"
  3. #include "../src/nmie-nearfield.hpp"
  4. #include "test_cases.hpp"
  5. //TEST(RunFieldCalculationCartesian, DISABLED_HandlesInput) {
  6. TEST(RunFieldCalculationCartesian, HandlesInput) {
  7. nmie::MultiLayerMie<nmie::FloatType> nmie;
  8. // EXPECT_THROW(nmie.RunFieldCalculationPolar(0), std::invalid_argument);
  9. // EXPECT_THROW(nmie.RunFieldCalculationPolar(1,1,10,5), std::invalid_argument);
  10. nmie::FloatType total_r = 2*nmie.PI_*1000/532;
  11. // double r = 1500;
  12. nmie.SetLayersSize({total_r/2, total_r});
  13. nmie.SetLayersIndex({ {1.330,0}, {1.33,0}});
  14. nmie.RunMieCalculation();
  15. double relative_max_distance = 1e-10;
  16. // nmie.SetModeNmaxAndType(3,-1);
  17. // int nmax = 21;
  18. nmie.RunFieldCalculationCartesian(2, 5, relative_max_distance, nmie::Planes::kEk,
  19. 1.0, 0, 0, false,3);
  20. auto Eabs = nmie.GetFieldEabs();
  21. auto E = nmie.GetFieldE();
  22. std::cout<<std::endl;
  23. {
  24. // Eabs points are located near the sphere outer border
  25. //
  26. // 0 1 2 3 4
  27. // ----- border ----
  28. // 5 6 7 8 9
  29. // distance between points (0) and (4) is relative_max_distance*total_r, initial
  30. // value used for the test was 1e-10*total_r, so we expect good linear dependence
  31. // for points from 0 to 4 and 5 to 9. In the asserts we check, that the slope doesn't
  32. // change too fast inside the curve. While writing this, the test was failing.
  33. // The value of z-coordinates of 2 and 7 points = 0
  34. using nmie::nmm::abs;
  35. EXPECT_TRUE(
  36. ( abs(Eabs[0] - Eabs[1]) + abs(Eabs[3] - Eabs[4]) ) >= abs(Eabs[1] - Eabs[2])
  37. );
  38. EXPECT_TRUE(
  39. ( abs(Eabs[5] - Eabs[6]) + abs(Eabs[8] - Eabs[9]) ) >= abs(Eabs[6] - Eabs[7])
  40. );
  41. }
  42. // nmie.RunFieldCalculationCartesian(2, 2, 2, nmie::Planes::kHk,
  43. // 0, 0, 0, true);
  44. // nmie.RunFieldCalculationCartesian(2, 2, 2, nmie::Planes::kEH,
  45. // 0, 0, 0, true);
  46. // TODO add check of E and H symmetry for X and Y axis inversion
  47. // EXPECT_EQ(1, nmie.GetMaxTerms());
  48. // EXPECT_FALSE(nmie.GetFieldConvergence());
  49. // auto Eabs = nmie.GetFieldEabs();
  50. // EXPECT_TRUE(std::isnan(static_cast<double>(Eabs[0])));
  51. }
  52. TEST(LargeBubbleSpectrum, DISABLED_HandlesInput) {
  53. //TEST(LargeBubbleSpectrum, HandlesInput) { // TODO fix fail...
  54. nmie::MultiLayerMie<nmie::FloatType> nmie;
  55. nmie::FloatType core_r = 2*nmie.PI_*100;
  56. nmie::FloatType shell_r = 2*nmie.PI_*(100+0.1);
  57. nmie.SetLayersIndex({ {1,0}, {1.33,0}});
  58. double central_WL = 0.7007;
  59. double relative_distance = 1e-10;
  60. double dWL = central_WL*relative_distance;
  61. std::vector<double> Qsca(5);
  62. for (int i = 0; i < 5; ++i) {
  63. auto WL = static_cast<nmie::FloatType>(central_WL +(i - 2)*dWL);
  64. nmie.SetLayersSize({core_r/WL, shell_r/WL});
  65. nmie.RunMieCalculation();
  66. Qsca[i] = static_cast<double>(nmie.GetQsca());
  67. std::cout<<"Qsca["<<i<<"]="<<Qsca[i]<<std::endl;
  68. }
  69. {
  70. // Eabs points are located near the sphere outer border
  71. //
  72. // 0 1 2 3 4
  73. // ------- WL ------>
  74. // distance between points (0) and (4) is 5*relative_distance*central_WL, initial
  75. // value used for the test was 5*1e-10*0.7007, so we expect good linear dependence
  76. // for points from 0 to 4. In the asserts we check, that the slope doesn't
  77. // change too fast inside the curve.
  78. using std::abs;
  79. EXPECT_TRUE(
  80. ( abs(Qsca[0] - Qsca[1]) + abs(Qsca[3] - Qsca[4]) ) >= abs(Qsca[1] - Qsca[2])
  81. );
  82. }
  83. }
  84. //TEST(RunFieldCalculationPolar, DISABLED_HandlesInput) {
  85. TEST(RunFieldCalculationPolar, HandlesInput) {
  86. nmie::MultiLayerMie<nmie::FloatType> nmie;
  87. EXPECT_THROW(nmie.RunFieldCalculationPolar(0), std::invalid_argument);
  88. EXPECT_THROW(nmie.RunFieldCalculationPolar(1,1,10,5), std::invalid_argument);
  89. double r = 60;
  90. // double r = 1500;
  91. nmie.SetLayersSize({r/2, r});
  92. nmie.SetLayersIndex({ {1.33,0}, {1.33,0}});
  93. nmie.RunMieCalculation();
  94. nmie.RunFieldCalculationPolar(1, 1,
  95. 0.5145*r,
  96. r*0.5148,
  97. 0, 3.14, 0, 0, true, 1);
  98. EXPECT_EQ(1, nmie.GetMaxTerms());
  99. EXPECT_FALSE(nmie.GetFieldConvergence());
  100. auto Eabs = nmie.GetFieldEabs();
  101. EXPECT_TRUE(std::isnan(static_cast<double>(Eabs[0])));
  102. }
  103. //#ifndef MULTI_PRECISION
  104. //TEST(BulkSphere, DISABLED_HandlesInput) {
  105. TEST(BulkSphere, HandlesInput) {
  106. nmie::MultiLayerMie<nmie::FloatType> nmie;
  107. for (const auto &data : parameters_bulk_sphere) {
  108. auto x = std::get<0>(data);
  109. auto m = std::get<1>(data);
  110. nmie.SetLayersSize({x});
  111. nmie.SetLayersIndex({m});
  112. nmie.SetMaxTerms(-1);
  113. // nmie.RunMieCalculation();
  114. // std::cout<<" test case: "<<std::get<2>(data)<<" Qsca="<<nmie.GetQsca()<<std::endl;
  115. nmie.RunFieldCalculationPolar(4,3,x,x*3, 0, static_cast<double>(nmie.PI_), 0, static_cast<double>(nmie.PI_),true, -1);
  116. auto Eabs = nmie.GetFieldEabs();
  117. for (auto &E:Eabs) E=nmie::pow2(E);
  118. // print(Eabs)
  119. EXPECT_TRUE(nmie.GetFieldConvergence())<<"Outside test for x="<<x<<" m="<<m<<" test case: "<<std::get<2>(data)<<std::endl;
  120. nmie.RunFieldCalculationPolar(4,10,x*0.01,x, 0, static_cast<double>(nmie.PI_), 0, static_cast<double>(nmie.PI_),true, -1);
  121. EXPECT_TRUE(nmie.GetFieldConvergence())<<"Inside test for x="<<x<<" m="<<m<<" test case: "<<std::get<2>(data)<<std::endl;
  122. }
  123. }
  124. //#endif
  125. int main(int argc, char **argv) {
  126. testing::InitGoogleTest(&argc, argv);
  127. return RUN_ALL_TESTS();
  128. }