(git:3d5d2e5)
Loading...
Searching...
No Matches
upf_inverse_quadrature.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 Common point quadratures and semilocal adjoints for inverse UPF basis blocks.
10!> Point rows may be MPI-distributed; each supplied column block is replicated.
11!> Original projector augmentation remains a separate, explicitly supplied operator.
12! **************************************************************************************************
14 USE ieee_arithmetic, ONLY: ieee_is_finite
15 USE kinds, ONLY: dp
17#include "../base/base_uses.f90"
18
19 IMPLICIT NONE
20 PRIVATE
21
24
25CONTAINS
26
27! **************************************************************************************************
28!> \brief Integrate left^dagger * weights * right, without discarding intercenter terms.
29!> Use the same rule for beta/Delta, beta/Gaussian and all operator blocks.
30!> \param left Local point rows and selected left columns.
31!> \param right Matching local point rows and selected right columns.
32!> \param weights Original quadrature measure for the local points.
33!> \param integral New rectangular block, globally reduced if comm is supplied.
34!> \param comm Optional communicator partitioning point rows; empty ranks participate.
35! **************************************************************************************************
36 SUBROUTINE upf_inverse_pair(left, right, weights, integral, comm)
37 COMPLEX(dp), INTENT(IN) :: left(:, :), right(:, :)
38 REAL(dp), INTENT(IN) :: weights(:)
39 COMPLEX(dp), INTENT(OUT) :: integral(:, :)
40 TYPE(mp_comm_type), INTENT(IN), OPTIONAL :: comm
41
42 CALL check_pair(left, right, weights, shape(integral))
43 integral = cmplx(0.0_dp, 0.0_dp, dp)
44 CALL add_pair(left, right, weights, integral)
45 IF (PRESENT(comm)) CALL comm%sum(integral)
46 END SUBROUTINE upf_inverse_pair
47
48! **************************************************************************************************
49!> \brief Differentiate both field legs and the original quadrature measure.
50!> \param left ...
51!> \param right ...
52!> \param weights ...
53!> \param left_response Response of local left fields.
54!> \param right_response Response of local right fields.
55!> \param weights_response Response of the same local measure, including strain Jacobians.
56!> \param response New rectangular response block.
57!> \param comm ...
58! **************************************************************************************************
59 SUBROUTINE upf_inverse_pair_response(left, right, weights, left_response, right_response, &
60 weights_response, response, comm)
61 COMPLEX(dp), INTENT(IN) :: left(:, :), right(:, :)
62 REAL(dp), INTENT(IN) :: weights(:)
63 COMPLEX(dp), INTENT(IN) :: left_response(:, :), right_response(:, :)
64 REAL(dp), INTENT(IN) :: weights_response(:)
65 COMPLEX(dp), INTENT(OUT) :: response(:, :)
66 TYPE(mp_comm_type), INTENT(IN), OPTIONAL :: comm
67
68 CALL check_pair(left, right, weights, shape(response))
69 cpassert(all(shape(left_response) == shape(left)))
70 cpassert(all(shape(right_response) == shape(right)))
71 cpassert(SIZE(weights_response) == SIZE(weights))
72 response = cmplx(0.0_dp, 0.0_dp, dp)
73 CALL add_pair(left_response, right, weights, response)
74 CALL add_pair(left, right_response, weights, response)
75 CALL add_pair(left, right, weights_response, response)
76 IF (PRESENT(comm)) CALL comm%sum(response)
77 END SUBROUTINE upf_inverse_pair_response
78
79! **************************************************************************************************
80!> \brief Semilocal rectangular block for density, Cartesian density gradient and tau potentials.
81!> Integral of vn L*R + vg.(grad(L*)R+L*grad(R)) + vt grad(L*).grad(R)/2.
82!> vgradient is dE/d(grad n), not dE/dsigma. Cartesian gradients are physical Bloch
83!> gradients; supplying gradients of periodic factors alone would omit k terms.
84!> Apply to GG/GD/DG/DD blocks and retain the original augmentation pullback separately.
85!> \param left Local original Gaussian or difference-wave values.
86!> \param right Matching right values.
87!> \param left_gradient Cartesian spatial derivatives, axis last.
88!> \param right_gradient ...
89!> \param weights Original local quadrature measure.
90!> \param vdensity Potential conjugate to smooth density.
91!> \param vgradient Cartesian potential conjugate to its gradient, axis last.
92!> \param vtau Potential conjugate to tau=sum(f*|grad psi|**2)/2.
93!> \param integral New rectangular operator block.
94!> \param comm Optional point-row communicator; reduction occurs once per block.
95! **************************************************************************************************
96 SUBROUTINE upf_inverse_semilocal_pair(left, right, left_gradient, right_gradient, weights, &
97 vdensity, vgradient, vtau, integral, comm)
98 COMPLEX(dp), INTENT(IN) :: left(:, :), right(:, :), &
99 left_gradient(:, :, :), &
100 right_gradient(:, :, :)
101 REAL(dp), INTENT(IN) :: weights(:), vdensity(:), &
102 vgradient(:, :), vtau(:)
103 COMPLEX(dp), INTENT(OUT) :: integral(:, :)
104 TYPE(mp_comm_type), INTENT(IN), OPTIONAL :: comm
105
106 COMPLEX(dp), ALLOCATABLE :: work(:, :)
107 INTEGER :: axis, nright
108 LOGICAL :: gradient_active(3), tau_active
109
110 CALL check_semilocal(left, right, left_gradient, right_gradient, weights, &
111 vdensity, vgradient, vtau, shape(integral))
112 integral = cmplx(0.0_dp, 0.0_dp, dp)
113 IF (.NOT. any(weights /= 0.0_dp) .OR. &
114 .NOT. (any(vdensity /= 0.0_dp) .OR. any(vgradient /= 0.0_dp) .OR. any(vtau /= 0.0_dp))) THEN
115 IF (PRESENT(comm)) CALL comm%sum(integral)
116 RETURN
117 END IF
118 DO axis = 1, 3
119 gradient_active(axis) = any(vgradient(:, axis) /= 0.0_dp)
120 END DO
121 tau_active = any(vtau /= 0.0_dp)
122 nright = SIZE(right, 2)
123 ALLOCATE (work(SIZE(right, 1), nright))
124 work(:, :) = spread(vdensity, 2, nright)*right
125 DO axis = 1, 3
126 IF (.NOT. gradient_active(axis)) cycle
127 work(:, :) = work + spread(vgradient(:, axis), 2, nright)*right_gradient(:, :, axis)
128 END DO
129 CALL add_pair(left, work, weights, integral)
130 DO axis = 1, 3
131 IF (.NOT. gradient_active(axis) .AND. .NOT. tau_active) cycle
132 work(:, :) = spread(vgradient(:, axis), 2, nright)*right
133 IF (tau_active) work(:, :) = work + 0.5_dp*spread(vtau, 2, nright)*right_gradient(:, :, axis)
134 CALL add_pair(left_gradient(:, :, axis), work, weights, integral)
135 END DO
136 IF (PRESENT(comm)) CALL comm%sum(integral)
137 END SUBROUTINE upf_inverse_semilocal_pair
138
139! **************************************************************************************************
140!> \brief Response of the same semilocal block, including moving gradients, potentials and measure.
141!> Geometry responses of spatial gradients must include the inverse and field Hessians.
142!> \param left ...
143!> \param right ...
144!> \param left_gradient ...
145!> \param right_gradient ...
146!> \param weights ...
147!> \param vdensity ...
148!> \param vgradient ...
149!> \param vtau ...
150!> \param left_response ...
151!> \param right_response ...
152!> \param left_gradient_response ...
153!> \param right_gradient_response ...
154!> \param weights_response ...
155!> \param vdensity_response Zero for a frozen conjugate potential.
156!> \param vgradient_response ...
157!> \param vtau_response ...
158!> \param response New rectangular response block.
159!> \param comm ...
160! **************************************************************************************************
161 SUBROUTINE upf_inverse_semilocal_response(left, right, left_gradient, right_gradient, weights, &
162 vdensity, vgradient, vtau, left_response, right_response, &
163 left_gradient_response, right_gradient_response, &
164 weights_response, vdensity_response, vgradient_response, &
165 vtau_response, response, comm)
166 COMPLEX(dp), INTENT(IN) :: left(:, :), right(:, :), &
167 left_gradient(:, :, :), &
168 right_gradient(:, :, :)
169 REAL(dp), INTENT(IN) :: weights(:), vdensity(:), &
170 vgradient(:, :), vtau(:)
171 COMPLEX(dp), INTENT(IN) :: left_response(:, :), right_response(:, :), &
172 left_gradient_response(:, :, :), right_gradient_response(:, :, :)
173 REAL(dp), INTENT(IN) :: weights_response(:), &
174 vdensity_response(:), &
175 vgradient_response(:, :), &
176 vtau_response(:)
177 COMPLEX(dp), INTENT(OUT) :: response(:, :)
178 TYPE(mp_comm_type), INTENT(IN), OPTIONAL :: comm
179
180 COMPLEX(dp), ALLOCATABLE :: work(:, :)
181
182 CALL check_semilocal(left, right, left_gradient, right_gradient, weights, &
183 vdensity, vgradient, vtau, shape(response))
184 CALL check_semilocal(left_response, right_response, left_gradient_response, right_gradient_response, &
185 weights_response, vdensity_response, vgradient_response, vtau_response, shape(response))
186 cpassert(all(shape(left_response) == shape(left)))
187 cpassert(all(shape(right_response) == shape(right)))
188 ALLOCATE (work(SIZE(response, 1), SIZE(response, 2)))
189 ! Linearity in either field leg and in each potential/measure gives the complete response.
190 CALL upf_inverse_semilocal_pair(left_response, right, left_gradient_response, right_gradient, &
191 weights, vdensity, vgradient, vtau, response)
192 CALL upf_inverse_semilocal_pair(left, right_response, left_gradient, right_gradient_response, &
193 weights, vdensity, vgradient, vtau, work)
194 response = response + work
195 CALL upf_inverse_semilocal_pair(left, right, left_gradient, right_gradient, &
196 weights_response, vdensity, vgradient, vtau, work)
197 response = response + work
198 CALL upf_inverse_semilocal_pair(left, right, left_gradient, right_gradient, &
199 weights, vdensity_response, vgradient_response, vtau_response, work)
200 response = response + work
201 IF (PRESENT(comm)) CALL comm%sum(response)
202 END SUBROUTINE upf_inverse_semilocal_response
203
204! **************************************************************************************************
205!> \brief Smooth density descriptors whose adjoint is the semilocal pair operator.
206!> Per-spin AO density includes occupations and k weights. Values/gradients are U*chi.
207!> Caller must add original charge, gradient and independent kinetic augmentation.
208!> A charge kernel does not determine a missing kinetic augmentation kernel.
209!> \param soft Local inverse basis values.
210!> \param soft_gradient Physical Cartesian Bloch gradients, axis last.
211!> \param ao_density Hermitian density matrix in reconstructed Gaussian variables.
212!> \param density Smooth local point density.
213!> \param gradient Smooth Cartesian density gradient, axis last.
214!> \param tau Smooth kinetic-energy density, with factor one half.
215! **************************************************************************************************
216 SUBROUTINE upf_inverse_smooth_density(soft, soft_gradient, ao_density, density, gradient, tau)
217 COMPLEX(dp), INTENT(IN) :: soft(:, :), soft_gradient(:, :, :), &
218 ao_density(:, :)
219 REAL(dp), INTENT(OUT) :: density(:), gradient(:, :), tau(:)
220
221 COMPLEX(dp), ALLOCATABLE :: derivative(:, :), work(:, :)
222 INTEGER :: axis, n, npoint
223 REAL(dp) :: tolerance
224
225 n = SIZE(soft, 2)
226 npoint = SIZE(soft, 1)
227 cpassert(all(shape(ao_density) == [n, n]))
228 cpassert(all(shape(soft_gradient) == [npoint, n, 3]))
229 cpassert(SIZE(density) == npoint .AND. SIZE(tau) == npoint)
230 cpassert(all(shape(gradient) == [npoint, 3]))
231 IF (n > 0) THEN
232 tolerance = 128.0_dp*epsilon(1.0_dp)*max(1.0_dp, maxval(abs(ao_density)))
233 IF (.NOT. (maxval(abs(ao_density - conjg(transpose(ao_density)))) <= tolerance)) THEN
234 cpabort("Inconsistent inverse quadrature dimensions")
235 END IF
236 END IF
237 ALLOCATE (work(npoint, n), derivative(npoint, n))
238 work(:, :) = matmul(soft, ao_density)
239 density = real(sum(conjg(soft)*work, dim=2), dp)
240 tau = 0.0_dp
241 DO axis = 1, 3
242 gradient(:, axis) = 2.0_dp*real(sum(conjg(soft_gradient(:, :, axis))*work, dim=2), dp)
243 derivative(:, :) = matmul(soft_gradient(:, :, axis), ao_density)
244 tau = tau + 0.5_dp*real(sum(conjg(soft_gradient(:, :, axis))*derivative, dim=2), dp)
245 END DO
246 END SUBROUTINE upf_inverse_smooth_density
247
248! **************************************************************************************************
249!> \brief Accumulate a local rectangular contraction, allowing zero local points/columns.
250!> \param left ...
251!> \param right ...
252!> \param weights ...
253!> \param integral ...
254! **************************************************************************************************
255 SUBROUTINE add_pair(left, right, weights, integral)
256 COMPLEX(dp), INTENT(IN) :: left(:, :), right(:, :)
257 REAL(dp), INTENT(IN) :: weights(:)
258 COMPLEX(dp), INTENT(INOUT) :: integral(:, :)
259
260 COMPLEX(dp), ALLOCATABLE :: work(:, :)
261
262 IF (SIZE(weights) == 0 .OR. SIZE(left, 2) == 0 .OR. SIZE(right, 2) == 0) RETURN
263 IF (.NOT. any(weights /= 0.0_dp)) RETURN
264 ALLOCATE (work(SIZE(right, 1), SIZE(right, 2)))
265 work(:, :) = spread(weights, 2, SIZE(right, 2))*right
266 integral = integral + matmul(conjg(transpose(left)), work)
267 END SUBROUTINE add_pair
268
269! **************************************************************************************************
270!> \brief Validate local point dimensions and finite original measure.
271!> \param left ...
272!> \param right ...
273!> \param weights ...
274!> \param output_shape ...
275! **************************************************************************************************
276 SUBROUTINE check_pair(left, right, weights, output_shape)
277 COMPLEX(dp), INTENT(IN) :: left(:, :), right(:, :)
278 REAL(dp), INTENT(IN) :: weights(:)
279 INTEGER, INTENT(IN) :: output_shape(2)
280
281 IF (.NOT. (SIZE(left, 1) == SIZE(weights) .AND. SIZE(right, 1) == SIZE(weights))) THEN
282 cpabort("Inconsistent inverse quadrature dimensions")
283 END IF
284 cpassert(all(output_shape == [SIZE(left, 2), SIZE(right, 2)]))
285 cpassert(all(ieee_is_finite(weights)))
286 END SUBROUTINE check_pair
287
288! **************************************************************************************************
289!> \brief ...
290!> \param left ...
291!> \param right ...
292!> \param left_gradient ...
293!> \param right_gradient ...
294!> \param weights ...
295!> \param vdensity ...
296!> \param vgradient ...
297!> \param vtau ...
298!> \param output_shape ...
299! **************************************************************************************************
300 SUBROUTINE check_semilocal(left, right, left_gradient, right_gradient, weights, vdensity, vgradient, vtau, output_shape)
301 COMPLEX(dp), INTENT(IN) :: left(:, :), right(:, :), &
302 left_gradient(:, :, :), &
303 right_gradient(:, :, :)
304 REAL(dp), INTENT(IN) :: weights(:), vdensity(:), &
305 vgradient(:, :), vtau(:)
306 INTEGER, INTENT(IN) :: output_shape(2)
307
308 CALL check_pair(left, right, weights, output_shape)
309 IF (.NOT. (all(shape(left_gradient) == [SIZE(left, 1), SIZE(left, 2), 3]))) THEN
310 cpabort("Inconsistent inverse quadrature dimensions")
311 END IF
312 IF (.NOT. (all(shape(right_gradient) == [SIZE(right, 1), SIZE(right, 2), 3]))) THEN
313 cpabort("Inconsistent inverse quadrature dimensions")
314 END IF
315 IF (.NOT. (SIZE(vdensity) == SIZE(weights) .AND. SIZE(vtau) == SIZE(weights))) THEN
316 cpabort("Inconsistent inverse quadrature dimensions")
317 END IF
318 cpassert(all(shape(vgradient) == [SIZE(weights), 3]))
319 END SUBROUTINE check_semilocal
320
321END MODULE upf_inverse_quadrature
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Interface to the message passing library MPI.
Common point quadratures and semilocal adjoints for inverse UPF basis blocks. Point rows may be MPI-d...
subroutine, public upf_inverse_semilocal_response(left, right, left_gradient, right_gradient, weights, vdensity, vgradient, vtau, left_response, right_response, left_gradient_response, right_gradient_response, weights_response, vdensity_response, vgradient_response, vtau_response, response, comm)
Response of the same semilocal block, including moving gradients, potentials and measure....
subroutine, public upf_inverse_pair(left, right, weights, integral, comm)
Integrate left^dagger * weights * right, without discarding intercenter terms. Use the same rule for ...
subroutine, public upf_inverse_smooth_density(soft, soft_gradient, ao_density, density, gradient, tau)
Smooth density descriptors whose adjoint is the semilocal pair operator. Per-spin AO density includes...
subroutine, public upf_inverse_pair_response(left, right, weights, left_response, right_response, weights_response, response, comm)
Differentiate both field legs and the original quadrature measure.
subroutine, public upf_inverse_semilocal_pair(left, right, left_gradient, right_gradient, weights, vdensity, vgradient, vtau, integral, comm)
Semilocal rectangular block for density, Cartesian density gradient and tau potentials....