(git:899a4fa)
Loading...
Searching...
No Matches
gaussian_basis_fields.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 Native contracted Gaussian fields and their consistent Cartesian/Bloch responses.
10!> Uses the stored sphi coefficients and native spherical AO order without renormalization.
11! **************************************************************************************************
14 USE kinds, ONLY: dp
15 USE mathconstants, ONLY: twopi
16 USE orbital_pointers, ONLY: indco,&
17 ncoset
18#include "../base/base_uses.f90"
19
20 IMPLICIT NONE
21 PRIVATE
23
24CONTAINS
25
26! **************************************************************************************************
27!> \brief Evaluate a native contracted basis at explicit point-minus-center displacements.
28!> No minimum-image operation is applied; an explicit cutoff can skip excluded points.
29!> \param basis Initialized native basis, including its original normalization in sphi.
30!> \param displacements Cartesian displacements, indexed (axis,point).
31!> \param value New values, indexed (point,AO).
32!> \param gradient Optional Cartesian derivatives, indexed (point,AO,axis).
33!> \param hessian Optional Cartesian Hessians, indexed (point,AO,axis,axis).
34!> \param cutoff_squared Optional squared cutoff radius; fields beyond it are zero.
35! **************************************************************************************************
36 SUBROUTINE gaussian_basis_fields_evaluate(basis, displacements, value, gradient, hessian, cutoff_squared)
37 TYPE(gto_basis_set_type), INTENT(IN) :: basis
38 REAL(dp), INTENT(IN) :: displacements(:, :)
39 REAL(dp), INTENT(OUT) :: value(:, :)
40 REAL(dp), INTENT(OUT), OPTIONAL :: gradient(:, :, :), hessian(:, :, :, :)
41 REAL(dp), INTENT(IN), OPTIONAL :: cutoff_squared
42
43 INTEGER :: axis, axis2, first, ico, ipgf, ipoint, &
44 irow, iset, isgf, ishell, l, last, &
45 ncart, powers(3), reduced(3)
46 REAL(dp) :: alpha, exponential, polynomial, polynomial_gradient(3), &
47 polynomial_hessian(3, 3), primitive_gradient(3), primitive_hessian(3, 3), r(3), &
48 r_squared, weight, coordinate_power(3, 0:maxval(basis%lmax)), &
49 cartesian_value(ncoset(maxval(basis%lmax))), &
50 cartesian_hessian(3, 3, ncoset(maxval(basis%lmax))), &
51 cartesian_gradient(3, ncoset(maxval(basis%lmax)))
52
53 cpassert(basis%nset > 0 .AND. basis%nsgf > 0)
54 cpassert(ASSOCIATED(basis%sphi) .AND. ASSOCIATED(basis%zet))
55 cpassert(ubound(ncoset, 1) >= maxval(basis%lmax))
56 cpassert(SIZE(displacements, 1) == 3)
57 cpassert(all(shape(value) == [SIZE(displacements, 2), basis%nsgf]))
58 IF (PRESENT(gradient)) THEN
59 cpassert(all(shape(gradient) == [SIZE(value, 1), SIZE(value, 2), 3]))
60 gradient = 0.0_dp
61 END IF
62 IF (PRESENT(hessian)) THEN
63 cpassert(all(shape(hessian) == [SIZE(value, 1), SIZE(value, 2), 3, 3]))
64 hessian = 0.0_dp
65 END IF
66 value = 0.0_dp
67 !$OMP PARALLEL DO DEFAULT(NONE) SCHEDULE(STATIC) &
68 !$OMP SHARED(basis, displacements, value, gradient, hessian, cutoff_squared, ncoset, indco) &
69 !$OMP PRIVATE(ipoint, r, iset, ncart, ishell, l, first, last, ipgf, alpha, exponential, &
70 !$OMP isgf, ico, irow, weight, powers, reduced, polynomial, polynomial_gradient, &
71 !$OMP polynomial_hessian, primitive_gradient, primitive_hessian, cartesian_value, &
72 !$OMP cartesian_gradient, cartesian_hessian, coordinate_power, r_squared, axis, axis2)
73 DO ipoint = 1, SIZE(displacements, 2)
74 r = displacements(:, ipoint)
75 r_squared = sum(r**2)
76 IF (PRESENT(cutoff_squared)) THEN
77 IF (r_squared > cutoff_squared) cycle
78 END IF
79 coordinate_power(:, 0) = 1.0_dp
80 DO l = 1, ubound(coordinate_power, 2)
81 coordinate_power(:, l) = coordinate_power(:, l - 1)*r
82 END DO
83 ! Cartesian monomials and their derivatives do not depend on the primitive or contraction.
84 DO ico = 1, SIZE(cartesian_value)
85 powers = indco(:, ico)
86 cartesian_value(ico) = coordinate_power(1, powers(1))*coordinate_power(2, powers(2))* &
87 coordinate_power(3, powers(3))
88 IF (.NOT. PRESENT(gradient) .AND. .NOT. PRESENT(hessian)) cycle
89 cartesian_gradient(:, ico) = 0.0_dp
90 cartesian_hessian(:, :, ico) = 0.0_dp
91 DO axis = 1, 3
92 IF (powers(axis) == 0) cycle
93 reduced = powers
94 reduced(axis) = reduced(axis) - 1
95 cartesian_gradient(axis, ico) = real(powers(axis), dp)*coordinate_power(1, reduced(1))* &
96 coordinate_power(2, reduced(2))*coordinate_power(3, reduced(3))
97 IF (.NOT. PRESENT(hessian)) cycle
98 DO axis2 = 1, 3
99 IF (reduced(axis2) == 0) cycle
100 reduced(axis2) = reduced(axis2) - 1
101 cartesian_hessian(axis, axis2, ico) = real(powers(axis)*(reduced(axis2) + 1), dp)* &
102 coordinate_power(1, reduced(1))*coordinate_power(2, reduced(2))* &
103 coordinate_power(3, reduced(3))
104 reduced(axis2) = reduced(axis2) + 1
105 END DO
106 END DO
107 END DO
108 DO iset = 1, basis%nset
109 ncart = ncoset(basis%lmax(iset))
110 DO ipgf = 1, basis%npgf(iset)
111 alpha = basis%zet(ipgf, iset)
112 exponential = exp(-alpha*r_squared)
113 DO ishell = 1, basis%nshell(iset)
114 l = basis%l(ishell, iset)
115 first = basis%first_sgf(ishell, iset)
116 last = basis%last_sgf(ishell, iset)
117 ! Primitive Cartesian derivatives are shared by every spherical contraction.
118 DO ico = ncoset(l - 1) + 1, ncoset(l)
119 irow = (ipgf - 1)*ncart + ico
120 polynomial = cartesian_value(ico)
121 IF (PRESENT(gradient) .OR. PRESENT(hessian)) THEN
122 polynomial_gradient = cartesian_gradient(:, ico)
123 polynomial_hessian = cartesian_hessian(:, :, ico)
124 DO axis = 1, 3
125 primitive_gradient(axis) = polynomial_gradient(axis) - 2.0_dp*alpha*r(axis)*polynomial
126 IF (.NOT. PRESENT(hessian)) cycle
127 DO axis2 = 1, 3
128 primitive_hessian(axis, axis2) = polynomial_hessian(axis, axis2) - 2.0_dp*alpha*( &
129 r(axis)*polynomial_gradient(axis2) + r(axis2)*polynomial_gradient(axis)) + &
130 4.0_dp*alpha**2*r(axis)*r(axis2)*polynomial
131 IF (axis == axis2) THEN
132 primitive_hessian(axis, axis2) = primitive_hessian(axis, axis2) - 2.0_dp*alpha*polynomial
133 END IF
134 END DO
135 END DO
136 END IF
137 DO isgf = first, last
138 weight = basis%sphi(irow, isgf)*exponential
139 value(ipoint, isgf) = value(ipoint, isgf) + weight*polynomial
140 IF (.NOT. PRESENT(gradient) .AND. .NOT. PRESENT(hessian)) cycle
141 DO axis = 1, 3
142 IF (PRESENT(gradient)) THEN
143 gradient(ipoint, isgf, axis) = gradient(ipoint, isgf, axis) + weight*primitive_gradient(axis)
144 END IF
145 IF (.NOT. PRESENT(hessian)) cycle
146 DO axis2 = 1, 3
147 hessian(ipoint, isgf, axis, axis2) = hessian(ipoint, isgf, axis, axis2) + &
148 weight*primitive_hessian(axis, axis2)
149 END DO
150 END DO
151 END DO
152 END DO
153 END DO
154 END DO
155 END DO
156 END DO
157 !$OMP END PARALLEL DO
158 END SUBROUTINE gaussian_basis_fields_evaluate
159
160! **************************************************************************************************
161!> \brief Sum explicit Gaussian images with physical Bloch gradients and joint geometry response.
162!> Caller owns the finite image set and its convergence; the set is held fixed in a response.
163!> \param basis ...
164!> \param points Cartesian points, indexed (axis,point).
165!> \param center Physical basis center, without implicit folding.
166!> \param cell Cartesian lattice vectors, one vector per column.
167!> \param images Explicit integer lattice images, indexed (axis,image).
168!> \param reduced_k Reduced Bloch vector; phase is exp(i*2*pi*k.image).
169!> \param value New Bloch values, indexed (point,AO).
170!> \param gradient Optional physical Cartesian Bloch gradients, axis last.
171!> \param response Optional joint first response of the values.
172!> \param gradient_response Optional response of the physical spatial gradients.
173!> \param point_response Optional Cartesian point response, default zero.
174!> \param center_response Optional Cartesian center response, default zero.
175!> \param cell_response Optional lattice response, default zero.
176!> \param reduced_k_response Optional reduced Bloch-vector response, default zero.
177!> \param cutoff_squared Optional squared spatial radius, fixed during geometry responses.
178! **************************************************************************************************
179 SUBROUTINE gaussian_basis_fields_periodic(basis, points, center, cell, images, reduced_k, value, gradient, &
180 response, gradient_response, point_response, center_response, &
181 cell_response, reduced_k_response, cutoff_squared)
182 TYPE(gto_basis_set_type), INTENT(IN) :: basis
183 REAL(dp), INTENT(IN) :: points(:, :), center(3), cell(3, 3)
184 INTEGER, INTENT(IN) :: images(:, :)
185 REAL(dp), INTENT(IN) :: reduced_k(3)
186 COMPLEX(dp), INTENT(OUT) :: value(:, :)
187 COMPLEX(dp), INTENT(OUT), OPTIONAL :: gradient(:, :, :), response(:, :), &
188 gradient_response(:, :, :)
189 REAL(dp), INTENT(IN), OPTIONAL :: point_response(:, :), &
190 center_response(3), &
191 cell_response(3, 3), &
192 reduced_k_response(3), cutoff_squared
193
194 COMPLEX(dp) :: phase, phase_response
195 INTEGER :: axis, axis2, image, npoint
196 LOGICAL :: need_gradient, need_hessian
197 REAL(dp) :: dc(3, 3), dcenter(3), dk(3), lattice(3)
198 REAL(dp), ALLOCATABLE :: ddisplacement(:, :), displacement(:, :), &
199 field(:, :), g(:, :, :), h(:, :, :, :)
200
201 npoint = SIZE(points, 2)
202 cpassert(SIZE(points, 1) == 3 .AND. SIZE(images, 1) == 3)
203 cpassert(all(shape(value) == [npoint, basis%nsgf]))
204 need_gradient = PRESENT(gradient) .OR. PRESENT(response) .OR. PRESENT(gradient_response)
205 need_hessian = PRESENT(gradient_response)
206 IF (PRESENT(gradient)) THEN
207 cpassert(all(shape(gradient) == [npoint, basis%nsgf, 3]))
208 gradient = cmplx(0.0_dp, 0.0_dp, dp)
209 END IF
210 IF (PRESENT(response)) THEN
211 cpassert(all(shape(response) == shape(value)))
212 response = cmplx(0.0_dp, 0.0_dp, dp)
213 END IF
214 IF (PRESENT(gradient_response)) THEN
215 cpassert(all(shape(gradient_response) == [npoint, basis%nsgf, 3]))
216 gradient_response = cmplx(0.0_dp, 0.0_dp, dp)
217 END IF
218 dc = 0.0_dp
219 dcenter = 0.0_dp
220 dk = 0.0_dp
221 IF (PRESENT(cell_response)) dc = cell_response
222 IF (PRESENT(center_response)) dcenter = center_response
223 IF (PRESENT(reduced_k_response)) dk = reduced_k_response
224 ALLOCATE (displacement(3, npoint), ddisplacement(3, npoint), field(npoint, basis%nsgf))
225 IF (need_gradient) ALLOCATE (g(npoint, basis%nsgf, 3))
226 IF (need_hessian) ALLOCATE (h(npoint, basis%nsgf, 3, 3))
227 value = cmplx(0.0_dp, 0.0_dp, dp)
228 DO image = 1, SIZE(images, 2)
229 lattice = real(images(:, image), dp)
230 displacement(:, :) = points - spread(center + matmul(cell, lattice), 2, npoint)
231 ddisplacement(:, :) = -spread(dcenter + matmul(dc, lattice), 2, npoint)
232 IF (PRESENT(point_response)) THEN
233 cpassert(all(shape(point_response) == shape(points)))
234 ddisplacement(:, :) = ddisplacement + point_response
235 END IF
236 phase = exp(cmplx(0.0_dp, twopi*dot_product(reduced_k, lattice), dp))
237 phase_response = cmplx(0.0_dp, twopi*dot_product(dk, lattice), dp)*phase
238 IF (need_hessian) THEN
239 CALL gaussian_basis_fields_evaluate(basis, displacement, field, g, h, cutoff_squared)
240 ELSE IF (need_gradient) THEN
241 CALL gaussian_basis_fields_evaluate(basis, displacement, field, g, cutoff_squared=cutoff_squared)
242 ELSE
243 CALL gaussian_basis_fields_evaluate(basis, displacement, field, cutoff_squared=cutoff_squared)
244 END IF
245 value = value + phase*field
246 IF (PRESENT(gradient)) gradient = gradient + phase*g
247 IF (PRESENT(response)) THEN
248 response = response + phase_response*field
249 DO axis = 1, 3
250 response = response + phase*spread(ddisplacement(axis, :), 2, basis%nsgf)*g(:, :, axis)
251 END DO
252 END IF
253 IF (PRESENT(gradient_response)) THEN
254 gradient_response = gradient_response + phase_response*g
255 DO axis = 1, 3
256 DO axis2 = 1, 3
257 gradient_response(:, :, axis) = gradient_response(:, :, axis) + &
258 phase*spread(ddisplacement(axis2, :), 2, basis%nsgf)*h(:, :, axis, axis2)
259 END DO
260 END DO
261 END IF
262 END DO
263 END SUBROUTINE gaussian_basis_fields_periodic
264
265END MODULE gaussian_basis_fields
Native contracted Gaussian fields and their consistent Cartesian/Bloch responses. Uses the stored sph...
subroutine, public gaussian_basis_fields_evaluate(basis, displacements, value, gradient, hessian, cutoff_squared)
Evaluate a native contracted basis at explicit point-minus-center displacements. No minimum-image ope...
subroutine, public gaussian_basis_fields_periodic(basis, points, center, cell, images, reduced_k, value, gradient, response, gradient_response, point_response, center_response, cell_response, reduced_k_response, cutoff_squared)
Sum explicit Gaussian images with physical Bloch gradients and joint geometry response....
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
real(kind=dp), parameter, public twopi
Provides Cartesian and spherical orbital pointers and indices.
integer, dimension(:), allocatable, public ncoset
integer, dimension(:, :), allocatable, public indco