Skip to content

Commit

Permalink
add to_save flag to Vessel
Browse files Browse the repository at this point in the history
  • Loading branch information
alemelis committed Mar 21, 2024
1 parent b47dad0 commit a83e672
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 37 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "2.0.0"
[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
4 changes: 3 additions & 1 deletion docs/src/man/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Following the `write_results` list is defined.
write_results: ["P"] # ["P", "Q", "u", "A"]
```

Here you can specify which quantities to write in the output files (`P`ressure, `Q` flow, `u` velocity and `A`rea). These are all optional except for `P`.
Here you can specify which quantities to write in the output files (`P`ressure, `Q` flow, `u` velocity and `A`rea). These are all optional but at least one is recommended.

Then there are three main sections: `solver`, `blood`, and `network`.

Expand Down Expand Up @@ -69,6 +69,8 @@ This contains the list of vessels in the network. Each vessel has the following

__Optional__ parameters are:

- `to_save` a boolean flag (default `true`) to tell openBF to save results for the current vessel;

- `M` is the number of division along the vessels used to compute the artery $\Delta x$. When not specified, openBF automatically meshes the arteries so that $\Delta x$ is at least $1 mm$;

- `Pext` vessel external pressure in $Pa$, default $0.0 Pa$;
Expand Down
1 change: 1 addition & 0 deletions src/openBF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using LinearAlgebra
using DelimitedFiles

using YAML
using Glob
using Graphs
using ProgressMeter
using Statistics
Expand Down
29 changes: 16 additions & 13 deletions src/output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
=#

flush_to_temp(t::Float64, n::Network, tosave::Vector{String}) =
flush_to_temp.(t, values(n.vessels), Ref(tosave))
function flush_to_temp(t::Float64, v::Vessel, tosave::Vector{String})
flush_to_temp(t::Float64, n::Network, temp_save::Vector{String}) =
flush_to_temp.(t, values(n.vessels), Ref(temp_save))
function flush_to_temp(t::Float64, v::Vessel, temp_save::Vector{String})
for (l, a) in zip(("P", "Q", "A", "u"), (v.P, v.Q, v.A, v.u))
l tosave && continue
l temp_save && continue
l ("P", "A") && ~v.tosave && continue
temp = open(v.label * "_$l.temp", "a")
line = join((t, a[1], a[v.node2], a[v.node3], a[v.node4], a[end]), " ")
println(temp, line)
Expand All @@ -27,21 +28,23 @@ function flush_to_temp(t::Float64, v::Vessel, tosave::Vector{String})
end


move_temp_to_last(n::Network, tosave::Vector{String}) =
move_temp_to_last.(values(n.vessels), Ref(tosave))
function move_temp_to_last(v::Vessel, tosave::Vector{String})
move_temp_to_last(n::Network, temp_save::Vector{String}) =
move_temp_to_last.(values(n.vessels), Ref(temp_save))
function move_temp_to_last(v::Vessel, temp_save::Vector{String})
for l in ("P", "Q", "A", "u")
l tosave && continue

l temp_save && continue
l ("P", "A") && ~v.tosave && continue
mv(v.label * "_$l.temp", v.label * "_$l.last", force = true)
end
end

append_last_to_out(n::Network, tosave::Vector{String}) =
append_last_to_out.(values(n.vessels), Ref(tosave))
function append_last_to_out(v::Vessel, tosave::Vector{String})

append_last_to_out(n::Network, temp_save::Vector{String}) =
append_last_to_out.(values(n.vessels), Ref(temp_save))
function append_last_to_out(v::Vessel, temp_save::Vector{String})
for l in ("P", "Q", "A", "u")
l tosave && continue
l temp_save && continue
l ("P", "A") && ~v.tosave && continue
out_file = open(v.label * "_$l.out", "a")
last_a = readdlm(v.label * "_$l.last")
writedlm(out_file, last_a, " ")
Expand Down
62 changes: 39 additions & 23 deletions src/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,40 @@ function get_inlet_file(config)
end
config["inlet_file"]
end
function run_simulation(
yaml_config::String;
verbose::Bool = true,
out_files::Bool = false,
conv_ceil::Bool = false,
)
verbose && println("Loading config...")
config = load_yaml_config(yaml_config)

tosave = config["write_results"]
isempty(tosave) && (tosave = ["P", "A"])
"P" tosave && push!(tosave, "P")
"A" tosave && push!(tosave, "A")
function preamble(yaml_config, verbose)

verbose && println("project name: $(config["project_name"])")
verbose && println("Loading config...")
config = load_yaml_config(yaml_config)

project_name = config["project_name"]
blood = Blood(config["blood"])
heart = Heart(get_inlet_file(config))
verbose && println("project name: $project_name")

temp_save = deepcopy(get(config, "write_results", []))
"P" temp_save && push!(temp_save, "P")
"A" temp_save && push!(temp_save, "A")

# TODO: results dir from config
########################################
results_dir = project_name * "_results"
results_dir = get(config, "output_directory", project_name * "_results")
isdir(results_dir) && rm(results_dir, recursive = true)
~isdir(results_dir) && mkdir(results_dir)
cp(yaml_config, joinpath(results_dir, yaml_config), force = true)
cp(config["inlet_file"], joinpath(results_dir, config["inlet_file"]), force = true)
cd(results_dir)
#########################################

config, temp_save
end

function run_simulation(
yaml_config::String;
verbose::Bool = true,
out_files::Bool = false,
conv_ceil::Bool = false,
)
initial_dir = pwd()
config, temp_save = preamble(yaml_config, verbose)

blood = Blood(config["blood"])
heart = Heart(get_inlet_file(config))

network = Network(
config["network"],
Expand Down Expand Up @@ -116,7 +122,7 @@ function run_simulation(
verbose && next!(prog)

if current_time >= checkpoints[counter]
flush_to_temp(current_time, network, tosave)
flush_to_temp(current_time, network, temp_save)
counter += 1
end

Expand All @@ -130,8 +136,8 @@ function run_simulation(
conv_error, error_loc = get_conv_error(network)
end

move_temp_to_last(network, tosave)
out_files && append_last_to_out(network, tosave)
move_temp_to_last(network, temp_save)
out_files && append_last_to_out(network, temp_save)

if verbose
if passed_cycles > 0
Expand Down Expand Up @@ -160,5 +166,15 @@ function run_simulation(
end
current_time += dt
end
cd("..")
tokeep = get(config, "write_results", [])
cleanup.(values(network.vessels), Ref(tokeep))
cd(initial_dir)
end

function cleanup(v::Vessel, tokeep)
println("Cleaning outputs directory")
~v.tosave && rm.(glob("$(v.label)_*"))
for l in ("P", "A", "Q", "u")
l tokeep && rm.(glob("$(v.label)_$l.*"), force=true)
end
end
3 changes: 3 additions & 0 deletions src/vessel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ end

mutable struct Vessel
label::SubString{String}
tosave::Bool

#Topological notation
sn::Int64
Expand Down Expand Up @@ -145,6 +146,7 @@ end

function Vessel(config::Dict{Any,Any}, b::Blood, Ccfl::Float64)
vessel_name = config["label"]
tosave = get(config, "to_save", true)
sn = config["sn"]
tn = config["tn"]
Rp, Rd = radii(config)
Expand Down Expand Up @@ -270,6 +272,7 @@ function Vessel(config::Dict{Any,Any}, b::Blood, Ccfl::Float64)

Vessel(
vessel_name,
tosave,
sn,
tn,
M,
Expand Down

0 comments on commit a83e672

Please sign in to comment.