Calculate Active NPA flux using a fast-ion distribution function F(E,p,r,z)
subroutine npa_f
!+ Calculate Active NPA flux using a fast-ion distribution function F(E,p,r,z)
integer :: i,j,k,det,ic
integer(Int64) :: iion
real(Float64), dimension(3) :: rg,ri,rf,vi
integer, dimension(3) :: ind,pind
real(Float64) :: denf
type(LocalProfiles) :: plasma
type(LocalEMFields) :: fields
type(GyroSurface) :: gs
real(Float64), dimension(2,4) :: gyrange
integer, dimension(5) :: neut_types=[1,2,3,4,5]
real(Float64), dimension(nlevs) :: rates
real(Float64), dimension(nlevs) :: states
real(Float64) :: flux, theta, dtheta, eb, ptch
integer :: inpa,ichan,nrange,ir,npart,ncell
integer, dimension(beam_grid%ngrid) :: cell_ind
real(Float64), dimension(beam_grid%nx,beam_grid%ny,beam_grid%nz) :: papprox
integer(Int32), dimension(beam_grid%nx,beam_grid%ny,beam_grid%nz) :: nlaunch
papprox=0.d0
do ic=1,beam_grid%ngrid
call ind2sub(beam_grid%dims,ic,ind)
i = ind(1) ; j = ind(2) ; k = ind(3)
call get_plasma(plasma,ind=ind)
if(.not.plasma%in_plasma) cycle
papprox(i,j,k)=(sum(neut%full(:,i,j,k)) + &
sum(neut%half(:,i,j,k)) + &
sum(neut%third(:,i,j,k)) + &
sum(neut%dcx(:,i,j,k)) + &
sum(neut%halo(:,i,j,k)))* &
plasma%denf
enddo
ncell = 0
do ic=1,beam_grid%ngrid
call ind2sub(beam_grid%dims,ic,ind)
i = ind(1) ; j = ind(2) ; k = ind(3)
if(papprox(i,j,k).gt.0.0) then
ncell = ncell + 1
cell_ind(ncell) = ic
endif
enddo
call get_nlaunch(inputs%n_npa, papprox, nlaunch)
if(inputs%verbose.ge.1) then
write(*,'(T6,"# of markers: ",i12)') sum(nlaunch)
endif
!! Loop over all cells that can contribute to NPA signal
!$OMP PARALLEL DO schedule(dynamic,1) private(ic,i,j,k,ind,iion,ichan,fields,nrange,gyrange, &
!$OMP& pind,vi,ri,rf,det,plasma,rates,states,flux,denf,eb,ptch,gs,ir,theta,dtheta)
loop_over_cells: do ic = istart, ncell, istep
call ind2sub(beam_grid%dims,cell_ind(ic),ind)
i = ind(1) ; j = ind(2) ; k = ind(3)
loop_over_fast_ions: do iion=1, nlaunch(i, j, k)
!! Sample fast ion distribution for energy and pitch
call mc_fastion(ind, fields, eb, ptch, denf)
if(denf.le.0.0) cycle loop_over_fast_ions
call gyro_surface(fields, eb, ptch, gs)
detector_loop: do ichan=1,npa_chords%nchan
call npa_gyro_range(ichan, gs, gyrange, nrange)
if(nrange.eq.0) cycle detector_loop
gyro_range_loop: do ir=1,nrange
dtheta = gyrange(2,ir)
theta = gyrange(1,ir) + 0.5*dtheta
call gyro_trajectory(gs, theta, ri, vi)
!! Check if particle hits a NPA detector
call hit_npa_detector(ri, vi ,det, rf, ichan)
if(det.ne.ichan) then
if (inputs%verbose.ge.0)then
write(*,*) "NPA_F: Missed Detector ",ichan
endif
cycle gyro_range_loop
endif
!! Get beam grid indices at ri
call get_indices(ri,pind)
!! Calculate CX probability with beam and halo neutrals
call get_beam_cx_rate(pind,ri,vi,beam_ion,neut_types,rates)
if(sum(rates).le.0.) cycle gyro_range_loop
!! Weight CX rates by ion source density
states=rates*denf
!! Attenuate states as the particle move through plasma
call attenuate(ri,rf,vi,states)
!! Store NPA Flux
flux = (dtheta/(2*pi))*sum(states)*beam_grid%dv/nlaunch(i,j,k)
call store_npa(det,ri,rf,vi,flux)
enddo gyro_range_loop
enddo detector_loop
enddo loop_over_fast_ions
enddo loop_over_cells
!$OMP END PARALLEL DO
npart = npa%npart
#ifdef _MPI
call parallel_sum(npart)
call parallel_sum(npa%flux)
#endif
if(inputs%verbose.ge.1) then
write(*,'(T4,"Number of Active NPA particles that hit a detector: ",i8)') npart
endif
end subroutine npa_f