Skip to content

Commit

Permalink
minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThummeTo committed Sep 3, 2024
1 parent 7947fc1 commit 24a37fa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FMIImport"
uuid = "9fcbc62e-52a0-44e9-a616-1359a0008194"
authors = ["TT <[email protected]>", "LM <[email protected]>", "JK <[email protected]>"]
version = "1.0.5"
version = "1.0.6"

[deps]
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Expand Down
22 changes: 17 additions & 5 deletions src/FMI2/prep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function prepareSolveFMU(

ignore_derivatives() do

autoInstantiated = false

# instantiate (hard)
if instantiate
# remove old one if we missed it (callback)
Expand All @@ -58,13 +60,15 @@ function prepareSolveFMU(
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
Expand Down Expand Up @@ -183,11 +187,19 @@ function prepareSolveFMU(
x0 = fmi2GetContinuousStates(c)
end

if instantiate || reset # we have a fresh instance
@debug "[NEW INST]"
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
Expand All @@ -208,7 +220,7 @@ function prepareSolveFMU(
elseif type == :SE
@assert false "FMU type `SE` is not supported in FMI2!"
else
@assert false "Unknwon FMU type `$(type)`"
@assert false "Unknown FMU type `$(type)`"
end
end

Expand Down Expand Up @@ -241,7 +253,7 @@ function finishSolveFMU(

# freeInstance (hard)
if freeInstance
fmi2FreeInstance!(c; popComponent = popComponent) # , doccall=freeInstance
fmi2FreeInstance!(c; popComponent = popComponent)
c = nothing
end
end
Expand Down
21 changes: 16 additions & 5 deletions src/FMI3/prep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function prepareSolveFMU(

ignore_derivatives() do

autoInstantiated = false

c = nothing

# instantiate (hard)
Expand All @@ -53,7 +55,7 @@ function prepareSolveFMU(
elseif type == fmi3TypeScheduledExecution
c = fmi3InstantiateScheduledExecution!(fmu; instantiateKwargs...)
else
@assert false "Unknwon fmi3Type `$(type)`"
@assert false "Unknown fmi3Type `$(type)`"
end
else
if c === nothing
Expand All @@ -63,12 +65,15 @@ function prepareSolveFMU(
@warn "Found no FMU instance, but executionConfig doesn't force allocation. Allocating one. Use `fmi3Instantiate[TYPE](fmu)` to prevent this message."
if type == fmi3TypeCoSimulation
c = fmi3InstantiateCoSimulation!(fmu; instantiateKwargs...)
autoInstantiated = true
elseif type == fmi3TypeModelExchange
c = fmi3InstantiateModelExchange!(fmu; instantiateKwargs...)
autoInstantiated = true
elseif type == fmi3TypeScheduledExecution
c = fmi3InstantiateScheduledExecution!(fmu; instantiateKwargs...)
autoInstantiated = true
else
@assert false "Unknwon FMU type `$(type)`."
@assert false "Unknown FMU type `$(type)`."
end
end
end
Expand Down Expand Up @@ -181,8 +186,14 @@ function prepareSolveFMU(
x0 = fmi3GetContinuousStates(c)
end

if instantiate || reset # we have a fresh instance
@debug "[NEW INST]"
if instantiate || reset # autoInstantiated
@debug "[AUTO] setup"

if !setup
fmi3EnterInitializationMode(c, t_start, t_stop; tolerance = tolerance)
fmi3ExitInitializationMode(c)
end

handleEvents(c)
end

Expand All @@ -202,7 +213,7 @@ function prepareSolveFMU(fmu::FMU3, c::Union{Nothing,FMU3Instance}, type::Symbol
elseif type == :SE
return prepareSolveFMU(fmu, c, fmi3TypeScheduledExecution; kwargs...)
else
@assert false "Unknwon FMU type `$(type)`"
@assert false "Unknown FMU type `$(type)`"
end
end

Expand Down

0 comments on commit 24a37fa

Please sign in to comment.