Calculates FIDA weights
subroutine fida_weights_mc
!+ Calculates FIDA weights
integer :: i,j,k !! indices x,y,z of cells
integer(Int64) :: iion,ip
real(Float64), dimension(3) :: ri,rg !! start position
real(Float64), dimension(3) :: vi !! velocity of fast ions
integer,dimension(3) :: ind !! new actual cell
integer,dimension(4) :: neut_types=[1,2,3,4]
logical :: los_intersect
!! Determination of the CX rates
type(LocalProfiles) :: plasma
type(LocalEMFields) :: fields
real(Float64), dimension(nlevs) :: rates !! CX rates
!! Collisiional radiative model along track
integer :: ncell
integer :: jj !! counter along track
type(ParticleTrack),dimension(beam_grid%ntrack) :: tracks
real(Float64) :: photons !! photon flux
real(Float64), dimension(nlevs) :: states !! Density of n-states
real(Float64), dimension(nlevs) :: denn
integer :: nwav
real(Float64) :: etov2, energy, pitch
real(Float64) :: dE, dP, dEdP
real(Float64), dimension(:), allocatable :: ebarr, ptcharr
integer, dimension(1) :: ienergy, ipitch
real(Float64), dimension(3) :: randomu3
!! Number of particles to launch
integer(kind=8) :: pcnt
real(Float64) :: papprox_tot,inv_maxcnt,cnt,fbm_denf,phase_area
integer,dimension(3,beam_grid%ngrid) :: pcell
real(Float64), dimension(beam_grid%nx,beam_grid%ny,beam_grid%nz) :: papprox,nlaunch !! approx. density
nwav = inputs%nlambda_wght
!! define arrays
!! define energy - array
allocate(ebarr(inputs%ne_wght))
do i=1,inputs%ne_wght
ebarr(i)=real(i-0.5)*inputs%emax_wght/real(inputs%ne_wght)
enddo
dE = abs(ebarr(2)-ebarr(1))
!! define pitch - array
allocate(ptcharr(inputs%np_wght))
do i=1,inputs%np_wght
ptcharr(i)=real(i-0.5)*2./real(inputs%np_wght)-1.
enddo
dP = abs(ptcharr(2)-ptcharr(1))
dEdP = dE*dP
phase_area = dEdP*real(inputs%np_wght)*real(inputs%ne_wght)
!! allocate storage arrays
allocate(fweight%weight(nwav,inputs%ne_wght,inputs%np_wght,spec_chords%nchan))
allocate(fweight%mean_f(inputs%ne_wght,inputs%np_wght,spec_chords%nchan))
if(inputs%verbose.ge.1) then
write(*,'(T2,"Number of Channels: ",i5)') spec_chords%nchan
write(*,'(T2,"Nlambda: ",i4)') nwav
write(*,'(T2,"Nenergy: ",i3)') inputs%ne_wght
write(*,'(T2,"Maximum Energy: ",f7.2)') inputs%emax_wght
write(*,'(T2,"LOS averaged: ",a)') "False"
endif
!! zero out arrays
fweight%weight = 0.d0
fweight%mean_f = 0.d0
etov2 = 1.d0/(v2_to_E_per_amu*inputs%ab)
!! Estimate how many particles to launch in each cell
papprox=0.d0
papprox_tot=0.d0
pcnt=1
do k=1,beam_grid%nz
do j=1,beam_grid%ny
do i=1,beam_grid%nx
ind =[i,j,k]
call get_plasma(plasma,ind=ind)
papprox(i,j,k)=(sum(neut%dens(:,nbif_type,i,j,k)) + &
sum(neut%dens(:,nbih_type,i,j,k)) + &
sum(neut%dens(:,nbit_type,i,j,k)) + &
sum(neut%dens(:,halo_type,i,j,k)))
if(papprox(i,j,k).gt.0) then
pcell(:,pcnt)= ind
pcnt=pcnt+1
endif
if(plasma%in_plasma) papprox_tot=papprox_tot+papprox(i,j,k)
enddo
enddo
enddo
pcnt=pcnt-1
inv_maxcnt=100.0/real(pcnt)
call get_nlaunch(10*inputs%n_fida,papprox,papprox_tot,nlaunch)
if(inputs%verbose.ge.1) then
write(*,'(T6,"# of markers: ",i9)') int(sum(nlaunch),Int64)
endif
!! Loop over all cells that have neutrals
cnt=0.d0
loop_over_cells: do ip = 1, int(pcnt)
i = pcell(1,ip)
j = pcell(2,ip)
k = pcell(3,ip)
ind = [i, j, k]
!$OMP PARALLEL DO schedule(guided) private(iion,vi,ri,rg,ienergy,ipitch, &
!$OMP tracks,ncell,jj,plasma,fields,rates,denn,states,photons,energy,pitch, &
!$OMP los_intersect,randomu3,fbm_denf)
loop_over_fast_ions: do iion=1,int(nlaunch(i, j, k),Int64)
!! Sample fast ion distribution uniformally
call randind(inputs%ne_wght, ienergy)
call randind(inputs%np_wght, ipitch)
call randu(randomu3)
energy = ebarr(ienergy(1)) + dE*(randomu3(1)-0.5)
pitch = ptcharr(ipitch(1)) + dP*(randomu3(2)-0.5)
if(energy.le.0) cycle loop_over_fast_ions
call randu(randomu3)
rg = [beam_grid%xc(i),beam_grid%yc(j),beam_grid%zc(k)] + beam_grid%dr*(randomu3-0.5)
!! Get velocity
call get_fields(fields,pos=rg)
if(.not.fields%in_plasma) cycle loop_over_fast_ions
call gyro_correction(fields,energy,pitch,ri,vi)
fbm_denf = 0.0
if (inputs%dist_type.eq.1) then
call get_ep_denf(energy,pitch,fbm_denf,coeffs=fields%c)
endif
!! Find the particles path through the beam grid
call track(ri, vi, tracks, ncell, los_intersect)
if(.not.los_intersect) cycle loop_over_fast_ions
if(ncell.eq.0) cycle loop_over_fast_ions
!! Calculate CX probability with beam and halo neutrals
call get_beam_cx_rate(tracks(1)%ind, ri, vi, beam_ion, neut_types, rates)
if(sum(rates).le.0.) cycle loop_over_fast_ions
states=rates*1.d20
!! Calculate the spectra produced in each cell along the path
loop_along_track: do jj=1,ncell
call get_plasma(plasma,pos=tracks(jj)%pos)
call colrad(plasma,beam_ion, vi, tracks(jj)%time, states, denn, photons)
call store_fw_photons(ienergy(1), ipitch(1), &
tracks(jj)%pos, vi, fbm_denf, photons/nlaunch(i,j,k))
enddo loop_along_track
enddo loop_over_fast_ions
!$OMP END PARALLEL DO
cnt=cnt+1
if(inputs%verbose.ge.2) then
WRITE(*,'(f7.2,"% completed",a,$)') cnt*inv_maxcnt,char(13)
endif
enddo loop_over_cells
fweight%weight = ((1.d-20)*phase_area/dEdP)*fweight%weight
fweight%mean_f = ((1.d-20)*phase_area/dEdP)*fweight%mean_f
call write_fida_weights()
end subroutine fida_weights_mc