(git:3d5d2e5)
Loading...
Searching...
No Matches
xc_gauxc_cache.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
13
15 USE iso_c_binding, ONLY: c_double
16 USE kinds, ONLY: default_path_length,&
18 USE message_passing, ONLY: mp_comm_self,&
23 USE xc_gauxc_interface, ONLY: &
24 cp_gauxc_basisset_type, cp_gauxc_grid_type, cp_gauxc_integrator_type, &
25 cp_gauxc_molecule_type, cp_gauxc_status_type, gauxc_check_status, gauxc_create_basisset, &
26 gauxc_create_grid, gauxc_create_integrator, gauxc_create_molecule, gauxc_destroy_basisset, &
27 gauxc_destroy_grid, gauxc_destroy_integrator, gauxc_destroy_molecule
28#include "../base/base_uses.f90"
29
30 IMPLICIT NONE
31 PRIVATE
32
34 INTEGER :: natom = -1
35 INTEGER :: nspins = -1
36 INTEGER :: batch_size = -1
37 REAL(c_double) :: device_runtime_fill_fraction = 0.0_c_double
38 CHARACTER(LEN=default_string_length) :: xc_fun_name = ""
39 CHARACTER(LEN=default_string_length) :: grid_type = ""
40 CHARACTER(LEN=default_string_length) :: radial_quadrature = ""
41 CHARACTER(LEN=default_string_length) :: pruning_scheme = ""
42 CHARACTER(LEN=default_string_length) :: lb_exec_space = ""
43 CHARACTER(LEN=default_string_length) :: int_exec_space = ""
44 CHARACTER(LEN=default_path_length) :: model_eval_name = ""
45 CHARACTER(LEN=default_string_length) :: lwd_kernel = ""
46 CHARACTER(LEN=default_string_length) :: gradient_runtime = "AUTO"
47 LOGICAL :: need_xc_gradient = .false.
48 LOGICAL :: gapw_method = .false.
49 LOGICAL :: force_fd_gradient = .false.
50 LOGICAL :: use_gradient_mpi_runtime = .false.
51 LOGICAL :: use_self_runtime = .false.
52 LOGICAL :: use_gradient_self_runtime = .false.
53 LOGICAL :: use_fd_gradient = .false.
54 LOGICAL :: use_gauxc_model = .false.
56
58 TYPE(cp_gauxc_molecule_type) :: molecule = cp_gauxc_molecule_type()
59 TYPE(cp_gauxc_basisset_type) :: basisset = cp_gauxc_basisset_type()
60 TYPE(cp_gauxc_grid_type) :: grid = cp_gauxc_grid_type()
61 TYPE(cp_gauxc_grid_type) :: gradient_grid = cp_gauxc_grid_type()
62 TYPE(cp_gauxc_integrator_type) :: integrator = cp_gauxc_integrator_type()
63 TYPE(cp_gauxc_integrator_type) :: gradient_integrator = cp_gauxc_integrator_type()
64 INTEGER :: mpi_comm = -1
65 LOGICAL :: is_init = .false.
66 LOGICAL :: gradient_is_init = .false.
67 LOGICAL :: gradient_notice_printed = .false.
68 REAL(c_double), DIMENSION(:, :), ALLOCATABLE :: last_positions
69 CONTAINS
70
71 PROCEDURE, PUBLIC :: has_gradient_grid => gauxc_cache_type_has_gradient_grid
72 PROCEDURE, PUBLIC :: max_l => gauxc_cache_type_max_l
73 END TYPE cp_gauxc_cache_type
74
75 PUBLIC :: &
81
82 LOGICAL, SAVE :: execution_spaces_logged = .false.
83
84CONTAINS
85
86! **************************************************************************************************
87!> \brief ...
88!> \param this ...
89!> \return ...
90! **************************************************************************************************
91 FUNCTION gauxc_cache_type_has_gradient_grid(this) RESULT(res)
92 class(cp_gauxc_cache_type) :: this
93 LOGICAL :: res
94
95 res = this%gradient_is_init
96 END FUNCTION gauxc_cache_type_has_gradient_grid
97
98! **************************************************************************************************
99!> \brief ...
100!> \param this ...
101!> \return ...
102! **************************************************************************************************
103 FUNCTION gauxc_cache_type_max_l(this) RESULT(res)
104 class(cp_gauxc_cache_type) :: this
105 INTEGER :: res
106
107 res = this%basisset%max_l
108 END FUNCTION gauxc_cache_type_max_l
109
110! **************************************************************************************************
111!> \brief ...
112!> \param cache ...
113!> \param params ...
114!> \param para_env ...
115!> \param particle_set ...
116!> \param qs_kind_set ...
117!> \param status ...
118! **************************************************************************************************
119 SUBROUTINE gauxc_cache_init( &
120 cache, &
121 params, &
122 para_env, &
123 particle_set, &
124 qs_kind_set, &
125 status)
126
127 TYPE(cp_gauxc_cache_type), INTENT(INOUT) :: cache
128 TYPE(cp_gauxc_cache_params), INTENT(INOUT) :: params
129 TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
130 TYPE(particle_type), DIMENSION(:), INTENT(IN), &
131 POINTER :: particle_set
132 TYPE(qs_kind_type), DIMENSION(:), INTENT(IN), &
133 POINTER :: qs_kind_set
134 TYPE(cp_gauxc_status_type), INTENT(INOUT) :: status
135
136#ifdef __GAUXC
137 INTEGER :: gradient_error, unit_nr
138
140 cache, &
141 params, &
142 para_env, &
143 particle_set)) THEN
144 params%use_fd_gradient = cache%use_fd_gradient
145 params%use_gradient_mpi_runtime = cache%use_gradient_mpi_runtime
146 params%use_gradient_self_runtime = cache%use_gradient_self_runtime
147 status%status%code = 0
148 RETURN
149 END IF
150
151 CALL gauxc_cache_release(cache)
152
153 cache%molecule = gauxc_create_molecule( &
154 particle_set, &
155 status)
156 IF (status%status%code /= 0) THEN
157 CALL cleanup_after_molecule()
158 RETURN
159 END IF
160
161 cache%basisset = gauxc_create_basisset( &
162 qs_kind_set, &
163 particle_set, &
164 status)
165 IF (status%status%code /= 0) THEN
166 CALL cleanup_after_basisset()
167 RETURN
168 END IF
169
171 .true., params%gapw_method, cache%max_l(), params%use_gauxc_model, para_env%num_pe, &
172 params%use_self_runtime, trim(params%gradient_runtime), params%force_fd_gradient, &
173 params%use_fd_gradient, params%use_gradient_mpi_runtime, params%use_gradient_self_runtime, gradient_error)
174 SELECT CASE (gradient_error)
175 CASE (1)
176 CALL cp_abort(__location__, &
177 "MODEL_GRADIENT_RUNTIME MPI requires verified GauXC OneDFT gradient support (PR #222).")
178 CASE (2)
179 CALL cp_abort(__location__, &
180 "MODEL_GRADIENT_RUNTIME MPI requires an MPI energy/VXC runtime. "// &
181 "Use AUTO/SELF to retain the open-shell replicated-runtime safeguard.")
182 CASE (3)
183 CALL cp_abort(__location__, &
184 "MODEL_GRADIENT_RUNTIME MPI is incompatible with FORCE_NUMERICAL_GRADIENT.")
185 CASE (4)
186 CALL cp_abort(__location__, "Unknown GAUXC%MODEL_GRADIENT_RUNTIME value.")
187 END SELECT
188
189 IF (params%use_self_runtime .OR. .NOT. params%use_gauxc_model) THEN
190 ! SKALA currently needs a replicated molecular runtime for reproducible
191 ! open-shell densities across CP2K MPI ranks.
192 ! Conventional GauXC also uses a replicated runtime here: CP2K has
193 ! already allreduced the dense AO density matrix on every rank.
194 cache%grid = gauxc_create_grid( &
195 cache%molecule, &
196 cache%basisset, &
197 params%grid_type, &
198 params%radial_quadrature, &
199 params%pruning_scheme, &
200 params%lb_exec_space, &
201 params%batch_size, &
202 params%device_runtime_fill_fraction, &
203 status, &
204 mpi_comm=mp_comm_self%get_handle(), &
205 force_new_runtime=.true., &
206 int_exec_space=params%int_exec_space)
207 ELSE
208 ! Use the QS force-evaluation communicator rather than GauXC's global
209 ! runtime. In mixed CDFT the diabatic states can be built on disjoint
210 ! MPI subgroups; using the global communicator would make GauXC wait
211 ! for ranks that are working on another state.
212 cache%grid = gauxc_create_grid( &
213 cache%molecule, &
214 cache%basisset, &
215 params%grid_type, &
216 params%radial_quadrature, &
217 params%pruning_scheme, &
218 params%lb_exec_space, &
219 params%batch_size, &
220 params%device_runtime_fill_fraction, &
221 status, &
222 mpi_comm=para_env%get_handle(), &
223 int_exec_space=params%int_exec_space)
224 END IF
225 IF (status%status%code /= 0) THEN
226 CALL cleanup_after_grid()
227 RETURN
228 END IF
229
230 cache%integrator = gauxc_create_integrator( &
231 trim(params%xc_fun_name), &
232 cache%grid, &
233 params%int_exec_space, &
234 params%lwd_kernel, &
235 params%nspins, &
236 status)
237 IF (status%status%code /= 0) THEN
238 CALL cleanup_after_integrator()
239 RETURN
240 END IF
241
242 IF (.NOT. execution_spaces_logged .AND. para_env%mepos == 0) THEN
244 IF (unit_nr > 0) THEN
245 WRITE (unit_nr, '(A)') " GAUXC| Execution spaces: LB "// &
246 trim(params%lb_exec_space)//", INT "//trim(params%int_exec_space)// &
247 ", runtime "//merge("DEVICE", "HOST ", cache%grid%uses_device_runtime)
248 END IF
250 END IF
251
252 cache%gradient_is_init = .false.
253 cache%gradient_notice_printed = .false.
254
255 cache%natom = params%natom
256 cache%nspins = params%nspins
257 cache%batch_size = params%batch_size
258 cache%device_runtime_fill_fraction = real(params%device_runtime_fill_fraction, c_double)
259 cache%xc_fun_name = trim(params%xc_fun_name)
260 cache%grid_type = trim(params%grid_type)
261 cache%radial_quadrature = trim(params%radial_quadrature)
262 cache%pruning_scheme = trim(params%pruning_scheme)
263 cache%lb_exec_space = trim(params%lb_exec_space)
264 cache%int_exec_space = trim(params%int_exec_space)
265 cache%model_eval_name = trim(params%model_eval_name)
266 cache%lwd_kernel = trim(params%lwd_kernel)
267 cache%gradient_runtime = params%gradient_runtime
268 cache%need_xc_gradient = params%need_xc_gradient
269 cache%gapw_method = params%gapw_method
270 cache%force_fd_gradient = params%force_fd_gradient
271 cache%use_gauxc_model = params%use_gauxc_model
272 cache%use_gradient_mpi_runtime = params%use_gradient_mpi_runtime
273 cache%use_gradient_self_runtime = params%use_gradient_self_runtime
274 cache%use_self_runtime = params%use_self_runtime
275 cache%use_fd_gradient = params%use_fd_gradient
276 IF (ALLOCATED(cache%last_positions)) THEN
277 DEALLOCATE (cache%last_positions)
278 END IF
279 ALLOCATE (cache%last_positions(3, params%natom))
280
281 block
282 INTEGER :: iatom
283 DO iatom = 1, params%natom
284 cache%last_positions(:, iatom) = real(particle_set(iatom)%r(:), c_double)
285 END DO
286 END block
287
288 cache%mpi_comm = para_env%get_handle()
289 cache%is_init = .true.
290
291#else
292 mark_used(params)
293 mark_used(para_env)
294 mark_used(particle_set)
295 mark_used(qs_kind_set)
296 mark_used(status)
297 cache%is_init = .false.
298#endif
299
300 CONTAINS
301! **************************************************************************************************
302!> \brief ...
303! **************************************************************************************************
304 SUBROUTINE cleanup_after_integrator()
305 CALL gauxc_destroy_integrator(cache%integrator, status)
306 CALL gauxc_check_status(status)
307 CALL cleanup_after_grid()
308 END SUBROUTINE cleanup_after_integrator
309
310! **************************************************************************************************
311!> \brief ...
312! **************************************************************************************************
313 SUBROUTINE cleanup_after_grid()
314 CALL gauxc_destroy_grid(cache%grid, status)
315 CALL gauxc_check_status(status)
316 CALL cleanup_after_basisset()
317 END SUBROUTINE cleanup_after_grid
318
319! **************************************************************************************************
320!> \brief ...
321! **************************************************************************************************
322 SUBROUTINE cleanup_after_basisset()
323 CALL gauxc_destroy_basisset(cache%basisset, status)
324 CALL gauxc_check_status(status)
325 CALL cleanup_after_molecule()
326 END SUBROUTINE cleanup_after_basisset
327
328! **************************************************************************************************
329!> \brief ...
330! **************************************************************************************************
331 SUBROUTINE cleanup_after_molecule()
332 CALL gauxc_destroy_molecule(cache%molecule, status)
333 CALL gauxc_check_status(status)
334 CALL cleanup_final()
335 END SUBROUTINE cleanup_after_molecule
336
337! **************************************************************************************************
338!> \brief ...
339! **************************************************************************************************
340 SUBROUTINE cleanup_final()
341 IF (ALLOCATED(cache%last_positions)) DEALLOCATE (cache%last_positions)
342 cache%gradient_is_init = .false.
343 cache%gradient_notice_printed = .false.
344 cache%is_init = .false.
345 END SUBROUTINE cleanup_final
346
347 END SUBROUTINE gauxc_cache_init
348
349! **************************************************************************************************
350!> \brief Release all GauXC objects in a cache and deallocate arrays
351!> \param cache ...
352! **************************************************************************************************
353 SUBROUTINE gauxc_cache_release(cache)
354 TYPE(cp_gauxc_cache_type), INTENT(INOUT) :: cache
355
356#ifdef __GAUXC
357 TYPE(cp_gauxc_status_type) :: status
358
359 IF (cache%is_init) THEN
360 IF (cache%has_gradient_grid()) THEN
361 CALL gauxc_destroy_integrator(cache%gradient_integrator, status)
362 CALL gauxc_check_status(status)
363 CALL gauxc_destroy_grid(cache%gradient_grid, status)
364 CALL gauxc_check_status(status)
365 END IF
366 CALL gauxc_destroy_integrator(cache%integrator, status)
367 CALL gauxc_check_status(status)
368 CALL gauxc_destroy_grid(cache%grid, status)
369 CALL gauxc_check_status(status)
370 CALL gauxc_destroy_basisset(cache%basisset, status)
371 CALL gauxc_check_status(status)
372 CALL gauxc_destroy_molecule(cache%molecule, status)
373 CALL gauxc_check_status(status)
374 END IF
375 IF (ALLOCATED(cache%last_positions)) DEALLOCATE (cache%last_positions)
376 cache%gradient_is_init = .false.
377 cache%gradient_notice_printed = .false.
378 cache%is_init = .false.
379#else
380 mark_used(cache)
381 IF (ALLOCATED(cache%last_positions)) DEALLOCATE (cache%last_positions)
382 cache%gradient_is_init = .false.
383 cache%gradient_notice_printed = .false.
384 cache%is_init = .false.
385#endif
386 END SUBROUTINE gauxc_cache_release
387
388! **************************************************************************************************
389!> \brief Build the separate gradient grid/integrator on first use and keep it cached.
390!>
391!> \param cache ...
392!> \param params ...
393!> \param status ...
394! **************************************************************************************************
395 SUBROUTINE gauxc_cache_ensure_gradient(cache, params, status)
396 TYPE(cp_gauxc_cache_type), INTENT(INOUT) :: cache
397 TYPE(cp_gauxc_cache_params), INTENT(INOUT) :: params
398 TYPE(cp_gauxc_status_type), INTENT(INOUT) :: status
399
400#ifdef __GAUXC
401 INTEGER :: error_code, unit_nr
402
403 IF (.NOT. params%need_xc_gradient) RETURN
404
405 IF (.NOT. cache%gradient_notice_printed) THEN
406 IF (params%use_fd_gradient .AND. .NOT. params%force_fd_gradient) THEN
407 CALL cp_warn( &
408 __location__, &
409 "Using finite-difference GauXC XC gradients for METHOD GAPW/GAPW_XC with "// &
410 "basis functions beyond f shells. Analytical high-l gradients have not been "// &
411 "enabled for this GauXC installation and functional path.")
412 END IF
413 unit_nr = cp_logger_get_default_io_unit()
414 IF (unit_nr > 0) THEN
415 WRITE (unit_nr, '(A,A)') " GAUXC| Molecular XC gradient method: ", &
416 trim(merge("FD ", "ANALYTICAL", params%use_fd_gradient))
417 WRITE (unit_nr, '(A,A)') " GAUXC| Molecular XC gradient runtime: ", &
418 merge("MPI ", "SELF", params%use_gradient_mpi_runtime)
419 END IF
420 cache%gradient_notice_printed = .true.
421 END IF
422
423 IF (params%use_fd_gradient) RETURN
424 IF (.NOT. params%use_gradient_self_runtime) RETURN
425 IF (.NOT. cache%is_init) RETURN
426 IF (cache%gradient_is_init) RETURN
427
428 cache%gradient_grid = gauxc_create_grid( &
429 cache%molecule, &
430 cache%basisset, &
431 params%grid_type, &
432 params%radial_quadrature, &
433 params%pruning_scheme, &
434 params%lb_exec_space, &
435 params%batch_size, &
436 params%device_runtime_fill_fraction, &
437 status, &
438 mpi_comm=mp_comm_self%get_handle(), &
439 force_new_runtime=.true., &
440 int_exec_space=params%int_exec_space)
441 IF (status%status%code /= 0) THEN
442 error_code = status%status%code
443 CALL gauxc_destroy_grid(cache%gradient_grid, status)
444 status%status%code = error_code
445 RETURN
446 END IF
447
448 cache%gradient_integrator = gauxc_create_integrator( &
449 trim(params%xc_fun_name), &
450 cache%gradient_grid, &
451 params%int_exec_space, &
452 params%lwd_kernel, &
453 params%nspins, &
454 status)
455 IF (status%status%code /= 0) THEN
456 error_code = status%status%code
457 CALL gauxc_destroy_grid(cache%gradient_grid, status)
458 status%status%code = error_code
459 RETURN
460 END IF
461 cache%gradient_is_init = .true.
462#else
463 mark_used(cache)
464 mark_used(params)
465 mark_used(status)
466#endif
467 END SUBROUTINE gauxc_cache_ensure_gradient
468
469! **************************************************************************************************
470!> \brief Check if the GauXC cache is valid for the given parameters
471!> \param cache ...
472!> \param params ...
473!> \param para_env ...
474!> \param particle_set ...
475!> \return ...
476!> \retval cache_valid ...
477! **************************************************************************************************
479 cache, &
480 params, &
481 para_env, &
482 particle_set) &
483 result(cache_valid)
484
485 TYPE(cp_gauxc_cache_type), INTENT(IN) :: cache
486 TYPE(cp_gauxc_cache_params), INTENT(IN) :: params
487 TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
488 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
489 LOGICAL :: cache_valid
490
491#ifdef __GAUXC
492 INTEGER :: iatom
493 REAL(c_double), PARAMETER :: pos_tol = 1.0e-8_c_double
494
495 cache_valid = .false.
496 IF (.NOT. cache%is_init) RETURN
497 IF (cache%natom /= params%natom) RETURN
498 IF (cache%nspins /= params%nspins) RETURN
499 IF (cache%batch_size /= params%batch_size) RETURN
500 IF (abs(cache%device_runtime_fill_fraction - real(params%device_runtime_fill_fraction, c_double)) > &
501 1.0e-10_c_double) RETURN
502 IF (trim(cache%xc_fun_name) /= trim(params%xc_fun_name)) RETURN
503 IF (trim(cache%grid_type) /= trim(params%grid_type)) RETURN
504 IF (trim(cache%radial_quadrature) /= trim(params%radial_quadrature)) RETURN
505 IF (trim(cache%pruning_scheme) /= trim(params%pruning_scheme)) RETURN
506 IF (trim(cache%lb_exec_space) /= trim(params%lb_exec_space)) RETURN
507 IF (trim(cache%int_exec_space) /= trim(params%int_exec_space)) RETURN
508 IF (trim(cache%model_eval_name) /= trim(params%model_eval_name)) RETURN
509 IF (trim(cache%lwd_kernel) /= trim(params%lwd_kernel)) RETURN
510 IF (cache%use_self_runtime .NEQV. params%use_self_runtime) RETURN
511
512 IF (cache%gradient_runtime /= params%gradient_runtime) RETURN
513 IF (cache%gapw_method .NEQV. params%gapw_method) RETURN
514 IF (cache%force_fd_gradient .NEQV. params%force_fd_gradient) RETURN
515 IF (cache%use_gauxc_model .NEQV. params%use_gauxc_model) RETURN
516 IF (cache%mpi_comm /= para_env%get_handle()) RETURN
517
518 IF (.NOT. ALLOCATED(cache%last_positions)) RETURN
519 IF (SIZE(cache%last_positions, 2) /= params%natom) RETURN
520 DO iatom = 1, params%natom
521 IF (any(abs(cache%last_positions(:, iatom) - &
522 REAL(particle_set(iatom)%r(:), c_double)) > pos_tol)) return
523 END DO
524 cache_valid = .true.
525#else
526 mark_used(cache)
527 mark_used(particle_set)
528 mark_used(params)
529 mark_used(para_env)
530 cache_valid = .false.
531#endif
532 END FUNCTION gauxc_cache_is_valid
533
534END MODULE xc_gauxc_cache
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...
Provides integrator routines (velocity verlet) for all the ensemble types.
Definition integrator.F:26
Defines the basic variable types.
Definition kinds.F:23
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.
Define the quickstep kind type and their sub types.
subroutine, public gauxc_cache_init(cache, params, para_env, particle_set, qs_kind_set, status)
...
logical, save execution_spaces_logged
logical function, public gauxc_cache_is_valid(cache, params, para_env, particle_set)
Check if the GauXC cache is valid for the given parameters.
subroutine, public gauxc_cache_ensure_gradient(cache, params, status)
Build the separate gradient grid/integrator on first use and keep it cached.
subroutine, public gauxc_cache_release(cache)
Release all GauXC objects in a cache and deallocate arrays.
pure subroutine, public gauxc_resolve_gradient_policy(need_gradient, gapw_method, max_l, is_model, nranks, energy_self, runtime, force_fd, use_fd, use_mpi, use_self, error)
Select molecular XC gradients without changing the energy/VXC runtime.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.