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
/
vpamu_grids.f90
731 lines (537 loc) · 19.5 KB
/
vpamu_grids.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
module vpamu_grids
implicit none
public :: init_vpamu_grids, finish_vpamu_grids
public :: read_vpamu_grids_parameters
public :: integrate_vmu, integrate_species
public :: integrate_mu
public :: vpa, nvgrid, nvpa
public :: wgts_vpa, dvpa
public :: mu, nmu, wgts_mu, dmu
public :: maxwell_vpa, maxwell_mu, ztmax
public :: maxwell_fac
public :: vperp2
public :: equally_spaced_mu_grid
public :: set_vpa_weights
logical :: vpamu_initialized = .false.
integer :: nvgrid, nvpa
integer :: nmu
real :: vpa_max, vperp_max
! arrays that are filled in vpamu_grids
real, dimension (:), allocatable :: vpa, wgts_vpa, wgts_vpa_default
real, dimension (:,:), allocatable :: maxwell_vpa
real, dimension (:), allocatable :: mu, maxwell_fac
real, dimension (:,:,:), allocatable :: wgts_mu
real, dimension (:,:,:,:), allocatable :: maxwell_mu
real, dimension (:,:), allocatable :: ztmax
real :: dvpa
real, dimension (:), allocatable :: dmu
complex, dimension (:), allocatable :: rbuffer
logical :: equally_spaced_mu_grid
! vpa-mu related arrays that are declared here
! but allocated and filled elsewhere because they depend on z, etc.
real, dimension (:,:,:), allocatable :: vperp2
interface integrate_species
! module procedure integrate_species_vmu
module procedure integrate_species_vmu_single
module procedure integrate_species_vmu_single_real
module procedure integrate_species_vmu_block_complex
module procedure integrate_species_vmu_block_real
! module procedure integrate_species_local_complex
! module procedure integrate_species_local_real
end interface
interface integrate_vmu
module procedure integrate_vmu_local_real
module procedure integrate_vmu_local_complex
module procedure integrate_vmu_vmulo_complex
module procedure integrate_vmu_vmulo_ivmu_only_real
end interface
interface integrate_mu
module procedure integrate_mu_local
module procedure integrate_mu_nonlocal
end interface
contains
subroutine read_vpamu_grids_parameters
use file_utils, only: input_unit_exist
use mp, only: proc0, broadcast
implicit none
namelist /vpamu_grids_parameters/ nvgrid, nmu, vpa_max, vperp_max, &
equally_spaced_mu_grid
integer :: in_file
logical :: exist
if (proc0) then
nvgrid = 24
vpa_max = 3.0
nmu = 12
vperp_max = 3.0
equally_spaced_mu_grid = .false.
in_file = input_unit_exist("vpamu_grids_parameters", exist)
if (exist) read (unit=in_file, nml=vpamu_grids_parameters)
end if
call broadcast (nvgrid)
call broadcast (vpa_max)
call broadcast (nmu)
call broadcast (vperp_max)
call broadcast (equally_spaced_mu_grid)
nvpa = 2*nvgrid
end subroutine read_vpamu_grids_parameters
subroutine init_vpamu_grids
use species, only: spec, nspec
implicit none
if (vpamu_initialized) return
vpamu_initialized = .true.
call init_vpa_grid
call init_mu_grid
if(.not.allocated(maxwell_fac)) then
allocate(maxwell_fac(nspec)) ; maxwell_fac = 1.0
endif
maxwell_fac = spec%dens/spec%dens_psi0*(spec%temp_psi0/spec%temp)**1.5
end subroutine init_vpamu_grids
subroutine init_vpa_grid
use mp, only: mp_abort
use species, only: spec, nspec
implicit none
integer :: iv, idx, iseg, nvpa_seg
real :: del
if (.not. allocated(vpa)) then
! vpa is the parallel velocity at grid points
allocate (vpa(nvpa)) ; vpa = 0.0
! wgts_vpa are the integration weights assigned
! to the parallel velocity grid points
allocate (wgts_vpa(nvpa)) ; wgts_vpa = 0.0
allocate (wgts_vpa_default(nvpa)) ; wgts_vpa_default = 0.0
! this is the Maxwellian in vpa
allocate (maxwell_vpa(nvpa,nspec)) ; maxwell_vpa = 0.0
allocate (ztmax(nvpa,nspec)) ; ztmax = 0.0
end if
! velocity grid goes from -vpa_max to vpa_max
! with a point at vpa = 0
! equal grid spacing in vpa
dvpa = 2.*vpa_max/(nvpa-1)
! obtain vpa grid for vpa > 0
do iv = nvgrid+1, nvpa
vpa(iv) = real(iv-nvgrid-0.5)*dvpa
end do
! fill in vpa grid for vpa < 0
vpa(:nvgrid) = -vpa(nvpa:nvgrid+1:-1)
! this is the equilibrium Maxwellian in vpa
maxwell_vpa = exp(-spread(vpa*vpa,2,nspec)*spread(spec%temp_psi0/spec%temp,1,nvpa))
ztmax = spread(spec%zt,1,nvpa)*maxwell_vpa
! get integration weights corresponding to vpa grid points
! for now use Simpson's rule;
! i.e. subdivide grid into 3-point segments, with each segment spanning vpa_low to vpa_up
! then the contribution of each segment to the integral is
! (vpa_up - vpa_low) * (f1 + 4*f2 + f3) / 6
! inner boundary points are used in two segments, so they get double the weight
if (nvpa < 6) &
call mp_abort ('stella does not currently support nvgrid < 3. aborting.')
! use simpson 3/8 rule at lower boundary and composite Simpson elsewhere
del=0.375*dvpa
wgts_vpa(1) = del
wgts_vpa(2:3) = 3.*del
wgts_vpa(4) = del
! composite simpson
nvpa_seg = (nvpa-4)/2
del = dvpa/3.
do iseg = 1, nvpa_seg
idx = 2*(iseg-1)+4
wgts_vpa(idx) = wgts_vpa(idx) + del
wgts_vpa(idx+1) = wgts_vpa(idx+1) +4.*del
wgts_vpa(idx+2) = wgts_vpa(idx+2) + del
end do
! for the sake of symmetry, do the same thing with 3/8 rule at upper boundary
! and composite elsewhere.
del = 0.375*dvpa
wgts_vpa(nvpa-3) = wgts_vpa(nvpa-3) + del
wgts_vpa(nvpa-2:nvpa-1) = wgts_vpa(nvpa-2:nvpa-1) + 3.*del
wgts_vpa(nvpa) = wgts_vpa(nvpa) + del
nvpa_seg = (nvpa-4)/2
del = dvpa/3.
do iseg = 1, nvpa_seg
idx = 2*(iseg-1)+1
wgts_vpa(idx) = wgts_vpa(idx) + del
wgts_vpa(idx+1) = wgts_vpa(idx+1) +4.*del
wgts_vpa(idx+2) = wgts_vpa(idx+2) + del
end do
! divide by 2 to account for double-counting
wgts_vpa = 0.5*wgts_vpa
wgts_vpa_default = wgts_vpa
end subroutine init_vpa_grid
subroutine set_vpa_weights (conservative)
implicit none
logical, intent (in) :: conservative
if (conservative) then
wgts_vpa = dvpa
else
wgts_vpa = wgts_vpa_default
end if
end subroutine set_vpa_weights
subroutine integrate_mu_local (iz, g, total)
use species, only: nspec
implicit none
integer, intent (in) :: iz
real, dimension (:,:), intent (in) :: g
real, dimension (:), intent (out) :: total
integer :: is, imu, ia
total = 0.
ia = 1
do is = 1, nspec
! sum over mu
do imu = 1, nmu
total(is) = total(is) + wgts_mu(ia,iz,imu)*g(imu,is)
end do
end do
end subroutine integrate_mu_local
subroutine integrate_mu_nonlocal (iz, g, total)
use mp, only: nproc, sum_reduce
use stella_layouts, only: vmu_lo
use stella_layouts, only: is_idx, imu_idx, iv_idx
implicit none
integer, intent (in) :: iz
real, dimension (vmu_lo%llim_proc:), intent (in) :: g
real, dimension (:,:), intent (out) :: total
integer :: is, imu, iv, ivmu, ia
total = 0.
ia = 1
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)
total(iv,is) = total(iv,is) + wgts_mu(ia,iz,imu)*g(ivmu)
end do
if (nproc > 1) call sum_reduce (total,0)
end subroutine integrate_mu_nonlocal
subroutine integrate_vmu_local_real (g, iz, total)
implicit none
real, dimension (:,:), intent (in) :: g
integer, intent (in) :: iz
real, intent (out) :: total
integer :: iv, imu, ia
total = 0.
ia = 1
do imu = 1, nmu
do iv = 1, nvpa
total = total + wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(iv,imu)
end do
end do
end subroutine integrate_vmu_local_real
subroutine integrate_vmu_local_complex (g, iz, total)
implicit none
complex, dimension (:,:), intent (in) :: g
integer, intent (in) :: iz
complex, intent (out) :: total
integer :: iv, imu, ia
total = 0.
ia = 1
do imu = 1, nmu
do iv = 1, nvpa
total = total + wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(iv,imu)
end do
end do
end subroutine integrate_vmu_local_complex
! integrave over v-space in vmu_lo
subroutine integrate_vmu_vmulo_complex (g, weights, total)
use mp, only: sum_allreduce
use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
use zgrid, only: nzgrid
implicit none
integer :: ivmu, iv, iz, is, imu, ia
complex, dimension (:,:,-nzgrid:,:,vmu_lo%llim_proc:), intent (in) :: g
real, dimension (:), intent (in) :: weights
complex, dimension (:,:,-nzgrid:,:,:), intent (out) :: total
total = 0.
ia = 1
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)
do iz = -nzgrid, nzgrid
total(:,:,iz,:,is) = total(:,:,iz,:,is) + &
wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(:,:,iz,:,ivmu)*weights(is)
end do
end do
call sum_allreduce (total)
end subroutine integrate_vmu_vmulo_complex
! integrave over v-space in vmu_lo
subroutine integrate_vmu_vmulo_ivmu_only_real (g, ia, iz, total)
use mp, only: sum_allreduce
use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
implicit none
integer :: ivmu, iv, is, imu
real, dimension (vmu_lo%llim_proc:), intent (in) :: g
integer, intent (in) :: ia, iz
real, dimension (:), intent (out) :: total
total = 0.
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)
total(is) = total(is) + &
wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(ivmu)
end do
call sum_allreduce (total)
end subroutine integrate_vmu_vmulo_ivmu_only_real
! subroutine integrate_species_local_real (g, weights, iz, total)
! use species, only: nspec
! use stella_geometry, only: bmag
! implicit none
! real, dimension (:,:,:), intent (in) :: g
! real, dimension (:), intent (in) :: weights
! integer, intent (in) :: iz
! real, intent (out) :: total
! integer :: iv, imu, is
! total = 0.
! do is = 1, nspec
! do imu = 1, nmu
! do iv = 1, nvpa
! total = total + wgts_mu(imu)*wgts_vpa(iv)*bmag(1,iz)*g(iv,imu,is)*weights(is)
! end do
! end do
! end do
! end subroutine integrate_species_local_real
! subroutine integrate_species_local_complex (g, weights, iz, total)
! use species, only: nspec
! use stella_geometry, only: bmag
! implicit none
! complex, dimension (:,:,:), intent (in) :: g
! real, dimension (:), intent (in) :: weights
! integer, intent (in) :: iz
! complex, intent (out) :: total
! integer :: iv, imu, is
! total = 0.
! do is = 1, nspec
! do imu = 1, nmu
! do iv = 1, nvpa
! total = total + wgts_mu(imu)*wgts_vpa(iv)*bmag(1,iz)*g(iv,imu,is)*weights(is)
! end do
! end do
! end do
! end subroutine integrate_species_local_complex
! ! integrave over v-space and sum over species
! subroutine integrate_species_vmu (g, weights, total)
! use mp, only: sum_allreduce
! use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
! use zgrid, only: nzgrid
! use stella_geometry, only: bmag
! implicit none
! integer :: ivmu, iv, iz, is, imu
! complex, dimension (:,:,-nzgrid:,vmu_lo%llim_proc:), intent (in) :: g
! real, dimension (:), intent (in) :: weights
! complex, dimension (:,:,-nzgrid:), intent (out) :: total
! total = 0.
! 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)
! do iz = -nzgrid, nzgrid
! total(:,:,iz) = total(:,:,iz) + &
! wgts_mu(imu)*wgts_vpa(iv)*bmag(1,iz)*g(:,:,iz,ivmu)*weights(is)
! end do
! end do
! call sum_allreduce (total)
! end subroutine integrate_species_vmu
! integrave over v-space and sum over species for given (ky,kx,z) point
subroutine integrate_species_vmu_single (g, iz, weights, total, ia_in, reduce_in)
use mp, only: sum_allreduce
use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
implicit none
integer :: ivmu, iv, is, imu, ia
logical :: reduce
complex, dimension (vmu_lo%llim_proc:), intent (in) :: g
integer, intent (in) :: iz
real, dimension (:), intent (in) :: weights
complex, intent (out) :: total
integer, intent (in), optional :: ia_in
logical, intent (in), optional :: reduce_in
total = 0.
if (present(ia_in)) then
ia = ia_in
else
ia = 1
end if
if (present(reduce_in)) then
reduce = reduce_in
else
reduce = .true.
end if
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)
total = total + &
wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(ivmu)*weights(is)
end do
if (reduce) call sum_allreduce (total)
end subroutine integrate_species_vmu_single
! integrave over v-space and sum over species for given (ky,kx,z) point
subroutine integrate_species_vmu_single_real (g, iz, weights, total, ia_in,reduce_in)
use mp, only: sum_allreduce
use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
implicit none
integer :: ivmu, iv, is, imu, ia
logical :: reduce
real, dimension (vmu_lo%llim_proc:), intent (in) :: g
integer, intent (in) :: iz
real, dimension (:), intent (in) :: weights
real, intent (out) :: total
integer, intent (in), optional :: ia_in
logical, intent (in), optional :: reduce_in
total = 0.
if (present(ia_in)) then
ia = ia_in
else
ia = 1
end if
if (present(reduce_in)) then
reduce = reduce_in
else
reduce = .true.
end if
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)
total = total + &
wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(ivmu)*weights(is)
end do
if (reduce) call sum_allreduce (total)
end subroutine integrate_species_vmu_single_real
subroutine integrate_species_vmu_block_complex (g, iz, weights, pout, ia_in, reduce_in)
use mp, only: sum_allreduce
use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
implicit none
integer :: ivmu, iv, is, imu, ia
logical :: reduce
complex, dimension (:,:,vmu_lo%llim_proc:), intent (in) :: g
integer, intent (in) :: iz
integer, intent (in), optional :: ia_in
logical, intent (in), optional :: reduce_in
real, dimension (:), intent (in) :: weights
complex, dimension (:,:), intent (out) :: pout
pout =0.
if (present(ia_in)) then
ia = ia_in
else
ia = 1
end if
if (present(reduce_in)) then
reduce = reduce_in
else
reduce = .true.
end if
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)
pout = pout + wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(:,:,ivmu)*weights(is)
end do
if (reduce) call sum_allreduce (pout)
end subroutine integrate_species_vmu_block_complex
subroutine integrate_species_vmu_block_real (g, iz, weights, pout, ia_in,reduce_in)
use mp, only: sum_allreduce
use stella_layouts, only: vmu_lo, iv_idx, imu_idx, is_idx
implicit none
integer :: ivmu, iv, is, imu, ia
logical :: reduce
real, dimension (:,:,vmu_lo%llim_proc:), intent (in) :: g
integer, intent (in) :: iz
integer, intent (in), optional :: ia_in
logical, intent (in), optional :: reduce_in
real, dimension (:), intent (in) :: weights
real, dimension (:,:), intent (out) :: pout
pout =0.
if (present(ia_in)) then
ia = ia_in
else
ia = 1
end if
if (present(reduce_in)) then
reduce = reduce_in
else
reduce = .true.
end if
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)
pout = pout + wgts_mu(ia,iz,imu)*wgts_vpa(iv)*g(:,:,ivmu)*weights(is)
end do
if (reduce) call sum_allreduce (pout)
end subroutine integrate_species_vmu_block_real
subroutine finish_vpa_grid
implicit none
if (allocated(vpa)) deallocate (vpa)
if (allocated(wgts_vpa)) deallocate (wgts_vpa)
if (allocated(wgts_vpa_default)) deallocate (wgts_vpa_default)
if (allocated(maxwell_vpa)) deallocate (maxwell_vpa)
if (allocated(ztmax)) deallocate (ztmax)
end subroutine finish_vpa_grid
subroutine init_mu_grid
use constants, only: pi
use gauss_quad, only: get_laguerre_grids
use zgrid, only: nzgrid, nztot
use kt_grids, only: nalpha
use species, only: spec, nspec
use stella_geometry, only: bmag, bmag_psi0
implicit none
integer :: imu
real :: mu_max
real, dimension (:), allocatable :: wgts_mu_tmp
! allocate arrays and initialize to zero
if (.not. allocated(mu)) then
allocate (mu(nmu)) ; mu = 0.0
allocate (wgts_mu(nalpha,-nzgrid:nzgrid,nmu)) ; wgts_mu = 0.0
allocate (maxwell_mu(nalpha,-nzgrid:nzgrid,nmu,nspec)) ; maxwell_mu = 0.0
allocate (dmu(nmu-1))
end if
allocate (wgts_mu_tmp(nmu)) ; wgts_mu_tmp = 0.0
! dvpe * vpe = d(2*mu*B0) * B/2B0
if (equally_spaced_mu_grid) then
! first get equally spaced grid in mu with max value
! mu_max = vperp_max**2/(2*max(bmag))
mu_max = vperp_max**2/(2.*maxval(bmag_psi0))
! want first grid point at dmu/2 to avoid mu=0 special point
! dmu/2 + (nmu-1)*dmu = mu_max
! so dmu = mu_max/(nmu-1/2)
dmu = mu_max/(nmu-0.5)
mu(1) = 0.5*dmu(1)
do imu = 2, nmu
mu(imu) = mu(1)+(imu-1)*dmu(1)
end do
! do simplest thing to start
wgts_mu_tmp = dmu(1)
else
! ! use Gauss-Laguerre quadrature in 2*mu*bmag(z=0)
! use Gauss-Laguerre quadrature in 2*mu*min(bmag)*max(
call get_laguerre_grids (mu, wgts_mu_tmp)
wgts_mu_tmp = wgts_mu_tmp*exp(mu)/(2.*minval(bmag_psi0)*mu(nmu)/vperp_max**2)
! mu = mu/(2.*bmag(1,0))
mu = mu/(2.*minval(bmag_psi0)*mu(nmu)/vperp_max**2)
dmu(:nmu-1) = mu(2:)-mu(:nmu-1)
! leave dmu(nmu) uninitialized. should never be used, so want
! valgrind or similar to return error if it is
end if
! this is the mu part of the v-space Maxwellian
maxwell_mu = exp(-2.*spread(spread(spread(mu,1,nalpha),2,nztot)*spread(bmag,3,nmu),4,nspec) &
*spread(spread(spread(spec%temp_psi0/spec%temp,1,nalpha),2,nztot),3,nmu))
! factor of 2./sqrt(pi) necessary to account for 2pi from
! integration over gyro-angle and 1/pi^(3/2) normalization
! of velocity space Jacobian
wgts_mu = 2./sqrt(pi)*spread(spread(wgts_mu_tmp,1,nalpha),2,nztot)*spread(bmag,3,nmu)
deallocate (wgts_mu_tmp)
end subroutine init_mu_grid
subroutine finish_mu_grid
implicit none
if (allocated(mu)) deallocate (mu)
if (allocated(wgts_mu)) deallocate (wgts_mu)
if (allocated(maxwell_mu)) deallocate (maxwell_mu)
if (allocated(dmu)) deallocate (dmu)
if (allocated(rbuffer)) deallocate (rbuffer)
end subroutine finish_mu_grid
subroutine finish_vpamu_grids
implicit none
call finish_vpa_grid
call finish_mu_grid
if(allocated(maxwell_fac)) deallocate(maxwell_fac)
vpamu_initialized = .false.
end subroutine finish_vpamu_grids
end module vpamu_grids