@@ -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]
@@ -1220,13 +1227,14 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1220
1227
# Analyze all equation sets eConstraints[i] from lowest-order to highest-order derivatives
1221
1228
for i in eachindex (eConstraints)
1222
1229
if log
1223
- println (" \n ... Equation set $j .$i (highest derivative level = $j . " , length (eConstraints), " ) ..................." )
1230
+ println (" \n ... Equation set $j .$i ........... ..................." )
1224
1231
println (" Equations: " )
1225
1232
printEquations (eq, eConstraints[i])
1226
1233
println (" Unknown variables: " )
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
@@ -1336,28 +1354,17 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1336
1354
# Check that equation system is linear in the unknowns
1337
1355
(isLinear, hasConstantCoefficients) = isLinearEquationSystem! (eq, eConstraints[i], vConstraints[i])
1338
1356
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
- #=
1352
1357
if i == length (eConstraints)
1353
1358
# On highest derivative level:
1354
1359
# 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
1361
1368
1362
1369
if linearAssumption
1363
1370
isLinear = true
@@ -1368,7 +1375,7 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1368
1375
equations = eConstraints[i])
1369
1376
end
1370
1377
end
1371
- =#
1378
+
1372
1379
if ! isLinear
1373
1380
showMessage2 (" Cannot transform to ODE, because equation system is not linear." ;
1374
1381
severity = ERROR,
@@ -1472,8 +1479,8 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1472
1479
1473
1480
1474
1481
# 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
1477
1484
showMessage2 (" The following variables have an 'init' initialization and are explicitly solved for." ,
1478
1485
details = " Therefore, the 'init' values have no effect, but must exactly match the values,\n " *
1479
1486
" computed during initialization. Otherwise this gives a run-time error.\n " *
0 commit comments