-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.