Skip to content

Commit 6c3b29d

Browse files
AnHeuermannclaude
andcommitted
Switch solver from Rodas5P to FBDF and log resolved solver settings
Replace Rodas5P with FBDF throughout run_simulate. Before solving, call init() to resolve all default kwargs (abstol, reltol, adaptive, dense, saveat) and write them to the simulation log. Fix saveat splatting bug where a NamedTuple was spread as positional args instead of kwargs, causing a convert(Vector{Float64}, ::Float64) error when the ODEProblem carries a scalar saveat in its own kwargs. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ec7f37d commit 6c3b29d

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
1010
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1111
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1212
OMJulia = "0f4fe800-344e-11e9-2949-fb537ad918e1"
13+
OrdinaryDiffEqBDF = "6ad6398a-0878-4a85-9266-38940aa047c8"
1314
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1415
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1516
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

src/BaseModelicaLibraryTesting.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import Pkg
44
import OMJulia
55
import OMJulia: sendExpression
66
import BaseModelica
7-
import DifferentialEquations: solve, Rodas5P, ReturnCode
7+
import DifferentialEquations: init, solve, ReturnCode
8+
import OrdinaryDiffEqBDF
89
import ModelingToolkit
910
import Dates: now
1011
import Printf: @sprintf

src/simulate.jl

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# ── Phase 3: ODE simulation with DifferentialEquations / MTK ──────────────────
22

3-
import DifferentialEquations: solve, Rodas5P, ReturnCode
3+
import DifferentialEquations: init, solve, ReturnCode
4+
import OrdinaryDiffEqBDF
45
import Logging
56
import ModelingToolkit
67
import Printf: @sprintf
78

89
"""
910
run_simulate(ode_prob, model_dir, model; cmp_signals, csv_max_size_mb) → (success, time, error, sol)
1011
11-
Solve `ode_prob` with Rodas5P (stiff solver). On success, also writes the
12+
Solve `ode_prob` with FBDF (stiff solver). On success, also writes the
1213
solution as a CSV file `<Short>_sim.csv` in `model_dir`.
1314
Writes a `<model>_sim.log` file in `model_dir`.
1415
Returns `nothing` as the fourth element on failure.
@@ -24,17 +25,18 @@ function run_simulate(ode_prob, model_dir::String,
2425
model::String;
2526
cmp_signals ::Vector{String} = String[],
2627
csv_max_size_mb::Int = CSV_MAX_SIZE_MB)::Tuple{Bool,Float64,String,Any}
27-
sim_success = false
28-
sim_time = 0.0
29-
sim_error = ""
30-
sol = nothing
28+
sim_success = false
29+
sim_time = 0.0
30+
sim_error = ""
31+
sol = nothing
32+
solver_settings_string = ""
3133

3234
log_file = open(joinpath(model_dir, "$(model)_sim.log"), "w")
3335
println(log_file, "Model: $model")
3436
logger = Logging.SimpleLogger(log_file, Logging.Debug)
3537
t0 = time()
3638
try
37-
# Rodas5P handles stiff DAE-like systems well.
39+
# FBDF handles stiff DAE-like systems and purely algebraic systems well.
3840
# Redirect all library log output (including Symbolics/MTK warnings)
3941
# to the log file so they don't clutter stdout.
4042
sol = Logging.with_logger(logger) do
@@ -46,7 +48,22 @@ function run_simulate(ode_prob, model_dir::String,
4648
saveat = isempty(ModelingToolkit.unknowns(sys)) ?
4749
collect(range(ode_prob.tspan[1], ode_prob.tspan[end]; length = 500)) :
4850
Float64[]
49-
solve(ode_prob, Rodas5P(); saveat = saveat, dense = true)
51+
kwargs = (saveat = saveat, dense = true)
52+
53+
# Log solver settings
54+
initializedSolver = init(ode_prob, OrdinaryDiffEqBDF.FBDF(); kwargs...)
55+
solver_settings_string =
56+
"""
57+
OrdinaryDiffEqBDF.FBDF()
58+
saveat: $(let sv = initializedSolver.opts.saveat; isempty(sv) ? "[]" : "$(length(sv)) points in [$(first(sv)), $(last(sv))]" end)
59+
abstol: $(@sprintf("%.2e", initializedSolver.opts.abstol))
60+
reltol: $(@sprintf("%.2e", initializedSolver.opts.reltol))
61+
adaptive: $(initializedSolver.opts.adaptive)
62+
dense: $(initializedSolver.opts.dense)
63+
"""
64+
65+
# Solve
66+
solve(ode_prob, OrdinaryDiffEqBDF.FBDF(); kwargs...)
5067
end
5168
sim_time = time() - t0
5269
if sol.retcode == ReturnCode.Success
@@ -67,7 +84,8 @@ function run_simulate(ode_prob, model_dir::String,
6784
sim_time = time() - t0
6885
sim_error = sprint(showerror, e, catch_backtrace())
6986
end
70-
println(log_file, "Time: $(round(sim_time; digits=3)) s")
87+
println(log_file, "Solver settings: $solver_settings_string")
88+
println(log_file, "Time: $(round(sim_time; digits=3)) s")
7189
println(log_file, "Success: $sim_success")
7290
isempty(sim_error) || println(log_file, "\n--- Error ---\n$sim_error")
7391
close(log_file)

0 commit comments

Comments
 (0)