-
Notifications
You must be signed in to change notification settings - Fork 17
/
prep.jl
262 lines (223 loc) · 8.9 KB
/
prep.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
#
# Copyright (c) 2021 Tobias Thummerer, Lars Mikelsons, Josef Kircher
# Licensed under the MIT license. See LICENSE file in the project root for details.
#
using FMIBase.FMICore: getAttributes, fmi2ScalarVariable
using FMIBase: handleEvents
import FMIImport: fmi2VariabilityConstant, fmi2InitialApprox, fmi2InitialExact
function setBeforeInitialization(mv::fmi2ScalarVariable)
causality, variability, initial = getAttributes(mv)
return variability != fmi2VariabilityConstant &&
initial ∈ (fmi2InitialApprox, fmi2InitialExact)
end
import FMIImport: fmi2CausalityInput, fmi2CausalityParameter, fmi2VariabilityTunable
function setInInitialization(mv::fmi2ScalarVariable)
causality, variability, initial = getAttributes(mv)
return causality == fmi2CausalityInput ||
(causality != fmi2CausalityParameter && variability == fmi2VariabilityTunable) ||
(variability != fmi2VariabilityConstant && initial == fmi2InitialExact)
end
function prepareSolveFMU(
fmu::FMU2,
c::Union{Nothing,FMU2Component},
type::fmi2Type = fmu.type;
instantiate::Union{Nothing,Bool} = fmu.executionConfig.instantiate,
freeInstance::Union{Nothing,Bool} = fmu.executionConfig.freeInstance,
terminate::Union{Nothing,Bool} = fmu.executionConfig.terminate,
reset::Union{Nothing,Bool} = fmu.executionConfig.reset,
setup::Union{Nothing,Bool} = fmu.executionConfig.setup,
parameters::Union{Dict{<:Any,<:Any},Nothing} = nothing,
t_start::Real = 0.0,
t_stop::Union{Real,Nothing} = nothing,
tolerance::Union{Real,Nothing} = nothing,
x0::Union{AbstractArray{<:Real},Nothing} = nothing,
inputs::Union{Dict{<:Any,<:Any},Nothing} = nothing,
cleanup::Bool = false,
handleEvents = handleEvents,
instantiateKwargs...,
)
ignore_derivatives() do
autoInstantiated = false
# instantiate (hard)
if instantiate
# remove old one if we missed it (callback)
if cleanup && c != nothing
c = finishSolveFMU(
fmu,
c;
freeInstance = freeInstance,
terminate = terminate,
)
end
c = fmi2Instantiate!(fmu; type = type, instantiateKwargs...)
else # use existing instance
if c === nothing
if hasCurrentInstance(fmu)
c = getCurrentInstance(fmu)
else
@warn "Found no FMU instance, but executionConfig doesn't force allocation. Allocating one.\nUse `fmi2Instantiate(fmu)` to prevent this message."
c = fmi2Instantiate!(fmu; type = type, instantiateKwargs...)
autoInstantiated = true
end
end
end
@assert !isnothing(c) "No FMU instance available, allocate one or use `fmu.executionConfig.instantiate=true`."
# soft terminate (if necessary)
# if terminate
# retcode = fmi2Terminate(c; soft=true)
# @assert retcode == fmi2StatusOK "fmi2Simulate(...): Termination failed with return code $(retcode)."
# end
# soft reset (if necessary)
if reset
retcode = fmi2Reset(c; soft = true)
@assert retcode == fmi2StatusOK "fmi2Simulate(...): Reset failed with return code $(retcode)."
end
# setup experiment (hard)
if setup
retcode = fmi2SetupExperiment(c, t_start, t_stop; tolerance = tolerance)
@assert retcode == fmi2StatusOK "fmi2Simulate(...): Setting up experiment failed with return code $(retcode)."
end
# parameters
if parameters !== nothing
retcodes = setValue(
c,
collect(keys(parameters)),
collect(values(parameters));
filter = setBeforeInitialization,
)
@assert all(retcodes .== fmi2StatusOK) "fmi2Simulate(...): Setting initial parameters failed with return code $(retcode)."
end
# inputs
if inputs !== nothing
retcodes = setValue(
c,
collect(keys(inputs)),
collect(values(inputs));
filter = setBeforeInitialization,
)
@assert all(retcodes .== fmi2StatusOK) "fmi2Simulate(...): Setting initial inputs failed with return code $(retcode)."
end
# start state
if !isnothing(x0)
#retcode = fmi2SetContinuousStates(c, x0)
#@assert retcode == fmi2StatusOK "fmi2Simulate(...): Setting initial state failed with return code $(retcode)."
retcodes = setValue(
c,
fmu.modelDescription.stateValueReferences,
x0;
filter = setBeforeInitialization,
)
@assert all(retcodes .== fmi2StatusOK) "fmi2Simulate(...): Setting initial states failed with return code $(retcode)."
end
# enter (hard)
if setup
retcode = fmi2EnterInitializationMode(c)
@assert retcode == fmi2StatusOK "fmi2Simulate(...): Entering initialization mode failed with return code $(retcode)."
end
# parameters
if parameters !== nothing
retcodes = setValue(
c,
collect(keys(parameters)),
collect(values(parameters));
filter = setInInitialization,
)
@assert all(retcodes .== fmi2StatusOK) "fmi2Simulate(...): Setting initial parameters failed with return code $(retcodes)."
end
# inputs
if inputs !== nothing
retcodes = setValue(
c,
collect(keys(inputs)),
collect(values(inputs));
filter = setInInitialization,
)
@assert all(retcodes .== fmi2StatusOK) "fmi2Simulate(...): Setting initial inputs failed with return code $(retcodes)."
end
# start state
if !isnothing(x0)
#retcode = fmi2SetContinuousStates(c, x0)
#@assert retcode == fmi2StatusOK "fmi2Simulate(...): Setting initial state failed with return code $(retcode)."
retcodes = setValue(
c,
fmu.modelDescription.stateValueReferences,
x0;
filter = setInInitialization,
)
@assert all(retcodes .== fmi2StatusOK) "fmi2Simulate(...): Setting initial inputs failed with return code $(retcodes)."
# safe start state in component
c.x = copy(x0)
end
# exit setup (hard)
if setup
retcode = fmi2ExitInitializationMode(c)
@assert retcode == fmi2StatusOK "fmi2Simulate(...): Exiting initialization mode failed with return code $(retcode)."
end
# allocate a solution object
c.solution = FMUSolution(c)
# ME specific
if type == fmi2TypeModelExchange
if isnothing(x0) && !c.fmu.isZeroState
x0 = fmi2GetContinuousStates(c)
end
if instantiate || reset # autoInstantiated
if !setup
@debug "[AUTO] setup"
fmi2SetupExperiment(c, t_start, t_stop; tolerance = tolerance)
fmi2EnterInitializationMode(c)
fmi2ExitInitializationMode(c)
end
handleEvents(c)
end
c.fmu.hasStateEvents = (c.fmu.modelDescription.numberOfEventIndicators > 0)
c.fmu.hasTimeEvents = isTrue(c.eventInfo.nextEventTimeDefined)
end
end
return c, x0
end
function prepareSolveFMU(
fmu::FMU2,
c::Union{Nothing,FMU2Component},
type::Symbol;
kwargs...,
)
if type == :CS
return prepareSolveFMU(fmu, c, fmi2TypeCoSimulation; kwargs...)
elseif type == :ME
return prepareSolveFMU(fmu, c, fmi2TypeModelExchange; kwargs...)
elseif type == :SE
@assert false "FMU type `SE` is not supported in FMI2!"
else
@assert false "Unknown FMU type `$(type)`"
end
end
function finishSolveFMU(
fmu::FMU2,
c::FMU2Component;
freeInstance::Union{Nothing,Bool} = nothing,
terminate::Union{Nothing,Bool} = nothing,
popComponent::Bool = true,
)
if isnothing(c)
return
end
ignore_derivatives() do
if terminate === nothing
terminate = fmu.executionConfig.terminate
end
if freeInstance === nothing
freeInstance = fmu.executionConfig.freeInstance
end
# soft terminate (if necessary)
if terminate
retcode = fmi2Terminate(c; soft = true)
@assert retcode == fmi2StatusOK "fmi2Simulate(...): Termination failed with return code $(retcode)."
end
# freeInstance (hard)
if freeInstance
fmi2FreeInstance!(c; popComponent = popComponent)
c = nothing
end
end
return c
end