Skip to content

Using zooMizer as zooplankton component in mizer #6

@gustavdelius

Description

@gustavdelius

I have thought a bit about how to use a zooMizer model as a zooplankton component in a Mizer fish model. It turns out that the new mizer extension mechanism is perfect for this.

ZooMizer needs to supply three functions. First of all, a function for setting up a MizerParams object describing the zooplankton model. Here is the skeleton:

#' Set up MizerParams object for zooplankton model
#' 
#' @param group TODO Patrick: document this
#' @param input 
#' 
#' @return A MizerParams object describing the zooplankton model
newZooMizerParams <- function(groups, input) {
    # This is essentially the current function fZooMizer_run() but without
    # actually running the model
    # TODO Patrick: put the code here
}

The idea is then to use this zooMizer model as a component of a mizer fish model. This would be set up as follows:

zoo_params <- newZooMizerParams()
fish_params <- NS_params  # or any other mizer params object

fish_params <- fish_params %>% 
    setComponent(component = "zoo",
                 initial_value = zoo_params@initial_n,
                 dynamics_fun = "zoo_dynamics",
                 component_params = zoo_params) %>% 
    setResource(resource_dynamics = "resource_zooMizer")

This registers two other functions that zooMizer needs to supply, one to run the zooplankton dynamics and one to aggregate all the plankton spectra into a single resource spectrum for the fish model.

#' Project zooplankton forward by one fish time step
#' 
#' This is called by the fish model at each fish time step to update its
#' zooplankton component.
#' 
#' @param params The MizerParams object describing the fish model
#' @param n_other A list containing the current values of all other ecosystem
#'   components. Only `n_other@zoo` will be needed by this function. It holds
#'   the array (type x size) with the current zooplankton abundances.
#' @param rates A list of rate functions calculated by the fish model, see
#'   [mizer::getRates()] for details. Probably only `` will
#'   be needed.
#' @param t The current time
#' @param dt The amount of time to project forward. This is the fish time step
#'   which will be much larger than the plankton time step.
#' @param ... Unused other parameters
#' 
#' @return An array (type * size) with the updated zooplankton abundances at
#'   time `t + dt`.
zoo_dynamics(params, n_other, rates, t, dt, ...) {
    # get the MizerParams object for the zooplankton
    zoo_params <- other_params(params)$zoo
    
    # get predation mortality imposed by the fish
    mort_from_fish <- rates$resource_mort
    # TODO Patrick: use this mortality to set the mortality in zoo_params
    
    # TODO Patrick: choose the plankton step size as a divisor of fish dt
    steps <- 30
    zoo_dt <- dt / steps
    
    # get array (type x size) with the current zooplankton abundances
    n <- n_other@zoo
    
    l <- new_project_simple(zoo_params, n = n, 
                            n_pp = zoo_params@initial_n_pp,
                            n_other = zoo_params@initial_n_pp,
                            t = t, dt = zoo_dt, steps = steps,
                            effort = list(),
                            resource_dynamics_fn = resource_constant,
                            other_dynamics_fns = list(),
                            rates_fns = lapply(zoo_params@rates_funcs, get))
    return(l$n)
}

#' Return total plankton spectrum
#' 
#' This function will be used as the resource dynamics function by the fish 
#' model. It is used at each fish time step to update the total plankton
#' abundance used by the fish model. However this function does not have to 
#' calculate any dynamics because that will already have been done by 
#' [zoo_dynamics()]
#' 
#' @param params The MizerParams object describing the fish model.
#' @param n_other A list containing the current values of all other ecosystem
#'   components. Only `n_other$zoo` will be needed by this function. It holds
#'   the array (type x size) with the current zooplankton abundances.
#' @param ... Unused other parameters
#' 
#' @return A vector with the total plankton abundances, aggregated over all
#'   plankton types, with one entry for each weight in `params@w_full`
resource_zooMizer(params, n_other, ...) {
    # get the MizerParams object for the zooplankton
    zoo_params <- other_params(params)$zoo
    # phytoplankton abundances stay constant
    n_pp <- zoo_params@initial_n_pp
    # get array (type x size) with the current zooplankton abundances
    # and aggregate over all types
    n <- colSums(n_other$zoo)
    
    # TODO Patrick:
    # Now add n and n_pp and put it into a vector of the right length
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions