Skip to content

Commit

Permalink
add auto_interval to simulateCS
Browse files Browse the repository at this point in the history
  • Loading branch information
halentin committed Nov 29, 2024
1 parent 4cddc12 commit 6434aef
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/sim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6434aef

Please sign in to comment.