Description Usage Arguments Details Value Examples
A factory function that creates a sdModelClass
object from
functions that describe the changes in the system states in response to
external actions and other system flows, and a default scenario describing
the system environment (variables and default values).
1 2 3 4 |
modelId |
A character string with the model ID. Any non-word character
will be removed and the result will be converted to a valid name (see
|
modelDescription |
A character string with the model description. |
defaultScenario |
The model default scenario, a
|
aux |
(Optional) A list with the model auxiliary equations in strings or
R-expressions written in R-format to assist in the
They have access to the following variables: (t, st, ct, par, inp, sw, aux).
Where The auxiliary equations are evaluated at each time step during simulations
and passed via the argument See the function |
DifferentialEquations |
An R-function that computes the values of the state variables derivatives in the ODE system (the model definition) at time t. It must be defined as: function(t, st, ct, par, inp, sw, aux).
Where The return value of the |
InitVars |
(Optional) An R-function that initialize or change the initial state values and/or other model variables before the solver call when running a simulation. It can be used, for example, to compute some dependent parameter variables or the initial state variables, using the arguments. It must be defined as function(st, ct, par, inp, sw, aux). Where The return value of the |
PostProcessVars |
(Optional) An R-function that receives the simulation
output inside the It must be defined as: function(outputTrajectory, auxTrajectory,
tsTrajectory, ct, par, inp, sw).
Where The return value of |
RootSpecification |
(Optional) A numeric vector containing the times to
trigger the When a root is found, the simulation triggers an event by calling
the When specified as a function it must be defined as: function(t, st, ct, par,
inp, sw, aux).
Where It should return a numeric vector. If any element of this vector is zero an event is trigged. |
EventFunction |
(Optional) An R-function that specifies the event. It must be defined as: function(t, st, ct, par, inp, sw, aux).
Where It should return the state-values (some of which modified), as a vector with
the variables in the right order. If no |
globalFunctions |
A named list of extra functions that can be executed in the scope of any other function or auxiliary equation defined in the model. They can be called using the list names. |
To load a model from a XML file use the sdLoadModel
function.
To simulate a model in different scenarios use the sdSimulate
function.
A sdModelClass
object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | ## HOW TO CREATE A MODEL
## The Lotka-Volterra consumer-prey model
# parameters in a list with units and descriptions in separete lists
pars <- list(rI = 0.2,
rG = 1.0,
rM = 0.2 ,
AE = 0.5,
K = 10)
parsUnits <- list(rI = "1/day",
rG = "1/day",
rM = "1/day" ,
AE = "dimensionless",
K = "mmol/m3")
parsDescription <- list(rI = "rate of ingestion",
rG = "growth rate of prey",
rM = "mortality rate of consumer" ,
AE = "assimilation efficiency",
K = "carrying capacity")
# state variables in a data.frame with values and description
st <- data.frame(Variable = c("P", "C"),
Value = c(1, 2),
Description = c("Prey", "Consumer"))
# time sequence
times <- list(from = 0, to = 200, by = 1)
# auxiliary equations
aux <- list(IngestC = "par$rI * st$P * st$C",
GrowthP = "par$rG * st$P * (1 - st$P/par$K)",
"MortC <- par$rM * st$C")
# differential equations
LVode <- function(t, st, ct, par, inp, sw, aux)
{
dP <- aux$GrowthP - aux$IngestC
dC <- aux$IngestC * par$AE - aux$MortC
return(list(c(dP, dC)))
}
# create the scenario
lvscen <- sdScenario(scenarioId = "LVscen", times = times, method = "lsoda",
state = st, parameter = pars,
unit = parsUnits, description = parsDescription)
# create the model object
lv <- sdModel(modelId = "Lotka-Volterra", defaultScenario = lvscen,
DifferentialEquations = LVode,
aux = aux)
# validate the model ode
lv$verifyModel(verbose = TRUE)
# simulate the model and plot the results
outlv <- sdSimulate(model = lv, storeAuxTrajectory = TRUE)
outlv$plot("P C", multipleYAxis = TRUE,
main = "Prey and Consumer by Lotka-Volterra")
outlv$saveSimulationTrajectories(path = "LV")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.