Skip to content

Commit

Permalink
Test: save precision results to file when building unit tests (#3829)
Browse files Browse the repository at this point in the history
  • Loading branch information
jieli-matrix authored Mar 29, 2024
1 parent a7c1865 commit 0dd5623
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions source/module_base/test/math_sphbes_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../math_sphbes.h"

#include <cmath>
#include <fstream>
#include <iostream>

Expand Down Expand Up @@ -257,6 +258,9 @@ TEST_F(Sphbes, SphericalBesselPrecisionGrid)
const double dr = rcut / nr;
double* r = new double[nr + 10];
double* Y = new double[nr * (l_hi - l_lo + 1) + 10];
// save errs to binary file
std::ofstream file_o("data/sphj_old_out.bin", std::ios::binary);
std::ofstream file_n("data/sphj_new_out.bin", std::ios::binary);

// case 0: x = 0, l = 0
EXPECT_NEAR(ModuleBase::Sphbes::sphbesj(0, 0), 1.0, 1e-12);
Expand Down Expand Up @@ -288,6 +292,8 @@ TEST_F(Sphbes, SphericalBesselPrecisionGrid)
for (int i = 0; i < nr; ++i)
{
EXPECT_NEAR(ModuleBase::Sphbes::sphbesj(l, r[i] * q), Y[l * nr + i], 1e-12);
double tmp = std::abs(Y[l * nr + i] - ModuleBase::Sphbes::sphbesj(l, r[i] * q));
file_n.write(reinterpret_cast<char*>(&tmp), sizeof(double));
}
}
// test for old Bessel
Expand All @@ -296,16 +302,18 @@ TEST_F(Sphbes, SphericalBesselPrecisionGrid)
for (int l = l_lo; l <= l_hi; ++l)
{
ModuleBase::Sphbes::Spherical_Bessel(nr, r, q, l, jl_old);
double errs = 0.0;
for (int i = 0; i < nr; ++i)
{
errs += MAX(jl_old[i] - Y[l * nr + i], Y[l * nr + i] - jl_old[i]);
double tmp = std::abs(jl_old[i] - Y[l * nr + i]);
file_o.write(reinterpret_cast<char*>(&tmp), sizeof(double));
}
// std::cout << " l = " << l << ", errors: " << std::scientific << errs / nr << std::endl;
}

delete[] r;
delete[] Y;
delete[] jl_old;
file_o.close();
file_n.close();
}

TEST_F(Sphbes, SphericalBesselPrecisionNearZero)
Expand Down

0 comments on commit 0dd5623

Please sign in to comment.