Octopus
poisson_isf.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2011 M. Marques, A. Castro, X. Andrade, J. Alberdi-Rodriguez, M. Oliveira
2!! Copyright (C) Luigi Genovese, Thierry Deutsch, CEA Grenoble, 2006
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
23 use, intrinsic :: iso_fortran_env
25 use cube_oct_m
26 use debug_oct_m
27 use global_oct_m
28 use io_oct_m
30 use mesh_oct_m
31 use mpi_oct_m
33 use parser_oct_m
36 use sgfft_oct_m
38
39 implicit none
40
41 private
42
43 public :: &
45 isf_cnf_t, &
49
50 ! Indices for the cnf array
51 integer, parameter :: SERIAL = 1
52 integer, parameter :: WORLD = 2
53 integer, parameter :: DOMAIN = 3
54 integer, parameter :: N_CNF = 3
55
56 ! Datatype to store kernel values to solve Poisson equation
57 ! on different communicators (configurations).
58 type isf_cnf_t
59 private
60 real(real64), allocatable :: kernel(:, :, :)
61 integer :: nfft1, nfft2, nfft3
62 type(mpi_grp_t) :: mpi_grp
63 logical :: all_nodes
64 end type isf_cnf_t
65
66 type poisson_isf_t
67 private
68 type(MPI_Comm) :: all_nodes_comm
69 type(isf_cnf_t) :: cnf(1:N_CNF)
70 end type poisson_isf_t
71
72 integer, parameter :: order_scaling_function = 8
73
74
75contains
76
77 ! ---------------------------------------------------------
78 subroutine poisson_isf_init(this, namespace, mesh, cube, all_nodes_comm, init_world)
79 type(poisson_isf_t), intent(out) :: this
80 type(namespace_t), target, intent(in) :: namespace
81 type(mesh_t), intent(in) :: mesh
82 type(cube_t), intent(inout) :: cube
83 type(MPI_Comm), intent(in) :: all_nodes_comm
84 logical, optional, intent(in) :: init_world
85
86 integer :: n1, n2, n3
87 integer :: i_cnf
88 integer :: m1, m2, m3, md1, md2, md3
89 integer :: n(3)
90 logical :: init_world_
91 integer :: default_nodes
92 integer :: ii
93 integer, allocatable :: ranks(:)
94 !data ranks /0, 1/
95 integer :: world_size
96 integer :: nodes
97#ifdef HAVE_MPI
98 integer :: ierr
99 type(MPI_Group) :: world_grp, poisson_grp
100#endif
101
102 push_sub(poisson_isf_init)
103
104 init_world_ = .true.
105 if (present(init_world)) init_world_ = init_world
106
107 if (.not. mesh%parallel_in_domains) then
108 ! The serial version is always needed (as used, e.g., in the casida runmode)
109 call calculate_dimensions(cube%rs_n_global(1), cube%rs_n_global(2), cube%rs_n_global(3), &
110 this%cnf(serial)%nfft1, this%cnf(serial)%nfft2, this%cnf(serial)%nfft3)
111
112 n1 = this%cnf(serial)%nfft1/2 + 1
113 n2 = this%cnf(serial)%nfft2/2 + 1
114 n3 = this%cnf(serial)%nfft3/2 + 1
115
116 safe_allocate(this%cnf(serial)%kernel(1:n1, 1:n2, 1:n3))
117
118 call build_kernel(cube%rs_n_global(1), cube%rs_n_global(2), cube%rs_n_global(3), &
119 this%cnf(serial)%nfft1, this%cnf(serial)%nfft2, this%cnf(serial)%nfft3, &
120 real(cube%spacing(1), real64), order_scaling_function, this%cnf(SERIAL)%kernel)
121 end if
122
123#if !defined(HAVE_MPI)
125 return
126#endif
127
128 ! Allocate to configurations. The initialisation, especially the kernel,
129 ! depends on the number of nodes used for the calculations. To avoid
130 ! recalculating the kernel on each call of poisson_isf_solve depending on
131 ! the all_nodes argument, both kernels are calculated.
132 this%cnf(domain)%mpi_grp = mesh%mpi_grp
134 ! For the world configuration we build a new communicator
136 default_nodes = 0 !All nodes
137
138 !%Variable PoissonSolverNodes
139 !%Type integer
140 !%Section Hamiltonian::Poisson
141 !%Default 0
142 !%Description
143 !% How many nodes to use to solve the Poisson equation. A value of
144 !% 0, the default, implies that all available nodes are used.
145 !%End
146 call parse_variable(namespace, 'PoissonSolverNodes', default_nodes, nodes)
147
148 this%all_nodes_comm = all_nodes_comm
149
150#if defined(HAVE_MPI)
151 call mpi_comm_size(all_nodes_comm, world_size)
152#endif
153
154 if (nodes <= 0 .or. nodes > world_size) nodes = world_size
155 this%cnf(world)%all_nodes = (nodes == world_size)
156
157 safe_allocate(ranks(1:nodes))
158
159 do ii = 1, nodes
160 ranks(ii) = ii - 1
161 end do
162
163 !create a new communicator
164 !Extract the original group handle and create new comm.
165 ! NOTE: all of world_size, poisson_grp and the created communicator must be derived from
166 ! all_nodes_comm (the communicator of the poisson group, e.g. mc%master_comm), NOT from mpi_world.
167#if defined(HAVE_MPI)
168 call mpi_comm_group(all_nodes_comm, world_grp, ierr)
169 call mpi_group_incl(world_grp, nodes, ranks, poisson_grp, ierr)
170 call mpi_comm_create(all_nodes_comm, poisson_grp, this%cnf(world)%mpi_grp%comm, ierr)
171#endif
172
173 safe_deallocate_a(ranks)
174
175 !Fill the new data structure, for all nodes
176#if defined(HAVE_MPI)
177 if (this%cnf(world)%mpi_grp%comm /= mpi_comm_null) then
178 call mpi_comm_rank(this%cnf(world)%mpi_grp%comm, this%cnf(world)%mpi_grp%rank, ierr)
179 call mpi_comm_size(this%cnf(world)%mpi_grp%comm, this%cnf(world)%mpi_grp%size, ierr)
180 else
181 this%cnf(world)%mpi_grp%rank = -1
182 this%cnf(world)%mpi_grp%size = -1
183 end if
184#endif
185
186 ! Build the kernel for all configurations. At the moment, this is
187 ! solving the poisson equation with all nodes (i_cnf == WORLD) and
188 ! with the domain nodes only (i_cnf == DOMAIN).
189 do i_cnf = 2, n_cnf
190 if ((i_cnf == world .and. .not. init_world_) & ! world is disabled
191 .or. (i_cnf == domain .and. .not. mesh%parallel_in_domains) & ! not parallel in domains
192 ) then
193 cycle
194 end if
195 if (this%cnf(i_cnf)%mpi_grp%rank /= -1 .or. i_cnf /= world) then
196 call par_calculate_dimensions(cube%rs_n_global(1), cube%rs_n_global(2), cube%rs_n_global(3), &
197 m1, m2, m3, n1, n2, n3, md1, md2, md3, this%cnf(i_cnf)%nfft1, this%cnf(i_cnf)%nfft2, &
198 this%cnf(i_cnf)%nfft3, this%cnf(i_cnf)%mpi_grp%size)
199
200 ! Shortcuts to avoid to "line too long" errors.
201 n(1) = this%cnf(i_cnf)%nfft1
202 n(2) = this%cnf(i_cnf)%nfft2
203 n(3) = this%cnf(i_cnf)%nfft3
204
205 safe_allocate(this%cnf(i_cnf)%kernel(1:n(1), 1:n(2), 1:n(3)/this%cnf(i_cnf)%mpi_grp%size))
206
207 call par_build_kernel(cube%rs_n_global(1), cube%rs_n_global(2), cube%rs_n_global(3), n1, n2, n3, &
208 this%cnf(i_cnf)%nfft1, this%cnf(i_cnf)%nfft2, this%cnf(i_cnf)%nfft3, &
209 cube%spacing(1), order_scaling_function, &
210 this%cnf(i_cnf)%mpi_grp%rank, this%cnf(i_cnf)%mpi_grp%size, this%cnf(i_cnf)%mpi_grp%comm, &
211 this%cnf(i_cnf)%kernel)
212 else
213 cycle
214 end if
215 end do
216
217 pop_sub(poisson_isf_init)
218 end subroutine poisson_isf_init
219
220 ! ---------------------------------------------------------
221 subroutine poisson_isf_solve(this, mesh, cube, pot, rho, all_nodes, sm)
222 type(poisson_isf_t), intent(in) :: this
223 type(mesh_t), intent(in) :: mesh
224 type(cube_t), intent(in) :: cube
225 real(real64), contiguous, intent(out) :: pot(:)
226 real(real64), contiguous, intent(in) :: rho(:)
227 logical, intent(in) :: all_nodes
228 type(submesh_t), optional, intent(in) :: sm
229
230 integer :: i_cnf, nn(1:3)
231 type(cube_function_t) :: rho_cf
232#if defined(HAVE_MPI)
233 integer(int64) :: number_points
234#endif
235
236 push_sub(poisson_isf_solve)
237
238 call dcube_function_alloc_rs(cube, rho_cf)
239
240 if (present(sm)) then
241 call dsubmesh_to_cube(sm, rho, cube, rho_cf)
242 else
243 call dmesh_to_cube(mesh, rho, cube, rho_cf)
244 end if
245
246 ! Choose configuration.
247 i_cnf = serial
248
249#if defined(HAVE_MPI)
250 if (all_nodes) then
251 i_cnf = world
252 else if (mesh%parallel_in_domains) then
253 i_cnf = domain
254 end if
255#endif
256
257#if !defined(HAVE_MPI)
258 assert(i_cnf == serial)
259#endif
260
261 if (i_cnf == serial) then
262
263 nn(1) = this%cnf(serial)%nfft1
264 nn(2) = this%cnf(serial)%nfft2
265 nn(3) = this%cnf(serial)%nfft3
266 call psolver_kernel(cube%rs_n_global(1), cube%rs_n_global(2), cube%rs_n_global(3), &
267 nn(1), nn(2), nn(3), &
268 real(mesh%spacing(1), real64), this%cnf(serial)%kernel, rho_cf%drs)
269
270 else
271 nn(1) = this%cnf(i_cnf)%nfft1
272 nn(2) = this%cnf(i_cnf)%nfft2
273 nn(3) = this%cnf(i_cnf)%nfft3
274 if (this%cnf(i_cnf)%mpi_grp%size /= -1 .or. i_cnf /= world) then
275 call par_psolver_kernel(cube%rs_n_global(1), cube%rs_n_global(2), cube%rs_n_global(3), &
276 nn(1), nn(2), nn(3), &
277 real(mesh%spacing(1), real64), this%cnf(i_cnf)%kernel, rho_cf%drs, &
278 this%cnf(i_cnf)%mpi_grp%rank, this%cnf(i_cnf)%mpi_grp%size, this%cnf(i_cnf)%mpi_grp%comm)
279 end if
280 ! we need to be sure that the root of every domain-partition has a copy of the potential
281 ! for the moment we broadcast to all nodes, but this is more than what we really need
282 if (i_cnf == world .and. .not. this%cnf(world)%all_nodes) then
283#if defined(HAVE_MPI)
284 ! make sure we do not run into integer overflow here
285 number_points = cube%rs_n_global(1) * cube%rs_n_global(2)
286 number_points = number_points * cube%rs_n_global(3)
287 if (number_points >= huge(0)) then
288 message(1) = "Error: too many points for the normal cube. Please try to use a distributed FFT."
289 call messages_fatal(1)
290 end if
291 call mpi_bcast(rho_cf%drs(1, 1, 1, 1), cube%rs_n_global(1)*cube%rs_n_global(2)*cube%rs_n_global(3), &
292 mpi_double_precision, 0, this%all_nodes_comm)
293#endif
294 end if
295 end if
296
297 if (present(sm)) then
298 call dcube_to_submesh(cube, rho_cf, sm, pot)
299 else
300 call dcube_to_mesh(cube, rho_cf, mesh, pot)
301 end if
302
303 call dcube_function_free_rs(cube, rho_cf)
304
305 pop_sub(poisson_isf_solve)
306 end subroutine poisson_isf_solve
307
308 ! ---------------------------------------------------------
309 subroutine poisson_isf_end(this)
310 type(poisson_isf_t), intent(inout) :: this
311
312#if defined(HAVE_MPI)
313 integer :: i_cnf
314#endif
315
316 push_sub(poisson_isf_end)
317
318#if defined(HAVE_MPI)
319 do i_cnf = 1, n_cnf
320 safe_deallocate_a(this%cnf(i_cnf)%kernel)
321 end do
322 if (this%cnf(world)%mpi_grp%comm /= mpi_comm_null) then
323 call mpi_comm_free(this%cnf(world)%mpi_grp%comm)
324 end if
325#else
326 safe_deallocate_a(this%cnf(serial)%kernel)
327#endif
328
329 pop_sub(poisson_isf_end)
330 end subroutine poisson_isf_end
331
332 ! --------------------------------------------------------------
333
334 !!****h* BigDFT/psolver_kernel
335 !! NAME
336 !! psolver_kernel
337 !!
338 !! FUNCTION
339 !! Solver of Poisson equation applying a kernel
340 !!
341 !! SYNOPSIS
342 !! Poisson solver applying a kernel and
343 !! using Fourier transform for the convolution.
344 !! rhopot : input -> the density
345 !! output -> the Hartree potential + pot_ion
346 !! The potential pot_ion is ADDED in the array rhopot.
347 !! Calculate also the Hartree potential
348 !!
349 !! Replaces the charge density contained in rhopot
350 !! by the Hartree stored as well in rhopot.
351 !! If xc_on is true, it also adds the XC potential and
352 !! ionic potential pot_ion
353 !!
354 !! We double the size of the mesh except in one dimension
355 !! in order to use the property of the density to be real.
356 !! WARNING
357 !! For the use of FFT routine
358 !! inzee=1: first part of Z is data (output) array,
359 !! second part work array
360 !! inzee=2: first part of Z is work array, second part data array
361 !! real(F(i1,i2,i3))=Z(1,i1,i2,i3,inzee)
362 !! imag(F(i1,i2,i3))=Z(2,i1,i2,i3,inzee)
363 !! inzee on output is in general different from inzee on input
364 !!
365 !! AUTHOR
366 !! Thierry Deutsch, Luigi Genovese
367 !! COPYRIGHT
368 !! Copyright (C) 2005 CEA
369 !! CREATION DATE
370 !! 13/07/2005
371 !!
372 !! MODIFICATION HISTORY
373 !! 12/2005 Kernel stored into memory
374 !! 12/2005 Real Kernel FFT and use less memory
375 !!
376 !! SOURCE
377 !!
378 subroutine psolver_kernel(n01, n02, n03, nfft1, nfft2, nfft3, hgrid, karray, rhopot)
379 integer, intent(in) :: n01
380 integer, intent(in) :: n02
381 integer, intent(in) :: n03
382 integer, intent(in) :: nfft1
383 integer, intent(in) :: nfft2
384 integer, intent(in) :: nfft3
385 real(real64), intent(in) :: hgrid
386 real(real64), intent(in) :: karray(nfft1/2 + 1,nfft2/2 + 1, nfft3/2 + 1)
387 real(real64), intent(inout) :: rhopot(n01, n02, n03)
388
389 real(real64), allocatable :: zarray(:,:,:)
390 real(real64) :: factor
391 integer :: n1, n2, n3, nd1, nd2, nd3, n1h, nd1h
392 integer :: inzee, i_sign
393
394 push_sub(psolver_kernel)
395
396 !Dimension of the FFT
397 call calculate_dimensions(n01, n02, n03, n1, n2, n3)
398
399 !Half size of nd1
400 n1h=n1/2
401 nd1 = n1 + modulo(n1+1,2)
402 nd2 = n2 + modulo(n2+1,2)
403 nd3 = n3 + modulo(n3+1,2)
404 nd1h=(nd1+1)/2
405
406 safe_allocate(zarray(1:2, 1:nd1h*nd2*nd3, 1:2))
407 ! The FFT reads the padding of the work slab, which zarray_in does not set
408 zarray(:, :, 2) = m_zero
409
410 !Set zarray
411 call zarray_in(n01,n02,n03,nd1h,nd2,nd3,rhopot,zarray)
412
413 !FFT
414 !print *,"Do a 3D HalFFT for the density"
415 i_sign=1
416 inzee=1
417 call fft(n1h,n2,n3,nd1h,nd2,nd3,zarray,i_sign,inzee)
418
419 !print *, "Apply the kernel"
420 call kernel_application(n1,n2,n3,nd1h,nd2,nd3,nfft1,nfft2,nfft3,zarray,karray,inzee)
421
422 !Inverse FFT
423 i_sign=-1
424 !print *,"Do a 3D inverse HalFFT"
425 call fft(n1h,n2,n3,nd1h,nd2,nd3,zarray,i_sign,inzee)
426
427 !Recollect the result
428 !We have to multiply by a factor
429 factor = hgrid**3/(real(n1*n2, real64)*real(n3, real64))
430
431 ! Calling this routine gives only the Hartree potential
432 call zarray_out(n01, n02, n03, nd1h, nd2, nd3, rhopot, zarray(1, 1, inzee), factor)
433
434 safe_deallocate_a(zarray)
435 pop_sub(psolver_kernel)
436 end subroutine psolver_kernel
437 !!***
438
439 !!****h* BigDFT/kernel_application
440 !! NAME
441 !! kernel_application
442 !!
443 !! FUNCTION
444 !! Multiply the FFT of the density by the FFT of the kernel
445 !!
446 !! SYNOPSIS
447 !! zarray(:,:,:,:,inzee) : IN -> FFT of the density with the x dimension divided by two
448 !! (HalFFT), OUT -> FFT of the potential
449 !! karray : kernel FFT (real, 1/8 of the total grid)
450 !! n1h,n2,n3 : dimension of the FFT grid for zarray
451 !! nd1h,nd2,nd3 : dimensions of zarray
452 !! nfft1,nfft2,nfft3 : original FFT grid dimensions, to be used for karray dimensions
453 !!
454 !! WARNING
455 !! We use all the zarray vector, storing the auxiliary part using ouzee=3-inzee
456 !! All the loop are unrolled such to avoid different conditions
457 !! the "min" functions are substituted by kink computed with absolute values
458 !! AUTHOR
459 !! Luigi Genovese
460 !! CREATION DATE
461 !! March 2006
462 !!
463 !! SOURCE
464 !!
465 subroutine kernel_application(n1,n2,n3,nd1h,nd2,nd3,nfft1,nfft2,nfft3,zarray,karray,inzee)
466 integer, intent(in) :: n1,n2,n3,nd1h,nd2,nd3,nfft1,nfft2,nfft3,inzee
467 real(real64), intent(in) :: karray(1:nfft1/2 + 1, 1:nfft2/2 + 1, 1:nfft3/2 + 1)
468 real(real64), intent(inout) :: zarray(1:2, 1:nd1h, 1:nd2, 1:nd3, 1:2)
469
470 real(real64), dimension(:), allocatable :: cos_array,sin_array
471 real(real64) :: a,b,c,d,pi2,g1,cp,sp
472 real(real64) :: rfe,ife,rfo,ifo,rk,ik,rk2,ik2,re,ro,ie,io,rhk,ihk
473 integer :: i1,i2,i3,j1,j2,j3, ouzee,n1h,n2h,n3h
474 integer :: si1,si2,si3
475
476 push_sub(kernel_application)
477
478 n1h = n1/2
479 n2h = n2/2
480 n3h = n3/2
481
482 safe_allocate(cos_array(1:n1h + 1))
483 safe_allocate(sin_array(1:n1h + 1))
484
485 pi2 = 8._real64*datan(1._real64)
486 pi2 = pi2/real(n1, real64)
487 do i1 = 1,n1h+1
488 cos_array(i1) = dcos(pi2*(i1-1))
489 sin_array(i1) = -dsin(pi2*(i1-1))
490 end do
491
492 ouzee = 3-inzee
493
494 !--------------------------------------------!
495 !--- Starting reconstruction half -> full ---!
496 !--------------------------------------------!
497
498 !-------------Case i3 = 1
499 i3 = 1
500 j3 = 1
501 si3 = 1
502
503 !-------------Case i2 = 1, i3 = 1
504 i2 = 1
505 j2 = 1
506 si2 = 1
507
508 !Case i1 == 1
509 i1 = 1
510 si1 = 1
511 a = zarray(1,i1,i2,i3,inzee)
512 b = zarray(2,i1,i2,i3,inzee)
513 c = zarray(1,si1,si2,si3,inzee)
514 d = zarray(2,si1,si2,si3,inzee)
515 rfe = .5_real64*(a+c)
516 ife = .5_real64*(b-d)
517 rfo = .5_real64*(a-c)
518 ifo = .5_real64*(b+d)
519 cp = cos_array(i1)
520 sp = sin_array(i1)
521 rk = rfe+cp*ifo-sp*rfo
522 ik = ife-cp*rfo-sp*ifo
523 g1 = karray(i1,j2,j3)
524 rk2 = rk*g1
525 ik2 = ik*g1
526
527 zarray(1,1,i2,i3,ouzee) = rk2
528 zarray(2,1,i2,i3,ouzee) = ik2
529
530 !Case i1=2,n1h
531 do i1=2,n1h
532 si1=n1h+2-i1
533
534 a=zarray(1,i1,i2,i3,inzee)
535 b=zarray(2,i1,i2,i3,inzee)
536 c=zarray(1,si1,si2,si3,inzee)
537 d=zarray(2,si1,si2,si3,inzee)
538 rfe=.5_real64*(a+c)
539 ife=.5_real64*(b-d)
540 rfo=.5_real64*(a-c)
541 ifo=.5_real64*(b+d)
542 cp=cos_array(i1)
543 sp=sin_array(i1)
544 rk=rfe+cp*ifo-sp*rfo
545 ik=ife-cp*rfo-sp*ifo
546 g1=karray(i1,j2,j3)
547 rk2=rk*g1
548 ik2=ik*g1
549
550 zarray(1,i1,i2,i3,ouzee) = rk2
551 zarray(2,i1,i2,i3,ouzee) = ik2
552 end do
553
554 !Case i1=n1h+1
555 i1=n1h+1
556 si1=n1h+2-i1
557
558 a=zarray(1,1,i2,i3,inzee) !beware here i1 -> 1
559 b=zarray(2,1,i2,i3,inzee) !beware here i1 -> 1
560 c=zarray(1,si1,si2,si3,inzee)
561 d=zarray(2,si1,si2,si3,inzee)
562 rfe=.5_real64*(a+c)
563 ife=.5_real64*(b-d)
564 rfo=.5_real64*(a-c)
565 ifo=.5_real64*(b+d)
566 cp=cos_array(i1)
567 sp=sin_array(i1)
568 rk=rfe+cp*ifo-sp*rfo
569 ik=ife-cp*rfo-sp*ifo
570 g1=karray(i1,j2,j3)
571 rk2=rk*g1
572 ik2=ik*g1
573
574 zarray(1,i1,i2,i3,ouzee) = rk2
575 zarray(2,i1,i2,i3,ouzee) = ik2
576 !-------------END case i2 = 1 , i3=1
577
578 !case i2 >=2
579 do i2 = 2, n2
580 j2=n2h+1-abs(n2h+1-i2)
581 si2=n2+2-i2 !if i2 /=1, otherwise si2=1
582
583 !Case i1 == 1
584 i1=1
585 si1=1
586 a=zarray(1,i1,i2,i3,inzee)
587 b=zarray(2,i1,i2,i3,inzee)
588 c=zarray(1,si1,si2,si3,inzee)
589 d=zarray(2,si1,si2,si3,inzee)
590 rfe=.5_real64*(a+c)
591 ife=.5_real64*(b-d)
592 rfo=.5_real64*(a-c)
593 ifo=.5_real64*(b+d)
594 cp=cos_array(i1)
595 sp=sin_array(i1)
596 rk=rfe+cp*ifo-sp*rfo
597 ik=ife-cp*rfo-sp*ifo
598 g1=karray(i1,j2,j3)
599 rk2=rk*g1
600 ik2=ik*g1
601
602 zarray(1,1,i2,i3,ouzee) = rk2
603 zarray(2,1,i2,i3,ouzee) = ik2
604
605 !Case i1=2,n1h
606 do i1=2,n1h
607 si1=n1h+2-i1
608
609 a=zarray(1,i1,i2,i3,inzee)
610 b=zarray(2,i1,i2,i3,inzee)
611 c=zarray(1,si1,si2,si3,inzee)
612 d=zarray(2,si1,si2,si3,inzee)
613 rfe=.5_real64*(a+c)
614 ife=.5_real64*(b-d)
615 rfo=.5_real64*(a-c)
616 ifo=.5_real64*(b+d)
617 cp=cos_array(i1)
618 sp=sin_array(i1)
619 rk=rfe+cp*ifo-sp*rfo
620 ik=ife-cp*rfo-sp*ifo
621 g1=karray(i1,j2,j3)
622 rk2=rk*g1
623 ik2=ik*g1
624
625 zarray(1,i1,i2,i3,ouzee) = rk2
626 zarray(2,i1,i2,i3,ouzee) = ik2
627 end do
628
629 !Case i1=n1h+1
630 i1=n1h+1
631 si1=n1h+2-i1
632
633 a=zarray(1,1,i2,i3,inzee) !beware here i1 -> 1
634 b=zarray(2,1,i2,i3,inzee) !beware here i1 -> 1
635 c=zarray(1,si1,si2,si3,inzee)
636 d=zarray(2,si1,si2,si3,inzee)
637 rfe=.5_real64*(a+c)
638 ife=.5_real64*(b-d)
639 rfo=.5_real64*(a-c)
640 ifo=.5_real64*(b+d)
641 cp=cos_array(i1)
642 sp=sin_array(i1)
643 rk=rfe+cp*ifo-sp*rfo
644 ik=ife-cp*rfo-sp*ifo
645 g1=karray(i1,j2,j3)
646 rk2=rk*g1
647 ik2=ik*g1
648
649 zarray(1,i1,i2,i3,ouzee) = rk2
650 zarray(2,i1,i2,i3,ouzee) = ik2
651 end do
652 !-------------END Case i3 = 1
653
654 !case i3 >=2
655 do i3=2,n3
656 j3=n3h+1-abs(n3h+1-i3)
657 si3=n3+2-i3 !if i3 /=1, otherwise si3=1
658
659 !-------------Case i2 = 1
660 i2=1
661 j2=1
662 si2=1
663
664 !Case i1 == 1
665 i1=1
666 si1=1
667 a=zarray(1,i1,i2,i3,inzee)
668 b=zarray(2,i1,i2,i3,inzee)
669 c=zarray(1,si1,si2,si3,inzee)
670 d=zarray(2,si1,si2,si3,inzee)
671 rfe=.5_real64*(a+c)
672 ife=.5_real64*(b-d)
673 rfo=.5_real64*(a-c)
674 ifo=.5_real64*(b+d)
675 cp=cos_array(i1)
676 sp=sin_array(i1)
677 rk=rfe+cp*ifo-sp*rfo
678 ik=ife-cp*rfo-sp*ifo
679 g1=karray(i1,j2,j3)
680 rk2=rk*g1
681 ik2=ik*g1
682
683 zarray(1,1,i2,i3,ouzee) = rk2
684 zarray(2,1,i2,i3,ouzee) = ik2
685
686 !Case i1=2,n1h
687 do i1=2,n1h
688 si1=n1h+2-i1
689
690 a=zarray(1,i1,i2,i3,inzee)
691 b=zarray(2,i1,i2,i3,inzee)
692 c=zarray(1,si1,si2,si3,inzee)
693 d=zarray(2,si1,si2,si3,inzee)
694 rfe=.5_real64*(a+c)
695 ife=.5_real64*(b-d)
696 rfo=.5_real64*(a-c)
697 ifo=.5_real64*(b+d)
698 cp=cos_array(i1)
699 sp=sin_array(i1)
700 rk=rfe+cp*ifo-sp*rfo
701 ik=ife-cp*rfo-sp*ifo
702 g1=karray(i1,j2,j3)
703 rk2=rk*g1
704 ik2=ik*g1
705
706 zarray(1,i1,i2,i3,ouzee) = rk2
707 zarray(2,i1,i2,i3,ouzee) = ik2
708 end do
709
710 !Case i1=n1h+1
711 i1=n1h+1
712 si1=n1h+2-i1
713
714 a=zarray(1,1,i2,i3,inzee) !beware here i1 -> 1
715 b=zarray(2,1,i2,i3,inzee) !beware here i1 -> 1
716 c=zarray(1,si1,si2,si3,inzee)
717 d=zarray(2,si1,si2,si3,inzee)
718 rfe=.5_real64*(a+c)
719 ife=.5_real64*(b-d)
720 rfo=.5_real64*(a-c)
721 ifo=.5_real64*(b+d)
722 cp=cos_array(i1)
723 sp=sin_array(i1)
724 rk=rfe+cp*ifo-sp*rfo
725 ik=ife-cp*rfo-sp*ifo
726 g1=karray(i1,j2,j3)
727 rk2=rk*g1
728 ik2=ik*g1
729
730 zarray(1,i1,i2,i3,ouzee) = rk2
731 zarray(2,i1,i2,i3,ouzee) = ik2
732 !-------------END case i2 = 1
733
734 !case i2 >=2
735 do i2=2,n2
736 j2=n2h+1-abs(n2h+1-i2)
737 si2=n2+2-i2 !if i2 /=1, otherwise si2=1
738
739 !Case i1 == 1
740 i1=1
741 si1=1
742 a=zarray(1,i1,i2,i3,inzee)
743 b=zarray(2,i1,i2,i3,inzee)
744 c=zarray(1,si1,si2,si3,inzee)
745 d=zarray(2,si1,si2,si3,inzee)
746 rfe=.5_real64*(a+c)
747 ife=.5_real64*(b-d)
748 rfo=.5_real64*(a-c)
749 ifo=.5_real64*(b+d)
750 cp=cos_array(i1)
751 sp=sin_array(i1)
752 rk=rfe+cp*ifo-sp*rfo
753 ik=ife-cp*rfo-sp*ifo
754 g1=karray(i1,j2,j3)
755 rk2=rk*g1
756 ik2=ik*g1
757
758 zarray(1,1,i2,i3,ouzee) = rk2
759 zarray(2,1,i2,i3,ouzee) = ik2
760
761 !Case i1=2,n1h
762 do i1=2,n1h
763 si1=n1h+2-i1
764
765 a=zarray(1,i1,i2,i3,inzee)
766 b=zarray(2,i1,i2,i3,inzee)
767 c=zarray(1,si1,si2,si3,inzee)
768 d=zarray(2,si1,si2,si3,inzee)
769 rfe=.5_real64*(a+c)
770 ife=.5_real64*(b-d)
771 rfo=.5_real64*(a-c)
772 ifo=.5_real64*(b+d)
773 cp=cos_array(i1)
774 sp=sin_array(i1)
775 rk=rfe+cp*ifo-sp*rfo
776 ik=ife-cp*rfo-sp*ifo
777 g1=karray(i1,j2,j3)
778 rk2=rk*g1
779 ik2=ik*g1
780
781 zarray(1,i1,i2,i3,ouzee) = rk2
782 zarray(2,i1,i2,i3,ouzee) = ik2
783 end do
784
785 !Case i1=n1h+1
786 i1=n1h+1
787 si1=n1h+2-i1
788
789 a=zarray(1,1,i2,i3,inzee) !beware here i1 -> 1
790 b=zarray(2,1,i2,i3,inzee) !beware here i1 -> 1
791 c=zarray(1,si1,si2,si3,inzee)
792 d=zarray(2,si1,si2,si3,inzee)
793 rfe=.5_real64*(a+c)
794 ife=.5_real64*(b-d)
795 rfo=.5_real64*(a-c)
796 ifo=.5_real64*(b+d)
797 cp=cos_array(i1)
798 sp=sin_array(i1)
799 rk=rfe+cp*ifo-sp*rfo
800 ik=ife-cp*rfo-sp*ifo
801 g1=karray(i1,j2,j3)
802 rk2=rk*g1
803 ik2=ik*g1
804
805 zarray(1,i1,i2,i3,ouzee) = rk2
806 zarray(2,i1,i2,i3,ouzee) = ik2
807 end do
808
809 end do
810
811
812 !--------------------------------------------!
813 !--- Starting reconstruction full -> half ---!
814 !--------------------------------------------!
815
816 !case i3=1
817 i3=1
818 j3=1
819 !case i2=1
820 i2=1
821 j2=1
822 do i1 = 1,n1h
823 j1=n1h+2-i1
824
825 a=zarray(1,i1,i2,i3,ouzee)
826 b=zarray(2,i1,i2,i3,ouzee)
827 c=zarray(1,j1,j2,j3,ouzee)
828 d=-zarray(2,j1,j2,j3,ouzee)
829 cp=cos_array(i1)
830 sp=sin_array(i1)
831 re=(a+c)
832 ie=(b+d)
833 ro=(a-c)*cp-(b-d)*sp
834 io=(a-c)*sp+(b-d)*cp
835 rhk=re-io
836 ihk=ie+ro
837
838 zarray(1,i1,i2,i3,inzee)=rhk
839 zarray(2,i1,i2,i3,inzee)=ihk
840 end do
841 !case i2 >= 2
842 do i2=2,n2
843 j2=nd2+1-i2
844 do i1 = 1,n1h
845 j1=n1h+2-i1
846
847 a=zarray(1,i1,i2,i3,ouzee)
848 b=zarray(2,i1,i2,i3,ouzee)
849 c=zarray(1,j1,j2,j3,ouzee)
850 d=-zarray(2,j1,j2,j3,ouzee)
851 cp=cos_array(i1)
852 sp=sin_array(i1)
853 re=(a+c)
854 ie=(b+d)
855 ro=(a-c)*cp-(b-d)*sp
856 io=(a-c)*sp+(b-d)*cp
857 rhk=re-io
858 ihk=ie+ro
859
860 zarray(1,i1,i2,i3,inzee)=rhk
861 zarray(2,i1,i2,i3,inzee)=ihk
862 end do
863 end do
864
865
866 !case i3 >=2
867 do i3=2,n3
868 j3=nd3+1-i3
869 !case i2=1
870 i2=1
871 j2=1
872 do i1 = 1,n1h
873 j1=n1h+2-i1
874
875 a=zarray(1,i1,i2,i3,ouzee)
876 b=zarray(2,i1,i2,i3,ouzee)
877 c=zarray(1,j1,j2,j3,ouzee)
878 d=-zarray(2,j1,j2,j3,ouzee)
879 cp=cos_array(i1)
880 sp=sin_array(i1)
881 re=(a+c)
882 ie=(b+d)
883 ro=(a-c)*cp-(b-d)*sp
884 io=(a-c)*sp+(b-d)*cp
885 rhk=re-io
886 ihk=ie+ro
887
888 zarray(1,i1,i2,i3,inzee)=rhk
889 zarray(2,i1,i2,i3,inzee)=ihk
890 end do
891 !case i2 >= 2
892 do i2=2,n2
893 j2=nd2+1-i2
894 do i1 = 1,n1h
895 j1=n1h+2-i1
896
897 a=zarray(1,i1,i2,i3,ouzee)
898 b=zarray(2,i1,i2,i3,ouzee)
899 c=zarray(1,j1,j2,j3,ouzee)
900 d=-zarray(2,j1,j2,j3,ouzee)
901 cp=cos_array(i1)
902 sp=sin_array(i1)
903 re=(a+c)
904 ie=(b+d)
905 ro=(a-c)*cp-(b-d)*sp
906 io=(a-c)*sp+(b-d)*cp
907 rhk=re-io
908 ihk=ie+ro
909
910 zarray(1,i1,i2,i3,inzee)=rhk
911 zarray(2,i1,i2,i3,inzee)=ihk
912 end do
913 end do
914
915 end do
916
917 !De-allocations
918 safe_deallocate_a(cos_array)
919 safe_deallocate_a(sin_array)
920
921 pop_sub(kernel_application)
922 end subroutine kernel_application
923
924 !!****h* BigDFT/norm_ind
925 !! NAME
926 !! norm_ind
927 !!
928 !! FUNCTION
929 !! Index in zarray
930 !!
931 !! SOURCE
932 !!
933 subroutine norm_ind(nd1,nd2,nd3,i1,i2,i3,ind)
934 integer :: nd1,nd2,nd3,i1,i2,i3
935 integer :: ind
936
937 !Local variables
938 integer :: a1,a2,a3
939 if (i1 == nd1) then
940 a1 = 1
941 else
942 a1 = i1
943 end if
944 if (i2 == nd2) then
945 a2 = 1
946 else
947 a2 = i2
948 end if
949 if (i3 == nd3) then
950 a3 = 1
951 else
952 a3 = i3
953 end if
954 ind = a1 + nd1 * (a2 - 1) + nd1 * nd2 * (a3 - 1)
955 end subroutine norm_ind
956 !!***
957
958
959 !!****h* BigDFT/symm_ind
960 !! NAME
961 !! symm_ind
962 !!
963 !! FUNCTION
964 !! Index in zarray for -g vector
965 !!
966 !! SOURCE
967 !!
968 subroutine symm_ind(nd1,nd2,nd3,i1,i2,i3,ind)
969 integer :: nd1,nd2,nd3,i1,i2,i3
970 integer :: ind
971
972 integer :: a1,a2,a3
973 if (i1 /= 1) then
974 a1=nd1+1-i1
975 else
976 a1=i1
977 end if
978 if (i2 /= 1) then
979 a2=nd2+1-i2
980 else
981 a2=i2
982 end if
983 if (i3 /= 1) then
984 a3=nd3+1-i3
985 else
986 a3=i3
987 end if
988 ind=a1+nd1*(a2-1)+nd1*nd2*(a3-1)
989 end subroutine symm_ind
990 !!***
991
992 !!****h* BigDFT/zarray_in
993 !! NAME
994 !! zarray_in
995 !!
996 !! FUNCTION
997 !! Put the density into zarray
998 !!
999 !! SOURCE
1000 !!
1001 subroutine zarray_in(n01,n02,n03,nd1,nd2,nd3,density,zarray)
1002 integer :: n01,n02,n03,nd1,nd2,nd3
1003 real(real64), dimension(n01,n02,n03) :: density
1004 real(real64), dimension(2,nd1,nd2,nd3) :: zarray
1005
1006 integer :: i1,i2,i3,n01h,nd1hm,nd3hm,nd2hm
1007
1008 push_sub(zarray_in)
1009
1010 !Half the size of n01
1011 n01h=n01/2
1012 nd1hm=(nd1-1)/2
1013 nd2hm=(nd2-1)/2
1014 nd3hm=(nd3-1)/2
1015 !Set to zero
1016 do i3 = 1,nd3
1017 do i2 = 1,nd2
1018 do i1 = 1,nd1
1019 zarray(1,i1,i2,i3) = 0.0_8
1020 zarray(2,i1,i2,i3) = 0.0_8
1021 end do
1022 end do
1023 end do
1024 !Set zarray
1025 do i3 = 1,n03
1026 do i2 = 1,n02
1027 do i1 = 1,n01h
1028 zarray(1,i1+nd1hm,i2+nd2hm,i3+nd3hm) = density(2*i1-1,i2,i3)
1029 zarray(2,i1+nd1hm,i2+nd2hm,i3+nd3hm) = density(2*i1,i2,i3)
1030 end do
1031 end do
1032 end do
1033 if (modulo(n01,2) == 1) then
1034 do i3 = 1,n03
1035 do i2 = 1,n02
1036 zarray(1,n01h+1+nd1hm,i2+nd2hm,i3+nd3hm) = density(n01,i2,i3)
1037 end do
1038 end do
1039 end if
1040
1041 pop_sub(zarray_in)
1042 end subroutine zarray_in
1043 !!***
1044
1045
1046 !!****h* BigDFT/zarray_out
1047 !! NAME
1048 !! zarray_out
1049 !!
1050 !! FUNCTION
1051 !! Set the potential (rhopot) from zarray
1052 !! Calculate the Hartree energy.
1053 !!
1054 !! SOURCE
1055 !!
1056 subroutine zarray_out(n01, n02, n03, nd1, nd2, nd3, rhopot, zarray, factor)
1057 integer, intent(in) :: n01,n02,n03,nd1,nd2,nd3
1058 real(real64), intent(out) :: rhopot(n01,n02,n03)
1059 real(real64), intent(in) :: zarray(2*nd1,nd2,nd3) ! Convert zarray(2,nd1,nd2,nd3) -> zarray(2*nd1,nd2,nd3) to use i1=1,n01
1060 ! ! instead of i1=1,n1h + special case for modulo(n01,2)
1061 real(real64), intent(in) :: factor
1062
1063 integer :: i1,i2,i3
1064
1065 push_sub(zarray_out)
1066
1067 do i3 = 1,n03
1068 do i2 = 1,n02
1069 do i1 = 1,n01
1070 rhopot(i1, i2, i3) = factor*zarray(i1,i2,i3)
1071 end do
1072 end do
1073 end do
1074
1075 pop_sub(zarray_out)
1076 end subroutine zarray_out
1077 !!***
1078
1079 !!****h* BigDFT/build_kernel
1080 !! NAME
1081 !! build_kernel
1082 !!
1083 !! FUNCTION
1084 !! Build the kernel of a gaussian function
1085 !! for interpolating scaling functions.
1086 !!
1087 !! SYNOPSIS
1088 !! Build the kernel (karrayout) of a gaussian function
1089 !! for interpolating scaling functions
1090 !! $$ K(j) = \int \int \phi(x) g(x`-x) \delta(x`- j) dx dx` $$
1091 !!
1092 !! n01,n02,n03 Mesh dimensions of the density
1093 !! n1k,n2k,n3k Dimensions of the kernel
1094 !! hgrid Mesh step
1095 !! itype_scf Order of the scaling function (8,14,16)
1096 !!
1097 !! AUTHORS
1098 !! T. Deutsch, L. Genovese
1099 !! COPYRIGHT
1100 !! Copyright (C) 2005 CEA
1101 !! CREATION DATE
1102 !! 13/07/2005
1103 !!
1104 !! MODIFICATION HISTORY
1105 !! 13/09/2005 Use hgrid instead of acell
1106 !! 09/12/2005 Real kernel, stocked only half components
1107 !! 13/12/2005 Add routines to simplify the port into Stefan`s code
1108 !!
1109 !! SOURCE
1110 !!
1111 subroutine build_kernel(n01,n02,n03,nfft1,nfft2,nfft3,hgrid,itype_scf,karrayout)
1112 integer, intent(in) :: n01,n02,n03,nfft1,nfft2,nfft3,itype_scf
1113 real(real64), intent(in) :: hgrid
1114 real(real64), dimension(nfft1/2+1,nfft2/2+1,nfft3/2+1), intent(out) :: karrayout
1115
1116 !Do not touch !!!!
1117 integer, parameter :: N_GAUSS = 89
1118 !Better if higher (1024 points are enough 10^{-14}: 2*itype_scf*n_points)
1119 integer, parameter :: n_points = 2**6
1120
1121 !Better p_gauss for calculation
1122 !(the support of the exponential should be inside [-n_range/2,n_range/2])
1123 real(real64), parameter :: p0_ref = 1._real64
1124 real(real64), dimension(N_GAUSS) :: p_gauss,w_gauss
1125
1126 real(real64), allocatable :: kernel_scf(:), kern_1_scf(:)
1127 real(real64), allocatable :: x_scf(:), y_scf(:)
1128 real(real64), allocatable :: karrayhalf(:, :, :)
1129
1130 real(real64) :: ur_gauss,dr_gauss,acc_gauss,pgauss,kern,a_range
1131 real(real64) :: factor,factor2,dx,absci,p0gauss,p0_cell
1132 real(real64) :: a1,a2,a3
1133 integer :: nd1,nd2,nd3,n1k,n2k,n3k,n_scf
1134 integer :: i_gauss,n_range,n_cell
1135 integer :: i,n_iter,i1,i2,i3,i_kern
1136 integer :: i01,i02,i03,inkee,n1h,n2h,n3h,nd1h
1137
1138 push_sub(build_kernel)
1139
1140 !Number of integration points : 2*itype_scf*n_points
1141 n_scf=2*itype_scf*n_points
1142 !Dimensions of Kernel
1143 n1k=nfft1/2+1
1144 n2k=nfft2/2+1
1145 n3k=nfft3/2+1
1146 n1h=nfft1/2
1147 n2h=nfft2/2
1148 n3h=nfft3/2
1149 nd1 = nfft1 + modulo(nfft1+1,2)
1150 nd2 = nfft2 + modulo(nfft2+1,2)
1151 nd3 = nfft3 + modulo(nfft3+1,2)
1152
1153 !Half size for the half FFT
1154 nd1h=(nd1+1)/2
1155
1156 !Allocations
1157 safe_allocate(x_scf(0:n_scf))
1158 safe_allocate(y_scf(0:n_scf))
1159
1160 !Build the scaling function
1161 call scaling_function(itype_scf,n_scf,n_range,x_scf,y_scf)
1162 !Step grid for the integration
1163 dx = real(n_range, real64)/real(n_scf, real64)
1164 !Extend the range (no more calculations because fill in by 0.0_8)
1165 n_cell = max(n01,n02,n03)
1166 n_range = max(n_cell,n_range)
1167
1168 !Allocations
1169 safe_allocate(kernel_scf(-n_range:n_range))
1170 safe_allocate(kern_1_scf(-n_range:n_range))
1171
1172 !Lengthes of the box (use FFT dimension)
1173 a1 = hgrid * real(n01, real64)
1174 a2 = hgrid * real(n02, real64)
1175 a3 = hgrid * real(n03, real64)
1176
1177 x_scf(:) = hgrid * x_scf(:)
1178 y_scf(:) = 1._real64/hgrid * y_scf(:)
1179 dx = hgrid * dx
1180 !To have a correct integration
1181 p0_cell = p0_ref/(hgrid*hgrid)
1182
1183 !Initialisation of the gaussian (Beylkin)
1184 call gequad(n_gauss,p_gauss,w_gauss,ur_gauss,dr_gauss,acc_gauss)
1185 !In order to have a range from a_range=sqrt(a1*a1+a2*a2+a3*a3)
1186 !(biggest length in the cube)
1187 !We divide the p_gauss by a_range**2 and a_gauss by a_range
1188 a_range = sqrt(a1*a1+a2*a2+a3*a3)
1189 factor = 1._real64/a_range
1190 !factor2 = factor*factor
1191 factor2 = 1._real64/(a1*a1+a2*a2+a3*a3)
1192 do i_gauss=1,n_gauss
1193 p_gauss(i_gauss) = factor2*p_gauss(i_gauss)
1194 end do
1195 do i_gauss=1,n_gauss
1196 w_gauss(i_gauss) = factor*w_gauss(i_gauss)
1197 end do
1198
1199 karrayout(:,:,:) = 0.0_8
1200
1201 !Use in this order (better for accuracy).
1202 loop_gauss: do i_gauss=n_gauss,1,-1
1203 !Gaussian
1204 pgauss = p_gauss(i_gauss)
1205
1206 !We calculate the number of iterations to go from pgauss to p0_ref
1207 n_iter = nint((log(pgauss) - log(p0_cell))/log(4._real64))
1208 if (n_iter <= 0) then
1209 n_iter = 0
1210 p0gauss = pgauss
1211 else
1212 p0gauss = pgauss/4._real64**n_iter
1213 end if
1214
1215 !Stupid integration
1216 !Do the integration with the exponential centered in i_kern
1217 kernel_scf(:) = 0.0_8
1218 do i_kern=0,n_range
1219 kern = 0.0_8
1220 do i=0,n_scf
1221 absci = x_scf(i) - real(i_kern, real64)*hgrid
1222 absci = absci*absci
1223 kern = kern + y_scf(i)*exp(-p0gauss*absci)*dx
1224 end do
1225 kernel_scf(i_kern) = kern
1226 kernel_scf(-i_kern) = kern
1227 if (abs(kern) < 1.d-18) then
1228 !Too small not useful to calculate
1229 exit
1230 end if
1231 end do
1232
1233 !Start the iteration to go from p0gauss to pgauss
1234 call scf_recursion(itype_scf,n_iter,n_range,kernel_scf,kern_1_scf)
1235
1236 !Add to the kernel.
1237 do i3 = 1,n03
1238 i03 = i3-1
1239 do i2 = 1,n02
1240 i02 = i2-1
1241 do i1 = 1,n01
1242 i01 = i1-1
1243 karrayout(i1,i2,i3) = karrayout(i1,i2,i3) + w_gauss(i_gauss)* &
1244 kernel_scf(i01)*kernel_scf(i02)*kernel_scf(i03)
1245 end do
1246 end do
1247 end do
1248
1249 end do loop_gauss
1250
1251 safe_deallocate_a(kernel_scf)
1252 safe_deallocate_a(kern_1_scf)
1253 safe_deallocate_a(x_scf)
1254 safe_deallocate_a(y_scf)
1255
1256 !Set karray
1257 safe_allocate(karrayhalf(1:2, 1:nd1h*nd2*nd3, 1:2))
1258 ! The FFT reads the padding of the work slab, which karrayhalf_in does not set
1259 karrayhalf(:, :, 2) = m_zero
1260
1261 !Set karray : use mirror symmetries
1262 inkee=1
1263 call karrayhalf_in(n01,n02,n03,n1k,n2k,n3k,nfft1,nfft2,nfft3,nd1,nd2,nd3,&
1264 karrayout,karrayhalf)
1265 call fft(n1h,nfft2,nfft3,nd1h,nd2,nd3,karrayhalf,1,inkee)
1266 !Reconstruct the real kernel
1267 call kernel_recon(n1k,n2k,n3k,nfft1,nfft2,nfft3,nd1,nd2,nd3,&
1268 karrayhalf(1,1,inkee),karrayout)
1269
1270 safe_deallocate_a(karrayhalf)
1271 pop_sub(build_kernel)
1272 end subroutine build_kernel
1273 !!***
1274
1275
1276 !!****h* BigDFT/calculate_dimensions
1277 !! NAME
1278 !! calculate_dimensions
1279 !!
1280 !! FUNCTION
1281 !! Give the dimensions of the FFT
1282 !!
1283 !! SOURCE
1284 !!
1285 subroutine calculate_dimensions(n01,n02,n03,nfft1,nfft2,nfft3)
1286 integer, intent(in) :: n01,n02,n03
1287 integer, intent(out) :: nfft1,nfft2,nfft3
1288
1289 integer :: i1,i2,i3,l1
1290
1291 push_sub(calculate_dimensions)
1292
1293 !Test 2*n01, 2*n02, 2*n03
1294 !write(*,*) 'in dimensions_fft',n01,n02,n03
1295 i1=2*n01
1296 i2=2*n02
1297 i3=2*n03
1298 do
1299 call fourier_dim(i1,nfft1)
1300 call fourier_dim(nfft1/2,l1)
1301 if (modulo(nfft1,2) == 0 .and. modulo(l1,2) == 0 .and. 2*l1 == nfft1) then
1302 exit
1303 end if
1304 i1=i1+1
1305 end do
1306 do
1307 call fourier_dim(i2,nfft2)
1308 if (modulo(nfft2,2) == 0) then
1309 exit
1310 end if
1311 i2=i2+1
1312 end do
1313 do
1314 call fourier_dim(i3,nfft3)
1315 if (modulo(nfft3,2) == 0) then
1316 exit
1317 end if
1318 i3=i3+1
1319 end do
1320 !nd1 = nfft1 + modulo(nfft1+1,2)
1321 !nd2 = nfft2 + modulo(nfft2+1,2)
1322 !nd3 = nfft3 + modulo(nfft3+1,2)
1323 !write(*,*) 'out dimensions_fft',nfft1,nfft2,nfft3
1324
1325 pop_sub(calculate_dimensions)
1326 end subroutine calculate_dimensions
1327 !!***
1328
1329
1330 !!****h* BigDFT/karrayhalf_in
1331 !! NAME
1332 !! karrayhalf_in
1333 !!
1334 !! FUNCTION
1335 !! Put in the array for4446666444 FFT
1336 !!
1337 !! SOURCE
1338 !!
1339 subroutine karrayhalf_in(n01,n02,n03,n1k,n2k,n3k,nfft1,nfft2,nfft3,nd1,nd2,nd3, kernel,karrayhalf)
1340 integer, intent(in) :: n01,n02,n03,n1k,n2k,n3k,nfft1,nfft2,nfft3,nd1,nd2,nd3
1341 real(real64), dimension(n1k,n2k,n3k), intent(in) :: kernel
1342 real(real64), dimension(2,(nd1+1)/2,nd2,nd3), intent(out) :: karrayhalf
1343
1344 real(real64), dimension(:), allocatable :: karray
1345 integer :: i1,i2,i3,nd1h,n1h,n2h,n3h
1346
1347 push_sub(karrayhalf_in)
1348
1349 !Body
1350 n1h=nfft1/2
1351 n2h=nfft2/2
1352 n3h=nfft3/2
1353
1354 safe_allocate(karray(1:nfft1))
1355
1356 nd1h=(nd1+1)/2
1357 karrayhalf(:,:,:,:) = 0.0_8
1358 do i3 = 1,n03
1359 do i2 = 1,n02
1360 karray(:) = 0.0_8
1361 do i1 = 1,n01
1362 karray(i1+n1h) = kernel(i1,i2,i3)
1363 end do
1364 do i1 = 2,n01
1365 karray(n1h-i1+1+nd1-nfft1) = kernel(i1,i2,i3)
1366 end do
1367 do i1 = 1,n1h
1368 karrayhalf(1,i1,i2+n2h,i3+n3h) = karray(2*i1-1)
1369 karrayhalf(2,i1,i2+n2h,i3+n3h) = karray(2*i1)
1370 end do
1371 end do
1372 do i2 = 2,n02
1373 do i1 = 1,nd1h
1374 karrayhalf(:,i1,n2h-i2+1+nd2-nfft2,i3+n3h) = &
1375 karrayhalf(:,i1,i2+n2h,i3+n3h)
1376 end do
1377 end do
1378 end do
1379 do i3 = 2,n03
1380 do i2 = 1,nd2
1381 do i1 = 1,nd1h
1382 karrayhalf(:,i1,i2,n3h-i3+1+nd3-nfft3) = karrayhalf(:,i1,i2,i3+n3h)
1383 end do
1384 end do
1385 end do
1386
1387 safe_deallocate_a(karray)
1388 pop_sub(karrayhalf_in)
1389 end subroutine karrayhalf_in
1390 !!***
1391
1392
1393 !!****h* BigDFT/kernel_recon
1394 !! NAME
1395 !! kernel_recon
1396 !!
1397 !! FUNCTION
1398 !! Reconstruction of the kernel from the FFT array zarray
1399 !! We keep only the half kernel in each direction (x,y,z).
1400 !!
1401 !! SOURCE
1402 !!
1403 subroutine kernel_recon(n1k,n2k,n3k,nfft1,nfft2,nfft3,nd1,nd2,nd3,zarray,karray)
1404 integer, intent(in) :: n1k,n2k,n3k,nfft1,nfft2,nfft3,nd1,nd2,nd3
1405 real(real64), dimension(2,(nd1+1)/2*nd2*nd3), intent(in) :: zarray
1406 real(real64), dimension(n1k,n2k,n3k), intent(out) :: karray
1407
1408 real(real64), dimension(:), allocatable :: cos_array,sin_array
1409 integer :: i1,i2,i3,ind1,ind2,nd1h,n1h,n2h,n3h
1410 real(real64) :: rfe,ife,rfo,ifo,cp,sp,rk,ik,a,b,c,d,pi2
1411
1412 push_sub(kernel_recon)
1413
1414 !Body
1415 n1h=nfft1/2
1416 n2h=nfft2/2
1417 n3h=nfft3/2
1418 nd1h=(nd1+1)/2
1419 pi2=8._real64*datan(1._real64)
1420 pi2=pi2/real(nfft1, real64)
1421
1422 safe_allocate(cos_array(1:nd1h))
1423 safe_allocate(sin_array(1:nd1h))
1424
1425 do i1 = 1,nd1h
1426 cos_array(i1)= dcos(pi2*(i1-1))
1427 sin_array(i1)=-dsin(pi2*(i1-1))
1428 end do
1429 do i3 = 1,n3h+1
1430 do i2 = 1,n2h+1
1431 do i1 = 1,nd1h
1432 call norm_ind(nd1h,nd2,nd3,i1,i2,i3,ind1)
1433 call symm_ind(nd1h,nd2,nd3,i1,i2,i3,ind2)
1434 a=zarray(1,ind1)
1435 b=zarray(2,ind1)
1436 c=zarray(1,ind2)
1437 d=zarray(2,ind2)
1438 rfe=0.5_real64*(a+c)
1439 ife=0.5_real64*(b-d)
1440 rfo=0.5_real64*(a-c)
1441 ifo=0.5_real64*(b+d)
1442 cp=cos_array(i1)
1443 sp=sin_array(i1)
1444 rk=rfe+cp*ifo-sp*rfo
1445 ik=ife-cp*rfo-sp*ifo
1446 !For big dimension 1.d-9 otherwise 1.d-10
1447 !Remove the test
1448 !if (abs(ik) >= 1.d-10) then
1449 ! print *,"non real kernel FFT",i1,i2,i3,ik
1450 ! stop
1451 !end if
1452 !Build the intermediate FFT convolution (full)
1453 !call norm_ind(nd1,nd2,nd3,i1,i2,i3,indA)
1454 karray(i1,i2,i3)=rk
1455 end do
1456 end do
1457 end do
1458
1459 safe_deallocate_a(cos_array)
1460 safe_deallocate_a(sin_array)
1461
1462 pop_sub(kernel_recon)
1463 end subroutine kernel_recon
1464 !!***
1465
1466 !!****h* BigDFT/par_calculate_dimensions
1467 !! NAME
1468 !! par_calculate_dimensions
1469 !!
1470 !! FUNCTION
1471 !! Calculate four sets of dimension needed for the calculation of the
1472 !! zero-padded convolution
1473 !!
1474 !! SYNOPSIS
1475 !! n01,n02,n03 original real dimensions (input)
1476 !!
1477 !! m1,m2,m3 original real dimension with the dimension 2 and 3 exchanged
1478 !!
1479 !! n1,n2 the first FFT even dimensions greater that 2*m1, 2*m2
1480 !! n3 the double of the first FFT even dimension greater than m3
1481 !! (improved for the HalFFT procedure)
1482 !!
1483 !! md1,md2,md3 half of n1,n2,n3 dimension. They contain the real unpadded space,
1484 !! properly enlarged to be compatible with the FFT dimensions n_i.
1485 !! md2 is further enlarged to be a multiple of nproc
1486 !!
1487 !! nd1,nd2,nd3 fourier dimensions for which the kernel FFT is injective,
1488 !! formally 1/8 of the fourier grid. Here the dimension nd3 is
1489 !! enlarged to be a multiple of nproc
1490 !!
1491 !! WARNING
1492 !! The dimension m2 and m3 correspond to n03 and n02 respectively
1493 !! this is needed since the convolution routine manage arrays of dimension
1494 !! (md1,md3,md2/nproc)
1495 !!
1496 !! AUTHOR
1497 !! Luigi Genovese
1498 !! CREATION DATE
1499 !! February 2006
1500 !!
1501 !! SOURCE
1502 !!
1503 subroutine par_calculate_dimensions(n01,n02,n03,m1,m2,m3,n1,n2,n3, md1,md2,md3,nd1,nd2,nd3,nproc)
1504 integer, intent(in) :: n01,n02,n03,nproc
1505 integer, intent(out) :: m1,m2,m3,n1,n2,n3,md1,md2,md3,nd1,nd2,nd3
1506
1507 integer :: l1,l2,l3
1508
1509 push_sub(par_calculate_dimensions)
1510
1511 !dimensions of the density in the real space, inverted for convenience
1512
1513 m1=n01
1514 m2=n03
1515 m3=n02
1516
1517 ! real space grid dimension (suitable for number of processors)
1518
1519 ! n1=2*m1
1520 ! n2=2*m2
1521 ! n3=2*m3
1522
1523 l1=2*m1
1524 l2=2*m2
1525 l3=m3 !beware of the half dimension
1526 do
1527 !this is for the FFT of the kernel
1528 !one can erase it when the kernel is parallelized
1529 call fourier_dim(l1,n1)
1530 !call fourier_dim(n1/2,l1A)
1531 if (modulo(n1,2) == 0&! .and. 2*l1A == n1
1532 ) then
1533 exit
1534 end if
1535 l1=l1+1
1536 end do
1537 do
1538 call fourier_dim(l2,n2)
1539 if (modulo(n2,2) == 0) then
1540 exit
1541 end if
1542 l2=l2+1
1543 end do
1544 do
1545 call fourier_dim(l3,n3)
1546 !call fourier_dim(n3/2,l3A)
1547 if (modulo(n3,2) == 0 &!.and. 2*l3A == n3 .and. modulo(l3A,2) == 0
1548 ) then
1549 exit
1550 end if
1551 l3=l3+1
1552 end do
1553 n3=2*n3
1554
1555 !dimensions that contain the unpadded real space,
1556 ! compatible with the number of processes
1557 md1=n1/2
1558 md2=n2/2
1559 md3=n3/2
1560151 if (nproc*(md2/nproc) < n2/2) then
1561 md2=md2+1
1562 goto 151
1563 end if
1564
1565
1566 !dimensions of the kernel, 1/8 of the total volume,
1567 !compatible with nproc
1568
1569 nd1=n1/2+1
1570 nd2=n2/2+1
1571 nd3=n3/2+1
1572250 if (modulo(nd3,nproc) /= 0) then
1573 nd3=nd3+1
1574 goto 250
1575 end if
1576
1578 end subroutine par_calculate_dimensions
1579 !!***
1580
1581
1582 !!****h* BigDFT/par_psolver_kernel
1583 !! NAME
1584 !! par_psolver_kernel
1585 !!
1586 !! FUNCTION
1587 !! Solver of Poisson equation applying a kernel, parallel computation
1588 !!
1589 !! SYNOPSIS
1590 !! Poisson solver applying a kernel and
1591 !! using Fourier transform for the convolution.
1592 !! rhopot : input -> the density
1593 !! output -> the Hartree potential + pot_ion
1594 !! All the processes manage the same global rhopot array
1595 !! The potential pot_ion is ADDED in the array rhopot.
1596 !! Calculate also the Hartree potential
1597 !!
1598 !! Replaces the charge density contained in rhopot
1599 !! by the Hartree stored as well in rhopot.
1600 !! If xc_on is true, it also adds the XC potential and
1601 !! ionic potential pot_ion
1602 !!
1603 !! kernelLOC: the kernel in fourier space, calculated from ParBuil_Kernel routine
1604 !! it is a local vector (each process have its own part)
1605 !!
1606 !! comm: MPI communicator to use
1607 !!
1608 !! We double the size of the mesh except in one dimension
1609 !! in order to use the property of the density to be real.
1610 !! WARNING
1611 !!
1612 !! AUTHOR
1613 !! Luigi Genovese
1614 !! CREATION DATE
1615 !! February 2006
1616 !!
1617 !! SOURCE
1618 !!
1619 subroutine par_psolver_kernel(n01, n02, n03, nd1, nd2, nd3, hgrid, kernelLOC, rhopot, iproc, nproc, comm)
1620 integer, intent(in) :: n01,n02,n03,iproc,nproc
1621 integer, intent(inout) :: nd1,nd2,nd3
1622 real(real64), intent(in) :: hgrid
1623 real(real64), intent(in), dimension(nd1,nd2,nd3/nproc) :: kernelLOC
1624 real(real64), intent(inout), dimension(n01,n02,n03) :: rhopot
1625 type(mpi_comm), intent(in) :: comm
1626
1627 integer :: m1,m2,m3,n1,n2,n3,md1,md2,md3
1628
1629 push_sub(par_psolver_kernel)
1630
1631 call par_calculate_dimensions(n01, n02, n03, m1, m2, m3, n1, n2, n3, md1, md2, md3, nd1, nd2, nd3, nproc)
1632 call pconvxc_off(m1, m2, m3, n1, n2, n3, nd1, nd2, nd3, md1, md2, md3, iproc, nproc, rhopot, kernelloc, hgrid, comm)
1633
1634 pop_sub(par_psolver_kernel)
1635 end subroutine par_psolver_kernel
1636 !!***
1637
1638
1639 !!****h* BigDFT/pconvxc_off
1640 !! NAME
1641 !! pconvxc_off
1642 !!
1643 !! FUNCTION
1644 !! Calculate the parallel convolution with the kernel
1645 !! without the exchange-correlation part
1646 !!
1647 !! SYNOPSIS
1648 !! Poisson solver applying a kernel and
1649 !! using Fourier transform for the convolution.
1650 !! rhopot : input -> the density
1651 !! output -> the Hartree potential + pot_ion
1652 !! All the processes manage the same global rhopot array
1653 !! The potential pot_ion is ADDED in the array rhopot.
1654 !! Calculate also the Hartree potential
1656 !! Replaces the charge density contained in rhopot
1657 !! by the Hartree stored as well in rhopot.
1658 !!
1659 !! kernelLOC: the kernel in fourier space, calculated from ParBuild_Kernel routine
1660 !! it is a local vector (each process have its own part)
1661 !!
1662 !! comm: MPI communicator to use
1663 !!
1664 !! We double the size of the mesh except in one dimension
1665 !! in order to use the property of the density to be real.
1666 !! WARNING
1667 !!
1668 !! AUTHOR
1669 !! Luigi Genovese
1670 !! CREATION DATE
1671 !! February 2006
1672 !!
1673 !! SOURCE
1674 !!
1675 subroutine pconvxc_off(m1, m2, m3, n1, n2, n3, nd1, nd2, nd3, md1, md2, md3, iproc, nproc, rhopot, kernelloc, hgrid, comm)
1676 integer, intent(in) :: m1,m2,m3,n1,n2,n3,nd1,nd2,nd3,md1,md2,md3,iproc,nproc
1677 real(real64), dimension(nd1,nd2,nd3/nproc), intent(in) :: kernelloc
1678 real(real64), dimension(m1,m3,m2), intent(inout) :: rhopot
1679 real(real64), intent(in) :: hgrid
1680 type(mpi_comm), intent(in) :: comm
1681
1682 !Local variables
1683 integer :: istart,iend,jend,jproc
1684 real(real64) :: scal
1685 real(real64), dimension(:,:,:), allocatable :: zf, lrhopot(:, :, :)
1686 integer, dimension(:), allocatable :: counts, displs
1687
1688 push_sub(pconvxc_off)
1689
1690 !factor to be used to keep unitarity
1691 scal=hgrid**3/(real(n1*n2, real64)*real(n3, real64))
1692
1693 safe_allocate(zf(1:md1, 1:md3, 1:md2/nproc))
1694 safe_allocate(counts(0:nproc-1))
1695 safe_allocate(displs(0:nproc-1))
1696
1697 !Here we insert the process-related values of the density, starting from the total density
1698 call enterdensity(rhopot(1,1,1), m1, m2, m3, md1, md2, md3, iproc, nproc, zf(1,1,1))
1699
1700 !this routine builds the values for each process of the potential (zf), multiplying by the factor
1701 call convolxc_off(n1, n2, n3, nd1, nd2, nd3, md1, md2, md3, nproc, iproc, kernelloc, zf, scal, comm)
1702
1703 !building the array of the data to be sent from each process
1704 !and the array of the displacement
1705 do jproc = 0, nproc - 1
1706 istart = min(jproc*(md2/nproc),m2-1)
1707 jend = max(min(md2/nproc, m2 - md2/nproc*jproc), 0)
1708 counts(jproc) = m1*m3*jend
1709 displs(jproc) = istart*m1*m3
1710 end do
1712 !assign the distributed density to the rhopot array
1713 istart=min(iproc*(md2/nproc),m2-1)
1714 jend=max(min(md2/nproc,m2-md2/nproc*iproc),0)
1715 iend=istart+jend
1716
1717 if (jend == 0) jend = 1
1718
1719 safe_allocate(lrhopot(1:m1, 1:m3, 1:jend))
1720
1721 lrhopot(1:m1, 1:m3, 1:jend) = zf(1:m1, 1:m3, 1:jend)
1722
1723 call profiling_in("ISF_GATHER")
1724#if defined(HAVE_MPI)
1725 call mpi_allgatherv(lrhopot(1, 1, 1), counts(iproc), mpi_double_precision, rhopot(1, 1, 1), counts,&
1726 displs, mpi_double_precision, comm)
1727#endif
1728 call profiling_out("ISF_GATHER")
1729
1730 safe_deallocate_a(zf)
1731 safe_deallocate_a(lrhopot)
1732 safe_deallocate_a(counts)
1733 safe_deallocate_a(displs)
1734
1735 pop_sub(pconvxc_off)
1736 end subroutine pconvxc_off
1737!!***
1738
1739
1740 !!****h* BigDFT/enterdensity
1741 !! NAME
1742 !! enterdensity
1743 !!
1744 !! FUNCTION
1745 !!
1746 !! Define a real space process-dependent vector zf with the global dimensions that are half of the FFT grid
1747 !! in order to perform convolution. The dimension md2 is a multiple of nproc
1748 !! Can be used also to define the local part of pot_ion
1749 !!
1750 !! AUTHOR
1751 !! L. Genovese
1752 !! CREATION DATE
1753 !! February 2006
1754 !!
1755 !! SOURCE
1756 !!
1757 subroutine enterdensity(rhopot,m1,m2,m3,md1,md2,md3,iproc,nproc,zf)
1758 integer, intent(in) :: m1,m2,m3,md1,md2,md3,iproc,nproc
1759 real(real64), dimension(0:md1-1,0:md3-1,0:md2/nproc-1), intent(out) :: zf
1760 real(real64), dimension(0:m1-1,0:m3-1,0:m2-1), intent(in) :: rhopot
1761
1762 integer :: j1,j2,j3,jp2
1763
1764 push_sub(enterdensity)
1765
1766 !Body
1767 do jp2=0,md2/nproc-1
1768 j2=iproc*(md2/nproc)+jp2
1769 if (j2 <= m2-1) then
1770 do j3=0,m3-1
1771 do j1=0,m1-1
1772 zf(j1,j3,jp2)=rhopot(j1,j3,j2)
1773 end do
1774 do j1=m1,md1-1
1775 zf(j1,j3,jp2)=0.0_8
1776 end do
1777 end do
1778 do j3=m3,md3-1
1779 do j1=0,md1-1
1780 zf(j1,j3,jp2)=0.0_8
1781 end do
1782 end do
1783 else
1784 do j3=0,md3-1
1785 do j1=0,md1-1
1786 zf(j1,j3,jp2)=0.0_8
1787 end do
1788 end do
1789 end if
1790 end do
1791
1792 pop_sub(enterdensity)
1793 end subroutine enterdensity
1794 !!***
1795
1796
1797 !!****h* BigDFT/par_build_kernel
1798 !! NAME
1799 !! par_build_kernel
1800 !!
1801 !! FUNCTION
1802 !! Build the kernel of a gaussian function
1803 !! for interpolating scaling functions.
1804 !! Do the parallel HalFFT of the symmetrized function and stores into
1805 !! memory only 1/8 of the grid divided by the number of processes nproc
1806 !!
1807 !! SYNOPSIS
1808 !! Build the kernel (karray) of a gaussian function
1809 !! for interpolating scaling functions
1810 !! $$ K(j) = \sum_k \omega_k \int \int \phi(x) g_k(y-x) \delta(y-j) dx dy $$
1811 !!
1812 !! n01,n02,n03 Mesh dimensions of the density
1813 !! nfft1,nfft2,nfft3 Dimensions of the FFT grid (HalFFT in the third direction)
1814 !! n1k,n2k,n3k Dimensions of the kernel FFT
1815 !! hgrid Mesh step
1816 !! itype_scf Order of the scaling function (8,14,16)
1817 !! comm MPI communicator to use
1818 !!
1819 !! AUTHORS
1820 !! T. Deutsch, L. Genovese
1821 !! CREATION DATE
1822 !! February 2006
1823 !!
1824 !! SOURCE
1825 !!
1826 subroutine par_build_kernel(n01,n02,n03,nfft1,nfft2,nfft3,n1k,n2k,n3k,hgrid,itype_scf, &
1827 iproc,nproc,comm,karrayoutLOC)
1828 integer, intent(in) :: n01,n02,n03,nfft1,nfft2,nfft3,n1k,n2k,n3k,itype_scf,iproc,nproc
1829 real(real64), intent(in) :: hgrid
1830 real(real64), intent(out) :: karrayoutLOC(1:n1k, 1:n2k, 1:n3k/nproc)
1831 type(mpi_comm), intent(in) :: comm
1832
1833 !Do not touch !!!!
1834 integer, parameter :: N_GAUSS = 89
1835 !Better if higher (1024 points are enough 10^{-14}: 2*itype_scf*n_points)
1836 integer, parameter :: N_POINTS = 2**6
1837
1838 !Better p_gauss for calculation
1839 !(the support of the exponential should be inside [-n_range/2,n_range/2])
1840 real(real64), parameter :: p0_ref = 1._real64
1841 real(real64) :: p_gauss(N_GAUSS), w_gauss(N_GAUSS)
1842
1843 real(real64), dimension(:), allocatable :: kernel_scf,kern_1_scf
1844 real(real64), dimension(:), allocatable :: x_scf ,y_scf
1845 real(real64), dimension(:,:,:,:), allocatable :: karrayfour
1846 real(real64), dimension(:,:,:), allocatable :: karray
1847
1848 real(real64) :: ur_gauss,dr_gauss,acc_gauss,pgauss,kern,a_range
1849 real(real64) :: factor,factor2,dx,absci,p0gauss,p0_cell
1850 real(real64) :: a1,a2,a3
1851 integer :: n_scf,nker1,nker2,nker3
1852 integer :: i_gauss,n_range,n_cell,istart,iend,istart1,istart2,iend1,iend2
1853 integer :: i,n_iter,i1,i2,i3,i_kern
1854 integer :: i01,i02,i03,n1h,n2h,n3h
1855
1856 push_sub(par_build_kernel)
1857
1858 !Number of integration points : 2*itype_scf*n_points
1859 n_scf=2*itype_scf*n_points
1860 !Set karray
1861 !dimension test
1863 !here we must set the dimensions for the fft part, starting from the nfft
1864 !remember that actually nfft2 is associated with n03 and viceversa
1865
1866 !dimensions that define the center of symmetry
1867 n1h=nfft1/2
1868 n2h=nfft2/2
1869 n3h=nfft3/2
1870
1871 !Auxiliary dimensions only for building the FFT part
1872 nker1=nfft1
1873 nker2=nfft2
1874 nker3=nfft3/2+1
1875
1876 !adjusting the last two dimensions to be multiples of nproc
1877 do
1878 if (modulo(nker2,nproc) == 0) exit
1879 nker2=nker2+1
1880 end do
1881 do
1882 if (modulo(nker3,nproc) == 0) exit
1883 nker3=nker3+1
1884 end do
1885
1886 !this will be the array of the kernel in the real space
1887 safe_allocate(karray(1:nker1,1:nfft3,1:nker2/nproc))
1888
1889 !defining proper extremes for the calculation of the
1890 !local part of the kernel
1891
1892 istart=iproc*nker2/nproc+1
1893 iend=min((iproc+1)*nker2/nproc,n2h+n03)
1894
1895 istart1=istart
1896 if (iproc == 0) istart1=n2h-n03+2
1897
1898 iend2=iend
1899
1900 iend1=n2h
1901 istart2=n2h+1
1902 if (istart > n2h) then
1903 iend1=istart1-1
1904 istart2=istart
1905 end if
1906 if (iend <= n2h) then
1907 istart2=iend2+1
1908 iend1=iend
1909 end if
1910
1911!!!!!START KERNEL CONSTRUCTION
1912 ! if (iproc == 0) then
1913 ! write(unit=*,fmt="(1x,a,i0,a)") &
1914 ! "Build the kernel in parallel using a sum of ",N_GAUSS," gaussians"
1915 ! write(unit=*,fmt="(1x,a,i0,a)") &
1916 ! "Use interpolating scaling functions of ",itype_scf," order"
1917 ! end if
1918
1919 safe_allocate(x_scf(0:n_scf))
1920 safe_allocate(y_scf(0:n_scf))
1921
1922 !Build the scaling function
1923 call scaling_function(itype_scf, n_scf, n_range, x_scf, y_scf)
1924 !Step grid for the integration
1925 dx = real(n_range, real64)/real(n_scf, real64)
1926 !Extend the range (no more calculations because fill in by 0.0_8)
1927 n_cell = max(n01,n02,n03)
1928 n_range = max(n_cell,n_range)
1929
1930 !Allocations
1931 safe_allocate(kernel_scf(-n_range:n_range))
1932 safe_allocate(kern_1_scf(-n_range:n_range))
1933
1934 !Lengthes of the box (use FFT dimension)
1935 a1 = hgrid * real(n01, real64)
1936 a2 = hgrid * real(n02, real64)
1937 a3 = hgrid * real(n03, real64)
1938
1939 x_scf(:) = hgrid * x_scf(:)
1940 y_scf(:) = 1._real64/hgrid * y_scf(:)
1941 dx = hgrid * dx
1942 !To have a correct integration
1943 p0_cell = p0_ref/(hgrid*hgrid)
1944
1945 !Initialization of the gaussian (Beylkin)
1946 call gequad(n_gauss,p_gauss,w_gauss,ur_gauss,dr_gauss,acc_gauss)
1947 !In order to have a range from a_range=sqrt(a1*a1+a2*a2+a3*a3)
1948 !(biggest length in the cube)
1949 !We divide the p_gauss by a_range**2 and a_gauss by a_range
1950 a_range = sqrt(a1*a1+a2*a2+a3*a3)
1951 factor = 1._real64/a_range
1952 !factor2 = factor*factor
1953 factor2 = 1._real64/(a1*a1+a2*a2+a3*a3)
1954 do i_gauss=1,n_gauss
1955 p_gauss(i_gauss) = factor2*p_gauss(i_gauss)
1956 end do
1957 do i_gauss=1,n_gauss
1958 w_gauss(i_gauss) = factor*w_gauss(i_gauss)
1959 end do
1960
1961 karray(:,:,:) = 0.0_8
1962 !Use in this order (better for accuracy).
1963 loop_gauss: do i_gauss = n_gauss, 1, -1
1964 !Gaussian
1965 pgauss = p_gauss(i_gauss)
1966
1967 !We calculate the number of iterations to go from pgauss to p0_ref
1968 n_iter = nint((log(pgauss) - log(p0_cell))/log(4._real64))
1969 if (n_iter <= 0) then
1970 n_iter = 0
1971 p0gauss = pgauss
1972 else
1973 p0gauss = pgauss/4._real64**n_iter
1974 end if
1975
1976 !Stupid integration
1977 !Do the integration with the exponential centered in i_kern
1978 kernel_scf(:) = 0.0_8
1979 do i_kern=0,n_range
1980 kern = 0.0_8
1981 do i=0,n_scf
1982 absci = x_scf(i) - real(i_kern, real64)*hgrid
1983 absci = absci*absci
1984 kern = kern + y_scf(i)*exp(-p0gauss*absci)*dx
1985 end do
1986 kernel_scf(i_kern) = kern
1987 kernel_scf(-i_kern) = kern
1988 if (abs(kern) < 1.d-18) then
1989 !Too small not useful to calculate
1990 exit
1991 end if
1992 end do
1993
1994 !Start the iteration to go from p0gauss to pgauss
1995 call scf_recursion(itype_scf,n_iter,n_range,kernel_scf,kern_1_scf)
1996
1997 !Add to the kernel (only the local part)
1998
1999 do i3=istart1,iend1
2000 i03 = n2h - i3 + 1
2001 do i2 = 1,n02
2002 i02 = i2-1
2003 do i1 = 1,n01
2004 i01 = i1-1
2005 karray(i1+n1h,i2+n3h,i3-istart+1) = karray(i1+n1h,i2+n3h,i3-istart+1) + w_gauss(i_gauss)* &
2006 kernel_scf(i01)*kernel_scf(i02)*kernel_scf(i03)
2007 end do
2008 end do
2009 end do
2010 do i3=istart2,iend2
2011 i03 = i3 - n2h -1
2012 do i2 = 1,n02
2013 i02 = i2-1
2014 do i1 = 1,n01
2015 i01 = i1-1
2016 karray(i1+n1h,i2+n3h,i3-istart+1) = karray(i1+n1h,i2+n3h,i3-istart+1) + w_gauss(i_gauss)* &
2017 kernel_scf(i01)*kernel_scf(i02)*kernel_scf(i03)
2018 end do
2019 end do
2020 end do
2021
2022
2023 end do loop_gauss
2024
2025 !Build the kernel in the real space as an even function, thus having a real FFT
2026
2027 do i3=istart1,iend2
2028 do i2 = 1,n02
2029 do i1=2,n01
2030 karray(n1h+2-i1,i2+n3h,i3-istart+1) = karray(i1+n1h,i2+n3h,i3-istart+1)
2031 end do
2032 end do
2033 do i2=2,n02
2034 do i1 = 1,nker1
2035 karray(i1,n3h+2-i2,i3-istart+1) = karray(i1,i2+n3h,i3-istart+1)
2036 end do
2037 end do
2038 end do
2039
2040
2041 !De-allocations
2042 safe_deallocate_a(kernel_scf)
2043 safe_deallocate_a(kern_1_scf)
2044 safe_deallocate_a(x_scf)
2045 safe_deallocate_a(y_scf)
2046
2047!!!!END KERNEL CONSTRUCTION
2048
2049 safe_allocate(karrayfour(1:2, 1:nker1, 1:nker2, 1:nker3/nproc))
2050
2051 ! if (iproc == 0) print *,"Do a 3D PHalFFT for the kernel"
2052
2053 call kernelfft(nfft1,nfft2,nfft3,nker1,nker2,nker3,nproc,iproc,karray,karrayfour,comm)
2054
2055 !Reconstruct the real kernel FFT
2056 do i3 = 1,n3k/nproc
2057 do i2 = 1,n2k
2058 do i1 = 1,n1k
2059 karrayoutloc(i1,i2,i3)=karrayfour(1,i1,i2,i3)
2060 end do
2061 end do
2062 end do
2063
2064 !De-allocations
2065 safe_deallocate_a(karray)
2066 safe_deallocate_a(karrayfour)
2067 pop_sub(par_build_kernel)
2068 end subroutine par_build_kernel
2069 !!***
2070
2071 ! -------------------------------------------------------------------------
2072
2073 subroutine gequad(n_gauss, p_gauss, w_gauss, ur_gauss, dr_gauss, acc_gauss)
2074 integer, intent(in) :: n_gauss
2075 real(real64), intent(out) :: p_gauss(:)
2076 real(real64), intent(out) :: w_gauss(:)
2077 real(real64), intent(out) :: ur_gauss
2078 real(real64), intent(out) :: dr_gauss
2079 real(real64), intent(out) :: acc_gauss
2080
2081 integer :: iunit, i, idx
2082
2083 push_sub(gequad)
2084
2085 ur_gauss = 1.0_8
2086 dr_gauss = 1.0e-08_8
2087 acc_gauss = 1.0e-08_8
2088
2089 iunit = io_open(trim(conf%share)//'/gequad.data', action = 'read', status = 'old')
2090
2091 do i = 1, n_gauss
2092 read(iunit, *) idx, p_gauss(i), w_gauss(i)
2093 end do
2094
2095 call io_close(iunit)
2096
2097 pop_sub(gequad)
2098 end subroutine gequad
2099
2100end module poisson_isf_oct_m
2101
2102!! Local Variables:
2103!! mode: f90
2104!! coding: utf-8
2105!! End:
double log(double __x) __attribute__((__nothrow__
double exp(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
Definition: io.F90:93
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine symm_ind(nd1, nd2, nd3, i1, i2, i3, ind)
subroutine zarray_in(n01, n02, n03, nd1, nd2, nd3, density, zarray)
subroutine karrayhalf_in(n01, n02, n03, n1k, n2k, n3k, nfft1, nfft2, nfft3, nd1, nd2, nd3, kernel, karrayhalf)
subroutine pconvxc_off(m1, m2, m3, n1, n2, n3, nd1, nd2, nd3, md1, md2, md3, iproc, nproc, rhopot, kernelloc, hgrid, comm)
subroutine, public poisson_isf_end(this)
subroutine build_kernel(n01, n02, n03, nfft1, nfft2, nfft3, hgrid, itype_scf, karrayout)
integer, parameter serial
subroutine norm_ind(nd1, nd2, nd3, i1, i2, i3, ind)
subroutine enterdensity(rhopot, m1, m2, m3, md1, md2, md3, iproc, nproc, zf)
subroutine par_psolver_kernel(n01, n02, n03, nd1, nd2, nd3, hgrid, kernelLOC, rhopot, iproc, nproc, comm)
integer, parameter n_cnf
integer, parameter world
subroutine kernel_application(n1, n2, n3, nd1h, nd2, nd3, nfft1, nfft2, nfft3, zarray, karray, inzee)
subroutine, public poisson_isf_init(this, namespace, mesh, cube, all_nodes_comm, init_world)
subroutine, public poisson_isf_solve(this, mesh, cube, pot, rho, all_nodes, sm)
subroutine gequad(n_gauss, p_gauss, w_gauss, ur_gauss, dr_gauss, acc_gauss)
subroutine calculate_dimensions(n01, n02, n03, nfft1, nfft2, nfft3)
subroutine par_build_kernel(n01, n02, n03, nfft1, nfft2, nfft3, n1k, n2k, n3k, hgrid, itype_scf, iproc, nproc, comm, karrayoutLOC)
integer, parameter domain
subroutine zarray_out(n01, n02, n03, nd1, nd2, nd3, rhopot, zarray, factor)
subroutine par_calculate_dimensions(n01, n02, n03, m1, m2, m3, n1, n2, n3, md1, md2, md3, nd1, nd2, nd3, nproc)
subroutine psolver_kernel(n01, n02, n03, nfft1, nfft2, nfft3, hgrid, karray, rhopot)
subroutine kernel_recon(n1k, n2k, n3k, nfft1, nfft2, nfft3, nd1, nd2, nd3, zarray, karray)
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
These routines are part of the ISF poisson solver, eventually they will be integrated with the other ...
Definition: sgfft.F90:96
int true(void)