This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtime_advance.f90
2706 lines (2150 loc) · 96.1 KB
/
time_advance.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 time_advance
public :: init_time_advance, finish_time_advance
public :: advance_stella
public :: time_gke, time_parallel_nl
public :: checksum
private
interface get_dgdy
module procedure get_dgdy_2d
module procedure get_dgdy_3d
module procedure get_dgdy_4d
end interface
interface get_dgdx
module procedure get_dgdx_2d
module procedure get_dgdx_3d
module procedure get_dgdx_4d
end interface
interface checksum
module procedure checksum_field
module procedure checksum_dist
end interface
logical :: time_advance_initialized = .false.
logical :: wdriftinit = .false.
logical :: wstarinit = .false.
logical :: parnlinit = .false.
logical :: readinit = .false.
logical :: radialinit = .false.
logical :: driftimpinit = .false.
! if .true., dist fn is represented on alpha grid
! if .false., dist fn is given on k-alpha grid
! default is .false.; will only ever be set to
! .true. during full_flux_surface simulations
! logical :: alpha_space = .false.
integer :: explicit_option_switch
integer, parameter :: explicit_option_rk3 = 1, &
explicit_option_rk2 = 2, &
explicit_option_rk4 = 3
real :: xdriftknob, ydriftknob, wstarknob
logical :: flip_flop
complex, dimension (:,:,:), allocatable :: gamtot_drifts!, apar_denom_drifts
complex, dimension (:,:), allocatable :: gamtot3_drifts
! factor multiplying parallel nonlinearity
real, dimension (:,:), allocatable :: par_nl_fac
! factor multiplying higher order linear term in parallel acceleration
real, dimension (:,:), allocatable :: par_nl_curv
real, dimension (:,:), allocatable :: par_nl_driftx, par_nl_drifty
! needed for timing various pieces of gke solve
real, dimension (2,9) :: time_gke = 0.
real, dimension (2,2) :: time_parallel_nl = 0.
logical :: debug = .false.
contains
subroutine init_time_advance
use mp, only: proc0
use stella_transforms, only: init_transforms
use run_parameters, only: drifts_implicit
use physics_flags, only: radial_variation
use physics_flags, only: include_parallel_nonlinearity
use neoclassical_terms, only: init_neoclassical_terms
use dissipation, only: init_dissipation
use parallel_streaming, only: init_parallel_streaming
use mirror_terms, only: init_mirror
use flow_shear, only: init_flow_shear
implicit none
if (time_advance_initialized) return
time_advance_initialized = .true.
debug = debug .and. proc0
if (debug) write (6,*) 'time_advance::init_time_advance::read_parameters'
call read_parameters
if (debug) write (6,*) 'time_advance::init_time_advance::allocate_arrays'
call allocate_arrays
if (debug) write (6,*) 'time_advance::init_time_advance::init_neoclassical_terms'
call init_neoclassical_terms
if (debug) write (6,*) 'time_advance::init_time_advance::init_mirror'
call init_mirror
if (debug) write (6,*) 'time_advance::init_time_advance::init_parstream'
call init_parallel_streaming
if (debug) write (6,*) 'time_advance::init_time_advance::init_wdrift'
call init_wdrift
if (debug) write (6,*) 'time_advance::init_time_advance::init_wstar'
call init_wstar
if (debug) write (6,*) 'time_advance::init_time_advance::init_flow_shear'
call init_flow_shear
if (debug) write (6,*) 'time_advance::init_time_advance::init_parallel_nonlinearity'
if (include_parallel_nonlinearity) call init_parallel_nonlinearity
if (debug) write (6,*) 'time_advance::init_time_advance::init_radial_variation'
if (radial_variation) call init_radial_variation
if (debug) write (6,*) 'time_advance::init_time_advance::init_drifts_implicit'
if (drifts_implicit) call init_drifts_implicit
if (debug) write (6,*) 'time_advance::init_time_advance::init_dissipation'
call init_dissipation
if (debug) write (6,*) 'time_advance::init_time_advance::init_cfl'
call init_cfl
!call write_drifts
end subroutine init_time_advance
subroutine read_parameters
use file_utils, only: error_unit, input_unit_exist
use text_options, only: text_option, get_option_value
use mp, only: proc0, broadcast
use run_parameters, only: fully_explicit
implicit none
logical :: taexist
type (text_option), dimension (4), parameter :: explicitopts = &
(/ text_option('default', explicit_option_rk3), &
text_option('rk3', explicit_option_rk3), &
text_option('rk2', explicit_option_rk2), &
text_option('rk4', explicit_option_rk4) /)
character(10) :: explicit_option
namelist /time_advance_knobs/ xdriftknob, ydriftknob, wstarknob, explicit_option, flip_flop
integer :: ierr, in_file
if (readinit) return
readinit = .true.
if (proc0) then
explicit_option = 'default'
xdriftknob = 1.0
ydriftknob = 1.0
wstarknob = 1.0
flip_flop = .false.
in_file = input_unit_exist("time_advance_knobs", taexist)
if (taexist) read (unit=in_file, nml=time_advance_knobs)
ierr = error_unit()
call get_option_value &
(explicit_option, explicitopts, explicit_option_switch, &
ierr, "explicit_option in time_advance_knobs")
end if
call broadcast (explicit_option_switch)
call broadcast (xdriftknob)
call broadcast (ydriftknob)
call broadcast (wstarknob)
call broadcast (flip_flop)
if (fully_explicit) flip_flop = .false.
end subroutine read_parameters
! subroutine write_drifts
! use dist_fn_arrays, only: wdriftx_g, wdrifty_g
! use dist_fn_arrays, only: wdriftx_phi, wdrifty_phi
! use dist_fn_arrays, only: wstar, wstarp
! use dist_fn_arrays, only: wdriftpx_g, wdriftpy_g
! use dist_fn_arrays, only: wdriftpx_phi, wdriftpy_phi
! use zgrid, only: nzgrid
! use stella_layouts, only: vmu_lo
! use file_utils, only: run_name
!
! implicit none
! integer ia, iz, ivmu
! character(len=512) :: filename
! ia=1
! filename=trim(run_name)//".drifts"
! open(3345,file=trim(filename),status='unknown')
! do ivmu = vmu_lo%llim_proc, vmu_lo%ulim_proc
! do iz= -nzgrid, nzgrid
! write(3345,'(10e25.8)') wstar(ia,iz,ivmu),wstarp(ia,iz,ivmu), &
! wdriftx_g(ia,iz,ivmu), wdriftpx_g(ia,iz,ivmu), &
! wdrifty_g(ia,iz,ivmu), wdriftpy_g(ia,iz,ivmu), &
! wdriftx_phi(ia,iz,ivmu), wdriftpx_phi(ia,iz,ivmu), &
! wdrifty_phi(ia,iz,ivmu), wdriftpy_phi(ia,iz,ivmu)
! enddo
! enddo
! close (3345)
! end subroutine write_drifts
subroutine init_wdrift
use dist_fn_arrays, only: wdriftx_g, wdrifty_g
use dist_fn_arrays, only: wdriftx_phi, wdrifty_phi
use stella_layouts, only: vmu_lo
use stella_layouts, only: iv_idx, imu_idx, is_idx
use stella_time, only: code_dt
use species, only: spec
use zgrid, only: nzgrid
use kt_grids, only: nalpha
use stella_geometry, only: cvdrift, gbdrift
use stella_geometry, only: cvdrift0, gbdrift0
use stella_geometry, only: gds23, gds24
use stella_geometry, only: geo_surf, q_as_x
use stella_geometry, only: dxdXcoord, drhodpsi, dydalpha
use vpamu_grids, only: vpa, vperp2
use vpamu_grids, only: maxwell_vpa, maxwell_mu, maxwell_fac
use neoclassical_terms, only: include_neoclassical_terms
use neoclassical_terms, only: dphineo_dzed, dphineo_drho, dphineo_dalpha
use neoclassical_terms, only: dfneo_dvpa, dfneo_dzed, dfneo_dalpha
implicit none
integer :: ivmu, iv, imu, is, ia
real :: fac
real, dimension (:,:), allocatable :: wcvdrifty, wgbdrifty
real, dimension (:,:), allocatable :: wcvdriftx, wgbdriftx
if (wdriftinit) return
wdriftinit = .true.
if (.not.allocated(wdriftx_phi)) then
allocate (wdriftx_phi(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
wdriftx_phi = 0.0
endif
if (.not.allocated(wdrifty_phi)) then
allocate (wdrifty_phi(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
wdrifty_phi = 0.0
endif
if (.not.allocated(wdriftx_g)) then
allocate (wdriftx_g(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
wdriftx_g = 0.0
endif
if (.not.allocated(wdrifty_g)) then
allocate (wdrifty_g(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
wdrifty_g = 0.0
endif
allocate (wcvdrifty(nalpha,-nzgrid:nzgrid))
allocate (wgbdrifty(nalpha,-nzgrid:nzgrid))
allocate (wcvdriftx(nalpha,-nzgrid:nzgrid))
allocate (wgbdriftx(nalpha,-nzgrid:nzgrid))
ia = 1
! FLAG -- need to deal with shat=0 case. ideally move away from q as x-coordinate
do ivmu = vmu_lo%llim_proc, vmu_lo%ulim_proc
iv = iv_idx(vmu_lo,ivmu)
imu = imu_idx(vmu_lo,ivmu)
is = is_idx(vmu_lo,ivmu)
fac = -ydriftknob*0.5*code_dt*spec(is)%tz_psi0
! this is the curvature drift piece of wdrifty with missing factor of vpa
! vpa factor is missing to avoid singularity when including
! non-Maxwellian corrections to equilibrium
wcvdrifty = fac*cvdrift*vpa(iv)
! this is the grad-B drift piece of wdrifty
wgbdrifty = fac*gbdrift*0.5*vperp2(:,:,imu)
wdrifty_g(:,:,ivmu) = wcvdrifty*vpa(iv) + wgbdrifty
! if including neoclassical correction to equilibrium Maxwellian,
! then add in v_E^{nc} . grad y dg/dy coefficient here
if (include_neoclassical_terms) then
wdrifty_g(:,:,ivmu) = wdrifty_g(:,:,ivmu)+code_dt*0.5*(gds23*dphineo_dzed &
+ drhodpsi*dydalpha*dphineo_drho)
end if
wdrifty_phi(:,:,ivmu) = spec(is)%zt*(wgbdrifty + wcvdrifty*vpa(iv)) &
* maxwell_vpa(iv,is)*maxwell_mu(:,:,imu,is)*maxwell_fac(is)
! if including neoclassical corrections to equilibrium,
! add in -(Ze/m) * v_curv/vpa . grad y d<phi>/dy * dF^{nc}/dvpa term
! and v_E . grad z dF^{nc}/dz (here get the dphi/dy part of v_E)
if (include_neoclassical_terms) then
wdrifty_phi(:,:,ivmu) = wdrifty_phi(:,:,ivmu) &
- 0.5*spec(is)%zt*dfneo_dvpa(:,:,ivmu)*wcvdrifty &
- code_dt*0.5*dfneo_dzed(:,:,ivmu)*gds23
end if
if(q_as_x) then
fac = -xdriftknob*0.5*code_dt*spec(is)%tz_psi0
else
fac = -xdriftknob*0.5*code_dt*spec(is)%tz_psi0/geo_surf%shat
endif
! this is the curvature drift piece of wdriftx with missing factor of vpa
! vpa factor is missing to avoid singularity when including
! non-Maxwellian corrections to equilibrium
wcvdriftx = fac*cvdrift0*vpa(iv)
! this is the grad-B drift piece of wdriftx
wgbdriftx = fac*gbdrift0*0.5*vperp2(:,:,imu)
wdriftx_g(:,:,ivmu) = wcvdriftx*vpa(iv) + wgbdriftx
! if including neoclassical correction to equilibrium Maxwellian,
! then add in v_E^{nc} . grad x dg/dx coefficient here
if (include_neoclassical_terms) then
wdriftx_g(:,:,ivmu) = wdriftx_g(:,:,ivmu)+code_dt*0.5*(gds24*dphineo_dzed &
- dxdXcoord*dphineo_dalpha)
end if
wdriftx_phi(:,:,ivmu) = spec(is)%zt*(wgbdriftx + wcvdriftx*vpa(iv)) &
* maxwell_vpa(iv,is)*maxwell_mu(:,:,imu,is)*maxwell_fac(is)
! if including neoclassical corrections to equilibrium,
! add in (Ze/m) * v_curv/vpa . grad x d<phi>/dx * dF^{nc}/dvpa term
! and v_E . grad z dF^{nc}/dz (here get the dphi/dx part of v_E)
! and v_E . grad alpha dF^{nc}/dalpha (dphi/dx part of v_E)
if (include_neoclassical_terms) then
wdriftx_phi(:,:,ivmu) = wdriftx_phi(:,:,ivmu) &
- 0.5*spec(is)%zt*dfneo_dvpa(:,:,ivmu)*wcvdriftx &
+ code_dt*0.5*(dfneo_dalpha(:,:,ivmu)*dxdXcoord-dfneo_dzed(:,:,ivmu)*gds24)
end if
end do
deallocate (wcvdriftx, wgbdriftx, wcvdrifty, wgbdrifty)
end subroutine init_wdrift
subroutine init_wstar
use stella_layouts, only: vmu_lo
use stella_layouts, only: iv_idx, imu_idx, is_idx
use stella_time, only: code_dt
use species, only: spec
use zgrid, only: nzgrid
use kt_grids, only: nalpha
use stella_geometry, only: dydalpha, drhodpsi
use vpamu_grids, only: vperp2, vpa
use vpamu_grids, only: maxwell_vpa, maxwell_mu, maxwell_fac
use dist_fn_arrays, only: wstar
use neoclassical_terms, only: include_neoclassical_terms
use neoclassical_terms, only: dfneo_drho
implicit none
integer :: is, imu, iv, ivmu
real, dimension (:,:), allocatable :: energy
if (wstarinit) return
wstarinit = .true.
if (.not.allocated(wstar)) &
allocate (wstar(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc)) ; wstar = 0.0
allocate (energy(nalpha,-nzgrid:nzgrid))
do ivmu = vmu_lo%llim_proc, vmu_lo%ulim_proc
is = is_idx(vmu_lo,ivmu)
imu = imu_idx(vmu_lo,ivmu)
iv = iv_idx(vmu_lo,ivmu)
energy = (vpa(iv)**2 + vperp2(:,:,imu))*(spec(is)%temp_psi0/spec(is)%temp)
if (include_neoclassical_terms) then
wstar(:,:,ivmu) = dydalpha*drhodpsi*wstarknob*0.5*code_dt &
* (maxwell_vpa(iv,is)*maxwell_mu(:,:,imu,is)*maxwell_fac(is) &
* (spec(is)%fprim+spec(is)%tprim*(energy-1.5)) &
- dfneo_drho(:,:,ivmu))
else
wstar(:,:,ivmu) = dydalpha*drhodpsi*wstarknob*0.5*code_dt &
* maxwell_vpa(iv,is)*maxwell_mu(:,:,imu,is)*maxwell_fac(is) &
* (spec(is)%fprim+spec(is)%tprim*(energy-1.5))
end if
end do
deallocate (energy)
end subroutine init_wstar
subroutine init_drifts_implicit
use constants, only: zi
use mp, only: sum_allreduce, mp_abort
use stella_layouts, only: vmu_lo
use stella_layouts, only: iv_idx, imu_idx, is_idx
use gyro_averages, only: aj0x
use dist_fn_arrays, only: wdriftx_phi, wdrifty_phi
use dist_fn_arrays, only: wdriftx_g, wdrifty_g
use dist_fn_arrays, only: wstar
use fields_arrays, only: gamtot
use fields, only: efac
use run_parameters, only: fphi, fapar, time_upwind
use species, only: spec, has_electron_species
use stella_geometry, only: dl_over_b
use zgrid, only: nzgrid
use vpamu_grids, only: integrate_species
use species, only: spec
use kt_grids, only: naky, nakx, aky, akx, zonal_mode
use dist_fn, only: adiabatic_option_switch
use dist_fn, only: adiabatic_option_fieldlineavg
implicit none
integer :: ivmu, iz, ikx, is, ia, iv, imu
complex :: tmps
complex, dimension (:,:,:), allocatable :: g0
complex, dimension (:,:), allocatable :: wd_g, wd_phi, wstr, tmp
if (driftimpinit) return
driftimpinit = .true.
ia = 1
allocate (wd_g(naky,nakx))
allocate (wd_phi(naky,nakx))
allocate (wstr(naky,nakx))
allocate (tmp(naky,nakx))
if (.not.allocated(gamtot_drifts)) &
allocate (gamtot_drifts(naky,nakx,-nzgrid:nzgrid))
gamtot_drifts = 0.
if (.not.allocated(gamtot3_drifts)) &
allocate (gamtot3_drifts(nakx,-nzgrid:nzgrid))
gamtot3_drifts = 0.
! if (.not.allocated(apar_denom_drifts)) &
! allocate (apar_denom_wstar(naky,nakx,-nzgrid:nzgrid))
! apar_denom_wstar = 0.
if (fphi > epsilon(0.0)) then
allocate (g0(naky,nakx,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
do iz = -nzgrid, nzgrid
do ivmu = vmu_lo%llim_proc, vmu_lo%ulim_proc
iv = iv_idx(vmu_lo,ivmu)
imu = imu_idx(vmu_lo,ivmu)
is = is_idx(vmu_lo,ivmu)
!there terms already contain a factor of code_dt as well as
!a negative sign to account for RHS
wd_g = -zi*(spread(akx,1,naky)*wdriftx_g(ia,iz,ivmu) &
+ spread(aky,2,nakx)*wdrifty_g(ia,iz,ivmu))
wd_phi = -zi*(spread(akx,1,naky)*wdriftx_phi(ia,iz,ivmu) &
+ spread(aky,2,nakx)*wdrifty_phi(ia,iz,ivmu))
wstr = -zi*spread(aky,2,nakx)*wstar(ia,iz,ivmu)
g0(:,:,ivmu) = 0.5*(1.0+time_upwind)*aj0x(:,:,iz,ivmu)**2 &
*(wd_phi + wstr)/(1.0 + 0.5*(1.0+time_upwind)*wd_g)
enddo
call integrate_species (g0, iz, spec%z*spec%dens_psi0, gamtot_drifts(:,:,iz))
enddo
gamtot_drifts = gamtot_drifts + gamtot
deallocate (g0)
if (.not.has_electron_species(spec)) then
! no need to do anything extra for ky /= 0 because
! already accounted for in gamtot_h
if (adiabatic_option_switch == adiabatic_option_fieldlineavg) then
if (zonal_mode(1)) then
do ikx = 1, nakx
! do not need kx=ky=0 mode
if (abs(akx(ikx)) < epsilon(0.)) cycle
tmps = 1.0/efac - sum(dl_over_b(ia,:)/gamtot_drifts(1,ikx,:))
gamtot3_drifts(ikx,:) = 1./(gamtot_drifts(1,ikx,:)*tmps)
end do
if (akx(1) < epsilon(0.)) gamtot3_drifts(1,:) = 0.0
end if
end if
end if
end if
deallocate (wd_g, wd_phi, wstr, tmp)
! FLAG -- NEED TO SORT OUT FINITE FAPAR FOR GSTAR
if (fapar > epsilon(0.)) then
write (*,*) 'APAR NOT SETUP FOR GSTAR YET. aborting'
call mp_abort ('APAR NOT SETUP FOR GSTAR YET. aborting')
end if
! allocate (g0(-nvgrid:nvgrid,nmu))
! do ikxkyz = kxkyz_lo%llim_proc, kxkyz_lo%ulim_proc
! iky = iky_idx(kxkyz_lo,ikxkyz)
! ikx = ikx_idx(kxkyz_lo,ikxkyz)
! ig = iz_idx(kxkyz_lo,ikxkyz)
! is = is_idx(kxkyz_lo,ikxkyz)
! g0 = spread(vpa**2,2,nmu)*spread(aj0v(:,ikxkyz)**2,1,nvpa)*anon(ig,:,:)
! wgt = 2.0*beta*spec(is)%z*spec(is)%z*spec(is)%dens/spec(is)%mass
! call integrate_vmu (g0, ig, tmp)
! apar_denom(iky,ikx,ig) = apar_denom(iky,ikx,ig) + tmp*wgt
! end do
! call sum_allreduce (apar_denom)
! apar_denom = apar_denom + kperp2
! deallocate (g0)
! end if
end subroutine init_drifts_implicit
subroutine init_parallel_nonlinearity
use physics_parameters, only: rhostar
use species, only: spec, nspec
use zgrid, only: nztot, nzgrid
use kt_grids, only: akx, aky
use kt_grids, only: nakx, naky
use stella_geometry, only: geo_surf, drhodpsi
use stella_geometry, only: gradpar, dbdzed, bmag
use stella_geometry, only: cvdrift, cvdrift0
implicit none
if (.not. allocated(par_nl_fac)) allocate (par_nl_fac(-nzgrid:nzgrid,nspec))
! this is the factor multiplying -dphi/dz * dg/dvpa in the parallel nonlinearity
par_nl_fac = 0.5*rhostar*spread(spec%stm*spec%zt,1,nztot)*spread(gradpar,2,nspec)
if (.not. allocated(par_nl_curv)) allocate (par_nl_curv(-nzgrid:nzgrid,nspec))
! ydriftknob is here because this term comes from bhat x curvature . grad B
par_nl_curv = -ydriftknob*rhostar*geo_surf%rgeo*geo_surf%betaprim*drhodpsi &
*spread(dbdzed(1,:)*gradpar/bmag(1,:),2,nspec)/spread(spec%zt,1,nztot)
if (.not. allocated(par_nl_drifty)) allocate (par_nl_drifty(naky,-nzgrid:nzgrid))
par_nl_drifty = 0.25*rhostar*spread(aky,2,nztot)*spread(cvdrift(1,:),1,naky)
if (.not. allocated(par_nl_drifty)) allocate (par_nl_driftx(nakx,-nzgrid:nzgrid))
par_nl_driftx = 0.25*rhostar*spread(akx/geo_surf%shat,2,nztot)*spread(cvdrift0(1,:),1,nakx)
parnlinit = .true.
end subroutine init_parallel_nonlinearity
subroutine init_radial_variation
use stella_layouts, only: vmu_lo
use stella_layouts, only: iv_idx, imu_idx, is_idx
use stella_time, only: code_dt
use species, only: spec, pfac
use zgrid, only: nzgrid
use kt_grids, only: nalpha
use stella_geometry, only: drhodpsi, dydalpha, gfac
use stella_geometry, only: dBdrho, geo_surf, q_as_x
use stella_geometry, only: dcvdriftdrho, dcvdrift0drho
use stella_geometry, only: dgbdriftdrho, dgbdrift0drho
use vpamu_grids, only: vperp2, vpa, mu
use vpamu_grids, only: maxwell_vpa, maxwell_mu, maxwell_fac
use dist_fn_arrays, only: wstarp
use dist_fn_arrays, only: wdrifty_phi
use dist_fn_arrays, only: wdriftpx_g, wdriftpy_g
use dist_fn_arrays, only: wdriftpx_phi, wdriftpy_phi, adiabatic_phi
! use neoclassical_terms, only: include_neoclassical_terms
implicit none
integer :: is, imu, iv, ivmu
real :: fac
real, dimension (:,:), allocatable :: energy
real, dimension (:,:), allocatable :: wcvdrifty, wgbdrifty
real, dimension (:,:), allocatable :: wcvdriftx, wgbdriftx
!wstar
if (radialinit) return
radialinit = .true.
allocate (wcvdrifty(nalpha,-nzgrid:nzgrid))
allocate (wgbdrifty(nalpha,-nzgrid:nzgrid))
allocate (wcvdriftx(nalpha,-nzgrid:nzgrid))
allocate (wgbdriftx(nalpha,-nzgrid:nzgrid))
if (.not.allocated(wstarp)) &
allocate (wstarp(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc)) ; wstarp = 0.0
if (.not.allocated(wdriftpx_phi)) &
allocate (wdriftpx_phi(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
if (.not.allocated(adiabatic_phi)) &
allocate (adiabatic_phi(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
if (.not.allocated(wdriftpy_phi)) &
allocate (wdriftpy_phi(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
if (.not.allocated(wdriftpx_g)) &
allocate (wdriftpx_g(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
if (.not.allocated(wdriftpy_g)) &
allocate (wdriftpy_g(nalpha,-nzgrid:nzgrid,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
allocate (energy(nalpha,-nzgrid:nzgrid))
do ivmu = vmu_lo%llim_proc, vmu_lo%ulim_proc
is = is_idx(vmu_lo,ivmu)
imu = imu_idx(vmu_lo,ivmu)
iv = iv_idx(vmu_lo,ivmu)
energy = (vpa(iv)**2 + vperp2(:,:,imu))*(spec(is)%temp_psi0/spec(is)%temp)
!FLAG DSO - THIS NEEDS TO BE ADDED SOMEDAY!
!if (include_neoclassical_terms) then
! wstarp(:,:,ivmu) = dydalpha*drhodpsi*wstarknob*0.5*code_dt &
! * (maxwell_vpa(iv)*maxwell_mu(:,:,imu) &
! * (spec(is)%fprim+spec(is)%tprim*(energy-1.5)) &
! - dfneo_drho(:,:,ivmu))
!else
!recall that fprim = -dn/dr and trpim = -dt/dr
wstarp(:,:,ivmu) = -wstarknob*0.5*code_dt &
* dydalpha*drhodpsi*maxwell_vpa(iv,is)*maxwell_mu(:,:,imu,is)*maxwell_fac(is) &
* (pfac*(spec(is)%d2ndr2-(spec(is)%fprim)**2-(spec(is)%tprim)**2*energy) &
+ pfac*(spec(is)%d2Tdr2-(spec(is)%tprim)**2)*(energy-1.5) &
- gfac*2*spec(is)%tprim*mu(imu)*spread(dBdrho,1,nalpha) &
+ (spec(is)%fprim+spec(is)%tprim*(energy-1.5)) &
* ( pfac*(spec(is)%fprim+spec(is)%tprim*(energy-1.5)) &
+ gfac*2*mu(imu)*spread(dBdrho,1,nalpha) &
+ gfac*drhodpsi*geo_surf%d2psidr2))
!end if
!wdrift
fac = -ydriftknob*0.5*code_dt*spec(is)%tz_psi0
! this is the curvature drift piece of wdrifty with missing factor of vpa
! vpa factor is missing to avoid singularity when including
! non-Maxwellian corrections to equilibrium
wcvdrifty = gfac*fac*dcvdriftdrho*vpa(iv)
! this is the grad-B drift piece of wdrifty
wgbdrifty = gfac*fac*dgbdriftdrho*0.5*vperp2(:,:,imu)
wdriftpy_g(:,:,ivmu) = wcvdrifty*vpa(iv) + wgbdrifty
wdriftpy_phi(:,:,ivmu) = spec(is)%zt*(wgbdrifty + wcvdrifty*vpa(iv)) &
* maxwell_vpa(iv,is)*maxwell_mu(:,:,imu,is)*maxwell_fac(is) &
- wdrifty_phi(:,:,ivmu)*(pfac*(spec(is)%fprim + spec(is)%tprim*(energy-2.5)) &
+ gfac*2.*mu(imu)*spread(dBdrho,1,nalpha))
if(q_as_x) then
fac = -xdriftknob*0.5*code_dt*spec(is)%tz_psi0
else
fac = -xdriftknob*0.5*code_dt*spec(is)%tz_psi0/geo_surf%shat
endif
! this is the curvature drift piece of wdriftx with missing factor of vpa
! vpa factor is missing to avoid singularity when including
! non-Maxwellian corrections to equilibrium
wcvdriftx = gfac*fac*dcvdrift0drho*vpa(iv)
! this is the grad-B drift piece of wdriftx
wgbdriftx = gfac*fac*dgbdrift0drho*0.5*vperp2(:,:,imu)
wdriftpx_g(:,:,ivmu) = wgbdriftx + wcvdriftx*vpa(iv)
!the next piece is everything under the x derivative, as this needs to be
!transformed separately
wdriftpx_phi(:,:,ivmu) = spec(is)%zt*(wgbdriftx + wcvdriftx*vpa(iv)) &
* maxwell_vpa(iv,is)*maxwell_mu(:,:,imu,is)*maxwell_fac(is)
!this is variation in the Maxwellian part of the adiabatic response of phi,
!which needs to be transformed separately before differentiation wrt x
!the gyroaveraging and quasineutrality is already done in fields
adiabatic_phi(:,:,ivmu) = -(pfac*(spec(is)%fprim+spec(is)%tprim*(energy-2.5)) &
+gfac*2.*mu(imu)*spread(dBdrho,1,nalpha))
end do
deallocate (energy, wcvdriftx, wgbdriftx, wcvdrifty, wgbdrifty)
end subroutine init_radial_variation
subroutine allocate_arrays
use stella_layouts, only: vmu_lo
use zgrid, only: nzgrid, ntubes
use kt_grids, only: naky, nakx
use dist_fn_arrays, only: g0, g1, g2, g3
implicit none
if (.not.allocated(g0)) &
allocate (g0(naky,nakx,-nzgrid:nzgrid,ntubes,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
g0 = 0.
if (.not.allocated(g1)) &
allocate (g1(naky,nakx,-nzgrid:nzgrid,ntubes,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
g1 = 0.
if (.not.allocated(g2)) &
allocate (g2(naky,nakx,-nzgrid:nzgrid,ntubes,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
g2 = 0.
if (.not.allocated(g3) .and. explicit_option_switch==explicit_option_rk4) then
allocate (g3(naky,nakx,-nzgrid:nzgrid,ntubes,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
g3 = 0.
else
allocate (g3(1,1,1,1,1))
end if
end subroutine allocate_arrays
subroutine init_cfl
use mp, only: proc0, nproc, max_allreduce, min_allreduce
use mp, only: scope, allprocs, subprocs
use dist_fn_arrays, only: wdriftx_g, wdrifty_g
use stella_time, only: cfl_dt, code_dt, write_dt
use run_parameters, only: cfl_cushion
use physics_flags, only: radial_variation, prp_shear_enabled
use zgrid, only: delzed
use vpamu_grids, only: dvpa
use kt_grids, only: akx, aky, nx, rho
use run_parameters, only: stream_implicit, mirror_implicit, drifts_implicit
use parallel_streaming, only: stream
use parallel_streaming, only: stream_rad_var1, stream_rad_var2
use mirror_terms, only: mirror
use flow_shear, only: prl_shear, shift_times
use file_utils, only: runtype_option_switch, runtype_multibox
use dissipation, only: include_collisions, collisions_implicit
use dissipation, only: vpa_operator, mu_operator
use dissipation, only: cfl_dt_vpadiff, cfl_dt_mudiff
implicit none
real :: cfl_dt_mirror, cfl_dt_stream, cfl_dt_shear
real :: cfl_dt_wdriftx, cfl_dt_wdrifty
real :: zero
real :: wdriftx_max, wdrifty_max
! avoid divide by zero in cfl_dt terms below
zero = 100.*epsilon(0.)
! FLAG -- assuming equal spacing in zed!
if (cfl_dt.lt.0) cfl_dt = code_dt/cfl_cushion
if(.not.drifts_implicit) then
! get the local max value of wdriftx on each processor
wdriftx_max = maxval(abs(wdriftx_g))
! compare these max values across processors to get global max
if (nproc > 1) then
call max_allreduce (wdriftx_max)
endif
! NB: wdriftx_g has code_dt built-in, which accounts for code_dt factor here
cfl_dt_wdriftx = abs(code_dt)/max(maxval(abs(akx))*wdriftx_max,zero)
cfl_dt = cfl_dt_wdriftx
endif
cfl_dt_shear = abs(code_dt)/max(maxval(abs(aky))*maxval(abs(prl_shear)),zero)
cfl_dt = min(cfl_dt,cfl_dt_shear)
if(prp_shear_enabled) then
cfl_dt_shear = minval(shift_times)
cfl_dt = min(cfl_dt,cfl_dt_shear)
endif
if (.not.stream_implicit) then
! NB: stream has code_dt built-in, which accounts for code_dt factor here
cfl_dt_stream = abs(code_dt)*delzed(0)/max(maxval(abs(stream)),zero)
cfl_dt = min(cfl_dt,cfl_dt_stream)
end if
if (.not.mirror_implicit) then
! NB: mirror has code_dt built-in, which accounts for code_dt factor here
cfl_dt_mirror = abs(code_dt)*dvpa/max(maxval(abs(mirror)),zero)
cfl_dt = min(cfl_dt,cfl_dt_mirror)
end if
if (radial_variation) then
!while other quantities should go here, parallel streaming with electrons
!is what will limit us
cfl_dt_stream = abs(code_dt)*delzed(0)/max(maxval(abs(stream_rad_var1)),zero)
cfl_dt_stream = cfl_dt_stream/abs(rho(nx)+zero)
cfl_dt = min(cfl_dt,cfl_dt_stream)
cfl_dt_stream = abs(code_dt)*delzed(0)/max(maxval(abs(stream_rad_var2)),zero)
cfl_dt_stream = cfl_dt_stream/abs(rho(nx)+zero)
cfl_dt = min(cfl_dt,cfl_dt_stream)
end if
if (include_collisions.and..not.collisions_implicit) then
if (vpa_operator) cfl_dt = min(cfl_dt,cfl_dt_vpadiff)
if (mu_operator) cfl_dt = min(cfl_dt,cfl_dt_mudiff)
endif
if(.not.drifts_implicit) then
! get the local max value of wdrifty on each processor
wdrifty_max = maxval(abs(wdrifty_g))
! compare these max values across processors to get global max
if (nproc > 1) then
call max_allreduce (wdrifty_max)
endif
! NB: wdrifty_g has code_dt built-in, which accounts for code_dt factor here
cfl_dt_wdrifty = abs(code_dt)/max(maxval(abs(aky))*wdrifty_max,zero)
cfl_dt = min(cfl_dt,cfl_dt_wdrifty)
endif
if(runtype_option_switch == runtype_multibox) call scope(allprocs)
call min_allreduce (cfl_dt)
if(runtype_option_switch == runtype_multibox) call scope(subprocs)
if (proc0) then
write (*,'(A)') "############################################################"
write (*,'(A)') " CFL CONDITION"
write (*,'(A)') "############################################################"
write (*,'(A16)') 'LINEAR CFL_DT: '
if (.not.drifts_implicit) write (*,'(A12,ES12.4)') ' wdriftx: ', cfl_dt_wdriftx
if (.not.drifts_implicit) write (*,'(A12,ES12.4)') ' wdrifty: ', cfl_dt_wdrifty
if (.not.stream_implicit) write (*,'(A12,ES12.4)') ' stream: ', cfl_dt_stream
if (.not.mirror_implicit) write (*,'(A12,ES12.4)') ' mirror: ', cfl_dt_mirror
write (*,*)
end if
if (abs(code_dt) > cfl_dt*cfl_cushion) then
if (proc0) then
write (*,*) 'CHANGING TIME STEP:'
write (*,'(A16, ES10.2E2)') " code_dt:"//REPEAT(' ',50),code_dt
write (*,'(A16, ES10.2E2)') " cfl_dt:"//REPEAT(' ',50),cfl_dt
write (*,'(A16, ES10.2E2)') " cfl_cushion:"//REPEAT(' ',50),cfl_cushion
write (*,'(A65)') ' ==> User-specified delt is larger than cfl_dt*cfl_cushion.'//REPEAT(' ',50)
write (*,'(A49,ES12.4)') ' ==> Changing code_dt to cfl_dt*cfl_cushion ='//REPEAT(' ',50), cfl_dt*cfl_cushion
end if
code_dt = sign(1.0,code_dt)*cfl_dt*cfl_cushion
call reset_dt
else if (proc0) then
call write_dt
write (*,*)
end if
end subroutine init_cfl
subroutine reset_dt
use parallel_streaming, only: parallel_streaming_initialized
use parallel_streaming, only: init_parallel_streaming
use dissipation, only: init_collisions, collisions_initialized, include_collisions
use run_parameters, only: stream_implicit, driftkinetic_implicit, drifts_implicit
use response_matrix, only: response_matrix_initialized
use response_matrix, only: init_response_matrix
use mirror_terms, only: mirror_initialized
use mirror_terms, only: init_mirror
use flow_shear, only: flow_shear_initialized
use flow_shear, only: init_flow_shear
use physics_flags, only: radial_variation
implicit none
! need to recompute mirror and streaming terms
! to account for updated code_dt
wdriftinit = .false.
wstarinit = .false.
radialinit = .false.
driftimpinit = .false.
flow_shear_initialized = .false.
mirror_initialized = .false.
parallel_streaming_initialized = .false.
call init_wstar
call init_wdrift
call init_mirror
call init_parallel_streaming
call init_flow_shear
if (radial_variation) call init_radial_variation
if (drifts_implicit) call init_drifts_implicit
if (include_collisions) then
collisions_initialized = .false.
call init_collisions
endif
! do not try to re-init response matrix
! before it has been initialized the first time
if ((stream_implicit.or.driftkinetic_implicit) .and. response_matrix_initialized) then
response_matrix_initialized = .false.
call init_response_matrix
end if
end subroutine reset_dt
subroutine advance_stella (istep)
use dist_fn_arrays, only: gold, gnew
use fields_arrays, only: phi, apar
use fields_arrays, only: phi_old
use fields, only: advance_fields, fields_updated
use run_parameters, only: fully_explicit
use multibox, only: RK_step
use dissipation, only: include_krook_operator, update_delay_krook
use dissipation, only: remove_zero_projection, project_out_zero
use zgrid, only: nzgrid, ntubes
use kt_grids, only: nakx
use stella_layouts, only: vmu_lo
implicit none
integer, intent (in) :: istep
complex, allocatable, dimension (:,:,:,:) :: g1
if(.not.RK_step) then
if (debug) write (*,*) 'time_advance::multibox'
call mb_communicate(gnew)
endif
! save value of phi
! for use in diagnostics (to obtain frequency)
fields_updated = .false.
call advance_fields (gnew, phi, apar, dist='gbar')
phi_old = phi
! reverse the order of operations every time step
! as part of alternating direction operator splitting
! this is needed to ensure 2nd order accuracy in time
if (mod(istep,2)==1 .or. .not.flip_flop) then
! advance the explicit parts of the GKE
call advance_explicit (gnew)
! enforce periodicity for zonal mode
! if (zonal_mode(1)) gnew(1,:,-nzgrid,:) = gnew(1,:,nzgrid,:)
! use operator splitting to separately evolve
! all terms treated implicitly
if (.not.fully_explicit) call advance_implicit (istep, phi, apar, gnew)
else
if (.not.fully_explicit) call advance_implicit (istep, phi, apar, gnew)
call advance_explicit (gnew)
end if
if(remove_zero_projection) then
allocate (g1(nakx,-nzgrid:nzgrid,ntubes,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
g1 = gnew(1,:,:,:,:) - gold(1,:,:,:,:)
call project_out_zero(g1)
gnew(1,:,:,:,:) = gnew(1,:,:,:,:) - g1
deallocate (g1)
end if
!update the delay parameters for the Krook operator
if(include_krook_operator) call update_delay_krook(gnew)
gold = gnew
end subroutine advance_stella
! subroutine advance_explicit (phi, apar, g)
subroutine advance_explicit (g)
use mp, only: proc0
use job_manage, only: time_message
use zgrid, only: nzgrid
use extended_zgrid, only: periodic
use kt_grids, only: naky
use stella_layouts, only: vmu_lo, iv_idx
use parallel_streaming, only: stream_sign
implicit none
! complex, dimension (:,:,-nzgrid:), intent (in out) :: phi, apar
complex, dimension (:,:,-nzgrid:,:,vmu_lo%llim_proc:), intent (in out) :: g
integer :: ivmu, iv, sgn, iky
! start the timer for the explicit part of the solve
if (proc0) call time_message(.false.,time_gke(:,8),' explicit')
select case (explicit_option_switch)
case (explicit_option_rk2)
! SSP RK2
call advance_explicit_rk2 (g)
case (explicit_option_rk3)
! default is SSP RK3
call advance_explicit_rk3 (g)
case (explicit_option_rk4)
! RK4
call advance_explicit_rk4 (g)
end select
! enforce periodicity for periodic (including zonal) modes
do iky = 1, naky
if (periodic(iky)) then
do ivmu = vmu_lo%llim_proc, vmu_lo%ulim_proc
iv = iv_idx(vmu_lo,ivmu)
! stream_sign > 0 corresponds to dz/dt < 0
sgn = stream_sign(iv)
g(iky,:,sgn*nzgrid,:,ivmu) = g(iky,:,-sgn*nzgrid,:,ivmu)
end do
end if
end do
! stop the timer for the explicit part of the solve
if (proc0) call time_message(.false.,time_gke(:,8),' explicit')
end subroutine advance_explicit
! strong stability-preserving RK2
subroutine advance_explicit_rk2 (g)
use dist_fn_arrays, only: g0, g1
use zgrid, only: nzgrid
use stella_layouts, only: vmu_lo
use multibox, only: RK_step
implicit none
complex, dimension (:,:,-nzgrid:,:,vmu_lo%llim_proc:), intent (in out) :: g
integer :: icnt
logical :: restart_time_step
! if CFL condition is violated by nonlinear term
! then must modify time step size and restart time step
! assume false and test
restart_time_step = .false.
if(RK_step) call mb_communicate (g)
g0 = g
icnt = 1
! SSP rk3 algorithm to advance explicit part of code
! if GK equation written as dg/dt = rhs - vpar . grad h,
! solve_gke returns rhs*dt
do while (icnt <= 2)
select case (icnt)