Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ThummeTo committed Feb 3, 2024
1 parent 63389bb commit dd493fb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
22 changes: 11 additions & 11 deletions test/FMI2/performance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fmu = fmiLoad("BouncingBall1D", "Dymola", "2022x"; type=:ME)

c = fmi2Instantiate!(fmu)

function evalBenchmark(b)
evalBenchmark = function(b)
res = run(b)
min_time = min(res.times...)
memory = res.memory
Expand Down Expand Up @@ -152,28 +152,28 @@ min_time, memory, allocs = evalBenchmark(b)

b = @benchmarkable $c(dx=$dx, y=$y, y_refs=$y_refs, x=$x, u=$u, u_refs=$u_refs, p=$p, p_refs=$p_refs, ec=$ec, ec_idcs=$ec_idcs, t=$t)
min_time, memory, allocs = evalBenchmark(b)
@test allocs <= 3 # `ignore_derivatives` causes an extra 3 allocations (48 bytes)
@test memory <= 48 # ToDo
@test allocs <= 9 # [ToDo] 3 `ignore_derivatives` causes an extra 3 allocations (48 bytes)
@test memory <= 224 # [ToDo] reduce again 48

_p = ()
b = @benchmarkable FMI.fx($c, $dx, $x, $_p, $t, nothing)
min_time, memory, allocs = evalBenchmark(b)
# ToDo: This is too much, but currently necessary to be compatible with all AD-frameworks, as well as ForwardDiffChainRules
@test allocs <= 3
@test memory <= 48 # ToDo
@test allocs <= 9 # [ToDo]3
@test memory <= 224 # [ToDo] reduce again 48

# AD

using FMISensitivity
import ChainRulesCore
import ChainRulesCore: ZeroTangent, NoTangent
import FMISensitivity.ChainRulesCore
import FMISensitivity.ChainRulesCore: ZeroTangent, NoTangent
import FMISensitivity.ForwardDiff
import FMISensitivity.ReverseDiff

# frule
Δx = similar(x)
Δtuple = (NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), Δx, NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent())
function fun(_x)
fun = function(_x)
Ω, ∂Ω = ChainRulesCore.frule(Δtuple, eval!, cRef, dx, dx_refs, y, y_refs, _x, u, u_refs, p, p_refs, ec, ec_idcs, t)
end

Expand All @@ -183,7 +183,7 @@ min_time, memory, allocs = evalBenchmark(b)
@test memory <= 144

# rrule
function fun(_x)
fun = function (_x)
Ω, pullback = ChainRulesCore.rrule(eval!, cRef, dx, dx_refs, y, y_refs, _x, u, u_refs, p, p_refs, ec, ec_idcs, t)
end

Expand All @@ -195,7 +195,7 @@ min_time, memory, allocs = evalBenchmark(b)
# rrule pullback
Ω, pullback = ChainRulesCore.rrule(eval!, cRef, dx, dx_refs, y, y_refs, x, u, u_refs, p, p_refs, ec, ec_idcs, t)
= copy(dx)
function fun(_r̄, _pullback)
fun = function(_r̄, _pullback)
_pullback(_r̄)
end

Expand All @@ -205,7 +205,7 @@ min_time, memory, allocs = evalBenchmark(b)
@test memory <= 144

# eval!
function fun(_x)
fun = function(_x)
eval!(cRef, dx, dx_refs, y, y_refs, _x, u, u_refs, p, p_refs, ec, ec_idcs, t)
end

Expand Down
4 changes: 2 additions & 2 deletions test/FMI2/sim_CS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ fmiUnload(myFMU)

# case 2: CS-FMU with input signal

function extForce_t(t::Real, u::AbstractArray{<:Real})
extForce_t = function (t::Real, u::AbstractArray{<:Real})
u[1] = sin(t)
end

function extForce_ct(c::Union{FMU2Component, Nothing}, t::Real, u::AbstractArray{<:Real})
extForce_ct = function (c::Union{FMU2Component, Nothing}, t::Real, u::AbstractArray{<:Real})
u[1] = sin(t)
end

Expand Down
6 changes: 3 additions & 3 deletions test/FMI2/sim_ME.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ dtmax_inputs = 1e-3
rand_x0 = rand(2)

kwargs = Dict(:dtmin => 1e-64, :abstol => 1e-8, :reltol => 1e-6, :dt => 1e-32)
solvers = [Rodas5()] # [Tsit5(), FBDF(autodiff=false), FBDF(autodiff=true), Rodas5(autodiff=false), Rodas5(autodiff=true)]
solvers = [Tsit5(), Rodas5(autodiff=false)] # [Tsit5(), FBDF(autodiff=false), FBDF(autodiff=true), Rodas5(autodiff=false), Rodas5(autodiff=true)]

