Skip to content

Commit cf0dc26

Browse files
committed
Documentation of StateSelection.jl and associated tests improved + new test model for automatic state selection added
1 parent 9efa471 commit cf0dc26

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

src/symbolic/StateSelection.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ mutable struct SortedEquationGraph
156156
Gunknowns::Vector{Vector{Int}} # Gunknowns[i] is the vector of variables of equation i that are treated as unknowns
157157
Gsolvable::Vector{Any} # Gsolvable[i] contains the variables that can be
158158
# solved for equation i (used for tearing)
159+
# Gsolvable is only needed for the non-differentiated equations
160+
# (tearing variables of differentiated equations are automatically decuded from
161+
# the non-differentiated equations)
159162
eConstraintsVec::Vector{Vector{Vector{Int}}} # ec = eConstraintsVec[i]: constraint sets of BLT block i;
160163
# ec[1] : lowest derivative equations
161164
# ec[end]: highest derivative equations (= BLT block i)
@@ -264,10 +267,10 @@ mutable struct SortedEquationGraph
264267
end
265268
end
266269

267-
if lowerDerivativeEquationsInBLT
268-
println("\n... Warning from SortedEquationGraph(..) (in StateSelection.jl):",
269-
"\n... BLT blocks with lower derivative equations have been ignored.\n")
270-
end
270+
#if lowerDerivativeEquationsInBLT
271+
# println("\n... Warning from SortedEquationGraph(..) (in StateSelection.jl):",
272+
# "\n... BLT blocks with lower derivative equations have been ignored.\n")
273+
#end
271274

272275
# Tearing information
273276
td = TraverseDAG(Gunknowns, length(VNames))
@@ -445,7 +448,8 @@ Return the sorted equation graph with selection of states and dummy states.
445448
446449
# Input arguments
447450
- `Gorigin`: Gorigin[i] is the vector of variable indices of equation i.
448-
- `Gsolvable`: Gsolvable[i] is a subset of Gorigin[i] and contains the unknowns that can be explicitly solved for (used for tearing)
451+
- `Gsolvable`: Gsolvable[i] is a subset of Gorigin[i] and contains the unknowns that can be explicitly solved for (used for tearing).
452+
Gsolvable is only needed for the non-differentiated equations (all differentiated equations j can have Gsolvable[j]=[])
449453
- `BLT`: BLT[i] is the vector of equations belonging to BLT-block i
450454
- `assign`: ei = assign[vi] is equation ei assigned to variable vi
451455
- `A`: A-Vector of Pantelides: `A[i] = if der(v[i]) == v[k] then k else 0` where `v[i]` is variable `i`.
@@ -520,7 +524,8 @@ Constructs all information for the sorted equation graph.
520524
Input arguments:
521525
522526
- `eqInit`: Initialized data structure instantiated with SortedEquationGraph()
523-
- `Gsolvable`: Subset of eqInit.Gunknowns that contains the unknowns that can be solved for (used for tearing)
527+
- `Gsolvable`: Subset of eqInit.Gunknowns that contains the unknowns that can be solved for (used for tearing).
528+
Gsolvable is only needed for the non-differentiated equations (all differentiated equations j can have Gsolvable[j]=[])
524529
525530
Output arguments in sortedEquationGraph:
526531

