Skip to content

Commit 489f8ec

Browse files
Remove @var and :Variable subtype
Now represented as a zero-dependent DependentVariable.
1 parent c226f09 commit 489f8ec

File tree

8 files changed

+18
-36
lines changed

8 files changed

+18
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ derivatives are zero. We could use dependent variables for our nonlinear system
9292
variables. Here we will show the latter. We write:
9393

9494
```julia
95-
@Var x y z
95+
@DVar x y z
9696
@Param σ ρ β
9797

9898
# Define a nonlinear system
@@ -285,7 +285,7 @@ D = Differential(t) # Default of first derivative, Derivative(t,1)
285285
The system building functions can handle intermediate calculations. For example,
286286

287287
```julia
288-
@Var a x y z
288+
@DVar a x y z
289289
@Param σ ρ β
290290
eqs = [a ~ y-x,
291291
0 ~ σ*a,

src/systems/diffeqs/diffeqsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function generate_ode_iW(sys::DiffEqSystem, simplify=true)
101101
diff_exprs = filter(!isintermediate, sys.eqs)
102102
jac = sys.jac
103103

104-
gam = Variable(:gam)
104+
gam = DependentVariable(:gam)
105105

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

src/systems/nonlinear/nonlinear_system.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@ struct NonlinearSystem <: AbstractSystem
77
end
88

99
function NonlinearSystem(eqs, vs, ps;
10-
v_name = :Variable,
11-
dv_name = :DependentVariable,
10+
v_name = :DependentVariable,
1211
p_name = :Parameter)
13-
NonlinearSystem(eqs, vs, ps, [v_name,dv_name], p_name)
12+
NonlinearSystem(eqs, vs, ps, [v_name], p_name)
1413
end
1514

1615
function NonlinearSystem(eqs;
17-
v_name = :Variable,
18-
dv_name = :DependentVariable,
16+
v_name = :DependentVariable,
1917
p_name = :Parameter)
20-
# Allow the use of :DependentVariable to make it seamless with DE use
21-
targetmap = Dict(v_name => v_name, dv_name => v_name, p_name => p_name)
18+
targetmap = Dict(v_name => v_name, p_name => p_name)
2219
vs, ps = extract_elements(eqs, targetmap)
23-
NonlinearSystem(eqs, vs, ps, [v_name,dv_name], p_name)
20+
NonlinearSystem(eqs, vs, ps, [v_name], p_name)
2421
end
2522

2623
function generate_nlsys_function(sys::NonlinearSystem)

src/variables.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ mutable struct Variable <: Expression
55
dependents::Vector{Variable}
66
end
77

8-
Variable(name; subtype::Symbol=:Variable, dependents::Vector{Variable} = Variable[]) =
8+
Variable(name; subtype::Symbol, dependents::Vector{Variable} = Variable[]) =
99
Variable(name, subtype, nothing, dependents)
10-
Variable(name, args...; kwargs...) = Variable(name, args...; subtype=:Variable, kwargs...)
1110

1211
Parameter(name,args...;kwargs...) = Variable(name,args...;subtype=:Parameter,kwargs...)
1312
IndependentVariable(name,args...;kwargs...) = Variable(name,args...;subtype=:IndependentVariable,kwargs...)
1413
DependentVariable(name,args...;kwargs...) = Variable(name,args...;subtype=:DependentVariable,kwargs...)
1514

1615
export Variable,Parameter,Constant,DependentVariable,IndependentVariable,
17-
@Var, @Param, @Const, @DVar, @IVar
16+
@Param, @Const, @DVar, @IVar
1817

1918

2019
Base.copy(x::Variable) = Variable(x.name, x.subtype, x.diff, x.dependents)
@@ -68,7 +67,7 @@ function _parse_vars(macroname, fun, x)
6867
@assert iscall || issym "@$macroname expects a tuple of expressions!\nE.g. `@$macroname x y z`"
6968

7069
if iscall
71-
dependents = :([$(_var.args[2:end]...)])
70+
dependents = :(Variable[$(_var.args[2:end]...)])
7271
lhs = _var.args[1]
7372
else
7473
dependents = Variable[]
@@ -84,9 +83,7 @@ function _parse_vars(macroname, fun, x)
8483
return ex
8584
end
8685

87-
for funs in ((:DVar, :DependentVariable), (:IVar, :IndependentVariable),
88-
(:Var, :Variable),
89-
(:Param, :Parameter))
86+
for funs in [(:DVar, :DependentVariable), (:IVar, :IndependentVariable), (:Param, :Parameter)]
9087
@eval begin
9188
macro ($(funs[1]))(x...)
9289
esc(_parse_vars(String($funs[1]), $funs[2], x))

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
@IVar t
6-
@Var x(t) y(t) z(t)
6+
@DVar x(t) y(t) z(t)
77
@Param σ ρ β
88
@Deriv D'~t
99
dsin = D(sin(t))
@@ -43,5 +43,5 @@ jac = ModelingToolkit.calculate_jacobian(sys)
4343
@test jac[3,3] == -1*β
4444

4545
# Variable dependence checking in differentiation
46-
@Var a(t) b(a)
46+
@DVar a(t) b(a)
4747
@test D(b) Constant(0)

test/internal.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ using Test
44
# `Expr`, `Number` -> `Operation`
55
@IVar a
66
@Param b
7-
@DVar x(t)
8-
@Var y
7+
@DVar x(t) y()
98
@test convert(Expression, 2) == 2
109
expr = :(-inv(2sqrt(+($a, $b))))
1110
op = Operation(-, [Operation(inv,

test/system_construction.jl

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

44
# Define some variables
55
@IVar t
6-
@DVar x(t) y(t) z(t)
6+
@DVar x(t) y(t) z(t) a()
77
@Deriv D'~t # Default of first derivative, Derivative(t,1)
88
@Param σ ρ β
99
@Const c=0
10-
@Var a
1110

1211
# Define a differential equation
1312
eqs = [D(x) ~ σ*(y-x),
@@ -85,7 +84,7 @@ end
8584

8685
ModelingToolkit.generate_nlsys_function(ns)
8786

88-
@Var _x
87+
@DVar _x
8988
@Deriv D'~t
9089
@Param A B C
9190
eqs = [_x ~ y/C,
@@ -95,7 +94,7 @@ de = DiffEqSystem(eqs,[t],[x,y],Variable[_x],[A,B,C])
9594
@test eval(ModelingToolkit.generate_ode_function(de))([0.0,0.0],[1.0,2.0],[1,2,3],0.0) -1/3
9695

9796
# Now nonlinear system with only variables
98-
@Var x y z
97+
@DVar x y z
9998
@Param σ ρ β
10099

101100
# Define a nonlinear system

test/variable_parsing.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
using ModelingToolkit
22
using Test
33

4-
@Var a b
5-
a1 = Variable(:a)
6-
@test a1 == a
7-
@test convert(Expr, a) == :a
8-
9-
@Var begin
10-
a
11-
b
12-
end
13-
144
@IVar t
155
@DVar x(t)
166
@DVar y(t)

0 commit comments

Comments
 (0)