action_trace.Rd
Tracing and debugging tools for a G3 model
A list of model actions to add tracing to
Boolean, notify if variable is not finite (i.e. Inf, NA, NaN)
Boolean, notify if variable is < 0
Boolean, notify if variable is <= 0
What to do when a variable fails one of the checks? NB: "browser" will not work in a TMB-compiled model.
Boolean, if true print the value of the variable at the point the test fails. NB: This will not work in a TMB-compiled model.
Regular expression(s), variable whose name matches will be traced.
Regular expression(s), action step IDs that match will be traced (or all if NULL)
The main reason to use g3a_trace_var
is to find out why a model is producing NaN in reports / likelihood.
Adding this to your model will help pinpoint the action this originally occurs in, so you can inspect closer for incorrect settings and/or bugs.
The var_re parameter chooses which variables are traced, and should be tweaked to further pinpoint the problem.
Generally, once an error has been found, dig into the code (e.g. by doing edit(g3_to_r(actions))
),
and see what other variables are available for tracing.
Some pre-canned suggestions follow:
c("__num$", "__wgt$") (i.e. default)
This will trace abundance/weight for all stocks, and a good starting point.
^[stock_name]__(num|wgt|cons|suit|totalpredate|consratio|feedinglevel)$
This will, once [stock_name]
is replaced with the name of your stock,
dig deeper into the predation mechanisms.
g3a_trace_timings
will report a variable, trace_timings
,
with the min/mean/max number of seconds spent computing each step in the model.
You can use g3_to_desc
to extract more descriptive names for each step.
stocks <- list(
st = g3_stock("st", 1:10 * 10) |> g3s_age(1, 5) )
actions <- list(
g3a_time(1990, 1995, c(3,3,3,3)),
g3a_initialconditions_normalcv(stocks$st),
g3a_growmature(stocks$st, impl_f = gadget3::g3a_grow_impl_bbinom(
maxlengthgroupgrowth = 2L) ),
NULL )
model_fn <- g3_to_r(c(actions, list(
g3a_trace_var(actions),
g3a_trace_timings(actions),
g3a_report_detail(actions) )))
# Configure set of working parameters
attr(model_fn, "parameter_template") |>
g3_init_val("*.K", 0.3) |>
g3_init_val("*.t0", 0.2) |>
g3_init_val("*.Linf", 80) |>
g3_init_val("*.lencv", 0.1) |>
g3_init_val("*.walpha", 0.01) |>
g3_init_val("*.wbeta", 3) |>
g3_init_val("*.M.#", 0.01) |>
identity() -> params.in
nll <- model_fn(params.in) ; r <- attributes(nll) ; nll <- as.vector(nll)
# Show timings of each step of model
r$trace_timings
#> total min
#> -01:- 0.000 0
#> -01:g3a_initialconditions:st :cf01db7cbcae6d233393 0.001 0
#> 001 0.000 0
#> 005:st :c253dae816f524c9ff63 0.008 0
#> max
#> -01:- 0.000
#> -01:g3a_initialconditions:st :cf01db7cbcae6d233393 0.001
#> 001 0.000
#> 005:st :c253dae816f524c9ff63 0.001
#> mean
#> -01:- 0.000000e+00
#> -01:g3a_initialconditions:st :cf01db7cbcae6d233393 4.166667e-05
#> 001 0.000000e+00
#> 005:st :c253dae816f524c9ff63 3.333333e-04
# Find more informative names with g3_to_desc
as.list(g3_to_desc(actions))
#> $`-01:- `
#> [1] "g3a_time: Start of time period"
#>
#> $`-01:g3a_initialconditions:st :cf01db7cbcae6d233393`
#> [1] "g3a_initialconditions for st"
#>
#> $`005:st :c253dae816f524c9ff63`
#> [1] "g3a_grow for st"
#>
# Try setting parameters to NaN and see what fails:
r <- model_fn(params.in |> g3_init_val("*.t0", NaN))
#> st__num failed test at 1990-1, after 'g3a_initialconditions for st'
#> st__wgt failed test at 1990-1, after 'g3a_grow for st'
r <- model_fn(params.in |> g3_init_val("*.bbin", NaN))
#> st__num failed test at 1990-1, after 'g3a_grow for st'
#> st__wgt failed test at 1990-1, after 'g3a_grow for st'