# Get started with CAMPSIS model

### Load example from model library

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

    library(campsismod)

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

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

    ## [MAIN]
    ## TVBIO=THETA_BIO
    ## TVKA=THETA_KA
    ## TVVC=THETA_VC
    ## TVVP=THETA_VP
    ## TVQ=THETA_Q
    ## TVCL=THETA_CL
    ##
    ## BIO=TVBIO
    ## KA=TVKA * exp(ETA_KA)
    ## VC=TVVC * exp(ETA_VC)
    ## VP=TVVP * exp(ETA_VP)
    ## Q=TVQ * exp(ETA_Q)
    ## CL=TVCL * exp(ETA_CL)
    ##
    ## [ODE]
    ## d/dt(A_ABS)=-KA*A_ABS
    ## d/dt(A_CENTRAL)=KA*A_ABS + Q/VP*A_PERIPHERAL - Q/VC*A_CENTRAL - CL/VC*A_CENTRAL
    ## d/dt(A_PERIPHERAL)=Q/VC*A_CENTRAL - Q/VP*A_PERIPHERAL
    ##
    ## [F]
    ## A_ABS=BIO
    ##
    ## [ERROR]
    ## CONC=A_CENTRAL/VC
    ## if (CONC <= 0.001) CONC=0.001
    ## CONC_ERR=CONC*(1 + EPS_PROP_RUV)
    ##
    ##
    ## THETA's:
    ##   name index value   fix                            label unit
    ## 1  BIO     1     1 FALSE                  Bioavailability <NA>
    ## 2   KA     2     1 FALSE                  Absorption rate  1/h
    ## 3   VC     3    10 FALSE    Volume of central compartment    L
    ## 4   VP     4    40 FALSE Volume of peripheral compartment    L
    ## 5    Q     5    20 FALSE           Inter-compartment flow  L/h
    ## 6   CL     6     3 FALSE                        Clearance  L/h
    ## OMEGA's:
    ##   name index index2 value   fix type
    ## 1   KA     1      1    25 FALSE  cv%
    ## 2   VC     2      2    25 FALSE  cv%
    ## 3   VP     3      3    25 FALSE  cv%
    ## 4    Q     4      4    25 FALSE  cv%
    ## 5   CL     5      5    25 FALSE  cv%
    ## SIGMA's:
    ##       name index index2 value   fix type
    ## 1 PROP_RUV     1      1   0.1 FALSE   sd
    ## No variance-covariance matrix
    ##
    ## Compartments:
    ## A_ABS (CMT=1)
    ## A_CENTRAL (CMT=2)
    ## A_PERIPHERAL (CMT=3)

### Write CAMPSIS model

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

    model %>% write(file="path_to_model_folder")

    ## [1] TRUE

    list.files("path_to_model_folder")

    ## [1] "model.campsis" "omega.csv"     "sigma.csv"     "theta.csv"

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.

    model <- read.campsis(file="path_to_model_folder")
    show(model)

    ## [MAIN]
    ## TVBIO=THETA_BIO
    ## TVKA=THETA_KA
    ## TVVC=THETA_VC
    ## TVVP=THETA_VP
    ## TVQ=THETA_Q
    ## TVCL=THETA_CL
    ##
    ## BIO=TVBIO
    ## KA=TVKA * exp(ETA_KA)
    ## VC=TVVC * exp(ETA_VC)
    ## VP=TVVP * exp(ETA_VP)
    ## Q=TVQ * exp(ETA_Q)
    ## CL=TVCL * exp(ETA_CL)
    ##
    ## [ODE]
    ## d/dt(A_ABS)=-KA*A_ABS
    ## d/dt(A_CENTRAL)=KA*A_ABS + Q/VP*A_PERIPHERAL - Q/VC*A_CENTRAL - CL/VC*A_CENTRAL
    ## d/dt(A_PERIPHERAL)=Q/VC*A_CENTRAL - Q/VP*A_PERIPHERAL
    ##
    ## [F]
    ## A_ABS=BIO
    ##
    ## [ERROR]
    ## CONC=A_CENTRAL/VC
    ## if (CONC <= 0.001) CONC=0.001
    ## CONC_ERR=CONC*(1 + EPS_PROP_RUV)
    ##
    ##
    ## THETA's:
    ##   name index value   fix                            label unit
    ## 1  BIO     1     1 FALSE                  Bioavailability <NA>
    ## 2   KA     2     1 FALSE                  Absorption rate  1/h
    ## 3   VC     3    10 FALSE    Volume of central compartment    L
    ## 4   VP     4    40 FALSE Volume of peripheral compartment    L
    ## 5    Q     5    20 FALSE           Inter-compartment flow  L/h
    ## 6   CL     6     3 FALSE                        Clearance  L/h
    ## OMEGA's:
    ##   name index index2 value   fix type
    ## 1   KA     1      1    25 FALSE  cv%
    ## 2   VC     2      2    25 FALSE  cv%
    ## 3   VP     3      3    25 FALSE  cv%
    ## 4    Q     4      4    25 FALSE  cv%
    ## 5   CL     5      5    25 FALSE  cv%
    ## SIGMA's:
    ##       name index index2 value   fix type
    ## 1 PROP_RUV     1      1   0.1 FALSE   sd
    ## No variance-covariance matrix
    ##
    ## Compartments:
    ## A_ABS (CMT=1)
    ## A_CENTRAL (CMT=2)
    ## A_PERIPHERAL (CMT=3)

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 RxODE

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

    rxmod <- model %>% export(dest="RxODE")
    rxmod

    ## An object of class "rxode_model"
    ## Slot "code":
    ##  [1] "TVBIO=THETA_BIO"
    ##  [2] "TVKA=THETA_KA"
    ##  [3] "TVVC=THETA_VC"
    ##  [4] "TVVP=THETA_VP"
    ##  [5] "TVQ=THETA_Q"
    ##  [6] "TVCL=THETA_CL"
    ##  [7] ""
    ##  [8] "BIO=TVBIO"
    ##  [9] "KA=TVKA * exp(ETA_KA)"
    ## [10] "VC=TVVC * exp(ETA_VC)"
    ## [11] "VP=TVVP * exp(ETA_VP)"
    ## [12] "Q=TVQ * exp(ETA_Q)"
    ## [13] "CL=TVCL * exp(ETA_CL)"
    ## [14] "d/dt(A_ABS)=-KA*A_ABS"
    ## [15] "d/dt(A_CENTRAL)=KA*A_ABS + Q/VP*A_PERIPHERAL - Q/VC*A_CENTRAL - CL/VC*A_CENTRAL"
    ## [16] "d/dt(A_PERIPHERAL)=Q/VC*A_CENTRAL - Q/VP*A_PERIPHERAL"
    ## [17] "f(A_ABS)=BIO"
    ## [18] "CONC=A_CENTRAL/VC"
    ## [19] "if (CONC <= 0.001) CONC=0.001"
    ## [20] "CONC_ERR=CONC*(1 + EPS_PROP_RUV)"
    ##
    ## Slot "theta":
    ## THETA_BIO  THETA_KA  THETA_VC  THETA_VP   THETA_Q  THETA_CL
    ##         1         1        10        40        20         3
    ##
    ## Slot "omega":
    ##            ETA_KA     ETA_VC     ETA_VP      ETA_Q     ETA_CL
    ## ETA_KA 0.06062462 0.00000000 0.00000000 0.00000000 0.00000000
    ## ETA_VC 0.00000000 0.06062462 0.00000000 0.00000000 0.00000000
    ## ETA_VP 0.00000000 0.00000000 0.06062462 0.00000000 0.00000000
    ## ETA_Q  0.00000000 0.00000000 0.00000000 0.06062462 0.00000000
    ## ETA_CL 0.00000000 0.00000000 0.00000000 0.00000000 0.06062462
    ##
    ## Slot "sigma":
    ##              EPS_PROP_RUV
    ## EPS_PROP_RUV         0.01

