20 printf(
"Unit test starts ...\n");
23 printf(
"Testing cp_c_get_version(): ");
24 char version_str[100];
26 printf(
"%s.\n", version_str);
32 const char *inp_fn =
"H2.inp";
35 f = fopen(inp_fn,
"w");
36 fprintf(f,
"&FORCE_EVAL\n");
37 fprintf(f,
" METHOD Quickstep\n");
38 fprintf(f,
" STRESS_TENSOR ANALYTICAL\n");
39 fprintf(f,
" &DFT\n");
40 fprintf(f,
" BASIS_SET_FILE_NAME BASIS_SET\n");
41 fprintf(f,
" POTENTIAL_FILE_NAME POTENTIAL\n");
43 fprintf(f,
" &MGRID\n");
44 fprintf(f,
" CUTOFF 140\n");
45 fprintf(f,
" &END MGRID\n");
47 fprintf(f,
" EPS_DEFAULT 1.0E-8\n");
48 fprintf(f,
" &END QS\n");
49 fprintf(f,
" &SCF\n");
50 fprintf(f,
" EPS_DIIS 0.1\n");
51 fprintf(f,
" EPS_SCF 1.0E-4\n");
52 fprintf(f,
" IGNORE_CONVERGENCE_FAILURE\n");
53 fprintf(f,
" MAX_DIIS 4\n");
54 fprintf(f,
" MAX_SCF 3\n");
55 fprintf(f,
" SCF_GUESS atomic\n");
56 fprintf(f,
" &END SCF\n");
58 fprintf(f,
" &XC_FUNCTIONAL Pade\n");
59 fprintf(f,
" &END XC_FUNCTIONAL\n");
60 fprintf(f,
" &END XC\n");
61 fprintf(f,
" &END DFT\n");
62 fprintf(f,
" &SUBSYS\n");
63 fprintf(f,
" &CELL\n");
64 fprintf(f,
" ABC 8.0 4.0 4.0\n");
65 fprintf(f,
" &END CELL\n");
66 fprintf(f,
" &COORD\n");
67 fprintf(f,
" H 0.000000 0.000000 0.000000\n");
68 fprintf(f,
" H 1.000000 0.000000 0.000000\n");
69 fprintf(f,
" &END COORD\n");
70 fprintf(f,
" &KIND H\n");
71 fprintf(f,
" BASIS_SET DZV-GTH-PADE\n");
72 fprintf(f,
" POTENTIAL GTH-PADE-q1\n");
73 fprintf(f,
" &END KIND\n");
74 fprintf(f,
" &END SUBSYS\n");
75 fprintf(f,
"&END FORCE_EVAL\n");
76 fprintf(f,
"&GLOBAL\n");
77 fprintf(f,
" PRINT_LEVEL SILENT\n");
78 fprintf(f,
" PROJECT libcp2k_unittest_H2\n");
79 fprintf(f,
"&END GLOBAL\n");
89 if (scf_status != -1) {
90 printf(
"SCF status must be unavailable before calculation\n");
95 if (scf_status != 0) {
96 printf(
"The deliberately truncated SCF must report non-convergence\n");
102 int stress_available = 0;
104 if (!stress_available) {
105 printf(
"Missing analytical stress\n");
108 for (
int i = 0;
i < 3; ++
i) {
109 for (
int j = 0; j < 3; ++j) {
110 if (!isfinite(stress[3 * j +
i]) ||
111 fabs(stress[3 * j +
i] - stress[3 *
i + j]) > 1e-10) {
112 printf(
"Invalid stress tensor\n");
121 printf(
"\n ENERGY: %.12f\n", energy);
122 if (fabs(-1.118912797546392 - energy) / fabs(energy) > 1e-13) {
123 printf(
"Wrong energy\n");
128 double positions[6], cell[9], velocities[6] = {0};
132 if (scf_status != -1)
138 if (scf_status != -1)
143 if (scf_status != -1)
148 const char *run_out =
"libcp2k_unittest_run.out";
154 f = fopen(run_out,
"r");
156 printf(
"run_input did not create its output file\n");
159 fseek(f, 0, SEEK_END);
160 first_size = ftell(f);
166 f = fopen(run_out,
"r");
168 printf(
"run_input output file disappeared\n");
171 fseek(f, 0, SEEK_END);
172 const long second_size = ftell(f);
174 if (first_size <= 0 || second_size <= first_size) {
175 printf(
"run_input did not append output\n");
184 if (fabs(-1.118912797546392 - energy) / fabs(energy) > 1e-13) {
185 printf(
"Wrong energy after run_input\n");
197 printf(
"Unit test finished, found no errors\n");
int cp_mpi_comm_c2f(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_c2f.
void cp_mpi_barrier(const cp_mpi_comm_t comm)
Wrapper around MPI_Barrier; a null communicator is a no-op.
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
static void const int const int i
void cp2k_create_force_env(force_env_t *new_force_env, const char *input_file_path, const char *output_file_path)
Create a new force environment.
void cp2k_run_input_comm(const char *input_file_path, const char *output_file_path, int mpi_comm)
Make a CP2K run with the given input file (custom managed MPI).
void cp2k_run_input(const char *input_file_path, const char *output_file_path)
Make a CP2K run with the given input file.
void cp2k_get_version(char *version_str, int str_length)
Get the CP2K version string.
void cp2k_set_positions(force_env_t force_env, const double *new_pos, int n_el)
Set positions of the particles.
int force_env_t
Definitions for the functions exported in libcp2k.F.
void cp2k_set_velocities(force_env_t force_env, const double *new_vel, int n_el)
Set velocity of the particles.
void cp2k_init(void)
Initialize CP2K, initializing or attaching to MPI as needed.
void cp2k_get_scf_convergence(force_env_t force_env, int *status)
Query convergence of the last Quickstep SCF, including outer/CDFT loops.
void cp2k_finalize(void)
Finalize CP2K and MPI if CP2K initialized it.
void cp2k_get_positions(force_env_t force_env, double *pos, int n_el)
Get the positions of the particles.
void cp2k_get_stress_tensor(force_env_t force_env, double *stress_tensor, int *available)
Get the potential (not kinetic) stress after cp2k_calc_energy_force().
void cp2k_destroy_force_env(force_env_t force_env)
Destroy/cleanup a force environment.
void cp2k_get_cell(force_env_t force_env, const double *cell)
Get the size of the cell.
void cp2k_set_cell(force_env_t force_env, const double *new_cell)
Set the size of the cell.
void cp2k_calc_energy_force(force_env_t force_env)
Calculate energy and forces of the system.
void cp2k_get_potential_energy(force_env_t force_env, double *e_pot)
Get the potential energy of the system.
void cp2k_calc_energy(force_env_t force_env)
Calculate only the energy of the system.
int main()
Unit test of the C-interface provided via libcp2k.h.