Octopus
nonlocal_overlap_matrix.F90
Go to the documentation of this file.
1!! Copyright (C) 2026 N. Tancogne-Dejean
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
33 use accel_oct_m
34 use debug_oct_m
35 use epot_oct_m
36 use global_oct_m
38 use mesh_oct_m
43 use space_oct_m
45
46 implicit none
47
48 private
49 public :: &
52
59 integer :: nmat = 0
60 integer, allocatable :: imats(:)
61 integer, allocatable :: coff(:)
62 integer :: nc = 0
63 integer :: sdim = 1
64 logical :: is_cmplx = .false.
67 real(real64), allocatable :: dt(:, :, :)
68 complex(real64), allocatable :: zt(:, :, :)
69 end type overlap_cluster_t
70
75 logical, public :: use_global_coupling = .false.
76 logical, public :: coupling_built = .false.
77 integer :: ncluster = 0
78 integer, allocatable :: proj_offset(:)
79 type(overlap_cluster_t), allocatable :: clusters(:)
80 contains
81 procedure :: detect => overlap_matrix_detect
82 procedure :: build => overlap_matrix_build
83 procedure :: end => overlap_matrix_end
84 procedure :: dapply => doverlap_matrix_apply
85 procedure :: zapply => zoverlap_matrix_apply
86 end type overlap_matrix_t
87
89 logical :: uspp_gpu_approx_warned = .false.
90
91contains
92
93 !--------------------------------------------------------
98 subroutine overlap_matrix_detect(this, nmat, projector_matrices, projector_to_atom, epot, space, self_overlap)
99 class(overlap_matrix_t), intent(inout) :: this
100 integer, intent(in) :: nmat
101 type(projector_matrix_t), intent(in) :: projector_matrices(:)
102 integer, intent(in) :: projector_to_atom(:)
103 type(epot_t), intent(in) :: epot
104 class(space_t), intent(in) :: space
105 logical, intent(in) :: self_overlap
106
107 integer :: im, jm, ia, ja, ic, root, nroots, kk
108 integer, allocatable :: parent(:), root_of(:), csize(:), cluster_of(:), fill(:)
109 logical :: any_overlap
110
111 push_sub(overlap_matrix_detect)
112
113 call overlap_matrix_end(this)
114
115 ! projection-vector offset of each matrix` slice (matches ind(imat) in the apply path)
116 safe_allocate(this%proj_offset(1:max(nmat, 1)))
117 kk = 0
118 do im = 1, nmat
119 this%proj_offset(im) = kk
120 kk = kk + projector_matrices(im)%nprojs
121 end do
122
123 if (nmat == 0) then
124 pop_sub(overlap_matrix_detect)
125 return
126 end if
127
128 ! union-find over projector matrices whose spheres share grid points (distinct atoms only:
129 ! a single matrix per atom, so same-atom pairs never arise here)
130 safe_allocate(parent(1:nmat))
131 do im = 1, nmat
132 parent(im) = im
133 end do
134 any_overlap = .false.
135 do im = 1, nmat
136 ia = projector_to_atom(im)
137 do jm = im + 1, nmat
138 ja = projector_to_atom(jm)
139 if (ia == ja) cycle
140 if (submesh_overlap(epot%proj(ia)%sphere, epot%proj(ja)%sphere, space)) then
141 call uf_union(parent, im, jm)
142 any_overlap = .true.
143 end if
144 end do
145 end do
146
147 ! the per-atom Gram ignores the image cross terms of a self-overlapping sphere
148 this%use_global_coupling = any_overlap .or. self_overlap
149 if (.not. this%use_global_coupling) then
150 safe_deallocate_a(parent)
152 return
153 end if
155 ! materialize the connected components as clusters
156 safe_allocate(cluster_of(1:nmat))
157 safe_allocate(root_of(1:nmat))
158 safe_allocate(csize(1:nmat))
159 nroots = 0
160 csize = 0
161 do im = 1, nmat
162 root = uf_find(parent, im)
163 ic = 0
164 do kk = 1, nroots
165 if (root_of(kk) == root) then
166 ic = kk
167 exit
168 end if
169 end do
170 if (ic == 0) then
171 nroots = nroots + 1
172 root_of(nroots) = root
173 ic = nroots
174 end if
175 cluster_of(im) = ic
176 csize(ic) = csize(ic) + 1
177 end do
178
179 this%ncluster = nroots
180 safe_allocate(this%clusters(1:nroots))
181 safe_allocate(fill(1:nroots))
182 fill = 0
183 do ic = 1, nroots
184 this%clusters(ic)%nmat = csize(ic)
185 safe_allocate(this%clusters(ic)%imats(1:csize(ic)))
186 safe_allocate(this%clusters(ic)%coff(1:csize(ic)))
187 end do
188 ! matrices are visited in ascending order, so imats(:) comes out sorted
189 do im = 1, nmat
190 ic = cluster_of(im)
191 fill(ic) = fill(ic) + 1
192 this%clusters(ic)%imats(fill(ic)) = im
193 end do
194 do ic = 1, nroots
195 kk = 0
196 do im = 1, this%clusters(ic)%nmat
197 this%clusters(ic)%coff(im) = kk
198 kk = kk + projector_matrices(this%clusters(ic)%imats(im))%nprojs
199 end do
200 this%clusters(ic)%nc = kk
201 ! Fully-relativistic ultrasoft: the overlap charges couple the two spinor components
202 ! (zqmix), so T is a dense 2nc x 2nc block, indexed (spin, projector) as (s-1)*nc + i.
203 ! The relativistic treatment is global, so a cluster is never mixed.
204 this%clusters(ic)%sdim = 1
205 if (allocated(projector_matrices(this%clusters(ic)%imats(1))%zqmix)) this%clusters(ic)%sdim = 2
206 do im = 1, this%clusters(ic)%nmat
207 assert(allocated(projector_matrices(this%clusters(ic)%imats(im))%zqmix) .eqv. this%clusters(ic)%sdim == 2)
208 end do
209 end do
210
211 safe_deallocate_a(parent)
212 safe_deallocate_a(cluster_of)
213 safe_deallocate_a(root_of)
214 safe_deallocate_a(csize)
215 safe_deallocate_a(fill)
216
217 pop_sub(overlap_matrix_detect)
218 end subroutine overlap_matrix_detect
219
220 !--------------------------------------------------------
222 recursive integer function uf_find(parent, ii) result(rr)
223 integer, intent(inout) :: parent(:)
224 integer, intent(in) :: ii
225
226 if (parent(ii) /= ii) parent(ii) = uf_find(parent, parent(ii))
227 rr = parent(ii)
228 end function uf_find
229
230 !--------------------------------------------------------
232 subroutine uf_union(parent, ii, jj)
233 integer, intent(inout) :: parent(:)
234 integer, intent(in) :: ii, jj
235
236 integer :: ri, rj
237
238 ri = uf_find(parent, ii)
239 rj = uf_find(parent, jj)
240 if (ri /= rj) parent(rj) = ri
241 end subroutine uf_union
242
243 ! ----------------------------------------------------------------------------------
259 subroutine overlap_matrix_build(this, projector_matrices, projector_phases, mesh, kstart, kend)
260 class(overlap_matrix_t), target, intent(inout) :: this
261 type(projector_matrix_t), intent(in) :: projector_matrices(:)
262 complex(real64), allocatable, intent(in) :: projector_phases(:, :, :, :)
263 class(mesh_t), intent(in) :: mesh
264 integer, intent(in) :: kstart
265 integer, intent(in) :: kend
266
267 integer :: c, ik, nc, sdim, n2, a, b, oa, ob, s1, s2, ii, jj, nI
268 integer :: blkmap(2, 2)
269 logical :: phased
270 type(overlap_cluster_t), pointer :: cl
271 complex(real64), allocatable :: zgI(:, :), zgJ(:, :), mmat(:, :), qmat(:, :), amat(:, :), tmat(:, :)
272
273 push_sub(overlap_matrix_build)
274
275 if (.not. this%use_global_coupling) then
276 pop_sub(overlap_matrix_build)
277 return
278 end if
279
280 ! The exact cross-block inverse is applied on the CPU only. On the GPU the coupling still uses
281 ! the per-atom (block-diagonal) buff_mix, i.e. the previous approximation; warn once. (The
282 ! device dense coupling is a follow-up that needs GPU hardware to implement and validate.)
283 if (accel_is_enabled() .and. .not. uspp_gpu_approx_warned) then
284 uspp_gpu_approx_warned = .true.
285 message(1) = "Ultrasoft projector spheres of different atoms overlap on the grid."
286 message(2) = "The exact inverse overlap S^{-1} is applied on the CPU only; the GPU path"
287 message(3) = "still uses the per-atom approximation. Convergence on GPU may be degraded."
288 call messages_warning(3)
289 end if
290
291 phased = allocated(projector_phases)
292
293 ! spin-block layout of zqmix (same as zmix)
294 blkmap(1, 1) = 1
295 blkmap(2, 2) = 2
296 blkmap(1, 2) = 3
297 blkmap(2, 1) = 4
298
299 do c = 1, this%ncluster
300 cl => this%clusters(c)
301 nc = cl%nc
302 sdim = cl%sdim
303 n2 = sdim*nc
304 ! T is complex if the projectors are phased (k /= 0 / vector potential), complex
305 ! (fully-relativistic Y_lm kets) or spin-coupled (spin-orbit); otherwise a real T is stored.
306 cl%is_cmplx = phased .or. sdim == 2 .or. projector_matrices(cl%imats(1))%is_cmplx
307
308 if (cl%is_cmplx) then
309 if (.not. allocated(cl%zt)) then
310 safe_allocate(cl%zt(1:n2, 1:n2, kstart:kend))
311 end if
312 else
313 if (.not. allocated(cl%dt)) then
314 safe_allocate(cl%dt(1:nc, 1:nc, kstart:kend))
315 end if
316 end if
317
318 safe_allocate(mmat(1:nc, 1:nc))
319 safe_allocate(qmat(1:n2, 1:n2))
320 safe_allocate(amat(1:n2, 1:n2))
321 safe_allocate(tmat(1:n2, 1:n2))
322
323 do ik = kstart, kend
324
325 ! Cluster Gram M(k): local contribution over shared grid points, upper blocks only
326 mmat = m_z0
327 do a = 1, cl%nmat
328 call fill_phased_ket(cl%imats(a), ik, zgi)
329 oa = cl%coff(a)
330 do b = a, cl%nmat
331 call fill_phased_ket(cl%imats(b), ik, zgj)
332 ob = cl%coff(b)
333 call accumulate_gram(projector_matrices(cl%imats(a)), &
334 projector_matrices(cl%imats(b)), zgi, zgj, mmat, oa, ob)
335 safe_deallocate_a(zgj)
336 end do
337 safe_deallocate_a(zgi)
338 end do
339 ! fill the strict-lower entries by Hermiticity (diagonal blocks are already full)
340 do jj = 1, nc
341 do ii = jj + 1, nc
342 mmat(ii, jj) = conjg(mmat(jj, ii))
343 end do
344 end do
345 ! the sphere may be split across domains
346 if (mesh%parallel_in_domains) call mesh%allreduce(mmat, dim = (/nc, nc/))
347 mmat = mmat * mesh%volume_element
348
349 ! Block-diagonal augmentation charges Q (per atom; no cross-atom q terms)
350 qmat = m_z0
351 do a = 1, cl%nmat
352 ni = projector_matrices(cl%imats(a))%nprojs
353 oa = cl%coff(a)
354 if (sdim == 1) then
355 qmat(oa+1:oa+ni, oa+1:oa+ni) = &
356 cmplx(projector_matrices(cl%imats(a))%qmix(1:ni, 1:ni), m_zero, real64)
357 else
358 ! spin-coupled overlap charges, index (spin, projector) as (s-1)*nc + i
359 do s2 = 1, 2
360 do s1 = 1, 2
361 qmat((s1-1)*nc + oa+1:(s1-1)*nc + oa+ni, (s2-1)*nc + oa+1:(s2-1)*nc + oa+ni) = &
362 projector_matrices(cl%imats(a))%zqmix(1:ni, 1:ni, blkmap(s1, s2))
363 end do
364 end do
365 end if
366 end do
367
368 ! A = 1 + Mtilde Q ; T = Q A^{-1}. The projectors carry no spin, so Mtilde = 1_sdim (x) M
369 ! is block-diagonal in spin: each spin-row block of A is M times that block of Q.
370 do s1 = 1, sdim
371 amat((s1-1)*nc + 1:s1*nc, :) = matmul(mmat, qmat((s1-1)*nc + 1:s1*nc, :))
372 end do
373 do ii = 1, n2
374 amat(ii, ii) = amat(ii, ii) + m_z1
375 end do
376 call lalg_inverse(n2, amat, 'svd')
377 tmat = matmul(qmat, amat)
378
379 if (cl%is_cmplx) then
380 cl%zt(1:n2, 1:n2, ik) = -tmat(1:n2, 1:n2)
381 else
382 cl%dt(1:nc, 1:nc, ik) = -real(tmat(1:nc, 1:nc), real64)
383 end if
384
385 end do
386
387 safe_deallocate_a(mmat)
388 safe_deallocate_a(qmat)
389 safe_deallocate_a(amat)
390 safe_deallocate_a(tmat)
391 end do
392
393 this%coupling_built = .true.
394
395 pop_sub(overlap_matrix_build)
396
397 contains
398
399 ! Build the phased ket weight g(i, p) = beta_i(p) * conjg(phase(p)) for projector matrix im.
400 subroutine fill_phased_ket(im, ik, zg)
401 integer, intent(in) :: im
402 integer, intent(in) :: ik
403 complex(real64), allocatable, intent(out) :: zg(:, :)
404
405 integer :: np, npr, p, i
406 complex(real64) :: ph
407
408 np = projector_matrices(im)%npoints
409 npr = projector_matrices(im)%nprojs
410 safe_allocate(zg(1:npr, 1:max(np, 1)))
411 if (np == 0) return
412
413 if (associated(projector_matrices(im)%zprojectors)) then
414 if (phased) then
415 do p = 1, np
416 ph = conjg(projector_phases(p, 1, im, ik))
417 do i = 1, npr
418 zg(i, p) = projector_matrices(im)%zprojectors(p, i)*ph
419 end do
420 end do
421 else
422 do p = 1, np
423 do i = 1, npr
424 zg(i, p) = projector_matrices(im)%zprojectors(p, i)
425 end do
426 end do
427 end if
428 else
429 if (phased) then
430 do p = 1, np
431 ph = conjg(projector_phases(p, 1, im, ik))
432 do i = 1, npr
433 zg(i, p) = projector_matrices(im)%dprojectors(p, i)*ph
434 end do
435 end do
436 else
437 do p = 1, np
438 do i = 1, npr
439 zg(i, p) = cmplx(projector_matrices(im)%dprojectors(p, i), m_zero, real64)
440 end do
441 end do
442 end if
443 end if
444 end subroutine fill_phased_ket
445
446 ! Accumulate M[oa+i, ob+j] += sum_{shared points} conjg(gI) gJ.
447 ! A self-overlapping sphere stores its map as injective regions,
448 ! so each region pair is merged; this covers every pair of
449 ! sphere points sharing a grid point, periodic images included.
450 subroutine accumulate_gram(pmI, pmJ, zgI, zgJ, mmat, oa, ob)
451 type(projector_matrix_t), intent(in) :: pmI
452 type(projector_matrix_t), intent(in) :: pmJ
453 complex(real64), intent(in) :: zgI(:, :)
454 complex(real64), intent(in) :: zgJ(:, :)
455 complex(real64), intent(inout) :: mmat(:, :)
456 integer, intent(in) :: oa
457 integer, intent(in) :: ob
458
459 integer :: ri, rj, ip, jp, ipe, jpe, ni, nj, i, j, dd
460 complex(real64) :: ket
461
462 ni = size(zgi, 1)
463 nj = size(zgj, 1)
464 do ri = 1, pmi%nregions
465 ipe = pmi%regions(ri + 1) - 1
466 do rj = 1, pmj%nregions
467 jpe = pmj%regions(rj + 1) - 1
468 ip = pmi%regions(ri)
469 jp = pmj%regions(rj)
470 do while (ip <= ipe .and. jp <= jpe)
471 dd = pmi%map(ip) - pmj%map(jp)
472 if (dd < 0) then
473 ip = ip + 1
474 else if (dd > 0) then
475 jp = jp + 1
476 else
477 do j = 1, nj
478 ket = zgj(j, jp)
479 do i = 1, ni
480 mmat(oa+i, ob+j) = mmat(oa+i, ob+j) + conjg(zgi(i, ip))*ket
481 end do
482 end do
483 ip = ip + 1
484 jp = jp + 1
485 end if
486 end do
487 end do
488 end do
489 end subroutine accumulate_gram
490
491 end subroutine overlap_matrix_build
492
493 !-----------------------------------------------------------------
495 subroutine overlap_matrix_end(this)
496 class(overlap_matrix_t), intent(inout) :: this
497
498 integer :: ic
499
500 push_sub(overlap_matrix_end)
501
502 if (allocated(this%clusters)) then
503 do ic = 1, size(this%clusters)
504 safe_deallocate_a(this%clusters(ic)%imats)
505 safe_deallocate_a(this%clusters(ic)%coff)
506 safe_deallocate_a(this%clusters(ic)%dt)
507 safe_deallocate_a(this%clusters(ic)%zt)
508 end do
509 safe_deallocate_a(this%clusters)
510 end if
511 safe_deallocate_a(this%proj_offset)
512 this%ncluster = 0
513 this%use_global_coupling = .false.
514 this%coupling_built = .false.
515
516 pop_sub(overlap_matrix_end)
517 end subroutine overlap_matrix_end
518
519#include "undef.F90"
520#include "real.F90"
521#include "nonlocal_overlap_matrix_inc.F90"
523#include "undef.F90"
524#include "complex.F90"
525#include "nonlocal_overlap_matrix_inc.F90"
526
528
529!! Local Variables:
530!! mode: f90
531!! coding: utf-8
532!! End:
pure logical function, public accel_is_enabled()
Definition: accel.F90:391
real(real64), parameter, public m_zero
Definition: global.F90:173
complex(real64), parameter, public m_z0
Definition: global.F90:183
complex(real64), parameter, public m_z1
Definition: global.F90:184
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:501
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
Exact inverse of the ultrasoft/PAW generalized overlap operator for overlapping spheres.
subroutine doverlap_matrix_apply(this, projector_matrices, proj, nst_linear, nst, spin_dim, ik)
Apply the exact inverse-overlap coupling p` = -T p (mix = -T stored per cluster), dense within each o...
subroutine overlap_matrix_build(this, projector_matrices, projector_phases, mesh, kstart, kend)
Assemble the exact inverse-overlap coupling -T for each overlap cluster.
subroutine zoverlap_matrix_apply(this, projector_matrices, proj, nst_linear, nst, spin_dim, ik)
Apply the exact inverse-overlap coupling p` = -T p (mix = -T stored per cluster), dense within each o...
subroutine uf_union(parent, ii, jj)
Union-find "union" of two projector matrices into one cluster.
subroutine overlap_matrix_detect(this, nmat, projector_matrices, projector_to_atom, epot, space, self_overlap)
Detect distinct-atom projector overlap and group the projector matrices into the connected components...
subroutine overlap_matrix_end(this)
Release the exact inverse-overlap cluster data.
recursive integer function uf_find(parent, ii)
Union-find "find" with path compression (roots of the projector-overlap graph).
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
logical function, public submesh_overlap(sm1, sm2, space)
Definition: submesh.F90:719
subroutine fill_phased_ket(im, ik, zg)
subroutine accumulate_gram(pmI, pmJ, zgI, zgJ, mmat, oa, ob)
Describes mesh distribution to nodes.
Definition: mesh.F90:165
One connected component of the projector-overlap graph.
The exact inverse-overlap operator, grouped into overlap clusters.
A set of projectors defined on a submesh.
int true(void)