### Export CAMPSIS model to mrgsolve

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

    mrgmod <- model %>% export(dest="mrgsolve")
    mrgmod

    ## An object of class "mrgsolve_model"
    ## Slot "param":
    ## [1] "[PARAM] @annotated"        "THETA_BIO : 1 : THETA_BIO"
    ## [3] "THETA_KA : 1 : THETA_KA"   "THETA_VC : 10 : THETA_VC"
    ## [5] "THETA_VP : 40 : THETA_VP"  "THETA_Q : 20 : THETA_Q"
    ## [7] "THETA_CL : 3 : THETA_CL"
    ##
    ## Slot "cmt":
    ## [1] "[CMT] @annotated"          "A_ABS : ABS"
    ## [3] "A_CENTRAL : CENTRAL"       "A_PERIPHERAL : PERIPHERAL"
    ##
    ## Slot "main":
    ##  [1] "[MAIN]"                        "double TVBIO=THETA_BIO;"
    ##  [3] "double TVKA=THETA_KA;"         "double TVVC=THETA_VC;"
    ##  [5] "double TVVP=THETA_VP;"         "double TVQ=THETA_Q;"
    ##  [7] "double TVCL=THETA_CL;"         ""
    ##  [9] "double BIO=TVBIO;"             "double KA=TVKA * exp(ETA_KA);"
    ## [11] "double VC=TVVC * exp(ETA_VC);" "double VP=TVVP * exp(ETA_VP);"
    ## [13] "double Q=TVQ * exp(ETA_Q);"    "double CL=TVCL * exp(ETA_CL);"
    ## [15] "F_A_ABS=BIO;"
    ##
    ## Slot "ode":
    ## [1] "[ODE]"
    ## [2] "dxdt_A_ABS=-KA*A_ABS;"
    ## [3] "dxdt_A_CENTRAL=KA*A_ABS + Q/VP*A_PERIPHERAL - Q/VC*A_CENTRAL - CL/VC*A_CENTRAL;"
    ## [4] "dxdt_A_PERIPHERAL=Q/VC*A_CENTRAL - Q/VP*A_PERIPHERAL;"
    ##
    ## Slot "omega":
    ## [1] "[OMEGA] @annotated @block"
    ## [2] "ETA_KA : 0.0606246218164348 : ETA_KA"
    ## [3] "ETA_VC : 0 0.0606246218164348 : ETA_VC"
    ## [4] "ETA_VP : 0 0 0.0606246218164348 : ETA_VP"
    ## [5] "ETA_Q : 0 0 0 0.0606246218164348 : ETA_Q"
    ## [6] "ETA_CL : 0 0 0 0 0.0606246218164348 : ETA_CL"
    ##
    ## Slot "sigma":
    ## [1] "[SIGMA] @annotated @block"          "EPS_PROP_RUV : 0.01 : EPS_PROP_RUV"
    ##
    ## Slot "table":
    ## [1] "[TABLE]"
    ## [2] "capture CONC=A_CENTRAL/VC;"
    ## [3] "if (CONC <= 0.001) CONC=0.001;"
    ## [4] "capture CONC_ERR=CONC*(1 + EPS_PROP_RUV);"
    ##
    ## Slot "capture":
    ## character(0)