(git:3d5d2e5)
Loading...
Searching...
No Matches
ps_wavelet_kernel.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 Creates the wavelet kernel for the wavelet based poisson solver.
10!> \author Florian Schiffmann (09.2007,fschiff)
11! **************************************************************************************************
13 USE fft_api, ONLY: fft_allocate,&
15 USE fft_tools, ONLY: bwfft,&
16 fft_1d,&
20 USE kinds, ONLY: dp
21 USE mathconstants, ONLY: pi
28#include "../base/base_uses.f90"
29
30 IMPLICIT NONE
31
32 PRIVATE
33
34 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'ps_wavelet_kernel'
35
36! *** Public data types ***
37
38 PUBLIC :: createkernel
39
40CONTAINS
41
42! **************************************************************************************************
43!> \brief Allocate a pointer which corresponds to the zero-padded FFT slice needed for
44!> calculating the convolution with the kernel expressed in the interpolating scaling
45!> function basis. The kernel pointer is unallocated on input, allocated on output.
46!> \param geocode Indicates the boundary conditions (BC) of the problem:
47!> 'F' free BC, isolated systems.
48!> The program calculates the solution as if the given density is
49!> "alone" in R^3 space.
50!> 'S' surface BC, isolated in y direction, periodic in xz plane
51!> The given density is supposed to be periodic in the xz plane,
52!> so the dimensions in these direction mus be compatible with the FFT
53!> Beware of the fact that the isolated direction is y!
54!> 'P' periodic BC.
55!> The density is supposed to be periodic in all the three directions,
56!> then all the dimensions must be compatible with the FFT.
57!> No need for setting up the kernel.
58!> \param n01 dimensions of the real space grid to be hit with the Poisson Solver
59!> \param n02 dimensions of the real space grid to be hit with the Poisson Solver
60!> \param n03 dimensions of the real space grid to be hit with the Poisson Solver
61!> \param hx grid spacings. For the isolated BC case for the moment they are supposed to
62!> be equal in the three directions
63!> \param hy grid spacings. For the isolated BC case for the moment they are supposed to
64!> be equal in the three directions
65!> \param hz grid spacings. For the isolated BC case for the moment they are supposed to
66!> be equal in the three directions
67!> \param itype_scf order of the interpolating scaling functions used in the decomposition
68!> \param iproc ,nproc: number of process, number of processes
69!> \param nproc ...
70!> \param kernel pointer for the kernel FFT. Unallocated on input, allocated on output.
71!> Its dimensions are equivalent to the region of the FFT space for which the
72!> kernel is injective. This will divide by two each direction,
73!> since the kernel for the zero-padded convolution is real and symmetric.
74!> \param mpi_group ...
75!> \date February 2007
76!> \author Luigi Genovese
77!> \note Due to the fact that the kernel dimensions are unknown before the calling, the kernel
78!> must be declared as pointer in input of this routine.
79!> To avoid that, one can properly define the kernel dimensions by adding
80!> the nd1,nd2,nd3 arguments to the PS_dim4allocation routine, then eliminating the pointer
81!> declaration.
82! **************************************************************************************************
83 SUBROUTINE createkernel(geocode, n01, n02, n03, hx, hy, hz, itype_scf, iproc, nproc, kernel, mpi_group)
84
85 CHARACTER(LEN=1), INTENT(IN) :: geocode
86 INTEGER, INTENT(IN) :: n01, n02, n03
87 REAL(kind=dp), INTENT(IN) :: hx, hy, hz
88 INTEGER, INTENT(IN) :: itype_scf, iproc, nproc
89 REAL(kind=dp), POINTER :: kernel(:)
90 CLASS(mp_comm_type), INTENT(IN) :: mpi_group
91
92 INTEGER :: m1, m2, m3, md1, md2, md3, n1, n2, n3, &
93 nd1, nd2, nd3, nlimd, nlimk
94 REAL(kind=dp) :: hgrid
95
96 hgrid = max(hx, hy, hz)
97
98 IF (geocode == 'P') THEN
99
100 CALL f_fft_dimensions(n01, n02, n03, m1, m2, m3, n1, n2, n3, &
101 md1, md2, md3, nd1, nd2, nd3, nproc)
102
103 ALLOCATE (kernel(1))
104 nlimd = n2
105 nlimk = 0
106
107 ELSE IF (geocode == 'S') THEN
108
109 CALL s_fft_dimensions(n01, n02, n03, m1, m2, m3, n1, n2, n3, &
110 md1, md2, md3, nd1, nd2, nd3, nproc)
111
112 ALLOCATE (kernel(nd1*nd2*nd3/nproc))
113
114 !the kernel must be built and scattered to all the processes
115
116 CALL surfaces_kernel(n1, n2, n3, m3, nd1, nd2, nd3, hx, hz, hy, &
117 itype_scf, kernel, iproc, nproc, mpi_group)
118 !last plane calculated for the density and the kernel
119
120 nlimd = n2
121 nlimk = n3/2 + 1
122 ELSE IF (geocode == 'F') THEN
123
124 !Build the Kernel
125
126 CALL f_fft_dimensions(n01, n02, n03, m1, m2, m3, n1, n2, n3, &
127 md1, md2, md3, nd1, nd2, nd3, nproc)
128 ALLOCATE (kernel(nd1*nd2*nd3/nproc))
129
130 !the kernel must be built and scattered to all the processes
131 CALL free_kernel(n01, n02, n03, n1, n2, n3, nd1, nd2, nd3, hgrid, &
132 itype_scf, iproc, nproc, kernel, mpi_group)
133
134 !last plane calculated for the density and the kernel
135 nlimd = n2/2
136 nlimk = n3/2 + 1
137
138 ELSE
139
140 cpabort("No wavelet based poisson solver for given geometry")
141
142 END IF
143 END SUBROUTINE createkernel
144
145! **************************************************************************************************
146!> \brief Build the kernel of the Poisson operator with
147!> surfaces Boundary conditions
148!> in an interpolating scaling functions basis.
149!> Beware of the fact that the nonperiodic direction is y!
150!> \param n1 Dimensions for the FFT
151!> \param n2 Dimensions for the FFT
152!> \param n3 Dimensions for the FFT
153!> \param m3 Actual dimension in non-periodic direction
154!> \param nker1 Dimensions of the kernel (nker3=n3/2+1) nker(1,2)=n(1,2)/2+1
155!> \param nker2 Dimensions of the kernel (nker3=n3/2+1) nker(1,2)=n(1,2)/2+1
156!> \param nker3 Dimensions of the kernel (nker3=n3/2+1) nker(1,2)=n(1,2)/2+1
157!> \param h1 Mesh steps in the three dimensions
158!> \param h2 Mesh steps in the three dimensions
159!> \param h3 Mesh steps in the three dimensions
160!> \param itype_scf Order of the scaling function
161!> \param karray output array
162!> \param iproc Number of process
163!> \param nproc number of processes
164!> \param mpi_group ...
165!> \date October 2006
166!> \author L. Genovese
167! **************************************************************************************************
168 SUBROUTINE surfaces_kernel(n1, n2, n3, m3, nker1, nker2, nker3, h1, h2, h3, &
169 itype_scf, karray, iproc, nproc, mpi_group)
170
171 INTEGER, INTENT(IN) :: n1, n2, n3, m3, nker1, nker2, nker3
172 REAL(kind=dp), INTENT(IN) :: h1, h2, h3
173 INTEGER, INTENT(IN) :: itype_scf, iproc, nproc
174 REAL(kind=dp), &
175 DIMENSION(nker1, nker2, nker3/nproc), &
176 INTENT(OUT) :: karray
177 TYPE(mp_comm_type), INTENT(IN) :: mpi_group
178
179 INTEGER, PARAMETER :: n_points = 2**6, ncache_optimal = 8*1024
180
181 COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:), &
182 POINTER :: halfft_cache1, halfft_cache2
183 INTEGER :: final_chunk_size, i1, i2, i3, iend, imu, ind1, ind2, ipolyord, ireim, istart, j2, &
184 j2nd, j2st, jnd1, jp2, jreim, n_cell, n_range, n_scf, nact2, ncache, nfft, num_of_mus, &
185 shift
186 REAL(kind=dp) :: a, b, c, cp, d, diff, dx, fei, fer, foi, &
187 for, fr, mu1, pion, ponx, pony, sp, &
188 value, x
189 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: kernel_scf, x_scf, y_scf
190 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: cossinarr
191 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: kernel
192 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: kernel_mpi
193 REAL(kind=dp), DIMENSION(9, 8) :: cpol
194 TYPE(fft_plan_type) :: fft_plan_bw, fft_plan_bw_last
195
196!Better if higher (1024 points are enough 10^{-14}: 2*itype_scf*n_points)
197!FFT arrays
198!coefficients for the polynomial interpolation
199!assign the values of the coefficients
200
201 karray = 0.0_dp
202 cpol(:, :) = 1._dp
203 cpol(1, 2) = .25_dp
204 cpol(1, 3) = 1._dp/3._dp
205 cpol(1, 4) = 7._dp/12._dp
206 cpol(2, 4) = 8._dp/3._dp
207 cpol(1, 5) = 19._dp/50._dp
208 cpol(2, 5) = 3._dp/2._dp
209 cpol(1, 6) = 41._dp/272._dp
210 cpol(2, 6) = 27._dp/34._dp
211 cpol(3, 6) = 27._dp/272._dp
212 cpol(1, 7) = 751._dp/2989._dp
213 cpol(2, 7) = 73._dp/61._dp
214 cpol(3, 7) = 27._dp/61._dp
215 cpol(1, 8) = -989._dp/4540._dp
216 cpol(2, 8) = -1472._dp/1135._dp
217 cpol(3, 8) = 232._dp/1135._dp
218 cpol(4, 8) = -2624._dp/1135._dp
219
220 !renormalize values
221 cpol(1, 1) = .5_dp*cpol(1, 1)
222 cpol(1:2, 2) = 2._dp/3._dp*cpol(1:2, 2)
223 cpol(1:2, 3) = 3._dp/8._dp*cpol(1:2, 3)
224 cpol(1:3, 4) = 2._dp/15._dp*cpol(1:3, 4)
225 cpol(1:3, 5) = 25._dp/144._dp*cpol(1:3, 5)
226 cpol(1:4, 6) = 34._dp/105._dp*cpol(1:4, 6)
227 cpol(1:4, 7) = 2989._dp/17280._dp*cpol(1:4, 7)
228 cpol(1:5, 8) = -454._dp/2835._dp*cpol(1:5, 8)
229
230 !assign the complete values
231 cpol(2, 1) = cpol(1, 1)
232 cpol(3, 2) = cpol(1, 2)
233 cpol(3, 3) = cpol(2, 3)
234 cpol(4, 3) = cpol(1, 3)
235 cpol(4, 4) = cpol(2, 4)
236 cpol(5, 4) = cpol(1, 4)
237 cpol(4, 5) = cpol(3, 5)
238 cpol(5, 5) = cpol(2, 5)
239 cpol(6, 5) = cpol(1, 5)
240 cpol(5, 6) = cpol(3, 6)
241 cpol(6, 6) = cpol(2, 6)
242 cpol(7, 6) = cpol(1, 6)
243 cpol(5, 7) = cpol(4, 7)
244 cpol(6, 7) = cpol(3, 7)
245 cpol(7, 7) = cpol(2, 7)
246 cpol(8, 7) = cpol(1, 7)
247 cpol(6, 8) = cpol(4, 8)
248 cpol(7, 8) = cpol(3, 8)
249 cpol(8, 8) = cpol(2, 8)
250 cpol(9, 8) = cpol(1, 8)
251
252 !Number of integration points : 2*itype_scf*n_points
253 n_scf = 2*itype_scf*n_points
254 !Allocations
255 ALLOCATE (x_scf(0:n_scf))
256 ALLOCATE (y_scf(0:n_scf))
257
258 !Build the scaling function
259 CALL scaling_function(itype_scf, n_scf, n_range, x_scf, y_scf)
260 !Step grid for the integration
261 dx = real(n_range, kind=dp)/real(n_scf, kind=dp)
262 !Extend the range (no more calculations because fill in by 0._dp)
263 n_cell = m3
264 n_range = max(n_cell, n_range)
265
266 !Allocations
267 ncache = ncache_optimal
268 !the HalFFT must be performed only in the third dimension,
269 !and nker3=n3/2+1, hence
270 IF (ncache <= (nker3 - 1)*4) ncache = nker3 - 1*4
271
272 !enlarge the second dimension of the kernel to be compatible with nproc
273 nact2 = nker2
274 enlarge_ydim: DO
275 IF (nproc*(nact2/nproc) /= nact2) THEN
276 nact2 = nact2 + 1
277 ELSE
278 EXIT enlarge_ydim
279 END IF
280 END DO enlarge_ydim
281
282 !array for the MPI procedure
283 ALLOCATE (kernel(nker1, nact2/nproc, nker3))
284 ALLOCATE (kernel_mpi(nker1, nact2/nproc, nker3/nproc, nproc))
285 ALLOCATE (kernel_scf(n_range))
286 CALL fft_allocate(halfft_cache1, [max(ncache/4, n3/2)])
287 halfft_cache1 = cmplx(0.0_dp, 0.0_dp, dp)
288 CALL fft_allocate(halfft_cache2, [max(ncache/4, n3/2)])
289 halfft_cache2 = cmplx(0.0_dp, 0.0_dp, dp)
290 ALLOCATE (cossinarr(2, n3/2 - 1))
291
292 !build the phases for the HalFFT reconstruction
293 pion = 2._dp*pi/real(n3, kind=dp)
294 DO i3 = 2, n3/2
295 x = real(i3 - 1, kind=dp)*pion
296 cossinarr(1, i3 - 1) = cos(x)
297 cossinarr(2, i3 - 1) = -sin(x)
298 END DO
299
300 ! satisfy valgrind, init arrays to large value, even if the offending bit is (likely?) padding
301 kernel = huge(0._dp)
302 kernel_mpi = huge(0._dp)
303
304 !calculate the limits of the FFT calculations
305 !that can be performed in a row remaining inside the cache
306 num_of_mus = max(1, ncache/(2*n3))
307
308 diff = 0._dp
309 !order of the polynomial to be used for integration (must be a power of two)
310 ipolyord = 8 !this part should be incorporated inside the numerical integration
311 !here we have to choice the piece of the x-y grid to cover
312
313 !let us now calculate the fraction of mu that will be considered
314 j2st = iproc*(nact2/nproc)
315 j2nd = min((iproc + 1)*(nact2/nproc), n2/2 + 1)
316
317 ! The size of the last chunk
318 final_chunk_size = mod((n1/2 + 1)*(j2nd - j2st), num_of_mus)
319
320 ! Prevent OOB-access if n1 < num_of_mus
321 IF ((n1/2 + 1)*(j2nd - j2st) >= num_of_mus) THEN
322 CALL fft_create_plan_1d(fft_plan_bw, bwfft, .true., .true., num_of_mus, num_of_mus, &
323 n3/2, num_of_mus, halfft_cache1, halfft_cache2)
324 END IF
325 IF (final_chunk_size > 0) THEN
326 CALL fft_create_plan_1d(fft_plan_bw_last, bwfft, .true., .true., num_of_mus, num_of_mus, &
327 n3/2, final_chunk_size, halfft_cache1, halfft_cache2)
328 END IF
329
330 DO ind2 = (n1/2 + 1)*j2st + 1, (n1/2 + 1)*j2nd, num_of_mus
331 istart = ind2
332 iend = min(ind2 + (num_of_mus - 1), (n1/2 + 1)*j2nd)
333 nfft = iend - istart + 1
334 shift = 0
335
336 !initialization of the interesting part of the cache array
337 ! THis could be replaced using a R2C-FFT (not yet available in CP2K)
338 halfft_cache1 = cmplx(0.0_dp, 0.0_dp, dp)
339 halfft_cache1 = cmplx(0.0_dp, 0.0_dp, dp)
340
341 IF (istart == 1) THEN
342 !i2=1
343 shift = 1
344
345 CALL calculates_green_opt_muzero(n_range, n_scf, ipolyord, x_scf, y_scf, &
346 cpol(1, ipolyord), dx, kernel_scf)
347
348 !copy of the first zero value
349 halfft_cache1(1) = cmplx(0._dp, aimag(halfft_cache1(1)), dp)
350
351 DO i3 = 1, m3
352
353 value = 0.5_dp*h3*kernel_scf(i3)
354 !index in where to copy the value of the kernel
355 CALL indices(ireim, num_of_mus, n3/2 + i3, 1, ind1)
356 !index in where to copy the symmetric value
357 CALL indices(jreim, num_of_mus, n3/2 + 2 - i3, 1, jnd1)
358 IF (ireim == 1) THEN
359 halfft_cache1(ind1) = cmplx(value, aimag(halfft_cache1(ind1)), dp)
360 ELSE
361 halfft_cache1(ind1) = cmplx(real(halfft_cache1(ind1), dp), value, dp)
362 END IF
363 IF (jreim == 1) THEN
364 halfft_cache1(jnd1) = cmplx(value, aimag(halfft_cache1(jnd1)), dp)
365 ELSE
366 halfft_cache1(jnd1) = cmplx(real(halfft_cache1(jnd1), dp), value, dp)
367 END IF
368
369 END DO
370
371 END IF
372
373 loopimpulses: DO imu = istart + shift, iend
374
375 !here there is the value of mu associated to hgrid
376 !note that we have multiplicated mu for hgrid to be comparable
377 !with mu0ref
378
379 !calculate the proper value of mu taking into account the periodic dimensions
380 !corresponding value of i1 and i2
381 i1 = mod(imu, n1/2 + 1)
382 IF (i1 == 0) i1 = n1/2 + 1
383 i2 = (imu - i1)/(n1/2 + 1) + 1
384 ponx = real(i1 - 1, kind=dp)/real(n1, kind=dp)
385 pony = real(i2 - 1, kind=dp)/real(n2, kind=dp)
386
387 mu1 = 2._dp*pi*sqrt((ponx/h1)**2 + (pony/h2)**2)*h3
388
389 CALL calculates_green_opt(n_range, n_scf, itype_scf, ipolyord, x_scf, y_scf, &
390 cpol(1, ipolyord), mu1, dx, kernel_scf)
391
392 !readjust the coefficient and define the final kernel
393
394 !copy of the first zero value
395 halfft_cache1(imu - istart + 1) = cmplx(0._dp, aimag(halfft_cache1(imu - istart + 1)), dp)
396 DO i3 = 1, m3
397 value = -0.5_dp*h3/mu1*kernel_scf(i3)
398 !index in where to copy the value of the kernel
399 CALL indices(ireim, num_of_mus, n3/2 + i3, imu - istart + 1, ind1)
400 !index in where to copy the symmetric value
401 CALL indices(jreim, num_of_mus, n3/2 + 2 - i3, imu - istart + 1, jnd1)
402 IF (ireim == 1) THEN
403 halfft_cache1(ind1) = cmplx(value, aimag(halfft_cache1(ind1)), dp)
404 ELSE
405 halfft_cache1(ind1) = cmplx(real(halfft_cache1(ind1), dp), value, dp)
406 END IF
407 IF (jreim == 1) THEN
408 halfft_cache1(jnd1) = cmplx(value, aimag(halfft_cache1(jnd1)), dp)
409 ELSE
410 halfft_cache1(jnd1) = cmplx(real(halfft_cache1(jnd1), dp), value, dp)
411 END IF
412 END DO
413
414 END DO loopimpulses
415
416 !now perform the FFT of the array in cache
417 ! This is equivalent to
418 IF (nfft == num_of_mus) THEN
419 CALL fft_1d(fft_plan_bw, halfft_cache1, halfft_cache2, 1.0_dp)
420 ELSE
421 CALL fft_1d(fft_plan_bw_last, halfft_cache1, halfft_cache2, 1.0_dp)
422 END IF
423
424 !assign the values of the FFT array
425 !and compare with the good results
426 DO imu = istart, iend
427
428 !corresponding value of i1 and i2
429 i1 = mod(imu, n1/2 + 1)
430 IF (i1 == 0) i1 = n1/2 + 1
431 i2 = (imu - i1)/(n1/2 + 1) + 1
432
433 j2 = i2 - j2st
434
435 a = real(halfft_cache2(imu - istart + 1), dp)
436 b = aimag(halfft_cache2(imu - istart + 1))
437 kernel(i1, j2, 1) = a + b
438 kernel(i1, j2, n3/2 + 1) = a - b
439
440 DO i3 = 2, n3/2
441 ind1 = imu - istart + 1 + num_of_mus*(i3 - 1)
442 jnd1 = imu - istart + 1 + num_of_mus*(n3/2 + 2 - i3 - 1)
443 cp = cossinarr(1, i3 - 1)
444 sp = cossinarr(2, i3 - 1)
445 a = real(halfft_cache2(ind1), dp)
446 b = aimag(halfft_cache2(ind1))
447 c = real(halfft_cache2(jnd1), dp)
448 d = aimag(halfft_cache2(jnd1))
449 fer = .5_dp*(a + c)
450 fei = .5_dp*(b - d)
451 for = .5_dp*(a - c)
452 foi = .5_dp*(b + d)
453 fr = fer + cp*foi - sp*for
454 kernel(i1, j2, i3) = fr
455 END DO
456 END DO
457
458 END DO
459
460 IF ((n1/2 + 1)*(j2nd - j2st) >= num_of_mus) CALL fft_destroy_plan(fft_plan_bw)
461 IF (final_chunk_size > 0) CALL fft_destroy_plan(fft_plan_bw_last)
462
463 !give to each processor a slice of the third dimension
464 IF (nproc > 1) THEN
465 CALL mpi_group%alltoall(kernel, &!nker1*(nact2/nproc)*(nker3/nproc), &
466 kernel_mpi, nker1*(nact2/nproc)*(nker3/nproc))
467
468 DO jp2 = 1, nproc
469 DO i3 = 1, nker3/nproc
470 DO i2 = 1, nact2/nproc
471 j2 = i2 + (jp2 - 1)*(nact2/nproc)
472 IF (j2 <= nker2) THEN
473 DO i1 = 1, nker1
474 karray(i1, j2, i3) = &
475 kernel_mpi(i1, i2, i3, jp2)
476 END DO
477 END IF
478 END DO
479 END DO
480 END DO
481
482 ELSE
483 karray(1:nker1, 1:nker2, 1:nker3) = kernel(1:nker1, 1:nker2, 1:nker3)
484 END IF
485
486 !De-allocations
487 DEALLOCATE (kernel)
488 DEALLOCATE (kernel_mpi)
489 CALL fft_deallocate(halfft_cache1)
490 CALL fft_deallocate(halfft_cache2)
491 DEALLOCATE (kernel_scf)
492 DEALLOCATE (x_scf)
493 DEALLOCATE (y_scf)
494
495 END SUBROUTINE surfaces_kernel
496
497! **************************************************************************************************
498!> \brief ...
499!> \param n ...
500!> \param n_scf ...
501!> \param itype_scf ...
502!> \param intorder ...
503!> \param xval ...
504!> \param yval ...
505!> \param c ...
506!> \param mu ...
507!> \param hres ...
508!> \param g_mu ...
509! **************************************************************************************************
510 SUBROUTINE calculates_green_opt(n, n_scf, itype_scf, intorder, xval, yval, c, mu, hres, g_mu)
511 INTEGER, INTENT(IN) :: n, n_scf, itype_scf, intorder
512 REAL(kind=dp), DIMENSION(0:n_scf), INTENT(IN) :: xval, yval
513 REAL(kind=dp), DIMENSION(intorder+1), INTENT(IN) :: c
514 REAL(kind=dp), INTENT(IN) :: mu, hres
515 REAL(kind=dp), DIMENSION(n), INTENT(OUT) :: g_mu
516
517 REAL(kind=dp), PARAMETER :: mu_max = 0.2_dp
518
519 INTEGER :: i, iend, ikern, ivalue, izero, n_iter, &
520 nrec
521 REAL(kind=dp) :: f, filter, fl, fr, gleft, gltmp, gright, &
522 grtmp, mu0, ratio, x, x0, x1
523 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: green, green1
524
525 g_mu = 0.0_dp
526 !We calculate the number of iterations to go from mu0 to mu0_ref
527 IF (mu <= mu_max) THEN
528 n_iter = 0
529 mu0 = mu
530 ELSE
531 n_iter = 1
532 loop_iter: DO
533 ratio = real(2**n_iter, kind=dp)
534 mu0 = mu/ratio
535 IF (mu0 <= mu_max) THEN
536 EXIT loop_iter
537 END IF
538 n_iter = n_iter + 1
539 END DO loop_iter
540 END IF
541
542 !dimension needed for the correct calculation of the recursion
543 nrec = 2**n_iter*n
544
545 ALLOCATE (green(-nrec:nrec))
546
547 !initialization of the branching value
548 ikern = 0
549 izero = 0
550 initialization: DO
551 IF (xval(izero) >= real(ikern, kind=dp) .OR. izero == n_scf) EXIT initialization
552 izero = izero + 1
553 END DO initialization
554 green = 0._dp
555 !now perform the interpolation in right direction
556 ivalue = izero
557 gright = 0._dp
558 loop_right: DO
559 IF (ivalue >= n_scf - intorder - 1) EXIT loop_right
560 DO i = 1, intorder + 1
561 x = xval(ivalue) - real(ikern, kind=dp)
562 f = yval(ivalue)*exp(-mu0*x)
563 filter = intorder*c(i)
564 gright = gright + filter*f
565 ivalue = ivalue + 1
566 END DO
567 ivalue = ivalue - 1
568 END DO loop_right
569 iend = n_scf - ivalue
570 DO i = 1, iend
571 x = xval(ivalue) - real(ikern, kind=dp)
572 f = yval(ivalue)*exp(-mu0*x)
573 filter = intorder*c(i)
574 gright = gright + filter*f
575 ivalue = ivalue + 1
576 END DO
577 gright = hres*gright
578
579 !the scaling function is symmetric, so the same for the other direction
580 gleft = gright
581
582 green(ikern) = gleft + gright
583
584 !now the loop until the last value
585 DO ikern = 1, nrec
586 gltmp = 0._dp
587 grtmp = 0._dp
588 ivalue = izero
589 x0 = xval(izero)
590 loop_integration: DO
591 IF (izero == n_scf) EXIT loop_integration
592 DO i = 1, intorder + 1
593 x = xval(ivalue)
594 fl = yval(ivalue)*exp(mu0*x)
595 fr = yval(ivalue)*exp(-mu0*x)
596 filter = intorder*c(i)
597 gltmp = gltmp + filter*fl
598 grtmp = grtmp + filter*fr
599 ivalue = ivalue + 1
600 IF (xval(izero) >= real(ikern, kind=dp) .OR. izero == n_scf) THEN
601 x1 = xval(izero)
602 EXIT loop_integration
603 END IF
604 izero = izero + 1
605 END DO
606 ivalue = ivalue - 1
607 izero = izero - 1
608 END DO loop_integration
609 gleft = exp(-mu0)*(gleft + hres*exp(-mu0*real(ikern - 1, kind=dp))*gltmp)
610 IF (izero == n_scf) THEN
611 gright = 0._dp
612 ELSE
613 gright = exp(mu0)*(gright - hres*exp(mu0*real(ikern - 1, kind=dp))*grtmp)
614 END IF
615 green(ikern) = gleft + gright
616 green(-ikern) = gleft + gright
617 IF (abs(green(ikern)) <= 1.e-20_dp) THEN
618 nrec = ikern
619 EXIT
620 END IF
621 END DO
622 !now we must calculate the recursion
623 ALLOCATE (green1(-nrec:nrec))
624
625 !Start the iteration to go from mu0 to mu
626 CALL scf_recursion(itype_scf, n_iter, nrec, green(-nrec), green1(-nrec))
627
628 DO i = 1, min(n, nrec)
629 g_mu(i) = green(i - 1)
630 END DO
631 DO i = min(n, nrec) + 1, n
632 g_mu(i) = 0._dp
633 END DO
634
635 DEALLOCATE (green, green1)
636
637 END SUBROUTINE calculates_green_opt
638
639! **************************************************************************************************
640!> \brief ...
641!> \param n ...
642!> \param n_scf ...
643!> \param intorder ...
644!> \param xval ...
645!> \param yval ...
646!> \param c ...
647!> \param hres ...
648!> \param green ...
649! **************************************************************************************************
650 PURE SUBROUTINE calculates_green_opt_muzero(n, n_scf, intorder, xval, yval, c, hres, green)
651 INTEGER, INTENT(IN) :: n, n_scf, intorder
652 REAL(kind=dp), DIMENSION(0:n_scf), INTENT(IN) :: xval, yval
653 REAL(kind=dp), DIMENSION(intorder+1), INTENT(IN) :: c
654 REAL(kind=dp), INTENT(IN) :: hres
655 REAL(kind=dp), DIMENSION(n), INTENT(OUT) :: green
656
657 INTEGER :: i, iend, ikern, ivalue, izero
658 REAL(kind=dp) :: c0, c1, filter, gl0, gl1, gr0, gr1, x, y
659
660!initialization of the branching value
661
662 ikern = 0
663 izero = 0
664 initialization: DO
665 IF (xval(izero) >= real(ikern, kind=dp) .OR. izero == n_scf) EXIT initialization
666 izero = izero + 1
667 END DO initialization
668 green = 0._dp
669 !first case, ikern=0
670 !now perform the interpolation in right direction
671 ivalue = izero
672 gr1 = 0._dp
673 loop_right: DO
674 IF (ivalue >= n_scf - intorder - 1) EXIT loop_right
675 DO i = 1, intorder + 1
676 x = xval(ivalue)
677 y = yval(ivalue)
678 filter = intorder*c(i)
679 gr1 = gr1 + filter*x*y
680 ivalue = ivalue + 1
681 END DO
682 ivalue = ivalue - 1
683 END DO loop_right
684 iend = n_scf - ivalue
685 DO i = 1, iend
686 x = xval(ivalue)
687 y = yval(ivalue)
688 filter = intorder*c(i)
689 gr1 = gr1 + filter*x*y
690 ivalue = ivalue + 1
691 END DO
692 gr1 = hres*gr1
693 !the scaling function is symmetric
694 gl1 = -gr1
695 gl0 = 0.5_dp
696 gr0 = 0.5_dp
697
698 green(1) = 2._dp*gr1
699
700 !now the loop until the last value
701 DO ikern = 1, n - 1
702 c0 = 0._dp
703 c1 = 0._dp
704 ivalue = izero
705 loop_integration: DO
706 IF (izero == n_scf) EXIT loop_integration
707 DO i = 1, intorder + 1
708 x = xval(ivalue)
709 y = yval(ivalue)
710 filter = intorder*c(i)
711 c0 = c0 + filter*y
712 c1 = c1 + filter*y*x
713 ivalue = ivalue + 1
714 IF (xval(izero) >= real(ikern, kind=dp) .OR. izero == n_scf) THEN
715 EXIT loop_integration
716 END IF
717 izero = izero + 1
718 END DO
719 ivalue = ivalue - 1
720 izero = izero - 1
721 END DO loop_integration
722 c0 = hres*c0
723 c1 = hres*c1
724
725 gl0 = gl0 + c0
726 gl1 = gl1 + c1
727 gr0 = gr0 - c0
728 gr1 = gr1 - c1
729 !general case
730 green(ikern + 1) = real(ikern, kind=dp)*(gl0 - gr0) + gr1 - gl1
731 END DO
732
733 END SUBROUTINE calculates_green_opt_muzero
734
735! **************************************************************************************************
736!> \brief ...
737!> \param var_realimag ...
738!> \param nelem ...
739!> \param intrn ...
740!> \param extrn ...
741!> \param index ...
742! **************************************************************************************************
743 PURE ELEMENTAL SUBROUTINE indices(var_realimag, nelem, intrn, extrn, index)
744
745 INTEGER, INTENT(OUT) :: var_realimag
746 INTEGER, INTENT(IN) :: nelem, intrn, extrn
747 INTEGER, INTENT(OUT) :: index
748
749 INTEGER :: i
750
751!real or imaginary part
752
753 var_realimag = 2 - mod(intrn, 2)
754!actual index over half the length
755
756 i = (intrn + 1)/2
757 !complete index to be assigned
758 index = extrn + nelem*(i - 1)
759
760 END SUBROUTINE indices
761
762! **************************************************************************************************
763!> \brief Build the kernel of a gaussian function
764!> for interpolating scaling functions.
765!> Do the parallel HalFFT of the symmetrized function and stores into
766!> memory only 1/8 of the grid divided by the number of processes nproc
767!>
768!> Build the kernel (karray) of a gaussian function
769!> for interpolating scaling functions
770!> $$ K(j) = \sum_k \omega_k \int \int \phi(x) g_k(x'-x) \delta(x'- j) dx dx' $$
771!> \param n01 Mesh dimensions of the density
772!> \param n02 Mesh dimensions of the density
773!> \param n03 Mesh dimensions of the density
774!> \param nfft1 Dimensions of the FFT grid (HalFFT in the third direction)
775!> \param nfft2 Dimensions of the FFT grid (HalFFT in the third direction)
776!> \param nfft3 Dimensions of the FFT grid (HalFFT in the third direction)
777!> \param n1k Dimensions of the kernel FFT
778!> \param n2k Dimensions of the kernel FFT
779!> \param n3k Dimensions of the kernel FFT
780!> \param hgrid Mesh step
781!> \param itype_scf Order of the scaling function (8,14,16)
782!> \param iproc ...
783!> \param nproc ...
784!> \param karray ...
785!> \param mpi_group ...
786!> \date February 2006
787!> \author T. Deutsch, L. Genovese
788! **************************************************************************************************
789 SUBROUTINE free_kernel(n01, n02, n03, nfft1, nfft2, nfft3, n1k, n2k, n3k, &
790 hgrid, itype_scf, iproc, nproc, karray, mpi_group)
791
792 INTEGER, INTENT(IN) :: n01, n02, n03, nfft1, nfft2, nfft3, n1k, &
793 n2k, n3k
794 REAL(kind=dp), INTENT(IN) :: hgrid
795 INTEGER, INTENT(IN) :: itype_scf, iproc, nproc
796 REAL(kind=dp), DIMENSION(n1k, n2k, n3k/nproc), &
797 INTENT(OUT) :: karray
798 TYPE(mp_comm_type), INTENT(IN) :: mpi_group
799
800 INTEGER, PARAMETER :: n_gauss = 89, n_points = 2**6
801 REAL(kind=dp), PARAMETER :: p0_ref = 1._dp
802
803 INTEGER :: i, i01, i02, i03, i1, i2, i3, i_gauss, &
804 i_kern, iend, istart, istart1, n1h, &
805 n2h, n3h, n_cell, n_iter, n_range, &
806 n_scf, nker1, nker2, nker3
807 REAL(kind=dp) :: a1, a2, a3, a_range, absci, acc_gauss, &
808 dr_gauss, dx, factor, factor2, kern, &
809 p0_cell, p0gauss, pgauss, ur_gauss
810 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: kern_1_scf, kernel_scf, x_scf, y_scf
811 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :, :) :: kp
812 REAL(kind=dp), DIMENSION(n_gauss) :: p_gauss, w_gauss
813
814!Do not touch !!!!
815!Better if higher (1024 points are enough 10^{-14}: 2*itype_scf*n_points)
816!Better p_gauss for calculation
817!(the support of the exponential should be inside [-n_range/2,n_range/2])
818!Number of integration points : 2*itype_scf*n_points
819
820 n_scf = 2*itype_scf*n_points
821 !Set karray
822 karray = 0.0_dp
823 !here we must set the dimensions for the fft part, starting from the nfft
824 !remember that actually nfft2 is associated to n03 and viceversa
825
826 !dimensions that define the center of symmetry
827 n1h = nfft1/2
828 n2h = nfft2/2
829 n3h = nfft3/2
830
831 !Auxiliary dimensions only for building the FFT part
832 nker1 = nfft1
833 nker2 = nfft2
834 nker3 = nfft3/2 + 1
835
836 !adjusting the last two dimensions to be multiples of nproc
837 DO
838 IF (modulo(nker2, nproc) == 0) EXIT
839 nker2 = nker2 + 1
840 END DO
841 DO
842 IF (modulo(nker3, nproc) == 0) EXIT
843 nker3 = nker3 + 1
844 END DO
845
846 !this will be the array of the kernel in the real space
847 ALLOCATE (kp(n1h + 1, n3h + 1, nker2/nproc))
848
849 !defining proper extremes for the calculation of the
850 !local part of the kernel
851
852 istart = iproc*nker2/nproc + 1
853 iend = min((iproc + 1)*nker2/nproc, n2h + n03)
854
855 istart1 = istart
856 IF (iproc == 0) istart1 = n2h - n03 + 2
857
858 !Allocations
859 ALLOCATE (x_scf(0:n_scf))
860 ALLOCATE (y_scf(0:n_scf))
861
862 !Build the scaling function
863 CALL scaling_function(itype_scf, n_scf, n_range, x_scf, y_scf)
864 !Step grid for the integration
865 dx = real(n_range, kind=dp)/real(n_scf, kind=dp)
866 !Extend the range (no more calculations because fill in by 0._dp)
867 n_cell = max(n01, n02, n03)
868 n_range = max(n_cell, n_range)
869
870 !Allocations
871 ALLOCATE (kernel_scf(-n_range:n_range))
872 ALLOCATE (kern_1_scf(-n_range:n_range))
873
874 !Lengthes of the box (use FFT dimension)
875 a1 = hgrid*real(n01, kind=dp)
876 a2 = hgrid*real(n02, kind=dp)
877 a3 = hgrid*real(n03, kind=dp)
878
879 x_scf(:) = hgrid*x_scf(:)
880 y_scf(:) = 1._dp/hgrid*y_scf(:)
881 dx = hgrid*dx
882 !To have a correct integration
883 p0_cell = p0_ref/(hgrid*hgrid)
884
885 !Initialization of the gaussian (Beylkin)
886 CALL gequad(p_gauss, w_gauss, ur_gauss, dr_gauss, acc_gauss)
887 !In order to have a range from a_range=sqrt(a1*a1+a2*a2+a3*a3)
888 !(biggest length in the cube)
889 !We divide the p_gauss by a_range**2 and a_gauss by a_range
890 a_range = sqrt(a1*a1 + a2*a2 + a3*a3)
891 factor = 1._dp/a_range
892 !factor2 = factor*factor
893 factor2 = 1._dp/(a1*a1 + a2*a2 + a3*a3)
894 DO i_gauss = 1, n_gauss
895 p_gauss(i_gauss) = factor2*p_gauss(i_gauss)
896 END DO
897 DO i_gauss = 1, n_gauss
898 w_gauss(i_gauss) = factor*w_gauss(i_gauss)
899 END DO
900
901 kp(:, :, :) = 0._dp
902 !Use in this order (better for accuracy).
903 loop_gauss: DO i_gauss = n_gauss, 1, -1
904 !Gaussian
905 pgauss = p_gauss(i_gauss)
906
907 !We calculate the number of iterations to go from pgauss to p0_ref
908 n_iter = nint((log(pgauss) - log(p0_cell))/log(4._dp))
909 IF (n_iter <= 0) THEN
910 n_iter = 0
911 p0gauss = pgauss
912 ELSE
913 p0gauss = pgauss/4._dp**n_iter
914 END IF
915
916 !Stupid integration
917 !Do the integration with the exponential centered in i_kern
918 kernel_scf(:) = 0._dp
919 DO i_kern = 0, n_range
920 kern = 0._dp
921 DO i = 0, n_scf
922 absci = x_scf(i) - real(i_kern, kind=dp)*hgrid
923 absci = absci*absci
924 kern = kern + y_scf(i)*exp(-p0gauss*absci)*dx
925 END DO
926 kernel_scf(i_kern) = kern
927 kernel_scf(-i_kern) = kern
928 IF (abs(kern) < 1.e-18_dp) THEN
929 !Too small not useful to calculate
930 EXIT
931 END IF
932 END DO
933
934 !Start the iteration to go from p0gauss to pgauss
935 CALL scf_recursion(itype_scf, n_iter, n_range, kernel_scf, kern_1_scf)
936
937 !Add to the kernel (only the local part)
938
939 DO i3 = istart1, iend
940 i03 = i3 - n2h - 1
941 ! Crash if index out of range
942 ! Without compiler bounds checking, the calculation might run successfully but
943 ! it is also possible that the Hartree energy will blow up
944 ! This seems to happen with large MPI processor counts if the size of the
945 ! RS grid is not directly compatible with the allowed FFT dimensions
946 IF (i03 < -n_range .OR. i03 > n_range) THEN
947 CALL cp_abort(__location__, "Index out of range in wavelet solver. "// &
948 "Try decreasing the number of MPI processors, or adjust the PW_CUTOFF or cell size "// &
949 "so that 2*(number of RS grid points) matches the allowed FFT dimensions "// &
950 "(see ps_wavelet_fft3d.F) exactly.")
951 END IF
952 DO i2 = 1, n02
953 i02 = i2 - 1
954 DO i1 = 1, n01
955 i01 = i1 - 1
956 kp(i1, i2, i3 - istart + 1) = kp(i1, i2, i3 - istart + 1) + w_gauss(i_gauss)* &
957 kernel_scf(i01)*kernel_scf(i02)*kernel_scf(i03)
958 END DO
959 END DO
960 END DO
961
962 END DO loop_gauss
963
964 !De-allocations
965 DEALLOCATE (kernel_scf)
966 DEALLOCATE (kern_1_scf)
967 DEALLOCATE (x_scf)
968 DEALLOCATE (y_scf)
969
970!!!!END KERNEL CONSTRUCTION
971
972!!$ if(iproc .eq. 0) print *,"Do a 3D PHalFFT for the kernel"
973
974 CALL kernelfft(nfft1, nfft2, nfft3, nker1, nker2, nker3, n1k, n2k, n3k, nproc, iproc, &
975 kp, karray, mpi_group)
976
977 !De-allocations
978 DEALLOCATE (kp)
979
980 END SUBROUTINE free_kernel
981
982! **************************************************************************************************
983!> \brief ...
984!> \param n1 ...
985!> \param n3 ...
986!> \param lot ...
987!> \param nfft ...
988!> \param i1 ...
989!> \param zf ...
990!> \param zw ...
991! **************************************************************************************************
992 PURE SUBROUTINE inserthalf(n1, n3, lot, nfft, i1, zf, zw)
993 INTEGER, INTENT(IN) :: n1, n3, lot, nfft, i1
994 REAL(kind=dp), DIMENSION(n1/2+1, n3/2+1), &
995 INTENT(IN) :: zf
996 COMPLEX(KIND=dp), DIMENSION(lot, n3/2), &
997 INTENT(OUT) :: zw
998
999 INTEGER :: i01, i03i, i03r, i3, l1, l3
1000
1001 zw = 0.0_dp
1002 i3 = 0
1003 DO l3 = 1, n3, 2
1004 i3 = i3 + 1
1005 i03r = abs(l3 - n3/2 - 1) + 1
1006 i03i = abs(l3 - n3/2) + 1
1007 DO l1 = 1, nfft
1008 i01 = abs(l1 - 1 + i1 - n1/2 - 1) + 1
1009 zw(l1, i3) = cmplx(zf(i01, i03r), zf(i01, i03i), dp)
1010 END DO
1011 END DO
1012
1013 END SUBROUTINE inserthalf
1014
1015! **************************************************************************************************
1016!> \brief (Based on suitable modifications of S.Goedecker routines)
1017!> Calculates the FFT of the distributed kernel
1018!> \param n1 logical dimension of the transform.
1019!> \param n2 logical dimension of the transform.
1020!> \param n3 logical dimension of the transform.
1021!> \param nd1 Dimensions of work arrays
1022!> \param nd2 Dimensions of work arrays
1023!> \param nd3 Dimensions of work arrays
1024!> \param nk1 ...
1025!> \param nk2 ...
1026!> \param nk3 ...
1027!> \param nproc number of processors used as returned by MPI_COMM_SIZE
1028!> \param iproc [0:nproc-1] number of processor as returned by MPI_COMM_RANK
1029!> \param zf Real kernel (input)
1030!> zf(i1,i2,i3)
1031!> \param zr Distributed Kernel FFT
1032!> zr(2,i1,i2,i3)
1033!> \param mpi_group ...
1034!> \date February 2006
1035!> \par Restrictions
1036!> Copyright (C) Stefan Goedecker, Cornell University, Ithaca, USA, 1994
1037!> Copyright (C) Stefan Goedecker, MPI Stuttgart, Germany, 1999
1038!> Copyright (C) 2002 Stefan Goedecker, CEA Grenoble
1039!> This file is distributed under the terms of the
1040!> GNU General Public License, see http://www.gnu.org/copyleft/gpl.txt .
1041!> \author S. Goedecker, L. Genovese
1042! **************************************************************************************************
1043 SUBROUTINE kernelfft(n1, n2, n3, nd1, nd2, nd3, nk1, nk2, nk3, nproc, iproc, zf, zr, mpi_group)
1044
1045 INTEGER, INTENT(IN) :: n1, n2, n3, nd1, nd2, nd3, nk1, nk2, &
1046 nk3, nproc, iproc
1047 REAL(kind=dp), &
1048 DIMENSION(n1/2+1, n3/2+1, nd2/nproc), &
1049 INTENT(IN) :: zf
1050 REAL(kind=dp), DIMENSION(nk1, nk2, nk3/nproc), &
1051 INTENT(INOUT) :: zr
1052 TYPE(mp_comm_type), INTENT(IN) :: mpi_group
1053
1054 INTEGER, PARAMETER :: ncache_optimal = 8*1024
1055
1056 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: zmpi2
1057 COMPLEX(KIND=dp), ALLOCATABLE, &
1058 DIMENSION(:, :, :, :) :: zmpi1
1059 COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:), &
1060 POINTER :: zw1, zw2
1061 COMPLEX(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
1062 POINTER :: zt
1063 INTEGER :: final_chunk_size1, final_chunk_size2, final_chunk_size3, i1, i3, j, j2, j2st, j3, &
1064 jp2st, lot1, lot2, lot3, lzt, ma, mb, ncache, nfft
1065 REAL(kind=dp) :: twopion
1066 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: cosinarr
1067 TYPE(fft_plan_type) :: fft_plan_bw1, fft_plan_bw1_last, &
1068 fft_plan_bw2, fft_plan_bw2_last, &
1069 fft_plan_bw3, fft_plan_bw3_last
1070
1071!work arrays for transpositions
1072!work arrays for MPI
1073!cache work array
1074!FFT work arrays
1075!Body
1076!check input
1077
1078 cpassert(nd1 >= n1)
1079 cpassert(nd2 >= n2)
1080 cpassert(nd3 >= n3/2 + 1)
1081 cpassert(mod(nd3, nproc) == 0)
1082 cpassert(mod(nd2, nproc) == 0)
1083 mark_used(nd1)
1084
1085 !defining work arrays dimensions
1086 ncache = ncache_optimal
1087 IF (ncache <= max(n1, n2, n3/2)*4) ncache = max(n1, n2, n3/2)*4
1088 lzt = n2
1089 IF (mod(n2, 2) == 0) lzt = lzt + 1
1090
1091 !Allocations
1092 CALL fft_allocate(zw1, [ncache/4])
1093 zw1 = cmplx(0.0_dp, 0.0_dp, kind=dp)
1094 CALL fft_allocate(zw2, [ncache/4])
1095 zw2 = cmplx(0.0_dp, 0.0_dp, kind=dp)
1096 CALL fft_allocate(zt, [lzt, n1])
1097 zt = cmplx(0.0_dp, 0.0_dp, kind=dp)
1098 ALLOCATE (zmpi2(n1, nd2/nproc, nd3), source=cmplx(0.0_dp, 0.0_dp, dp))
1099 ALLOCATE (cosinarr(2, n3/2))
1100 IF (nproc > 1) ALLOCATE (zmpi1(n1, nd2/nproc, nd3/nproc, nproc), source=cmplx(0.0_dp, 0.0_dp, dp))
1101
1102 !Calculating array of phases for HalFFT decoding
1103 twopion = 8._dp*atan(1._dp)/real(n3, kind=dp)
1104 DO i3 = 1, n3/2
1105 cosinarr(1, i3) = cos(twopion*(i3 - 1))
1106 cosinarr(2, i3) = -sin(twopion*(i3 - 1))
1107 END DO
1108
1109 !transform along z axis
1110
1111 lot1 = ncache/(4*n1)
1112 lot2 = ncache/(4*n2)
1113 lot3 = ncache/(2*n3)
1114
1115 ! The size of the last chunk
1116 final_chunk_size1 = mod(n2, lot1)
1117 final_chunk_size2 = mod(nk1, lot2)
1118 final_chunk_size3 = mod(n1, lot3)
1119
1120 ! Prevent OOB-access if n2 < lot1
1121 IF (n2 >= lot1) THEN
1122 CALL fft_create_plan_1d(fft_plan_bw1, bwfft, .true., .true., lot1, lzt, n1, lot1, zw1, zt)
1123 END IF
1124 IF (final_chunk_size1 > 0) THEN
1125 CALL fft_create_plan_1d(fft_plan_bw1_last, bwfft, .true., .true., lot1, lzt, n1, &
1126 final_chunk_size1, zw1, zt)
1127 END IF
1128
1129 ! Prevent OOB-access if n2 < lot1
1130 IF (nk1 >= lot2) THEN
1131 CALL fft_create_plan_1d(fft_plan_bw2, bwfft, .true., .true., lot2, lot2, n2, lot2, zw1, zw2)
1132 END IF
1133 IF (final_chunk_size2 > 0) THEN
1134 CALL fft_create_plan_1d(fft_plan_bw2_last, bwfft, .true., .true., lot2, lot2, n2, &
1135 final_chunk_size2, zw1, zw2)
1136 END IF
1137
1138 ! Prevent OOB-access if n1 < lot3
1139 IF (n1 >= lot3) THEN
1140 CALL fft_create_plan_1d(fft_plan_bw3, bwfft, .true., .true., lot3, lot3, n3/2, lot3, zw1, zw2)
1141 END IF
1142 IF (final_chunk_size3 > 0) THEN
1143 CALL fft_create_plan_1d(fft_plan_bw3_last, bwfft, .true., .true., &
1144 lot3, lot3, n3/2, final_chunk_size3, zw1, zw2)
1145 END IF
1146
1147 DO j2 = 1, nd2/nproc
1148 !this condition ensures that we manage only the interesting part for the FFT
1149 IF (iproc*(nd2/nproc) + j2 <= n2) THEN
1150 DO i1 = 1, n1, lot3
1151 ma = i1
1152 mb = min(i1 + (lot3 - 1), n1)
1153 nfft = mb - ma + 1
1154
1155 !inserting real data into complex array of half length
1156 !input: I1,I3,J2,(Jp2)
1157
1158 CALL inserthalf(n1, n3, lot3, nfft, i1, zf(1, 1, j2), zw1)
1159
1160 !performing FFT
1161 ! This is equivalent to
1162 IF (nfft == lot3) THEN
1163 CALL fft_1d(fft_plan_bw3, zw1, zw2, 1.0_dp)
1164 ELSE
1165 CALL fft_1d(fft_plan_bw3_last, zw1, zw2, 1.0_dp)
1166 END IF
1167 !output: I1,i3,J2,(Jp2)
1168
1169 !unpacking FFT in order to restore correct result,
1170 !while exchanging components
1171 !input: I1,i3,J2,(Jp2)
1172 CALL scramble_unpack(i1, j2, lot3, nfft, n1, n3, nd2, nproc, nd3, zw2, zmpi2, cosinarr)
1173 !output: I1,J2,i3,(Jp2)
1174 END DO
1175 END IF
1176 END DO
1177
1178 !Interprocessor data transposition
1179 !input: I1,J2,j3,jp3,(Jp2)
1180 IF (nproc > 1) THEN
1181 !communication scheduling
1182 CALL mpi_group%alltoall(zmpi2, zmpi1, n1*(nd2/nproc)*(nd3/nproc))
1183 ! output: I1,J2,j3,Jp2,(jp3)
1184 END IF
1185
1186 DO j3 = 1, nd3/nproc
1187 !this condition ensures that we manage only the interesting part for the FFT
1188 IF (iproc*(nd3/nproc) + j3 <= n3/2 + 1) THEN
1189 jp2st = 1
1190 j2st = 1
1191
1192 !transform along x axis
1193
1194 DO j = 1, n2, lot1
1195 ma = j
1196 mb = min(j + (lot1 - 1), n2)
1197 nfft = mb - ma + 1
1198
1199 !reverse ordering
1200 !input: I1,J2,j3,Jp2,(jp3)
1201 IF (nproc == 1) THEN
1202 CALL mpiswitch(j3, nfft, jp2st, j2st, lot1, n1, nd2, nd3, nproc, zmpi2, zw1)
1203 ELSE
1204 CALL mpiswitch(j3, nfft, jp2st, j2st, lot1, n1, nd2, nd3, nproc, zmpi1, zw1)
1205 END IF
1206 !output: J2,Jp2,I1,j3,(jp3)
1207
1208 !performing FFT
1209 !input: I2,I1,j3,(jp3)
1210 ! This is equivalent to
1211 IF (nfft == lot1) THEN
1212 CALL fft_1d(fft_plan_bw1, zw1, zt(j:, 1), 1.0_dp)
1213 ELSE
1214 CALL fft_1d(fft_plan_bw1_last, zw1, zt(j:, 1), 1.0_dp)
1215 END IF
1216 !output: I2,i1,j3,(jp3)
1217 END DO
1218
1219 !transform along y axis, and taking only the first half
1220
1221 DO j = 1, nk1, lot2
1222 ma = j
1223 mb = min(j + (lot2 - 1), nk1)
1224 nfft = mb - ma + 1
1225
1226 !reverse ordering
1227 !input: I2,i1,j3,(jp3)
1228 CALL switch(nfft, n2, lot2, n1, lzt, zt(:, j), zw1)
1229 !output: i1,I2,j3,(jp3)
1230
1231 !performing FFT
1232 !input: i1,I2,j3,(jp3)
1233 ! This is equivalent to
1234 IF (nfft == lot2) THEN
1235 CALL fft_1d(fft_plan_bw2, zw1, zw2, 1.0_dp)
1236 ELSE
1237 CALL fft_1d(fft_plan_bw2_last, zw1, zw2, 1.0_dp)
1238 END IF
1239
1240 CALL realcopy(lot2, nfft, n2, nk1, nk2, zw2, zr(j, 1, j3))
1241
1242 END DO
1243 !output: i1,i2,j3,(jp3)
1244 END IF
1245 END DO
1246
1247 IF (n2 >= lot1) CALL fft_destroy_plan(fft_plan_bw1)
1248 IF (final_chunk_size1 > 0) CALL fft_destroy_plan(fft_plan_bw1_last)
1249
1250 IF (nk1 >= lot2) CALL fft_destroy_plan(fft_plan_bw2)
1251 IF (final_chunk_size2 > 0) CALL fft_destroy_plan(fft_plan_bw2_last)
1252
1253 IF (n1 >= lot3) CALL fft_destroy_plan(fft_plan_bw3)
1254 IF (final_chunk_size3 > 0) CALL fft_destroy_plan(fft_plan_bw3_last)
1255
1256 !De-allocations
1257 DEALLOCATE (zmpi2)
1258 CALL fft_deallocate(zw1)
1259 CALL fft_deallocate(zw2)
1260 CALL fft_deallocate(zt)
1261 DEALLOCATE (cosinarr)
1262 IF (nproc > 1) DEALLOCATE (zmpi1)
1263
1264 END SUBROUTINE kernelfft
1265
1266! **************************************************************************************************
1267!> \brief ...
1268!> \param lot ...
1269!> \param nfft ...
1270!> \param n2 ...
1271!> \param nk1 ...
1272!> \param nk2 ...
1273!> \param zin ...
1274!> \param zout ...
1275! **************************************************************************************************
1276 PURE SUBROUTINE realcopy(lot, nfft, n2, nk1, nk2, zin, zout)
1277 INTEGER, INTENT(IN) :: lot, nfft, n2, nk1, nk2
1278 COMPLEX(KIND=dp), DIMENSION(lot, n2), INTENT(IN) :: zin
1279 REAL(kind=dp), DIMENSION(nk1, nk2), INTENT(INOUT) :: zout
1280
1281 INTEGER :: i, j
1282
1283 DO i = 1, nk2
1284 DO j = 1, nfft
1285 zout(j, i) = real(zin(j, i), dp)
1286 END DO
1287 END DO
1288
1289 END SUBROUTINE realcopy
1290
1291! **************************************************************************************************
1292!> \brief ...
1293!> \param nfft ...
1294!> \param n2 ...
1295!> \param lot ...
1296!> \param n1 ...
1297!> \param lzt ...
1298!> \param zt ...
1299!> \param zw ...
1300! **************************************************************************************************
1301 PURE SUBROUTINE switch(nfft, n2, lot, n1, lzt, zt, zw)
1302 INTEGER, INTENT(IN) :: nfft, n2, lot, n1, lzt
1303 COMPLEX(KIND=dp), INTENT(IN) :: zt(lzt, n1)
1304 COMPLEX(KIND=dp), INTENT(INOUT) :: zw(lot, n2)
1305
1306 INTEGER :: i, j
1307
1308 DO j = 1, nfft
1309 DO i = 1, n2
1310 zw(j, i) = zt(i, j)
1311 END DO
1312 END DO
1313 END SUBROUTINE switch
1314
1315! **************************************************************************************************
1316!> \brief ...
1317!> \param j3 ...
1318!> \param nfft ...
1319!> \param Jp2st ...
1320!> \param J2st ...
1321!> \param lot ...
1322!> \param n1 ...
1323!> \param nd2 ...
1324!> \param nd3 ...
1325!> \param nproc ...
1326!> \param zmpi1 ...
1327!> \param zw ...
1328! **************************************************************************************************
1329 PURE SUBROUTINE mpiswitch(j3, nfft, Jp2st, J2st, lot, n1, nd2, nd3, nproc, zmpi1, zw)
1330 INTEGER, INTENT(IN) :: j3, nfft
1331 INTEGER, INTENT(INOUT) :: jp2st, j2st
1332 INTEGER, INTENT(IN) :: lot, n1, nd2, nd3, nproc
1333 COMPLEX(KIND=dp), INTENT(IN) :: zmpi1(n1, nd2/nproc, nd3/nproc, nproc)
1334 COMPLEX(KIND=dp), INTENT(INOUT) :: zw(lot, n1)
1335
1336 INTEGER :: i1, j2, jp2, mfft
1337
1338 mfft = 0
1339 DO jp2 = jp2st, nproc
1340 DO j2 = j2st, nd2/nproc
1341 mfft = mfft + 1
1342 IF (mfft > nfft) THEN
1343 jp2st = jp2
1344 j2st = j2
1345 RETURN
1346 END IF
1347 DO i1 = 1, n1
1348 zw(mfft, i1) = zmpi1(i1, j2, j3, jp2)
1349 END DO
1350 END DO
1351 j2st = 1
1352 END DO
1353 END SUBROUTINE mpiswitch
1354
1355! **************************************************************************************************
1356!> \brief ...
1357!> \param p ...
1358!> \param w ...
1359!> \param urange ...
1360!> \param drange ...
1361!> \param acc ...
1362! **************************************************************************************************
1363 PURE SUBROUTINE gequad(p, w, urange, drange, acc)
1364!
1365 REAL(kind=dp), INTENT(OUT) :: p(89), w(89), urange, drange, acc
1366
1367!
1368!
1369! range [10^(-9),1] and accuracy ~10^(-8);
1370!
1371!
1372
1373 p(1) = 4.96142640560223544e19_dp
1374 p(2) = 1.37454269147978052e19_dp
1375 p(3) = 7.58610013441204679e18_dp
1376 p(4) = 4.42040691347806996e18_dp
1377 p(5) = 2.61986077948367892e18_dp
1378 p(6) = 1.56320138155496681e18_dp
1379 p(7) = 9.35645215863028402e17_dp
1380 p(8) = 5.60962910452691703e17_dp
1381 p(9) = 3.3666225119686761e17_dp
1382 p(10) = 2.0218253197947866e17_dp
1383 p(11) = 1.21477756091902017e17_dp
1384 p(12) = 7.3012982513608503e16_dp
1385 p(13) = 4.38951893556421099e16_dp
1386 p(14) = 2.63949482512262325e16_dp
1387 p(15) = 1.58742054072786174e16_dp
1388 p(16) = 9.54806587737665531e15_dp
1389 p(17) = 5.74353712364571709e15_dp
1390 p(18) = 3.455214877389445e15_dp
1391 p(19) = 2.07871658520326804e15_dp
1392 p(20) = 1.25064667315629928e15_dp
1393 p(21) = 7.52469429541933745e14_dp
1394 p(22) = 4.5274603337253175e14_dp
1395 p(23) = 2.72414006900059548e14_dp
1396 p(24) = 1.63912168349216752e14_dp
1397 p(25) = 9.86275802590865738e13_dp
1398 p(26) = 5.93457701624974985e13_dp
1399 p(27) = 3.5709554322296296e13_dp
1400 p(28) = 2.14872890367310454e13_dp
1401 p(29) = 1.29294719957726902e13_dp
1402 p(30) = 7.78003375426361016e12_dp
1403 p(31) = 4.68148199759876704e12_dp
1404 p(32) = 2.8169955024829868e12_dp
1405 p(33) = 1.69507790481958464e12_dp
1406 p(34) = 1.01998486064607581e12_dp
1407 p(35) = 6.13759486539856459e11_dp
1408 p(36) = 3.69320183828682544e11_dp
1409 p(37) = 2.22232783898905102e11_dp
1410 p(38) = 1.33725247623668682e11_dp
1411 p(39) = 8.0467192739036288e10_dp
1412 p(40) = 4.84199582415144143e10_dp
1413 p(41) = 2.91360091170559564e10_dp
1414 p(42) = 1.75321747475309216e10_dp
1415 p(43) = 1.0549735552210995e10_dp
1416 p(44) = 6.34815321079006586e9_dp
1417 p(45) = 3.81991113733594231e9_dp
1418 p(46) = 2.29857747533101109e9_dp
1419 p(47) = 1.38313653595483694e9_dp
1420 p(48) = 8.32282908580025358e8_dp
1421 p(49) = 5.00814519374587467e8_dp
1422 p(50) = 3.01358090773319025e8_dp
1423 p(51) = 1.81337994217503535e8_dp
1424 p(52) = 1.09117589961086823e8_dp
1425 p(53) = 6.56599771718640323e7_dp
1426 p(54) = 3.95099693638497164e7_dp
1427 p(55) = 2.37745694710665991e7_dp
1428 p(56) = 1.43060135285912813e7_dp
1429 p(57) = 8.60844290313506695e6_dp
1430 p(58) = 5.18000974075383424e6_dp
1431 p(59) = 3.116998193057466e6_dp
1432 p(60) = 1.87560993870024029e6_dp
1433 p(61) = 1.12862197183979562e6_dp
1434 p(62) = 679132.441326077231_dp
1435 p(63) = 408658.421279877969_dp
1436 p(64) = 245904.473450669789_dp
1437 p(65) = 147969.568088321005_dp
1438 p(66) = 89038.612357311147_dp
1439 p(67) = 53577.7362552358895_dp
1440 p(68) = 32239.6513926914668_dp
1441 p(69) = 19399.7580852362791_dp
1442 p(70) = 11673.5323603058634_dp
1443 p(71) = 7024.38438577707758_dp
1444 p(72) = 4226.82479307685999_dp
1445 p(73) = 2543.43254175354295_dp
1446 p(74) = 1530.47486269122675_dp
1447 p(75) = 920.941785160749482_dp
1448 p(76) = 554.163803906291646_dp
1449 p(77) = 333.46029740785694_dp
1450 p(78) = 200.6550575335041_dp
1451 p(79) = 120.741366914147284_dp
1452 p(80) = 72.6544243200329916_dp
1453 p(81) = 43.7187810415471025_dp
1454 p(82) = 26.3071631447061043_dp
1455 p(83) = 15.8299486353816329_dp
1456 p(84) = 9.52493152341244004_dp
1457 p(85) = 5.72200417067776041_dp
1458 p(86) = 3.36242234070940928_dp
1459 p(87) = 1.75371394604499472_dp
1460 p(88) = 0.64705932650658966_dp
1461 p(89) = 0.072765905943708247_dp
1462 !
1463 w(1) = 47.67445484528304247e10_dp
1464 w(2) = 11.37485774750442175e9_dp
1465 w(3) = 78.64340976880190239e8_dp
1466 w(4) = 46.27335788759590498e8_dp
1467 w(5) = 24.7380464827152951e8_dp
1468 w(6) = 13.62904116438987719e8_dp
1469 w(7) = 92.79560029045882433e8_dp
1470 w(8) = 52.15931216254660251e8_dp
1471 w(9) = 31.67018011061666244e8_dp
1472 w(10) = 1.29291036801493046e8_dp
1473 w(11) = 1.00139319988015862e8_dp
1474 w(12) = 7.75892350510188341e7_dp
1475 w(13) = 6.01333567950731271e7_dp
1476 w(14) = 4.66141178654796875e7_dp
1477 w(15) = 3.61398903394911448e7_dp
1478 w(16) = 2.80225846672956389e7_dp
1479 w(17) = 2.1730509180930247e7_dp
1480 w(18) = 1.68524482625876965e7_dp
1481 w(19) = 1.30701489345870338e7_dp
1482 w(20) = 1.01371784832269282e7_dp
1483 w(21) = 7.86264116300379329e6_dp
1484 w(22) = 6.09861667912273717e6_dp
1485 w(23) = 4.73045784039455683e6_dp
1486 w(24) = 3.66928949951594161e6_dp
1487 w(25) = 2.8462050836230259e6_dp
1488 w(26) = 2.20777394798527011e6_dp
1489 w(27) = 1.71256191589205524e6_dp
1490 w(28) = 1.32843556197737076e6_dp
1491 w(29) = 1.0304731275955989e6_dp
1492 w(30) = 799345.206572271448_dp
1493 w(31) = 620059.354143595343_dp
1494 w(32) = 480986.704107449333_dp
1495 w(33) = 373107.167700228515_dp
1496 w(34) = 289424.08337412132_dp
1497 w(35) = 224510.248231581788_dp
1498 w(36) = 174155.825690028966_dp
1499 w(37) = 135095.256919654065_dp
1500 w(38) = 104795.442776800312_dp
1501 w(39) = 81291.4458222430418_dp
1502 w(40) = 63059.0493649328682_dp
1503 w(41) = 48915.9040455329689_dp
1504 w(42) = 37944.8484018048756_dp
1505 w(43) = 29434.4290473253969_dp
1506 w(44) = 22832.7622054490044_dp
1507 w(45) = 17711.743950151233_dp
1508 w(46) = 13739.287867104177_dp
1509 w(47) = 10657.7895710752585_dp
1510 w(48) = 8267.42141053961834_dp
1511 w(49) = 6413.17397520136448_dp
1512 w(50) = 4974.80402838654277_dp
1513 w(51) = 3859.03698188553047_dp
1514 w(52) = 2993.51824493299154_dp
1515 w(53) = 2322.1211966811754_dp
1516 w(54) = 1801.30750964719641_dp
1517 w(55) = 1397.30379659817038_dp
1518 w(56) = 1083.91149143250697_dp
1519 w(57) = 840.807939169209188_dp
1520 w(58) = 652.228524366749422_dp
1521 w(59) = 505.944376983506128_dp
1522 w(60) = 392.469362317941064_dp
1523 w(61) = 304.444930257324312_dp
1524 w(62) = 236.162932842453601_dp
1525 w(63) = 183.195466078603525_dp
1526 w(64) = 142.107732186551471_dp
1527 w(65) = 110.23530215723992_dp
1528 w(66) = 85.5113346705382257_dp
1529 w(67) = 66.3325469806696621_dp
1530 w(68) = 51.4552463353841373_dp
1531 w(69) = 39.9146798429449273_dp
1532 w(70) = 30.9624728409162095_dp
1533 w(71) = 24.018098812215013_dp
1534 w(72) = 18.6312338024296588_dp
1535 w(73) = 14.4525541233150501_dp
1536 w(74) = 11.2110836519105938_dp
1537 w(75) = 8.69662175848497178_dp
1538 w(76) = 6.74611236165731961_dp
1539 w(77) = 5.23307018057529994_dp
1540 w(78) = 4.05937850501539556_dp
1541 w(79) = 3.14892659076635714_dp
1542 w(80) = 2.44267408211071604_dp
1543 w(81) = 1.89482240522855261_dp
1544 w(82) = 1.46984505907050079_dp
1545 w(83) = 1.14019261330527007_dp
1546 w(84) = 0.884791217422925293_dp
1547 w(85) = 0.692686387080616483_dp
1548 w(86) = 0.585244576897023282_dp
1549 w(87) = 0.576182522545327589_dp
1550 w(88) = 0.596688817388997178_dp
1551 w(89) = 0.607879901151108771_dp
1552 !
1553 !
1554 urange = 1._dp
1555 drange = 1e-08_dp
1556 acc = 1e-08_dp
1557 END SUBROUTINE gequad
1558
1559END MODULE ps_wavelet_kernel
static int modulo(int a, int m)
Equivalent of Fortran's MODULO which always returns a positive number. https://gcc....
Definition fft_utils.h:60
for(int lxp=0;lxp<=lp;lxp++)
Fortran API for the FFT package, which is written in C.
Definition fft_api.F:12
subroutine, public fft_destroy_plan(plan)
...
Definition fft_tools.F:757
subroutine, public fft_1d(plan, zin, zout, scale)
...
Definition fft_tools.F:771
subroutine, public fft_create_plan_1d(plan, fsign, trans_in, trans_out, ldx_in, ldx_out, n, m, zin, zout)
...
Definition fft_tools.F:575
logical, parameter, public bwfft
Definition fft_tools.F:155
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public sp
Definition kinds.F:33
Definition of mathematical constants and functions.
real(kind=dp), parameter, public pi
Interface to the message passing library MPI.
Creates the wavelet kernel for the wavelet based poisson solver.
subroutine, public scramble_unpack(i1, j2, lot, nfft, n1, n3, md2, nproc, nd3, zw, zmpi2, cosinarr)
(Based on suitable modifications of S.Goedecker routines) Assign the correct planes to the work array...
Creates the wavelet kernel for the wavelet based poisson solver.
subroutine, public createkernel(geocode, n01, n02, n03, hx, hy, hz, itype_scf, iproc, nproc, kernel, mpi_group)
Allocate a pointer which corresponds to the zero-padded FFT slice needed for calculating the convolut...
Creates the wavelet kernel for the wavelet based poisson solver.
subroutine, public scaling_function(itype, nd, nrange, a, x)
Calculate the values of a scaling function in real uniform grid.
pure subroutine, public scf_recursion(itype, n_iter, n_range, kernel_scf, kern_1_scf)
Do iterations to go from p0gauss to pgauss order interpolating scaling function.
Performs a wavelet based solution of the Poisson equation.
subroutine, public s_fft_dimensions(n01, n02, n03, m1, m2, m3, n1, n2, n3, md1, md2, md3, nd1, nd2, nd3, nproc)
Calculate four sets of dimension needed for the calculation of the convolution for the surface system...
subroutine, public f_fft_dimensions(n01, n02, n03, m1, m2, m3, n1, n2, n3, md1, md2, md3, nd1, nd2, nd3, nproc)
Calculate four sets of dimension needed for the calculation of the zero-padded convolution.