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
function __init__()
### Setup logger (PowerModels.jl logger will inherit the format)
global _LOGGER = Memento.config!(
global LOGGER = Memento.config!(
"info", fmt="[{date} | {level} | {name}]: {msg}"
)
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)
=#
function config_logger(level="info")
info(_LOGGER, "Logger level set to $(level)!")
setlevel!(_LOGGER, level)
info(LOGGER, "Logger level set to $(level)!")
setlevel!(LOGGER, level)
return nothing
end
......
......@@ -161,9 +161,8 @@ function calc_total_impact!(
### Check whether a .log file is passed for logging
if isempty(logpath) == false
# push!(_LOGGER, DefaultHandler(logpath)) # add .log file to logger
### Add .log file to logger and set format
push!(_LOGGER,
push!(LOGGER,
DefaultHandler(
logpath, DefaultFormatter("[{date} | {level} | {name}]: {msg}")
)
......@@ -178,19 +177,25 @@ function calc_total_impact!(
### Deactivate transmission lines destroyed in frame f and log them
destroyed_tl = [dmg[1] for dmg in tl_damage if dmg[2] == f]
destroy_tl!(network_data, destroyed_tl) # update network topology
info(_LOGGER,
info(LOGGER,
"Overhead transmission lines destroyed in wind frame $f: " *
"$destroyed_tl"
) # log destroyed transmission lines
### 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
if isempty(solpath) == false # save final network data
save(solpath, "network_data", network_data)
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
end
......@@ -199,10 +204,11 @@ Calculates whether a cascading failure unfolds in a damaged network. If any netw
=#
function calc_cascade!(
network_data::Dict{String,<:Any}, # network with destroyed lines
method = :JuMP,
print_level = 5, # level of detail for Ipopt output
logpath = "", # optional path to .log file for saving logs of interest
solpath = "" # path for saving results/solutions of interest
method = :JuMP, # solver to use
print_level = 5, # level of detail for Ipopt output (if method = :JuMP)
iteration = 0 # counting iterations during a cascade (starts with 0)
# 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.)
......@@ -216,27 +222,23 @@ function calc_cascade!(
### Calculate new AC-PF solution
set_ac_pf_start_values!(network_data) # last state as initial conditions
### Check whether to write Ipopt output to a .log file
if isempty(logpath) == false
output = @capture_out calc_ac_pf!(
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
output = @capture_out calc_ac_pf!(
network_data, method, print_level=print_level
)
info(LOGGER, output) # pass Ipopt output to logger
### Check for overloaded branches
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
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
disable_branches!(network_data, [br[1] for br in overloaded_br])
### Repeat cascade algorithm
calc_cascade!(network_data, method, print_level, logpath, solpath)
calc_cascade!(network_data, method, print_level, iteration)
end
return nothing
......@@ -278,7 +280,7 @@ function restore_p_balance!(network_data::Dict{String,<:Any})
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!(
network_data::Dict{String,<:Any},
......@@ -300,51 +302,62 @@ function _restore_p_balance!(
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
nothing # nothing has to be done
elseif cc_Δp > 0 # overproduction
debug(_LOGGER, "Overproduction in CC #$cc_i: $cc_Δp")
if cc_Δp > ϵ # overproduction
info(LOGGER, "Detected overproduction in (sub-)grid #$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
for g in active_cc_gens
id = string(g)
network_data["gen"][id]["pg"] = network_data["gen"][id]["pmin"]
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_p_dispatch!(
cc_Δp = _reduce_p_dispatch!(
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
### Fail complete component
info(LOGGER, "Overproduction is unavoidable => #$cc_i fails!")
_deactivate_cc!(network_data, active_cc_gens, active_cc_loads)
end
elseif cc_Δp < 0 # underproduction
if isapprox(cc_Δp_lim, 0, atol=1e-10) # power balance at maximum
elseif cc_Δp < -ϵ # underproduction
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
for g in active_cc_gens
id = string(g)
network_data["gen"][id]["pg"] = network_data["gen"][id]["pmax"]
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
for g in active_cc_gens
id = string(g)
network_data["gen"][id]["pg"] = network_data["gen"][id]["pmax"]
end
info(LOGGER, "Amount of load to be shed in #$cc_i: $cc_Δp_lim")
### 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
)
info(LOGGER, "Mismatch in #$cc_i after load shedding: $cc_Δp")
else # sufficient dispatch increase possible
info(LOGGER, "Mismatch in #$cc_i requires increasing generation!")
### Increase all dispatches uniformly
_increase_p_dispatch!(
cc_Δp = _increase_p_dispatch!(
network_data, active_cc_gens, active_cc_loads, cc, cc_Δp
)
info(LOGGER, "Mismatch in #$cc_i with increased generation: $cc_Δp")
end
end
......@@ -405,7 +418,7 @@ function _reduce_p_dispatch!(
active_cc_gens::Array{Int64,1},
active_cc_loads::Array{Int64,1},
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
......@@ -435,9 +448,8 @@ function _reduce_p_dispatch!(
) # repeat
end
# info(_LOGGER, "2 Active power mismatch in CC #1: $cc_Δp")
return nothing
# return nothing
return cc_Δp
end
#=
......@@ -448,7 +460,7 @@ function _increase_p_dispatch!(
active_cc_gens::Array{Int64,1},
active_cc_loads::Array{Int64,1},
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
......@@ -478,7 +490,10 @@ function _increase_p_dispatch!(
) # repeat
end
return nothing
# info(LOGGER, "New mismatch with increased generation: $cc_Δp")
# return nothing
return cc_Δp
end
#=
......@@ -518,5 +533,5 @@ function _shed_minimum_load!(
) # repeat
end
return nothing
return cc_Δp
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