Skip to content

Commit

Permalink
gracefully catch any error
Browse files Browse the repository at this point in the history
  • Loading branch information
alemelis committed Jun 6, 2024
1 parent bff2e18 commit 95a376f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "openBF"
uuid = "e815b1a4-10eb-11ea-25f1-272ff651e618"
authors = ["alessandro <[email protected]>"]
version = "2.6.2"
version = "2.7.0"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand Down
100 changes: 53 additions & 47 deletions src/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,65 +129,71 @@ function run_simulation(
counter = 1
conv_error::Float64 = floatmax()
converged = false
stats = @timed while true

# step
dt = calculateΔt(network)
solve!(network, dt, current_time)
update_ghost_cells!(network)
verbose && next!(prog)

if current_time >= checkpoints[counter]
save_waveforms.(counter, current_time, values(network.vessels))
counter += 1
end
try
stats = @timed while true

# at the end of the cardiac cycle
if (current_time - heart.cardiac_period * passed_cycles) >= heart.cardiac_period &&
(current_time - heart.cardiac_period * passed_cycles + dt) > heart.cardiac_period
# step
dt = calculateΔt(network)
solve!(network, dt, current_time)
update_ghost_cells!(network)
verbose && next!(prog)

# check convergence
if passed_cycles > 0
# compute_pressure(network)
conv_error, error_loc = get_conv_error(network)
if current_time >= checkpoints[counter]
save_waveforms.(counter, current_time, values(network.vessels))
counter += 1
end

flush_waveforms.(values(network.vessels))
out_files && append_last_to_out.(values(network.vessels))
# at the end of the cardiac cycle
if (current_time - heart.cardiac_period * passed_cycles) >= heart.cardiac_period &&
(current_time - heart.cardiac_period * passed_cycles + dt) > heart.cardiac_period

if verbose
# check convergence
if passed_cycles > 0
finish!(
prog,
showvalues = [("RMSE (mmHg)", conv_error), ("@", error_loc)],
)
else
finish!(prog)
# compute_pressure(network)
conv_error, error_loc = get_conv_error(network)
end
println()
end

checkpoints = checkpoints .+ heart.cardiac_period
passed_cycles += 1
prog = getprog(passed_cycles, verbose)
counter = 1
end
flush_waveforms.(values(network.vessels))
out_files && append_last_to_out.(values(network.vessels))

if verbose
if passed_cycles > 0
finish!(
prog,
showvalues = [("RMSE (mmHg)", conv_error), ("@", error_loc)],
)
else
finish!(prog)
end
println()
end

checkpoints = checkpoints .+ heart.cardiac_period
passed_cycles += 1
prog = getprog(passed_cycles, verbose)
counter = 1
end

# at the end of the simulation
if current_time >= total_time ||
passed_cycles == config["solver"]["cycles"] ||
conv_error < config["solver"]["convergence_tolerance"]
verbose && finish!(prog)
converged = true
break
# at the end of the simulation
if current_time >= total_time ||
passed_cycles == config["solver"]["cycles"] ||
conv_error < config["solver"]["convergence_tolerance"]
verbose && finish!(prog)
converged = true
break
end
current_time += dt
end
current_time += dt
tokeep::Vector{String} = get(config, "write_results", [])
cleanup.(values(network.vessels), Ref(tokeep))

verbose && printstats(stats, converged, passed_cycles)
save_stats && savestats(stats, converged, passed_cycles, config["project_name"])
catch e
println("\nAn error occurred, terminating simulation.\n")
println(e)
end
tokeep::Vector{String} = get(config, "write_results", [])
cleanup.(values(network.vessels), Ref(tokeep))

verbose && printstats(stats, converged, passed_cycles)
save_stats && savestats(stats, converged, passed_cycles, config["project_name"])

cd(initial_dir)
end
Expand Down

0 comments on commit 95a376f

Please sign in to comment.