forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
118 changed files
with
1,475 additions
and
807 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ jobs: | |
--from-ref ${{ github.event.pull_request.base.sha }} | ||
--to-ref ${{ github.event.pull_request.head.sha }} | ||
continue-on-error: true | ||
- uses: pre-commit-ci/[email protected].2 | ||
- uses: pre-commit-ci/[email protected].3 | ||
|
||
- name: Build | ||
run: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef GRID_DELLEY_H | ||
#define GRID_DELLEY_H | ||
|
||
#include <vector> | ||
#include <functional> | ||
|
||
/** | ||
* @brief Delley's grid for quadrature on the unit sphere. | ||
* | ||
* Reference: | ||
* Delley, B. (1996). High order integration schemes on the unit sphere. | ||
* Journal of computational chemistry, 17(9), 1152-1155. | ||
* | ||
*/ | ||
namespace Grid { | ||
namespace Delley { | ||
|
||
/** | ||
* @brief Number of Delley's grid points for a certain order of accuracy. | ||
* | ||
* This function finds the minimum Delley's grid with an accuracy | ||
* of order "lmax". On exit, lmax is set to the order of this grid, and | ||
* its corresponding number of grid points is returned. If no such grid | ||
* is available, lmax is left unchanged and the function will return -1. | ||
* | ||
* For example, if lmax = 20 on input, the function will return 194 and | ||
* lmax will be set to 23. | ||
* | ||
*/ | ||
int ngrid(int& lmax); | ||
|
||
|
||
/** | ||
* @brief Delley's quadrature grid and weights. | ||
* | ||
* This function retrieves the minimum Delley's grid with an accuray | ||
* of order "lmax". On exit, lmax is set to the order of this grid, and | ||
* the coordinates & weights are returned in "grid" & "weight". | ||
* | ||
* Coordinates are stored in the following order: | ||
* | ||
* x0, y0, z0, x1, y1, z1, x2, y2, z2, ... | ||
* | ||
* "grid" and "weight" must be pre-allocated to hold 3*ngrid(lmax) and | ||
* ngrid(lmax) elements, respectively. The function will return 0 if | ||
* successful, or -1 if the requested order cannot be fulfilled. | ||
* | ||
*/ | ||
int get(int& lmax, double* grid, double* weight); | ||
|
||
// a handy wrapper doing the same as above | ||
int get(int& lmax, std::vector<double>& grid, std::vector<double>& weight); | ||
|
||
} // end of namespace Delley | ||
} // end of namespace Grid | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
remove_definitions(-D__MPI) | ||
|
||
AddTest( | ||
TARGET test_delley | ||
SOURCES test_delley.cpp | ||
../delley.cpp | ||
../../ylm.cpp | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include "module_base/grid/delley.h" | ||
#include "module_base/ylm.h" | ||
|
||
#include "gtest/gtest.h" | ||
#include <random> | ||
#ifdef __MPI | ||
#include <mpi.h> | ||
#endif | ||
|
||
using namespace Grid; | ||
|
||
// mock the function to prevent unnecessary dependency | ||
namespace ModuleBase { | ||
void WARNING_QUIT(const std::string&, const std::string&) {} | ||
} | ||
|
||
class DelleyTest: public ::testing::Test { | ||
protected: | ||
void randgen(int lmax, std::vector<double>& coef); | ||
const double tol = 1e-12; | ||
}; | ||
|
||
|
||
void DelleyTest::randgen(int lmax, std::vector<double>& coef) { | ||
coef.resize((lmax + 1) * (lmax + 1)); | ||
|
||
// fill coef with uniformly distributed random numbers | ||
std::random_device rd; | ||
std::mt19937 gen(rd()); | ||
std::uniform_real_distribution<double> dis(0.0, 1.0); | ||
for (size_t i = 0; i < coef.size(); ++i) { | ||
coef[i] = dis(gen); | ||
} | ||
|
||
// normalize the coefficients | ||
double fac = 0.0; | ||
for (size_t i = 0; i < coef.size(); ++i) { | ||
fac += coef[i] * coef[i]; | ||
} | ||
|
||
fac = 1.0 / std::sqrt(fac); | ||
for (size_t i = 0; i < coef.size(); ++i) { | ||
coef[i] *= fac; | ||
} | ||
} | ||
|
||
|
||
TEST_F(DelleyTest, NumGrid) { | ||
int lmax = 5; | ||
int ngrid = Delley::ngrid(lmax); | ||
EXPECT_EQ(lmax, 17); | ||
EXPECT_EQ(ngrid, 110); | ||
|
||
lmax = 17; | ||
ngrid = Delley::ngrid(lmax); | ||
EXPECT_EQ(lmax, 17); | ||
EXPECT_EQ(ngrid, 110); | ||
|
||
lmax = 20; | ||
ngrid = Delley::ngrid(lmax); | ||
EXPECT_EQ(lmax, 23); | ||
EXPECT_EQ(ngrid, 194); | ||
|
||
lmax = 59; | ||
ngrid = Delley::ngrid(lmax); | ||
EXPECT_EQ(lmax, 59); | ||
EXPECT_EQ(ngrid, 1202); | ||
|
||
lmax = 60; | ||
ngrid = Delley::ngrid(lmax); | ||
EXPECT_EQ(lmax, 60); | ||
EXPECT_EQ(ngrid, -1); | ||
} | ||
|
||
|
||
TEST_F(DelleyTest, Accuracy) { | ||
/* | ||
* Given | ||
* | ||
* f = c[0]*Y00 + c[1]*Y10 + c[2]*Y11 + ..., | ||
* | ||
* where c[0], c[1], c[2], ... are some random numbers, the integration | ||
* of |f|^2 on the unit sphere | ||
* | ||
* \int |f|^2 d\Omega = c[0]^2 + c[1]^2 + c[2]^2 + ... . | ||
* | ||
* This test verifies with the above integral that quadrature with | ||
* Delley's grid is exact up to floating point errors. | ||
* | ||
*/ | ||
std::vector<double> grid, weight, coef; | ||
|
||
for (int grid_lmax = 17; grid_lmax < 60; grid_lmax +=6) { | ||
Delley::get(grid_lmax, grid, weight); | ||
int func_lmax = grid_lmax / 2; | ||
randgen(func_lmax, coef); | ||
|
||
double val = 0.0; | ||
std::vector<double> ylm_real; | ||
for (size_t i = 0; i < weight.size(); i++) { | ||
ModuleBase::Ylm::sph_harm(func_lmax, | ||
grid[3*i], grid[3*i+1], grid[3*i+2], ylm_real); | ||
double tmp = 0.0; | ||
for (size_t i = 0; i < coef.size(); ++i) { | ||
tmp += coef[i] * ylm_real[i]; | ||
} | ||
val += weight[i] * tmp * tmp; | ||
} | ||
val *= 4.0 * std::acos(-1.0); | ||
|
||
double val_ref = 0.0; | ||
for (size_t i = 0; i < coef.size(); ++i) { | ||
val_ref += coef[i] * coef[i]; | ||
} | ||
|
||
double abs_diff = std::abs(val - val_ref); | ||
EXPECT_LT(abs_diff, tol); | ||
//printf("order = %2i val_ref = %8.5f abs_diff = %8.5e\n", | ||
// grid_lmax, val_ref, abs_diff); | ||
} | ||
} | ||
|
||
|
||
int main(int argc, char** argv) | ||
{ | ||
#ifdef __MPI | ||
MPI_Init(&argc, &argv); | ||
#endif | ||
|
||
testing::InitGoogleTest(&argc, argv); | ||
int result = RUN_ALL_TESTS(); | ||
|
||
#ifdef __MPI | ||
MPI_Finalize(); | ||
#endif | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.