Build an odin model generator from its intermediate representation, as generated by odin_parse. This function is for advanced use.

odin_build(x, options = NULL)

Arguments

x

An odin ir (json) object or output from odin_validate.

options

Options to pass to the build stage (see odin_options

Details

In applications that want to inspect the intermediate representation rather before compiling, rather than directly using odin, use either odin_parse or odin_validate and then pass the result to odin::odin_build.

The return value of this function includes information about how long the compilation took, if it was successful, etc, in the same style as odin_validate:

success

Logical, indicating if compilation was successful

elapsed

Time taken to compile the model, as a proc_time object, as returned by proc.time.

output

Any output produced when compiling the model (only present if compiling to C, and if the cache was not hit.

model

The model itself, as an odin_generator object, as returned by odin.

ir

The intermediate representation.

error

Any error thrown during compilation

See also

odin_parse, which creates intermediate representations used by this function.

Examples

# Parse a model of exponential decay
ir <- odin::odin_parse({
  deriv(y) <- -0.5 * y
  initial(y) <- 1
})

# Compile the model:
options <- odin::odin_options(target = "r")
res <- odin::odin_build(ir, options)

# All results:
res
#> $success
#> [1] TRUE
#> 
#> $elapsed
#>    user  system elapsed 
#>   0.000   0.002   0.002 
#> 
#> $output
#> character(0)
#> 
#> $model
#> <odin_model> object generator
#>   Public:
#>     ir: function () 
#>     initialize: function (..., user = list(...), unused_user_action = NULL, use_dde = FALSE) 
#>     set_user: function (..., user = list(...), unused_user_action = NULL) 
#>     deriv: function (t, y) 
#>     initial: function (t) 
#>     run: function (t, y = NULL, ..., use_names = TRUE, tcrit = NULL) 
#>     contents: function () 
#>     engine: function () 
#>     transform_variables: function (y) 
#>   Private:
#>     name: odin
#>     core: list
#>     data: NULL
#>     use_dde: NULL
#>     init: NULL
#>     interpolate_t: NULL
#>     delay: FALSE
#>     discrete: FALSE
#>     variable_order: NULL
#>     output_order: NULL
#>     ynames: NULL
#>     n_out: NULL
#>   Parent env: <environment: namespace:odin>
#>   Locked objects: TRUE
#>   Locked class: FALSE
#>   Portable: TRUE
#> 
#> $ir
#> {"version":"1.5.10","config":{"base":"odin","include":null,"custom":null},"meta":{"internal":"internal","user":"user","state":"state","result":"dstatedt","output":"output","time":"t","initial_time":"initial_t"},"features":{"continuous":true,"discrete":false,"mixed":false,"has_array":false,"has_output":false,"has_user":false,"has_delay":false,"has_interpolate":false,"has_stochastic":false,"has_data":false,"has_compare":false,"has_include":false,"has_debug":false,"has_derivative":false,"initial_time_dependent":false},"data":{"elements":[{"name":"initial_y","location":"internal","storage_type":"double","rank":0,"dimnames":null,"stage":"constant"},{"name":"y","location":"variable","storage_type":"double","rank":0,"dimnames":null,"stage":"time"}],"variable":{"length":1,"contents":[{"name":"y","offset":0,"initial":"initial_y"}]},"output":{"length":0,"contents":[]}},"equations":[{"name":"deriv_y","type":"expression_scalar","source":[1],"depends":{"functions":["*","-"],"variables":["y"]},"lhs":"y","rhs":{"value":["*",["-",0.5],"y"]}},{"name":"initial_y","type":"expression_scalar","source":[2],"depends":null,"lhs":"initial_y","rhs":{"value":1}}],"debug":[],"components":{"create":{"variables":[],"equations":["initial_y"]},"user":{"variables":[],"equations":[]},"initial":{"variables":[],"equations":[]},"rhs":{"variables":["y"],"equations":["deriv_y"]},"update_stochastic":{"variables":[],"equations":[]},"output":{"variables":[],"equations":[]},"compare":{"variables":[],"equations":[]}},"user":[],"interpolate":{"min":[],"max":[],"critical":[]},"source":["deriv(y) <- -0.5 * y","initial(y) <- 1"]} 
#> 
#> $error
#> NULL
#> 

# The model:
mod <- res$model$new()
mod$run(0:10)
#>     t           y
#> 1   0 1.000000000
#> 2   1 0.606531435
#> 3   2 0.367878262
#> 4   3 0.223131090
#> 5   4 0.135336063
#> 6   5 0.082085691
#> 7   6 0.049787658
#> 8   7 0.030197826
#> 9   8 0.018316115
#> 10  9 0.011109359
#> 11 10 0.006738194