Skip to content

Commit 6f8cfcc

Browse files
simplified interface for declaring parameters
1 parent 254d106 commit 6f8cfcc

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and parameters. Therefore we label them as follows:
2828
using ModelingToolkit
2929

3030
# Define some variables
31-
@parameters t() σ() ρ() β()
31+
@parameters t σ ρ β
3232
@variables x(t) y(t) z(t)
3333
@derivatives D'~t
3434
```
@@ -80,8 +80,8 @@ state of the previous ODE. This is the nonlinear system defined by where the
8080
derivatives are zero. We use (unknown) variables for our nonlinear system.
8181
8282
```julia
83-
@variables x() y() z()
84-
@parameters σ() ρ() β()
83+
@variables x y z
84+
@parameters σ ρ β
8585

8686
# Define a nonlinear system
8787
eqs = [0 ~ σ*(y-x),
@@ -166,12 +166,14 @@ including and excluding empty parentheses. When in call format, variables are
166166
aliased to the given call, allowing implicit use of dependents for convenience.
167167
168168
```julia
169-
@parameters t() α() σ
169+
@dependent_parameters t() α() σ
170170
@variables w x(t) y() z(t, α, x)
171171

172172
expr = x + y^α + σ(3) * (z - t) - w(t - 1)
173173
```
174174
175+
`@parameters` is a simplified tool for defining constant parameters, meaning
176+
`@parameters t` is the same as `@dependent_parameters t()`
175177
176178
### Constants
177179
@@ -274,7 +276,7 @@ is accessible via a function-based interface. This means that all macros are
274276
syntactic sugar in some form. For example, the variable construction:
275277
276278
```julia
277-
@parameters t() σ ρ() β()
279+
@parameters t σ ρ β
278280
@variables x(t) y(t) z(t)
279281
@derivatives D'~t
280282
```
@@ -297,8 +299,8 @@ D = Differential(t)
297299
The system building functions can handle intermediate calculations. For example,
298300
299301
```julia
300-
@variables x() y() z()
301-
@parameters σ() ρ() β()
302+
@variables x y z
303+
@parameters σ ρ β
302304
a = y - x
303305
eqs = [0 ~ σ*a,
304306
0 ~ x*-z)-y,

src/variables.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export Variable, @variables, @parameters
1+
export Variable, @variables, @parameters, @dependent_parameters
22

33

44
struct Variable <: Function
@@ -29,7 +29,7 @@ Base.convert(::Type{Expr}, c::Constant) = c.value
2929

3030

3131
# Build variables more easily
32-
function _parse_vars(macroname, known, x)
32+
function _parse_vars(macroname, known, x, add_call = true)
3333
ex = Expr(:block)
3434
var_names = Symbol[]
3535
# if parsing things in the form of
@@ -50,7 +50,11 @@ function _parse_vars(macroname, known, x)
5050
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)($(_var.args[2:end]...)))
5151
else
5252
var_name = _var
53-
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known))
53+
if add_call
54+
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)())
55+
else
56+
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known))
57+
end
5458
end
5559

5660
push!(var_names, var_name)
@@ -65,3 +69,6 @@ end
6569
macro parameters(xs...)
6670
esc(_parse_vars(:parameters, true, xs))
6771
end
72+
macro dependent_parameters(xs...)
73+
esc(_parse_vars(:parameters, true, xs, false))
74+
end

test/derivatives.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
# Derivatives
5-
@parameters t() σ() ρ() β()
5+
@parameters t σ ρ β
66
@variables x(t) y(t) z(t)
77
@derivatives D'~t D2''~t Dx'~x
88

test/simplify.jl

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

4-
@parameters t()
4+
@parameters t
55
@variables x(t) y(t) z(t)
66

77
null_op = 0*t

test/system_construction.jl

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

44
# Define some variables
5-
@parameters t() σ() ρ() β()
5+
@parameters t σ ρ β
66
@variables x(t) y(t) z(t)
77
@derivatives D'~t
88

@@ -60,7 +60,7 @@ fwt(FW, u, p, 0.2, 0.1)
6060
du [11, -3, -7]
6161
end
6262

63-
@parameters σ
63+
@dependent_parameters σ
6464
eqs = [D(x) ~ σ(t-1)*(y-x),
6565
D(y) ~ x*-z)-y,
6666
D(z) ~ x*y - β*z]
@@ -127,7 +127,7 @@ test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β))
127127
end
128128

129129
@derivatives D'~t
130-
@parameters A() B() C()
130+
@parameters A B C
131131
_x = y / C
132132
eqs = [D(x) ~ -A*x,
133133
D(y) ~ A*x - B*_x]
@@ -140,8 +140,8 @@ de = ODESystem(eqs)
140140
end
141141

142142
# Now nonlinear system with only variables
143-
@variables x() y() z()
144-
@parameters σ() ρ() β()
143+
@variables x y z
144+
@parameters σ ρ β
145145

146146
# Define a nonlinear system
147147
eqs = [0 ~ σ*(y-x),

test/variable_parsing.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ModelingToolkit
22
using Test
33

4-
@parameters t()
4+
@parameters t
55
@variables x(t) y(t) # test multi-arg
66
@variables z(t) # test single-arg
77
x1 = Variable(:x)(t)
@@ -12,10 +12,11 @@ z1 = Variable(:z)(t)
1212
@test isequal(z1, z)
1313

1414
@parameters begin
15-
t()
16-
s()
17-
σ
15+
t
16+
s
1817
end
18+
@dependent_parameters σ
19+
1920
t1 = Variable(:t; known = true)()
2021
s1 = Variable(:s; known = true)()
2122
σ1 = Variable(; known = true)

0 commit comments

Comments
 (0)