Skip to content
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

Remove Memento.jl and use Base Julia logging #887

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ InfrastructureModels = "2030c09a-7f63-5d83-885d-db604e0e9cc0"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Memento = "f28f55f0-a522-5efc-85c2-fe41dfb9b2d9"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

Expand All @@ -20,7 +19,6 @@ Ipopt = "~0.8, ~0.9, ~1"
JSON = "~0.18, ~0.19, ~0.20, ~0.21"
JuMP = "~0.22, ~0.23, 1"
Juniper = "~0.8, ~0.9"
Memento = "~1.0, ~1.1, ~1.2, ~1.3, ~1.4"
NLsolve = "4.0"
SCS = "~0.9, ~1.0"
julia = "1.6"
Expand Down
20 changes: 0 additions & 20 deletions src/PowerModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module PowerModels
import LinearAlgebra, SparseArrays

import JSON
import Memento

import NLsolve

Expand All @@ -13,25 +12,6 @@ import InfrastructureModels
import InfrastructureModels: optimize_model!, @im_fields, nw_id_default
const _IM = InfrastructureModels

# Create our module level logger (this will get precompiled)
const _LOGGER = Memento.getlogger(@__MODULE__)

# Register the module level logger at runtime so that folks can access the logger via `getlogger(PowerModels)`
# NOTE: If this line is not included then the precompiled `PowerModels._LOGGER` won't be registered at runtime.
__init__() = Memento.register(_LOGGER)

"Suppresses information and warning messages output by PowerModels, for fine grained control use the Memento package"
function silence()
Memento.info(_LOGGER, "Suppressing information and warning messages for the rest of this session. Use the Memento package for more fine-grained control of logging.")
Memento.setlevel!(Memento.getlogger(InfrastructureModels), "error")
Memento.setlevel!(Memento.getlogger(PowerModels), "error")
end

"alows the user to set the logging level without the need to add Memento"
function logger_config!(level)
Memento.config!(Memento.getlogger("PowerModels"), level)
end

