Skip to content

Commit f2fd6f9

Browse files
Remove value field from Variable
1 parent 8bd3c34 commit f2fd6f9

File tree

7 files changed

+39
-56
lines changed

7 files changed

+39
-56
lines changed

src/differentials.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function (D::Differential)(x::Variable)
1616
end
1717
Base.:(==)(D1::Differential, D2::Differential) = D1.order == D2.order && D1.x == D2.x
1818

19-
Variable(x::Variable, D::Differential) = Variable(x.name,x.value,x.value_type,
19+
Variable(x::Variable, D::Differential) = Variable(x.name,x.value_type,
2020
x.subtype,D,x.dependents,x.description,x.flow,x.domain,
2121
x.size,x.context)
2222

src/systems/diffeqs/first_order_transform.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function lower_varname(var::Variable, naming_scheme; lower=false)
55
lower_varname(var.name, D.x, order, var.subtype, naming_scheme)
66
end
77
function lower_varname(sym::Symbol, idv, order::Int, subtype::Symbol, naming_scheme)
8-
order == 0 && return Variable(sym, subtype)
8+
order == 0 && return Variable(sym, subtype=subtype)
99
name = Symbol(String(sym)*naming_scheme*String(idv.name)^order)
1010
Variable(name, subtype=subtype)
1111
end

src/variables.jl

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mutable struct Variable <: Expression
22
name::Symbol
3-
value
43
value_type::DataType
54
subtype::Symbol
65
diff::Union{Function,Nothing} # FIXME
@@ -13,20 +12,19 @@ mutable struct Variable <: Expression
1312
end
1413

1514
Variable(name,
16-
value = nothing,
17-
value_type = typeof(value);
15+
value_type = Any;
1816
subtype::Symbol=:Variable,
1917
dependents::Vector{Variable} = Variable[],
2018
flow::Bool = false,
2119
description::String = "",
2220
domain = Reals(),
2321
size = nothing,
2422
context = nothing) =
25-
Variable(name,value,value_type,subtype,nothing,
23+
Variable(name,value_type,subtype,nothing,
2624
dependents,description,flow,domain,size,context)
2725
Variable(name,args...;kwargs...) = Variable(name,args...;subtype=:Variable,kwargs...)
2826

29-
Variable(name,x::Variable) = Variable(name,x.value,x.value_type,
27+
Variable(name,x::Variable) = Variable(name,x.value_type,
3028
x.subtype,D,x.dependents,x.description,x.flow,x.domain,
3129
x.size,x.context)
3230

@@ -73,8 +71,8 @@ Base.isone(ex::Expression) = isa(ex, Constant) && isone(ex.value)
7371

7472

7573
# Variables use isequal for equality since == is an Operation
76-
function Base.:(==)(x::Variable,y::Variable)
77-
x.name == y.name && x.subtype == y.subtype && x.value == y.value &&
74+
function Base.:(==)(x::Variable, y::Variable)
75+
x.name == y.name && x.subtype == y.subtype &&
7876
x.value_type == y.value_type && x.diff == y.diff
7977
end
8078
Base.:(==)(::Variable, ::Number) = false
@@ -142,45 +140,29 @@ function _parse_vars(macroname, fun, x)
142140
# begin
143141
# x
144142
# y
145-
# z = exp(2)
143+
# z
146144
# end
147145
x = flatten_expr!(x)
148146
for _var in x
149147
iscall = typeof(_var) <: Expr && _var.head == :call
150148
issym = _var isa Symbol
151-
isassign = issym ? false : _var.head == :(=)
152-
@assert iscall || issym || isassign "@$macroname expects a tuple of expressions!\nE.g. `@$macroname x y z=1`"
153-
if iscall || issym
154-
if iscall
155-
dependents = :([$(_var.args[2:end]...)])
156-
var = _var.args[1]
157-
else
158-
dependents = Variable[]
159-
var = _var
160-
end
161-
lhs = var
162-
push!(lhss, lhs)
163-
expr = :( $lhs = $fun( Symbol($(String(lhs))) ,
164-
dependents = $dependents))
165-
end
166-
if isassign
167-
iscall = typeof(_var.args[1]) <: Expr && _var.args[1].head == :call
168-
if iscall
169-
dependents = :([$(_var.args[1].args[2:end]...)])
170-
lhs = _var.args[1].args[1]
171-
else
172-
dependents = Variable[]
173-
lhs = _var.args[1]
174-
end
175-
rhs = _var.args[2]
176-
push!(lhss, lhs)
177-
expr = :( $lhs = $fun( Symbol($(String(lhs))) , $rhs,
178-
dependents = $dependents))
149+
@assert iscall || issym "@$macroname expects a tuple of expressions!\nE.g. `@$macroname x y z`"
150+
151+
if iscall
152+
dependents = :([$(_var.args[2:end]...)])
153+
lhs = _var.args[1]
154+
else
155+
dependents = Variable[]
156+
lhs = _var
179157
end
158+
159+
push!(lhss, lhs)
160+
expr = :( $lhs = $fun( Symbol($(String(lhs))) ,
161+
dependents = $dependents))
180162
push!(ex.args, expr)
181163
end
182-
push!(ex.args, Expr(:tuple, lhss...))
183-
ex
164+
push!(ex.args, build_expr(:tuple, lhss))
165+
return ex
184166
end
185167

186168
for funs in ((:DVar, :DependentVariable), (:IVar, :IndependentVariable),

test/basic_variables_and_operations.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ using Test
1010
@Const c=0
1111

1212
# Default values
13-
p = Parameter(:p, 1)
14-
u = DependentVariable(:u, [1], dependents = [t])
13+
p = Parameter(:p)
14+
u = DependentVariable(:u, dependents = [t])
1515

16-
s = JumpVariable(:s,3,dependents=[t])
17-
n = NoiseVariable(:n,dependents=[t])
16+
s = JumpVariable(:s, dependents=[t])
17+
n = NoiseVariable(:n, dependents=[t])
1818

1919
σ*(y-x)
2020
D(x)

test/derivatives.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ expand_derivatives(dsin)
1111

1212
@test expand_derivatives(dsin) == cos(t)
1313
dcsch = D(csch(t))
14-
@test expand_derivatives(dcsch) == simplify_constants(Operation(coth(t)*csch(t)*-1))
14+
@test expand_derivatives(dcsch) == simplify_constants(coth(t) * csch(t) * -1)
1515

1616
# Chain rule
1717
dsinsin = D(sin(sin(t)))

test/system_construction.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ function test_eqs(eqs1, eqs2)
5050
eq = true
5151
for i in eachindex(eqs1)
5252
lhs1, lhs2 = eqs1[i].args[1], eqs2[i].args[1]
53+
typeof(lhs1) === typeof(lhs2) || return false
5354
for f in fieldnames(typeof(lhs1))
5455
eq = eq && isequal(getfield(lhs1, f), getfield(lhs2, f))
5556
end
5657
eq = eq && isequal(eqs1[i].args[2], eqs2[i].args[2])
5758
end
58-
@test_broken eq
59+
eq
5960
end
60-
test_eqs(de1.eqs, lowered_eqs)
61+
@test_broken test_eqs(de1.eqs, lowered_eqs)
6162

6263
# Internal calculations
6364
eqs = [a ~ y-x,

test/variable_parsing.jl

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

4-
@Var a=1.0 b
5-
a1 = Variable(:a,1.0)
4+
@Var a b
5+
a1 = Variable(:a)
66
@test a1 == a
77
@test convert(Expr, a) == :a
88

99
@Var begin
10-
a = 1.0
10+
a
1111
b
1212
end
1313

1414
@IVar t
1515
@DVar x(t)
16-
@DVar y(t)=sin(1)+exp(1)
16+
@DVar y(t)
1717
@DVar z(t)
18-
x1 = DependentVariable(:x,dependents = [t])
19-
y1 = DependentVariable(:y, sin(1) + exp(1),dependents = [t])
20-
z1 = DependentVariable(:z,dependents = [t])
18+
x1 = DependentVariable(:x ,dependents = [t])
19+
y1 = DependentVariable(:y ,dependents = [t])
20+
z1 = DependentVariable(:z ,dependents = [t])
2121
@test x1 == x
2222
@test y1 == y
2323
@test z1 == z
@@ -27,10 +27,10 @@ z1 = DependentVariable(:z,dependents = [t])
2727

2828
@IVar begin
2929
t
30-
s = cos(2.5)
30+
s
3131
end
3232
t1 = IndependentVariable(:t)
33-
s1 = IndependentVariable(:s, cos(2.5))
33+
s1 = IndependentVariable(:s)
3434
@test t1 == t
3535
@test s1 == s
3636
@test convert(Expr, t) == :t

0 commit comments

Comments
 (0)