forked from mabarnes/stella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist_fn.f90
369 lines (266 loc) · 10.1 KB
/
dist_fn.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
module dist_fn
implicit none
public :: init_gxyz
public :: init_dist_fn, finish_dist_fn
public :: adiabatic_option_switch
public :: adiabatic_option_fieldlineavg
private
logical :: dist_fn_initialized = .false.
logical :: gxyz_initialized = .false.
logical :: kp2init = .false.
logical :: vp2init = .false.
! logical :: bessinit = .false.
logical :: readinit = .false.
integer :: adiabatic_option_switch
integer, parameter :: adiabatic_option_default = 1, &
adiabatic_option_zero = 2, &
adiabatic_option_fieldlineavg = 3, &
adiabatic_option_yavg = 4
logical :: debug = .false.
contains
subroutine init_gxyz (restarted)
use dist_fn_arrays, only: gvmu, gold, gnew
use redistribute, only: gather, scatter
use dist_redistribute, only: kxkyz2vmu
use physics_flags, only: radial_variation
use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
use stella_transforms, only: transform_kx2x_xfirst, transform_x2kx_xfirst
use kt_grids, only: nalpha, nakx, naky, nx, multiply_by_rho
use vpamu_grids, only: mu, vpa, vperp2
use zgrid, only: nzgrid, ntubes
use species, only: spec, pfac
use stella_geometry, only: dBdrho, gfac
implicit none
real :: corr
integer :: ivmu, is, imu, iv, it, iz, ia
real, dimension (:,:), allocatable :: energy
complex, dimension (:,:), allocatable :: g0k
logical, intent(in) :: restarted
if (gxyz_initialized) return
gxyz_initialized = .false.
! get version of g that has ky,kx,z local
call gather (kxkyz2vmu, gvmu, gnew)
ia = 1
!calculate radial corrections to F0 for use in Krook operator, as well as g1 from initialization
if(radial_variation) then
!init_g uses maxwellians, so account for variation in temperature, density, and B
allocate (energy(nalpha,-nzgrid:nzgrid))
allocate (g0k(naky,nakx))
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)
do it = 1, ntubes
do iz = -nzgrid, nzgrid
corr = -( pfac*(spec(is)%fprim+spec(is)%tprim*(energy(ia,iz)-1.5)) &
+ 2*gfac*mu(imu)*dBdrho(iz))
if(.not.restarted) then
g0k = corr*gnew(:,:,iz,it,ivmu)
call multiply_by_rho(g0k)
gnew(:,:,iz,it,ivmu) = gnew(:,:,iz,it,ivmu) + g0k
endif
enddo
enddo
enddo
deallocate(energy, g0k)
if(.not.restarted) call scatter(kxkyz2vmu,gnew,gvmu)
endif
gold = gnew
end subroutine init_gxyz
subroutine init_dist_fn
use mp, only: proc0
use stella_layouts, only: init_dist_fn_layouts
use species, only: nspec
use zgrid, only: ntubes
use gyro_averages, only: init_bessel
use physics_flags, only: full_flux_surface
use physics_flags, only: nonlinear
use physics_flags, only: include_parallel_nonlinearity
implicit none
if (dist_fn_initialized) return
dist_fn_initialized = .true.
debug = debug .and. proc0
if (debug) write (*,*) 'dist_fn::init_dist_fn::read_parameters'
call read_parameters
if (debug) write (*,*) 'dist_fn::init_dist_fn::allocate_arrays'
call allocate_arrays
if (debug) write (*,*) 'dist_fn::init_dist_fn::init_kperp2'
call init_kperp2
if (debug) write (*,*) 'dist_fn::init_dist_fn::init_vperp2'
call init_vperp2
if (debug) write (*,*) 'dist_fn::init_dist_fn::init_bessel'
call init_bessel
end subroutine init_dist_fn
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
implicit none
logical :: dfexist
type (text_option), dimension (7), parameter :: adiabaticopts = &
(/ text_option('default', adiabatic_option_default), &
text_option('no-field-line-average-term', adiabatic_option_default), &
text_option('field-line-average-term', adiabatic_option_fieldlineavg), &
text_option('iphi00=0', adiabatic_option_default), &
text_option('iphi00=1', adiabatic_option_default), &
text_option('iphi00=2', adiabatic_option_fieldlineavg), &
text_option('iphi00=3', adiabatic_option_yavg)/)
character(30) :: adiabatic_option
namelist /dist_fn_knobs/ adiabatic_option
integer :: ierr, in_file
if (readinit) return
readinit = .true.
if (proc0) then
adiabatic_option = 'default'
in_file = input_unit_exist("dist_fn_knobs", dfexist)
if (dfexist) read (unit=in_file, nml=dist_fn_knobs)
ierr = error_unit()
call get_option_value &
(adiabatic_option, adiabaticopts, adiabatic_option_switch, &
ierr, "adiabatic_option in dist_fn_knobs")
end if
call broadcast (adiabatic_option_switch)
end subroutine read_parameters
subroutine init_kperp2
use dist_fn_arrays, only: kperp2, dkperp2dr
use stella_geometry, only: gds2, gds21, gds22
use stella_geometry, only: dgds2dr, dgds21dr
use stella_geometry, only: dgds22dr
use stella_geometry, only: geo_surf, q_as_x
use zgrid, only: nzgrid
use kt_grids, only: naky, nakx, theta0
use kt_grids, only: akx, aky
use kt_grids, only: zonal_mode
use kt_grids, only: nalpha
implicit none
integer :: iky, ikx
if (kp2init) return
kp2init = .true.
allocate (kperp2(naky,nakx,nalpha,-nzgrid:nzgrid))
allocate (dkperp2dr(naky,nakx,nalpha,-nzgrid:nzgrid))
do iky = 1, naky
if (zonal_mode(iky)) then
do ikx = 1, nakx
if(q_as_x) then
kperp2(iky,ikx,:,:) = akx(ikx)*akx(ikx)*gds22
dkperp2dr(iky,ikx,:,:) = akx(ikx)*akx(ikx)*dgds22dr/kperp2(iky,ikx,:,:)
else
kperp2(iky,ikx,:,:) = akx(ikx)*akx(ikx)*gds22/(geo_surf%shat**2)
dkperp2dr(iky,ikx,:,:) = akx(ikx)*akx(ikx)*dgds22dr/(geo_surf%shat**2*kperp2(iky,ikx,:,:))
endif
if(any(kperp2(iky,ikx,:,:) .lt. epsilon(0.))) dkperp2dr(iky,ikx,:,:) = 0.
end do
else
do ikx = 1, nakx
kperp2(iky,ikx,:,:) = aky(iky)*aky(iky) &
*(gds2 + 2.0*theta0(iky,ikx)*gds21 &
+ theta0(iky,ikx)*theta0(iky,ikx)*gds22)
dkperp2dr(iky,ikx,:,:) = aky(iky)*aky(iky) &
*(dgds2dr + 2.0*theta0(iky,ikx)*dgds21dr &
+ theta0(iky,ikx)*theta0(iky,ikx)*dgds22dr)
dkperp2dr(iky,ikx,:,:)=dkperp2dr(iky,ikx,:,:)/kperp2(iky,ikx,:,:)
if(any(kperp2(iky,ikx,:,:) .lt. epsilon(0.))) dkperp2dr(iky,ikx,:,:) = 0.
end do
end if
end do
call enforce_single_valued_kperp2
! filename=trim(run_name)//".kperp2"
! open (1232,file=trim(filename),status='unknown')
! ia=1
! do iz=-nzgrid,nzgrid
! do ikx=1, naky
! do iky = 1, 1
! write(1232,'(2e15.8)') kperp2(iky,ikx,ia,iz), dkperp2dr(iky,ikx,ia,iz)
! enddo
! enddo
! enddo
! close (1232)
end subroutine init_kperp2
subroutine enforce_single_valued_kperp2
use dist_fn_arrays, only: kperp2
use kt_grids, only: naky, nalpha
use zgrid, only: nzgrid
use extended_zgrid, only: neigen, nsegments, ikxmod
implicit none
integer :: iky, ie, iseg
real, dimension (:), allocatable :: tmp
allocate (tmp(nalpha)) ; tmp = 0.0
do iky = 1, naky
do ie = 1, neigen(iky)
if (nsegments(ie,iky) > 1) then
do iseg = 2, nsegments(ie,iky)
tmp = 0.5*(kperp2(iky,ikxmod(iseg-1,ie,iky),:,nzgrid) + kperp2(iky,ikxmod(iseg,ie,iky),:,-nzgrid))
kperp2(iky,ikxmod(iseg,ie,iky),:,-nzgrid) = tmp
kperp2(iky,ikxmod(iseg-1,ie,iky),:,nzgrid) = tmp
end do
end if
end do
end do
deallocate (tmp)
end subroutine enforce_single_valued_kperp2
subroutine allocate_arrays
use stella_layouts, only: kxkyz_lo, vmu_lo
use zgrid, only: nzgrid, ntubes
use kt_grids, only: naky, nakx
use vpamu_grids, only: nvpa, nmu
use dist_fn_arrays, only: gnew, gold
use dist_fn_arrays, only: gvmu
implicit none
if (.not.allocated(gnew)) &
allocate (gnew(naky,nakx,-nzgrid:nzgrid,ntubes,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
gnew = 0.
if (.not.allocated(gold)) &
allocate (gold(naky,nakx,-nzgrid:nzgrid,ntubes,vmu_lo%llim_proc:vmu_lo%ulim_alloc))
gold = 0.
if (.not.allocated(gvmu)) &
allocate (gvmu(nvpa,nmu,kxkyz_lo%llim_proc:kxkyz_lo%ulim_alloc))
gvmu = 0.
end subroutine allocate_arrays
subroutine init_vperp2
use stella_geometry, only: bmag
use zgrid, only: nzgrid
use vpamu_grids, only: vperp2
use vpamu_grids, only: nmu, mu
use kt_grids, only: nalpha
implicit none
integer :: imu
if (vp2init) return
vp2init = .true.
if (.not.allocated(vperp2)) allocate (vperp2(nalpha,-nzgrid:nzgrid,nmu)) ; vperp2 = 0.
do imu = 1, nmu
vperp2(:,:,imu) = 2.0*mu(imu)*bmag
end do
end subroutine init_vperp2
subroutine finish_dist_fn
use gyro_averages, only: finish_bessel
implicit none
call finish_bessel
call finish_kperp2
call finish_vperp2
call deallocate_arrays
dist_fn_initialized = .false.
readinit = .false.
gxyz_initialized = .false.
end subroutine finish_dist_fn
subroutine deallocate_arrays
use dist_fn_arrays, only: gnew, gold, gvmu
implicit none
if (allocated(gnew)) deallocate (gnew)
if (allocated(gold)) deallocate (gold)
if (allocated(gvmu)) deallocate (gvmu)
end subroutine deallocate_arrays
subroutine finish_kperp2
use dist_fn_arrays, only: kperp2, dkperp2dr
implicit none
if (allocated(kperp2)) deallocate (kperp2)
if (allocated(dkperp2dr)) deallocate (dkperp2dr)
kp2init = .false.
end subroutine finish_kperp2
subroutine finish_vperp2
use vpamu_grids, only: vperp2
implicit none
if (allocated(vperp2)) deallocate (vperp2)
vp2init = .false.
end subroutine finish_vperp2
end module dist_fn