Skip to content

Commit

Permalink
v0.10.5 (#93)
Browse files Browse the repository at this point in the history
* fix for documentatio script / examples

* modified tutotial

* workshop enhanced

* tutorial

* final workshop

* tutorial juliacon
  • Loading branch information
ThummeTo committed Jul 25, 2023
1 parent f8f26eb commit e766d40
Show file tree
Hide file tree
Showing 7 changed files with 2,177 additions and 236 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
env:
FILE: examples/src/${{ matrix.file-name }}.ipynb
run: |
jupyter nbconvert --ExecutePreprocessor.kernel_name="julia-1.8" --to notebook --inplace --execute ${{ env.FILE }}
jupyter nbconvert --ExecutePreprocessor.kernel_name="julia-1.9" --to notebook --inplace --execute ${{ env.FILE }}
jupyter nbconvert --to script ${{ env.FILE }}
jupyter nbconvert --to markdown ${{ env.FILE }}
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FMIFlux"
uuid = "fabad875-0d53-4e47-9446-963b74cae21f"
version = "0.10.4"
version = "0.10.5"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
Binary file modified docs/src/examples/img/juliacon_2023/neuralfmu_topology.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion examples/src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
params/
*.png
*.png
*.gif
2,271 changes: 2,108 additions & 163 deletions examples/src/juliacon_2023.ipynb

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions examples/src/juliacon_2023_distributedhyperopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ nprocs()
workers = addprocs(5)
@everywhere include(joinpath(@__DIR__, "workshop_module.jl"))

# set the current hyperparameter optimization run
@everywhere NODE_Training.HPRUN = 1
@info "Run: $(NODE_Training.HPRUN)"

# creating paths for log files (logs), parameter sets (params) and hyperparameter plots (plots)
for dir ("logs", "params", "plots", "results")
path = joinpath(@__DIR__, dir, "$(NODE_Training.HPRUN)")
for dir ("logs", "params", "plots")
path = joinpath(@__DIR__, dir)
@info "Creating (if not already) path: $(path)"
mkpath(path)
end
Expand All @@ -43,8 +39,8 @@ DistributedHyperOpt.optimize(optimization;
sampler=sampler,
plot=true,
plot_ressources=true,
save_plot=joinpath(@__DIR__, "plots", "$(NODE_Training.HPRUN)", "hyperoptim.png"),
redirect_worker_io_dir=joinpath(@__DIR__, "logs", "$(NODE_Training.HPRUN)"))
save_plot=joinpath(@__DIR__, "plots", "hyperoptim.png"),
redirect_worker_io_dir=joinpath(@__DIR__, "logs"))

Plots.plot(optimization; size=(1024, 1024), ressources=true)
minimum, minimizer, ressource = DistributedHyperOpt.results(optimization)
123 changes: 61 additions & 62 deletions examples/src/juliacon_2023_helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@
using LaTeXStrings

import FMIFlux: roundToLength
import FMIZoo:movavg
import FMIZoo: movavg

import FMI: FMU2Solution
import FMIZoo: VLDM, VLDM_Data

function plotANNError(neuralFMU::NeuralFMU, data::FMIZoo.VLDM_Data; reductionFactor::Int=10, field=:consumption, mov_avg::Int=100, filename=nothing)
function fmiSingleInstanceMode(fmu::FMU2, mode::Bool)
if mode
# switch to a more efficient execution configuration, allocate only a single FMU instance, see:
# https://thummeto.github.io/FMI.jl/dev/features/#Execution-Configuration
fmu.executionConfig = FMI.FMIImport.FMU2_EXECUTION_CONFIGURATION_NOTHING
c, _ = FMIFlux.prepareSolveFMU(fmu, nothing, fmu.type, true, false, false, false, true, data.params; x0=x0)
else
c = FMI.getCurrentComponent(fmu)
# switch back to the default execution configuration, allocate a new FMU instance for every run, see:
# https://thummeto.github.io/FMI.jl/dev/features/#Execution-Configuration
fmu.executionConfig = FMI.FMIImport.FMU2_EXECUTION_CONFIGURATION_NO_RESET
FMIFlux.finishSolveFMU(fmu, c, false, true)
end
return nothing
end

function dataIndexForTime(t::Real)
return 1+round(Int, t/dt)
end

