test_near_field.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef SKIP_LONG_TESTS
  53. TEST(LargeBubbleSpectrum, DISABLED_HandlesInput) {
  54. // TEST(LargeBubbleSpectrum, HandlesInput) { // TODO fix fail...
  55. nmie::MultiLayerMie<nmie::FloatType> nmie;
  56. nmie::FloatType core_r = 2 * nmie::PI_ * 100;
  57. nmie::FloatType shell_r = 2 * nmie::PI_ * (100 + 0.1);
  58. nmie.SetLayersIndex({{1, 0}, {1.33, 0}});
  59. double central_WL = 0.7007;
  60. double relative_distance = 1e-10;
  61. double dWL = central_WL * relative_distance;
  62. std::vector<double> Qsca(5);
  63. for (int i = 0; i < 5; ++i) {
  64. auto WL = static_cast<nmie::FloatType>(central_WL + (i - 2) * dWL);
  65. nmie.SetLayersSize({core_r / WL, shell_r / WL});
  66. nmie.RunMieCalculation();
  67. Qsca[i] = static_cast<double>(nmie.GetQsca());
  68. std::cout << "Qsca[" << i << "]=" << Qsca[i] << std::endl;
  69. }
  70. {
  71. // Eabs points are located near the sphere outer border
  72. //
  73. // 0 1 2 3 4
  74. // ------- WL ------>
  75. // distance between points (0) and (4) is 5*relative_distance*central_WL,
  76. // initial value used for the test was 5*1e-10*0.7007, so we expect good
  77. // linear dependence for points from 0 to 4. In the asserts we check, that
  78. // the slope doesn't change too fast inside the curve.
  79. using std::abs;
  80. EXPECT_TRUE((abs(Qsca[0] - Qsca[1]) + abs(Qsca[3] - Qsca[4])) >=
  81. abs(Qsca[1] - Qsca[2]));
  82. }
  83. }
  84. #endif
  85. // TEST(RunFieldCalculationPolar, DISABLED_HandlesInput) {
  86. TEST(RunFieldCalculationPolar, HandlesInput) {
  87. nmie::MultiLayerMie<nmie::FloatType> nmie;
  88. EXPECT_THROW(nmie.RunFieldCalculationPolar(0), std::invalid_argument);
  89. EXPECT_THROW(nmie.RunFieldCalculationPolar(1, 1, 10, 5),
  90. std::invalid_argument);
  91. double r = 60;
  92. // double r = 1500;
  93. nmie.SetLayersSize({r / 2, r});
  94. nmie.SetLayersIndex({{1.33, 0}, {1.33, 0}});
  95. nmie.RunMieCalculation();
  96. nmie.RunFieldCalculationPolar(1, 1, 0.5145 * r, r * 0.5148, 0, 3.14, 0, 0,
  97. 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)<<"
  115. // Qsca="<<nmie.GetQsca()<<std::endl;
  116. nmie.RunFieldCalculationPolar(4, 3, x, x * 3, 0,
  117. static_cast<double>(nmie::PI_), 0,
  118. static_cast<double>(nmie::PI_), true, -1);
  119. auto Eabs = nmie.GetFieldEabs();
  120. for (auto& E : Eabs)
  121. E = nmie::pow2(E);
  122. // print(Eabs)
  123. EXPECT_TRUE(nmie.GetFieldConvergence())
  124. << "Outside test for x=" << x << " m=" << m
  125. << " test case: " << std::get<2>(data) << std::endl;
  126. nmie.RunFieldCalculationPolar(4, 10, x * 0.01, x, 0,
  127. static_cast<double>(nmie::PI_), 0,
  128. static_cast<double>(nmie::PI_), true, -1);
  129. EXPECT_TRUE(nmie.GetFieldConvergence())
  130. << "Inside test for x=" << x << " m=" << m
  131. << " test case: " << std::get<2>(data) << std::endl;
  132. }
  133. }
  134. //#endif
  135. int main(int argc, char** argv) {
  136. testing::InitGoogleTest(&argc, argv);
  137. return RUN_ALL_TESTS();
  138. }