Skip to content

Commit e42a074

Browse files
Replace @unknown with @variable, remove Parameter
1 parent bd482b1 commit e42a074

File tree

7 files changed

+38
-39
lines changed

7 files changed

+38
-39
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ to manipulate.
2121
### Example: ODE
2222

2323
Let's build an ODE. First we define some variables. In a differential equation
24-
system, we need to differentiate between our unknown (dependent) variables
24+
system, we need to differentiate between our (dependent) variables
2525
and parameters. Therefore we label them as follows:
2626

2727
```julia
2828
using ModelingToolkit
2929

3030
# Define some variables
3131
@Param t σ ρ β
32-
@Unknown x(t) y(t) z(t)
32+
@Variable x(t) y(t) z(t)
3333
@Deriv D'~t
3434
```
3535

@@ -78,10 +78,10 @@ f = ODEFunction(de)
7878
7979
We can also build nonlinear systems. Let's say we wanted to solve for the steady
8080
state of the previous ODE. This is the nonlinear system defined by where the
81-
derivatives are zero. We use unknown variables for our nonlinear system.
81+
derivatives are zero. We use (unknown) variables for our nonlinear system.
8282
8383
```julia
84-
@Unknown x y z
84+
@Variable x y z
8585
@Param σ ρ β
8686

8787
# Define a nonlinear system
@@ -173,7 +173,7 @@ structure is as follows:
173173
the system of equations.
174174
- Name to subtype mappings: these describe how variable `subtype`s are mapped
175175
to the contexts of the system. For example, for a differential equation,
176-
the unknown variable corresponds to given subtypes and then the `eqs` can
176+
the variable corresponds to given subtypes and then the `eqs` can
177177
be analyzed knowing what the state variables are.
178178
- Variable names which do not fall into one of the system's core subtypes are
179179
treated as intermediates which can be used for holding subcalculations and
@@ -244,29 +244,29 @@ syntactic sugar in some form. For example, the variable construction:
244244
245245
```julia
246246
@Param t σ ρ β
247-
@Unknown x(t) y(t) z(t)
247+
@Variable x(t) y(t) z(t)
248248
@Deriv D'~t
249249
```
250250
251251
is syntactic sugar for:
252252
253253
```julia
254-
t = Parameter(:t)
255-
x = Unknown(:x, [t])
256-
y = Unknown(:y, [t])
257-
z = Unknown(:z, [t])
254+
t = Variable(:t; known = true)
255+
x = Variable(:x, [t])
256+
y = Variable(:y, [t])
257+
z = Variable(:z, [t])
258258
D = Differential(t)
259-
σ = Parameter()
260-
ρ = Parameter()
261-
β = Parameter()
259+
σ = Variable(; known = true)
260+
ρ = Variable(; known = true)
261+
β = Variable(; known = true)
262262
```
263263
264264
### Intermediate Calculations
265265
266266
The system building functions can handle intermediate calculations. For example,
267267
268268
```julia
269-
@Unknown x y z
269+
@Variable x y z
270270
@Param σ ρ β
271271
a = y - x
272272
eqs = [0 ~ σ*a,

src/systems/diffeqs/diffeqsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ end
7979
function generate_ode_iW(sys::DiffEqSystem, simplify=true; version::FunctionVersion = ArrayFunction)
8080
jac = calculate_jacobian(sys)
8181

82-
gam = Parameter(:gam)
82+
gam = Variable(:gam; known = true)
8383

8484
W = LinearAlgebra.I - gam*jac
8585
W = SMatrix{size(W,1),size(W,2)}(W)

src/variables.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
export Variable, Unknown, Parameter, @Unknown, @Param
1+
export Variable, @Variable, @Param
22

33

44
struct Variable <: Expression
55
name::Symbol
6-
known::Bool
76
dependents::Vector{Variable}
7+
known::Bool
8+
Variable(name, dependents = Variable[]; known = false) =
9+
new(name, dependents, known)
810
end
911

10-
Parameter(name, dependents = Variable[]) = Variable(name, true, dependents)
11-
Unknown(name, dependents = Variable[]) = Variable(name, false, dependents)
12-
1312

1413
struct Constant <: Expression
1514
value::Number
@@ -41,7 +40,7 @@ Base.convert(::Type{Expr}, c::Constant) = c.value
4140
Base.show(io::IO, x::Variable) = print(io, x.name)
4241

4342
# Build variables more easily
44-
function _parse_vars(macroname, fun, x)
43+
function _parse_vars(macroname, known, x)
4544
ex = Expr(:block)
4645
var_names = Symbol[]
4746
# if parsing things in the form of
@@ -65,15 +64,15 @@ function _parse_vars(macroname, fun, x)
6564
end
6665

6766
push!(var_names, var_name)
68-
expr = :($var_name = $fun($(Meta.quot(var_name)), $dependents))
67+
expr = :($var_name = $Variable($(Meta.quot(var_name)), $dependents; known = $known))
6968
push!(ex.args, expr)
7069
end
7170
push!(ex.args, build_expr(:tuple, var_names))
7271
return ex
7372
end
74-
macro Unknown(xs...)
75-
esc(_parse_vars(:Unknown, Unknown, xs))
73+
macro Variable(xs...)
74+
esc(_parse_vars(:Variable, false, xs))
7675
end
7776
macro Param(xs...)
78-
esc(_parse_vars(:Param, Parameter, xs))
77+
esc(_parse_vars(:Param, true, xs))
7978
end

test/derivatives.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using Test
33

44
# Derivatives
55
@Param t σ ρ β
6-
@Unknown x(t) y(t) z(t)
6+
@Variable x(t) y(t) z(t)
77
@Deriv D'~t D2''~t
88

99
@test isequal(expand_derivatives(D(t)), 1)
@@ -47,7 +47,7 @@ jac = calculate_jacobian(sys)
4747
@test isequal(jac[3,3], -1*β)
4848

4949
# Variable dependence checking in differentiation
50-
@Unknown a(t) b(a)
50+
@Variable a(t) b(a)
5151
@test !isequal(D(b), 0)
5252

5353
@test isequal(expand_derivatives(D(x * y)), simplify_constants(y*D(x) + x*D(y)))

test/simplify.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using ModelingToolkit
22
using Test
33

44
@Param t
5-
@Unknown x(t) y(t) z(t)
5+
@Variable x(t) y(t) z(t)
66

77
null_op = 0*t
88
@test isequal(simplify_constants(null_op), 0)

test/system_construction.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using Test
33

44
# Define some variables
55
@Param t σ ρ β
6-
@Unknown x(t) y(t) z(t)
6+
@Variable x(t) y(t) z(t)
77
@Deriv D'~t
88

99
# Define a differential equation
@@ -48,7 +48,7 @@ end
4848
# Conversion to first-order ODEs #17
4949
@Deriv D3'''~t
5050
@Deriv D2''~t
51-
@Unknown u(t) u_tt(t) u_t(t) x_t(t)
51+
@Variable u(t) u_tt(t) u_t(t) x_t(t)
5252
eqs = [D3(u) ~ 2(D2(u)) + D(u) + D(x) + 1
5353
D2(x) ~ D(x) + 2]
5454
de = DiffEqSystem(eqs, t)
@@ -100,7 +100,7 @@ test_vars_extraction(de, DiffEqSystem(eqs))
100100
end
101101

102102
# Now nonlinear system with only variables
103-
@Unknown x y z
103+
@Variable x y z
104104
@Param σ ρ β
105105

106106
# Define a nonlinear system

test/variable_parsing.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ using ModelingToolkit
22
using Test
33

44
@Param t
5-
@Unknown x(t)
6-
@Unknown y(t)
7-
@Unknown z(t)
8-
x1 = Unknown(:x, [t])
9-
y1 = Unknown(:y, [t])
10-
z1 = Unknown(:z, [t])
5+
@Variable x(t)
6+
@Variable y(t)
7+
@Variable z(t)
8+
x1 = Variable(:x, [t])
9+
y1 = Variable(:y, [t])
10+
z1 = Variable(:z, [t])
1111
@test isequal(x1, x)
1212
@test isequal(y1, y)
1313
@test isequal(z1, z)
@@ -19,8 +19,8 @@ z1 = Unknown(:z, [t])
1919
t
2020
s
2121
end
22-
t1 = Parameter(:t)
23-
s1 = Parameter(:s)
22+
t1 = Variable(:t; known = true)
23+
s1 = Variable(:s; known = true)
2424
@test isequal(t1, t)
2525
@test isequal(s1, s)
2626
@test convert(Expr, t) == :t

0 commit comments

Comments
 (0)