You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
iv = ModelingToolkit.has_iv(sys) ? ModelingToolkit.get_iv(sys) :nothing
255
256
256
257
for var in1:length(fullvars)
257
258
dv = var_to_diff[var]
@@ -337,31 +338,32 @@ variables and equations, don't add them when they already exist.
337
338
###### DISCRETE SYSTEMS #######
338
339
339
340
Documenting the differences to structural simplification for discrete systems:
340
-
In discrete systems the lowest-order term is x_k-i, instead of x(t).
341
+
342
+
1. In discrete systems the lowest-order term is Shift(t, k)(x(t)), instead of x(t). We need to substitute the k-1 lowest order terms instead of the k-1 highest order terms.
341
343
342
344
The orders will also be off by one. The reason this is is that the dynamics of
343
345
the system should be given in terms of Shift(t, 1)(x(t), x(t-1), ...). But
344
346
having the observables be indexed by the next time step is not so nice. So we
345
-
handle the shifts in the renaming, rather than explicitly.
347
+
handle the shifts in the renaming.
346
348
347
349
The substitution should look like the following:
348
350
x(t) -> Shift(t, 1)(x(t))
349
-
Shift(t, -1)(x(t)) -> x(t)
351
+
Shift(t, -1)(x(t)) -> Shift(t, 0)(x(t))
350
352
Shift(t, -2)(x(t)) -> x_{t-1}(t)
351
353
Shift(t, -3)(x(t)) -> x_{t-2}(t)
352
354
and so on...
353
355
354
356
In the implicit discrete case this shouldn't happen. The simplification should
355
357
look like a NonlinearSystem.
356
358
357
-
For discrete systems Shift(t, 2)(x(t)) is not equivalent to Shift(t, 1)(Shift(t,1)(x(t))
359
+
2. For discrete systems Shift(t, 2)(x(t)) cannot be substituted as Shift(t, 1)(Shift(t,1)(x(t)).
358
360
This is different from the continuous case where D(D(x)) can be substituted for
359
361
by iteratively substituting x_t ~ D(x), then x_tt ~ D(x_t). For this reason the
360
-
total_sub dict is updated at the time that the renamed variables are written,
362
+
shift_sub dict is updated at the time that the renamed variables are written,
361
363
inside the loop where new variables are generated.
@@ -540,6 +552,7 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v
540
552
# fullvars[iv] is a differential variable of the form D^n(x), and neweqs[ieq]
541
553
# is solved to give the RHS.
542
554
for ieq in eqs
555
+
println()
543
556
iv = eq_var_matching[ieq]
544
557
ifis_solvable(ieq, iv)
545
558
# We don't solve differential equations, but we will need to try to
@@ -549,7 +562,9 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v
549
562
isnothing(D) &&
550
563
error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])")
551
564
order, lv =var_order(diff_to_var, iv)
552
-
dx =D(fullvars[lv])
565
+
@show fullvars[lv]
566
+
@showsimplify_shifts(fullvars[lv])
567
+
dx =D(simplify_shifts(fullvars[lv]))
553
568
eq = dx ~simplify_shifts(Symbolics.fixpoint_sub(
554
569
Symbolics.symbolic_linear_solve(neweqs[ieq],
555
570
fullvars[iv]),
@@ -560,6 +575,9 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v
560
575
end
561
576
push!(diff_eqs, eq)
562
577
total_sub[simplify_shifts(eq.lhs)] = eq.rhs
578
+
dx_sub[simplify_shifts(eq.lhs)] = eq.rhs
579
+
@show total_sub
580
+
@show eq
563
581
push!(diffeq_idxs, ieq)
564
582
push!(diff_vars, diff_to_var[iv])
565
583
continue
@@ -575,10 +593,10 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v
575
593
@warn"Tearing: solving $eq for $var is singular!"
576
594
else
577
595
rhs =-b / a
578
-
neweq = var ~ Symbolics.fixpoint_sub(
596
+
neweq = var ~simplify_shifts(Symbolics.fixpoint_sub(
579
597
simplify ?
580
598
Symbolics.simplify(rhs) : rhs,
581
-
total_sub; operator = ModelingToolkit.Shift)
599
+
dx_sub; operator = ModelingToolkit.Shift))
582
600
push!(subeqs, neweq)
583
601
push!(solved_equations, ieq)
584
602
push!(solved_variables, iv)
@@ -589,7 +607,7 @@ function solve_and_generate_equations!(state::TearingState, neweqs, total_sub, v
0 commit comments