Skip to content

Commit 64cde91

Browse files
committed
State selection improved, in order to compute residuals of linear equations with a function call
(current assumption: function is linear with respect to the variable vector to be solved for; should be explicitly defined in the future).
1 parent 5967603 commit 64cde91

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/StateSelection.jl

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
@@ -1227,6 +1234,7 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
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
@@ -1339,12 +1357,14 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
13391357
if i == length(eConstraints)
13401358
# On highest derivative level:
13411359
# 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
13481368

13491369
if linearAssumption
13501370
isLinear = true

0 commit comments

Comments
 (0)