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.11.0 #103

Closed
wants to merge 8 commits into from
Closed
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
11 changes: 6 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
DifferentiableEigen = "73a20539-4e65-4dcb-a56d-dc20f210a01b"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
FMIImport = "9fcbc62e-52a0-44e9-a616-1359a0008194"
FMISensitivity = "3e748fe5-cd7f-4615-8419-3159287187d2"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand All @@ -18,13 +19,13 @@ ThreadPools = "b189fb0b-2eb5-4ed4-bc0c-d34c51242431"

[compat]
Colors = "0.12.8"
DiffEqCallbacks = "2.26.0"
DifferentiableEigen = "0.2.0"
DifferentialEquations = "7.8.0"
FMIImport = "0.15.8"
Flux = "0.13, 0.14"
DifferentialEquations = "7.10.0 - 7.11.0"
FMIImport = "0.16.2"
FMISensitivity = "0.1.0"
Flux = "0.14"
Optim = "1.7.0"
ProgressMeter = "1.7.0"
ProgressMeter = "1.7.0 - 1.9.0"
Requires = "1.3.0"
ThreadPools = "2.1.1"
julia = "1.6"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ You can evaluate FMUs inside of your loss function.
- building and training NeuralFMUs consisiting of multiple FMUs
- building and training FMUINNs (PINNs)
- different AD-frameworks: ForwardDiff.jl (CI-tested), ReverseDiff.jl (CI-tested, default setting), FiniteDiff.jl (not CI-tested) and Zygote.jl (not CI-tested)
- use `Flux.jl` optimisers as well as the ones from `Optim.jl`
- ...

