<- mread("model/azithro.mod") mod
3 Read and write models
3.1 Read in a model file
Use the mread()
function to read in a model coded in a file on the disk. For example, to read in a model for azithromycin population PK
You can use any extension, but I’m preferring something like .mod
or .txt
to prevent your text editor from trying to reformat the model code.
You can update the model object on the fly by passing arguments to mread()
. For example, to run the simulation out to one week and only have CP
appear in the simulated output
<- mread("model/azithro.mod", end = 168, outvars = "CP") mod
To do dynamic capture on loading the model, pass the capture
argument. For example, I want to get the clearance (CL
) and the ETA associated with clearance (ETACL
) into the simulation output
<- mread("model/azithro.mod", capture = "CL, ETACL") mod
To cache the compiled model, use mread_cache()
<- mread_cache("model/azithro.mod") mod
When you try to read the model again, it will load from the cache
<- mread_cache("model/azithro.mod") mod
. Loading model from cache.
To read in a model from the model library, run modlib()
. For example, to read in a two-compartment model from the library
<- modlib("pk2") mod
To build the model in a specific location , use the soloc
argument
<- modlib("irm1", soloc = "build") mod
. Loading model from cache.
This will build the model in the directory build
off the current working directory.
You don’t have to compile the model
<- modlib("popex", compile = FALSE) mod
3.2 Write a model to file
With mrgsolve 1.5.1, you can also write a model object back to a file. The format of the file can be either native mrgsolve format or a yaml-formatted file. When the model object is written back to file, all of the updates that were made to the model object will be written to the file.
Once we have a model object
<- modlib("popex") mod
we can update it
<- update(mod, end = 72, outvars = "IPRED, CL, DV")
mod <- param(mod, TVCL = 1.2) mod
and then write the object out to yaml
format with mwrite_yaml()
mwrite_yaml(mod, file = "popex.yaml")
Now, this model object is in a file called popex.yaml
.
To read the model back in, use mread_yaml()
<- mread_yaml("popex.yaml") mod2
You’ll see that the updated settings are retained in mod2
param(mod2)
.
. Model parameters (N=4):
. name value . name value
. TVCL 1.2 | TVV 24
. TVKA 0.5 | WT 70
@end mod2
. [1] 72
outvars(mod2)
. $cmt
. character(0)
.
. $capture
. [1] "CL" "IPRED" "DV"
In addition to writing the model object to yaml format, you can also write to native mrgsolve format with the mwrite_cpp()
function
mwrite_cpp(mod2, file = "popex-updated.mod")