Skip to content

Commit

Permalink
0.1.4 (#7)
Browse files Browse the repository at this point in the history
* Update Project.toml

* added ability to add FMU resources
  • Loading branch information
ThummeTo authored Mar 24, 2022
1 parent 8c57b21 commit 62eab61
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FMIBuild"
uuid = "226f0e26-6dd6-4589-ada7-1d32f6e1d800"
authors = ["TT <tobias.thummerer@informatik.uni-augsburg.de>", "LM <lars.mikelsons@informatik.uni-augsburg.de>"]
version = "0.1.3"
version = "0.1.4"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -13,7 +13,7 @@ ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"

[compat]
EzXML = "1.1.0"
FMICore = "0.6.1"
FMICore = "^0.7.0"
PackageCompiler = "2.0.5"
ZipFile = "0.9.4"
julia = "1.6"
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
# FMIBuild.jl

## What is FMIBuild.jl?
*FMIBuild.jl* holds dependencies that are required to compile and zip a Functional Mock-Up Unit (FMU) compliant to the FMI-standard ([fmi-standard.org](http://fmi-standard.org/)). Because this dependencies should not be part of the compiled FMU, they are out-sourced into this package.
*FMIBuild.jl* provides the build-commands for the Julia package [*FMIExport.jl*](https://github.com/ThummeTo/FMIExport.jl).
[*FMIBuild.jl*](https://github.com/ThummeTo/FMIBuild.jl) holds dependencies that are required to compile and zip a Functional Mock-Up Unit (FMU) compliant to the FMI-standard ([fmi-standard.org](http://fmi-standard.org/)). Because this dependencies should not be part of the compiled FMU, they are out-sourced into this package.
[*FMIBuild.jl*](https://github.com/ThummeTo/FMIBuild.jl) provides the build-commands for the Julia package [*FMIExport.jl*](https://github.com/ThummeTo/FMIExport.jl).

[![CI Testing](https://github.com/ThummeTo/FMIBuild.jl/actions/workflows/Test.yml/badge.svg)](https://github.com/ThummeTo/FMIBuild.jl/actions)
[![Coverage](https://codecov.io/gh/ThummeTo/FMIBuild.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ThummeTo/FMIBuild.jl)

## How can I use FMIBuild.jl?
**Please note:** *FMIBuild.jl* is not meant to be used as it is, but as part of [*FMI.jl*](https://github.com/ThummeTo/FMI.jl) and [*FMIExport.jl*](https://github.com/ThummeTo/FMIExport.jl). However you can install *FMIBuild.jl* by following these steps.
**Please note:** [*FMIBuild.jl*](https://github.com/ThummeTo/FMIBuild.jl) is not meant to be used as it is, but as part of [*FMI.jl*](https://github.com/ThummeTo/FMI.jl) and [*FMIExport.jl*](https://github.com/ThummeTo/FMIExport.jl). However you can install *FMIBuild.jl* by following these steps.
1. open a Julia-Command-Window, activate your preferred environment
1. goto package manager using ```]```
1. type ```add FMIBuild```

## What FMI.jl-Library should I use?
![FMI.jl Family](https://github.com/ThummeTo/FMI.jl/blob/main/docs/src/assets/FMI_JL_family.png "FMI.jl Family")
To keep dependencies nice and clean, the original package *FMI.jl* had been split into new packages:
To keep dependencies nice and clean, the original package [*FMI.jl*](https://github.com/ThummeTo/FMI.jl) had been split into new packages:
- [*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
- [*FMICore.jl*](https://github.com/ThummeTo/FMICore.jl): C-code wrapper for the FMI-standard
- [*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)
- [*FMIZoo.jl*](https://github.com/ThummeTo/FMIZoo.jl): A collection of testing and example FMUs

## What Platforms are supported?
*FMIBuild.jl* is tested (and testing) under Julia Versions *1.6.5 LTS* and *latest* on Windows *latest* and Ubuntu *latest*. `x64` architectures are tested. Mac and x86-architectures might work, but are not tested.
[*FMIBuild.jl*](https://github.com/ThummeTo/FMIBuild.jl) is tested (and testing) under Julia Versions *1.6.5 LTS* and *latest* on Windows *latest* and Ubuntu *latest*. `x64` architectures are tested. Mac and x86-architectures might work, but are not tested.

## How to cite?
Tobias Thummerer, Lars Mikelsons and Josef Kircher. 2021. **NeuralFMU: towards structural integration of FMUs into neural networks.** Martin Sjölund, Lena Buffoni, Adrian Pop and Lennart Ochel (Ed.). Proceedings of 14th Modelica Conference 2021, Linköping, Sweden, September 20-24, 2021. Linköping University Electronic Press, Linköping (Linköping Electronic Conference Proceedings ; 181), 297-306. [DOI: 10.3384/ecp21181297](https://doi.org/10.3384/ecp21181297)
Expand Down
83 changes: 77 additions & 6 deletions src/FMIBuild.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri
coSimulation=false,
removeLibDependency=true,
removeNoExportBlocks=true,
resources::Union{Dict{String, String}, Nothing}=nothing,
pkg_comp_kwargs...)

# @assert fmi2Check(fmu) == true ["fmiBuild(...): FMU-Pre-Check failed. See messages above for further information."]
Expand Down Expand Up @@ -195,8 +196,17 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri

@info "[Build FMU] Adding/removing dependencies ..."
currentEnv = Base.active_project()
preCompState = 1
try
preCompState = ENV["JULIA_PKG_PRECOMPILE_AUTO"]
catch e
preCompState = 1
end
ENV["JULIA_PKG_PRECOMPILE_AUTO"] = 0

Pkg.activate(merge_dir)
Pkg.add("FMICore") #Pkg.add(name="FMICore", version="0.6.1")
Pkg.add("FMICore")
#Pkg.add(name="FMICore", version="0.7.1")
@info "[Build FMU] > Added FMICore"
if removeLibDependency
cdata = replace(cdata, r"(using|import) FMIBuild" => "")
Expand All @@ -207,11 +217,12 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri
@info "[Build FMU] > Not used FMIBuild"
end
end

Pkg.activate(currentEnv)
Pkg.resolve()
#Pkg.resolve()
ENV["JULIA_PKG_PRECOMPILE_AUTO"]=preCompState
@info "[Build FMU] ... adding/removing dependencies done."
close(f)


@info "[Build FMU] ... generating new FMU source file at $(joinpath(merge_dir, fmu_src_in_merge_dir))"

f = open(joinpath(merge_dir, fmu_src_in_merge_dir), "w")
Expand Down Expand Up @@ -241,6 +252,16 @@ function fmi2Save(fmu::FMU2, fmu_path::String, fmu_src_file::Union{Nothing, Stri
cp(joinpath(target_dir, "_" * fmu_name, "share"), joinpath(bin_dir, "..", "share"); force=true)
cp(joinpath(target_dir, "_" * fmu_name, "include"), joinpath(bin_dir, "..", "include"); force=true)

if resources != nothing
@info "[Build FMU] Adding resource files ..."
mkdir(joinpath(bin_dir, "..", "..", "resources"))
for (key, val) in resources
cp(key, joinpath(bin_dir, "..", "..", "resources", val); force=true)
@info "[Build FMU] \t $val"
end
@info "[Build FMU] ... adding resoruce files done."
end

@info "[Build FMU] Patching libjulia.$(libext) @ `$(bin_dir)`..."
patchJuliaLib(joinpath(bin_dir, "libjulia.$(libext)"))
@info "[Build FMU] ... patching libjulia.$(libext) done."
Expand Down Expand Up @@ -405,7 +426,7 @@ function fmi2SaveModelDescription(md::fmi2ModelDescription, file_path::String)
link!(sv_node, AttributeNode("causality", fmi2CausalityToString(sv.causality)))
end
if sv.variability != nothing
link!(sv_node, AttributeNode("causality", fmi2VariabilityToString(sv.variability)))
link!(sv_node, AttributeNode("variability", fmi2VariabilityToString(sv.variability)))
end
if sv.initial != nothing
link!(sv_node, AttributeNode("initial", fmi2InitialToString(sv.initial)))
Expand All @@ -430,7 +451,57 @@ function fmi2SaveModelDescription(md::fmi2ModelDescription, file_path::String)
link!(sv_node, r_node)
end

# ToDo: Implement remaining DataTypes: Integer, Boolean, String, Enumeration
# Integer
if sv._Integer != nothing
i_node = ElementNode("Integer")

if sv._Integer.start != nothing
link!(i_node, AttributeNode("start", "$sv._Integer.start)"))
end

# ToDo: Implement remaining attributes

link!(sv_node, i_node)
end

# Boolean
if sv._Boolean != nothing
b_node = ElementNode("Boolean")

if sv._Boolean.start != nothing
link!(b_node, AttributeNode("start", "$sv._Boolean.start)"))
end

# ToDo: Implement remaining attributes

link!(sv_node, b_node)
end

# String
if sv._String != nothing
s_node = ElementNode("String")

if sv._String.start != nothing
link!(s_node, AttributeNode("start", "$sv._String.start)"))
end

# ToDo: Implement remaining attributes

link!(sv_node, s_node)
end

# _Enumeration
if sv._Enumeration != nothing
e_node = ElementNode("Enumeration")

# if sv._Enumeration.start != nothing
# link!(e_node, AttributeNode("start", "$sv._Enumeration.start)"))
# end

# ToDo: Implement remaining attributes

link!(sv_node, e_node)
end
end

ms = ElementNode("ModelStructure")
Expand Down
8 changes: 6 additions & 2 deletions template/ME/FMU2/src/FMU2_content.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ using FMICore: fmi2Status, fmi2Type
global FMIBUILD_FMU = nothing
global FMIBUILD_CONSTRUCTOR = nothing

Base.@ccallable function init_FMU()::Cvoid
Base.@ccallable function init_FMU(_dllLoc::Ptr{Cchar})::Cvoid
dllLoc = unsafe_string(_dllLoc)
comps = splitpath(dllLoc)
dllLoc = joinpath(comps[1:end-3]..., "resources")

global FMIBUILD_FMU, FMIBUILD_CONSTRUCTOR
FMIBUILD_FMU = FMIBUILD_CONSTRUCTOR()
FMIBUILD_FMU = FMIBUILD_CONSTRUCTOR(dllLoc)
nothing
end

Expand Down
21 changes: 20 additions & 1 deletion template/ME/header/FMU2_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,26 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
case DLL_PROCESS_ATTACH:
init_julia(0, NULL);
init_FMU();

// get DLL path for resource location
char path[MAX_PATH];
HMODULE hm = NULL;

if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &init_julia, &hm) == 0)
{
int ret = GetLastError();
fprintf(stderr, "GetModuleHandle failed, error = %d\n", ret);
return (FALSE);
}
if (GetModuleFileName(hm, path, sizeof(path)) == 0)
{
int ret = GetLastError();
fprintf(stderr, "GetModuleFileName failed, error = %d\n", ret);
return (FALSE);
}

char* ptr = (char*)&path;
init_FMU(ptr);
break;
case DLL_PROCESS_DETACH:
shutdown_julia(0);
Expand Down
4 changes: 2 additions & 2 deletions template/ME/header/FMU2_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ fmi2Status fmi2GetNominalsOfContinuousStates(fmi2Component, fmi2Real[], size_t);
#include <WinDef.h>
#include <stdbool.h>
#define BOOL bool
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);
void init_FMU(void);
BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
void init_FMU(char*);

// from julia_init.h
//void init_julia(int argc, char *argv[]);
Expand Down
2 changes: 1 addition & 1 deletion template/ME/precompile/FMU2_additional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

precompile(Tuple{typeof(.init_FMU)})
precompile(Tuple{typeof(.init_FMU), Ptr{Cchar} })

precompile(Tuple{typeof(.fmi2GetTypesPlatform)})
precompile(Tuple{typeof(.fmi2GetVersion})
Expand Down

2 comments on commit 62eab61

@ThummeTo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/57213

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.4 -m "<description of version>" 62eab618e9dc6cfe589e78587241593233cf6cd0
git push origin v0.1.4

Please sign in to comment.