26static void print_func(
const char *msg,
int msglen,
int output_unit) {
28 if (output_unit == 0) {
37static int run_test(
const char cp2k_root_dir[],
const char task_file[]) {
38 if (strlen(cp2k_root_dir) > 512) {
39 fprintf(stderr,
"Error: cp2k_root_dir too long.\n");
44 strcpy(filename, cp2k_root_dir);
45 if (filename[strlen(filename) - 1] !=
'/') {
46 strcat(filename,
"/");
49 strcat(filename,
"src/grid/sample_tasks/");
50 strcat(filename, task_file);
52 const double tolerance = 1e-12;
54 for (
int icol = 0; icol < 2; icol++) {
55 for (
int ibatch = 0; ibatch < 2; ibatch++) {
57 grid_replay(filename, 1, icol == 1, ibatch == 1, 1, tolerance);
59 printf(
"Max diff too high, test failed.\n\n");
71 const double radius,
const double zeta,
72 const bool compare_ref) {
73 const double det = dh[0][0] * (dh[1][1] * dh[2][2] - dh[1][2] * dh[2][1]) -
74 dh[0][1] * (dh[1][0] * dh[2][2] - dh[1][2] * dh[2][0]) +
75 dh[0][2] * (dh[1][0] * dh[2][1] - dh[1][1] * dh[2][0]);
76 const double inv_det = 1.0 / det;
77 const double dh_inv[3][3] = {
78 {(dh[1][1] * dh[2][2] - dh[1][2] * dh[2][1]) * inv_det,
79 (dh[0][2] * dh[2][1] - dh[0][1] * dh[2][2]) * inv_det,
80 (dh[0][1] * dh[1][2] - dh[0][2] * dh[1][1]) * inv_det},
81 {(dh[1][2] * dh[2][0] - dh[1][0] * dh[2][2]) * inv_det,
82 (dh[0][0] * dh[2][2] - dh[0][2] * dh[2][0]) * inv_det,
83 (dh[0][2] * dh[1][0] - dh[0][0] * dh[1][2]) * inv_det},
84 {(dh[1][0] * dh[2][1] - dh[1][1] * dh[2][0]) * inv_det,
85 (dh[0][1] * dh[2][0] - dh[0][0] * dh[2][1]) * inv_det,
86 (dh[0][0] * dh[1][1] - dh[0][1] * dh[1][0]) * inv_det}};
87 const double rab[3] = {0.0, 0.0, 0.0};
88 const int npts_global[3] = {compare_ref ? 4 : 8000, 4,
89 compare_ref ? 4 : 8000};
90 const int npts_local[3] = {compare_ref ? 4 : 1, compare_ref ? 4 : 1,
92 const int ng = compare_ref ? 64 : 1;
93 const int zero3[3] = {0, 0, 0};
94 const double pab[1][1] = {{1.0}};
95 double grid_ref[64] = {0.0}, grid_cpu[64] = {0.0};
96 double hab_ref[1][1] = {{0.0}}, hab_cpu[1][1] = {{0.0}};
97 double forces[2][3] = {{0.0}}, virials[2][3][3] = {{{0.0}}};
100 false, 0,
GRID_FUNC_AB, 0, 0, 0, 0, zeta, zeta, 1.0, dh, dh_inv, ra, rab,
101 npts_global,
npts_local, zero3, zero3, radius, 0, 0, 1, 1, pab, grid_cpu);
104 zeta, 1.0, dh, dh_inv, ra, rab, npts_global,
108 false,
false, 0, 0, 0, 0, 0, zeta, zeta, dh, dh_inv, ra, rab,
109 npts_global,
npts_local, zero3, zero3, radius, 0, 0, 1, 1, grid_ref,
110 hab_ref, pab, forces, virials, NULL, NULL, NULL);
114 const double r2 = ra[0] * ra[0] + ra[1] * ra[1] + ra[2] * ra[2];
115 grid_ref[0] = exp(-2.0 * zeta * r2);
116 hab_ref[0][0] = grid_ref[0] * grid_ref[0];
119 false,
false, 0, 0, 0, 0, 0, zeta, zeta, dh, dh_inv, ra, rab, npts_global,
120 npts_local, zero3, zero3, radius, 0, 0, 1, 1, grid_ref, hab_cpu, pab,
121 forces, virials, NULL, NULL, NULL);
123 double ref_norm = 0.0, max_diff = fabs(hab_cpu[0][0] - hab_ref[0][0]);
124 for (
int i = 0;
i < ng;
i++) {
125 ref_norm = fmax(ref_norm, fabs(grid_ref[
i]));
126 max_diff = fmax(max_diff, fabs(grid_cpu[
i] - grid_ref[
i]));
128 if (ref_norm < 0.5 || max_diff > 1e-12) {
129 printf(
"Sheared-cell test failed: ref_norm = %le, max_diff = %le\n",
139 const double dh_extreme[3][3] = {
140 {0.7443799999999999, 0.0, 0.0}, {0.0, 0.5, 0.0}, {0.73102, 0.0, 0.00023}};
141 const double r_pt = 0.505 * dh_extreme[0][0];
142 const double ra_extreme[3] = {0.0, 0.0, -r_pt};
143 errors +=
run_sheared_case(dh_extreme, ra_extreme, r_pt * (1.0 + 5e-10), 2e-5,
147 const double q[3][3] = {{2.0 / 3.0, 2.0 / 3.0, 1.0 / 3.0},
148 {2.0 / 3.0, -1.0 / 3.0, -2.0 / 3.0},
149 {-1.0 / 3.0, 2.0 / 3.0, -2.0 / 3.0}};
150 const double c[3][3] = {{0.8, 0.0, 0.0}, {0.24, 0.8, 0.0}, {0.8, 0.8, 0.015}};
151 double dh_active[3][3] = {{0.0}};
152 for (
int i = 0;
i < 3;
i++) {
153 for (
int m = 0; m < 3; m++) {
155 c[
i][0] * q[0][m] + c[
i][1] * q[1][m] + c[
i][2] * q[2][m];
158 const double ra_active[3] = {-0.55 * q[2][0], -0.55 * q[2][1],
165int main(
int argc,
char *argv[]) {
169 printf(
"Usage: grid_unittest.x <cp2k-root-dir>\n");
178 errors +=
run_test(argv[1],
"ortho_density_l0000.task");
179 errors +=
run_test(argv[1],
"ortho_density_l0122.task");
180 errors +=
run_test(argv[1],
"ortho_density_l2200.task");
181 errors +=
run_test(argv[1],
"ortho_density_l3300.task");
182 errors +=
run_test(argv[1],
"ortho_density_l3333.task");
183 errors +=
run_test(argv[1],
"ortho_density_l0505.task");
184 errors +=
run_test(argv[1],
"ortho_non_periodic.task");
185 errors +=
run_test(argv[1],
"ortho_tau.task");
186 errors +=
run_test(argv[1],
"general_density.task");
187 errors +=
run_test(argv[1],
"general_tau.task");
188 errors +=
run_test(argv[1],
"general_subpatch0.task");
189 errors +=
run_test(argv[1],
"general_subpatch16.task");
190 errors +=
run_test(argv[1],
"general_overflow.task");
191 errors +=
run_test(argv[1],
"general_skewed.task");
193 errors +=
run_test(argv[1],
"ortho_large_cube.task");
194 errors +=
run_test(argv[1],
"general_large_cube.task");
201 printf(
"\nAll tests have passed :-)\n");
204 printf(
"\nFound %i errors :-(\n", errors);
void cp_mpi_init(int *argc, char ***argv)
Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
void cp_mpi_finalize(void)
Detach from MPI and finalize it only if cp_mpi_init initialized it.
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to dbm_library_print_stats.
static void const int const int i
static void const int const int const int const int const int const double const int const int const int npts_local[3]
void grid_cpu_collocate_pgf_product(const bool orthorhombic, const int border_mask, const enum grid_func func, const int la_max, const int la_min, const int lb_max, const int lb_min, const double zeta, const double zetb, const double rscale, const double dh[3][3], const double dh_inv[3][3], const double ra[3], const double rab[3], const int npts_global[3], const int npts_local[3], const int shift_local[3], const int border_width[3], const double radius, const int o1, const int o2, const int n1, const int n2, const double pab[n2][n1], double *grid)
Public entry point. A thin wrapper with the only purpose of calling write_task_file when DUMP_TASKS =...
void grid_cpu_integrate_pgf_product(const bool orthorhombic, const bool compute_tau, const int border_mask, const int la_max, const int la_min, const int lb_max, const int lb_min, const double zeta, const double zetb, const double dh[3][3], const double dh_inv[3][3], const double ra[3], const double rab[3], const int npts_global[3], const int npts_local[3], const int shift_local[3], const int border_width[3], const double radius, const int o1, const int o2, const int n1, const int n2, const double *grid, double hab[n2][n1], const double pab[n2][n1], double forces[2][3], double virials[2][3][3], double hdab[n2][n1][3], double hadb[n2][n1][3], double a_hdab[n2][n1][3][3])
Integrates a single task. See grid_cpu_integrate.h for details.
void grid_library_finalize(void)
Finalizes the grid library.
void grid_library_init(void)
Initializes the grid library.
void grid_library_print_stats(const int fortran_comm, void(*print_func)(const char *, int, int), const int output_unit)
Prints statistics gathered by the grid library.
void grid_ref_collocate_pgf_product(const bool orthorhombic, const int border_mask, const enum grid_func func, const int la_max, const int la_min, const int lb_max, const int lb_min, const double zeta, const double zetb, const double rscale, const double dh[3][3], const double dh_inv[3][3], const double ra[3], const double rab[3], const int npts_global[3], const int npts_local[3], const int shift_local[3], const int border_width[3], const double radius, const int o1, const int o2, const int n1, const int n2, const double pab[n2][n1], double *grid)
Collocates a single product of primitiv Gaussians. See grid_ref_collocate.h for details.
void grid_ref_integrate_pgf_product(const bool orthorhombic, const bool compute_tau, const int border_mask, const int la_max, const int la_min, const int lb_max, const int lb_min, const double zeta, const double zetb, const double dh[3][3], const double dh_inv[3][3], const double ra[3], const double rab[3], const int npts_global[3], const int npts_local[3], const int shift_local[3], const int border_width[3], const double radius, const int o1, const int o2, const int n1, const int n2, const double *grid, double hab[n2][n1], const double pab[n2][n1], double forces[2][3], double virials[2][3][3], double hdab[n2][n1][3], double hadb[n2][n1][3], double a_hdab[n2][n1][3][3])
Integrates a single task. See grid_ref_integrate.h for details.
bool grid_replay(const char *filename, const int cycles, const bool collocate, const bool batch, const int cycles_per_block, const double tolerance)
Reads a .task file, collocates/integrates it, and compares results. See grid_replay....
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to grid_library_print_stats.
static int run_sheared_case(const double dh[3][3], const double ra[3], const double radius, const double zeta, const bool compare_ref)
Regression tests for general-cell bounds on strongly sheared cells.
static int run_sheared_tests(void)
static int run_test(const char cp2k_root_dir[], const char task_file[])
Unit test for the grid code.
int main()
Unit test of the C-interface provided via libcp2k.h.
void offload_set_chosen_device(int device_id)
Selects the chosen device to be used.
void offload_mempool_stats_print(int fortran_comm, void(*print_func)(const char *, int, int), int output_unit)
Print allocation statistics..