## What is under development in FMIFlux.jl?
Expand All @@ -62,6 +63,7 @@ To keep dependencies nice and clean, the original package [*FMI.jl*](https://git
- [*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
- [*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)
- [*FMIZoo.jl*](https://github.com/ThummeTo/FMIZoo.jl): A collection of testing and example FMUs
Expand Down
39 changes: 19 additions & 20 deletions examples/src/advanced_hybrid_ME.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@
"| 1. | Enter Package Manager via | ] |\n",
"| 2. | Install FMI via | add FMI | \n",
"| 3. | Install FMIFlux via | add FMIFlux | \n",
"| 4. | Install FMIZoo via | add FMIZoo | \n",
"| 5. | Install DifferentialEquations via | add DifferentialEquations | \n",
"| 6. | Install Plots via | add Plots | \n",
"| 7. | Install Random via | add Random | "
"| 4. | Install FMIZoo via | add FMIZoo | \n",
"| 5. | Install Plots via | add Plots | \n",
"| 6. | Install Random via | add Random | "
]
},
{
Expand Down Expand Up @@ -100,7 +99,7 @@
"using FMIFlux\n",
"using FMIFlux.Flux\n",
"using FMIZoo\n",
"using DifferentialEquations: Tsit5\n",
"using FMI.DifferentialEquations: Tsit5\n",
"using Statistics: mean, std\n",
"import Plots\n",
"\n",
Expand All @@ -120,7 +119,7 @@
"\n",
"![svg](https://github.com/thummeto/FMIFlux.jl/blob/main/docs/src/examples/img/SpringPendulum1D.svg?raw=true)\n",
"\n",
"In contrast, the model *SpringFrictionPendulum1D* (*realFMU*) is somewhat more accurate, because it includes a friction component. \n",
"In contrast, the model *SpringFrictionPendulum1D* (*fricFMU*) is somewhat more accurate, because it includes a friction component. \n",
"\n",
"![svg](https://github.com/thummeto/FMIFlux.jl/blob/main/docs/src/examples/img/SpringFrictionPendulum1D.svg?raw=true)"
]
Expand Down Expand Up @@ -156,9 +155,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### RealFMU\n",
"### *fricFMU*\n",
"\n",
"In the next lines of code the FMU of the *realFMU* model from *FMIZoo.jl* is loaded and the information about the FMU is shown."
"In the next lines of code the FMU of the *fricFMU* model from *FMIZoo.jl* is loaded and the information about the FMU is shown."
]
},
{
Expand All @@ -175,15 +174,15 @@
},
"outputs": [],
"source": [
"realFMU = fmiLoad(\"SpringFrictionPendulum1D\", \"Dymola\", \"2022x\")\n",
"fmiInfo(realFMU)"
"fricFMU = fmiLoad(\"SpringFrictionPendulum1D\", \"Dymola\", \"2022x\")\n",
"fmiInfo(fricFMU)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the function fmiSimulate() the *realFMU* is simulated, still specifying the start and end time, the parameters and which variables are recorded. After the simulation is finished the result of the *realFMU* can be plotted. This plot also serves as a reference for the other model (*simpleFMU*)."
"In the function fmiSimulate() the *fricFMU* is simulated, still specifying the start and end time, the parameters and which variables are recorded. After the simulation is finished the result of the *fricFMU* can be plotted. This plot also serves as a reference for the other model (*simpleFMU*)."
]
},
{
Expand All @@ -201,15 +200,15 @@
"outputs": [],
"source": [
"vrs = [\"mass.s\", \"mass.v\", \"mass.a\", \"mass.f\"]\n",
"realSimData = fmiSimulate(realFMU, (tStart, tStop); recordValues=vrs, saveat=tSave)\n",
"realSimData = fmiSimulate(fricFMU, (tStart, tStop); recordValues=vrs, saveat=tSave)\n",
"fmiPlot(realSimData)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data from the simulation of the *realFMU*, are divided into position and velocity data. These data will be needed later. "
"The data from the simulation of the *fricFMU*, are divided into position and velocity data. These data will be needed later. "
]
},
{
Expand Down Expand Up @@ -273,7 +272,7 @@
},
"outputs": [],
"source": [
"fmiUnload(realFMU)"
"fmiUnload(fricFMU)"
]
},
{
Expand All @@ -282,7 +281,7 @@
"source": [
"### SimpleFMU\n",
"\n",
"The following lines load, simulate and plot the *simpleFMU* just like the *realFMU*. The differences between both systems can be clearly seen from the plots. In the plot for the *realFMU* it can be seen that the oscillation continues to decrease due to the effect of the friction. If you simulate long enough, the oscillation would come to a standstill in a certain time. The oscillation in the *simpleFMU* behaves differently, since the friction was not taken into account here. The oscillation in this model would continue to infinity with the same oscillation amplitude. From this observation the desire of an improvement of this model arises. "
"The following lines load, simulate and plot the *simpleFMU* just like the *fricFMU*. The differences between both systems can be clearly seen from the plots. In the plot for the *fricFMU* it can be seen that the oscillation continues to decrease due to the effect of the friction. If you simulate long enough, the oscillation would come to a standstill in a certain time. The oscillation in the *simpleFMU* behaves differently, since the friction was not taken into account here. The oscillation in this model would continue to infinity with the same oscillation amplitude. From this observation the desire of an improvement of this model arises. "
]
},
{
Expand Down Expand Up @@ -343,7 +342,7 @@
"\n",
"In order to train our model, a loss function must be implemented. The solver of the NeuralFMU can calculate the gradient of the loss function. The gradient descent is needed to adjust the weights in the neural network so that the sum of the error is reduced and the model becomes more accurate.\n",
"\n",
"The loss function in this implementation consists of the mean squared error (mse) from the real position of the *realFMU* simulation (posReal) and the position data of the network (posNet).\n",
"The loss function in this implementation consists of the mean squared error (mse) from the real position of the *fricFMU* simulation (posReal) and the position data of the network (posNet).\n",
"$$ e_{mse} = \\frac{1}{n} \\sum\\limits_{i=0}^n (posReal[i] - posNet[i])^2 $$\n",
"A growing horizon is applied, whereby the horizon only goes over the first five values. For this horizon the mse is calculated."
]
Expand Down Expand Up @@ -382,7 +381,7 @@
"source": [
"#### Function for plotting\n",
"\n",
"In this section the function for plotting is defined. The function `plotResults()` creates a new figure object. In dieses figure objekt werden dann die aktuellsten Ergebnisse von *realFMU*, *simpleFMU* und *neuralFMU* gegenübergestellt. \n",
"In this section the function for plotting is defined. The function `plotResults()` creates a new figure object. In dieses figure objekt werden dann die aktuellsten Ergebnisse von *fricFMU*, *simpleFMU* und *neuralFMU* gegenübergestellt. \n",
"\n",
"To output the loss in certain time intervals, a callback is implemented as a function in the following. Here a counter is incremented, every twentieth pass the loss function is called and the average error is printed out."
]
Expand Down Expand Up @@ -413,7 +412,7 @@
" legendfontsize=8, legend=:topright)\n",
" \n",
" Plots.plot!(fig, tSave, posSimple, label=\"SimpleFMU\", linewidth=2)\n",
" Plots.plot!(fig, tSave, posReal, label=\"RealFMU\", linewidth=2)\n",
" Plots.plot!(fig, tSave, posReal, label=\"fricFMU\", linewidth=2)\n",
" Plots.plot!(fig, time, posNeural, label=\"NeuralFMU\", linewidth=2)\n",
" fig\n",
"end"
Expand Down Expand Up @@ -658,7 +657,7 @@
"source": [
"#### Comparison of the plots\n",
"\n",
"Here three plots are compared with each other and only the position of the mass is considered. The first plot represents the *simpleFMU*, the second represents the *realFMU* (reference) and the third plot represents the result after training the NeuralFMU. "
"Here three plots are compared with each other and only the position of the mass is considered. The first plot represents the *simpleFMU*, the second represents the *fricFMU* (reference) and the third plot represents the result after training the NeuralFMU. "
]
},
{
Expand Down Expand Up @@ -716,7 +715,7 @@
"source": [
"### Summary\n",
"\n",
"Based on the plots, it can be seen that the NeuralFMU is able to adapt the friction model of the *realFMU*. After 1000 training steps, the curves already overlap quite well, but this can be further improved by longer training or a better initialization."
"Based on the plots, it can be seen that the NeuralFMU is able to adapt the friction model of the *fricFMU*. After 1000 training steps, the curves already overlap quite well, but this can be further improved by longer training or a better initialization."
]
},
{
Expand Down
6 changes: 5 additions & 1 deletion src/FMIFlux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ using FMIImport: fmi2SetTime, fmi2CompletedIntegratorStep, fmi2GetEventIndicator
using FMIImport: fmi2SampleJacobian, fmi2GetDirectionalDerivative, fmi2GetJacobian, fmi2GetJacobian!
using FMIImport: fmi2True, fmi2False

import FMIImport.FMICore: fmi2ValueReferenceFormat

include("optimiser.jl")
include("hotfixes.jl")
include("convert.jl")
include("flux_overload.jl")
include("neural.jl")
Expand All @@ -80,7 +84,7 @@ include("deprecated.jl")
include("batch.jl")
include("losses.jl")
include("scheduler.jl")
#include("optimiser.jl")
include("compatibility_check.jl")

# from Plots.jl
# No export here, Plots.plot is extended if available.
Expand Down
Loading
Loading