! written by nbigen.py
! *** do not edit *** (use nbigen.py instead)
 
module nbi_lost_ptcl 
  use nbi_com
  implicit NONE
  integer, dimension(:), allocatable :: mlost_all  !arrays containing the number of elements
                                                   !that are received from each process
  real(fp) :: gtime,yyion,yze   !time, Z, energy lost ptcl.
  real(fp) :: ynbsce, ynbien   !source and energy fraction index of lost ptcl.
 
 
  integer, parameter ::  num_lst_arrays = 10 !number of arrays to gather lost ptcl
 
contains
  subroutine store_lost_ptcl
    real(fp), dimension(:), allocatable :: tmp_lost
  
    if(mlost_data.ge.minb) return   !do not gather lost orbit, too many data
  
    if(mlost_data.gt.0) then
       if(allocated(tmp_lost)) deallocate(tmp_lost)
       allocate(tmp_lost(mlost_data))
 
       tmp_lost = gtime_lost
       if(allocated(gtime_lost)) deallocate(gtime_lost) 
       allocate(gtime_lost(mlost_data+1))
       gtime_lost(1:mlost_data)=tmp_lost
       gtime_lost(mlost_data+1)=gtime
 
 
       tmp_lost = ynbsce_lost
       if(allocated(ynbsce_lost)) deallocate(ynbsce_lost) 
       allocate(ynbsce_lost(mlost_data+1))
       ynbsce_lost(1:mlost_data)=tmp_lost
       ynbsce_lost(mlost_data+1)=ynbsce
 
 
       tmp_lost = ynbien_lost
       if(allocated(ynbien_lost)) deallocate(ynbien_lost) 
       allocate(ynbien_lost(mlost_data+1))
       ynbien_lost(1:mlost_data)=tmp_lost
       ynbien_lost(mlost_data+1)=ynbien
 
 
       tmp_lost = wght_lost
       if(allocated(wght_lost)) deallocate(wght_lost) 
       allocate(wght_lost(mlost_data+1))
       wght_lost(1:mlost_data)=tmp_lost
       wght_lost(mlost_data+1)=wght
 
 
       tmp_lost = rmjion_lost
       if(allocated(rmjion_lost)) deallocate(rmjion_lost) 
       allocate(rmjion_lost(mlost_data+1))
       rmjion_lost(1:mlost_data)=tmp_lost
       rmjion_lost(mlost_data+1)=rmjion
 
 
       tmp_lost = yyion_lost
       if(allocated(yyion_lost)) deallocate(yyion_lost) 
       allocate(yyion_lost(mlost_data+1))
       yyion_lost(1:mlost_data)=tmp_lost
       yyion_lost(mlost_data+1)=yyion
 
 
       tmp_lost = yze_lost
       if(allocated(yze_lost)) deallocate(yze_lost) 
       allocate(yze_lost(mlost_data+1))
       yze_lost(1:mlost_data)=tmp_lost
       yze_lost(mlost_data+1)=yze
 
 
       tmp_lost = xksid_lost
       if(allocated(xksid_lost)) deallocate(xksid_lost) 
       allocate(xksid_lost(mlost_data+1))
       xksid_lost(1:mlost_data)=tmp_lost
       xksid_lost(mlost_data+1)=xksid
 
 
       tmp_lost = goose_lost
       if(allocated(goose_lost)) deallocate(goose_lost) 
       allocate(goose_lost(mlost_data+1))
       goose_lost(1:mlost_data)=tmp_lost
       goose_lost(mlost_data+1)=goose
 
 
       tmp_lost = species_lost
       if(allocated(species_lost)) deallocate(species_lost) 
       allocate(species_lost(mlost_data+1))
       species_lost(1:mlost_data)=tmp_lost
       species_lost(mlost_data+1)= lsbeam
 
    else
 
       if(allocated(gtime_lost)) deallocate(gtime_lost) 
       allocate(gtime_lost(mlost_data+1))
       gtime_lost(mlost_data+1)=gtime
 
       if(allocated(ynbsce_lost)) deallocate(ynbsce_lost) 
       allocate(ynbsce_lost(mlost_data+1))
       ynbsce_lost(mlost_data+1)=ynbsce
 
       if(allocated(ynbien_lost)) deallocate(ynbien_lost) 
       allocate(ynbien_lost(mlost_data+1))
       ynbien_lost(mlost_data+1)=ynbien
 
       if(allocated(wght_lost)) deallocate(wght_lost) 
       allocate(wght_lost(mlost_data+1))
       wght_lost(mlost_data+1)=wght
 
       if(allocated(rmjion_lost)) deallocate(rmjion_lost) 
       allocate(rmjion_lost(mlost_data+1))
       rmjion_lost(mlost_data+1)=rmjion
 
       if(allocated(yyion_lost)) deallocate(yyion_lost) 
       allocate(yyion_lost(mlost_data+1))
       yyion_lost(mlost_data+1)=yyion
 
       if(allocated(yze_lost)) deallocate(yze_lost) 
       allocate(yze_lost(mlost_data+1))
       yze_lost(mlost_data+1)=yze
 
       if(allocated(xksid_lost)) deallocate(xksid_lost) 
       allocate(xksid_lost(mlost_data+1))
       xksid_lost(mlost_data+1)=xksid
 
       if(allocated(goose_lost)) deallocate(goose_lost) 
       allocate(goose_lost(mlost_data+1))
       goose_lost(mlost_data+1)=goose
 
       if(allocated(species_lost)) deallocate(species_lost) 
       allocate(species_lost(mlost_data+1))
       species_lost(mlost_data+1)= lsbeam
 
    endif
    mlost_data=mlost_data+1
  end subroutine store_lost_ptcl
  
  subroutine write_lost_ptcl
    integer ii
 
    do ii=1,mlost_data
       write(lunnbx,'(10(1x,1pe12.5))')& 
            &gtime_lost(ii),&
            &ynbsce_lost(ii),&
            &ynbien_lost(ii),&
            &wght_lost(ii),&
            &rmjion_lost(ii),&
            &yyion_lost(ii),&
            &yze_lost(ii),&
            &xksid_lost(ii),&
            &goose_lost(ii),&
            &species_lost(ii)
    enddo
  end subroutine write_lost_ptcl
end module nbi_lost_ptcl
