@@ -480,6 +480,8 @@ mutable struct EquationGraph
480
480
481
481
# Sorted equations
482
482
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).
483
485
tearedEquations:: Vector{TearedEquations} # Info about teared equation system
484
486
tearedEquations_indices:: Vector{Int} # teq = tearedEquations_indices[e] is tearedEquations[teq]
485
487
eAST:: Vector{Expr} # eAST[e] is the AST of equation e
@@ -854,11 +856,11 @@ function addSolvedEquations!(eq::EquationGraph, eSolved::Vector{Int}, vSolved::V
854
856
# Push solved equation to eAST
855
857
v = vSolved[i]
856
858
e = eSolved[i]
857
- eq. fullAssignRev[e] = v
858
859
eq. eAST[e] = eq. fc. getSolvedEquationAST (e, v) # filter_lineno( )
859
860
if eq. log
860
861
printEquations (eq. eAST[e])
861
862
end
863
+ eq. fullAssignRev[e] = v
862
864
addFixedVariable! (eq,v)
863
865
end
864
866
@@ -1000,17 +1002,22 @@ function sortEquations!(eq::EquationGraph)::Nothing
1000
1002
for blt_i in blt
1001
1003
@assert (length (blt_i) >= 1 )
1002
1004
1005
+ solveLinearEquation = true
1003
1006
if length (blt_i) == 1 # One equation in one unknown
1004
1007
e = blt_i[1 ]
1005
1008
1006
1009
# Check
1007
1010
v = eq. fullAssignRev[e]
1008
- @assert (e == assign[v])
1011
+ if v != - 1
1012
+ @assert (e == assign[v])
1009
1013
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
1012
1019
1013
- else # Equation system
1020
+ if solveLinearEquation # Equation system
1014
1021
# Check that all equations reference the same equation system
1015
1022
e1 = blt_i[1 ]
1016
1023
teq_index = eq. tearedEquations_indices[e1]
@@ -1227,6 +1234,7 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1227
1234
printVariables (eq, vConstraints[i])
1228
1235
end
1229
1236
1237
+ solveLinearEquation = true
1230
1238
if length (eConstraints[i]) == 1 && length (vConstraints[i]) == 1
1231
1239
# One equation in one unknown
1232
1240
if i < length (eConstraints)
@@ -1244,9 +1252,19 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1244
1252
end
1245
1253
1246
1254
# 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
1250
1268
# N equations in M unknowns (M >= N)
1251
1269
@assert (length (vConstraints[i]) >= length (eConstraints[i]))
1252
1270
if log
@@ -1339,12 +1357,14 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1339
1357
if i == length (eConstraints)
1340
1358
# On highest derivative level:
1341
1359
# Assume that the equation system is linear, if at least one of the unknowns is a derivative
1342
- linearAssumption = false
1343
- for v in vConstraints[i]
1344
- if eq. Arev[v] > 0
1345
- linearAssumption = true
1346
- end
1347
- 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
1348
1368
1349
1369
if linearAssumption
1350
1370
isLinear = true
0 commit comments