Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expansion data file and model for expansion pumps #154

Open
wants to merge 10 commits into
base: v0.10-cleanup
Choose a base branch
from
1 change: 1 addition & 0 deletions src/WaterModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ include("util/obbt.jl")
# Deprecated functions.
include("deprecated.jl")

println("Running the cleaned up version of WaterModels.jl")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed.

# This must be included last to support automated export.
include("core/export.jl")

Expand Down
130 changes: 108 additions & 22 deletions src/core/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ function _check_connectivity(data::Dict{String,<:Any})
end

for comp_type in _LINK_COMPONENTS
for (i, comp) in data[comp_type]
if !(comp["node_fr"] in node_ids)
error_message = "From node $(comp["node_fr"]) in "
error_message *= "$(replace(comp_type, "_" => " ")) $(i) is not defined."
Memento.error(_LOGGER, error_message)
end
if(haskey(data,comp_type))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data should always include ne_pump as a key, even if there are no corresponding entries. This if statement should be removed.

for (i, comp) in data[comp_type]
if !(comp["node_fr"] in node_ids)
error_message = "From node $(comp["node_fr"]) in "
error_message *= "$(replace(comp_type, "_" => " ")) $(i) is not defined."
Memento.error(_LOGGER, error_message)
end

if !(comp["node_to"] in node_ids)
error_message = "To node $(comp["node_to"]) in "
error_message *= "$(replace(comp_type, "_" => " ")) $(i) is not defined."
Memento.error(_LOGGER, error_message)
if !(comp["node_to"] in node_ids)
error_message = "To node $(comp["node_to"]) in "
error_message *= "$(replace(comp_type, "_" => " ")) $(i) is not defined."
Memento.error(_LOGGER, error_message)
end
end
end
end
Expand Down Expand Up @@ -112,17 +114,19 @@ function _check_status(data::Dict{String,<:Any})
end

for comp_type in _LINK_COMPONENTS
for (i, comp) in data[comp_type]
if comp["status"] != STATUS_INACTIVE && !(comp["node_fr"] in active_node_ids)
warning_message = "Active $(comp_type) $(i) is connected to inactive "
warning_message *= "from node $(comp["node_fr"])."
Memento.warn(_LOGGER, warning_message)
end
if(haskey(data,comp_type))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here concerning the ne_pump key.

for (i, comp) in data[comp_type]
if comp["status"] != STATUS_INACTIVE && !(comp["node_fr"] in active_node_ids)
warning_message = "Active $(comp_type) $(i) is connected to inactive "
warning_message *= "from node $(comp["node_fr"])."
Memento.warn(_LOGGER, warning_message)
end

if comp["status"] != STATUS_INACTIVE && !(comp["node_to"] in active_node_ids)
warning_message = "Active $(comp_type) $(i) is connected to inactive "
warning_message *= "to node $(comp["node_to"])."
Memento.warn(_LOGGER, warning_message)
if comp["status"] != STATUS_INACTIVE && !(comp["node_to"] in active_node_ids)
warning_message = "Active $(comp_type) $(i) is connected to inactive "
warning_message *= "to node $(comp["node_to"])."
Memento.warn(_LOGGER, warning_message)
end
end
end
end
Expand Down Expand Up @@ -512,6 +516,14 @@ function _calc_head_max(data::Dict{String, <:Any})
head_max = max(head_max, node_to["elevation"] + head_gain)
end

for (i, pump) in wm_data["ne_pump"]
# Consider possible pump head gains in computation of head_max.
node_fr = wm_data["node"][string(pump["node_fr"])]
node_to = wm_data["node"][string(pump["node_to"])]
head_gain = _calc_pump_head_gain_max(pump, node_fr, node_to)
head_max = max(head_max, node_to["elevation"] + head_gain)
end

for (i, regulator) in wm_data["regulator"]
# Consider possible downstream regulator heads in computation of head_max.
p_setting, node_to_index = regulator["setting"], string(regulator["node_to"])
Expand Down Expand Up @@ -1023,7 +1035,7 @@ end

function _apply_pipe_unit_transform!(data::Dict{String,<:Any}, transform_length::Function, head_loss::String)
wm_data = get_wm_data(data)

