test_near_field.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "../src/nmie-basic.hpp"
  2. #include "../src/nmie-nearfield.hpp"
  3. #include "gtest/gtest.h"
  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),
  10. // std::invalid_argument);
  11. nmie::FloatType total_r = 2 * nmie::PI_ * 1000 / 532;
  12. // double r = 1500;
  13. nmie.SetLayersSize({total_r / 2, total_r});
  14. nmie.SetLayersIndex({{1.330, 0}, {1.33, 0}});
  15. nmie.RunMieCalculation();
  16. double relative_max_distance = 1e-10;
  17. // nmie.SetModeNmaxAndType(3,-1);
  18. // int nmax = 21;
  19. nmie.RunFieldCalculationCartesian(2, 5, relative_max_distance,
  20. nmie::Planes::kEk, 1.0, 0, 0, false, 3);
  21. auto Eabs = nmie.GetFieldEabs();
  22. auto E = nmie.GetFieldE();
  23. std::cout << std::endl;
  24. {
  25. // Eabs points are located near the sphere outer border
  26. //
  27. // 0 1 2 3 4
  28. // ----- border ----
  29. // 5 6 7 8 9
  30. // distance between points (0) and (4) is relative_max_distance*total_r,
  31. // initial value used for the test was 1e-10*total_r, so we expect good
  32. // linear dependence for points from 0 to 4 and 5 to 9. In the asserts we
  33. // check, that the slope doesn't change too fast inside the curve. While
  34. // writing this, the test was failing. The value of z-coordinates of 2 and 7
  35. // points = 0
  36. using nmie::nmm::abs;
  37. EXPECT_TRUE((abs(Eabs[0] - Eabs[1]) + abs(Eabs[3] - Eabs[4])) >=
  38. abs(Eabs[1] - Eabs[2]));
  39. EXPECT_TRUE((abs(Eabs[5] - Eabs[6]) + abs(Eabs[8] - Eabs[9])) >=
  40. abs(Eabs[6] - Eabs[7]));
  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,
  75. // initial value used for the test was 5*1e-10*0.7007, so we expect good
  76. // linear dependence for points from 0 to 4. In the asserts we check, that
  77. // the slope doesn't change too fast inside the curve.
  78. using std::abs;
  79. EXPECT_TRUE((abs(Qsca[0] - Qsca[1]) + abs(Qsca[3] - Qsca[4])) >=
  80. abs(Qsca[1] - Qsca[2]));
  81. }
  82. }
  83. // TEST(RunFieldCalculationPolar, DISABLED_HandlesInput) {
  84. TEST(RunFieldCalculationPolar, HandlesInput) {
  85. nmie::MultiLayerMie<nmie::FloatType> nmie;
  86. EXPECT_THROW(nmie.RunFieldCalculationPolar(0), std::invalid_argument);
  87. EXPECT_THROW(nmie.RunFieldCalculationPolar(1, 1, 10, 5),
  88. 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, 0.5145 * r, r * 0.5148, 0, 3.14, 0, 0,
  95. true, 1);
  96. EXPECT_EQ(1, nmie.GetMaxTerms());
  97. EXPECT_FALSE(nmie.GetFieldConvergence());
  98. auto Eabs = nmie.GetFieldEabs();
  99. EXPECT_TRUE(std::isnan(static_cast<double>(Eabs[0])));
  100. }
  101. //#ifndef MULTI_PRECISION
  102. // TEST(BulkSphere, DISABLED_HandlesInput) {
  103. TEST(BulkSphere, HandlesInput) {
  104. nmie::MultiLayerMie<nmie::FloatType> nmie;
  105. for (const auto& data : parameters_bulk_sphere) {
  106. auto x = std::get<0>(data);
  107. auto m = std::get<1>(data);
  108. nmie.SetLayersSize({x});
  109. nmie.SetLayersIndex({m});
  110. nmie.SetMaxTerms(-1);
  111. // nmie.RunMieCalculation();
  112. // std::cout<<" test case: "<<std::get<2>(data)<<"
  113. // Qsca="<<nmie.GetQsca()<<std::endl;
  114. nmie.RunFieldCalculationPolar(4, 3, x, x * 3, 0,
  115. static_cast<double>(nmie::PI_), 0,
  116. static_cast<double>(nmie::PI_), true, -1);
  117. auto Eabs = nmie.GetFieldEabs();
  118. for (auto& E : Eabs)
  119. E = nmie::pow2(E);
  120. // print(Eabs)
  121. EXPECT_TRUE(nmie.GetFieldConvergence())
  122. << "Outside test for x=" << x << " m=" << m
  123. << " test case: " << std::get<2>(data) << std::endl;
  124. nmie.RunFieldCalculationPolar(4, 10, x * 0.01, x, 0,
  125. static_cast<double>(nmie::PI_), 0,
  126. static_cast<double>(nmie::PI_), true, -1);
  127. EXPECT_TRUE(nmie.GetFieldConvergence())
  128. << "Inside test for x=" << x << " m=" << m
  129. << " test case: " << std::get<2>(data) << std::endl;
  130. }
  131. }
  132. //#endif
  133. int main(int argc, char** argv) {
  134. testing::InitGoogleTest(&argc, argv);
  135. return RUN_ALL_TESTS();
  136. }