---
title: "Get started with Campsis model"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Get started with Campsis model}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

### Load example from model library

First import the `campsismod` package. This step is not required if you have already loaded the `campsis` package.

```{r, message=FALSE}
library(campsismod)
```

Load 2-compartment PK model from built-in model library and show content.

```{r}
model <- model_suite$pk$`2cpt_fo`
show(model)
```

### Write Campsis model

A Campsis model can be persisted on your local drive as follows:

```{r}
model %>% write(file="path_to_model_folder")
list.files("path_to_model_folder")
```

As shown, the output directory will contain the model (all code records) and 1 `csv` file per type of parameter (THETA, OMEGA and SIGMA). 

### Read Campsis model

To read a Campsis model from your local drive, use the `read.campsis` function.
The exact same model should be retrieved.

```{r}
model <- read.campsis(file="path_to_model_folder")
show(model)
```

The `MAIN` record is the part of the model where your model parameters are defined. The `ODE` record is where your ordinary differential equations (ODE) go, as well as any equation depending on the simulation time. The `ERROR` record is the place where the error model is defined. The model parameters are then shown, followed by the all the compartments.
 
### Export Campsis model to rxode2

`campsismod` has powerful export capabilities to `rxode2` and `mrgsolve`, the 2 simulation engines supported by `campsis`.
The following code exports the model to `rxode2`. Please note that this step is implicit in Campsis when you call the `simulate` method with your preferred simulation engine.

```{r}
rxmod <- model %>% export(dest="rxode2")
rxmod
```

### Export Campsis model to mrgsolve

The following code exports the model to `mrgsolve` (text form).

```{r}
mrgmod <- model %>% export(dest="mrgsolve")
mrgmod
```