LCOV - code coverage report
Current view: top level - src/grid - grid_unittest.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 90.8 % 120 109
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 5 5

            Line data    Source code
       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"
       8              : #include "../offload/offload_library.h"
       9              : #include "../offload/offload_mempool.h"
      10              : #include "common/grid_library.h"
      11              : #include "cpu/grid_cpu_collocate.h"
      12              : #include "cpu/grid_cpu_integrate.h"
      13              : #include "grid_replay.h"
      14              : #include "ref/grid_ref_collocate.h"
      15              : #include "ref/grid_ref_integrate.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              :  ******************************************************************************/
      26           66 : static void print_func(const char *msg, int msglen, int output_unit) {
      27           66 :   (void)msglen;           // mark used
      28           66 :   if (output_unit == 0) { // i.e. my_rank == 0
      29           66 :     printf("%s", msg);
      30              :   }
      31           66 : }
      32              : 
      33              : /*******************************************************************************
      34              :  * \brief Unit test for the grid code.
      35              :  * \author Ole Schuett
      36              :  ******************************************************************************/
      37           32 : static int run_test(const char cp2k_root_dir[], const char task_file[]) {
      38           32 :   if (strlen(cp2k_root_dir) > 512) {
      39            0 :     fprintf(stderr, "Error: cp2k_root_dir too long.\n");
      40            0 :     abort();
      41              :   }
      42              : 
      43           32 :   char filename[1024];
      44           32 :   strcpy(filename, cp2k_root_dir);
      45           32 :   if (filename[strlen(filename) - 1] != '/') {
      46           32 :     strcat(filename, "/");
      47              :   }
      48              : 
      49           32 :   strcat(filename, "src/grid/sample_tasks/");
      50           32 :   strcat(filename, task_file);
      51              : 
      52           32 :   const double tolerance = 1e-12;
      53           32 :   int errors = 0;
      54           96 :   for (int icol = 0; icol < 2; icol++) {
      55          192 :     for (int ibatch = 0; ibatch < 2; ibatch++) {
      56          128 :       const bool success =
      57          128 :           grid_replay(filename, 1, icol == 1, ibatch == 1, 1, tolerance);
      58          128 :       if (!success) {
      59            0 :         printf("Max diff too high, test failed.\n\n");
      60            0 :         errors++;
      61              :       }
      62              :     }
      63              :   }
      64           32 :   return errors;
      65              : }
      66              : 
      67              : /*******************************************************************************
      68              :  * \brief Regression tests for general-cell bounds on strongly sheared cells.
      69              :  ******************************************************************************/
      70            4 : static 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            4 :   const double det = dh[0][0] * (dh[1][1] * dh[2][2] - dh[1][2] * dh[2][1]) -
      74            4 :                      dh[0][1] * (dh[1][0] * dh[2][2] - dh[1][2] * dh[2][0]) +
      75            4 :                      dh[0][2] * (dh[1][0] * dh[2][1] - dh[1][1] * dh[2][0]);
      76            4 :   const double inv_det = 1.0 / det;
      77            4 :   const double dh_inv[3][3] = {
      78            4 :       {(dh[1][1] * dh[2][2] - dh[1][2] * dh[2][1]) * inv_det,
      79            4 :        (dh[0][2] * dh[2][1] - dh[0][1] * dh[2][2]) * inv_det,
      80            4 :        (dh[0][1] * dh[1][2] - dh[0][2] * dh[1][1]) * inv_det},
      81            4 :       {(dh[1][2] * dh[2][0] - dh[1][0] * dh[2][2]) * inv_det,
      82            4 :        (dh[0][0] * dh[2][2] - dh[0][2] * dh[2][0]) * inv_det,
      83            4 :        (dh[0][2] * dh[1][0] - dh[0][0] * dh[1][2]) * inv_det},
      84            4 :       {(dh[1][0] * dh[2][1] - dh[1][1] * dh[2][0]) * inv_det,
      85            4 :        (dh[0][1] * dh[2][0] - dh[0][0] * dh[2][1]) * inv_det,
      86            4 :        (dh[0][0] * dh[1][1] - dh[0][1] * dh[1][0]) * inv_det}};
      87            4 :   const double rab[3] = {0.0, 0.0, 0.0};
      88            4 :   const int npts_global[3] = {compare_ref ? 4 : 8000, 4,
      89            4 :                               compare_ref ? 4 : 8000};
      90            4 :   const int npts_local[3] = {compare_ref ? 4 : 1, compare_ref ? 4 : 1,
      91              :                              compare_ref ? 4 : 1};
      92            4 :   const int ng = compare_ref ? 64 : 1;
      93            4 :   const int zero3[3] = {0, 0, 0};
      94            4 :   const double pab[1][1] = {{1.0}};
      95            4 :   double grid_ref[64] = {0.0}, grid_cpu[64] = {0.0};
      96            4 :   double hab_ref[1][1] = {{0.0}}, hab_cpu[1][1] = {{0.0}};
      97            4 :   double forces[2][3] = {{0.0}}, virials[2][3][3] = {{{0.0}}};
      98              :   // Optional derivative outputs; dummy arrays keep GCC's -Wnonnull happy.
      99            4 :   double dummy_hdab[1][1][3] = {{{0.0}}};
     100            4 :   double dummy_hadb[1][1][3] = {{{0.0}}};
     101            4 :   double dummy_a_hdab[1][1][3][3] = {{{{0.0}}}};
     102              : 
     103            4 :   grid_cpu_collocate_pgf_product(
     104              :       false, 0, GRID_FUNC_AB, 0, 0, 0, 0, zeta, zeta, 1.0, dh, dh_inv, ra, rab,
     105              :       npts_global, npts_local, zero3, zero3, radius, 0, 0, 1, 1, pab, grid_cpu);
     106            4 :   if (compare_ref) {
     107            2 :     grid_ref_collocate_pgf_product(false, 0, GRID_FUNC_AB, 0, 0, 0, 0, zeta,
     108              :                                    zeta, 1.0, dh, dh_inv, ra, rab, npts_global,
     109              :                                    npts_local, zero3, zero3, radius, 0, 0, 1, 1,
     110              :                                    pab, grid_ref);
     111            2 :     grid_ref_integrate_pgf_product(
     112              :         false, false, 0, 0, 0, 0, 0, zeta, zeta, dh, dh_inv, ra, rab,
     113              :         npts_global, npts_local, zero3, zero3, radius, 0, 0, 1, 1, grid_ref,
     114              :         hab_ref, pab, forces, virials, dummy_hdab, dummy_hadb, dummy_a_hdab);
     115              :   } else {
     116              :     // Only (0, 0, 0) maps into npts_local={1,1,1}; avoids stack VLA in grid_ref
     117              :     // when the sheared sphere spans thousands of lattice indices.
     118            2 :     const double r2 = ra[0] * ra[0] + ra[1] * ra[1] + ra[2] * ra[2];
     119            2 :     grid_ref[0] = exp(-2.0 * zeta * r2);
     120            2 :     hab_ref[0][0] = grid_ref[0] * grid_ref[0];
     121              :   }
     122            4 :   grid_cpu_integrate_pgf_product(
     123              :       false, false, 0, 0, 0, 0, 0, zeta, zeta, dh, dh_inv, ra, rab, npts_global,
     124              :       npts_local, zero3, zero3, radius, 0, 0, 1, 1, grid_ref, hab_cpu, pab,
     125              :       forces, virials, dummy_hdab, dummy_hadb, dummy_a_hdab);
     126              : 
     127            4 :   double ref_norm = 0.0, max_diff = fabs(hab_cpu[0][0] - hab_ref[0][0]);
     128          134 :   for (int i = 0; i < ng; i++) {
     129          130 :     ref_norm = fmax(ref_norm, fabs(grid_ref[i]));
     130          130 :     max_diff = fmax(max_diff, fabs(grid_cpu[i] - grid_ref[i]));
     131              :   }
     132            4 :   if (ref_norm < 0.5 || max_diff > 1e-12) {
     133            0 :     printf("Sheared-cell test failed: ref_norm = %le, max_diff = %le\n",
     134              :            ref_norm, max_diff);
     135            0 :     return 1;
     136              :   }
     137              :   return 0;
     138              : }
     139              : 
     140            2 : static int run_sheared_tests(void) {
     141            2 :   int errors = 0;
     142              :   // 1. Strongly sheared cell with grazing point (0, 0, 0) (sin^2 < 1e-6).
     143            2 :   const double dh_extreme[3][3] = {
     144              :       {0.7443799999999999, 0.0, 0.0}, {0.0, 0.5, 0.0}, {0.73102, 0.0, 0.00023}};
     145            2 :   const double r_pt = 0.505 * dh_extreme[0][0];
     146            2 :   const double ra_extreme[3] = {0.0, 0.0, -r_pt};
     147            2 :   errors += run_sheared_case(dh_extreme, ra_extreme, r_pt * (1.0 + 5e-10), 2e-5,
     148              :                              false);
     149              : 
     150              :   // 2. Rotated 3D sheared cell inside the active j-bound regime (sin^2 > 1e-6).
     151            2 :   const double q[3][3] = {{2.0 / 3.0, 2.0 / 3.0, 1.0 / 3.0},
     152              :                           {2.0 / 3.0, -1.0 / 3.0, -2.0 / 3.0},
     153              :                           {-1.0 / 3.0, 2.0 / 3.0, -2.0 / 3.0}};
     154            2 :   const double c[3][3] = {{0.8, 0.0, 0.0}, {0.24, 0.8, 0.0}, {0.8, 0.8, 0.015}};
     155            2 :   double dh_active[3][3] = {{0.0}};
     156            8 :   for (int i = 0; i < 3; i++) {
     157           24 :     for (int m = 0; m < 3; m++) {
     158           18 :       dh_active[i][m] =
     159           18 :           c[i][0] * q[0][m] + c[i][1] * q[1][m] + c[i][2] * q[2][m];
     160              :     }
     161              :   }
     162            2 :   const double ra_active[3] = {-0.55 * q[2][0], -0.55 * q[2][1],
     163              :                                -0.55 * q[2][2]};
     164            4 :   errors +=
     165            2 :       run_sheared_case(dh_active, ra_active, 0.55 * (1.0 + 1e-9), 5e-4, true);
     166            2 :   return errors;
     167              : }
     168              : 
     169            2 : int main(int argc, char *argv[]) {
     170            2 :   cp_mpi_init(&argc, &argv);
     171              : 
     172            2 :   if (argc != 2) {
     173            0 :     printf("Usage: grid_unittest.x <cp2k-root-dir>\n");
     174            0 :     cp_mpi_finalize();
     175            0 :     return 1;
     176              :   }
     177              : 
     178            2 :   offload_set_chosen_device(0);
     179            2 :   grid_library_init();
     180              : 
     181            2 :   int errors = 0;
     182            2 :   errors += run_test(argv[1], "ortho_density_l0000.task");
     183            2 :   errors += run_test(argv[1], "ortho_density_l0122.task");
     184            2 :   errors += run_test(argv[1], "ortho_density_l2200.task");
     185            2 :   errors += run_test(argv[1], "ortho_density_l3300.task");
     186            2 :   errors += run_test(argv[1], "ortho_density_l3333.task");
     187            2 :   errors += run_test(argv[1], "ortho_density_l0505.task");
     188            2 :   errors += run_test(argv[1], "ortho_non_periodic.task");
     189            2 :   errors += run_test(argv[1], "ortho_tau.task");
     190            2 :   errors += run_test(argv[1], "general_density.task");
     191            2 :   errors += run_test(argv[1], "general_tau.task");
     192            2 :   errors += run_test(argv[1], "general_subpatch0.task");
     193            2 :   errors += run_test(argv[1], "general_subpatch16.task");
     194            2 :   errors += run_test(argv[1], "general_overflow.task");
     195            2 :   errors += run_test(argv[1], "general_skewed.task");
     196              :   // Exercise shared-memory limits in GPU collocation and integration.
     197            2 :   errors += run_test(argv[1], "ortho_large_cube.task");
     198            2 :   errors += run_test(argv[1], "general_large_cube.task");
     199            2 :   errors += run_sheared_tests();
     200              : 
     201            2 :   if (errors == 0) {
     202            2 :     grid_library_print_stats(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
     203            2 :     offload_mempool_stats_print(0 /*fortran_comm*/, &print_func, 0 /*rank*/);
     204            2 :     grid_library_finalize();
     205            2 :     printf("\nAll tests have passed :-)\n");
     206              :   } else {
     207            0 :     grid_library_finalize();
     208            0 :     printf("\nFound %i errors :-(\n", errors);
     209              :   }
     210              : 
     211            2 :   cp_mpi_finalize();
     212              : 
     213            2 :   return errors;
     214              : }
     215              : 
     216              : // EOF
        

Generated by: LCOV version 2.0-1