R/dispersal.R

#' Adult or larval dispersal
#'
#' Disperses individuals of specified \code{ages} according to a user defined connectivity matrix. Consider using the function \code{\link{larvaldispersal}} for dispersing larvae as that function also incorporates mortality linked to habitat carrying capacity.
#'
#' @param fish a matrix of fish numbers by site (rows) and by age (columns). Can easily be generated by \code{initpop}
#' @param cm a connectivity matrix containing the probability of dispersing from each starting location (rows) to each end location (columns). Can easily be generated by \code{initcm}
#' @param ages age of fish to be dispersed
#'
#' @return
#' @export
#'
#' @examples
#'
#' cells=5000
#'
#' # homogeneous dispersal
#' cm <- matrix(1/cells,nrow=cells,ncol=cells)
#' fish <- initpop(initial_abun=250*10^6,cells=cells,maxage=50,rate=0.7)
#' fish <- dispersal(fish,cm,ages=5:50)
#'
#' # exponential dispersal kernal
#' cm <- initcm(domain=BESTMPA_domain,e_fold=75000,cell_size=20000)
#' fish <- initpop(initial_abun=250*10^6,cells=length(BESTMPA_domain),maxage=50,rate=0.7)
#' fish <- dispersal(fish,cm,ages=5:50)
#'
#' @seealso \code{\link{larvaldispersal}}
dispersal <- function(fish,cm,ages=5:50){
    if(length(ages)==1){
        fish[,ages+1] <- roundprob(colSums(fish[,ages+1]*cm))
    } else {
        fish[,ages+1] <- roundprob(apply(fish[,ages+1],2,function(x) colSums(x*cm)))
    }
    return(fish)
}
remi-daigle/BESTMPA documentation built on May 27, 2019, 4:55 a.m.