sdCoupledModel: Creates a Coupled System Dynamics Model

Description Usage Arguments Details Value Examples

Description

A factory function that creates a sdCoupledModelClass object made up of instanced sdModelClass, sdStaticModelClass and/or sdCoupledModelClass components and a list of connections that define the flow of information between components.

To load a coupled model from a XML file use the sdLoadModel function.

Usage

1
2
sdCoupledModel(coupledModelId = NULL, coupledModelDescription = NULL,
  components = NULL, connections = NULL)

Arguments

coupledModelId

A character string with the coupled model ID. Any non-word character will be removed and the result will be converted to a valid name (see make.names).

coupledModelDescription

A character string with the coupled model description.

components

A list of sdModelClass, sdStaticModelClass and/or sdCoupledModelClass objects.

The models must have different ID's that will be used as unique identifiers, otherwise only the last model added with the same ID will be kept.

connections

A list of vectors that specifies the connections.

Each vector represents a connection, it must have 5 elements and be defined as:

c(ID, Component 1, Input 1, Component 2, Output 2).

Where ID: is the connection identification; Component 1: is the identification of the receiver component; Input 1: is the name of the input variable from the receiver component (component 1); Component 2: is the identification of the sender component; and Output 2: is the name of the connected state variable or, auxiliary or algebraic, equation with prefix st$, aux$ or eq$, respectively, indicating the output type from the sender component (Component 2), e.g. st$<varName>, aux$<eqName> or eq$<eqName>.

Details

To simulate a coupled model in different scenarios use the sdSimulate function.

To build the default coupled scenario use the sdBuildCoupledScenario function. Make sure to build it before running the simulation to save some computation time.

Value

A sdCoupledModelClass object.

Examples

 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# The Lotka-Volterra consumer-prey model implemented as a coupled model with
# the components being the population models of a prey and a consumer

# set the time sequence and use the default method
times <- list(from = 0, to = 200, by = 1)

# Prey model variables and ode function
stPrey <- list(P = 1)
parsPrey      <- list(rG = 1.0,      
                      K  = 10)
inpPrey <- list(IngestC = 0)
auxPrey <- list(GrowthP = "par$rG * st$P * (1 - st$P/par$K)")

LVodePrey <- function(t, st, ct, par, inp, sw, aux) 
{
  dP    <- aux$GrowthP - inp$IngestC
  
  return(list(c(dP)))
}

# create the component prey model
prey <- sdModel(modelId = "Prey",
                defaultScenario = sdScenario(scenarioId = "preyScen",
                                             times = times,
                                             state = stPrey,
                                             parameter = parsPrey,
                                             input = inpPrey),
                aux = auxPrey,
                DifferentialEquations = LVodePrey)

# Consumer model variables and ode function
stConsumer <- list(C = 2)
parsConsumer  <- list(rI = 0.2,
                      rM = 0.2 ,   
                      AE = 0.5) 
inpConsumer <- list(P = 0)
auxConsumer <- list(IngestC = "par$rI * inp$P * st$C",
                    "MortC <- par$rM * st$C")

LVodeConsumer <- function(t, st, ct, par, inp, sw, aux) 
{
  dC    <- aux$IngestC * par$AE - aux$MortC
  
  return(list(c(dC)))
}

# create the component consumer model
consumer <- sdModel(modelId = "Consumer",
                    defaultScenario = sdScenario(scenarioId = "consumerScen",
                                                 times = times,
                                                 state = stConsumer,
                                                 parameter = parsConsumer,
                                                 input = inpConsumer),
                    aux = auxConsumer,
                    DifferentialEquations = LVodeConsumer)

# create the coupled model connections list 
# conP: inform the consumer model about the amount of preys and 
# conIngestC: inform the prey model about the consumer ingestion
lvConnections <- list(c("conP", "Consumer", "P", "Prey", "st$P"),
                      c("conIngestC", "Prey", "IngestC", "Consumer", "aux$IngestC"))

# Create the Lotka-Volterra coupled model
coupledLV <- sdCoupledModel(coupledModelId = "LVCoupled",
                            components = c(prey, consumer),
                            connections = lvConnections,
                            "Lotka-Volterra Equations implemented as coupled model")
# build the coupled model and validate it
coupledLV$buildCoupledModel(from = 0, 
                            to = 200, 
                            by = 1)
coupledLV$verifyModel(verbose = TRUE)

# simulate the coupled model and plot the results
outclv <- sdSimulate(model = coupledLV)
outclv$plot("Prey.P Consumer.C", 
            main = "Coupled Prey and Consumer by Lotka-Volterra",
            multipleYAxis = TRUE)

EmbrapaInformaticaAgropecuaria/sdsim documentation built on May 10, 2019, 9:58 a.m.