Octopus
paw_ae_reconstruction.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
30 use debug_oct_m
32 use epot_oct_m
33 use global_oct_m
34 use, intrinsic :: iso_fortran_env
36 use mesh_oct_m
45
46 implicit none
47
48 private
49
50 public :: &
54
55contains
56
57 ! ----------------------------------------------------------------------------------
74 subroutine paw_ae_reconstruct_field(this, epot, mesh, density, tau, current)
75 class(nonlocal_pseudopotential_t), target, intent(in) :: this
76 type(epot_t), target, intent(in) :: epot
77 class(mesh_t), intent(in) :: mesh
78 real(real64), optional, intent(inout) :: density(:, :)
79 real(real64), optional, intent(inout) :: tau(:, :)
80 real(real64), optional, intent(inout) :: current(:, :, :)
81
82 integer :: imat, iatom, is, nsp, nprojs, nlm, ncol, idir, dim
83 integer, allocatable :: map(:), ll(:), mm(:)
84 real(real64), allocatable :: rho(:,:,:), arho(:,:,:)
85 real(real64) :: rcut
86 type(spline_t), allocatable :: spl(:)
87 type(projector_matrix_t), pointer :: pmat
88 type(projector_t), pointer :: proj
89
91 call profiling_in("PAW_RECONSTRUCT_FIELD")
92
93 if (.not. allocated(this%onsite_density_matrix) .or. &
94 .not. (present(density) .or. present(tau) .or. present(current))) then
95 call profiling_out("PAW_RECONSTRUCT_FIELD")
97 return
98 end if
99
100 dim = 0
101 if (present(current)) dim = size(current, 2)
102
103 do imat = 1, this%nprojector_matrices
104 iatom = this%projector_to_atom(imat)
105 proj => epot%proj(iatom)
106 if (.not. associated(proj%aug%paw)) cycle
107 pmat => this%projector_matrices(imat)
108 nprojs = pmat%nprojs
109 nlm = paw_onsite_nlm(proj%aug%paw)
110 nsp = paw_onsite_nspin(proj%aug%paw)
111 assert(nlm == nprojs)
112 assert(size(this%onsite_density_matrix, 4) >= nsp)
113 if (present(density)) then
114 assert(size(density, 2) >= nsp)
115 end if
116 if (present(tau)) then
117 assert(size(tau, 2) >= min(nsp, 2))
118 end if
119
120 ! Reorder the projector (l, m, channel) enumeration into the one-center (radial, m) index,
121 ! and assemble the one-center density matrix once (shared by all requested quantities).
122 safe_allocate(map(1:nprojs))
123 call paw_onsite_pmat_map(proj%aug%paw, nprojs, pmat%l, pmat%m, pmat%channel, map)
124 safe_allocate(rho(1:nlm, 1:nlm, 1:nsp))
125 do is = 1, nsp
126 call paw_onsite_reorder_pmat(nprojs, map, this%onsite_density_matrix(:, :, imat, is), rho(:, :, is))
127 end do
128 ! the current uses the antisymmetric (imaginary) one-center matrix, reordered the same way
129 if (present(current)) then
130 assert(allocated(this%onsite_current_matrix))
131 safe_allocate(arho(1:nlm, 1:nlm, 1:nsp))
132 do is = 1, nsp
133 call paw_onsite_reorder_pmat(nprojs, map, this%onsite_current_matrix(:, :, imat, is), arho(:, :, is))
134 end do
135 end if
136
137 do is = 1, nsp
138 if (present(density)) then
139 call paw_onsite_density_diff(proj%aug%paw, rho(:,:,is), ncol, spl, ll, mm, rcut)
140 call paw_splat_onsite_columns(epot%dgrid, proj%sphere, ncol, spl, ll, mm, rcut, density(:, is), &
141 cache = proj%dg_cache)
142 end if
143 if (present(tau) .and. is <= 2) then
144 call paw_onsite_tau_diff(proj%aug%paw, rho(:,:,is), ncol, spl, ll, mm, rcut)
145 call paw_splat_onsite_columns(epot%dgrid, proj%sphere, ncol, spl, ll, mm, rcut, tau(:, is), &
146 cache = proj%dg_cache)
147 end if
148 if (present(current)) then
149 do idir = 1, dim
150 call paw_onsite_current_diff(proj%aug%paw, arho(:,:,is), idir, ncol, spl, ll, mm, rcut)
151 call paw_splat_onsite_columns(epot%dgrid, proj%sphere, ncol, spl, ll, mm, rcut, current(:, idir, is), &
152 cache = proj%dg_cache)
153 end do
154 end if
155 end do
156
157 safe_deallocate_a(map)
158 safe_deallocate_a(rho)
159 safe_deallocate_a(arho)
160 end do
161
162 call profiling_out("PAW_RECONSTRUCT_FIELD")
164 end subroutine paw_ae_reconstruct_field
165
166 ! ----------------------------------------------------------------------------------
170 subroutine paw_splat_onsite_columns(dgrid, sphere, ncol, spl, ll, mm, rcut, field_col, cache)
171 type(double_grid_t), intent(in) :: dgrid
172 type(submesh_t), intent(in) :: sphere
173 integer, intent(in) :: ncol
174 type(spline_t), allocatable, intent(inout) :: spl(:)
175 integer, allocatable, intent(inout) :: ll(:), mm(:)
176 real(real64), intent(in) :: rcut
177 real(real64), intent(inout) :: field_col(:)
178 type(double_grid_cache_t), optional, intent(in) :: cache
179
180 real(real64), allocatable :: feff(:,:), sphi(:), rcutv(:), ones(:)
181
182 ! No PUSH_SUB: light helper called inside the reconstruction loop.
183 if (ncol > 0 .and. sphere%np > 0) then
184 safe_allocate(rcutv(1:ncol))
185 rcutv(1:ncol) = rcut
186 safe_allocate(feff(1:sphere%np, 1:ncol))
187 call double_grid_transform(dgrid, sphere, ncol, spl, ll, mm, rcutv, feff, cache = cache)
188 ! Sum the (L,M) columns onto the sphere: sphi = feff . 1 (each column already carries its
189 ! coefficient), then scatter onto the mesh.
190 safe_allocate(sphi(1:sphere%np))
191 safe_allocate(ones(1:ncol))
192 ones(1:ncol) = m_one
193 call lalg_gemv(sphere%np, ncol, m_one, feff, ones, m_zero, sphi)
194 call submesh_add_to_mesh(sphere, sphi, field_col)
195 safe_deallocate_a(ones)
196 safe_deallocate_a(sphi)
197 safe_deallocate_a(feff)
198 safe_deallocate_a(rcutv)
199 end if
200 if (ncol > 0) call spline_end(spl(1:ncol))
201 safe_deallocate_a(spl)
202 safe_deallocate_a(ll)
203 safe_deallocate_a(mm)
204 end subroutine paw_splat_onsite_columns
205
206 ! ----------------------------------------------------------------------------------
215 subroutine paw_ae_build_current_basis(this, epot, dim)
216 class(nonlocal_pseudopotential_t), target, intent(in) :: this
217 type(epot_t), target, intent(in) :: epot
218 integer, intent(in) :: dim
219
220 integer :: imat, iatom, nprojs, nlm, iproj, jproj, idir, ncol, pairidx, col, nsph
221 integer :: ngrp, maxcols, ntot, g, base
222 integer, allocatable :: map(:), ll(:), mm(:), big_ll(:), big_mm(:), grp_start(:), grp_ncol(:)
223 real(real64), allocatable :: arho(:,:), big_rcut(:), feff(:,:)
224 real(real64) :: rcut
225 type(spline_t), allocatable :: spl(:), big_spl(:)
226 type(projector_matrix_t), pointer :: pmat
227 type(projector_t), pointer :: proj
228
230
231 do imat = 1, this%nprojector_matrices
232 iatom = this%projector_to_atom(imat)
233 proj => epot%proj(iatom)
234 if (.not. associated(proj%aug%paw)) cycle
235 pmat => this%projector_matrices(imat)
236 nprojs = pmat%nprojs
237 nlm = paw_onsite_nlm(proj%aug%paw)
238 assert(nlm == nprojs)
239 nsph = proj%sphere%np
240
241 safe_allocate(map(1:nprojs))
242 call paw_onsite_pmat_map(proj%aug%paw, nprojs, pmat%l, pmat%m, pmat%channel, map)
243
244 safe_deallocate_a(proj%aug%jbasis)
245 safe_allocate(proj%aug%jbasis(1:nsph, 1:nprojs*nprojs, 1:dim))
246 proj%aug%jbasis = m_zero
247 proj%aug%jbasis_nprojs = nprojs
248
249 ! Collect the radial x Y_LM columns of every unit pair (i,j) and direction into one list, then
250 ! splat them all with a SINGLE double-grid transform (the fine-point geometry and Y_lm are
251 ! shared across columns, so batching is much cheaper than one transform per pair/direction).
252 ! Group g = (idir-1)*nprojs^2 + (i-1)*nprojs + j occupies columns [grp_start, grp_start+grp_ncol).
253 ngrp = nprojs*nprojs*dim
254 maxcols = ngrp*paw_onsite_nlm_rho(proj%aug%paw)
255 safe_allocate(big_spl(1:maxcols))
256 safe_allocate(big_ll(1:maxcols))
257 safe_allocate(big_mm(1:maxcols))
258 safe_allocate(big_rcut(1:maxcols))
259 safe_allocate(grp_start(1:ngrp))
260 safe_allocate(grp_ncol(1:ngrp))
261 safe_allocate(arho(1:nlm, 1:nlm))
262 ntot = 0
263 do iproj = 1, nprojs
264 do jproj = 1, nprojs
265 pairidx = (iproj - 1)*nprojs + jproj
266 arho = m_zero
267 arho(map(iproj), map(jproj)) = m_one
268 do idir = 1, dim
269 g = (idir - 1)*nprojs*nprojs + pairidx
270 call paw_onsite_current_diff(proj%aug%paw, arho, idir, ncol, spl, ll, mm, rcut)
271 grp_start(g) = ntot + 1
272 grp_ncol(g) = ncol
273 do col = 1, ncol
274 ntot = ntot + 1
275 big_spl(ntot) = spl(col) ! move the spline handle; freed once after the batch transform
276 big_ll(ntot) = ll(col)
277 big_mm(ntot) = mm(col)
278 big_rcut(ntot) = rcut
279 end do
280 ! drop the column arrays WITHOUT spline_end: the GSL handles now live in big_spl
281 safe_deallocate_a(spl)
282 safe_deallocate_a(ll)
283 safe_deallocate_a(mm)
284 end do
285 end do
286 end do
288 if (ntot > 0 .and. nsph > 0) then
289 safe_allocate(feff(1:nsph, 1:ntot))
290 call double_grid_transform(epot%dgrid, proj%sphere, ntot, big_spl, big_ll, big_mm, &
291 big_rcut, feff, cache = proj%dg_cache)
292 do idir = 1, dim
293 do iproj = 1, nprojs
294 do jproj = 1, nprojs
295 pairidx = (iproj - 1)*nprojs + jproj
296 g = (idir - 1)*nprojs*nprojs + pairidx
297 base = grp_start(g)
298 do col = 1, grp_ncol(g)
299 proj%aug%jbasis(1:nsph, pairidx, idir) = proj%aug%jbasis(1:nsph, pairidx, idir) &
300 + feff(1:nsph, base + col - 1)
301 end do
302 end do
303 end do
304 end do
305 safe_deallocate_a(feff)
306 end if
307
308 if (ntot > 0) call spline_end(big_spl(1:ntot))
309 safe_deallocate_a(big_spl)
310 safe_deallocate_a(big_ll)
311 safe_deallocate_a(big_mm)
312 safe_deallocate_a(big_rcut)
313 safe_deallocate_a(grp_start)
314 safe_deallocate_a(grp_ncol)
315 safe_deallocate_a(arho)
316 safe_deallocate_a(map)
317 end do
318
320 end subroutine paw_ae_build_current_basis
321
322 ! ----------------------------------------------------------------------------------
331 subroutine paw_ae_add_onsite_current(this, epot, current)
332 class(nonlocal_pseudopotential_t), target, intent(in) :: this
333 type(epot_t), target, intent(in) :: epot
334 real(real64), intent(inout) :: current(:, :, :)
335
336 integer :: imat, iatom, nprojs, np, idir, is, nsp, iproj, jproj
337 real(real64), allocatable :: wcol(:), acc(:)
338 type(projector_matrix_t), pointer :: pmat
339 type(projector_t), pointer :: proj
340
342 call profiling_in("PAW_ADD_ONSITE_CURRENT")
343
344 if (.not. allocated(this%onsite_current_matrix)) then
345 call profiling_out("PAW_ADD_ONSITE_CURRENT")
347 return
348 end if
349 nsp = min(size(this%onsite_current_matrix, 4), size(current, 3))
350
351 do imat = 1, this%nprojector_matrices
352 iatom = this%projector_to_atom(imat)
353 proj => epot%proj(iatom)
354 if (.not. allocated(proj%aug%jbasis)) cycle
355 pmat => this%projector_matrices(imat)
356 nprojs = pmat%nprojs
357 np = proj%sphere%np
358 if (np == 0) cycle
359
360 safe_allocate(wcol(1:nprojs*nprojs))
361 safe_allocate(acc(1:np))
362 do is = 1, nsp
363 do idir = 1, size(current, 2)
364 do iproj = 1, nprojs
365 do jproj = 1, nprojs
366 wcol((iproj - 1)*nprojs + jproj) = this%onsite_current_matrix(iproj, jproj, imat, is)
367 end do
368 end do
369 ! acc(:) = jbasis(:, :, idir) . wcol : one matrix-vector product, no double grid
370 call lalg_gemv(np, nprojs*nprojs, m_one, proj%aug%jbasis(:, :, idir), wcol, m_zero, acc)
371 call submesh_add_to_mesh(proj%sphere, acc, current(:, idir, is))
372 end do
373 end do
374 safe_deallocate_a(wcol)
375 safe_deallocate_a(acc)
376 end do
377
378 call profiling_out("PAW_ADD_ONSITE_CURRENT")
380 end subroutine paw_ae_add_onsite_current
381
383
384!! Local Variables:
385!! mode: f90
386!! coding: utf-8
387!! End:
The Ono-Hirose double-grid technique [T. Ono and K. Hirose, Phys. Rev. Lett. 82, 5016 (1999); Phys....
subroutine, public double_grid_transform(this, sm, nf, spl, ll, mm, rcut, feff, cart_dir, cache)
Evaluate a batch of localized functions f_jf(x) = spl_jf(r) * Y_{ll,mm}(x) (or a purely radial spl_jf...
real(real64), parameter, public m_zero
Definition: global.F90:173
real(real64), parameter, public m_one
Definition: global.F90:174
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
All-electron reconstruction of mesh fields from the PAW one-center terms.
subroutine, public paw_ae_add_onsite_current(this, epot, current)
Add the time-dependent one-center all-electron current to the current density.
subroutine, public paw_ae_build_current_basis(this, epot, dim)
Tabulate the one-center all-electron current basis on each projector sphere.
subroutine, public paw_ae_reconstruct_field(this, epot, mesh, density, tau, current)
Reconstruct all-electron mesh fields from the PAW one-center terms. Any subset of the supported quant...
subroutine paw_splat_onsite_columns(dgrid, sphere, ncol, spl, ll, mm, rcut, field_col, cache)
Splat the radial x Y_LM columns of a one-center difference (spl/ll/mm from paw_onsite_density_diff / ...
PAW radial engine.
Definition: paw_onsite.F90:131
subroutine, public paw_onsite_tau_diff(this, rho, ncol, spl, ll, mm, rcut, tau_integral)
Build the one-center all-electron-minus-pseudo kinetic-energy-density difference for a single colline...
subroutine, public paw_onsite_current_diff(this, arho, idir, ncol, spl, ll, mm, rcut, current_integral)
One-center all-electron current-density correction Delta j^alpha = j^1 - j~^1.
integer pure function, public paw_onsite_nlm(this)
Number of (l,m)-resolved projectors, i.e. the leading dimension of rho_ij.
integer pure function, public paw_onsite_nlm_rho(this)
Number of real-harmonic columns (lmax_rho+1)^2 of the one-center density expansion; the upper bound o...
subroutine, public paw_onsite_reorder_pmat(nprojs, map, src, dst)
Reorder one spin channel of a projector-ordered matrix.
subroutine, public paw_onsite_pmat_map(this, nprojs, pl, pm, pchannel, map)
Map the projector-matrix enumeration (l, m, channel) used by the mesh nonlocal machinery to the one-c...
integer pure function, public paw_onsite_nspin(this)
Number of collinear spin channels (1 or 2) the sphere was built for.
subroutine, public paw_onsite_density_diff(this, rho, ncol, spl, ll, mm, rcut)
Build the one-center all-electron-minus-(pseudo+compensation) density difference for a single colline...
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
Persistent, function-independent geometry of the double-grid transpose scatter for one submesh....
A set of projectors defined on a submesh.
The projector data type is intended to hold the local and non-local parts of the pseudopotentials....
Definition: projector.F90:168
the basic spline datatype
Definition: splines.F90:281
A submesh is a type of mesh, used for the projectors in the pseudopotentials It contains points on a ...
Definition: submesh.F90:154