if !haskey(wm_data, "pipe")
return
end
Expand Down Expand Up @@ -1139,6 +1151,78 @@ function _apply_pump_unit_transform!(
end
end

function _apply_ne_pump_unit_transform!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there additional constants associated with network expansion, here, that need to be scaled? For example, construction cost?

data::Dict{String,<:Any}, transform_mass::Function, transform_flow::Function,
transform_length::Function, transform_time::Function)
wm_data = get_wm_data(data)

if !haskey(wm_data, "ne_pump")
return
end

power_scalar = transform_mass(1.0) * transform_length(1.0)^2 / transform_time(1.0)^3
energy_scalar = transform_mass(1.0) * transform_length(1.0)^2 / transform_time(1.0)^2

for (_, ne_pump) in wm_data["ne_pump"]
if haskey(ne_pump, "head_curve")
ne_pump["head_curve"] = [(transform_flow(x[1]), x[2]) for x in ne_pump["head_curve"]]
ne_pump["head_curve"] = [(x[1], transform_length(x[2])) for x in ne_pump["head_curve"]]
end

if haskey(ne_pump, "efficiency_curve")
ne_pump["efficiency_curve"] = [(transform_flow(x[1]), x[2]) for x in ne_pump["efficiency_curve"]]
end

if haskey(ne_pump, "energy_price")
ne_pump["energy_price"] /= energy_scalar
end

if haskey(ne_pump, "E")
ne_pump["E"] *= energy_scalar
end

if haskey(ne_pump, "power_fixed")
ne_pump["power_fixed"] *= power_scalar
end

if haskey(ne_pump, "P")
ne_pump["P"] *= power_scalar
end

if haskey(ne_pump, "c")
ne_pump["c"] *= energy_scalar
end

if haskey(ne_pump, "min_inactive_time")
ne_pump["min_inactive_time"] = transform_time(ne_pump["min_inactive_time"])
end

if haskey(ne_pump, "min_active_time")
ne_pump["min_active_time"] = transform_time(ne_pump["min_active_time"])
end

if haskey(ne_pump, "power_per_unit_flow")
ne_pump["power_per_unit_flow"] *= power_scalar / transform_flow(1.0)
end
end

if haskey(wm_data, "time_series") && haskey(wm_data["time_series"], "ne_pump")
for ne_pump in values(wm_data["time_series"]["ne_pump"])
if haskey(ne_pump, "energy_price")
ne_pump["energy_price"] ./= energy_scalar
end

if haskey(ne_pump, "power_fixed")
ne_pump["power_fixed"] .*= power_scalar
end

if haskey(ne_pump, "power_per_unit_flow")
ne_pump["power_per_unit_flow"] .*= power_scalar / transform_flow(1.0)
end
end
end
end


function _apply_regulator_unit_transform!(data::Dict{String,<:Any}, transform_head::Function, transform_length::Function)
wm_data = get_wm_data(data)
Expand Down Expand Up @@ -1169,7 +1253,7 @@ function _calc_scaled_gravity(data::Dict{String, <:Any})