const _pm_global_keys = Set(["time_series", "per_unit"])
const pm_it_name = "pm"
const pm_it_sym = Symbol(pm_it_name)
Expand Down
18 changes: 9 additions & 9 deletions src/core/admittance_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Base.show(io::IO, x::AdmittanceMatrix{<:Number}) = print(io, "AdmittanceMatrix($
"data should be a PowerModels network data model; only supports networks with exactly one reference bus"
function calc_admittance_matrix(data::Dict{String,<:Any})
if length(data["dcline"]) > 0
Memento.error(_LOGGER, "calc_admittance_matrix does not support data with dclines")
@error("calc_admittance_matrix does not support data with dclines")
end
if length(data["switch"]) > 0
Memento.error(_LOGGER, "calc_admittance_matrix does not support data with switches")
@error("calc_admittance_matrix does not support data with switches")
end

#TODO check single connected component
Expand Down Expand Up @@ -79,10 +79,10 @@ end
"data should be a PowerModels network data model; only supports networks with exactly one refrence bus"
function calc_susceptance_matrix(data::Dict{String,<:Any})
if length(data["dcline"]) > 0
Memento.error(_LOGGER, "calc_susceptance_matrix does not support data with dclines")
@error("calc_susceptance_matrix does not support data with dclines")
end
if length(data["switch"]) > 0
Memento.error(_LOGGER, "calc_susceptance_matrix does not support data with switches")
@error("calc_susceptance_matrix does not support data with switches")
end

#TODO check single connected component
Expand Down Expand Up @@ -157,15 +157,15 @@ function calc_susceptance_matrix_inv(data::Dict{String,<:Any})
ref_bus = reference_bus(data)
ref_idx = sm.bus_to_idx[ref_bus["index"]]
if !(ref_idx > 0 && ref_idx <= num_buses)
Memento.error(_LOGGER, "invalid ref_idx in calc_susceptance_matrix_inv")
@error("invalid ref_idx in calc_susceptance_matrix_inv")
end
S[ref_idx, :] .= 0.0
S[:, ref_idx] .= 0.0
S[ref_idx, ref_idx] = 1.0

F = LinearAlgebra.ldlt(Symmetric(S); check=false)
if !LinearAlgebra.issuccess(F)
Memento.error(_LOGGER, "Failed factorization in calc_susceptance_matrix_inv")
@error("Failed factorization in calc_susceptance_matrix_inv")
end
M = F \ Matrix(1.0I, num_buses, num_buses)
M[ref_idx, :] .= 0.0 # zero-out the row of the slack bus
Expand All @@ -178,7 +178,7 @@ function calc_admittance_matrix_inv(am::AdmittanceMatrix, ref_idx::Int)
num_buses = length(am.idx_to_bus)

if !(ref_idx > 0 && ref_idx <= num_buses)
Memento.error(_LOGGER, "invalid ref_idx in calc_admittance_matrix_inv")
@error("invalid ref_idx in calc_admittance_matrix_inv")
end

M = Matrix(am.matrix)
Expand Down Expand Up @@ -274,10 +274,10 @@ data should be a PowerModels network data model
"""
function calc_bus_injection(data::Dict{String,<:Any})
if length(data["dcline"]) > 0
Memento.error(_LOGGER, "calc_bus_injection does not support data with dclines")
@error("calc_bus_injection does not support data with dclines")
end
if length(data["switch"]) > 0
Memento.error(_LOGGER, "calc_bus_injection does not support data with switches")
@error("calc_bus_injection does not support data with switches")
end

bus_values = Dict(bus["index"] => Dict{String,Float64}() for (i,bus) in data["bus"])
Expand Down
12 changes: 6 additions & 6 deletions src/core/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ function solve_model(data::Dict{String,<:Any}, model_type::Type, optimizer, buil
if multinetwork != _IM.ismultinetwork(data)
model_requirement = multinetwork ? "multi-network" : "single-network"
data_type = _IM.ismultinetwork(data) ? "multi-network" : "single-network"
Memento.error(_LOGGER, "attempted to build a $(model_requirement) model with $(data_type) data")
@error("attempted to build a $(model_requirement) model with $(data_type) data")
end

if multiconductor != ismulticonductor(data)
model_requirement = multiconductor ? "multi-conductor" : "single-conductor"
data_type = ismulticonductor(data) ? "multi-conductor" : "single-conductor"
Memento.error(_LOGGER, "attempted to build a $(model_requirement) model with $(data_type) data")
@error("attempted to build a $(model_requirement) model with $(data_type) data")
end

start_time = time()
pm = instantiate_model(data, model_type, build_method; ref_extensions=ref_extensions, kwargs...)
Memento.debug(_LOGGER, "pm model build time: $(time() - start_time)")
@debug("pm model build time: $(time() - start_time)")

start_time = time()
result = optimize_model!(pm, relax_integrality=relax_integrality, optimizer=optimizer, solution_processors=solution_processors)
Memento.debug(_LOGGER, "pm model solve and solution time: $(time() - start_time)")
@debug("pm model solve and solution time: $(time() - start_time)")

return result
end
Expand Down Expand Up @@ -194,7 +194,7 @@ function ref_add_core!(ref::Dict{Symbol,Any})
nw_ref[:ref_buses] = ref_buses

if length(ref_buses) > 1
Memento.warn(_LOGGER, "multiple reference buses found, $(keys(ref_buses)), this can cause infeasibility if they are in the same connected component")
@warn("multiple reference buses found, $(keys(ref_buses)), this can cause infeasibility if they are in the same connected component")
end

### aggregate info for pairs of connected buses ###
Expand All @@ -214,7 +214,7 @@ function _check_missing_keys(dict, keys, type)
end
end
if length(missing) > 0
error(_LOGGER, "the formulation $(type) requires the following varible(s) $(keys) but the $(missing) variable(s) were not found in the model")
error("the formulation $(type) requires the following varible(s) $(keys) but the $(missing) variable(s) were not found in the model")
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/core/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"checks if a sufficient number of variables exist for the given keys collection"
function _check_var_keys(vars, keys, var_name, comp_name)
if length(vars) < length(keys)
error(_LOGGER, "$(var_name) decision variables appear to be missing for $(comp_name) components")
error("$(var_name) decision variables appear to be missing for $(comp_name) components")
end
end

Expand Down
14 changes: 7 additions & 7 deletions src/core/constraint_template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ function constraint_thermal_limit_from_on_off(pm::AbstractPowerModel, i::Int; nw
f_idx = (i, f_bus, t_bus)

if !haskey(branch, "rate_a")
Memento.error(_LOGGER, "constraint_thermal_limit_from_on_off requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
@error("constraint_thermal_limit_from_on_off requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
end

constraint_thermal_limit_from_on_off(pm, nw, i, f_idx, branch["rate_a"])
Expand All @@ -658,7 +658,7 @@ function constraint_thermal_limit_to_on_off(pm::AbstractPowerModel, i::Int; nw::
t_idx = (i, t_bus, f_bus)

if !haskey(branch, "rate_a")
Memento.error(_LOGGER, "constraint_thermal_limit_to_on_off requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
@error("constraint_thermal_limit_to_on_off requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
end

constraint_thermal_limit_to_on_off(pm, nw, i, t_idx, branch["rate_a"])
Expand All @@ -673,7 +673,7 @@ function constraint_ne_thermal_limit_from(pm::AbstractPowerModel, i::Int; nw::In
f_idx = (i, f_bus, t_bus)

if !haskey(branch, "rate_a")
Memento.error(_LOGGER, "constraint_thermal_limit_from_ne requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
@error("constraint_thermal_limit_from_ne requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
end

constraint_ne_thermal_limit_from(pm, nw, i, f_idx, branch["rate_a"])
Expand All @@ -688,7 +688,7 @@ function constraint_ne_thermal_limit_to(pm::AbstractPowerModel, i::Int; nw::Int=
t_idx = (i, t_bus, f_bus)

if !haskey(branch, "rate_a")
Memento.error(_LOGGER, "constraint_thermal_limit_to_ne requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
@error("constraint_thermal_limit_to_ne requires a rate_a value on all branches, calc_thermal_limits! can be used to generate reasonable values")
end

constraint_ne_thermal_limit_to(pm, nw, i, t_idx, branch["rate_a"])
Expand Down Expand Up @@ -895,7 +895,7 @@ function constraint_storage_state(pm::AbstractPowerModel, i::Int; nw::Int=nw_id_
if haskey(ref(pm, nw), :time_elapsed)
time_elapsed = ref(pm, nw, :time_elapsed)
else
Memento.warn(_LOGGER, "network data should specify time_elapsed, using 1.0 as a default")
@warn("network data should specify time_elapsed, using 1.0 as a default")
time_elapsed = 1.0
end

Expand All @@ -909,15 +909,15 @@ function constraint_storage_state(pm::AbstractPowerModel, i::Int, nw_1::Int, nw_
if haskey(ref(pm, nw_2), :time_elapsed)
time_elapsed = ref(pm, nw_2, :time_elapsed)
else
Memento.warn(_LOGGER, "network $(nw_2) should specify time_elapsed, using 1.0 as a default")
@warn("network $(nw_2) should specify time_elapsed, using 1.0 as a default")
time_elapsed = 1.0
end

if haskey(ref(pm, nw_1, :storage), i)
constraint_storage_state(pm, nw_1, nw_2, i, storage["charge_efficiency"], storage["discharge_efficiency"], time_elapsed)
else
# if the storage device has status=0 in nw_1, then the stored energy variable will not exist. Initialize storage from data model instead.
Memento.warn(_LOGGER, "storage component $(i) was not found in network $(nw_1) while building constraint_storage_state between networks $(nw_1) and $(nw_2). Using the energy value from the storage component in network $(nw_2) instead")
@warn("storage component $(i) was not found in network $(nw_1) while building constraint_storage_state between networks $(nw_1) and $(nw_2). Using the energy value from the storage component in network $(nw_2) instead")
constraint_storage_state_initial(pm, nw_2, i, storage["energy"], storage["charge_efficiency"], storage["discharge_efficiency"], time_elapsed)
end
end
Expand Down
Loading