Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbasquensmunoz committed Oct 24, 2024
1 parent 9c1e91e commit 6ddf49f
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 39 deletions.
7 changes: 3 additions & 4 deletions BNSPlots/src/borefield.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ function plot_borefield(network, positions; distinguished_boreholes = [])
xlims!(axis, min_x-margin, max_x+margin)
ylims!(axis, min_y-margin, max_y+margin)

N = nv(network.graph)
borefield = SimpleGraph(network.graph)
rem_vertex!(borefield, N)
rem_vertex!(borefield, N - 1)
rem_vertex!(borefield, nv(borefield))
rem_vertex!(borefield, nv(borefield))

Nb = nv(borefield)
borehole_colors = [:black for i in 1:Nb]
borehole_colors = [colorant"black" for i in 1:Nb]
borehole_sizes = 12 * ones(Int, Nb)
for (bh, color) in distinguished_boreholes
borehole_colors[bh] = color
Expand Down
27 changes: 15 additions & 12 deletions BNSPlots/src/monitor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ end
Creates a plot of the result of the simulation.
# Arguments
- `containers`: The containers (`SimulationContainers`) containing the result of the simulation through `simulate!`.
- `branch`: A vector containing the IDs of the boreholes whose data will be displayed.
- `boreholes`: A vector containing the IDs of the boreholes whose data will be displayed.
- `t`: The times at which the data corresponds. It should normally be `options.t`.
# Optional arguments
- `steps`: Index of the steps to display in the plot.
- `display`: A vector describing which plots that will be generated. If `:Tfin`, `:Tfout` or `:Tb` are specified, a temperature plot will be created showing the inlet fluid temperature,
the outlet fluid temperature, and the borehole wall temperature, respectively. If `:q` is specified, a separate power plot will be created shwoing the heat extracted per meter.
- `Δt`: The scale of the x-axis in the plot. Possible options: `:year`, `:month`, `:hour`.
- `color_pair`: A pair of colors used as extrema to generate a range of colors for each borehole.
- `colors`: A list of colors used for each borehole. If not specified, the colors used will be between colorant"navajowhite2" and colorant"darkgreen".
"""
function monitor(containers, branch, t; steps = 1:length(t), display = [:Tfin, :Tfout, :Tb, :q, :mf], Δt = :year, color_pair = (colorant"navajowhite2", colorant"darkgreen"))
function monitor(containers, boreholes, t; steps = 1:length(t), display = [:Tfin, :Tfout, :Tb, :q, :mf], Δt = :year, colors = [])
if isempty(display)
return
end
Expand All @@ -36,8 +36,12 @@ function monitor(containers, branch, t; steps = 1:length(t), display = [:Tfin, :
grid = scene[1, 1] = GridLayout()
axes = []

color_range = make_color_range(color_pair, length(branch))

if !isempty(colors)
color_range = colors
else
color_range = make_color_range((colorant"navajowhite2", colorant"darkgreen"), length(boreholes))
end

if anyin([:Tfin, :Tfout, :Tb], display)
axis_T = Axis(grid[length(axes)+1, 1], ylabel = L"T \, \left[ °C \right]")
push!(axes, axis_T)
Expand All @@ -51,11 +55,11 @@ function monitor(containers, branch, t; steps = 1:length(t), display = [:Tfin, :
push!(axes, axis_m)
end

Tfin = get_Tfin(containers, branch)
Tfout = get_Tfout(containers, branch)
Tb = get_Tb(containers, branch)
q = get_q(containers, branch)
mf = get_mf(containers, branch)
Tfin = get_Tfin(containers, boreholes)
Tfout = get_Tfout(containers, boreholes)
Tb = get_Tb(containers, boreholes)
q = get_q(containers, boreholes)
mf = get_mf(containers, boreholes)

secs_in_year = 8760*3600
conversion = Dict(:year => 1, :month => 12, :hour => 8760)
Expand All @@ -79,10 +83,9 @@ function monitor(containers, branch, t; steps = 1:length(t), display = [:Tfin, :
end
end


group_color = [PolyElement(color = color, strokecolor = :transparent) for color in color_range]
group_marker = [LineElement(color = :black), LineElement(color = :black, linestyle = :dash), MarkerElement(marker = :circle, color = :black, strokecolor = :transparent, markersize = 5.)]
legend = Legend(scene, [group_color, group_marker], [string.(branch), ["Tfin", "Tfout", "Tb"]], ["Boreholes", "Temperatures"], tellheight = true, tellwidth = false)
legend = Legend(scene, [group_color, group_marker], [string.(boreholes), ["Tfin", "Tfout", "Tb"]], ["Boreholes", "Temperatures"], tellheight = true, tellwidth = false)
legend.titleposition = :top
legend.orientation = :horizontal
legend.nbanks = 1
Expand Down
9 changes: 4 additions & 5 deletions examples/Braedstrup/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ end

function BoreholeNetworksSimulator.operate(operator::SeasonalOperator, i, options, X)
active_network = options.configurations[operator.seasonal_configuration[i]]
BoreholeOperation(active_network, operator.mass_flows)
BoreholeOperation(network=active_network, mass_flows=operator.mass_flows)
end

operator = SeasonalOperator(mass_flows=0.5 .* ones(n_branches(network)), seasonal_configuration=[i%12 in 1:6 ? 2 : 1 for i in 1:Nt])
Expand All @@ -76,11 +76,10 @@ containers.X
############
# Draw plots

monitored_branches = [3, 8]
color_ranges = [(colorant"darkorange", colorant"blue"), (colorant"red", colorant"green")]
monitoring = [(27, colorant"darkorange"), (28, colorant"green")]

borefiled_plot = plot_borefield(network, borehole_positions, distinguished_branches = monitored_branches, colors = color_ranges)
branch1 = monitor(containers, network.branches[monitored_branches[1]], options.t, display = [:Tfin], color_pair=color_ranges[1])
borefield_plot = plot_borefield(network, borehole_positions, distinguished_boreholes = monitoring)
branch1 = monitor(containers, boreholes_in_branch(network, first_bh=19), options.t, display = [:Tfin])

# save("examples/Braedstrup/plots/Braedstrup_borefield.png", borefiled_plot)
# save("examples/Braedstrup/plots/branch1.png", branch1)
Expand Down
4 changes: 2 additions & 2 deletions examples/g-function/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function make_plot(axis, d)
α = 1e-6
λ = 3.

network = BoreholeNetwork([[i] for i in 1:n*m])
network = all_parallel_network(n*m)
configurations = [network]

function create_rectangular_field(n, m, d)
Expand All @@ -41,7 +41,7 @@ function make_plot(axis, d)
configurations = configurations
)

operator = SimpleOperator(mass_flow = 1., branches = n_branches(network))
operator = ConstantOperator(network, mass_flows = ones(n*m))
containers = @time initialize(options)

@time simulate!(operator=operator, options=options, containers=containers)
Expand Down
6 changes: 3 additions & 3 deletions examples/tekniska/constant_m.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ using BNSPlots

include("defs.jl")

operator = SimpleOperator(mass_flow=0.5, branches=n_branches(network))
operator = ConstantOperator(network, mass_flows=0.5 * ones(10))
containers = @time initialize(options)
@time simulate!(operator=operator, options=options, containers=containers)

t_range = (5*8760-24*7):5*8760
const_m_plot = monitor(containers, [4, 7], options.t, steps = t_range, color_pair = (colorant"darkgreen", colorant"red"))
const_m_plot = monitor(containers, [4, 7], options.t, steps = t_range, colors = [colorant"darkgreen", colorant"red"])
# save("examples/tekniska/plots/const_m.png", const_m_plot)

const_m_plot_5_year = monitor(containers, [4, 7], options.t, color_pair = (colorant"darkgreen", colorant"red"))
const_m_plot_5_year = monitor(containers, [4, 7], options.t, colors = [colorant"darkgreen", colorant"red"])
# save("examples/tekniska/plots/const_m_5_years.png", const_m_plot_5_year)
4 changes: 2 additions & 2 deletions examples/tekniska/defs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using WGLMakie
Δt = 3600.
Nt = 8760*5

network = BoreholeNetwork([[i] for i in 1:10])
network = all_parallel_network(10)
configurations = [
network
]
Expand Down Expand Up @@ -60,7 +60,7 @@ options = SimulationOptions(
)

group1 = [1, 6, 2, 7, 3, 8]
borefield_fig = plot_borefield(network, borehole_positions, distinguished_branches=collect(1:10), colors=[i in group1 ? (colorant"red", colorant"red") : (colorant"green", colorant"green") for i in 1:10])
borefield_fig = plot_borefield(network, borehole_positions, distinguished_boreholes=[(i, i in group1 ? colorant"red" : colorant"green") for i in 1:10])

function plot_weekly_Q()
fig = Figure(size=(1500, 400))
Expand Down
4 changes: 2 additions & 2 deletions examples/tekniska/prop_m.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ end

function BoreholeNetworksSimulator.operate(operator::VariableMFOperator, step, options, X)
operator.mass_flows .= operator.mass_flow_series[step]
BoreholeOperation(options.configurations[1], operator.mass_flows)
BoreholeOperation(network = options.configurations[1], mass_flows = operator.mass_flows)
end

operator = VariableMFOperator(0.5 .* Q_tot ./ Q_ref, zeros(n_branches(network)))
Expand All @@ -19,6 +19,6 @@ containers = @time initialize(options)
@time simulate!(operator=operator, options=options, containers=containers)

t_range = (5*8760-24*7):5*8760
prop_m_plot = monitor(containers, [4, 7], options.t, steps = t_range, color_pair = (colorant"darkgreen", colorant"red"))
prop_m_plot = monitor(containers, [4, 7], options.t, steps = t_range, colors = [colorant"darkgreen", colorant"red"])

# save("examples/tekniska/plots/prop_m.png", prop_m_plot)
4 changes: 2 additions & 2 deletions examples/tekniska/toggle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ function BoreholeNetworksSimulator.operate(operator::ToggleOperator, step, optio
mass_flow_containers[branch2] .= mf2

@pack! operator = single_branch, hours_used, toggle, mass_flow_containers
BoreholeOperation(options.configurations[1], mass_flow_containers)
BoreholeOperation(network = options.configurations[1], mass_flows = mass_flow_containers)
end

operator = ToggleOperator(Q_threshold = 20000., mass_flow = 0.5, mass_flow_containers = zeros(n_branches(network)))
containers = @time initialize(options)
@time simulate!(operator=operator, options=options, containers=containers)

t_range = (5*8760-24*7):5*8760
toggle_plot = monitor(containers, [4, 7], options.t, steps = t_range, color_pair = (colorant"darkgreen", colorant"red"))
toggle_plot = monitor(containers, [4, 7], options.t, steps = t_range, colors = [colorant"darkgreen", colorant"red"])

# save("examples/tekniska/plots/toggle.png", toggle_plot)
2 changes: 1 addition & 1 deletion src/BoreholeNetworksSimulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export BoundaryCondition, NoBoundary, DirichletBoundaryCondition, AdiabaticBound
export Approximation, MidPointApproximation, MeanApproximation
export SimulationOptions, SimulationContainers
export BoreholeNetwork, Valve, BoreholeOperation
export connect_to_source!, connect_to_sink!, connect!, all_series_network, all_parallel_network, compute_mass_flows!, source, sink, connect_in_series!, equal_valve, valve, absolute_valve, initialize_mass_flows
export connect_to_source!, connect_to_sink!, connect!, all_series_network, all_parallel_network, compute_mass_flows!, source, sink, connect_in_series!, equal_valve, valve, absolute_valve, initialize_mass_flows, boreholes_in_branch
export Operator, SimpleOperator, operate
export ConstantOperator
export simulate!, compute_parameters, load_cache!, save_cache, initialize
Expand Down
6 changes: 1 addition & 5 deletions src/modular/core/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ function initialize(options::SimulationOptions)
end
function SimulationContainers(options::SimulationOptions)
@unpack Nb, Nt = options
if Nb > 50
SimulationContainers(M = spzeros(4Nb, 4Nb), b = zeros(4Nb), X = zeros(4Nb, Nt), mf = zeros(Nb, Nt))
else
SimulationContainers(M = zeros(4Nb, 4Nb), b = zeros(4Nb), X = zeros(4Nb, Nt), mf = zeros(Nb, Nt))
end
SimulationContainers(M = Nb > 100 ? spzeros(4Nb, 4Nb) : zeros(4Nb, 4Nb), b = zeros(4Nb), X = zeros(4Nb, Nt), mf = zeros(Nb, Nt))
end

function branch_of_borehole(operation::BoreholeOperation, borehole)
Expand Down
8 changes: 7 additions & 1 deletion src/modular/network/network.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function Base.reverse(network::BoreholeNetwork)
end
BoreholeNetwork(reverse_graph)
end

function boreholes_in_branch(n::BoreholeNetwork; first_bh::Int)
boreholes = neighborhood(n.graph, first_bh, nv(n.graph))
filter!(i -> i sink(n), boreholes)
boreholes
end
n_boreholes(n::BoreholeNetwork) = nv(n.graph) - 2
n_branches(n::BoreholeNetwork) = length(neighbors(n.graph, source(n)))
initialize_mass_flows(network::BoreholeNetwork) = zeros(nv(network.graph))
Expand Down Expand Up @@ -81,7 +87,7 @@ function absolute_valve(nodes::Vector{Int}, mass_flows::Vector{T}) where {T <: N
valve = Valve(Dict{Int, T}())
total_mass_flow = sum(mass_flows)
for (i, n) in enumerate(nodes)
valve.split[n] = mass_flows[i] / total_mass_flow
valve.split[n] = total_mass_flow != 0 ? mass_flows[i] / total_mass_flow : 0.
end
return valve
end
Expand Down

0 comments on commit 6ddf49f

Please sign in to comment.