Skip to content

Commit a664182

Browse files
committed
Modify run! (batch.jl) and batchDataSolution, such that for FMUs which can't get/set their state, this is not done (the warning is kept). Could fix ThummeTo#119 (FMIFlux).
1 parent fae8ac4 commit a664182

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/batch.jl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,32 @@ end
165165

166166
function run!(neuralFMU::ME_NeuralFMU, batchElement::FMU2SolutionBatchElement; lastBatchElement=nothing, kwargs...)
167167

168-
neuralFMU.customCallbacksAfter = []
169-
neuralFMU.customCallbacksBefore = []
168+
if fmi2CanGetSetState(neuralFMU.fmu)
169+
@assert !neuralFMU.fmu.executionConfig.instantiate "Batching not possible for auto-instanciating FMUs."
170+
#if the fmu can get and set its state then do this before simulating the neuralFMU:
171+
neuralFMU.customCallbacksAfter = []
172+
neuralFMU.customCallbacksBefore = []
170173

171-
# STOP CALLBACK
172-
if !isnothing(lastBatchElement)
173-
stopcb = FunctionCallingCallback((u, t, integrator) -> copyState!(neuralFMU.fmu, lastBatchElement);
174-
funcat=[batchElement.tStop])
175-
push!(neuralFMU.customCallbacksAfter, stopcb)
176-
end
177-
178-
if isnothing(batchElement.initialState)
179-
startcb = FunctionCallingCallback((u, t, integrator) -> copyState!(neuralFMU.fmu, batchElement);
180-
funcat=[batchElement.tStart], func_start=true)
181-
push!(neuralFMU.customCallbacksAfter, startcb)
182-
183-
c = getCurrentComponent(neuralFMU.fmu)
184-
FMI.fmi2SetContinuousStates(c, batchElement.xStart)
185-
FMI.fmi2SetTime(c, batchElement.tStart)
174+
# STOP CALLBACK
175+
if !isnothing(lastBatchElement)
176+
stopcb = FunctionCallingCallback((u, t, integrator) -> copyState!(neuralFMU.fmu, lastBatchElement);
177+
funcat=[batchElement.tStop])
178+
push!(neuralFMU.customCallbacksAfter, stopcb)
179+
end
180+
181+
if isnothing(batchElement.initialState)
182+
startcb = FunctionCallingCallback((u, t, integrator) -> copyState!(neuralFMU.fmu, batchElement);
183+
funcat=[batchElement.tStart], func_start=true)
184+
push!(neuralFMU.customCallbacksAfter, startcb)
185+
186+
c = getCurrentComponent(neuralFMU.fmu)
187+
FMI.fmi2SetContinuousStates(c, batchElement.xStart)
188+
FMI.fmi2SetTime(c, batchElement.tStart)
189+
else
190+
pasteState!(neuralFMU.fmu, batchElement)
191+
end
186192
else
187-
pasteState!(neuralFMU.fmu, batchElement)
193+
@warn "This FMU can't set/get a FMU state. So discrete states can't be estimated together with the continuous solution."
188194
end
189195

190196
batchElement.solution = neuralFMU(batchElement.xStart, (batchElement.tStart, batchElement.tStop); saveat=batchElement.saveat, kwargs...)
@@ -377,12 +383,6 @@ end
377383
function batchDataSolution(neuralFMU::NeuralFMU, x0_fun, train_t::AbstractArray{<:Real}, targets::AbstractArray;
378384
batchDuration::Real=(train_t[end]-train_t[1]), indicesModel=1:length(targets[1]), plot::Bool=false, scalarLoss::Bool=true, solverKwargs...)
379385

380-
if fmi2CanGetSetState(neuralFMU.fmu)
381-
@assert !neuralFMU.fmu.executionConfig.instantiate "Batching not possible for auto-instanciating FMUs."
382-
else
383-
@warn "This FMU can't set/get a FMU state. So discrete states can't be estimated together with the continuous solution."
384-
end
385-
386386
batch = Array{FMIFlux.FMU2SolutionBatchElement,1}()
387387

388388
# indicesData = 1:1

0 commit comments

Comments
 (0)