@@ -480,6 +480,8 @@ mutable struct EquationGraph
480480
481481 # Sorted equations
482482 fullAssignRev:: Vector{Int} # fullAssignRev[e] = v: If e is a scalar equation system, it is solved for v.
483+ # If v = -1, the scalar equation system is not solved directly, but
484+ # as linear equation system (with tearing).
483485 tearedEquations:: Vector{TearedEquations} # Info about teared equation system
484486 tearedEquations_indices:: Vector{Int} # teq = tearedEquations_indices[e] is tearedEquations[teq]
485487 eAST:: Vector{Expr} # eAST[e] is the AST of equation e
@@ -854,11 +856,11 @@ function addSolvedEquations!(eq::EquationGraph, eSolved::Vector{Int}, vSolved::V
854856 # Push solved equation to eAST
855857 v = vSolved[i]
856858 e = eSolved[i]
857- eq. fullAssignRev[e] = v
858859 eq. eAST[e] = eq. fc. getSolvedEquationAST (e, v) # filter_lineno( )
859860 if eq. log
860861 printEquations (eq. eAST[e])
861862 end
863+ eq. fullAssignRev[e] = v
862864 addFixedVariable! (eq,v)
863865 end
864866
@@ -1000,17 +1002,22 @@ function sortEquations!(eq::EquationGraph)::Nothing
10001002 for blt_i in blt
10011003 @assert (length (blt_i) >= 1 )
10021004
1005+ solveLinearEquation = true
10031006 if length (blt_i) == 1 # One equation in one unknown
10041007 e = blt_i[1 ]
10051008
10061009 # Check
10071010 v = eq. fullAssignRev[e]
1008- @assert (e == assign[v])
1011+ if v != - 1
1012+ @assert (e == assign[v])
10091013
1010- # Push AST
1011- push! (eq. AST, eq. eAST[e])
1014+ # Push AST
1015+ push! (eq. AST, eq. eAST[e])
1016+ solveLinearEquation = false
1017+ end
1018+ end
10121019
1013- else # Equation system
1020+ if solveLinearEquation # Equation system
10141021 # Check that all equations reference the same equation system
10151022 e1 = blt_i[1 ]
10161023 teq_index = eq. tearedEquations_indices[e1]
@@ -1220,13 +1227,14 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
12201227 # Analyze all equation sets eConstraints[i] from lowest-order to highest-order derivatives
12211228 for i in eachindex (eConstraints)
12221229 if log
1223- println (" \n ... Equation set $j .$i (highest derivative level = $j . " , length (eConstraints), " ) ..................." )
1230+ println (" \n ... Equation set $j .$i ........... ..................." )
12241231 println (" Equations: " )
12251232 printEquations (eq, eConstraints[i])
12261233 println (" Unknown variables: " )
12271234 printVariables (eq, vConstraints[i])
12281235 end
12291236
1237+ solveLinearEquation = true
12301238 if length (eConstraints[i]) == 1 && length (vConstraints[i]) == 1
12311239 # One equation in one unknown
12321240 if i < length (eConstraints)
@@ -1244,9 +1252,19 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
12441252 end
12451253
12461254 # Solve equation for unknown v and add solved equation to AST
1247- addSolvedEquations! (eq, eConstraints[i], vConstraints[i])
1248-
1249- else
1255+ try
1256+ addSolvedEquations! (eq, eConstraints[i], vConstraints[i])
1257+ solveLinearEquation = false
1258+ catch
1259+ if log
1260+ println (" Not possible to solve the equation directly. Try so solve it as linear equation:" )
1261+ end
1262+ solveLinearEquation = true
1263+ eq. fullAssignRev[eConstraints[i][1 ]] = - 1
1264+ end
1265+ end
1266+
1267+ if solveLinearEquation
12501268 # N equations in M unknowns (M >= N)
12511269 @assert (length (vConstraints[i]) >= length (eConstraints[i]))
12521270 if log
@@ -1336,28 +1354,17 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
13361354 # Check that equation system is linear in the unknowns
13371355 (isLinear, hasConstantCoefficients) = isLinearEquationSystem! (eq, eConstraints[i], vConstraints[i])
13381356 if ! isLinear
1339- # Temporary fix for Rectifier
1340- if i == length (eConstraints)
1341- # On highest derivative level:
1342- # Assume that the equation system is linear
1343- isLinear = true
1344- hasConstantCoefficients = false
1345- showMessage2 (" It is heuristically assumed that equation system is linear (although isLinearEquation returned isLinear=false)." ;
1346- severity = WARNING,
1347- variables = vConstraints[i],
1348- equations = eConstraints[i])
1349- end
1350-
1351- #=
13521357 if i == length (eConstraints)
13531358 # On highest derivative level:
13541359 # Assume that the equation system is linear, if at least one of the unknowns is a derivative
1355- linearAssumption = false
1356- for v in vConstraints[i]
1357- if eq.Arev[v] > 0
1358- linearAssumption = true
1359- end
1360- end
1360+ linearAssumption = true # temporarily
1361+
1362+ # linearAssumption = false
1363+ # for v in vConstraints[i]
1364+ # if eq.Arev[v] > 0
1365+ # linearAssumption = true
1366+ # end
1367+ # end
13611368
13621369 if linearAssumption
13631370 isLinear = true
@@ -1368,7 +1375,7 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
13681375 equations = eConstraints[i])
13691376 end
13701377 end
1371- =#
1378+
13721379 if ! isLinear
13731380 showMessage2 (" Cannot transform to ODE, because equation system is not linear." ;
13741381 severity = ERROR,
@@ -1472,8 +1479,8 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
14721479
14731480
14741481 # Print warning, if there are variables with fixed=true that are
1475- # explicitly solved for
1476- if length (eq. equationInfo. vSolvedWithFixedTrue) > 0
1482+ # explicitly solved for and log=true
1483+ if log && length (eq. equationInfo. vSolvedWithFixedTrue) > 0
14771484 showMessage2 (" The following variables have an 'init' initialization and are explicitly solved for." ,
14781485 details = " Therefore, the 'init' values have no effect, but must exactly match the values,\n " *
14791486 " computed during initialization. Otherwise this gives a run-time error.\n " *
0 commit comments