-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgem_main.F90
11585 lines (10330 loc) · 368 KB
/
gem_main.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
! 3D Flux Tube Toroidal Electromagnetic GK Delta-f Code
! global variables...
program gem_main
use gem_com
use gem_equil
use gem_fft_wrapper
implicit none
integer :: n,i,j,k,ip,ns
real :: others,tmp
character(len=70) fname
character(len=5) holymyid
real :: others
call initialize
write(holdmyid,'(I5.5)') MyId
fname=outdir//'gem_timing_'//holdmyid//'.txt'
open(510+MyId,file=fname,status="replace",action="write")
end_tm = MPI_WTIME()
write(510+MyId,*)'initilization', end_tm - start_tm
! use the following two lines for r-theta contour plot
if(iCrs_Sec==1)then
call pol2d
! call balloon
goto 100
end if
start_tm = MPI_WTIME()
if(iget.eq.0)call loader_wrapper
end_tm = MPI_WTIME()
write(510+MyId,*)'load', end_tm - start_tm
if(iget.eq.1)then
call restart(1,0)
end if
starttm=MPI_WTIME()
!if(isft==1)then
!call ftcamp
! goto 100
!end if
do timestep=ncurr,nm
tcurr = tcurr+dt
if((iput.eq.1).and.mod(timestep,50).eq.0)call restart(2,timestep-1)
do ns = 1,nsm
if(ision==1)call wiatxeps(ns,0)
end do
if(ifluid==1)call weatxeps(0)
call accumulate(timestep-1,0)
call ampere(timestep-1,0) !Apara, Apara_h, Apara_s
call poisson(timestep-1,0)
!MVPT
if (ipbm == 1) call pbm_pb(0)
call field(timestep-1,0)
call split_weight(timestep-1,0)
call diagnose(timestep-1)
!call reporter(timestep-1)
call push_wrapper(timestep,1)
call reporter(timestep-1)
call accumulate(timestep,1)
call ampere(timestep,1)
call poisson(timestep,1)
call field(timestep,1)
call split_weight(timestep,1)
call push_wrapper(timestep,0)
if(mod(timestep,1000)==0)then
do i=0,last
! if(myid==i)write(*,*)myid,mm(1),mme
call MPI_BARRIER(MPI_COMM_WORLD,ierr)
end do
end if
end do
!call ftcamp
!call wiatxeps(1,1)
!if(ifluid==1)call weatxeps(1)
lasttm=MPI_WTIME()
tottm=lasttm-starttm
tottm = tottm - tot_init_lap_tm - tot_init_fltm_tm
tot_ampere_tm = tot_ampere_tm - tot_init_lap_tm - tot_init_fltm_tm
others = tottm-tot_grid_tm-tot_ppush_tm-tot_cpush_tm-tot_pint_tm-tot_cint_tm-tot_setw_tm-tot_weatxeps_tm-tot_wiatxeps_tm-tot_pbi_tm-tot_pbe_tm-tot_jpar0_tm-tot_init_pmove_tm-tot_pmove_tm-tot_poisson_tm-tot_ampere_tm
write(holdmyid,'(I5.5)') MyId
fname='gem_timing_'//holdmyid//'.txt'
open(510+MyId,file=fname,status="replace",action="write")
write(510+MyId,*)'myid=',myid, 'mm(ns)', mm(1), 'mme', mme
write(510+MyId,*)'tot time=',tottm, 'ppush time', tot_ppush_tm, 'cpush time', tot_cpush_tm, 'pint time', tot_pint_tm, 'cint time', tot_cint_tm, 'grid time', tot_grid_tm, 'jie time', tot_jie_tm, 'den0 time', tot_den0_tm, 'setw time', tot_setw_tm, 'weatxeps time', tot_weatxeps_tm, 'wiatxeps time', tot_wiatxeps_tm, 'pbi time', tot_pbi_tm, 'pbe time', tot_pbe_tm, 'jpar0 time', tot_jpar0_tm
write(510+MyId,*)'init pmove', tot_init_pmove_tm, 'pmove', tot_pmove_tm, 'poisson', tot_poisson_tm, 'ampere', tot_ampere_tm, 'init_lap', tot_init_lap_tm, 'init_fltm', tot_init_fltm_tm, 'other', others
write(510+MyId,*)'field solver percentage is', (tot_ampere_tm+tot_poisson_tm)/tottm, 'others precentage is', others/tottm, 'field solver + others', (tot_ampere_tm+tot_poisson_tm+others)/tottm
call flush(510+MyId)
close(510+MyId)
call mpi_barrier(mpi_comm_world,ierr)
call MPI_reduce(tottm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tottm = tmp/real(numprocs)
call MPI_reduce(tot_ppush_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_ppush_tm = tmp/real(numprocs)
call MPI_reduce(tot_cpush_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_cpush_tm = tmp/real(numprocs)
call MPI_reduce(tot_pint_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_pint_tm = tmp/real(numprocs)
call MPI_reduce(tot_cint_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_cint_tm = tmp/real(numprocs)
call MPI_reduce(tot_grid_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_grid_tm = tmp/real(numprocs)
call MPI_reduce(tot_jie_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_jie_tm = tmp/real(numprocs)
call MPI_reduce(tot_den0_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_den0_tm = tmp/real(numprocs)
call MPI_reduce(tot_setw_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_setw_tm = tmp/real(numprocs)
call MPI_reduce(tot_weatxeps_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_weatxeps_tm = tmp/real(numprocs)
call MPI_reduce(tot_wiatxeps_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_wiatxeps_tm = tmp/real(numprocs)
call MPI_reduce(tot_pbi_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_pbi_tm = tmp/real(numprocs)
call MPI_reduce(tot_pbe_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_pbe_tm = tmp/real(numprocs)
call MPI_reduce(tot_jpar0_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_jpar0_tm = tmp/real(numprocs)
call MPI_reduce(tot_init_pmove_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_init_pmove_tm = tmp/real(numprocs)
call MPI_reduce(tot_pmove_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_pmove_tm = tmp/real(numprocs)
call MPI_reduce(tot_poisson_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_poisson_tm = tmp/real(numprocs)
call MPI_reduce(tot_ampere_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_ampere_tm = tmp/real(numprocs)
call MPI_reduce(tot_init_lap_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_init_lap_tm = tmp/real(numprocs)
call MPI_reduce(tot_init_fltm_tm, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)tot_init_fltm_tm = tmp/real(numprocs)
call MPI_reduce(others, tmp, 1, MPI_REAL8, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if(myid==0)others = tmp/real(numprocs)
if(myid==0)open(123, file = "gem_timing.txt", status = "replace", action="write")
if(myid==0) write(123,*)'tot time=',tottm, 'ppush time', tot_ppush_tm, 'cpush time', tot_cpush_tm, 'pint time', tot_pint_tm, 'cint time', tot_cint_tm, 'grid time', tot_grid_tm, 'jie time', tot_jie_tm, 'den0 time', tot_den0_tm, 'setw time', tot_setw_tm, 'weatxeps time', tot_weatxeps_tm, 'wiatxeps time', tot_wiatxeps_tm, 'pbi time', tot_pbi_tm, 'pbe time', tot_pbe_tm, 'jpar0 time', tot_jpar0_tm
if(myid==0) write(123,*)'init pmove', tot_init_pmove_tm, 'pmove', tot_pmove_tm, 'poisson', tot_poisson_tm, 'ampere', tot_ampere_tm, 'init_lap', tot_init_lap_tm, 'init_fltm', tot_init_fltm_tm, 'other', others
if(myid==0)write(123,*)'field solver percentage is', (tot_ampere_tm+tot_poisson_tm)/tottm, 'others precentage is', others/tottm, 'field solver + others', (tot_ampere_tm+tot_poisson_tm+others)/tottm
if(myid==0)call flush(123)
if(myid==0)close(123)
call MPI_BARRIER(MPI_COMM_WORLD,ierr)
100 call MPI_FINALIZE(ierr)
end program gem_main
!ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
subroutine hybinit
use gem_com
use gem_equil
implicit none
GCLR = int(MyId/ntube)
GLST = numprocs/ntube-1
TCLR = mod(MyId,ntube)
TLST = ntube-1
!***MPI variables
! if (GCLR.eq.GLST)
! % mykm=km-GLST*mykm
mykm = 1
rngbr = GCLR+1
if (GCLR.eq.GLST)rngbr = 0
lngbr = GCLR-1
if (GCLR.eq.0) lngbr = GLST
idnxt = TCLR+1
if (TCLR.eq.TLST)idnxt = 0
idprv = TCLR-1
if (TCLR.eq.0) idprv = TLST
!
! return
end subroutine hybinit
!ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
subroutine init
use gem_com
use gem_equil
implicit none
include 'fftw3.f03'
character(len=62) dumchar
INTEGER :: i,j,k,m,n,ns,idum,i1,j1,k1,nthreads,iam,i_err
INTEGER*8 :: mm1,mm2
INTEGER :: lr1
real :: mims1,tets1,q1,kappan,kappat,r,qr,th,cost,dum,zdum
real :: dbdrp,dbdtp,grcgtp,bfldp,fp,radiusp,dydrp,qhatp,psipp
real :: grp,gxdgyp,jacp,jfnp,gn0ep,gt0ep,gt0ip,grdgtp,gthp,gsfp,gthfp
real,DIMENSION (1:5) :: gn0sp,gt0sp
real :: wx0,wx1,wz0,wz1,b
!!$omp parallel private(iam)
!nthreads = omp_get_num_threads()
!iam = omp_get_thread_num()
!write(*,*)'myid, nthreads, iam = ',myid, nthreads, iam
!!$omp end parallel
!n_omp = nthreads
!jycheng
namelist /primary_parameters/ itube,mimp,mcmp,chgi,chgc,imx,jmx,kmx,nsmx,ntube,lxa, &
lymult,jcnt,dt,nm,nsm,amp,r0a,ifluid,ipbm,ias,isg,amie,rneui, &
betai,nonlin1,nonlin2,nonline,iflut,iexb,ipara,vcut,wecut,micell,mecell, &
qbeam,mbeam,isonew
namelist /control_parameters/ iperi,iperidf,delra,delri,delre,delrn,nlow,xshape, &
yshape,zshape,iput,iget,igetmx,ision,isiap,peritr,llk,mlk,onemd,izonal, &
adiabatic_electron,ineq0,nzcrt,npze,npzi,npzc,npzb,iphbf,iapbf, &
idpbf,cut,kxcut,kycut,bcut,c4,vexbsw,vparsw,mach,gamma_E,isuni,isunie, &
lr1,iflr,iorb,nxsrc,nesrc,nzsrc,gammah,ghzon,gamgtc,gamtoy,gamgyro,nzgyro
namelist /diagnostics_parameters/ icrs_sec,ipg,isphi,nplot,xnplt,isft,mynf,frmax,ifskp,idg
namelist /fluxtube/ lxmult,Rovera,elon0,selon0,tria0,stria0,rmaj0p,q0,shat0,teti,tcti,rhoia,Rovlni,Rovlti, &
Rovlne,Rovlte,Rovlnc,Rovltc,ncne,nuacs
namelist /others/ nrst,eprs,tor,vpp,vt0,yd0,nmx
IU=cmplx(0.,1.)
pi=4.0*atan(1.0)
pi2 = pi*2.
call ppinit_mpi(myid,numprocs)
start_tm=MPI_WTIME()
last=numprocs-1
!the initial timestep index
timestep=0
!the initial timestep
tcurr=0.
open(unit=115,file='gem.in',status='old',action='read')
read(115,nml=primary_parameters)
read(115,nml=control_parameters)
read(115,nml=diagnostics_parameters)
read(115,nml=fluxtube)
read(115,nml=others)
close(115)
if(idg==1)then
do i = 0,last
if(myid==i)then
open(9, file='plot',status='unknown',position='append')
write(9,*)'pass read namelists',myid
close(9)
end if
call MPI_BARRIER(MPI_COMM_WORLD,ierr)
end do
end if
nonlin(1)=nonlin1
nonlin(2)=nonlin2
ntube=int(numprocs/kmx)
if((mod(numprocs,kmx) .ne. 0) .and. myid==0)then
write(*,*)'WARNING: MPI number is a multiple of kmx, ntube is not an integer'
stop
endif
mm1=int(imx,8)*int(jmx,8)*int(kmx,8)*int(micell,8)
!mm1=imx*jmx*kmx*micell
mm2=int(imx,8)*int(jmx,8)*int(kmx,8)*int(mecell,8)
!mm2=imx*jmx*kmx*mecell
! increase mmx and mmxe from 1.5 to 3, to solve PE 7 xp size overflow issue
mmx=int(real(imx*jmx*(micell/int(ntube)))*3.0)
mmxe=int(real(imx*jmx*(mecell/int(ntube)))*3.0)
call ppinit_decomp(myid,numprocs,ntube,tube_comm,grid_comm)
call hybinit
call mpi_barrier(mpi_comm_world,ierr)
if(idg==1)then
do i = 0,last
if(myid==i)then
open(9, file='plot',status='unknown',position='append')
write(9,*)'pass hybinit',myid
close(9)
end if
call MPI_BARRIER(MPI_COMM_WORLD,ierr)
end do
end if
im=imx;jm=jmx;km=kmx
cgpfacx = 4
cgpfacy = 2
icgp = imx/cgpfacx
jcgp = jmx/cgpfacy
if(isft==1) iget = 1
nfreq = 400
call new_gem_com()
if(idg==1)then
do i = 0,last
if(myid==i)then
open(9, file='plot',status='unknown',position='append')
write(9,*)'pass new_equil',myid
close(9)
end if
call MPI_BARRIER(MPI_COMM_WORLD,ierr)
end do
end if
! rin = r0-lx/2
! rout = r0+lx/2
rina = r0a-lxa/2.
routa = r0a+lxa/2.
call new_equil()
lx=lxa*a
ly=2.*pi*r0/q0abs/lymult
!beta has been used all the time, so the definition is fixed, declared in gem_com.f. betai is declared in equil.f
!in gem.in betai is intended to be GYRO's beta_e (within a factor of 2). It is defined to be (mu0 ne Te)/Bunit**2
! beta=betai
br0 = rmaj0
lr0 = r0
qp = q0p
lz = pi2*q0abs*rmaj0
delz = lz/ntheta
rneu = nuacs
kycut=pi2/ly*(jcnt-1)/2*1.1
if(itube==1)then
kxcut=pi2*shat0*kycut*4
end if
if(myid.eq.master)then
open(19, file='eqdat', status='unknown')
write(19,*) 'r0a,lxa,beta,a,rmaj0=', r0a,lxa,beta,a,rmaj0
write(19,*)'elon0,tria0,rmaj0p,stria0,selon0,q0,shat=', elon0,tria0,rmaj0p,stria0,selon0,q0,r0*q0p/q0
write(19,*)'xn0s(1,nr/2),xn0s(2,nr/2),t0s(1,nr/2),t0s(2,nr/2)=',xn0s(1,nr/2),xn0s(2,nr/2),t0s(1,nr/2),t0s(2,nr/2)
write(19,*)'capns(1,nr/2),capns(2,nr/2),capts(1,nr/2),capts(2,nr/2)=', capns(1,nr/2),capns(2,nr/2),capts(1,nr/2),capts(2,nr/2)
write(19,*)'capne(nr/2),capte(nr/2)=', capne(nr/2),capte(nr/2)
write(19,*)'t0i0,t0e0,ni0,ne0=', t0i(nr/2),t0e(nr/2),xn0i(nr/2),xn0e(nr/2)
write(19,*)'a/cs=', a/sqrt(t0e(nr/2)/mimp)
write(19,*)'i, sf, ni, ne, nc, ti, tc, capni, capnc, captc'
do i = 0,nr
write(19,99)i,sf(i),xn0e(i),xn0i(i),xn0c(i),t0e(i),t0i(i),t0c(i),capne(i),capni(i),capnc(i),capte(i),capti(i),captc(i)
end do
write(19,99)i,cn0e,cn0s(1),cn0s(2),cn0s(3)
close(19)
end if
98 format(7(1x,e14.7))
99 format(1x,i3,2x,15(1x,e10.3))
! equilibrium data for transport coefficients calculation by mablab with flux data
if(myid.eq.master)then
open(912, file='eqflux', status='unknown')
do j = 1, nsubd
i = int(nr*(2*j-1)/(2*nsubd))
write(912,9991) xn0e(i)*cn0e,capne(i),xn0c(i)*cn0c,capnc(i),t0i(i),capti(i)
enddo
close(912)
endif
9991 format(6(2x,e12.4))
!
! write(*,*)'br0,lr0,q0,qp = ', br0,lr0,q0,qp
dte = dt
iadi = 0
if(isg.gt.0.)fradi = isg
if(ifluid.eq.0)then
iadi = 1
fradi = 1.0
end if
! begin reading species info, ns=1,nsm...
ns = 1
mims(3)=mbeam
q(3)=qbeam
mims(1)=mimp;mims(2)=mcmp;q(1)=chgi;q(2)=chgc
rhoia = mims(1)*sqrt(T0e(nr/2)/mims(1)) /(q(1)*1.) /a
tmm(1)=mm1
tmm(2)=mm2
mm(:)=imx*jmx*(micell/int(ntube))
mme = imx*jmx*(micell/int(ntube))
! write(*,*)'in init ',Myid,mm(ns)
tets(1)=1
lr(1)=lr1
lr(2)=lr1
lr(3)=lr1
pzcrite = abs(psi(nr)-psi(0))/br0/npze
encrit = 0.2*t0e(nr/2)
pzcrit(1) = q(1)*abs(psi(nr)-psi(0))/br0/npzi
pzcrit(2) = q(2)*abs(psi(nr)-psi(0))/br0/npzc
pzcrit(3) = q(3)*abs(psi(nr)-psi(0))/br0/npzb
kapn(ns)=kappan
kapt(ns)=kappat
emass = 1./amie
qel = -1.
vsphere = sqrt(vcut*1.1) * sqrt(tge/emass)
cv = 4.0/3.0*pi * vsphere**3
ecutsrc = vcut
if(iget.eq.1) amp=0.
! totvol is the square for now...
dx=lx/real(im)
dy=ly/real(jm)
dz=lz/real(km)
! totvol=lx*ly*lz
dxcgp = lx/icgp
dycgp = ly/jcgp
dxsrc = lx/nxsrc
desrc = ecutsrc/nesrc
e0=lr0/q0/br0
!
do i=0,nxpp
xg(i)=i*dx !dx*(tclr*nxpp+i)
enddo
do j=0,jm
yg(j)=dy*real(j)
enddo
kcnt=1
do k=0,mykm
n=GCLR*kcnt+k
zg(k)=dz*real(n)
enddo
! jcnt = 3 !jmx/ntube
mstart = 0
ntor0 = mstart+1
do m = 0,jcnt-1
isgnft(m) = 1
j1 = mstart+int((real(m)+1.0)/2)
jft(m) = j1
if(m==0)then
isgnft(m) = 1
jft(m) = 0
end if
if(m>0.and.mod(m,2)==0)then
isgnft(m) = -1
jft(m) = jmx-j1
end if
end do
! initialize bfld
zfnth(0) = 0.
do j = 1,ntheta
zfnth(j) = zfnth(j-1)+dth*q0*br0*(1./jfn(j-1)+1./jfn(j))/2
end do
if(q0<0)zfnth = zfnth+lz
thfnz(0) = -pi
thfnz(ntheta) = pi
if(q0<0.)then
thfnz(0) = pi
thfnz(ntheta) = -pi
end if
if(q0>0)then
k = 0
do j = 1,ntheta-1
zdum = j*lz/ntheta
do i = k,ntheta-1
if(zfnth(i)<=zdum.and.zfnth(i+1)>zdum)then
k = i
dum = (zdum-zfnth(i))*dth/(zfnth(i+1)-zfnth(i))
thfnz(j) = i*dth-pi+dum
go to 127
end if
end do
127 continue
end do
end if
if(q0<0)then
k = 0
do j = 1,ntheta-1
zdum = lz-j*lz/ntheta
do i = k,ntheta-1
if(zfnth(i)>=zdum.and.zfnth(i+1)<zdum)then
k = i
dum = (zdum-zfnth(i))*dth/(zfnth(i+1)-zfnth(i))
thfnz(ntheta-j) = i*dth-pi+dum
go to 128
end if
end do
128 continue
end do
end if
do i1 = 0,nxpp
r = xg(i1)-0.5*lx+lr0
do k1 = 0,mykm
k = int(zg(k1)/delz)
k = min(k,ntheta-1)
wz0 = ((k+1)*delz-zg(k1))/delz
wz1 = 1-wz0
th = wz0*thfnz(k)+wz1*thfnz(k+1)
i = int((r-rin)/dr)
i = min(i,nr-1)
wx0 = (rin+(i+1)*dr-r)/dr
wx1 = 1.-wx0
k = int((th+pi)/dth)
k = min(k,ntheta-1)
wz0 = (-pi+(k+1)*dth-th)/dth
wz1 = 1.-wz0
dbdrp = wx0*wz0*dbdr(i,k)+wx0*wz1*dbdr(i,k+1) &
+wx1*wz0*dbdr(i+1,k)+wx1*wz1*dbdr(i+1,k+1)
dbdtp = wx0*wz0*dbdth(i,k)+wx0*wz1*dbdth(i,k+1) &
+wx1*wz0*dbdth(i+1,k)+wx1*wz1*dbdth(i+1,k+1)
grcgtp = wx0*wz0*grcgt(i,k)+wx0*wz1*grcgt(i,k+1) &
+wx1*wz0*grcgt(i+1,k)+wx1*wz1*grcgt(i+1,k+1)
grdgtp = wx0*wz0*grdgt(i,k)+wx0*wz1*grdgt(i,k+1) &
+wx1*wz0*grdgt(i+1,k)+wx1*wz1*grdgt(i+1,k+1)
gthp = wx0*wz0*gth(i,k)+wx0*wz1*gth(i,k+1) &
+wx1*wz0*gth(i+1,k)+wx1*wz1*gth(i+1,k+1)
bfldp = wx0*wz0*bfld(i,k)+wx0*wz1*bfld(i,k+1) &
+wx1*wz0*bfld(i+1,k)+wx1*wz1*bfld(i+1,k+1)
radiusp = wx0*wz0*radius(i,k)+wx0*wz1*radius(i,k+1) &
+wx1*wz0*radius(i+1,k)+wx1*wz1*radius(i+1,k+1)
dydrp = wx0*wz0*dydr(i,k)+wx0*wz1*dydr(i,k+1) &
+wx1*wz0*dydr(i+1,k)+wx1*wz1*dydr(i+1,k+1)
qhatp = wx0*wz0*qhat(i,k)+wx0*wz1*qhat(i,k+1) &
+wx1*wz0*qhat(i+1,k)+wx1*wz1*qhat(i+1,k+1)
grp = wx0*wz0*gr(i,k)+wx0*wz1*gr(i,k+1) &
+wx1*wz0*gr(i+1,k)+wx1*wz1*gr(i+1,k+1)
jacp = wx0*wz0*jacob(i,k)+wx0*wz1*jacob(i,k+1) &
+wx1*wz0*jacob(i+1,k)+wx1*wz1*jacob(i+1,k+1)
gxdgyp = wx0*wz0*gxdgy(i,k)+wx0*wz1*gxdgy(i,k+1) &
+wx1*wz0*gxdgy(i+1,k)+wx1*wz1*gxdgy(i+1,k+1)
gthfp = wx0*wz0*thflx(i,k)+wx0*wz1*thflx(i,k+1) &
+wx1*wz0*thflx(i+1,k)+wx1*wz1*thflx(i+1,k+1)
fp = wx0*f(i)+wx1*f(i+1)
gsfp = wx0*sf(i)+wx1*sf(i+1)
jfnp = wz0*jfn(k)+wz1*jfn(k+1)
psipp = wx0*psip(i)+wx1*psip(i+1)
gn0ep = wx0*xn0e(i)+wx1*xn0e(i+1)
gt0ep = wx0*t0e(i)+wx1*t0e(i+1)
do ns = 1, nsm
gn0sp(ns) = wx0*xn0s(ns,i)+wx1*xn0s(ns,i+1)
gt0sp(ns) = wx0*t0s(ns,i)+wx1*t0s(ns,i+1)
gn0s(ns,i1) = gn0sp(ns)
gt0s(ns,i1) = gt0sp(ns)
enddo
gt0ip = wx0*t0s(1,i)+wx1*t0s(1,i+1)
b=1.-tor+tor*bfldp
cfx(i1,k1) = br0/b**3*fp/radiusp*dbdtp*grcgtp
cfy(i1,k1) = br0/b**3*fp/radiusp* &
(dydrp*dbdtp-lr0/q0*qhatp*dbdrp)*grcgtp
bdgxcgy(i1,k1) = 1./b*lr0/q0*qhatp*fp/radiusp*grcgtp
bmag(i1,k1) = b
jac(i1,k1) = jacp*jfnp
bdgrzn(i1,k1) = q0*br0/radiusp/b*psipp*grcgtp/jfnp
gn0e(i1) = gn0ep
gt0e(i1) = gt0ep
gt0i(i1) = gt0ip
ggxdgy(i1,k1) = gxdgyp
ggy2(i1,k1) = dydrp**2*grp**2 + (r0/q0*qhatp*gthp)**2 + 2*dydrp*r0/q0*qhatp*grdgtp
ggx(i1,k1) = grp
gsf(i1) = gsfp
gthf(i1,k1) = gthfp
end do
end do
iseed = -(1777+myid*13)
idum = ran2(iseed)
phi = 0.
apar = 0.
dene = 0.
upar = 0.
upa0 = 0.
camp = 0.
!MVPT
if(ipbm==1)then
apars=0
aparss = 0
end if
if(myid.eq.master)then
write(*,*)zfnth(ntheta),thfnz(ntheta/2),thfnz(ntheta/2+1)
if(myid.eq.master)open(9, file='plot', &
status='replace',position='write')
write(9,*)'dt,beta= ',dt, beta
write(9,*)'amp = ',amp
write(9,*)'peritr,ifluid= ',peritr,ifluid
write(9,*)'tor,nonlin(1:2),nonline= ',tor,nonlin(1),nonlin(2),nonline
write(9,*)'isuni= ',isuni, 'amie= ',amie
write(9,*)'kxcut,kycut,bcut,wecut= ',kxcut,kycut,bcut,wecut
write(9,*)'fradi,isg= ',fradi,isg
write(9,*)'llk,mlk,onemd =',llk,mlk,onemd
write(9,*)'vcut= ', vcut
write(9,*)'rneu= ', rneu
write(9,*)'V-ExB switch= ', vexbsw
write(9,*)'V-parallel switch= ', vparsw
write(9,*)'tot mmi,tot mme= ',mm1,mm2
write(9,*)'mm, mme is', mm(1), mme
write(9,*)'numprocs is', numprocs
write(9,*)'pzcrite,encrit = ',pzcrite,encrit
write(9,*) 'lxa,lymult,delra,r0a,rina,routa=',lxa,lymult,delra,r0a,rina,routa
write(9,*) 'a,r0,rmaj0,q0,lx,ly,lz=',a,r0,rmaj0,q0,lx,ly,lz
write(9,*) 't0,kyrhoi_local=',t0i(nr/2),2*pi*sqrt(mims(1))*sqrt(t0i(nr/2))/ly
write(9,*) 'isonew,nzcrt,nzsrc=',isonew,nzcrt,nzsrc
close(9)
end if
if(myid.eq.master)then
write(*,*)'dt,beta= ',dt, beta
write(*,*)'amp,vpp,yd0 = ',amp,vpp,yd0
write(*,*)'peritr,ifluid= ',peritr,ifluid
write(*,*)'tor,nonlin= ',tor,nonlin(1),nonlin(2)
write(*,*)'isuni= ',isuni, 'amie= ',amie
write(*,*)'kxcut,kycut,bcut= ',kxcut,kycut,bcut
write(*,*)'fradi,isg= ',fradi,isg
write(*,*)'llk,mlk,onemd =',llk,mlk,onemd
write(*,*)'vcut= ', vcut
write(*,*)'rneu= ', rneu
write(*,*)'V-ExB switch= ', vexbsw
write(*,*)'V-parallel switch= ', vparsw
write(*,*)'mm1= ',mm1
write(*,*)'pzcrite,encrit = ',pzcrite,encrit
write(*,*)'nue0 = ',nue0(1),nue0(nr/2),nue0(nr-1)
write(*,*)'xn0e(1),xnir0 = ',xn0e(1),xnir0
write(*,*)'frequ, eru = ', frequ, eru
write(*,*) 'lxa,lymult,delra,r0a,rina,routa=',lxa,lymult,delra,r0a,rina,routa
write(*,*) 'a,r0,rmaj0,q0,lx,ly,lz=',a,r0,rmaj0,q0,lx,ly,lz
write(*,*) 't0,kyrhoi_local=',t0i(nr/2),2*pi*sqrt(mims(1))*sqrt(t0i(nr/2))/ly
write(*,*) 'coefu = ', xu**2*frequ
write(*,*) 'ktheta*rhos = ',2*pi*sqrt(mims(1))*sqrt(t0e(nr/2))/ly
write(*,*) 'cs/a, q0, q0p, s^hat = ',sqrt(t0e(nr/2)/2.)/a, q0, q0p, q0p/q0*r0
write(*,*) 'rho* = rhos/a = ', sqrt(mims(1))*sqrt(t0e(nr/2))/a
write(*,*) 'f0p,psip(nr/2),Bunit,candyf0p = ',f0p,psip(nr/2),bunit,candyf0p
write(*,*) 'lxa min = ', ly*q0/(2*pi*r0*q0p)/a
write(*,*) 't0i(nr/2)= ', t0i(nr/2)
write(*,*) 'Gyrokrs = ', 2*pi*sqrt(mims(1))*sqrt(t0e(nr/2))/ly/bunit
end if
close(115)
!$acc update device( bfld,qhat,radius,gr,gth,grdgt,grcgt)
!$omp target update to(bfld,qhat,radius,gr,gth,grdgt,grcgt)
!$acc update device( gxdgy,dydr,dbdr,dbdth,jacob)
!$omp target update to(gxdgy,dydr,dbdr,dbdth,jacob)
!$acc update device( yfn,hght,thflx)
!$omp target update to(yfn,hght,thflx)
!$acc update device( psi)
!$omp target update to(psi)
!$acc update device( f,psip,sf,jacoba,jfn,zfnth,thfnz)
!$omp target update to(f,psip,sf,jacoba,jfn,zfnth,thfnz)
!$acc update device( t0i,t0e,t0b,t0c,t0ip,t0ep,t0bp,t0cp)
!$omp target update to(t0i,t0e,t0b,t0c,t0ip,t0ep,t0bp,t0cp)
!$acc update device( xn0i,xn0e,xn0c,xn0b,xn0ip,xn0ep,xn0bp)
!$omp target update to(xn0i,xn0e,xn0c,xn0b,xn0ip,xn0ep,xn0bp)
!$acc update device( xn0cp,vpari,vparc,vparb)
!$omp target update to(xn0cp,vpari,vparc,vparb)
!$acc update device( vparip,vparcp,vparbp)
!$omp target update to(vparip,vparcp,vparbp)
!$acc update device( capti,capte,captb,captc,capni,capne)
!$omp target update to(capti,capte,captb,captc,capni,capne)
!$acc update device( capnb,capnc,zeff,nue0,phinc,phincp)
!$omp target update to(capnb,capnc,zeff,nue0,phinc,phincp)
!$acc update device( er,upari,dipdr)
!$omp target update to(er,upari,dipdr)
!$acc update device( e1gx,e1gy,e2gx,e2gy,bdge1gx,bdge1gy,bdge2gx,bdge2gy)
!$omp target update to(e1gx,e1gy,e2gx,e2gy,bdge1gx,bdge1gy,bdge2gx,&
!$omp bdge2gy)
!$acc update device( psip2)
!$omp target update to(psip2)
!$acc update device( curvbz,srbr,srbz,thbr,thbz,prsrbr,prsrbz,pthsrbr,pthsrbz,bdcrvb)
!$omp target update to(curvbz,srbr,srbz,thbr,thbz,prsrbr,prsrbz,&
!$omp pthsrbr,pthsrbz,bdcrvb)
!$acc update device( t0s,xn0s,capts,capns,vpars,vparsp)
!$omp target update to(t0s,xn0s,capts,capns,vpars,vparsp)
!$acc update device( cn0s,n0smax,tgis)
!$omp target update to(cn0s,n0smax,tgis)
if(myid==0)then
open(gemout,file='gem.out',status='replace',action='write')
write(gemout,*)'start gem.out'
call flush(gemout)
endif
! return
end subroutine init
!ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
subroutine ppush(n,ns)
use gem_com
use gem_equil
implicit none
real :: phip,exp1,eyp,ezp,delbxp,delbyp,dpdzp,dadzp,aparp
real :: wx0,wx1,wy0,wy1,wz0,wz1,dum,vxdum,dum1,bstar
INTEGER :: m,i,j,k,l,n,ns
INTEGER :: np_old,np_new
real :: rhog,vfac,kap,vpar,pidum,kaptp,kapnp,xnp
real :: b,th,r,enerb,cost,sint,qr,laps,sz,ter,dtp
real :: xt,xs,yt,xdot,ydot,zdot,pzdot,edot,pzd0,vp0
real :: dbdrp,dbdtp,grcgtp,bfldp,fp,radiusp,dydrp,qhatp,psipp,jfnp,grdgtp
real :: grp,gxdgyp,rhox(4),rhoy(4),psp,pzp,vncp,vparspp,psip2p,bdcrvbp,curvbzp,dipdrp
integer :: mynopi
real :: fdum,gdum,fisrcp,dnisrcp,avwixepsp,fovg,avwixezp,dnisrczp
!MVPT
real :: dapsdtp, delbxhp, delbyhp, dahdzp, aparhp,zdot0,zdot1
mynopi = 0
nopi(ns) = 0
start_ppush_tm = MPI_WTIME()
!$acc parallel
!$omp target teams
!$acc loop gang vector private(rhox,rhoy)
!$omp distribute parallel do private(rhox,rhoy)
do m=1,mm(ns)
r=x2(m,ns)-0.5*lx+lr0
k = int(z2(m,ns)/delz)
wz0 = ((k+1)*delz-z2(m,ns))/delz
wz1 = 1-wz0
th = wz0*thfnz(k)+wz1*thfnz(k+1)
i = int((r-rin)/dr)
wx0 = (rin+(i+1)*dr-r)/dr
wx1 = 1.-wx0
k = int((th+pi)/dth)
wz0 = (-pi+(k+1)*dth-th)/dth
wz1 = 1.-wz0
dbdrp = wx0*wz0*dbdr(i,k)+wx0*wz1*dbdr(i,k+1) &
+wx1*wz0*dbdr(i+1,k)+wx1*wz1*dbdr(i+1,k+1)
dbdtp = wx0*wz0*dbdth(i,k)+wx0*wz1*dbdth(i,k+1) &
+wx1*wz0*dbdth(i+1,k)+wx1*wz1*dbdth(i+1,k+1)
grcgtp = wx0*wz0*grcgt(i,k)+wx0*wz1*grcgt(i,k+1) &
+wx1*wz0*grcgt(i+1,k)+wx1*wz1*grcgt(i+1,k+1)
bfldp = wx0*wz0*bfld(i,k)+wx0*wz1*bfld(i,k+1) &
+wx1*wz0*bfld(i+1,k)+wx1*wz1*bfld(i+1,k+1)
radiusp = wx0*wz0*radius(i,k)+wx0*wz1*radius(i,k+1) &
+wx1*wz0*radius(i+1,k)+wx1*wz1*radius(i+1,k+1)
dydrp = wx0*wz0*dydr(i,k)+wx0*wz1*dydr(i,k+1) &
+wx1*wz0*dydr(i+1,k)+wx1*wz1*dydr(i+1,k+1)
qhatp = wx0*wz0*qhat(i,k)+wx0*wz1*qhat(i,k+1) &
+wx1*wz0*qhat(i+1,k)+wx1*wz1*qhat(i+1,k+1)
grp = wx0*wz0*gr(i,k)+wx0*wz1*gr(i,k+1) &
+wx1*wz0*gr(i+1,k)+wx1*wz1*gr(i+1,k+1)
gxdgyp = wx0*wz0*gxdgy(i,k)+wx0*wz1*gxdgy(i,k+1) &
+wx1*wz0*gxdgy(i+1,k)+wx1*wz1*gxdgy(i+1,k+1)
curvbzp = wx0*wz0*curvbz(i,k)+wx0*wz1*curvbz(i,k+1) &
+wx1*wz0*curvbz(i+1,k)+wx1*wz1*curvbz(i+1,k+1)
bdcrvbp = wx0*wz0*bdcrvb(i,k)+wx0*wz1*bdcrvb(i,k+1) &
+wx1*wz0*bdcrvb(i+1,k)+wx1*wz1*bdcrvb(i+1,k+1)
grdgtp = wx0*wz0*grdgt(i,k)+wx0*wz1*grdgt(i,k+1) &
+wx1*wz0*grdgt(i+1,k)+wx1*wz1*grdgt(i+1,k+1)
fp = wx0*f(i)+wx1*f(i+1)
jfnp = wz0*jfn(k)+wz1*jfn(k+1)
psipp = wx0*psip(i)+wx1*psip(i+1)
psp = wx0*psi(i)+wx1*psi(i+1)
ter = wx0*t0s(ns,i)+wx1*t0s(ns,i+1)
kaptp = wx0*capts(ns,i)+wx1*capts(ns,i+1)
kapnp = wx0*capns(ns,i)+wx1*capns(ns,i+1)
xnp = wx0*xn0s(ns,i)+wx1*xn0s(ns,i+1)
vncp = wx0*phincp(i)+wx1*phincp(i+1)
vparspp = wx0*vparsp(ns,i)+wx1*vparsp(ns,i+1)
psip2p = wx0*psip2(i)+wx1*psip2(i+1)
dipdrp = wx0*dipdr(i)+wx1*dipdr(i+1)
b=1.-tor+tor*bfldp
pzp = mims(ns)*u2(m,ns)/b*fp/br0-q(ns)*psp/br0
rhog=sqrt(2.*b*mu(m,ns)*mims(ns))/(q(ns)*b)*iflr
rhox(1) = rhog*(1-tor)+rhog*grp*tor
rhoy(1) = rhog*gxdgyp/grp*tor
rhox(2) = -rhox(1)
rhoy(2) = -rhoy(1)
rhox(3) = 0
rhoy(3) = rhog*(1-tor)+rhog/b/grp*fp/radiusp*qhatp*lr0/q0*grcgtp*tor
rhox(4) = 0
rhoy(4) = -rhoy(3)
! calculate avg. e-field...
! do 1,2,4 point average, where lr is the no. of points...
phip=0.
exp1=0.
eyp=0.
ezp=0.
delbxp=0.
delbyp=0.
dpdzp = 0.
dadzp = 0.
aparp = 0.
!MVPT
delbxhp = 0.
delbyhp = 0.
dahdzp = 0.
aparhp = 0.
! 4 pt. avg. done explicitly for vectorization...
!$acc loop seq
!!$omp loop
do l=1,lr(1)
!
xs=x2(m,ns)+rhox(l) !rwx(1,l)*rhog
yt=y2(m,ns)+rhoy(l) !(rwy(1,l)+sz*rwx(1,l))*rhog
!
! particle can go out of bounds during gyroavg...
xt=mod(xs+800.*lx,lx)
yt=mod(yt+800.*ly,ly)
xt = min(xt,lx-1.0e-8)
yt = min(yt,ly-1.0e-8)
i=int(xt/dx+0.5)
j=int(yt/dy+0.5)
k=int(z2(m,ns)/dz+0.5)-gclr*kcnt
exp1= exp1 + ex(i,j,k)
eyp= eyp + ey(i,j,k)
ezp= ezp + ez(i,j,k)
delbxp= delbxp + delbx(i,j,k)
delbyp=delbyp+delby(i,j,k)
dpdzp=dpdzp+dpdz(i,j,k)
dadzp=dadzp+dadz(i,j,k)
aparp=aparp+apar(i,j,k)
if(ipbm==1)then
delbxhp=delbxhp+delbxh(i,j,k)
delbyhp=delbyhp+delbyh(i,j,k)
dahdzp=dahdzp+dahdz(i,j,k)
aparhp=aparhp+aparh(i,j,k)
endif
enddo
exp1 = exp1/4.
eyp = eyp/4.
ezp = ezp/4.
delbxp = delbxp/4.
delbyp = delbyp/4.
dpdzp = dpdzp/4.
dadzp = dadzp/4.
aparp = aparp/4.
!MVPT
delbxhp = delbxhp/4.
delbyhp = delbyhp/4.
dahdzp = dahdzp/4.
aparhp = aparhp/4.
!
vfac = 0.5*(mims(ns)*u2(m,ns)**2 + 2.*mu(m,ns)*b)
vp0 = 1./b**2*lr0/q0*qhatp*fp/radiusp*grcgtp
vp0 = vp0*vncp*vexbsw
vpar = u2(m,ns)-q(ns)/mims(ns)*aparp*nonlin(ns)*0.
bstar = b*(1+mims(ns)*vpar/(q(ns)*b)*bdcrvbp)
enerb=(mu(m,ns)+mims(ns)*vpar*vpar/b)/q(ns)*b/bstar*tor
kap = kapnp - (1.5-vfac/ter)*kaptp-vpar*mims(ns)/ter*vparspp*vparsw
dum1 = 1./b*lr0/q0*qhatp*fp/radiusp*grcgtp
vxdum = (eyp/b+vpar/b*delbxp)*dum1
xdot = vxdum*nonlin(ns)*b/bstar -iorb*enerb/bfldp/bfldp*fp/radiusp*dbdtp*grcgtp
ydot = (-exp1/b+vpar/b*delbyp)*dum1*b/bstar*nonlin(ns) &
+iorb*enerb/bfldp/bfldp*fp/radiusp*grcgtp* &
(-dydrp*dbdtp+r0/q0*qhatp*dbdrp)+vp0*b/bstar &
+enerb/(bfldp**2)*psipp*lr0/q0/radiusp**2*(dbdrp*grp**2+dbdtp*grdgtp) &
-mims(ns)*vpar**2/(q(ns)*bstar*b)*(psip2p*grp**2/radiusp+curvbzp)*lr0/(radiusp*q0) &
-dipdrp/radiusp*mims(ns)*vpar**2/(q(ns)*bstar*b)*grcgtp*lr0/q0*qhatp
!MVPT
zdot0 = vpar*b/bstar*(1-tor+tor*q0*br0/radiusp/b*psipp*grcgtp)/jfnp
zdot1 = q0*br0*enerb/(b*b)*fp/radiusp*dbdrp*grcgtp/jfnp &
-1./b**2*q0*br0*fp/radiusp*grcgtp*vncp*vexbsw/jfnp*b/bstar &
-dipdrp/radiusp*mims(ns)*vpar**2/(q(ns)*bstar*b)*q0*br0*grcgtp/jfnp
zdot = zdot0+zdot1
pzd0 = tor*(-mu(m,ns)/mims(ns)/radiusp/bfldp*psipp*dbdtp*grcgtp)*b/bstar &
+mu(m,ns)*vpar/(q(ns)*bstar*b)*dipdrp/radiusp*dbdtp*grcgtp
pzdot = pzd0 + (q(ns)/mims(ns)*ezp*q0*br0/radiusp/b*psipp*grcgtp/jfnp*b/bstar &
+q(ns)/mims(ns)*(-xdot*delbyp+ydot*delbxp+zdot*dadzp))*0.
edot = q(ns)*(xdot*exp1+(ydot-vp0)*eyp+zdot*ezp) &
+q(ns)*pzdot*aparp*tor &
+q(ns)*vpar*(-xdot*delbyp+ydot*delbxp+zdot*dadzp) &
-q(ns)*vpar*delbxp*vp0*b/bstar
!MVPT
if(ipbm==1)then
edot = q(ns)*(xdot*exp1+(ydot-vp0)*eyp+zdot*ezp-zdot0*bstar*ezp) &
+q(ns)*pzdot*aparhp*tor &
+q(ns)*vpar*(-xdot*delbyhp+ydot*delbxhp+zdot*dahdzp) &
-q(ns)*vpar*delbxhp*vp0*b/bstar
end if
x3(m,ns) = x2(m,ns) + 0.5*dt*xdot
y3(m,ns) = y2(m,ns) + 0.5*dt*ydot
z3(m,ns) = z2(m,ns) + 0.5*dt*zdot
u3(m,ns) = u2(m,ns) + 0.5*dt*pzdot
fdum = xnp/(2*pi*ter)**1.5*exp(-vfac/ter) *mims(ns)**1.5
if(ildu==0)gdum = fdum/xnp
if(ildu==1)gdum = 1./(2*pi*tgis(ns))**1.5*exp(-vfac/tgis(ns)) *mims(ns)**1.5
i = int(x2(m,ns)/dxsrc)
i = min(i,nxsrc-1)
i = max(i,0)
k = int(vfac/(ter*desrc))
k = min(k,nesrc-1)
if(igmrkr==1)gdum = gmrkr(ns,i,k)
fovg = fdum/gdum
avwixepsp = avwixeps(ns,i,k)
avwixezp = avwixez(ns,i,k)
dnisrcp = dnisrc(ns,i)
dnisrczp = dnisrcz(ns,i)
fisrcp = fisrc(ns,i)
dtp = dtiz(ns,int(xt/dx))
dum = 1-w2(m,ns)*nonlin(ns)*0.
vxdum = (eyp/b+vpar/b*delbxp)*dum1*b/bstar
! vxdum = eyp+vpar/b*delbxp
w3(m,ns)=w2(m,ns) + 0.5*dt*(vxdum*kap + edot/ter)*(fovg-w2(m,ns)*nonlin(ns)*isonew) - 0.5*dt*gammah*(avwixepsp-dnisrcp/(fisrcp+1e-8)*fovg) &
- 0.5*dt*ghzon*dnisrczp/(fisrcp+1e-8)*fovg &
- 0.5*dt*gamgtc*(vfac/ter-1.5)*dtp/ter*fovg &
- 0.5*dt*gamtoy*(1.5*sqrt(pi*vfac/ter)-3)*dtp/ter*fovg &
- 0.5*dt*gamgyro*avwixezp
! if(x3(ns,m)>lx .or. x3(ns,m)<0.)w3(ns,m) = 0.
if(itube==1)go to 333
if(abs(pzp-pzi(m,ns))>pzcrit(ns).or.abs(vfac-eki(m,ns))>0.5*eki(m,ns))then
!$acc atomic
!$omp atomic
mynopi = mynopi+1
x3(m,ns) = xii(m,ns)
z3(m,ns) = z0i(m,ns)
r = x3(m,ns)-lx/2+lr0
k = int(z3(m,ns)/delz)
wz0 = ((k+1)*delz-z3(m,ns))/delz
wz1 = 1-wz0
th = wz0*thfnz(k)+wz1*thfnz(k+1)
i = int((r-rin)/dr)
wx0 = (rin+(i+1)*dr-r)/dr
wx1 = 1.-wx0
k = int((th+pi)/dth)
wz0 = (-pi+(k+1)*dth-th)/dth
wz1 = 1.-wz0
b = wx0*wz0*bfld(i,k)+wx0*wz1*bfld(i,k+1) &
+wx1*wz0*bfld(i+1,k)+wx1*wz1*bfld(i+1,k+1)
u3(m,ns) = u0i(m,ns)
u2(m,ns) = u3(m,ns)
w3(m,ns) = 0.
w2(m,ns) = 0.
x2(m,ns) = x3(m,ns)
z2(m,ns) = z3(m,ns)
end if
333 continue
laps=anint((z3(m,ns)/lz)-.5)*(1-peritr)
r=x3(m,ns)-0.5*lx+lr0
i = int((r-rin)/dr)
i = min(i,nr-1)
i = max(i,0)
wx0 = (rin+(i+1)*dr-r)/dr
wx1 = 1.-wx0
qr = wx0*sf(i)+wx1*sf(i+1)
y3(m,ns)=mod(y3(m,ns)-laps*2*pi*qr*lr0/q0*sign(1.0,q0)+8000.*ly,ly)
if(x3(m,ns)>lx.and.iperidf==0)then
x3(m,ns) = lx-1.e-8
z3(m,ns)=lz-z3(m,ns)
x2(m,ns) = x3(m,ns)
z2(m,ns) = z3(m,ns)
w2(m,ns) = 0.
w3(m,ns) = 0.
end if
if(x3(m,ns)<0..and.iperidf==0)then
x3(m,ns) = 1.e-8
z3(m,ns)=lz-z3(m,ns)
x2(m,ns) = x3(m,ns)
z2(m,ns) = z3(m,ns)
w2(m,ns) = 0.
w3(m,ns) = 0.
end if
z3(m,ns)=mod(z3(m,ns)+8.*lz,lz)
x3(m,ns)=mod(x3(m,ns)+800.*lx,lx)
x3(m,ns) = min(x3(m,ns),lx-1.0e-8)
y3(m,ns) = min(y3(m,ns),ly-1.0e-8)
z3(m,ns) = min(z3(m,ns),lz-1.0e-8)
enddo
!$acc end parallel
!$omp end target teams
end_ppush_tm = MPI_WTIME()
tot_ppush_tm = tot_ppush_tm + end_ppush_tm - start_ppush_tm
call MPI_ALLREDUCE(mynopi,nopi(ns),1,MPI_integer, &
MPI_SUM, MPI_COMM_WORLD,ierr)
np_old=mm(ns)
start_init_pmove_tm = MPI_WTIME()
call test_init_pmove(z3(:,ns),np_old,lz,ierr)
end_init_pmove_tm = MPI_WTIME()
tot_init_pmove_tm = tot_init_pmove_tm + end_init_pmove_tm - start_init_pmove_tm
start_pmove_tm = MPI_WTIME()
call test_pmove(x2(:,ns),np_old,np_new,ierr)
if (ierr.ne.0) call ppexit