function plotEnhancements(neuralFMU::NeuralFMU, fmu::FMU2, data::FMIZoo.VLDM_Data; reductionFactor::Int=10, mov_avg::Int=100, filename=nothing)
colorMin = 0
colorMax = 0
okregion = 0
Expand All @@ -19,7 +39,8 @@ function plotANNError(neuralFMU::NeuralFMU, data::FMIZoo.VLDM_Data; reductionFac
tStart = data.consumption_t[1]
tStop = data.consumption_t[end]
x0 = FMIZoo.getStateVector(data, tStart)
result = neuralFMU(x0, (tStart, tStop); parameters=data.params, showProgress=true, recordValues=:derivatives)
resultNFMU = neuralFMU(x0, (tStart, tStop); parameters=data.params, showProgress=false, recordValues=:derivatives, saveat=data.consumption_t)
resultFMU = fmiSimulate(fmu, (tStart, tStop); parameters=data.params, showProgress=false, recordValues=:derivatives, saveat=data.consumption_t)

# Finite differences for acceleration
dt = data.consumption_t[2]-data.consumption_t[1]
Expand All @@ -28,40 +49,38 @@ function plotANNError(neuralFMU::NeuralFMU, data::FMIZoo.VLDM_Data; reductionFac
acceleration_dev = (data.speed_dev[2:end] - data.speed_dev[1:end-1]) / dt
acceleration_dev = [acceleration_dev..., 0.0]

ANNInputs = fmiGetSolutionValue(result, :derivatives) # collect([0.0, 0.0, 0.0, data.speed_val[i], acceleration_val[i], data.consumption_val[i]] for i in 1:length(data.consumption_t))
ANNInputs = fmiGetSolutionValue(resultNFMU, :derivatives) # collect([0.0, 0.0, 0.0, data.speed_val[i], acceleration_val[i], data.consumption_val[i]] for i in 1:length(data.consumption_t))
ANNInputs = collect([ANNInputs[1][i], ANNInputs[2][i], ANNInputs[3][i], ANNInputs[4][i], ANNInputs[5][i], ANNInputs[6][i]] for i in 1:length(ANNInputs[1]))
ANNOutputs = fmiGetSolutionDerivative(result, 5:6; isIndex=true)

ANNOutputs = fmiGetSolutionDerivative(resultNFMU, 5:6; isIndex=true)
ANNOutputs = collect([ANNOutputs[1][i], ANNOutputs[2][i]] for i in 1:length(ANNOutputs[1]))

ANN_error = nothing

if field == :consumption
ANN_consumption = collect(o[2] for o in ANNOutputs)
ANN_error = ANN_consumption - data.consumption_val
ANN_error = collect(ANN_error[i] > 0.0 ? max(0.0, ANN_error[i]-data.consumption_dev[i]) : min(0.0, ANN_error[i]+data.consumption_dev[i]) for i in 1:length(data.consumption_t))

label = L"consumption [W]"
colorMin=-610.0
colorMax=610.0
else # :acceleration
ANN_acceleration = collect(o[1] for o in ANNOutputs)
ANN_error = ANN_acceleration - acceleration_val
ANN_error = collect(ANN_error[i] > 0.0 ? max(0.0, ANN_error[i]-acceleration_dev[i]) : min(0.0, ANN_error[i]+acceleration_dev[i]) for i in 1:length(data.consumption_t))

label = L"acceleration [m/s^2]"
colorMin=-0.04
colorMax=0.04
end
FMUOutputs = fmiGetSolutionDerivative(resultFMU, 5:6; isIndex=true)
FMUOutputs = collect([FMUOutputs[1][i], FMUOutputs[2][i]] for i in 1:length(FMUOutputs[1]))

ANN_consumption = collect(o[2] for o in ANNOutputs)
ANN_error = ANN_consumption - data.consumption_val
ANN_error = collect(ANN_error[i] > 0.0 ? max(0.0, ANN_error[i]-data.consumption_dev[i]) : min(0.0, ANN_error[i]+data.consumption_dev[i]) for i in 1:length(data.consumption_t))

FMU_consumption = collect(o[2] for o in FMUOutputs)
FMU_error = FMU_consumption - data.consumption_val
FMU_error = collect(FMU_error[i] > 0.0 ? max(0.0, FMU_error[i]-data.consumption_dev[i]) : min(0.0, FMU_error[i]+data.consumption_dev[i]) for i in 1:length(data.consumption_t))

colorMin=-231.0
colorMax=231.0

FMU_error = movavg(FMU_error, mov_avg)
ANN_error = movavg(ANN_error, mov_avg)

ANN_error = ANN_error .- FMU_error

ANNInput_vel = collect(o[4] for o in ANNInputs)
ANNInput_acc = collect(o[5] for o in ANNInputs)
ANNInput_con = collect(o[6] for o in ANNInputs)

_max = max(ANN_error...)
_min = min(ANN_error...)
neutral = -colorMin/(colorMax-colorMin) # -_min/(_max-_min)
neutral = 0.5

if _max > colorMax
@warn "max value ($(_max)) is larger than colorMax ($(colorMax)) - values will be cut"
Expand All @@ -71,31 +90,27 @@ function plotANNError(neuralFMU::NeuralFMU, data::FMIZoo.VLDM_Data; reductionFac
@warn "min value ($(_min)) is smaller than colorMin ($(colorMin)) - values will be cut"
end

ANN_error = collect(min(max(e, colorMin), colorMax) for e in ANN_error)

@info "$(_min) $(_max) $(neutral)"

anim = @animate for ang in 0:5:360
l = Plots.@layout [Plots.grid(3,1) r{0.85w}]
fig = Plots.plot(layout=l, size=(1600,800), left_margin = 10Plots.mm, right_margin = 10Plots.mm, bottom_margin = 10Plots.mm)

colorgrad = cgrad([:orange, :white, :blue], [0.0, 0.5, 1.0]) # , scale = :log)

colorgrad = cgrad([:green, :white, :red], [0.0, 0.5, 1.0]) # , scale = :log)
scatter!(fig[1], ANNInput_vel[1:reductionFactor:end], ANNInput_acc[1:reductionFactor:end],
xlabel=L"velocity [m/s]", ylabel=L"acceleration [m/s^2]",
xlabel="velocity [m/s]", ylabel="acceleration [m/s^2]",
color=colorgrad, zcolor=ANN_error[1:reductionFactor:end], label=:none, colorbar=:none) #

scatter!(fig[2], ANNInput_acc[1:reductionFactor:end], ANNInput_con[1:reductionFactor:end],
xlabel=L"acceleration [m/s^2]", ylabel=L"consumption [W]",
xlabel="acceleration [m/s^2]", ylabel="consumption [W]",
color=colorgrad, zcolor=ANN_error[1:reductionFactor:end], label=:none, colorbar=:none) #

scatter!(fig[3], ANNInput_vel[1:reductionFactor:end], ANNInput_con[1:reductionFactor:end],
xlabel=L"velocity [m/s]", ylabel=L"consumption [W]",
xlabel="velocity [m/s]", ylabel="consumption [W]",
color=colorgrad, zcolor=ANN_error[1:reductionFactor:end], label=:none, colorbar=:none) #

scatter!(fig[4], ANNInput_vel[1:reductionFactor:end], ANNInput_acc[1:reductionFactor:end], ANNInput_con[1:reductionFactor:end],
xlabel=L"velocity [m/s]", ylabel=L"acceleration [m/s^2]", zlabel=L"consumption [W]",
color=colorgrad, zcolor=ANN_error[1:reductionFactor:end], markersize=8, label=:none, camera=(ang,20), colorbar_title=" \n\n\n\n" * L"Δ" * label * " (smoothed)") #
xlabel="velocity [m/s]", ylabel="acceleration [m/s^2]", zlabel="consumption [W]",
color=colorgrad, zcolor=ANN_error[1:reductionFactor:end], markersize=8, label=:none, camera=(ang,20), colorbar_title=" \n\n\n\n" * L"ΔMAE" * " (smoothed)")

# draw invisible dummys to scale colorbar to fixed size
for i in 1:3
Expand All @@ -110,7 +125,11 @@ function plotANNError(neuralFMU::NeuralFMU, data::FMIZoo.VLDM_Data; reductionFac
end
end

return gif(anim, filename; fps=10)
if !isnothing(filename)
return gif(anim, filename; fps=10)
else
return gif(anim; fps=10)
end
end

function plotCumulativeConsumption(solutionNFMU::FMU2Solution, solutionFMU::FMU2Solution, data::FMIZoo.VLDM_Data; range=(0.0,1.0), filename=nothing)
Expand Down Expand Up @@ -159,24 +178,4 @@ function simPlotCumulativeConsumption(cycle::Symbol, filename=nothing; kwargs...
savefig(fig, filename)
end
return fig
end

function checkMSE(cycle; init::Bool=false)

data = FMIZoo.VLDM(cycle)
tStart = data.consumption_t[1]
tStop = data.consumption_t[end]
tSave = data.consumption_t

if init
c = FMI.FMIImport.getCurrentComponent(fmu)
FMI.FMIImport.fmi2SetFMUstate(c, batch[1].initialState)
c.eventInfo = deepcopy(batch[1].initialEventInfo)
c.t = batch[1].tStart
end
resultNFMU = neuralFMU(x0, (tStart, tStop); parameters=data.params, showProgress=true, maxiters=1e7, saveat=tSave)

mse_NFMU = FMIFlux.Losses.mse_dev(data.cumconsumption_val, fmiGetSolutionState(resultNFMU, 6; isIndex=true), data.cumconsumption_dev)

return mse_NFMU
end
end

2 comments on commit e766d40

@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/88313

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.10.5 -m "<description of version>" e766d40443c6ecc626cf1d01a434769dfac8bf95
git push origin v0.10.5

Please sign in to comment.