-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: v0.10-cleanup
Are you sure you want to change the base?
Changes from all commits
61accbb
7d6d45e
aa17241
6f4dcfc
47a6f1d
48cb3e2
f0529dc
c12702f
e47ced8
88e9280
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
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 | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here concerning the |
||
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 | ||
|
@@ -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"]) | ||
|
@@ -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 | ||
|
@@ -1139,6 +1151,78 @@ function _apply_pump_unit_transform!( | |
end | ||
end | ||
|
||
function _apply_ne_pump_unit_transform!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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 | ||
|
@@ -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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,27 @@ | ||
################################################################################# | ||
# This file defines the nonlinear head loss functions for water systems models. # | ||
################################################################################# | ||
|
||
|
||
function _f_alpha(alpha::Float64; convex::Bool = false) | ||
if convex | ||
return function (x::Float64) | ||
return (x * x)^(0.5 + 0.5 * alpha) | ||
end | ||
else | ||
return function (x::Float64) | ||
return sign(x) * (x * x)^(0.5 + 0.5 * alpha) | ||
end | ||
end | ||
end | ||
|
||
# This file defines the nonlinear head loss functions for water systems models. # | ||
################################################################################# | ||
|
||
function _df_alpha(alpha::Float64; convex::Bool = false) | ||
if convex | ||
return function (x::Float64) | ||
return (1.0 + alpha) * sign(x) * (x * x)^(0.5 * alpha) | ||
end | ||
else | ||
return function (x::Float64) | ||
return (1.0 + alpha) * (x * x)^(0.5 * alpha) | ||
end | ||
function _get_alpha(wm::AbstractWaterModel) | ||
head_loss_form = wm.ref[:it][wm_it_sym][:head_loss] | ||
alphas = uppercase(head_loss_form) == "H-W" ? 1.852 : 2.0 | ||
# alphas = [ref(wm, nw, :alpha) for nw in nw_ids(wm)] | ||
if any(x -> x != alphas[1], alphas) | ||
Memento.error(_LOGGER, "Head loss exponents are different across the multinetwork.") | ||
end | ||
return alphas[1] | ||
end | ||
|
||
|
||
function _d2f_alpha(alpha::Float64; convex::Bool = false) | ||
if convex | ||
return function (x::Float64) | ||
return alpha * (1.0 + alpha) * (x * x)^(0.5 * alpha - 0.5) | ||
end | ||
else | ||
return function (x::Float64) | ||
return x != 0.0 ? | ||
sign(x) * alpha * (1.0 + alpha) * (x * x)^(0.5 * alpha - 0.5) : | ||
prevfloat(Inf) | ||
end | ||
end | ||
function head_loss(wm::Union{NCDWaterModel,CRDWaterModel}, x) | ||
p = _get_alpha(wm) | ||
# An expression equivalent to abspower(x, p) = abs(x)^p | ||
# return JuMP.@expression(wm.model, ifelse(x > 0, x^p, (-x)^p)) | ||
return JuMP.@expression(wm.model, (x^2)^(p / 2)) | ||
end | ||
|
||
|
||
function _get_alpha_min_1(wm::AbstractWaterModel) | ||
return _get_exponent_from_head_loss_form(wm.ref[:it][wm_it_sym][:head_loss]) - 1.0 | ||
end | ||
|
||
|
||
function head_loss_args(wm::Union{NCDWaterModel,CRDWaterModel}) | ||
alpha_m1 = _get_alpha_min_1(wm) | ||
return ( | ||
:head_loss, | ||
1, | ||
_f_alpha(alpha_m1, convex = true), | ||
_df_alpha(alpha_m1, convex = true), | ||
_d2f_alpha(alpha_m1, convex = true), | ||
) | ||
end | ||
|
||
|
||
function head_loss_args(wm::NCWaterModel) | ||
alpha_m1 = _get_alpha_min_1(wm) | ||
|
||
return ( | ||
:head_loss, | ||
1, | ||
_f_alpha(alpha_m1, convex = false), | ||
_df_alpha(alpha_m1, convex = false), | ||
_d2f_alpha(alpha_m1, convex = false), | ||
) | ||
end | ||
|
||
|
||
function _function_head_loss(wm::AbstractWaterModel) | ||
# By default, head loss is not defined by nonlinear registered functions. | ||
end | ||
|
||
|
||
function _function_head_loss(wm::AbstractNonlinearModel) | ||
JuMP.register(wm.model, head_loss_args(wm)...) | ||
function head_loss(wm::AbstractNonlinearModel, x) | ||
p = _get_alpha(wm) | ||
# An expression equivalent to signpower(x, p) = sign(x) * abs(x)^p | ||
return JuMP.@expression(wm.model, ifelse(x > 0, x^p, -(-x)^p)) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data
should always includene_pump
as a key, even if there are no corresponding entries. Thisif
statement should be removed.