(git:3d5d2e5)
Loading...
Searching...
No Matches
upf_inverse.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 Inverse of a specified finite-rank UPF reconstruction, including intercenter coupling.
10!> Delta contains original AE-minus-PS waves, P the original projector bras.
11!> For K=I+P*Delta and B=P*chi, W=K^-1*B gives U*chi=chi-Delta*W and P*U*chi=W.
12!> Atomic hard projectors use the nonoverlapping one-center construction. The general
13!> complex inverse remains available for checking the original functional explicitly.
14! **************************************************************************************************
16 USE atom_upf, ONLY: atom_upfpot_type
17 USE ieee_arithmetic, ONLY: ieee_is_finite
18 USE kinds, ONLY: dp
19 USE mathconstants, ONLY: fourpi
26#include "../base/base_uses.f90"
27
28 IMPLICIT NONE
29 PRIVATE
30
32 INTEGER :: nproj = 0
33 LOGICAL :: ready = .false.
34 REAL(kind=dp) :: rcond = 0.0_dp
35 COMPLEX(KIND=dp), ALLOCATABLE :: lu(:, :)
36 INTEGER, ALLOCATABLE :: pivots(:)
37 END TYPE upf_inverse_type
38
42
44 ! The projector coupling is D-T in the original partial-wave channel order.
45 TYPE(upf_projector_type) :: hard, difference
46 TYPE(upf_inverse_type) :: inverse
47 REAL(dp), ALLOCATABLE :: kinetic(:, :), metric(:, :), wave_overlap(:, :), difference_kinetic(:, :), difference_overlap(:, :)
49
58
59CONTAINS
60
61! **************************************************************************************************
62!> \brief Local hard projectors and D-T for the nonoverlapping partial-wave construction.
63!> Both factors of a nonlocal term use these transformed projectors. Original Q and
64!> norm data remain independent and are not replaced by a partial-wave identity here.
65!> The hard reference may be an AE or a harder NC wave, as supplied by its generator.
66!> \param pot Original scalar NC/USPP and compatible projector-indexed hard/soft reference pairs.
67!> \param degree Largest Gaussian Cartesian degree plus derivatives or moments.
68!> \param atomic New owned one-center inverse, hard projectors and kinetic correction.
69!> \param info Zero on success, otherwise the atomic inverse-factorization status.
70!> \param direct Use original projectors and soft orbitals without reconstruction.
71! **************************************************************************************************
72 SUBROUTINE upf_inverse_atomic_init(pot, degree, atomic, info, direct)
73 TYPE(atom_upfpot_type), INTENT(IN), TARGET :: pot
74 INTEGER, INTENT(IN) :: degree
75 TYPE(upf_inverse_atomic_type), INTENT(OUT) :: atomic
76 INTEGER, INTENT(OUT) :: info
77 LOGICAL, INTENT(IN), OPTIONAL :: direct
78
79 CALL prepare_atomic(pot, degree, atomic, info, radial_only=.false., direct=direct)
80 END SUBROUTINE upf_inverse_atomic_init
81
82! **************************************************************************************************
83!> \brief Prepare the local difference integrals without factorizing a local inverse.
84!> \param pot Original scalar USPP dataset.
85!> \param degree Largest Gaussian Cartesian degree including derivatives.
86!> \param atomic Difference projectors and their radial matrices.
87! **************************************************************************************************
88 SUBROUTINE upf_inverse_atomic_difference_init(pot, degree, atomic)
89 TYPE(atom_upfpot_type), INTENT(IN), TARGET :: pot
90 INTEGER, INTENT(IN) :: degree
91 TYPE(upf_inverse_atomic_type), INTENT(OUT) :: atomic
92
93 INTEGER :: info
94
95 CALL prepare_atomic(pot, degree, atomic, info, radial_only=.true.)
96 cpassert(info == 0)
98
99! **************************************************************************************************
100!> \brief Common radial integrals for the local and coupled representations.
101!> \param pot ...
102!> \param degree ...
103!> \param atomic ...
104!> \param info ...
105!> \param radial_only Skip the local factorization and the associated hard projectors.
106!> \param direct ...
107! **************************************************************************************************
108 SUBROUTINE prepare_atomic(pot, degree, atomic, info, radial_only, direct)
109 TYPE(atom_upfpot_type), INTENT(IN), TARGET :: pot
110 INTEGER, INTENT(IN) :: degree
111 TYPE(upf_inverse_atomic_type), INTENT(OUT) :: atomic
112 INTEGER, INTENT(OUT) :: info
113 LOGICAL, INTENT(IN) :: radial_only
114 LOGICAL, INTENT(IN), OPTIONAL :: direct
115
116 COMPLEX(dp), ALLOCATABLE :: coupling(:, :), identity(:, :), &
117 rotation(:, :)
118 INTEGER :: i, j, nb, np, p, q
119 REAL(dp) :: norm_difference
120 REAL(dp), ALLOCATABLE :: delta_kinetic(:, :), &
121 delta_overlap(:, :), &
122 radial_kinetic(:, :), radial_weights(:)
123 TYPE(atom_upfpot_type) :: hard, radial
124 TYPE(upf_inverse_kinetic_type) :: kinetic
125
126 nb = pot%number_of_proj
127 cpassert(nb > 0)
128 info = 0
129 IF (pot%is_paw .OR. trim(pot%pseudo_type) == "PAW") THEN
130 cpabort("Atom-local inverse PAW fixed-term accounting is not implemented")
131 END IF
132 cpassert(pot%is_ultrasoft)
133 IF (PRESENT(direct)) THEN
134 IF (direct) THEN
135 CALL upf_projector_init(pot, degree, atomic%hard)
136 CALL compact_projector(atomic%hard)
137 RETURN
138 END IF
139 END IF
140 CALL inverse_radial_data(pot, radial)
141 CALL upf_projector_init(radial, degree, atomic%difference)
142 CALL compact_projector(atomic%difference)
143 CALL upf_inverse_kinetic_init(pot, kinetic)
144 CALL upf_inverse_kinetic_matrix(pot, kinetic, radial_kinetic, delta_kinetic, delta_overlap)
145 ALLOCATE (radial_weights, source=upf_radial_weights(pot))
146 IF (.NOT. radial_only) THEN
147 ALLOCATE (coupling(nb, nb), identity(nb, nb), rotation(nb, nb))
148 coupling(:, :) = inverse_radial_coupling(pot, radial, radial_weights)
149 identity = cmplx(0.0_dp, 0.0_dp, dp)
150 DO j = 1, nb
151 identity(j, j) = cmplx(1.0_dp, 0.0_dp, dp)
152 END DO
153 CALL upf_inverse_init(coupling, atomic%inverse, info)
154 IF (info /= 0) RETURN
155 CALL upf_inverse_project(atomic%inverse, identity, rotation)
156 ! Bra_i = sum_j K^-1_ij beta_j^*. The stored real ket uses the transpose.
157 hard%number_of_proj = nb
158 hard%has_so = pot%has_so
159 ALLOCATE (hard%r, source=pot%r)
160 ALLOCATE (hard%rab, source=pot%rab)
161 ALLOCATE (hard%lbeta, source=pot%lbeta)
162 ALLOCATE (hard%beta, source=matmul(pot%beta, real(transpose(rotation), dp)))
163 ALLOCATE (hard%dion, source=pot%dion - radial_kinetic)
164 CALL upf_projector_init(hard, degree, atomic%hard)
165 CALL compact_projector(atomic%hard)
166 END IF
167 np = atomic%difference%nproj
168 ALLOCATE (atomic%kinetic(np, np), atomic%metric(np, np), atomic%wave_overlap(np, np), &
169 atomic%difference_kinetic(np, np), atomic%difference_overlap(np, np), source=0.0_dp)
170 cpassert(ALLOCATED(pot%qij))
171 cpassert(all(shape(pot%qij) == [nb, nb]))
172 DO q = 1, np
173 j = atomic%difference%radial_index(q)
174 DO p = 1, np
175 IF (atomic%difference%angular_index(p) /= atomic%difference%angular_index(q)) cycle
176 i = atomic%difference%radial_index(p)
177 atomic%kinetic(p, q) = radial_kinetic(i, j)
178 atomic%difference_kinetic(p, q) = delta_kinetic(i, j)
179 atomic%difference_overlap(p, q) = delta_overlap(i, j)
180 ! The Gaussian norm already contains the hard-wave norm within local closure.
181 norm_difference = sum(radial_weights*( &
182 pot%partial_waves%ae_wfc(:, i)*pot%partial_waves%ae_wfc(:, j) - &
183 pot%partial_waves%ps_wfc(:, i)*pot%partial_waves%ps_wfc(:, j)))
184 atomic%metric(p, q) = pot%qij(i, j) - norm_difference
185 atomic%wave_overlap(p, q) = norm_difference
186 END DO
187 END DO
188 END SUBROUTINE prepare_atomic
189
190! **************************************************************************************************
191!> \brief Drop exactly zero trailing quadrature rows without changing radial weights or support.
192!> \param projector Prepared radial integrals, including their original quadrature weights.
193! **************************************************************************************************
194 SUBROUTINE compact_projector(projector)
195 TYPE(upf_projector_type), INTENT(INOUT) :: projector
196
197 INTEGER :: last
198 REAL(dp), ALLOCATABLE :: radial(:), weighted(:, :)
199
200 last = SIZE(projector%r)
201 DO WHILE (last > 1)
202 IF (any(projector%weighted_beta(last, :) /= 0.0_dp)) EXIT
203 last = last - 1
204 END DO
205 IF (last == SIZE(projector%r)) RETURN
206 ALLOCATE (radial, source=projector%r(:last))
207 ALLOCATE (weighted, source=projector%weighted_beta(:last, :))
208 CALL move_alloc(radial, projector%r)
209 CALL move_alloc(weighted, projector%weighted_beta)
210 END SUBROUTINE compact_projector
211
212! **************************************************************************************************
213!> \brief Full radial-channel kinetic matrix from the same interpolated hard/soft reference waves.
214!> The spherical-harmonic addition theorem integrates the angular gradient products
215!> analytically. Off-diagonal channels are retained, with one half in Hartree units.
216!> Only the compact difference contributes, so reference-wave tails cancel exactly.
217!> \param pot Original radial mesh, quadrature and angular channel order.
218!> \param kinetic Compatible reference-wave interpolants.
219!> \param matrix Owned symmetric radial-channel T_hard-T_soft, without magnetic multiplicity.
220!> \param difference_kinetic Optional owned Delta kinetic matrix, required with difference_overlap.
221!> \param difference_overlap Optional owned Delta norm matrix, required with difference_kinetic.
222! **************************************************************************************************
223 SUBROUTINE upf_inverse_kinetic_matrix(pot, kinetic, matrix, difference_kinetic, difference_overlap)
224 TYPE(atom_upfpot_type), INTENT(IN) :: pot
225 TYPE(upf_inverse_kinetic_type), INTENT(IN) :: kinetic
226 REAL(dp), ALLOCATABLE, INTENT(OUT) :: matrix(:, :)
227 REAL(dp), ALLOCATABLE, INTENT(OUT), OPTIONAL :: difference_kinetic(:, :), &
228 difference_overlap(:, :)
229
230 INTEGER :: i, ir, j, l, m, nb, p, q
231 INTEGER, ALLOCATABLE :: first(:)
232 REAL(dp) :: factor
233 REAL(dp), ALLOCATABLE :: gradient(:, :), kernel(:, :), value(:), &
234 weights(:)
235
236 nb = SIZE(pot%lbeta)
237 cpassert(kinetic%delta%nproj == sum(2*pot%lbeta + 1))
238 cpassert(all(kinetic%delta%lbeta == pot%lbeta))
239 ALLOCATE (matrix(nb, nb), source=0.0_dp)
240 ALLOCATE (kernel(kinetic%delta%nproj, kinetic%delta%nproj), first(nb))
241 ALLOCATE (weights, source=upf_radial_weights(pot)*pot%r**2)
242 cpassert(PRESENT(difference_kinetic) .EQV. PRESENT(difference_overlap))
243 IF (PRESENT(difference_kinetic)) THEN
244 ALLOCATE (difference_kinetic(nb, nb), difference_overlap(nb, nb), &
245 value(kinetic%delta%nproj), gradient(3, kinetic%delta%nproj))
246 difference_kinetic = 0.0_dp
247 difference_overlap = 0.0_dp
248 END IF
249 p = 1
250 DO i = 1, nb
251 first(i) = p
252 p = p + 2*pot%lbeta(i) + 1
253 END DO
254 DO ir = 1, SIZE(pot%r)
255 IF (weights(ir) == 0.0_dp) cycle
256 IF (pot%r(ir) >= kinetic%delta%radius) EXIT
257 CALL upf_inverse_kinetic_evaluate(kinetic, [0.0_dp, 0.0_dp, pot%r(ir)], kernel)
258 IF (PRESENT(difference_kinetic)) THEN
259 CALL upf_radial_fields_evaluate(kinetic%delta, [0.0_dp, 0.0_dp, pot%r(ir)], value, gradient)
260 END IF
261 DO j = 1, nb
262 l = pot%lbeta(j)
263 factor = fourpi*weights(ir)/real(2*l + 1, dp)
264 DO i = 1, j
265 IF (pot%lbeta(i) /= l) cycle
266 DO m = 0, 2*l
267 p = first(i) + m
268 q = first(j) + m
269 matrix(i, j) = matrix(i, j) + factor*kernel(p, q)
270 IF (PRESENT(difference_kinetic)) THEN
271 difference_kinetic(i, j) = difference_kinetic(i, j) + 0.5_dp*factor*dot_product(gradient(:, p), gradient(:, q))
272 difference_overlap(i, j) = difference_overlap(i, j) + factor*value(p)*value(q)
273 END IF
274 END DO
275 END DO
276 END DO
277 END DO
278 DO j = 1, nb
279 DO i = 1, j - 1
280 matrix(j, i) = matrix(i, j)
281 IF (PRESENT(difference_kinetic)) THEN
282 difference_kinetic(j, i) = difference_kinetic(i, j)
283 difference_overlap(j, i) = difference_overlap(i, j)
284 END IF
285 END DO
286 END DO
287 END SUBROUTINE upf_inverse_kinetic_matrix
288
289! **************************************************************************************************
290!> \brief Finite partial-wave closure residuals added to Gaussian T plus hard D-T and Gaussian S.
291!> These vanish only when the local hard-wave expansion satisfies the corresponding
292!> operator identity. Nonoverlap alone does not remove them on an arbitrary AO space.
293!> \param atomic Original one-center matrices and transformed hard projectors.
294!> \param coefficients Hard projector bras against Gaussian AO columns.
295!> \param delta_overlap Delta bras against the same Gaussian columns.
296!> \param delta_kinetic Delta bras against the Gaussian kinetic operator.
297!> \param kinetic_residual Owned AO correction beyond hard D-T.
298!> \param metric_residual Owned AO correction beyond the original q minus wave-norm residual.
299! **************************************************************************************************
300 SUBROUTINE upf_inverse_atomic_residual(atomic, coefficients, delta_overlap, delta_kinetic, &
301 kinetic_residual, metric_residual)
302 TYPE(upf_inverse_atomic_type), INTENT(IN) :: atomic
303 COMPLEX(dp), INTENT(IN) :: coefficients(:, :), delta_overlap(:, :), &
304 delta_kinetic(:, :)
305 COMPLEX(dp), ALLOCATABLE, INTENT(OUT) :: kinetic_residual(:, :), &
306 metric_residual(:, :)
307
308 COMPLEX(dp), ALLOCATABLE :: mixed(:, :)
309 INTEGER :: n, np
310
311 np = SIZE(atomic%difference_kinetic, 1)
312 n = SIZE(coefficients, 2)
313 cpassert(SIZE(coefficients, 1) == np)
314 cpassert(all(shape(delta_overlap) == [np, n]))
315 cpassert(all(shape(delta_kinetic) == [np, n]))
316 ALLOCATE (mixed, source=matmul(conjg(transpose(delta_kinetic)), coefficients))
317 ALLOCATE (kinetic_residual, source=-mixed - conjg(transpose(mixed)) + &
318 matmul(conjg(transpose(coefficients)), matmul(atomic%difference_kinetic + atomic%kinetic, coefficients)))
319 mixed(:, :) = matmul(conjg(transpose(delta_overlap)), coefficients)
320 ALLOCATE (metric_residual, source=-mixed - conjg(transpose(mixed)) + &
321 matmul(conjg(transpose(coefficients)), matmul(atomic%difference_overlap + atomic%wave_overlap, coefficients)))
322 END SUBROUTINE upf_inverse_atomic_residual
323
324! **************************************************************************************************
325!> \brief Independent valence kinetic augmentation from original projector-indexed AE/PS pairs.
326!> Charge Q is not used. The noncompact PS table is retained over its full supplied mesh.
327!> Only the compact AE-minus-PS difference is evaluated outside the reconstruction region.
328!> This kernel does not supply a frozen-core kinetic density or a pseudization of tau.
329!> \param pot Original scalar dataset with compatible full-wave pairs.
330!> \param kinetic New radial caches in the original magnetic-projector order.
331! **************************************************************************************************
332 SUBROUTINE upf_inverse_kinetic_init(pot, kinetic)
333 TYPE(atom_upfpot_type), INTENT(IN) :: pot
334 TYPE(upf_inverse_kinetic_type), INTENT(OUT) :: kinetic
335
336 INTEGER :: i
337 TYPE(atom_upfpot_type) :: radial
338
339 CALL inverse_radial_data(pot, radial)
340 CALL upf_radial_fields_init(radial%r, radial%beta, radial%lbeta, kinetic%delta)
341 DO i = 1, pot%number_of_proj
342 IF (pot%r(1) == 0.0_dp) THEN
343 radial%beta(2:, i) = pot%partial_waves%ps_wfc(2:, i)/pot%r(2:)
344 radial%beta(1, i) = 0.0_dp
345 ELSE
346 radial%beta(:, i) = pot%partial_waves%ps_wfc(:, i)/pot%r
347 END IF
348 END DO
349 CALL upf_radial_fields_init(radial%r, radial%beta, radial%lbeta, kinetic%pseudo, finite_table=.true.)
350 END SUBROUTINE upf_inverse_kinetic_init
351
352! **************************************************************************************************
353!> \brief One-half grad(AE_i).grad(AE_j) minus one-half grad(PS_i).grad(PS_j).
354!> Factoring through Delta preserves an exact zero outside its compact support.
355!> Values and spatial derivatives use the same interpolated reference waves.
356!> \param kinetic Original reference-wave caches, independent of the supplied charge Q.
357!> \param displacement Point minus original center, in bohr.
358!> \param matrix Symmetric kinetic augmentation in the native magnetic-projector order.
359!> \param gradient Optional Cartesian point gradient. Center motion has the opposite sign.
360! **************************************************************************************************
361 SUBROUTINE upf_inverse_kinetic_evaluate(kinetic, displacement, matrix, gradient)
362 TYPE(upf_inverse_kinetic_type), INTENT(IN) :: kinetic
363 REAL(dp), INTENT(IN) :: displacement(3)
364 REAL(dp), INTENT(OUT) :: matrix(:, :)
365 REAL(dp), INTENT(OUT), OPTIONAL :: gradient(:, :, :)
366
367 INTEGER :: axis, i, j, n
368 REAL(dp), ALLOCATABLE :: d(:, :), dd(:, :, :), p(:, :), &
369 pd(:, :, :), value(:)
370
371 n = kinetic%delta%nproj
372 cpassert(kinetic%pseudo%nproj == n .AND. all(shape(matrix) == [n, n]))
373 matrix = 0.0_dp
374 IF (PRESENT(gradient)) THEN
375 cpassert(all(shape(gradient) == [n, n, 3]))
376 gradient = 0.0_dp
377 END IF
378 IF (sum(displacement**2) >= kinetic%delta%radius**2) RETURN
379 ALLOCATE (value(n), d(3, n), p(3, n))
380 IF (PRESENT(gradient)) THEN
381 ALLOCATE (dd(3, 3, n), pd(3, 3, n))
382 CALL upf_radial_fields_evaluate(kinetic%delta, displacement, value, d, dd)
383 CALL upf_radial_fields_evaluate(kinetic%pseudo, displacement, value, p, pd)
384 ELSE
385 CALL upf_radial_fields_evaluate(kinetic%delta, displacement, value, d)
386 CALL upf_radial_fields_evaluate(kinetic%pseudo, displacement, value, p)
387 END IF
388 DO j = 1, n
389 DO i = 1, j
390 matrix(i, j) = 0.5_dp*(dot_product(d(:, i), d(:, j)) + &
391 dot_product(d(:, i), p(:, j)) + dot_product(p(:, i), d(:, j)))
392 matrix(j, i) = matrix(i, j)
393 IF (PRESENT(gradient)) THEN
394 DO axis = 1, 3
395 gradient(i, j, axis) = 0.5_dp*(dot_product(dd(:, axis, i), d(:, j) + p(:, j)) + &
396 dot_product(d(:, i) + p(:, i), dd(:, axis, j)) + &
397 dot_product(pd(:, axis, i), d(:, j)) + &
398 dot_product(d(:, i), pd(:, axis, j)))
399 gradient(j, i, axis) = gradient(i, j, axis)
400 END DO
401 END IF
402 END DO
403 END DO
404 END SUBROUTINE upf_inverse_kinetic_evaluate
405
406! **************************************************************************************************
407!> \brief Prepare original AE-minus-PS radial overlaps in the native projector/harmonic order.
408!> The ordinary radial/Gaussian integrator supplies overlaps and AO-center derivatives.
409!> This is an overlap cache, not a replacement for the original nonlocal operator.
410!> \param pot Original data with a complete full-wave pair for every beta projector.
411!> \param degree Required Cartesian degree, including derivatives or moments.
412!> \param delta Original radial quadrature and angular maps for (u_AE-u_PS)/r.
413! **************************************************************************************************
414 SUBROUTINE upf_inverse_delta_init(pot, degree, delta)
415 TYPE(atom_upfpot_type), INTENT(IN) :: pot
416 INTEGER, INTENT(IN) :: degree
417 TYPE(upf_projector_type), INTENT(OUT) :: delta
418
419 TYPE(atom_upfpot_type) :: radial
420
421 CALL inverse_radial_data(pot, radial)
422 CALL upf_projector_init(radial, degree, delta)
423 END SUBROUTINE upf_inverse_delta_init
424
425! **************************************************************************************************
426!> \brief Prepare original beta and reconstruction fields for common intercenter quadratures.
427!> Native radial channels and real harmonics agree with the Gaussian overlap cache.
428!> NC channels have zero reconstruction but retain their original projector bras.
429!> \param pot Original UPF data, with complete full waves for reconstructing channels.
430!> \param beta Original projector fields, without weights.
431!> \param delta Original reconstruction differences, without weights.
432!> \param direct Use original projectors and soft orbitals without reconstruction.
433!> \param home_coupling Optional on-site overlaps between the projectors and the reconstruction differences.
434! **************************************************************************************************
435 SUBROUTINE upf_inverse_fields_init(pot, beta, delta, direct, home_coupling)
436 TYPE(atom_upfpot_type), INTENT(IN) :: pot
437 TYPE(upf_radial_fields_type), INTENT(OUT) :: beta, delta
438 LOGICAL, INTENT(IN), OPTIONAL :: direct
439 REAL(dp), ALLOCATABLE, INTENT(OUT), OPTIONAL :: home_coupling(:, :)
440
441 INTEGER :: p, q
442 REAL(dp), ALLOCATABLE :: radial_coupling(:, :)
443 TYPE(atom_upfpot_type) :: radial
444
445 CALL upf_radial_fields_init(pot%r, pot%beta, pot%lbeta, beta)
446 IF (PRESENT(home_coupling)) ALLOCATE (home_coupling(beta%nproj, beta%nproj), source=0.0_dp)
447 IF (PRESENT(direct)) THEN
448 IF (direct) THEN
449 ALLOCATE (radial%beta(SIZE(pot%r), SIZE(pot%lbeta)), source=0.0_dp)
450 CALL upf_radial_fields_init(pot%r, radial%beta, pot%lbeta, delta)
451 RETURN
452 END IF
453 END IF
454 IF (trim(pot%pseudo_type) == "NC" .AND. .NOT. pot%is_ultrasoft .AND. .NOT. pot%is_paw) THEN
455 IF (pot%has_so) cpabort("Spin-orbit UPF inverse fields are not supported")
456 ALLOCATE (radial%beta(SIZE(pot%r), SIZE(pot%lbeta)))
457 radial%beta = 0.0_dp
458 CALL upf_radial_fields_init(pot%r, radial%beta, pot%lbeta, delta)
459 ELSE
460 CALL inverse_radial_data(pot, radial)
461 CALL upf_radial_fields_init(radial%r, radial%beta, radial%lbeta, delta)
462 IF (PRESENT(home_coupling)) THEN
463 ALLOCATE (radial_coupling(pot%number_of_proj, pot%number_of_proj))
464 radial_coupling(:, :) = inverse_radial_coupling(pot, radial, upf_radial_weights(pot))
465 DO q = 1, delta%nproj
466 DO p = 1, beta%nproj
467 IF (beta%angular_index(p) /= delta%angular_index(q)) cycle
468 home_coupling(p, q) = radial_coupling(beta%radial_index(p), delta%radial_index(q))
469 END DO
470 END DO
471 END IF
472 END IF
473 END SUBROUTINE upf_inverse_fields_init
474
475! **************************************************************************************************
476!> \brief Integrate the same-center beta-Delta block on the original radial mesh.
477!> \param pot Original UPF projectors.
478!> \param radial Reconstruction differences on the same mesh.
479!> \param radial_weights Original UPF radial integration weights.
480!> \return Coupling in the original radial channel order.
481! **************************************************************************************************
482 FUNCTION inverse_radial_coupling(pot, radial, radial_weights) RESULT(coupling)
483 TYPE(atom_upfpot_type), INTENT(IN) :: pot, radial
484 REAL(dp), INTENT(IN) :: radial_weights(:)
485 REAL(dp) :: coupling(pot%number_of_proj, pot%number_of_proj)
486
487 INTEGER :: i, j
488 REAL(dp) :: weights(size(radial_weights))
489
490 weights = radial_weights*pot%r**2
491 coupling = 0.0_dp
492 DO j = 1, pot%number_of_proj
493 DO i = 1, pot%number_of_proj
494 IF (pot%lbeta(i) /= pot%lbeta(j)) cycle
495 coupling(i, j) = sum(weights*pot%beta(:, i)*radial%beta(:, j))
496 END DO
497 END DO
498 END FUNCTION inverse_radial_coupling
499
500! **************************************************************************************************
501!> \brief Assemble original reconstruction differences for overlap and point-field caches.
502!> \param pot Original UPF data.
503!> \param radial Unweighted original differences in the native radial-channel order.
504! **************************************************************************************************
505 SUBROUTINE inverse_radial_data(pot, radial)
506 TYPE(atom_upfpot_type), INTENT(IN) :: pot
507 TYPE(atom_upfpot_type), INTENT(OUT) :: radial
508
509 INTEGER :: i, n, nb
510
511 IF (pot%has_so) THEN
512 cpabort("Spin-orbit UPF inverse fields are not supported")
513 END IF
514 IF (.NOT. ALLOCATED(pot%partial_waves)) THEN
515 cpabort("Inverse UPF requires original AE/PS full waves for every projector")
516 END IF
517 cpassert(ALLOCATED(pot%r) .AND. ALLOCATED(pot%rab))
518 cpassert(ALLOCATED(pot%lbeta))
519 IF (.NOT. (ALLOCATED(pot%partial_waves%ae_wfc) .AND. ALLOCATED(pot%partial_waves%ps_wfc))) THEN
520 cpabort("Incomplete original AE/PS full waves")
521 END IF
522 cpassert(ALLOCATED(pot%partial_waves%angular_momenta))
523 n = SIZE(pot%r)
524 nb = pot%number_of_proj
525 cpassert(n >= 3 .AND. nb > 0 .AND. all(pot%r >= 0.0_dp))
526 cpassert(SIZE(pot%rab) == n .AND. SIZE(pot%lbeta) == nb)
527 cpassert(all(shape(pot%partial_waves%ae_wfc) == [n, nb]))
528 cpassert(all(shape(pot%partial_waves%ps_wfc) == [n, nb]))
529 cpassert(all(pot%partial_waves%angular_momenta == pot%lbeta))
530 cpassert(all(ieee_is_finite(pot%partial_waves%ae_wfc)))
531 cpassert(all(ieee_is_finite(pot%partial_waves%ps_wfc)))
532 radial%number_of_proj = nb
533 radial%has_so = pot%has_so
534 ALLOCATE (radial%r, source=pot%r)
535 ALLOCATE (radial%rab, source=pot%rab)
536 ALLOCATE (radial%lbeta, source=pot%lbeta)
537 ALLOCATE (radial%beta(n, nb), radial%dion(nb, nb))
538 radial%dion(:, :) = 0.0_dp
539 DO i = 1, nb
540 radial%beta(:, i) = pot%partial_waves%ae_wfc(:, i) - pot%partial_waves%ps_wfc(:, i)
541 IF (pot%r(1) == 0.0_dp) THEN
542 radial%beta(2:, i) = radial%beta(2:, i)/pot%r(2:)
543 ! Zero quadrature weight at r=0; this storage does not define a field continuation.
544 radial%beta(1, i) = 0.0_dp
545 ELSE
546 radial%beta(:, i) = radial%beta(:, i)/pot%r
547 END IF
548 END DO
549 END SUBROUTINE inverse_radial_data
550
551! **************************************************************************************************
552!> \brief Factor K with original projector normalization and all supplied intercenter blocks.
553!> \param beta_delta P*Delta, including periodic images and Bloch phases.
554!> \param inverse New factors; a failed construction cannot reuse an earlier factorization.
555!> \param info Zero on success, LAPACK pivot on singularity, nproj+1 for poor conditioning,
556!> nproj+2 for nonfinite data. Failed factors must not be used.
557!> \param minimum_rcond Optional condition limit; no regularization changes the reconstruction.
558! **************************************************************************************************
559 SUBROUTINE upf_inverse_init(beta_delta, inverse, info, minimum_rcond)
560 COMPLEX(KIND=dp), INTENT(IN) :: beta_delta(:, :)
561 TYPE(upf_inverse_type), INTENT(OUT) :: inverse
562 INTEGER, INTENT(OUT) :: info
563 REAL(kind=dp), INTENT(IN), OPTIONAL :: minimum_rcond
564
565 COMPLEX(KIND=dp), ALLOCATABLE :: work(:)
566 INTEGER :: i, n
567 REAL(kind=dp) :: anorm, limit
568 REAL(kind=dp), ALLOCATABLE :: rwork(:)
569 REAL(kind=dp), EXTERNAL :: zlange
570
571 EXTERNAL zgecon, zgetrf
572
573 n = SIZE(beta_delta, 1)
574 cpassert(SIZE(beta_delta, 2) == n)
575 limit = 64.0_dp*epsilon(0.0_dp)
576 IF (PRESENT(minimum_rcond)) limit = minimum_rcond
577 cpassert(ieee_is_finite(limit) .AND. limit >= 0.0_dp .AND. limit <= 1.0_dp)
578 inverse%nproj = n
579 info = 0
580 IF (.NOT. all(ieee_is_finite(real(beta_delta, dp))) .OR. &
581 .NOT. all(ieee_is_finite(aimag(beta_delta)))) THEN
582 info = n + 2
583 RETURN
584 END IF
585 ALLOCATE (inverse%lu(n, n), inverse%pivots(n))
586 inverse%lu(:, :) = beta_delta
587 DO i = 1, n
588 inverse%lu(i, i) = inverse%lu(i, i) + 1.0_dp
589 END DO
590 IF (n == 0) THEN
591 inverse%rcond = 1.0_dp
592 inverse%ready = .true.
593 RETURN
594 END IF
595 ALLOCATE (work(2*n), rwork(2*n))
596 anorm = zlange('1', n, n, inverse%lu, n, rwork)
597 CALL zgetrf(n, n, inverse%lu, n, inverse%pivots, info)
598 IF (info /= 0) RETURN
599 CALL zgecon('1', n, inverse%lu, n, anorm, inverse%rcond, work, rwork, info)
600 IF (info /= 0) RETURN
601 IF (.NOT. ieee_is_finite(inverse%rcond)) THEN
602 info = n + 2
603 RETURN
604 END IF
605 IF (inverse%rcond < limit) THEN
606 info = n + 1
607 RETURN
608 END IF
609 inverse%ready = .true.
610 END SUBROUTINE upf_inverse_init
611
612! **************************************************************************************************
613!> \brief Solve for W=P*U*chi; do not replace original q by an orthogonalized metric.
614!> \param inverse ...
615!> \param beta_gaussian B=P*chi, with one Gaussian function per column.
616!> \param coefficients W, using the already factored full K.
617! **************************************************************************************************
618 SUBROUTINE upf_inverse_project(inverse, beta_gaussian, coefficients)
619 TYPE(upf_inverse_type), INTENT(IN) :: inverse
620 COMPLEX(KIND=dp), INTENT(IN) :: beta_gaussian(:, :)
621 COMPLEX(KIND=dp), INTENT(OUT) :: coefficients(:, :)
622
623 INTEGER :: info, n, nrhs
624
625 EXTERNAL zgetrs
626
627 cpassert(inverse%ready)
628 n = inverse%nproj
629 nrhs = SIZE(beta_gaussian, 2)
630 cpassert(SIZE(beta_gaussian, 1) == n)
631 cpassert(all(shape(coefficients) == shape(beta_gaussian)))
632 coefficients = beta_gaussian
633 IF (n == 0 .OR. nrhs == 0) RETURN
634 CALL zgetrs('N', n, nrhs, inverse%lu, n, inverse%pivots, coefficients, n, info)
635 cpassert(info == 0)
636 END SUBROUTINE upf_inverse_project
637
638! **************************************************************************************************
639!> \brief Differentiate the same reconstruction: K*dW=dB-dK*W.
640!> Displacement/strain responses must include the original waves, projectors,
641!> their moving integration measure, and all relevant Bloch-image terms.
642!> \param inverse ...
643!> \param coefficients ...
644!> \param beta_gaussian_response ...
645!> \param beta_delta_response ...
646!> \param response ...
647! **************************************************************************************************
648 SUBROUTINE upf_inverse_project_response(inverse, coefficients, beta_gaussian_response, &
649 beta_delta_response, response)
650 TYPE(upf_inverse_type), INTENT(IN) :: inverse
651 COMPLEX(KIND=dp), INTENT(IN) :: coefficients(:, :), &
652 beta_gaussian_response(:, :), &
653 beta_delta_response(:, :)
654 COMPLEX(KIND=dp), INTENT(OUT) :: response(:, :)
655
656 COMPLEX(KIND=dp), ALLOCATABLE :: rhs(:, :)
657 INTEGER :: n
658
659 n = inverse%nproj
660 cpassert(SIZE(coefficients, 1) == n)
661 cpassert(all(shape(beta_delta_response) == [n, n]))
662 cpassert(all(shape(beta_gaussian_response) == shape(coefficients)))
663 ALLOCATE (rhs(n, SIZE(coefficients, 2)))
664 rhs(:, :) = beta_gaussian_response - matmul(beta_delta_response, coefficients)
665 CALL upf_inverse_project(inverse, rhs, response)
666 END SUBROUTINE upf_inverse_project_response
667
668! **************************************************************************************************
669!> \brief Evaluate inverse images on a field batch: U*chi=chi-Delta*W.
670!> Spatial derivatives use the corresponding derivative fields, with W fixed.
671!> \param gaussian ...
672!> \param delta ...
673!> \param coefficients ...
674!> \param soft ...
675! **************************************************************************************************
676 SUBROUTINE upf_inverse_basis(gaussian, delta, coefficients, soft)
677 COMPLEX(KIND=dp), INTENT(IN) :: gaussian(:, :), delta(:, :), &
678 coefficients(:, :)
679 COMPLEX(KIND=dp), INTENT(OUT) :: soft(:, :)
680
681 cpassert(SIZE(delta, 1) == SIZE(gaussian, 1))
682 cpassert(SIZE(delta, 2) == SIZE(coefficients, 1))
683 cpassert(SIZE(coefficients, 2) == SIZE(gaussian, 2))
684 cpassert(all(shape(soft) == shape(gaussian)))
685 soft = gaussian - matmul(delta, coefficients)
686 END SUBROUTINE upf_inverse_basis
687
688! **************************************************************************************************
689!> \brief Joint field response d(U*chi)=dchi-dDelta*W-Delta*dW.
690!> \param gaussian_response ...
691!> \param delta ...
692!> \param delta_response ...
693!> \param coefficients ...
694!> \param coefficients_response ...
695!> \param response ...
696! **************************************************************************************************
697 SUBROUTINE upf_inverse_basis_response(gaussian_response, delta, delta_response, &
698 coefficients, coefficients_response, response)
699 COMPLEX(KIND=dp), INTENT(IN) :: gaussian_response(:, :), delta(:, :), delta_response(:, :), &
700 coefficients(:, :), coefficients_response(:, :)
701 COMPLEX(KIND=dp), INTENT(OUT) :: response(:, :)
702
703 cpassert(all(shape(delta_response) == shape(delta)))
704 cpassert(all(shape(coefficients_response) == shape(coefficients)))
705 CALL upf_inverse_basis(gaussian_response, delta_response, coefficients, response)
706 response = response - matmul(delta, coefficients_response)
707 END SUBROUTINE upf_inverse_basis_response
708
709! **************************************************************************************************
710!> \brief Pull back A+P^dagger*augmentation*P into reconstructed Gaussian variables.
711!> \param gaussian_operator <chi|A|chi>.
712!> \param gaussian_delta <chi|A|Delta>.
713!> \param delta_gaussian <Delta|A|chi>; supplied separately for non-Hermitian operators.
714!> \param delta_operator <Delta|A|Delta>.
715!> \param augmentation Original projector-space contribution; q for the metric,
716!> Dij+integral(v*Q) for nonlocal/local-augmentation terms.
717!> \param coefficients W=K^-1*B. Original operators are retained, including residuals.
718!> \param transformed <U*chi|A|U*chi>+W^dagger*augmentation*W.
719! **************************************************************************************************
720 SUBROUTINE upf_inverse_operator(gaussian_operator, gaussian_delta, delta_gaussian, delta_operator, &
721 augmentation, coefficients, transformed)
722 COMPLEX(KIND=dp), INTENT(IN) :: gaussian_operator(:, :), gaussian_delta(:, :), &
723 delta_gaussian(:, :), delta_operator(:, :), augmentation(:, :), coefficients(:, :)
724 COMPLEX(KIND=dp), INTENT(OUT) :: transformed(:, :)
725
726 CALL check_operator_shapes(gaussian_operator, gaussian_delta, delta_gaussian, delta_operator, &
727 augmentation, coefficients, shape(transformed))
728 transformed = gaussian_operator - matmul(gaussian_delta, coefficients) - &
729 matmul(conjg(transpose(coefficients)), delta_gaussian) + &
730 matmul(conjg(transpose(coefficients)), matmul(delta_operator + augmentation, coefficients))
731 END SUBROUTINE upf_inverse_operator
732
733! **************************************************************************************************
734!> \brief Differentiate the full operator pullback, including both inverse-basis legs.
735!> Matrix responses already include their moving original fields and quadratures.
736!> The stationary energy also requires the transformed-metric Pulay term.
737!> \param gaussian_response ...
738!> \param gaussian_delta ...
739!> \param delta_gaussian ...
740!> \param delta_operator ...
741!> \param augmentation ...
742!> \param coefficients ...
743!> \param gaussian_delta_response ...
744!> \param delta_gaussian_response ...
745!> \param delta_operator_response ...
746!> \param augmentation_response ...
747!> \param coefficients_response ...
748!> \param response ...
749! **************************************************************************************************
750 SUBROUTINE upf_inverse_operator_response(gaussian_response, gaussian_delta, delta_gaussian, &
751 delta_operator, augmentation, coefficients, &
752 gaussian_delta_response, delta_gaussian_response, &
753 delta_operator_response, augmentation_response, &
754 coefficients_response, response)
755 COMPLEX(KIND=dp), INTENT(IN) :: gaussian_response(:, :), gaussian_delta(:, :), &
756 delta_gaussian(:, :), delta_operator(:, :), augmentation(:, :), coefficients(:, :), &
757 gaussian_delta_response(:, :), delta_gaussian_response(:, :), &
758 delta_operator_response(:, :), augmentation_response(:, :), coefficients_response(:, :)
759 COMPLEX(KIND=dp), INTENT(OUT) :: response(:, :)
760
761 CALL check_operator_shapes(gaussian_response, gaussian_delta, delta_gaussian, delta_operator, &
762 augmentation, coefficients, shape(response))
763 cpassert(all(shape(coefficients_response) == shape(coefficients)))
764 CALL upf_inverse_operator(gaussian_response, gaussian_delta_response, delta_gaussian_response, &
765 delta_operator_response, augmentation_response, coefficients, response)
766 response = response - matmul(gaussian_delta, coefficients_response) - &
767 matmul(conjg(transpose(coefficients_response)), delta_gaussian) + &
768 matmul(conjg(transpose(coefficients_response)), matmul(delta_operator + augmentation, coefficients)) + &
769 matmul(conjg(transpose(coefficients)), matmul(delta_operator + augmentation, coefficients_response))
770 END SUBROUTINE upf_inverse_operator_response
771
772! **************************************************************************************************
773!> \brief ...
774!> \param gg ...
775!> \param gd ...
776!> \param dg ...
777!> \param dd ...
778!> \param augmentation ...
779!> \param coefficients ...
780!> \param output_shape ...
781! **************************************************************************************************
782 SUBROUTINE check_operator_shapes(gg, gd, dg, dd, augmentation, coefficients, output_shape)
783 COMPLEX(KIND=dp), INTENT(IN) :: gg(:, :), gd(:, :), dg(:, :), dd(:, :), &
784 augmentation(:, :), coefficients(:, :)
785 INTEGER, INTENT(IN) :: output_shape(2)
786
787 INTEGER :: m, n
788
789 m = SIZE(coefficients, 1)
790 n = SIZE(coefficients, 2)
791 cpassert(all(shape(gg) == [n, n]))
792 cpassert(all(shape(gd) == [n, m]))
793 cpassert(all(shape(dg) == [m, n]))
794 cpassert(all(shape(dd) == [m, m]))
795 cpassert(all(shape(augmentation) == [m, m]))
796 cpassert(all(output_shape == [n, n]))
797 END SUBROUTINE check_operator_shapes
798
799END MODULE upf_inverse
Routines that process Quantum Espresso UPF files.
Definition atom_upf.F:14
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 degree
real(kind=dp), parameter, public fourpi
Inverse of a specified finite-rank UPF reconstruction, including intercenter coupling....
Definition upf_inverse.F:15
subroutine, public upf_inverse_kinetic_evaluate(kinetic, displacement, matrix, gradient)
One-half grad(AE_i).grad(AE_j) minus one-half grad(PS_i).grad(PS_j). Factoring through Delta preserve...
subroutine, public upf_inverse_project(inverse, beta_gaussian, coefficients)
Solve for W=P*U*chi; do not replace original q by an orthogonalized metric.
subroutine, public upf_inverse_init(beta_delta, inverse, info, minimum_rcond)
Factor K with original projector normalization and all supplied intercenter blocks.
subroutine, public upf_inverse_basis_response(gaussian_response, delta, delta_response, coefficients, coefficients_response, response)
Joint field response d(U*chi)=dchi-dDelta*W-Delta*dW.
subroutine, public upf_inverse_basis(gaussian, delta, coefficients, soft)
Evaluate inverse images on a field batch: U*chi=chi-Delta*W. Spatial derivatives use the correspondin...
subroutine, public upf_inverse_fields_init(pot, beta, delta, direct, home_coupling)
Prepare original beta and reconstruction fields for common intercenter quadratures....
subroutine, public upf_inverse_kinetic_init(pot, kinetic)
Independent valence kinetic augmentation from original projector-indexed AE/PS pairs....
subroutine, public upf_inverse_atomic_residual(atomic, coefficients, delta_overlap, delta_kinetic, kinetic_residual, metric_residual)
Finite partial-wave closure residuals added to Gaussian T plus hard D-T and Gaussian S....
subroutine, public upf_inverse_atomic_difference_init(pot, degree, atomic)
Prepare the local difference integrals without factorizing a local inverse.
Definition upf_inverse.F:89
subroutine, public upf_inverse_project_response(inverse, coefficients, beta_gaussian_response, beta_delta_response, response)
Differentiate the same reconstruction: K*dW=dB-dK*W. Displacement/strain responses must include the o...
subroutine, public upf_inverse_operator(gaussian_operator, gaussian_delta, delta_gaussian, delta_operator, augmentation, coefficients, transformed)
Pull back A+P^dagger*augmentation*P into reconstructed Gaussian variables.
subroutine, public upf_inverse_delta_init(pot, degree, delta)
Prepare original AE-minus-PS radial overlaps in the native projector/harmonic order....
subroutine, public upf_inverse_atomic_init(pot, degree, atomic, info, direct)
Local hard projectors and D-T for the nonoverlapping partial-wave construction. Both factors of a non...
Definition upf_inverse.F:73
subroutine, public upf_inverse_operator_response(gaussian_response, gaussian_delta, delta_gaussian, delta_operator, augmentation, coefficients, gaussian_delta_response, delta_gaussian_response, delta_operator_response, augmentation_response, coefficients_response, response)
Differentiate the full operator pullback, including both inverse-basis legs. Matrix responses already...
subroutine, public upf_inverse_kinetic_matrix(pot, kinetic, matrix, difference_kinetic, difference_overlap)
Full radial-channel kinetic matrix from the same interpolated hard/soft reference waves....
Half-numerical overlaps with the original scalar UPF radial projectors. Cartesian polynomials and ang...
subroutine, public upf_projector_init(pot, degree, projectors)
Prepare immutable radial and angular data outside parallel regions.
Original scalar radial fields in the native real-harmonic projector order. Regular radial factors are...
subroutine, public upf_radial_fields_evaluate(fields, displacement, value, gradient, hessian)
Evaluate values and Cartesian derivatives from the same radial/harmonic polynomial.
subroutine, public upf_radial_fields_init(r, radial, lbeta, fields, finite_table)
Cache original f_l(r)Y_lm, without weights, normalization changes or channel rotations....
Owned UPF dataset and radial conventions shared by ATOM and Quickstep. Dataset availability does not ...
Definition upf_types.F:12
real(dp) function, dimension(size(pot%r)), public upf_radial_weights(pot)
Composite Simpson weights for dr on the supplied UPF mesh index.
Definition upf_types.F:189