Title: Introduction to DEVS and DevsJava
1Introduction to DEVS and DevsJava
- Bernard P. Zeigler
- ACIMS
- Fall, 2002
2Basic Entities and Relations in Modeling and
Simulation
Experimental Frame
Source
Simulator
System
behavior database
Modeling Relation
Simulation Relation
Model
3Discrete Event Time Segments
X
t1
t0
t2
S
e
y0
Y
4DEVS Introduction Phrases
- faithfully yours
- step by step, brick by brick
- swimmers strokes
- how fast, how far, how soon
- dont interrupt
- together we can do it!
- the instant manager
- crossing the threshold, testing the limits
- when to say when (acceptor)
- let others have a chance (refractoriness)
5DEVS Hierarchical Modular Composition
- Atomic lowest level model, contains structural
dynamics -- model level modularity
Coupled composed of one or more atomic and/or
coupled models
hierarchical construction
coupling
6DEVS Atomic Model
Elements of an atomic model
- input events
- output events
- state variables
- state transition functions
- output function
- time advance function
7DEVS Atomic Model
Make a transition (internal)
Make a transition (external)
input
State
internal
external
time advance
Handle input
output
output
Hold for some time
Send an output
8Atomic Model Examples
pulse
Pulse Generator
time
interPulseTime gt0
Pulse Generator
Output
start
Output
Fire-once Neuron
Input
Firing delay gt0
external event
Internal event
output event
9Internal Transition /Output Generation
Time advance
10Response to External Input
elapsed time
Time advance
11DEVS Coupled Model
Elements of coupled model
- Components
- Interconnections
- Internal Couplings
- External Input Couplings
- External Output Couplings
12Coupling in Action
Coupling (internal)
AB
B
A
Output port
Input port
internal
State
external
time advance
output
internal
State
external
time advance
output
13Coupled Model Example
FireOnce Neuron 1
FireOnce Neuron 2
Pulse Generator
pulseOut
FireOnce Neuron 4
FireOnce Neuron 3
Neural net of Fire Once Neurons solves Shortest
Time Problem
14Class realDevs
- realDevs class allows developing models without
having to use advanced object oriented concepts
such as polymorphism and casting - its methods can be used to send and receive real
and string values - use it by inheritance, e.g.,
- public myModel extends realDevs
15Class realDevs Methods
public boolean somethingOnPort(message x,String
port is there a value on the given port, e.g.,
somethingOnPort(x,in) public double
getRealValueOnPort(message x,String port) get
the real (double) value on the given port, e.g.,
getRealValueOnPort(x,in) Note assumes that
the port only receives real values public double
sumValuesOnPort(message x,String port) sum up
all the real values on the given port, e.g.,
sumValuesOnPort(x,in) Note assumes that the
port only receives real values public double
getNameOnPort(message x,String port) get the
name (string) value on the given port, e.g.,
getStringOnPort(x,in) Note assumes that the
port only receives name values public message
outputNameOnPort(String nm,String port) output a
name (string) on the given port, e.g.,
outputNameOnPort(hello,out) public message
outputRealOnPort(double r,String port) output a
real value (double) on the given port, e.g.,
outputRealOnPort(5,out) public void
addNameTestInput(String port,String name,double
elapsed) add a test which inputs the given name
on the given port after an elapsed time, e.g.,
addNameTestInput(in,hello,5) public void
addRealTestInput(String port,double
value,double) add a test which inputs the given
name on the given port after an elapsed time, e,
e.g., addRealTestInput(in,10,5)
16Class realDevs Methods Relation to Full-up
Methods
public boolean somethingOnPort(message x,String
port) for (int i0 ilt x.getLength()i) if
(messageOnPort(x,port,i)) return true
else return false public double
getRealValueOnPort(message x,String port) for
(int i0 ilt x.getLength()i) if
(messageOnPort(x,port,i)) doubleEnt dv
(doubleEnt)x.getValOnPort(port,i) return
dv.getv() return 0 public double
sumValuesOnPort(message x,String port) double
val 0 for (int i0 ilt x.getLength()i)
if (messageOnPort(x,port,i)) doubleEnt dv
(doubleEnt)x.getValOnPort(port,i) val
dv.getv() return val
public message outputNameOnPort(String nm,String
port) message m new message() m.add(makeConten
t(port,new entity(nm))) return m public
message outputRealOnPort(double r,String
port) message m new message() m.add(makeConten
t(port,new doubleEnt(r))) return m public
void addNameTestInput(String port,String
name,double elapsed) addTestInput(port,new
entity(name),elapsed) public void
addNameTestInput(String port,String name)
addTestInput(port,new entity(name),0)
public void addPortTestInput(String port,double
elapsed) addTestInput(port,new
entity(),elapsed) public void
addRealTestInput(String port,double value,double
elapsed) addTestInput(port,new
doubleEnt(value),elapsed)
17Example of basicGenr Scuba Diving Plan
surface
15 ft
10 min
40 ft
20 min
60 ft
20 min
A diver drops immediately to 60 ft depth, stays
there for 20 min., then rises to 40 ft, depth
staying 20 min., and finally spends 10 minutes at
15ft before rising to the surface. Modify the
basicGenr to represent this dive plan. Your model
should output the new depth to which the diver
goes at each stage.
18Class pulseGenr generates pulses size and
frequency can be specified
out
interPulseTime
public void initialize() super.initialize() hold
In("active",interPulseTime) public void
deltext(double e,message x) Continue(e) if
(somethingOnPort(x,"start")) holdIn("active",int
erPulseTime) else if (somethingOnPort(x,"stop"))
passivate()
public void deltint() holdIn("active",interPuls
eTime) public message out() return
outputRealOnPort(pulse,"out")
Note stop and start do not remember the time
already elapsed in last period this can be done
by employing the elapsed time, e.
19Defining the rate or frequency of pulses (e.g.,
number/sec)
rate number of events/ time
interval N/T Note the rate can be
time- varying
rateEstimator Time
rate Estimator
divide
Time-Based Estimation wait until a certain time
has elapsed, measure the number of events that
occurred and divide the first by the second.
Event-based Estimation wait until a certain
number have arrived, measure the elapsed time,
and divide the first by the second
20Coupled Model using a basic generator to
dynamically determine the period of a pulse
generator
boxCar (first half)
ViewableAtomic basic new
basicGenr("basic",15,20,14, 2,1,1000)
\\ d1,d2,d3
,p1,p2,p3 add(basic) ViewableAtomic pg new
pulseGenr("pg") add(pg) addCoupling(basic,"out
",pg,"setInterPulseTime")
21Class sum accumulates the pulses it receives
public void deltext(double e,message
x) Continue(e) if (somethingOnPort(x,"in")) sta
te sumValuesOnPort(x,"in") holdIn("active
"state,0) else if (somethingOnPort(x,"reset
")) state 0 passivateIn("passive
"state) public void deltint()
passivateIn("passive "state) public void
deltcon(double e,message x) deltext(e,x) pu
blic message out() return outputRealOnPort(state,
"out")
22BoxCar continued
ViewableAtomic basic new
basicGenr("basic",15,20,14, 2,1,-.5)
\\ d1,d2,d3
,p1,p2,p3 add(basic) ViewableAtomic pg new
pulseGenr("pg") add(pg) addCoupling(basic,"out
",pg,"setInterPulseTime") ViewableAtomic sum
new sum("sum",0) add(sum) addCoupling(pg,"out",
sum,"in")
23Modeling swimming styles which is better
fast/short strokes versus slow/long ones?
time to swim 50 yards
24A Pair of DEVS Primitives for Differential
Equation Modeling
q
varGen
-q
0
-10
q
differencer
out
-q
25Differencer replicates the original pulses after
summing
in(x)lastValx
output
0 (time advance)
passive
out(x-lastVal)
public void initialize() lastVal
initLastVal super.initialize() public void
deltext(double e,message x) Continue(e) if
(somethingOnPort(x,"in")) double newVal
getRealValueOnPort(x,"in") outval newVal -
lastVal lastVal newVal holdIn("output",0)
lastVal
in(x)
input
lastVal
state
out(x - lastVal)
output
Note In the limit of small quantum size, the
output of the differencer approximates
differentiation and shows that differentiation
and integration are inverses of each other
26High school algebra Mary takes half the time
that Jill takes to paint a house how long does
it take for both working together?
ViewableAtomic MaryRate new varGen("MaryRate",20
0,quantum) //rate
4800/24 ViewableAtomic JillRate new
varGen("JillRate",100,quantum)
//rate 4800/48 //combined rate
is 300, so will take 4800/300 16
hours ViewableAtomic houseBricks new
sum("houseBricks",0)//state ViewableAtomic
MaryBricks new sum("MaryBricks",0) ViewableAtom
ic JillBricks new sum("JillBricks",0) add(MaryR
ate) add(JillRate) add(houseBricks) add(MaryBri
cks) add(JillBricks) addCoupling(MaryRate,"out",
MaryBricks,"in") addCoupling(JillRate,"out",JillB
ricks,"in") addCoupling(MaryRate,"out",houseBrick
s,"in") addCoupling(JillRate,"out",houseBricks,"i
n")
houseBricks
MaryBricks
JillBricks
27Quantizing a function into pulses approximates
its derivative
As the quantum size gets smaller, quantization
produces more frequent pulses of smaller size.
When fed to a summer, the sum gets closer to the
integral of the derivative, which is the original
function.
28A simple diff. eqn. model linear feadback
//linearGen public double rateFn() return
coefficientinput public void deltext(double
e,message x) if (somethingOnPort(x,"setInput"))
input getRealValueOnPort(x,"setInput")
rate rateFn() holdIn("active",Math.abs(signed
TimeAdvance())) //always use the new rate
immediately
//feedback coupled model ViewableAtomic fb
new linearGen("fb",-.05,5)//coefficient,quantum
gtnegative feedback //new linearGen("fb",.01,5)
//coefficient,quantum gtpositive
feedback add(fb) ViewableAtomic sum new
sum("sum",100)//initState add(sum) addCoupling(f
b,"out",sum,"in") addCoupling(sum,"out",fb,"setIn
put")
29class instantReal instantaneous (memoryless)
function response
in(x)
output
0 (time advance)
passive
public double fn(double x) return (x-1)/(xx
-1) //example
out(fn(x))
public void deltext(double e,message x) if
(somethingOnPort(x,"in")) inval
getRealValueOnPort(x,"in") outval
fn(inval) holdIn("output",0) else
passivate() public void deltint() passivate()
public message out() return
outputRealOnPort(outval,"out")
in(x)
input
state
out(fn(x))
output
30Experimental Frame Components
- An experimental frame specifies the conditions
under which a model or real system is
experimented or tested - Some useful components are
- threshold tester
- timer
- stopwatch
31class threshold tester tests any incoming real
value for crossing of threshold
thresholdTester Refract
in(x) fn(x) 0
output
output
public double fn(double x) if (x gt
threshold) return 0 else return 1
passive
0
refractPeriod
0 (time advance)
passive
out(fn(x))
refract
public void deltext(double e,message x) if
(somethingOnPort(x,"setThreshold")) threshold
getRealValueOnPort(x,"setThreshold") if
(somethingOnPort(x,"in")) inval
getRealValueOnPort(x,"in") outval
fn(inval) if (outval 0) holdIn("output",0)
//only output a 0 if threshold passed else
passivate()
in(x)
out(fn(x))
input
fn(x) ! 0
fn(x) 0
state
out(fn(x))
output
32class timer waits for a specified time then
outputs pulse
start(dur) sigma dur time 0
active
public void deltext(double e, message x)
Continue(e) time e if
(phaseIs("passive")) if (somethingOnPort(x,"
start")) double dur getRealValueOnPort(
x,"start") time 0
holdIn("active",dur) public void
deltint() time sigma
passivate() public message out()
if (phaseIs("active")) return
outputRealOnPort(timesigma,"out") else
return outputRealOnPort(0,"dum")
sigma
passive
out(dur)
start(dur)
in
input
dur
continue sigma sigma - e
sigma
state
e
out(dur)
output
33class stopwatch measures and reports elapsed
time
out(time)
start
active
public void deltext(double e, message x)
Continue(e) if (somethingOnPort(x,"time
?")) if (phaseIs("passive"))
response true //for simultaneous stop
reset else passivate() else if
(somethingOnPort(x,"start"))
passivateIn("active") else if
(somethingOnPort(x,"stop")) time e
passivate() else if (somethingOnPort(x,
"reset")) time 0 if (response)
holdIn("respond", 0) public message
out() if (phaseIs("respond"))
return outputRealOnPort(time,"timeIs") else
return outputNameOnPort("","dum")
stop
passive
time?
respond
start
stop
time?
input
t
actjve
respond
state
passive
e
out(t)
output
34How much nitrogen is needed to reduce methane to
1 ?
feedback instances
experimental frame
nitrogen used to flush
methaneEvac new feedback("methaneEvac",-los
sRate,100,quantum) add(methaneEvac) methaneEvac.
setBlackBox(true) nitrogenFill new
feedback("nitrogenFill", lossRate,0,quantum) add(
nitrogenFill) nitrogenFill.setBlackBox(true)
35Harmonic oscillator two
linear feadbacks with opposite signs
Note a small amount is siphoned of the sum at
each transition in order to prevent instability
36Bouncing Ball
demonstrates Newtons laws
conservation of momentum some loss is effected
by reducing v(t)
the loss shows up in the diminishing bounce
There are two pairs each consists of a variable
rate generator feeding a summer this is a
general approach to diff. eq. modelling
37Example Bunjee Jumping
gravity
velocity
airRes
spring
position
velocity