(git:cd590b0)
Loading...
Searching...
No Matches
libcp2k.F
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!--------------------------------------------------------------------------------------------------!
9! IMPORTANT: Update libcp2k.h when you add, remove or change a function in this file. !
10!--------------------------------------------------------------------------------------------------!
11
12! **************************************************************************************************
13!> \brief CP2K C/C++ interface
14!> \par History
15!> 12.2012 created [Hossein Bani-Hashemian]
16!> 04.2016 restructured [Hossein Bani-Hashemian, Ole Schuett]
17!> 03.2018 added Active Space functions [Tiziano Mueller]
18!> \author Mohammad Hossein Bani-Hashemian
19! **************************************************************************************************
20MODULE libcp2k
21 USE iso_c_binding, ONLY: c_char,&
22 c_double,&
23 c_funptr,&
24 c_int,&
25 c_long,&
26 c_null_char
27 USE cp2k_info, ONLY: cp2k_version
28 USE cp2k_runs, ONLY: run_input
29 USE cp_files, ONLY: close_file,&
32 USE f77_interface, ONLY: &
40 USE input_cp2k_read, ONLY: empty_initial_variables
43 USE kinds, ONLY: default_path_length,&
45 dp
50#include "../base/base_uses.f90"
51
52 IMPLICIT NONE
53
54 PRIVATE
55
67
68 TYPE, EXTENDS(eri_type_eri_element_func) :: eri2array
69 INTEGER(C_INT), POINTER :: coords(:) => null()
70 REAL(C_DOUBLE), POINTER :: values(:) => null()
71 INTEGER :: idx = 1
72 CONTAINS
73 PROCEDURE :: func => eri2array_func
74 END TYPE eri2array
75
76CONTAINS
77
78! **************************************************************************************************
79!> \brief ...
80!> \param version_str ...
81!> \param str_length ...
82! **************************************************************************************************
83 SUBROUTINE cp2k_get_version(version_str, str_length) BIND(C)
84 CHARACTER(LEN=1, KIND=C_CHAR), INTENT(OUT) :: version_str(*)
85 INTEGER(C_INT), VALUE :: str_length
86
87 INTEGER :: i, n
88
89 n = len_trim(cp2k_version)
90 cpassert(str_length >= n + 1)
91 mark_used(str_length)
92
93 ! copy string
94 DO i = 1, n
95 version_str(i) = cp2k_version(i:i)
96 END DO
97 version_str(n + 1) = c_null_char
98 END SUBROUTINE cp2k_get_version
99
100! **************************************************************************************************
101!> \brief ...
102! **************************************************************************************************
103 SUBROUTINE cp2k_init() BIND(C)
104 INTEGER :: ierr
105
106 CALL init_cp2k(.true., ierr)
107 cpassert(ierr == 0)
108 END SUBROUTINE cp2k_init
109
110! **************************************************************************************************
111!> \brief ...
112! **************************************************************************************************
113 SUBROUTINE cp2k_init_without_mpi() BIND(C)
114 INTEGER :: ierr
115
116 CALL init_cp2k(.false., ierr)
117 cpassert(ierr == 0)
118 END SUBROUTINE cp2k_init_without_mpi
119
120! **************************************************************************************************
121!> \brief ...
122!> \param mpi_comm ...
123! **************************************************************************************************
124 SUBROUTINE cp2k_init_without_mpi_comm(mpi_comm) BIND(C)
125 INTEGER(C_INT), VALUE :: mpi_comm
126
127 INTEGER :: ierr
128 TYPE(mp_comm_type) :: my_mpi_comm
129
130 CALL my_mpi_comm%set_handle(int(mpi_comm))
131 CALL init_cp2k(.false., ierr, my_mpi_comm)
132 cpassert(ierr == 0)
133 END SUBROUTINE cp2k_init_without_mpi_comm
134
135! **************************************************************************************************
136!> \brief ...
137! **************************************************************************************************
138 SUBROUTINE cp2k_finalize() BIND(C)
139 INTEGER :: ierr
140
141 CALL finalize_cp2k(.true., ierr)
142 cpassert(ierr == 0)
143 END SUBROUTINE cp2k_finalize
144
145! **************************************************************************************************
146!> \brief ...
147! **************************************************************************************************
148 SUBROUTINE cp2k_finalize_without_mpi() BIND(C)
149 INTEGER :: ierr
150
151 CALL finalize_cp2k(.false., ierr)
152 cpassert(ierr == 0)
153 END SUBROUTINE cp2k_finalize_without_mpi
154
155! **************************************************************************************************
156!> \brief ...
157!> \param new_env_id ...
158!> \param input_file_path ...
159!> \param output_file_path ...
160! **************************************************************************************************
161 SUBROUTINE cp2k_create_force_env(new_env_id, input_file_path, output_file_path) BIND(C)
162 INTEGER(C_INT), INTENT(OUT) :: new_env_id
163 CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
164
165 CHARACTER(LEN=default_path_length) :: ifp, ofp
166 INTEGER :: ierr, ncopied
167 TYPE(section_type), POINTER :: input_declaration
168
169 ifp = " "; ofp = " "
170 ncopied = strlcpy_c2f(ifp, input_file_path)
171 ncopied = strlcpy_c2f(ofp, output_file_path)
172
173 NULLIFY (input_declaration)
174 CALL create_cp2k_root_section(input_declaration)
175 CALL create_force_env(new_env_id, input_declaration, ifp, ofp, ierr=ierr)
176 CALL section_release(input_declaration)
177 cpassert(ierr == 0)
178 END SUBROUTINE cp2k_create_force_env
179
180! **************************************************************************************************
181!> \brief ...
182!> \param new_env_id ...
183!> \param input_file_path ...
184!> \param output_file_path ...
185!> \param mpi_comm ...
186! **************************************************************************************************
187 SUBROUTINE cp2k_create_force_env_comm(new_env_id, input_file_path, output_file_path, mpi_comm) BIND(C)
188 INTEGER(C_INT), INTENT(OUT) :: new_env_id
189 CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
190 INTEGER(C_INT), VALUE :: mpi_comm
191
192 CHARACTER(LEN=default_path_length) :: ifp, ofp
193 INTEGER :: ierr, ncopied
194 TYPE(mp_comm_type) :: my_mpi_comm
195 TYPE(section_type), POINTER :: input_declaration
196
197 ifp = " "; ofp = " "
198 ncopied = strlcpy_c2f(ifp, input_file_path)
199 ncopied = strlcpy_c2f(ofp, output_file_path)
200
201 NULLIFY (input_declaration)
202 CALL create_cp2k_root_section(input_declaration)
203 CALL my_mpi_comm%set_handle(int(mpi_comm))
204 CALL create_force_env(new_env_id, input_declaration, ifp, ofp, my_mpi_comm, ierr=ierr)
205 CALL section_release(input_declaration)
206 cpassert(ierr == 0)
207 END SUBROUTINE cp2k_create_force_env_comm
208
209! **************************************************************************************************
210!> \brief ...
211!> \param env_id ...
212! **************************************************************************************************
213 SUBROUTINE cp2k_destroy_force_env(env_id) BIND(C)
214 INTEGER(C_INT), VALUE :: env_id
215
216 INTEGER :: ierr
217
218 CALL destroy_force_env(env_id, ierr)
219 cpassert(ierr == 0)
220 END SUBROUTINE cp2k_destroy_force_env
221
222! **************************************************************************************************
223!> \brief ...
224!> \param env_id ...
225!> \param new_pos ...
226!> \param n_el ...
227! **************************************************************************************************
228 SUBROUTINE cp2k_set_positions(env_id, new_pos, n_el) BIND(C)
229 INTEGER(C_INT), VALUE :: env_id, n_el
230 REAL(c_double), DIMENSION(1:n_el), INTENT(IN) :: new_pos
231
232 INTEGER :: ierr
233
234 CALL set_pos(env_id, new_pos, n_el, ierr)
235 cpassert(ierr == 0)
236 END SUBROUTINE cp2k_set_positions
237
238! **************************************************************************************************
239!> \brief ...
240!> \param env_id ...
241!> \param new_vel ...
242!> \param n_el ...
243! **************************************************************************************************
244 SUBROUTINE cp2k_set_velocities(env_id, new_vel, n_el) BIND(C)
245 INTEGER(C_INT), VALUE :: env_id, n_el
246 REAL(c_double), DIMENSION(1:n_el), INTENT(IN) :: new_vel
247
248 INTEGER :: ierr
249
250 CALL set_vel(env_id, new_vel, n_el, ierr)
251 cpassert(ierr == 0)
252 END SUBROUTINE cp2k_set_velocities
253
254! **************************************************************************************************
255!> \brief ...
256!> \param env_id ...
257!> \param new_cell ...
258! **************************************************************************************************
259 SUBROUTINE cp2k_set_cell(env_id, new_cell) BIND(C)
260 INTEGER(C_INT), VALUE :: env_id
261 REAL(c_double), DIMENSION(3, 3), INTENT(IN) :: new_cell
262
263 INTEGER :: ierr
264
265 CALL set_cell(env_id, new_cell, ierr)
266 cpassert(ierr == 0)
267 END SUBROUTINE cp2k_set_cell
268
269! **************************************************************************************************
270!> \brief ...
271!> \param env_id ...
272!> \param description ...
273!> \param RESULT ...
274!> \param n_el ...
275! **************************************************************************************************
276 SUBROUTINE cp2k_get_result(env_id, description, RESULT, n_el) BIND(C)
277 INTEGER(C_INT), VALUE :: env_id
278 CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: description(*)
279 INTEGER(C_INT), VALUE :: n_el
280 REAL(c_double), DIMENSION(1:n_el), INTENT(OUT) :: result
281
282 CHARACTER(LEN=default_string_length) :: desc_low
283 INTEGER :: ierr, ncopied
284
285 desc_low = " "
286 ncopied = strlcpy_c2f(desc_low, description)
287
288 CALL get_result_r1(env_id, desc_low, n_el, result, ierr=ierr)
289 cpassert(ierr == 0)
290 END SUBROUTINE cp2k_get_result
291
292! **************************************************************************************************
293!> \brief ...
294!> \param env_id ...
295!> \param natom ...
296! **************************************************************************************************
297 SUBROUTINE cp2k_get_natom(env_id, natom) BIND(C)
298 INTEGER(C_INT), VALUE :: env_id
299 INTEGER(C_INT), INTENT(OUT) :: natom
300
301 INTEGER :: ierr
302
303 CALL get_natom(env_id, natom, ierr)
304 cpassert(ierr == 0)
305 END SUBROUTINE cp2k_get_natom
306
307! **************************************************************************************************
308!> \brief ...
309!> \param env_id ...
310!> \param nparticle ...
311! **************************************************************************************************
312 SUBROUTINE cp2k_get_nparticle(env_id, nparticle) BIND(C)
313 INTEGER(C_INT), VALUE :: env_id
314 INTEGER(C_INT), INTENT(OUT) :: nparticle
315
316 INTEGER :: ierr
317
318 CALL get_nparticle(env_id, nparticle, ierr)
319 cpassert(ierr == 0)
320 END SUBROUTINE cp2k_get_nparticle
321
322! **************************************************************************************************
323!> \brief ...
324!> \param env_id ...
325!> \param pos ...
326!> \param n_el ...
327! **************************************************************************************************
328 SUBROUTINE cp2k_get_positions(env_id, pos, n_el) BIND(C)
329 INTEGER(C_INT), VALUE :: env_id, n_el
330 REAL(c_double), DIMENSION(1:n_el), INTENT(OUT) :: pos
331
332 INTEGER :: ierr
333
334 CALL get_pos(env_id, pos, n_el, ierr)
335 cpassert(ierr == 0)
336 END SUBROUTINE cp2k_get_positions
337
338! **************************************************************************************************
339!> \brief ...
340!> \param env_id ...
341!> \param force ...
342!> \param n_el ...
343! **************************************************************************************************
344 SUBROUTINE cp2k_get_forces(env_id, force, n_el) BIND(C)
345 INTEGER(C_INT), VALUE :: env_id, n_el
346 REAL(c_double), DIMENSION(1:n_el), INTENT(OUT) :: force
347
348 INTEGER :: ierr
349
350 CALL get_force(env_id, force, n_el, ierr)
351 cpassert(ierr == 0)
352 END SUBROUTINE cp2k_get_forces
353
354! **************************************************************************************************
355!> \brief Get the configurational stress (pressure-positive, atomic units).
356!> \param env_id Force environment ID
357!> \param stress_tensor Column-major 3x3 tensor
358!> \param available 1 if stress was enabled in the input, otherwise 0 (tensor is zero).
359! **************************************************************************************************
360 SUBROUTINE cp2k_get_stress_tensor(env_id, stress_tensor, available) BIND(C)
361 INTEGER(C_INT), VALUE :: env_id
362 REAL(c_double), DIMENSION(3, 3), INTENT(OUT) :: stress_tensor
363 INTEGER(C_INT), INTENT(OUT) :: available
364
365 INTEGER :: ierr
366 LOGICAL :: has_stress
367
368 CALL get_stress_tensor(env_id, stress_tensor, ierr, available=has_stress)
369 cpassert(ierr == 0)
370 available = merge(1, 0, has_stress)
371 END SUBROUTINE cp2k_get_stress_tensor
372
373! **************************************************************************************************
374!> \brief ...
375!> \param env_id ...
376!> \param e_pot ...
377! **************************************************************************************************
378 SUBROUTINE cp2k_get_potential_energy(env_id, e_pot) BIND(C)
379 INTEGER(C_INT), VALUE :: env_id
380 REAL(c_double), INTENT(OUT) :: e_pot
381
382 INTEGER :: ierr
383
384 CALL get_energy(env_id, e_pot, ierr)
385 cpassert(ierr == 0)
386 END SUBROUTINE cp2k_get_potential_energy
387
388! **************************************************************************************************
389!> \brief ...
390!> \param env_id ...
391!> \param cell ...
392! **************************************************************************************************
393 SUBROUTINE cp2k_get_cell(env_id, cell) BIND(C)
394 INTEGER(C_INT), VALUE :: env_id
395 REAL(c_double), DIMENSION(3, 3), INTENT(OUT) :: cell
396
397 INTEGER :: ierr
398
399 CALL get_cell(env_id, cell=cell, ierr=ierr)
400 cpassert(ierr == 0)
401 END SUBROUTINE cp2k_get_cell
402
403! **************************************************************************************************
404!> \brief ...
405!> \param env_id ...
406!> \param cell ...
407! **************************************************************************************************
408 SUBROUTINE cp2k_get_qmmm_cell(env_id, cell) BIND(C)
409 INTEGER(C_INT), VALUE :: env_id
410 REAL(c_double), DIMENSION(3, 3), INTENT(OUT) :: cell
411
412 INTEGER :: ierr
413
414 CALL get_qmmm_cell(env_id, cell=cell, ierr=ierr)
415 cpassert(ierr == 0)
416 END SUBROUTINE cp2k_get_qmmm_cell
417
418! **************************************************************************************************
419!> \brief ...
420!> \param env_id ...
421! **************************************************************************************************
422 SUBROUTINE cp2k_calc_energy_force(env_id) BIND(C)
423 INTEGER(C_INT), VALUE :: env_id
424
425 INTEGER :: ierr
426
427 CALL calc_energy_force(env_id, .true., ierr)
428 cpassert(ierr == 0)
429 END SUBROUTINE cp2k_calc_energy_force
430
431! **************************************************************************************************
432!> \brief ...
433!> \param env_id ...
434! **************************************************************************************************
435 SUBROUTINE cp2k_calc_energy(env_id) BIND(C)
436 INTEGER(C_INT), VALUE :: env_id
437
438 INTEGER :: ierr
439
440 CALL calc_energy_force(env_id, .false., ierr)
441 cpassert(ierr == 0)
442 END SUBROUTINE cp2k_calc_energy
443
444! **************************************************************************************************
445!> \brief Get the last Quickstep SCF convergence status, including outer loops
446!> \param env_id force environment handle
447!> \param status -1: unavailable, 0: not converged, 1: converged
448! **************************************************************************************************
449 SUBROUTINE cp2k_get_scf_convergence(env_id, status) BIND(C)
450 INTEGER(C_INT), VALUE :: env_id
451 INTEGER(C_INT), INTENT(OUT) :: status
452
453 INTEGER :: ierr, in_use
454 TYPE(f_env_type), POINTER :: f_env
455 TYPE(qs_environment_type), POINTER :: qs_env
456
457 CALL f_env_add_defaults(env_id, f_env)
458 status = -1
459 CALL force_env_get(f_env%force_env, in_use=in_use)
460 IF (.NOT. f_env%is_dirty .AND. in_use == use_qs_force) THEN
461 CALL force_env_get(f_env%force_env, qs_env=qs_env)
462 IF (qs_env%scf_convergence_available) status = merge(1, 0, qs_env%scf_converged)
463 END IF
464 CALL f_env_rm_defaults(f_env, ierr)
465 cpassert(ierr == 0)
466 END SUBROUTINE cp2k_get_scf_convergence
467
468! **************************************************************************************************
469!> \brief ...
470!> \param input_file_path ...
471!> \param output_file_path ...
472! **************************************************************************************************
473 SUBROUTINE cp2k_run_input(input_file_path, output_file_path) BIND(C)
474 CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
475
476 CALL cp2k_run_input_comm(input_file_path, output_file_path, int(default_para_env%get_handle(), c_int))
477 END SUBROUTINE cp2k_run_input
478
479! **************************************************************************************************
480!> \brief ...
481!> \param input_file_path ...
482!> \param output_file_path ...
483!> \param mpi_comm ...
484! **************************************************************************************************
485 SUBROUTINE cp2k_run_input_comm(input_file_path, output_file_path, mpi_comm) BIND(C)
486 CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
487 INTEGER(C_INT), VALUE :: mpi_comm
488
489 CHARACTER(LEN=default_path_length) :: ifp, ofp
490 INTEGER :: ncopied, unit_nr
491 TYPE(mp_comm_type) :: my_mpi_comm
492 TYPE(section_type), POINTER :: input_declaration
493
494 ifp = " "; ofp = " "
495 ncopied = strlcpy_c2f(ifp, input_file_path)
496 ncopied = strlcpy_c2f(ofp, output_file_path)
497
498 NULLIFY (input_declaration)
499 CALL create_cp2k_root_section(input_declaration)
500 CALL my_mpi_comm%set_handle(int(mpi_comm))
501 IF (my_mpi_comm%is_source() .AND. ofp /= "__STD_OUT__") THEN
502 CALL open_file(file_name=ofp, &
503 file_status="UNKNOWN", &
504 file_action="WRITE", &
505 file_position="APPEND", &
506 unit_number=unit_nr)
507 END IF
508 CALL run_input(input_declaration, ifp, ofp, empty_initial_variables, my_mpi_comm)
509 IF (my_mpi_comm%is_source() .AND. ofp /= "__STD_OUT__") CALL close_file(unit_number=unit_nr)
510 CALL section_release(input_declaration)
511 END SUBROUTINE cp2k_run_input_comm
512
513! **************************************************************************************************
514!> \brief Gets a function pointer pointing to a routine defined in C/C++ and
515!> passes it to the transport environment in force environment
516!> \param f_env_id the force env id
517!> \param func_ptr the function pointer
518!> \par History
519!> 12.2012 created [Hossein Bani-Hashemian]
520!> \author Mohammad Hossein Bani-Hashemian
521! **************************************************************************************************
522 SUBROUTINE cp2k_transport_set_callback(f_env_id, func_ptr) BIND(C)
523 INTEGER(C_INT), VALUE :: f_env_id
524 TYPE(c_funptr), VALUE :: func_ptr
525
526 INTEGER :: ierr, in_use
527 TYPE(f_env_type), POINTER :: f_env
528
529 NULLIFY (f_env)
530 CALL f_env_add_defaults(f_env_id, f_env)
531 CALL force_env_get(f_env%force_env, in_use=in_use)
532 IF (in_use == use_qs_force) THEN
533 f_env%force_env%qs_env%transport_env%ext_c_method_ptr = func_ptr
534 END IF
535 CALL f_env_rm_defaults(f_env, ierr)
536 cpassert(ierr == 0)
537 END SUBROUTINE cp2k_transport_set_callback
538
539! **************************************************************************************************
540!> \brief Get the number of molecular orbitals
541!> \param f_env_id the force env id
542!> \return The number of elements or -1 if unavailable
543!> \author Tiziano Mueller
544! **************************************************************************************************
545 INTEGER(C_INT) FUNCTION cp2k_active_space_get_mo_count(f_env_id) RESULT(nmo) BIND(C)
547 USE qs_mo_types, ONLY: get_mo_set
549 INTEGER(C_INT), VALUE :: f_env_id
550
551 INTEGER :: ierr
552 TYPE(active_space_type), POINTER :: active_space_env
553 TYPE(f_env_type), POINTER :: f_env
554
555 nmo = -1
556 NULLIFY (f_env)
557
558 CALL f_env_add_defaults(f_env_id, f_env)
559
560 try: block
561 CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
562
563 IF (.NOT. ASSOCIATED(active_space_env)) THEN
564 EXIT try
565 END IF
566
567 CALL get_mo_set(active_space_env%mos_active(1), nmo=nmo)
568 END BLOCK try
569
570 CALL f_env_rm_defaults(f_env, ierr)
571 cpassert(ierr == 0)
573
574! **************************************************************************************************
575!> \brief Get the active space Fock sub-matrix (as a full matrix)
576!> \param f_env_id the force env id
577!> \param buf C array to write the data to
578!> \param buf_len The length of the C array to write the data to (must be at least mo_count^2)
579!> \return The number of elements written or -1 if unavailable or buffer too small
580!> \author Tiziano Mueller
581! **************************************************************************************************
582 INTEGER(C_LONG) FUNCTION cp2k_active_space_get_fock_sub(f_env_id, buf, buf_len) RESULT(nelem) BIND(C)
584 USE qs_mo_types, ONLY: get_mo_set
586 INTEGER(C_INT), VALUE :: f_env_id
587 INTEGER(C_LONG), VALUE :: buf_len
588 REAL(c_double), DIMENSION(0:buf_len-1), &
589 INTENT(OUT) :: buf
590
591 INTEGER :: i, ierr, j, norb
592 REAL(c_double) :: mval
593 TYPE(active_space_type), POINTER :: active_space_env
594 TYPE(f_env_type), POINTER :: f_env
595
596 nelem = -1
597 NULLIFY (f_env)
598
599 CALL f_env_add_defaults(f_env_id, f_env)
600
601 try: block
602 CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
603
604 IF (.NOT. ASSOCIATED(active_space_env)) THEN
605 EXIT try
606 END IF
607
608 CALL get_mo_set(active_space_env%mos_active(1), nmo=norb)
609
610 IF (buf_len < norb*norb) THEN
611 EXIT try
612 END IF
613
614 DO i = 0, norb - 1
615 DO j = 0, norb - 1
616 CALL cp_fm_get_element(active_space_env%fock_sub(1), i + 1, j + 1, mval)
617 buf(norb*i + j) = mval
618 buf(norb*j + i) = mval
619 END DO
620 END DO
621
622 ! finished successfully, set number of written elements
623 nelem = norb**norb
624 END BLOCK try
625
626 CALL f_env_rm_defaults(f_env, ierr)
627 cpassert(ierr == 0)
629
630! **************************************************************************************************
631!> \brief Get the number of non-zero elements of the ERI
632!> \param f_env_id the force env id
633!> \return The number of elements or -1 if unavailable
634!> \author Tiziano Mueller
635! **************************************************************************************************
636 INTEGER(C_LONG) FUNCTION cp2k_active_space_get_eri_nze_count(f_env_id) RESULT(nze_count) BIND(C)
639 INTEGER(C_INT), VALUE :: f_env_id
640
641 INTEGER :: ierr
642 TYPE(active_space_type), POINTER :: active_space_env
643 TYPE(f_env_type), POINTER :: f_env
644
645 nze_count = -1
646 NULLIFY (f_env)
647
648 CALL f_env_add_defaults(f_env_id, f_env)
649
650 try: block
651 CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
652
653 IF (.NOT. ASSOCIATED(active_space_env)) THEN
654 EXIT try
655 END IF
656
657 nze_count = int(active_space_env%eri%eri(1)%csr_mat%nze_total, kind(nze_count))
658 END BLOCK try
659
660 CALL f_env_rm_defaults(f_env, ierr)
661 cpassert(ierr == 0)
663
664! **************************************************************************************************
665!> \brief Get the electron repulsion integrals (as a sparse tensor)
666!> \param f_env_id the force env id
667!> \param buf_coords C array to write the indizes (i,j,k,l) to
668!> \param buf_coords_len size of the buffer, must be at least 4*nze_count
669!> \param buf_values C array to write the values to
670!> \param buf_values_len size of the buffer, must be at least nze_count
671!> \return The number of elements written or -1 if unavailable or buffer too small
672!> \author Tiziano Mueller
673! **************************************************************************************************
674 INTEGER(C_LONG) FUNCTION cp2k_active_space_get_eri(f_env_id, &
675 buf_coords, buf_coords_len, &
676 buf_values, buf_values_len) RESULT(nelem) BIND(C)
678 USE qs_mo_types, ONLY: get_mo_set
680 INTEGER(C_INT), INTENT(IN), VALUE :: f_env_id
681 INTEGER(C_LONG), INTENT(IN), VALUE :: buf_coords_len
682 INTEGER(C_INT), INTENT(OUT), TARGET :: buf_coords(1:buf_coords_len)
683 INTEGER(C_LONG), INTENT(IN), VALUE :: buf_values_len
684 REAL(c_double), INTENT(OUT), TARGET :: buf_values(1:buf_values_len)
685
686 INTEGER :: ierr
687 TYPE(active_space_type), POINTER :: active_space_env
688 TYPE(f_env_type), POINTER :: f_env
689
690 nelem = -1
691 NULLIFY (f_env)
692
693 CALL f_env_add_defaults(f_env_id, f_env)
694
695 try: block
696 CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
697
698 IF (.NOT. ASSOCIATED(active_space_env)) THEN
699 EXIT try
700 END IF
701
702 associate(nze => active_space_env%eri%eri(1)%csr_mat%nze_total)
703 IF (buf_coords_len < 4*nze .OR. buf_values_len < nze) THEN
704 EXIT try
705 END IF
706
707 CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri2array(buf_coords, buf_values))
708
709 nelem = int(nze, kind(nelem))
710 END associate
711 END BLOCK try
712
713 CALL f_env_rm_defaults(f_env, ierr)
714 cpassert(ierr == 0)
715 END FUNCTION cp2k_active_space_get_eri
716
717! **************************************************************************************************
718!> \brief Copy the active space ERI to C buffers
719!> \param this Class pointer
720!> \param i The i index of the value `val`
721!> \param j The j index of the value `val`
722!> \param k The k index of the value `val`
723!> \param l The l index of the value `val`
724!> \param val The value at the given index
725!> \return Always true to continue with the loop
726!> \author Tiziano Mueller
727! **************************************************************************************************
728 LOGICAL FUNCTION eri2array_func(this, i, j, k, l, val) RESULT(cont)
729 CLASS(eri2array), INTENT(inout) :: this
730 INTEGER, INTENT(in) :: i, j, k, l
731 REAL(kind=dp), INTENT(in) :: val
732
733 this%coords(4*(this%idx - 1) + 1) = i
734 this%coords(4*(this%idx - 1) + 2) = j
735 this%coords(4*(this%idx - 1) + 3) = k
736 this%coords(4*(this%idx - 1) + 4) = l
737 this%values(this%idx) = val
738
739 this%idx = this%idx + 1
740
741 cont = .true.
742 END FUNCTION eri2array_func
743
744END MODULE libcp2k
some minimal info about CP2K, including its version and license
Definition cp2k_info.F:22
character(len= *), parameter, public cp2k_version
Definition cp2k_info.F:50
subroutine, public run_input(input_declaration, input_file_path, output_file_path, initial_variables, mpi_comm)
runs the given input
Definition cp2k_runs.F:938
Utility routines to open and close files. Tracking of preconnections.
Definition cp_files.F:16
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.
Definition cp_files.F:322
subroutine, public close_file(unit_number, file_status, keep_preconnection)
Close an open file given by its logical unit number. Optionally, keep the file and unit preconnected.
Definition cp_files.F:122
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_get_element(matrix, irow_global, icol_global, alpha, local)
returns an element of a fm this value is valid on every cpu using this call is expensive
interface to use cp2k as library
recursive subroutine, public destroy_force_env(env_id, ierr, q_finalize)
deallocates the force_env with the given id
subroutine, public set_vel(env_id, new_vel, n_el, ierr)
sets the velocities of the particles
subroutine, public get_nparticle(env_id, n_particle, ierr)
returns the number of particles in the given force env
subroutine, public get_natom(env_id, n_atom, ierr)
returns the number of atoms in the given force env
subroutine, public get_stress_tensor(env_id, stress_tensor, ierr, available)
gets the stress tensor
subroutine, public f_env_add_defaults(f_env_id, f_env, handle)
adds the default environments of the f_env to the stack of the defaults, and returns a new error and ...
subroutine, public get_cell(env_id, cell, per, ierr)
gets a cell
type(mp_para_env_type), pointer, save, public default_para_env
recursive subroutine, public calc_energy_force(env_id, calc_force, ierr)
updates the energy and the forces of given force_env
subroutine, public get_energy(env_id, e_pot, ierr)
returns the energy of the last configuration calculated
subroutine, public init_cp2k(init_mpi, ierr, mpi_comm)
initializes cp2k, needs to be called once before using any of the other functions when using cp2k as ...
subroutine, public get_qmmm_cell(env_id, cell, ierr)
gets the qmmm cell
subroutine, public get_pos(env_id, pos, n_el, ierr)
gets the positions of the particles
recursive subroutine, public create_force_env(new_env_id, input_declaration, input_path, output_path, mpi_comm, output_unit, owns_out_unit, input, ierr, work_dir, initial_variables)
creates a new force environment using the given input, and writing the output to the given output uni...
subroutine, public get_result_r1(env_id, description, n, result, res_exist, ierr)
gets a result from CP2K that is a real 1D array
subroutine, public set_cell(env_id, new_cell, ierr)
sets a new cell
subroutine, public finalize_cp2k(finalize_mpi, ierr)
cleanup after you have finished using this interface
subroutine, public set_pos(env_id, new_pos, n_el, ierr)
sets the positions of the particles
subroutine, public f_env_rm_defaults(f_env, ierr, handle)
removes the default environments of the f_env to the stack of the defaults, and sets ierr accordingly...
subroutine, public get_force(env_id, frc, n_el, ierr)
gets the forces of the particles
Interface for the force calculations.
recursive subroutine, public force_env_get(force_env, in_use, fist_env, qs_env, meta_env, fp_env, subsys, para_env, potential_energy, additional_potential, kinetic_energy, harmonic_shell, kinetic_shell, cell, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, globenv, input, force_env_section, method_name_id, root_section, mixed_env, nnp_env, embed_env, ipi_env)
returns various attributes about the force environment
integer, parameter, public use_qs_force
parse cp2k input files
builds the input structure for cp2k
Definition input_cp2k.F:14
subroutine, public create_cp2k_root_section(root_section)
creates the input structure of the file used by cp2k
Definition input_cp2k.F:75
objects that represent the structure of input sections and the data contained in an input section
recursive subroutine, public section_release(section)
releases the given keyword list (see doc/ReferenceCounting.html)
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
integer, parameter, public default_path_length
Definition kinds.F:58
CP2K C/C++ interface.
Definition libcp2k.F:20
subroutine, public cp2k_create_force_env_comm(new_env_id, input_file_path, output_file_path, mpi_comm)
...
Definition libcp2k.F:188
subroutine, public cp2k_get_nparticle(env_id, nparticle)
...
Definition libcp2k.F:313
subroutine, public cp2k_get_natom(env_id, natom)
...
Definition libcp2k.F:298
subroutine, public cp2k_calc_energy_force(env_id)
...
Definition libcp2k.F:423
subroutine, public cp2k_finalize()
...
Definition libcp2k.F:139
subroutine, public cp2k_finalize_without_mpi()
...
Definition libcp2k.F:149
subroutine, public cp2k_run_input_comm(input_file_path, output_file_path, mpi_comm)
...
Definition libcp2k.F:486
subroutine, public cp2k_run_input(input_file_path, output_file_path)
...
Definition libcp2k.F:474
subroutine, public cp2k_init_without_mpi()
...
Definition libcp2k.F:114
subroutine, public cp2k_get_potential_energy(env_id, e_pot)
...
Definition libcp2k.F:379
integer(c_int) function, public cp2k_active_space_get_mo_count(f_env_id)
Get the number of molecular orbitals.
Definition libcp2k.F:546
subroutine, public cp2k_get_cell(env_id, cell)
...
Definition libcp2k.F:394
subroutine, public cp2k_create_force_env(new_env_id, input_file_path, output_file_path)
...
Definition libcp2k.F:162
subroutine, public cp2k_init_without_mpi_comm(mpi_comm)
...
Definition libcp2k.F:125
subroutine, public cp2k_set_velocities(env_id, new_vel, n_el)
...
Definition libcp2k.F:245
subroutine, public cp2k_init()
...
Definition libcp2k.F:104
subroutine, public cp2k_get_result(env_id, description, result, n_el)
...
Definition libcp2k.F:277
subroutine, public cp2k_get_forces(env_id, force, n_el)
...
Definition libcp2k.F:345
integer(c_long) function, public cp2k_active_space_get_eri_nze_count(f_env_id)
Get the number of non-zero elements of the ERI.
Definition libcp2k.F:637
subroutine, public cp2k_get_stress_tensor(env_id, stress_tensor, available)
Get the configurational stress (pressure-positive, atomic units).
Definition libcp2k.F:361
integer(c_long) function, public cp2k_active_space_get_fock_sub(f_env_id, buf, buf_len)
Get the active space Fock sub-matrix (as a full matrix).
Definition libcp2k.F:583
subroutine, public cp2k_transport_set_callback(f_env_id, func_ptr)
Gets a function pointer pointing to a routine defined in C/C++ and passes it to the transport environ...
Definition libcp2k.F:523
subroutine, public cp2k_get_qmmm_cell(env_id, cell)
...
Definition libcp2k.F:409
subroutine, public cp2k_get_scf_convergence(env_id, status)
Get the last Quickstep SCF convergence status, including outer loops.
Definition libcp2k.F:450
subroutine, public cp2k_get_version(version_str, str_length)
...
Definition libcp2k.F:84
subroutine, public cp2k_set_cell(env_id, new_cell)
...
Definition libcp2k.F:260
integer(c_long) function, public cp2k_active_space_get_eri(f_env_id, buf_coords, buf_coords_len, buf_values, buf_values_len)
Get the electron repulsion integrals (as a sparse tensor).
Definition libcp2k.F:677
subroutine, public cp2k_get_positions(env_id, pos, n_el)
...
Definition libcp2k.F:329
subroutine, public cp2k_set_positions(env_id, new_pos, n_el)
...
Definition libcp2k.F:229
subroutine, public cp2k_calc_energy(env_id)
...
Definition libcp2k.F:436
subroutine, public cp2k_destroy_force_env(env_id)
...
Definition libcp2k.F:214
Interface to the message passing library MPI.
The types needed for the calculation of active space Hamiltonians.
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count)
Get the components of a MO set data structure.
Utilities for string manipulations.
integer function, public strlcpy_c2f(fstring, cstring)
Copy the content of a \0-terminated C-string to a finite-length Fortran string.
represent a section of the input file
Abstract function object for the eri_type_eri_foreach method.