diff --git a/README.md b/README.md index 258123d3a9..87bc964518 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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), @@ -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 @@ -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 ``` @@ -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, diff --git a/src/variables.jl b/src/variables.jl index c1f773dbcc..2b9dfeced7 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -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) diff --git a/test/derivatives.jl b/test/derivatives.jl index 1569420da6..194184fca1 100644 --- a/test/derivatives.jl +++ b/test/derivatives.jl @@ -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 diff --git a/test/simplify.jl b/test/simplify.jl index 0f723fa1ce..be6c1554df 100644 --- a/test/simplify.jl +++ b/test/simplify.jl @@ -1,7 +1,7 @@ using ModelingToolkit using Test -@parameters t() +@parameters t @variables x(t) y(t) z(t) null_op = 0*t diff --git a/test/system_construction.jl b/test/system_construction.jl index 2126a2b5b2..5b550393f4 100644 --- a/test/system_construction.jl +++ b/test/system_construction.jl @@ -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 @@ -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] @@ -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] @@ -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), diff --git a/test/variable_parsing.jl b/test/variable_parsing.jl index 2b51bbd68b..d406bdd5a8 100644 --- a/test/variable_parsing.jl +++ b/test/variable_parsing.jl @@ -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) @@ -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)