(git:34ef472)
libcp2k.F
Go to the documentation of this file.
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 !--------------------------------------------------------------------------------------------------!
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 ! **************************************************************************************************
20 MODULE 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
30  USE f77_interface, ONLY: &
34  USE force_env_types, ONLY: force_env_get,&
37  USE input_cp2k_read, ONLY: empty_initial_variables
39  section_type
40  USE kinds, ONLY: default_path_length,&
42  dp
43  USE message_passing, ONLY: mp_comm_type
44  USE qs_active_space_types, ONLY: eri_type_eri_element_func
45  USE string_utilities, ONLY: strlcpy_c2f
46 #include "../base/base_uses.f90"
47 
48  IMPLICIT NONE
49 
50  PRIVATE
51 
52  TYPE, EXTENDS(eri_type_eri_element_func) :: eri2array
53  INTEGER(C_INT), POINTER :: coords(:) => null()
54  REAL(C_DOUBLE), POINTER :: values(:) => null()
55  INTEGER :: idx = 1
56  CONTAINS
57  PROCEDURE :: func => eri2array_func
58  END TYPE
59 
60 CONTAINS
61 
62 ! **************************************************************************************************
63 !> \brief ...
64 !> \param version_str ...
65 !> \param str_length ...
66 ! **************************************************************************************************
67  SUBROUTINE cp2k_get_version(version_str, str_length) BIND(C)
68  CHARACTER(LEN=1, KIND=C_CHAR), INTENT(OUT) :: version_str(*)
69  INTEGER(C_INT), VALUE :: str_length
70 
71  INTEGER :: i, n
72 
73  n = len_trim(cp2k_version)
74  cpassert(str_length >= n + 1)
75  mark_used(str_length)
76 
77  ! copy string
78  DO i = 1, n
79  version_str(i) = cp2k_version(i:i)
80  END DO
81  version_str(n + 1) = c_null_char
82  END SUBROUTINE cp2k_get_version
83 
84 ! **************************************************************************************************
85 !> \brief ...
86 ! **************************************************************************************************
87  SUBROUTINE cp2k_init() BIND(C)
88  INTEGER :: ierr
89 
90  CALL init_cp2k(.true., ierr)
91  cpassert(ierr == 0)
92  END SUBROUTINE cp2k_init
93 
94 ! **************************************************************************************************
95 !> \brief ...
96 ! **************************************************************************************************
97  SUBROUTINE cp2k_init_without_mpi() BIND(C)
98  INTEGER :: ierr
99 
100  CALL init_cp2k(.false., ierr)
101  cpassert(ierr == 0)
102  END SUBROUTINE cp2k_init_without_mpi
103 
104 ! **************************************************************************************************
105 !> \brief ...
106 ! **************************************************************************************************
107  SUBROUTINE cp2k_finalize() BIND(C)
108  INTEGER :: ierr
109 
110  CALL finalize_cp2k(.true., ierr)
111  cpassert(ierr == 0)
112  END SUBROUTINE cp2k_finalize
113 
114 ! **************************************************************************************************
115 !> \brief ...
116 ! **************************************************************************************************
117  SUBROUTINE cp2k_finalize_without_mpi() BIND(C)
118  INTEGER :: ierr
119 
120  CALL finalize_cp2k(.false., ierr)
121  cpassert(ierr == 0)
122  END SUBROUTINE cp2k_finalize_without_mpi
123 
124 ! **************************************************************************************************
125 !> \brief ...
126 !> \param new_env_id ...
127 !> \param input_file_path ...
128 !> \param output_file_path ...
129 ! **************************************************************************************************
130  SUBROUTINE cp2k_create_force_env(new_env_id, input_file_path, output_file_path) BIND(C)
131  INTEGER(C_INT), INTENT(OUT) :: new_env_id
132  CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
133 
134  CHARACTER(LEN=default_path_length) :: ifp, ofp
135  INTEGER :: ierr, ncopied
136  TYPE(section_type), POINTER :: input_declaration
137 
138  ifp = " "; ofp = " "
139  ncopied = strlcpy_c2f(ifp, input_file_path)
140  ncopied = strlcpy_c2f(ofp, output_file_path)
141 
142  NULLIFY (input_declaration)
143  CALL create_cp2k_root_section(input_declaration)
144  CALL create_force_env(new_env_id, input_declaration, ifp, ofp, ierr=ierr)
145  CALL section_release(input_declaration)
146  cpassert(ierr == 0)
147  END SUBROUTINE cp2k_create_force_env
148 
149 ! **************************************************************************************************
150 !> \brief ...
151 !> \param new_env_id ...
152 !> \param input_file_path ...
153 !> \param output_file_path ...
154 !> \param mpi_comm ...
155 ! **************************************************************************************************
156  SUBROUTINE cp2k_create_force_env_comm(new_env_id, input_file_path, output_file_path, mpi_comm) BIND(C)
157  INTEGER(C_INT), INTENT(OUT) :: new_env_id
158  CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
159  INTEGER(C_INT), VALUE :: mpi_comm
160 
161  CHARACTER(LEN=default_path_length) :: ifp, ofp
162  INTEGER :: ierr, ncopied
163  TYPE(mp_comm_type) :: my_mpi_comm
164  TYPE(section_type), POINTER :: input_declaration
165 
166  ifp = " "; ofp = " "
167  ncopied = strlcpy_c2f(ifp, input_file_path)
168  ncopied = strlcpy_c2f(ofp, output_file_path)
169 
170  NULLIFY (input_declaration)
171  CALL create_cp2k_root_section(input_declaration)
172  CALL my_mpi_comm%set_handle(int(mpi_comm))
173  CALL create_force_env(new_env_id, input_declaration, ifp, ofp, my_mpi_comm, ierr=ierr)
174  CALL section_release(input_declaration)
175  cpassert(ierr == 0)
176  END SUBROUTINE cp2k_create_force_env_comm
177 
178 ! **************************************************************************************************
179 !> \brief ...
180 !> \param env_id ...
181 ! **************************************************************************************************
182  SUBROUTINE cp2k_destroy_force_env(env_id) BIND(C)
183  INTEGER(C_INT), VALUE :: env_id
184 
185  INTEGER :: ierr
186 
187  CALL destroy_force_env(env_id, ierr)
188  cpassert(ierr == 0)
189  END SUBROUTINE cp2k_destroy_force_env
190 
191 ! **************************************************************************************************
192 !> \brief ...
193 !> \param env_id ...
194 !> \param new_pos ...
195 !> \param n_el ...
196 ! **************************************************************************************************
197  SUBROUTINE cp2k_set_positions(env_id, new_pos, n_el) BIND(C)
198  INTEGER(C_INT), VALUE :: env_id, n_el
199  REAL(C_DOUBLE), DIMENSION(1:n_el), INTENT(IN) :: new_pos
200 
201  INTEGER :: ierr
202 
203  CALL set_pos(env_id, new_pos, n_el, ierr)
204  cpassert(ierr == 0)
205  END SUBROUTINE cp2k_set_positions
206 
207 ! **************************************************************************************************
208 !> \brief ...
209 !> \param env_id ...
210 !> \param new_vel ...
211 !> \param n_el ...
212 ! **************************************************************************************************
213  SUBROUTINE cp2k_set_velocities(env_id, new_vel, n_el) BIND(C)
214  INTEGER(C_INT), VALUE :: env_id, n_el
215  REAL(C_DOUBLE), DIMENSION(1:n_el), INTENT(IN) :: new_vel
216 
217  INTEGER :: ierr
218 
219  CALL set_vel(env_id, new_vel, n_el, ierr)
220  cpassert(ierr == 0)
221  END SUBROUTINE cp2k_set_velocities
222 
223 ! **************************************************************************************************
224 !> \brief ...
225 !> \param env_id ...
226 !> \param new_cell ...
227 ! **************************************************************************************************
228  SUBROUTINE cp2k_set_cell(env_id, new_cell) BIND(C)
229  INTEGER(C_INT), VALUE :: env_id
230  REAL(C_DOUBLE), DIMENSION(3, 3), INTENT(IN) :: new_cell
231 
232  INTEGER :: ierr
233 
234  CALL set_cell(env_id, new_cell, ierr)
235  cpassert(ierr == 0)
236  END SUBROUTINE cp2k_set_cell
237 
238 ! **************************************************************************************************
239 !> \brief ...
240 !> \param env_id ...
241 !> \param description ...
242 !> \param RESULT ...
243 !> \param n_el ...
244 ! **************************************************************************************************
245  SUBROUTINE cp2k_get_result(env_id, description, RESULT, n_el) BIND(C)
246  INTEGER(C_INT), VALUE :: env_id
247  CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: description(*)
248  INTEGER(C_INT), VALUE :: n_el
249  REAL(C_DOUBLE), DIMENSION(1:n_el), INTENT(OUT) :: RESULT
250 
251  CHARACTER(LEN=default_string_length) :: desc_low
252  INTEGER :: ierr, ncopied
253 
254  desc_low = " "
255  ncopied = strlcpy_c2f(desc_low, description)
256 
257  CALL get_result_r1(env_id, desc_low, n_el, result, ierr=ierr)
258  cpassert(ierr == 0)
259  END SUBROUTINE cp2k_get_result
260 
261 ! **************************************************************************************************
262 !> \brief ...
263 !> \param env_id ...
264 !> \param natom ...
265 ! **************************************************************************************************
266  SUBROUTINE cp2k_get_natom(env_id, natom) BIND(C)
267  INTEGER(C_INT), VALUE :: env_id
268  INTEGER(C_INT), INTENT(OUT) :: natom
269 
270  INTEGER :: ierr
271 
272  CALL get_natom(env_id, natom, ierr)
273  cpassert(ierr == 0)
274  END SUBROUTINE cp2k_get_natom
275 
276 ! **************************************************************************************************
277 !> \brief ...
278 !> \param env_id ...
279 !> \param nparticle ...
280 ! **************************************************************************************************
281  SUBROUTINE cp2k_get_nparticle(env_id, nparticle) BIND(C)
282  INTEGER(C_INT), VALUE :: env_id
283  INTEGER(C_INT), INTENT(OUT) :: nparticle
284 
285  INTEGER :: ierr
286 
287  CALL get_nparticle(env_id, nparticle, ierr)
288  cpassert(ierr == 0)
289  END SUBROUTINE cp2k_get_nparticle
290 
291 ! **************************************************************************************************
292 !> \brief ...
293 !> \param env_id ...
294 !> \param pos ...
295 !> \param n_el ...
296 ! **************************************************************************************************
297  SUBROUTINE cp2k_get_positions(env_id, pos, n_el) BIND(C)
298  INTEGER(C_INT), VALUE :: env_id, n_el
299  REAL(C_DOUBLE), DIMENSION(1:n_el), INTENT(OUT) :: pos
300 
301  INTEGER :: ierr
302 
303  CALL get_pos(env_id, pos, n_el, ierr)
304  cpassert(ierr == 0)
305  END SUBROUTINE cp2k_get_positions
306 
307 ! **************************************************************************************************
308 !> \brief ...
309 !> \param env_id ...
310 !> \param force ...
311 !> \param n_el ...
312 ! **************************************************************************************************
313  SUBROUTINE cp2k_get_forces(env_id, force, n_el) BIND(C)
314  INTEGER(C_INT), VALUE :: env_id, n_el
315  REAL(C_DOUBLE), DIMENSION(1:n_el), INTENT(OUT) :: force
316 
317  INTEGER :: ierr
318 
319  CALL get_force(env_id, force, n_el, ierr)
320  cpassert(ierr == 0)
321  END SUBROUTINE cp2k_get_forces
322 
323 ! **************************************************************************************************
324 !> \brief ...
325 !> \param env_id ...
326 !> \param e_pot ...
327 ! **************************************************************************************************
328  SUBROUTINE cp2k_get_potential_energy(env_id, e_pot) BIND(C)
329  INTEGER(C_INT), VALUE :: env_id
330  REAL(C_DOUBLE), INTENT(OUT) :: e_pot
331 
332  INTEGER :: ierr
333 
334  CALL get_energy(env_id, e_pot, ierr)
335  cpassert(ierr == 0)
336  END SUBROUTINE cp2k_get_potential_energy
337 
338 ! **************************************************************************************************
339 !> \brief ...
340 !> \param env_id ...
341 !> \param cell ...
342 ! **************************************************************************************************
343  SUBROUTINE cp2k_get_cell(env_id, cell) BIND(C)
344  INTEGER(C_INT), VALUE :: env_id
345  REAL(C_DOUBLE), DIMENSION(3, 3), INTENT(OUT) :: cell
346 
347  INTEGER :: ierr
348 
349  CALL get_cell(env_id, cell=cell, ierr=ierr)
350  cpassert(ierr == 0)
351  END SUBROUTINE cp2k_get_cell
352 
353 ! **************************************************************************************************
354 !> \brief ...
355 !> \param env_id ...
356 !> \param cell ...
357 ! **************************************************************************************************
358  SUBROUTINE cp2k_get_qmmm_cell(env_id, cell) BIND(C)
359  INTEGER(C_INT), VALUE :: env_id
360  REAL(C_DOUBLE), DIMENSION(3, 3), INTENT(OUT) :: cell
361 
362  INTEGER :: ierr
363 
364  CALL get_qmmm_cell(env_id, cell=cell, ierr=ierr)
365  cpassert(ierr == 0)
366  END SUBROUTINE cp2k_get_qmmm_cell
367 
368 ! **************************************************************************************************
369 !> \brief ...
370 !> \param env_id ...
371 ! **************************************************************************************************
372  SUBROUTINE cp2k_calc_energy_force(env_id) BIND(C)
373  INTEGER(C_INT), VALUE :: env_id
374 
375  INTEGER :: ierr
376 
377  CALL calc_energy_force(env_id, .true., ierr)
378  cpassert(ierr == 0)
379  END SUBROUTINE cp2k_calc_energy_force
380 
381 ! **************************************************************************************************
382 !> \brief ...
383 !> \param env_id ...
384 ! **************************************************************************************************
385  SUBROUTINE cp2k_calc_energy(env_id) BIND(C)
386  INTEGER(C_INT), VALUE :: env_id
387 
388  INTEGER :: ierr
389 
390  CALL calc_energy_force(env_id, .false., ierr)
391  cpassert(ierr == 0)
392  END SUBROUTINE cp2k_calc_energy
393 
394 ! **************************************************************************************************
395 !> \brief ...
396 !> \param input_file_path ...
397 !> \param output_file_path ...
398 ! **************************************************************************************************
399  SUBROUTINE cp2k_run_input(input_file_path, output_file_path) BIND(C)
400  CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
401 
402  CHARACTER(LEN=default_path_length) :: ifp, ofp
403  INTEGER :: ncopied
404  TYPE(section_type), POINTER :: input_declaration
405 
406  ifp = " "; ofp = " "
407  ncopied = strlcpy_c2f(ifp, input_file_path)
408  ncopied = strlcpy_c2f(ofp, output_file_path)
409 
410  NULLIFY (input_declaration)
411  CALL create_cp2k_root_section(input_declaration)
412  CALL run_input(input_declaration, ifp, ofp, empty_initial_variables)
413  CALL section_release(input_declaration)
414  END SUBROUTINE cp2k_run_input
415 
416 ! **************************************************************************************************
417 !> \brief ...
418 !> \param input_file_path ...
419 !> \param output_file_path ...
420 !> \param mpi_comm ...
421 ! **************************************************************************************************
422  SUBROUTINE cp2k_run_input_comm(input_file_path, output_file_path, mpi_comm) BIND(C)
423  CHARACTER(LEN=1, KIND=C_CHAR), INTENT(IN) :: input_file_path(*), output_file_path(*)
424  INTEGER(C_INT), VALUE :: mpi_comm
425 
426  CHARACTER(LEN=default_path_length) :: ifp, ofp
427  INTEGER :: ncopied
428  TYPE(mp_comm_type) :: my_mpi_comm
429  TYPE(section_type), POINTER :: input_declaration
430 
431  ifp = " "; ofp = " "
432  ncopied = strlcpy_c2f(ifp, input_file_path)
433  ncopied = strlcpy_c2f(ofp, output_file_path)
434 
435  NULLIFY (input_declaration)
436  CALL create_cp2k_root_section(input_declaration)
437  CALL my_mpi_comm%set_handle(int(mpi_comm))
438  CALL run_input(input_declaration, ifp, ofp, empty_initial_variables, my_mpi_comm)
439  CALL section_release(input_declaration)
440  END SUBROUTINE cp2k_run_input_comm
441 
442 ! **************************************************************************************************
443 !> \brief Gets a function pointer pointing to a routine defined in C/C++ and
444 !> passes it to the transport environment in force environment
445 !> \param f_env_id the force env id
446 !> \param func_ptr the function pointer
447 !> \par History
448 !> 12.2012 created [Hossein Bani-Hashemian]
449 !> \author Mohammad Hossein Bani-Hashemian
450 ! **************************************************************************************************
451  SUBROUTINE cp2k_transport_set_callback(f_env_id, func_ptr) BIND(C)
452  INTEGER(C_INT), VALUE :: f_env_id
453  TYPE(C_FUNPTR), VALUE :: func_ptr
454 
455  INTEGER :: ierr, in_use
456  TYPE(f_env_type), POINTER :: f_env
457 
458  NULLIFY (f_env)
459  CALL f_env_add_defaults(f_env_id, f_env)
460  CALL force_env_get(f_env%force_env, in_use=in_use)
461  IF (in_use .EQ. use_qs_force) THEN
462  f_env%force_env%qs_env%transport_env%ext_c_method_ptr = func_ptr
463  END IF
464  CALL f_env_rm_defaults(f_env, ierr)
465  cpassert(ierr == 0)
466  END SUBROUTINE cp2k_transport_set_callback
467 
468 ! **************************************************************************************************
469 !> \brief Get the number of molecular orbitals
470 !> \param f_env_id the force env id
471 !> \return The number of elements or -1 if unavailable
472 !> \author Tiziano Mueller
473 ! **************************************************************************************************
474  INTEGER(C_INT) FUNCTION cp2k_active_space_get_mo_count(f_env_id) RESULT(nmo) BIND(C)
475  USE qs_active_space_types, ONLY: active_space_type
476  USE qs_mo_types, ONLY: get_mo_set
478  INTEGER(C_INT), VALUE :: f_env_id
479 
480  INTEGER :: ierr
481  TYPE(active_space_type), POINTER :: active_space_env
482  TYPE(f_env_type), POINTER :: f_env
483 
484  nmo = -1
485  NULLIFY (f_env)
486 
487  CALL f_env_add_defaults(f_env_id, f_env)
488 
489  try: block
490  CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
491 
492  IF (.NOT. ASSOCIATED(active_space_env)) &
493  EXIT try
494 
495  CALL get_mo_set(active_space_env%mos_active(1), nmo=nmo)
496  END BLOCK try
497 
498  CALL f_env_rm_defaults(f_env, ierr)
499  cpassert(ierr == 0)
500  END FUNCTION cp2k_active_space_get_mo_count
501 
502 ! **************************************************************************************************
503 !> \brief Get the active space Fock sub-matrix (as a full matrix)
504 !> \param f_env_id the force env id
505 !> \param buf C array to write the data to
506 !> \param buf_len The length of the C array to write the data to (must be at least mo_count^2)
507 !> \return The number of elements written or -1 if unavailable or buffer too small
508 !> \author Tiziano Mueller
509 ! **************************************************************************************************
510  INTEGER(C_LONG) FUNCTION cp2k_active_space_get_fock_sub(f_env_id, buf, buf_len) RESULT(nelem) BIND(C)
511  USE qs_active_space_types, ONLY: active_space_type
512  USE qs_mo_types, ONLY: get_mo_set
514  INTEGER(C_INT), VALUE :: f_env_id
515  INTEGER(C_LONG), VALUE :: buf_len
516  REAL(c_double), DIMENSION(0:buf_len-1), &
517  INTENT(OUT) :: buf
518 
519  INTEGER :: i, ierr, j, norb
520  REAL(c_double) :: mval
521  TYPE(active_space_type), POINTER :: active_space_env
522  TYPE(f_env_type), POINTER :: f_env
523 
524  nelem = -1
525  NULLIFY (f_env)
526 
527  CALL f_env_add_defaults(f_env_id, f_env)
528 
529  try: block
530  CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
531 
532  IF (.NOT. ASSOCIATED(active_space_env)) &
533  EXIT try
534 
535  CALL get_mo_set(active_space_env%mos_active(1), nmo=norb)
536 
537  IF (buf_len < norb*norb) &
538  EXIT try
539 
540  DO i = 0, norb - 1
541  DO j = 0, norb - 1
542  CALL cp_fm_get_element(active_space_env%fock_sub(1), i + 1, j + 1, mval)
543  buf(norb*i + j) = mval
544  buf(norb*j + i) = mval
545  END DO
546  END DO
547 
548  ! finished successfully, set number of written elements
549  nelem = norb**norb
550  END BLOCK try
551 
552  CALL f_env_rm_defaults(f_env, ierr)
553  cpassert(ierr == 0)
554  END FUNCTION cp2k_active_space_get_fock_sub
555 
556 ! **************************************************************************************************
557 !> \brief Get the number of non-zero elements of the ERI
558 !> \param f_env_id the force env id
559 !> \return The number of elements or -1 if unavailable
560 !> \author Tiziano Mueller
561 ! **************************************************************************************************
562  INTEGER(C_LONG) FUNCTION cp2k_active_space_get_eri_nze_count(f_env_id) RESULT(nze_count) BIND(C)
563  USE qs_active_space_types, ONLY: active_space_type
565  INTEGER(C_INT), VALUE :: f_env_id
566 
567  INTEGER :: ierr
568  TYPE(active_space_type), POINTER :: active_space_env
569  TYPE(f_env_type), POINTER :: f_env
570 
571  nze_count = -1
572  NULLIFY (f_env)
573 
574  CALL f_env_add_defaults(f_env_id, f_env)
575 
576  try: block
577  CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
578 
579  IF (.NOT. ASSOCIATED(active_space_env)) &
580  EXIT try
581 
582  nze_count = int(active_space_env%eri%eri(1)%csr_mat%nze_total, kind(nze_count))
583  END BLOCK try
584 
585  CALL f_env_rm_defaults(f_env, ierr)
586  cpassert(ierr == 0)
588 
589 ! **************************************************************************************************
590 !> \brief Get the electron repulsion integrals (as a sparse tensor)
591 !> \param f_env_id the force env id
592 !> \param buf_coords C array to write the indizes (i,j,k,l) to
593 !> \param buf_coords_len size of the buffer, must be at least 4*nze_count
594 !> \param buf_values C array to write the values to
595 !> \param buf_values_len size of the buffer, must be at least nze_count
596 !> \return The number of elements written or -1 if unavailable or buffer too small
597 !> \author Tiziano Mueller
598 ! **************************************************************************************************
599  INTEGER(C_LONG) FUNCTION cp2k_active_space_get_eri(f_env_id, &
600  buf_coords, buf_coords_len, &
601  buf_values, buf_values_len) RESULT(nelem) BIND(C)
602  USE qs_active_space_types, ONLY: active_space_type
603  USE qs_mo_types, ONLY: get_mo_set
605  INTEGER(C_INT), INTENT(IN), VALUE :: f_env_id
606  INTEGER(C_LONG), INTENT(IN), VALUE :: buf_coords_len
607  INTEGER(C_INT), INTENT(OUT), TARGET :: buf_coords(1:buf_coords_len)
608  INTEGER(C_LONG), INTENT(IN), VALUE :: buf_values_len
609  REAL(c_double), INTENT(OUT), TARGET :: buf_values(1:buf_values_len)
610 
611  INTEGER :: ierr
612  TYPE(active_space_type), POINTER :: active_space_env
613  TYPE(f_env_type), POINTER :: f_env
614 
615  nelem = -1
616  NULLIFY (f_env)
617 
618  CALL f_env_add_defaults(f_env_id, f_env)
619 
620  try: block
621  CALL get_qs_env(f_env%force_env%qs_env, active_space=active_space_env)
622 
623  IF (.NOT. ASSOCIATED(active_space_env)) &
624  EXIT try
625 
626  associate(nze => active_space_env%eri%eri(1)%csr_mat%nze_total)
627  IF (buf_coords_len < 4*nze .OR. buf_values_len < nze) &
628  EXIT try
629 
630  CALL active_space_env%eri%eri_foreach(1, active_space_env%active_orbitals, eri2array(buf_coords, buf_values))
631 
632  nelem = int(nze, kind(nelem))
633  END associate
634  END BLOCK try
635 
636  CALL f_env_rm_defaults(f_env, ierr)
637  cpassert(ierr == 0)
638  END FUNCTION cp2k_active_space_get_eri
639 
640 ! **************************************************************************************************
641 !> \brief Copy the active space ERI to C buffers
642 !> \param this Class pointer
643 !> \param i The i index of the value `val`
644 !> \param j The j index of the value `val`
645 !> \param k The k index of the value `val`
646 !> \param l The l index of the value `val`
647 !> \param val The value at the given index
648 !> \return Always true to continue with the loop
649 !> \author Tiziano Mueller
650 ! **************************************************************************************************
651  LOGICAL FUNCTION eri2array_func(this, i, j, k, l, val) RESULT(cont)
652  CLASS(eri2array), INTENT(inout) :: this
653  INTEGER, INTENT(in) :: i, j, k, l
654  REAL(kind=dp), INTENT(in) :: val
655 
656  this%coords(4*(this%idx - 1) + 1) = i
657  this%coords(4*(this%idx - 1) + 2) = j
658  this%coords(4*(this%idx - 1) + 3) = k
659  this%coords(4*(this%idx - 1) + 4) = l
660  this%values(this%idx) = val
661 
662  this%idx = this%idx + 1
663 
664  cont = .true.
665  END FUNCTION eri2array_func
666 
667 END MODULE libcp2k
some minimal info about CP2K, including its version and license
Definition: cp2k_info.F:16
character(len= *), parameter, public cp2k_version
Definition: cp2k_info.F:40
subroutine, public run_input(input_declaration, input_file_path, output_file_path, initial_variables, mpi_comm)
runs the given input
Definition: cp2k_runs.F:958
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
Definition: cp_fm_types.F:643
interface to use cp2k as library
Definition: f77_interface.F:20
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 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
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)
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
subroutine, public get_result_r1(env_id, description, N, RESULT, res_exist, ierr)
gets a result from CP2K that is a real 1D array
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 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)
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
integer(c_long) function cp2k_active_space_get_eri_nze_count(f_env_id)
Get the number of non-zero elements of the ERI.
Definition: libcp2k.F:563
logical function eri2array_func(this, i, j, k, l, val)
Copy the active space ERI to C buffers.
Definition: libcp2k.F:652
integer(c_long) function 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:511
integer(c_long) function 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:602
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, 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_nonbond, sab_almo, sab_kp, sab_kp_nosym, 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, 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, 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, ecoul_1c, rho0_s_rs, rho0_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, 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, rhs)
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.
Definition: qs_mo_types.F:397
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.