LCOV - code coverage report
Current view: top level - src/start - libcp2k_unittest.c (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:2fce0f8) Lines: 64 66 97.0 %
Date: 2024-12-21 06:28:57 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*----------------------------------------------------------------------------*/
       2             : /*  CP2K: A general program to perform molecular dynamics simulations         */
       3             : /*  Copyright 2000-2024 CP2K developers group <https://cp2k.org>              */
       4             : /*                                                                            */
       5             : /*  SPDX-License-Identifier: GPL-2.0-or-later                                 */
       6             : /*----------------------------------------------------------------------------*/
       7             : 
       8             : #include "libcp2k.h"
       9             : #include <math.h>
      10             : #include <stdio.h>
      11             : #include <stdlib.h>
      12             : 
      13             : /*******************************************************************************
      14             :  * \brief Unit test of the C-interface provided via libcp2k.h
      15             :  * \author Ole Schuett
      16             :  ******************************************************************************/
      17           2 : int main() {
      18             : 
      19           2 :   printf("Unit test starts ...\n");
      20             : 
      21             :   // test cp2k_get_version()
      22           2 :   printf("Testing cp_c_get_version(): ");
      23           2 :   char version_str[100];
      24           2 :   cp2k_get_version(version_str, 100);
      25           2 :   printf("%s.\n", version_str);
      26             : 
      27             :   // create simple input file
      28           2 :   const char *inp_fn = "H2.inp";
      29           2 :   FILE *f = fopen(inp_fn, "w");
      30           2 :   fprintf(f, "&FORCE_EVAL\n");
      31           2 :   fprintf(f, "  METHOD Quickstep\n");
      32           2 :   fprintf(f, "  &DFT\n");
      33           2 :   fprintf(f, "    BASIS_SET_FILE_NAME BASIS_SET\n");
      34           2 :   fprintf(f, "    POTENTIAL_FILE_NAME POTENTIAL\n");
      35           2 :   fprintf(f, "    LSD\n");
      36           2 :   fprintf(f, "    &MGRID\n");
      37           2 :   fprintf(f, "      CUTOFF 140\n");
      38           2 :   fprintf(f, "    &END MGRID\n");
      39           2 :   fprintf(f, "    &QS\n");
      40           2 :   fprintf(f, "      EPS_DEFAULT 1.0E-8\n");
      41           2 :   fprintf(f, "    &END QS\n");
      42           2 :   fprintf(f, "    &SCF\n");
      43           2 :   fprintf(f, "      EPS_DIIS 0.1\n");
      44           2 :   fprintf(f, "      EPS_SCF 1.0E-4\n");
      45           2 :   fprintf(f, "      IGNORE_CONVERGENCE_FAILURE\n");
      46           2 :   fprintf(f, "      MAX_DIIS 4\n");
      47           2 :   fprintf(f, "      MAX_SCF 3\n");
      48           2 :   fprintf(f, "      SCF_GUESS atomic\n");
      49           2 :   fprintf(f, "    &END SCF\n");
      50           2 :   fprintf(f, "    &XC\n");
      51           2 :   fprintf(f, "      &XC_FUNCTIONAL Pade\n");
      52           2 :   fprintf(f, "      &END XC_FUNCTIONAL\n");
      53           2 :   fprintf(f, "    &END XC\n");
      54           2 :   fprintf(f, "  &END DFT\n");
      55           2 :   fprintf(f, "  &SUBSYS\n");
      56           2 :   fprintf(f, "    &CELL\n");
      57           2 :   fprintf(f, "      ABC 8.0 4.0 4.0\n");
      58           2 :   fprintf(f, "    &END CELL\n");
      59           2 :   fprintf(f, "    &COORD\n");
      60           2 :   fprintf(f, "    H     0.000000  0.000000  0.000000\n");
      61           2 :   fprintf(f, "    H     1.000000  0.000000  0.000000\n");
      62           2 :   fprintf(f, "    &END COORD\n");
      63           2 :   fprintf(f, "    &KIND H\n");
      64           2 :   fprintf(f, "      BASIS_SET DZV-GTH-PADE\n");
      65           2 :   fprintf(f, "      POTENTIAL GTH-PADE-q1\n");
      66           2 :   fprintf(f, "    &END KIND\n");
      67           2 :   fprintf(f, "  &END SUBSYS\n");
      68           2 :   fprintf(f, "&END FORCE_EVAL\n");
      69           2 :   fprintf(f, "&GLOBAL\n");
      70           2 :   fprintf(f, "  PRINT_LEVEL SILENT\n");
      71           2 :   fprintf(f, "  PROJECT libcp2k_unittest_H2\n");
      72           2 :   fprintf(f, "&END GLOBAL\n");
      73           2 :   fclose(f);
      74             : 
      75             :   // use input file to create a force environment
      76           2 :   force_env_t force_env;
      77           2 :   cp2k_init();
      78           2 :   cp2k_create_force_env(&force_env, inp_fn, "__STD_OUT__");
      79           2 :   cp2k_calc_energy_force(force_env);
      80             : 
      81             :   // check energy
      82           2 :   double energy;
      83           2 :   cp2k_get_potential_energy(force_env, &energy);
      84           2 :   printf("\n ENERGY: %.12f\n", energy);
      85           2 :   if (fabs(-1.118912797546392 - energy) / fabs(energy) > 1e-13) {
      86           0 :     printf("Wrong energy\n");
      87           0 :     return (-1);
      88             :   }
      89             : 
      90             :   // clean up
      91           2 :   cp2k_finalize();
      92           2 :   remove(inp_fn);
      93             : 
      94           2 :   printf("Unit test finished, found no errors\n");
      95           2 :   return (0);
      96             : }
      97             : 
      98             : // EOF

Generated by: LCOV version 1.15