Skip to content

Commit

Permalink
refactor: change the flow of read_demand_flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lanesmith committed May 2, 2021
1 parent 09248fc commit 382f1bf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 82 deletions.
8 changes: 5 additions & 3 deletions src/loop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ function interval_loop(factory_like, model_kwargs::Dict,
interval_start = start_index + (i - 1) * interval
interval_end = interval_start + interval - 1
model_kwargs["start_index"] = interval_start
bus_flex_amt = _make_bus_demand_flexibility_amount(
case, demand_flexibility, interval_start, interval_end
)
if demand_flexibility.enabled
bus_flex_amt = _make_bus_demand_flexibility_amount(
case, demand_flexibility, interval_start, interval_end
)
end
if i == 1
# Build a model with no initial ramp constraint
if storage_enabled
Expand Down
26 changes: 14 additions & 12 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,22 @@ function _build_model(
sets.num_bus, sets.num_storage)::SparseMatrixCSC
end
# Demand flexibility parameters (if present)
bus_demand_flex_amt = _make_bus_demand_flexibility_amount(
case, demand_flexibility, start_index, end_index
)
if demand_flexibility.enabled && (
demand_flexibility.duration == nothing
if demand_flexibility.enabled
bus_demand_flex_amt = _make_bus_demand_flexibility_amount(
case, demand_flexibility, start_index, end_index
)
if (
demand_flexibility.duration == nothing
|| demand_flexibility.duration > interval_length
)
if demand_flexibility.duration > interval_length
@warn (
"Demand flexibility durations greater than the interval length are set "
* "equal to the interval length."
)
)
if demand_flexibility.duration > interval_length
@warn (
"Demand flexibility durations greater than the interval length are "
* "set equal to the interval length."
)
end
demand_flexibility.duration = interval_length
end
demand_flexibility.duration = interval_length
end

println("variables: ", Dates.now())
Expand Down
106 changes: 40 additions & 66 deletions src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,89 +112,63 @@ function read_demand_flexibility(filepath)::DemandFlexibility
joinpath(filepath, "demand_flexibility.csv")
) |> DataFrames.DataFrame
println("...loading demand flexibility profiles")
demand_flexibility["enabled"] = true
catch e
println("Demand flexibility profiles not found in " * filepath)
demand = CSV.File(joinpath(filepath, "demand.csv")) |> DataFrames.DataFrame
demand_flexibility["flex_amt"] = DataFrames.DataFrame()
demand_flexibility["flex_amt"][:, names(demand)[1]] = demand[:, "UTC Time"]
for c in names(demand)[2:end]
demand_flexibility["flex_amt"][:, c] = zeros(DataFrames.nrow(demand))
end
demand_flexibility["flex_amt"] = nothing
demand_flexibility["enabled"] = false
end

# Try loading the demand flexibility parameters
if ! haskey(demand_flexibility, "enabled")
# Set the demand flexibility parameters
if demand_flexibility["enabled"]
# Pre-specify the demand flexibility parameters
demand_flexibility["duration"] = nothing
demand_flexibility["interval_balance"] = true
demand_flexibility["rolling_balance"] = true

# Try loading the demand flexibility parameters
demand_flexibility_parameters = DataFrames.DataFrame()
try
demand_flexibility_parameters = CSV.File(
joinpath(filepath, "demand_flexibility_parameters.csv")
) |> DataFrames.DataFrame
println("...loading demand flexibility parameters")

# Try loading the duration for the demand flexibility
try
demand_flexibility["duration"] = demand_flexibility_parameters[
1, "duration"
]
catch e
println(
"The demand flexibility duration parameter is not defined. Will "
* "default to nothing."
)
demand_flexibility["duration"] = nothing
end

# Try loading the parameter that indicates if demand flexibility is enabled
try
demand_flexibility["enabled"] = demand_flexibility_parameters[
1, "demand_flexibility_enabled"
]
catch e
println(
"The parameter that indicates if demand flexibility is enabled is "
* "not defined. Will default to being enabled."
)
demand_flexibility["enabled"] = true
end

# Try loading the interval load balance indicator parameter
try
demand_flexibility["interval_balance"] = demand_flexibility_parameters[
1, "interval_load_balance"
]
catch e
println(
"The parameter that indicates if the interval load balance "
* "constraint is enabled is not defined. Will default to being "
* "enabled."
)
demand_flexibility["interval_balance"] = true
end

# Try loading the rolling load balance indicator parameter
try
demand_flexibility["rolling_balance"] = demand_flexibility_parameters[
1, "rolling_load_balance"
]
catch e
println(
"The parameter that indicates if the rolling load balance "
* "constraint is enabled is not defined. Will default to being "
* "enabled."
)
demand_flexibility["rolling_balance"] = true

# Create a dictionary to hold the error messages relevant to loading the
# demand flexibility parameters
demand_flexibility_params_errs = Dict()
demand_flexibility_params_errs["duration"] = (
"The demand flexibility duration parameter is not defined. Will "
* "default to nothing."
)
demand_flexibility_params_errs["enabled"] = (
"The parameter that indicates if demand flexibility is enabled is not "
* "defined. Will default to being enabled."
)
demand_flexibility_params_errs["interval_balance"] = (
"The parameter that indicates if the interval load balance constraint "
* "is enabled is not defined. Will default to being enabled."
)
demand_flexibility_params_errs["rolling_balance"] = (
"The parameter that indicates if the rolling load balance constraint "
* "is enabled is not defined. Will default to being enabled."
)

# Try assigning the different demand flexibility parameters from the file
for k in keys(demand_flexibility_params_errs)
try
demand_flexibility[k] = demand_flexibility_parameters[1, k]
catch e
println(demand_flexibility_params_errs[k])
end
end
catch e
println("Demand flexibility parameters not found in " * filepath)
println(
"Demand flexibility parameters will default to allowing demand "
* "flexibility to occur."
)
demand_flexibility["duration"] = nothing
demand_flexibility["enabled"] = true
demand_flexibility["interval_balance"] = true
demand_flexibility["rolling_balance"] = true
end
end
end

# Convert Dict to NamedTuple
Expand Down
2 changes: 1 addition & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end


Base.@kwdef struct DemandFlexibility
flex_amt::DataFrames.DataFrame
flex_amt::Union{DataFrames.DataFrame,Nothing}
duration::Union{Int64,Nothing}
enabled::Bool
interval_balance::Bool
Expand Down

0 comments on commit 382f1bf

Please sign in to comment.