|
| 1 | +#include"../math_polyint.h" |
| 2 | +#include"gtest/gtest.h" |
| 3 | +#include"../realarray.h" |
| 4 | +#include<math.h> |
| 5 | + |
| 6 | +#define doublethreshold 1e-9 |
| 7 | + |
| 8 | +/************************************************ |
| 9 | +* unit test of class PolyInt |
| 10 | +***********************************************/ |
| 11 | + |
| 12 | +/** |
| 13 | + * This unit test is to verify the accuracy of |
| 14 | + * interpolation method on the function sin(x)/x |
| 15 | + * with a interval of 0.01. |
| 16 | + * sin(x)/x is one of the solution of spherical bessel |
| 17 | + * function when l=0. |
| 18 | + * |
| 19 | + * - Tested function: |
| 20 | + * - 4 types of Polynomial_Interpolation |
| 21 | + * - Polynomial_Interpolation_xy |
| 22 | + */ |
| 23 | + |
| 24 | + |
| 25 | +class bessell0 : public testing::Test |
| 26 | +{ |
| 27 | + protected: |
| 28 | + |
| 29 | + int TableLength = 400; |
| 30 | + double interval = 0.01; |
| 31 | + ModuleBase::realArray table3,table4; |
| 32 | + ModuleBase::realArray y3; |
| 33 | + double *tablex; |
| 34 | + double *tabley; |
| 35 | + |
| 36 | + double sinc(double x) {return sin(x)/x;} |
| 37 | + |
| 38 | + void SetUp() |
| 39 | + { |
| 40 | + tablex = new double[TableLength]; |
| 41 | + tabley = new double[TableLength]; |
| 42 | + table3.create(1,1,TableLength); |
| 43 | + table4.create(1,1,1,TableLength); |
| 44 | + y3.create(1,1,TableLength); |
| 45 | + |
| 46 | + for(int i=1;i<TableLength;++i) |
| 47 | + { |
| 48 | + table3(0,0,i) = sinc(i * interval); |
| 49 | + table4(0,0,0,i) = sinc(i * interval); |
| 50 | + tablex[i] = i * interval; |
| 51 | + tabley[i] = sinc(i * interval); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + void TearDown() |
| 56 | + { |
| 57 | + delete [] tablex; |
| 58 | + delete [] tabley; |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | + |
| 63 | +TEST_F(bessell0,PolynomialInterpolationThreeDimensionY) |
| 64 | +{ |
| 65 | + ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,1,TableLength,interval,0.1); |
| 66 | + ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,2,TableLength,interval,1.005); |
| 67 | + ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,3,TableLength,interval,2.005); |
| 68 | + ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,4,TableLength,interval,3.005); |
| 69 | + ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,y3,5,TableLength,interval,3.505); |
| 70 | + |
| 71 | + EXPECT_NEAR(y3(0,0,1),sinc(0.1),doublethreshold); |
| 72 | + EXPECT_NEAR(y3(0,0,2),sinc(1.005),doublethreshold); |
| 73 | + EXPECT_NEAR(y3(0,0,3),sinc(2.005),doublethreshold); |
| 74 | + EXPECT_NEAR(y3(0,0,4),sinc(3.005),doublethreshold); |
| 75 | + EXPECT_NEAR(y3(0,0,5),sinc(3.505),doublethreshold); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(bessell0,PolynomialInterpolationThreeDimension) |
| 79 | +{ |
| 80 | + double y1 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,0.1); |
| 81 | + double y2 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,1.005); |
| 82 | + double y3 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,2.005); |
| 83 | + double y4 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,3.005); |
| 84 | + double y5 = ModuleBase::PolyInt::Polynomial_Interpolation(table3,0,0,TableLength,interval,3.505); |
| 85 | + |
| 86 | + EXPECT_NEAR(y1,sinc(0.1),doublethreshold); |
| 87 | + EXPECT_NEAR(y2,sinc(1.005),doublethreshold); |
| 88 | + EXPECT_NEAR(y3,sinc(2.005),doublethreshold); |
| 89 | + EXPECT_NEAR(y4,sinc(3.005),doublethreshold); |
| 90 | + EXPECT_NEAR(y5,sinc(3.505),doublethreshold); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(bessell0,PolynomialInterpolationFourDimension) |
| 94 | +{ |
| 95 | + double y1 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,0.1); |
| 96 | + double y2 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,1.005); |
| 97 | + double y3 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,2.005); |
| 98 | + double y4 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,3.005); |
| 99 | + double y5 = ModuleBase::PolyInt::Polynomial_Interpolation(table4,0,0,0,TableLength,interval,3.505); |
| 100 | + |
| 101 | + EXPECT_NEAR(y1,sinc(0.1),doublethreshold); |
| 102 | + EXPECT_NEAR(y2,sinc(1.005),doublethreshold); |
| 103 | + EXPECT_NEAR(y3,sinc(2.005),doublethreshold); |
| 104 | + EXPECT_NEAR(y4,sinc(3.005),doublethreshold); |
| 105 | + EXPECT_NEAR(y5,sinc(3.505),doublethreshold); |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F(bessell0,PolynomialInterpolation) |
| 109 | +{ |
| 110 | + double y1 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,0.1); |
| 111 | + double y2 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,1.005); |
| 112 | + double y3 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,2.005); |
| 113 | + double y4 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,3.005); |
| 114 | + double y5 = ModuleBase::PolyInt::Polynomial_Interpolation(tabley,TableLength,interval,3.505); |
| 115 | + |
| 116 | + EXPECT_NEAR(y1,sinc(0.1),doublethreshold); |
| 117 | + EXPECT_NEAR(y2,sinc(1.005),doublethreshold); |
| 118 | + EXPECT_NEAR(y3,sinc(2.005),doublethreshold); |
| 119 | + EXPECT_NEAR(y4,sinc(3.005),doublethreshold); |
| 120 | + EXPECT_NEAR(y5,sinc(3.505),doublethreshold); |
| 121 | +} |
| 122 | + |
| 123 | +TEST_F(bessell0,PolynomialInterpolationXY) |
| 124 | +{ |
| 125 | + double y1 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,0.1); |
| 126 | + double y2 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,1.005); |
| 127 | + double y3 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,2.005); |
| 128 | + double y4 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,3.005); |
| 129 | + double y5 = ModuleBase::PolyInt::Polynomial_Interpolation_xy(tablex,tabley,TableLength,3.505); |
| 130 | + |
| 131 | + EXPECT_NEAR(y1,sinc(0.1),doublethreshold); |
| 132 | + EXPECT_NEAR(y2,sinc(1.005),doublethreshold); |
| 133 | + EXPECT_NEAR(y3,sinc(2.005),doublethreshold); |
| 134 | + EXPECT_NEAR(y4,sinc(3.005),doublethreshold); |
| 135 | + EXPECT_NEAR(y5,sinc(3.505),doublethreshold); |
| 136 | +} |
0 commit comments