Skip to content

Commit

Permalink
Merge pull request #123 from JuliaDiffEq/parameter
Browse files Browse the repository at this point in the history
simplified interface for declaring parameters
  • Loading branch information
ChrisRackauckas authored Apr 13, 2019
2 parents 254d106 + 0a1b3c2 commit 42a0904
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ and parameters. Therefore we label them as follows:
using ModelingToolkit

# Define some variables
@parameters t() σ() ρ() β()
@parameters t σ ρ β
@variables x(t) y(t) z(t)
@derivatives D'~t
```
Expand Down Expand Up @@ -80,8 +80,8 @@ state of the previous ODE. This is the nonlinear system defined by where the
derivatives are zero. We use (unknown) variables for our nonlinear system.
```julia
@variables x() y() z()
@parameters σ() ρ() β()
@variables x y z
@parameters σ ρ β

# Define a nonlinear system
eqs = [0 ~ σ*(y-x),
Expand Down Expand Up @@ -166,12 +166,16 @@ including and excluding empty parentheses. When in call format, variables are
aliased to the given call, allowing implicit use of dependents for convenience.
```julia
@parameters t() α() σ
@variables w x(t) y() z(t, α, x)
@parameters t α σ(..)
@variables w(..) x(t) y() z(t, α, x)

expr = x + y^α + σ(3) * (z - t) - w(t - 1)
```
Note that `@parameters` and `@variables` implicitly add `()` to values that
are not given a call. The former specifies the values as known, while the
latter specifies it as unknown. `(..)` signifies that the value should be
left uncalled.
### Constants
Expand Down Expand Up @@ -274,7 +278,7 @@ is accessible via a function-based interface. This means that all macros are
syntactic sugar in some form. For example, the variable construction:
```julia
@parameters t() σ ρ() β()
@parameters t σ ρ β
@variables x(t) y(t) z(t)
@derivatives D'~t
```
Expand All @@ -297,8 +301,8 @@ D = Differential(t)
The system building functions can handle intermediate calculations. For example,
```julia
@variables x() y() z()
@parameters σ() ρ() β()
@variables x y z
@parameters σ ρ β
a = y - x
eqs = [0 ~ σ*a,
0 ~ x*-z)-y,
Expand Down
9 changes: 7 additions & 2 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ function _parse_vars(macroname, known, x)

if iscall
var_name = _var.args[1]
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)($(_var.args[2:end]...)))
if _var.args[end] == :..
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known))
else
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)($(_var.args[2:end]...)))
end
else
# Implicit 0-args call
var_name = _var
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known))
expr = :($var_name = $Variable($(Meta.quot(var_name)); known = $known)())
end

push!(var_names, var_name)
Expand Down
2 changes: 1 addition & 1 deletion test/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using ModelingToolkit
using Test

# Derivatives
@parameters t() σ() ρ() β()
@parameters t σ ρ β
@variables x(t) y(t) z(t)
@derivatives D'~t D2''~t Dx'~x

Expand Down
2 changes: 1 addition & 1 deletion test/simplify.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ModelingToolkit
using Test

@parameters t()
@parameters t
@variables x(t) y(t) z(t)

null_op = 0*t
Expand Down
10 changes: 5 additions & 5 deletions test/system_construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using ModelingToolkit
using Test

# Define some variables
@parameters t() σ() ρ() β()
@parameters t σ ρ β
@variables x(t) y(t) z(t)
@derivatives D'~t

Expand Down Expand Up @@ -60,7 +60,7 @@ fwt(FW, u, p, 0.2, 0.1)
du [11, -3, -7]
end

@parameters σ
@parameters σ(..)
eqs = [D(x) ~ σ(t-1)*(y-x),
D(y) ~ x*-z)-y,
D(z) ~ x*y - β*z]
Expand Down Expand Up @@ -127,7 +127,7 @@ test_nlsys_inference("standard", ns, (x, y, z), (σ, ρ, β))
end

@derivatives D'~t
@parameters A() B() C()
@parameters A B C
_x = y / C
eqs = [D(x) ~ -A*x,
D(y) ~ A*x - B*_x]
Expand All @@ -140,8 +140,8 @@ de = ODESystem(eqs)
end

# Now nonlinear system with only variables
@variables x() y() z()
@parameters σ() ρ() β()
@variables x y z
@parameters σ ρ β

# Define a nonlinear system
eqs = [0 ~ σ*(y-x),
Expand Down
9 changes: 5 additions & 4 deletions test/variable_parsing.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ModelingToolkit
using Test

@parameters t()
@parameters t
@variables x(t) y(t) # test multi-arg
@variables z(t) # test single-arg
x1 = Variable(:x)(t)
Expand All @@ -12,10 +12,11 @@ z1 = Variable(:z)(t)
@test isequal(z1, z)

@parameters begin
t()
s()
σ
t
s
end
@parameters σ(..)

t1 = Variable(:t; known = true)()
s1 = Variable(:s; known = true)()
σ1 = Variable(; known = true)
Expand Down

0 comments on commit 42a0904

Please sign in to comment.