Skip to content

Commit

Permalink
REF: main function to add Docs, better efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudocubic committed Feb 8, 2021
1 parent a5173d3 commit 72dd88d
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/app/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ function parse_commandline()
help = "path to output file"
default = "./output.json"
"--formulation", "-f"
help = "mathematical formulation to solve (acr, acp, lindistflow, nfa)"
default = "acr"
help = "mathematical formulation to solve (lindistflow (default), acr, acp, nfa)"
default = "lindistflow"
"--problem", "-p"
help = "optimization problem type (opf, mld)"
default = "mld"
default = "opf"
"--protection-settings"
help = "XLSX (Excel) File with Protection settings"
default = ""
Expand Down Expand Up @@ -55,46 +55,66 @@ function entrypoint(args::Dict{String,<:Any})
Memento.setlevel!(Memento.getlogger(PowerModelsONM.PMD._PM), "error")
end

# Load events
events = haskey(args, "events") && !isempty(args["events"]) && !isnothing(args["events"]) ? parse_events(args["events"]) : Vector{Dict{String,Any}}([])

data_eng, mn_data_eng = prepare_network_case(args["network-file"]; events=events)
# ENGINEERING MODEL, mutlinetwork and base case with timeseries objects
(data_eng, mn_data_eng) = prepare_network_case(args["network-file"]; events=events);

# Initialize output data structure
output_data = build_blank_output(data_eng)

# MATHEMATICAL MULTINETWORK MODEL
mn_data_math = PMD._map_eng2math_multinetwork(mn_data_eng)
PMD.correct_network_data!(mn_data_math; make_pu=true)

form = get_formulation(args["formulation"])
problem = get_problem(args["problem"], haskey(mn_data_math, "nw"))

# NLP Solver to use for Load shed and OPF
solver = build_solver_instance(args["solver-tolerance"], get(args, "verbose", false))

result = solve_problem(problem, mn_data_math, form, solver)
# Optimal Load Shed
result = solve_problem(PMD.solve_mn_mc_mld_simple, mn_data_math, PMD.LPUBFDiagPowerModel, solver; solution_processors=[getproperty(PowerModelsONM, Symbol("sol_ldf2$(args["formulation"])!"))])

# Apply the results of the load-shed to the MATHEMATICAL MODEL
apply_load_shed!(mn_data_math, result)

optimize_switches!(mn_data_math)
# Optimal Switching
if haskey(data_eng, "switch") && !isempty(data_eng["switch"]) && any(sw["dispatchable"] == PMD.YES for (_,sw) in data_eng["switch"])
osw_result = optimize_switches!(mn_data_math; solution_processors=[getproperty(PowerModelsONM, Symbol("sol_ldf2$(args["formulation"])!"))]);
end

# Output switching actions to output data
get_timestep_device_actions!(output_data, mn_data_math)

sol_pu, sol_si = transform_solutions(result["solution"], mn_data_math)
# Final optimal dispatch
form = get_formulation(args["formulation"])
problem = get_problem(args["problem"], haskey(mn_data_math, "nw"))
result = solve_problem(problem, mn_data_math, form, solver; solution_processors=[PMD.sol_data_model!])

# Build solutions for statistics outputs
sol_pu, sol_si = transform_solutions(result["solution"], mn_data_math);

# Building output statistics
get_timestep_voltage_stats!(output_data, sol_pu, data_eng)
get_timestep_load_served!(output_data, sol_si, data_eng)
get_timestep_generator_profiles!(output_data, sol_si)
get_timestep_powerflow_output!(output_data, sol_si, data_eng)
get_timestep_storage_soc!(output_data, sol_si, data_eng)

# Get Protection Settings for Switch settings
protection_data = haskey(args, "protection-settings") && !isempty(args["protection-settings"]) && !isnothing(args["protection-settings"]) ? parse_protection_tables(args["protection-settings"]) : Dict{NamedTuple,Dict{String,Any}}()
get_timestep_protection_settings!(output_data, protection_data)

# Pass events to output data
output_data["Events"] = events

# Export final result dict (debugging)
if !isempty(args["export"])
open(args["export"], "w") do f
JSON.print(f, result, 2)
end
end

# Save output data
open(args["output-file"], "w") do f
JSON.print(f, output_data, 2)
end
Expand Down

0 comments on commit 72dd88d

Please sign in to comment.