(git:27c966b)
Loading...
Searching...
No Matches
grid_unittest.c
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7#include "../mpiwrap/cp_mpi.h"
10#include "common/grid_library.h"
13#include "grid_replay.h"
16
17#include <math.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22/*******************************************************************************
23 * \brief Wrapper for printf, passed to grid_library_print_stats.
24 * \author Ole Schuett
25 ******************************************************************************/
26static void print_func(const char *msg, int msglen, int output_unit) {
27 (void)msglen; // mark used
28 if (output_unit == 0) { // i.e. my_rank == 0
29 printf("%s", msg);
30 }
31}
32
33/*******************************************************************************
34 * \brief Unit test for the grid code.
35 * \author Ole Schuett
36 ******************************************************************************/
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");
40 abort();
41 }
42
43 char filename[1024];
44 strcpy(filename, cp2k_root_dir);
45 if (filename[strlen(filename) - 1] != '/') {
46 strcat(filename, "/");
47 }
48
49 strcat(filename, "src/grid/sample_tasks/");
50 strcat(filename, task_file);
51
52 const double tolerance = 1e-12;
53 int errors = 0;
54 for (int icol = 0; icol < 2; icol++) {
55 for (int ibatch = 0; ibatch < 2; ibatch++) {
56 const bool success =
57 grid_replay(filename, 1, icol == 1, ibatch == 1, 1, tolerance);
58 if (!success) {
59 printf("Max diff too high, test failed.\n\n");
60 errors++;
61 }
62 }
63 }
64 return errors;
65}
66
67/*******************************************************************************
68 * \brief Regression tests for general-cell bounds on strongly sheared cells.
69 ******************************************************************************/
70static int run_sheared_case(const double dh[3][3], const double ra[3],
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,
91 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}}};
98
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);
102 if (compare_ref) {
103 grid_ref_collocate_pgf_product(false, 0, GRID_FUNC_AB, 0, 0, 0, 0, zeta,
104 zeta, 1.0, dh, dh_inv, ra, rab, npts_global,
105 npts_local, zero3, zero3, radius, 0, 0, 1, 1,
106 pab, grid_ref);
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);
111 } else {
112 // Only (0, 0, 0) maps into npts_local={1,1,1}; avoids stack VLA in grid_ref
113 // when the sheared sphere spans thousands of lattice indices.
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];
117 }
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);
122
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]));
127 }
128 if (ref_norm < 0.5 || max_diff > 1e-12) {
129 printf("Sheared-cell test failed: ref_norm = %le, max_diff = %le\n",
130 ref_norm, max_diff);
131 return 1;
132 }
133 return 0;
134}
135
136static int run_sheared_tests(void) {
137 int errors = 0;
138 // 1. Strongly sheared cell with grazing point (0, 0, 0) (sin^2 < 1e-6).
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,
144 false);
145
146 // 2. Rotated 3D sheared cell inside the active j-bound regime (sin^2 > 1e-6).
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++) {
154 dh_active[i][m] =
155 c[i][0] * q[0][m] + c[i][1] * q[1][m] + c[i][2] * q[2][m];
156 }
157 }
158 const double ra_active[3] = {-0.55 * q[2][0], -0.55 * q[2][1],
159 -0.55 * q[2][2]};
160 errors +=
161 run_sheared_case(dh_active, ra_active, 0.55 * (1.0 + 1e-9), 5e-4, true);
162 return errors;
163}
164
165int main(int argc, char *argv[]) {
166 cp_mpi_init(&argc, &argv);
167
168 if (argc != 2) {
169 printf("Usage: grid_unittest.x <cp2k-root-dir>\n");
171 return 1;
172 }
173
176
177 int errors = 0;
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");
192 // Exercise shared-memory limits in GPU collocation and integration.
193 errors += run_test(argv[1], "ortho_large_cube.task");
194 errors += run_test(argv[1], "general_large_cube.task");
195 errors += run_sheared_tests();
196
197 if (errors == 0) {
198 grid_library_print_stats(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
199 offload_mempool_stats_print(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
201 printf("\nAll tests have passed :-)\n");
202 } else {
204 printf("\nFound %i errors :-(\n", errors);
205 }
206
208
209 return errors;
210}
211
212// EOF
void cp_mpi_init(int *argc, char ***argv)
Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
Definition cp_mpi.c:78
void cp_mpi_finalize(void)
Detach from MPI and finalize it only if cp_mpi_init initialized it.
Definition cp_mpi.c:112
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to dbm_library_print_stats.
Definition dbm_miniapp.c:26
@ GRID_FUNC_AB
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..