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
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)))
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]))
62 IF (
PRESENT(hessian))
THEN
63 cpassert(all(shape(hessian) == [
SIZE(
value, 1),
SIZE(
value, 2), 3, 3]))
73 DO ipoint = 1,
SIZE(displacements, 2)
74 r = displacements(:, ipoint)
76 IF (
PRESENT(cutoff_squared))
THEN
77 IF (r_squared > cutoff_squared) cycle
79 coordinate_power(:, 0) = 1.0_dp
80 DO l = 1, ubound(coordinate_power, 2)
81 coordinate_power(:, l) = coordinate_power(:, l - 1)*r
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
92 IF (powers(axis) == 0) cycle
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
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
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)
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)
125 primitive_gradient(axis) = polynomial_gradient(axis) - 2.0_dp*alpha*r(axis)*polynomial
126 IF (.NOT.
PRESENT(hessian)) cycle
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
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
142 IF (
PRESENT(gradient))
THEN
143 gradient(ipoint, isgf, axis) = gradient(ipoint, isgf, axis) + weight*primitive_gradient(axis)
145 IF (.NOT.
PRESENT(hessian)) cycle
147 hessian(ipoint, isgf, axis, axis2) = hessian(ipoint, isgf, axis, axis2) + &
148 weight*primitive_hessian(axis, axis2)
180 response, gradient_response, point_response, center_response, &
181 cell_response, reduced_k_response, cutoff_squared)
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
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(:, :, :, :)
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)
210 IF (
PRESENT(response))
THEN
211 cpassert(all(shape(response) == shape(
value)))
212 response = cmplx(0.0_dp, 0.0_dp,
dp)
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)
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
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
240 ELSE IF (need_gradient)
THEN
245 value =
value + phase*field
246 IF (
PRESENT(gradient)) gradient = gradient + phase*g
247 IF (
PRESENT(response))
THEN
248 response = response + phase_response*field
250 response = response + phase*spread(ddisplacement(axis, :), 2, basis%nsgf)*g(:, :, axis)
253 IF (
PRESENT(gradient_response))
THEN
254 gradient_response = gradient_response + phase_response*g
257 gradient_response(:, :, axis) = gradient_response(:, :, axis) + &
258 phase*spread(ddisplacement(axis2, :), 2, basis%nsgf)*h(:, :, axis, axis2)