if wm_data["per_unit"]
base_time = 1.0 / _calc_time_per_unit_transform(wm_data)(1.0)
base_length = 1.0 / _calc_length_per_unit_transform(wm_data)(1.0)
base_length = 1.0 / _calc_length_per_unit_transform(wm_data)(1.0)
return _calc_scaled_gravity(base_length, base_time)
else
return _GRAVITY
Expand Down Expand Up @@ -1368,6 +1452,8 @@ function _transform_nw!(
_apply_des_pipe_unit_transform!(wm_nw_data, length_transform, head_loss)
_apply_pump_unit_transform!(wm_nw_data, mass_transform,
flow_transform, length_transform, time_transform)
_apply_ne_pump_unit_transform!(wm_nw_data, mass_transform,
flow_transform, length_transform, time_transform)
_apply_regulator_unit_transform!(wm_nw_data, head_transform, length_transform)

# Apply transformations to nodal components.
Expand Down
14 changes: 8 additions & 6 deletions src/core/pump.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ end


function correct_ne_pumps!(data::Dict{String, <:Any})
wm_data = get_wm_data(data)
base_flow = wm_data["per_unit"] ? wm_data["base_flow"] : 1.0
correction_func = x -> _correct_ne_pumps!(x, base_flow)
apply_wm!(correction_func, data; apply_to_subnetworks = true)
if(haskey(data,"ne_pump"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment that ne_pump should always be within data.

wm_data = get_wm_data(data)
base_flow = wm_data["per_unit"] ? wm_data["base_flow"] : 1.0
correction_func = x -> _correct_ne_pumps!(x, base_flow)
apply_wm!(correction_func, data; apply_to_subnetworks = true)
end
end


Expand Down Expand Up @@ -407,7 +409,7 @@ function _calc_pump_power_points(wm::AbstractWaterModel, nw::Int, pump_id::Int,
q_min = max(get(pump, "flow_min_forward", _flow_min), _flow_min)

q_max = max(q_min, pump["flow_max"])
q_build = range(0.0, stop = q_max + 1.0e-7, length = num_points)
q_build = range(q_min - 1.0e-7, stop = q_max + 1.0e-7, length = num_points)
f_build = head_curve_function.(collect(q_build)) .* q_build

if haskey(pump, "efficiency_curve")
Expand All @@ -427,7 +429,7 @@ function _calc_pump_power_points(wm::AbstractWaterModel, nw::Int, pump_id::Int,
end


function _calc_ne_pump_power_points(wm::AbstractWaterModel, nw::Int, pump_id::Int, num_points::Int)
function _calc_pump_power_points_ne(wm::AbstractWaterModel, nw::Int, pump_id::Int, num_points::Int)
ne_pump = ref(wm, nw, :ne_pump, pump_id)
head_curve_function = ref(wm, nw, :ne_pump, pump_id, "head_curve_function")

Expand Down
2 changes: 0 additions & 2 deletions src/form/crd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ function constraint_on_off_pump_power(
append!(con(wm, n, :on_off_pump_power)[a], [c])
else
f_ua = _calc_pump_power_ua(wm, n, a, [q_lb, q_ub])

if f_ua[1] != f_ua[2]
# Build a linear under-approximation of the power.
slope = (f_ua[2] - f_ua[1]) / (q_ub - q_lb)
Expand Down Expand Up @@ -209,7 +208,6 @@ function constraint_on_off_pump_power_ne(
append!(con(wm, n, :on_off_pump_power_ne)[a], [c])
else
f_ua = _calc_pump_power_ua_ne(wm, n, a, [q_lb, q_ub])

if f_ua[1] != f_ua[2]
# Build a linear under-approximation of the power.
slope = (f_ua[2] - f_ua[1]) / (q_ub - q_lb)
Expand Down
8 changes: 5 additions & 3 deletions src/io/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ dictionary of data). Here, `skip_correct` will skip data correction routines
(e.g., component status propagation) if set to `true`, and `per_unit` will
translate the data model to a per-unit measurement system if set to `true`.
"""
function parse_file(path::String; skip_correct::Bool = false, per_unit::Bool = true)
function parse_file(path::String; ne_path::String = "", skip_correct::Bool = false, per_unit::Bool = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this should be the preferred method for passing in network expansion data. For nonstandard network components, the preferred way would be to pass in modifications via an auxiliary JSON file, then update the base network data with these modifications. For example,

network = WaterModels.parse_file(network_path)
modifications = WaterModels.parse_file(modifications_path; skip_correct = true)
InfrastructureModels.update_data!(network, modifications)
WaterModels.correct_network_data!(network)

Here, network_path would be the original .inp file and modifications_path would be a JSON file that includes the data for ne_pump that would ultimately extend network. Does this make sense? Happy to help if there's a more concrete example of these data.

if endswith(path, ".inp")
network_data = WaterModels.parse_epanet(path)
network_data = WaterModels.parse_epanet(path,ne_filename = ne_path)
elseif endswith(path, ".json")
network_data = WaterModels.parse_json(path)
correct_enums!(network_data)
Expand Down Expand Up @@ -70,7 +70,9 @@ function correct_network_data!(data::Dict{String, <:Any}; per_unit::Bool = true)
correct_pipes!(data)
correct_des_pipes!(data)
correct_pumps!(data)
correct_ne_pumps!(data)
if(haskey(data,"ne_pump"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove per comments above.

correct_ne_pumps!(data)
end
correct_regulators!(data)
correct_short_pipes!(data)
correct_ne_short_pipes!(data)
Expand Down
Loading