Skip to content

Commit 28eb81b

Browse files
AnHeuermannclaude
andauthored
Suppress library warnings on stdout; redirect them to per-phase log files (#21)
Wrap the BaseModelica.create_odeproblem and DifferentialEquations.solve calls with Logging.with_logger(SimpleLogger(log_file)) so that warnings from Symbolics, MTK, and the solver (e.g. "Did not converge after maxiters substitutions") are captured in the existing _parsing.log / _sim.log files instead of cluttering stdout. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 5f95ebd commit 28eb81b

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ authors = ["AnHeuermann"]
77
BaseModelica = "a17d5099-185d-4ff5-b5d3-51aa4569e56d"
88
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
99
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
10+
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1011
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1112
OMJulia = "0f4fe800-344e-11e9-2949-fb537ad918e1"
1213
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

src/parse_bm.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ── Phase 2: Base Modelica parsing with BaseModelica.jl ───────────────────────
22

33
import BaseModelica
4+
import Logging
45

56
"""
67
run_parse(bm_path, model_dir, model) → (success, time, error, ode_prob)
@@ -17,24 +18,28 @@ function run_parse(bm_path::String, model_dir::String,
1718
parse_error = ""
1819
ode_prob = nothing
1920

21+
log_file = open(joinpath(model_dir, "$(model)_parsing.log"), "w")
22+
println(log_file, "Model: $model")
23+
logger = Logging.SimpleLogger(log_file, Logging.Debug)
2024
t0 = time()
2125
try
2226
# create_odeproblem returns an ODEProblem using the Experiment
2327
# annotation for StartTime/StopTime/Tolerance/Interval.
24-
ode_prob = BaseModelica.create_odeproblem(bm_path)
28+
# Redirect all library log output (including Symbolics warnings)
29+
# to the log file so they don't clutter stdout.
30+
ode_prob = Logging.with_logger(logger) do
31+
BaseModelica.create_odeproblem(bm_path)
32+
end
2533
parse_time = time() - t0
2634
parse_success = true
2735
catch e
2836
parse_time = time() - t0
2937
parse_error = sprint(showerror, e, catch_backtrace())
3038
end
31-
32-
open(joinpath(model_dir, "$(model)_parsing.log"), "w") do f
33-
println(f, "Model: $model")
34-
println(f, "Time: $(round(parse_time; digits=3)) s")
35-
println(f, "Success: $parse_success")
36-
isempty(parse_error) || println(f, "\n--- Error ---\n$parse_error")
37-
end
39+
println(log_file, "Time: $(round(parse_time; digits=3)) s")
40+
println(log_file, "Success: $parse_success")
41+
isempty(parse_error) || println(log_file, "\n--- Error ---\n$parse_error")
42+
close(log_file)
3843

3944
return parse_success, parse_time, parse_error, ode_prob
4045
end

src/simulate.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ── Phase 3: ODE simulation with DifferentialEquations / MTK ──────────────────
22

33
import DifferentialEquations: solve, Rodas5P, ReturnCode
4+
import Logging
45
import ModelingToolkit
56
import Printf: @sprintf
67

@@ -19,10 +20,17 @@ function run_simulate(ode_prob, model_dir::String,
1920
sim_error = ""
2021
sol = nothing
2122

23+
log_file = open(joinpath(model_dir, "$(model)_sim.log"), "w")
24+
println(log_file, "Model: $model")
25+
logger = Logging.SimpleLogger(log_file, Logging.Debug)
2226
t0 = time()
2327
try
2428
# Rodas5P handles stiff DAE-like systems well.
25-
sol = solve(ode_prob, Rodas5P())
29+
# Redirect all library log output (including Symbolics/MTK warnings)
30+
# to the log file so they don't clutter stdout.
31+
sol = Logging.with_logger(logger) do
32+
solve(ode_prob, Rodas5P())
33+
end
2634
sim_time = time() - t0
2735
if sol.retcode == ReturnCode.Success
2836
sim_success = true
@@ -33,13 +41,10 @@ function run_simulate(ode_prob, model_dir::String,
3341
sim_time = time() - t0
3442
sim_error = sprint(showerror, e, catch_backtrace())
3543
end
36-
37-
open(joinpath(model_dir, "$(model)_sim.log"), "w") do f
38-
println(f, "Model: $model")
39-
println(f, "Time: $(round(sim_time; digits=3)) s")
40-
println(f, "Success: $sim_success")
41-
isempty(sim_error) || println(f, "\n--- Error ---\n$sim_error")
42-
end
44+
println(log_file, "Time: $(round(sim_time; digits=3)) s")
45+
println(log_file, "Success: $sim_success")
46+
isempty(sim_error) || println(log_file, "\n--- Error ---\n$sim_error")
47+
close(log_file)
4348

4449
# Write simulation results CSV (time + all state variables)
4550
if sim_success && sol !== nothing

0 commit comments

Comments
 (0)