@@ -329,6 +329,7 @@ mutable struct SimulationModel{FloatType,TimeType}
329
329
330
330
parameters:: OrderedDict{Symbol,Any} # Parameters as provided to SimulationModel constructor
331
331
equationInfo:: Modia.EquationInfo # Invariant part of equations are available
332
+ x_terminate:: Vector{FloatType} # States x used at the last terminate!(..) call or [], if terminate!(..) not yet called.
332
333
333
334
# Available after propagateEvaluateAndInstantiate!(..) called
334
335
instantiateFunctions:: Vector{Tuple{Union{Expr,Symbol},OrderedDict{Symbol,Any},String}}
@@ -353,6 +354,7 @@ mutable struct SimulationModel{FloatType,TimeType}
353
354
der_x_segmented:: Vector{FloatType} # Derivatives of states x or x_init that correspond to segmented states (defined in functions and not visible in getDerivatives!(..))
354
355
der_x:: Vector{FloatType} # Derivatives of states x
355
356
357
+
356
358
function SimulationModel {FloatType,TimeType} (modelModule, modelName, buildDict, getDerivatives!, equationInfo,
357
359
previousVars, preVars, holdVars,
358
360
parameterDefinition, timeName, w_invariant_names, hideResult_names;
@@ -408,6 +410,7 @@ mutable struct SimulationModel{FloatType,TimeType}
408
410
instantiateResult = true
409
411
newResultSegment = false
410
412
parameters = deepcopy (parameterDefinition)
413
+ x_terminate = FloatType[]
411
414
412
415
new (modelModule, modelName, buildDict, TimerOutputs. TimerOutput (), UInt64 (0 ), UInt64 (0 ), SimulationOptions {FloatType,TimeType} (), getDerivatives!,
413
416
equationInfo, linearEquations, eventHandler,
@@ -418,7 +421,7 @@ mutable struct SimulationModel{FloatType,TimeType}
418
421
isInitial, solve_leq, true , storeResult, convert (TimeType, 0 ), nGetDerivatives, nf,
419
422
odeIntegrator, daeCopyInfo, algorithmName, sundials, addEventPointsDueToDEBug, success, unitless,
420
423
string (timeName), w_invariant_names, hideResult_names, vEliminated, vProperty, var_name, result,
421
- parameters, equationInfo)
424
+ parameters, equationInfo, x_terminate )
422
425
end
423
426
424
427
#=
@@ -876,11 +879,11 @@ Return the names of the elements of the x-vector in a Vector{String}.
876
879
get_xNames (m:: SimulationModel ) = Modia. get_xNames (m. equationInfo)
877
880
878
881
879
-
880
882
"""
881
883
isInitial(instantiatedModel)
882
884
883
- Return true, if **initialization phase** of simulation.
885
+ Return true, if **initialization phase** of simulation
886
+ (of the current segment of a segmented simulation).
884
887
"""
885
888
isInitial (m:: SimulationModel ) = m. eventHandler. initial
886
889
initial ( m:: SimulationModel ) = m. eventHandler. initial
@@ -898,19 +901,20 @@ isFirstInitialOfAllSegments(m::SimulationModel) = m.eventHandler.firstInitialOfA
898
901
"""
899
902
isTerminal(instantiatedModel)
900
903
901
- Return true, if **terminal phase** of simulation.
904
+ Return true, if **terminal phase** of simulation
905
+ (of the current segment of a segmented simulation).
902
906
"""
903
907
isTerminal (m:: SimulationModel ) = m. eventHandler. terminal
904
908
terminal ( m:: SimulationModel ) = m. eventHandler. terminal
905
909
906
910
907
911
"""
908
- isTerminalOfAllSegmenteds (instantiatedModel)
912
+ isTerminalOfAllSegments (instantiatedModel)
909
913
910
914
Return true, if **terminal phase** of simulation of the **last segment**
911
915
of a segmented simulation.
912
916
"""
913
- isTerminalOfAllSegmenteds (m:: SimulationModel ) = m. eventHandler. terminalOfAllSegments
917
+ isTerminalOfAllSegments (m:: SimulationModel ) = m. eventHandler. terminalOfAllSegments
914
918
915
919
916
920
"""
@@ -1073,7 +1077,6 @@ function init!(m::SimulationModel{FloatType,TimeType})::Bool where {FloatType,Ti
1073
1077
if isnothing (evaluatedParameters)
1074
1078
return false
1075
1079
end
1076
-
1077
1080
m. evaluatedParameters = evaluatedParameters
1078
1081
m. nextPrevious = deepcopy (m. previous)
1079
1082
m. nextPre = deepcopy (m. pre)
@@ -1122,7 +1125,7 @@ function init!(m::SimulationModel{FloatType,TimeType})::Bool where {FloatType,Ti
1122
1125
# xe_nominal = isnan(xe_info.nominal) ? "" : xe_info.nominal
1123
1126
push! (x_table, (xe_info. x_name, xe_init, xe_info. unit)) # , xe_nominal))
1124
1127
end
1125
- show (stdout , x_table; allrows= true , allcols= true , rowlabel = Symbol (" #" ), summary= false , eltypes= false )
1128
+ show (stdout , x_table; allrows= true , allcols= true , rowlabel = Symbol (" #" ), summary= false , eltypes= false , truncate = 60 )
1126
1129
println (" \n " )
1127
1130
end
1128
1131
@@ -1171,9 +1174,10 @@ function initFullRestart!(m::SimulationModel{FloatType,TimeType})::Nothing where
1171
1174
logInstantiatedFunctionCalls = false
1172
1175
Core. eval (m. modelModule, :($ (fc[1 ])($ m, $ (fc[2 ]), $ (fc[3 ]), log= $ logInstantiatedFunctionCalls)))
1173
1176
end
1177
+ resizeLinearEquations! (m, m. evaluatedParameters, m. options. log)
1174
1178
1175
1179
# Get initial state vector
1176
- m. x_start = initialStateVector! (m. equationInfo, FloatType )
1180
+ m. x_start = initialStateVector! (m)
1177
1181
1178
1182
# update equationInfo
1179
1183
x_info = m. equationInfo. x_info
@@ -1222,7 +1226,6 @@ function initFullRestart!(m::SimulationModel{FloatType,TimeType})::Nothing where
1222
1226
m. x_init[i] = deepcopy (m. x_start[i])
1223
1227
end
1224
1228
eventIteration! (m, m. x_init, m. options. startTime)
1225
- m. success = false # is set to true at the first outputs! call.
1226
1229
eh. fullRestart = false
1227
1230
eh. initial = false
1228
1231
m. isInitial = false
@@ -1287,8 +1290,11 @@ function terminate!(m::SimulationModel, x, t)::Nothing
1287
1290
# println("... terminate! called at time = $t")
1288
1291
eh = m. eventHandler
1289
1292
eh. terminal = true
1293
+ eh. terminalOfAllSegments = m. eventHandler. restart != Modia. FullRestart
1290
1294
invokelatest_getDerivatives_without_der_x! (x, m, t)
1291
1295
eh. terminal = false
1296
+ eh. terminalOfAllSegments = false
1297
+ m. x_terminate = deepcopy (x)
1292
1298
return nothing
1293
1299
end
1294
1300
@@ -1886,7 +1892,7 @@ function initialStateVector!(m::SimulationModel{FloatType,TimeType})::Vector{Flo
1886
1892
# differential equation der_x[1] = -x[1], with state name _dummy_x
1887
1893
new_x_segmented_variable! (m, " _dummy_x" , " der(_dummy_x)" , FloatType (0 ))
1888
1894
end
1889
- return initialStateVector! (m. equationInfo, FloatType)
1895
+ return initialStateVector! (m. equationInfo, FloatType, ! isFullRestart (m), m . x_terminate )
1890
1896
end
1891
1897
1892
1898
0 commit comments