Debugging odin models can be challenging because:
Here, we outline some strategies for debugging, and describe the new features that aim to make this easier.
print()
As of odin 1.4.5, you can print the value of some variables in the middle of running your model. We will expand and change this functionality in future versions, your feedback is very welcome.
Consider the simple model below, which illustrates the idea:
gen <- odin2::odin({
update(x) <- Normal(x, 1)
initial(x) <- 1
print("x: {x}")
}, quiet = TRUE, debug = TRUE)
sys <- dust_system_create(gen(), list(), 1)
dust_system_run_to_time(sys, 10)
#> [0.000000] x: 0.000000
#> [1.000000] x: 0.367074
#> [2.000000] x: -0.682729
#> [3.000000] x: -2.373219
#> [4.000000] x: -3.115467
#> [5.000000] x: -3.114798
#> [6.000000] x: -2.126540
#> [7.000000] x: -2.125140
#> [8.000000] x: -3.459709
#> [9.000000] x: -4.160030
Here we’ve told odin that we want to watch the variable
x
and print its value at every evaluation (the third line
of the model code. When we run the model it prints out the time in
square brackets then the debug information following. Notice that we
only requested the solution at times 0 and 0.1 but the debug information
shows every point in time that the ODE solver evaluated this system of
equations.
While this function shares its name with R’s print()
it
has entirely different functionality.
print
format stringsFor print formatting, we use glue to drive the formatting, and if you have used that package the format will feel familiar.
The most simple usage is as above; you can refer to variables within
{curly braces}
; so long as your variable is a scalar this
will work. Outside of curly braces the string is printed verbatim.
If your model takes many steps, or if you want to narrow down on a
problem, you may want to enable conditional display of your debug
information. Use the argument when =
to control display,
such as
which will display the value of x
when it is greater
than 1. You can chain together expressions with parentheses and
&&
or ||
and reference any value in
your system. For example:
You can control the way that quantities are displayed through the use of formatting options. The formatting is the same as used by R, so you can experiment in the console easily. The default is to print as a generic floating point number, so this:
is roughly equivalent to writing
See ?sprintf
for more information; but this defaults to
6 decimal places of precision. This may not be appropriate if you are
dealing with numbers that are very large or very small; these both look
a bit silly:
The first loses all information - the only non-zero parts of the number fall after the precision cut-off, while in the second the 6 decimal places just add noise. So above we might prefer:
which we could write in odin’s approach as
Anything after the ;
is interpreted as a format
specifier. You could also do
which would format y
to 2 decimal places. We follow here
the example of the sprintf
transformer example in glue by not including the %
placeholder, but allow all formats that the underlying library
supports.
This is an experimental interface, and it has not been exposed to much real-world use. As such it is possible that you might write fairly innocent looking code and it produce a compiler error rather than a nicer R error - please let us know so we can fix this.
print
{x; d}
) for
variables that are merely integer-like, or you will get unexpected junk
output out. You can however write {as.integer(x); d}
which
will do a conversion to integer and then print thatSometimes just looking at the generated code can be helpful. You can
do this with odin_show
:
odin_show({
initial(x) <- 0
update(x) <- Normal(x, 1)
})
#>
#> ── odin code: ──────────────────────────────────────────────────────────────────
#> #include <dust2/common.hpp>
#> // [[dust2::class(odin)]]
#> // [[dust2::time_type(discrete)]]
#> class odin {
#> public:
#> odin() = delete;
#> using real_type = double;
#> using rng_state_type = monty::random::generator<real_type>;
#> struct shared_state {
#> struct offset_type {
#> struct {
#> size_t x;
#> } state;
#> } offset;
#> };
#> struct internal_state {};
#> using data_type = dust2::no_data;
#> static dust2::packing packing_state(const shared_state& shared) {
#> return dust2::packing{
#> {"x", {}}
#> };
#> }
#> static dust2::packing packing_gradient(const shared_state& shared) {
#> return dust2::packing{
#> };
#> }
#> static shared_state build_shared(cpp11::list parameters) {
#> shared_state::offset_type offset;
#> offset.state.x = 0;
#> return shared_state{offset};
#> }
#> static internal_state build_internal(const shared_state& shared) {
#> return internal_state{};
#> }
#> static void update_shared(cpp11::list parameters, shared_state& shared) {
#> }
#> static void update_internal(const shared_state& shared, internal_state& internal) {
#> }
#> static void initial(real_type time, const shared_state& shared, internal_state& internal, rng_state_type& rng_state, real_type* state) {
#> state[0] = 0;
#> }
#> static void update(real_type time, real_type dt, const real_type* state, const shared_state& shared, internal_state& internal, rng_state_type& rng_state, real_type* state_next) {
#> const auto x = state[0];
#> state_next[0] = monty::random::normal<real_type>(rng_state, x, 1);
#> }
#> static auto zero_every(const shared_state& shared) {
#> return dust2::zero_every_type<real_type>();
#> }
#> };