@@ -46,6 +46,7 @@ const ERROR = 2
46
46
var_has_start = v_original -> false,
47
47
var_fixed = v_original -> nothing,
48
48
var_length = v_original -> 1,
49
+ var_is_state = v_original -> false,
49
50
equation = e -> ""
50
51
isSolvableEquation = (e_original,v_original) -> false,
51
52
isLinearEquation = (e_original,v_original) -> (false, false),
@@ -78,6 +79,9 @@ The functions need to have the following arguments:
78
79
79
80
- `var_length(v_original::Int)::Int`:\\
80
81
Return length of variable `v_original` from the original, undifferentiated equations.
82
+
83
+ - `var_is_state(v_original::Int)::Bool`:\\
84
+ Return true, if variable `v_original` is defined to be a state.
81
85
82
86
- `equation(e::Int)`:\\
83
87
Return equation as string or "", if equation is not provided.
@@ -113,6 +117,7 @@ struct StateSelectionFunctions
113
117
var_nominal:: Function
114
118
var_unbounded:: Function
115
119
var_length:: Function
120
+ var_is_state:: Function
116
121
equation:: Function
117
122
isSolvableEquation:: Function
118
123
isLinearEquation:: Function
@@ -129,6 +134,7 @@ struct StateSelectionFunctions
129
134
var_nominal = v_original -> NaN ,
130
135
var_unbounded = v_original -> false ,
131
136
var_length = v_original -> 1 ,
137
+ var_is_state = v_original -> false ,
132
138
equation = e -> " " ,
133
139
isSolvableEquation = (e_original,v_original) -> false ,
134
140
isLinearEquation = (e_original,v_original) -> (false , false ),
@@ -144,6 +150,7 @@ struct StateSelectionFunctions
144
150
var_nominal,
145
151
var_unbounded,
146
152
var_length,
153
+ var_is_state,
147
154
equation,
148
155
isSolvableEquation,
149
156
isLinearEquation,
@@ -207,7 +214,8 @@ showCodeWithoutComments(code) = println("code = ", replace(sprint(show,code), r"
207
214
208
215
209
216
"""
210
- (eConstraints,vConstraints) = getConstraintSets(BLT_i, assignRev, Arev, Brev, showMessage)
217
+ (eConstraints,vConstraints) = getConstraintSets(BLT_i, assignRev, Arev, Brev,
218
+ var_is_state, showMessage)
211
219
212
220
Determines the set of equations/constraints and their unknowns that are associated with
213
221
the highest derivative level BLT block i (BLT_i), according to the dummy derivative
@@ -231,13 +239,16 @@ vConstraints[j] are the unknowns of eConstraints[j].
231
239
232
240
- `Brev::Vector{Int}`: Reverted equation association: `Erev[i] = der(E[k]) == E[i] ? k : 0`.
233
241
242
+ - `var_is_state::Function`: var_is_state(`vi`) returns true, if `vi` is defined to be a definite state.
243
+
234
244
- showMessage: showMessage2 below to print error/warning message
235
245
236
246
"""
237
247
function getConstraintSets (BLT_i:: Vector{Int} ,
238
248
assignRev:: Vector{Int} ,
239
249
Arev:: Vector{Int} ,
240
250
Brev:: Vector{Int} ,
251
+ var_is_state:: Function ,
241
252
showMessage:: Function )
242
253
# Determine unknowns of BLT block i
243
254
BLT_i_unknowns = fill (0 , length (BLT_i))
@@ -271,13 +282,25 @@ function getConstraintSets(BLT_i::Vector{Int},
271
282
pushfirst! (eConstraints, ceq) # move ceq to the beginning of the constraints vector
272
283
273
284
# Determine unknowns of ceq
274
- veq = fill (0 , 0 )
285
+ veq = fill (0 ,0 )
286
+ veq_states = fill (0 ,0 )
275
287
for vc in vConstraints[1 ]
276
288
if Arev[vc] > 0
277
- push! (veq, Arev[vc])
289
+ if var_is_state ( Arev[vc] )
290
+ push! (veq_states, Arev[vc])
291
+ else
292
+ push! (veq, Arev[vc])
293
+ end
278
294
end
279
295
end
280
- @assert ( length (veq) > 0 )
296
+ if length (veq) < length (ceq)
297
+ nRemoveStates = length (veq_states) - (length (ceq) - length (veq))
298
+ showMessage (" $nRemoveStates of the followig defined states cannot be states!" ;
299
+ severity = ERROR,
300
+ variables = veq_states,
301
+ equations = ceq)
302
+ return (eConstraints, vConstraints, false )
303
+ end
281
304
pushfirst! (vConstraints, veq) # move veq to the beginning of the constraints vector
282
305
end
283
306
@@ -479,7 +502,12 @@ mutable struct EquationGraph
479
502
480
503
# Initialize the active variables
481
504
vActive = fill (true , length (A))
482
-
505
+ for v = 1 : length (vActive)
506
+ if Arev[v] > 0
507
+ vActive[ Arev[v] ] = false # Arev[v] is a potential state
508
+ end
509
+ end
510
+
483
511
# Define empty constraint sets
484
512
eConstraintsVec = Vector{Vector{Int}}[]
485
513
vConstraintsVec = Vector{Vector{Int}}[]
@@ -521,20 +549,21 @@ mutable struct EquationGraph
521
549
if highest
522
550
# Get all equation sets eConstraints and their corresponding unknowns vConstraints
523
551
# from lowest to highest differentiation order (eConstraints[end] is c)
524
- (eConstraints, vConstraints, success2) = getConstraintSets (BLT_i, assignRev, Arev, Brev, showMessage)
552
+ (eConstraints, vConstraints, success2) = getConstraintSets (BLT_i, assignRev, Arev, Brev,
553
+ stateSelectionFunctions. var_is_state, showMessage)
525
554
if ! success2
526
555
success= false
527
556
break
528
557
end
529
558
530
559
# Initialize vActive
531
- for vc in vConstraints
532
- for v in vc
533
- if Arev[v] > 0
534
- vActive[ Arev[v] ] = false # Arev[v] is a potential state
535
- end
536
- end
537
- end
560
+ # for vc in vConstraints
561
+ # for v in vc
562
+ # if Arev[v] > 0
563
+ # vActive[ Arev[v] ] = false # Arev[v] is a potential state
564
+ # end
565
+ # end
566
+ # end
538
567
539
568
push! (eConstraintsVec, eConstraints)
540
569
push! (vConstraintsVec, vConstraints)
@@ -963,7 +992,7 @@ function sortEquations!(eq::EquationGraph)::Nothing
963
992
# Matching of all equations
964
993
assign = matching (eq. G, length (eq. A), eq. vActive)
965
994
assignRev = revertAssociation (assign)
966
-
995
+
967
996
# Sort equations and find strong components
968
997
blt = BLT (eq. G,assign)
969
998
@@ -1160,6 +1189,7 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1160
1189
stateSelectionFunctions:: StateSelectionFunctions ;
1161
1190
log:: Bool = false ,
1162
1191
logDetails:: Bool = false ,
1192
+ logStates:: Bool = false ,
1163
1193
modelName = " ???" ,
1164
1194
unitless:: Bool = false ,
1165
1195
defaultParameterAndStartValues = nothing )
@@ -1270,16 +1300,29 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1270
1300
1271
1301
# Select dummy states
1272
1302
if i < length (eConstraints)
1273
- # Not on highest derivative level. Solved variables are dummy states.
1303
+ # Not on highest derivative level. Solved variables are dummy states
1274
1304
for v in eq. vSolved
1275
1305
@assert (! eq. vActive[v])
1276
1306
eq. vActive[v] = true
1277
- end
1278
- if log
1279
- println (" All other unknowns are solved and are dummy states." )
1307
+ end
1308
+
1309
+ if length (eConstraints[i]) == length (vConstraints[i])
1310
+ # N equations in N unknowns - tearing variables are dummy states
1311
+ for v in eq. vTear
1312
+ @assert (! eq. vActive[v])
1313
+ eq. vActive[v] = true
1314
+ end
1315
+ if log
1316
+ println (" All unknowns are dummy states." )
1317
+ end
1318
+
1319
+ else
1320
+ if log
1321
+ println (" All solved unknowns are dummy states." )
1322
+ end
1280
1323
end
1281
1324
elseif log
1282
- println (" All other unknowns are solved." )
1325
+ println (" All unknowns are solved." )
1283
1326
end
1284
1327
1285
1328
# Further actions depending on type of equations
@@ -1379,7 +1422,7 @@ function getSortedAndSolvedAST(G, # Typically ::Vector{Vector{Int}}
1379
1422
v_stateCategory, v_length, v_unit, v_fixed2,
1380
1423
v_nominal, v_unbounded))
1381
1424
end
1382
- if log
1425
+ if log || logStates
1383
1426
println (" \n Selected ODE states: " )
1384
1427
x_table = ModiaBase. get_x_table (x_info)
1385
1428
show (stdout , x_table; allrows= true , allcols= true , summary= false , eltypes= false )
0 commit comments