@@ -329,6 +329,7 @@ mutable struct SimulationModel{FloatType,TimeType}
329329
330330 parameters:: OrderedDict{Symbol,Any} # Parameters as provided to SimulationModel constructor
331331 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.
332333
333334 # Available after propagateEvaluateAndInstantiate!(..) called
334335 instantiateFunctions:: Vector{Tuple{Union{Expr,Symbol},OrderedDict{Symbol,Any},String}}
@@ -353,6 +354,7 @@ mutable struct SimulationModel{FloatType,TimeType}
353354 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!(..))
354355 der_x:: Vector{FloatType} # Derivatives of states x
355356
357+
356358 function SimulationModel {FloatType,TimeType} (modelModule, modelName, buildDict, getDerivatives!, equationInfo,
357359 previousVars, preVars, holdVars,
358360 parameterDefinition, timeName, w_invariant_names, hideResult_names;
@@ -408,6 +410,7 @@ mutable struct SimulationModel{FloatType,TimeType}
408410 instantiateResult = true
409411 newResultSegment = false
410412 parameters = deepcopy (parameterDefinition)
413+ x_terminate = FloatType[]
411414
412415 new (modelModule, modelName, buildDict, TimerOutputs. TimerOutput (), UInt64 (0 ), UInt64 (0 ), SimulationOptions {FloatType,TimeType} (), getDerivatives!,
413416 equationInfo, linearEquations, eventHandler,
@@ -418,7 +421,7 @@ mutable struct SimulationModel{FloatType,TimeType}
418421 isInitial, solve_leq, true , storeResult, convert (TimeType, 0 ), nGetDerivatives, nf,
419422 odeIntegrator, daeCopyInfo, algorithmName, sundials, addEventPointsDueToDEBug, success, unitless,
420423 string (timeName), w_invariant_names, hideResult_names, vEliminated, vProperty, var_name, result,
421- parameters, equationInfo)
424+ parameters, equationInfo, x_terminate )
422425 end
423426
424427#=
@@ -876,11 +879,11 @@ Return the names of the elements of the x-vector in a Vector{String}.
876879get_xNames (m:: SimulationModel ) = Modia. get_xNames (m. equationInfo)
877880
878881
879-
880882"""
881883 isInitial(instantiatedModel)
882884
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).
884887"""
885888isInitial (m:: SimulationModel ) = m. eventHandler. initial
886889initial ( m:: SimulationModel ) = m. eventHandler. initial
@@ -898,19 +901,20 @@ isFirstInitialOfAllSegments(m::SimulationModel) = m.eventHandler.firstInitialOfA
898901"""
899902 isTerminal(instantiatedModel)
900903
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).
902906"""
903907isTerminal (m:: SimulationModel ) = m. eventHandler. terminal
904908terminal ( m:: SimulationModel ) = m. eventHandler. terminal
905909
906910
907911"""
908- isTerminalOfAllSegmenteds (instantiatedModel)
912+ isTerminalOfAllSegments (instantiatedModel)
909913
910914Return true, if **terminal phase** of simulation of the **last segment**
911915of a segmented simulation.
912916"""
913- isTerminalOfAllSegmenteds (m:: SimulationModel ) = m. eventHandler. terminalOfAllSegments
917+ isTerminalOfAllSegments (m:: SimulationModel ) = m. eventHandler. terminalOfAllSegments
914918
915919
916920"""
@@ -1073,7 +1077,6 @@ function init!(m::SimulationModel{FloatType,TimeType})::Bool where {FloatType,Ti
10731077 if isnothing (evaluatedParameters)
10741078 return false
10751079 end
1076-
10771080 m. evaluatedParameters = evaluatedParameters
10781081 m. nextPrevious = deepcopy (m. previous)
10791082 m. nextPre = deepcopy (m. pre)
@@ -1122,7 +1125,7 @@ function init!(m::SimulationModel{FloatType,TimeType})::Bool where {FloatType,Ti
11221125 # xe_nominal = isnan(xe_info.nominal) ? "" : xe_info.nominal
11231126 push! (x_table, (xe_info. x_name, xe_init, xe_info. unit)) # , xe_nominal))
11241127 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 )
11261129 println (" \n " )
11271130 end
11281131
@@ -1171,9 +1174,10 @@ function initFullRestart!(m::SimulationModel{FloatType,TimeType})::Nothing where
11711174 logInstantiatedFunctionCalls = false
11721175 Core. eval (m. modelModule, :($ (fc[1 ])($ m, $ (fc[2 ]), $ (fc[3 ]), log= $ logInstantiatedFunctionCalls)))
11731176 end
1177+ resizeLinearEquations! (m, m. evaluatedParameters, m. options. log)
11741178
11751179 # Get initial state vector
1176- m. x_start = initialStateVector! (m. equationInfo, FloatType )
1180+ m. x_start = initialStateVector! (m)
11771181
11781182 # update equationInfo
11791183 x_info = m. equationInfo. x_info
@@ -1222,7 +1226,6 @@ function initFullRestart!(m::SimulationModel{FloatType,TimeType})::Nothing where
12221226 m. x_init[i] = deepcopy (m. x_start[i])
12231227 end
12241228 eventIteration! (m, m. x_init, m. options. startTime)
1225- m. success = false # is set to true at the first outputs! call.
12261229 eh. fullRestart = false
12271230 eh. initial = false
12281231 m. isInitial = false
@@ -1287,8 +1290,11 @@ function terminate!(m::SimulationModel, x, t)::Nothing
12871290 # println("... terminate! called at time = $t")
12881291 eh = m. eventHandler
12891292 eh. terminal = true
1293+ eh. terminalOfAllSegments = m. eventHandler. restart != Modia. FullRestart
12901294 invokelatest_getDerivatives_without_der_x! (x, m, t)
12911295 eh. terminal = false
1296+ eh. terminalOfAllSegments = false
1297+ m. x_terminate = deepcopy (x)
12921298 return nothing
12931299end
12941300
@@ -1886,7 +1892,7 @@ function initialStateVector!(m::SimulationModel{FloatType,TimeType})::Vector{Flo
18861892 # differential equation der_x[1] = -x[1], with state name _dummy_x
18871893 new_x_segmented_variable! (m, " _dummy_x" , " der(_dummy_x)" , FloatType (0 ))
18881894 end
1889- return initialStateVector! (m. equationInfo, FloatType)
1895+ return initialStateVector! (m. equationInfo, FloatType, ! isFullRestart (m), m . x_terminate )
18901896end
18911897
18921898
0 commit comments