(git:3d5d2e5)
Loading...
Searching...
No Matches
xc_util.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 contains utility functions for the xc package
10!> \par History
11!> 03.2022 created [F. Stein]
12!> \author Frederick Stein
13! **************************************************************************************************
14MODULE xc_util
15 USE mathconstants, ONLY: gaussi
16 USE pw_methods, ONLY: pw_axpy,&
17 pw_copy,&
18 pw_derive,&
23 USE pw_spline_utils, ONLY: &
28 USE pw_types, ONLY: pw_c1d_gs_type,&
30 USE xc_input_constants, ONLY: &
34#include "../base/base_uses.f90"
35
36 IMPLICIT NONE
37
38 PRIVATE
39
42 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'xc_util'
43
44 INTERFACE xc_pw_derive
45 MODULE PROCEDURE xc_pw_derive_r3d_rs, xc_pw_derive_c1d_gs
46 END INTERFACE
47
48 INTERFACE xc_pw_laplace
49 MODULE PROCEDURE xc_pw_laplace_r3d_rs, xc_pw_laplace_c1d_gs
50 END INTERFACE
51
52CONTAINS
53
54! **************************************************************************************************
55!> \brief ...
56!> \param xc_deriv_id ...
57!> \return ...
58! **************************************************************************************************
59 ELEMENTAL FUNCTION xc_requires_tmp_g(xc_deriv_id) RESULT(requires)
60 INTEGER, INTENT(IN) :: xc_deriv_id
61 LOGICAL :: requires
62
63 requires = (xc_deriv_id == xc_deriv_spline2) .OR. &
64 (xc_deriv_id == xc_deriv_spline3) .OR. &
65 (xc_deriv_id == xc_deriv_pw)
66 END FUNCTION xc_requires_tmp_g
67
68! **************************************************************************************************
69!> \brief ...
70!> \param pw_in ...
71!> \param pw_out ...
72!> \param xc_smooth_id ...
73! **************************************************************************************************
74 SUBROUTINE xc_pw_smooth(pw_in, pw_out, xc_smooth_id)
75 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw_in
76 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw_out
77 INTEGER, INTENT(IN) :: xc_smooth_id
78
79 CHARACTER(LEN=*), PARAMETER :: routinen = 'xc_pw_smooth'
80
81 INTEGER :: handle
82
83 CALL timeset(routinen, handle)
84
85 SELECT CASE (xc_smooth_id)
86 CASE (xc_rho_no_smooth)
87 CALL pw_copy(pw_in, pw_out)
89 CALL pw_zero(pw_out)
90 CALL pw_nn_smear_r(pw_in=pw_in, &
91 pw_out=pw_out, &
92 coeffs=spline2_coeffs)
94 CALL pw_zero(pw_out)
95 CALL pw_nn_smear_r(pw_in=pw_in, &
96 pw_out=pw_out, &
97 coeffs=spline3_coeffs)
98 CASE (xc_rho_nn10)
99 CALL pw_zero(pw_out)
100 CALL pw_nn_smear_r(pw_in=pw_in, &
101 pw_out=pw_out, &
102 coeffs=nn10_coeffs)
103 CASE (xc_rho_nn50)
104 CALL pw_zero(pw_out)
105 CALL pw_nn_smear_r(pw_in=pw_in, &
106 pw_out=pw_out, &
107 coeffs=nn50_coeffs)
108 CASE default
109 cpabort("Unsupported smoothing")
110 END SELECT
111
112 CALL timestop(handle)
113
114 END SUBROUTINE xc_pw_smooth
115
116! **************************************************************************************************
117!> \brief ...
118!> \param pw_r ...
119!> \param pw_g ...
120!> \param tmp_g ...
121!> \param gradient ...
122!> \param xc_deriv_method_id ...
123! **************************************************************************************************
124 SUBROUTINE xc_pw_gradient(pw_r, pw_g, tmp_g, gradient, xc_deriv_method_id)
125 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw_r
126 TYPE(pw_c1d_gs_type), INTENT(INOUT) :: pw_g, tmp_g
127 TYPE(pw_r3d_rs_type), DIMENSION(3), INTENT(INOUT) :: gradient
128 INTEGER, INTENT(IN) :: xc_deriv_method_id
129
130 INTEGER :: idir, ig
131
132 DO idir = 1, 3
133 CALL pw_zero(gradient(idir))
134 IF (xc_deriv_method_id == xc_deriv_pw .AND. ASSOCIATED(pw_g%pw_grid, tmp_g%pw_grid)) THEN
135!$OMP PARALLEL DO IF(SIZE(pw_g%array) >= 8192) DEFAULT(NONE) SHARED(pw_g,tmp_g,idir) SCHEDULE(STATIC)
136 DO ig = 1, SIZE(pw_g%array)
137 tmp_g%array(ig) = gaussi*(pw_g%array(ig)*pw_g%pw_grid%g(idir, ig))
138 END DO
139!$OMP END PARALLEL DO
140 CALL pw_transfer(tmp_g, gradient(idir))
141 ELSE
142 CALL xc_pw_derive(pw_r, tmp_g, gradient(idir), idir, xc_deriv_method_id, pw_g=pw_g)
143 END IF
144 END DO
145
146 END SUBROUTINE xc_pw_gradient
147
148! **************************************************************************************************
149!> \brief Calculates the Laplacian of pw
150!> \param pw on input: pw of which the Laplacian shall be calculated, on output if pw_out is absent: Laplacian of input
151!> \param pw_pool ...
152!> \param deriv_method_id ...
153!> \param pw_out if present, save the Laplacian of pw here
154!> \param tmp_g scratch grid in reciprocal space, used instead of the internal grid if given explicitly to save memory
155! **************************************************************************************************
156 SUBROUTINE xc_pw_laplace_r3d_rs(pw, pw_pool, deriv_method_id, pw_out, tmp_g)
157 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: pw
158 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
159 INTEGER, INTENT(IN) :: deriv_method_id
160 TYPE(pw_r3d_rs_type), INTENT(INOUT), OPTIONAL :: pw_out
161 TYPE(pw_c1d_gs_type), INTENT(IN), OPTIONAL :: tmp_g
162
163 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_pw_laplace'
164
165 INTEGER :: handle
166 LOGICAL :: owns_tmp_g
167 TYPE(pw_c1d_gs_type) :: my_tmp_g
168
169 CALL timeset(routinen, handle)
170
171 SELECT CASE (deriv_method_id)
172 CASE (xc_deriv_pw)
173
174 IF (PRESENT(tmp_g)) my_tmp_g = tmp_g
175
176 owns_tmp_g = .false.
177 IF (.NOT. ASSOCIATED(my_tmp_g%pw_grid)) THEN
178 CALL pw_pool%create_pw(my_tmp_g)
179 owns_tmp_g = .true.
180 END IF
181 CALL pw_zero(my_tmp_g)
182 CALL pw_transfer(pw, my_tmp_g)
183
184 CALL pw_laplace(my_tmp_g)
185
186 IF (PRESENT(pw_out)) THEN
187 CALL pw_transfer(my_tmp_g, pw_out)
188 ELSE
189 CALL pw_transfer(my_tmp_g, pw)
190 END IF
191 IF (owns_tmp_g) THEN
192 CALL pw_pool%give_back_pw(my_tmp_g)
193 END IF
194 CASE default
195 cpabort("Unsupported derivative method")
196 END SELECT
197
198 CALL timestop(handle)
199
200 END SUBROUTINE xc_pw_laplace_r3d_rs
201! **************************************************************************************************
202!> \brief Calculates the Laplacian of pw
203!> \param pw on input: pw of which the Laplacian shall be calculated, on output if pw_out is absent: Laplacian of input
204!> \param pw_pool ...
205!> \param deriv_method_id ...
206!> \param pw_out if present, save the Laplacian of pw here
207!> \param tmp_g scratch grid in reciprocal space, used instead of the internal grid if given explicitly to save memory
208! **************************************************************************************************
209 SUBROUTINE xc_pw_laplace_c1d_gs(pw, pw_pool, deriv_method_id, pw_out, tmp_g)
210 TYPE(pw_c1d_gs_type), INTENT(INOUT) :: pw
211 TYPE(pw_pool_type), INTENT(IN), POINTER :: pw_pool
212 INTEGER, INTENT(IN) :: deriv_method_id
213 TYPE(pw_r3d_rs_type), INTENT(INOUT), OPTIONAL :: pw_out
214 TYPE(pw_c1d_gs_type), INTENT(IN), OPTIONAL :: tmp_g
215
216 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_pw_laplace'
217
218 INTEGER :: handle
219 LOGICAL :: owns_tmp_g
220 TYPE(pw_c1d_gs_type) :: my_tmp_g
221
222 CALL timeset(routinen, handle)
223
224 SELECT CASE (deriv_method_id)
225 CASE (xc_deriv_pw)
226
227 IF (PRESENT(tmp_g)) my_tmp_g = tmp_g
228
229 owns_tmp_g = .false.
230 IF (.NOT. ASSOCIATED(my_tmp_g%pw_grid)) THEN
231 CALL pw_pool%create_pw(my_tmp_g)
232 owns_tmp_g = .true.
233 END IF
234 CALL pw_zero(my_tmp_g)
235 CALL pw_transfer(pw, my_tmp_g)
236
237 CALL pw_laplace(my_tmp_g)
238
239 IF (PRESENT(pw_out)) THEN
240 CALL pw_transfer(my_tmp_g, pw_out)
241 ELSE
242 CALL pw_transfer(my_tmp_g, pw)
243 END IF
244 IF (owns_tmp_g) THEN
245 CALL pw_pool%give_back_pw(my_tmp_g)
246 END IF
247 CASE default
248 cpabort("Unsupported derivative method")
249 END SELECT
250
251 CALL timestop(handle)
252
253 END SUBROUTINE xc_pw_laplace_c1d_gs
254
255! **************************************************************************************************
256!> \brief Calculates the divergence of pw_to_deriv
257!> \param xc_deriv_method_id ...
258!> \param pw_to_deriv ...
259!> \param tmp_g ...
260!> \param vxc_g ...
261!> \param vxc_r ...
262! **************************************************************************************************
263 SUBROUTINE xc_pw_divergence(xc_deriv_method_id, pw_to_deriv, tmp_g, vxc_g, vxc_r)
264 INTEGER, INTENT(IN) :: xc_deriv_method_id
265 TYPE(pw_r3d_rs_type), DIMENSION(3), INTENT(INOUT) :: pw_to_deriv
266 TYPE(pw_c1d_gs_type), INTENT(INOUT) :: tmp_g, vxc_g
267 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: vxc_r
268
269 CHARACTER(LEN=*), PARAMETER :: routinen = 'xc_pw_divergence'
270
271 INTEGER :: handle, idir, ig
272
273 CALL timeset(routinen, handle)
274
275 ! partial integration
276 IF (xc_deriv_method_id /= xc_deriv_pw) THEN
277 CALL pw_spline_scale_deriv(pw_to_deriv, transpose=.true.)
278 END IF
279
280 IF (ASSOCIATED(vxc_g%pw_grid)) CALL pw_zero(vxc_g)
281
282 DO idir = 1, 3
283 IF (xc_deriv_method_id == xc_deriv_pw .AND. ASSOCIATED(tmp_g%pw_grid, vxc_g%pw_grid)) THEN
284 CALL pw_transfer(pw_to_deriv(idir), tmp_g)
285!$OMP PARALLEL DO IF(SIZE(tmp_g%array) >= 8192) DEFAULT(NONE) SHARED(tmp_g,vxc_g,idir) SCHEDULE(STATIC)
286 DO ig = 1, SIZE(tmp_g%array)
287 tmp_g%array(ig) = gaussi*(tmp_g%array(ig)*tmp_g%pw_grid%g(idir, ig))
288 vxc_g%array(ig) = vxc_g%array(ig) + tmp_g%array(ig)
289 END DO
290!$OMP END PARALLEL DO
291 ELSE
292 CALL xc_pw_derive(pw_to_deriv(idir), tmp_g, vxc_r, idir, xc_deriv_method_id, copy_to_vxcr=.false.)
293 IF (ASSOCIATED(tmp_g%pw_grid) .AND. ASSOCIATED(vxc_g%pw_grid)) CALL pw_axpy(tmp_g, vxc_g)
294 END IF
295 END DO
296
297 IF (ASSOCIATED(vxc_g%pw_grid)) THEN
298 CALL pw_transfer(vxc_g, pw_to_deriv(1))
299 CALL pw_axpy(pw_to_deriv(1), vxc_r)
300 END IF
301
302 CALL timestop(handle)
303
304 END SUBROUTINE xc_pw_divergence
305
306! **************************************************************************************************
307!> \brief Calculates the derivative of a function on a planewave grid in a given direction
308!> \param pw function to derive
309!> \param tmp_g temporary grid in reciprocal space, only required if derivative method is pw or spline
310!> \param vxc_r if tmp_g is not required, add derivative here
311!> \param idir direction of derivative
312!> \param xc_deriv_method_id ...
313!> \param copy_to_vxcr ...
314!> \param pw_g ...
315! **************************************************************************************************
316 SUBROUTINE xc_pw_derive_r3d_rs(pw, tmp_g, vxc_r, idir, xc_deriv_method_id, copy_to_vxcr, pw_g)
317 TYPE(pw_r3d_rs_type), INTENT(IN) :: pw
318 TYPE(pw_c1d_gs_type), INTENT(INOUT) :: tmp_g
319 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: vxc_r
320 INTEGER, INTENT(IN) :: idir, xc_deriv_method_id
321 LOGICAL, INTENT(IN), OPTIONAL :: copy_to_vxcr
322 TYPE(pw_c1d_gs_type), INTENT(IN), OPTIONAL :: pw_g
323
324 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_pw_derive'
325 INTEGER, DIMENSION(3, 3), PARAMETER :: nd = reshape([1, 0, 0, 0, 1, 0, 0, 0, 1], [3, 3])
326
327 INTEGER :: handle
328 LOGICAL :: my_copy_to_vxcr
329
330 CALL timeset(routinen, handle)
331
332 my_copy_to_vxcr = .true.
333 IF (PRESENT(copy_to_vxcr)) my_copy_to_vxcr = copy_to_vxcr
334
335 IF (xc_requires_tmp_g(xc_deriv_method_id)) THEN
336
337 IF (PRESENT(pw_g)) THEN
338 IF (ASSOCIATED(pw_g%pw_grid)) THEN
339 CALL pw_copy(pw_g, tmp_g)
340 ELSE
341 CALL pw_transfer(pw, tmp_g)
342 END IF
343 ELSE
344 CALL pw_transfer(pw, tmp_g)
345 END IF
346
347 SELECT CASE (xc_deriv_method_id)
348 CASE (xc_deriv_pw)
349 CALL pw_derive(tmp_g, nd(:, idir))
350 CASE (xc_deriv_spline2)
352 CALL pw_spline2_deriv_g(tmp_g, idir=idir)
353 CASE (xc_deriv_spline3)
355 CALL pw_spline3_deriv_g(tmp_g, idir=idir)
356 CASE default
357 cpabort("Unsupported deriv method")
358 END SELECT
359
360 IF (my_copy_to_vxcr) CALL pw_transfer(tmp_g, vxc_r)
361 ELSE
362 SELECT CASE (xc_deriv_method_id)
364 CALL pw_nn_deriv_r(pw_in=pw, &
365 pw_out=vxc_r, coeffs=spline2_deriv_coeffs, &
366 idir=idir)
368 CALL pw_nn_deriv_r(pw_in=pw, &
369 pw_out=vxc_r, coeffs=spline3_deriv_coeffs, &
370 idir=idir)
372 CALL pw_nn_deriv_r(pw_in=pw, &
373 pw_out=vxc_r, coeffs=nn10_deriv_coeffs, &
374 idir=idir)
376 CALL pw_nn_deriv_r(pw_in=pw, &
377 pw_out=vxc_r, coeffs=nn50_deriv_coeffs, &
378 idir=idir)
379 CASE default
380 cpabort("Unsupported derivative method")
381 END SELECT
382 END IF
383
384 CALL timestop(handle)
385
386 END SUBROUTINE xc_pw_derive_r3d_rs
387! **************************************************************************************************
388!> \brief Calculates the derivative of a function on a planewave grid in a given direction
389!> \param pw function to derive
390!> \param tmp_g temporary grid in reciprocal space, only required if derivative method is pw or spline
391!> \param vxc_r if tmp_g is not required, add derivative here
392!> \param idir direction of derivative
393!> \param xc_deriv_method_id ...
394!> \param copy_to_vxcr ...
395!> \param pw_g ...
396! **************************************************************************************************
397 SUBROUTINE xc_pw_derive_c1d_gs(pw, tmp_g, vxc_r, idir, xc_deriv_method_id, copy_to_vxcr, pw_g)
398 TYPE(pw_c1d_gs_type), INTENT(IN) :: pw
399 TYPE(pw_c1d_gs_type), INTENT(INOUT) :: tmp_g
400 TYPE(pw_r3d_rs_type), INTENT(INOUT) :: vxc_r
401 INTEGER, INTENT(IN) :: idir, xc_deriv_method_id
402 LOGICAL, INTENT(IN), OPTIONAL :: copy_to_vxcr
403 TYPE(pw_c1d_gs_type), INTENT(IN), OPTIONAL :: pw_g
404
405 CHARACTER(LEN=*), PARAMETER :: routineN = 'xc_pw_derive'
406 INTEGER, DIMENSION(3, 3), PARAMETER :: nd = reshape([1, 0, 0, 0, 1, 0, 0, 0, 1], [3, 3])
407
408 INTEGER :: handle
409 LOGICAL :: my_copy_to_vxcr
410 TYPE(pw_r3d_rs_type) :: tmp_r
411
412 CALL timeset(routinen, handle)
413
414 my_copy_to_vxcr = .true.
415 IF (PRESENT(copy_to_vxcr)) my_copy_to_vxcr = copy_to_vxcr
416
417 IF (xc_requires_tmp_g(xc_deriv_method_id)) THEN
418
419 IF (PRESENT(pw_g)) THEN
420 IF (ASSOCIATED(pw_g%pw_grid)) THEN
421 CALL pw_copy(pw_g, tmp_g)
422 ELSE
423 CALL pw_transfer(pw, tmp_g)
424 END IF
425 ELSE
426 CALL pw_transfer(pw, tmp_g)
427 END IF
428
429 SELECT CASE (xc_deriv_method_id)
430 CASE (xc_deriv_pw)
431 CALL pw_derive(tmp_g, nd(:, idir))
432 CASE (xc_deriv_spline2)
434 CALL pw_spline2_deriv_g(tmp_g, idir=idir)
435 CASE (xc_deriv_spline3)
437 CALL pw_spline3_deriv_g(tmp_g, idir=idir)
438 CASE default
439 cpabort("Unsupported deriv method")
440 END SELECT
441
442 IF (my_copy_to_vxcr) CALL pw_transfer(tmp_g, vxc_r)
443 ELSE
444 CALL tmp_r%create(pw%pw_grid)
445 SELECT CASE (xc_deriv_method_id)
447 CALL pw_nn_deriv_r(pw_in=tmp_r, &
448 pw_out=vxc_r, coeffs=spline2_deriv_coeffs, &
449 idir=idir)
451 CALL pw_nn_deriv_r(pw_in=tmp_r, &
452 pw_out=vxc_r, coeffs=spline3_deriv_coeffs, &
453 idir=idir)
455 CALL pw_nn_deriv_r(pw_in=tmp_r, &
456 pw_out=vxc_r, coeffs=nn10_deriv_coeffs, &
457 idir=idir)
459 CALL pw_nn_deriv_r(pw_in=tmp_r, &
460 pw_out=vxc_r, coeffs=nn50_deriv_coeffs, &
461 idir=idir)
462 CASE default
463 cpabort("Unsupported derivative method")
464 END SELECT
465 CALL tmp_r%release()
466 END IF
467
468 CALL timestop(handle)
469
470 END SUBROUTINE xc_pw_derive_c1d_gs
471
472END MODULE xc_util
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public gaussi
subroutine, public pw_laplace(pw)
Calculate the Laplacian of a plane wave vector.
subroutine, public pw_derive(pw, n)
Calculate the derivative of a plane wave vector.
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
different utils that are useful to manipulate splines on the regular grid of a pw
subroutine, public pw_nn_deriv_r(pw_in, pw_out, coeffs, idir)
calculates a nearest neighbor central derivative. for the x dir: pw_outarray(i,j,k)=( pw_in(i+1,...
subroutine, public pw_spline3_deriv_g(spline_g, idir)
calculates the FFT of the values of the x,y,z (idir=1,2,3) derivative of the cubic spline
real(kind=dp), dimension(4), parameter, public spline2_coeffs
real(kind=dp), dimension(3), parameter, public spline3_deriv_coeffs
subroutine, public pw_spline_scale_deriv(deriv_vals_r, transpose, scale)
rescales the derivatives from gridspacing=1 to the real derivatives
real(kind=dp), dimension(3), parameter, public spline2_deriv_coeffs
subroutine, public pw_nn_smear_r(pw_in, pw_out, coeffs)
calculates the values of a nearest neighbor smearing
real(kind=dp), dimension(4), parameter, public spline3_coeffs
real(kind=dp), dimension(3), parameter, public nn50_deriv_coeffs
subroutine, public pw_spline3_interpolate_values_g(spline_g)
calculates the FFT of the coefficients of the2 cubic spline that interpolates the given values
subroutine, public pw_spline2_interpolate_values_g(spline_g)
calculates the FFT of the coefficients of the quadratic spline that interpolates the given values
real(kind=dp), dimension(3), parameter, public nn10_deriv_coeffs
subroutine, public pw_spline2_deriv_g(spline_g, idir)
calculates the FFT of the values of the x,y,z (idir=1,2,3) derivative of the quadratic spline
real(kind=dp), dimension(4), parameter, public nn10_coeffs
real(kind=dp), dimension(4), parameter, public nn50_coeffs
input constants for xc
integer, parameter, public xc_deriv_spline2_smooth
integer, parameter, public xc_rho_spline2_smooth
integer, parameter, public xc_rho_spline3_smooth
integer, parameter, public xc_deriv_nn10_smooth
integer, parameter, public xc_deriv_pw
integer, parameter, public xc_deriv_spline2
integer, parameter, public xc_rho_nn10
integer, parameter, public xc_deriv_nn50_smooth
integer, parameter, public xc_deriv_spline3
integer, parameter, public xc_rho_nn50
integer, parameter, public xc_deriv_spline3_smooth
integer, parameter, public xc_rho_no_smooth
contains utility functions for the xc package
Definition xc_util.F:14
subroutine, public xc_pw_divergence(xc_deriv_method_id, pw_to_deriv, tmp_g, vxc_g, vxc_r)
Calculates the divergence of pw_to_deriv.
Definition xc_util.F:264
subroutine, public xc_pw_smooth(pw_in, pw_out, xc_smooth_id)
...
Definition xc_util.F:75
elemental logical function, public xc_requires_tmp_g(xc_deriv_id)
...
Definition xc_util.F:60
subroutine, public xc_pw_gradient(pw_r, pw_g, tmp_g, gradient, xc_deriv_method_id)
...
Definition xc_util.F:125
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...