Skip to content

Commit 3c28f9e

Browse files
committed
Automatic state selection improved
- Models with lambda variables allowed; lambda variables are ignored when selecting non-states - _x and _derx vectors of StateSelection printed, when an error occurs.
1 parent 59f60a6 commit 3c28f9e

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/symbolic/DAEquations/BasicStructuralTransform.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ function analyzeStructurally(equations, params, unknowns_indices, deriv, unknown
994994
for i in 1:length(IG)-length(Gsolvable)
995995
push!(Gsolvable, []) # This is conservative since highest derivative is not marked as solvable.
996996
end
997-
997+
998998
if automaticStateSelection
999999
startValues = []
10001000
fixedFlags = []
@@ -1004,7 +1004,7 @@ function analyzeStructurally(equations, params, unknowns_indices, deriv, unknown
10041004
end
10051005
@show startValues
10061006
@show fixedFlags
1007-
1007+
println(... 2)
10081008
components = Array{Array{Int64,1},1}(BLT(IG, assign))
10091009
vNames = makeList(unknownsNames, 1:length(assign), Avar) # ::Vector{String}
10101010
eqGraph = StateSelection.getSortedEquationGraph(IG, Gsolvable, components, assign, Avar, Bequ, vNames, withStabilization=false)
@@ -1016,17 +1016,17 @@ function analyzeStructurally(equations, params, unknowns_indices, deriv, unknown
10161016
newAssign = copy(assign)
10171017
# newG = [newG[i] for i in 1:length(newG) if Bequ[i] == 0]
10181018
end
1019-
1019+
10201020
vActive = fill(true, length(Avar))
10211021
if ! automaticStateSelection
10221022
vActive[statesIndices] .= false
10231023
else
1024-
stateIndices = eqGraph.Vx
1024+
stateIndices = eqGraph.Vx[ findall(x->x>0, eqGraph.Vx) ] # Only use Vx indices that are > 0 (= no lambda variables)
10251025
@show names Avar stateIndices
10261026
@show Gsolvable
10271027
for i in 1:length(stateIndices)
10281028
j = stateIndices[i]
1029-
if j in Avar # selected state is a derivative
1029+
if j in Avar # selected state is a derivative, but not a lambda variable and not an algebraic variable
10301030
# search for corresponding variable
10311031
alias = 0
10321032
for ie in 1:length(G)
@@ -1049,7 +1049,7 @@ function analyzeStructurally(equations, params, unknowns_indices, deriv, unknown
10491049
realStates = [GetField(This(), Symbol(name)) for name in stateNames]
10501050
setRealStates(realStates)
10511051
end
1052-
1052+
10531053
if log
10541054
println("\nNot active:")
10551055
for i in 1:length(vActive)

src/symbolic/StateSelection.jl

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,12 @@ Return the sorted equation graph with selection of states and dummy states.
474474
function getSortedEquationGraph(G, Gsolvable, BLT, assign, A, B, VNames; withStabilization::Bool=true)
475475
eqInit = StateSelection.SortedEquationGraph(G, BLT, assign, A, B, VNames)
476476
eq = getSortedEquationGraph!(eqInit, Gsolvable)
477+
#printSortedEquationGraph(eq; equations=false)
478+
#println("\n")
477479

478480
if !withStabilization
479481
# Check whether lambda and/or mue variables are present
482+
#=
480483
lambdaVariables = String[]
481484
for i in eachindex(eq.Vx)
482485
vx = eq.Vx[i]
@@ -489,25 +492,41 @@ function getSortedEquationGraph(G, Gsolvable, BLT, assign, A, B, VNames; withSta
489492
490493
if length(lambdaVariables) > 0 || eq.nmue > 0
491494
# Code generator not prepared to handle DAE stabilization -> Trigger error
495+
printSortedEquationGraph(eq; equations=false)
496+
492497
constraintVariables = String[]
493498
for i in eq.ider1n1
494499
vc = eq.Vx[ eq.VxRev[ i ] ]
495500
push!(constraintVariables, eq.VNames[ vc ])
496501
end
497502
498503
if length(lambdaVariables) > 0
499-
error("... Automatic state selection is not possible, because the code generator does not\n",
504+
error("\n... Automatic state selection is not possible, because the code generator does not\n",
500505
" yet support DAE stabilization of higher index systems.\n",
501506
" Number of lambda variables: ", length(lambdaVariables), ", number of mue variables: ", eq.nmue, "\n",
502-
" The following (lambda) variables are the reason: ", lambdaVariables, "\n",
503-
" The following (state) variables are the reason: ", constraintVariables, ".")
507+
" The following (lambda) variables are the reason: ", lambdaVariables, ".")
504508
elseif eq.nmue > 0
505-
error("... Automatic state selection is not possible, because the code generator does not\n",
509+
error("\n... Automatic state selection is not possible, because the code generator does not\n",
506510
" yet support the generation of stabilizing equations.\n",
507511
" Number of lambda variables: ", length(lambdaVariables), ", number of mue variables: ", eq.nmue, "\n",
508512
" The following (state) variables are the reason: ", constraintVariables, ".")
509513
end
510514
end
515+
=#
516+
if eq.nmue > 0
517+
# Code generator not prepared to handle DAE stabilization -> Trigger error
518+
printSortedEquationGraph(eq; equations=false)
519+
520+
constraintVariables = String[]
521+
for i in eq.ider1n1
522+
vc = eq.Vx[ eq.VxRev[ i ] ]
523+
push!(constraintVariables, eq.VNames[ vc ])
524+
end
525+
error("\n... Automatic state selection is not possible, because the code generator does not\n",
526+
" yet support the generation of stabilizing equations.\n",
527+
" Number of mue variables: ", eq.nmue, "\n",
528+
" The following (state) variables are the reason: ", constraintVariables, ".")
529+
end
511530

512531
# Determine differentiated variables in the original equations that are no states (dummy states)
513532
end
@@ -700,11 +719,12 @@ end
700719

701720

702721
"""
703-
printSortedEquationGraph(eq)
722+
printSortedEquationGraph(eq; equations=true)
704723
705-
Print information about the sorted equation graph
724+
Print information about the sorted equation graph.
725+
If `equations = false`, the equations are not printed.
706726
"""
707-
function printSortedEquationGraph(eq::SortedEquationGraph)
727+
function printSortedEquationGraph(eq::SortedEquationGraph; equations::Bool=true)
708728
# Determine original number of equations (that are not differentiated)
709729
neqOrig = 0
710730
i = 0
@@ -786,6 +806,10 @@ function printSortedEquationGraph(eq::SortedEquationGraph)
786806
print("\n")
787807
end
788808

809+
if !equations
810+
return
811+
end
812+
789813
# Print sorted equations
790814
println("\n Sorted equations (length(_r) = ", length(eq.Er), ", nc = ", eq.nc, "):")
791815

0 commit comments

Comments
 (0)