function extForce_t(t::Real, u::AbstractArray{<:Real})
extForce_t = function(t::Real, u::AbstractArray{<:Real})
sense_setindex!(u, sin(t), 1)
end

function extForce_cxt(c::Union{FMU2Component, Nothing}, x::Union{AbstractArray{<:Real}, Nothing}, t::Real, u::AbstractArray{<:Real})
extForce_cxt = function(c::Union{FMU2Component, Nothing}, x::Union{AbstractArray{<:Real}, Nothing}, t::Real, u::AbstractArray{<:Real})
x1 = 0.0
if x != nothing
x1 = x[1]
Expand Down
2 changes: 1 addition & 1 deletion test/FMI2/sim_zero_state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ t_stop = 8.0
solver=FBDF(autodiff=false)
dtmax = 0.01

function extForce_t!(t, u)
extForce_t! = function(t, u)
u[1] = sin(t)
end

Expand Down
90 changes: 45 additions & 45 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,50 @@ function runtestsFMI2(exportingTool)
@testset "Functions for $str" begin
ENV["FMUSTRUCT"] = str

# @info "Variable Getters / Setters (getter_setter.jl)"
# @testset "Variable Getters / Setters" begin
# include("FMI2/getter_setter.jl")
# end

# @info "Execution Configurations (exec_config.jl)"
# @testset "Execution Configurations" begin
# include("FMI2/exec_config.jl")
# end

# @info "State Manipulation (state.jl)"
# @testset "State Manipulation" begin
# include("FMI2/state.jl")
# end

# @info "Automatic Simulation (sim_auto.jl)"
# @testset "Automatic Simulation (CS or ME)" begin
# include("FMI2/sim_auto.jl")
# end

# @info "CS Simulation (sim_CS.jl)"
# @testset "CS Simulation" begin
# include("FMI2/sim_CS.jl")
# end
@info "Variable Getters / Setters (getter_setter.jl)"
@testset "Variable Getters / Setters" begin
include("FMI2/getter_setter.jl")
end

@info "Execution Configurations (exec_config.jl)"
@testset "Execution Configurations" begin
include("FMI2/exec_config.jl")
end

@info "State Manipulation (state.jl)"
@testset "State Manipulation" begin
include("FMI2/state.jl")
end

@info "Automatic Simulation (sim_auto.jl)"
@testset "Automatic Simulation (CS or ME)" begin
include("FMI2/sim_auto.jl")
end

@info "CS Simulation (sim_CS.jl)"
@testset "CS Simulation" begin
include("FMI2/sim_CS.jl")
end

@info "ME Simulation (sim_ME.jl)"
@testset "ME Simulation" begin
include("FMI2/sim_ME.jl")
end

# @info "Support CS and ME simultaneously (cs_me.jl)"
# @testset "Support CS and ME simultaneously" begin
# include("FMI2/cs_me.jl")
# end
@info "Support CS and ME simultaneously (cs_me.jl)"
@testset "Support CS and ME simultaneously" begin
include("FMI2/cs_me.jl")
end

# @info "Simulation FMU without states (sim_zero_state.jl)"
# @testset "Simulation FMU without states" begin
# include("FMI2/sim_zero_state.jl")
# end
@info "Simulation FMU without states (sim_zero_state.jl)"
@testset "Simulation FMU without states" begin
include("FMI2/sim_zero_state.jl")
end

# @info "Loading/Saving simulation results (load_save.jl)"
# @testset "Loading/Saving simulation results" begin
# include("FMI2/load_save.jl")
# end
@info "Loading/Saving simulation results (load_save.jl)"
@testset "Loading/Saving simulation results" begin
include("FMI2/load_save.jl")
end
end
end

Expand All @@ -87,13 +87,13 @@ function runtestsFMI2(exportingTool)
# include("FMI2/performance.jl")
# end
# else
# @info "Julia Version $(VERSION), skipping performance tests ..."
# end
@info "Julia Version $(VERSION), skipping performance tests ..."
#end

# @info "Plotting (plots.jl)"
# @testset "Plotting" begin
# include("FMI2/plots.jl")
# end
@info "Plotting (plots.jl)"
@testset "Plotting" begin
include("FMI2/plots.jl")
end
end
end

Expand Down Expand Up @@ -134,9 +134,9 @@ function runtestsFMI3(exportingTool)
end
end

# @testset "Plotting (plots.jl)" begin
# include("FMI3/plots.jl")
# end
@testset "Plotting (plots.jl)" begin
include("FMI3/plots.jl")
end
end
end

Expand Down

0 comments on commit dd493fb

Please sign in to comment.