test/models/TestAutomaticStateSelection.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ result = simulate(ParallelCapacitors2, 1; logTranslation=true, logSimulation=log
8383
plot(result, ("C1.v", "C2.v"), figure=4)
8484

8585

86-
# Simulates with tearing = false and u2.state=false
87-
# Translation error if tearing = true and u2.state=false
8886
@model ParallelCapacitors2b begin
8987
C1 = 1e-3
9088
C2 = 2e-3
@@ -105,4 +103,26 @@ result = simulate(ParallelCapacitors2b , 1.0; logTranslation=true, logSimulation
105103
plot(result, ("u1", "u2", "i1", "v1"), figure=6)
106104

107105

106+
@model TwoInertiasConnectedViaIdealGearWithPositionConstraints begin
107+
J1 = 2.0
108+
J2 = 3.0
109+
ratio = 4.0
110+
phi1 = Float(size=(), start=1.0)
111+
phi2 = Float(size=(), start=1.0)
112+
w1 = Float(size=(), start=1.0)
113+
w2 = Float(size=(), start=1.0)
114+
tau = Float(size=())
115+
t = Float(size=())
116+
@equations begin
117+
der(t) = 1.0
118+
w1 = der(phi1)
119+
w2 = der(phi2)
120+
J1*der(w1) = sin(t) - ratio*tau
121+
J2*der(w2) = tau
122+
phi1 = ratio*phi2
123+
end
124+
end
125+
result = simulate(TwoInertiasConnectedViaIdealGearWithPositionConstraints, 3.0; logTranslation=true, logSimulation=logSimulation, tearing=true, removeSingularities=true, automaticStateSelection=true)
126+
plot(result, [("phi1", "phi2"), ("w1", "w2")], figure=7)
127+
108128
end

test/symbolic/TestStateSelectionAlgorithm.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ vNames:
148148

149149
# First version where all unknowns can be solved for
150150
println("\n\n... Test two coupled inertias (all unknowns can be solved for)")
151-
Gsolvable = Gorigin
151+
# Gsolvable = Gorigin
152+
Gsolvable = StateSelection.newRaggedIntMatrix(length(Gorigin))
153+
Gsolvable[1:6] = Gorigin[1:6]
152154
eq = getSortedEquationGraph(Gorigin, Gsolvable, BLT, assign, Avar, Bequ, VNames)
153155
if printSortedEquations
154156
printSortedEquationGraph(eq)
@@ -157,7 +159,7 @@ vNames:
157159

158160

159161
println("\n\n... Test two coupled inertias (only a subset of unknowns can be solved for)")
160-
Gsolvable = Any[Any[3,7],Any[1],Any[6,9],Any[4],Any[2,5],Any[1,4],Any[7,9],[8,11],[10,12],[12,11]]
162+
Gsolvable = Any[Any[3,7],Any[1],Any[6,9],Any[4],Any[2,5],Any[1,4],Any[],[],[],[]]
161163
eq = getSortedEquationGraph(Gorigin, Gsolvable, BLT, assign, Avar, Bequ, VNames)
162164
if printSortedEquations
163165
printSortedEquationGraph(eq)
@@ -214,7 +216,7 @@ vNames:
214216
9: (9)
215217
=#
216218

217-
219+
println("\n\n... Test simple sliding mass model with Tearing")
218220
assign = [0,0,0,3,5,0,4,0,7,8,0,9]
219221
A = [6,8,10,0,0,7,0,9,0,0,12,0]
220222
B = [6,8,0,0,0,7,0,0,0]
@@ -235,6 +237,7 @@ vNames:
235237
[10,9],
236238
[12,11]]
237239

240+
#=
238241
Gsolvable = [[2],
239242
[3,8],
240243
[10,4,5],
@@ -244,6 +247,17 @@ vNames:
244247
[9],
245248
[10,9],
246249
[12]]
250+
=#
251+
252+
Gsolvable = [[2],
253+
[3,8],
254+
[10,4,5],
255+
[],
256+
[5],
257+
[],
258+
[],
259+
[],
260+
[12]]
247261

248262
eq = getSortedEquationGraph(G, Gsolvable, BLT, assign, A, B, VNames)
249263
if printSortedEquations
@@ -359,8 +373,9 @@ vNames:
359373

360374

361375
println("\n\n... Test Multi-Index DAE WITH tearing")
362-
Gsolvable = copy(G)
363-
Gsolvable[8] = fill(0, 0) # "0 = u8(t) + x8 - sin(x8)" cannot be solved for x8
376+
# Gsolvable = copy(G)
377+
# Gsolvable[8] = fill(0, 0) # "0 = u8(t) + x8 - sin(x8)" cannot be solved for x8
378+
Gsolvable[1:7] = G[1:7]
364379
eq = getSortedEquationGraph(G, Gsolvable, BLT, assign, A, B, VNames)
365380
if printSortedEquations
366381
printSortedEquationGraph(eq)

0 commit comments

Comments
 (0)