@@ -149,133 +149,6 @@ function default_toterm(x)
149
149
end
150
150
end
151
151
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
-
279
152
# # Bounds ======================================================================
280
153
struct VariableBounds end
281
154
Symbolics. option_to_metadata_type (:: Val{:bounds} ) = VariableBounds
0 commit comments