Skip to content
Snippets Groups Projects
Commit dbb082af authored by Julian Stürmer's avatar Julian Stürmer
Browse files

Rename logger and delete file handler in calc_total_impact!

parent 3c46bced
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,7 @@ using Suppressor ...@@ -23,7 +23,7 @@ using Suppressor
function __init__() function __init__()
### Setup logger (PowerModels.jl logger will inherit the format) ### Setup logger (PowerModels.jl logger will inherit the format)
global _LOGGER = Memento.config!( global LOGGER = Memento.config!(
"info", fmt="[{date} | {level} | {name}]: {msg}" "info", fmt="[{date} | {level} | {name}]: {msg}"
) )
end end
...@@ -32,8 +32,8 @@ end ...@@ -32,8 +32,8 @@ end
Changes the logger level to the passed level. Common options are "info" (default) and "debug" (additional logs for debugging) Changes the logger level to the passed level. Common options are "info" (default) and "debug" (additional logs for debugging)
=# =#
function config_logger(level="info") function config_logger(level="info")
info(_LOGGER, "Logger level set to $(level)!") info(LOGGER, "Logger level set to $(level)!")
setlevel!(_LOGGER, level) setlevel!(LOGGER, level)
return nothing return nothing
end end
......
...@@ -161,9 +161,8 @@ function calc_total_impact!( ...@@ -161,9 +161,8 @@ function calc_total_impact!(
### Check whether a .log file is passed for logging ### Check whether a .log file is passed for logging
if isempty(logpath) == false if isempty(logpath) == false
# push!(_LOGGER, DefaultHandler(logpath)) # add .log file to logger
### Add .log file to logger and set format ### Add .log file to logger and set format
push!(_LOGGER, push!(LOGGER,
DefaultHandler( DefaultHandler(
logpath, DefaultFormatter("[{date} | {level} | {name}]: {msg}") logpath, DefaultFormatter("[{date} | {level} | {name}]: {msg}")
) )
...@@ -178,19 +177,25 @@ function calc_total_impact!( ...@@ -178,19 +177,25 @@ function calc_total_impact!(
### Deactivate transmission lines destroyed in frame f and log them ### Deactivate transmission lines destroyed in frame f and log them
destroyed_tl = [dmg[1] for dmg in tl_damage if dmg[2] == f] destroyed_tl = [dmg[1] for dmg in tl_damage if dmg[2] == f]
destroy_tl!(network_data, destroyed_tl) # update network topology destroy_tl!(network_data, destroyed_tl) # update network topology
info(_LOGGER, info(LOGGER,
"Overhead transmission lines destroyed in wind frame $f: " * "Overhead transmission lines destroyed in wind frame $f: " *
"$destroyed_tl" "$destroyed_tl"
) # log destroyed transmission lines ) # log destroyed transmission lines
### Recalculate AC-PF and see whether a cascade unfolds ### Recalculate AC-PF and see whether a cascade unfolds
calc_cascade!(network_data, method, print_level, logpath, solpath) calc_cascade!(network_data, method, print_level)
end end
if isempty(solpath) == false # save final network data if isempty(solpath) == false # save final network data
save(solpath, "network_data", network_data) save(solpath, "network_data", network_data)
end end
# println(Memento.gethandlers(LOGGER))
for key in [h for h in keys(gethandlers(LOGGER)) if h != "console"]
delete!(gethandlers(LOGGER), key)
end
GC.gc()
return nothing return nothing
end end
...@@ -199,10 +204,11 @@ Calculates whether a cascading failure unfolds in a damaged network. If any netw ...@@ -199,10 +204,11 @@ Calculates whether a cascading failure unfolds in a damaged network. If any netw
=# =#
function calc_cascade!( function calc_cascade!(
network_data::Dict{String,<:Any}, # network with destroyed lines network_data::Dict{String,<:Any}, # network with destroyed lines
method = :JuMP, method = :JuMP, # solver to use
print_level = 5, # level of detail for Ipopt output print_level = 5, # level of detail for Ipopt output (if method = :JuMP)
logpath = "", # optional path to .log file for saving logs of interest iteration = 0 # counting iterations during a cascade (starts with 0)
solpath = "" # path for saving results/solutions of interest # logpath = "", # optional path to .log file for saving logs of interest
# solpath = "" # path for saving results/solutions of interest
) )
### Treat all connected components (restore APB, define new slacks etc.) ### Treat all connected components (restore APB, define new slacks etc.)
...@@ -216,27 +222,23 @@ function calc_cascade!( ...@@ -216,27 +222,23 @@ function calc_cascade!(
### Calculate new AC-PF solution ### Calculate new AC-PF solution
set_ac_pf_start_values!(network_data) # last state as initial conditions set_ac_pf_start_values!(network_data) # last state as initial conditions
output = @capture_out calc_ac_pf!(
### Check whether to write Ipopt output to a .log file network_data, method, print_level=print_level
if isempty(logpath) == false )
output = @capture_out calc_ac_pf!( info(LOGGER, output) # pass Ipopt output to logger
network_data, method, print_level=print_level
)
info(_LOGGER, output) # pass Ipopt output to logger
else
calc_ac_pf!(network_data, method, print_level=print_level)
end
### Check for overloaded branches ### Check for overloaded branches
overloaded_br = get_branches(network_data, min_loading=1.) overloaded_br = get_branches(network_data, min_loading=1.)
if isempty(overloaded_br) == false if isempty(overloaded_br) == false # cascade advances
iteration += 1
### Log overloaded branches ### Log overloaded branches
info(_LOGGER, "The following branches are overloaded: $overloaded_br") info(LOGGER, "The following branches are overloaded: $overloaded_br")
info(LOGGER, "=> Cascade continues (iteration $iteration)")
### Deactivate overloaded branches ### Deactivate overloaded branches
disable_branches!(network_data, [br[1] for br in overloaded_br]) disable_branches!(network_data, [br[1] for br in overloaded_br])
### Repeat cascade algorithm ### Repeat cascade algorithm
calc_cascade!(network_data, method, print_level, logpath, solpath) calc_cascade!(network_data, method, print_level, iteration)
end end
return nothing return nothing
...@@ -278,7 +280,7 @@ function restore_p_balance!(network_data::Dict{String,<:Any}) ...@@ -278,7 +280,7 @@ function restore_p_balance!(network_data::Dict{String,<:Any})
end end
#= #=
Restores active power balance in a connected component identified by the set of buses cc. Depending on whether an overproduction or underproduction exists, generator dispatches may be increased or reduced. If necessary, load can be shed by reducing the demands uniformly until the total demand matches generation. For more details see flow chart. Restores active power balance (APB) in a connected component identified by the set of buses cc. Depending on whether an overproduction or underproduction exists, generator dispatches may be increased or reduced. If necessary, load can be shed by reducing the demands uniformly until the total demand matches generation. For more details see flow chart.
=# =#
function _restore_p_balance!( function _restore_p_balance!(
network_data::Dict{String,<:Any}, network_data::Dict{String,<:Any},
...@@ -300,51 +302,62 @@ function _restore_p_balance!( ...@@ -300,51 +302,62 @@ function _restore_p_balance!(
network_data, active_cc_gens, active_cc_loads, cc network_data, active_cc_gens, active_cc_loads, cc
) )
info(_LOGGER, "Active power mismatch in CC #$cc_i: $cc_Δp") ϵ = 1e-10 # mismatch tolerated for APB
if isapprox(cc_Δp, 0, atol=1e-10) # "sufficient" power balance if cc_Δp > ϵ # overproduction
nothing # nothing has to be done info(LOGGER, "Detected overproduction in (sub-)grid #$cc_i: $cc_Δp")
elseif cc_Δp > 0 # overproduction
debug(_LOGGER, "Overproduction in CC #$cc_i: $cc_Δp")
if isapprox(cc_Δp_lim, 0, atol=1e-10) # power balance at minimum ### Check how to restore APB (if possible)
if isapprox(cc_Δp_lim, 0, atol=ϵ) # power balance at minimum
info(LOGGER, "Setting dispatches in #$cc_i to their lower bounds!")
### Set all active generators to their minimum ### Set all active generators to their minimum
for g in active_cc_gens for g in active_cc_gens
id = string(g) id = string(g)
network_data["gen"][id]["pg"] = network_data["gen"][id]["pmin"] network_data["gen"][id]["pg"] = network_data["gen"][id]["pmin"]
end end
elseif cc_Δp_lim < 0 # sufficient dispatch reduction possible elseif cc_Δp_lim < -ϵ # sufficient dispatch reduction possible
info(LOGGER, "Mismatch in #$cc_i requires reducing generation!")
### Reduce all dispatches uniformly ### Reduce all dispatches uniformly
_reduce_p_dispatch!( cc_Δp = _reduce_p_dispatch!(
network_data, active_cc_gens, active_cc_loads, cc, cc_Δp network_data, active_cc_gens, active_cc_loads, cc, cc_Δp
) )
info(LOGGER, "Mismatch in #$cc_i with reduced generation: $cc_Δp")
else # overproduction is unavoidable else # overproduction is unavoidable
### Fail complete component ### Fail complete component
info(LOGGER, "Overproduction is unavoidable => #$cc_i fails!")
_deactivate_cc!(network_data, active_cc_gens, active_cc_loads) _deactivate_cc!(network_data, active_cc_gens, active_cc_loads)
end end
elseif cc_Δp < 0 # underproduction elseif cc_Δp < -ϵ # underproduction
if isapprox(cc_Δp_lim, 0, atol=1e-10) # power balance at maximum info(LOGGER, "Detected underproduction in (sub-)grid #$cc_i: $cc_Δp")
### Check how to restore APB
if isapprox(cc_Δp_lim, 0, atol=ϵ) # power balance at maximum
info(LOGGER, "Setting dispatches in #$cc_i to their upper bounds!")
### Set all active generators to their maximum ### Set all active generators to their maximum
for g in active_cc_gens for g in active_cc_gens
id = string(g) id = string(g)
network_data["gen"][id]["pg"] = network_data["gen"][id]["pmax"] network_data["gen"][id]["pg"] = network_data["gen"][id]["pmax"]
end end
elseif cc_Δp_lim < 0 # total demand cannot be matched elseif cc_Δp_lim < -ϵ # total demand cannot be matched
info(LOGGER, "Mismatch in #$cc_i requires shedding load!")
### Set all active generators to their maximum ### Set all active generators to their maximum
for g in active_cc_gens for g in active_cc_gens
id = string(g) id = string(g)
network_data["gen"][id]["pg"] = network_data["gen"][id]["pmax"] network_data["gen"][id]["pg"] = network_data["gen"][id]["pmax"]
end end
info(LOGGER, "Amount of load to be shed in #$cc_i: $cc_Δp_lim")
### Shed the minimum amount of load ### Shed the minimum amount of load
_shed_minimum_load!( cc_Δp = _shed_minimum_load!(
network_data, active_cc_gens, active_cc_loads, cc, cc_Δp_lim network_data, active_cc_gens, active_cc_loads, cc, cc_Δp_lim
) )
info(LOGGER, "Mismatch in #$cc_i after load shedding: $cc_Δp")
else # sufficient dispatch increase possible else # sufficient dispatch increase possible
info(LOGGER, "Mismatch in #$cc_i requires increasing generation!")
### Increase all dispatches uniformly ### Increase all dispatches uniformly
_increase_p_dispatch!( cc_Δp = _increase_p_dispatch!(
network_data, active_cc_gens, active_cc_loads, cc, cc_Δp network_data, active_cc_gens, active_cc_loads, cc, cc_Δp
) )
info(LOGGER, "Mismatch in #$cc_i with increased generation: $cc_Δp")
end end
end end
...@@ -405,7 +418,7 @@ function _reduce_p_dispatch!( ...@@ -405,7 +418,7 @@ function _reduce_p_dispatch!(
active_cc_gens::Array{Int64,1}, active_cc_gens::Array{Int64,1},
active_cc_loads::Array{Int64,1}, active_cc_loads::Array{Int64,1},
cc::Set{Int64}, cc::Set{Int64},
cc_Δp::Float64 # positive mismatch cc_Δp::Float64 # mismatch larger than tolerance ϵ
) )
### Find active generators that are able to reduce their dispatch ### Find active generators that are able to reduce their dispatch
...@@ -435,9 +448,8 @@ function _reduce_p_dispatch!( ...@@ -435,9 +448,8 @@ function _reduce_p_dispatch!(
) # repeat ) # repeat
end end
# info(_LOGGER, "2 Active power mismatch in CC #1: $cc_Δp") # return nothing
return cc_Δp
return nothing
end end
#= #=
...@@ -448,7 +460,7 @@ function _increase_p_dispatch!( ...@@ -448,7 +460,7 @@ function _increase_p_dispatch!(
active_cc_gens::Array{Int64,1}, active_cc_gens::Array{Int64,1},
active_cc_loads::Array{Int64,1}, active_cc_loads::Array{Int64,1},
cc::Set{Int64}, cc::Set{Int64},
cc_Δp::Float64 # negative mismatch cc_Δp::Float64 # mismatch smaller than tolerance -ϵ
) )
### Find active generators that are able to increase their dispatch ### Find active generators that are able to increase their dispatch
...@@ -478,7 +490,10 @@ function _increase_p_dispatch!( ...@@ -478,7 +490,10 @@ function _increase_p_dispatch!(
) # repeat ) # repeat
end end
return nothing # info(LOGGER, "New mismatch with increased generation: $cc_Δp")
# return nothing
return cc_Δp
end end
#= #=
...@@ -518,5 +533,5 @@ function _shed_minimum_load!( ...@@ -518,5 +533,5 @@ function _shed_minimum_load!(
) # repeat ) # repeat
end end
return nothing return cc_Δp
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment