This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
poisson.f90
executable file
·2171 lines (1920 loc) · 74.9 KB
/
poisson.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!################################################################################
!This file is part of Incompact3d.
!
!Incompact3d
!Copyright (c) 2012 Eric Lamballais and Sylvain Laizet
!
! Incompact3d is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation.
!
! Incompact3d is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with the code. If not, see <http://www.gnu.org/licenses/>.
!-------------------------------------------------------------------------------
!-------------------------------------------------------------------------------
! We kindly request that you cite Incompact3d in your publications and
! presentations. The following citations are suggested:
!
! 1-Laizet S. & Lamballais E., 2009, High-order compact schemes for
! incompressible flows: a simple and efficient method with the quasi-spectral
! accuracy, J. Comp. Phys., vol 228 (15), pp 5989-6015
!
! 2-Laizet S. & Li N., 2011, Incompact3d: a powerful tool to tackle turbulence
! problems with up to 0(10^5) computational cores, Int. J. of Numerical
! Methods in Fluids, vol 67 (11), pp 1735-1757
!################################################################################
module decomp_2d_poisson
use decomp_2d
use decomp_2d_fft
use param
use variables
implicit none
private ! Make everything private unless declared public
! real(mytype), private, parameter :: PI = 3.14159265358979323846_mytype
#ifdef DOUBLE_PREC
real(mytype), parameter :: epsilon = 1.e-16
#else
real(mytype), parameter :: epsilon = 1.e-8
#endif
! boundary conditions
integer, save :: bcx, bcy, bcz
! decomposition object for physical space
TYPE(DECOMP_INFO), save :: ph
! decomposition object for spectral space
TYPE(DECOMP_INFO), save :: sp
! store sine/cosine factors
real(mytype), save, allocatable, dimension(:) :: az,bz
real(mytype), save, allocatable, dimension(:) :: ay,by
real(mytype), save, allocatable, dimension(:) :: ax,bx
! wave numbers
complex(mytype), save, allocatable, dimension(:,:,:) :: kxyz
!wave numbers for stretching in a pentadiagonal matrice
complex(mytype), save, allocatable, dimension(:,:,:,:) :: a,a2,a3
! work arrays,
! naming convention: cw (complex); rw (real);
! 1 = X-pencil; 2 = Y-pencil; 3 = Z-pencil
real(mytype), allocatable, dimension(:,:,:) :: rw1,rw1b,rw2,rw2b,rw3
complex(mytype), allocatable, dimension(:,:,:) :: cw1,cw1b,cw2,cw22,cw2b,cw2c
! underlying FFT library only needs to be initialised once
logical, save :: fft_initialised = .false.
public :: decomp_2d_poisson_stg, decomp_2d_poisson_init, &
decomp_2d_poisson_finalize
! For staggered mesh where main variables are defined in the centre of
! control volumes while boundary conditions are defined on interfaces
interface decomp_2d_poisson_stg
module procedure poisson
end interface
contains
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Initialise Poisson solver for given boundary conditions
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine decomp_2d_poisson_init(bcx1, bcy1, bcz1)
implicit none
integer, intent(IN) :: bcx1, bcy1, bcz1
integer :: nx, ny, nz, i
bcx = bcx1
bcy = bcy1
bcz = bcz1
nx = nx_global
ny = ny_global
nz = nz_global
! pressure-grid having 1 fewer point for non-periodic directions
if (bcx==1) nx=nx-1
if (bcy==1) ny=ny-1
if (bcz==1) nz=nz-1
allocate(ax(nx),bx(nx))
allocate(ay(ny),by(ny))
allocate(az(nz),bz(nz))
call abxyz(ax,ay,az,bx,by,bz,nx,ny,nz,bcx,bcy,bcz)
call decomp_info_init(nx, ny, nz, ph)
call decomp_info_init(nx, ny, nz/2+1, sp)
! allocate work space
if (bcx==0 .and. bcy==0 .and. bcz==0) then
allocate(cw1(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(kxyz(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(a(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a2(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a3(sp%yst(1):sp%yen(1),ny,sp%yst(3):sp%yen(3),5))
else if (bcx==1 .and. bcy==0 .and. bcz==0) then
allocate(cw1(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(cw1b(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(rw1(ph%xst(1):ph%xen(1),ph%xst(2):ph%xen(2), &
ph%xst(3):ph%xen(3)))
allocate(rw1b(ph%xst(1):ph%xen(1),ph%xst(2):ph%xen(2), &
ph%xst(3):ph%xen(3)))
allocate(rw2(ph%yst(1):ph%yen(1),ph%yst(2):ph%yen(2), &
ph%yst(3):ph%yen(3)))
allocate(kxyz(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(a(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a2(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a3(sp%yst(1):sp%yen(1),ny,sp%yst(3):sp%yen(3),5))
else if (bcx==0 .and. bcy==1 .and. bcz==0) then
allocate(rw2(ph%yst(1):ph%yen(1),ph%yst(2):ph%yen(2), &
ph%yst(3):ph%yen(3)))
allocate(rw2b(ph%yst(1):ph%yen(1),ph%yst(2):ph%yen(2), &
ph%yst(3):ph%yen(3)))
allocate(cw1(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(cw2(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(cw22(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(cw2b(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(cw2c(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(kxyz(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(a(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a2(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a3(sp%yst(1):sp%yen(1),ny,sp%yst(3):sp%yen(3),5))
else if (bcx==1 .and. bcy==1) then
allocate(cw1(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(cw1b(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(cw2(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(cw22(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(cw2b(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(cw2c(sp%yst(1):sp%yen(1),sp%yst(2):sp%yen(2), &
sp%yst(3):sp%yen(3)))
allocate(rw1(ph%xst(1):ph%xen(1),ph%xst(2):ph%xen(2), &
ph%xst(3):ph%xen(3)))
allocate(rw1b(ph%xst(1):ph%xen(1),ph%xst(2):ph%xen(2), &
ph%xst(3):ph%xen(3)))
allocate(rw2(ph%yst(1):ph%yen(1),ph%yst(2):ph%yen(2), &
ph%yst(3):ph%yen(3)))
allocate(rw2b(ph%yst(1):ph%yen(1),ph%yst(2):ph%yen(2), &
ph%yst(3):ph%yen(3)))
if (bcz==1) then
allocate(rw3(ph%zsz(1),ph%zsz(2),ph%zsz(3)))
end if
allocate(kxyz(sp%xst(1):sp%xen(1),sp%xst(2):sp%xen(2), &
sp%xst(3):sp%xen(3)))
allocate(a(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a2(sp%yst(1):sp%yen(1),ny/2,sp%yst(3):sp%yen(3),5))
allocate(a3(sp%yst(1):sp%yen(1),nym,sp%yst(3):sp%yen(3),5))
end if
call waves()
return
end subroutine decomp_2d_poisson_init
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Release memory used by Poisson solver
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine decomp_2d_poisson_finalize
implicit none
deallocate(ax,bx,ay,by,az,bz)
call decomp_info_finalize(ph)
call decomp_info_finalize(sp)
call decomp_2d_fft_finalize
fft_initialised = .false.
deallocate(kxyz)
if (bcx==0 .and. bcy==0 .and. bcz==0) then
deallocate(cw1)
else if (bcx==1 .and. bcy==0 .and. bcz==0) then
deallocate(cw1,cw1b,rw1,rw1b,rw2)
else if (bcx==0 .and. bcy==1 .and. bcz==0) then
deallocate(cw1,cw2,cw2b,rw2,rw2b)
else if (bcx==1 .and. bcy==1) then
deallocate(cw1,cw1b,cw2,cw2b,rw1,rw1b,rw2,rw2b)
if (bcz==1) then
deallocate(rw3)
end if
end if
return
end subroutine decomp_2d_poisson_finalize
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Top level wrapper
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine poisson(rhs, bcx, bcy, bcz)
implicit none
real(mytype), dimension(:,:,:), intent(INOUT) :: rhs
integer, intent(IN) :: bcx, bcy, bcz ! boundary conditions
integer :: i
if (bcx==0 .and. bcy==0 .and. bcz==0) then
call poisson_000(rhs)
else if (bcx==1 .and. bcy==0 .and. bcz==0) then
call poisson_100(rhs)
else if (bcx==0 .and. bcy==1 .and. bcz==0) then
call poisson_010(rhs)
else if (bcx==1 .and. bcy==1) then ! 110 & 111
call poisson_11x(rhs, bcz)
else
stop 'boundary condition not supported'
end if
return
end subroutine poisson
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Solving 3D Poisson equation with periodic B.C in all 3 dimensions
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine poisson_000(rhs)
use derivX
use derivY
use derivZ
! right-hand-side of Poisson as input
! solution of Poisson as output
real(mytype), dimension(:,:,:), intent(INOUT) :: rhs
integer, dimension(3) :: fft_start, fft_end, fft_size
complex(mytype) :: xyzk
complex(mytype) :: ytt,xtt,ztt,yt1,xt1,yt2,xt2
complex(mytype) :: xtt1,ytt1,ztt1,zt1,zt2
real(mytype) :: tmp1, tmp2,x ,y, z
integer :: nx,ny,nz, i,j,k
nx = nx_global
ny = ny_global
nz = nz_global
if (.not. fft_initialised) then
call decomp_2d_fft_init(PHYSICAL_IN_Z)
fft_initialised = .true.
end if
! compute r2c transform
call decomp_2d_fft_3d(rhs,cw1)
! normalisation
cw1 = cw1 / real(nx, kind=mytype) /real(ny, kind=mytype) &
/ real(nz, kind=mytype)
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
! post-processing in spectral space
! POST PROCESSING IN Z
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bz(k)+tmp2*az(k), &
tmp2*bz(k)-tmp1*az(k), kind=mytype)
! POST PROCESSING IN Y
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*by(j)+tmp2*ay(j), &
tmp2*by(j)-tmp1*ay(j), kind=mytype)
if (j.gt.(ny/2+1)) cw1(i,j,k)=-cw1(i,j,k)
! POST PROCESSING IN X
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bx(i)+tmp2*ax(i), &
tmp2*bx(i)-tmp1*ax(i), kind=mytype)
if (i.gt.(nx/2+1)) cw1(i,j,k)=-cw1(i,j,k)
! Solve Poisson
tmp1=real(kxyz(i,j,k), kind=mytype)
tmp2=aimag(kxyz(i,j,k))
! CANNOT DO A DIVISION BY ZERO
if ((tmp1.lt.epsilon).or.(tmp2.lt.epsilon)) then
cw1(i,j,k)=0._mytype
! print *,'DIV 0',i,j,k,epsilon
else
cw1(i,j,k)=cmplx( real(cw1(i,j,k), kind=mytype) / (-tmp1), &
aimag(cw1(i,j,k))/(-tmp2), kind=mytype)
end if
!Print result in spectal space after Poisson
! if (abs(out(i,j,k)) > 1.0e-4) then
! write(*,*) 'AFTER',i,j,k,out(i,j,k),xyzk
! end if
! post-processing backward
! POST PROCESSING IN Z
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bz(k)-tmp2*az(k), &
-tmp2*bz(k)-tmp1*az(k), kind=mytype)
! POST PROCESSING IN Y
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*by(j)+tmp2*ay(j), &
tmp2*by(j)-tmp1*ay(j), kind=mytype)
if (j.gt.(ny/2+1)) cw1(i,j,k)=-cw1(i,j,k)
! POST PROCESSING IN X
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bx(i)+tmp2*ax(i), &
-tmp2*bx(i)+tmp1*ax(i), kind=mytype)
if (i.gt.(nx/2+1)) cw1(i,j,k)=-cw1(i,j,k)
end do
end do
end do
! compute c2r transform
call decomp_2d_fft_3d(cw1,rhs)
! call decomp_2d_fft_finalize
return
end subroutine poisson_000
subroutine poisson_100(rhs)
implicit none
real(mytype), dimension(:,:,:), intent(INOUT) :: rhs
complex(mytype) :: xyzk
real(mytype) :: tmp1, tmp2, tmp3, tmp4
real(mytype) :: xx1,xx2,xx3,xx4,xx5,xx6,xx7,xx8
integer :: nx,ny,nz, i,j,k, itmp
100 format(1x,a8,3I4,2F12.6)
nx = nx_global - 1
ny = ny_global
nz = nz_global
! rhs is in Z-pencil but requires global operations in X
call transpose_z_to_y(rhs,rw2,ph)
call transpose_y_to_x(rw2,rw1,ph)
do k=ph%xst(3),ph%xen(3)
do j=ph%xst(2),ph%xen(2)
do i=1,nx/2
rw1b(i,j,k)=rw1(2*(i-1)+1,j,k)
enddo
do i=nx/2+1,nx
rw1b(i,j,k)=rw1(2*nx-2*i+2,j,k)
enddo
enddo
end do
call transpose_x_to_y(rw1b,rw2,ph)
call transpose_y_to_z(rw2,rhs,ph)
if (.not. fft_initialised) then
call decomp_2d_fft_init(PHYSICAL_IN_Z,nx,ny,nz)
fft_initialised = .true.
end if
! compute r2c transform
call decomp_2d_fft_3d(rhs,cw1)
! normalisation
cw1 = cw1 / real(nx, kind=mytype) /real(ny, kind=mytype) &
/ real(nz, kind=mytype)
#ifdef DEBUG
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
if (abs(cw1(i,j,k)) > 1.0e-4) then
write(*,100) 'START',i,j,k,cw1(i,j,k)
end if
end do
end do
end do
#endif
! post-processing in spectral space
! POST PROCESSING IN Z
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bz(k)+tmp2*az(k), &
tmp2*bz(k)-tmp1*az(k), kind=mytype)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'after z',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! POST PROCESSING IN Y
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*by(j)+tmp2*ay(j), &
tmp2*by(j)-tmp1*ay(j), kind=mytype)
if (j.gt.(ny/2+1)) cw1(i,j,k)=-cw1(i,j,k)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'after y',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! POST PROCESSING IN X
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
cw1b(1,j,k)=cw1(1,j,k)
do i = 2,nx
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
tmp3 = real(cw1(nx-i+2,j,k), kind=mytype)
tmp4 = aimag(cw1(nx-i+2,j,k))
xx1=tmp1*bx(i)/2._mytype
xx2=tmp1*ax(i)/2._mytype
xx3=tmp2*bx(i)/2._mytype
xx4=tmp2*ax(i)/2._mytype
xx5=tmp3*bx(i)/2._mytype
xx6=tmp3*ax(i)/2._mytype
xx7=tmp4*bx(i)/2._mytype
xx8=tmp4*ax(i)/2._mytype
cw1b(i,j,k) = cmplx(xx1+xx4+xx5-xx8,-xx2+xx3+xx6+xx7, &
kind=mytype)
end do
end do
end do
#ifdef DEBUG
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
if (abs(cw1b(i,j,k)) > 1.0e-4) then
write(*,100) 'after x',i,j,k,cw1b(i,j,k)
end if
end do
end do
end do
#endif
! Solve Poisson
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
!tmp1=real(zk2(k)+yk2(j)+xk2(i), kind=mytype)
!tmp2=aimag(zk2(k)+yk2(j)+xk2(i))
tmp1=real(kxyz(i,j,k), kind=mytype)
tmp2=aimag(kxyz(i,j,k))
!xyzk=cmplx(tmp1,tmp2, kind=mytype)
! CANNOT DO A DIVISION BY ZERO
if ((abs(tmp1).lt.epsilon).and.(abs(tmp2).lt.epsilon)) then
cw1b(i,j,k)=cmplx(0._mytype,0._mytype, kind=mytype)
end if
if ((abs(tmp1).lt.epsilon).and.(abs(tmp2).ge.epsilon)) then
cw1b(i,j,k)=cmplx(0._mytype, &
aimag(cw1b(i,j,k))/(-tmp2), kind=mytype)
end if
if ((abs(tmp1).ge.epsilon).and.(abs(tmp2).lt.epsilon)) then
cw1b(i,j,k)=cmplx( real(cw1b(i,j,k), kind=mytype) &
/(-tmp1), 0._mytype, kind=mytype)
end if
if ((abs(tmp1).ge.epsilon).and.(abs(tmp2).ge.epsilon)) then
cw1b(i,j,k)=cmplx( real(cw1b(i,j,k), kind=mytype) &
/(-tmp1), &
aimag(cw1b(i,j,k))/(-tmp2), kind=mytype)
end if
#ifdef DEBUG
if (abs(cw1b(i,j,k)) > 1.0e-4) &
write(*,100) 'AFTER',i,j,k,cw1b(i,j,k)
#endif
end do
end do
end do
! post-processing backward
! POST PROCESSING IN X
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
cw1(1,j,k)=cw1b(1,j,k)
do i = 2,nx
tmp1 = real(cw1b(i,j,k), kind=mytype)
tmp2 = aimag(cw1b(i,j,k))
tmp3 = real(cw1b(nx-i+2,j,k), kind=mytype)
tmp4 = aimag(cw1b(nx-i+2,j,k))
xx1=tmp1*bx(i)
xx2=tmp1*ax(i)
xx3=tmp2*bx(i)
xx4=tmp2*ax(i)
xx5=tmp3*bx(i)
xx6=tmp3*ax(i)
xx7=tmp4*bx(i)
xx8=tmp4*ax(i)
cw1(i,j,k) = cmplx(xx1-xx4+xx6+xx7,-(-xx2-xx3+xx5-xx8), &
kind=mytype)
end do
end do
end do
#ifdef DEBUG
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
if (abs(cw1(i,j,k)) > 1.0e-4) then
write(*,100) 'AFTER X',i,j,k,cw1(i,j,k)
end if
end do
end do
end do
#endif
! POST PROCESSING IN Y
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*by(j)-tmp2*ay(j), &
tmp2*by(j)+tmp1*ay(j), kind=mytype)
if (j.gt.(ny/2+1)) cw1(i,j,k)=-cw1(i,j,k)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'AFTER Y',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! POST PROCESSING IN Z
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bz(k)-tmp2*az(k), &
tmp2*bz(k)+tmp1*az(k), kind=mytype)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'END',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! compute c2r transform
call decomp_2d_fft_3d(cw1,rhs)
! rhs is in Z-pencil but requires global operations in X
call transpose_z_to_y(rhs,rw2,ph)
call transpose_y_to_x(rw2,rw1,ph)
do k=ph%xst(3),ph%xen(3)
do j=ph%xst(2),ph%xen(2)
do i=1,nx/2
rw1b(2*i-1,j,k)=rw1(i,j,k)
enddo
do i=1,nx/2
rw1b(2*i,j,k)=rw1(nx-i+1,j,k)
enddo
enddo
end do
call transpose_x_to_y(rw1b,rw2,ph)
call transpose_y_to_z(rw2,rhs,ph)
! call decomp_2d_fft_finalize
return
end subroutine poisson_100
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Solving 3D Poisson equation: Neumann in Y; periodic in X & Z
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine poisson_010(rhs)
implicit none
real(mytype), dimension(:,:,:), intent(INOUT) :: rhs
complex(mytype) :: xyzk
real(mytype) :: tmp1, tmp2, tmp3, tmp4
real(mytype) :: xx1,xx2,xx3,xx4,xx5,xx6,xx7,xx8
integer :: nx,ny,nz, i,j,k
100 format(1x,a8,3I4,2F12.6)
nx = nx_global
ny = ny_global - 1
nz = nz_global
! rhs is in Z-pencil but requires global operations in Y
call transpose_z_to_y(rhs,rw2,ph)
do k=ph%yst(3),ph%yen(3)
do i=ph%yst(1),ph%yen(1)
do j=1,ny/2
rw2b(i,j,k)=rw2(i,2*(j-1)+1,k)
enddo
do j=ny/2+1,ny
rw2b(i,j,k)=rw2(i,2*ny-2*j+2,k)
enddo
enddo
end do
call transpose_y_to_z(rw2b,rhs,ph)
if (.not. fft_initialised) then
call decomp_2d_fft_init(PHYSICAL_IN_Z,nx,ny,nz)
fft_initialised = .true.
end if
! compute r2c transform
call decomp_2d_fft_3d(rhs,cw1)
! normalisation
cw1 = cw1 / real(nx, kind=mytype) /real(ny, kind=mytype) &
/ real(nz, kind=mytype)
#ifdef DEBUG
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
if (abs(cw1(i,j,k)) > 1.0e-4) then
write(*,100) 'START',i,j,k,cw1(i,j,k)
end if
end do
end do
end do
#endif
! post-processing in spectral space
! POST PROCESSING IN Z
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bz(k)+tmp2*az(k), &
tmp2*bz(k)-tmp1*az(k), kind=mytype)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'after z',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! POST PROCESSING IN X
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bx(i)+tmp2*ax(i), &
tmp2*bx(i)-tmp1*ax(i), kind=mytype)
if (i.gt.(nx/2+1)) cw1(i,j,k)=-cw1(i,j,k)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'after x',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! POST PROCESSING IN Y
! NEED TO BE IN Y PENCILS!!!!!!!!!!!!!!!
call transpose_x_to_y(cw1,cw2,sp)
do k = sp%yst(3), sp%yen(3)
do i = sp%yst(1), sp%yen(1)
cw2b(i,1,k)=cw2(i,1,k)
do j = 2,ny
tmp1 = real(cw2(i,j,k), kind=mytype)
tmp2 = aimag(cw2(i,j,k))
tmp3 = real(cw2(i,ny-j+2,k), kind=mytype)
tmp4 = aimag(cw2(i,ny-j+2,k))
xx1=tmp1*by(j)/2._mytype
xx2=tmp1*ay(j)/2._mytype
xx3=tmp2*by(j)/2._mytype
xx4=tmp2*ay(j)/2._mytype
xx5=tmp3*by(j)/2._mytype
xx6=tmp3*ay(j)/2._mytype
xx7=tmp4*by(j)/2._mytype
xx8=tmp4*ay(j)/2._mytype
cw2b(i,j,k) = cmplx(xx1+xx4+xx5-xx8,-xx2+xx3+xx6+xx7, &
kind=mytype)
end do
end do
end do
#ifdef DEBUG
do k = sp%yst(3), sp%yen(3)
do j = sp%yst(2), sp%yen(2)
do i = sp%yst(1), sp%yen(1)
if (abs(cw2b(i,j,k)) > 1.0e-4) then
write(*,100) 'after y',i,j,k,cw2b(i,j,k)
print *,kxyz(i,j,k)
end if
end do
end do
end do
#endif
if (istret==0) then
! Solve Poisson
! doing wave number division in Y-pencil
do k = sp%yst(3), sp%yen(3)
do j = sp%yst(2), sp%yen(2)
do i = sp%yst(1), sp%yen(1)
!tmp1=real(zk2(k)+yk2(j)+xk2(i), kind=mytype)
!tmp2=aimag(zk2(k)+yk2(j)+xk2(i))
tmp1=real(kxyz(i,j,k), kind=mytype)
tmp2=aimag(kxyz(i,j,k))
!xyzk=cmplx(tmp1,tmp2, kind=mytype)
!CANNOT DO A DIVISION BY ZERO
if ((abs(tmp1).lt.epsilon).and.(abs(tmp2).lt.epsilon)) then
cw2b(i,j,k)=cmplx(0._mytype,0._mytype, kind=mytype)
end if
if ((abs(tmp1).lt.epsilon).and.(abs(tmp2).ge.epsilon)) then
cw2b(i,j,k)=cmplx(0._mytype, &
aimag(cw2b(i,j,k))/(-tmp2), kind=mytype)
end if
if ((abs(tmp1).ge.epsilon).and.(abs(tmp2).lt.epsilon)) then
cw2b(i,j,k)=cmplx( real(cw2b(i,j,k), kind=mytype) &
/(-tmp1), 0._mytype, kind=mytype)
end if
if ((abs(tmp1).ge.epsilon).and.(abs(tmp2).ge.epsilon)) then
cw2b(i,j,k)=cmplx( real(cw2b(i,j,k), kind=mytype) &
/(-tmp1), &
aimag(cw2b(i,j,k))/(-tmp2), kind=mytype)
end if
end do
end do
end do
else
call matrice_refinement()
! do k = sp%yst(3), sp%yen(3)
! do j = 1,ny/2
! do i = sp%yst(1), sp%yen(1)
! print *,i,j,k,a(i,j,k,3)
!! if (nrank.le.1) print *,i,j,k,a(i,j,k,3)
!! if (nrank.gt.1) print *,i+4,j,k,a(i,j,k,3)
! enddo
! enddo
! enddo
if (istret.ne.3) then
cw2(:,:,:)=0.;cw2c(:,:,:)=0.
do k = sp%yst(3), sp%yen(3)
do j = 1,ny/2
do i = sp%yst(1), sp%yen(1)
cw2(i,j,k)=cw2b(i,2*j-1,k)
cw2c(i,j,k)=cw2b(i,2*j,k)
enddo
enddo
enddo
! do k = sp%yst(3), sp%yen(3)
! do j = 1,ny/2
! do i = sp%yst(1), sp%yen(1)
! if (abs(cw2(i,j,k)) > 1.0e-4) then
! write(*,*) 'before IN',i,j,k,cw2(i,j,k)!*2.
!! end if
! end do
! end do
! end do
call inversion5_v1(a,cw2,sp)
call inversion5_v1(a2,cw2c,sp)
! cw2(1,1,1)=cw2(1,1,1)*0.5
! do k = sp%yst(3), sp%yen(3)
! do j = 1,ny/2
! do i = sp%yst(1), sp%yen(1)
! if (abs(cw2c(i,j,k)) > 1.0e-4) then
! write(*,*) 'after IN',i,j,k,cw2c(i,j,k)!*2.
! end if
! end do
! end do
! end do
cw2b(:,:,:)=0.
do k=sp%yst(3), sp%yen(3)
do j=1,ny-1,2
do i=sp%yst(1), sp%yen(1)
cw2b(i,j,k)=cw2(i,(j+1)/2,k)
enddo
enddo
do j=2,ny,2
do i=sp%yst(1), sp%yen(1)
cw2b(i,j,k)=cw2c(i,j/2,k)
enddo
enddo
enddo
!do k=sp%yst(3), sp%yen(3)
!do i=sp%yst(1), sp%yen(1)
! if ((xkx(i)==0).and.(zkz(k)==0)) then
! ! cw2b(i,1,1)=0.
! ! cw2b(i,ny,1)=0.
! endif
!enddo
!enddo
else
do k = sp%yst(3), sp%yen(3)
do j = 1,ny
do i = sp%yst(1), sp%yen(1)
cw2(i,j,k)=cw2b(i,j,k)
enddo
enddo
enddo
call inversion5_v2(a3,cw2,sp)
do k = sp%yst(3), sp%yen(3)
do j = 1,ny
do i = sp%yst(1), sp%yen(1)
cw2b(i,j,k)=cw2(i,j,k)
enddo
enddo
enddo
endif
endif
! print *,nrank, sp%yst(3),sp%yen(3),sp%yst(1),sp%yen(1)
!we are in Y pencil
do k = sp%yst(3), sp%yen(3)
do i = sp%yst(1), sp%yen(1)
if ((i==nx/2+1).and.(k==nz/2+1)) then
cw2b(i,:,k)=0.
endif
enddo
enddo
#ifdef DEBUG
do k = sp%yst(3), sp%yen(3)
do j = sp%yst(2), sp%yen(2)
do i = sp%yst(1), sp%yen(1)
if (abs(cw2b(i,j,k)) > 1.0e-4) then
write(*,100) 'AFTER',i,j,k,cw2b(i,j,k)
print *,kxyz(i,j,k)
end if
end do
end do
end do
#endif
! post-processing backward
! POST PROCESSING IN Y
do k = sp%yst(3), sp%yen(3)
do i = sp%yst(1), sp%yen(1)
cw2(i,1,k)=cw2b(i,1,k)
do j = 2,ny
tmp1 = real(cw2b(i,j,k), kind=mytype)
tmp2 = aimag(cw2b(i,j,k))
tmp3 = real(cw2b(i,ny-j+2,k), kind=mytype)
tmp4 = aimag(cw2b(i,ny-j+2,k))
xx1=tmp1*by(j)
xx2=tmp1*ay(j)
xx3=tmp2*by(j)
xx4=tmp2*ay(j)
xx5=tmp3*by(j)
xx6=tmp3*ay(j)
xx7=tmp4*by(j)
xx8=tmp4*ay(j)
cw2(i,j,k) = cmplx(xx1-xx4+xx6+xx7,-(-xx2-xx3+xx5-xx8), &
kind=mytype)
end do
end do
end do
! Back to X-pencil
call transpose_y_to_x(cw2,cw1,sp)
#ifdef DEBUG
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
if (abs(cw1(i,j,k)) > 1.0e-4) then
write(*,100) 'AFTER Y',i,j,k,cw1(i,j,k)
end if
end do
end do
end do
#endif
! POST PROCESSING IN X
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bx(i)-tmp2*ax(i), &
tmp2*bx(i)+tmp1*ax(i), kind=mytype)
if (i.gt.(nx/2+1)) cw1(i,j,k)=-cw1(i,j,k)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'AFTER X',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! POST PROCESSING IN Z
do k = sp%xst(3),sp%xen(3)
do j = sp%xst(2),sp%xen(2)
do i = sp%xst(1),sp%xen(1)
tmp1 = real(cw1(i,j,k), kind=mytype)
tmp2 = aimag(cw1(i,j,k))
cw1(i,j,k) = cmplx(tmp1*bz(k)-tmp2*az(k), &
tmp2*bz(k)+tmp1*az(k), kind=mytype)
#ifdef DEBUG
if (abs(cw1(i,j,k)) > 1.0e-4) &
write(*,100) 'END',i,j,k,cw1(i,j,k)
#endif
end do
end do
end do
! compute c2r transform, back to physical space
call decomp_2d_fft_3d(cw1,rhs)
! rhs is in Z-pencil but requires global operations in Y
call transpose_z_to_y(rhs,rw2,ph)
do k=ph%yst(3),ph%yen(3)
do i=ph%yst(1),ph%yen(1)
do j=1,ny/2
rw2b(i,2*j-1,k)=rw2(i,j,k)
enddo
do j=1,ny/2