Nothing
#' Calculate initial population abundances
#'
#' This function uses the model parameters and other parameters to calculate
#' initial values for the species number densities. These initial
#' abundances are currently quite arbitrary and not close to the steady state.
#' We intend to improve this in the future.
#'
#' @param params The model parameters. An object of type
#' \linkS4class{MizerParams}.
#' @param a A parameter with a default value of 0.35.
#' @param n0_mult Multiplier for the abundance at size 0 when using defaults
#' edition 1. If not supplied, `kappa / 1000` is used. This argument is
#' ignored for defaults edition 2 and later.
#' @export
#' @concept helper
#' @return An `ArraySpeciesBySize` object (species x size) of population abundances.
#' @examples
#' init_n <- get_initial_n(NS_params)
get_initial_n <- function(params, n0_mult = NULL, a = 0.35) {
if (!is(params,"MizerParams"))
stop("params argument must of type MizerParams")
no_sp <- nrow(params@species_params)
no_w <- length(params@w)
initial_n <- params@initial_n
if (defaults_edition() < 2) {
# N = N0 * Winf^(2*n-q-2+a) * w^(-n-a)
# Reverse calc n and q from intake_max and search_vol slots (could add get_n function)
n <- params@species_params[[1, "n"]]
q <- params@species_params[[1, "q"]]
# Guessing at a suitable n0 value based on kappa - this was figured out
# using trial and error and should be updated
if (is.null(n0_mult)) {
lambda <- 2 + q - n
kappa <- params@cc_pp[1] / (params@w_full[1]^(-lambda))
n0_mult <- kappa / 1000
}
initial_n[] <- unlist(tapply(params@w, 1:no_w, function(wx,n0_mult,w_max,a,n,q)
n0_mult * w_max^(2 * n - q - 2 + a) * wx^(-n - a),
n0_mult = n0_mult, w_max = params@species_params$w_max, a=a, n=n, q=q))
#set densities at w > w_max to 0
initial_n[unlist(tapply(params@w,1:no_w,function(wx,w_max) w_max<wx, w_max=params@species_params$w_max))] <- 0
# Also any densities at w < w_min set to 0
initial_n[unlist(tapply(params@w, 1:no_w,
function(wx, w_min) w_min > wx,
w_min = params@species_params$w_min)
)
] <- 0
return(ArraySpeciesBySize(initial_n, value_name = "Number density",
type = "density", params = params))
}
p <- params
p@initial_n[] <- 0
p@initial_n_pp[] <- resource_power_law(p, p@resource_params$kappa,
p@resource_params$lambda)
p@interaction[] <- 0
income <- getEReproAndGrowth(p) + p@metab
mort <- getFMort(p)
growth <- getEGrowth(p)
diffusion <- getDiffusion(p)
N0_vec <- numeric(no_sp)
for (i in seq_len(no_sp)) {
# At small sizes the income should be A w^n. Determine A
# Use w_min_idx + 1 in case user has implemented reduced growth
# for the smallest size class (see e.g. #241)
iw <- p@w_min_idx[i] + 1
A <- income[i, iw] / (p@w[iw] ^ p@species_params[[i, "n"]])
mort[i, ] <- mort[i, ] + 0.4 * A * p@w ^ (p@species_params[[i, "n"]] - 1)
# We start with an arbitrary population at the smallest size class
N0_vec[i] <- 1
}
n_exact_matrix <- get_steady_state_n(p, growth, mort, diffusion, N0_vec)
for (i in seq_len(no_sp)) {
idxs <- p@w_min_idx[i]:(min(which(c(growth[i, ], 0) <= 0)) - 1)
# Steady state solution of the upwind-difference scheme used in project
p@initial_n[i, idxs] <- n_exact_matrix[i, idxs]
}
p <- matchBiomasses(p)
return(ArraySpeciesBySize(p@initial_n, value_name = "Number density",
type = "density", params = p))
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.