integer function reckon_minb(numproc)
!
! this function gives # of particles per each processor (minb). 
! minb * numproc ~
!(max(nptcl_max,nptcls,nptclf,nptclh,ndep0) + 1000)*1.1_r8*(1.0_r8+xpand_nptcl+frac_orbrr)
!
! where xpand_nptcl is a fraction by which nptcl{*} can be expanded during 
! deposition i.e. to avoid russian roulette (min val 0.05d0) (max val 0.5d0), and
! frac_orbrr is a fraction of population needing to be "underweight"
! before application of russian roulette in orball routine
! accepted values betwe 0.05d0 and 0.5d0.
!
! in case of mpi nubeam, minb can be increase by one  ptcls.to insure that all processors have 
! equal number of ptcls.
! if   minb < numproc, then minb will be set to 1, i.e. each processor has one ptcl.
! by mg, 03 2009
!
  use nbi_com
  implicit none
  real(fp) :: zminb                   ! local
  integer, intent(in) :: numproc ! number of processors
  integer minb_new, minb_one              ! local

  if(xpand_nptcl.le.0.5_fp) then
     xpand_nptcl = max(0.05_fp,min(0.5_fp,xpand_nptcl))
  endif
  frac_orbrr = max(0.05_fp,min(0.5_fp,frac_orbrr))
  zminb=(max(nptcl_max,nptcls,nptclf,nptclh) + 1000)*1.1_fp
  zminb=zminb*(1.0_fp+xpand_nptcl+frac_orbrr)
  minb_new = zminb + 0.5_fp !minb for all processors
  call split_minb(minb_new,minb_one,numproc)
  reckon_minb=minb_one ! minb on each  processor
  return
end function reckon_minb
!
subroutine split_minb(minb_tot,minb_new,numproc)
  use logmod
  implicit none
  integer, intent(in) :: numproc ! number of processors
  integer, intent(inout) :: minb_tot  ! minb for all processors, could be 
                                      ! changed to have factor     
                                      ! equals  'numproc'    
  integer, intent(out) :: minb_new    ! minb for one  processor
  minb_new = minb_tot
  if(numproc>1) then
     if(minb_tot<numproc) then
        call warnlog('split_minb: total #ptcl = ',minb_tot)
        call warnlog('split_minb: less than #processors = ',numproc)
        call warnlog('split_minb: set #ptcl=#processor=',numproc)
        minb_new=1
     endif
     if(mod(minb_tot,numproc).ne.0) then
        minb_new =minb_tot/numproc+1
     else
        minb_new =minb_tot/numproc
     endif
  endif
  minb_tot=minb_new*numproc 

end subroutine split_minb


