Add area dimensions to g3_stock classes

g3_areas(area_names)

g3s_livesonareas(inner_stock, areas)

g3s_areagroup(inner_stock, areagroups)

Arguments

area_names

A character vector of area names to use in the model

inner_stock

A g3_stock that we extend with an area dimension

areas

A vector of numeric areas that the stock is part of

areagroups

A list mapping names to vectors of numeric areas the stock is part of

Details

g3s_livesonareas breaks up a stock by area. Within a model, areas are only referred to by integer, however if these are named then that name will be used for report output.

Each area will be defined as a variable in your model as area_x, allowing you to use names in formulas, e.g. run_f = quote( area == area_x ).

g3_areas is a helper to map a set of names to an integer

Inside a model each area will only be referred to by integer.

g3s_areagroup allows areas to be combined, this is mostly used internally by g3l_catchdistribution.

Value

g3_areas

A named integer vector, assigning each of area_names a number.

g3s_livesonareas

A g3_stock with an additional 'area' dimension.

When iterating over the stock, iterate over each area in turn, area will be set to the current integer area.

When intersecting with another stock, only do anything if area is also part of our list of areas.

g3s_areagroup

A g3_stock with an additional 'area' dimension.

When iterating over the stock, iterate over each areagroup in turn, area will be set to the first area in the group.

When intersecting with another stock, only do anything if area is part of one of the groups.

Examples

library(magrittr)

# Make a lookup so we can refer to areas by name
area_names <- g3_areas(c('a', 'b', 'c', 'd', 'e'))
stopifnot(area_names == c(a=1, b=2, c=3, d=4, e=5))

# Define a stock with 3 lengthgroups and 3 areas
stock <- g3_stock('name', c(1, 10, 100)) %>%
    g3s_livesonareas(area_names[c('a', 'b', 'c')])

# Area variables will be defined, so you can refer to them in formulas:
g3a_migrate(stock, g3_parameterized("migrate_spring"),
    run_f = ~area == area_b && cur_step == 2)
#> $`002:name                :001`
#> ~{
#>     debug_trace("Clear name migration matrix")
#>     name__migratematrix[] <- NaN
#> }
#> <environment: 0x55977c88bfd0>
#> 
#> $`002:name                :002:f43e386b07dca64199ee`
#> ~{
#>     debug_label("g3a_migrate: Migration of name")
#>     name__postmigratenum[] <- 0
#>     name__postmigratewgt[] <- 0
#>     for (name__area_idx in seq_along(name__areas)) g3_with(`:=`(area, 
#>         name__areas[[name__area_idx]]), if (area == area_b && 
#>         cur_step == 2) {
#>         debug_trace("Fill in any gaps in the migration matrix")
#>         if (any(is.nan(name__migratematrix[, name__area_idx]))) {
#>             for (name__destarea_idx in seq_along(name__areas)) g3_with(`:=`(dest_area, 
#>                 name__areas[[name__destarea_idx]]), name__migratematrix[[name__destarea_idx, 
#>                 name__area_idx]] <- g3_param("migrate_spring"))
#>             name__migratematrix[, name__area_idx] <- g3a_migrate_normalize(name__migratematrix[, 
#>                 name__area_idx], name__area_idx, 1)
#>         }
#>         debug_trace("Apply migration matrix to current stock")
#>         for (name__destarea_idx in seq_along(name__areas)) g3_with(`:=`(dest_area, 
#>             name__areas[[name__destarea_idx]]), `:=`(migrate_prop, 
#>             name__migratematrix[[name__destarea_idx, name__area_idx]]), 
#>             {
#>                 name__postmigratewgt[, name__destarea_idx] <- ratio_add_vec(name__postmigratewgt[, 
#>                   name__destarea_idx], name__postmigratenum[, 
#>                   name__destarea_idx], name__wgt[, name__area_idx], 
#>                   migrate_prop * name__num[, name__area_idx])
#>                 name__postmigratenum[, name__destarea_idx] <- name__postmigratenum[, 
#>                   name__destarea_idx] + migrate_prop * name__num[, 
#>                   name__area_idx]
#>             })
#>     }
#>     else {
#>         debug_trace("Copy not-migrating stocks")
#>         name__postmigratewgt[, name__area_idx] <- name__postmigratewgt[, 
#>             name__area_idx] + name__wgt[, name__area_idx]
#>         name__postmigratenum[, name__area_idx] <- name__postmigratenum[, 
#>             name__area_idx] + name__num[, name__area_idx]
#>     })
#>     debug_trace("Switch around __postmigratenum and __num")
#>     name__num <- name__postmigratenum
#>     name__wgt <- name__postmigratewgt
#> }
#> <environment: 0x5597798ab1a8>
#> 

# Use stock_instance to see what the array would look like
g3_stock_instance(stock)
#>          area
#> length     a  b  c
#>   1:10    NA NA NA
#>   10:100  NA NA NA
#>   100:Inf NA NA NA

# Define a stock that groups areas into "north" and "south"
stock <- g3_stock('name', c(1, 10, 100)) %>%
    g3s_areagroup(list(
        north = area_names[c('a', 'b', 'c')],
        south = area_names[c('d', 'e')]))

# Use stock_instance to see what the array would look like
g3_stock_instance(stock)
#>          area
#> length    north south
#>   1:10       NA    NA
#>   10:100     NA    NA
#>   100:Inf    NA    NA