Octopus
double_grid.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 X. Andrade
2!! Copyright (C) 2026 N. Tancogne-Dejean
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
44 use debug_oct_m
45 use global_oct_m
46 use iso_c_binding
47 use, intrinsic :: iso_fortran_env
48 use blas_oct_m
50 use math_oct_m
51 use mesh_oct_m
54!$ use omp_lib
55 use parser_oct_m
59
60 implicit none
61
62 private
63
64 public :: &
73
80 private
81 integer :: ng = 0
82 integer :: nc = 0
83 integer :: amin = 0
84 integer, allocatable :: nt(:)
85 integer, allocatable :: ct(:, :)
86 real(real64), allocatable :: wt(:, :)
87 real(real64), allocatable :: kd(:, :)
88 end type dg_axis_kernel_t
89
97 private
98 logical :: valid = .false.
99 integer :: np = 0
100 integer :: divisor = 0
101 integer :: order = 0
102 integer :: noff = 0
103 integer :: kmin(3) = 0
104 integer :: kmax(3) = 0
105 real(real64) :: rel_ref(3) = m_zero
106 real(real64), allocatable :: xstep(:, :)
107 integer, allocatable :: offs(:, :)
108 real(real64), allocatable :: xoff(:, :)
109 integer, allocatable :: isub(:, :, :)
110 integer, allocatable :: tgt(:, :)
111 real(real64), allocatable :: wgt(:, :)
112 integer, allocatable :: ntgt(:)
113 type(dg_axis_kernel_t) :: ax(3)
115
116 type double_grid_t
117 ! Components are public by default
118 logical :: enabled = .false.
119 integer :: order = 0
120 integer :: divisor = 0
121 integer :: nn = 0
122 integer :: interp_min = 0
123 integer :: interp_max = 0
128 real(real64), allocatable :: co(:, :)
129 end type double_grid_t
130
131contains
132
133 ! ---------------------------------------------------------
134 subroutine double_grid_init(this, namespace, dim)
135 type(double_grid_t), intent(out) :: this
136 type(namespace_t), intent(in) :: namespace
137 integer, intent(in) :: dim
138
139 integer :: aa, ll
140 real(real64), allocatable :: points(:)
141
142 push_sub(double_grid_init)
143
144 !%Variable DoubleGrid
145 !%Type logical
146 !%Default no
147 !%Section Mesh
148 !%Description
149 !% Enables the Ono-Hirose double-grid technique [T. Ono and K. Hirose,
150 !% <i>Phys. Rev. Lett.</i> <b>82</b>, 5016 (1999)]: localized functions of the
151 !% pseudopotentials (Kleinman-Bylander projectors, ultrasoft augmentation
152 !% charges) are evaluated on a fine grid and folded back onto the simulation
153 !% grid with the transpose of a high-order interpolation, at construction time.
154 !% This increases the accuracy of ultrasoft pseudopotentials on coarse grids
155 !% and reduces the egg-box effect. Only available in 3D.
156 !%End
157 if (dim == 3) then
158 call parse_variable(namespace, 'DoubleGrid', .false., this%enabled)
159 else
160 this%enabled = .false.
161 end if
162
163 if (.not. this%enabled) then
164 pop_sub(double_grid_init)
165 return
166 end if
167
168 call messages_experimental('DoubleGrid', namespace=namespace)
169
170 !%Variable DoubleGridOrder
171 !%Type integer
172 !%Default 7
173 !%Section Mesh
174 !%Description
175 !% Order of the Lagrange interpolation whose transpose is used by the
176 !% double-grid technique. Must be an odd number; the integration error
177 !% decreases as O(h^(order+1)). Low-order interpolation is not recommended.
178 !% The interpolation-stencil reach sets the submesh padding
179 !% (margin = (order/2+1)*spacing), so the cost of the double grid grows
180 !% steeply with the order.
181 !%End
182 call parse_variable(namespace, 'DoubleGridOrder', 7, this%order)
183 if (mod(this%order, 2) == 0 .or. this%order < 3) then
184 call messages_input_error(namespace, 'DoubleGridOrder', 'must be an odd number >= 3')
185 end if
186
187 !%Variable DoubleGridDivisor
188 !%Type integer
189 !%Default 3
190 !%Section Mesh
191 !%Description
192 !% Number of fine-grid points per coarse-grid spacing and axis used by the
193 !% double-grid technique. Must be an odd number.
194 !%End
195 call parse_variable(namespace, 'DoubleGridDivisor', 3, this%divisor)
196 if (mod(this%divisor, 2) == 0 .or. this%divisor < 3) then
197 call messages_input_error(namespace, 'DoubleGridDivisor', 'must be an odd number >= 3')
198 end if
199
200 this%nn = (this%divisor - 1)/2
201 this%interp_min = -this%order/2
202 this%interp_max = this%order/2 + 1
203
204 ! Lagrange weights of the coarse points at l*h, l = interp_min:interp_max,
205 ! for interpolation at the fine offset a*h/divisor, a = 1:nn.
206 ! In units of h/divisor the coarse points sit at divisor*l - a relative to
207 ! the fine point, so we evaluate the interpolant at 0.
208 safe_allocate(this%co(this%interp_min:this%interp_max, 1:this%nn))
209 safe_allocate(points(1:this%order + 1))
210 do aa = 1, this%nn
211 do ll = this%interp_min, this%interp_max
212 points(ll - this%interp_min + 1) = real(this%divisor*ll - aa, real64)
213 end do
214 call interpolation_coefficients(this%order + 1, points, m_zero, this%co(:, aa))
215 end do
216 safe_deallocate_a(points)
217
218 pop_sub(double_grid_init)
219 end subroutine double_grid_init
220
221 ! ---------------------------------------------------------
222 subroutine double_grid_end(this)
223 type(double_grid_t), intent(inout) :: this
224
225 push_sub(double_grid_end)
226
227 safe_deallocate_a(this%co)
228 this%enabled = .false.
229
230 pop_sub(double_grid_end)
231 end subroutine double_grid_end
232
233 ! ---------------------------------------------------------
238 real(real64) function double_grid_margin(this, mesh) result(margin)
239 type(double_grid_t), intent(in) :: this
240 class(mesh_t), intent(in) :: mesh
241
242 integer :: i1, i2, i3
243
244 margin = m_zero
245 if (.not. this%enabled) return
246
247 do i1 = -1, 1, 2
248 do i2 = -1, 1, 2
249 do i3 = -1, 1, 2
250 margin = max(margin, norm2(mesh%coord_system%to_cartesian(mesh%spacing(1:3)*[i1, i2, i3])))
251 end do
252 end do
253 end do
254 margin = this%interp_max*margin
255
256 end function double_grid_margin
257
258 ! ---------------------------------------------------------
268 subroutine double_grid_transform(this, sm, nf, spl, ll, mm, rcut, feff, cart_dir, cache)
269 type(double_grid_t), intent(in) :: this
270 type(submesh_t), intent(in) :: sm
271 integer, intent(in) :: nf
272 type(spline_t), intent(in) :: spl(:)
273 integer, intent(in) :: ll(:)
274 integer, intent(in) :: mm(:)
275 real(real64), intent(in) :: rcut(:)
276 real(real64), intent(out) :: feff(:, :)
277 integer, optional, intent(in) :: cart_dir(:)
279 type(double_grid_cache_t), optional, intent(in) :: cache
280
281 integer :: ip, jf
282 real(real64) :: ylm
283 real(real64), allocatable :: rcut_eff(:)
284 integer, allocatable :: cdir(:)
285
286 push_sub(double_grid_transform)
287
288 if (sm%np == 0) then
289 pop_sub(double_grid_transform)
290 return
291 end if
292
293 assert(sm%np > 0)
294 assert(size(feff, 1) >= sm%np)
295 assert(size(spl) >= nf .and. size(ll) >= nf .and. size(mm) >= nf .and. size(rcut) >= nf)
296
297 ! Never evaluate a spline beyond its radial range.
298 safe_allocate(rcut_eff(1:nf))
299 do jf = 1, nf
300 rcut_eff(jf) = min(rcut(jf), spline_range_max(spl(jf)))
301 end do
302
303 safe_allocate(cdir(1:nf))
304 cdir = 0
305 if (present(cart_dir)) cdir(1:nf) = cart_dir(1:nf)
306
307 if (.not. this%enabled) then
308
309 do jf = 1, nf
310 do ip = 1, sm%np
311 if (sm%r(ip) <= rcut_eff(jf)) then
312 feff(ip, jf) = spline_eval(spl(jf), sm%r(ip))
313 if (ll(jf) >= 0) then
314 call ylmr_real(sm%rel_x(1:3, ip), ll(jf), mm(jf), ylm)
315 feff(ip, jf) = feff(ip, jf)*ylm
316 end if
317 if (cdir(jf) > 0) feff(ip, jf) = feff(ip, jf)*sm%rel_x(cdir(jf), ip)
318 else
319 feff(ip, jf) = m_zero
320 end if
321 end do
322 end do
323
324 else
325
326 call double_grid_transform_kernel(this, sm, nf, spl, ll, mm, rcut_eff, cdir, feff, cache)
327
328 end if
329
330 safe_deallocate_a(rcut_eff)
331 safe_deallocate_a(cdir)
332
333 pop_sub(double_grid_transform)
334 end subroutine double_grid_transform
335
336 ! ---------------------------------------------------------
354 subroutine double_grid_transform_kernel(this, sm, nf, spl, ll, mm, rcut, cdir, feff, cache)
355 type(double_grid_t), intent(in) :: this
356 type(submesh_t), intent(in) :: sm
357 integer, intent(in) :: nf
358 type(spline_t), intent(in) :: spl(:)
359 integer, intent(in) :: ll(:)
360 integer, intent(in) :: mm(:)
361 real(real64), intent(in) :: rcut(:)
362 integer, intent(in) :: cdir(:)
363 real(real64), intent(out) :: feff(:, :)
365 type(double_grid_cache_t), optional, target, intent(in) :: cache
366
367 integer :: jf, nlm, im, ngrp, igrp
368 integer :: ng1, ng2, ng3, nc1, nc2, nc3, g1, g2, g3, c1, c2, c3, ip
369 integer, allocatable :: ilm(:), lmlist(:, :), gfirst(:)
370 real(real64) :: rmax, rr, rad, frac, ylm, xfine(1:3), xfine_yz(1:3), e1(1:3)
371 real(real64), allocatable :: vals(:), ylm_lm(:)
372 real(real64), allocatable :: vplane(:, :, :), bplane(:, :, :)
373 integer, allocatable :: active(:)
374 integer :: nact, ia
375 real(real64), allocatable :: xc1(:, :), xc2(:, :), xc3(:, :)
376 real(real64), allocatable :: b1(:, :, :, :), b2(:, :, :, :), feffbox(:, :, :, :)
377 type(c_ptr), allocatable :: acc(:)
378 logical :: own_cache
379 type(double_grid_cache_t), target :: tmp_cache
380 type(double_grid_cache_t), pointer :: cc
381
382 call profiling_in("DOUBLE_GRID")
384
385 assert(.not. sm%mesh%use_curvilinear)
386 assert(sm%mesh%box%dim == 3)
387
388 ! The function-independent geometry (integer keys, bounding box, fine offsets and scatter
389 ! stencil) is taken from a precomputed cache when the caller provides one, or built here for a
390 ! single call otherwise.
391 own_cache = .not. present(cache)
392 if (own_cache) then
393 call double_grid_cache_init(tmp_cache, this, sm)
394 cc => tmp_cache
395 else
396 cc => cache
397 assert(cc%valid)
398 assert(cc%np == sm%np)
399 assert(cc%divisor == this%divisor .and. cc%order == this%order)
400 end if
401
402 rmax = maxval(rcut(1:nf))
403
404 ! Consecutive functions sharing a radial spline and cutoff form a group, whose
405 ! spline is evaluated once per fine point.
406 safe_allocate(gfirst(1:nf + 1))
407 ngrp = 1
408 gfirst(1) = 1
409 do jf = 2, nf
410 if (spline_same(spl(jf), spl(jf - 1)) .and. abs(rcut(jf) - rcut(jf - 1)) < m_epsilon) cycle
411 ngrp = ngrp + 1
412 gfirst(ngrp) = jf
413 end do
414 gfirst(ngrp + 1) = nf + 1
415
416 ! Distinct (l, m) pairs among the functions: the spherical harmonics are evaluated once per
417 ! fine point and distinct pair, and reused by every function. This matters most for the
418 ! ultrasoft augmentation build, where many Q_ij^L columns share a few (l, m).
419 safe_allocate(ilm(1:nf))
420 safe_allocate(lmlist(1:2, 1:nf))
421 nlm = 0
422 do jf = 1, nf
423 ilm(jf) = 0
424 if (ll(jf) < 0) cycle
425 do im = 1, nlm
426 if (lmlist(1, im) == ll(jf) .and. lmlist(2, im) == mm(jf)) then
427 ilm(jf) = im
428 exit
429 end if
430 end do
431 if (ilm(jf) == 0) then
432 nlm = nlm + 1
433 lmlist(1:2, nlm) = [ll(jf), mm(jf)]
434 ilm(jf) = nlm
435 end if
436 end do
437
438 ! Pass 1 builds B1(c1,g2,g3) contracting axis 1 as the fine values are formed; pass 2 -> B2, pass
439 ! 3 -> feff_box on the coarse (submesh-box) grid, which is read out to the submesh points via the
440 ! key map cc%isub. Cost is O(n_fine (order+1)) per axis rather than O(n_fine (order+1)^3).
441 ng1 = cc%ax(1)%ng; ng2 = cc%ax(2)%ng; ng3 = cc%ax(3)%ng
442 nc1 = cc%ax(1)%nc; nc2 = cc%ax(2)%nc; nc3 = cc%ax(3)%nc
443
444 ! Cartesian contribution of each fine index along each axis: xstep(:,k)*(amin_k + (g-1-nn)/div).
445 safe_allocate(xc1(1:3, 1:ng1))
446 safe_allocate(xc2(1:3, 1:ng2))
447 safe_allocate(xc3(1:3, 1:ng3))
448 do g1 = 1, ng1
449 frac = cc%ax(1)%amin + real(g1 - 1 - this%nn, real64)/real(this%divisor, real64)
450 xc1(1:3, g1) = cc%xstep(1:3, 1)*frac
451 end do
452 do g2 = 1, ng2
453 frac = cc%ax(2)%amin + real(g2 - 1 - this%nn, real64)/real(this%divisor, real64)
454 xc2(1:3, g2) = cc%xstep(1:3, 2)*frac
455 end do
456 do g3 = 1, ng3
457 frac = cc%ax(3)%amin + real(g3 - 1 - this%nn, real64)/real(this%divisor, real64)
458 xc3(1:3, g3) = cc%xstep(1:3, 3)*frac
459 end do
460
461 ! Pass 1: form the fine values plane by plane and contract axis 1 into B1(c1,g2,jf,g3). Each
462 ! (g2,g3) plane of fine values is formed pointwise and each pass is a plain matrix
463 ! product with the dense axis kernels kd stored in the cache:
464 ! pass 1 per g3: B1(c1, (f g2)) = K1(nc1, ng1) . V(g1, (f g2))
465 ! pass 2 per (f, g3): B2(c1, c2) = B1(c1, g2) . K2^T(g2, c2)
466 ! pass 3 per f: Fbox((c1 c2), c3) = B2((c1 c2), g3) . K3^T(g3, c3)
467 ! Whole g1 lines are culled by the distance of the line to the sphere center, which is
468 ! the norm of the component of the line offset perpendicular to the axis-1 direction.
469 ! Only the g2 lines that cross the sphere are formed and contracted: they are packed
470 ! in the plane buffer, so that the matrix product does not run over the (many) empty lines
471 ! of the bounding box. Each line writes its own slice of the plane, so the loop parallelizes
472 ! without a reduction and the result is deterministic. Each thread owns a GSL accelerator
473 ! per spline group, as the shared ones are modified on evaluation.
474 e1(1:3) = cc%xstep(1:3, 1)/norm2(cc%xstep(1:3, 1))
475 safe_allocate(b1(1:nc1, 1:ng2, 1:nf, 1:ng3))
476 safe_allocate(vplane(1:ng1, 1:nf, 1:ng2))
477 safe_allocate(bplane(1:nc1, 1:nf, 1:ng2))
478 safe_allocate(active(1:ng2))
479 b1 = m_zero
480 do g3 = 1, ng3
481 nact = 0
482 do g2 = 1, ng2
483 xfine_yz(1:3) = cc%rel_ref(1:3) + xc2(1:3, g2) + xc3(1:3, g3)
484 if (norm2(xfine_yz - dot_product(xfine_yz, e1)*e1) > rmax) cycle
485 nact = nact + 1
486 active(nact) = g2
487 end do
488 if (nact == 0) cycle
489
490 !$omp parallel private(ia, g2, g1, xfine, xfine_yz, rr, rad, im, igrp, ylm, ylm_lm, jf, vals, acc)
491 safe_allocate(vals(1:nf))
492 safe_allocate(ylm_lm(1:max(nlm, 1)))
493 safe_allocate(acc(1:ngrp))
494 do igrp = 1, ngrp
495 call spline_accel_init(acc(igrp))
496 end do
497 !$omp do schedule(dynamic)
498 do ia = 1, nact
499 g2 = active(ia)
500 vplane(:, :, ia) = m_zero
501 xfine_yz(1:3) = cc%rel_ref(1:3) + xc2(1:3, g2) + xc3(1:3, g3)
502 do g1 = 1, ng1
503 xfine(1:3) = xfine_yz(1:3) + xc1(1:3, g1)
504 rr = norm2(xfine)
505 if (rr > rmax) cycle
506 do im = 1, nlm
507 call ylmr_real(xfine, lmlist(1, im), lmlist(2, im), ylm)
508 ylm_lm(im) = ylm
509 end do
510 do igrp = 1, ngrp
511 if (rr > rcut(gfirst(igrp))) then
512 vals(gfirst(igrp):gfirst(igrp + 1) - 1) = m_zero
513 cycle
514 end if
515 rad = spline_eval_accel(spl(gfirst(igrp)), rr, acc(igrp))
516 do jf = gfirst(igrp), gfirst(igrp + 1) - 1
517 vals(jf) = rad
518 if (ilm(jf) > 0) vals(jf) = vals(jf)*ylm_lm(ilm(jf))
519 if (cdir(jf) > 0) vals(jf) = vals(jf)*xfine(cdir(jf))
520 end do
521 end do
522 vplane(g1, 1:nf, ia) = vals(1:nf)
523 end do
524 end do
525 !$omp end do
526 do igrp = 1, ngrp
527 call spline_accel_end(acc(igrp))
528 end do
529 safe_deallocate_a(acc)
530 safe_deallocate_a(vals)
531 safe_deallocate_a(ylm_lm)
532 !$omp end parallel
533
534 call blas_gemm('N', 'N', nc1, nf*nact, ng1, m_one, cc%ax(1)%kd(1, 1), nc1, &
535 vplane(1, 1, 1), ng1, m_zero, bplane(1, 1, 1), nc1)
536 do ia = 1, nact
537 do jf = 1, nf
538 b1(1:nc1, active(ia), jf, g3) = bplane(1:nc1, jf, ia)
539 end do
540 end do
541 end do
542 safe_deallocate_a(vplane)
543 safe_deallocate_a(bplane)
544 safe_deallocate_a(active)
545
546 ! Pass 2: contract g2 against K2 for every (f, g3) slab, disjoint outputs.
547 safe_allocate(b2(1:nc1, 1:nc2, 1:nf, 1:ng3))
548 do g3 = 1, ng3
549 do jf = 1, nf
550 call blas_gemm('N', 'T', nc1, nc2, ng2, m_one, b1(1, 1, jf, g3), nc1, &
551 cc%ax(2)%kd(1, 1), nc2, m_zero, b2(1, 1, jf, g3), nc1)
552 end do
553 end do
554
555 ! Pass 3: contract g3 against K3 per function; the g3 slabs of one function are
556 ! nc1*nc2*nf apart, handled by the leading dimension.
557 safe_allocate(feffbox(1:nc1, 1:nc2, 1:nc3, 1:nf))
558 do jf = 1, nf
559 call blas_gemm('N', 'T', nc1*nc2, nc3, ng3, m_one, b2(1, 1, jf, 1), nc1*nc2*nf, &
560 cc%ax(3)%kd(1, 1), nc3, m_zero, feffbox(1, 1, 1, jf), nc1*nc2)
561 end do
562
563 ! Read the coarse box out to the submesh points via the key map, normalizing by the number of
564 ! fine offsets. Every submesh point has a key in the box, so all feff(1:np) are written.
565 !$omp parallel do schedule(static) private(c3, c2, c1, ip, jf)
566 do c3 = 1, nc3
567 do c2 = 1, nc2
568 do c1 = 1, nc1
569 ip = cc%isub(cc%kmin(1) + c1 - 1, cc%kmin(2) + c2 - 1, cc%kmin(3) + c3 - 1)
570 if (ip > 0) then
571 do jf = 1, nf
572 feff(ip, jf) = feffbox(c1, c2, c3, jf)/real(cc%noff, real64)
573 end do
574 end if
575 end do
576 end do
577 end do
578 !$omp end parallel do
579
580 safe_deallocate_a(xc1)
581 safe_deallocate_a(xc2)
582 safe_deallocate_a(xc3)
583 safe_deallocate_a(b1)
584 safe_deallocate_a(b2)
585 safe_deallocate_a(feffbox)
586 safe_deallocate_a(ilm)
587 safe_deallocate_a(lmlist)
588 safe_deallocate_a(gfirst)
589 if (own_cache) call double_grid_cache_end(tmp_cache)
590
592 call profiling_out("DOUBLE_GRID")
593 end subroutine double_grid_transform_kernel
594
595 ! ---------------------------------------------------------
604 subroutine double_grid_cache_init(cache, this, sm)
605 type(double_grid_cache_t), intent(out) :: cache
606 type(double_grid_t), intent(in) :: this
607 type(submesh_t), intent(in) :: sm
608
609 integer :: ip, idir, io, io1, io2, io3, il1
610 integer :: kmin(1:3), kmax(1:3), key(1:3)
611 real(real64) :: hh(1:3), chi(1:3), rel_ref(1:3)
612
613 push_sub(double_grid_cache_init)
614
615 assert(.not. sm%mesh%use_curvilinear)
616 assert(sm%mesh%box%dim == 3)
617
618 cache%np = sm%np
619 cache%divisor = this%divisor
620 cache%order = this%order
621 hh(1:3) = sm%mesh%spacing(1:3)
622 rel_ref(1:3) = sm%rel_x(1:3, 1)
623 cache%rel_ref(1:3) = rel_ref(1:3)
624
625 ! Integer keys of the local submesh points, relative to point 1, and their bounding box.
626 ! Coordinate differences are mapped to the primitive space, where mesh points (and their
627 ! periodic replicas) are exact multiples of the spacing.
628 kmin = 0
629 kmax = 0
630 do ip = 1, sm%np
631 chi = sm%mesh%coord_system%from_cartesian(sm%rel_x(1:3, ip) - rel_ref(1:3))
632 key = nint(chi/hh)
633 assert(all(abs(chi/hh - key) < 1e-6_real64))
634 kmin = min(kmin, key)
635 kmax = max(kmax, key)
636 end do
637 cache%kmin(1:3) = kmin(1:3)
638 cache%kmax(1:3) = kmax(1:3)
639
640 safe_allocate(cache%isub(kmin(1):kmax(1), kmin(2):kmax(2), kmin(3):kmax(3)))
641 cache%isub = 0
642 do ip = 1, sm%np
643 chi = sm%mesh%coord_system%from_cartesian(sm%rel_x(1:3, ip) - rel_ref(1:3))
644 key = nint(chi/hh)
645 assert(cache%isub(key(1), key(2), key(3)) == 0)
646 cache%isub(key(1), key(2), key(3)) = ip
647 end do
648
649 ! Cartesian steps of one grid cell per axis, and of the fine offsets.
650 safe_allocate(cache%xstep(1:3, 1:3))
651 do idir = 1, 3
652 chi = m_zero
653 chi(idir) = hh(idir)
654 cache%xstep(1:3, idir) = sm%mesh%coord_system%to_cartesian(chi)
655 end do
656
657 cache%noff = this%divisor**3
658 safe_allocate(cache%offs(1:3, 1:cache%noff))
659 safe_allocate(cache%xoff(1:3, 1:cache%noff))
660 io = 0
661 do io3 = -this%nn, this%nn
662 do io2 = -this%nn, this%nn
663 do io1 = -this%nn, this%nn
664 io = io + 1
665 cache%offs(1:3, io) = [io1, io2, io3]
666 cache%xoff(1:3, io) = (cache%xstep(1:3, 1)*io1 + cache%xstep(1:3, 2)*io2 &
667 + cache%xstep(1:3, 3)*io3)/this%divisor
668 end do
669 end do
670 end do
671
672 ! Per-axis scatter targets and weights for each fine offset o: the sites l*sign(o) of the
673 ! mirrored stencil with weights co(l, |o|); for o = 0 the interpolation is exact at the site
674 ! itself (single target, weight 1).
675 safe_allocate(cache%tgt(1:this%order + 1, -this%nn:this%nn))
676 safe_allocate(cache%wgt(1:this%order + 1, -this%nn:this%nn))
677 safe_allocate(cache%ntgt(-this%nn:this%nn))
678 do io1 = -this%nn, this%nn
679 if (io1 == 0) then
680 cache%ntgt(io1) = 1
681 cache%tgt(1, io1) = 0
682 cache%wgt(1, io1) = m_one
683 else
684 cache%ntgt(io1) = this%order + 1
685 do il1 = this%interp_min, this%interp_max
686 cache%tgt(il1 - this%interp_min + 1, io1) = il1*sign(1, io1)
687 cache%wgt(il1 - this%interp_min + 1, io1) = this%co(il1, abs(io1))
688 end do
689 end if
690 end do
691
692 ! Separable 1D transpose-interpolation kernels K_k(c_k, g_k), one per axis. The fine index
693 ! g_k = (ia_k - amin_k)*divisor + (o_k + nn) + 1 enumerates anchor ia_k in [amin_k, amax_k] (the
694 ! submesh key box expanded by the stencil reach) and sub-offset o_k in [-nn, nn]. Each fine index
695 ! scatters to the coarse (submesh-box) points c_k = ia_k + tgt(il, o_k) that fall inside
696 ! [kmin_k, kmax_k], with weight wgt(il, o_k); targets outside the box are dropped exactly as in
697 ! the reference scatter.
698 do idir = 1, 3
699 cache%ax(idir)%amin = kmin(idir) - this%interp_max
700 cache%ax(idir)%nc = kmax(idir) - kmin(idir) + 1
701 cache%ax(idir)%ng = (kmax(idir) - kmin(idir) + 1 + 2*this%interp_max)*this%divisor
702 safe_allocate(cache%ax(idir)%nt(1:cache%ax(idir)%ng))
703 safe_allocate(cache%ax(idir)%ct(1:this%order + 1, 1:cache%ax(idir)%ng))
704 safe_allocate(cache%ax(idir)%wt(1:this%order + 1, 1:cache%ax(idir)%ng))
705 cache%ax(idir)%nt = 0
706 call build_axis_kernel(cache%ax(idir), kmin(idir), kmax(idir))
707 end do
708
709 cache%valid = .true.
710
712 contains
713
715 subroutine build_axis_kernel(ax, kmn, kmx)
716 type(dg_axis_kernel_t), intent(inout) :: ax
717 integer, intent(in) :: kmn, kmx
718
719 integer :: g, g0, iia, oo, il, cc, nt
720
721 do g = 1, ax%ng
722 g0 = g - 1
723 iia = ax%amin + g0/this%divisor ! anchor key
724 oo = mod(g0, this%divisor) - this%nn ! sub-offset in [-nn, nn]
725 nt = 0
726 do il = 1, cache%ntgt(oo)
727 cc = iia + cache%tgt(il, oo) ! coarse key
728 if (cc < kmn .or. cc > kmx) cycle ! drop targets outside the submesh box
729 nt = nt + 1
730 ax%ct(nt, g) = cc - kmn + 1 ! local coarse index in [1, nc]
731 ax%wt(nt, g) = cache%wgt(il, oo)
732 end do
733 ax%nt(g) = nt
734 end do
735
736 ! dense form of the same weights, for the matrix-product transform passes
737 safe_allocate(ax%kd(1:ax%nc, 1:ax%ng))
738 ax%kd = m_zero
739 do g = 1, ax%ng
740 do il = 1, ax%nt(g)
741 ax%kd(ax%ct(il, g), g) = ax%wt(il, g)
742 end do
743 end do
744 end subroutine build_axis_kernel
745
746 end subroutine double_grid_cache_init
747
748 ! ---------------------------------------------------------
749 subroutine double_grid_cache_end(cache)
750 type(double_grid_cache_t), intent(inout) :: cache
751
752 integer :: idir
753
754 push_sub(double_grid_cache_end)
755
756 cache%valid = .false.
757 cache%np = 0
758 cache%noff = 0
759 safe_deallocate_a(cache%isub)
760 safe_deallocate_a(cache%offs)
761 safe_deallocate_a(cache%xoff)
762 safe_deallocate_a(cache%xstep)
763 safe_deallocate_a(cache%tgt)
764 safe_deallocate_a(cache%wgt)
765 safe_deallocate_a(cache%ntgt)
766 do idir = 1, 3
767 cache%ax(idir)%ng = 0
768 cache%ax(idir)%nc = 0
769 safe_deallocate_a(cache%ax(idir)%nt)
770 safe_deallocate_a(cache%ax(idir)%ct)
771 safe_deallocate_a(cache%ax(idir)%wt)
772 safe_deallocate_a(cache%ax(idir)%kd)
773 end do
774
775 pop_sub(double_grid_cache_end)
776 end subroutine double_grid_cache_end
777
778end module double_grid_oct_m
779
780!! Local Variables:
781!! mode: f90
782!! coding: utf-8
783!! End:
subroutine build_axis_kernel(ax, kmn, kmx)
Fill one axis kernel: for every fine index g = (ia,o), enumerate its coarse targets.
This module contains interfaces for BLAS routines You should not use these routines directly....
Definition: blas.F90:97
The Ono-Hirose double-grid technique [T. Ono and K. Hirose, Phys. Rev. Lett. 82, 5016 (1999); Phys....
real(real64) function, public double_grid_margin(this, mesh)
Submesh radius enlargement needed to contain the effective support of a transformed function: the ten...
subroutine, public double_grid_end(this)
subroutine double_grid_transform_kernel(this, sm, nf, spl, ll, mm, rcut, cdir, feff, cache)
The transpose-interpolation scatter. All arithmetic is done with integer grid keys relative to the fi...
subroutine, public double_grid_cache_init(cache, this, sm)
Precompute the function-independent geometry of the double-grid transpose scatter for one submesh: th...
subroutine, public double_grid_init(this, namespace, dim)
subroutine, public double_grid_cache_end(cache)
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
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
subroutine, public interpolation_coefficients(nn, xa, xx, cc)
Definition: math.F90:736
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:671
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1020
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
One-dimensional transpose-interpolation kernel along a single Cartesian axis. The full 3D tensor-prod...
Persistent, function-independent geometry of the double-grid transpose scatter for one submesh....
Describes mesh distribution to nodes.
Definition: mesh.F90:165
int true(void)