(git:3d5d2e5)
Loading...
Searching...
No Matches
qs_vxc_atom.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief routines that build the integrals of the Vxc potential calculated
10!> for the atomic density in the basis set of spherical primitives
11! **************************************************************************************************
16 USE cell_types, ONLY: cell_type
31 USE kinds, ONLY: dp,&
32 int_8
39 USE orbital_pointers, ONLY: indco,&
40 indso,&
41 nco,&
42 ncoset,&
43 nsoset
46 USE pw_env_types, ONLY: pw_env_get,&
49 USE pw_methods, ONLY: pw_axpy
51 USE pw_types, ONLY: pw_c1d_gs_type,&
65 USE qs_kind_types, ONLY: get_qs_kind,&
66 has_nlcc,&
80 USE qs_rho_types, ONLY: qs_rho_get,&
82 USE qs_vxc_atom_utils, ONLY: &
87 USE skala_gpw_features, ONLY: build_periodic_atom_image_layout,&
88 periodic_atom_image_partition_from_layout,&
90 skala_periodic_partition_type,&
92 USE skala_gpw_functional, ONLY: &
99 USE spherical_harmonics, ONLY: y_lm
100 USE util, ONLY: get_limit
101 USE virial_types, ONLY: virial_type
102 USE xc_atom, ONLY: fill_rho_set,&
121#include "./base/base_uses.f90"
122
123 IMPLICIT NONE
124
125 PRIVATE
126
127 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'qs_vxc_atom'
128
129 ! A wrapped stencil can touch both end tiles and one adjacent interior tile when the final
130 ! tile is shorter than the stencil. Three tiles per direction are therefore sufficient.
131 INTEGER, PARAMETER, PRIVATE :: native_grid_adjoint_tile_edge = 64, &
132 native_grid_adjoint_max_tiles_per_direction = 3, &
133 native_grid_adjoint_max_bins_per_row = &
134 native_grid_adjoint_max_tiles_per_direction**3
135
142
143! **************************************************************************************************
144
145CONTAINS
146
147! **************************************************************************************************
148!> \brief Initialize atom-centered quadrature for native Skala layouts.
149!> \param kind_set quantum kinds
150!> \param dft_control DFT controls supplying the radial quadrature
151! **************************************************************************************************
152 SUBROUTINE ensure_native_skala_atom_grids(kind_set, dft_control)
153 TYPE(qs_kind_type), DIMENSION(:), POINTER :: kind_set
154 TYPE(dft_control_type), POINTER :: dft_control
155
156 INTEGER :: ikind, ll, na, nr, quadrature
157 TYPE(grid_atom_type), POINTER :: grid_atom
158
159 quadrature = dft_control%qs_control%gapw_control%quadrature
160 CALL init_lebedev_grids()
161 DO ikind = 1, SIZE(kind_set)
162 NULLIFY (grid_atom)
163 CALL get_qs_kind(kind_set(ikind), grid_atom=grid_atom, ngrid_ang=na, ngrid_rad=nr)
164 IF (ASSOCIATED(grid_atom)) THEN
165 IF (ASSOCIATED(grid_atom%weight) .AND. grid_atom%nr == nr) cycle
166 ELSE
167 CALL allocate_grid_atom(kind_set(ikind)%grid_atom)
168 grid_atom => kind_set(ikind)%grid_atom
169 END IF
171 na = lebedev_grid(ll)%n
172 grid_atom%ng_sphere = na
173 grid_atom%nr = nr
174 CALL create_grid_atom(grid_atom, nr, na, 0, ll, quadrature)
175 END DO
177
178 END SUBROUTINE ensure_native_skala_atom_grids
179
180! **************************************************************************************************
181!> \brief Decide whether a kind contributes hard-minus-soft primitive fields.
182!> \param paw_atom whether CP2K constructed a one-center representation for the kind
183!> \param gapw_representation requested Skala pseudopotential GAPW representation
184!> \param has_pseudopotential whether the kind uses a GTH or semi-global pseudopotential
185!> \param zeff valence charge of the potential
186!> \param zatom atomic number
187!> \return true when hard-minus-soft fields contribute for this kind
188! **************************************************************************************************
189 PURE FUNCTION native_skala_uses_one_center_kind( &
190 paw_atom, gapw_representation, has_pseudopotential, zeff, zatom) RESULT(use_one_center)
191 LOGICAL, INTENT(IN) :: paw_atom
192 INTEGER, INTENT(IN) :: gapw_representation
193 LOGICAL, INTENT(IN) :: has_pseudopotential
194 REAL(dp), INTENT(IN) :: zeff
195 INTEGER, INTENT(IN) :: zatom
196 LOGICAL :: use_one_center
197
198 use_one_center = paw_atom
199 IF (.NOT. use_one_center) RETURN
200
201 SELECT CASE (gapw_representation)
203 use_one_center = .NOT. has_pseudopotential
205 CONTINUE
207 IF (has_pseudopotential .AND. &
208 abs(zeff - real(zatom, dp)) <= 1.0e-10_dp) use_one_center = .false.
209 END SELECT
210
211 END FUNCTION native_skala_uses_one_center_kind
212
213! **************************************************************************************************
214!> \brief ...
215!> \param qs_env ...
216!> \param energy_only ...
217!> \param exc1 the on-body ex energy contribution
218!> \param adiabatic_rescale_factor ...
219!> \param kind_set_external provides a non-default kind_set to use
220!> \param rho_atom_set_external provides a non-default atomic density set to use
221!> \param xc_section_external provides an external non-default XC
222!> \param calculate_forces ...
223!> \param composite_vxc_rho ...
224!> \param composite_vxc_tau ...
225!> \param composite_reference_active ...
226!> \param direct_valence_atom_grid evaluate the smooth valence fields on atom-centered grids
227!> \param atom_composite_grid evaluate GAPW primitive fields on atom-centered composite grids
228! **************************************************************************************************
229 SUBROUTINE calculate_vxc_atom(qs_env, energy_only, exc1, &
230 adiabatic_rescale_factor, kind_set_external, &
231 rho_atom_set_external, xc_section_external, calculate_forces, &
232 composite_vxc_rho, composite_vxc_tau, composite_reference_active, &
233 direct_valence_atom_grid, atom_composite_grid)
234
235 TYPE(qs_environment_type), POINTER :: qs_env
236 LOGICAL, INTENT(IN) :: energy_only
237 REAL(dp), INTENT(INOUT) :: exc1
238 REAL(dp), INTENT(IN), OPTIONAL :: adiabatic_rescale_factor
239 TYPE(qs_kind_type), DIMENSION(:), OPTIONAL, &
240 POINTER :: kind_set_external
241 TYPE(rho_atom_type), DIMENSION(:), OPTIONAL, &
242 POINTER :: rho_atom_set_external
243 TYPE(section_vals_type), OPTIONAL, POINTER :: xc_section_external
244 LOGICAL, INTENT(IN), OPTIONAL :: calculate_forces
245 TYPE(pw_r3d_rs_type), DIMENSION(:), OPTIONAL, &
246 POINTER :: composite_vxc_rho, composite_vxc_tau
247 LOGICAL, INTENT(OUT), OPTIONAL :: composite_reference_active
248 LOGICAL, INTENT(IN), OPTIONAL :: direct_valence_atom_grid, &
249 atom_composite_grid
250
251 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_vxc_atom'
252
253 INTEGER :: adjoint_bin, adjoint_entry, adjoint_nbins, adjoint_nchannels, &
254 adjoint_tile_count(3), adjoint_tile_lower(3), adjoint_tile_upper(3), &
255 atom_composite_components, bo(2), composite_image_periodicity(3), composite_local_atom, &
256 composite_local_natom, composite_nflat, composite_partition_target_image, &
257 composite_pw_nflat, composite_row, gapw_density_partition, gapw_representation, handle, &
258 ia, iat, iatom, icomponent, idir, ikind, image_i1, image_i2, image_i3, image_lower(3), &
259 image_shell(3), image_shift(3), image_upper(3), ir, ispin, iw, jdir, myfun, na, natom, &
260 nr, nspins, num_pe, on
261 INTEGER :: source_atom, target_atom, xc_deriv_method_id, xc_rho_smooth_id, zatom
262 INTEGER(KIND=int_8), ALLOCATABLE, DIMENSION(:) :: composite_atomic_grid_sizes, &
263 composite_local_grid_sizes
264 INTEGER, ALLOCATABLE, DIMENSION(:) :: adjoint_bin_offsets, adjoint_bin_rows, &
265 composite_atom_end, composite_atom_kind, composite_atom_kind_index, composite_atom_start, &
266 composite_grid_atom, composite_local_atoms
267 INTEGER, DIMENSION(2, 3) :: bounds
268 INTEGER, DIMENSION(:), POINTER :: atom_list
269 LOGICAL :: accint, atom_composite_active, atom_composite_diagnostic, &
270 atom_composite_reference, direct_valence_atom_composite, donlcc, evaluate_hard, &
271 evaluate_soft, gradient_f, image_partition_atom_composite, lsd, my_calculate_forces, &
272 native_grid_diagnostics, nlcc, one_center_kind, paw_atom, paw_pseudopotentials, &
273 requested_atom_composite_grid, rho_g_valid, skala_atom_grid, source_matrix_local, tau_f, &
274 tau_r_valid, use_atom_composite_density, use_atom_composite_gradient, &
275 use_atom_composite_tau, use_virial
276 LOGICAL, ALLOCATABLE, DIMENSION(:) :: composite_partition_included
277 REAL(dp) :: agr, alpha, atom_composite_exc, atom_composite_nelec, &
278 composite_cross_cutoff_max, composite_cross_density_max, composite_cross_grad_max, &
279 composite_cross_kin_max, composite_density_max, composite_density_min, &
280 composite_grad_max, composite_kin_max, composite_kin_min, composite_tau_integral, &
281 cross_cutoff, cross_distance, damping, damping_derivative, density_cut, exc_h, exc_s, &
282 feature_vxc_analytic, feature_vxc_fd, feature_vxc_minus, feature_vxc_plus, &
283 feature_vxc_step, gradient_cut, local_partition_weight, my_adiabatic_rescale_factor, &
284 nlcc_density, nlcc_spin_factor
285 REAL(dp) :: one_center_density_field_contraction, one_center_density_matrix_contraction, &
286 one_center_field_contraction, one_center_gradient_field_contraction, &
287 one_center_gradient_matrix_contraction, one_center_matrix_contraction, &
288 one_center_rho_grad_field_contraction, one_center_rho_grad_matrix_contraction, &
289 one_center_tau_field_contraction, one_center_tau_matrix_contraction, &
290 one_center_tensor_contraction, partition_adjoint, partition_weight, &
291 smooth_grid_contraction, smooth_input_contraction, target_partition_adjoint, tau_cut, zeff
292 REAL(dp), ALLOCATABLE, DIMENSION(:) :: composite_atomic_grid_weight_grad, &
293 composite_atomic_grid_weights, composite_base_grid_weights, composite_distances, &
294 composite_grid_weight_grad, composite_grid_weights, composite_partition_weights, fw
295 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: composite_atom_coord_grad, composite_atom_coords, &
296 composite_cross_density, composite_cross_force, composite_cross_force_local, &
297 composite_cross_kin, composite_density, composite_density_grad, composite_explicit_force, &
298 composite_grid_coord_force, composite_grid_coord_grad, composite_grid_coords, &
299 composite_kin, composite_kin_grad, composite_local_atom_coords, &
300 composite_model_atom_force, composite_moving_smooth_force, composite_nlcc_center_force, &
301 composite_nlcc_center_force_local, composite_nlcc_target_force, &
302 composite_partition_atom_coords
303 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: composite_partition_force, &
304 composite_partition_force_local, composite_smooth_density_cache, &
305 composite_smooth_kin_cache, local_partition_datom
306 REAL(dp), ALLOCATABLE, DIMENSION(:, :), TARGET :: smooth_density_adjoint_storage, &
307 smooth_kin_adjoint_storage
308 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :) :: composite_cross_grad, composite_grad, &
309 composite_grad_grad, composite_int_h, composite_int_s, composite_partition_datom, &
310 composite_partition_dstrain, composite_smooth_gradient_cache, vtau_h_local, vtau_s_local, &
311 vxc_h_local, vxc_s_local
312 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :), TARGET :: smooth_grad_adjoint_storage
313 REAL(dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: vxg_h_local, vxg_s_local
314 REAL(dp), DIMENSION(1, 1, 1) :: tau_d
315 REAL(dp), DIMENSION(1, 1, 1, 1) :: rho_d
316 REAL(dp), DIMENSION(2) :: composite_smooth_density_adjoint_value, &
317 composite_smooth_density_value, composite_smooth_kin_adjoint_value, &
318 composite_smooth_kin_value, cross_density, cross_density_adjoint, cross_kin, &
319 cross_kin_adjoint
320 REAL(dp), DIMENSION(3) :: composite_point, cross_displacement, cross_spatial_derivative, &
321 damping_spatial_derivative, fractional, image_translation, nlcc_gradient, &
322 nlcc_spatial_derivative, skala_atom_force_h, skala_atom_force_s, spatial_derivative
323 REAL(dp), DIMENSION(3, 2) :: composite_smooth_gradient_adjoint_value, &
324 composite_smooth_gradient_value, cross_density_spatial, cross_grad, cross_grad_adjoint, &
325 cross_kin_spatial
326 REAL(dp), DIMENSION(3, 3) :: composite_cross_image_virial, &
327 composite_cross_image_virial_local, composite_explicit_virial, composite_feature_virial, &
328 composite_interpolation_virial, composite_partition_strain_virial, &
329 local_partition_dstrain, nlcc_hessian, skala_atom_virial, skala_atom_virial_h, &
330 skala_atom_virial_s
331 REAL(dp), DIMENSION(3, 3, 2) :: cross_grad_spatial
332 REAL(dp), DIMENSION(4) :: feature_component_analytic, &
333 feature_component_fd
334 REAL(dp), DIMENSION(:, :), POINTER :: rho_nlcc, smooth_density_adjoint, &
335 smooth_kin_adjoint, weight_h, weight_s
336 REAL(dp), DIMENSION(:, :, :), POINTER :: composite_smooth_rho, composite_smooth_rhoa, &
337 composite_smooth_rhob, composite_smooth_tau, composite_smooth_tau_a, &
338 composite_smooth_tau_b, rho_h, rho_s, smooth_grad_adjoint, smooth_rho, smooth_rhoa, &
339 smooth_rhob, smooth_tau, smooth_tau_a, smooth_tau_b, tau_h, tau_s, vtau_h, vtau_s, vxc_h, &
340 vxc_s
341 REAL(dp), DIMENSION(:, :, :, :), POINTER :: drho_h, drho_s, vxg_h, vxg_s
342 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
343 TYPE(cell_type), POINTER :: cell
344 TYPE(cp_3d_r_cp_type), DIMENSION(3) :: composite_smooth_drho, composite_smooth_drhoa, &
345 composite_smooth_drhob, smooth_drho, smooth_drhoa, smooth_drhob
346 TYPE(dft_control_type), POINTER :: dft_control
347 TYPE(grid_atom_type), POINTER :: grid_atom
348 TYPE(gth_potential_type), POINTER :: gth_potential
349 TYPE(gto_basis_set_type), POINTER :: basis_1c
350 TYPE(harmonics_atom_type), POINTER :: harmonics
351 TYPE(mp_para_env_type), POINTER :: para_env
352 TYPE(native_grid_interpolation_stencil_type) :: interpolation_stencil
353 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
354 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: smooth_rho_g
355 TYPE(pw_env_type), POINTER :: pw_env
356 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
357 TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: smooth_rho_r, smooth_tau_r, &
358 smooth_vxc_rho, smooth_vxc_tau
359 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
360 TYPE(qs_kind_type), DIMENSION(:), POINTER :: my_kind_set
361 TYPE(qs_rho_type), POINTER :: rho_struct
362 TYPE(rho_atom_coeff), DIMENSION(:), POINTER :: cpc_h, cpc_s, dr_h, dr_s, int_hh, &
363 int_ss, r_h, r_s
364 TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER :: r_h_d, r_s_d
365 TYPE(rho_atom_type), DIMENSION(:), POINTER :: my_rho_atom_set
366 TYPE(rho_atom_type), POINTER :: rho_atom
367 TYPE(section_vals_type), POINTER :: input, model_section, my_xc_section, &
368 native_section, xc_fun_section
369 TYPE(sgp_potential_type), POINTER :: sgp_potential
370 TYPE(skala_periodic_partition_type) :: composite_partition_image_coords
371 TYPE(tau_basis_cache_type) :: tau_basis_cache
372 TYPE(virial_type), POINTER :: virial
373 TYPE(xc_derivative_set_type) :: deriv_set
374 TYPE(xc_rho_cflags_type) :: needs
375 TYPE(xc_rho_set_type) :: rho_set_h, rho_set_s, smooth_rho_set
376
377! -------------------------------------------------------------------------
378
379 CALL timeset(routinen, handle)
380
381 NULLIFY (atom_list)
382 NULLIFY (auxbas_pw_pool)
383 NULLIFY (my_kind_set)
384 NULLIFY (atomic_kind_set)
385 NULLIFY (cell)
386 NULLIFY (grid_atom)
387 NULLIFY (gth_potential)
388 NULLIFY (force)
389 NULLIFY (harmonics)
390 NULLIFY (input)
391 NULLIFY (para_env)
392 NULLIFY (particle_set)
393 NULLIFY (pw_env)
394 NULLIFY (rho_atom)
395 NULLIFY (rho_struct)
396 NULLIFY (my_rho_atom_set)
397 NULLIFY (rho_nlcc)
398 NULLIFY (smooth_rho, smooth_rhoa, smooth_rhob, smooth_tau, smooth_tau_a, smooth_tau_b)
399 NULLIFY (composite_smooth_rho, composite_smooth_rhoa, composite_smooth_rhob, &
400 composite_smooth_tau, composite_smooth_tau_a, composite_smooth_tau_b)
401 NULLIFY (smooth_rho_g, smooth_rho_r, smooth_tau_r)
402 NULLIFY (smooth_vxc_rho, smooth_vxc_tau)
403 DO idir = 1, 3
404 NULLIFY (smooth_drho(idir)%array, smooth_drhoa(idir)%array, smooth_drhob(idir)%array)
405 NULLIFY (composite_smooth_drho(idir)%array, &
406 composite_smooth_drhoa(idir)%array, &
407 composite_smooth_drhob(idir)%array)
408 END DO
409 NULLIFY (sgp_potential)
410 NULLIFY (virial)
411 my_calculate_forces = .false.
412 IF (PRESENT(calculate_forces)) my_calculate_forces = calculate_forces
413 IF (PRESENT(composite_reference_active)) composite_reference_active = .false.
414 direct_valence_atom_composite = .false.
415 IF (PRESENT(direct_valence_atom_grid)) THEN
416 direct_valence_atom_composite = direct_valence_atom_grid
417 END IF
418 requested_atom_composite_grid = .false.
419 IF (PRESENT(atom_composite_grid)) requested_atom_composite_grid = atom_composite_grid
420
421 IF (PRESENT(adiabatic_rescale_factor)) THEN
422 my_adiabatic_rescale_factor = adiabatic_rescale_factor
423 ELSE
424 my_adiabatic_rescale_factor = 1.0_dp
425 END IF
426
427 CALL get_qs_env(qs_env=qs_env, &
428 dft_control=dft_control, &
429 cell=cell, &
430 para_env=para_env, &
431 atomic_kind_set=atomic_kind_set, &
432 qs_kind_set=my_kind_set, &
433 input=input, &
434 particle_set=particle_set, &
435 pw_env=pw_env, &
436 virial=virial, &
437 rho_atom_set=my_rho_atom_set, &
438 force=force)
439
440 IF (dft_control%qs_control%gapw_xc) THEN
441 CALL get_qs_env(qs_env=qs_env, rho_xc=rho_struct)
442 ELSE
443 CALL get_qs_env(qs_env=qs_env, rho=rho_struct)
444 END IF
445
446 IF (PRESENT(kind_set_external)) my_kind_set => kind_set_external
447 IF (PRESENT(rho_atom_set_external)) my_rho_atom_set => rho_atom_set_external
448
449 nlcc = has_nlcc(my_kind_set)
450 accint = dft_control%qs_control%gapw_control%accurate_xcint
451
452 my_xc_section => section_vals_get_subs_vals(input, "DFT%XC")
453
454 IF (PRESENT(xc_section_external)) my_xc_section => xc_section_external
455
456 xc_fun_section => section_vals_get_subs_vals(my_xc_section, "XC_FUNCTIONAL")
457 CALL section_vals_val_get(xc_fun_section, "_SECTION_PARAMETERS_", &
458 i_val=myfun)
459 skala_atom_grid = xc_section_uses_gauxc_model(my_xc_section)
460 gapw_representation = skala_gapw_cp2k_default
461 atom_composite_diagnostic = .false.
462 atom_composite_reference = .false.
463 paw_pseudopotentials = .false.
464 native_grid_diagnostics = .false.
465 atom_composite_components = 1
466 feature_vxc_step = 3.0e-3_dp
467 IF (skala_atom_grid) THEN
468 native_section => get_skala_native_section(my_xc_section)
469 model_section => native_section
470 IF (.NOT. ASSOCIATED(model_section)) model_section => get_gauxc_section(my_xc_section)
471 cpassert(ASSOCIATED(model_section))
472 CALL section_vals_val_get(model_section, "PSEUDOPOTENTIAL_GAPW_REPRESENTATION", &
473 i_val=gapw_representation)
474 IF (ASSOCIATED(native_section)) THEN
475 CALL section_vals_val_get(native_section, "DIAGNOSTICS", &
476 l_val=native_grid_diagnostics)
477 END IF
478 END IF
479 IF (skala_atom_grid) THEN
480 DO ikind = 1, SIZE(my_kind_set)
481 NULLIFY (gth_potential, sgp_potential)
482 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
483 gth_potential=gth_potential, sgp_potential=sgp_potential)
484 paw_pseudopotentials = paw_pseudopotentials .OR. &
485 (paw_atom .AND. (ASSOCIATED(gth_potential) .OR. &
486 ASSOCIATED(sgp_potential)))
487 END DO
488 END IF
489 IF (skala_atom_grid .AND. xc_section_uses_native_skala_evaluator(my_xc_section)) THEN
490 native_section => get_skala_native_section(my_xc_section)
491 cpassert(ASSOCIATED(native_section))
492 CALL section_vals_val_get(native_section, &
493 "GAPW_ATOM_COMPOSITE_DIAGNOSTIC", &
494 l_val=atom_composite_diagnostic)
495 CALL section_vals_val_get(native_section, &
496 "GAPW_ATOM_COMPOSITE_REFERENCE", &
497 l_val=atom_composite_reference)
498 CALL section_vals_val_get(native_section, &
499 "GAPW_ATOM_COMPOSITE_COMPONENTS", &
500 i_val=atom_composite_components)
501 CALL section_vals_val_get(native_section, &
502 "GAPW_ATOM_COMPOSITE_FD_STEP", &
503 r_val=feature_vxc_step)
504 END IF
505 atom_composite_reference = atom_composite_reference .OR. &
506 (gapw_representation == skala_gapw_paw_one_center .AND. &
507 paw_pseudopotentials)
508 atom_composite_reference = atom_composite_reference .OR. requested_atom_composite_grid
509 atom_composite_reference = atom_composite_reference .OR. direct_valence_atom_composite
510 atom_composite_active = atom_composite_diagnostic .OR. atom_composite_reference
511 use_atom_composite_density = atom_composite_components <= 2
512 use_atom_composite_gradient = atom_composite_components <= 2
513 use_atom_composite_tau = atom_composite_components == 1 .OR. &
514 atom_composite_components == 3
515 IF (atom_composite_active) THEN
516 CALL ensure_native_skala_atom_grids(my_kind_set, dft_control)
517 END IF
518 IF (direct_valence_atom_composite) THEN
519 use_atom_composite_density = .false.
520 use_atom_composite_gradient = .false.
521 use_atom_composite_tau = .false.
522 END IF
523 ! CP2K's auxiliary PW fields are represented on one index-periodic cell even
524 ! when the physical Poisson problem is isolated or partially periodic.
525 image_partition_atom_composite = atom_composite_active
526 composite_image_periodicity = 1
527 IF (PRESENT(composite_reference_active)) composite_reference_active = atom_composite_reference
528 gapw_density_partition = skala_gapw_density_partition_hard_minus_soft
529 IF (skala_atom_grid) THEN
530 gapw_density_partition = native_skala_gapw_density_partition(my_xc_section)
531 END IF
532 use_virial = ASSOCIATED(virial)
533 IF (use_virial) use_virial = my_calculate_forces .AND. &
534 virial%pv_calculate .AND. (.NOT. virial%pv_numer)
535
536 IF (myfun == xc_none) THEN
537 exc1 = 0.0_dp
538 my_rho_atom_set(:)%exc_h = 0.0_dp
539 my_rho_atom_set(:)%exc_s = 0.0_dp
540 ELSE
541 CALL section_vals_val_get(my_xc_section, "DENSITY_CUTOFF", &
542 r_val=density_cut)
543 CALL section_vals_val_get(my_xc_section, "GRADIENT_CUTOFF", &
544 r_val=gradient_cut)
545 CALL section_vals_val_get(my_xc_section, "TAU_CUTOFF", &
546 r_val=tau_cut)
547
548 lsd = dft_control%lsd
549 nspins = dft_control%nspins
550 needs = xc_functionals_get_needs(xc_fun_section, &
551 lsd=lsd, &
552 calc_potential=.true.)
553
554 gradient_f = (needs%drho .OR. needs%drho_spin) .OR. skala_atom_grid
555 tau_f = (needs%tau .OR. needs%tau_spin) .OR. skala_atom_grid
556
557 IF (atom_composite_active) THEN
558 IF (lsd) THEN
559 needs%rho_spin = .true.
560 needs%drho_spin = .true.
561 needs%tau_spin = .true.
562 ELSE
563 needs%rho = .true.
564 needs%drho = .true.
565 needs%tau = .true.
566 END IF
567
568 ALLOCATE (composite_atomic_grid_sizes(SIZE(particle_set)), &
569 composite_atom_kind(SIZE(particle_set)), &
570 composite_atom_kind_index(SIZE(particle_set)), &
571 composite_atom_start(SIZE(particle_set)), &
572 composite_atom_end(SIZE(particle_set)), &
573 composite_atom_coords(3, SIZE(particle_set)), &
574 composite_partition_weights(SIZE(particle_set)), &
575 composite_partition_atom_coords(3, SIZE(particle_set)), &
576 composite_distances(SIZE(particle_set)))
577 composite_atomic_grid_sizes = 0_int_8
578 composite_atom_kind = 0
579 composite_atom_kind_index = 0
580 composite_atom_start = 0
581 composite_atom_end = 0
582 DO iatom = 1, SIZE(particle_set)
583 composite_atom_coords(:, iatom) = particle_set(iatom)%r
584 END DO
585 DO ikind = 1, SIZE(atomic_kind_set)
586 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
587 NULLIFY (gth_potential, sgp_potential)
588 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
589 gth_potential=gth_potential, grid_atom=grid_atom, &
590 sgp_potential=sgp_potential, zatom=zatom, zeff=zeff)
591 DO iat = 1, natom
592 iatom = atom_list(iat)
593 composite_atomic_grid_sizes(iatom) = int(grid_atom%nr*grid_atom%ng_sphere, kind=int_8)
594 composite_atom_kind(iatom) = ikind
595 composite_atom_kind_index(iatom) = iat
596 END DO
597 END DO
598 IF (any(composite_atomic_grid_sizes <= 0_int_8)) THEN
599 CALL cp_abort(__location__, &
600 "The atom-composite diagnostic requires a GAPW one-center grid for every atom.")
601 END IF
602
603 composite_local_natom = 0
604 DO ikind = 1, SIZE(atomic_kind_set)
605 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
606 NULLIFY (gth_potential, sgp_potential)
607 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
608 gth_potential=gth_potential, sgp_potential=sgp_potential, &
609 zatom=zatom, zeff=zeff)
610 bo = get_limit(natom, para_env%num_pe, para_env%mepos)
611 composite_local_natom = composite_local_natom + max(0, bo(2) - bo(1) + 1)
612 END DO
613 ALLOCATE (composite_local_atoms(composite_local_natom), &
614 composite_local_grid_sizes(composite_local_natom), &
615 composite_local_atom_coords(3, composite_local_natom))
616 composite_local_atom = 0
617 composite_nflat = 0
618 DO ikind = 1, SIZE(atomic_kind_set)
619 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
620 NULLIFY (gth_potential, sgp_potential)
621 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
622 gth_potential=gth_potential, sgp_potential=sgp_potential, &
623 zatom=zatom, zeff=zeff)
624 bo = get_limit(natom, para_env%num_pe, para_env%mepos)
625 DO iat = bo(1), bo(2)
626 iatom = atom_list(iat)
627 composite_local_atom = composite_local_atom + 1
628 composite_local_atoms(composite_local_atom) = iatom
629 composite_local_grid_sizes(composite_local_atom) = &
630 composite_atomic_grid_sizes(iatom)
631 composite_local_atom_coords(:, composite_local_atom) = &
632 composite_atom_coords(:, iatom)
633 composite_atom_start(iatom) = composite_nflat + 1
634 composite_nflat = composite_nflat + int(composite_atomic_grid_sizes(iatom))
635 composite_atom_end(iatom) = composite_nflat
636 END DO
637 END DO
638 cpassert(composite_local_atom == composite_local_natom)
639 ALLOCATE (composite_density(composite_nflat, 2), &
640 composite_grad(composite_nflat, 3, 2), &
641 composite_kin(composite_nflat, 2), &
642 composite_grid_atom(composite_nflat), &
643 composite_smooth_density_cache(composite_nflat, 2), &
644 composite_smooth_gradient_cache(composite_nflat, 3, 2), &
645 composite_smooth_kin_cache(composite_nflat, 2), &
646 composite_grid_coords(3, composite_nflat), &
647 composite_grid_weights(composite_nflat), &
648 composite_base_grid_weights(composite_nflat), &
649 composite_atomic_grid_weights(composite_nflat))
650 composite_density = 0.0_dp
651 composite_grad = 0.0_dp
652 composite_kin = 0.0_dp
653 DO composite_local_atom = 1, composite_local_natom
654 iatom = composite_local_atoms(composite_local_atom)
655 composite_grid_atom(composite_atom_start(iatom):composite_atom_end(iatom)) = iatom
656 END DO
657 composite_smooth_density_cache = 0.0_dp
658 composite_smooth_gradient_cache = 0.0_dp
659 composite_smooth_kin_cache = 0.0_dp
660 composite_grid_coords = 0.0_dp
661 composite_grid_weights = 0.0_dp
662 composite_base_grid_weights = 0.0_dp
663 composite_atomic_grid_weights = 0.0_dp
664
665 CALL qs_rho_get(rho_struct, rho_r=smooth_rho_r, rho_g=smooth_rho_g, &
666 tau_r=smooth_tau_r, rho_g_valid=rho_g_valid, &
667 tau_r_valid=tau_r_valid)
668 cpassert(rho_g_valid)
669 cpassert(tau_r_valid)
670 cpassert(ASSOCIATED(smooth_rho_r))
671 cpassert(ASSOCIATED(smooth_rho_g))
672 cpassert(ASSOCIATED(smooth_tau_r))
673 CALL prepare_native_grid_cache(qs_env%native_grid_cache, smooth_rho_r(1)%pw_grid, &
674 cell, composite_nflat, image_partition_atom_composite)
675 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
676 CALL section_vals_val_get(my_xc_section, "XC_GRID%XC_DERIV", &
677 i_val=xc_deriv_method_id)
678 CALL section_vals_val_get(my_xc_section, "XC_GRID%XC_SMOOTH_RHO", &
679 i_val=xc_rho_smooth_id)
680 CALL xc_rho_set_create(smooth_rho_set, smooth_rho_r(1)%pw_grid%bounds_local, &
681 rho_cutoff=section_get_rval(my_xc_section, "density_cutoff"), &
682 drho_cutoff=section_get_rval(my_xc_section, "gradient_cutoff"), &
683 tau_cutoff=section_get_rval(my_xc_section, "tau_cutoff"))
684 CALL xc_rho_set_update(smooth_rho_set, smooth_rho_r, smooth_rho_g, smooth_tau_r, needs, &
685 xc_deriv_method_id, xc_rho_smooth_id, auxbas_pw_pool)
686 IF (lsd) THEN
687 CALL xc_rho_set_get(smooth_rho_set, rhoa=smooth_rhoa, rhob=smooth_rhob, &
688 drhoa=smooth_drhoa, drhob=smooth_drhob, &
689 tau_a=smooth_tau_a, tau_b=smooth_tau_b)
690 CALL gather_native_grid_field(smooth_rhoa, smooth_rho_r(1)%pw_grid, para_env, &
691 composite_smooth_rhoa)
692 CALL gather_native_grid_field(smooth_rhob, smooth_rho_r(1)%pw_grid, para_env, &
693 composite_smooth_rhob)
694 CALL gather_native_grid_field(smooth_tau_a, smooth_rho_r(1)%pw_grid, para_env, &
695 composite_smooth_tau_a)
696 CALL gather_native_grid_field(smooth_tau_b, smooth_rho_r(1)%pw_grid, para_env, &
697 composite_smooth_tau_b)
698 DO idir = 1, 3
699 CALL gather_native_grid_field(smooth_drhoa(idir)%array, &
700 smooth_rho_r(1)%pw_grid, para_env, &
701 composite_smooth_drhoa(idir)%array)
702 CALL gather_native_grid_field(smooth_drhob(idir)%array, &
703 smooth_rho_r(1)%pw_grid, para_env, &
704 composite_smooth_drhob(idir)%array)
705 END DO
706 ELSE
707 CALL xc_rho_set_get(smooth_rho_set, rho=smooth_rho, drho=smooth_drho, &
708 tau=smooth_tau)
709 CALL gather_native_grid_field(smooth_rho, smooth_rho_r(1)%pw_grid, para_env, &
710 composite_smooth_rho)
711 CALL gather_native_grid_field(smooth_tau, smooth_rho_r(1)%pw_grid, para_env, &
712 composite_smooth_tau)
713 DO idir = 1, 3
714 CALL gather_native_grid_field(smooth_drho(idir)%array, &
715 smooth_rho_r(1)%pw_grid, para_env, &
716 composite_smooth_drho(idir)%array)
717 END DO
718 END IF
719 END IF
720
721 ! Initialize energy contribution from the one center XC terms to zero
722 exc1 = 0.0_dp
723
724 ! Nullify some pointers for work-arrays
725 NULLIFY (rho_h, drho_h, rho_s, drho_s, weight_h, weight_s)
726 NULLIFY (vxc_h, vxc_s, vxg_h, vxg_s)
727 NULLIFY (tau_h, tau_s)
728 NULLIFY (vtau_h, vtau_s)
729
730 ! Here starts the loop over all the atoms
731
732 DO ikind = 1, SIZE(atomic_kind_set)
733 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
734 NULLIFY (gth_potential, sgp_potential)
735 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
736 gth_potential=gth_potential, harmonics=harmonics, &
737 grid_atom=grid_atom, sgp_potential=sgp_potential, &
738 zatom=zatom, zeff=zeff)
739 one_center_kind = .NOT. direct_valence_atom_composite
740 IF (one_center_kind) THEN
741 CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
742 one_center_kind = paw_atom
743 IF (skala_atom_grid) THEN
744 one_center_kind = native_skala_uses_one_center_kind( &
745 paw_atom, gapw_representation, &
746 ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential), &
747 zeff, zatom)
748 END IF
749 END IF
750 IF (.NOT. one_center_kind .AND. .NOT. atom_composite_active) cycle
751
752 nr = grid_atom%nr
753 na = grid_atom%ng_sphere
754
755 IF (one_center_kind) THEN
756 ! Prepare the structures needed to calculate and store the one-center XC derivatives.
757
758 ! Array dimension: here anly one dimensional arrays are used,
759 ! i.e. only the first column of deriv_data is read.
760 ! The other to dimensions are set to size equal 1
761 bounds(1:2, 1:3) = 1
762 bounds(2, 1) = na
763 bounds(2, 2) = nr
764
765 ! set integration weights
766 weight_h => grid_atom%weight
767 weight_s => grid_atom%weight
768 IF (accint) THEN
769 on = dft_control%qs_control%gapw_control%oweights
770 alpha = dft_control%qs_control%gapw_control%aw(ikind)
771 IF (ASSOCIATED(grid_atom%gapw_weight_s)) THEN
772 IF (grid_atom%gapw_weight_alpha /= alpha) DEALLOCATE (grid_atom%gapw_weight_s)
773 END IF
774 IF (.NOT. ASSOCIATED(grid_atom%gapw_weight_s)) THEN
775 ALLOCATE (grid_atom%gapw_weight_s(na, nr))
776 ALLOCATE (fw(nr))
777 CALL calc_weight_function(fw, grid_atom%rad2, on, alpha)
778 DO ir = 1, nr
779 agr = 1.0_dp - fw(ir)
780 grid_atom%gapw_weight_s(:, ir) = agr*grid_atom%weight(:, ir)
781 END DO
782 DEALLOCATE (fw)
783 grid_atom%gapw_weight_alpha = alpha
784 END IF
785 weight_s => grid_atom%gapw_weight_s
786 END IF
787
788 ! create a place where to put the derivatives
789 CALL xc_dset_create(deriv_set, local_bounds=bounds)
790 ! create the place where to store the argument for the functionals
791 CALL xc_rho_set_create(rho_set_h, bounds, rho_cutoff=density_cut, &
792 drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
793 CALL xc_rho_set_create(rho_set_s, bounds, rho_cutoff=density_cut, &
794 drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
795
796 ! allocate the required 3d arrays where to store rho and drho
797 CALL xc_rho_set_atom_update(rho_set_h, needs, nspins, bounds)
798 CALL xc_rho_set_atom_update(rho_set_s, needs, nspins, bounds)
799
800 CALL reallocate(rho_h, 1, na, 1, nr, 1, nspins)
801 CALL reallocate(rho_s, 1, na, 1, nr, 1, nspins)
802 CALL reallocate(vxc_h, 1, na, 1, nr, 1, nspins)
803 CALL reallocate(vxc_s, 1, na, 1, nr, 1, nspins)
804 !
805 IF (gradient_f) THEN
806 CALL reallocate(drho_h, 1, 4, 1, na, 1, nr, 1, nspins)
807 CALL reallocate(drho_s, 1, 4, 1, na, 1, nr, 1, nspins)
808 CALL reallocate(vxg_h, 1, 3, 1, na, 1, nr, 1, nspins)
809 CALL reallocate(vxg_s, 1, 3, 1, na, 1, nr, 1, nspins)
810 END IF
811
812 IF (tau_f) THEN
813 CALL create_tau_basis_cache(tau_basis_cache, grid_atom, basis_1c, harmonics)
814 CALL reallocate(tau_h, 1, na, 1, nr, 1, nspins)
815 CALL reallocate(tau_s, 1, na, 1, nr, 1, nspins)
816 CALL reallocate(vtau_h, 1, na, 1, nr, 1, nspins)
817 CALL reallocate(vtau_s, 1, na, 1, nr, 1, nspins)
818 END IF
819
820 ! NLCC for separate hard and soft one-center densities.
821 donlcc = .false.
822 IF (nlcc) THEN
823 NULLIFY (rho_nlcc)
824 rho_nlcc => my_kind_set(ikind)%nlcc_pot
825 IF (ASSOCIATED(rho_nlcc)) donlcc = .true.
826 END IF
827 END IF
828
829 ! Distribute the atoms of this kind
830
831 num_pe = para_env%num_pe
832 bo = get_limit(natom, para_env%num_pe, para_env%mepos)
833
834 DO iat = bo(1), bo(2)
835 iatom = atom_list(iat)
836
837 IF (one_center_kind) THEN
838 my_rho_atom_set(iatom)%exc_h = 0.0_dp
839 my_rho_atom_set(iatom)%exc_s = 0.0_dp
840
841 rho_atom => my_rho_atom_set(iatom)
842 rho_h = 0.0_dp
843 rho_s = 0.0_dp
844 IF (gradient_f) THEN
845 NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
846 CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, &
847 rho_rad_s=r_s, drho_rad_h=dr_h, &
848 drho_rad_s=dr_s, rho_rad_h_d=r_h_d, &
849 rho_rad_s_d=r_s_d)
850 drho_h = 0.0_dp
851 drho_s = 0.0_dp
852 ELSE
853 NULLIFY (r_h, r_s)
854 CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, rho_rad_s=r_s)
855 rho_d = 0.0_dp
856 END IF
857 IF (tau_f) THEN
858 CALL calc_tau_atom(tau_h, tau_s, rho_atom, tau_basis_cache, nspins)
859 ELSE
860 tau_d = 0.0_dp
861 END IF
862
863 DO ir = 1, nr
864 CALL calc_rho_angular(grid_atom, harmonics, nspins, gradient_f, &
865 ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, &
866 r_h_d, r_s_d, drho_h, drho_s)
867 IF (donlcc) THEN
868 CALL calc_rho_nlcc(grid_atom, nspins, gradient_f, &
869 ir, rho_nlcc(:, 1), rho_h, rho_s, &
870 rho_nlcc(:, 2), drho_h, drho_s)
871 END IF
872 END DO
873 END IF
874
875 IF (atom_composite_active) THEN
876 IF (image_partition_atom_composite) THEN
877 CALL build_periodic_atom_image_layout( &
878 composite_atom_coords, cell, iatom, composite_image_periodicity, &
879 composite_partition_image_coords, composite_partition_target_image)
880 END IF
881!$OMP PARALLEL DO COLLAPSE(2) IF (image_partition_atom_composite) SCHEDULE(STATIC) DEFAULT(NONE) &
882!$OMP PRIVATE(composite_row, composite_point, composite_smooth_density_value, &
883!$OMP composite_smooth_gradient_value, composite_smooth_kin_value, &
884!$OMP idir, ispin, &
885!$OMP gth_potential, nlcc_density, nlcc_gradient, nlcc_hessian, nlcc_spin_factor, &
886!$OMP interpolation_stencil, partition_weight, sgp_potential, source_atom) &
887!$OMP SHARED(atom_composite_reference, cell, composite_atom_coords, composite_atom_kind, &
888!$OMP composite_atom_start, composite_atomic_grid_weights, composite_base_grid_weights, &
889!$OMP composite_density, composite_distances, composite_grad, &
890!$OMP composite_grid_coords, composite_grid_weights, composite_kin, &
891!$OMP composite_partition_atom_coords, composite_partition_image_coords, &
892!$OMP composite_partition_target_image, composite_partition_weights, &
893!$OMP composite_smooth_density_cache, composite_smooth_drho, composite_smooth_drhoa, &
894!$OMP composite_smooth_drhob, composite_smooth_gradient_cache, &
895!$OMP composite_smooth_kin_cache, composite_smooth_rho, composite_smooth_rhoa, &
896!$OMP composite_smooth_rhob, composite_smooth_tau, composite_smooth_tau_a, &
897!$OMP composite_smooth_tau_b, drho_h, drho_s, grid_atom, iatom, &
898!$OMP image_partition_atom_composite, lsd, my_kind_set, na, nlcc, nr, one_center_kind, &
899!$OMP particle_set, qs_env, rho_h, rho_s, smooth_rho_r, tau_h, tau_s, &
900!$OMP use_atom_composite_density, use_atom_composite_gradient, use_atom_composite_tau)
901 DO ir = 1, nr
902 DO ia = 1, na
903 composite_row = composite_atom_start(iatom) + (ir - 1)*na + ia - 1
904 composite_point(1) = particle_set(iatom)%r(1) + grid_atom%rad(ir)* &
905 grid_atom%sin_pol(ia)*grid_atom%cos_azi(ia)
906 composite_point(2) = particle_set(iatom)%r(2) + grid_atom%rad(ir)* &
907 grid_atom%sin_pol(ia)*grid_atom%sin_azi(ia)
908 composite_point(3) = particle_set(iatom)%r(3) + &
909 grid_atom%rad(ir)*grid_atom%cos_pol(ia)
910 composite_grid_coords(:, composite_row) = composite_point
911 IF (image_partition_atom_composite) THEN
912 CALL periodic_atom_image_partition_from_layout( &
913 composite_point, composite_partition_image_coords, &
914 composite_partition_target_image, partition_weight)
915 ELSE
917 composite_point, composite_atom_coords, cell, &
918 composite_partition_weights, composite_partition_atom_coords, &
919 composite_distances)
920 partition_weight = composite_partition_weights(iatom)
921 END IF
922 composite_base_grid_weights(composite_row) = grid_atom%weight(ia, ir)
923 composite_atomic_grid_weights(composite_row) = &
924 composite_base_grid_weights(composite_row)
925 composite_grid_weights(composite_row) = &
926 composite_base_grid_weights(composite_row)* &
927 partition_weight
928 IF (.NOT. fetch_native_grid_stencil(qs_env%native_grid_cache, composite_row, &
929 composite_point, interpolation_stencil)) THEN
930 CALL create_native_grid_interpolation_stencil( &
931 interpolation_stencil, smooth_rho_r(1)%pw_grid, cell, composite_point, &
932 image_partition_atom_composite)
933 CALL store_native_grid_stencil(qs_env%native_grid_cache, composite_row, &
934 composite_point, interpolation_stencil)
935 END IF
936 IF (lsd) THEN
937 CALL interpolate_native_grid_fields( &
938 composite_smooth_rhoa, composite_smooth_drhoa(1)%array, &
939 composite_smooth_drhoa(2)%array, composite_smooth_drhoa(3)%array, &
940 composite_smooth_tau_a, interpolation_stencil, &
941 composite_smooth_density_value(1), &
942 composite_smooth_gradient_value(:, 1), &
943 composite_smooth_kin_value(1))
944 CALL interpolate_native_grid_fields( &
945 composite_smooth_rhob, composite_smooth_drhob(1)%array, &
946 composite_smooth_drhob(2)%array, composite_smooth_drhob(3)%array, &
947 composite_smooth_tau_b, interpolation_stencil, &
948 composite_smooth_density_value(2), &
949 composite_smooth_gradient_value(:, 2), &
950 composite_smooth_kin_value(2))
951 composite_smooth_density_cache(composite_row, :) = &
952 composite_smooth_density_value
953 composite_smooth_gradient_cache(composite_row, :, :) = &
954 composite_smooth_gradient_value
955 composite_smooth_kin_cache(composite_row, :) = &
956 composite_smooth_kin_value
957 DO ispin = 1, 2
958 composite_density(composite_row, ispin) = &
959 composite_smooth_density_value(ispin)
960 composite_grad(composite_row, :, ispin) = &
961 composite_smooth_gradient_value(:, ispin)
962 composite_kin(composite_row, ispin) = &
963 composite_smooth_kin_value(ispin)
964 IF (one_center_kind .AND. use_atom_composite_density) THEN
965 composite_density(composite_row, ispin) = &
966 composite_density(composite_row, ispin) + &
967 rho_h(ia, ir, ispin) - rho_s(ia, ir, ispin)
968 END IF
969 IF (one_center_kind .AND. use_atom_composite_gradient) THEN
970 DO idir = 1, 3
971 composite_grad(composite_row, idir, ispin) = &
972 composite_grad(composite_row, idir, ispin) + &
973 drho_h(idir, ia, ir, ispin) - drho_s(idir, ia, ir, ispin)
974 END DO
975 END IF
976 IF (one_center_kind .AND. use_atom_composite_tau) THEN
977 composite_kin(composite_row, ispin) = &
978 composite_kin(composite_row, ispin) + &
979 tau_h(ia, ir, ispin) - tau_s(ia, ir, ispin)
980 END IF
981 END DO
982 ELSE
983 CALL interpolate_native_grid_fields( &
984 composite_smooth_rho, composite_smooth_drho(1)%array, &
985 composite_smooth_drho(2)%array, composite_smooth_drho(3)%array, &
986 composite_smooth_tau, interpolation_stencil, &
987 composite_smooth_density_value(1), &
988 composite_smooth_gradient_value(:, 1), &
989 composite_smooth_kin_value(1))
990 composite_smooth_density_cache(composite_row, 1) = &
991 composite_smooth_density_value(1)
992 composite_smooth_gradient_cache(composite_row, :, 1) = &
993 composite_smooth_gradient_value(:, 1)
994 composite_smooth_kin_cache(composite_row, 1) = &
995 composite_smooth_kin_value(1)
996 composite_density(composite_row, :) = &
997 0.5_dp*composite_smooth_density_value(1)
998 DO idir = 1, 3
999 composite_grad(composite_row, idir, :) = &
1000 0.5_dp*composite_smooth_gradient_value(idir, 1)
1001 END DO
1002 composite_kin(composite_row, :) = &
1003 0.5_dp*composite_smooth_kin_value(1)
1004 IF (one_center_kind .AND. use_atom_composite_density) THEN
1005 composite_density(composite_row, :) = &
1006 composite_density(composite_row, :) + &
1007 0.5_dp*(rho_h(ia, ir, 1) - rho_s(ia, ir, 1))
1008 END IF
1009 IF (one_center_kind .AND. use_atom_composite_gradient) THEN
1010 DO idir = 1, 3
1011 composite_grad(composite_row, idir, :) = &
1012 composite_grad(composite_row, idir, :) + &
1013 0.5_dp*(drho_h(idir, ia, ir, 1) - &
1014 drho_s(idir, ia, ir, 1))
1015 END DO
1016 END IF
1017 IF (one_center_kind .AND. use_atom_composite_tau) THEN
1018 composite_kin(composite_row, :) = composite_kin(composite_row, :) + &
1019 0.5_dp*(tau_h(ia, ir, 1) - &
1020 tau_s(ia, ir, 1))
1021 END IF
1022 END IF
1023 IF (atom_composite_reference .AND. nlcc) THEN
1024 nlcc_spin_factor = merge(1.0_dp, 0.5_dp, lsd)
1025 DO source_atom = 1, SIZE(particle_set)
1026 NULLIFY (gth_potential, sgp_potential)
1027 CALL get_qs_kind(my_kind_set(composite_atom_kind(source_atom)), &
1028 gth_potential=gth_potential, &
1029 sgp_potential=sgp_potential)
1031 composite_point, particle_set(source_atom)%r, &
1032 gth_potential, sgp_potential, nlcc_density, &
1033 nlcc_gradient, nlcc_hessian)
1034 composite_density(composite_row, :) = &
1035 composite_density(composite_row, :) + &
1036 nlcc_spin_factor*nlcc_density
1037 DO idir = 1, 3
1038 composite_grad(composite_row, idir, :) = &
1039 composite_grad(composite_row, idir, :) + &
1040 nlcc_spin_factor*nlcc_gradient(idir)
1041 END DO
1042 END DO
1043 END IF
1044 END DO
1045 END DO
1046!$OMP END PARALLEL DO
1047 cpassert(nr*na == composite_atom_end(iatom) - composite_atom_start(iatom) + 1)
1048 END IF
1049
1050 IF (.NOT. one_center_kind) cycle
1051
1052 DO ir = 1, nr
1053 IF (tau_f) THEN
1054 CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, tau_h, na, ir)
1055 CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, tau_s, na, ir)
1056 ELSE IF (gradient_f) THEN
1057 CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, tau_d, na, ir)
1058 CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, tau_d, na, ir)
1059 ELSE
1060 CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, rho_d, tau_d, na, ir)
1061 CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, rho_d, tau_d, na, ir)
1062 END IF
1063 END DO
1064
1065 evaluate_hard = .true.
1066 evaluate_soft = .true.
1067 skala_atom_force_h = 0.0_dp
1068 skala_atom_force_s = 0.0_dp
1069 skala_atom_virial_h = 0.0_dp
1070 skala_atom_virial_s = 0.0_dp
1071 IF (skala_atom_grid) THEN
1072 SELECT CASE (gapw_density_partition)
1074 CONTINUE
1076 evaluate_soft = .false.
1078 evaluate_hard = .false.
1080 evaluate_hard = .false.
1081 evaluate_soft = .false.
1082 CASE DEFAULT
1083 CALL cp_abort(__location__, &
1084 "Unknown SKALA_NATIVE%GAPW_DENSITY_PARTITION value.")
1085 END SELECT
1086 END IF
1087 IF (atom_composite_reference) THEN
1088 evaluate_hard = .false.
1089 evaluate_soft = .false.
1090 END IF
1091
1092 !-------------------!
1093 ! hard atom density !
1094 !-------------------!
1095 CALL xc_dset_zero_all(deriv_set)
1096 IF (.NOT. evaluate_hard) THEN
1097 exc_h = 0.0_dp
1098 IF (.NOT. energy_only) THEN
1099 vxc_h = 0.0_dp
1100 IF (ASSOCIATED(vxg_h)) vxg_h = 0.0_dp
1101 IF (ASSOCIATED(vtau_h)) vtau_h = 0.0_dp
1102 END IF
1103 ELSE IF (skala_atom_grid) THEN
1105 my_xc_section, grid_atom, para_env, particle_set(iatom)%r, &
1106 rho_h, drho_h, tau_h, weight_h, lsd, nspins, na, nr, &
1107 exc_h, vxc_h, vxg_h, vtau_h, energy_only=energy_only, &
1108 atom_force=skala_atom_force_h, atom_virial=skala_atom_virial_h)
1109 ELSE
1110 CALL vxc_of_r_new(xc_fun_section, rho_set_h, deriv_set, 1, needs, weight_h, &
1111 lsd, na, nr, exc_h, vxc_h, vxg_h, vtau_h, energy_only=energy_only, &
1112 adiabatic_rescale_factor=my_adiabatic_rescale_factor)
1113 END IF
1114 rho_atom%exc_h = rho_atom%exc_h + exc_h
1115
1116 !-------------------!
1117 ! soft atom density !
1118 !-------------------!
1119 CALL xc_dset_zero_all(deriv_set)
1120 IF (.NOT. evaluate_soft) THEN
1121 exc_s = 0.0_dp
1122 IF (.NOT. energy_only) THEN
1123 vxc_s = 0.0_dp
1124 IF (ASSOCIATED(vxg_s)) vxg_s = 0.0_dp
1125 IF (ASSOCIATED(vtau_s)) vtau_s = 0.0_dp
1126 END IF
1127 ELSE IF (skala_atom_grid) THEN
1129 my_xc_section, grid_atom, para_env, particle_set(iatom)%r, &
1130 rho_s, drho_s, tau_s, weight_s, lsd, nspins, na, nr, &
1131 exc_s, vxc_s, vxg_s, vtau_s, energy_only=energy_only, &
1132 atom_force=skala_atom_force_s, atom_virial=skala_atom_virial_s)
1133 ELSE
1134 CALL vxc_of_r_new(xc_fun_section, rho_set_s, deriv_set, 1, needs, weight_s, &
1135 lsd, na, nr, exc_s, vxc_s, vxg_s, vtau_s, energy_only=energy_only, &
1136 adiabatic_rescale_factor=my_adiabatic_rescale_factor)
1137 END IF
1138 rho_atom%exc_s = rho_atom%exc_s + exc_s
1139
1140 ! Add contributions to the exc energy
1141
1142 exc1 = exc1 + rho_atom%exc_h - rho_atom%exc_s
1143 IF (skala_atom_grid .AND. my_calculate_forces .AND. ASSOCIATED(force)) THEN
1144 force(ikind)%rho_elec(:, iat) = force(ikind)%rho_elec(:, iat) + &
1145 skala_atom_force_h - skala_atom_force_s
1146 END IF
1147 IF (skala_atom_grid .AND. use_virial) THEN
1148 skala_atom_virial = skala_atom_virial_h - skala_atom_virial_s
1149 DO idir = 1, 3
1150 DO jdir = 1, 3
1151 virial%pv_gapw(idir, jdir) = virial%pv_gapw(idir, jdir) + &
1152 skala_atom_virial(idir, jdir)
1153 virial%pv_virial(idir, jdir) = virial%pv_virial(idir, jdir) + &
1154 skala_atom_virial(idir, jdir)
1155 END DO
1156 END DO
1157 END IF
1158
1159 ! Integration to get the matrix elements relative to the vxc_atom
1160 ! here the products with the primitives is done: gaVxcgb
1161 ! internal transformation to get the integral in cartesian Gaussians
1162
1163 IF (.NOT. energy_only) THEN
1164 NULLIFY (int_hh, int_ss)
1165 CALL get_rho_atom(rho_atom=rho_atom, ga_vlocal_gb_h=int_hh, ga_vlocal_gb_s=int_ss)
1166 IF (gradient_f) THEN
1167 CALL gavxcgb_gc(vxc_h, vxc_s, vxg_h, vxg_s, int_hh, int_ss, &
1168 grid_atom, basis_1c, harmonics, nspins)
1169 ELSE
1170 CALL gavxcgb_nogc(vxc_h, vxc_s, int_hh, int_ss, &
1171 grid_atom, basis_1c, harmonics, nspins)
1172 END IF
1173 IF (tau_f) THEN
1174 CALL dgavtaudgb(vtau_h, vtau_s, int_hh, int_ss, tau_basis_cache, nspins)
1175 END IF
1176 END IF ! energy_only
1177 NULLIFY (r_h, r_s, dr_h, dr_s)
1178 END DO ! iat
1179
1180 IF (one_center_kind) THEN
1181 IF (tau_f) CALL release_tau_basis_cache(tau_basis_cache)
1182
1183 CALL xc_dset_release(deriv_set)
1184 CALL xc_rho_set_release(rho_set_h)
1185 CALL xc_rho_set_release(rho_set_s)
1186 END IF
1187 END DO ! ikind
1188
1189 IF (atom_composite_active) THEN
1190 ALLOCATE (composite_cross_density(composite_nflat, 2), &
1191 composite_cross_grad(composite_nflat, 3, 2), &
1192 composite_cross_kin(composite_nflat, 2))
1193 composite_cross_density = 0.0_dp
1194 composite_cross_grad = 0.0_dp
1195 composite_cross_kin = 0.0_dp
1196 composite_cross_cutoff_max = 0.0_dp
1197 IF (.NOT. direct_valence_atom_composite) THEN
1198 DO ikind = 1, SIZE(atomic_kind_set)
1199 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
1200 NULLIFY (gth_potential, sgp_potential)
1201 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
1202 gth_potential=gth_potential, harmonics=harmonics, &
1203 grid_atom=grid_atom, sgp_potential=sgp_potential, &
1204 zatom=zatom, zeff=zeff)
1205 CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
1206 IF (.NOT. native_skala_uses_one_center_kind( &
1207 paw_atom, gapw_representation, &
1208 ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential), &
1209 zeff, zatom)) cycle
1210
1212 para_env, my_rho_atom_set, my_kind_set(ikind), atom_list, natom, nspins)
1213 nr = grid_atom%nr
1214 na = grid_atom%ng_sphere
1215 CALL create_tau_basis_cache(tau_basis_cache, grid_atom, basis_1c, harmonics)
1216 CALL reallocate(rho_h, 1, na, 1, nr, 1, nspins)
1217 CALL reallocate(rho_s, 1, na, 1, nr, 1, nspins)
1218 CALL reallocate(drho_h, 1, 4, 1, na, 1, nr, 1, nspins)
1219 CALL reallocate(drho_s, 1, 4, 1, na, 1, nr, 1, nspins)
1220 CALL reallocate(tau_h, 1, na, 1, nr, 1, nspins)
1221 CALL reallocate(tau_s, 1, na, 1, nr, 1, nspins)
1222
1223 ! The one-center density matrices are already globally reduced. Distribute the
1224 ! overlap work by target atom so that every rank constructs only its model rows.
1225 DO iat = 1, natom
1226 source_atom = atom_list(iat)
1227 rho_atom => my_rho_atom_set(source_atom)
1228 NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
1229 CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, rho_rad_s=r_s, &
1230 drho_rad_h=dr_h, drho_rad_s=dr_s, &
1231 rho_rad_h_d=r_h_d, rho_rad_s_d=r_s_d)
1232 rho_h = 0.0_dp
1233 rho_s = 0.0_dp
1234 drho_h = 0.0_dp
1235 drho_s = 0.0_dp
1236 CALL calc_tau_atom(tau_h, tau_s, rho_atom, tau_basis_cache, nspins)
1237 DO ir = 1, nr
1238 CALL calc_rho_angular(grid_atom, harmonics, nspins, .true., &
1239 ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, &
1240 r_h_d, r_s_d, drho_h, drho_s)
1241 END DO
1242
1243 cross_cutoff = gapw_atom_grid_support_radius( &
1244 grid_atom, rho_h, rho_s, drho_h, drho_s, tau_h, tau_s)
1245 IF (cross_cutoff <= 0.0_dp) cycle
1246 composite_cross_cutoff_max = max(composite_cross_cutoff_max, cross_cutoff)
1247 image_shell = 0
1248 DO idir = 1, 3
1249 IF (cell%perd(idir) == 1) THEN
1250 image_shell(idir) = ceiling( &
1251 cross_cutoff*sqrt(sum(cell%h_inv(idir, :)**2))) + 1
1252 END IF
1253 END DO
1254
1255!$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
1256!$OMP PRIVATE(image_lower, image_upper, composite_row, cross_density, cross_density_spatial, &
1257!$OMP cross_displacement, cross_distance, cross_grad, cross_grad_spatial, cross_kin, &
1258!$OMP cross_kin_spatial, damping, damping_derivative, fractional, idir, image_i1, &
1259!$OMP image_i2, image_i3, jdir, image_shift, image_translation, target_atom) &
1260!$OMP SHARED(cell, composite_atomic_grid_weights, composite_base_grid_weights, &
1261!$OMP composite_cross_density, composite_cross_grad, composite_cross_kin, &
1262!$OMP composite_grid_atom, composite_grid_coords, composite_nflat, cross_cutoff, &
1263!$OMP drho_h, drho_s, grid_atom, harmonics, image_shell, lsd, nspins, &
1264!$OMP particle_set, rho_h, rho_s, source_atom, tau_h, tau_s, &
1265!$OMP use_atom_composite_density, use_atom_composite_gradient, use_atom_composite_tau)
1266 DO composite_row = 1, composite_nflat
1267 target_atom = composite_grid_atom(composite_row)
1268 fractional = 0.0_dp
1269 DO idir = 1, 3
1270 DO jdir = 1, 3
1271 fractional(idir) = fractional(idir) + cell%h_inv(idir, jdir)* &
1272 (composite_grid_coords(jdir, composite_row) - &
1273 particle_set(source_atom)%r(jdir))
1274 END DO
1275 END DO
1276 CALL atom_grid_image_bounds(cell, fractional, cross_cutoff, image_shell, &
1277 image_lower, image_upper)
1278 DO image_i3 = image_lower(3), image_upper(3)
1279 DO image_i2 = image_lower(2), image_upper(2)
1280 DO image_i1 = image_lower(1), image_upper(1)
1281 image_shift = [image_i1, image_i2, image_i3]
1282 IF (target_atom == source_atom .AND. &
1283 all(image_shift == 0)) cycle
1284 image_translation = matmul( &
1285 cell%hmat, real(image_shift, dp))
1286 cross_displacement = composite_grid_coords(:, composite_row) - &
1287 particle_set(source_atom)%r - &
1288 image_translation
1289 cross_distance = sqrt(sum(cross_displacement**2))
1291 cross_distance, composite_base_grid_weights(composite_row), &
1292 damping, damping_derivative)
1293 composite_atomic_grid_weights(composite_row) = &
1294 composite_atomic_grid_weights(composite_row)*damping
1295 IF (cross_distance > cross_cutoff) cycle
1297 grid_atom, harmonics, cross_displacement, cross_cutoff, nspins, &
1298 rho_h, rho_s, drho_h, drho_s, tau_h, tau_s, &
1299 cross_density, cross_grad, cross_kin, cross_density_spatial, &
1300 cross_grad_spatial, cross_kin_spatial, calculate_spatial=.false.)
1301 IF (lsd) THEN
1302 IF (use_atom_composite_density) THEN
1303 composite_cross_density(composite_row, 1:2) = &
1304 composite_cross_density(composite_row, 1:2) + &
1305 cross_density(1:2)
1306 END IF
1307 IF (use_atom_composite_gradient) THEN
1308 composite_cross_grad(composite_row, :, 1:2) = &
1309 composite_cross_grad(composite_row, :, 1:2) + &
1310 cross_grad(:, 1:2)
1311 END IF
1312 IF (use_atom_composite_tau) THEN
1313 composite_cross_kin(composite_row, 1:2) = &
1314 composite_cross_kin(composite_row, 1:2) + cross_kin(1:2)
1315 END IF
1316 ELSE
1317 IF (use_atom_composite_density) THEN
1318 composite_cross_density(composite_row, :) = &
1319 composite_cross_density(composite_row, :) + &
1320 0.5_dp*cross_density(1)
1321 END IF
1322 IF (use_atom_composite_gradient) THEN
1323 DO idir = 1, 3
1324 composite_cross_grad(composite_row, idir, :) = &
1325 composite_cross_grad(composite_row, idir, :) + &
1326 0.5_dp*cross_grad(idir, 1)
1327 END DO
1328 END IF
1329 IF (use_atom_composite_tau) THEN
1330 composite_cross_kin(composite_row, :) = &
1331 composite_cross_kin(composite_row, :) + &
1332 0.5_dp*cross_kin(1)
1333 END IF
1334 END IF
1335 END DO
1336 END DO
1337 END DO
1338 END DO
1339!$OMP END PARALLEL DO
1340 END DO
1341
1342 CALL release_tau_basis_cache(tau_basis_cache)
1343 END DO
1344 END IF
1345
1346 IF (native_grid_diagnostics) THEN
1347 composite_cross_density_max = maxval(abs(composite_cross_density))
1348 composite_cross_grad_max = maxval(abs(composite_cross_grad))
1349 composite_cross_kin_max = maxval(abs(composite_cross_kin))
1350 CALL para_env%max(composite_cross_cutoff_max)
1351 CALL para_env%max(composite_cross_density_max)
1352 CALL para_env%max(composite_cross_grad_max)
1353 CALL para_env%max(composite_cross_kin_max)
1355 IF (iw > 0) THEN
1356 WRITE (unit=iw, fmt="(T2,A,4(1X,ES20.12))") &
1357 "SKALA_GPW| Atom-composite cross support/maxima", &
1358 composite_cross_cutoff_max, composite_cross_density_max, &
1359 composite_cross_grad_max, composite_cross_kin_max
1360 END IF
1361 END IF
1362 composite_density(:, :) = composite_density(:, :) + composite_cross_density(:, :)
1363 composite_grad(:, :, :) = composite_grad(:, :, :) + composite_cross_grad(:, :, :)
1364 composite_kin(:, :) = composite_kin(:, :) + composite_cross_kin(:, :)
1365 DEALLOCATE (composite_cross_density, composite_cross_grad, composite_cross_kin)
1366
1367 cpassert(all(composite_grid_weights >= 0.0_dp))
1368 atom_composite_nelec = sum(composite_grid_weights* &
1369 (composite_density(:, 1) + composite_density(:, 2)))
1370 CALL para_env%sum(atom_composite_nelec)
1371 IF (atom_composite_reference .AND. my_calculate_forces) THEN
1373 my_xc_section, para_env, composite_density, composite_grad, composite_kin, &
1374 composite_grid_coords, composite_grid_weights, composite_atomic_grid_weights, &
1375 composite_local_grid_sizes, composite_local_atom_coords, atom_composite_exc, &
1376 composite_density_grad, composite_grad_grad, composite_kin_grad, &
1377 composite_grid_coord_grad, composite_grid_weight_grad, &
1378 composite_atomic_grid_weight_grad, composite_atom_coord_grad)
1379 ALLOCATE (composite_cross_force(3, SIZE(particle_set)), &
1380 composite_explicit_force(3, SIZE(particle_set)), &
1381 composite_grid_coord_force(3, SIZE(particle_set)), &
1382 composite_model_atom_force(3, SIZE(particle_set)), &
1383 composite_moving_smooth_force(3, SIZE(particle_set)), &
1384 composite_nlcc_center_force(3, SIZE(particle_set)), &
1385 composite_nlcc_target_force(3, SIZE(particle_set)), &
1386 composite_partition_force(3, SIZE(particle_set)), &
1387 composite_partition_included(SIZE(particle_set)), &
1388 composite_partition_datom(3, SIZE(particle_set), SIZE(particle_set)), &
1389 composite_partition_dstrain(3, 3, SIZE(particle_set)))
1390 composite_cross_force = 0.0_dp
1391 composite_cross_image_virial = 0.0_dp
1392 composite_model_atom_force = 0.0_dp
1393 composite_grid_coord_force = 0.0_dp
1394 composite_moving_smooth_force = 0.0_dp
1395 composite_nlcc_center_force = 0.0_dp
1396 composite_nlcc_target_force = 0.0_dp
1397 composite_partition_force = 0.0_dp
1398 composite_explicit_virial = 0.0_dp
1399 composite_feature_virial = 0.0_dp
1400 composite_interpolation_virial = 0.0_dp
1401 composite_partition_strain_virial = 0.0_dp
1402!$OMP PARALLEL DO IF (image_partition_atom_composite) SCHEDULE(STATIC) DEFAULT(NONE) &
1403!$OMP PRIVATE(composite_local_atom, composite_row, composite_smooth_gradient_value, &
1404!$OMP iatom, idir, ispin, jdir) &
1405!$OMP SHARED(cell, composite_atom_end, composite_atom_start, composite_grad_grad, &
1406!$OMP composite_grid_coords, composite_local_atoms, composite_local_natom, &
1407!$OMP composite_smooth_drho, composite_smooth_drhoa, composite_smooth_drhob, &
1408!$OMP image_partition_atom_composite, lsd, smooth_rho_r) &
1409!$OMP REDUCTION(+:composite_feature_virial)
1410 DO composite_local_atom = 1, composite_local_natom
1411 iatom = composite_local_atoms(composite_local_atom)
1412 DO composite_row = composite_atom_start(iatom), composite_atom_end(iatom)
1413 IF (lsd) THEN
1414 DO jdir = 1, 3
1415 composite_smooth_gradient_value(jdir, 1) = &
1416 interpolate_native_grid( &
1417 composite_smooth_drhoa(jdir)%array, smooth_rho_r(1)%pw_grid, &
1418 cell, composite_grid_coords(:, composite_row), &
1419 image_partition_atom_composite)
1420 composite_smooth_gradient_value(jdir, 2) = &
1421 interpolate_native_grid( &
1422 composite_smooth_drhob(jdir)%array, smooth_rho_r(1)%pw_grid, &
1423 cell, composite_grid_coords(:, composite_row), &
1424 image_partition_atom_composite)
1425 END DO
1426 ELSE
1427 DO jdir = 1, 3
1428 composite_smooth_gradient_value(jdir, :) = 0.5_dp* &
1429 interpolate_native_grid( &
1430 composite_smooth_drho(jdir)%array, smooth_rho_r(1)%pw_grid, &
1431 cell, composite_grid_coords(:, composite_row), &
1432 image_partition_atom_composite)
1433 END DO
1434 END IF
1435 DO ispin = 1, 2
1436 DO idir = 1, 3
1437 DO jdir = 1, 3
1438 composite_feature_virial(jdir, idir) = &
1439 composite_feature_virial(jdir, idir) - &
1440 composite_grad_grad(composite_row, idir, ispin)* &
1441 composite_smooth_gradient_value(jdir, ispin)
1442 END DO
1443 END DO
1444 END DO
1445 END DO
1446 END DO
1447!$OMP END PARALLEL DO
1448!$OMP PARALLEL IF (image_partition_atom_composite) DEFAULT(NONE) &
1449!$OMP PRIVATE(composite_local_atom, composite_row, gth_potential, iatom, idir, ispin, jdir, &
1450!$OMP nlcc_density, nlcc_gradient, nlcc_hessian, nlcc_spatial_derivative, &
1451!$OMP nlcc_spin_factor, partition_adjoint, composite_nlcc_center_force_local, &
1452!$OMP composite_partition_force_local, local_partition_datom, local_partition_dstrain, &
1453!$OMP local_partition_weight, sgp_potential, source_atom, &
1454!$OMP composite_partition_image_coords, composite_partition_target_image, &
1455!$OMP spatial_derivative, target_atom, target_partition_adjoint) &
1456!$OMP REDUCTION(+:composite_interpolation_virial, composite_partition_strain_virial) &
1457!$OMP SHARED(cell, composite_atom_coord_grad, composite_atom_coords, composite_atom_end, &
1458!$OMP composite_atom_kind, composite_atom_start, composite_atomic_grid_weight_grad, &
1459!$OMP composite_atomic_grid_weights, composite_base_grid_weights, composite_density_grad, &
1460!$OMP composite_grad_grad, composite_grid_coord_force, composite_grid_coord_grad, &
1461!$OMP composite_grid_coords, composite_grid_weight_grad, composite_image_periodicity, &
1462!$OMP composite_kin_grad, composite_local_atoms, composite_local_natom, &
1463!$OMP composite_model_atom_force, composite_moving_smooth_force, &
1464!$OMP composite_nlcc_center_force, composite_nlcc_target_force, composite_partition_datom, &
1465!$OMP composite_partition_dstrain, composite_partition_force, composite_partition_included, &
1466!$OMP composite_partition_weights, composite_smooth_drho, composite_smooth_drhoa, &
1467!$OMP composite_smooth_drhob, composite_smooth_rho, composite_smooth_rhoa, &
1468!$OMP composite_smooth_rhob, composite_smooth_tau, composite_smooth_tau_a, &
1469!$OMP composite_smooth_tau_b, image_partition_atom_composite, lsd, my_kind_set, nlcc, &
1470!$OMP particle_set, smooth_rho_r)
1471 ALLOCATE (composite_nlcc_center_force_local(3, SIZE(particle_set)), &
1472 composite_partition_force_local(3, SIZE(particle_set)), &
1473 local_partition_datom(3, SIZE(particle_set)))
1474 composite_nlcc_center_force_local = 0.0_dp
1475 composite_partition_force_local = 0.0_dp
1476!$OMP DO SCHEDULE(DYNAMIC)
1477 DO composite_local_atom = 1, composite_local_natom
1478 iatom = composite_local_atoms(composite_local_atom)
1479 composite_model_atom_force(:, iatom) = &
1480 composite_atom_coord_grad(:, composite_local_atom)
1481 IF (image_partition_atom_composite) THEN
1482 CALL build_periodic_atom_image_layout( &
1483 composite_atom_coords, cell, iatom, composite_image_periodicity, &
1484 composite_partition_image_coords, composite_partition_target_image)
1485 END IF
1486 DO composite_row = composite_atom_start(iatom), composite_atom_end(iatom)
1487 composite_grid_coord_force(:, iatom) = &
1488 composite_grid_coord_force(:, iatom) + &
1489 composite_grid_coord_grad(:, composite_row)
1490 IF (lsd) THEN
1491 spatial_derivative = &
1492 composite_density_grad(composite_row, 1)* &
1493 interpolate_native_grid_gradient( &
1494 composite_smooth_rhoa, smooth_rho_r(1)%pw_grid, cell, &
1495 composite_grid_coords(:, composite_row), &
1496 image_partition_atom_composite) + &
1497 composite_density_grad(composite_row, 2)* &
1498 interpolate_native_grid_gradient( &
1499 composite_smooth_rhob, smooth_rho_r(1)%pw_grid, cell, &
1500 composite_grid_coords(:, composite_row), &
1501 image_partition_atom_composite) + &
1502 composite_kin_grad(composite_row, 1)* &
1503 interpolate_native_grid_gradient( &
1504 composite_smooth_tau_a, smooth_rho_r(1)%pw_grid, cell, &
1505 composite_grid_coords(:, composite_row), &
1506 image_partition_atom_composite) + &
1507 composite_kin_grad(composite_row, 2)* &
1508 interpolate_native_grid_gradient( &
1509 composite_smooth_tau_b, smooth_rho_r(1)%pw_grid, cell, &
1510 composite_grid_coords(:, composite_row), &
1511 image_partition_atom_composite)
1512 DO idir = 1, 3
1513 spatial_derivative = spatial_derivative + &
1514 composite_grad_grad(composite_row, idir, 1)* &
1515 interpolate_native_grid_gradient( &
1516 composite_smooth_drhoa(idir)%array, &
1517 smooth_rho_r(1)%pw_grid, cell, &
1518 composite_grid_coords(:, composite_row), &
1519 image_partition_atom_composite) + &
1520 composite_grad_grad(composite_row, idir, 2)* &
1521 interpolate_native_grid_gradient( &
1522 composite_smooth_drhob(idir)%array, &
1523 smooth_rho_r(1)%pw_grid, cell, &
1524 composite_grid_coords(:, composite_row), &
1525 image_partition_atom_composite)
1526 END DO
1527 ELSE
1528 spatial_derivative = 0.5_dp*sum( &
1529 composite_density_grad(composite_row, :))* &
1530 interpolate_native_grid_gradient( &
1531 composite_smooth_rho, smooth_rho_r(1)%pw_grid, cell, &
1532 composite_grid_coords(:, composite_row), &
1533 image_partition_atom_composite) + &
1534 0.5_dp*sum(composite_kin_grad(composite_row, :))* &
1535 interpolate_native_grid_gradient( &
1536 composite_smooth_tau, smooth_rho_r(1)%pw_grid, cell, &
1537 composite_grid_coords(:, composite_row), &
1538 image_partition_atom_composite)
1539 DO idir = 1, 3
1540 spatial_derivative = spatial_derivative + 0.5_dp*sum( &
1541 composite_grad_grad(composite_row, idir, :))* &
1542 interpolate_native_grid_gradient( &
1543 composite_smooth_drho(idir)%array, &
1544 smooth_rho_r(1)%pw_grid, &
1545 cell, composite_grid_coords(:, composite_row), &
1546 image_partition_atom_composite)
1547 END DO
1548 END IF
1549 DO idir = 1, 3
1550 DO jdir = 1, 3
1551 composite_interpolation_virial(idir, jdir) = &
1552 composite_interpolation_virial(idir, jdir) + &
1553 spatial_derivative(idir)*( &
1554 composite_grid_coords(jdir, composite_row) - &
1555 particle_set(iatom)%r(jdir))
1556 END DO
1557 END DO
1558 composite_moving_smooth_force(:, iatom) = &
1559 composite_moving_smooth_force(:, iatom) + spatial_derivative
1560 IF (nlcc) THEN
1561 nlcc_spin_factor = merge(1.0_dp, 0.5_dp, lsd)
1562 DO source_atom = 1, SIZE(particle_set)
1563 NULLIFY (gth_potential, sgp_potential)
1564 CALL get_qs_kind(my_kind_set(composite_atom_kind(source_atom)), &
1565 gth_potential=gth_potential, &
1566 sgp_potential=sgp_potential)
1568 composite_grid_coords(:, composite_row), &
1569 particle_set(source_atom)%r, gth_potential, sgp_potential, &
1570 nlcc_density, nlcc_gradient, nlcc_hessian)
1571 nlcc_spatial_derivative = 0.0_dp
1572 DO ispin = 1, 2
1573 nlcc_spatial_derivative = nlcc_spatial_derivative + &
1574 nlcc_spin_factor*composite_density_grad(composite_row, ispin)* &
1575 nlcc_gradient
1576 DO idir = 1, 3
1577 DO jdir = 1, 3
1578 nlcc_spatial_derivative(jdir) = &
1579 nlcc_spatial_derivative(jdir) + nlcc_spin_factor* &
1580 composite_grad_grad(composite_row, idir, ispin)* &
1581 nlcc_hessian(idir, jdir)
1582 END DO
1583 END DO
1584 END DO
1585 composite_nlcc_target_force(:, iatom) = &
1586 composite_nlcc_target_force(:, iatom) + nlcc_spatial_derivative
1587 composite_nlcc_center_force_local(:, source_atom) = &
1588 composite_nlcc_center_force_local(:, source_atom) - nlcc_spatial_derivative
1589 END DO
1590 END IF
1591 IF (image_partition_atom_composite) THEN
1592 CALL periodic_atom_image_partition_from_layout( &
1593 composite_grid_coords(:, composite_row), composite_partition_image_coords, &
1594 composite_partition_target_image, local_partition_weight, local_partition_datom, &
1595 local_partition_dstrain)
1596 target_partition_adjoint = composite_base_grid_weights(composite_row)* &
1597 composite_grid_weight_grad(composite_row)
1598 DO target_atom = 1, SIZE(particle_set)
1599 composite_partition_force_local(:, target_atom) = &
1600 composite_partition_force_local(:, target_atom) + &
1601 target_partition_adjoint*local_partition_datom(:, target_atom)
1602 END DO
1603 composite_partition_force_local(:, iatom) = &
1604 composite_partition_force_local(:, iatom) - target_partition_adjoint* &
1605 sum(local_partition_datom, dim=2)
1606 composite_partition_strain_virial = &
1607 composite_partition_strain_virial - target_partition_adjoint* &
1608 local_partition_dstrain
1609 ELSE
1611 composite_grid_coords(:, composite_row), composite_atom_coords, cell, &
1612 composite_partition_weights, composite_partition_included, &
1613 composite_partition_datom, composite_partition_dstrain)
1614 partition_adjoint = composite_grid_weight_grad(composite_row)* &
1615 composite_base_grid_weights(composite_row)
1616 DO target_atom = 1, SIZE(particle_set)
1617 composite_partition_force_local(:, target_atom) = &
1618 composite_partition_force_local(:, target_atom) + &
1619 partition_adjoint* &
1620 composite_partition_datom(:, target_atom, iatom)
1621 END DO
1622 composite_partition_force_local(:, iatom) = &
1623 composite_partition_force_local(:, iatom) - partition_adjoint* &
1624 sum(composite_partition_datom(:, :, iatom), dim=2)
1625 END IF
1626 END DO
1627 END DO
1628!$OMP END DO
1629!$OMP CRITICAL(skala_atom_composite_force_reduction)
1630 composite_nlcc_center_force(:, :) = composite_nlcc_center_force(:, :) + &
1631 composite_nlcc_center_force_local
1632 composite_partition_force(:, :) = composite_partition_force(:, :) + &
1633 composite_partition_force_local
1634!$OMP END CRITICAL(skala_atom_composite_force_reduction)
1635 DEALLOCATE (composite_nlcc_center_force_local, composite_partition_force_local, &
1636 local_partition_datom)
1637!$OMP END PARALLEL
1638 ! The target atom grids can overlap augmentation regions of other atoms
1639 ! and periodic images. Differentiate the same discrete interpolation used
1640 ! in the forward composite fields, including the explicit image strain.
1641 IF (.NOT. direct_valence_atom_composite) THEN
1642 DO ikind = 1, SIZE(atomic_kind_set)
1643 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
1644 NULLIFY (gth_potential, sgp_potential)
1645 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
1646 gth_potential=gth_potential, harmonics=harmonics, &
1647 grid_atom=grid_atom, sgp_potential=sgp_potential, &
1648 zatom=zatom, zeff=zeff)
1649 CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
1650 IF (.NOT. native_skala_uses_one_center_kind( &
1651 paw_atom, gapw_representation, &
1652 ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential), &
1653 zeff, zatom)) cycle
1654
1655 nr = grid_atom%nr
1656 na = grid_atom%ng_sphere
1657 CALL create_tau_basis_cache(tau_basis_cache, grid_atom, basis_1c, harmonics)
1658 CALL reallocate(rho_h, 1, na, 1, nr, 1, nspins)
1659 CALL reallocate(rho_s, 1, na, 1, nr, 1, nspins)
1660 CALL reallocate(drho_h, 1, 4, 1, na, 1, nr, 1, nspins)
1661 CALL reallocate(drho_s, 1, 4, 1, na, 1, nr, 1, nspins)
1662 CALL reallocate(tau_h, 1, na, 1, nr, 1, nspins)
1663 CALL reallocate(tau_s, 1, na, 1, nr, 1, nspins)
1664
1665 DO iat = 1, natom
1666 source_atom = atom_list(iat)
1667 rho_atom => my_rho_atom_set(source_atom)
1668 NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
1669 CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, rho_rad_s=r_s, &
1670 drho_rad_h=dr_h, drho_rad_s=dr_s, &
1671 rho_rad_h_d=r_h_d, rho_rad_s_d=r_s_d)
1672 rho_h = 0.0_dp
1673 rho_s = 0.0_dp
1674 drho_h = 0.0_dp
1675 drho_s = 0.0_dp
1676 CALL calc_tau_atom(tau_h, tau_s, rho_atom, tau_basis_cache, nspins)
1677 DO ir = 1, nr
1678 CALL calc_rho_angular(grid_atom, harmonics, nspins, .true., &
1679 ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, &
1680 r_h_d, r_s_d, drho_h, drho_s)
1681 END DO
1682
1683 cross_cutoff = gapw_atom_grid_support_radius( &
1684 grid_atom, rho_h, rho_s, drho_h, drho_s, tau_h, tau_s)
1685 IF (cross_cutoff <= 0.0_dp) cycle
1686 image_shell = 0
1687 DO idir = 1, 3
1688 IF (cell%perd(idir) == 1) THEN
1689 image_shell(idir) = ceiling( &
1690 cross_cutoff*sqrt(sum(cell%h_inv(idir, :)**2))) + 1
1691 END IF
1692 END DO
1693
1694!$OMP PARALLEL DEFAULT(NONE) &
1695!$OMP PRIVATE(image_lower, image_upper, composite_cross_force_local, composite_cross_image_virial_local, &
1696!$OMP composite_row, cross_density, cross_density_adjoint, cross_density_spatial, &
1697!$OMP cross_displacement, cross_distance, cross_grad, cross_grad_adjoint, &
1698!$OMP cross_grad_spatial, cross_kin, cross_kin_adjoint, cross_kin_spatial, &
1699!$OMP cross_spatial_derivative, damping, damping_derivative, damping_spatial_derivative, &
1700!$OMP fractional, idir, image_i1, image_i2, image_i3, image_shift, image_translation, &
1701!$OMP ispin, jdir, target_atom) &
1702!$OMP SHARED(cell, composite_atomic_grid_weight_grad, composite_atomic_grid_weights, &
1703!$OMP composite_base_grid_weights, composite_cross_force, composite_cross_image_virial, &
1704!$OMP composite_density_grad, composite_grad_grad, composite_grid_atom, &
1705!$OMP composite_grid_coords, composite_kin_grad, composite_nflat, cross_cutoff, &
1706!$OMP drho_h, drho_s, grid_atom, harmonics, image_shell, lsd, nspins, &
1707!$OMP particle_set, rho_h, rho_s, source_atom, tau_h, tau_s, &
1708!$OMP use_atom_composite_density, use_atom_composite_gradient, use_atom_composite_tau)
1709 ALLOCATE (composite_cross_force_local(3, SIZE(particle_set)))
1710 composite_cross_force_local = 0.0_dp
1711 composite_cross_image_virial_local = 0.0_dp
1712!$OMP DO SCHEDULE(STATIC)
1713 DO composite_row = 1, composite_nflat
1714 target_atom = composite_grid_atom(composite_row)
1715 IF (lsd) THEN
1716 cross_density_adjoint = 0.0_dp
1717 cross_grad_adjoint = 0.0_dp
1718 cross_kin_adjoint = 0.0_dp
1719 IF (use_atom_composite_density) THEN
1720 cross_density_adjoint(1:2) = &
1721 composite_density_grad(composite_row, 1:2)
1722 END IF
1723 IF (use_atom_composite_gradient) THEN
1724 cross_grad_adjoint(:, 1:2) = &
1725 composite_grad_grad(composite_row, :, 1:2)
1726 END IF
1727 IF (use_atom_composite_tau) THEN
1728 cross_kin_adjoint(1:2) = &
1729 composite_kin_grad(composite_row, 1:2)
1730 END IF
1731 ELSE
1732 cross_density_adjoint = 0.0_dp
1733 cross_grad_adjoint = 0.0_dp
1734 cross_kin_adjoint = 0.0_dp
1735 IF (use_atom_composite_density) THEN
1736 cross_density_adjoint(1) = 0.5_dp* &
1737 sum(composite_density_grad(composite_row, :))
1738 END IF
1739 IF (use_atom_composite_gradient) THEN
1740 DO idir = 1, 3
1741 cross_grad_adjoint(idir, 1) = 0.5_dp* &
1742 sum(composite_grad_grad(composite_row, idir, :))
1743 END DO
1744 END IF
1745 IF (use_atom_composite_tau) THEN
1746 cross_kin_adjoint(1) = 0.5_dp* &
1747 sum(composite_kin_grad(composite_row, :))
1748 END IF
1749 END IF
1750
1751 fractional = 0.0_dp
1752 DO idir = 1, 3
1753 DO jdir = 1, 3
1754 fractional(idir) = fractional(idir) + cell%h_inv(idir, jdir)* &
1755 (composite_grid_coords(jdir, composite_row) - &
1756 particle_set(source_atom)%r(jdir))
1757 END DO
1758 END DO
1759 CALL atom_grid_image_bounds(cell, fractional, cross_cutoff, image_shell, &
1760 image_lower, image_upper)
1761 DO image_i3 = image_lower(3), image_upper(3)
1762 DO image_i2 = image_lower(2), image_upper(2)
1763 DO image_i1 = image_lower(1), image_upper(1)
1764 image_shift = [image_i1, image_i2, image_i3]
1765 IF (target_atom == source_atom .AND. &
1766 all(image_shift == 0)) cycle
1767 image_translation = matmul( &
1768 cell%hmat, real(image_shift, dp))
1769 cross_displacement = &
1770 composite_grid_coords(:, composite_row) - &
1771 particle_set(source_atom)%r - image_translation
1772 cross_distance = sqrt(sum(cross_displacement**2))
1773 ! Forces and virial from damping_of_grid_point_close_to_atom
1775 cross_distance, composite_base_grid_weights(composite_row), &
1776 damping, damping_derivative)
1777 IF (damping_derivative > 0.0_dp) THEN
1778 damping_spatial_derivative = &
1779 composite_atomic_grid_weight_grad(composite_row)* &
1780 composite_atomic_grid_weights(composite_row)/damping* &
1781 damping_derivative*cross_displacement/cross_distance
1782 composite_cross_force_local(:, target_atom) = &
1783 composite_cross_force_local(:, target_atom) + &
1784 damping_spatial_derivative
1785 composite_cross_force_local(:, source_atom) = &
1786 composite_cross_force_local(:, source_atom) - &
1787 damping_spatial_derivative
1788 DO idir = 1, 3
1789 composite_cross_image_virial_local(idir, :) = &
1790 composite_cross_image_virial_local(idir, :) + &
1791 damping_spatial_derivative(idir)*image_translation
1792 END DO
1793 END IF
1794 IF (cross_distance > cross_cutoff) cycle
1796 grid_atom, harmonics, cross_displacement, cross_cutoff, &
1797 nspins, rho_h, rho_s, drho_h, drho_s, tau_h, tau_s, &
1798 cross_density, cross_grad, cross_kin, cross_density_spatial, &
1799 cross_grad_spatial, cross_kin_spatial)
1800 cross_spatial_derivative = 0.0_dp
1801 DO ispin = 1, nspins
1802 DO idir = 1, 3
1803 cross_spatial_derivative(idir) = &
1804 cross_spatial_derivative(idir) + &
1805 cross_density_adjoint(ispin)* &
1806 cross_density_spatial(idir, ispin) + &
1807 cross_kin_adjoint(ispin)* &
1808 cross_kin_spatial(idir, ispin)
1809 DO jdir = 1, 3
1810 cross_spatial_derivative(idir) = &
1811 cross_spatial_derivative(idir) + &
1812 cross_grad_adjoint(jdir, ispin)* &
1813 cross_grad_spatial(jdir, idir, ispin)
1814 END DO
1815 END DO
1816 END DO
1817 composite_cross_force_local(:, target_atom) = &
1818 composite_cross_force_local(:, target_atom) + &
1819 cross_spatial_derivative
1820 composite_cross_force_local(:, source_atom) = &
1821 composite_cross_force_local(:, source_atom) - &
1822 cross_spatial_derivative
1823 DO idir = 1, 3
1824 DO jdir = 1, 3
1825 composite_cross_image_virial_local(idir, jdir) = &
1826 composite_cross_image_virial_local(idir, jdir) + &
1827 cross_spatial_derivative(idir)*image_translation(jdir)
1828 END DO
1829 END DO
1830 END DO
1831 END DO
1832 END DO
1833 END DO
1834!$OMP END DO
1835!$OMP CRITICAL(skala_atom_composite_cross_reduction)
1836 composite_cross_force(:, :) = &
1837 composite_cross_force(:, :) + composite_cross_force_local(:, :)
1838 composite_cross_image_virial = composite_cross_image_virial + &
1839 composite_cross_image_virial_local
1840!$OMP END CRITICAL(skala_atom_composite_cross_reduction)
1841 DEALLOCATE (composite_cross_force_local)
1842!$OMP END PARALLEL
1843 END DO
1844 CALL release_tau_basis_cache(tau_basis_cache)
1845 END DO
1846 END IF
1847
1848 composite_explicit_force(:, :) = composite_model_atom_force(:, :) + &
1849 composite_grid_coord_force(:, :) + &
1850 composite_moving_smooth_force(:, :) + &
1851 composite_cross_force(:, :) + &
1852 composite_nlcc_center_force(:, :) + &
1853 composite_nlcc_target_force(:, :) + &
1854 composite_partition_force(:, :)
1855 ! CP2K stores +dE/dR in the electronic force components, while the
1856 ! virial is -dE/dstrain. Model coordinates, atom-grid centers,
1857 ! NLCC centers, and partition centers move affinely with their atoms.
1858 ! The smooth-field interpolation force is excluded here: its affine
1859 ! response is already in the PW stress and its non-affine local-grid
1860 ! correction is composite_interpolation_virial.
1861 DO iatom = 1, SIZE(particle_set)
1862 DO idir = 1, 3
1863 DO jdir = 1, 3
1864 composite_explicit_virial(idir, jdir) = &
1865 composite_explicit_virial(idir, jdir) - ( &
1866 composite_model_atom_force(idir, iatom) + &
1867 composite_grid_coord_force(idir, iatom) + &
1868 composite_cross_force(idir, iatom) + &
1869 composite_nlcc_center_force(idir, iatom) + &
1870 composite_nlcc_target_force(idir, iatom) + &
1871 composite_partition_force(idir, iatom))*particle_set(iatom)%r(jdir)
1872 END DO
1873 END DO
1874 END DO
1875 composite_explicit_virial = composite_explicit_virial + &
1876 composite_partition_strain_virial + &
1877 composite_cross_image_virial
1878 IF (ASSOCIATED(force)) THEN
1879 DO iatom = 1, SIZE(particle_set)
1880 ikind = composite_atom_kind(iatom)
1881 iat = composite_atom_kind_index(iatom)
1882 cpassert(ikind > 0 .AND. iat > 0)
1883 force(ikind)%rho_elec(:, iat) = force(ikind)%rho_elec(:, iat) + &
1884 composite_explicit_force(:, iatom)
1885 END DO
1886 END IF
1887 IF (use_virial) THEN
1888 ! The local radial vectors of the atom-centered quadrature do not
1889 ! deform with the periodic cell. Correct the standard PW response,
1890 ! which follows fixed fractional coordinates, by the corresponding
1891 ! non-affine interpolation derivative.
1892 virial%pv_xc = composite_feature_virial - composite_interpolation_virial
1893 virial%pv_gapw = virial%pv_gapw + composite_explicit_virial
1894 virial%pv_virial = virial%pv_virial + composite_explicit_virial
1895 END IF
1896 IF (native_grid_diagnostics) THEN
1897 CALL para_env%sum(composite_cross_force)
1898 CALL para_env%sum(composite_cross_image_virial)
1899 CALL para_env%sum(composite_model_atom_force)
1900 CALL para_env%sum(composite_grid_coord_force)
1901 CALL para_env%sum(composite_moving_smooth_force)
1902 CALL para_env%sum(composite_nlcc_center_force)
1903 CALL para_env%sum(composite_nlcc_target_force)
1904 CALL para_env%sum(composite_partition_force)
1905 CALL para_env%sum(composite_explicit_force)
1906 CALL para_env%sum(composite_explicit_virial)
1907 CALL para_env%sum(composite_feature_virial)
1908 CALL para_env%sum(composite_interpolation_virial)
1910 IF (iw > 0) THEN
1911 DO iatom = 1, SIZE(particle_set)
1912 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1913 "SKALA_GPW| Atom-composite model-atom force", iatom, &
1914 composite_model_atom_force(:, iatom)
1915 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1916 "SKALA_GPW| Atom-composite grid-coordinate force", iatom, &
1917 composite_grid_coord_force(:, iatom)
1918 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1919 "SKALA_GPW| Atom-composite moving-smooth force", iatom, &
1920 composite_moving_smooth_force(:, iatom)
1921 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1922 "SKALA_GPW| Atom-composite cross-region force", iatom, &
1923 composite_cross_force(:, iatom)
1924 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1925 "SKALA_GPW| Atom-composite NLCC-center force", iatom, &
1926 composite_nlcc_center_force(:, iatom)
1927 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1928 "SKALA_GPW| Atom-composite NLCC-target force", iatom, &
1929 composite_nlcc_target_force(:, iatom)
1930 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1931 "SKALA_GPW| Atom-composite partition force", iatom, &
1932 composite_partition_force(:, iatom)
1933 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES20.12))") &
1934 "SKALA_GPW| Atom-composite explicit force", iatom, &
1935 composite_explicit_force(:, iatom)
1936 END DO
1937 WRITE (unit=iw, fmt="(T2,A)") &
1938 "SKALA_GPW| Atom-composite explicit virial"
1939 DO idir = 1, 3
1940 WRITE (unit=iw, fmt="(T2,A,1X,3ES20.10)") &
1941 "SKALA_GPW|", composite_explicit_virial(idir, :)
1942 END DO
1943 WRITE (unit=iw, fmt="(T2,A)") &
1944 "SKALA_GPW| Atom-composite cross-image virial"
1945 DO idir = 1, 3
1946 WRITE (unit=iw, fmt="(T2,A,1X,3ES20.10)") &
1947 "SKALA_GPW|", composite_cross_image_virial(idir, :)
1948 END DO
1949 WRITE (unit=iw, fmt="(T2,A)") &
1950 "SKALA_GPW| Atom-composite feature virial"
1951 DO idir = 1, 3
1952 WRITE (unit=iw, fmt="(T2,A,1X,3ES20.10)") &
1953 "SKALA_GPW|", composite_feature_virial(idir, :)
1954 END DO
1955 WRITE (unit=iw, fmt="(T2,A)") &
1956 "SKALA_GPW| Atom-composite interpolation virial"
1957 DO idir = 1, 3
1958 WRITE (unit=iw, fmt="(T2,A,1X,3ES20.10)") &
1959 "SKALA_GPW|", composite_interpolation_virial(idir, :)
1960 END DO
1961 END IF
1962 END IF
1963 DEALLOCATE (composite_atom_coord_grad, composite_atomic_grid_weight_grad, &
1964 composite_cross_force, &
1965 composite_explicit_force, composite_grid_coord_force, &
1966 composite_grid_coord_grad, composite_grid_weight_grad, &
1967 composite_model_atom_force, composite_moving_smooth_force, &
1968 composite_nlcc_center_force, &
1969 composite_nlcc_target_force, &
1970 composite_partition_datom, composite_partition_dstrain, &
1971 composite_partition_force, composite_partition_included)
1972 ELSE
1974 my_xc_section, para_env, composite_density, composite_grad, composite_kin, &
1975 composite_grid_coords, composite_grid_weights, composite_atomic_grid_weights, &
1976 composite_local_grid_sizes, composite_local_atom_coords, atom_composite_exc, &
1977 composite_density_grad, composite_grad_grad, composite_kin_grad)
1978 END IF
1979 composite_pw_nflat = product(smooth_rho_r(1)%pw_grid%npts)
1980 adjoint_nchannels = merge(2, 1, lsd)
1981 ALLOCATE (smooth_density_adjoint_storage(composite_pw_nflat, adjoint_nchannels), &
1982 smooth_grad_adjoint_storage(composite_pw_nflat, 3, adjoint_nchannels), &
1983 smooth_kin_adjoint_storage(composite_pw_nflat, adjoint_nchannels))
1984 smooth_density_adjoint_storage = 0.0_dp
1985 smooth_grad_adjoint_storage = 0.0_dp
1986 smooth_kin_adjoint_storage = 0.0_dp
1987 CALL build_native_grid_adjoint_bins( &
1988 smooth_rho_r(1)%pw_grid, cell, composite_grid_coords, &
1989 image_partition_atom_composite, adjoint_tile_count, adjoint_bin_offsets, &
1990 adjoint_bin_rows)
1991 adjoint_nbins = SIZE(adjoint_bin_offsets) - 1
1992!$OMP PARALLEL DO SCHEDULE(DYNAMIC) DEFAULT(NONE) &
1993!$OMP PRIVATE(adjoint_entry, composite_row, composite_smooth_density_adjoint_value, &
1994!$OMP composite_smooth_gradient_adjoint_value, composite_smooth_kin_adjoint_value, &
1995!$OMP adjoint_tile_lower, adjoint_tile_upper, interpolation_stencil) &
1996!$OMP SHARED(adjoint_bin_offsets, adjoint_bin_rows, adjoint_nbins, adjoint_nchannels, &
1997!$OMP adjoint_tile_count, cell, composite_density_grad, composite_grad_grad, &
1998!$OMP composite_grid_coords, composite_kin_grad, image_partition_atom_composite, lsd, &
1999!$OMP smooth_density_adjoint_storage, smooth_grad_adjoint_storage, &
2000!$OMP smooth_kin_adjoint_storage, smooth_rho_r, qs_env)
2001 DO adjoint_bin = 1, adjoint_nbins
2002 CALL native_grid_adjoint_tile_bounds( &
2003 smooth_rho_r(1)%pw_grid, adjoint_bin, adjoint_tile_count, &
2004 adjoint_tile_lower, adjoint_tile_upper)
2005 DO adjoint_entry = adjoint_bin_offsets(adjoint_bin), &
2006 adjoint_bin_offsets(adjoint_bin + 1) - 1
2007 composite_row = adjoint_bin_rows(adjoint_entry)
2008 IF (.NOT. fetch_native_grid_stencil(qs_env%native_grid_cache, composite_row, &
2009 composite_grid_coords(:, composite_row), interpolation_stencil)) THEN
2010 CALL create_native_grid_interpolation_stencil( &
2011 interpolation_stencil, smooth_rho_r(1)%pw_grid, cell, &
2012 composite_grid_coords(:, composite_row), image_partition_atom_composite)
2013 END IF
2014 IF (lsd) THEN
2015 composite_smooth_density_adjoint_value = &
2016 composite_density_grad(composite_row, :)
2017 composite_smooth_gradient_adjoint_value = &
2018 composite_grad_grad(composite_row, :, :)
2019 composite_smooth_kin_adjoint_value = composite_kin_grad(composite_row, :)
2020 ELSE
2021 composite_smooth_density_adjoint_value = 0.0_dp
2022 composite_smooth_gradient_adjoint_value = 0.0_dp
2023 composite_smooth_kin_adjoint_value = 0.0_dp
2024 composite_smooth_density_adjoint_value(1) = &
2025 sum(composite_density_grad(composite_row, :))
2026 composite_smooth_gradient_adjoint_value(:, 1) = &
2027 sum(composite_grad_grad(composite_row, :, :), dim=2)
2028 composite_smooth_kin_adjoint_value(1) = &
2029 sum(composite_kin_grad(composite_row, :))
2030 END IF
2031 CALL add_native_grid_fields_adjoint_tile( &
2032 smooth_density_adjoint_storage, smooth_grad_adjoint_storage, &
2033 smooth_kin_adjoint_storage, smooth_rho_r(1)%pw_grid, interpolation_stencil, &
2034 composite_smooth_density_adjoint_value, &
2035 composite_smooth_gradient_adjoint_value, &
2036 composite_smooth_kin_adjoint_value, adjoint_nchannels, &
2037 adjoint_tile_lower, adjoint_tile_upper)
2038 END DO
2039 END DO
2040!$OMP END PARALLEL DO
2041 DEALLOCATE (adjoint_bin_offsets, adjoint_bin_rows)
2042 smooth_input_contraction = 0.0_dp
2043!$OMP PARALLEL DO SCHEDULE(STATIC) REDUCTION(+:smooth_input_contraction) DEFAULT(NONE) &
2044!$OMP PRIVATE(composite_smooth_density_value, composite_smooth_gradient_value, &
2045!$OMP composite_smooth_kin_value, idir, ispin) &
2046!$OMP SHARED(composite_density_grad, composite_grad_grad, composite_kin_grad, composite_nflat, &
2047!$OMP composite_smooth_density_cache, composite_smooth_gradient_cache, &
2048!$OMP composite_smooth_kin_cache, lsd)
2049 DO composite_row = 1, composite_nflat
2050 composite_smooth_density_value = composite_smooth_density_cache(composite_row, :)
2051 composite_smooth_gradient_value = &
2052 composite_smooth_gradient_cache(composite_row, :, :)
2053 composite_smooth_kin_value = composite_smooth_kin_cache(composite_row, :)
2054 DO ispin = 1, 2
2055 IF (lsd) THEN
2056 smooth_input_contraction = smooth_input_contraction + &
2057 composite_density_grad(composite_row, ispin)* &
2058 composite_smooth_density_value(ispin) + &
2059 composite_kin_grad(composite_row, ispin)* &
2060 composite_smooth_kin_value(ispin)
2061 DO idir = 1, 3
2062 smooth_input_contraction = smooth_input_contraction + &
2063 composite_grad_grad(composite_row, idir, ispin)* &
2064 composite_smooth_gradient_value(idir, ispin)
2065 END DO
2066 ELSE
2067 smooth_input_contraction = smooth_input_contraction + 0.5_dp*( &
2068 composite_density_grad(composite_row, ispin)* &
2069 composite_smooth_density_value(1) + &
2070 composite_kin_grad(composite_row, ispin)* &
2071 composite_smooth_kin_value(1))
2072 DO idir = 1, 3
2073 smooth_input_contraction = smooth_input_contraction + 0.5_dp* &
2074 composite_grad_grad(composite_row, idir, ispin)* &
2075 composite_smooth_gradient_value(idir, 1)
2076 END DO
2077 END IF
2078 END DO
2079 END DO
2080!$OMP END PARALLEL DO
2081 smooth_density_adjoint => smooth_density_adjoint_storage
2082 smooth_grad_adjoint => smooth_grad_adjoint_storage
2083 smooth_kin_adjoint => smooth_kin_adjoint_storage
2084 ! The interpolation transpose still uses CP2K's global FFT-grid layout. Reduce only
2085 ! this PW adjoint; atom-grid feature rows and their model derivatives stay rank-local.
2086 CALL para_env%sum(smooth_density_adjoint)
2087 CALL para_env%sum(smooth_grad_adjoint)
2088 CALL para_env%sum(smooth_kin_adjoint)
2089 CALL para_env%sum(smooth_input_contraction)
2091 smooth_vxc_rho, smooth_vxc_tau, smooth_rho_r, auxbas_pw_pool, &
2092 smooth_density_adjoint, smooth_grad_adjoint, smooth_kin_adjoint, &
2093 xc_deriv_method_id, global_grid_layout=.true.)
2094 NULLIFY (smooth_density_adjoint, smooth_grad_adjoint, smooth_kin_adjoint)
2095 DEALLOCATE (smooth_density_adjoint_storage, smooth_grad_adjoint_storage, &
2096 smooth_kin_adjoint_storage)
2097 smooth_grid_contraction = 0.0_dp
2098 DO ispin = 1, nspins
2099 smooth_grid_contraction = smooth_grid_contraction + smooth_rho_r(1)%pw_grid%dvol* &
2100 (sum(smooth_vxc_rho(ispin)%array* &
2101 smooth_rho_r(ispin)%array) + &
2102 sum(smooth_vxc_tau(ispin)%array* &
2103 smooth_tau_r(ispin)%array))
2104 IF (atom_composite_reference) THEN
2105 cpassert(PRESENT(composite_vxc_rho))
2106 cpassert(PRESENT(composite_vxc_tau))
2107 cpassert(ASSOCIATED(composite_vxc_rho))
2108 cpassert(ASSOCIATED(composite_vxc_tau))
2109 cpassert(SIZE(composite_vxc_rho) == nspins)
2110 cpassert(SIZE(composite_vxc_tau) == nspins)
2111 CALL pw_axpy(smooth_vxc_rho(ispin), composite_vxc_rho(ispin), 1.0_dp)
2112 CALL pw_axpy(smooth_vxc_tau(ispin), composite_vxc_tau(ispin), 1.0_dp)
2113 END IF
2114 CALL auxbas_pw_pool%give_back_pw(smooth_vxc_rho(ispin))
2115 CALL auxbas_pw_pool%give_back_pw(smooth_vxc_tau(ispin))
2116 END DO
2117 CALL para_env%sum(smooth_grid_contraction)
2118 DEALLOCATE (smooth_vxc_rho, smooth_vxc_tau)
2119
2120 one_center_field_contraction = 0.0_dp
2121 one_center_matrix_contraction = 0.0_dp
2122 one_center_density_field_contraction = 0.0_dp
2123 one_center_density_matrix_contraction = 0.0_dp
2124 one_center_gradient_field_contraction = 0.0_dp
2125 one_center_gradient_matrix_contraction = 0.0_dp
2126 one_center_rho_grad_field_contraction = 0.0_dp
2127 one_center_rho_grad_matrix_contraction = 0.0_dp
2128 one_center_tau_field_contraction = 0.0_dp
2129 one_center_tau_matrix_contraction = 0.0_dp
2130 IF (.NOT. direct_valence_atom_composite) THEN
2131 DO ikind = 1, SIZE(atomic_kind_set)
2132 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
2133 NULLIFY (gth_potential, sgp_potential)
2134 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
2135 gth_potential=gth_potential, harmonics=harmonics, &
2136 grid_atom=grid_atom, sgp_potential=sgp_potential, &
2137 zatom=zatom, zeff=zeff)
2138 CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
2139 IF (.NOT. native_skala_uses_one_center_kind( &
2140 paw_atom, gapw_representation, &
2141 ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential), &
2142 zeff, zatom)) cycle
2143
2144 nr = grid_atom%nr
2145 na = grid_atom%ng_sphere
2146 CALL reallocate(rho_h, 1, na, 1, nr, 1, nspins)
2147 CALL reallocate(rho_s, 1, na, 1, nr, 1, nspins)
2148 CALL reallocate(vxc_h, 1, na, 1, nr, 1, nspins)
2149 CALL reallocate(vxc_s, 1, na, 1, nr, 1, nspins)
2150 CALL reallocate(drho_h, 1, 4, 1, na, 1, nr, 1, nspins)
2151 CALL reallocate(drho_s, 1, 4, 1, na, 1, nr, 1, nspins)
2152 CALL reallocate(vxg_h, 1, 3, 1, na, 1, nr, 1, nspins)
2153 CALL reallocate(vxg_s, 1, 3, 1, na, 1, nr, 1, nspins)
2154 CALL reallocate(tau_h, 1, na, 1, nr, 1, nspins)
2155 CALL reallocate(tau_s, 1, na, 1, nr, 1, nspins)
2156 CALL reallocate(vtau_h, 1, na, 1, nr, 1, nspins)
2157 CALL reallocate(vtau_s, 1, na, 1, nr, 1, nspins)
2158 CALL create_tau_basis_cache(tau_basis_cache, grid_atom, basis_1c, harmonics)
2159
2160 bo = get_limit(natom, para_env%num_pe, para_env%mepos)
2161 DO iat = 1, natom
2162 iatom = atom_list(iat)
2163 source_matrix_local = iat >= bo(1) .AND. iat <= bo(2)
2164 rho_atom => my_rho_atom_set(iatom)
2165 NULLIFY (cpc_h, cpc_s, r_h, r_s, dr_h, dr_s, r_h_d, r_s_d, int_hh, int_ss)
2166 CALL get_rho_atom(rho_atom=rho_atom, cpc_h=cpc_h, cpc_s=cpc_s, &
2167 rho_rad_h=r_h, rho_rad_s=r_s, drho_rad_h=dr_h, &
2168 drho_rad_s=dr_s, rho_rad_h_d=r_h_d, rho_rad_s_d=r_s_d, &
2169 ga_vlocal_gb_h=int_hh, ga_vlocal_gb_s=int_ss)
2170 rho_h = 0.0_dp
2171 rho_s = 0.0_dp
2172 drho_h = 0.0_dp
2173 drho_s = 0.0_dp
2174 CALL calc_tau_atom(tau_h, tau_s, rho_atom, tau_basis_cache, nspins)
2175 DO ir = 1, nr
2176 CALL calc_rho_angular(grid_atom, harmonics, nspins, .true., &
2177 ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, &
2178 r_h_d, r_s_d, drho_h, drho_s)
2179 END DO
2180
2181 vxc_h = 0.0_dp
2182 vxc_s = 0.0_dp
2183 vxg_h = 0.0_dp
2184 vxg_s = 0.0_dp
2185 vtau_h = 0.0_dp
2186 vtau_s = 0.0_dp
2187 IF (source_matrix_local) THEN
2188 composite_row = composite_atom_start(iatom) - 1
2189 DO ir = 1, nr
2190 DO ia = 1, na
2191 composite_row = composite_row + 1
2192 IF (lsd) THEN
2193 IF (use_atom_composite_density) THEN
2194 vxc_h(ia, ir, 1:2) = composite_density_grad(composite_row, 1:2)
2195 vxc_s(ia, ir, 1:2) = composite_density_grad(composite_row, 1:2)
2196 END IF
2197 IF (use_atom_composite_gradient) THEN
2198 DO idir = 1, 3
2199 vxg_h(idir, ia, ir, 1:2) = &
2200 composite_grad_grad(composite_row, idir, 1:2)
2201 vxg_s(idir, ia, ir, 1:2) = &
2202 composite_grad_grad(composite_row, idir, 1:2)
2203 END DO
2204 END IF
2205 IF (use_atom_composite_tau) THEN
2206 vtau_h(ia, ir, 1:2) = composite_kin_grad(composite_row, 1:2)
2207 vtau_s(ia, ir, 1:2) = composite_kin_grad(composite_row, 1:2)
2208 END IF
2209 ELSE
2210 IF (use_atom_composite_density) THEN
2211 vxc_h(ia, ir, 1) = 0.5_dp* &
2212 sum(composite_density_grad(composite_row, :))
2213 vxc_s(ia, ir, 1) = vxc_h(ia, ir, 1)
2214 END IF
2215 IF (use_atom_composite_gradient) THEN
2216 DO idir = 1, 3
2217 vxg_h(idir, ia, ir, 1) = 0.5_dp* &
2218 sum(composite_grad_grad( &
2219 composite_row, idir, :))
2220 vxg_s(idir, ia, ir, 1) = vxg_h(idir, ia, ir, 1)
2221 END DO
2222 END IF
2223 IF (use_atom_composite_tau) THEN
2224 vtau_h(ia, ir, 1) = 0.5_dp* &
2225 sum(composite_kin_grad(composite_row, :))
2226 vtau_s(ia, ir, 1) = vtau_h(ia, ir, 1)
2227 END IF
2228 END IF
2229 END DO
2230 END DO
2231 cpassert(composite_row == composite_atom_end(iatom))
2232 END IF
2233
2234 cross_cutoff = gapw_atom_grid_support_radius( &
2235 grid_atom, rho_h, rho_s, drho_h, drho_s, tau_h, tau_s)
2236 IF (cross_cutoff > 0.0_dp) THEN
2237 image_shell = 0
2238 DO idir = 1, 3
2239 IF (cell%perd(idir) == 1) THEN
2240 image_shell(idir) = ceiling( &
2241 cross_cutoff*sqrt(sum(cell%h_inv(idir, :)**2))) + 1
2242 END IF
2243 END DO
2244 ! Each thread accumulates its own one-center potentials; combine them
2245 ! before the existing MPI reduction over target-atom owners.
2246!$OMP PARALLEL DEFAULT(NONE) &
2247!$OMP PRIVATE(image_lower, image_upper, composite_row, cross_density_adjoint, cross_displacement, &
2248!$OMP cross_grad_adjoint, cross_kin_adjoint, fractional, idir, image_i1, &
2249!$OMP image_i2, image_i3, image_shift, image_translation, jdir, target_atom, &
2250!$OMP vxc_h_local, vxc_s_local, vxg_h_local, vxg_s_local, vtau_h_local, vtau_s_local) &
2251!$OMP SHARED(cell, composite_density_grad, composite_grad_grad, composite_grid_atom, &
2252!$OMP composite_grid_coords, composite_kin_grad, composite_nflat, cross_cutoff, &
2253!$OMP grid_atom, harmonics, iatom, image_shell, lsd, nspins, particle_set, &
2254!$OMP use_atom_composite_density, use_atom_composite_gradient, use_atom_composite_tau, &
2255!$OMP vxc_h, vxc_s, vxg_h, vxg_s, vtau_h, vtau_s)
2256 ALLOCATE (vxc_h_local, mold=vxc_h)
2257 ALLOCATE (vxc_s_local, mold=vxc_s)
2258 ALLOCATE (vxg_h_local, mold=vxg_h)
2259 ALLOCATE (vxg_s_local, mold=vxg_s)
2260 ALLOCATE (vtau_h_local, mold=vtau_h)
2261 ALLOCATE (vtau_s_local, mold=vtau_s)
2262 vxc_h_local = 0.0_dp
2263 vxc_s_local = 0.0_dp
2264 vxg_h_local = 0.0_dp
2265 vxg_s_local = 0.0_dp
2266 vtau_h_local = 0.0_dp
2267 vtau_s_local = 0.0_dp
2268!$OMP DO SCHEDULE(STATIC)
2269 DO composite_row = 1, composite_nflat
2270 target_atom = composite_grid_atom(composite_row)
2271 cross_density_adjoint = 0.0_dp
2272 cross_grad_adjoint = 0.0_dp
2273 cross_kin_adjoint = 0.0_dp
2274 IF (lsd) THEN
2275 IF (use_atom_composite_density) THEN
2276 cross_density_adjoint(1:2) = &
2277 composite_density_grad(composite_row, 1:2)
2278 END IF
2279 IF (use_atom_composite_gradient) THEN
2280 cross_grad_adjoint(:, 1:2) = &
2281 composite_grad_grad(composite_row, :, 1:2)
2282 END IF
2283 IF (use_atom_composite_tau) THEN
2284 cross_kin_adjoint(1:2) = &
2285 composite_kin_grad(composite_row, 1:2)
2286 END IF
2287 ELSE
2288 IF (use_atom_composite_density) THEN
2289 cross_density_adjoint(1) = 0.5_dp* &
2290 sum(composite_density_grad(composite_row, :))
2291 END IF
2292 IF (use_atom_composite_gradient) THEN
2293 DO idir = 1, 3
2294 cross_grad_adjoint(idir, 1) = 0.5_dp* &
2295 sum(composite_grad_grad(composite_row, idir, :))
2296 END DO
2297 END IF
2298 IF (use_atom_composite_tau) THEN
2299 cross_kin_adjoint(1) = 0.5_dp* &
2300 sum(composite_kin_grad(composite_row, :))
2301 END IF
2302 END IF
2303 fractional = 0.0_dp
2304 DO idir = 1, 3
2305 DO jdir = 1, 3
2306 fractional(idir) = fractional(idir) + cell%h_inv(idir, jdir)* &
2307 (composite_grid_coords(jdir, composite_row) - &
2308 particle_set(iatom)%r(jdir))
2309 END DO
2310 END DO
2311 CALL atom_grid_image_bounds(cell, fractional, cross_cutoff, image_shell, &
2312 image_lower, image_upper)
2313 DO image_i3 = image_lower(3), image_upper(3)
2314 DO image_i2 = image_lower(2), image_upper(2)
2315 DO image_i1 = image_lower(1), image_upper(1)
2316 image_shift = [image_i1, image_i2, image_i3]
2317 IF (target_atom == iatom .AND. all(image_shift == 0)) cycle
2318 image_translation = matmul( &
2319 cell%hmat, real(image_shift, dp))
2320 cross_displacement = &
2321 composite_grid_coords(:, composite_row) - &
2322 particle_set(iatom)%r - image_translation
2323 ! Reject inactive images before allocating interpolation scratch.
2324 IF (sqrt(sum(cross_displacement**2)) > cross_cutoff) cycle
2326 grid_atom, harmonics, cross_displacement, cross_cutoff, &
2327 nspins, cross_density_adjoint, cross_grad_adjoint, &
2328 cross_kin_adjoint, vxc_h_local, vxc_s_local, vxg_h_local, vxg_s_local, &
2329 vtau_h_local, vtau_s_local)
2330 END DO
2331 END DO
2332 END DO
2333 END DO
2334!$OMP END DO
2335!$OMP CRITICAL(skala_atom_composite_adjoint_reduction)
2336 vxc_h = vxc_h + vxc_h_local
2337 vxc_s = vxc_s + vxc_s_local
2338 vxg_h = vxg_h + vxg_h_local
2339 vxg_s = vxg_s + vxg_s_local
2340 vtau_h = vtau_h + vtau_h_local
2341 vtau_s = vtau_s + vtau_s_local
2342!$OMP END CRITICAL(skala_atom_composite_adjoint_reduction)
2343 DEALLOCATE (vxc_h_local, vxc_s_local, vxg_h_local, vxg_s_local, vtau_h_local, vtau_s_local)
2344!$OMP END PARALLEL
2345 END IF
2346
2347 ! Model rows are distributed by target atom, while CP2K stores each
2348 ! one-center matrix on the rank owning its source atom. Sum the exact
2349 ! interpolation transpose before forming that matrix.
2350 CALL para_env%sum(vxc_h)
2351 CALL para_env%sum(vxc_s)
2352 CALL para_env%sum(vxg_h)
2353 CALL para_env%sum(vxg_s)
2354 CALL para_env%sum(vtau_h)
2355 CALL para_env%sum(vtau_s)
2356 IF (.NOT. source_matrix_local) cycle
2357
2358 one_center_rho_grad_field_contraction = &
2359 one_center_rho_grad_field_contraction + sum(vxc_h*(rho_h - rho_s)) + &
2360 sum(vxg_h*(drho_h(1:3, :, :, :) - drho_s(1:3, :, :, :)))
2361 one_center_density_field_contraction = one_center_density_field_contraction + &
2362 sum(vxc_h*(rho_h - rho_s))
2363 one_center_gradient_field_contraction = one_center_gradient_field_contraction + &
2364 sum(vxg_h*(drho_h(1:3, :, :, :) - &
2365 drho_s(1:3, :, :, :)))
2366 one_center_tau_field_contraction = one_center_tau_field_contraction + &
2367 sum(vtau_h*(tau_h - tau_s))
2368
2369 ALLOCATE (composite_int_h(SIZE(int_hh(1)%r_coef, 1), &
2370 SIZE(int_hh(1)%r_coef, 2), nspins), &
2371 composite_int_s(SIZE(int_ss(1)%r_coef, 1), &
2372 SIZE(int_ss(1)%r_coef, 2), nspins))
2373 DO ispin = 1, nspins
2374 composite_int_h(:, :, ispin) = int_hh(ispin)%r_coef
2375 composite_int_s(:, :, ispin) = int_ss(ispin)%r_coef
2376 int_hh(ispin)%r_coef = 0.0_dp
2377 int_ss(ispin)%r_coef = 0.0_dp
2378 END DO
2379 CALL gavxcgb_nogc(vxc_h, vxc_s, int_hh, int_ss, &
2380 grid_atom, basis_1c, harmonics, nspins)
2381 DO ispin = 1, nspins
2382 one_center_density_matrix_contraction = &
2383 one_center_density_matrix_contraction + &
2384 contract_one_center_matrix(cpc_h(ispin)%r_coef, &
2385 int_hh(ispin)%r_coef, &
2386 tau_basis_cache%n2oindex) - &
2387 contract_one_center_matrix(cpc_s(ispin)%r_coef, &
2388 int_ss(ispin)%r_coef, &
2389 tau_basis_cache%n2oindex)
2390 int_hh(ispin)%r_coef = 0.0_dp
2391 int_ss(ispin)%r_coef = 0.0_dp
2392 END DO
2393 CALL gavxcgb_gc(vxc_h, vxc_s, vxg_h, vxg_s, int_hh, int_ss, &
2394 grid_atom, basis_1c, harmonics, nspins)
2395 DO ispin = 1, nspins
2396 one_center_rho_grad_matrix_contraction = &
2397 one_center_rho_grad_matrix_contraction + &
2398 contract_one_center_matrix(cpc_h(ispin)%r_coef, &
2399 int_hh(ispin)%r_coef, &
2400 tau_basis_cache%n2oindex) - &
2401 contract_one_center_matrix(cpc_s(ispin)%r_coef, &
2402 int_ss(ispin)%r_coef, &
2403 tau_basis_cache%n2oindex)
2404 END DO
2405 CALL dgavtaudgb(vtau_h, vtau_s, int_hh, int_ss, tau_basis_cache, nspins)
2406 DO ispin = 1, nspins
2407 one_center_matrix_contraction = one_center_matrix_contraction + &
2408 contract_one_center_matrix(cpc_h(ispin)%r_coef, &
2409 int_hh(ispin)%r_coef, &
2410 tau_basis_cache%n2oindex) - &
2411 contract_one_center_matrix(cpc_s(ispin)%r_coef, &
2412 int_ss(ispin)%r_coef, &
2413 tau_basis_cache%n2oindex)
2414 IF (.NOT. atom_composite_reference) THEN
2415 int_hh(ispin)%r_coef = composite_int_h(:, :, ispin)
2416 int_ss(ispin)%r_coef = composite_int_s(:, :, ispin)
2417 END IF
2418 END DO
2419 DEALLOCATE (composite_int_h, composite_int_s)
2420 END DO
2421 CALL release_tau_basis_cache(tau_basis_cache)
2422 END DO
2423 END IF
2424 CALL para_env%sum(one_center_density_field_contraction)
2425 CALL para_env%sum(one_center_density_matrix_contraction)
2426 CALL para_env%sum(one_center_gradient_field_contraction)
2427 CALL para_env%sum(one_center_matrix_contraction)
2428 CALL para_env%sum(one_center_rho_grad_field_contraction)
2429 CALL para_env%sum(one_center_rho_grad_matrix_contraction)
2430 CALL para_env%sum(one_center_tau_field_contraction)
2431 one_center_field_contraction = one_center_rho_grad_field_contraction + &
2432 one_center_tau_field_contraction
2433 one_center_gradient_matrix_contraction = one_center_rho_grad_matrix_contraction - &
2434 one_center_density_matrix_contraction
2435 one_center_tau_matrix_contraction = one_center_matrix_contraction - &
2436 one_center_rho_grad_matrix_contraction
2437 feature_component_analytic(1) = sum(composite_density_grad*composite_density)
2438 feature_component_analytic(2) = sum(composite_grad_grad*composite_grad)
2439 feature_component_analytic(3) = sum(composite_kin_grad*composite_kin)
2440 feature_component_analytic(4) = sum(feature_component_analytic(1:3))
2441 CALL para_env%sum(feature_component_analytic)
2442 one_center_tensor_contraction = feature_component_analytic(4) - &
2443 smooth_input_contraction
2444 IF (atom_composite_diagnostic) THEN
2445 DO icomponent = 1, 4
2446 SELECT CASE (icomponent)
2447 CASE (1)
2448 composite_density = (1.0_dp + feature_vxc_step)*composite_density
2449 CASE (2)
2450 composite_grad = (1.0_dp + feature_vxc_step)*composite_grad
2451 CASE (3)
2452 composite_kin = (1.0_dp + feature_vxc_step)*composite_kin
2453 CASE (4)
2454 composite_density = (1.0_dp + feature_vxc_step)*composite_density
2455 composite_grad = (1.0_dp + feature_vxc_step)*composite_grad
2456 composite_kin = (1.0_dp + feature_vxc_step)*composite_kin
2457 END SELECT
2459 my_xc_section, para_env, composite_density, composite_grad, composite_kin, &
2460 composite_grid_coords, composite_grid_weights, composite_atomic_grid_weights, &
2461 composite_local_grid_sizes, composite_local_atom_coords, feature_vxc_plus)
2462 SELECT CASE (icomponent)
2463 CASE (1)
2464 composite_density = ((1.0_dp - feature_vxc_step)/ &
2465 (1.0_dp + feature_vxc_step))*composite_density
2466 CASE (2)
2467 composite_grad = ((1.0_dp - feature_vxc_step)/ &
2468 (1.0_dp + feature_vxc_step))*composite_grad
2469 CASE (3)
2470 composite_kin = ((1.0_dp - feature_vxc_step)/ &
2471 (1.0_dp + feature_vxc_step))*composite_kin
2472 CASE (4)
2473 composite_density = ((1.0_dp - feature_vxc_step)/ &
2474 (1.0_dp + feature_vxc_step))*composite_density
2475 composite_grad = ((1.0_dp - feature_vxc_step)/ &
2476 (1.0_dp + feature_vxc_step))*composite_grad
2477 composite_kin = ((1.0_dp - feature_vxc_step)/ &
2478 (1.0_dp + feature_vxc_step))*composite_kin
2479 END SELECT
2481 my_xc_section, para_env, composite_density, composite_grad, composite_kin, &
2482 composite_grid_coords, composite_grid_weights, composite_atomic_grid_weights, &
2483 composite_local_grid_sizes, composite_local_atom_coords, feature_vxc_minus)
2484 SELECT CASE (icomponent)
2485 CASE (1)
2486 composite_density = composite_density/(1.0_dp - feature_vxc_step)
2487 CASE (2)
2488 composite_grad = composite_grad/(1.0_dp - feature_vxc_step)
2489 CASE (3)
2490 composite_kin = composite_kin/(1.0_dp - feature_vxc_step)
2491 CASE (4)
2492 composite_density = composite_density/(1.0_dp - feature_vxc_step)
2493 composite_grad = composite_grad/(1.0_dp - feature_vxc_step)
2494 composite_kin = composite_kin/(1.0_dp - feature_vxc_step)
2495 END SELECT
2496 feature_component_fd(icomponent) = &
2497 (feature_vxc_plus - feature_vxc_minus)/(2.0_dp*feature_vxc_step)
2498 END DO
2499 feature_vxc_analytic = feature_component_analytic(4)
2500 feature_vxc_fd = feature_component_fd(4)
2502 IF (iw > 0) THEN
2503 WRITE (unit=iw, fmt="(T2,A,1X,I0)") &
2504 "SKALA_GPW| Atom-composite reference components", atom_composite_components
2505 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2506 "SKALA_GPW| Atom-composite reference electrons", atom_composite_nelec
2507 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2508 "SKALA_GPW| Atom-composite reference XC energy", atom_composite_exc
2509 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2510 "SKALA_GPW| Atom-composite feature VXC contraction", feature_vxc_analytic
2511 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2512 "SKALA_GPW| Atom-composite feature finite difference", feature_vxc_fd
2513 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2514 "SKALA_GPW| Atom-composite feature VXC error", &
2515 feature_vxc_analytic - feature_vxc_fd
2516 WRITE (unit=iw, fmt="(T2,A,3(1X,ES24.16))") &
2517 "SKALA_GPW| Atom-composite smooth PW adjoint", smooth_input_contraction, &
2518 smooth_grid_contraction, smooth_grid_contraction - smooth_input_contraction
2519 WRITE (unit=iw, fmt="(T2,A,4(1X,ES24.16))") &
2520 "SKALA_GPW| Atom-composite one-center adjoint", one_center_tensor_contraction, &
2521 one_center_field_contraction, one_center_matrix_contraction, &
2522 one_center_matrix_contraction - one_center_tensor_contraction
2523 WRITE (unit=iw, fmt="(T2,A,4(1X,ES24.16))") &
2524 "SKALA_GPW| Atom-composite one-center channels", &
2525 one_center_rho_grad_field_contraction, one_center_rho_grad_matrix_contraction, &
2526 one_center_tau_field_contraction, one_center_tau_matrix_contraction
2527 WRITE (unit=iw, fmt="(T2,A,4(1X,ES24.16))") &
2528 "SKALA_GPW| Atom-composite one-center rho-gradient", &
2529 one_center_density_field_contraction, one_center_density_matrix_contraction, &
2530 one_center_gradient_field_contraction, one_center_gradient_matrix_contraction
2531 DO icomponent = 1, 3
2532 WRITE (unit=iw, fmt="(T2,A,1X,I0,3(1X,ES24.16))") &
2533 "SKALA_GPW| Atom-composite component VXC", icomponent, &
2534 feature_component_analytic(icomponent), feature_component_fd(icomponent), &
2535 feature_component_analytic(icomponent) - feature_component_fd(icomponent)
2536 END DO
2537 END IF
2538 END IF
2539 IF (atom_composite_reference) THEN
2540 exc1 = atom_composite_exc
2541 IF (native_grid_diagnostics) THEN
2542 IF (composite_nflat > 0) THEN
2543 composite_density_min = minval(composite_density)
2544 composite_density_max = maxval(composite_density)
2545 composite_kin_min = minval(composite_kin)
2546 composite_kin_max = maxval(composite_kin)
2547 composite_grad_max = maxval(abs(composite_grad))
2548 ELSE
2549 composite_density_min = huge(1.0_dp)
2550 composite_density_max = -huge(1.0_dp)
2551 composite_kin_min = huge(1.0_dp)
2552 composite_kin_max = -huge(1.0_dp)
2553 composite_grad_max = 0.0_dp
2554 END IF
2555 composite_tau_integral = &
2556 sum(composite_grid_weights*sum(composite_kin, dim=2))
2557 CALL para_env%min(composite_density_min)
2558 CALL para_env%max(composite_density_max)
2559 CALL para_env%min(composite_kin_min)
2560 CALL para_env%max(composite_kin_max)
2561 CALL para_env%max(composite_grad_max)
2562 CALL para_env%sum(composite_tau_integral)
2563 END IF
2565 IF (iw > 0) THEN
2566 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2567 "SKALA_GPW| Active atom-composite XC energy", atom_composite_exc
2568 IF (native_grid_diagnostics) THEN
2569 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2570 "SKALA_GPW| Active atom-composite electrons", atom_composite_nelec
2571 WRITE (unit=iw, fmt="(T2,A,2(1X,ES24.16))") &
2572 "SKALA_GPW| Active atom-composite density range", &
2573 composite_density_min, composite_density_max
2574 WRITE (unit=iw, fmt="(T2,A,2(1X,ES24.16))") &
2575 "SKALA_GPW| Active atom-composite tau range", &
2576 composite_kin_min, composite_kin_max
2577 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2578 "SKALA_GPW| Active atom-composite tau integral", &
2579 composite_tau_integral
2580 WRITE (unit=iw, fmt="(T2,A,1X,ES24.16)") &
2581 "SKALA_GPW| Active atom-composite max gradient", &
2582 composite_grad_max
2583 END IF
2584 END IF
2585 END IF
2586 CALL xc_rho_set_release(smooth_rho_set, pw_pool=auxbas_pw_pool)
2587 DEALLOCATE (composite_atomic_grid_sizes, composite_atom_kind, &
2588 composite_atom_kind_index, composite_atom_start, composite_atom_end, &
2589 composite_atom_coords, composite_local_atoms, composite_local_grid_sizes, &
2590 composite_local_atom_coords, composite_grid_atom, &
2591 composite_partition_weights, composite_partition_atom_coords, &
2592 composite_distances, composite_density, composite_grad, composite_kin, &
2593 composite_smooth_density_cache, composite_smooth_gradient_cache, &
2594 composite_smooth_kin_cache, &
2595 composite_density_grad, composite_grad_grad, composite_kin_grad, &
2596 composite_grid_coords, composite_grid_weights, &
2597 composite_base_grid_weights, composite_atomic_grid_weights)
2598 IF (lsd) THEN
2599 DEALLOCATE (composite_smooth_rhoa, composite_smooth_rhob, &
2600 composite_smooth_tau_a, composite_smooth_tau_b)
2601 DO idir = 1, 3
2602 DEALLOCATE (composite_smooth_drhoa(idir)%array, &
2603 composite_smooth_drhob(idir)%array)
2604 END DO
2605 ELSE
2606 DEALLOCATE (composite_smooth_rho, composite_smooth_tau)
2607 DO idir = 1, 3
2608 DEALLOCATE (composite_smooth_drho(idir)%array)
2609 END DO
2610 END IF
2611 END IF
2612
2613 IF (.NOT. atom_composite_reference) CALL para_env%sum(exc1)
2614
2615 IF (ASSOCIATED(rho_h)) DEALLOCATE (rho_h)
2616 IF (ASSOCIATED(rho_s)) DEALLOCATE (rho_s)
2617 IF (ASSOCIATED(vxc_h)) DEALLOCATE (vxc_h)
2618 IF (ASSOCIATED(vxc_s)) DEALLOCATE (vxc_s)
2619
2620 IF (gradient_f) THEN
2621 IF (ASSOCIATED(drho_h)) DEALLOCATE (drho_h)
2622 IF (ASSOCIATED(drho_s)) DEALLOCATE (drho_s)
2623 IF (ASSOCIATED(vxg_h)) DEALLOCATE (vxg_h)
2624 IF (ASSOCIATED(vxg_s)) DEALLOCATE (vxg_s)
2625 END IF
2626
2627 IF (tau_f) THEN
2628 IF (ASSOCIATED(tau_h)) DEALLOCATE (tau_h)
2629 IF (ASSOCIATED(tau_s)) DEALLOCATE (tau_s)
2630 IF (ASSOCIATED(vtau_h)) DEALLOCATE (vtau_h)
2631 IF (ASSOCIATED(vtau_s)) DEALLOCATE (vtau_s)
2632 END IF
2633
2634 END IF !xc_none
2635
2636 CALL timestop(handle)
2637
2638 END SUBROUTINE calculate_vxc_atom
2639
2640! **************************************************************************************************
2641!> \brief Add the GAPW one-center correction to CDFT values and operators.
2642!> \param qs_env Quickstep environment
2643!> \param energy_only skip construction of the CDFT one-center operator
2644!> \param calculate_forces evaluate explicit derivatives of the partition weights
2645!> \param values constraint values from the hard-minus-soft one-center densities
2646!> \param electronic_charge optional one-center corrections to atomic populations
2647!> \param operator_group optional group for which to build the unscaled weight operator
2648!> \param rho_atom_operator_set optional destination for the one-center operator integrals
2649! **************************************************************************************************
2650 SUBROUTINE gapw_cdft_one_center(qs_env, energy_only, calculate_forces, values, &
2651 electronic_charge, operator_group, rho_atom_operator_set)
2652 TYPE(qs_environment_type), POINTER :: qs_env
2653 LOGICAL, INTENT(IN) :: energy_only, calculate_forces
2654 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: values
2655 REAL(kind=dp), DIMENSION(:, :), INTENT(OUT), &
2656 OPTIONAL :: electronic_charge
2657 INTEGER, INTENT(IN), OPTIONAL :: operator_group
2658 TYPE(rho_atom_type), DIMENSION(:), POINTER, &
2659 OPTIONAL :: rho_atom_operator_set
2660
2661 INTEGER :: atom, channel, ia, iat, igroup, ikind, &
2662 ir, natom, natom_kind, nspins
2663 INTEGER, DIMENSION(2) :: atom_bounds
2664 INTEGER, DIMENSION(:), POINTER :: atom_list
2665 LOGICAL :: lsd, paw_atom
2666 REAL(kind=dp) :: delta_density, point_factor, &
2667 spin_factor
2668 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: atomic_weights, group_weights
2669 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: explicit_derivative, &
2670 group_point_derivative
2671 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: group_atom_derivative
2672 REAL(kind=dp), DIMENSION(3) :: point
2673 REAL(kind=dp), DIMENSION(:, :, :), POINTER :: rho_h, rho_s
2674 REAL(kind=dp), DIMENSION(:, :, :, :), POINTER :: drho_h, drho_s
2675 REAL(kind=dp), DIMENSION(:, :, :), POINTER :: vlocal_h, vlocal_s
2676 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
2677 TYPE(cdft_control_type), POINTER :: cdft_control
2678 TYPE(cdft_point_context_type) :: context
2679 TYPE(dft_control_type), POINTER :: dft_control
2680 TYPE(grid_atom_type), POINTER :: grid_atom
2681 TYPE(gto_basis_set_type), POINTER :: basis_1c
2682 TYPE(harmonics_atom_type), POINTER :: harmonics
2683 TYPE(mp_para_env_type), POINTER :: para_env
2684 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
2685 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
2686 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
2687 TYPE(rho_atom_coeff), DIMENSION(:), POINTER :: dr_h, dr_s, int_hh, int_ss, r_h, r_s
2688 TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER :: r_h_d, r_s_d
2689 TYPE(rho_atom_type), DIMENSION(:), POINTER :: operator_atom_set, rho_atom_set
2690 TYPE(rho_atom_type), POINTER :: operator_atom, rho_atom
2691
2692 NULLIFY (atom_list, atomic_kind_set, basis_1c, cdft_control, dft_control, force, &
2693 grid_atom, harmonics, int_hh, int_ss, para_env, particle_set, r_h, r_s, &
2694 dr_h, dr_s, r_h_d, r_s_d, rho_h, rho_s, drho_h, drho_s, &
2695 operator_atom, operator_atom_set, rho_atom, rho_atom_set, qs_kind_set, &
2696 vlocal_h, vlocal_s)
2697 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, dft_control=dft_control, &
2698 force=force, natom=natom, para_env=para_env, particle_set=particle_set, &
2699 qs_kind_set=qs_kind_set, rho_atom_set=rho_atom_set)
2700 cpassert(ASSOCIATED(atomic_kind_set))
2701 cpassert(ASSOCIATED(dft_control))
2702 cpassert(ASSOCIATED(para_env))
2703 cpassert(ASSOCIATED(particle_set))
2704 cpassert(ASSOCIATED(qs_kind_set))
2705 cpassert(ASSOCIATED(rho_atom_set))
2706 operator_atom_set => rho_atom_set
2707 IF (PRESENT(rho_atom_operator_set)) operator_atom_set => rho_atom_operator_set
2708 cpassert(ASSOCIATED(operator_atom_set))
2709 cdft_control => dft_control%qs_control%cdft_control
2710 cpassert(ASSOCIATED(cdft_control))
2711 nspins = dft_control%nspins
2712 lsd = dft_control%lsd
2713 cpassert(SIZE(values) == SIZE(cdft_control%group))
2714 IF (PRESENT(operator_group)) THEN
2715 cpassert(operator_group >= 1 .AND. operator_group <= SIZE(cdft_control%group))
2716 END IF
2717 IF (PRESENT(electronic_charge)) THEN
2718 cpassert(SIZE(electronic_charge, 1) == natom)
2719 cpassert(SIZE(electronic_charge, 2) == nspins)
2720 END IF
2721
2722 CALL cdft_point_context_create(qs_env, context, calculate_forces)
2723 ALLOCATE (group_weights(context%ngroup), group_point_derivative(3, context%ngroup), &
2724 group_atom_derivative(3, natom, context%ngroup), &
2725 explicit_derivative(3, natom))
2726 IF (PRESENT(electronic_charge)) ALLOCATE (atomic_weights(natom))
2727 values = 0.0_dp
2728 explicit_derivative = 0.0_dp
2729 IF (PRESENT(electronic_charge)) electronic_charge = 0.0_dp
2730
2731 DO ikind = 1, SIZE(atomic_kind_set)
2732 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom_kind)
2733 CALL get_qs_kind(qs_kind_set(ikind), paw_atom=paw_atom, grid_atom=grid_atom, &
2734 harmonics=harmonics)
2735 IF (.NOT. paw_atom) cycle
2736 CALL get_qs_kind(qs_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
2737 cpassert(ASSOCIATED(grid_atom))
2738 cpassert(ASSOCIATED(harmonics))
2739 cpassert(ASSOCIATED(basis_1c))
2740 ALLOCATE (vlocal_h(grid_atom%ng_sphere, grid_atom%nr, nspins), &
2741 vlocal_s(grid_atom%ng_sphere, grid_atom%nr, nspins))
2742
2743 atom_bounds = get_limit(natom_kind, para_env%num_pe, para_env%mepos)
2744 DO iat = atom_bounds(1), atom_bounds(2)
2745 atom = atom_list(iat)
2746 rho_atom => rho_atom_set(atom)
2747 NULLIFY (r_h, r_s)
2748 CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, rho_rad_s=r_s)
2749 CALL reallocate(rho_h, 1, grid_atom%ng_sphere, 1, grid_atom%nr, 1, nspins)
2750 CALL reallocate(rho_s, 1, grid_atom%ng_sphere, 1, grid_atom%nr, 1, nspins)
2751 rho_h = 0.0_dp
2752 rho_s = 0.0_dp
2753 DO ir = 1, grid_atom%nr
2754 CALL calc_rho_angular(grid_atom, harmonics, nspins, .false., ir, r_h, r_s, &
2755 rho_h, rho_s, dr_h, dr_s, r_h_d, r_s_d, drho_h, drho_s)
2756 END DO
2757 vlocal_h = 0.0_dp
2758 vlocal_s = 0.0_dp
2759
2760 DO ir = 1, grid_atom%nr
2761 DO ia = 1, grid_atom%ng_sphere
2762 point(1) = particle_set(atom)%r(1) + grid_atom%rad(ir)* &
2763 grid_atom%sin_pol(ia)*grid_atom%cos_azi(ia)
2764 point(2) = particle_set(atom)%r(2) + grid_atom%rad(ir)* &
2765 grid_atom%sin_pol(ia)*grid_atom%sin_azi(ia)
2766 point(3) = particle_set(atom)%r(3) + grid_atom%rad(ir)*grid_atom%cos_pol(ia)
2767 IF (PRESENT(electronic_charge)) THEN
2768 CALL cdft_point_weights(context, point, group_weights, &
2769 group_point_derivative, group_atom_derivative, &
2770 atomic_weights)
2771 ELSE
2772 CALL cdft_point_weights(context, point, group_weights, &
2773 group_point_derivative, group_atom_derivative)
2774 END IF
2775 DO channel = 1, nspins
2776 delta_density = rho_h(ia, ir, channel) - rho_s(ia, ir, channel)
2777 IF (PRESENT(electronic_charge)) THEN
2778 electronic_charge(:, channel) = electronic_charge(:, channel) + &
2779 grid_atom%weight(ia, ir)*atomic_weights* &
2780 delta_density
2781 END IF
2782 point_factor = 0.0_dp
2783 DO igroup = 1, context%ngroup
2784 spin_factor = cdft_spin_factor( &
2785 cdft_control%group(igroup)%constraint_type, channel, lsd)
2786 IF (PRESENT(operator_group)) THEN
2787 IF (igroup == operator_group) THEN
2788 point_factor = point_factor + group_weights(igroup)
2789 END IF
2790 ELSE
2791 point_factor = point_factor + cdft_control%strength(igroup)* &
2792 group_weights(igroup)*spin_factor
2793 END IF
2794 values(igroup) = values(igroup) + grid_atom%weight(ia, ir)* &
2795 group_weights(igroup)*delta_density*spin_factor
2796 IF (calculate_forces) THEN
2797 explicit_derivative(:, :) = &
2798 explicit_derivative + grid_atom%weight(ia, ir)* &
2799 cdft_control%strength(igroup)*delta_density* &
2800 spin_factor*group_atom_derivative(:, :, igroup)
2801 explicit_derivative(:, atom) = explicit_derivative(:, atom) + &
2802 grid_atom%weight(ia, ir)* &
2803 cdft_control%strength(igroup)*delta_density* &
2804 spin_factor*group_point_derivative(:, igroup)
2805 END IF
2806 END DO
2807 vlocal_h(ia, ir, channel) = grid_atom%weight(ia, ir)*point_factor
2808 vlocal_s(ia, ir, channel) = vlocal_h(ia, ir, channel)
2809 END DO
2810 END DO
2811 END DO
2812
2813 IF (.NOT. energy_only) THEN
2814 NULLIFY (int_hh, int_ss)
2815 operator_atom => operator_atom_set(atom)
2816 CALL get_rho_atom(rho_atom=operator_atom, ga_vlocal_gb_h=int_hh, ga_vlocal_gb_s=int_ss)
2817 CALL gavxcgb_nogc(vlocal_h, vlocal_s, int_hh, int_ss, grid_atom, &
2818 basis_1c, harmonics, nspins)
2819 END IF
2820 END DO
2821 DEALLOCATE (vlocal_h, vlocal_s)
2822 END DO
2823
2824 CALL para_env%sum(values)
2825 CALL para_env%sum(explicit_derivative)
2826 IF (PRESENT(electronic_charge)) CALL para_env%sum(electronic_charge)
2827 IF (calculate_forces .AND. ASSOCIATED(force) .AND. para_env%is_source()) THEN
2828 DO ikind = 1, SIZE(atomic_kind_set)
2829 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom_kind)
2830 DO iat = 1, natom_kind
2831 atom = atom_list(iat)
2832 force(ikind)%rho_elec(:, iat) = force(ikind)%rho_elec(:, iat) + &
2833 explicit_derivative(:, atom)
2834 END DO
2835 END DO
2836 END IF
2837
2838 IF (ASSOCIATED(rho_h)) DEALLOCATE (rho_h)
2839 IF (ASSOCIATED(rho_s)) DEALLOCATE (rho_s)
2840 IF (ALLOCATED(atomic_weights)) DEALLOCATE (atomic_weights)
2841 DEALLOCATE (explicit_derivative, group_atom_derivative, group_point_derivative, group_weights)
2842 CALL cdft_point_context_release(context)
2843
2844 CONTAINS
2845
2846! **************************************************************************************************
2847!> \brief ...
2848!> \param constraint_type ...
2849!> \param channel ...
2850!> \param lsd ...
2851!> \return ...
2852! **************************************************************************************************
2853 FUNCTION cdft_spin_factor(constraint_type, channel, lsd) RESULT(factor)
2854 INTEGER, INTENT(IN) :: constraint_type, channel
2855 LOGICAL, INTENT(IN) :: lsd
2856 REAL(kind=dp) :: factor
2857
2858 SELECT CASE (constraint_type)
2860 factor = 1.0_dp
2862 cpassert(lsd)
2863 factor = merge(1.0_dp, -1.0_dp, channel == 1)
2865 cpassert(lsd)
2866 factor = merge(1.0_dp, 0.0_dp, channel == 1)
2868 cpassert(lsd)
2869 factor = merge(1.0_dp, 0.0_dp, channel == 2)
2870 CASE DEFAULT
2871 cpabort("Unknown CDFT constraint type.")
2872 END SELECT
2873 END FUNCTION cdft_spin_factor
2874
2875 END SUBROUTINE gapw_cdft_one_center
2876
2877! **************************************************************************************************
2878!> \brief Contract a compact one-center density matrix with an integral in the padded old basis.
2879!> \param density_matrix ...
2880!> \param integral_matrix ...
2881!> \param new_to_old ...
2882!> \return ...
2883! **************************************************************************************************
2884 FUNCTION contract_one_center_matrix(density_matrix, integral_matrix, new_to_old) RESULT(value)
2885 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: density_matrix, integral_matrix
2886 INTEGER, DIMENSION(:), INTENT(IN) :: new_to_old
2887 REAL(kind=dp) :: value
2888
2889 INTEGER :: ibas, jbas, nbas
2890
2891 nbas = SIZE(density_matrix, 1)
2892 cpassert(SIZE(density_matrix, 2) == nbas)
2893 cpassert(SIZE(new_to_old) >= nbas)
2894 cpassert(minval(new_to_old(1:nbas)) >= 1)
2895 cpassert(maxval(new_to_old(1:nbas)) <= SIZE(integral_matrix, 1))
2896 cpassert(maxval(new_to_old(1:nbas)) <= SIZE(integral_matrix, 2))
2897
2898 value = 0.0_dp
2899 DO jbas = 1, nbas
2900 DO ibas = 1, nbas
2901 value = value + density_matrix(ibas, jbas)* &
2902 integral_matrix(new_to_old(ibas), new_to_old(jbas))
2903 END DO
2904 END DO
2905
2906 END FUNCTION contract_one_center_matrix
2907
2908! **************************************************************************************************
2909!> \brief Replicate a distributed real-space field for atom-grid interpolation.
2910!> \param local_values ...
2911!> \param pw_grid ...
2912!> \param group ...
2913!> \param global_values ...
2914! **************************************************************************************************
2915 SUBROUTINE gather_native_grid_field(local_values, pw_grid, group, global_values)
2916 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN), &
2917 POINTER :: local_values
2918 TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
2919 TYPE(mp_para_env_type), INTENT(IN) :: group
2920 REAL(kind=dp), DIMENSION(:, :, :), INTENT(OUT), &
2921 POINTER :: global_values
2922
2923 INTEGER, DIMENSION(2, 3) :: bo
2924
2925 cpassert(ASSOCIATED(local_values))
2926 cpassert(ASSOCIATED(pw_grid))
2927 cpassert(.NOT. ASSOCIATED(global_values))
2928 bo = pw_grid%bounds_local
2929 ALLOCATE (global_values(pw_grid%bounds(1, 1):pw_grid%bounds(2, 1), &
2930 pw_grid%bounds(1, 2):pw_grid%bounds(2, 2), &
2931 pw_grid%bounds(1, 3):pw_grid%bounds(2, 3)))
2932 global_values = 0.0_dp
2933 global_values(bo(1, 1):bo(2, 1), bo(1, 2):bo(2, 2), bo(1, 3):bo(2, 3)) = &
2934 local_values
2935 CALL group%sum(global_values)
2936
2937 END SUBROUTINE gather_native_grid_field
2938
2939! **************************************************************************************************
2940!> \brief Build the tensor-product interpolation stencil for one Cartesian point.
2941!> \param stencil ...
2942!> \param pw_grid ...
2943!> \param cell ...
2944!> \param point ...
2945!> \param wrap_auxiliary_cell wrap all auxiliary-grid directions
2946!> \param indices_only skip interpolation weights when only grid indices are needed
2947! **************************************************************************************************
2948 SUBROUTINE create_native_grid_interpolation_stencil(stencil, pw_grid, cell, point, &
2949 wrap_auxiliary_cell, indices_only)
2951 INTENT(OUT) :: stencil
2952 TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
2953 TYPE(cell_type), INTENT(IN), POINTER :: cell
2954 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: point
2955 LOGICAL, INTENT(IN), OPTIONAL :: wrap_auxiliary_cell, indices_only
2956
2957 INTEGER :: idir, inode, relative_index
2958 INTEGER, DIMENSION(3) :: base
2959 LOGICAL :: build_weights, wrap_grid
2960 REAL(kind=dp), DIMENSION(3) :: fraction, relative
2961
2962 cpassert(ASSOCIATED(pw_grid))
2963 cpassert(ASSOCIATED(cell))
2964 stencil%active = .false.
2965 stencil%valid = .false.
2966 stencil%weight = 0.0_dp
2967 build_weights = .true.
2968 IF (PRESENT(indices_only)) build_weights = .NOT. indices_only
2969 wrap_grid = any(cell%perd == 0)
2970 IF (PRESENT(wrap_auxiliary_cell)) wrap_grid = wrap_auxiliary_cell
2971 relative = matmul(pw_grid%dh_inv, point)
2972 DO idir = 1, 3
2973 IF (cell%perd(idir) == 1 .OR. wrap_grid) THEN
2974 relative(idir) = modulo(relative(idir), real(pw_grid%npts(idir), kind=dp))
2975 ELSE IF (relative(idir) <= -real(native_grid_interp_offset_max, dp) .OR. &
2976 relative(idir) >= real(pw_grid%npts(idir) - &
2978 RETURN
2979 END IF
2980 base(idir) = floor(relative(idir))
2981 fraction(idir) = relative(idir) - real(base(idir), kind=dp)
2982 IF (build_weights) THEN
2983 CALL native_grid_lagrange_weights(fraction(idir), stencil%weight(:, idir))
2984 END IF
2985 DO inode = 1, native_grid_interp_npts
2986 relative_index = base(idir) + native_grid_interp_offset_min + inode - 1
2987 IF (cell%perd(idir) == 1 .OR. wrap_grid) THEN
2988 relative_index = modulo(relative_index, pw_grid%npts(idir))
2989 stencil%valid(inode, idir) = .true.
2990 ELSE IF (relative_index >= 0 .AND. relative_index < pw_grid%npts(idir)) THEN
2991 stencil%valid(inode, idir) = .true.
2992 END IF
2993 stencil%relative_index(inode, idir) = relative_index
2994 END DO
2995 END DO
2996 stencil%active = .true.
2997
2998 END SUBROUTINE create_native_grid_interpolation_stencil
2999
3000! **************************************************************************************************
3001!> \brief Interpolate density, gradient, and kinetic-density fields in one stencil traversal.
3002!> \param density ...
3003!> \param grad_x ...
3004!> \param grad_y ...
3005!> \param grad_z ...
3006!> \param kin ...
3007!> \param stencil ...
3008!> \param density_value ...
3009!> \param grad_value ...
3010!> \param kin_value ...
3011! **************************************************************************************************
3012 SUBROUTINE interpolate_native_grid_fields(density, grad_x, grad_y, grad_z, kin, stencil, &
3013 density_value, grad_value, kin_value)
3014 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: density, grad_x, grad_y, grad_z, kin
3016 INTENT(IN) :: stencil
3017 REAL(kind=dp), INTENT(OUT) :: density_value
3018 REAL(kind=dp), DIMENSION(3), INTENT(OUT) :: grad_value
3019 REAL(kind=dp), INTENT(OUT) :: kin_value
3020
3021 INTEGER :: inode_x, inode_y, inode_z
3022 INTEGER, DIMENSION(3) :: index, lower_bound
3023 REAL(kind=dp) :: coefficient
3024
3025 cpassert(all(shape(grad_x) == shape(density)))
3026 cpassert(all(shape(grad_y) == shape(density)))
3027 cpassert(all(shape(grad_z) == shape(density)))
3028 cpassert(all(shape(kin) == shape(density)))
3029 density_value = 0.0_dp
3030 grad_value = 0.0_dp
3031 kin_value = 0.0_dp
3032 IF (.NOT. stencil%active) RETURN
3033 lower_bound = lbound(density)
3034 DO inode_z = 1, native_grid_interp_npts
3035 IF (.NOT. stencil%valid(inode_z, 3)) cycle
3036 index(3) = lower_bound(3) + stencil%relative_index(inode_z, 3)
3037 DO inode_y = 1, native_grid_interp_npts
3038 IF (.NOT. stencil%valid(inode_y, 2)) cycle
3039 index(2) = lower_bound(2) + stencil%relative_index(inode_y, 2)
3040 DO inode_x = 1, native_grid_interp_npts
3041 IF (.NOT. stencil%valid(inode_x, 1)) cycle
3042 index(1) = lower_bound(1) + stencil%relative_index(inode_x, 1)
3043 coefficient = stencil%weight(inode_x, 1)* &
3044 stencil%weight(inode_y, 2)* &
3045 stencil%weight(inode_z, 3)
3046 density_value = density_value + coefficient*density(index(1), index(2), index(3))
3047 grad_value(1) = grad_value(1) + coefficient*grad_x(index(1), index(2), index(3))
3048 grad_value(2) = grad_value(2) + coefficient*grad_y(index(1), index(2), index(3))
3049 grad_value(3) = grad_value(3) + coefficient*grad_z(index(1), index(2), index(3))
3050 kin_value = kin_value + coefficient*kin(index(1), index(2), index(3))
3051 END DO
3052 END DO
3053 END DO
3054
3055 END SUBROUTINE interpolate_native_grid_fields
3056
3057! **************************************************************************************************
3058!> \brief Group atom-grid rows by the disjoint PW tiles touched by their interpolation stencils.
3059!> \param pw_grid ...
3060!> \param cell ...
3061!> \param points atom-grid coordinates
3062!> \param wrap_auxiliary_cell wrap all auxiliary-grid directions
3063!> \param tile_count number of tiles along each PW-grid direction
3064!> \param bin_offsets CSR offsets into bin_rows
3065!> \param bin_rows atom-grid rows touching each tile
3066! **************************************************************************************************
3067 SUBROUTINE build_native_grid_adjoint_bins(pw_grid, cell, points, wrap_auxiliary_cell, &
3068 tile_count, bin_offsets, bin_rows)
3069 TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
3070 TYPE(cell_type), INTENT(IN), POINTER :: cell
3071 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: points
3072 LOGICAL, INTENT(IN) :: wrap_auxiliary_cell
3073 INTEGER, DIMENSION(3), INTENT(OUT) :: tile_count
3074 INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: bin_offsets, bin_rows
3075
3076 INTEGER :: candidate, ibin, idir, inode, irow, ix, &
3077 iy, iz, nbin, ntouched, position
3078 INTEGER, ALLOCATABLE, DIMENSION(:) :: bin_counts, bin_cursor, row_bin_count
3079 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: row_bins
3080 INTEGER, DIMENSION(3) :: touched_count
3081 INTEGER, DIMENSION(& native_grid_adjoint_max_tiles_per_direction, 3) :: touched_tiles
3083
3084 cpassert(ASSOCIATED(pw_grid))
3085 cpassert(ASSOCIATED(cell))
3086 cpassert(SIZE(points, 1) == 3)
3087 tile_count = (pw_grid%npts + native_grid_adjoint_tile_edge - 1)/ &
3088 native_grid_adjoint_tile_edge
3089 nbin = product(tile_count)
3090 ALLOCATE (row_bin_count(SIZE(points, 2)), &
3091 row_bins(native_grid_adjoint_max_bins_per_row, SIZE(points, 2)))
3092 row_bin_count = 0
3093 row_bins = 0
3094!$OMP PARALLEL DO SCHEDULE(STATIC) DEFAULT(NONE) &
3095!$OMP PRIVATE(candidate, idir, inode, ix, iy, iz, ntouched, stencil, &
3096!$OMP touched_count, touched_tiles) &
3097!$OMP SHARED(cell, points, pw_grid, row_bin_count, row_bins, tile_count, wrap_auxiliary_cell)
3098 DO irow = 1, SIZE(points, 2)
3099 CALL create_native_grid_interpolation_stencil( &
3100 stencil, pw_grid, cell, points(:, irow), wrap_auxiliary_cell, indices_only=.true.)
3101 IF (.NOT. stencil%active) cycle
3102 touched_count = 0
3103 touched_tiles = 0
3104 DO idir = 1, 3
3105 DO inode = 1, native_grid_interp_npts
3106 IF (.NOT. stencil%valid(inode, idir)) cycle
3107 candidate = stencil%relative_index(inode, idir)/native_grid_adjoint_tile_edge + 1
3108 IF (all(touched_tiles(1:touched_count(idir), idir) /= candidate)) THEN
3109 touched_count(idir) = touched_count(idir) + 1
3110 cpassert(touched_count(idir) <= SIZE(touched_tiles, 1))
3111 touched_tiles(touched_count(idir), idir) = candidate
3112 END IF
3113 END DO
3114 END DO
3115 ntouched = 0
3116 DO iz = 1, touched_count(3)
3117 DO iy = 1, touched_count(2)
3118 DO ix = 1, touched_count(1)
3119 ntouched = ntouched + 1
3120 cpassert(ntouched <= native_grid_adjoint_max_bins_per_row)
3121 row_bins(ntouched, irow) = 1 + touched_tiles(ix, 1) - 1 + tile_count(1)*( &
3122 touched_tiles(iy, 2) - 1 + tile_count(2)*( &
3123 touched_tiles(iz, 3) - 1))
3124 END DO
3125 END DO
3126 END DO
3127 row_bin_count(irow) = ntouched
3128 END DO
3129!$OMP END PARALLEL DO
3130
3131 ALLOCATE (bin_counts(nbin), bin_cursor(nbin), bin_offsets(nbin + 1))
3132 bin_counts = 0
3133 DO irow = 1, SIZE(points, 2)
3134 DO ibin = 1, row_bin_count(irow)
3135 bin_counts(row_bins(ibin, irow)) = bin_counts(row_bins(ibin, irow)) + 1
3136 END DO
3137 END DO
3138 bin_offsets(1) = 1
3139 DO ibin = 1, nbin
3140 bin_offsets(ibin + 1) = bin_offsets(ibin) + bin_counts(ibin)
3141 END DO
3142 ALLOCATE (bin_rows(bin_offsets(nbin + 1) - 1))
3143 bin_cursor(:) = bin_offsets(1:nbin)
3144 DO irow = 1, SIZE(points, 2)
3145 DO ibin = 1, row_bin_count(irow)
3146 candidate = row_bins(ibin, irow)
3147 position = bin_cursor(candidate)
3148 bin_rows(position) = irow
3149 bin_cursor(candidate) = position + 1
3150 END DO
3151 END DO
3152 DEALLOCATE (bin_counts, bin_cursor, row_bin_count, row_bins)
3153
3154 END SUBROUTINE build_native_grid_adjoint_bins
3155
3156! **************************************************************************************************
3157!> \brief Return the inclusive PW-grid bounds owned by one linear tile index.
3158!> \param pw_grid ...
3159!> \param tile_index linear tile index
3160!> \param tile_count number of tiles along each PW-grid direction
3161!> \param tile_lower zero-based lower grid index
3162!> \param tile_upper zero-based upper grid index
3163! **************************************************************************************************
3164 SUBROUTINE native_grid_adjoint_tile_bounds(pw_grid, tile_index, tile_count, &
3165 tile_lower, tile_upper)
3166 TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
3167 INTEGER, INTENT(IN) :: tile_index
3168 INTEGER, DIMENSION(3), INTENT(IN) :: tile_count
3169 INTEGER, DIMENSION(3), INTENT(OUT) :: tile_lower, tile_upper
3170
3171 INTEGER :: linear_tile
3172 INTEGER, DIMENSION(3) :: expected_tile_count, tile_coord
3173
3174 cpassert(ASSOCIATED(pw_grid))
3175 expected_tile_count = (pw_grid%npts + native_grid_adjoint_tile_edge - 1)/ &
3176 native_grid_adjoint_tile_edge
3177 cpassert(all(tile_count == expected_tile_count))
3178 cpassert(tile_index >= 1 .AND. tile_index <= product(tile_count))
3179 linear_tile = tile_index - 1
3180 tile_coord(1) = mod(linear_tile, tile_count(1))
3181 linear_tile = linear_tile/tile_count(1)
3182 tile_coord(2) = mod(linear_tile, tile_count(2))
3183 tile_coord(3) = linear_tile/tile_count(2)
3184 tile_lower = tile_coord*native_grid_adjoint_tile_edge
3185 tile_upper = min(tile_lower + native_grid_adjoint_tile_edge - 1, pw_grid%npts - 1)
3186
3187 END SUBROUTINE native_grid_adjoint_tile_bounds
3188
3189! **************************************************************************************************
3190!> \brief Apply the primitive-field interpolation transpose inside one disjoint PW tile.
3191!> \param density ...
3192!> \param grad ...
3193!> \param kin ...
3194!> \param pw_grid ...
3195!> \param stencil ...
3196!> \param density_value ...
3197!> \param grad_value ...
3198!> \param kin_value ...
3199!> \param nchannels number of spin channels accumulated by the adjoint
3200!> \param tile_lower zero-based lower grid index owned by this call
3201!> \param tile_upper zero-based upper grid index owned by this call
3202! **************************************************************************************************
3203 SUBROUTINE add_native_grid_fields_adjoint_tile( &
3204 density, grad, kin, pw_grid, stencil, density_value, grad_value, kin_value, nchannels, &
3205 tile_lower, tile_upper)
3206 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: density
3207 REAL(kind=dp), DIMENSION(:, :, :), INTENT(INOUT) :: grad
3208 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: kin
3209 TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
3211 INTENT(IN) :: stencil
3212 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: density_value
3213 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: grad_value
3214 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: kin_value
3215 INTEGER, INTENT(IN) :: nchannels
3216 INTEGER, DIMENSION(3), INTENT(IN) :: tile_lower, tile_upper
3217
3218 INTEGER :: idir, inode_x, inode_y, inode_z, ipt, &
3219 ispin
3220 INTEGER, DIMENSION(3) :: relative_index
3221 REAL(kind=dp) :: coefficient
3222
3223 cpassert(ASSOCIATED(pw_grid))
3224 cpassert(SIZE(density, 1) == product(pw_grid%npts))
3225 cpassert(SIZE(density, 2) == nchannels)
3226 cpassert(SIZE(grad, 1) == product(pw_grid%npts))
3227 cpassert(SIZE(grad, 2) == 3)
3228 cpassert(SIZE(grad, 3) == nchannels)
3229 cpassert(SIZE(kin, 1) == product(pw_grid%npts))
3230 cpassert(SIZE(kin, 2) == nchannels)
3231 cpassert(SIZE(density_value) == 2)
3232 cpassert(all(shape(grad_value) == [3, 2]))
3233 cpassert(SIZE(kin_value) == 2)
3234 cpassert(nchannels >= 1 .AND. nchannels <= 2)
3235 cpassert(all(tile_lower >= 0))
3236 cpassert(all(tile_upper >= tile_lower))
3237 cpassert(all(tile_upper < pw_grid%npts))
3238 IF (.NOT. stencil%active) RETURN
3239 DO inode_z = 1, native_grid_interp_npts
3240 IF (.NOT. stencil%valid(inode_z, 3)) cycle
3241 relative_index(3) = stencil%relative_index(inode_z, 3)
3242 IF (relative_index(3) < tile_lower(3) .OR. relative_index(3) > tile_upper(3)) cycle
3243 DO inode_y = 1, native_grid_interp_npts
3244 IF (.NOT. stencil%valid(inode_y, 2)) cycle
3245 relative_index(2) = stencil%relative_index(inode_y, 2)
3246 IF (relative_index(2) < tile_lower(2) .OR. relative_index(2) > tile_upper(2)) cycle
3247 DO inode_x = 1, native_grid_interp_npts
3248 IF (.NOT. stencil%valid(inode_x, 1)) cycle
3249 relative_index(1) = stencil%relative_index(inode_x, 1)
3250 IF (relative_index(1) < tile_lower(1) .OR. relative_index(1) > tile_upper(1)) cycle
3251 ipt = 1 + relative_index(1) + pw_grid%npts(1)*( &
3252 relative_index(2) + pw_grid%npts(2)*relative_index(3))
3253 coefficient = stencil%weight(inode_x, 1)* &
3254 stencil%weight(inode_y, 2)* &
3255 stencil%weight(inode_z, 3)
3256 DO ispin = 1, nchannels
3257 density(ipt, ispin) = density(ipt, ispin) + coefficient*density_value(ispin)
3258 DO idir = 1, 3
3259 grad(ipt, idir, ispin) = grad(ipt, idir, ispin) + &
3260 coefficient*grad_value(idir, ispin)
3261 END DO
3262 kin(ipt, ispin) = kin(ipt, ispin) + coefficient*kin_value(ispin)
3263 END DO
3264 END DO
3265 END DO
3266 END DO
3267
3268 END SUBROUTINE add_native_grid_fields_adjoint_tile
3269
3270! **************************************************************************************************
3271!> \brief Interpolate a replicated native-grid field at a Cartesian point.
3272!> \param values ...
3273!> \param pw_grid ...
3274!> \param cell ...
3275!> \param point ...
3276!> \param wrap_auxiliary_cell wrap all auxiliary-grid directions
3277!> \return ...
3278! **************************************************************************************************
3279 FUNCTION interpolate_native_grid(values, pw_grid, cell, point, wrap_auxiliary_cell) RESULT(value)
3280 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: values
3281 TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
3282 TYPE(cell_type), INTENT(IN), POINTER :: cell
3283 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: point
3284 LOGICAL, INTENT(IN), OPTIONAL :: wrap_auxiliary_cell
3285 REAL(kind=dp) :: value
3286
3287 INTEGER :: corner_x, corner_y, corner_z, idir
3288 INTEGER, DIMENSION(3) :: base, index, relative_index
3289 LOGICAL :: wrap_grid
3290 REAL(kind=dp) :: coefficient
3291 REAL(kind=dp), DIMENSION(3) :: fraction, relative
3292 REAL(kind=dp), &
3293 DIMENSION(native_grid_interp_npts, 3) :: weights
3294
3295 cpassert(ASSOCIATED(pw_grid))
3296 cpassert(ASSOCIATED(cell))
3297 DO idir = 1, 3
3298 cpassert(SIZE(values, idir) == pw_grid%npts(idir))
3299 END DO
3300
3301 wrap_grid = any(cell%perd == 0)
3302 IF (PRESENT(wrap_auxiliary_cell)) wrap_grid = wrap_auxiliary_cell
3303 relative = matmul(pw_grid%dh_inv, point)
3304 DO idir = 1, 3
3305 IF (cell%perd(idir) == 1 .OR. wrap_grid) THEN
3306 relative(idir) = modulo(relative(idir), real(pw_grid%npts(idir), kind=dp))
3307 ELSE IF (relative(idir) <= -real(native_grid_interp_offset_max, dp) .OR. &
3308 relative(idir) >= real(pw_grid%npts(idir) - &
3310 value = 0.0_dp
3311 RETURN
3312 END IF
3313 base(idir) = floor(relative(idir))
3314 fraction(idir) = relative(idir) - real(base(idir), kind=dp)
3315 CALL native_grid_lagrange_weights(fraction(idir), weights(:, idir))
3316 END DO
3317
3318 value = 0.0_dp
3322 relative_index = base + [corner_x, corner_y, corner_z]
3323 coefficient = weights(corner_x - native_grid_interp_offset_min + 1, 1)* &
3324 weights(corner_y - native_grid_interp_offset_min + 1, 2)* &
3325 weights(corner_z - native_grid_interp_offset_min + 1, 3)
3326 DO idir = 1, 3
3327 IF (cell%perd(idir) == 1 .OR. wrap_grid) THEN
3328 relative_index(idir) = modulo(relative_index(idir), pw_grid%npts(idir))
3329 ELSE IF (relative_index(idir) < 0 .OR. &
3330 relative_index(idir) >= pw_grid%npts(idir)) THEN
3331 coefficient = 0.0_dp
3332 END IF
3333 END DO
3334 IF (coefficient == 0.0_dp) cycle
3335 index = lbound(values) + relative_index
3336 value = value + coefficient*values(index(1), index(2), index(3))
3337 END DO
3338 END DO
3339 END DO
3340
3341 END FUNCTION interpolate_native_grid
3342
3343! **************************************************************************************************
3344!> \brief Return the Cartesian gradient of native-grid interpolation at one point.
3345!> \param values ...
3346!> \param pw_grid ...
3347!> \param cell ...
3348!> \param point ...
3349!> \param wrap_auxiliary_cell wrap all auxiliary-grid directions
3350!> \return ...
3351! **************************************************************************************************
3352 FUNCTION interpolate_native_grid_gradient(values, pw_grid, cell, point, &
3353 wrap_auxiliary_cell) RESULT(gradient)
3354 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: values
3355 TYPE(pw_grid_type), INTENT(IN), POINTER :: pw_grid
3356 TYPE(cell_type), INTENT(IN), POINTER :: cell
3357 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: point
3358 LOGICAL, INTENT(IN), OPTIONAL :: wrap_auxiliary_cell
3359 REAL(kind=dp), DIMENSION(3) :: gradient
3360
3361 INTEGER :: corner_x, corner_y, corner_z, idir, jdir
3362 INTEGER, DIMENSION(3) :: base, corner_index, index, relative_index
3363 LOGICAL :: wrap_grid
3364 REAL(kind=dp) :: coefficient
3365 REAL(kind=dp), DIMENSION(3) :: fraction, gradient_relative, relative
3366 REAL(kind=dp), &
3367 DIMENSION(native_grid_interp_npts, 3) :: derivative_weights, weights
3368
3369 cpassert(ASSOCIATED(pw_grid))
3370 cpassert(ASSOCIATED(cell))
3371 DO idir = 1, 3
3372 cpassert(SIZE(values, idir) == pw_grid%npts(idir))
3373 END DO
3374
3375 wrap_grid = any(cell%perd == 0)
3376 IF (PRESENT(wrap_auxiliary_cell)) wrap_grid = wrap_auxiliary_cell
3377 relative = matmul(pw_grid%dh_inv, point)
3378 DO idir = 1, 3
3379 IF (cell%perd(idir) == 1 .OR. wrap_grid) THEN
3380 relative(idir) = modulo(relative(idir), real(pw_grid%npts(idir), kind=dp))
3381 ELSE IF (relative(idir) <= -real(native_grid_interp_offset_max, dp) .OR. &
3382 relative(idir) >= real(pw_grid%npts(idir) - &
3384 gradient = 0.0_dp
3385 RETURN
3386 END IF
3387 base(idir) = floor(relative(idir))
3388 fraction(idir) = relative(idir) - real(base(idir), kind=dp)
3389 CALL native_grid_lagrange_weights( &
3390 fraction(idir), weights(:, idir), derivative_weights(:, idir))
3391 END DO
3392
3393 gradient_relative = 0.0_dp
3397 relative_index = base + [corner_x, corner_y, corner_z]
3398 corner_index = [corner_x, corner_y, corner_z] - &
3400 DO idir = 1, 3
3401 IF (cell%perd(idir) == 1 .OR. wrap_grid) THEN
3402 relative_index(idir) = modulo(relative_index(idir), pw_grid%npts(idir))
3403 ELSE IF (relative_index(idir) < 0 .OR. &
3404 relative_index(idir) >= pw_grid%npts(idir)) THEN
3405 EXIT
3406 END IF
3407 END DO
3408 IF (idir <= 3) cycle
3409 index = lbound(values) + relative_index
3410 DO idir = 1, 3
3411 coefficient = 1.0_dp
3412 DO jdir = 1, 3
3413 IF (jdir == idir) THEN
3414 coefficient = coefficient*derivative_weights( &
3415 corner_index(jdir), jdir)
3416 ELSE
3417 coefficient = coefficient*weights( &
3418 corner_index(jdir), jdir)
3419 END IF
3420 END DO
3421 gradient_relative(idir) = gradient_relative(idir) + &
3422 coefficient*values(index(1), index(2), index(3))
3423 END DO
3424 END DO
3425 END DO
3426 END DO
3427 gradient = matmul(transpose(pw_grid%dh_inv), gradient_relative)
3428
3429 END FUNCTION interpolate_native_grid_gradient
3430
3431! **************************************************************************************************
3432!> \brief Build the value and optional derivative weights for the native-grid interpolation.
3433!> \param fraction fractional coordinate between two grid points
3434!> \param weights interpolation weights
3435!> \param derivative_weights optional derivatives with respect to fraction
3436! **************************************************************************************************
3437 SUBROUTINE native_grid_lagrange_weights(fraction, weights, derivative_weights)
3438 REAL(kind=dp), INTENT(IN) :: fraction
3439 REAL(kind=dp), DIMENSION(:), INTENT(OUT) :: weights
3440 REAL(kind=dp), DIMENSION(:), INTENT(OUT), OPTIONAL :: derivative_weights
3441
3442 INTEGER :: inode, jnode, knode, npoints
3443 REAL(kind=dp) :: denominator, derivative, numerator
3444 REAL(kind=dp), DIMENSION(SIZE(weights)) :: nodes
3445
3446 npoints = SIZE(weights)
3447 IF (PRESENT(derivative_weights)) THEN
3448 cpassert(SIZE(derivative_weights) == npoints)
3449 END IF
3450 DO inode = 1, npoints
3451 nodes(inode) = real(native_grid_interp_offset_min + inode - 1, dp)
3452 END DO
3453 DO inode = 1, npoints
3454 denominator = 1.0_dp
3455 numerator = 1.0_dp
3456 DO jnode = 1, npoints
3457 IF (jnode == inode) cycle
3458 denominator = denominator*(nodes(inode) - nodes(jnode))
3459 numerator = numerator*(fraction - nodes(jnode))
3460 END DO
3461 weights(inode) = numerator/denominator
3462 IF (PRESENT(derivative_weights)) THEN
3463 derivative = 0.0_dp
3464 DO knode = 1, npoints
3465 IF (knode == inode) cycle
3466 numerator = 1.0_dp
3467 DO jnode = 1, npoints
3468 IF (jnode == inode .OR. jnode == knode) cycle
3469 numerator = numerator*(fraction - nodes(jnode))
3470 END DO
3471 derivative = derivative + numerator/denominator
3472 END DO
3473 derivative_weights(inode) = derivative
3474 END IF
3475 END DO
3476
3477 END SUBROUTINE native_grid_lagrange_weights
3478
3479! **************************************************************************************************
3480!> \brief Conservatively bound periodic images that can intersect a Cartesian support sphere.
3481!> \param cell current cell and periodicity
3482!> \param fractional target-minus-source displacement in lattice coordinates
3483!> \param cutoff current, density-dependent Cartesian support radius
3484!> \param image_shell original enclosing image shell
3485!> \param lower inclusive lower image indices
3486!> \param upper inclusive upper image indices (may be smaller than lower)
3487! **************************************************************************************************
3488 SUBROUTINE atom_grid_image_bounds(cell, fractional, cutoff, image_shell, lower, upper)
3489 TYPE(cell_type), INTENT(IN), POINTER :: cell
3490 REAL(dp), INTENT(IN) :: fractional(3), cutoff
3491 INTEGER, INTENT(IN) :: image_shell(3)
3492 INTEGER, INTENT(OUT) :: lower(3), upper(3)
3493
3494 INTEGER :: center, idir
3495 REAL(dp) :: condition_bound, padding, radius
3496
3497 ! |(H^-1 d)_i| <= cutoff ||(H^-1)_i|| also holds for triclinic cells.
3498 ! Inflate the bounds for cancellation in fractional coordinates and the inverse cell.
3499 condition_bound = max(1.0_dp, sum(abs(cell%hmat))*sum(abs(cell%h_inv)))
3500 lower = 0
3501 upper = 0
3502 DO idir = 1, 3
3503 IF (cell%perd(idir) == 0) cycle
3504 center = nint(fractional(idir))
3505 radius = cutoff*sqrt(sum(cell%h_inv(idir, :)**2))
3506 padding = 128.0_dp*epsilon(1.0_dp)*condition_bound* &
3507 max(1.0_dp, maxval(abs(fractional)), radius)
3508 lower(idir) = max(center - image_shell(idir), ceiling(fractional(idir) - radius - padding))
3509 upper(idir) = min(center + image_shell(idir), floor(fractional(idir) + radius + padding))
3510 END DO
3511 END SUBROUTINE atom_grid_image_bounds
3512
3513! **************************************************************************************************
3514!> \brief Express one radial node's first and second derivatives as linear combinations of the
3515!> local node values used by the C2-continuous quintic Hermite interpolant.
3516!> \param grid_atom radial source grid
3517!> \param descending whether radial nodes are stored in descending order
3518!> \param node logical index of the node whose derivatives are required
3519!> \param logical_start first logical index represented by the coefficient arrays
3520!> \param slope coefficients of the first derivative
3521!> \param curvature coefficients of the second derivative
3522! **************************************************************************************************
3523 SUBROUTINE radial_node_derivative_coefficients( &
3524 grid_atom, descending, node, logical_start, slope, curvature)
3525 TYPE(grid_atom_type), POINTER :: grid_atom
3526 LOGICAL, INTENT(IN) :: descending
3527 INTEGER, INTENT(IN) :: node, logical_start
3528 REAL(dp), DIMENSION(4), INTENT(OUT) :: slope, curvature
3529
3530 INTEGER :: center, hi, lo, n
3531 REAL(dp) :: h_hi, h_lo, x_center, x_hi, x_lo
3532
3533 n = grid_atom%nr
3534 slope = 0.0_dp
3535 curvature = 0.0_dp
3536 IF (node == 1) THEN
3537 lo = 1
3538 hi = 2
3539 ELSE IF (node == n) THEN
3540 lo = n - 1
3541 hi = n
3542 ELSE
3543 lo = node - 1
3544 hi = node + 1
3545 END IF
3546 x_lo = grid_atom%rad(merge(n + 1 - lo, lo, descending))
3547 x_hi = grid_atom%rad(merge(n + 1 - hi, hi, descending))
3548 slope(lo - logical_start + 1) = -1.0_dp/(x_hi - x_lo)
3549 slope(hi - logical_start + 1) = 1.0_dp/(x_hi - x_lo)
3550
3551 IF (n < 3) RETURN
3552 center = min(max(node, 2), n - 1)
3553 lo = center - 1
3554 hi = center + 1
3555 x_lo = grid_atom%rad(merge(n + 1 - lo, lo, descending))
3556 x_center = grid_atom%rad(merge(n + 1 - center, center, descending))
3557 x_hi = grid_atom%rad(merge(n + 1 - hi, hi, descending))
3558 h_lo = x_center - x_lo
3559 h_hi = x_hi - x_center
3560 curvature(lo - logical_start + 1) = 2.0_dp/(h_lo*(h_lo + h_hi))
3561 curvature(center - logical_start + 1) = &
3562 -2.0_dp*(1.0_dp/h_lo + 1.0_dp/h_hi)/(h_lo + h_hi)
3563 curvature(hi - logical_start + 1) = 2.0_dp/(h_hi*(h_lo + h_hi))
3564
3565 END SUBROUTINE radial_node_derivative_coefficients
3566
3567! **************************************************************************************************
3568!> \brief Build mutually consistent value and Cartesian-derivative weights for interpolation from
3569!> a GAPW radial/Lebedev atom grid to one arbitrary local displacement.
3570!> \param grid_atom source atom grid
3571!> \param harmonics source spherical harmonics
3572!> \param displacement point minus source-image coordinate
3573!> \param cutoff compact support radius
3574!> \param radial_indices active radial nodes
3575!> \param radial_weights interpolation weights on the active radial nodes
3576!> \param radial_derivative_weights optional radial derivatives of the interpolation weights
3577!> \param nradial number of active radial nodes
3578!> \param angular_weights angular interpolation weights
3579!> \param angular_derivative_weights optional Cartesian derivatives of the angular weights
3580!> \param active whether the point lies inside the compact support
3581! **************************************************************************************************
3582 SUBROUTINE atom_grid_interpolation_weights( &
3583 grid_atom, harmonics, displacement, cutoff, radial_indices, radial_weights, &
3584 radial_derivative_weights, nradial, angular_weights, angular_derivative_weights, active)
3585 TYPE(grid_atom_type), POINTER :: grid_atom
3586 TYPE(harmonics_atom_type), POINTER :: harmonics
3587 REAL(dp), DIMENSION(3), INTENT(IN) :: displacement
3588 REAL(dp), INTENT(IN) :: cutoff
3589 INTEGER, DIMENSION(4), INTENT(OUT) :: radial_indices
3590 REAL(dp), DIMENSION(4), INTENT(OUT) :: radial_weights
3591 REAL(dp), DIMENSION(4), INTENT(OUT), OPTIONAL :: radial_derivative_weights
3592 INTEGER, INTENT(OUT) :: nradial
3593 REAL(dp), DIMENSION(:), INTENT(OUT) :: angular_weights
3594 REAL(dp), DIMENSION(:, :), INTENT(OUT), OPTIONAL :: angular_derivative_weights
3595 LOGICAL, INTENT(OUT) :: active
3596
3597 INTEGER :: ia, ic, inode, iso, l, left, left_pos, &
3598 logical_end, logical_start, lx, ly, &
3599 lz, n, right_pos, shell_index
3600 LOGICAL :: descending
3601 REAL(dp) :: dh00, dh01, dh10, dh11, dh20, dh21, h00, &
3602 h01, h10, h11, h20, h21, h_interval, &
3603 monomial, radius, solid_derivative, t, &
3604 x1, x2
3605 REAL(dp), DIMENSION(3) :: direction
3606 REAL(dp), DIMENSION(4) :: curvature_left, curvature_right, &
3607 slope_left, slope_right
3608 REAL(dp), DIMENSION(harmonics%max_s_harm) :: angular_values
3609 REAL(dp), DIMENSION(3, harmonics%max_s_harm) :: angular_value_derivatives
3610
3611 cpassert(ASSOCIATED(grid_atom))
3612 cpassert(ASSOCIATED(harmonics))
3613 cpassert(SIZE(angular_weights) == grid_atom%ng_sphere)
3614 IF (PRESENT(angular_derivative_weights)) THEN
3615 cpassert(SIZE(angular_derivative_weights, 1) == 3)
3616 cpassert(SIZE(angular_derivative_weights, 2) == grid_atom%ng_sphere)
3617 END IF
3618
3619 radial_indices = 0
3620 radial_weights = 0.0_dp
3621 IF (PRESENT(radial_derivative_weights)) radial_derivative_weights = 0.0_dp
3622 angular_weights = 0.0_dp
3623 IF (PRESENT(angular_derivative_weights)) angular_derivative_weights = 0.0_dp
3624 nradial = 0
3625 active = .false.
3626 n = grid_atom%nr
3627 descending = grid_atom%rad(1) > grid_atom%rad(n)
3628 radius = sqrt(sum(displacement**2))
3629 IF (radius > cutoff .OR. &
3630 radius > grid_atom%rad(merge(1, n, descending))) RETURN
3631 IF (radius <= 1.0e-12_dp) RETURN
3632 direction = displacement/radius
3633
3634 cpassert(n >= 2)
3635 left = n - 1
3636 IF (radius <= grid_atom%rad(merge(n, 1, descending))) THEN
3637 left = 1
3638 ELSE
3639 DO inode = 1, n - 1
3640 IF (radius <= grid_atom%rad(merge(n - inode, inode + 1, descending))) THEN
3641 left = inode
3642 EXIT
3643 END IF
3644 END DO
3645 END IF
3646
3647 logical_start = max(1, left - 1)
3648 logical_end = min(n, left + 2)
3649 nradial = logical_end - logical_start + 1
3650 DO inode = 1, nradial
3651 radial_indices(inode) = merge(n + 2 - logical_start - inode, &
3652 logical_start + inode - 1, descending)
3653 END DO
3654 left_pos = left - logical_start + 1
3655 right_pos = left_pos + 1
3656 x1 = grid_atom%rad(radial_indices(left_pos))
3657 x2 = grid_atom%rad(radial_indices(right_pos))
3658 h_interval = x2 - x1
3659 t = (radius - x1)/h_interval
3660
3661 h00 = 1.0_dp - 10.0_dp*t**3 + 15.0_dp*t**4 - 6.0_dp*t**5
3662 h10 = t - 6.0_dp*t**3 + 8.0_dp*t**4 - 3.0_dp*t**5
3663 h20 = 0.5_dp*(t**2 - 3.0_dp*t**3 + 3.0_dp*t**4 - t**5)
3664 h01 = 10.0_dp*t**3 - 15.0_dp*t**4 + 6.0_dp*t**5
3665 h11 = -4.0_dp*t**3 + 7.0_dp*t**4 - 3.0_dp*t**5
3666 h21 = 0.5_dp*(t**3 - 2.0_dp*t**4 + t**5)
3667
3668 CALL radial_node_derivative_coefficients( &
3669 grid_atom, descending, left, logical_start, slope_left, curvature_left)
3670 CALL radial_node_derivative_coefficients( &
3671 grid_atom, descending, left + 1, logical_start, slope_right, curvature_right)
3672 radial_weights(left_pos) = radial_weights(left_pos) + h00
3673 radial_weights(right_pos) = radial_weights(right_pos) + h01
3674 radial_weights(1:nradial) = radial_weights(1:nradial) + &
3675 h_interval*(h10*slope_left(1:nradial) + &
3676 h11*slope_right(1:nradial)) + &
3677 h_interval**2*(h20*curvature_left(1:nradial) + &
3678 h21*curvature_right(1:nradial))
3679 IF (PRESENT(radial_derivative_weights)) THEN
3680 dh00 = -30.0_dp*t**2 + 60.0_dp*t**3 - 30.0_dp*t**4
3681 dh10 = 1.0_dp - 18.0_dp*t**2 + 32.0_dp*t**3 - 15.0_dp*t**4
3682 dh20 = 0.5_dp*(2.0_dp*t - 9.0_dp*t**2 + 12.0_dp*t**3 - 5.0_dp*t**4)
3683 dh01 = 30.0_dp*t**2 - 60.0_dp*t**3 + 30.0_dp*t**4
3684 dh11 = -12.0_dp*t**2 + 28.0_dp*t**3 - 15.0_dp*t**4
3685 dh21 = 0.5_dp*(3.0_dp*t**2 - 8.0_dp*t**3 + 5.0_dp*t**4)
3686 radial_derivative_weights(left_pos) = &
3687 radial_derivative_weights(left_pos) + dh00/h_interval
3688 radial_derivative_weights(right_pos) = &
3689 radial_derivative_weights(right_pos) + dh01/h_interval
3690 radial_derivative_weights(1:nradial) = radial_derivative_weights(1:nradial) + &
3691 dh10*slope_left(1:nradial) + &
3692 dh11*slope_right(1:nradial) + &
3693 h_interval*(dh20*curvature_left(1:nradial) + &
3694 dh21*curvature_right(1:nradial))
3695 END IF
3696
3697 angular_values = 0.0_dp
3698 IF (PRESENT(angular_derivative_weights)) angular_value_derivatives = 0.0_dp
3699 DO iso = 1, harmonics%max_s_harm
3700 l = indso(1, iso)
3701 CALL y_lm(direction, angular_values(iso), l, indso(2, iso))
3702 IF (l == 0 .OR. .NOT. PRESENT(angular_derivative_weights)) cycle
3703 shell_index = iso - nsoset(l - 1)
3704 DO ic = 1, nco(l)
3705 lx = indco(1, ic + ncoset(l - 1))
3706 ly = indco(2, ic + ncoset(l - 1))
3707 lz = indco(3, ic + ncoset(l - 1))
3708 IF (lx > 0) THEN
3709 monomial = real(lx, dp)*direction(1)**(lx - 1)* &
3710 direction(2)**ly*direction(3)**lz
3711 angular_value_derivatives(1, iso) = angular_value_derivatives(1, iso) + &
3712 orbtramat(l)%slm(shell_index, ic)*monomial
3713 END IF
3714 IF (ly > 0) THEN
3715 monomial = direction(1)**lx*real(ly, dp)*direction(2)**(ly - 1)* &
3716 direction(3)**lz
3717 angular_value_derivatives(2, iso) = angular_value_derivatives(2, iso) + &
3718 orbtramat(l)%slm(shell_index, ic)*monomial
3719 END IF
3720 IF (lz > 0) THEN
3721 monomial = direction(1)**lx*direction(2)**ly* &
3722 REAL(lz, dp)*direction(3)**(lz - 1)
3723 angular_value_derivatives(3, iso) = angular_value_derivatives(3, iso) + &
3724 orbtramat(l)%slm(shell_index, ic)*monomial
3725 END IF
3726 END DO
3727 DO inode = 1, 3
3728 solid_derivative = angular_value_derivatives(inode, iso)
3729 angular_value_derivatives(inode, iso) = (solid_derivative - &
3730 REAL(l, dp)*angular_values(iso)* &
3731 direction(inode))/radius
3732 END DO
3733 END DO
3734
3735 DO ia = 1, grid_atom%ng_sphere
3736 angular_weights(ia) = grid_atom%wa(ia)* &
3737 dot_product(harmonics%slm(ia, 1:harmonics%max_s_harm), &
3738 angular_values)
3739 IF (PRESENT(angular_derivative_weights)) THEN
3740 DO inode = 1, 3
3741 angular_derivative_weights(inode, ia) = grid_atom%wa(ia)* &
3742 dot_product(harmonics%slm(ia, 1:harmonics%max_s_harm), &
3743 angular_value_derivatives(inode, :))
3744 END DO
3745 END IF
3746 END DO
3747 active = .true.
3748
3749 END SUBROUTINE atom_grid_interpolation_weights
3750
3751! **************************************************************************************************
3752!> \brief Interpolate hard-minus-soft primitive fields and their spatial derivatives from one
3753!> GAPW atom grid.
3754!> \param grid_atom radial and angular source grid
3755!> \param harmonics spherical-harmonic representation of the source grid
3756!> \param displacement target point relative to the source-atom image
3757!> \param cutoff compact support radius of the source fields
3758!> \param nspins number of spin channels
3759!> \param rho_h hard one-center density values
3760!> \param rho_s soft one-center density values
3761!> \param drho_h hard one-center density-gradient values
3762!> \param drho_s soft one-center density-gradient values
3763!> \param tau_h hard one-center kinetic-energy-density values
3764!> \param tau_s soft one-center kinetic-energy-density values
3765!> \param density interpolated hard-minus-soft density
3766!> \param gradient interpolated hard-minus-soft density gradient
3767!> \param kin interpolated hard-minus-soft kinetic-energy density
3768!> \param density_spatial Cartesian derivatives of density
3769!> \param gradient_spatial Cartesian derivatives of the density gradient
3770!> \param kin_spatial Cartesian derivatives of the kinetic-energy density
3771!> \param calculate_spatial evaluate spatial derivatives (default true), otherwise return zeros
3772! **************************************************************************************************
3774 grid_atom, harmonics, displacement, cutoff, nspins, rho_h, rho_s, drho_h, drho_s, &
3775 tau_h, tau_s, density, gradient, kin, density_spatial, gradient_spatial, kin_spatial, calculate_spatial)
3776 TYPE(grid_atom_type), POINTER :: grid_atom
3777 TYPE(harmonics_atom_type), POINTER :: harmonics
3778 REAL(dp), DIMENSION(3), INTENT(IN) :: displacement
3779 REAL(dp), INTENT(IN) :: cutoff
3780 INTEGER, INTENT(IN) :: nspins
3781 REAL(dp), DIMENSION(:, :, :), INTENT(IN) :: rho_h, rho_s
3782 REAL(dp), DIMENSION(:, :, :, :), INTENT(IN) :: drho_h, drho_s
3783 REAL(dp), DIMENSION(:, :, :), INTENT(IN) :: tau_h, tau_s
3784 REAL(dp), DIMENSION(2), INTENT(OUT) :: density
3785 REAL(dp), DIMENSION(3, 2), INTENT(OUT) :: gradient
3786 REAL(dp), DIMENSION(2), INTENT(OUT) :: kin
3787 REAL(dp), DIMENSION(3, 2), INTENT(OUT) :: density_spatial
3788 REAL(dp), DIMENSION(3, 3, 2), INTENT(OUT) :: gradient_spatial
3789 REAL(dp), DIMENSION(3, 2), INTENT(OUT) :: kin_spatial
3790 LOGICAL, INTENT(IN), OPTIONAL :: calculate_spatial
3791
3792 INTEGER :: ia, idir, inode, ir, ispin, jdir, nradial
3793 INTEGER, DIMENSION(4) :: radial_indices
3794 LOGICAL :: active, need_spatial
3795 REAL(dp) :: derivative_weight, weight
3796 REAL(dp), DIMENSION(4) :: radial_derivative_weights, radial_weights
3797 REAL(dp), DIMENSION(grid_atom%ng_sphere) :: angular_weights
3798 REAL(dp), DIMENSION(3, grid_atom%ng_sphere) :: angular_derivative_weights
3799
3800 density = 0.0_dp
3801 gradient = 0.0_dp
3802 kin = 0.0_dp
3803 density_spatial = 0.0_dp
3804 gradient_spatial = 0.0_dp
3805 kin_spatial = 0.0_dp
3806 need_spatial = .true.
3807 IF (PRESENT(calculate_spatial)) need_spatial = calculate_spatial
3808 IF (need_spatial) THEN
3809 CALL atom_grid_interpolation_weights( &
3810 grid_atom, harmonics, displacement, cutoff, radial_indices, radial_weights, &
3811 radial_derivative_weights, nradial, angular_weights, angular_derivative_weights, active)
3812 ELSE
3813 CALL atom_grid_interpolation_weights( &
3814 grid_atom, harmonics, displacement, cutoff, radial_indices, radial_weights, &
3815 nradial=nradial, angular_weights=angular_weights, active=active)
3816 END IF
3817 IF (active) THEN
3818 DO inode = 1, nradial
3819 ir = radial_indices(inode)
3820 DO ia = 1, grid_atom%ng_sphere
3821 weight = radial_weights(inode)*angular_weights(ia)
3822 DO ispin = 1, nspins
3823 density(ispin) = density(ispin) + &
3824 weight*(rho_h(ia, ir, ispin) - rho_s(ia, ir, ispin))
3825 kin(ispin) = kin(ispin) + &
3826 weight*(tau_h(ia, ir, ispin) - tau_s(ia, ir, ispin))
3827 DO idir = 1, 3
3828 gradient(idir, ispin) = gradient(idir, ispin) + &
3829 weight*(drho_h(idir, ia, ir, ispin) - &
3830 drho_s(idir, ia, ir, ispin))
3831 IF (.NOT. need_spatial) cycle
3832 derivative_weight = radial_derivative_weights(inode)* &
3833 displacement(idir)/sqrt(sum(displacement**2))* &
3834 angular_weights(ia) + radial_weights(inode)* &
3835 angular_derivative_weights(idir, ia)
3836 density_spatial(idir, ispin) = density_spatial(idir, ispin) + &
3837 derivative_weight*(rho_h(ia, ir, ispin) - rho_s(ia, ir, ispin))
3838 kin_spatial(idir, ispin) = kin_spatial(idir, ispin) + &
3839 derivative_weight*(tau_h(ia, ir, ispin) - tau_s(ia, ir, ispin))
3840 DO jdir = 1, 3
3841 gradient_spatial(jdir, idir, ispin) = &
3842 gradient_spatial(jdir, idir, ispin) + derivative_weight*( &
3843 drho_h(jdir, ia, ir, ispin) - drho_s(jdir, ia, ir, ispin))
3844 END DO
3845 END DO
3846 END DO
3847 END DO
3848 END DO
3849 END IF
3851
3852! **************************************************************************************************
3853!> \brief Apply the exact transpose of interpolate_gapw_atom_grid_fields to one-center potentials.
3854!> \param grid_atom radial and angular source grid
3855!> \param harmonics spherical-harmonic representation of the source grid
3856!> \param displacement target point relative to the source-atom image
3857!> \param cutoff compact support radius of the source fields
3858!> \param nspins number of spin channels
3859!> \param density_adjoint model derivative with respect to density
3860!> \param gradient_adjoint model derivative with respect to the density gradient
3861!> \param kin_adjoint model derivative with respect to kinetic-energy density
3862!> \param vxc_h accumulated hard one-center density potential
3863!> \param vxc_s accumulated soft one-center density potential
3864!> \param vxg_h accumulated hard one-center density-gradient potential
3865!> \param vxg_s accumulated soft one-center density-gradient potential
3866!> \param vtau_h accumulated hard one-center kinetic-energy-density potential
3867!> \param vtau_s accumulated soft one-center kinetic-energy-density potential
3868! **************************************************************************************************
3870 grid_atom, harmonics, displacement, cutoff, nspins, density_adjoint, gradient_adjoint, &
3871 kin_adjoint, vxc_h, vxc_s, vxg_h, vxg_s, vtau_h, vtau_s)
3872 TYPE(grid_atom_type), POINTER :: grid_atom
3873 TYPE(harmonics_atom_type), POINTER :: harmonics
3874 REAL(dp), DIMENSION(3), INTENT(IN) :: displacement
3875 REAL(dp), INTENT(IN) :: cutoff
3876 INTEGER, INTENT(IN) :: nspins
3877 REAL(dp), DIMENSION(2), INTENT(IN) :: density_adjoint
3878 REAL(dp), DIMENSION(3, 2), INTENT(IN) :: gradient_adjoint
3879 REAL(dp), DIMENSION(2), INTENT(IN) :: kin_adjoint
3880 REAL(dp), DIMENSION(:, :, :), INTENT(INOUT) :: vxc_h, vxc_s
3881 REAL(dp), DIMENSION(:, :, :, :), INTENT(INOUT) :: vxg_h, vxg_s
3882 REAL(dp), DIMENSION(:, :, :), INTENT(INOUT) :: vtau_h, vtau_s
3883
3884 INTEGER :: ia, idir, inode, ir, ispin, nradial
3885 INTEGER, DIMENSION(4) :: radial_indices
3886 LOGICAL :: active
3887 REAL(dp) :: value
3888 REAL(dp), DIMENSION(4) :: radial_weights
3889 REAL(dp), DIMENSION(grid_atom%ng_sphere) :: angular_weights
3890
3891 CALL atom_grid_interpolation_weights( &
3892 grid_atom, harmonics, displacement, cutoff, radial_indices, radial_weights, &
3893 nradial=nradial, angular_weights=angular_weights, active=active)
3894 IF (active) THEN
3895 DO inode = 1, nradial
3896 ir = radial_indices(inode)
3897 DO ia = 1, grid_atom%ng_sphere
3898 value = radial_weights(inode)*angular_weights(ia)
3899 DO ispin = 1, nspins
3900 ! CP2K applies the hard-minus-soft sign when the two one-center
3901 ! matrices are assembled, so both stored potentials carry the
3902 ! same transpose-interpolation coefficient.
3903 vxc_h(ia, ir, ispin) = vxc_h(ia, ir, ispin) + value*density_adjoint(ispin)
3904 vxc_s(ia, ir, ispin) = vxc_s(ia, ir, ispin) + value*density_adjoint(ispin)
3905 vtau_h(ia, ir, ispin) = vtau_h(ia, ir, ispin) + value*kin_adjoint(ispin)
3906 vtau_s(ia, ir, ispin) = vtau_s(ia, ir, ispin) + value*kin_adjoint(ispin)
3907 DO idir = 1, 3
3908 vxg_h(idir, ia, ir, ispin) = vxg_h(idir, ia, ir, ispin) + &
3909 value*gradient_adjoint(idir, ispin)
3910 vxg_s(idir, ia, ir, ispin) = vxg_s(idir, ia, ir, ispin) + &
3911 value*gradient_adjoint(idir, ispin)
3912 END DO
3913 END DO
3914 END DO
3915 END DO
3916 END IF
3918
3919! **************************************************************************************************
3920!> \brief ...
3921!> \param qs_env ...
3922!> \param exc1 the on-body ex energy contribution
3923!> \param gradient_atom_set ...
3924! **************************************************************************************************
3925 SUBROUTINE calculate_vxc_atom_epr(qs_env, exc1, gradient_atom_set)
3927 TYPE(qs_environment_type), POINTER :: qs_env
3928 REAL(dp), INTENT(INOUT) :: exc1
3929 TYPE(nablavks_atom_type), DIMENSION(:), POINTER :: gradient_atom_set
3930
3931 CHARACTER(LEN=*), PARAMETER :: routinen = 'calculate_vxc_atom_epr'
3932
3933 INTEGER :: bo(2), handle, ia, iat, iatom, idir, &
3934 ikind, ir, ispin, myfun, na, natom, &
3935 nr, nspins, num_pe, on
3936 INTEGER, DIMENSION(2, 3) :: bounds
3937 INTEGER, DIMENSION(:), POINTER :: atom_list
3938 LOGICAL :: accint, donlcc, gradient_f, lsd, nlcc, &
3939 paw_atom, tau_f
3940 REAL(dp) :: agr, alpha, density_cut, exc_h, exc_s, &
3941 gradient_cut, tau_cut
3942 REAL(dp), ALLOCATABLE, DIMENSION(:) :: fw
3943 REAL(dp), DIMENSION(1, 1, 1) :: tau_d
3944 REAL(dp), DIMENSION(1, 1, 1, 1) :: rho_d
3945 REAL(dp), DIMENSION(:, :), POINTER :: rho_nlcc, weight_h, weight_s
3946 REAL(dp), DIMENSION(:, :, :), POINTER :: rho_h, rho_s, tau_h, tau_s, vtau_h, &
3947 vtau_s, vxc_h, vxc_s
3948 REAL(dp), DIMENSION(:, :, :, :), POINTER :: drho_h, drho_s, vxg_h, vxg_s
3949 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
3950 TYPE(dft_control_type), POINTER :: dft_control
3951 TYPE(grid_atom_type), POINTER :: grid_atom
3952 TYPE(gto_basis_set_type), POINTER :: basis_1c
3953 TYPE(harmonics_atom_type), POINTER :: harmonics
3954 TYPE(mp_para_env_type), POINTER :: para_env
3955 TYPE(qs_kind_type), DIMENSION(:), POINTER :: my_kind_set
3956 TYPE(rho_atom_coeff), DIMENSION(:), POINTER :: dr_h, dr_s, int_hh, int_ss, r_h, r_s
3957 TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER :: r_h_d, r_s_d
3958 TYPE(rho_atom_type), DIMENSION(:), POINTER :: my_rho_atom_set
3959 TYPE(rho_atom_type), POINTER :: rho_atom
3960 TYPE(section_vals_type), POINTER :: input, my_xc_section, xc_fun_section
3961 TYPE(tau_basis_cache_type) :: tau_basis_cache
3962 TYPE(xc_derivative_set_type) :: deriv_set
3963 TYPE(xc_rho_cflags_type) :: needs
3964 TYPE(xc_rho_set_type) :: rho_set_h, rho_set_s
3965
3966! -------------------------------------------------------------------------
3967
3968 CALL timeset(routinen, handle)
3969
3970 NULLIFY (atom_list)
3971 NULLIFY (my_kind_set)
3972 NULLIFY (atomic_kind_set)
3973 NULLIFY (grid_atom)
3974 NULLIFY (harmonics)
3975 NULLIFY (input)
3976 NULLIFY (para_env)
3977 NULLIFY (rho_atom)
3978 NULLIFY (my_rho_atom_set)
3979 NULLIFY (rho_nlcc)
3980
3981 CALL get_qs_env(qs_env=qs_env, &
3982 dft_control=dft_control, &
3983 para_env=para_env, &
3984 atomic_kind_set=atomic_kind_set, &
3985 qs_kind_set=my_kind_set, &
3986 input=input, &
3987 rho_atom_set=my_rho_atom_set)
3988
3989 nlcc = has_nlcc(my_kind_set)
3990 accint = dft_control%qs_control%gapw_control%accurate_xcint
3991
3992 my_xc_section => section_vals_get_subs_vals(input, &
3993 "PROPERTIES%LINRES%EPR%PRINT%G_TENSOR%XC")
3994 xc_fun_section => section_vals_get_subs_vals(my_xc_section, "XC_FUNCTIONAL")
3995 CALL section_vals_val_get(xc_fun_section, "_SECTION_PARAMETERS_", &
3996 i_val=myfun)
3997
3998 IF (myfun == xc_none) THEN
3999 exc1 = 0.0_dp
4000 my_rho_atom_set(:)%exc_h = 0.0_dp
4001 my_rho_atom_set(:)%exc_s = 0.0_dp
4002 ELSE
4003 CALL section_vals_val_get(my_xc_section, "DENSITY_CUTOFF", &
4004 r_val=density_cut)
4005 CALL section_vals_val_get(my_xc_section, "GRADIENT_CUTOFF", &
4006 r_val=gradient_cut)
4007 CALL section_vals_val_get(my_xc_section, "TAU_CUTOFF", &
4008 r_val=tau_cut)
4009
4010 lsd = dft_control%lsd
4011 nspins = dft_control%nspins
4012 needs = xc_functionals_get_needs(xc_fun_section, &
4013 lsd=lsd, &
4014 calc_potential=.true.)
4015
4016 ! whatever the xc, if epr_xc, drho_spin is needed
4017 needs%drho_spin = .true.
4018
4019 gradient_f = (needs%drho .OR. needs%drho_spin)
4020 tau_f = (needs%tau .OR. needs%tau_spin)
4021
4022 ! Initialize energy contribution from the one center XC terms to zero
4023 exc1 = 0.0_dp
4024
4025 ! Nullify some pointers for work-arrays
4026 NULLIFY (rho_h, drho_h, rho_s, drho_s, weight_h, weight_s)
4027 NULLIFY (vxc_h, vxc_s, vxg_h, vxg_s)
4028 NULLIFY (tau_h, tau_s)
4029 NULLIFY (vtau_h, vtau_s)
4030
4031 ! Here starts the loop over all the atoms
4032
4033 DO ikind = 1, SIZE(atomic_kind_set)
4034 CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
4035 CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
4036 harmonics=harmonics, grid_atom=grid_atom)
4037 CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
4038
4039 IF (.NOT. paw_atom) cycle
4040
4041 nr = grid_atom%nr
4042 na = grid_atom%ng_sphere
4043
4044 ! Prepare the structures needed to calculate and store the xc derivatives
4045
4046 ! Array dimension: here anly one dimensional arrays are used,
4047 ! i.e. only the first column of deriv_data is read.
4048 ! The other to dimensions are set to size equal 1
4049 bounds(1:2, 1:3) = 1
4050 bounds(2, 1) = na
4051 bounds(2, 2) = nr
4052
4053 ! set integration weights
4054 weight_h => grid_atom%weight
4055 weight_s => grid_atom%weight
4056 IF (accint) THEN
4057 on = dft_control%qs_control%gapw_control%oweights
4058 alpha = dft_control%qs_control%gapw_control%aw(ikind)
4059 IF (ASSOCIATED(grid_atom%gapw_weight_s)) THEN
4060 IF (grid_atom%gapw_weight_alpha /= alpha) DEALLOCATE (grid_atom%gapw_weight_s)
4061 END IF
4062 IF (.NOT. ASSOCIATED(grid_atom%gapw_weight_s)) THEN
4063 ALLOCATE (grid_atom%gapw_weight_s(na, nr))
4064 ALLOCATE (fw(nr))
4065 CALL calc_weight_function(fw, grid_atom%rad2, on, alpha)
4066 DO ir = 1, nr
4067 agr = 1.0_dp - fw(ir)
4068 grid_atom%gapw_weight_s(:, ir) = agr*grid_atom%weight(:, ir)
4069 END DO
4070 DEALLOCATE (fw)
4071 grid_atom%gapw_weight_alpha = alpha
4072 END IF
4073 weight_s => grid_atom%gapw_weight_s
4074 END IF
4075
4076 ! create a place where to put the derivatives
4077 CALL xc_dset_create(deriv_set, local_bounds=bounds)
4078 ! create the place where to store the argument for the functionals
4079 CALL xc_rho_set_create(rho_set_h, bounds, rho_cutoff=density_cut, &
4080 drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
4081 CALL xc_rho_set_create(rho_set_s, bounds, rho_cutoff=density_cut, &
4082 drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
4083
4084 ! allocate the required 3d arrays where to store rho and drho
4085 CALL xc_rho_set_atom_update(rho_set_h, needs, nspins, bounds)
4086 CALL xc_rho_set_atom_update(rho_set_s, needs, nspins, bounds)
4087
4088 CALL reallocate(rho_h, 1, na, 1, nr, 1, nspins)
4089 CALL reallocate(rho_s, 1, na, 1, nr, 1, nspins)
4090 CALL reallocate(vxc_h, 1, na, 1, nr, 1, nspins)
4091 CALL reallocate(vxc_s, 1, na, 1, nr, 1, nspins)
4092 !
4093 IF (gradient_f) THEN
4094 CALL reallocate(drho_h, 1, 4, 1, na, 1, nr, 1, nspins)
4095 CALL reallocate(drho_s, 1, 4, 1, na, 1, nr, 1, nspins)
4096 CALL reallocate(vxg_h, 1, 3, 1, na, 1, nr, 1, nspins)
4097 CALL reallocate(vxg_s, 1, 3, 1, na, 1, nr, 1, nspins)
4098 END IF
4099
4100 IF (tau_f) THEN
4101 CALL create_tau_basis_cache(tau_basis_cache, grid_atom, basis_1c, harmonics)
4102 CALL reallocate(tau_h, 1, na, 1, nr, 1, nspins)
4103 CALL reallocate(tau_s, 1, na, 1, nr, 1, nspins)
4104 CALL reallocate(vtau_h, 1, na, 1, nr, 1, nspins)
4105 CALL reallocate(vtau_s, 1, na, 1, nr, 1, nspins)
4106 END IF
4107
4108 ! NLCC: prepare rho and drho of the core charge for this KIND
4109 donlcc = .false.
4110 IF (nlcc) THEN
4111 NULLIFY (rho_nlcc)
4112 rho_nlcc => my_kind_set(ikind)%nlcc_pot
4113 IF (ASSOCIATED(rho_nlcc)) donlcc = .true.
4114 END IF
4115
4116 ! Distribute the atoms of this kind
4117
4118 num_pe = para_env%num_pe
4119 bo = get_limit(natom, para_env%num_pe, para_env%mepos)
4120
4121 DO iat = bo(1), bo(2)
4122 iatom = atom_list(iat)
4123
4124 my_rho_atom_set(iatom)%exc_h = 0.0_dp
4125 my_rho_atom_set(iatom)%exc_s = 0.0_dp
4126
4127 rho_atom => my_rho_atom_set(iatom)
4128 rho_h = 0.0_dp
4129 rho_s = 0.0_dp
4130 IF (gradient_f) THEN
4131 NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
4132 CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, &
4133 rho_rad_s=r_s, drho_rad_h=dr_h, &
4134 drho_rad_s=dr_s, rho_rad_h_d=r_h_d, &
4135 rho_rad_s_d=r_s_d)
4136 drho_h = 0.0_dp
4137 drho_s = 0.0_dp
4138 ELSE
4139 NULLIFY (r_h, r_s)
4140 CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, rho_rad_s=r_s)
4141 rho_d = 0.0_dp
4142 END IF
4143 IF (tau_f) THEN
4144 !compute tau on the grid all at once
4145 CALL calc_tau_atom(tau_h, tau_s, rho_atom, tau_basis_cache, nspins)
4146 ELSE
4147 tau_d = 0.0_dp
4148 END IF
4149
4150 DO ir = 1, nr
4151 CALL calc_rho_angular(grid_atom, harmonics, nspins, gradient_f, &
4152 ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, &
4153 r_h_d, r_s_d, drho_h, drho_s)
4154 IF (donlcc) THEN
4155 CALL calc_rho_nlcc(grid_atom, nspins, gradient_f, &
4156 ir, rho_nlcc(:, 1), rho_h, rho_s, rho_nlcc(:, 2), drho_h, drho_s)
4157 END IF
4158 END DO
4159 DO ir = 1, nr
4160 IF (tau_f) THEN
4161 CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, tau_h, na, ir)
4162 CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, tau_s, na, ir)
4163 ELSE IF (gradient_f) THEN
4164 CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, tau_d, na, ir)
4165 CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, tau_d, na, ir)
4166 ELSE
4167 CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, rho_d, tau_d, na, ir)
4168 CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, rho_d, tau_d, na, ir)
4169 END IF
4170 END DO
4171
4172 !-------------------!
4173 ! hard atom density !
4174 !-------------------!
4175 CALL xc_dset_zero_all(deriv_set)
4176 CALL vxc_of_r_epr(xc_fun_section, rho_set_h, deriv_set, needs, weight_h, &
4177 lsd, na, nr, exc_h, vxc_h, vxg_h, vtau_h)
4178 rho_atom%exc_h = rho_atom%exc_h + exc_h
4179
4180 !-------------------!
4181 ! soft atom density !
4182 !-------------------!
4183 CALL xc_dset_zero_all(deriv_set)
4184 CALL vxc_of_r_epr(xc_fun_section, rho_set_s, deriv_set, needs, weight_s, &
4185 lsd, na, nr, exc_s, vxc_s, vxg_s, vtau_s)
4186 rho_atom%exc_s = rho_atom%exc_s + exc_s
4187
4188 DO ispin = 1, nspins
4189 DO idir = 1, 3
4190 DO ir = 1, nr
4191 DO ia = 1, na
4192 gradient_atom_set(iatom)%nablavks_vec_rad_h(idir, ispin)%r_coef(ir, ia) = &
4193 gradient_atom_set(iatom)%nablavks_vec_rad_h(idir, ispin)%r_coef(ir, ia) &
4194 + vxg_h(idir, ia, ir, ispin)
4195 gradient_atom_set(iatom)%nablavks_vec_rad_s(idir, ispin)%r_coef(ir, ia) = &
4196 gradient_atom_set(iatom)%nablavks_vec_rad_s(idir, ispin)%r_coef(ir, ia) &
4197 + vxg_s(idir, ia, ir, ispin)
4198 END DO ! ia
4199 END DO ! ir
4200 END DO ! idir
4201 END DO ! ispin
4202
4203 ! Add contributions to the exc energy
4204
4205 exc1 = exc1 + rho_atom%exc_h - rho_atom%exc_s
4206
4207 ! Integration to get the matrix elements relative to the vxc_atom
4208 ! here the products with the primitives is done: gaVxcgb
4209 ! internal transformation to get the integral in cartesian Gaussians
4210
4211 NULLIFY (int_hh, int_ss)
4212 CALL get_rho_atom(rho_atom=rho_atom, ga_vlocal_gb_h=int_hh, ga_vlocal_gb_s=int_ss)
4213 IF (gradient_f) THEN
4214 CALL gavxcgb_gc(vxc_h, vxc_s, vxg_h, vxg_s, int_hh, int_ss, &
4215 grid_atom, basis_1c, harmonics, nspins)
4216 ELSE
4217 CALL gavxcgb_nogc(vxc_h, vxc_s, int_hh, int_ss, &
4218 grid_atom, basis_1c, harmonics, nspins)
4219 END IF
4220 IF (tau_f) THEN
4221 CALL dgavtaudgb(vtau_h, vtau_s, int_hh, int_ss, &
4222 tau_basis_cache, nspins)
4223 END IF
4224 NULLIFY (r_h, r_s, dr_h, dr_s)
4225 END DO ! iat
4226
4227 IF (tau_f) CALL release_tau_basis_cache(tau_basis_cache)
4228
4229 ! Release the xc structure used to store the xc derivatives
4230 CALL xc_dset_release(deriv_set)
4231 CALL xc_rho_set_release(rho_set_h)
4232 CALL xc_rho_set_release(rho_set_s)
4233 END DO ! ikind
4234
4235 CALL para_env%sum(exc1)
4236
4237 IF (ASSOCIATED(rho_h)) DEALLOCATE (rho_h)
4238 IF (ASSOCIATED(rho_s)) DEALLOCATE (rho_s)
4239 IF (ASSOCIATED(vxc_h)) DEALLOCATE (vxc_h)
4240 IF (ASSOCIATED(vxc_s)) DEALLOCATE (vxc_s)
4241
4242 IF (gradient_f) THEN
4243 IF (ASSOCIATED(drho_h)) DEALLOCATE (drho_h)
4244 IF (ASSOCIATED(drho_s)) DEALLOCATE (drho_s)
4245 IF (ASSOCIATED(vxg_h)) DEALLOCATE (vxg_h)
4246 IF (ASSOCIATED(vxg_s)) DEALLOCATE (vxg_s)
4247 END IF
4248
4249 IF (tau_f) THEN
4250 IF (ASSOCIATED(tau_h)) DEALLOCATE (tau_h)
4251 IF (ASSOCIATED(tau_s)) DEALLOCATE (tau_s)
4252 IF (ASSOCIATED(vtau_h)) DEALLOCATE (vtau_h)
4253 IF (ASSOCIATED(vtau_s)) DEALLOCATE (vtau_s)
4254 END IF
4255
4256 END IF !xc_none
4257
4258 CALL timestop(handle)
4259
4260 END SUBROUTINE calculate_vxc_atom_epr
4261
4262END MODULE qs_vxc_atom
4263
static int modulo(int a, int m)
Equivalent of Fortran's MODULO which always returns a positive number. https://gcc....
Definition fft_utils.h:60
Definition atom.F:9
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
various utilities that regard array of different kinds: output, allocation,... maybe it is not a good...
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
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 cdft_beta_constraint
integer, parameter, public cdft_magnetization_constraint
integer, parameter, public cdft_charge_constraint
integer, parameter, public cdft_alpha_constraint
integer, parameter, public xc_none
objects that represent the structure of input sections and the data contained in an input section
real(kind=dp) function, public section_get_rval(section_vals, keyword_name)
...
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 int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Generation of the spherical Lebedev grids. All Lebedev grids were generated with a precision of at le...
Definition lebedev.F:57
subroutine, public deallocate_lebedev_grids()
...
Definition lebedev.F:328
type(oh_grid), dimension(nlg), target, public lebedev_grid
Definition lebedev.F:85
integer function, public get_number_of_lebedev_grid(l, n)
Get the number of the Lebedev grid, which has the requested angular momentum quantnum number l or siz...
Definition lebedev.F:118
subroutine, public init_lebedev_grids()
Load the coordinates and weights of the nonredundant Lebedev grid points.
Definition lebedev.F:348
Utility routines for the memory handling.
Interface to the message passing library MPI.
Provides Cartesian and spherical orbital pointers and indices.
integer, dimension(:), allocatable, public nco
integer, dimension(:), allocatable, public nsoset
integer, dimension(:, :), allocatable, public indso
integer, dimension(:), allocatable, public ncoset
integer, dimension(:, :), allocatable, public indco
Calculation of the spherical harmonics and the corresponding orbital transformation matrices.
type(orbtramat_type), dimension(:), pointer, public orbtramat
Define the data structure for the particle information.
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Pointwise CDFT partition functions for nonuniform integration grids.
subroutine, public cdft_point_context_create(qs_env, context, calculate_derivatives)
Initialize reusable data for pointwise CDFT partition evaluation.
subroutine, public cdft_point_context_release(context)
Release a pointwise CDFT partition context.
subroutine, public cdft_point_weights(context, point, weights, point_derivative, atom_derivative, atomic_weights)
Evaluate CDFT weights and coordinate derivatives at one point.
Defines CDFT control structures.
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.
subroutine, public allocate_grid_atom(grid_atom)
Initialize components of the grid_atom_type structure.
subroutine, public create_grid_atom(grid_atom, nr, na, llmax, ll, quadrature)
...
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.
Type definitiona for linear response calculations.
Bounded geometry-only cache for native atom-grid interpolation.
logical function, public fetch_native_grid_stencil(cache, row, point, stencil)
Read a stencil only on an exact coordinate match. Safe for concurrent readers.
subroutine, public store_native_grid_stencil(cache, row, point, stencil)
Store from the unique forward owner of a row, never from concurrent adjoint readers.
integer, parameter, public native_grid_interp_npts
integer, parameter, public native_grid_interp_offset_max
subroutine, public prepare_native_grid_cache(cache, grid, cell, nrows, wrap, max_bytes)
Prepare outside parallel regions. Cache a bounded prefix and compute the rest normally.
integer, parameter, public native_grid_interp_offset_min
subroutine, public replicate_rho_atom_radial(para_env, rho_atom_set, qs_kind, atom_list, natom, nspins)
Replicate the radial hard/soft density data needed to evaluate one-center tails on rank-local target ...
subroutine, public get_rho_atom(rho_atom, cpc_h, cpc_s, rho_rad_h, rho_rad_s, drho_rad_h, drho_rad_s, vrho_rad_h, vrho_rad_s, rho_rad_h_d, rho_rad_s_d, ga_vlocal_gb_h, ga_vlocal_gb_s, int_scr_h, int_scr_s)
...
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...
Support routines for integrals of the Vxc/Fxc/Gxc potentials calculated for the atomic density in the...
subroutine, public calc_rho_angular(grid_atom, harmonics, nspins, grad_func, ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, r_h_d, r_s_d, drho_h, drho_s)
...
subroutine, public release_tau_basis_cache(tau_cache)
Release precomputed GAPW meta-GGA tau factors.
subroutine, public create_tau_basis_cache(tau_cache, grid_atom, basis_1c, harmonics)
Precompute radial and angular factors for GAPW meta-GGA tau contractions.
subroutine, public gavxcgb_nogc(vxc_h, vxc_s, int_hh, int_ss, grid_atom, basis_1c, harmonics, nspins)
...
subroutine, public gavxcgb_gc(vxc_h, vxc_s, vxg_h, vxg_s, int_hh, int_ss, grid_atom, basis_1c, harmonics, nspins)
Integrate gradient-corrected Vxc and Vxg potentials with spherical primitive pairs.
subroutine, public calc_weight_function(fun, r2, order, alpha)
Calculates the radial weight function for accurate XC integration.
subroutine, public calc_rho_nlcc(grid_atom, nspins, grad_func, ir, rho_nlcc, rho_h, rho_s, drho_nlcc, drho_h, drho_s)
...
subroutine, public dgavtaudgb(vtau_h, vtau_s, int_hh, int_ss, tau_cache, nspins)
Integrates 0.5 * grad_ga .dot. (V_tau * grad_gb) on the atomic grid for meta-GGA.
subroutine, public calc_tau_atom(tau_h, tau_s, rho_atom, tau_cache, nspins)
Computes tau hard and soft on the atomic grids for meta-GGA calculations.
pure subroutine, public damping_of_grid_point_close_to_atom(distance, quadrature_weight, damping, damping_derivative)
Damping of the weight that a grid point of one atom has in the non-local sums of Skala when the point...
real(dp) function, public gapw_atom_grid_support_radius(grid_atom, rho_h, rho_s, drho_h, drho_s, tau_h, tau_s)
Compact support radius of one hard-minus-soft atom-grid field.
subroutine, public evaluate_nlcc_primitive_fields(point, center, gth_potential, sgp_potential, rho, gradient, hessian)
Evaluate an NLCC density and its first two Cartesian derivatives.
routines that build the integrals of the Vxc potential calculated for the atomic density in the basis...
Definition qs_vxc_atom.F:12
subroutine, public gapw_cdft_one_center(qs_env, energy_only, calculate_forces, values, electronic_charge, operator_group, rho_atom_operator_set)
Add the GAPW one-center correction to CDFT values and operators.
subroutine, public calculate_vxc_atom(qs_env, energy_only, exc1, adiabatic_rescale_factor, kind_set_external, rho_atom_set_external, xc_section_external, calculate_forces, composite_vxc_rho, composite_vxc_tau, composite_reference_active, direct_valence_atom_grid, atom_composite_grid)
...
subroutine, public atom_grid_image_bounds(cell, fractional, cutoff, image_shell, lower, upper)
Conservatively bound periodic images that can intersect a Cartesian support sphere.
subroutine, public interpolate_gapw_atom_grid_fields(grid_atom, harmonics, displacement, cutoff, nspins, rho_h, rho_s, drho_h, drho_s, tau_h, tau_s, density, gradient, kin, density_spatial, gradient_spatial, kin_spatial, calculate_spatial)
Interpolate hard-minus-soft primitive fields and their spatial derivatives from one GAPW atom grid.
subroutine, public add_gapw_atom_grid_interpolation_adjoint(grid_atom, harmonics, displacement, cutoff, nspins, density_adjoint, gradient_adjoint, kin_adjoint, vxc_h, vxc_s, vxg_h, vxg_s, vtau_h, vtau_s)
Apply the exact transpose of interpolate_gapw_atom_grid_fields to one-center potentials.
subroutine, public calculate_vxc_atom_epr(qs_env, exc1, gradient_atom_set)
...
Build SKALA TorchScript feature dictionaries from CP2K GPW real-space grids.
subroutine, public smooth_atom_partition(grid_point, atom_coords, cell, weights, partition_atom_coords, distances, pair_distances)
Build Becke-like smooth atom weights for one native-grid point.
subroutine, public skala_gpw_smooth_partition_derivatives(grid_point, atom_coords, cell, weights, included, dweights_datom, dweights_dstrain)
Build smooth atom weights and their atom/cell deformation derivatives.
Experimental CP2K-native GPW real-space-grid path for SKALA TorchScript models.
integer, parameter, public skala_gapw_density_partition_soft_only
subroutine, public skala_gapw_atom_composite_energy(xc_section, group, density, grad, kin, grid_coords, grid_weights, atomic_grid_weights, atomic_grid_sizes, atomic_coords, exc, density_grad_out, grad_grad_out, kin_grad_out, grid_coord_grad_out, grid_weight_grad_out, atomic_grid_weight_grad_out, atom_coord_grad_out)
Evaluate a rank-local set of complete atom blocks and sum their SKALA energies.
subroutine, public build_vxc_from_feature_grads(vxc_rho, vxc_tau, rho_r, pw_pool, density_grad, grad_grad, kin_grad, xc_deriv_method_id, global_grid_layout)
Fill CP2K VXC real-space arrays from Torch feature gradients.
integer, parameter, public skala_gapw_density_partition_none
logical function, public xc_section_uses_gauxc_model(xc_section)
Return true if the GAUXC subsection requests a model evaluation.
logical function, public xc_section_uses_native_skala_evaluator(xc_section)
Return true when SKALA must be evaluated by the CP2K-native grid machinery.
type(section_vals_type) function, pointer, public get_skala_native_section(xc_section)
Return the CP2K-native SKALA functional subsection, if present.
integer, parameter, public skala_gapw_density_partition_hard_minus_soft
integer function, public native_skala_gapw_density_partition(xc_section)
Return the hard/soft GAPW one-center density partition for native SKALA.
type(section_vals_type) function, pointer, public get_gauxc_section(xc_section)
Return the first GAUXC functional subsection, if present.
subroutine, public skala_gapw_atom_vxc_of_r(xc_section, grid_atom, group, atom_coord, rho, drho, tau, weights, lsd, nspins, na, nr, exc, vxc, vxg, vtau, energy_only, atom_force, atom_virial)
Evaluate SKALA on a GAPW one-center atomic grid.
integer, parameter, public skala_gapw_density_partition_hard_only
Calculate spherical harmonics.
All kind of helpful little routines.
Definition util.F:14
pure integer function, dimension(2), public get_limit(m, n, me)
divide m entries into n parts, return size of part me
Definition util.F:333
subroutine, public vxc_of_r_epr(xc_fun_section, rho_set, deriv_set, needs, w, lsd, na, nr, exc, vxc, vxg, vtau)
Specific EPR version of vxc_of_r_new.
Definition xc_atom.F:313
subroutine, public vxc_of_r_new(xc_fun_section, rho_set, deriv_set, deriv_order, needs, w, lsd, na, nr, exc, vxc, vxg, vtau, energy_only, adiabatic_rescale_factor)
...
Definition xc_atom.F:64
subroutine, public xc_rho_set_atom_update(rho_set, needs, nspins, bo)
...
Definition xc_atom.F:523
subroutine, public fill_rho_set(rho_set, lsd, nspins, needs, rho, drho, tau, na, ir)
...
Definition xc_atom.F:683
represent a group ofunctional derivatives
subroutine, public xc_dset_zero_all(deriv_set)
...
subroutine, public xc_dset_release(derivative_set)
releases a derivative set
subroutine, public xc_dset_create(derivative_set, pw_pool, local_bounds)
creates a derivative set object
type(xc_rho_cflags_type) function, public xc_functionals_get_needs(functionals, lsd, calc_potential)
...
input constants for xc
integer, parameter, public skala_gapw_direct_valence
integer, parameter, public skala_gapw_paw_one_center
integer, parameter, public skala_gapw_cp2k_default
integer, parameter, public skala_gapw_paw_one_center_split
contains the structure
contains the structure
subroutine, public xc_rho_set_create(rho_set, local_bounds, rho_cutoff, drho_cutoff, tau_cutoff)
allocates and does (minimal) initialization of a rho_set
subroutine, public xc_rho_set_release(rho_set, pw_pool)
releases the given rho_set
subroutine, public xc_rho_set_update(rho_set, rho_r, rho_g, tau, needs, xc_deriv_method_id, xc_rho_smooth_id, pw_pool, spinflip, ground_state_rho_set)
updates the given rho set with the density given by rho_r (and rho_g). The rho set will contain the c...
subroutine, public xc_rho_set_get(rho_set, can_return_null, rho, drho, norm_drho, rhoa, rhob, norm_drhoa, norm_drhob, rho_1_3, rhoa_1_3, rhob_1_3, laplace_rho, laplace_rhoa, laplace_rhob, drhoa, drhob, rho_cutoff, drho_cutoff, tau_cutoff, tau, tau_a, tau_b, local_bounds)
returns the various attributes of rho_set
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a pointer to a contiguous 3d array
stores all the informations relevant to an mpi environment
contained for different pw related things
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Provides all information about a quickstep kind.
keeps the density in various representations, keeping track of which ones are valid.
A derivative set contains the different derivatives of a xc-functional in form of a linked list.
contains a flag for each component of xc_rho_set, so that you can use it to tell which components you...
represent a density, with all the representation and data needed to perform a functional evaluation