-
Notifications
You must be signed in to change notification settings - Fork 24
/
sim.jl
557 lines (465 loc) · 22.7 KB
/
sim.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#
# Copyright (c) 2021 Tobias Thummerer, Lars Mikelsons, Josef Kircher
# Licensed under the MIT license. See LICENSE file in the project root for details.
#
using FMIImport.FMIBase.SciMLBase: solve, RightRootFind, ReturnCode
using FMIImport.FMIBase.DiffEqCallbacks: CallbackSet, SavedValues, copyat_or_push!
import LinearAlgebra: eigvals
import FMIImport.FMIBase.ProgressMeter
import FMIImport: prepareSolveFMU, finishSolveFMU
import FMIImport.FMIBase: setupODEProblem, setupCallbacks, setupSolver!, eval!
import FMIImport.FMIBase: getEmptyReal, getEmptyValueReference, isTrue, isStatusOK
import FMIImport.FMIBase: doStep
"""
simulate(fmu, instance=nothing, tspan=nothing; kwargs...)
simulate(fmu, tspan; kwargs...)
simulate(instance, tspan; kwargs...)
Starts a simulation of the `FMU2` for the instantiated type: CS, ME or SE (this is selected automatically or during loading of the FMU).
You can force a specific simulation mode by calling [`simulateCS`](@ref), [`simulateME`](@ref) or [`simulateSE`](@ref) directly.
# Arguments
- `fmu::FMU`: The FMU to be simulated.
- `c::Union{FMUInstance, Nothing}=nothing`: The instance (FMI3) or component (FMI2) of the FMU, `nothing` if not available.
- `tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing`: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)
# Keyword arguments
- `recordValues::fmi2ValueReferenceFormat` = nothing: Array of variables (Strings or variableIdentifiers) to record. Results are returned as `DiffEqCallbacks.SavedValues`
- `saveat = nothing`: Time points to save (interpolated) values at (default = nothing: save at each solver timestep)
- `setup::Bool`: call fmi2SetupExperiment, fmi2EnterInitializationMode and fmi2ExitInitializationMode before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `reset::Bool`: call fmi2Reset before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `instantiate::Bool`: call fmi2Instantiate! before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `freeInstance::Bool`: call fmi2FreeInstance after each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `terminate::Bool`: call fmi2Terminate after each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `inputValueReferences::fmi2ValueReferenceFormat = nothing`: Input variables (Strings or variableIdentifiers) to set at each simulation step
- `inputFunction = nothing`: Function to get values for the input variables at each simulation step.
- `parameters::Union{Dict{<:Any, <:Any}, Nothing} = nothing`: Dict of parameter variables (strings or variableIdentifiers) and values (Real, Integer, Boolean, String) to set parameters during initialization
- `showProgress::Bool = true`: print simulation progress meter in REPL
## Input function pattern
[`c`: current component, `u`: current state ,`t`: current time, returning array of values to be passed to `fmi2SetReal(..., inputValueReferences, inputFunction(...))` or `fmi3SetFloat64`]:
- `inputFunction(t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, u::AbstractVector{<:Real})`
- `inputFunction(x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})`
# Returns:
- A [`FMUSolution`](@ref) struct.
See also [`simulate`](@ref), [`simulateME`](@ref), [`simulateCS`](@ref), [`simulateSE`](@ref).
"""
function simulate(
fmu::FMU2,
c::Union{FMU2Component,Nothing} = nothing,
tspan::Union{Tuple{Float64,Float64},Nothing} = nothing;
kwargs...,
)
if fmu.type == fmi2TypeCoSimulation
return simulateCS(fmu, c, tspan; kwargs...)
elseif fmu.type == fmi2TypeModelExchange
return simulateME(fmu, c, tspan; kwargs...)
else
error(unknownFMUType)
end
end
function simulate(
fmu::FMU3,
c::Union{FMU3Instance,Nothing} = nothing,
tspan::Union{Tuple{Float64,Float64},Nothing} = nothing;
kwargs...,
)
if fmu.type == fmi3TypeCoSimulation
return simulateCS(fmu, c, tspan; kwargs...)
elseif fmu.type == fmi3TypeModelExchange
return simulateME(fmu, c, tspan; kwargs...)
elseif fmu.type == fmi3TypeScheduledExecution
return simulateSE(fmu, c, tspan; kwargs...)
else
error(unknownFMUType)
end
end
simulate(c::FMUInstance, tspan::Tuple{Float64,Float64}; kwargs...) =
simulate(c.fmu, c, tspan; kwargs...)
simulate(fmu::FMU, tspan::Tuple{Float64,Float64}; kwargs...) =
simulate(fmu, nothing, tspan; kwargs...)
export simulate
"""
simulateME(fmu, instance=nothing, tspan=nothing; kwargs...)
simulateME(fmu, tspan; kwargs...)
simulateME(instance, tspan; kwargs...)
Simulate ME-FMU for the given simulation time interval.
State- and Time-Events are handled correctly.
# Arguments
- `fmu::FMU`: The FMU to be simulated.
- `c::Union{FMUInstance, Nothing}=nothing`: The instance (FMI3) or component (FMI2) of the FMU, `nothing` if not available.
- `tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing`: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)
# Keyword arguments
- `solver = nothing`: Any Julia-supported ODE-solver (default = nothing: use DifferentialEquations.jl default solver)
- `recordValues::fmi2ValueReferenceFormat` = nothing: Array of variables (Strings or variableIdentifiers) to record. Results are returned as `DiffEqCallbacks.SavedValues`
- `recordEventIndicators::Union{AbstractArray{<:Integer, 1}, UnitRange{<:Integer}, Nothing} = nothing`: Array or Range of event indicators to record
- `recordEigenvalues::Bool=false`: compute and record eigenvalues
- `saveat = nothing`: Time points to save (interpolated) values at (default = nothing: save at each solver timestep)
- `x0::Union{AbstractArray{<:Real}, Nothing} = nothing`: initial fmu State (default = nothing: use current or default-initial fmu state)
- `setup::Bool`: call fmi2SetupExperiment, fmi2EnterInitializationMode and fmi2ExitInitializationMode before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `reset::Bool`: call fmi2Reset before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `instantiate::Bool`: call fmi2Instantiate! before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `freeInstance::Bool`: call fmi2FreeInstance after each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `terminate::Bool`: call fmi2Terminate after each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `inputValueReferences::fmi2ValueReferenceFormat = nothing`: Input variables (Strings or variableIdentifiers) to set at each simulation step
- `inputFunction = nothing`: Function to get values for the input variables at each simulation step.
- `parameters::Union{Dict{<:Any, <:Any}, Nothing} = nothing`: Dict of parameter variables (strings or variableIdentifiers) and values (Real, Integer, Boolean, String) to set parameters during initialization
- `callbacksBefore = []`: callbacks to call *before* the internal callbacks for state- and time-events are called
- `callbacksAfter = []`: callbacks to call *after* the internal callbacks for state- and time-events are called
- `showProgress::Bool = true`: print simulation progress meter in REPL
- `solveKwargs...`: keyword arguments that get passed onto the solvers solve call
## Input function pattern
[`c`: current component, `u`: current state ,`t`: current time, returning array of values to be passed to `fmi2SetReal(..., inputValueReferences, inputFunction(...))` or `fmi3SetFloat64`]:
- `inputFunction(t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, u::AbstractVector{<:Real})`
- `inputFunction(x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})`
# Returns:
- A [`FMUSolution`](@ref) struct.
See also [`simulate`](@ref), [`simulateCS`](@ref), [`simulateSE`](@ref).
"""
function simulateME(
fmu::FMU,
c::Union{FMUInstance,Nothing},
tspan::Union{Tuple{Float64,Float64},Nothing} = nothing;
solver = nothing, # [ToDo] type
recordValues::fmi2ValueReferenceFormat = nothing,
recordEventIndicators::Union{AbstractArray{<:Integer,1},UnitRange{<:Integer},Nothing} = nothing,
recordEigenvalues::Bool = false,
saveat = nothing, # [ToDo] type
x0::Union{AbstractArray{<:Real},Nothing} = nothing,
setup::Bool = fmu.executionConfig.setup,
reset::Bool = fmu.executionConfig.reset,
instantiate::Bool = fmu.executionConfig.instantiate,
freeInstance::Bool = fmu.executionConfig.freeInstance,
terminate::Bool = fmu.executionConfig.terminate,
inputValueReferences::fmi2ValueReferenceFormat = nothing,
inputFunction = nothing,
parameters::Union{Dict{<:Any,<:Any},Nothing} = nothing,
callbacksBefore::AbstractVector = [], # [ToDo] type
callbacksAfter::AbstractVector = [], # [ToDo] type
showProgress::Bool = true,
solveKwargs...,
)
@assert isModelExchange(fmu) "simulateME(...): This function supports Model Exchange FMUs only."
recordValues = prepareValueReference(fmu, recordValues)
inputValueReferences = prepareValueReference(fmu, inputValueReferences)
hasInputs = length(inputValueReferences) > 0
solveKwargs = Dict{Symbol,Any}(solveKwargs...)
tspan = setupSolver!(fmu, tspan, solveKwargs)
t_start, t_stop = tspan
if !isnothing(saveat)
solveKwargs[:saveat] = saveat
end
progressMeter = nothing
if showProgress
progressMeter = ProgressMeter.Progress(
1000;
desc = "Simulating ME-FMU ...",
color = :blue,
dt = 1.0,
) #, barglyphs=ProgressMeter.BarGlyphs("[=> ]"))
ProgressMeter.update!(progressMeter, 0) # show it!
end
# input function handling
_inputFunction = nothing
if !isnothing(inputFunction)
_inputFunction = FMUInputFunction(inputFunction, inputValueReferences)
end
inputs = nothing
if hasInputs
inputValues = eval!(_inputFunction, nothing, nothing, t_start)
inputs = Dict(inputValueReferences .=> inputValues)
end
c, x0 = prepareSolveFMU(
fmu,
c,
:ME;
parameters = parameters,
t_start = t_start,
t_stop = t_stop,
x0 = x0,
inputs = inputs,
)
# Zero state FMU: add dummy state
if c.fmu.isZeroState
x0 = [0.0]
end
@assert !isnothing(x0) "x0 is nothing after prepare!"
c.problem = setupODEProblem(c, x0, tspan; inputFunction = _inputFunction)
cbs = setupCallbacks(
c,
recordValues,
recordEventIndicators,
recordEigenvalues,
_inputFunction,
inputValueReferences,
progressMeter,
t_start,
t_stop,
saveat,
)
#solveKwargs = Dict(solveKwargs...)
#setupSolver(fmu, solveKwargs)
for cb in callbacksBefore
insertAt!(cbs, cb, 1)
end
for cb in callbacksAfter
push!(cbs, cb)
end
# from here on, we are in event mode, if `setup=false` this is the job of the user
#@assert c.state == fmi2ComponentStateEventMode "FMU needs to be in event mode after setup."
# if x0 === nothing
# x0 = fmi2GetContinuousStates(c)
# x0_nom = fmi2GetNominalsOfContinuousStates(c)
# end
# initial event handling
#handleEvents(c)
#fmi2EnterContinuousTimeMode(c)
# callback functions
if isnothing(solver)
c.solution.states = solve(c.problem; callback = CallbackSet(cbs...), solveKwargs...)
else
c.solution.states =
solve(c.problem, solver; callback = CallbackSet(cbs...), solveKwargs...)
end
c.solution.success = (c.solution.states.retcode == ReturnCode.Success)
if !c.solution.success
logWarning(
fmu,
"FMU simulation failed with solver return code `$(c.solution.states.retcode)`, please check log for hints.",
)
end
# ZeroStateFMU: remove dummy state
if c.fmu.isZeroState
c.solution.states = nothing
end
# cleanup progress meter
if showProgress
ProgressMeter.finish!(progressMeter)
end
finishSolveFMU(fmu, c; freeInstance = freeInstance, terminate = terminate)
return c.solution
end
simulateME(c::FMUInstance, tspan::Tuple{Float64,Float64}; kwargs...) =
simulateME(c.fmu, c, tspan; kwargs...)
simulateME(fmu::FMU, tspan::Tuple{Float64,Float64}; kwargs...) =
simulateME(fmu, nothing, tspan; kwargs...)
export simulateME
############ Co-Simulation ############
"""
simulateCS(fmu, instance=nothing, tspan=nothing; kwargs...)
simulateCS(fmu, tspan; kwargs...)
simulateCS(instance, tspan; kwargs...)
Simulate CS-FMU for the given simulation time interval.
State- and Time-Events are handled internally by the FMU.
# Arguments
- `fmu::FMU`: The FMU to be simulated.
- `c::Union{FMUInstance, Nothing}=nothing`: The instance (FMI3) or component (FMI2) of the FMU, `nothing` if not available.
- `tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing`: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)
# Keyword arguments
- `tolerance::Union{Real, Nothing} = nothing`: The tolerance for the internal FMU solver.
- `recordValues::fmi2ValueReferenceFormat` = nothing: Array of variables (Strings or variableIdentifiers) to record. Results are returned as `DiffEqCallbacks.SavedValues`
- `saveat = nothing`: Time points to save (interpolated) values at (default = nothing: save at each solver timestep)
- `setup::Bool`: call fmi2SetupExperiment, fmi2EnterInitializationMode and fmi2ExitInitializationMode before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `reset::Bool`: call fmi2Reset before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `instantiate::Bool`: call fmi2Instantiate! before each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `freeInstance::Bool`: call fmi2FreeInstance after each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `terminate::Bool`: call fmi2Terminate after each step (default = nothing: use value from `fmu`'s `FMUExecutionConfiguration`)
- `inputValueReferences::fmi2ValueReferenceFormat = nothing`: Input variables (Strings or variableIdentifiers) to set at each simulation step
- `inputFunction = nothing`: Function to get values for the input variables at each simulation step.
- `parameters::Union{Dict{<:Any, <:Any}, Nothing} = nothing`: Dict of parameter variables (strings or variableIdentifiers) and values (Real, Integer, Boolean, String) to set parameters during initialization
- `showProgress::Bool = true`: print simulation progress meter in REPL
## Input function pattern
[`c`: current component, `u`: current state ,`t`: current time, returning array of values to be passed to `fmi2SetReal(..., inputValueReferences, inputFunction(...))` or `fmi3SetFloat64`]:
- `inputFunction(t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, u::AbstractVector{<:Real})`
- `inputFunction(x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})`
- `inputFunction(c::Union{FMUInstance, Nothing}, x::AbstractVector{<:Real}, t::Real, u::AbstractVector{<:Real})`
# Returns:
- A [`FMUSolution`](@ref) struct.
See also [`simulate`](@ref), [`simulateME`](@ref), [`simulateSE`](@ref).
"""
function simulateCS(
fmu::FMU,
c::Union{FMUInstance,Nothing},
tspan::Union{Tuple{Float64,Float64},Nothing} = nothing;
tolerance::Union{Real,Nothing} = nothing,
dt::Union{Real,Nothing} = nothing,
recordValues::fmi2ValueReferenceFormat = nothing,
saveat = [],
setup::Bool = fmu.executionConfig.setup,
reset::Bool = fmu.executionConfig.reset,
instantiate::Bool = fmu.executionConfig.instantiate,
freeInstance::Bool = fmu.executionConfig.freeInstance,
terminate::Bool = fmu.executionConfig.terminate,
inputValueReferences::fmiValueReferenceFormat = nothing,
inputFunction = nothing,
showProgress::Bool = true,
parameters::Union{Dict{<:Any,<:Any},Nothing} = nothing,
)
@assert isCoSimulation(fmu) "simulateCS(...): This function supports Co-Simulation FMUs only."
# input function handling
@debug "Simulating CS-FMU: Preparing input function ..."
inputValueReferences = prepareValueReference(fmu, inputValueReferences)
hasInputs = (length(inputValueReferences) > 0)
_inputFunction = nothing
u = getEmptyReal(fmu)
u_refs = getEmptyValueReference(fmu)
if hasInputs
_inputFunction = FMUInputFunction(inputFunction, inputValueReferences)
u_refs = _inputFunction.vrs
end
# outputs
@debug "Simulating CS-FMU: Preparing outputs ..."
y_refs = getEmptyValueReference(fmu)
y = getEmptyReal(fmu)
if !isnothing(recordValues)
y_refs = prepareValueReference(fmu, recordValues)
y = zeros(fmi2Real, length(y_refs))
end
t_start, t_stop = (tspan == nothing ? (nothing, nothing) : tspan)
# pull default values from the model description - if not given by user
@debug "Simulating CS-FMU: Pulling default values ..."
variableSteps =
isCoSimulation(fmu) &&
isTrue(fmu.modelDescription.coSimulation.canHandleVariableCommunicationStepSize)
t_start = t_start === nothing ? getDefaultStartTime(fmu.modelDescription) : t_start
t_start = t_start === nothing ? 0.0 : t_start
t_stop = t_stop === nothing ? getDefaultStopTime(fmu.modelDescription) : t_stop
t_stop = t_stop === nothing ? 1.0 : t_stop
tolerance =
tolerance === nothing ? getDefaultTolerance(fmu.modelDescription) : tolerance
tolerance = tolerance === nothing ? 0.0 : tolerance
dt = dt === nothing ? getDefaultStepSize(fmu.modelDescription) : dt
dt = dt === nothing ? 1e-3 : dt
@debug "Simulating CS-FMU: Preparing inputs ..."
inputs = nothing
if hasInputs
inputValues = eval!(_inputFunction, nothing, nothing, t_start)
inputs = Dict(inputValueReferences .=> inputValues)
end
@debug "Simulating CS-FMU: Preparing solve ..."
c, _ = prepareSolveFMU(
fmu,
c,
:CS;
instantiate = instantiate,
freeInstance = freeInstance,
terminate = terminate,
reset = reset,
setup = setup,
parameters = parameters,
t_start = t_start,
t_stop = t_stop,
tolerance = tolerance,
inputs = inputs,
)
fmusol = c.solution
# default setup
if length(saveat) == 0
saveat = t_start:dt:t_stop
end
# setup if no variable steps
if variableSteps == false
if length(saveat) >= 2
dt = saveat[2] - saveat[1]
end
end
t = t_start
progressMeter = nothing
if showProgress
progressMeter =
ProgressMeter.Progress(1000; desc = "Sim. CS-FMU ...", color = :blue, dt = 1.0)
ProgressMeter.update!(progressMeter, 0) # show it!
end
first_step = true
fmusol.values =
SavedValues(Float64, Tuple{collect(Float64 for i = 1:length(y_refs))...})
fmusol.valueReferences = copy(y_refs)
i = 1
fmusol.success = true
@debug "Starting simulation from $(t_start) to $(t_stop), variable steps: $(variableSteps)"
while t < t_stop
if variableSteps
if length(saveat) > (i + 1)
dt = saveat[i+1] - saveat[i]
else
dt = t_stop - t
end
end
if !first_step
ret = doStep(c, dt; currentCommunicationPoint = t)
if !isStatusOK(fmu, ret)
fmusol.success = false
end
t = t + dt
i += 1
else
first_step = false
end
if hasInputs
u = eval!(_inputFunction, c, nothing, t)
end
c(u = u, u_refs = u_refs, y = y, y_refs = y_refs)
svalues = (y...,)
copyat_or_push!(fmusol.values.t, i, t)
copyat_or_push!(fmusol.values.saveval, i, svalues, Val{false})
if !isnothing(progressMeter)
ProgressMeter.update!(
progressMeter,
floor(Integer, 1000.0 * (t - t_start) / (t_stop - t_start)),
)
end
end
if !fmusol.success
logWarning(fmu, "FMU simulation failed, please check log for hints.")
end
if !isnothing(progressMeter)
ProgressMeter.finish!(progressMeter)
end
finishSolveFMU(fmu, c; freeInstance = freeInstance, terminate = terminate)
return fmusol
end
simulateCS(c::FMUInstance, tspan::Tuple{Float64,Float64}; kwargs...) =
simulateCS(c.fmu, c, tspan; kwargs...)
simulateCS(fmu::FMU, tspan::Tuple{Float64,Float64}; kwargs...) =
simulateCS(fmu, nothing, tspan; kwargs...)
export simulateCS
# [TODO] simulate ScheduledExecution
"""
simulateSE(fmu, instance=nothing, tspan=nothing; kwargs...)
simulateSE(fmu, tspan; kwargs...)
simulateSE(instance, tspan; kwargs...)
To be implemented ...
# Arguments
- `fmu::FMU3`: The FMU to be simulated. Note: SE is only available in FMI3.
- `c::Union{FMU3Instance, Nothing}=nothing`: The instance (FMI3) of the FMU, `nothing` if not available.
- `tspan::Union{Tuple{Float64, Float64}, Nothing}=nothing`: Simulation-time-span as tuple (default = nothing: use default value from FMU's model description or (0.0, 1.0) if not specified)
# Keyword arguments
- To be implemented ...
# Returns:
- A [`FMUSolution`](@ref) struct.
See also [`simulate`](@ref), [`simulateME`](@ref), [`simulateCS`](@ref).
"""
function simulateSE(
fmu::FMU2,
c::Union{FMU2Component,Nothing},
tspan::Union{Tuple{Float64,Float64},Nothing} = nothing,
)
@assert false "This is a FMI2-FMU, scheduled execution is not supported in FMI2."
end
function simulateSE(
fmu::FMU3,
c::Union{FMU3Instance,Nothing},
tspan::Union{Tuple{Float64,Float64},Nothing} = nothing,
)
# [ToDo]
@assert false "Not implemented yet. Please open an issue if this is needed."
end
simulateSE(c::FMUInstance, tspan::Tuple{Float64,Float64}; kwargs...) =
simulateSE(c.fmu, c, tspan; kwargs...)
simulateSE(fmu::FMU, tspan::Tuple{Float64,Float64}; kwargs...) =
simulateSE(fmu, nothing, tspan; kwargs...)
export simulateSE