Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.4.0 #44

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
name = "FMIExport"
uuid = "31b88311-cab6-44ed-ba9c-fe5a9abbd67a"
authors = ["TT <[email protected]>", "LM <[email protected]>"]
version = "0.3.2"
version = "0.4.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
FMICore = "8af89139-c281-408e-bce2-3005eb87462f"
FMIBase = "900ee838-d029-460e-b485-d98a826ceef2"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
Dates = "1"
EzXML = "1.1.0"
FMICore = "0.18.0 - 0.20"
FMIBase = "1.0.0"
UUIDs = "1"
julia = "1.6"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ To keep dependencies nice and clean, the original package [*FMI.jl*](https://git
- [*FMI.jl*](https://github.com/ThummeTo/FMI.jl): High level loading, manipulating, saving or building entire FMUs from scratch
- [*FMIImport.jl*](https://github.com/ThummeTo/FMIImport.jl): Importing FMUs into Julia
- [*FMIExport.jl*](https://github.com/ThummeTo/FMIExport.jl): Exporting stand-alone FMUs from Julia Code
- [*FMIBase.jl*](https://github.com/ThummeTo/FMIBase.jl): Common concepts for import and export of FMUs
- [*FMICore.jl*](https://github.com/ThummeTo/FMICore.jl): C-code wrapper for the FMI-standard
- [*FMISensitivity.jl*](https://github.com/ThummeTo/FMISensitivity.jl): Static and dynamic sensitivities over FMUs
- [*FMIBuild.jl*](https://github.com/ThummeTo/FMIBuild.jl): Compiler/Compilation dependencies for FMIExport.jl
- [*FMIFlux.jl*](https://github.com/ThummeTo/FMIFlux.jl): Machine Learning with FMUs (differentiation over FMUs)
- [*FMIFlux.jl*](https://github.com/ThummeTo/FMIFlux.jl): Machine Learning with FMUs
- [*FMIZoo.jl*](https://github.com/ThummeTo/FMIZoo.jl): A collection of testing and example FMUs

## What Platforms are supported?
Expand Down
2 changes: 1 addition & 1 deletion examples/FMI2/BouncingBall/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ FMIBuild = "226f0e26-6dd6-4589-ada7-1d32f6e1d800"
FMIExport = "31b88311-cab6-44ed-ba9c-fe5a9abbd67a"

[compat]
FMIBuild = "0.2.0"
FMIBuild = "0.3.2"
julia = "1.6"
21 changes: 13 additions & 8 deletions examples/FMI2/BouncingBall/src/BouncingBall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#

using FMIExport
using FMIExport.FMICore: fmi2True, fmi2False
using FMIExport.FMIBase.FMICore: fmi2True, fmi2False

EPS = 1e-6

FMU_FCT_INIT = function()
m = 1.0 # ball mass
Expand Down Expand Up @@ -34,14 +36,12 @@ FMU_FCT_EVALUATE = function(t, x_c, ẋ_c, x_d, u, p, eventMode)
sticking = x_d[1]
_, a = ẋ_c

eps = 1e-10

if sticking == fmi2True
a = 0.0
else
elseif sticking == fmi2False
if eventMode
if s < r && v < 0.0
s = r + eps # so that indicator is not triggered again
s = r + EPS # so that indicator is not triggered again
v = -v*d

# stop bouncing to prevent high frequency bouncing (and maybe tunneling the floor)
Expand All @@ -50,9 +50,14 @@ FMU_FCT_EVALUATE = function(t, x_c, ẋ_c, x_d, u, p, eventMode)
v = 0.0
end
end
else
# no specials in continuos time mode
end

a = (m * -g) / m # the system's physical equation
a = (m * -g) / m # the system's physical equation (a little longer than necessary)
else
@error "Unknown value for `sticking` == $(sticking)."
return (x_c, ẋ_c, x_d, p)
end

x_c = [s, v]
Expand Down Expand Up @@ -134,8 +139,8 @@ tmpDir = mktempdir(; prefix="fmibuildjl_test_", cleanup=false)
fmu_save_path = joinpath(tmpDir, "BouncingBall.fmu")

fmu = FMIBUILD_CONSTRUCTOR()
using FMIBuild: fmi2Save # <= this must be excluded during export, because FMIBuild cannot execute itself (but it is able to build)
fmi2Save(fmu, fmu_save_path; debug=true) # <= this must be excluded during export, because fmi2Save would start an infinte build loop with itself (debug=true allows debug messages, but is slow during execution!)
using FMIBuild: saveFMU # <= this must be excluded during export, because FMIBuild cannot execute itself (but it is able to build)
saveFMU(fmu, fmu_save_path; debug=true, compress=false) # <= this must be excluded during export, because fmi2Save would start an infinte build loop with itself (debug=true allows debug messages, but is slow during execution!)

### some tests ###
# using FMI
Expand Down
2 changes: 1 addition & 1 deletion examples/FMI2/Manipulation/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ FMIImport = "9fcbc62e-52a0-44e9-a616-1359a0008194"
FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195"

[compat]
FMIBuild = "0.2.0"
FMIBuild = "0.3.2"
julia = "1.6"
14 changes: 7 additions & 7 deletions examples/FMI2/Manipulation/src/Manipulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#

using FMIExport: fmi2SetFctGetReal, fmi2CreateEmbedded
using FMIExport.FMICore: fmi2Real, fmi2Component, fmi2StatusOK, fmi2ValueReference
using FMIExport.FMICore: fmi2CausalityParameter, fmi2VariabilityTunable, fmi2InitialExact
using FMIImport: fmi2Load
using FMIExport.FMIBase.FMICore: fmi2Real, fmi2Component, fmi2StatusOK, fmi2ValueReference
using FMIExport.FMIBase.FMICore: fmi2CausalityParameter, fmi2VariabilityTunable, fmi2InitialExact
using FMIImport: loadFMU
import FMIExport

originalGetReal = nothing # function pointer to the original fmi2GetReal c-function
Expand All @@ -17,7 +17,7 @@ function myGetReal!(c::fmi2Component, vr::Union{Array{fmi2ValueReference}, Ptr{f
global originalGetReal

# first, we do what the original function does
status = FMIExport.FMICore.fmi2GetReal!(originalGetReal, c, vr, nvr, value)
status = FMIExport.FMIBase.FMICore.fmi2GetReal!(originalGetReal, c, vr, nvr, value)

# if we have a pointer to an array, we must interprete it as array to access elements
if isa(value, Ptr{fmi2Real})
Expand All @@ -44,7 +44,7 @@ FMIBUILD_CONSTRUCTOR = function(resPath)
global originalGetReal

# loads an existing FMU inside the FMU
fmu = fmi2Load(joinpath(resPath, "SpringDamperPendulum1D.fmu"))
fmu = loadFMU(joinpath(resPath, "SpringDamperPendulum1D.fmu"))

# create a FMU that embedds the existing FMU
fmu = fmi2CreateEmbedded(fmu)
Expand Down Expand Up @@ -78,8 +78,8 @@ fmu_save_path = joinpath(tmpDir, "Manipulation.fmu")

sourceFMU = FMIZoo.get_model_filename("SpringDamperPendulum1D", "Dymola", "2022x")
fmu = FMIBUILD_CONSTRUCTOR(dirname(sourceFMU))
import FMIBuild:fmi2Save # <= this must be excluded during export, because FMIBuild cannot execute itself (but it is able to build)
fmi2Save(fmu, fmu_save_path; resources=Dict(sourceFMU=>"SpringDamperPendulum1D.fmu")) # <= this must be excluded during export, because fmi2Save would start an infinte build loop with itself
import FMIBuild:saveFMU # <= this must be excluded during export, because FMIBuild cannot execute itself (but it is able to build)
saveFMU(fmu, fmu_save_path; resources=Dict(sourceFMU=>"SpringDamperPendulum1D.fmu")) # <= this must be excluded during export, because fmi2Save would start an infinte build loop with itself

# some tests
# using FMI
Expand Down
2 changes: 1 addition & 1 deletion examples/FMI2/NeuralFMU/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ FMIImport = "9fcbc62e-52a0-44e9-a616-1359a0008194"
FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195"

[compat]
FMIBuild = "0.2.0"
FMIBuild = "0.3.2"
julia = "1.6"
14 changes: 7 additions & 7 deletions examples/FMI2/NeuralFMU/src/NeuralFMU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ using FMIExport: Dense, Chain
using FMIExport: fmi2SetFctGetDerivatives, fmi2SetFctGetReal, fmi2SetFctSetReal, fmi2SetFctSetTime, fmi2SetFctSetContinuousStates
using FMIExport: fmi2CreateEmbedded
using FMIExport: fmi2AddRealParameter
using FMIExport.FMICore: fmi2Real, fmi2Component, fmi2StatusOK, fmi2ValueReference
using FMIExport.FMICore: fmi2CausalityParameter, fmi2VariabilityTunable, fmi2InitialExact
using FMIImport: fmi2Load
using FMIExport.FMIBase.FMICore: fmi2Real, fmi2Component, fmi2StatusOK, fmi2ValueReference
using FMIExport.FMIBase.FMICore: fmi2CausalityParameter, fmi2VariabilityTunable, fmi2InitialExact
using FMIImport: loadFMU
import FMIExport

fmu = nothing
Expand Down Expand Up @@ -141,7 +141,7 @@ function updateDerivatives(_component::fmi2Component, ndx::Csize_t)

if !DERIVATIVES_VALID
# first, we do what the original function does
status = FMIExport.FMICore.fmi2GetDerivatives!(originalGetDerivatives, _component, DERIVATIVES, ndx)
status = FMIExport.FMIBase.FMICore.fmi2GetDerivatives!(originalGetDerivatives, _component, DERIVATIVES, ndx)

if status != fmi2StatusOK
logError(_component, "fmi2GetDerivatives failed!")
Expand Down Expand Up @@ -305,7 +305,7 @@ FMIBUILD_CONSTRUCTOR = function(resPath)
global fmu, ANN_PARAMETERS

# loads an existing FMU
fmu = fmi2Load(joinpath(resPath, "SpringDamperPendulum1D.fmu"))
fmu = loadFMU(joinpath(resPath, "SpringDamperPendulum1D.fmu"))

# create a FMU that embedds the existing FMU
fmu = fmi2CreateEmbedded(fmu)
Expand Down Expand Up @@ -366,8 +366,8 @@ fmu_save_path = joinpath(tmpDir, "NeuralFMU.fmu")

sourceFMU = FMIZoo.get_model_filename("SpringDamperPendulum1D", "Dymola", "2022x")
fmu = FMIBUILD_CONSTRUCTOR(dirname(sourceFMU))
import FMIBuild:fmi2Save # <= this must be excluded during export, because FMIBuild cannot execute itself (but it is able to build)
fmi2Save(fmu, fmu_save_path; compress=false, debug=true, resources=Dict(sourceFMU=>"SpringDamperPendulum1D.fmu")) # <= this must be excluded during export, because fmi2Save would start an infinte build loop with itself
import FMIBuild:saveFMU # <= this must be excluded during export, because FMIBuild cannot execute itself (but it is able to build)
saveFMU(fmu, fmu_save_path; compress=false, debug=true, resources=Dict(sourceFMU=>"SpringDamperPendulum1D.fmu")) # <= this must be excluded during export, because fmi2Save would start an infinte build loop with itself

### some tests ###
# using FMI
Expand Down
12 changes: 9 additions & 3 deletions src/FMI2_md.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

using EzXML
using FMIBase.EzXML
import UUIDs
import Dates

import FMIBase.FMICore: fmi2ModelDescriptionModelExchange, fmi2ModelDescriptionCoSimulation
import FMIBase.FMICore: fmi2RealAttributesExt, fmi2IntegerAttributesExt, fmi2BooleanAttributesExt, fmi2StringAttributesExt, fmi2EnumerationAttributesExt

function fmi2CreateModelDescription()
md = fmi2ModelDescription()
md.guid = UUIDs.uuid1()
Expand Down Expand Up @@ -138,7 +141,10 @@ function fmi2ModelDescriptionAddIntegerDiscreteState(md::fmi2ModelDescription, n
_Integer = fmi2IntegerAttributesExt()
_Integer.start = start

sv = fmi2ModelDescriptionAddModelVariable(md, name; attribute=_Integer, kwargs...)
sv = fmi2ModelDescriptionAddModelVariable(md, name;
attribute=_Integer,
variability=fmi2VariabilityDiscrete,
kwargs...)

push!(md.discreteStateValueReferences, sv.valueReference)
push!(md.stringValueReferences, sv.name => sv.valueReference)
Expand All @@ -147,7 +153,7 @@ function fmi2ModelDescriptionAddIntegerDiscreteState(md::fmi2ModelDescription, n
end

function fmi2ModelDescriptionAddEventIndicator(md::fmi2ModelDescription)
if md.numberOfEventIndicators == nothing
if isnothing(md.numberOfEventIndicators)
md.numberOfEventIndicators = 0
end
md.numberOfEventIndicators += 1
Expand Down
Loading
Loading