@@ -248,11 +248,10 @@ 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)
251+ function substitute_derivatives_algevars! (ts:: TearingState , neweqs, var_eq_matching, dummy_sub; iv = nothing , D = nothing )
252252 @unpack fullvars, sys, structure = ts
253253 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
254254 diff_to_var = invview (var_to_diff)
255- iv = ModelingToolkit. has_iv (sys) ? ModelingToolkit. get_iv (sys) : nothing
256255
257256 for var in 1 : length (fullvars)
258257 dv = var_to_diff[var]
@@ -361,12 +360,11 @@ Effects on the system structure:
361360- solvable_graph:
362361- var_eq_matching: match D(x) to the added identity equation
363362"""
364- function generate_derivative_variables! (ts:: TearingState , neweqs, var_eq_matching; mm = nothing )
363+ function generate_derivative_variables! (ts:: TearingState , neweqs, var_eq_matching; mm = nothing , iv = nothing , D = nothing )
365364 @unpack fullvars, sys, structure = ts
366365 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
367366 eq_var_matching = invview (var_eq_matching)
368367 diff_to_var = invview (var_to_diff)
369- iv = ModelingToolkit. has_iv (sys) ? ModelingToolkit. get_iv (sys) : nothing
370368 is_discrete = is_only_discrete (structure)
371369 lower_varname = is_discrete ? lower_shift_varname : lower_varname_with_unit
372370 linear_eqs = mm === nothing ? Dict {Int, Int} () :
@@ -461,27 +459,18 @@ end
461459Solve the solvable equations of the system and generate differential (or discrete)
462460equations in terms of the selected states.
463461"""
464- function generate_system_equations! (state:: TearingState , neweqs, var_eq_matching; simplify = false )
462+ function generate_system_equations! (state:: TearingState , neweqs, var_eq_matching; simplify = false , iv = nothing , D = nothing )
465463 @unpack fullvars, sys, structure = state
466464 @unpack solvable_graph, var_to_diff, eq_to_diff, graph = structure
467465 eq_var_matching = invview (var_eq_matching)
468466 diff_to_var = invview (var_to_diff)
469467 total_sub = Dict ()
470-
471- if ModelingToolkit. has_iv (sys)
472- iv = get_iv (sys)
473- if is_only_discrete (structure)
474- D = Shift (iv, 1 )
475- for v in fullvars
476- op = operation (v)
477- op isa Shift && (op. steps < 0 ) && (total_sub[v] = lower_shift_varname (v, iv))
478- end
479- else
480- D = Differential (iv)
468+ if is_only_discrete (structure)
469+ for v in fullvars
470+ op = operation (v)
471+ op isa Shift && (op. steps < 0 ) && (total_sub[v] = lower_shift_varname (v, iv))
481472 end
482- else
483- iv = D = nothing
484- end
473+ end
485474
486475 # if var is like D(x) or Shift(t, 1)(x)
487476 isdervar = let diff_to_var = diff_to_var
@@ -727,13 +716,23 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
727716 neweqs = collect (equations (state))
728717 dummy_sub = Dict ()
729718
719+ if ModelingToolkit. has_iv (state. sys)
720+ iv = get_iv (state. sys)
721+ if ! is_only_discrete (state. structure)
722+ D = Differential (iv)
723+ else
724+ D = Shift (iv, 1 )
725+ end
726+ iv = D = nothing
727+ end
728+
730729 # Structural simplification
731- substitute_derivatives_algevars! (state, neweqs, var_eq_matching, dummy_sub)
730+ substitute_derivatives_algevars! (state, neweqs, var_eq_matching, dummy_sub; iv, D )
732731
733- generate_derivative_variables! (state, neweqs, var_eq_matching; mm)
732+ generate_derivative_variables! (state, neweqs, var_eq_matching; mm, iv, D )
734733
735734 neweqs, solved_eqs, eq_ordering, var_ordering, nelim_eq, nelim_var =
736- generate_system_equations! (state, neweqs, var_eq_matching; simplify)
735+ generate_system_equations! (state, neweqs, var_eq_matching; simplify, iv, D )
737736
738737 state = reorder_vars! (state, var_eq_matching, eq_ordering, var_ordering, nelim_eq, nelim_var)
739738
0 commit comments