• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gyrokinetics / gs2 / 2021218321

04 Sep 2025 07:44AM UTC coverage: 10.606% (+0.03%) from 10.577%
2021218321

push

gitlab-ci

David Dickinson
Merged in feature/move_more_initialisation_to_init_levels (pull request #1161)

4710 of 44407 relevant lines covered (10.61%)

125698.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/diagnostics/diagnostics_zonal_transfer.f90
1
!> FIXME : Add documentation
2
module diagnostics_zonal_transfer
3

4
!> calculate tau(kx,ky), the transfer of free energy by the non-linearity in fourier space
5
  implicit none
6

7
  private
8

9
!> allocate free energy transfer arrays
10
  public :: init_diagnostics_transfer
11
  public :: calculate_zonal_transfer, write_zonal_transfer
12

13
contains
14
  !> FIXME : Add documentation
15
  subroutine init_diagnostics_transfer(gnostics)
×
16
    use diagnostics_config, only: diagnostics_type
17
    implicit none
18
    type(diagnostics_type), intent(inout) :: gnostics
19

20
    if(.not. gnostics%write_zonal_transfer) return
×
21

22
    !> initialize zonal_transfer to zero
23
    gnostics%current_results%zonal_transfer = 0.0
×
24
  end subroutine init_diagnostics_transfer
25
  
26
  !> calculate nonlinear term appearing in GKE
27
  !! for now assume electrostatic
28
  subroutine non_linear_term (g1)
×
29
    use theta_grid, only: ntgrid, kxfac
30
    use gs2_layouts, only: g_lo, ik_idx, it_idx, is_idx
31
    use gs2_layouts, only: yxf_lo
32
    use dist_fn_arrays, only: g, g_adjust, to_g_gs2, from_g_gs2
33
    use species, only: spec
34
    use gs2_transforms, only: transform2, inverse2
35
    use kt_grids, only: aky, akx
36
    use fields_arrays, only: phi, bpar
37
    use constants, only: zi
38

39
    implicit none
40

41
    complex, dimension (-ntgrid:,:,g_lo%llim_proc:), intent(out) :: g1
42
    integer :: i, j
43
    integer :: iglo, ik, it, ig, is
44

45
    real, dimension(:,:), allocatable :: banew, gbnew, bracketnew
×
46

47
    allocate (banew(yxf_lo%ny,yxf_lo%llim_proc:yxf_lo%ulim_alloc)) ; banew = 0.
×
48
    allocate (gbnew(yxf_lo%ny,yxf_lo%llim_proc:yxf_lo%ulim_alloc)) ; gbnew = 0.
×
49
    allocate (bracketnew(yxf_lo%ny,yxf_lo%llim_proc:yxf_lo%ulim_alloc)) ; bracketnew = 0.
×
50

51
    ! Form g1=i*kx*phi
52
    call load_kx_phi
×
53

54
    ! Transform to real space
55
    ! 2D fft of g1 is returned as banew
56
    call transform2 (g1, banew)
×
57

58
    ! Form g1=i*ky*g
59
    do iglo = g_lo%llim_proc, g_lo%ulim_proc
×
60
       ik = ik_idx(g_lo,iglo)
×
61
       g1(:,:,iglo)=g(:,:,iglo)*zi*aky(ik)
×
62
    end do
63

64
    ! Transform to real space
65
    call transform2 (g1, gbnew)
×
66

67
    ! Calculate (d phi /dx).(dg/dy)
68
    do j = yxf_lo%llim_proc, yxf_lo%ulim_proc
×
69
       do i = 1, yxf_lo%ny
×
70
          bracketnew(i,j) = banew(i,j)*gbnew(i,j)*kxfac
×
71
       end do
72
    end do
73

74
    ! Form g1=i*ky*chi
75
    call load_ky_phi
×
76

77
    !Transform to real space
78
    call transform2 (g1, banew)
×
79

80
    do iglo = g_lo%llim_proc, g_lo%ulim_proc
×
81
       it = it_idx(g_lo,iglo)
×
82
       g1(:,:,iglo)=g(:,:,iglo)*zi*akx(it)
×
83
    enddo
84

85
    ! Transform to real space
86
    call transform2 (g1, gbnew)
×
87

88
    ! Calculate (d phi /dy).(dg/dx) and subtract from (d phi /dx).(dg/dy)
89
    do j = yxf_lo%llim_proc, yxf_lo%ulim_proc
×
90
       do i = 1, yxf_lo%ny
×
91
          bracketnew(i,j) = bracketnew(i,j) - banew(i,j)*gbnew(i,j)*kxfac
×
92
       end do
93
    end do
94

95
    ! Transform nonlinearity back to spectral space
96
    ! g1 contains Fourier coefficients associated with nonlinearity
97
    call inverse2 (bracketnew, g1)
×
98

99
    call g_adjust(g,phi,bpar, direction = from_g_gs2)
×
100

101
    do iglo = g_lo%llim_proc, g_lo%ulim_proc
×
102
       is = is_idx(g_lo,iglo)
×
103
       g1(:,:,iglo)=g1(:,:,iglo)*conjg(g(:,:,iglo))*spec(is)%temp
×
104
    end do
105

106
    call g_adjust(g,phi,bpar, direction = to_g_gs2)
×
107

108
    deallocate (banew, gbnew, bracketnew)
×
109

110
  contains
111

112
    !> FIXME : Add documentation
113
    subroutine load_kx_phi
×
114
      use dist_fn_arrays, only: aj0  !< bessel function from the fourier coeff of average
115
      implicit none
116
      complex :: fac
117

118
      do iglo = g_lo%llim_proc, g_lo%ulim_proc
×
119
         it = it_idx(g_lo,iglo)
×
120
         ik = ik_idx(g_lo,iglo)
×
121
         do ig = -ntgrid, ntgrid
×
122
            fac = zi*akx(it)*aj0(ig,iglo)*phi(ig,it,ik)
×
123
            g1(ig,1,iglo) = fac
×
124
            g1(ig,2,iglo) = fac
×
125
         end do
126
      end do
127

128
    end subroutine load_kx_phi
×
129

130
    !> FIXME : Add documentation    
131
    subroutine load_ky_phi
×
132
      use dist_fn_arrays, only: aj0
133
      implicit none
134
      complex :: fac
135

136
      do iglo = g_lo%llim_proc, g_lo%ulim_proc
×
137
         it = it_idx(g_lo,iglo)
×
138
         ik = ik_idx(g_lo,iglo)
×
139
         do ig = -ntgrid, ntgrid
×
140
            fac = zi*aky(ik)*aj0(ig,iglo)*phi(ig,it,ik)
×
141
            g1(ig,1,iglo) = fac
×
142
            g1(ig,2,iglo) = fac
×
143
         end do
144
      end do
145

146
    end subroutine load_ky_phi
×
147

148
  end subroutine non_linear_term
149

150
  !> Returns tau, the transfer of free energy as a function of (kx,ky)
151
  subroutine total_term(tau)
×
152
    use mp, only: proc0, broadcast
153
    use theta_grid, only: ntgrid, field_line_average
154
    use gs2_layouts, only: g_lo, ik_idx, it_idx
155
    use species, only: nspec
156
    use kt_grids, only: ntheta0, naky
157
    use le_grids, only: integrate_moment
158
    implicit none
159
    complex, dimension(:,:), intent(out) :: tau
160
    complex, dimension (:,:,:), allocatable :: g0
×
161
    complex, dimension (:,:,:), allocatable :: inter
×
162
    complex, dimension (:,:,:,:), allocatable :: inter_s
×
163
    integer :: is
164
    
165
    allocate (g0(-ntgrid:ntgrid,2,g_lo%llim_proc:g_lo%ulim_alloc))
×
166
    allocate (inter(-ntgrid:ntgrid,ntheta0,naky)) ; inter = 0.
×
167
    allocate (inter_s(-ntgrid:ntgrid,ntheta0,naky,nspec)) ; inter_s = 0.
×
168

169
    ! obtain the nonlinear term
170
    call non_linear_term(g0)
×
171

172
    ! integrate over velocities
173
    ! can only be called after w and wl are assigned in init_le_grids
174
    call integrate_moment(g0, inter_s)
×
175

176
    if (proc0) then
×
177
       ! sum over species
178
       do is=1,nspec
×
179
          inter = inter + inter_s(:,:,:,is)
×
180
       end do
181

182
       ! integrate over theta, must be done after velocity space integration
183
       tau = field_line_average(inter)
×
184
    end if
185
    
186
    call broadcast (tau)
×
187
    deallocate (g0, inter, inter_s)
×
188
  end subroutine total_term
×
189

190
  !> FIXME : Add documentation  
191
  subroutine write_zonal_transfer(gnostics)
×
192
    use gs2_io, only: starts, netcdf_write_complex
193
    use diagnostics_config, only: diagnostics_type
194
    use mp, only: proc0
195
    implicit none
196
    
197
    type(diagnostics_type), intent(in out) :: gnostics
198
    if (.not. proc0) return
×
199
    call netcdf_write_complex(gnostics%file_id, "zonal_transfer", gnostics%current_results%zonal_transfer, &
200
         dim_names=["ri" ,"kx", "ky", "t "], start=starts(4, gnostics%nout), &
201
         long_name="Time rate of change of free energy due to nonlinear transfer, as a function of kx, ky and time", &
202
         units="Tr2*rhor*c*ns*vth6 / Ba2*e*a3")
×
203
    
204
  end subroutine write_zonal_transfer
205

206
  !> FIXME : Add documentation  
207
  subroutine calculate_zonal_transfer(gnostics)
×
208
    use diagnostics_config, only: diagnostics_type
209
    use kt_grids, only: ntheta0, naky
210
    implicit none
211
    
212
    type(diagnostics_type), intent(inout) :: gnostics
213
    
214
    complex, dimension (:,:), allocatable ::  zonal_transfer
×
215

216
    allocate (zonal_transfer(ntheta0,naky)) ; zonal_transfer = 0.
×
217

218
    call total_term(zonal_transfer)
×
219
    gnostics%current_results%zonal_transfer = zonal_transfer
×
220

221
    deallocate (zonal_transfer)
×
222

223
  end subroutine calculate_zonal_transfer
×
224

225
end module diagnostics_zonal_transfer
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc