(git:3d5d2e5)
Loading...
Searching...
No Matches
xc_gauxc_functional.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#ifdef __GAUXC
9#include "gauxc/gauxc_config.f"
10#endif
11
15 USE cell_types, ONLY: cell_type
17 USE cp_dbcsr_api, ONLY: dbcsr_add,&
29 USE input_constants, ONLY: do_gpu_auto,&
37 USE iso_c_binding, ONLY: c_char,&
38 c_double,&
39 c_int,&
40 c_null_char
41 USE kinds, ONLY: default_path_length,&
43 dp
44 USE message_passing, ONLY: mp_comm_self,&
51 USE qs_kind_types, ONLY: get_qs_kind,&
52 has_nlcc,&
54 USE qs_ks_types, ONLY: qs_ks_env_type,&
56 USE qs_rho_types, ONLY: qs_rho_get,&
64 USE xc_gauxc_interface, ONLY: &
65 cp_gauxc_basisset_type, cp_gauxc_grid_type, cp_gauxc_integrator_type, &
66 cp_gauxc_molecule_type, cp_gauxc_status_type, cp_gauxc_xc_gradient_type, cp_gauxc_xc_type, &
67 gauxc_check_status, gauxc_compute_xc, gauxc_compute_xc_gradient, gauxc_create_basisset, &
68 gauxc_create_grid, gauxc_create_integrator, gauxc_create_molecule, gauxc_destroy_basisset, &
69 gauxc_destroy_grid, gauxc_destroy_integrator, gauxc_destroy_molecule, &
70 gauxc_write_basisset_hdf5, gauxc_write_molecule_hdf5
72#include "../base/base_uses.f90"
73
74 IMPLICIT NONE
75
76 PRIVATE
77
78 LOGICAL, PARAMETER :: debug_this_module = .true.
79 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'xc_gauxc_functional'
80
84
85 INTERFACE
86 INTEGER(c_int) FUNCTION c_setenv(name, value, overwrite) BIND(C, name="setenv")
87 IMPORT :: c_char, c_int
88 CHARACTER(KIND=c_char), DIMENSION(*), INTENT(IN) :: name, value
89 INTEGER(c_int), VALUE :: overwrite
90 END FUNCTION c_setenv
91
92 INTEGER(c_int) FUNCTION c_unsetenv(name) BIND(C, name="unsetenv")
93 IMPORT :: c_char, c_int
94 CHARACTER(KIND=c_char), DIMENSION(*), INTENT(IN) :: name
95 END FUNCTION c_unsetenv
96 END INTERFACE
97
98CONTAINS
99
100! **************************************************************************************************
101!> \brief Return whether every GAPW kind uses an all-electron potential.
102!> \param qs_kind_set ...
103!> \return ...
104! **************************************************************************************************
105 FUNCTION gauxc_gapw_all_all_electron(qs_kind_set) RESULT(all_all_electron)
106 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
107 LOGICAL :: all_all_electron
108
109 INTEGER :: ikind
110 TYPE(all_potential_type), POINTER :: all_potential
111
112 cpassert(ASSOCIATED(qs_kind_set))
113
114 all_all_electron = .true.
115 DO ikind = 1, SIZE(qs_kind_set)
116 NULLIFY (all_potential)
117 CALL get_qs_kind(qs_kind_set(ikind), all_potential=all_potential)
118 IF (.NOT. ASSOCIATED(all_potential)) THEN
119 all_all_electron = .false.
120 EXIT
121 END IF
122 END DO
123
124 END FUNCTION gauxc_gapw_all_all_electron
125
126! **************************************************************************************************
127!> \brief Set the GauXC Skala atom chunk environment knob when the CP2K keyword is explicit.
128!> \param atom_chunk_size ...
129!> \param is_explicit ...
130! **************************************************************************************************
131 SUBROUTINE set_gauxc_model_atom_chunk_env(atom_chunk_size, is_explicit)
132 INTEGER, INTENT(IN) :: atom_chunk_size
133 LOGICAL, INTENT(IN) :: is_explicit
134
135 CHARACTER(LEN=32) :: chunk_value
136 INTEGER(c_int) :: ierr
137
138 IF (.NOT. is_explicit) RETURN
139
140 IF (atom_chunk_size < 0) THEN
141 ierr = c_unsetenv("GAUXC_ONEDFT_ATOM_CHUNK_SIZE"//c_null_char)
142 ELSE
143 WRITE (chunk_value, '(I0)') atom_chunk_size
144 ierr = c_setenv( &
145 "GAUXC_ONEDFT_ATOM_CHUNK_SIZE"//c_null_char, &
146 trim(chunk_value)//c_null_char, &
147 1_c_int)
148 END IF
149 IF (ierr /= 0_c_int) THEN
150 CALL cp_abort(__location__, &
151 "Could not set GAUXC_ONEDFT_ATOM_CHUNK_SIZE for GauXC Skala.")
152 END IF
153 END SUBROUTINE set_gauxc_model_atom_chunk_env
154
155! **************************************************************************************************
156!> \brief ...
157!> \param dbcsr_mat ...
158!> \param dense_mat ...
159!> \param para_env ...
160! **************************************************************************************************
161 SUBROUTINE dbcsr_to_dense(dbcsr_mat, dense_mat, para_env)
165 dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_type_antisymmetric, &
166 dbcsr_type_symmetric
167 TYPE(dbcsr_p_type), INTENT(IN) :: dbcsr_mat
168 REAL(c_double), ALLOCATABLE, DIMENSION(:, :), &
169 INTENT(INOUT) :: dense_mat
170 TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
171
172 CHARACTER :: matrix_type
173 INTEGER :: col_end, col_start, icol, irow, ncol, &
174 ncols, nrow, nrows, numnodes, row_end, &
175 row_start
176 LOGICAL :: has_symmetry, transposed
177 REAL(c_double), POINTER :: block(:, :)
178 REAL(kind=dp) :: symmetry_sign
179 TYPE(dbcsr_distribution_type) :: dist
180 TYPE(dbcsr_iterator_type) :: iter
181
182 CALL dbcsr_get_info(dbcsr_mat%matrix, &
183 nfullrows_total=nrows, &
184 nfullcols_total=ncols, &
185 distribution=dist)
186 CALL dbcsr_distribution_get(dist, numnodes=numnodes)
187 matrix_type = dbcsr_get_matrix_type(dbcsr_mat%matrix)
188 has_symmetry = matrix_type == dbcsr_type_symmetric .OR. matrix_type == dbcsr_type_antisymmetric
189 symmetry_sign = 1.0_dp
190 IF (matrix_type == dbcsr_type_antisymmetric) symmetry_sign = -1.0_dp
191
192 IF (.NOT. ALLOCATED(dense_mat)) THEN
193 ALLOCATE (dense_mat(nrows, ncols))
194 ELSE IF (.NOT. all(shape(dense_mat) == [nrows, ncols])) THEN
195 DEALLOCATE (dense_mat)
196 ALLOCATE (dense_mat(nrows, ncols))
197 ELSE
198 cpassert(all(shape(dense_mat) == [nrows, ncols]))
199 END IF
200 dense_mat = 0._dp
201
202 ! Replicated DBCSR blocks must enter the following MPI sum exactly once.
203 IF (numnodes == 1 .AND. para_env%num_pe > 1 .AND. para_env%mepos /= 0) RETURN
204
205 ! Stored blocks and their symmetry mirrors occupy disjoint dense slices.
206!$OMP PARALLEL DEFAULT(NONE) SHARED(dbcsr_mat, dense_mat, has_symmetry, symmetry_sign) &
207!$OMP PRIVATE(iter, irow, icol, block, transposed, row_start, col_start, nrow, ncol, row_end, col_end)
208 CALL dbcsr_iterator_readonly_start(iter, dbcsr_mat%matrix)
209 DO WHILE (dbcsr_iterator_blocks_left(iter))
210 CALL dbcsr_iterator_next_block(iter, irow, icol, block, transposed=transposed, &
211 row_size=nrow, col_size=ncol, row_offset=row_start, col_offset=col_start)
212 row_end = row_start + nrow - 1
213 col_end = col_start + ncol - 1
214 IF (transposed) THEN
215 dense_mat(row_start:row_end, col_start:col_end) = symmetry_sign*transpose(block)
216 ELSE
217 dense_mat(row_start:row_end, col_start:col_end) = block
218 END IF
219 IF (has_symmetry .AND. irow /= icol) THEN
220 dense_mat(col_start:col_end, row_start:row_end) = &
221 symmetry_sign*transpose(dense_mat(row_start:row_end, col_start:col_end))
222 END IF
223 END DO
224 CALL dbcsr_iterator_stop(iter)
225!$OMP END PARALLEL
226
227 END SUBROUTINE dbcsr_to_dense
228
229! ******, ***********************************************************************************
230!> \brief Convert a dense symmetric matrix to a DBCSR matrix with full upper block structure.
231!> This creates all upper-triangular blocks, not just those present in a template.
232!> This is needed because GauXC computes VXC for the full dense density matrix.
233!> \param dense_mat Input dense matrix
234!> \param template_dbcsr Template DBCSR matrix for distribution and block sizes
235!> \return dbcsr_mat Output DBCSR matrix with full upper block structure
236! **************************************************************************************************
237 FUNCTION dense_to_dbcsr(dense_mat, template_dbcsr) RESULT(dbcsr_mat)
238 USE cp_dbcsr_api, ONLY: &
239 dbcsr_create, &
245 dbcsr_init_p, &
248 dbcsr_type_symmetric, &
250 REAL(c_double), DIMENSION(:, :), INTENT(IN) :: dense_mat
251 TYPE(dbcsr_p_type), INTENT(IN) :: template_dbcsr
252 TYPE(dbcsr_p_type) :: dbcsr_mat
253
254 INTEGER :: col, icol, irow, mynode, nblkcols_total, &
255 nblkrows_total, ncols, nrows, owner, &
256 row
257 INTEGER, ALLOCATABLE, DIMENSION(:) :: c_offset, r_offset
258 INTEGER, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
259 TYPE(dbcsr_distribution_type) :: dist
260
261 CALL dbcsr_get_info(template_dbcsr%matrix, &
262 row_blk_size=row_blk_size, &
263 col_blk_size=col_blk_size, &
264 nblkrows_total=nblkrows_total, &
265 nblkcols_total=nblkcols_total, &
266 nfullrows_total=nrows, &
267 nfullcols_total=ncols, &
268 distribution=dist)
269 CALL dbcsr_distribution_get(dist, mynode=mynode)
270
271 cpassert(nrows == SIZE(dense_mat, 1))
272 cpassert(ncols == SIZE(dense_mat, 2))
273
274 CALL dbcsr_init_p(dbcsr_mat%matrix)
275 CALL dbcsr_create(dbcsr_mat%matrix, &
276 template=template_dbcsr%matrix, &
277 name="VXC from GauXC (dense)", &
278 matrix_type=dbcsr_type_symmetric)
279 CALL dbcsr_work_create(dbcsr_mat%matrix, work_mutable=.true.)
280
281 ALLOCATE (r_offset(nblkrows_total), c_offset(nblkcols_total))
282
283 r_offset(1) = 1
284 DO row = 2, nblkrows_total
285 r_offset(row) = r_offset(row - 1) + row_blk_size(row - 1)
286 END DO
287 c_offset(1) = 1
288 DO col = 2, nblkcols_total
289 c_offset(col) = c_offset(col - 1) + col_blk_size(col - 1)
290 END DO
291
292 DO irow = 1, nblkrows_total
293 DO icol = 1, nblkcols_total
294 IF (irow > icol) cycle
295 CALL dbcsr_get_stored_coordinates(dbcsr_mat%matrix, irow, icol, owner)
296 IF (owner /= mynode) cycle
297 CALL dbcsr_put_block(dbcsr_mat%matrix, irow, icol, &
298 0.5_dp*( &
299 dense_mat(r_offset(irow):r_offset(irow) + row_blk_size(irow) - 1, &
300 c_offset(icol):c_offset(icol) + col_blk_size(icol) - 1) + &
301 transpose(dense_mat(r_offset(icol):r_offset(icol) + row_blk_size(icol) - 1, &
302 c_offset(irow):c_offset(irow) + col_blk_size(irow) - 1))))
303 END DO
304 END DO
305
306 CALL dbcsr_finalize(dbcsr_mat%matrix)
307
308 DEALLOCATE (r_offset, c_offset)
309
310 END FUNCTION dense_to_dbcsr
311
312! **************************************************************************************************
313!> \brief ...
314!> \param xc_section ...
315!> \return ...
316! **************************************************************************************************
317 FUNCTION get_gauxc_functional(xc_section) RESULT(gauxc_functional_section)
318 TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
319 TYPE(section_vals_type), POINTER :: gauxc_functional_section
320
321 INTEGER :: ifun
322 TYPE(section_vals_type), POINTER :: functionals, xc_fun
323
324 NULLIFY (gauxc_functional_section)
325
326 functionals => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
327 IF (.NOT. ASSOCIATED(functionals)) THEN
328 cpabort("XC_FUNCTIONAL section not found")
329 END IF
330
331 ifun = 0
332 DO
333 ifun = ifun + 1
334 xc_fun => section_vals_get_subs_vals2(functionals, i_section=ifun)
335 IF (.NOT. ASSOCIATED(xc_fun)) EXIT
336 IF (xc_fun%section%name /= "GAUXC" .OR. ifun > 1) THEN
337 cpabort("GauXC functionals are mutually exclusive with any other functional.")
338 END IF
339 gauxc_functional_section => xc_fun
340 END DO
341
342 IF (.NOT. ASSOCIATED(gauxc_functional_section)) THEN
343 cpabort("No XC functional found in XC_FUNCTIONAL section")
344 END IF
345 END FUNCTION get_gauxc_functional
346
347! **************************************************************************************************
348!> \brief ...
349!> \param xc_section ...
350!> \return ...
351! **************************************************************************************************
352 FUNCTION xc_section_uses_gauxc(xc_section) RESULT(uses_gauxc)
353 TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
354 LOGICAL :: uses_gauxc
355
356 INTEGER :: ifun
357 TYPE(section_vals_type), POINTER :: functionals, xc_fun
358
359 uses_gauxc = .false.
360 IF (.NOT. ASSOCIATED(xc_section)) RETURN
361
362 functionals => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
363 IF (.NOT. ASSOCIATED(functionals)) RETURN
364
365 ifun = 0
366 DO
367 ifun = ifun + 1
368 xc_fun => section_vals_get_subs_vals2(functionals, i_section=ifun)
369 IF (.NOT. ASSOCIATED(xc_fun)) EXIT
370 IF (xc_fun%section%name == "GAUXC" .OR. xc_fun%section%name == "SKALA_NATIVE") THEN
371 uses_gauxc = .true.
372 EXIT
373 END IF
374 END DO
375
376 END FUNCTION xc_section_uses_gauxc
377
378! **************************************************************************************************
379!> \brief Return whether GauXC GAPW mode sees pseudopotential kinds.
380!> \param qs_kind_set ...
381!> \return ...
382! **************************************************************************************************
383 FUNCTION gauxc_gapw_has_pseudopotentials(qs_kind_set) RESULT(has_pseudopotentials)
384 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
385 LOGICAL :: has_pseudopotentials
386
387 INTEGER :: ikind
388 TYPE(gth_potential_type), POINTER :: gth_potential
389 TYPE(sgp_potential_type), POINTER :: sgp_potential
390
391 cpassert(ASSOCIATED(qs_kind_set))
392
393 has_pseudopotentials = .false.
394 DO ikind = 1, SIZE(qs_kind_set)
395 NULLIFY (gth_potential, sgp_potential)
396 CALL get_qs_kind(qs_kind_set(ikind), &
397 gth_potential=gth_potential, &
398 sgp_potential=sgp_potential)
399 IF (ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)) THEN
400 has_pseudopotentials = .true.
401 EXIT
402 END IF
403 END DO
404
406
407! **************************************************************************************************
408!> \brief Return whether every GAPW kind uses a pseudopotential.
409!> \param qs_kind_set ...
410!> \return ...
411! **************************************************************************************************
412 FUNCTION gauxc_gapw_all_pseudopotentials(qs_kind_set) RESULT(all_pseudopotentials)
413 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
414 LOGICAL :: all_pseudopotentials
415
416 INTEGER :: ikind
417 TYPE(gth_potential_type), POINTER :: gth_potential
418 TYPE(sgp_potential_type), POINTER :: sgp_potential
419
420 cpassert(ASSOCIATED(qs_kind_set))
421
422 all_pseudopotentials = .true.
423 DO ikind = 1, SIZE(qs_kind_set)
424 NULLIFY (gth_potential, sgp_potential)
425 CALL get_qs_kind(qs_kind_set(ikind), &
426 gth_potential=gth_potential, &
427 sgp_potential=sgp_potential)
428 IF (.NOT. (ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential))) THEN
429 all_pseudopotentials = .false.
430 EXIT
431 END IF
432 END DO
433
435
436! **************************************************************************************************
437!> \brief Return whether GauXC GAPW mode sees pseudopotential one-center GAPW kinds.
438!> \param qs_kind_set ...
439!> \return ...
440! **************************************************************************************************
441 FUNCTION gauxc_gapw_has_paw_pseudopotentials(qs_kind_set) RESULT(has_paw_pseudopotentials)
442 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
443 LOGICAL :: has_paw_pseudopotentials
444
445 INTEGER :: ikind
446 LOGICAL :: paw_atom
447 TYPE(gth_potential_type), POINTER :: gth_potential
448 TYPE(sgp_potential_type), POINTER :: sgp_potential
449
450 cpassert(ASSOCIATED(qs_kind_set))
451
452 has_paw_pseudopotentials = .false.
453 DO ikind = 1, SIZE(qs_kind_set)
454 NULLIFY (gth_potential, sgp_potential)
455 CALL get_qs_kind(qs_kind_set(ikind), &
456 gth_potential=gth_potential, &
457 paw_atom=paw_atom, &
458 sgp_potential=sgp_potential)
459 IF ((ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)) .AND. &
460 paw_atom) THEN
461 has_paw_pseudopotentials = .true.
462 EXIT
463 END IF
464 END DO
465
467
468! **************************************************************************************************
469!> \brief Check the current periodic scope of the CP2K-GauXC bridge
470!> \param dft_control ...
471!> \param cell ...
472!> \param qs_kind_set ...
473!> \param do_kpoints ...
474!> \param periodic_reference ...
475!> \note This path keeps isolated validation cells usable under PERIODIC XYZ.
476!> It intentionally does not implement compact periodic GauXC quadrature.
477! **************************************************************************************************
478 SUBROUTINE ensure_gauxc_periodic_reference_scope( &
479 dft_control, cell, qs_kind_set, do_kpoints, periodic_reference)
480 TYPE(dft_control_type), POINTER :: dft_control
481 TYPE(cell_type), POINTER :: cell
482 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
483 LOGICAL, INTENT(IN) :: do_kpoints, periodic_reference
484
485 INTEGER :: ikind
486 LOGICAL :: is_periodic
487 TYPE(gth_potential_type), POINTER :: gth_potential
488 TYPE(sgp_potential_type), POINTER :: sgp_potential
489
490 cpassert(ASSOCIATED(dft_control))
491 cpassert(ASSOCIATED(qs_kind_set))
492
493 is_periodic = .false.
494 IF (ASSOCIATED(cell)) is_periodic = any(cell%perd /= 0)
495
496 IF (do_kpoints) THEN
497 CALL cp_abort(__location__, &
498 "GauXC currently supports only Gamma-only density matrices in CP2K. "// &
499 "Periodic k-point density matrices require a dedicated GauXC periodic interface.")
500 END IF
501 IF (dft_control%nimages /= 1) THEN
502 CALL cp_abort(__location__, &
503 "GauXC currently supports only a single AO image in CP2K. "// &
504 "Periodic neighbour-cell AO blocks require a dedicated GauXC periodic interface.")
505 END IF
506 IF (.NOT. is_periodic) RETURN
507
508 IF (.NOT. periodic_reference) THEN
509 CALL cp_abort(__location__, &
510 "Periodic GauXC calculations in CP2K require GAUXC%PERIODIC_REFERENCE T. "// &
511 "This opt-in documents that the current path is only an isolated-cell, "// &
512 "Gamma-only, single-image METHOD GPW reference path using GauXC molecular "// &
513 "quadrature, not a dedicated periodic GauXC interface.")
514 END IF
515
516 IF (.NOT. all(cell%perd == 1)) THEN
517 CALL cp_abort(__location__, &
518 "The current GauXC isolated-cell reference path supports only PERIODIC XYZ. "// &
519 "Partial periodicity requires a dedicated GauXC periodic interface.")
520 END IF
521 IF (.NOT. dft_control%qs_control%gpw) THEN
522 CALL cp_abort(__location__, &
523 "The current GauXC isolated-cell reference path is limited to METHOD GPW with GTH "// &
524 "pseudopotentials. GAPW, GAPW_XC, and other QS methods are not supported here.")
525 END IF
526
527 DO ikind = 1, SIZE(qs_kind_set)
528 NULLIFY (gth_potential, sgp_potential)
529 CALL get_qs_kind(qs_kind_set(ikind), &
530 gth_potential=gth_potential, &
531 sgp_potential=sgp_potential)
532 IF (.NOT. ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)) THEN
533 CALL cp_abort(__location__, &
534 "The current GauXC isolated-cell reference path is limited to GTH pseudopotentials. "// &
535 "Use non-periodic all-electron GAPW validation for molecular GAPW cases.")
536 END IF
537 END DO
538
539 END SUBROUTINE ensure_gauxc_periodic_reference_scope
540
541! **************************************************************************************************
542!> \brief adds a replicated GauXC energy gradient to the local CP2K force accumulator
543!> \param exc_grad ...
544!> \param force ...
545!> \param atomic_kind_set ...
546!> \param para_env ...
547! **************************************************************************************************
548 SUBROUTINE add_gauxc_gradient_to_force(exc_grad, force, atomic_kind_set, para_env)
549 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: exc_grad
550 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
551 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
552 TYPE(mp_para_env_type), POINTER :: para_env
553
554 INTEGER :: ia, iatom, ikind, natom_kind
555 TYPE(atomic_kind_type), POINTER :: atomic_kind
556
557 cpassert(ASSOCIATED(force))
558 cpassert(ASSOCIATED(atomic_kind_set))
559
560 IF (para_env%mepos /= 0) RETURN
561
562 DO ikind = 1, SIZE(atomic_kind_set, 1)
563 atomic_kind => atomic_kind_set(ikind)
564 CALL get_atomic_kind(atomic_kind=atomic_kind, natom=natom_kind)
565 DO ia = 1, natom_kind
566 iatom = atomic_kind%atom_list(ia)
567 force(ikind)%rho_elec(:, ia) = force(ikind)%rho_elec(:, ia) + &
568 exc_grad(3*iatom - 2:3*iatom)
569 END DO
570 END DO
571
572 END SUBROUTINE add_gauxc_gradient_to_force
573
574! **************************************************************************************************
575!> \brief compute a GauXC XC energy for diagnostic finite differences
576!> \param particle_set_eval ...
577!> \param qs_kind_set ...
578!> \param density_scalar ...
579!> \param nspins ...
580!> \param model_name ...
581!> \param xc_fun_name ...
582!> \param grid_type ...
583!> \param radial_quadrature ...
584!> \param pruning_scheme ...
585!> \param lb_exec_space ...
586!> \param int_exec_space ...
587!> \param lwd_kernel ...
588!> \param batch_size ...
589!> \param device_runtime_fill_fraction ...
590!> \param exc ...
591!> \param density_zeta ...
592! **************************************************************************************************
593 SUBROUTINE gauxc_xc_energy_for_particles( &
594 particle_set_eval, qs_kind_set, density_scalar, nspins, model_name, &
595 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
596 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, exc, density_zeta)
597 TYPE(particle_type), DIMENSION(:), INTENT(IN) :: particle_set_eval
598 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
599 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: density_scalar
600 INTEGER, INTENT(IN) :: nspins
601 CHARACTER(LEN=*), INTENT(IN) :: model_name, xc_fun_name, grid_type, radial_quadrature, &
602 pruning_scheme, lb_exec_space, int_exec_space, lwd_kernel
603 INTEGER, INTENT(IN) :: batch_size
604 REAL(kind=dp), INTENT(IN) :: device_runtime_fill_fraction
605 REAL(kind=dp), INTENT(OUT) :: exc
606 REAL(kind=dp), DIMENSION(:, :), INTENT(IN), &
607 OPTIONAL :: density_zeta
608
609 TYPE(cp_gauxc_basisset_type) :: gauxc_basis_fd
610 TYPE(cp_gauxc_grid_type) :: gauxc_grid_fd
611 TYPE(cp_gauxc_integrator_type) :: gauxc_integrator_fd
612 TYPE(cp_gauxc_molecule_type) :: gauxc_mol_fd
613 TYPE(cp_gauxc_status_type) :: gauxc_status
614 TYPE(cp_gauxc_xc_type) :: gauxc_xc_result
615
616 gauxc_mol_fd = gauxc_create_molecule(particle_set_eval, gauxc_status)
617 CALL gauxc_check_status(gauxc_status)
618 gauxc_basis_fd = gauxc_create_basisset(qs_kind_set, particle_set_eval, gauxc_status)
619 CALL gauxc_check_status(gauxc_status)
620 gauxc_grid_fd = gauxc_create_grid( &
621 gauxc_mol_fd, &
622 gauxc_basis_fd, &
623 grid_type, &
624 radial_quadrature, &
625 pruning_scheme, &
626 lb_exec_space, &
627 batch_size, &
628 device_runtime_fill_fraction, &
629 gauxc_status, &
630 mpi_comm=mp_comm_self%get_handle(), &
631 force_new_runtime=.true., &
632 int_exec_space=int_exec_space)
633 CALL gauxc_check_status(gauxc_status)
634 gauxc_integrator_fd = gauxc_create_integrator( &
635 trim(xc_fun_name), &
636 gauxc_grid_fd, &
637 int_exec_space, &
638 lwd_kernel, &
639 nspins, &
640 gauxc_status)
641 CALL gauxc_check_status(gauxc_status)
642
643 IF (nspins == 1) THEN
644 gauxc_xc_result = gauxc_compute_xc( &
645 gauxc_integrator_fd, &
646 density_scalar, &
647 nspins=nspins, &
648 status=gauxc_status, &
649 model=trim(model_name))
650 ELSE
651 cpassert(nspins == 2)
652 cpassert(PRESENT(density_zeta))
653 gauxc_xc_result = gauxc_compute_xc( &
654 gauxc_integrator_fd, &
655 density_scalar, &
656 density_zeta, &
657 nspins, &
658 gauxc_status, &
659 model=trim(model_name))
660 END IF
661 CALL gauxc_check_status(gauxc_status)
662 exc = gauxc_xc_result%exc
663
664 IF (ALLOCATED(gauxc_xc_result%vxc_scalar)) DEALLOCATE (gauxc_xc_result%vxc_scalar)
665 IF (ALLOCATED(gauxc_xc_result%vxc_zeta)) DEALLOCATE (gauxc_xc_result%vxc_zeta)
666
667 CALL gauxc_destroy_integrator(gauxc_integrator_fd, gauxc_status)
668 CALL gauxc_check_status(gauxc_status)
669 CALL gauxc_destroy_grid(gauxc_grid_fd, gauxc_status)
670 CALL gauxc_check_status(gauxc_status)
671 CALL gauxc_destroy_basisset(gauxc_basis_fd, gauxc_status)
672 CALL gauxc_check_status(gauxc_status)
673 CALL gauxc_destroy_molecule(gauxc_mol_fd, gauxc_status)
674 CALL gauxc_check_status(gauxc_status)
675
676 END SUBROUTINE gauxc_xc_energy_for_particles
677
678! **************************************************************************************************
679!> \brief compute a finite-difference GauXC XC nuclear gradient at fixed density
680!> \param particle_set ...
681!> \param qs_kind_set ...
682!> \param density_scalar ...
683!> \param nspins ...
684!> \param model_name ...
685!> \param xc_fun_name ...
686!> \param grid_type ...
687!> \param radial_quadrature ...
688!> \param pruning_scheme ...
689!> \param lb_exec_space ...
690!> \param int_exec_space ...
691!> \param lwd_kernel ...
692!> \param batch_size ...
693!> \param device_runtime_fill_fraction ...
694!> \param dx ...
695!> \param para_env ...
696!> \param exc_grad ...
697!> \param density_zeta ...
698! **************************************************************************************************
699 SUBROUTINE gauxc_xc_gradient_fd( &
700 particle_set, qs_kind_set, density_scalar, nspins, model_name, &
701 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
702 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, dx, para_env, exc_grad, &
703 density_zeta)
704 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
705 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
706 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: density_scalar
707 INTEGER, INTENT(IN) :: nspins
708 CHARACTER(LEN=*), INTENT(IN) :: model_name, xc_fun_name, grid_type, radial_quadrature, &
709 pruning_scheme, lb_exec_space, int_exec_space, lwd_kernel
710 INTEGER, INTENT(IN) :: batch_size
711 REAL(kind=dp), INTENT(IN) :: device_runtime_fill_fraction, dx
712 TYPE(mp_para_env_type), POINTER :: para_env
713 REAL(kind=dp), ALLOCATABLE, DIMENSION(:), &
714 INTENT(OUT) :: exc_grad
715 REAL(kind=dp), DIMENSION(:, :), INTENT(IN), &
716 OPTIONAL :: density_zeta
717
718 INTEGER :: iatom, idir
719 REAL(kind=dp) :: xc_minus, xc_plus
720 TYPE(particle_type), ALLOCATABLE, DIMENSION(:) :: particle_set_minus, particle_set_plus
721
722 cpassert(ASSOCIATED(particle_set))
723 cpassert(dx > 0.0_dp)
724
725 ALLOCATE (exc_grad(3*SIZE(particle_set)))
726 exc_grad = 0.0_dp
727
728 IF (para_env%mepos == 0) THEN
729 ALLOCATE (particle_set_minus(SIZE(particle_set)), particle_set_plus(SIZE(particle_set)))
730
731 DO iatom = 1, SIZE(particle_set)
732 DO idir = 1, 3
733 particle_set_minus = particle_set
734 particle_set_plus = particle_set
735 particle_set_minus(iatom)%r(idir) = particle_set_minus(iatom)%r(idir) - dx
736 particle_set_plus(iatom)%r(idir) = particle_set_plus(iatom)%r(idir) + dx
737 IF (PRESENT(density_zeta)) THEN
738 CALL gauxc_xc_energy_for_particles( &
739 particle_set_plus, qs_kind_set, density_scalar, nspins, model_name, &
740 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
741 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_plus, &
742 density_zeta=density_zeta)
743 CALL gauxc_xc_energy_for_particles( &
744 particle_set_minus, qs_kind_set, density_scalar, nspins, model_name, &
745 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
746 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_minus, &
747 density_zeta=density_zeta)
748 ELSE
749 CALL gauxc_xc_energy_for_particles( &
750 particle_set_plus, qs_kind_set, density_scalar, nspins, model_name, &
751 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
752 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_plus)
753 CALL gauxc_xc_energy_for_particles( &
754 particle_set_minus, qs_kind_set, density_scalar, nspins, model_name, &
755 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
756 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_minus)
757 END IF
758 exc_grad(3*iatom - 3 + idir) = (xc_plus - xc_minus)/(2.0_dp*dx)
759 END DO
760 END DO
761
762 DEALLOCATE (particle_set_minus, particle_set_plus)
763 END IF
764
765 CALL para_env%bcast(exc_grad, 0)
766
767 END SUBROUTINE gauxc_xc_gradient_fd
768
769! **************************************************************************************************
770!> \brief finite-difference check of the molecular GauXC XC virial diagnostic
771!> \param exc_grad ...
772!> \param particle_set ...
773!> \param qs_kind_set ...
774!> \param density_scalar ...
775!> \param nspins ...
776!> \param model_name ...
777!> \param xc_fun_name ...
778!> \param grid_type ...
779!> \param radial_quadrature ...
780!> \param pruning_scheme ...
781!> \param lb_exec_space ...
782!> \param int_exec_space ...
783!> \param lwd_kernel ...
784!> \param batch_size ...
785!> \param device_runtime_fill_fraction ...
786!> \param dx ...
787!> \param para_env ...
788!> \param density_zeta ...
789! **************************************************************************************************
790 SUBROUTINE debug_gauxc_molecular_virial( &
791 exc_grad, particle_set, qs_kind_set, density_scalar, nspins, model_name, &
792 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
793 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, dx, para_env, density_zeta)
794 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: exc_grad
795 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
796 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
797 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: density_scalar
798 INTEGER, INTENT(IN) :: nspins
799 CHARACTER(LEN=*), INTENT(IN) :: model_name, xc_fun_name, grid_type, radial_quadrature, &
800 pruning_scheme, lb_exec_space, int_exec_space, lwd_kernel
801 INTEGER, INTENT(IN) :: batch_size
802 REAL(kind=dp), INTENT(IN) :: device_runtime_fill_fraction, dx
803 TYPE(mp_para_env_type), POINTER :: para_env
804 REAL(kind=dp), DIMENSION(:, :), INTENT(IN), &
805 OPTIONAL :: density_zeta
806
807 INTEGER :: iatom, iw
808 REAL(kind=dp) :: analytic_trace, diff_trace, &
809 numerical_trace, xc_minus, xc_plus
810 REAL(kind=dp), DIMENSION(3) :: center, displacement, grad
811 TYPE(particle_type), ALLOCATABLE, DIMENSION(:) :: particle_set_minus, particle_set_plus
812
813 cpassert(ASSOCIATED(particle_set))
814 cpassert(SIZE(exc_grad) == 3*SIZE(particle_set))
815
816 IF (para_env%mepos /= 0) RETURN
817
818 center = 0.0_dp
819 DO iatom = 1, SIZE(particle_set)
820 center = center + particle_set(iatom)%r
821 END DO
822 center = center/real(SIZE(particle_set), dp)
823
824 ALLOCATE (particle_set_minus(SIZE(particle_set)), particle_set_plus(SIZE(particle_set)))
825 particle_set_minus = particle_set
826 particle_set_plus = particle_set
827
828 analytic_trace = 0.0_dp
829 DO iatom = 1, SIZE(particle_set)
830 grad = exc_grad(3*iatom - 2:3*iatom)
831 displacement = particle_set(iatom)%r - center
832 analytic_trace = analytic_trace + dot_product(grad, displacement)
833 particle_set_minus(iatom)%r = center + (1.0_dp - dx)*displacement
834 particle_set_plus(iatom)%r = center + (1.0_dp + dx)*displacement
835 END DO
836 analytic_trace = analytic_trace/3.0_dp
837
838 IF (PRESENT(density_zeta)) THEN
839 CALL gauxc_xc_energy_for_particles( &
840 particle_set_plus, qs_kind_set, density_scalar, nspins, model_name, &
841 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
842 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_plus, &
843 density_zeta=density_zeta)
844 CALL gauxc_xc_energy_for_particles( &
845 particle_set_minus, qs_kind_set, density_scalar, nspins, model_name, &
846 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
847 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_minus, &
848 density_zeta=density_zeta)
849 ELSE
850 CALL gauxc_xc_energy_for_particles( &
851 particle_set_plus, qs_kind_set, density_scalar, nspins, model_name, &
852 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
853 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_plus)
854 CALL gauxc_xc_energy_for_particles( &
855 particle_set_minus, qs_kind_set, density_scalar, nspins, model_name, &
856 xc_fun_name, grid_type, radial_quadrature, pruning_scheme, lb_exec_space, &
857 int_exec_space, lwd_kernel, batch_size, device_runtime_fill_fraction, xc_minus)
858 END IF
859
860 numerical_trace = (xc_plus - xc_minus)/(2.0_dp*dx)/3.0_dp
861 diff_trace = analytic_trace - numerical_trace
862
864 IF (iw > 0) THEN
865 WRITE (unit=iw, fmt="(/,T2,A,1X,ES11.4)") &
866 "GAUXC| Molecular XC virial finite-difference dx", dx
867 WRITE (unit=iw, fmt="(T2,A,3(1X,ES19.11))") &
868 "GAUXC| Molecular XC virial FD 1/3 Trace", &
869 analytic_trace, numerical_trace, diff_trace
870 END IF
871
872 DEALLOCATE (particle_set_minus, particle_set_plus)
873
874 END SUBROUTINE debug_gauxc_molecular_virial
875
876! **************************************************************************************************
877!> \brief prints a force-based molecular XC virial diagnostic from GauXC gradients
878!> \param exc_grad ...
879!> \param particle_set ...
880!> \param para_env ...
881! **************************************************************************************************
882 SUBROUTINE print_gauxc_molecular_virial(exc_grad, particle_set, para_env)
883 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: exc_grad
884 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
885 TYPE(mp_para_env_type), POINTER :: para_env
886
887 CHARACTER(LEN=1), DIMENSION(3), PARAMETER :: label = ["x", "y", "z"]
888
889 INTEGER :: i, iatom, iw, j
890 REAL(kind=dp), DIMENSION(3) :: center, displacement, grad, grad_sum
891 REAL(kind=dp), DIMENSION(3, 3) :: molecular_virial
892
893 cpassert(ASSOCIATED(particle_set))
894 cpassert(SIZE(exc_grad) == 3*SIZE(particle_set))
895
896 IF (para_env%mepos /= 0) RETURN
897
898 center = 0.0_dp
899 DO iatom = 1, SIZE(particle_set)
900 center = center + particle_set(iatom)%r
901 END DO
902 center = center/real(SIZE(particle_set), dp)
903
904 grad_sum = 0.0_dp
905 molecular_virial = 0.0_dp
906 DO iatom = 1, SIZE(particle_set)
907 grad = exc_grad(3*iatom - 2:3*iatom)
908 displacement = particle_set(iatom)%r - center
909 grad_sum = grad_sum + grad
910 DO i = 1, 3
911 DO j = 1, 3
912 molecular_virial(i, j) = molecular_virial(i, j) + grad(i)*displacement(j)
913 END DO
914 END DO
915 END DO
916
918 IF (iw <= 0) RETURN
919
920 WRITE (unit=iw, fmt="(/,T2,A)") &
921 "GAUXC| Molecular XC gradient virial diagnostic [a.u.]"
922 WRITE (unit=iw, fmt="(T2,A,T20,A,T40,A,T60,A)") "GAUXC|", "x", "y", "z"
923 DO i = 1, 3
924 WRITE (unit=iw, fmt="(T2,A,1X,A1,3(1X,ES19.11))") &
925 "GAUXC|", label(i), molecular_virial(i, :)
926 END DO
927 WRITE (unit=iw, fmt="(T2,A,1X,ES19.11)") &
928 "GAUXC| Molecular XC gradient virial 1/3 Trace", &
929 (molecular_virial(1, 1) + molecular_virial(2, 2) + molecular_virial(3, 3))/3.0_dp
930 WRITE (unit=iw, fmt="(T2,A,3(1X,ES19.11))") &
931 "GAUXC| Molecular XC gradient sum", grad_sum
932 WRITE (unit=iw, fmt="(T2,A)") &
933 "GAUXC| Diagnostic only; this is not an analytical periodic stress tensor."
934
935 END SUBROUTINE print_gauxc_molecular_virial
936
937! **************************************************************************************************
938!> \brief Return information about the Skala functional
939!> \param functional section containing the SKALA subsection
940!> \param lsd if you are using lsd or lda
941!> \param reference the reference to the article where the functional is explained
942!> \param shortform the short definition of the functional
943!> \param needs the flags corresponding to the inputs needed by this
944!> functional are set to true (the flags not needed aren't touched)
945!> \param max_deriv the maximal derivative available
946! **************************************************************************************************
947 SUBROUTINE skala_info(functional, lsd, reference, shortform, needs, max_deriv)
948 TYPE(section_vals_type), POINTER :: functional
949 LOGICAL, INTENT(IN) :: lsd
950 CHARACTER(LEN=*), INTENT(OUT), OPTIONAL :: reference, shortform
951 TYPE(xc_rho_cflags_type), INTENT(INOUT), OPTIONAL :: needs
952 INTEGER, INTENT(OUT), OPTIONAL :: max_deriv
953
954 CHARACTER(LEN=default_path_length) :: model_key, model_name
955 CHARACTER(LEN=default_string_length) :: xc_fun_key, xc_fun_name
956
957 IF (functional%section%name == "SKALA_NATIVE") THEN
958 CALL section_vals_val_get(functional, "MODEL", c_val=model_name)
959 IF (PRESENT(reference)) reference = "CP2K-native SKALA TorchScript model "//trim(model_name)
960 IF (PRESENT(shortform)) shortform = "SKALA (native)"
961 IF (PRESENT(needs)) THEN
962 IF (lsd) THEN
963 needs%rho_spin = .true.
964 needs%drho_spin = .true.
965 needs%tau_spin = .true.
966 ELSE
967 needs%rho = .true.
968 needs%drho = .true.
969 needs%tau = .true.
970 END IF
971 END IF
972 IF (PRESENT(max_deriv)) max_deriv = 1
973 RETURN
974 END IF
975
976 CALL section_vals_val_get(functional, "FUNCTIONAL", c_val=xc_fun_name)
977 CALL section_vals_val_get(functional, "MODEL", c_val=model_name)
978 model_key = adjustl(model_name)
979 xc_fun_key = adjustl(xc_fun_name)
980 CALL uppercase(model_key)
981 CALL uppercase(xc_fun_key)
982
983 IF (PRESENT(reference)) THEN
984 IF (trim(model_key) == "NONE" .OR. trim(model_key) == "" .OR. &
985 trim(model_key) == trim(xc_fun_key)) THEN
986 reference = "Functional computed by GauXC (underlying: "//trim(xc_fun_name)//")"
987 ELSE
988 reference = "Functional computed by GauXC Skala model "//trim(model_name)
989 END IF
990 END IF
991 IF (PRESENT(shortform)) THEN
992 IF (trim(model_key) == "NONE" .OR. trim(model_key) == "" .OR. &
993 trim(model_key) == trim(xc_fun_key)) THEN
994 shortform = "GAUXC ("//trim(xc_fun_name)//")"
995 ELSE
996 shortform = "GAUXC Skala"
997 END IF
998 END IF
999 IF (PRESENT(needs)) THEN
1000 needs%rho = .true.
1001 IF (lsd) THEN
1002 needs%rho_spin = .true.
1003 END IF
1004 END IF
1005 IF (PRESENT(max_deriv)) max_deriv = 1
1006
1007 END SUBROUTINE skala_info
1008
1009! GauXC uses replicated dense density and VXC matrices. The DBCSR density matrix
1010! is distributed over MPI ranks, so apply_gauxc allreduces the dense copy before
1011! passing it to GauXC.
1012
1013! **************************************************************************************************
1014!> \brief Evaluate molecular GauXC, optionally for an explicit configuration density.
1015!> \param qs_env ...
1016!> \param xc_section ...
1017!> \param calculate_forces ...
1018!> \param rho_ao_external Optional spin/image AO density; supply all three external arguments together
1019!> \param exc_external Configuration XC energy, leaving the baseline energy unchanged
1020!> \param matrix_vxc_external Initially null; caller owns the returned spin AO potential matrices
1021!> \note External-density mode preserves baseline energy and KS storage and shares
1022!> the geometry/model cache.
1023! **************************************************************************************************
1024 SUBROUTINE apply_gauxc(qs_env, xc_section, calculate_forces, &
1025 rho_ao_external, exc_external, matrix_vxc_external)
1026 TYPE(qs_environment_type), INTENT(IN), POINTER :: qs_env
1027 TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
1028 LOGICAL, INTENT(IN) :: calculate_forces
1029 TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(IN), &
1030 OPTIONAL, POINTER :: rho_ao_external
1031 REAL(kind=dp), INTENT(OUT), OPTIONAL :: exc_external
1032 TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT), &
1033 OPTIONAL, POINTER :: matrix_vxc_external
1034
1035 CHARACTER(LEN=*), PARAMETER :: nonlocal_vdw_abort_message = &
1036 "GauXC does not support non-local VDW_POTENTIAL corrections. "// &
1037 "Use an additive PAIR_POTENTIAL dispersion correction or disable GauXC."
1038 REAL(kind=dp), PARAMETER :: gapw_fd_gradient_dx = 1.0e-4_dp
1039
1040 CHARACTER(LEN=default_path_length) :: model_key, model_name, output_path
1041 CHARACTER(LEN=default_string_length) :: gradient_runtime, gradient_runtime_key, grid_key, &
1042 int_exec_space_key, lb_exec_space_key, lwd_kernel_key, pruning_key, skala_runtime, &
1043 skala_runtime_key, xc_fun_key
1044 INTEGER :: atom_chunk_size, env_status, gpu_mode, &
1045 img, ispin, nimages
1046 LOGICAL :: atom_chunk_size_explicit, do_kpoints, external_density, gapw_method, &
1047 gapw_paw_pseudopotentials, gapw_pseudopotentials, grid_explicit, hdf5_output, &
1048 int_space_explicit, is_periodic, lb_space_explicit, low_spin_scope, molecular_virial, &
1049 molecular_virial_debug, need_xc_gradient, periodic_reference, pruning_explicit, use_cuda, &
1050 use_skala_model, write_hdf5_output
1051 REAL(kind=dp) :: exc_value, molecular_virial_debug_dx
1052 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: density_scalar, density_zeta
1053 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
1054 TYPE(cell_type), POINTER :: cell
1055 TYPE(cp_gauxc_cache_params) :: params
1056 TYPE(cp_gauxc_cache_type), POINTER :: cache
1057 TYPE(cp_gauxc_status_type) :: gauxc_status
1058 TYPE(cp_gauxc_xc_gradient_type) :: exc_grad
1059 TYPE(cp_gauxc_xc_type) :: gauxc_xc_result
1060 TYPE(dbcsr_p_type) :: vxc_zeta_tmp
1061 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_vxc
1062 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao
1063 TYPE(dft_control_type), POINTER :: dft_control
1064 TYPE(mp_para_env_type), POINTER :: para_env
1065 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1066 TYPE(qs_energy_type), POINTER :: energy
1067 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
1068 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1069 TYPE(qs_ks_env_type), POINTER :: ks_env
1070 TYPE(qs_rho_type), POINTER :: rho, rho_use, rho_xc
1071 TYPE(qs_scf_env_type), POINTER :: scf_env
1072 TYPE(section_vals_type), POINTER :: gauxc_functional_section
1073
1074 external_density = PRESENT(rho_ao_external)
1075 cpassert(PRESENT(exc_external) .EQV. external_density)
1076 cpassert(PRESENT(matrix_vxc_external) .EQV. external_density)
1077 IF (external_density) THEN
1078 cpassert(ASSOCIATED(rho_ao_external))
1079 cpassert(.NOT. ASSOCIATED(matrix_vxc_external))
1080 END IF
1081
1082 NULLIFY ( &
1083 atomic_kind_set, &
1084 cell, &
1085 dft_control, &
1086 energy, &
1087 force, &
1088 ks_env, &
1089 matrix_vxc, &
1090 para_env, &
1091 particle_set, &
1092 qs_kind_set, &
1093 rho, &
1094 rho_use, &
1095 rho_xc, &
1096 rho_ao, &
1097 scf_env)
1098
1099 CALL get_qs_env( &
1100 qs_env, &
1101 cell=cell, &
1102 dft_control=dft_control, &
1103 do_kpoints=do_kpoints, &
1104 energy=energy, &
1105 ks_env=ks_env, &
1106 matrix_vxc=matrix_vxc, &
1107 natom=params%natom, &
1108 atomic_kind_set=atomic_kind_set, &
1109 force=force, &
1110 para_env=para_env, &
1111 particle_set=particle_set, &
1112 qs_kind_set=qs_kind_set, &
1113 rho=rho, &
1114 rho_xc=rho_xc, &
1115 scf_env=scf_env)
1116
1117 gapw_method = dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc
1118 gapw_pseudopotentials = gapw_method .AND. &
1120 gapw_paw_pseudopotentials = gapw_method .AND. &
1122 IF (dft_control%qs_control%gapw_xc) THEN
1123 cpassert(ASSOCIATED(rho_xc))
1124 rho_use => rho_xc
1125 ELSE
1126 cpassert(ASSOCIATED(rho))
1127 rho_use => rho
1128 END IF
1129 CALL qs_rho_get( &
1130 rho_use, &
1131 rho_ao_kp=rho_ao)
1132
1133 IF (external_density) THEN
1134 rho_ao => rho_ao_external
1135 ! get_qs_env returned the baseline pointer. Never release or replace it.
1136 NULLIFY (matrix_vxc)
1137 END IF
1138 nimages = dft_control%nimages
1139 params%nspins = dft_control%nspins
1140 IF (external_density) THEN
1141 cpassert(SIZE(rho_ao, 1) == params%nspins)
1142 cpassert(SIZE(rho_ao, 2) == nimages)
1143 END IF
1144 low_spin_scope = external_density .OR. (dft_control%low_spin_roks .AND. gapw_method)
1145 is_periodic = .false.
1146 IF (ASSOCIATED(cell)) is_periodic = any(cell%perd /= 0)
1147
1148 IF (ASSOCIATED(qs_env%dispersion_env)) THEN
1149 IF (qs_env%dispersion_env%type == xc_vdw_fun_nonloc) THEN
1150 cpabort(nonlocal_vdw_abort_message)
1151 END IF
1152 END IF
1153 NULLIFY (vxc_zeta_tmp%matrix)
1154
1155 gauxc_functional_section => get_gauxc_functional(xc_section)
1156 CALL section_vals_val_get( &
1157 gauxc_functional_section, &
1158 "FUNCTIONAL", &
1159 c_val=params%xc_fun_name)
1160 CALL section_vals_val_get( &
1161 gauxc_functional_section, &
1162 "MODEL", &
1163 c_val=model_name)
1164 CALL section_vals_val_get( &
1165 gauxc_functional_section, &
1166 "GRID", &
1167 c_val=params%grid_type, &
1168 explicit=grid_explicit)
1169 CALL section_vals_val_get( &
1170 gauxc_functional_section, &
1171 "RADIAL_QUADRATURE", &
1172 c_val=params%radial_quadrature)
1173 CALL section_vals_val_get( &
1174 gauxc_functional_section, &
1175 "PRUNING_SCHEME", &
1176 c_val=params%pruning_scheme, &
1177 explicit=pruning_explicit)
1178 CALL section_vals_val_get( &
1179 gauxc_functional_section, &
1180 "BATCH_SIZE", &
1181 i_val=params%batch_size)
1182 CALL section_vals_val_get( &
1183 gauxc_functional_section, &
1184 "DEVICE_RUNTIME_FILL_FRACTION", &
1185 r_val=params%device_runtime_fill_fraction)
1186 CALL section_vals_val_get( &
1187 gauxc_functional_section, &
1188 "MODEL_ATOM_CHUNK_SIZE", &
1189 i_val=atom_chunk_size, &
1190 explicit=atom_chunk_size_explicit)
1191 CALL section_vals_val_get( &
1192 gauxc_functional_section, &
1193 "PERIODIC_REFERENCE", &
1194 l_val=periodic_reference)
1195 CALL section_vals_val_get( &
1196 gauxc_functional_section, &
1197 "MOLECULAR_VIRIAL", &
1198 l_val=molecular_virial)
1199 CALL section_vals_val_get( &
1200 gauxc_functional_section, &
1201 "MOLECULAR_VIRIAL_DEBUG", &
1202 l_val=molecular_virial_debug)
1203 CALL section_vals_val_get( &
1204 gauxc_functional_section, &
1205 "MOLECULAR_VIRIAL_DEBUG_DX", &
1206 r_val=molecular_virial_debug_dx)
1207 CALL section_vals_val_get( &
1208 gauxc_functional_section, &
1209 "USE_GPU", &
1210 i_val=gpu_mode)
1211 SELECT CASE (gpu_mode)
1212 CASE (do_gpu_auto)
1213#if defined(GAUXC_HAS_DEVICE) && defined(__LIBTORCH_CUDA)
1214 use_cuda = .true.
1215#else
1216 use_cuda = .false.
1217#endif
1218 CASE (do_gpu_host)
1219 use_cuda = .false.
1220 CASE (do_gpu_device)
1221 use_cuda = .true.
1222 CASE DEFAULT
1223 CALL cp_abort(__location__, "Unknown GAUXC%USE_GPU value.")
1224 END SELECT
1225 CALL section_vals_val_get( &
1226 gauxc_functional_section, &
1227 "LB_EXECUTION_SPACE", &
1228 c_val=params%lb_exec_space, &
1229 explicit=lb_space_explicit)
1230 CALL section_vals_val_get( &
1231 gauxc_functional_section, &
1232 "INT_EXECUTION_SPACE", &
1233 c_val=params%int_exec_space, &
1234 explicit=int_space_explicit)
1235 lb_exec_space_key = adjustl(params%lb_exec_space)
1236 int_exec_space_key = adjustl(params%int_exec_space)
1237 CALL uppercase(lb_exec_space_key)
1238 CALL uppercase(int_exec_space_key)
1239 IF (.NOT. use_cuda) THEN
1240 IF ((lb_space_explicit .AND. trim(lb_exec_space_key) == "DEVICE") .OR. &
1241 (int_space_explicit .AND. trim(int_exec_space_key) == "DEVICE")) THEN
1242 CALL cp_abort(__location__, &
1243 "GAUXC%USE_GPU disables GPU execution, but an explicit "// &
1244 "DEVICE execution space was requested. Use USE_GPU GPU "// &
1245 "or remove the explicit DEVICE setting.")
1246 END IF
1247 params%lb_exec_space = "HOST"
1248 params%int_exec_space = "HOST"
1249 ELSE
1250 IF (.NOT. lb_space_explicit) params%lb_exec_space = "DEVICE"
1251 IF (.NOT. int_space_explicit) params%int_exec_space = "DEVICE"
1252 END IF
1253 CALL section_vals_val_get( &
1254 gauxc_functional_section, &
1255 "LWD_KERNEL", &
1256 c_val=params%lwd_kernel)
1257 CALL section_vals_val_get( &
1258 gauxc_functional_section, &
1259 "SKALA_RUNTIME", &
1260 c_val=skala_runtime)
1261 CALL section_vals_val_get( &
1262 gauxc_functional_section, &
1263 "MODEL_GRADIENT_RUNTIME", &
1264 c_val=gradient_runtime)
1265 CALL section_vals_val_get( &
1266 gauxc_functional_section, &
1267 "FORCE_NUMERICAL_GRADIENT", &
1268 l_val=params%force_fd_gradient)
1269 CALL section_vals_val_get( &
1270 gauxc_functional_section, &
1271 "OUTPUT_PATH", &
1272 c_val=output_path)
1273
1274 model_key = adjustl(model_name)
1275 CALL uppercase(model_key)
1276 xc_fun_key = adjustl(params%xc_fun_name)
1277 CALL uppercase(xc_fun_key)
1278 skala_runtime_key = adjustl(skala_runtime)
1279 CALL uppercase(skala_runtime_key)
1280 gradient_runtime_key = adjustl(gradient_runtime)
1281 CALL uppercase(gradient_runtime_key)
1282 int_exec_space_key = adjustl(params%int_exec_space)
1283 CALL uppercase(int_exec_space_key)
1284 params%use_gauxc_model = (trim(model_key) /= "" .AND. trim(model_key) /= "NONE" .AND. &
1285 trim(model_key) /= trim(xc_fun_key))
1286 use_skala_model = (index(trim(model_key), "SKALA") > 0)
1287 lwd_kernel_key = adjustl(params%lwd_kernel)
1288 CALL uppercase(lwd_kernel_key)
1289 IF (trim(lwd_kernel_key) == "AUTO") THEN
1290 params%lwd_kernel = "DEFAULT"
1291#if defined(GAUXC_HAS_CUTLASS)
1292 IF (use_skala_model .AND. trim(params%int_exec_space) == "DEVICE") THEN
1293 params%lwd_kernel = "SCHEME1-CUTLASS"
1294 END IF
1295#endif
1296 END IF
1297 params%model_eval_name = model_name
1298 IF (.NOT. params%use_gauxc_model) THEN
1299 ! MODEL NONE and MODEL equal to FUNCTIONAL select conventional GauXC.
1300 params%model_eval_name = "NONE"
1301 END IF
1302 IF (gapw_pseudopotentials .AND. params%use_gauxc_model .AND. .NOT. dft_control%qs_control%gapw_xc .AND. &
1303 .NOT. gapw_paw_pseudopotentials .AND. para_env%mepos == 0 .AND. ASSOCIATED(scf_env)) THEN
1304 IF (scf_env%iter_count == 1) THEN
1305 CALL cp_warn( &
1306 __location__, &
1307 "GauXC Skala with METHOD GAPW and GPW_TYPE pseudopotentials evaluates "// &
1308 "the XC term directly on the molecular AO/valence density; no GAPW one-center "// &
1309 "XC correction is used for those regular-grid kinds.")
1310 END IF
1311 END IF
1312 IF (params%device_runtime_fill_fraction <= 0.0_dp .OR. params%device_runtime_fill_fraction > 1.0_dp) THEN
1313 CALL cp_abort(__location__, &
1314 "GAUXC%DEVICE_RUNTIME_FILL_FRACTION must be > 0 and <= 1.")
1315 END IF
1316 IF (atom_chunk_size < -1) THEN
1317 CALL cp_abort(__location__, &
1318 "GAUXC%MODEL_ATOM_CHUNK_SIZE must be -1, zero, or positive.")
1319 END IF
1320 IF (molecular_virial_debug) THEN
1321 IF (molecular_virial_debug_dx <= 0.0_dp) THEN
1322 CALL cp_abort(__location__, &
1323 "GauXC MOLECULAR_VIRIAL_DEBUG_DX must be positive.")
1324 END IF
1325 molecular_virial = .true.
1326 END IF
1327 need_xc_gradient = calculate_forces .OR. molecular_virial
1328 IF (low_spin_scope) THEN
1329 IF (.NOT. dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
1330 cpabort("GauXC low spin ROKS requires METHOD GAPW.")
1331 END IF
1332 IF (.NOT. gauxc_gapw_all_all_electron(qs_kind_set)) THEN
1333 cpabort("GAPW low spin ROKS GauXC requires all-electron kinds.")
1334 END IF
1335 IF (is_periodic .OR. do_kpoints .OR. nimages /= 1) THEN
1336 cpabort("GAPW low spin ROKS GauXC requires an isolated Gamma-point calculation.")
1337 END IF
1338 IF (need_xc_gradient) THEN
1339 CALL cp_abort(__location__, &
1340 "GAPW low spin ROKS GauXC nuclear gradients and molecular virial are not implemented.")
1341 END IF
1342 IF (trim(int_exec_space_key) /= "HOST" .OR. trim(params%lb_exec_space) /= "HOST") THEN
1343 cpabort("GAPW low spin ROKS GauXC currently requires HOST execution.")
1344 END IF
1345 IF (params%use_gauxc_model .AND. .NOT. use_skala_model) THEN
1346 cpabort("GAPW low spin ROKS GauXC supports only the SKALA model or conventional XC.")
1347 END IF
1348 IF (use_skala_model .AND. trim(skala_runtime_key) == "MPI") THEN
1349 cpabort("GAPW low spin ROKS Skala requires SKALA_RUNTIME AUTO or SELF.")
1350 END IF
1351 END IF
1352 CALL ensure_gauxc_periodic_reference_scope( &
1353 dft_control, cell, qs_kind_set, do_kpoints, periodic_reference)
1354 IF (is_periodic .AND. periodic_reference .AND. para_env%mepos == 0) THEN
1355 IF (ASSOCIATED(scf_env)) THEN
1356 IF (scf_env%iter_count == 1) THEN
1357 CALL cp_warn( &
1358 __location__, &
1359 "GAUXC%PERIODIC_REFERENCE uses GauXC molecular quadrature for isolated validation "// &
1360 "cells. Compact periodic materials require a dedicated periodic GauXC interface.")
1361 END IF
1362 END IF
1363 END IF
1364 IF (params%use_gauxc_model) THEN
1365 IF (has_nlcc(qs_kind_set)) THEN
1366 CALL cp_abort(__location__, &
1367 "GauXC Skala with NLCC pseudopotentials is not implemented. "// &
1368 "The frozen core density would need a SKALA-consistent feature definition.")
1369 END IF
1370 END IF
1371 IF (params%use_gauxc_model) THEN
1372 CALL set_gauxc_model_atom_chunk_env( &
1373 atom_chunk_size, atom_chunk_size_explicit)
1374 IF (.NOT. grid_explicit) params%grid_type = "SUPERFINE"
1375 IF (.NOT. pruning_explicit) params%pruning_scheme = "UNPRUNED"
1376
1377 grid_key = adjustl(params%grid_type)
1378 pruning_key = adjustl(params%pruning_scheme)
1379 CALL uppercase(grid_key)
1380 CALL uppercase(pruning_key)
1381 IF (use_skala_model .AND. need_xc_gradient .AND. &
1382 (trim(grid_key) /= "SUPERFINE" .OR. trim(pruning_key) /= "UNPRUNED")) THEN
1383 CALL cp_warn( &
1384 __location__, &
1385 "GauXC Skala nuclear gradients are sensitive to the GauXC molecular grid. "// &
1386 "Use GRID SUPERFINE and PRUNING_SCHEME UNPRUNED for quantitative force checks.")
1387 END IF
1388 IF (trim(model_key) == "SKALA") THEN
1389 model_name = ""
1390 env_status = 1
1391 IF (trim(int_exec_space_key) == "DEVICE") THEN
1392 CALL get_environment_variable("SKALA_CUDA_MODEL", model_name, status=env_status)
1393 END IF
1394 IF (env_status /= 0 .OR. len_trim(model_name) == 0) THEN
1395 CALL get_environment_variable("SKALA_MODEL", model_name, status=env_status)
1396 END IF
1397 IF (env_status /= 0 .OR. len_trim(model_name) == 0) THEN
1398 IF (trim(int_exec_space_key) == "DEVICE") THEN
1399 CALL cp_abort( &
1400 __location__, &
1401 "MODEL SKALA with DEVICE execution requires SKALA_CUDA_MODEL or "// &
1402 "GAUXC_SKALA_MODEL")
1403 ELSE
1404 cpabort("MODEL SKALA requires the GAUXC_SKALA_MODEL environment variable")
1405 END IF
1406 END IF
1407 params%model_eval_name = model_name
1408 END IF
1409 END IF
1410 SELECT CASE (trim(skala_runtime_key))
1411 CASE ("AUTO")
1412 params%use_self_runtime = use_skala_model .AND. para_env%num_pe > 1 .AND. params%nspins > 1
1413 CASE ("MPI")
1414 params%use_self_runtime = .false.
1415 CASE ("SELF")
1416 params%use_self_runtime = use_skala_model .AND. para_env%num_pe > 1
1417 CASE DEFAULT
1418 CALL cp_abort(__location__, "Unknown GAUXC%SKALA_RUNTIME value.")
1419 END SELECT
1420 IF (.NOT. use_skala_model) params%use_self_runtime = .false.
1421 params%gradient_runtime = gradient_runtime_key
1422 IF (use_skala_model .AND. para_env%num_pe > 1 .AND. .NOT. params%use_self_runtime .AND. &
1423 para_env%mepos == 0 .AND. ASSOCIATED(scf_env)) THEN
1424 IF (scf_env%iter_count == 1) THEN
1425 CALL cp_warn( &
1426 __location__, &
1427 "GAUXC%SKALA_RUNTIME uses the MPI communicator for energy/VXC. "// &
1428 "SKALA Torch atom chunks can be distributed across MPI ranks; "// &
1429 "set GAUXC_ONEDFT_DISTRIBUTED_TORCH=0 to force rank-0 Torch inference.")
1430 END IF
1431 END IF
1432
1433 params%gapw_method = gapw_method
1434 params%need_xc_gradient = need_xc_gradient
1435
1436 IF (.NOT. ASSOCIATED(qs_env%gauxc_cache)) ALLOCATE (qs_env%gauxc_cache)
1437 cache => qs_env%gauxc_cache
1438 CALL gauxc_cache_init( &
1439 cache, &
1440 params, &
1441 para_env, &
1442 particle_set, &
1443 qs_kind_set, &
1444 gauxc_status)
1445
1446 CALL gauxc_cache_ensure_gradient(cache, params, gauxc_status)
1447 CALL gauxc_check_status(gauxc_status)
1448
1449 hdf5_output = (trim(output_path) /= "")
1450 write_hdf5_output = hdf5_output .AND. para_env%mepos == 0 .AND. .NOT. external_density
1451 IF (write_hdf5_output .AND. ASSOCIATED(scf_env)) THEN
1452 write_hdf5_output = scf_env%iter_count == 1
1453 END IF
1454 IF (write_hdf5_output) THEN
1455 CALL gauxc_write_molecule_hdf5( &
1456 cache%molecule, &
1457 output_path, &
1458 "molecule.h5", &
1459 "molecule", &
1460 gauxc_status)
1461 CALL gauxc_check_status(gauxc_status)
1462 CALL gauxc_write_basisset_hdf5( &
1463 cache%basisset, &
1464 output_path, &
1465 "basisset.h5", &
1466 "basisset", &
1467 gauxc_status)
1468 CALL gauxc_check_status(gauxc_status)
1469 END IF
1470
1471 IF (qs_env%run_rtp) THEN
1472 cpabort("GAUXC XC energy currently does not support real-time propagation")
1473 END IF
1474
1475 exc_value = 0.0_dp
1476
1477 IF (ASSOCIATED(matrix_vxc)) CALL dbcsr_deallocate_matrix_set(matrix_vxc)
1478 CALL dbcsr_allocate_matrix_set(matrix_vxc, params%nspins)
1479
1480 DO img = 1, nimages
1481 IF (img > 1) THEN
1482 cpabort("UNIMPLEMENTED: Handling nimg>1 in k-point integration")
1483 END IF
1484 CALL dbcsr_to_dense(rho_ao(1, img), density_scalar, para_env)
1485 CALL para_env%sum(density_scalar)
1486 IF (params%nspins == 1) THEN
1487 gauxc_xc_result = gauxc_compute_xc( &
1488 cache%integrator, &
1489 density_scalar, &
1490 nspins=params%nspins, &
1491 status=gauxc_status, &
1492 model=trim(params%model_eval_name))
1493 CALL gauxc_check_status(gauxc_status)
1494 IF (need_xc_gradient) THEN
1495 IF (params%use_fd_gradient) THEN
1496 CALL gauxc_xc_gradient_fd( &
1497 particle_set, qs_kind_set, density_scalar, params%nspins, params%model_eval_name, &
1498 params%xc_fun_name, params%grid_type, params%radial_quadrature, params%pruning_scheme, &
1499 params%lb_exec_space, params%int_exec_space, params%lwd_kernel, params%batch_size, &
1500 params%device_runtime_fill_fraction, gapw_fd_gradient_dx, para_env, &
1501 exc_grad%exc_grad)
1502 ELSE IF (params%use_gradient_self_runtime) THEN
1503 exc_grad = gauxc_compute_xc_gradient( &
1504 cache%gradient_integrator, &
1505 density_scalar, &
1506 nspins=params%nspins, &
1507 natom=params%natom, &
1508 status=gauxc_status, &
1509 model=trim(params%model_eval_name))
1510 ELSE
1511 exc_grad = gauxc_compute_xc_gradient( &
1512 cache%integrator, &
1513 density_scalar, &
1514 nspins=params%nspins, &
1515 natom=params%natom, &
1516 status=gauxc_status, &
1517 model=trim(params%model_eval_name))
1518 END IF
1519 CALL gauxc_check_status(gauxc_status)
1520 IF (calculate_forces) THEN
1521 CALL add_gauxc_gradient_to_force( &
1522 exc_grad%exc_grad, &
1523 force, &
1524 atomic_kind_set, &
1525 para_env)
1526 END IF
1527 IF (molecular_virial) THEN
1528 CALL print_gauxc_molecular_virial(exc_grad%exc_grad, particle_set, para_env)
1529 END IF
1530 IF (molecular_virial_debug) THEN
1531 CALL debug_gauxc_molecular_virial( &
1532 exc_grad%exc_grad, particle_set, qs_kind_set, density_scalar, params%nspins, &
1533 params%model_eval_name, params%xc_fun_name, params%grid_type, params%radial_quadrature, params%pruning_scheme, &
1534 params%lb_exec_space, params%int_exec_space, params%lwd_kernel, params%batch_size, &
1535 params%device_runtime_fill_fraction, molecular_virial_debug_dx, para_env)
1536 END IF
1537 DEALLOCATE (exc_grad%exc_grad)
1538 END IF
1539 ELSE
1540 cpassert(params%nspins == 2)
1541 ! In here:
1542 ! scalar <- rho_ao(1, :) + rho_ao(2, :)
1543 ! zeta <- rho_ao(1, :) - rho_ao(2, :)
1544 CALL dbcsr_to_dense(rho_ao(2, img), density_zeta, para_env)
1545 CALL para_env%sum(density_zeta)
1546 ! Do NOT reorder the following lines!
1547 density_scalar(:, :) = density_scalar(:, :) + density_zeta(:, :)
1548 ! Factor two because the next line is evaluated after the above line.
1549 ! We need to subtract density_zeta once to undo the above line and
1550 ! a second time because that is what UKS requires.
1551 ! This style lowers memory footprint.
1552 density_zeta(:, :) = density_scalar(:, :) - 2.0_dp*density_zeta(:, :)
1553 gauxc_xc_result = gauxc_compute_xc( &
1554 cache%integrator, &
1555 density_scalar, &
1556 density_zeta, &
1557 params%nspins, &
1558 gauxc_status, &
1559 model=trim(params%model_eval_name))
1560 CALL gauxc_check_status(gauxc_status)
1561 IF (need_xc_gradient) THEN
1562 IF (params%use_fd_gradient) THEN
1563 CALL gauxc_xc_gradient_fd( &
1564 particle_set, qs_kind_set, density_scalar, params%nspins, params%model_eval_name, &
1565 params%xc_fun_name, params%grid_type, params%radial_quadrature, params%pruning_scheme, &
1566 params%lb_exec_space, params%int_exec_space, params%lwd_kernel, params%batch_size, &
1567 params%device_runtime_fill_fraction, gapw_fd_gradient_dx, para_env, &
1568 exc_grad%exc_grad, density_zeta=density_zeta)
1569 ELSE IF (params%use_gradient_self_runtime) THEN
1570 exc_grad = gauxc_compute_xc_gradient( &
1571 cache%gradient_integrator, &
1572 density_scalar, &
1573 density_zeta, &
1574 params%nspins, &
1575 params%natom, &
1576 gauxc_status, &
1577 model=trim(params%model_eval_name))
1578 ELSE
1579 exc_grad = gauxc_compute_xc_gradient( &
1580 cache%integrator, &
1581 density_scalar, &
1582 density_zeta, &
1583 params%nspins, &
1584 params%natom, &
1585 gauxc_status, &
1586 model=trim(params%model_eval_name))
1587 END IF
1588 CALL gauxc_check_status(gauxc_status)
1589 IF (calculate_forces) THEN
1590 CALL add_gauxc_gradient_to_force( &
1591 exc_grad%exc_grad, &
1592 force, &
1593 atomic_kind_set, &
1594 para_env)
1595 END IF
1596 IF (molecular_virial) THEN
1597 CALL print_gauxc_molecular_virial(exc_grad%exc_grad, particle_set, para_env)
1598 END IF
1599 IF (molecular_virial_debug) THEN
1600 CALL debug_gauxc_molecular_virial( &
1601 exc_grad%exc_grad, particle_set, qs_kind_set, density_scalar, params%nspins, &
1602 params%model_eval_name, params%xc_fun_name, params%grid_type, params%radial_quadrature, params%pruning_scheme, &
1603 params%lb_exec_space, params%int_exec_space, params%lwd_kernel, params%batch_size, &
1604 params%device_runtime_fill_fraction, molecular_virial_debug_dx, para_env, &
1605 density_zeta=density_zeta)
1606 END IF
1607 DEALLOCATE (exc_grad%exc_grad)
1608 END IF
1609 END IF
1610
1611 exc_value = exc_value + gauxc_xc_result%exc
1612
1613 IF (params%nspins == 1) THEN
1614 IF (img == 1) THEN
1615 matrix_vxc(1) = dense_to_dbcsr(gauxc_xc_result%vxc_scalar, rho_ao(1, img))
1616 ELSE
1617 cpabort("UNIMPLEMENTED: Handling multiple result matrices in k-point integration")
1618 END IF
1619 ELSE
1620 cpassert(params%nspins == 2)
1621 ! Transform derivatives from total/spin density back to alpha/beta channels.
1622 vxc_zeta_tmp = dense_to_dbcsr(gauxc_xc_result%vxc_zeta, rho_ao(1, img))
1623 IF (img == 1) THEN
1624 DO ispin = 1, 2
1625 matrix_vxc(ispin) = dense_to_dbcsr(gauxc_xc_result%vxc_scalar, rho_ao(ispin, 1))
1626 CALL dbcsr_add( &
1627 matrix_vxc(ispin)%matrix, &
1628 vxc_zeta_tmp%matrix, &
1629 1.0_dp, &
1630 ! 1.0 for ispin==1, -1.0 for ispin==2
1631 1.0_dp - real(ispin - 1, dp)*2.0_dp)
1632 END DO
1633 ELSE
1634 cpabort("UNIMPLEMENTED: Handling multiple result matrices in k-point integration")
1635 END IF
1636 CALL dbcsr_release(vxc_zeta_tmp%matrix)
1637 DEALLOCATE (vxc_zeta_tmp%matrix)
1638 END IF
1639 END DO
1640
1641 DEALLOCATE (density_scalar)
1642 IF (ALLOCATED(density_zeta)) DEALLOCATE (density_zeta)
1643 DEALLOCATE (gauxc_xc_result%vxc_scalar)
1644 IF (ALLOCATED(gauxc_xc_result%vxc_zeta)) DEALLOCATE (gauxc_xc_result%vxc_zeta)
1645
1646 IF (external_density) THEN
1647 exc_external = exc_value
1648 matrix_vxc_external => matrix_vxc
1649 ELSE
1650 energy%exc = exc_value
1651 CALL set_ks_env(ks_env, matrix_vxc=matrix_vxc)
1652 END IF
1653 DO ispin = 1, params%nspins
1654 CALL dbcsr_finalize(matrix_vxc(ispin)%matrix)
1655 END DO
1656
1657 END SUBROUTINE apply_gauxc
1658
1659END MODULE xc_gauxc_functional
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
Handles all functions related to the CELL.
Definition cell_types.F:15
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
character function, public dbcsr_get_matrix_type(matrix)
...
logical function, public dbcsr_iterator_blocks_left(iterator)
...
subroutine, public dbcsr_iterator_stop(iterator)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_get_stored_coordinates(matrix, row, column, processor)
...
subroutine, public dbcsr_init_p(matrix)
...
subroutine, public dbcsr_work_create(matrix, nblks_guess, sizedata_guess, n, work_mutable)
...
subroutine, public dbcsr_iterator_next_block(iterator, row, column, block, block_number_argument_has_been_removed, row_size, col_size, row_offset, col_offset, transposed)
...
subroutine, public dbcsr_finalize(matrix)
...
subroutine, public dbcsr_release(matrix)
...
subroutine, public dbcsr_iterator_readonly_start(iterator, matrix, shared, dynamic, dynamic_byrows)
Like dbcsr_iterator_start() but with matrix being INTENT(IN). When invoking this routine,...
subroutine, public dbcsr_put_block(matrix, row, col, block, summation)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
subroutine, public dbcsr_distribution_get(dist, row_dist, col_dist, nrows, ncols, has_threads, group, mynode, numnodes, nprows, npcols, myprow, mypcol, pgrid, subgroups_defined, prow_group, pcol_group)
...
DBCSR operations in CP2K.
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
Definition of the atomic potential types.
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public xc_vdw_fun_nonloc
integer, parameter, public do_gpu_device
integer, parameter, public do_gpu_host
integer, parameter, public do_gpu_auto
objects that represent the structure of input sections and the data contained in an input section
type(section_vals_type) function, pointer, public section_vals_get_subs_vals2(section_vals, i_section, i_rep_section)
returns the values of the n-th non default subsection (null if no such section exists (not so many no...
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
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
Interface to the message passing library MPI.
type(mp_comm_type), parameter, public mp_comm_self
Define the data structure for the particle information.
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, matrix_metric, matrix_metric_kp)
Get the QUICKSTEP environment.
Define the quickstep kind type and their sub types.
logical function, public has_nlcc(qs_kind_set)
finds if a given qs run needs to use nlcc
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
subroutine, public set_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, exc_accint, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, kpoints, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, task_list, task_list_soft, subsys, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env, uspp, upf_inverse)
...
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
module that contains the definitions of the scf types
Utilities for string manipulations.
elemental subroutine, public uppercase(string)
Convert all lower case characters in a string to upper case.
subroutine, public gauxc_cache_init(cache, params, para_env, particle_set, qs_kind_set, status)
...
subroutine, public gauxc_cache_ensure_gradient(cache, params, status)
Build the separate gradient grid/integrator on first use and keep it cached.
logical function, public gauxc_gapw_has_paw_pseudopotentials(qs_kind_set)
Return whether GauXC GAPW mode sees pseudopotential one-center GAPW kinds.
logical function, public xc_section_uses_gauxc(xc_section)
...
logical function, public gauxc_gapw_has_pseudopotentials(qs_kind_set)
Return whether GauXC GAPW mode sees pseudopotential kinds.
subroutine, public skala_info(functional, lsd, reference, shortform, needs, max_deriv)
Return information about the Skala functional.
logical function, public gauxc_gapw_all_all_electron(qs_kind_set)
Return whether every GAPW kind uses an all-electron potential.
logical function, public gauxc_gapw_all_pseudopotentials(qs_kind_set)
Return whether every GAPW kind uses a pseudopotential.
subroutine, public apply_gauxc(qs_env, xc_section, calculate_forces, rho_ao_external, exc_external, matrix_vxc_external)
Evaluate molecular GauXC, optionally for an explicit configuration density.
contains the structure
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
keeps the density in various representations, keeping track of which ones are valid.
contains a flag for each component of xc_rho_set, so that you can use it to tell which components you...