(git:cd590b0)
Loading...
Searching...
No Matches
libcp2k.h
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: GPL-2.0-or-later */
6/*----------------------------------------------------------------------------*/
7
8#include <stdbool.h>
9
10/*******************************************************************************
11 * \brief Definitions for the functions exported in libcp2k.F
12 * \author Mohammad Hossein Bani-Hashemian
13 ******************************************************************************/
14
15#ifndef LIBCP2K_H
16#define LIBCP2K_H
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22typedef int force_env_t;
23
24/*******************************************************************************
25 * \brief Get the CP2K version string
26 * \param version_str The buffer to write the version string into
27 * \param str_length The size of the buffer (must be large enough)
28 ******************************************************************************/
29void cp2k_get_version(char *version_str, int str_length);
30
31/*******************************************************************************
32 * \brief Initialize CP2K, initializing or attaching to MPI as needed
33 * \warning You are supposed to call cp2k_finalize() before exiting the program.
34 ******************************************************************************/
35void cp2k_init(void);
36
37/*******************************************************************************
38 * \brief Initialize CP2K without initializing MPI (on MPI_COMM_WORLD)
39 * \warning You are supposed to call cp2k_finalize_without_mpi() before exiting
40 * the program.
41 ******************************************************************************/
43
44/*******************************************************************************
45 * \brief Initialize CP2K without initializing MPI on the given comm
46 * \warning You are supposed to call cp2k_finalize_without_mpi() before exiting
47 * the program.
48 * \param mpi_comm Fortran MPI communicator if MPI is not managed by CP2K
49 ******************************************************************************/
50void cp2k_init_without_mpi_comm(int mpi_comm);
51
52/*******************************************************************************
53 * \brief Finalize CP2K and MPI if CP2K initialized it
54 ******************************************************************************/
55void cp2k_finalize(void);
56
57/*******************************************************************************
58 * \brief Finalize CP2K without finalizing MPI
59 ******************************************************************************/
61
62/*******************************************************************************
63 * \brief Create a new force environment
64 * \param new_force_env the created force environment
65 * \param input_file_path Path to a CP2K input file
66 * \param output_file_path Path to a file where CP2K is going to append its
67 * output (created if non-existent)
68 * \warning You are supposed to call cp2k_destroy_force_env() to cleanup,
69 * before cp2k_finalize().
70 ******************************************************************************/
72 const char *input_file_path,
73 const char *output_file_path);
74
75/*******************************************************************************
76 * \brief Create a new force environment (custom managed MPI)
77 * \param new_force_env the created force environment
78 * \param input_file_path Path to a CP2K input file
79 * \param output_file_path Path to a file where CP2K is will write its output.
80 * Will be created if not existent, otherwise appended.
81 * \param mpi_comm Fortran MPI communicator if MPI is not managed by CP2K
82 * \warning You are supposed to call cp2k_destroy_force_env() to cleanup,
83 * before cp2k_finalize().
84 ******************************************************************************/
86 const char *input_file_path,
87 const char *output_file_path, int mpi_comm);
88
89/*******************************************************************************
90 * \brief Destroy/cleanup a force environment
91 * \param force_env the force environment
92 ******************************************************************************/
94
95/*******************************************************************************
96 * \brief Set positions of the particles
97 * \param force_env the force environment
98 * \param new_pos Array containing the new positions of the particles
99 * \param n_el Size of the new_pos array
100 ******************************************************************************/
101void cp2k_set_positions(force_env_t force_env, const double *new_pos, int n_el);
102
103/*******************************************************************************
104 * \brief Set velocity of the particles
105 * \param force_env the force environment
106 * \param new_vel Array containing the new velocities of the particles
107 * \param n_el Size of the new_vel array
108 ******************************************************************************/
109void cp2k_set_velocities(force_env_t force_env, const double *new_vel,
110 int n_el);
111
112/*******************************************************************************
113 * \brief Set the size of the cell
114 * \param force_env the force environment
115 * \param new_cell Array containing the new cell
116 ******************************************************************************/
117void cp2k_set_cell(force_env_t force_env, const double *new_cell);
118
119/*******************************************************************************
120 * \brief Get an arbitrary result as 1D array from CP2K
121 * \param force_env the force environment
122 * \param description The string tag of the result
123 * \param results Pre-allocated array
124 * \param n_el size of the results array
125 ******************************************************************************/
126void cp2k_get_result(force_env_t force_env, const char *description,
127 double *result, int n_el);
128
129/*******************************************************************************
130 * \brief Get the number of atoms
131 * \param force_env the force environment
132 * \param natom The number of atoms
133 ******************************************************************************/
134void cp2k_get_natom(force_env_t force_env, int *natom);
135
136/*******************************************************************************
137 * \brief Get the number of particles
138 * \param force_env the force environment
139 * \param nparticle The number of particles
140 ******************************************************************************/
141void cp2k_get_nparticle(force_env_t force_env, int *nparticle);
142
143/*******************************************************************************
144 * \brief Get the positions of the particles
145 * \param force_env the force environment
146 * \param pos Pre-allocated array of at least 3*nparticle elements.
147 * Use cp2k_get_nparticle() to get the number of particles.
148 * \param n_el Size of the force array
149 ******************************************************************************/
150void cp2k_get_positions(force_env_t force_env, double *pos, int n_el);
151
152/*******************************************************************************
153 * \brief Get the forces for the particles
154 * \param force_env the force environment
155 * \param force Pre-allocated array of at least 3*nparticle elements.
156 * Use cp2k_get_nparticle() to get the number of particles.
157 * \param n_el Size of the force array
158 ******************************************************************************/
159void cp2k_get_forces(force_env_t force_env, double *force, int n_el);
160
161/*******************************************************************************
162 * \brief Get the potential (not kinetic) stress after cp2k_calc_energy_force().
163 * \param force_env the force environment
164 * \param stress_tensor Nine doubles in column-major order, in hartree/bohr^3.
165 * CP2K uses pressure-positive stress: virial = stress * cell volume.
166 * This is the opposite sign to the usual tensile-positive convention.
167 * \param available Set to 1 if STRESS_TENSOR was enabled, otherwise 0.
168 * When unavailable the tensor is zero, NOT a computed zero stress.
169 ******************************************************************************/
170void cp2k_get_stress_tensor(force_env_t force_env, double *stress_tensor,
171 int *available);
172
173/*******************************************************************************
174 * \brief Get the potential energy of the system
175 * \param force_env the force environment
176 * \param e_pot The potential energy
177 ******************************************************************************/
178void cp2k_get_potential_energy(force_env_t force_env, double *e_pot);
179
180/*******************************************************************************
181 * \brief Get the size of the cell
182 * \param force_env the force environment
183 * \param cell Array containing the cell
184 ******************************************************************************/
185void cp2k_get_cell(force_env_t force_env, const double *cell);
186
187/*******************************************************************************
188 * \brief Get the size of the qmmm cell
189 * \param force_env the force environment
190 * \param cell Array containing the qmmm cell
191 ******************************************************************************/
192void cp2k_get_qmmm_cell(force_env_t force_env, const double *cell);
193
194/*******************************************************************************
195 * \brief Calculate energy and forces of the system
196 * \param force_env the force environment
197 ******************************************************************************/
199
200/*******************************************************************************
201 * \brief Calculate only the energy of the system
202 * \param force_env the force environment
203 ******************************************************************************/
205
206/*******************************************************************************
207 * \brief Query convergence of the last Quickstep SCF, including outer/CDFT
208 * loops
209 * \param force_env the force environment
210 * \param status -1 if unavailable, 0 if not converged, 1 if converged
211 * \note Unavailable before calculation or after changing positions, cell or
212 * velocities, for non-Quickstep methods, and for alternative solvers
213 * (e.g. LS-SCF, ALMO, RTP, non-SCF or MAX_SCF 0). This is not a
214 * convergence certificate for post-SCF methods, geometry optimization or MD.
215 * IGNORE_CONVERGENCE_FAILURE allows an unconverged SCF to return; this
216 * query does not prevent native CP2K aborts when that keyword is absent.
217 ******************************************************************************/
218void cp2k_get_scf_convergence(force_env_t force_env, int *status);
219
220/*******************************************************************************
221 * \brief Make a CP2K run with the given input file
222 * \param input_file_path Path to a CP2K input file
223 * \param output_file_path Path to a file where CP2K is going to append its
224 * output (created if non-existent)
225 ******************************************************************************/
226void cp2k_run_input(const char *input_file_path, const char *output_file_path);
227
228/*******************************************************************************
229 * \brief Make a CP2K run with the given input file (custom managed MPI)
230 * \param input_file_path Path to a CP2K input file
231 * \param output_file_path Path to a file where CP2K is going to append its
232 * output (created if non-existent)
233 * \param mpi_comm Fortran MPI communicator if MPI is not managed by CP2K
234 ******************************************************************************/
235void cp2k_run_input_comm(const char *input_file_path,
236 const char *output_file_path, int mpi_comm);
237
238/*******************************************************************************
239 * \brief Transport parameters read from a CP2K input file.
240 * This definition matches the respective type definition in the
241 * transport_env_types module
242 ******************************************************************************/
297
298/*******************************************************************************
299 * \brief CP2K's C-interoperable CSR matrix
300 * This definition matches the respective type definition in the
301 * transport_env_types module
302 ******************************************************************************/
316
317/*******************************************************************************
318 * \brief Function pointer type for the externally evaluated density matrix
319 * Function pointer type pointing to a C routine that takes the S and H
320 * matrices as input and outputs a P matrix.
321 *
322 * Function definition example:
323 * \code{.c}
324 * void c_scf_method(
325 * cp2k_transport_parameters cp2k_transport_params,
326 * cp2k_csr_interop_type S,
327 * cp2k_csr_interop_type KS,
328 * cp2k_csr_interop_type* P,
329 * cp2k_csr_interop_type* PImag
330 * );
331 * \endcode
332 * \sa cp2k_transport_parameters, cp2k_csr_interop_type
333 ******************************************************************************/
335 cp2k_transport_parameters, // Transport parameters
336 cp2k_csr_interop_type, // S-Matrix
337 cp2k_csr_interop_type, // H-Matrix
338 cp2k_csr_interop_type *, // P-Matrix
339 cp2k_csr_interop_type * // PImag-Matrix
340);
341
342/*******************************************************************************
343 * \brief Set the function callback for the externally evaluated density matrix
344 ******************************************************************************/
347
348/*******************************************************************************
349 * \brief Get the number of molecular orbitals in the active space
350 * \param force_env the force environment
351 * \returns The number of elements or -1 if unavailable
352 ******************************************************************************/
354
355/*******************************************************************************
356 * \brief Get the Fock submatrix for the active space
357 * \param force_env the force environment
358 * \param buf Pre-allocated array of at least mo_count^2 elements.
359 * Use `cp2k_active_space_get_mo_count()` to get the number of
360 * molecular orbitals.
361 * \param buf_len Size of the buf array
362 * \returns The number of elements written to buf or -1 if unavailable
363 ******************************************************************************/
364long int cp2k_active_space_get_fock_sub(force_env_t force_env, double *buf,
365 long int buf_len);
366
367/*******************************************************************************
368 * \brief Get the number of non-zero elements in the ERI matrix
369 * \param force_env the force environment
370 * \returns The number of elements or -1 if unavailable
371 ******************************************************************************/
373
374/*******************************************************************************
375 * \brief Get the non-zero elements of the ERI matrix
376 * The buf_coords will contain the coordinates in the format
377 * `[i1, j1, k1, l1, i2, j2, k2, l2, ... ]`.
378 *
379 * \param force_env the force environment
380 * \param buf_coords Pre-allocated array of at least 4*nze_count elements.
381 * Use `cp2k_active_space_get_eri_nze_count()` to get the
382 * number of non-zero elements.
383 * \param buf_coords_len Size of the buf_coords array
384 * \param buf_values Pre-allocated array of at least nze_count elements.
385 * Use `cp2k_active_space_get_eri_nze_count()` to get the
386 * number of non-zero elements.
387 * \param buf_values_len Size of the buf_values array
388 * \returns The number of elements written to buf_values or -1 if unavailable
389 ******************************************************************************/
390int cp2k_active_space_get_eri(force_env_t force_env, int *buf_coords,
391 long int buf_coords_len, double *buf_values,
392 long int buf_values_len);
393
394#ifdef __cplusplus
395}
396#endif
397
398#endif
void cp2k_init_without_mpi(void)
Initialize CP2K without initializing MPI (on MPI_COMM_WORLD).
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.
int cp2k_active_space_get_mo_count(force_env_t force_env)
Get the number of molecular orbitals in the active space.
void cp2k_get_qmmm_cell(force_env_t force_env, const double *cell)
Get the size of the qmmm cell.
void(* ext_method_callback_f_ptr)(cp2k_transport_parameters, cp2k_csr_interop_type, cp2k_csr_interop_type, cp2k_csr_interop_type *, cp2k_csr_interop_type *)
Function pointer type for the externally evaluated density matrix Function pointer type pointing to a...
Definition libcp2k.h:334
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_transport_set_callback(force_env_t force_env, ext_method_callback_f_ptr func)
Set the function callback for the externally evaluated density matrix.
void cp2k_get_forces(force_env_t force_env, double *force, int n_el)
Get the forces for the particles.
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.
Definition libcp2k.h:22
long int cp2k_active_space_get_fock_sub(force_env_t force_env, double *buf, long int buf_len)
Get the Fock submatrix for the active space.
void cp2k_set_velocities(force_env_t force_env, const double *new_vel, int n_el)
Set velocity of the particles.
void cp2k_get_natom(force_env_t force_env, int *natom)
Get the number of atoms.
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.
int cp2k_active_space_get_eri(force_env_t force_env, int *buf_coords, long int buf_coords_len, double *buf_values, long int buf_values_len)
Get the non-zero elements of the ERI matrix The buf_coords will contain the coordinates in the format...
long int cp2k_active_space_get_eri_nze_count(force_env_t force_env)
Get the number of non-zero elements in the ERI matrix.
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_finalize_without_mpi(void)
Finalize CP2K without finalizing MPI.
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_get_nparticle(force_env_t force_env, int *nparticle)
Get the number of particles.
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_get_result(force_env_t force_env, const char *description, double *result, int n_el)
Get an arbitrary result as 1D array from CP2K.
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.
void cp2k_init_without_mpi_comm(int mpi_comm)
Initialize CP2K without initializing MPI on the given comm.
void cp2k_create_force_env_comm(force_env_t *new_force_env, const char *input_file_path, const char *output_file_path, int mpi_comm)
Create a new force environment (custom managed MPI).
CP2K's C-interoperable CSR matrix This definition matches the respective type definition in the trans...
Definition libcp2k.h:303
double * nzvals_local
Definition libcp2k.h:314
Transport parameters read from a CP2K input file. This definition matches the respective type definit...
Definition libcp2k.h:243
double eps_singularity_curvatures
Definition libcp2k.h:280