
## Creating rxode2 models


The ODE-based model specification may be coded inside four places:

- Inside a `rxode2({})` block statements:

``` r
library(rxode2)
mod <- rxode2({
  # simple assignment
  C2 <- centr/V2

  # time-derivative assignment
  d/dt(centr) <- F*KA*depot - CL*C2 - Q*C2 + Q*C3;
})
```

- Inside a `rxode2("")` string statement:

``` r
mod <- rxode2("
  # simple assignment
  C2 <- centr/V2

  # time-derivative assignment
  d/dt(centr) <- F*KA*depot - CL*C2 - Q*C2 + Q*C3;
")
```

- In a file name to be loaded by rxode2:

``` r
writeLines("
  # simple assignment
  C2 <- centr/V2

  # time-derivative assignment
  d/dt(centr) <- F*KA*depot - CL*C2 - Q*C2 + Q*C3;
", "modelFile.rxode2")
mod <- rxode2(filename='modelFile.rxode2')
unlink("modelFile.rxode2")
```

- In a model function which can be parsed by `rxode2`:

``` r
mod <- function() {
  model({
    # simple assignment
    C2 <- centr/V2

    # time-derivative assignment
    d/dt(centr) <- F*KA*depot - CL*C2 - Q*C2 + Q*C3;
  })
}

mod <- rxode2(mod) # or simply mod() if the model is at the end of the function

# These model functions often have residual components and initial
# (`ini({})`) conditions attached as well. For example the
# theophylline model can be written as:

one.compartment <- function() {
  ini({
    tka <- 0.45 # Log Ka
    tcl <- 1 # Log Cl
    tv <- 3.45    # Log V
    eta.ka ~ 0.6
    eta.cl ~ 0.3
    eta.v ~ 0.1
    add.sd <- 0.7
  })
  model({
    ka <- exp(tka + eta.ka)
    cl <- exp(tcl + eta.cl)
    v <- exp(tv + eta.v)
    d/dt(depot) = -ka * depot
    d/dt(center) = ka * depot - cl / v * center
    cp = center / v
    cp ~ add(add.sd)
  })
}

# after parsing the model
mod <- one.compartment()
```

For the block statement, character string or text file an internal
`rxode2` compilation manager translates the ODE system into C,
compiles it and loads it into the R session.  The
call to `rxode2` produces an object of class `rxode2` which
consists of a list-like structure (environment) with various member
functions.

For the last type of model (a model function), a call to `rxode2`
creates a parsed `rxode2` ui that can be translated to the `rxode2`
compilation model.

``` r
mod$simulationModel

# or
mod$simulationIniModel
```

This is the same type of function required for `nlmixr2` estimation
and can be extended and modified by model piping.  For this reason
will be focused on in the documentation.
