Skip to content

Commit a5a92e4

Browse files
Merge pull request #97 from JuliaDiffEq/hg/cleanup
General cleanup
2 parents 79b8772 + 2547a4e commit a5a92e4

17 files changed

+72
-175
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ context-aware single variable of the IR. Its fields are described as follows:
136136
- `name`: the name of the `Variable`. Note that this is not necessarily
137137
the same as the name of the Julia variable. But this symbol itself is considered
138138
the core identifier of the `Variable` in the sense of equality.
139-
- `subtype`: the main denotation of context. Variables within systems
140-
are grouped according to their `subtype`.
139+
- `known`: the main denotation of context, storing whether or not the value of
140+
the variable is known.
141141
- `dependents`: the vector of variables on which the current variable
142142
is dependent. For example, `u(t,x)` has dependents `[t,x]`. Derivatives thus
143143
require this information in order to simplify down.
@@ -252,9 +252,9 @@ is syntactic sugar for:
252252
253253
```julia
254254
t = Parameter(:t)
255-
x = Unknown(:x, dependents = [t])
256-
y = Unknown(:y, dependents = [t])
257-
z = Unknown(:z, dependents = [t])
255+
x = Unknown(:x, [t])
256+
y = Unknown(:y, [t])
257+
z = Unknown(:z, [t])
258258
D = Differential(t)
259259
σ = Parameter()
260260
ρ = Parameter()

src/ModelingToolkit.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,32 @@ using MacroTools
77
import MacroTools: splitdef, combinedef
88

99
abstract type Expression <: Number end
10-
abstract type AbstractOperation <: Expression end
11-
abstract type AbstractComponent <: Expression end
10+
abstract type AbstractSystem end
1211

13-
include("variables.jl")
14-
15-
Base.promote_rule(::Type{T},::Type{T2}) where {T<:Number,T2<:Expression} = Expression
12+
Base.promote_rule(::Type{<:Number},::Type{<:Expression}) = Expression
1613
Base.zero(::Type{<:Expression}) = Constant(0)
1714
Base.one(::Type{<:Expression}) = Constant(1)
18-
Base.convert(::Type{Variable},x::Int64) = Constant(x)
1915

20-
function caclulate_jacobian end
16+
function calculate_jacobian end
17+
function generate_jacobian end
18+
function generate_function end
2119

2220
@enum FunctionVersion ArrayFunction=1 SArrayFunction=2
2321

22+
include("variables.jl")
2423
include("operations.jl")
2524
include("differentials.jl")
2625
include("equations.jl")
27-
include("systems/systems.jl")
2826
include("systems/diffeqs/diffeqsystem.jl")
2927
include("systems/diffeqs/first_order_transform.jl")
3028
include("systems/nonlinear/nonlinear_system.jl")
3129
include("function_registration.jl")
3230
include("simplify.jl")
3331
include("utils.jl")
3432

35-
export Operation, Expression, AbstractComponent, AbstractDomain
33+
export Operation, Expression, AbstractComponent
34+
export calculate_jacobian, generate_jacobian, generate_function
35+
export ArrayFunction, SArrayFunction
3636
export @register
37+
3738
end # module

src/equations.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ struct Equation
55
lhs::Expression
66
rhs::Expression
77
end
8-
Base.broadcastable(eq::Equation) = Ref(eq)
98
Base.:(==)(a::Equation, b::Equation) = (a.lhs, a.rhs) == (b.lhs, b.rhs)
109

1110
Base.:~(lhs::Expression, rhs::Expression) = Equation(lhs, rhs)
1211
Base.:~(lhs::Expression, rhs::Number ) = Equation(lhs, rhs)
1312
Base.:~(lhs::Number , rhs::Expression) = Equation(lhs, rhs)
1413

1514

16-
_is_dependent(x::Variable) = x.subtype === :Unknown && !isempty(x.dependents)
17-
_is_parameter(iv) = x -> x.subtype === :Parameter && x iv
18-
_subtype(subtype::Symbol) = x -> x.subtype === subtype
15+
_is_dependent(x::Variable) = !x.known && !isempty(x.dependents)
16+
_is_parameter(iv) = x -> x.known && x iv
17+
_is_known(x::Variable) = x.known
18+
_is_unknown(x::Variable) = !x.known
1919

2020
function extract_elements(eqs, predicates)
2121
result = [Variable[] for p predicates]

src/operations.jl

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Parameterize by T so that way it can be Vector{Expression} which is defined after
2-
struct Operation <: AbstractOperation
1+
struct Operation <: Expression
32
op::Function
43
args::Vector{Expression}
54
end
@@ -19,22 +18,6 @@ Base.convert(::Type{Expr}, O::Operation) =
1918
build_expr(:call, Any[Symbol(O.op); convert.(Expr, O.args)])
2019
Base.show(io::IO, O::Operation) = print(io, convert(Expr, O))
2120

22-
23-
"""
24-
find_replace(O::Operation, x::Expression, y::Expression)
25-
26-
Finds the expression `x` in Operation `O` and replaces it with the Expression `y`
27-
"""
28-
function find_replace!(O::Operation, x::Expression, y::Expression)
29-
for i in eachindex(O.args)
30-
if isequal(O.args[i], x)
31-
O.args[i] = y
32-
elseif typeof(O.args[i]) <: Operation
33-
find_replace!(O.args[i],x,y)
34-
end
35-
end
36-
end
37-
3821
# For inv
3922
Base.convert(::Type{Operation}, x::Number) = Operation(identity, Expression[Constant(x)])
4023
Base.convert(::Type{Operation}, x::Operation) = x

src/systems/diffeqs/first_order_transform.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
extract_idv(eq::DiffEq) = eq.D.x
2-
31
function lower_varname(D::Differential, x; lower=false)
42
order = lower ? D.order-1 : D.order
53
return lower_varname(x, D.x, order)
64
end
75
function lower_varname(var::Variable, idv, order::Int)
86
sym = var.name
97
name = order == 0 ? sym : Symbol(sym, :_, string(idv.name)^order)
10-
return Variable(name, var.subtype, var.dependents)
8+
return Variable(name, var.known, var.dependents)
119
end
1210

1311
function ode_order_lowering(sys::DiffEqSystem)
@@ -21,7 +19,7 @@ function ode_order_lowering(eqs, iv)
2119
new_eqs = similar(eqs, DiffEq)
2220

2321
for (i, eq) enumerate(eqs)
24-
var, maxorder = extract_var_order(eq)
22+
var, maxorder = eq.var, eq.D.order
2523
maxorder == 1 && continue # fast pass
2624
if maxorder > get(var_order, var, 0)
2725
var_order[var] = maxorder
@@ -51,6 +49,4 @@ function rename(O::Expression)
5149
return Operation(O.op, rename.(O.args))
5250
end
5351

54-
extract_var_order(eq::DiffEq) = (eq.var, eq.D.order)
55-
5652
export ode_order_lowering

src/systems/nonlinear/nonlinear_system.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct NonlinearSystem <: AbstractSystem
1818
end
1919

2020
function NonlinearSystem(eqs)
21-
vs, ps = extract_elements(eqs, [_subtype(:Unknown), _subtype(:Parameter)])
21+
vs, ps = extract_elements(eqs, [_is_unknown, _is_known])
2222
NonlinearSystem(eqs, vs, ps)
2323
end
2424

src/systems/systems.jl

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/utils.jl

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function build_expr(head::Symbol, args)
1717
append!(ex.args, args)
1818
ex
1919
end
20-
expr_arr_to_block(exprs) = build_expr(:block, exprs)
2120

2221
# used in parsing
2322
isblock(x) = length(x) == 1 && x[1] isa Expr && x[1].head == :block
@@ -31,11 +30,30 @@ function flatten_expr!(x)
3130
x
3231
end
3332

34-
function partition(f, xs)
35-
idxs = map(f, xs)
36-
return (xs[idxs], xs[(!).(idxs)])
33+
function build_function(rhss, vs, ps, args = (); version::FunctionVersion)
34+
var_pairs = [(u.name, :(u[$i])) for (i, u) enumerate(vs)]
35+
param_pairs = [(p.name, :(p[$i])) for (i, p) enumerate(ps)]
36+
(ls, rs) = zip(var_pairs..., param_pairs...)
37+
38+
var_eqs = Expr(:(=), build_expr(:tuple, ls), build_expr(:tuple, rs))
39+
40+
if version === ArrayFunction
41+
X = gensym()
42+
sys_exprs = [:($X[$i] = $(convert(Expr, rhs))) for (i, rhs) enumerate(rhss)]
43+
let_expr = Expr(:let, var_eqs, build_expr(:block, sys_exprs))
44+
:(($X,u,p,$(args...)) -> $let_expr)
45+
elseif version === SArrayFunction
46+
sys_expr = build_expr(:tuple, [convert(Expr, rhs) for rhs rhss])
47+
let_expr = Expr(:let, var_eqs, sys_expr)
48+
:((u,p,$(args...)) -> begin
49+
X = $let_expr
50+
T = StaticArrays.similar_type(typeof(u), eltype(X))
51+
T(X)
52+
end)
53+
end
3754
end
3855

56+
3957
is_constant(::Constant) = true
4058
is_constant(::Any) = false
4159

src/variables.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ export Variable, Unknown, Parameter, @Unknown, @Param
33

44
struct Variable <: Expression
55
name::Symbol
6-
subtype::Symbol
6+
known::Bool
77
dependents::Vector{Variable}
88
end
99

10-
Parameter(name; dependents = Variable[]) = Variable(name, :Parameter, dependents)
11-
Unknown(name; dependents = Variable[]) = Variable(name, :Unknown, dependents)
10+
Parameter(name, dependents = Variable[]) = Variable(name, true, dependents)
11+
Unknown(name, dependents = Variable[]) = Variable(name, false, dependents)
1212

1313

1414
struct Constant <: Expression
@@ -22,7 +22,7 @@ Base.isone(ex::Expression) = isa(ex, Constant) && isone(ex.value)
2222

2323

2424
# Variables use isequal for equality since == is an Operation
25-
Base.:(==)(x::Variable, y::Variable) = (x.name, x.subtype) == (y.name, y.subtype)
25+
Base.:(==)(x::Variable, y::Variable) = (x.name, x.known) == (y.name, y.known)
2626
Base.:(==)(::Variable, ::Number) = false
2727
Base.:(==)(::Number, ::Variable) = false
2828
Base.:(==)(::Variable, ::Constant) = false
@@ -32,13 +32,18 @@ Base.:(==)(n::Number, c::Constant) = c.value == n
3232
Base.:(==)(a::Constant, b::Constant) = a.value == b.value
3333

3434
function Base.convert(::Type{Expr}, x::Variable)
35-
x.subtype === :Parameter || return x.name
36-
isempty(x.dependents) && return x.name
35+
x.known || return x.name
36+
isempty(x.dependents) && return x.name
3737
return :($(x.name)($(convert.(Expr, x.dependents)...)))
3838
end
3939
Base.convert(::Type{Expr}, c::Constant) = c.value
4040

41-
Base.show(io::IO, x::Variable) = print(io, x.subtype, '(', x.name, ')')
41+
function Base.show(io::IO, x::Variable)
42+
subtype = x.known ? :Parameter : :Unknown
43+
print(io, subtype, '(', repr(x.name))
44+
isempty(x.dependents) || print(io, ", ", x.dependents)
45+
print(io, ')')
46+
end
4247

4348
# Build variables more easily
4449
function _parse_vars(macroname, fun, x)
@@ -65,7 +70,7 @@ function _parse_vars(macroname, fun, x)
6570
end
6671

6772
push!(var_names, var_name)
68-
expr = :($var_name = $fun($(Meta.quot(var_name)), dependents = $dependents))
73+
expr = :($var_name = $fun($(Meta.quot(var_name)), $dependents))
6974
push!(ex.args, expr)
7075
end
7176
push!(ex.args, build_expr(:tuple, var_names))

test/ambiguity.jl

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)