-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_bm.jl
More file actions
54 lines (49 loc) · 2.06 KB
/
parse_bm.jl
File metadata and controls
54 lines (49 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ── Phase 2: Base Modelica parsing with BaseModelica.jl ───────────────────────
import BaseModelica
import Logging
"""
run_parse(bm_path, model_dir, model) → (success, time, error, ode_prob)
Parse a Base Modelica `.bmo` file with BaseModelica.jl and create an
`ODEProblem` from the `Experiment` annotation.
Writes a `<model>_parsing.log` file in `model_dir`.
Returns `nothing` as the fourth element on failure.
"""
function run_parse(bm_path::String, model_dir::String,
model::String)::Tuple{Bool,Float64,String,Any}
parse_success = false
parse_time = 0.0
parse_error = ""
ode_prob = nothing
isdir(model_dir) || mkpath(model_dir)
log_file = open(joinpath(model_dir, "$(model)_parsing.log"), "w")
stdout_pipe = Pipe()
println(log_file, "Model: $model")
logger = Logging.SimpleLogger(log_file, Logging.Debug)
t0 = time()
try
# create_odeproblem returns an ODEProblem using the Experiment
# annotation for StartTime/StopTime/Tolerance/Interval.
# Redirect Julia log output to the log file and stdout/stderr to a
# buffer so they can be appended after the summary lines.
ode_prob = redirect_stdout(stdout_pipe) do
redirect_stderr(stdout_pipe) do
Logging.with_logger(logger) do
BaseModelica.create_odeproblem(bm_path)
end
end
end
parse_time = time() - t0
parse_success = true
catch e
parse_time = time() - t0
parse_error = sprint(showerror, e, catch_backtrace())
end
close(stdout_pipe.in)
captured = read(stdout_pipe.out, String)
println(log_file, "Time: $(round(parse_time; digits=3)) s")
println(log_file, "Success: $parse_success")
isempty(captured) || print(log_file, "\n--- Parser output ---\n", captured)
isempty(parse_error) || println(log_file, "\n--- Error ---\n$parse_error")
close(log_file)
return parse_success, parse_time, parse_error, ode_prob
end