@@ -203,19 +203,14 @@ function generate_discrete_affect(
203
203
@static if VERSION < v " 1.7"
204
204
error (" The `generate_discrete_affect` function requires at least Julia 1.7" )
205
205
end
206
- use_index_cache = has_index_cache (osys) && get_index_cache (osys) != = nothing
206
+ has_index_cache (osys) && get_index_cache (osys) != = nothing ||
207
+ error (" Hybrid systems require `split = true`" )
207
208
out = Sym {Any} (:out )
208
209
appended_parameters = full_parameters (syss[continuous_id])
209
210
offset = length (appended_parameters)
210
- param_to_idx = if use_index_cache
211
- Dict {Any, ParameterIndex} (p => parameter_index (osys, p)
212
- for p in appended_parameters)
213
- else
214
- Dict {Any, Int} (reverse (en) for en in enumerate (appended_parameters))
215
- end
211
+ param_to_idx = Dict {Any, ParameterIndex} (p => parameter_index (osys, p)
212
+ for p in appended_parameters)
216
213
affect_funs = []
217
- init_funs = []
218
- svs = []
219
214
clocks = TimeDomain[]
220
215
for (i, (sys, input)) in enumerate (zip (syss, inputs))
221
216
i == continuous_id && continue
@@ -231,11 +226,7 @@ function generate_discrete_affect(
231
226
push! (fullvars, s)
232
227
end
233
228
needed_disc_to_cont_obs = []
234
- if use_index_cache
235
- disc_to_cont_idxs = ParameterIndex[]
236
- else
237
- disc_to_cont_idxs = Int[]
238
- end
229
+ disc_to_cont_idxs = ParameterIndex[]
239
230
for v in inputs[continuous_id]
240
231
_v = arguments (v)[1 ]
241
232
if _v in fullvars
@@ -255,7 +246,7 @@ function generate_discrete_affect(
255
246
end
256
247
append! (appended_parameters, input)
257
248
cont_to_disc_obs = build_explicit_observed_function (
258
- use_index_cache ? osys : syss[continuous_id] ,
249
+ osys,
259
250
needed_cont_to_disc_obs,
260
251
throw = false ,
261
252
expression = true ,
@@ -281,56 +272,16 @@ function generate_discrete_affect(
281
272
disc_range = [parameter_index (osys, sym) for sym in unknowns (sys)]
282
273
save_expr = :($ (SciMLBase. save_discretes!)(integrator, $ i))
283
274
empty_disc = isempty (disc_range)
284
- disc_init = if use_index_cache
285
- :(function (u, p, t)
286
- c2d_obs = $ cont_to_disc_obs
287
- d2c_obs = $ disc_to_cont_obs
288
- result = c2d_obs (u, p... , t)
289
- for (val, i) in zip (result, $ cont_to_disc_idxs)
290
- $ (_set_parameter_unchecked!)(p, val, i; update_dependent = false )
291
- end
292
-
293
- disc_state = Tuple ($ (parameter_values)(p, i) for i in $ disc_range)
294
- result = d2c_obs (disc_state, p... , t)
295
- for (val, i) in zip (result, $ disc_to_cont_idxs)
296
- # prevent multiple updates to dependents
297
- _set_parameter_unchecked! (p, val, i; update_dependent = false )
298
- end
299
- discretes, repack, _ = $ (SciMLStructures. canonicalize)(
300
- $ (SciMLStructures. Discrete ()), p)
301
- repack (discretes) # to force recalculation of dependents
302
- end )
303
- else
304
- :(function (u, p, t)
305
- c2d_obs = $ cont_to_disc_obs
306
- d2c_obs = $ disc_to_cont_obs
307
- c2d_view = view (p, $ cont_to_disc_idxs)
308
- d2c_view = view (p, $ disc_to_cont_idxs)
309
- disc_unknowns = view (p, $ disc_range)
310
- copyto! (c2d_view, c2d_obs (u, p, t))
311
- copyto! (d2c_view, d2c_obs (disc_unknowns, p, t))
312
- end )
313
- end
314
275
315
276
# @show disc_to_cont_idxs
316
277
# @show cont_to_disc_idxs
317
278
# @show disc_range
318
- affect! = :(function (integrator, saved_values )
279
+ affect! = :(function (integrator)
319
280
@unpack u, p, t = integrator
320
281
c2d_obs = $ cont_to_disc_obs
321
282
d2c_obs = $ disc_to_cont_obs
322
- $ (
323
- if use_index_cache
324
- :(disc_unknowns = [$ (parameter_values)(p, i) for i in $ disc_range])
325
- else
326
- quote
327
- c2d_view = view (p, $ cont_to_disc_idxs)
328
- d2c_view = view (p, $ disc_to_cont_idxs)
329
- disc_unknowns = view (p, $ disc_range)
330
- end
331
- end
332
- )
333
283
# TODO : find a way to do this without allocating
284
+ disc_unknowns = [$ (parameter_values)(p, i) for i in $ disc_range]
334
285
disc = $ disc
335
286
336
287
# Write continuous into to discrete: handles `Sample`
@@ -353,71 +304,32 @@ function generate_discrete_affect(
353
304
$ (_set_parameter_unchecked!)(p, val, i; update_dependent = false )
354
305
end
355
306
end
356
- else
357
- :(copyto! (c2d_view, c2d_obs (integrator. u, p, t)))
358
- end
359
- )
307
+ end )
360
308
# @show "after c2d", p
361
- $ (
362
- if use_index_cache
363
- quote
364
- if ! $ empty_disc
365
- disc (disc_unknowns, integrator. u, p... , t)
366
- for (val, i) in zip (disc_unknowns, $ disc_range)
367
- $ (_set_parameter_unchecked!)(p, val, i; update_dependent = false )
368
- end
369
- end
370
- end
371
- else
372
- :($ empty_disc || disc (disc_unknowns, disc_unknowns, p, t))
373
- end
374
- )
375
309
# @show "after state update", p
376
- $ (
377
- if use_index_cache
378
- quote
379
- result = d2c_obs (disc_unknowns, p... , t)
380
- for (val, i) in zip (result, $ disc_to_cont_idxs)
381
- $ (_set_parameter_unchecked!)(p, val, i; update_dependent = false )
382
- end
383
- end
384
- else
385
- :(copyto! (d2c_view, d2c_obs (disc_unknowns, p, t)))
310
+ result = d2c_obs (disc_unknowns, p... , t)
311
+ for (val, i) in zip (result, $ disc_to_cont_idxs)
312
+ $ (_set_parameter_unchecked!)(p, val, i; update_dependent = false )
386
313
end
387
- )
388
314
389
- push! (saved_values. t, t)
390
- push! (saved_values. saveval, $ save_vec)
315
+ $ save_expr
391
316
392
317
# @show "after d2c", p
393
- $ (
394
- if use_index_cache
395
- quote
396
- discretes, repack, _ = $ (SciMLStructures. canonicalize)(
397
- $ (SciMLStructures. Discrete ()), p)
398
- repack (discretes)
399
- end
400
- end
401
- )
318
+ discretes, repack, _ = $ (SciMLStructures. canonicalize)(
319
+ $ (SciMLStructures. Discrete ()), p)
320
+ repack (discretes)
402
321
end )
403
322
404
323
push! (affect_funs, affect!)
405
- push! (init_funs, disc_init)
406
- push! (svs, sv)
407
324
end
408
325
if eval_expression
409
326
affects = map (a -> eval_module. eval (toexpr (LiteralExpr (a))), affect_funs)
410
- inits = map (a -> eval_module. eval (toexpr (LiteralExpr (a))), init_funs)
411
327
else
412
328
affects = map (affect_funs) do a
413
329
drop_expr (RuntimeGeneratedFunction (
414
330
eval_module, eval_module, toexpr (LiteralExpr (a))))
415
331
end
416
- inits = map (init_funs) do a
417
- drop_expr (RuntimeGeneratedFunction (
418
- eval_module, eval_module, toexpr (LiteralExpr (a))))
419
- end
420
332
end
421
333
defaults = Dict {Any, Any} (v => 0.0 for v in Iterators. flatten (inputs))
422
- return affects, inits, clocks, svs , appended_parameters, defaults
334
+ return affects, clocks, appended_parameters, defaults
423
335
end
0 commit comments