Skip to content

Commit 91822bc

Browse files
refactor: remove varmap_to_vars and related outdated infrastructure
1 parent dd71755 commit 91822bc

File tree

3 files changed

+0
-142
lines changed

3 files changed

+0
-142
lines changed

src/variables.jl

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -149,133 +149,6 @@ function default_toterm(x)
149149
end
150150
end
151151

152-
"""
153-
$(SIGNATURES)
154-
155-
Takes a list of pairs of `variables=>values` and an ordered list of variables
156-
and creates the array of values in the correct order with default values when
157-
applicable.
158-
"""
159-
function varmap_to_vars(varmap, varlist; defaults = Dict(), check = true,
160-
toterm = default_toterm, promotetoconcrete = nothing,
161-
tofloat = true, use_union = true)
162-
varlist = collect(map(unwrap, varlist))
163-
164-
# Edge cases where one of the arguments is effectively empty.
165-
is_incomplete_initialization = varmap isa DiffEqBase.NullParameters ||
166-
varmap === nothing
167-
if is_incomplete_initialization || isempty(varmap)
168-
if isempty(defaults)
169-
if !is_incomplete_initialization && check
170-
isempty(varlist) || throw(MissingVariablesError(varlist))
171-
end
172-
return nothing
173-
else
174-
varmap = Dict()
175-
end
176-
end
177-
178-
# We respect the input type if it's a static array
179-
# otherwise canonicalize to a normal array
180-
# container_type = T <: Union{Dict,Tuple} ? Array : T
181-
if varmap isa StaticArray
182-
container_type = typeof(varmap)
183-
else
184-
container_type = Array
185-
end
186-
187-
vals = if eltype(varmap) <: Pair # `varmap` is a dict or an array of pairs
188-
varmap = todict(varmap)
189-
_varmap_to_vars(varmap, varlist; defaults, check, toterm)
190-
else # plain array-like initialization
191-
varmap
192-
end
193-
194-
promotetoconcrete === nothing && (promotetoconcrete = container_type <: AbstractArray)
195-
if promotetoconcrete
196-
vals = promote_to_concrete(vals; tofloat, use_union)
197-
end
198-
199-
if isempty(vals)
200-
return nothing
201-
elseif container_type <: Tuple
202-
(vals...,)
203-
else
204-
SymbolicUtils.Code.create_array(container_type, eltype(vals), Val{1}(),
205-
Val(length(vals)), vals...)
206-
end
207-
end
208-
209-
const MISSING_VARIABLES_MESSAGE = """
210-
Initial condition underdefined. Some are missing from the variable map.
211-
Please provide a default (`u0`), initialization equation, or guess
212-
for the following variables:
213-
"""
214-
215-
struct MissingVariablesError <: Exception
216-
vars::Any
217-
end
218-
219-
function Base.showerror(io::IO, e::MissingVariablesError)
220-
println(io, MISSING_VARIABLES_MESSAGE)
221-
println(io, join(e.vars, ", "))
222-
end
223-
224-
function _varmap_to_vars(varmap::Dict, varlist; defaults = Dict(), check = false,
225-
toterm = Symbolics.diff2term, initialization_phase = false)
226-
varmap = canonicalize_varmap(varmap; toterm)
227-
defaults = canonicalize_varmap(defaults; toterm)
228-
varmap = merge(defaults, varmap)
229-
values = Dict()
230-
231-
T = Union{}
232-
for var in varlist
233-
var = unwrap(var)
234-
val = unwrap(fixpoint_sub(var, varmap; operator = Symbolics.Operator))
235-
if !isequal(val, var)
236-
values[var] = val
237-
end
238-
end
239-
missingvars = setdiff(varlist, collect(keys(values)))
240-
check && (isempty(missingvars) || throw(MissingVariablesError(missingvars)))
241-
return [values[unwrap(var)] for var in varlist]
242-
end
243-
244-
function varmap_with_toterm(varmap; toterm = Symbolics.diff2term)
245-
return merge(todict(varmap), Dict(toterm(unwrap(k)) => v for (k, v) in varmap))
246-
end
247-
248-
function canonicalize_varmap(varmap; toterm = Symbolics.diff2term)
249-
new_varmap = Dict()
250-
for (k, v) in varmap
251-
k = unwrap(k)
252-
v = unwrap(v)
253-
new_varmap[k] = v
254-
new_varmap[toterm(k)] = v
255-
if Symbolics.isarraysymbolic(k) && Symbolics.shape(k) !== Symbolics.Unknown()
256-
for i in eachindex(k)
257-
new_varmap[k[i]] = v[i]
258-
new_varmap[toterm(k[i])] = v[i]
259-
end
260-
end
261-
end
262-
return new_varmap
263-
end
264-
265-
@noinline function throw_missingvars(vars)
266-
throw(ArgumentError("$vars are missing from the variable map."))
267-
end
268-
269-
struct IsHistory end
270-
ishistory(x::Num) = ishistory(unwrap(x))
271-
ishistory(x::Symbolic) = getmetadata(x, IsHistory, false)
272-
hist(x, t) = wrap(hist(unwrap(x), t))
273-
function hist(x::Symbolic, t)
274-
setmetadata(
275-
toparam(maketerm(typeof(x), operation(x), [unwrap(t)], metadata(x))),
276-
IsHistory, true)
277-
end
278-
279152
## Bounds ======================================================================
280153
struct VariableBounds end
281154
Symbolics.option_to_metadata_type(::Val{:bounds}) = VariableBounds

test/initial_values.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ ps = [k1 => 1.0, k2 => 5.0]
6666
# overdetermined because parameter initialization isn't in yet
6767
@test_warn "overdetermined" oprob=ODEProblem(osys_m, [u0; ps], tspan)
6868

69-
# Make sure it doesn't error on array variables with unspecified size
70-
@parameters p::Vector{Real} q[1:3]
71-
varmap = Dict(p => ones(3), q => 2ones(3))
72-
cvarmap = ModelingToolkit.canonicalize_varmap(varmap)
73-
target_varmap = Dict(p => ones(3), q => 2ones(3), q[1] => 2.0, q[2] => 2.0, q[3] => 2.0)
74-
@test cvarmap == target_varmap
75-
7669
# Initialization of ODEProblem with dummy derivatives of multidimensional arrays
7770
# Issue#1283
7871
@variables z(t)[1:2, 1:2]

test/odesystem.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,6 @@ end
434434

435435
@named sys = System([0 ~ sys1.y + sys2.y], t; systems = [sys1, sys2])
436436

437-
# DelayDiffEq
438-
using ModelingToolkit: hist
439-
@variables x(t) y(t)
440-
xₜ₋₁ = hist(x, t - 1)
441-
eqs = [D(x) ~ x * y
442-
D(y) ~ y * x - xₜ₋₁]
443-
@named sys = System(eqs, t)
444-
445437
# register
446438
using StaticArrays
447439
using SymbolicUtils: term

0 commit comments

Comments
 (0)