Skip to content

Commit 0453200

Browse files
committed
Merge pull request #11 from tshort/new-equation-notation
New equation notation
2 parents 07f3c38 + e58e8f5 commit 0453200

40 files changed

+1297
-1281
lines changed

README.md

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ MExpr(*(##1243,+(##1243,1)))
8686
In a simulation, the unknowns are to be solved based on a set of
8787
equations. Equations are built from device models.
8888
89-
A device model is a function that returns a list of equations or
89+
A device model is a function that returns a vector of equations or
9090
other devices that also return lists of equations. The equations
9191
each are assumed equal to zero. So,
9292
@@ -112,11 +112,11 @@ function Vanderpol()
112112
# The following gives the return value which is a list of equations.
113113
# Expressions with Unknowns are kept as expressions. Expressions of
114114
# regular variables are evaluated immediately.
115-
{
116-
# The -1.0 in der(x, -1.0) is the initial value for the derivative
117-
der(x, -1.0) - ((1 - y^2) * x - y) # == 0 is assumed
118-
der(y) - x
119-
}
115+
Equation[
116+
# The -1.0 in der(x, -1.0) is the initial value for the derivative
117+
der(x, -1.0) - ((1 - y^2) * x - y) # == 0 is assumed
118+
der(y) - x
119+
]
120120
end
121121

122122
y = sim(Vanderpol(), 10.0) # Run the simulation to 10 seconds and return
@@ -127,8 +127,26 @@ gplot(y)
127127
128128
Here are the results:
129129
130-
![plot results](https://github.com/tshort/Sims.jl/blob/master/examples/vanderpol.png?raw=true "Van Der Pol results")
130+
![plot results](https://github.com/tshort/Sims.jl/blob/master/examples/basics/vanderpol.png?raw=true "Van Der Pol results")
131131
132+
An `@equations` macro is provided to return `Equation[]` allowing for
133+
the use of equals in equations, so the example above can be:
134+
135+
``` julia
136+
function Vanderpol()
137+
y = Unknown(1.0, "y")
138+
x = Unknown("x")
139+
@equations begin
140+
der(x, -1.0) = (1 - y^2) * x - y
141+
der(y) = x
142+
end
143+
end
144+
145+
y = sim(Vanderpol(), 10.0) # Run the simulation to 10 seconds and return
146+
# the result as an array.
147+
# plot the results with Gaston
148+
gplot(y)
149+
```
132150
133151
Electrical example
134152
------------------
@@ -159,19 +177,19 @@ voltage.
159177
function Resistor(n1, n2, R::Real)
160178
i = Current() # This is simply an Unknown.
161179
v = Voltage()
162-
{
163-
Branch(n1, n2, v, i)
164-
R * i - v # == 0 is implied
165-
}
180+
@equations begin
181+
Branch(n1, n2, v, i)
182+
R * i = v
183+
end
166184
end
167185

168186
function Capacitor(n1, n2, C::Real)
169187
i = Current()
170188
v = Voltage()
171-
{
172-
Branch(n1, n2, v, i)
173-
C * der(v) - i
174-
}
189+
@equations begin
190+
Branch(n1, n2, v, i)
191+
C * der(v) = i
192+
end
175193
end
176194
```
177195
@@ -188,13 +206,13 @@ function Circuit()
188206
n2 = Voltage("Output voltage")
189207
n3 = Voltage()
190208
g = 0.0 # A ground has zero volts; it's not an unknown.
191-
{
192-
SineVoltage(n1, g, 10.0, 60.0)
193-
Resistor(n1, n2, 10.0)
194-
Resistor(n2, g, 5.0)
195-
SeriesProbe(n2, n3, "Capacitor current")
196-
Capacitor(n3, g, 5.0e-3)
197-
}
209+
Equation[
210+
SineVoltage(n1, g, 10.0, 60.0)
211+
Resistor(n1, n2, 10.0)
212+
Resistor(n2, g, 5.0)
213+
SeriesProbe(n2, n3, "Capacitor current")
214+
Capacitor(n3, g, 5.0e-3)
215+
]
198216
end
199217

200218
ckt = Circuit()
@@ -203,7 +221,7 @@ gplot(ckt_y)
203221
```
204222
Here are the results:
205223
206-
![plot results](https://github.com/tshort/Sims.jl/blob/master/examples/circuit.png?raw=true "Circuit results")
224+
![plot results](https://github.com/tshort/Sims.jl/blob/master/examples/basics/circuit.png?raw=true "Circuit results")
207225
208226
Initialization and Solving Sets of Equations
209227
--------------------------------------------
@@ -216,10 +234,10 @@ equations:
216234
```julia
217235
function test()
218236
@unknown x y
219-
{
220-
2*x - y - exp(-x)
221-
-x + 2*y - exp(-y)
222-
}
237+
@equations begin
238+
2*x - y = exp(-x)
239+
-x + 2*y = exp(-y)
240+
end
223241
end
224242

225243
solution = solve(create_sim(test()))
@@ -231,7 +249,7 @@ Hybrid Modeling and Structural Variability
231249
Sims supports basic hybrid modeling, including the ability to handle
232250
structural model changes. Consider the following example:
233251
234-
[Breaking pendulum](https://github.com/tshort/Sims.jl/blob/master/examples/breaking_pendulum_in_box.jl)
252+
[Breaking pendulum](https://github.com/tshort/Sims.jl/blob/master/examples/basics/breaking_pendulum_in_box.jl)
235253
236254
This model starts as a pendulum, then the wire breaks, and the ball
237255
goes into free fall. Sims handles this much like
@@ -245,7 +263,7 @@ adjusted for the "bounce".
245263
Here is an animation of the results. Note that the actual animation
246264
was done in R, not Julia.
247265
248-
![plot results](https://github.com/tshort/Sims.jl/blob/master/examples/pendulum.gif?raw=true "Pendulum")
266+
![plot results](https://github.com/tshort/Sims.jl/blob/master/examples/basics/pendulum.gif?raw=true "Pendulum")
249267
250268
To Look Deeper
251269
--------------

doc/Possible-future-developments.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ leading "tag".
9292
function Resistor(n1::ElectricalNode, n2::ElectricalNode, R::Signal)
9393
i = Current(compatible_values(n1, n2))
9494
v = Voltage(value(n1) - value(n2))
95-
{
96-
Branch(n1, n2, v, i)
97-
R .* i - v # == 0 is implied
98-
}
95+
Equation[
96+
Branch(n1, n2, v, i)
97+
R .* i - v # == 0 is implied
98+
]
9999
end
100100

101101
# help info - returns a string in Markdown format

doc/README.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ function Vanderpol()
8888
# The following gives the return value which is a list of equations.
8989
# Expressions with Unknowns are kept as expressions. Regular
9090
# variables are evaluated immediately (like normal).
91-
{
92-
der(x, -1.0) - ((1 - y^2) * x - y) # == 0 is assumed
93-
der(y) - x
94-
}
91+
Equation[
92+
der(x, -1.0) - ((1 - y^2) * x - y) # == 0 is assumed
93+
der(y) - x
94+
]
9595
end
9696
```
9797

@@ -108,10 +108,10 @@ unknowns and equations as shown below:
108108
function Capacitor(n1, n2, C::Real)
109109
i = Current() # Unknown #1
110110
v = Voltage() # Unknown #2
111-
{
112-
Branch(n1, n2, v, i) # Equation #1 - this returns n1 - n2 - v
113-
C * der(v) - i # Equation #2
114-
}
111+
Equation[
112+
Branch(n1, n2, v, i) # Equation #1 - this returns n1 - n2 - v
113+
C * der(v) - i # Equation #2
114+
]
115115
end
116116
```
117117

@@ -127,13 +127,13 @@ function Circuit()
127127
n2 = ElectricalNode("Output voltage")
128128
n3 = ElectricalNode()
129129
g = 0.0 # a ground has zero volts; it's not an Unknown.
130-
{
131-
VSource(n1, g, 10.0, 60.0)
132-
Resistor(n1, n2, 10.0)
133-
Resistor(n2, g, 5.0)
134-
SeriesProbe(n2, n3, "Capacitor current")
135-
Capacitor(n3, g, 5.0e-3)
136-
}
130+
Equation[
131+
VSource(n1, g, 10.0, 60.0)
132+
Resistor(n1, n2, 10.0)
133+
Resistor(n2, g, 5.0)
134+
SeriesProbe(n2, n3, "Capacitor current")
135+
Capacitor(n3, g, 5.0e-3)
136+
]
137137
end
138138
```
139139

@@ -212,13 +212,13 @@ function VSquare(n1, n2, V::Real, f::Real)
212212
i = Current()
213213
v = Voltage()
214214
v_mag = Discrete(V)
215-
{
216-
Branch(n1, n2, v, i)
217-
v - v_mag
218-
Event(sin(2 * pi * f * MTime),
219-
{reinit(v_mag, V)}, # positive crossing
220-
{reinit(v_mag, -V)}) # negative crossing
221-
}
215+
Equation[
216+
Branch(n1, n2, v, i)
217+
v - v_mag
218+
Event(sin(2 * pi * f * MTime),
219+
Equation[reinit(v_mag, V)], # positive crossing
220+
Equation[reinit(v_mag, -V)]) # negative crossing
221+
]
222222
end
223223
```
224224

@@ -238,12 +238,12 @@ function IdealDiode(n1, n2)
238238
v = Voltage()
239239
s = Unknown() # dummy variable
240240
openswitch = Discrete(false) # on/off state of diode
241-
{
242-
Branch(n1, n2, v, i)
243-
BoolEvent(openswitch, -s) # openswitch becomes true when s goes negative
244-
v - ifelse(openswitch, s, 0.0)
245-
i - ifelse(openswitch, 0.0, s)
246-
}
241+
Equation[
242+
Branch(n1, n2, v, i)
243+
BoolEvent(openswitch, -s) # openswitch becomes true when s goes negative
244+
v - ifelse(openswitch, s, 0.0)
245+
i - ifelse(openswitch, 0.0, s)
246+
]
247247
end
248248
```
249249

@@ -274,11 +274,11 @@ function BreakingPendulum()
274274
y = Unknown(-cos(pi/4), "y")
275275
vx = Unknown()
276276
vy = Unknown()
277-
{
278-
StructuralEvent(MTime - 5.0, # when time hits 5 sec, switch to FreeFall
279-
Pendulum(x,y,vx,vy),
280-
() -> FreeFall(x,y,vx,vy))
281-
}
277+
Equation[
278+
StructuralEvent(MTime - 5.0, # when time hits 5 sec, switch to FreeFall
279+
Pendulum(x,y,vx,vy),
280+
() -> FreeFall(x,y,vx,vy))
281+
]
282282
end
283283
```
284284

examples/basics/breaking_pendulum.jl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@ using Sims
66
########################################
77

88
function FreeFall(x,y,vx,vy)
9-
{
10-
vx - der(x)
11-
vy - der(y)
12-
der(vx)
13-
der(vy) + 9.81
14-
}
9+
@equations begin
10+
der(x) = vx
11+
der(y) = vy
12+
der(vx) = 0.0
13+
der(vy) = -9.81
14+
end
1515
end
1616

1717
function Pendulum(x,y,vx,vy)
1818
len = sqrt(x.value^2 + y.value^2)
1919
phi0 = atan2(x.value, -y.value)
2020
phi = Unknown(phi0)
2121
phid = Unknown()
22-
{
23-
phid - der(phi)
24-
vx - der(x)
25-
vy - der(y)
26-
x - len * sin(phi)
27-
y + len * cos(phi)
28-
der(phid) + 9.81 / len * sin(phi)
29-
}
22+
@equations begin
23+
der(phi) = phid
24+
der(x) = vx
25+
der(y) = vy
26+
x = len * sin(phi)
27+
y = -len * cos(phi)
28+
der(phid) = -9.81 / len * sin(phi)
29+
end
3030
end
3131

3232
function BreakingPendulum()
3333
x = Unknown(cos(pi/4), "x")
3434
y = Unknown(-cos(pi/4), "y")
3535
vx = Unknown()
3636
vy = Unknown()
37-
{
38-
StructuralEvent(MTime - 5.0, # when time hits 5 sec, switch to FreeFall
39-
Pendulum(x,y,vx,vy),
40-
() -> FreeFall(x,y,vx,vy))
41-
}
37+
Equation[
38+
StructuralEvent(MTime - 5.0, # when time hits 5 sec, switch to FreeFall
39+
Pendulum(x,y,vx,vy),
40+
() -> FreeFall(x,y,vx,vy))
41+
]
4242
end
4343

4444
p = BreakingPendulum()

0 commit comments

Comments
 (0)