From 6434aef8e69dae7134aa99def8e1a145e0a8f816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20H=C3=B6pfner?= Date: Fri, 29 Nov 2024 14:41:10 +0100 Subject: [PATCH] add auto_interval to simulateCS --- src/sim.jl | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/sim.jl b/src/sim.jl index 0b2c03ee..e0edb894 100644 --- a/src/sim.jl +++ b/src/sim.jl @@ -291,7 +291,33 @@ simulateME(fmu::FMU, tspan::Tuple{Float64,Float64}; kwargs...) = export simulateME ############ Co-Simulation ############ - +function auto_interval(t) + """ + Find a nice interval that divides t into 500 - 1000 steps + """ + # Initial interval estimation + h = 10 ^ (round(log10(t)) - 3) + + # Number of samples + n_samples = t / h + + # Adjust interval based on number of samples + if n_samples >= 2500 + h *= 5 + elseif n_samples >= 2000 + h *= 4 + elseif n_samples >= 1000 + h *= 2 + elseif n_samples <= 200 + h /= 5 + elseif n_samples <= 250 + h /= 4 + elseif n_samples <= 500 + h /= 2 + end + + return h +end """ simulateCS(fmu, instance=nothing, tspan=nothing; kwargs...) simulateCS(fmu, tspan; kwargs...) @@ -394,7 +420,7 @@ function simulateCS( tolerance = tolerance === nothing ? 0.0 : tolerance dt = dt === nothing ? getDefaultStepSize(fmu.modelDescription) : dt - dt = dt === nothing ? 1e-3 : dt + dt = dt === nothing ? auto_interval(t_stop-t_start) : dt @debug "Simulating CS-FMU: Preparing inputs ..." inputs = nothing