Calculate Active NPA flux using a Monte Carlo fast-ion distribution
subroutine npa_mc
!+ Calculate Active NPA flux using a Monte Carlo fast-ion distribution
integer :: iion,igamma,ngamma,npart
type(FastIon) :: fast_ion
real(Float64) :: phi,theta,dtheta
real(Float64), dimension(3) :: ri, rf, rg, vi
integer :: det,ichan,ir,nrange,it
type(LocalEMFields) :: fields
type(GyroSurface) :: gs
real(Float64), dimension(nlevs) :: rates
real(Float64), dimension(nlevs) :: states
real(Float64) :: flux
integer, dimension(5) :: neut_types=[1,2,3,4,5]
integer, dimension(3) :: ind
real(Float64), dimension(3) :: uvw, uvw_vi
real(Float64), dimension(2,4) :: gyrange
real(Float64) :: s,c
real(Float64), dimension(1) :: randomu
ngamma = 1
if(particles%axisym.or.(inputs%dist_type.eq.2)) then
ngamma = ceiling(dble(inputs%n_npa)/particles%nparticle)
endif
if(inputs%verbose.ge.1) then
write(*,'(T6,"# of markers: ",i10)') int(particles%nparticle*ngamma,Int64)
endif
!$OMP PARALLEL DO schedule(guided) private(iion,igamma,ind,fast_ion,vi,ri,rf,phi,s,c,ir,it, &
!$OMP& randomu,rg,fields,uvw,uvw_vi,rates,states,flux,det,ichan,gs,nrange,gyrange,theta,dtheta)
loop_over_fast_ions: do iion=istart,particles%nparticle,istep
fast_ion = particles%fast_ion(iion)
if(fast_ion%vabs.eq.0) cycle loop_over_fast_ions
if(.not.fast_ion%beam_grid_cross_grid) cycle loop_over_fast_ions
gamma_loop: do igamma=1,ngamma
if(particles%axisym) then
!! Pick random toroidal angle
call randu(randomu)
phi = fast_ion%beam_grid_phi_enter + fast_ion%delta_phi*randomu(1)
else
phi = fast_ion%phi
endif
s = sin(phi)
c = cos(phi)
!! Calculate position in machine coordinates
uvw(1) = fast_ion%r*c
uvw(2) = fast_ion%r*s
uvw(3) = fast_ion%z
if(inputs%dist_type.eq.2) then
!! Convert to beam grid coordinates
call uvw_to_xyz(uvw, rg)
!! Get electomagnetic fields
call get_fields(fields, pos=rg)
!! Correct for gyro motion and get position and velocity
call gyro_surface(fields, fast_ion%energy, fast_ion%pitch, 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, det=ichan)
if(det.ne.ichan) then
if (inputs%verbose.ge.0)then
write(*,*) "NPA_MC: Missed Detector ",ichan
endif
cycle gyro_range_loop
endif
!! Get beam grid indices at ri
call get_indices(ri,ind)
!! Calculate CX probability with beam and halo neutrals
call get_beam_cx_rate(ind,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*fast_ion%weight*(fast_ion%delta_phi/(2*pi))/beam_grid%dv/ngamma
!! 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
spread_loop: do it=1,25
theta = gyrange(1,ir) + (it-0.5)*dtheta/25
call gyro_trajectory(gs, theta, ri, vi)
call hit_npa_detector(ri, vi ,det, rf, det=ichan)
if(det.ne.ichan) then
if (inputs%verbose.ge.0)then
write(*,*) "NPA_MC: Missed Detector ",ichan
endif
cycle spread_loop
endif
call store_npa(det,ri,rf,vi,flux/25,fast_ion%class)
enddo spread_loop
enddo gyro_range_loop
enddo detector_loop
else !! Full Orbit
!! Convert to beam grid coordinates
call uvw_to_xyz(uvw, ri)
!! Calculate velocity vector
uvw_vi(1) = c*fast_ion%vr - s*fast_ion%vt
uvw_vi(2) = s*fast_ion%vr + c*fast_ion%vt
uvw_vi(3) = fast_ion%vz
vi = matmul(beam_grid%inv_basis,uvw_vi)
!! Check if particle hits a NPA detector
call hit_npa_detector(ri, vi ,det, rf)
if(det.eq.0) cycle gamma_loop
!! Get beam grid indices at ri
call get_indices(ri,ind)
!! Calculate CX probability with beam and halo neutrals
call get_beam_cx_rate(ind,ri,vi,beam_ion,neut_types,rates)
if(sum(rates).le.0.) cycle gamma_loop
!! Weight CX rates by ion source density
states=rates*fast_ion%weight*(fast_ion%delta_phi/(2*pi))/beam_grid%dv/ngamma
!! Attenuate states as the particle moves though plasma
call attenuate(ri,rf,vi,states)
!! Store NPA Flux
flux = sum(states)*beam_grid%dv
call store_npa(det,ri,rf,vi,flux,fast_ion%class)
endif
enddo gamma_loop
enddo loop_over_fast_ions
!$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_mc