@@ -248,7 +248,8 @@ called dummy derivatives.
248248State selection is done. All non-differentiated variables are algebraic
249249variables, and all variables that appear differentiated are differential variables.
250250"""
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 )
252253 @unpack fullvars, sys, structure = ts
253254 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
254255 diff_to_var = invview (var_to_diff)
288289#=
289290There are three cases where we want to generate new variables to convert
290291the system into first order (semi-implicit) ODEs.
291-
292+
2922931. To first order:
293294Whenever higher order differentiated variable like `D(D(D(x)))` appears,
294295we introduce new variables `x_t`, `x_tt`, and `x_ttt` and new equations
@@ -364,7 +365,8 @@ Effects on the system structure:
364365- solvable_graph:
365366- var_eq_matching: match D(x) to the added identity equation D(x) ~ x_t
366367"""
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 )
368370 @unpack fullvars, sys, structure = ts
369371 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
370372 eq_var_matching = invview (var_eq_matching)
@@ -395,7 +397,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin
395397 dx = fullvars[dv]
396398 order, lv = var_order (dv, diff_to_var)
397399 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)
399401
400402 # Add `x_t` to the graph
401403 v_t = add_dd_variable! (structure, fullvars, x_t, dv)
@@ -405,7 +407,7 @@ function generate_derivative_variables!(ts::TearingState, neweqs, var_eq_matchin
405407 # Update matching
406408 push! (var_eq_matching, unassigned)
407409 var_eq_matching[dv] = unassigned
408- eq_var_matching[dummy_eq] = dv
410+ eq_var_matching[dummy_eq] = dv
409411 end
410412end
411413
@@ -428,7 +430,7 @@ function find_duplicate_dd(dv, solvable_graph, diff_to_var, linear_eqs, mm)
428430 return eq, v_t
429431 end
430432 end
431- return nothing
433+ return nothing
432434end
433435
434436"""
@@ -492,8 +494,9 @@ Order the new equations and variables such that the differential equations
492494and variables come first. Return the new equations, the solved equations,
493495the new orderings, and the number of solved variables and equations.
494496"""
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
497500 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
498501 eq_var_matching = invview (var_eq_matching)
499502 diff_to_var = invview (var_to_diff)
@@ -502,11 +505,12 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching
502505 if is_only_discrete (structure)
503506 for (i, v) in enumerate (fullvars)
504507 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
510514 end
511515 end
512516
@@ -581,10 +585,11 @@ function generate_system_equations!(state::TearingState, neweqs, var_eq_matching
581585 end
582586 solved_vars_set = BitSet (solved_vars)
583587 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)]
586590
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)
588593end
589594
590595"""
@@ -648,7 +653,8 @@ Eliminate the solved variables and equations from the graph and permute the
648653graph's vertices to account for the new variable/equation ordering.
649654"""
650655# 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)
652658 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state. structure
653659
654660 eqsperm = zeros (Int, nsrcs (graph))
692698"""
693699Update the system equations, unknowns, and observables after simplification.
694700"""
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;
696703 cse_hack = true , array_hack = true )
697704 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = state. structure
698705 diff_to_var = invview (var_to_diff)
@@ -732,7 +739,6 @@ function update_simplified_system!(state::TearingState, neweqs, solved_eqs, dumm
732739 sys = schedule (sys)
733740end
734741
735-
736742"""
737743Give the order of the variable indexed by dv.
738744"""
@@ -790,12 +796,14 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
790796
791797 generate_derivative_variables! (state, neweqs, var_eq_matching; mm, iv, D)
792798
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)
795801
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)
797804
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)
799807
800808 @set! state. sys = sys
801809 @set! sys. tearing_state = state
0 commit comments