-
Notifications
You must be signed in to change notification settings - Fork 1
/
PoissonSolverMultigrid_good.f90
1936 lines (1518 loc) · 46.6 KB
/
PoissonSolverMultigrid_good.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
MODULE LevelType
TYPE :: LevelStruct
INTEGER:: ndim
INTEGER:: downsweep
INTEGER:: upsweep
INTEGER:: PlaneMaxDim
INTEGER:: LineMaxDim
INTEGER:: BuffElements
REAL*8 :: dVol
REAL*8,pointer::x(:)
REAL*8,pointer::y(:)
REAL*8,pointer::z(:)
REAL*8,pointer::rhs(:,:,:)
REAL*8,pointer::phi(:,:,:)
REAL*8,pointer::eps(:,:,:,:)
REAL*8,pointer::err(:,:,:)
REAL*8,pointer::PlaneBuffRecv(:,:)
REAL*8,pointer::LineBuffRecV(:,:,:)
REAL*8,pointer::PointBuffRecV(:,:,:)
INTEGER, pointer:: nElements(:)
INTEGER, pointer:: nb(:)
INTEGER, pointer:: X1(:)
INTEGER, pointer:: X2(:)
INTEGER, pointer:: Y1(:)
INTEGER, pointer:: Y2(:)
INTEGER, pointer:: Z1(:)
INTEGER, pointer:: Z2(:)
INTEGER, pointer:: RecV(:,:)
INTEGER, pointer:: SenD(:,:)
INTEGER, pointer:: PlanesSend(:,:)
INTEGER, pointer:: X1Plane(:,:)
INTEGER, pointer:: X2Plane(:,:)
INTEGER, pointer:: Y1Plane(:,:)
INTEGER, pointer:: Y2Plane(:,:)
INTEGER, pointer:: Z1Plane(:,:)
INTEGER, pointer:: Z2Plane(:,:)
INTEGER, pointer:: RecV1(:,:,:)
INTEGER, pointer:: SenD1(:,:,:)
INTEGER, pointer:: LinesSend(:,:,:)
INTEGER, pointer:: RecV2(:,:,:)
INTEGER, pointer:: SenD2(:,:,:)
INTEGER, pointer:: PointsSend(:,:,:)
INTEGER, pointer:: X1Line(:,:,:)
INTEGER, pointer:: X2Line(:,:,:)
INTEGER, pointer:: Y1Line(:,:,:)
INTEGER, pointer:: Y2Line(:,:,:)
INTEGER, pointer:: Z1Line(:,:,:)
INTEGER, pointer:: Z2Line(:,:,:)
INTEGER, pointer:: XPoint(:,:,:)
INTEGER, pointer:: YPoint(:,:,:)
INTEGER, pointer:: ZPoint(:,:,:)
END TYPE LevelStruct
END Module LevelType
PROGRAM POISSON_MGRID
Use LevelType
IMPLICIT NONE
INCLUDE 'mpif.h'
INTEGER MYID,TOTPS,IERR,stat(MPI_STATUS_SIZE),NCPUS
!########## GAUSS UNITS IMPLIED ############
INTEGER I,J,K,L,M,P,NDIM,ITER,MAXITER,MAXDIM,Ip,Im,Jp,Jm,Kp,Km,NDIM2H,NDIM4H,NLEVELS,Jnow
INTEGER :: ndim1,i1,j1,k1,PlaneMaxDim,NBb,II,JJ,KK,MM
REAL*8 A2B,lamCoarse
REAL*8 Xmin,Xmax,Ymin,Ymax,Zmin,Zmax,dhX,dhY,dhZ,summ_prev,summE,summEprev,summE1
REAL*8 coeff,d_map,const,R(6),summ,Xoffset,Yoffset,Zoffset,Rpol,summRho,vol,dvol
REAL*8, DIMENSION(:,:,:), ALLOCATABLE :: PHI,RHO
REAL*8, DIMENSION(:,:,:), ALLOCATABLE :: Rk,Rkt,Rk1,Rkt1,Rkp1,Pk,PkT,Rtemp,Rk2H,PHI2H,RHO2H,Rk4H,PHI4H,Rtemp2H
REAL*8, DIMENSION(:,:,:,:), ALLOCATABLE :: EPS
REAL*8 summ_even,summ_odd,omega,coeff1,coeff2,delta,Pi,summHartree,summHartree1,Xtemp,Ytemp,lamN,Momega
REAL*8 eps1,eps2,eps3,eps4,eps5,eps6,Ztemp,temp1,temp2,temp3,temp,alphaK,betaK,dVol2H,summRhoTemp
REAL*8 lamN1,lamN2,summRes(1000)
REAL t_start(2),t_stop(2),result
REAL, DIMENSION(:), ALLOCATABLE :: X,Y,Z,Xmid,Ymid,Zmid,X2H,Y2H,Z2H
PARAMETER(A2B=1./0.529177249)
PARAMETER(NDIM=120)
PARAMETER(NDIM2H=NDIM/2)
PARAMETER(NDIM4H=NDIM2H/2)
PARAMETER(Xoffset=4.)
PARAMETER(Yoffset=12.)
PARAMETER(Zoffset=5.)
PARAMETER(Rpol=3.)
PARAMETER(MAXITER=22242)
TYPE (LevelStruct), DIMENSION(:), ALLOCATABLE ::Level
open (UNIT=85, FILE='eps', STATUS='unknown')
open (UNIT=86, FILE='contour', STATUS='unknown')
open (UNIT=84, FILE='potential', STATUS='unknown')
open (UNIT=83, FILE='field_magniture', STATUS='unknown')
open (UNIT=101, FILE='field', STATUS='unknown')
open (UNIT=103, FILE='ratioHelm_par.dat', STATUS='unknown')
open (UNIT=104, FILE='residiual.dat', STATUS='unknown')
Pi=4.*Atan(1.)
call mpi_init( IERR)
call MPI_COMM_RANK(MPI_COMM_WORLD,MYID,IERR)
call MPI_COMM_SIZE(MPI_COMM_WORLD,NCPUS,IERR)
ALLOCATE(EPS(6,NDIM,NDIM,NDIM))
ALLOCATE(RHO(NDIM,NDIM,NDIM))
ALLOCATE(PHI(NDIM,NDIM,NDIM))
ALLOCATE(X(NDIM))
ALLOCATE(Y(NDIM))
ALLOCATE(Z(NDIM))
lamN1=0.99
lamN2=0.99
lamCoarse=0.92
!lamN1=0.993
!lamN2=0.993
!lamCoarse=0.92
call GetNLevels(NDIM,nlevels)
! nlevels=2
! nlevels=3
ALLOCATE(Level(nlevels))
ndim1=ndim
DO i=nlevels,1,-1
write(*,*) "NLEVELS=",nlevels,ndim1
Level(i)%ndim=ndim1
ALLOCATE(level(i)%x(ndim1))
ALLOCATE(level(i)%y(ndim1))
ALLOCATE(level(i)%z(ndim1))
ALLOCATE(level(i)%rhs(ndim1,ndim1,ndim1))
ALLOCATE(level(i)%err(ndim1,ndim1,ndim1))
ALLOCATE(level(i)%phi(ndim1,ndim1,ndim1))
ALLOCATE(level(i)%eps(6,ndim1,ndim1,ndim1))
ALLOCATE(level(i)%X1(NCPUS))
ALLOCATE(level(i)%X2(NCPUS))
ALLOCATE(level(i)%Y1(NCPUS))
ALLOCATE(level(i)%Y2(NCPUS))
ALLOCATE(level(i)%Z1(NCPUS))
ALLOCATE(level(i)%Z2(NCPUS))
ALLOCATE(level(i)%Nb(NCPUS))
ALLOCATE(level(i)%Nelements(NCPUS))
ALLOCATE(level(i)%RecV(NCPUS,6))
ALLOCATE(level(i)%SenD(NCPUS,6))
ALLOCATE(level(i)%PlanesSend(NCPUS,6))
ALLOCATE(level(i)%X1Plane(NCPUS,6))
ALLOCATE(level(i)%X2Plane(NCPUS,6))
ALLOCATE(level(i)%Y1Plane(NCPUS,6))
ALLOCATE(level(i)%Y2Plane(NCPUS,6))
ALLOCATE(level(i)%Z1Plane(NCPUS,6))
ALLOCATE(level(i)%Z2Plane(NCPUS,6))
ALLOCATE(level(i)%RecV1(NCPUS,6,4))
ALLOCATE(level(i)%SenD1(NCPUS,6,4))
ALLOCATE(level(i)%LinesSend(NCPUS,6,4))
ALLOCATE(level(i)%PointsSend(NCPUS,6,4))
ALLOCATE(level(i)%X1Line(4,NCPUS,6))
ALLOCATE(level(i)%X2Line(4,NCPUS,6))
ALLOCATE(level(i)%Y1Line(4,NCPUS,6))
ALLOCATE(level(i)%Y2Line(4,NCPUS,6))
ALLOCATE(level(i)%Z1Line(4,NCPUS,6))
ALLOCATE(level(i)%Z2Line(4,NCPUS,6))
ALLOCATE(level(i)%RecV2(NCPUS,6,4))
ALLOCATE(level(i)%SenD2(NCPUS,6,4))
ALLOCATE(level(i)%PointsSend(NCPUS,6,4))
ALLOCATE(level(i)%XPoint(4,NCPUS,6))
ALLOCATE(level(i)%YPoint(4,NCPUS,6))
ALLOCATE(level(i)%ZPoint(4,NCPUS,6))
level(i)%phi=0.
level(i)%err=0.
level(i)%rhs=0.
level(i)%downsweep=3
level(i)%upsweep=4
ndim1=ndim1/2
ENDDO
call INITIALIZE(MYID,NCPUS,LEVEL(nlevels),LEVEL(nlevels),nlevels,nlevels)
DO i=nlevels-1,1,-1
call INITIALIZE(MYID,NCPUS,LEVEL(i),LEVEL(i+1),i,nlevels)
ENDDO
DO i=nlevels,1,-1
call GetNelements(NCPUS,LEVEL(i))
ALLOCATE(level(i)%PlaneBuffRecV(level(i)%PlaneMaxDim+7,level(i)%NB(MYID+1)))
ALLOCATE(level(i)%LineBuffRecV(level(i)%LineMaxDim+7,level(i)%NB(MYID+1),4))
ALLOCATE(level(i)%PointBuffRecV(5,level(i)%NB(MYID+1),4))
level(i)%BUFFElements=MAXVAL(level(i)%NElements)
IF(MYID.eq.0) write(*,*) "PlaneMaxDim,levelN ", Level(i)%PlaneMaxDim,level(i)%BUFFELEMENTS,i
ENDDO
call ReadData(vol,LEVEL(nlevels))
DO i=nlevels-1,1,-1
call propagateXYZ(LEVEL(I+1),LEVEL(I))
ENDDO
DO i=nlevels,1,-1
call fillEps(level(i),Xoffset,Yoffset,Zoffset)
ENDDO
summ=0.
summ_prev=0.
level(nlevels)%rhs(:,:,:)=level(nlevels)%rhs(:,:,:)*level(nlevels)%eps(2,:,:,:)
level(nlevels)%phi=0.
!DO I=1,NDIM
! DO J=1,NDIM
! DO K=1,NDIM
! IF(MYID.eq.0) write(84,*) level(nlevels)%rhs(i,j,k)
!ENDDO
!ENDDO
!ENDDO
123 continue
write(*,*) "**********************************"
call etime (t_start,result)
Jnow=0
! call MPI_BARRIER(MPI_COMM_WORLD,IERR)
DO J=1,90
call MPI_BARRIER(MPI_COMM_WORLD,IERR)
call GaussSeidelSmoothSOR(LEVEL(nlevels),level(nlevels)%downsweep,Vol,1,1,lamN1,NCPUS,MYID,MPI_COMM_WORLD)
call computeDefectpar(MYID,NCPUS,LeveL(nlevels),MPI_COMM_WORLD,summE,1)
call MPI_ALLREDUCE(summE,summE1,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,IERR)
summRes(J)=summE1
IF(DSQRT(summRes(J)).le.1.d-5) goto 122
IF(MYID.eq.0) write(*,*) DSQRT(summE1),J
DO I=Nlevels-1,1,-1
level(i)%phi=0.
if(i.eq.1) then
call restrictDefectpar(MYID,Level(i+1),Level(i),MPI_COMM_WORLD)
call GaussSeidelSmoothSOR(LEVEL(i),22,Vol,0,1,lamCoarse,NCPUS,MYID,MPI_COMM_WORLD)
else
call restrictDefectpar(MYID,Level(i+1),Level(i),MPI_COMM_WORLD)
call GaussSeidelSmoothSOR(LEVEL(i),LEVEL(i)%downsweep,Vol,1,1,lamN1,NCPUS,MYID,MPI_COMM_WORLD)
call computeDefectpar(MYID,NCPUS,LeveL(i),MPI_COMM_WORLD,summE,0)
endif
ENDDO
DO I=1,Nlevels-1
call prolongateDefectpar(MYID,Level(i+1),Level(i),MPI_COMM_WORLD)
call GaussSeidelSmoothSOR(LEVEL(i+1),LEVEL(i+1)%upsweep,Vol,0,1,lamN2,NCPUS,MYID,MPI_COMM_WORLD)
ENDDO
ENDDO
122 continue
call etime (t_stop,result )
IF(MYID.eq.0) write(*,*) "Elapsed time", t_stop(1)-t_start(1),t_stop(2)-t_start(2)
dhX=ABS(level(nlevels)%x(2)-level(nlevels)%x(1))
summ=0.
DO K=LEVEL(nlevels)%Z1(MYID+1),LEVEL(nlevels)%Z2(MYID+1)
DO J=LEVEL(nlevels)%Y1(MYID+1),LEVEL(nlevels)%Y2(MYID+1)
DO I=LEVEL(nlevels)%X1(MYID+1),LEVEL(nlevels)%X2(MYID+1)
summ=summ+level(nlevels)%phi(i,j,k)*level(nlevels)%eps(2,i,j,k)*level(nlevels)%rhs(i,j,k)/(4.*PI)*dhX*0.5
ENDDO
ENDDO
ENDDO
call MPI_ALLREDUCE(summ,summE,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,IERR)
IF(MYID.eq.0) write(*,*) "Energy=",summE
IF(MYID.eq.0) write(*,*) "I'm here"
DEALLOCATE(EPS)
DEALLOCATE(PHI)
DEALLOCATE(X)
DEALLOCATE(Y)
DEALLOCATE(Z)
DEALLOCATE(RHO)
DO i=nlevels,1,-1
DEALLOCATE(level(i)%x)
DEALLOCATE(level(i)%y)
DEALLOCATE(level(i)%z)
DEALLOCATE(level(i)%rhs)
DEALLOCATE(level(i)%err)
DEALLOCATE(level(i)%phi)
DEALLOCATE(level(i)%eps)
DEALLOCATE(level(i)%X1)
DEALLOCATE(level(i)%X2)
DEALLOCATE(level(i)%Y1)
DEALLOCATE(level(i)%Y2)
DEALLOCATE(level(i)%Z1)
DEALLOCATE(level(i)%Z2)
DEALLOCATE(level(i)%Nb)
DEALLOCATE(level(i)%Nelements)
DEALLOCATE(level(i)%RecV)
DEALLOCATE(level(i)%SenD)
DEALLOCATE(level(i)%PlanesSend)
DEALLOCATE(level(i)%X1Plane)
DEALLOCATE(level(i)%X2Plane)
DEALLOCATE(level(i)%Y1Plane)
DEALLOCATE(level(i)%Y2Plane)
DEALLOCATE(level(i)%Z1Plane)
DEALLOCATE(level(i)%Z2Plane)
DEALLOCATE(level(i)%RecV1)
DEALLOCATE(level(i)%SenD1)
DEALLOCATE(level(i)%LinesSend)
DEALLOCATE(level(i)%X1Line)
DEALLOCATE(level(i)%X2Line)
DEALLOCATE(level(i)%Y1Line)
DEALLOCATE(level(i)%Y2Line)
DEALLOCATE(level(i)%Z1Line)
DEALLOCATE(level(i)%Z2Line)
DEALLOCATE(level(i)%RecV2)
DEALLOCATE(level(i)%SenD2)
DEALLOCATE(level(i)%PointsSend)
DEALLOCATE(level(i)%XPoint)
DEALLOCATE(level(i)%YPoint)
DEALLOCATE(level(i)%ZPoint)
DEALLOCATE(level(i)%PlaneBuffRecV)
DEALLOCATE(level(i)%LineBuffRecV)
DEALLOCATE(level(i)%PointBuffRecV)
ENDDO
DEALLOCATE(level)
END PROGRAM POISSON_MGRID
SUBROUTINE computeDefectpar(MYID,NCPUS,LeveL,COMM,summE,flag)
Use LevelType
IMPLICIT NONE
INCLUDE 'mpif.h'
INTEGER :: MYID,IERR,stat(MPI_STATUS_SIZE),COMM,JJ,II,KK,Df1,Df2,PNT,CNT,Df3
INTEGER :: NCPUS,PlaneMaxDim,NBI,FLAG1,NN,FLAG2
REAL*8 :: phiIm,phiIp,phiJm,phiJp,phiKm,phiKp,summE
INTEGER :: CpuN
INTEGER :: NDIM,ITER,MAXITER,I,J,K,IP,IM,JP,JM,KP,KM,P,NDIM2,CNT2,CNT1,flag,M,O,CNTTT
REAL*8 :: SUMMHARTREE,EPS1,D_MAP,const,diag,offdiag,tmp
TYPE (LevelStruct)::Level
CpuN=MYID+1
NDIM=LEVEL%NDIM
! call UpdateBoundary(MYID,Level,COMM,0)
summE=0.
IF(FLAG.eq.1) THEN
DO K=LEVEL%Z1(CpuN),LEVEL%Z2(CpuN)
DO J=LEVEL%Y1(CpuN),LEVEL%Y2(CpuN)
DO I=LEVEL%X1(CpuN),LEVEL%X2(CpuN)
ip=I+1
im=I-1
jp=J+1
jm=J-1
kp=K+1
km=K-1
IF(I.EQ.NDIM) ip=1
IF(J.EQ.NDIM) jp=1
IF(K.EQ.NDIM) kp=1
IF(I.EQ.1) im=NDIM
IF(J.EQ.1) jm=NDIM
IF(K.EQ.1) km=NDIM
phiIm=LEVEL%phi(im,j,k)
phiIp=LEVEL%phi(ip,j,k)
phiJm=LEVEL%phi(i,jm,k)
phiJp=LEVEL%phi(i,jp,k)
phiKm=LEVEL%phi(i,j,km)
phiKp=LEVEL%phi(i,j,kp)
eps1=LEVEL%eps(5,i,j,k)
const=(6.+eps1)
offdiag=-(phiIm+phiIp+phiJm+phiJp+phiKm+phiKp)
diag=const*LEVEL%phi(i,j,k)
tmp=LEVEL%rhs(I,J,K)-(diag+offdiag)
summE=summE+tmp*tmp
LEVEL%err(I,J,K)=tmp
ENDDO
ENDDO
ENDDO
ELSE
DO K=LEVEL%Z1(CpuN),LEVEL%Z2(CpuN)
DO J=LEVEL%Y1(CpuN),LEVEL%Y2(CpuN)
DO I=LEVEL%X1(CpuN),LEVEL%X2(CpuN)
ip=I+1
im=I-1
jp=J+1
jm=J-1
kp=K+1
km=K-1
IF(I.EQ.NDIM) ip=1
IF(J.EQ.NDIM) jp=1
IF(K.EQ.NDIM) kp=1
IF(I.EQ.1) im=NDIM
IF(J.EQ.1) jm=NDIM
IF(K.EQ.1) km=NDIM
phiIm=LEVEL%phi(im,j,k)
phiIp=LEVEL%phi(ip,j,k)
phiJm=LEVEL%phi(i,jm,k)
phiJp=LEVEL%phi(i,jp,k)
phiKm=LEVEL%phi(i,j,km)
phiKp=LEVEL%phi(i,j,kp)
eps1=LEVEL%eps(5,i,j,k)
const=(6.+eps1)
offdiag=-(phiIm+phiIp+phiJm+phiJp+phiKm+phiKp)
diag=const*LEVEL%phi(i,j,k)
LEVEL%err(I,J,K)=LEVEL%rhs(I,J,K)-(diag+offdiag)
ENDDO
ENDDO
ENDDO
ENDIF
END SUBROUTINE computeDefectpar
SUBROUTINE restrictDefectpar(MYID,Level,Level1,COMM)
Use LevelType
IMPLICIT NONE
INCLUDE 'mpif.h'
REAL*8 summE,summE1
INTEGER NDIM,NDIM2H,I,J,K,Ih,Jh,Kh,Im,Jm,Km,Ip,Jp,Kp,MYID,CpuN,COMM
REAL*8 temp,tmp1
TYPE (LevelStruct)::Level,Level1
NDIM=LEVEL%NDIM
NDIM2H=LEVEL1%NDIM
CpuN=MYID+1
call UPDATE(MYID,LEVEL,COMM,1)
call UpdateBoundary(MYID,Level,COMM,1)
summE=0.
temp=1./64.
DO K=LEVEL1%Z1(CpuN),LEVEL1%Z2(CpuN)
DO J=LEVEL1%Y1(CpuN),LEVEL1%Y2(CpuN)
DO I=LEVEL1%X1(CpuN),LEVEL1%X2(CpuN)
Ih=2*I-1
Jh=2*J-1
Kh=2*K-1
Ip=MOD(Ih,NDIM)+1
Im=Ih-1
Jp=MOD(Jh,NDIM)+1
Jm=Jh-1
Kp=MOD(Kh,NDIM)+1
Km=Kh-1
IF(Ih.EQ.1) Im=NDIM
IF(Jh.EQ.1) Jm=NDIM
IF(Kh.EQ.1) Km=NDIM
tmp1=(4.*LEVEL%ERR(Ih,Jh,Km)+2.*LEVEL%ERR(Im,Jh,Km)+2.*LEVEL%ERR(Ip,Jh,Km)+2.*LEVEL%ERR(Ih,Jm,Km)+2.*LEVEL%ERR(Ih,Jp,Km)+LEVEL%ERR(Im,Jp,Km)&
&+LEVEL%ERR(Im,Jm,Km)+LEVEL%ERR(Ip,Jm,Km)+LEVEL%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SOR(LEVEL,MAXITER,Vol,flag,flag1,lamN,NCPUS,MYID,COMM)
Use LevelType
IMPLICIT NONE
INCLUDE 'mpif.h'
INTEGER MYID,IERR,stat(MPI_STATUS_SIZE),COMM,JJ,II,KK,Df1,Df2,PNT,CNT,Df3
INTEGER NCPUS,PlaneMaxDim,NBI,FLAG1,NN,FLAG2
REAL*8 :: phiIm,phiIp,phiJm,phiJp,phiKm,phiKp,summE
INTEGER:: XrecV(7,6),CpuN,Nelements(NCPUS),firstTimeflagX,firstTimeflagY,firstTimeflagZ,X1s,X2s,Y1s,Y2s,Z1s,Z2s
INTEGER NDIM,ITER,MAXITER,I,J,K,IP,IM,JP,JM,KP,KM,P,NDIM2,CNT2,CNT1,flag,M,O,CNTTT
REAL*8 SUMMHARTREE,EPS1,EPS2,EPS3,EPS4,EPS5,EPS6,D_MAP,summ,summ_prev,error
REAL*8 Vol,coeff2,omega,coeff1,summ1,IND(7),lamN,Momega,summHRecv,summRecV,summE1
INTEGER,SAVE:: FirstTime=1
LOGICAL FlagRB
TYPE (LevelStruct)::Level
NDIM=LEVEL%NDIM
omega=2./(1.+SQRT(1.-lamN))
Momega=1.-omega
CpuN=MYID+1
summRecV=0.
summHRecV=0.
summE=0.
DO K=LEVEL%Z1(CpuN),LEVEL%Z2(CpuN)
DO J=LEVEL%Y1(CpuN),LEVEL%Y2(CpuN)
DO I=LEVEL%X1(CpuN),LEVEL%X2(CpuN)
summE=summE+abs(LEVEL%rhs(i,j,k))
ENDDO
ENDDO
ENDDO
call MPI_ALLREDUCE(summE,summE1,1,MPI_DOUBLE_PRECISION,MPI_SUM,COMM,IERR)
IF(MYID.eq.0) write(*,*) "SMOTRI=ABS(PHI)=",summE1
DO ITER=1,MAXITER
! call MPI_BARRIER(COMM,IERR)
call UPDATE(MYID,LEVEL,COMM,0)
summHartree=0.
summ_prev=summRecV
summ=0.
FlagRB=.TRUE.
call ITERATE(LEVEL,FlagRB,lamN,MYID,COMM,0)
FlagRB=.FALSE.
call MPI_BARRIER(COMM,IERR)
call UPDATE(MYID,LEVEL,COMM,0)
call ITERATE(LEVEL,FlagRB,lamN,MYID,COMM,0)
DO K=LEVEL%Z1(CpuN),LEVEL%Z2(CpuN)
DO J=LEVEL%Y1(CpuN),LEVEL%Y2(CpuN)
DO I=LEVEL%X1(CpuN),LEVEL%X2(CpuN)
coeff2=LEVEL%phi(i,j,k)
summ=abs(LEVEL%phi(i,j,k))+summ
summHartree=summHartree+coeff2*LEVEL%eps(2,i,j,k)
ENDDO
ENDDO
ENDDO
summHartree=summHartree*LEVEL%dVol
summHartree=summHartree/Vol
call MPI_ALLREDUCE(summHartree,summHRecv,1,MPI_DOUBLE_PRECISION,MPI_SUM,COMM,IERR)
call MPI_ALLREDUCE(summ,summRecv,1,MPI_DOUBLE_PRECISION,MPI_SUM,COMM,IERR)
IF(MYID.eq.0) write(*,*) "summHreCv=",summHRecV,LEVEL%dVol,Vol
IF(FLAG1.eq.1) THEN
DO K=LEVEL%Z1(CpuN),LEVEL%Z2(CpuN)
DO J=LEVEL%Y1(CpuN),LEVEL%Y2(CpuN)
DO I=LEVEL%X1(CpuN),LEVEL%X2(CpuN)
LEVEL%PHI(I,J,K)=LEVEL%PHI(I,J,K)*LEVEL%eps(2,I,J,K)
LEVEL%PHI(I,J,K)=LEVEL%PHI(I,J,K)-summHRecV
LEVEL%PHI(I,J,K)=LEVEL%PHI(I,J,K)/LEVEL%eps(2,I,J,K)
ENDDO
ENDDO
ENDDO
ENDIF
IF(MYID.eq.0) then
IF(flag.eq.1) WRITE(*,*) "Error",ABS(summRecV-summ_prev)/summRecV*100,ITER
ENDIF
ENDDO
call UPDATE(MYID,LEVEL,COMM,0)
call UpdateBoundary(MYID,Level,COMM,0)
END SUBROUTINE GaussSeidelSmoothSOR
SUBROUTINE GetNLevels(NDIM,nlevels)
IMPLICIT NONE
INTEGER I,J,K,NDIM,nlevels,NDIM1
nlevels=1
NDIM1=NDIM
DO WHILE(1.GT.0)
IF(MOD(NDIM1,2).EQ.0.AND.NDIM1/2.GE.10) THEN
! IF(MOD(NDIM,2).EQ.0.AND.NDIM.GE.8) THEN
! IF(MOD(NDIM,2).EQ.0) THEN
nlevels=nlevels+1
NDIM1=NDIM1/2
ELSE
goto 122
ENDIF
ENDDO
122 continue
END SUBROUTINE GetNLevels
SUBROUTINE ReadData(vol,LEVEL)
USE levelType
IMPLICIT NONE
INTEGER I,J,K
REAL*8 summRho
REAL*8 Xtemp,Ytemp,Ztemp,A2B,dhX,dhY,dhZ,Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,dVol,Pi,vol
TYPE (LevelStruct)::Level
open (UNIT=82, FILE='sim_rho_r.dat', STATUS='OLD')
!open (UNIT=102, FILE='hartree.dat', STATUS='OLD')
write(*,*) LEVEL%ndim
!pause
A2B=1./0.529177249
Pi=4.*Atan(1.d0)
summRho=0.
DO K=1,LEVEL%NDIM
DO J=1,LEVEL%NDIM
DO I=1,LEVEL%NDIM
READ(82,*) LEVEL%X(I),LEVEL%Y(J),LEVEL%Z(K),LEVEL%RHS(i,j,k)
summRho=summRho+LEVEL%RHS(i,j,k)
! READ(102,*) Xtemp,Ytemp,Ztemp,vHartree(I,J,K)
ENDDO
ENDDO
ENDDO
LEVEL%X=LEVEL%X*A2B
LEVEL%Y=LEVEL%Y*A2B
LEVEL%Z=LEVEL%Z*A2B
write(*,*) LEVEL%X(1),LEVEL%X(LEVEL%NDIM-1)
write(*,*) LEVEL%X(1),LEVEL%X(LEVEL%NDIM-1)
dhX=ABS(LEVEL%X(2)-LEVEL%X(1))
dhY=ABS(LEVEL%Y(2)-LEVEL%Y(1))
dhZ=ABS(LEVEL%Z(2)-LEVEL%Z(1))
Xmin=MINVAL(LEVEL%X)
Ymin=MINVAL(LEVEL%Y)
Zmin=MINVAL(LEVEL%Z)
Xmax=MAXVAL(LEVEL%X)
Ymax=MAXVAL(LEVEL%Y)
Zmax=MAXVAL(LEVEL%Z)
LEVEL%dvol=dhX*dhY*dhZ
vol=(Xmax-Xmin+dhX)*(Ymax-Ymin+dhY)*(Zmax-Zmin+dhZ)
write(*,*) "vol,dvol =",vol,LEVEL%dVol,dhX
write(*,*) "SumRho before =",summRho*dVol
write(*,*) "X(0), X(NDIM) =",LEVEL%X(1)/A2B, LEVEL%X(LEVEL%NDIM)/A2B
LEVEL%RHS=LEVEL%RHS-summRho/Vol*LEVEL%dVol
LEVEL%RHS=LEVEL%RHS*4.*Pi*dhX*dhX
summRho=0.
DO K=1,LEVEL%NDIM
DO J=1,LEVEL%NDIM
DO I=1,LEVEL%NDIM
summRho=summRho+LEVEL%RHS(i,j,k)*LEVEL%dVol
ENDDO
ENDDO
ENDDO
write(*,*) "SumRho after =",summRho*LEVEL%dVol
write(*,*) Xmin,Ymin,Zmin
write(*,*) Xmax,Ymax,Zmax
close (82)
close (102)
END SUBROUTINE ReadData
!SUBROUTINE propagateXYZ(Xb,Yb,Zb,X,Y,Z,NDIMb,NDIM)
SUBROUTINE propagateXYZ(LEVEL1,LEVEL)
USE levelType
IMPLICIT NONE
INTEGER I,Ism
TYPE (LevelStruct)::LEVEL,LEVEL1
DO I=1,LEVEL%NDIM
LEVEL%X(I)=LEVEL1%X(2*I-1)
LEVEL%Y(I)=LEVEL1%Y(2*I-1)
LEVEL%Z(I)=LEVEL1%Z(2*I-1)
ENDDO
END SUBROUTINE propagateXYZ
SUBROUTINE fillEps(LEVEL,Xoffset,Yoffset,Zoffset)
Use LevelType
IMPLICIT NONE
INTEGER I,J,K,M,NDIM,Ip,Im,Jp,Jm,Kp,Km,P
REAL*8 Xoffset,Yoffset,Zoffset
REAL*8 R(6),dhX,dhY,dhZ,b,v,a1,a2,a3,a,d12,d32,temp1,temp2,temp,F
TYPE (LevelStruct)::LEVEL
NDIM=LEVEL%NDIM
!dhX=ABS(LEVEL%X(2)-LEVEL%X(1))
!dhY=ABS(LEVEL%Y(2)-LEVEL%Y(1))
!dhZ=ABS(LEVEL%Z(2)-LEVEL%Z(1))
b=12.
v=0.8
a=1.
DO K=1,N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int,Z_int,Nx,Ny,Nz,NSUB1,P,INCX,INCY,INCZ,MODX,MODY,MODZ
INTEGER levelN,nlevels,KK
INTEGER, DIMENSION(:), ALLOCATABLE ::NDIM_MAX,NDIM_MAX_LINE
TYPE (LevelStruct)::Level,Level1
NDIM=LEVEL%NDIM
NDIM3=NDIM*NDIM*NDIM
NSUB=NDIM3/NCPUS
NDIV=INT(NCPUS**(1./3.))+1
!NDIV=NCPUS-1
NSUB1=NCPUS
CNT=0
121 continue
DO I=NDIV,1,-1
IF(MOD(NSUB1,I).EQ.0) THEN
NSUB1=NSUB1/I
CNT=CNT+1
CNT1(CNT)=I
goto 122
ENDIF
ENDDO
122 continue
IF(CNT.eq.2) THEN
goto 123
ELSE
goto 121
ENDIF
123 continue
CNT=CNT+1
CNT1(CNT)=NSUB1
MULT=1
! IF(MYID.eq.0) WRITE(*,*) "######################################"
call SSORT(CNT1,3)
IF(MYID.eq.0) THEN
WRITE(*,*) "######################################"
WRITE(*,*) CNT1(1),CNT1(2),CNT1(3),NDIM
ENDIF
DO I=1,CNT
MULT=MULT*CNT1(I)
ENDDO
CNT=0
DO K=1,CNT1(3)
DO J=1,CNT1(2)
DO I=1,CNT1(1)
CNT=CNT+1
LEVEL%X1(CNT)=NDIM/CNT1(1)*(I-1)+1
LEVEL%X2(CNT)=NDIM/CNT1(1)*I
LEVEL%Y1(CNT)=NDIM/CNT1(2)*(J-1)+1
LEVEL%Y2(CNT)=NDIM/CNT1(2)*J
LEVEL%Z1(CNT)=NDIM/CNT1(3)*(K-1)+1
LEVEL%Z2(CNT)=NDIM/CNT1(3)*K
IF(I.eq.CNT1(1)) THEN
LEVEL%X2(CNT)=NDIM
ENDIF
IF(J.eq.CNT1(2)) THEN
LEVEL%Y2(CNT)=NDIM
ENDIF
IF(K.eq.CNT1(3)) THEN
LEVEL%Z2(CNT)=NDIM
ENDIF
IF(levelN.le.nlevels-1) THEN
LEVEL%X1(CNT)=(LEVEL1%X1(CNT)+1)/2
LEVEL%Y1(CNT)=(LEVEL1%Y1(CNT)+1)/2
LEVEL%Z1(CNT)=(LEVEL1%Z1(CNT)+1)/2
LEVEL%X2(CNT)=(LEVEL1%X2(CNT)+1)/2
LEVEL%Y2(CNT)=(LEVEL1%Y2(CNT)+1)/2
LEVEL%Z2(CNT)=(LEVEL1%Z2(CNT)+1)/2
DO KK=1,CNT-1