In theory, the TMB and R output of gadget3 should be identically-behaving functions, so instead of trying to decipher what the TMB model is doing wrong, you can use R function instead in a more familiar environment, for example using standard tools such as
You can also use
edit() to edit the R function directly and re-run the model:
# Model setup will look something like this ling_model <- g3_to_r(...) ling_model <- edit(ling_model) ; ling_model(ling_param)
…this is useful if you want to add trace
print() statements around a particular part of the model that’s failing, e.g. or insert a breakpoint by adding a
It’s possible to have a working R model that doesn’t compile using TMB, due to the relative strictness of the Eigen array library in comparison to R arrays, for example. In which case you’ll need to debug the TMB version.
If the model crashes whilst forming the TMB ADFun object, then it takes your R session with it. To prevent this, wrap
TMB::gdbsource() as follows:
# Model setup will look something like this tmb_ling <- g3_to_tmb(...) tmb_param <- attr(tmb_ling, 'parameter_template') writeLines(TMB::gdbsource(g3_tmb_adfun( tmb_ling, tmb_param, compile_flags = "-g", output_script = TRUE)))
output_script = TRUE tells
g3_tmb_adfun() to, after compilation, write a temporary R script that will build the TMB ADFun object (and presumably crash in the process).
TMB::gdbsource() in turn runs a provided R script in a fresh R session wrapped in gdb. By default it will print a stacktrace and quit, which should show you where the crash occured.
As with the R model you can edit the raw C++ source before building:
tmb_ling <- edit(tmb_ling) writeLines(TMB::gdbsource(g3_tmb_adfun( tmb_ling, tmb_param, compile_flags = "-g", output_script = TRUE)))
Through this you can…
std::cout << ling__Linf << std::endl;
().rows()to get the size of an array expression
In theory you can use
interactive = TRUE with
TMB::gdbsource(), however as this eats error messages it’s better to do this by hand:
> g3_tmb_adfun(tmb_ling, tmb_param, compile_flags = "-g", output_script = TRUE)  "/tmp/RtmpysTVvW/file3da4a6f13a80c.R" R -d gdb (gdb) run --vanilla < /tmp/RtmpysTVvW/file3da4a6f13a80c.R . . . Compilation, crash at some point . . . (gdb) up (gdb) up (gdb) up (gdb) call ling_imm__consratio.print() Array dim: 35 1 8 Array val: -nan -nan -nan -nan (gdb) call ling_imm__num.print() Array dim: 35 1 8 Array val: -nan -nan -nan -nan -nan (gdb) print cur_time $5 = 0
Note that for the
.print() method to be available for arrays, it has to be referenced at least once in the model source, otherwise it won’t be compiled in. Use
edit(tmb_ling) to add it somewhere first.