@@ -248,7 +248,8 @@ called dummy derivatives.
248
248
State selection is done. All non-differentiated variables are algebraic
249
249
variables, and all variables that appear differentiated are differential variables.
250
250
"""
251
- function substitute_derivatives_algevars! (ts:: TearingState , neweqs, var_eq_matching, dummy_sub; iv = nothing , D = nothing )
251
+ function substitute_derivatives_algevars! (
252
+ ts:: TearingState , neweqs, var_eq_matching, dummy_sub; iv = nothing , D = nothing )
252
253
@unpack fullvars, sys, structure = ts
253
254
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
254
255
diff_to_var = invview (var_to_diff)
288
289
#=
289
290
There are three cases where we want to generate new variables to convert
290
291
the system into first order (semi-implicit) ODEs.
291
-
292
+
292
293
1. To first order:
293
294
Whenever higher order differentiated variable like `D(D(D(x)))` appears,
294
295
we introduce new variables `x_t`, `x_tt`, and `x_ttt` and new equations
@@ -364,7 +365,8 @@ Effects on the system structure:
364
365
- solvable_graph:
365
366
- var_eq_matching: match D(x) to the added identity equation D(x) ~ x_t
366
367
"""
367
- function generate_derivative_variables! (ts:: TearingState , neweqs, var_eq_matching; mm = nothing , iv = nothing , D = nothing )
368
+ function generate_derivative_variables! (
369
+ ts:: TearingState , neweqs, var_eq_matching; mm = nothing , iv = nothing , D = nothing )
368
370
@unpack fullvars, sys, structure = ts
369
371
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
370
372
eq_var_matching = invview (var_eq_matching)
@@ -395,7 +397,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin
395
397
dx = fullvars[dv]
396
398
order, lv = var_order (dv, diff_to_var)
397
399
x_t = is_discrete ? lower_shift_varname_with_unit (fullvars[dv], iv) :
398
- lower_varname_with_unit (fullvars[lv], iv, order)
400
+ lower_varname_with_unit (fullvars[lv], iv, order)
399
401
400
402
# Add `x_t` to the graph
401
403
v_t = add_dd_variable! (structure, fullvars, x_t, dv)
@@ -405,7 +407,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin
405
407
# Update matching
406
408
push! (var_eq_matching, unassigned)
407
409
var_eq_matching[dv] = unassigned
408
- eq_var_matching[dummy_eq] = dv
410
+ eq_var_matching[dummy_eq] = dv
409
411
end
410
412
end
411
413
@@ -428,7 +430,7 @@ function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm)
428
430
return eq, v_t
429
431
end
430
432
end
431
- return nothing
433
+ return nothing
432
434
end
433
435
434
436
"""
@@ -492,8 +494,9 @@ Order the new equations and variables such that the differential equations
492
494
and variables come first. Return the new equations, the solved equations,
493
495
the new orderings, and the number of solved variables and equations.
494
496
"""
495
- function generate_system_equations! (state:: TearingState , neweqs, var_eq_matching; simplify = false , iv = nothing , D = nothing )
496
- @unpack fullvars, sys, structure = state
497
+ function generate_system_equations! (state:: TearingState , neweqs, var_eq_matching;
498
+ simplify = false , iv = nothing , D = nothing )
499
+ @unpack fullvars, sys, structure = state
497
500
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
498
501
eq_var_matching = invview (var_eq_matching)
499
502
diff_to_var = invview (var_to_diff)
@@ -502,11 +505,12 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching
502
505
if is_only_discrete (structure)
503
506
for (i, v) in enumerate (fullvars)
504
507
op = operation (v)
505
- op isa Shift && (op. steps < 0 ) && begin
506
- lowered = lower_shift_varname_with_unit (v, iv)
507
- total_sub[v] = lowered
508
- fullvars[i] = lowered
509
- end
508
+ op isa Shift && (op. steps < 0 ) &&
509
+ begin
510
+ lowered = lower_shift_varname_with_unit (v, iv)
511
+ total_sub[v] = lowered
512
+ fullvars[i] = lowered
513
+ end
510
514
end
511
515
end
512
516
@@ -581,10 +585,11 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching
581
585
end
582
586
solved_vars_set = BitSet (solved_vars)
583
587
var_ordering = [diff_vars;
584
- setdiff! (setdiff (1 : ndsts (graph), diff_vars_set),
585
- solved_vars_set)]
588
+ setdiff! (setdiff (1 : ndsts (graph), diff_vars_set),
589
+ solved_vars_set)]
586
590
587
- return neweqs, solved_eqs, eq_ordering, var_ordering, length (solved_vars), length (solved_vars_set)
591
+ return neweqs, solved_eqs, eq_ordering, var_ordering, length (solved_vars),
592
+ length (solved_vars_set)
588
593
end
589
594
590
595
"""
@@ -648,7 +653,8 @@ Eliminate the solved variables and equations from the graph and permute the
648
653
graph's vertices to account for the new variable/equation ordering.
649
654
"""
650
655
# TODO : BLT sorting
651
- function reorder_vars! (state:: TearingState , var_eq_matching, eq_ordering, var_ordering, nsolved_eq, nsolved_var)
656
+ function reorder_vars! (state:: TearingState , var_eq_matching, eq_ordering,
657
+ var_ordering, nsolved_eq, nsolved_var)
652
658
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = state. structure
653
659
654
660
eqsperm = zeros (Int, nsrcs (graph))
692
698
"""
693
699
Update the system equations, unknowns, and observables after simplification.
694
700
"""
695
- function update_simplified_system! (state:: TearingState , neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns;
701
+ function update_simplified_system! (
702
+ state:: TearingState , neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns;
696
703
cse_hack = true , array_hack = true )
697
704
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = state. structure
698
705
diff_to_var = invview (var_to_diff)
@@ -732,7 +739,6 @@ function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dumm
732
739
sys = schedule (sys)
733
740
end
734
741
735
-
736
742
"""
737
743
Give the order of the variable indexed by dv.
738
744
"""
@@ -790,12 +796,14 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
790
796
791
797
generate_derivative_variables! (state, neweqs, var_eq_matching; mm, iv, D)
792
798
793
- neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var =
794
- generate_system_equations! ( state, neweqs, var_eq_matching; simplify, iv, D)
799
+ neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var = generate_system_equations! (
800
+ state, neweqs, var_eq_matching; simplify, iv, D)
795
801
796
- state = reorder_vars! (state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var)
802
+ state = reorder_vars! (
803
+ state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var)
797
804
798
- sys = update_simplified_system! (state, neweqs, solved_eqs, dummy_sub, var_eq_matching, extra_unknowns; cse_hack, array_hack)
805
+ sys = update_simplified_system! (state, neweqs, solved_eqs, dummy_sub, var_eq_matching,
806
+ extra_unknowns; cse_hack, array_hack)
799
807
800
808
@set! state. sys = sys
801
809
@set! sys. tearing_state = state
0 commit comments