Skip to content

Commit 3ab4bf3

Browse files
committed
Improvements to the code for integration with the power flow model
1 parent ca2cbc1 commit 3ab4bf3

File tree

1 file changed

+118
-37
lines changed

1 file changed

+118
-37
lines changed

src/core/microgrid.jl

Lines changed: 118 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ function Microgrid_Model(Microgrid_Inputs; JuMP_Model="", ldf_inputs_dictionary=
268268

269269
results, TerminationStatus = Run_REopt_PMD_Model(pm)
270270

271-
REopt_Results, PMD_Results, DataDictionaryForEachNode = Results_Processing_REopt_PMD_Model(pm.model, results, data_math_mn, REoptInputs_Combined, Microgrid_Settings)
272-
#model = pm.model
271+
REopt_Results, PMD_Results, DataDictionaryForEachNode, Dictionary_LineFlow_Power_Series_output, DataFrame_LineFlow_output = Results_Processing_REopt_PMD_Model(pm.model, results, data_math_mn, REoptInputs_Combined, Microgrid_Settings)
272+
model = pm.model
273273

274274
if Microgrid_Settings.RunOutageSimulator == true
275275
OutageLengths = Microgrid_Settings.LengthOfOutages_timesteps
@@ -306,8 +306,8 @@ function Microgrid_Model(Microgrid_Inputs; JuMP_Model="", ldf_inputs_dictionary=
306306
ComputationTime_EntireModel = EndTime_EntireModel - StartTime_EntireModel
307307

308308
#Results Compilation function
309-
DataFrame_LineFlow_Summary = "TBD"
310-
Dictionary_LineFlow_Power_Series = "TBD"
309+
DataFrame_LineFlow_Summary = DataFrame_LineFlow_output
310+
Dictionary_LineFlow_Power_Series = Dictionary_LineFlow_Power_Series_output
311311
system_results = REopt.Results_Compilation(REopt_Results, Outage_Results, Microgrid_Settings, DataFrame_LineFlow_Summary, Dictionary_LineFlow_Power_Series, TimeStamp, ComputationTime_EntireModel)
312312

313313

@@ -410,7 +410,7 @@ function Microgrid_Model(Microgrid_Inputs; JuMP_Model="", ldf_inputs_dictionary=
410410
if Microgrid_Settings.Model_Type == "BasicLinear"
411411
CreateResultsMap(CompiledResults, Microgrid_Settings, TimeStamp)
412412
end
413-
Aggregated_PowerFlows_Plot(CompiledResults, TimeStamp, Microgrid_Settings)
413+
Aggregated_PowerFlows_Plot(CompiledResults, TimeStamp, Microgrid_Settings, REoptInputs_Combined, model)
414414
end
415415

416416
return CompiledResults, model
@@ -887,9 +887,44 @@ function Results_Processing_REopt_PMD_Model(m, results, data_math_mn, REoptInput
887887
REopt_results = reopt_results(m, REoptInputs_Combined)
888888

889889
DataDictionaryForEachNodeForOutageSimulator = REopt.GenerateInputsForOutageSimulator(Microgrid_Settings, REopt_results)
890-
return REopt_results, sol_eng, DataDictionaryForEachNodeForOutageSimulator
891-
end
892890

891+
# Compute values for each line and store line power flows in a dataframe and dictionary
892+
DataLineFlow = zeros(7)
893+
DataFrame_LineFlow = DataFrame(fill(Any[],7), [:LineCode, :Minimum_LineFlow_ActivekW, :Maximum_LineFlow_ActivekW, :Average_LineFlow_ActivekW, :Minimum_LineFlow_ReactivekVAR, :Maximum_LineFlow_ReactivekVAR, :Average_LineFlow_ReactivekVAR ])
894+
Dictionary_LineFlow_Power_Series = Dict([])
895+
896+
for line in keys(sol_eng["nw"]["1"]["line"]) # read all of the line names from the first time step
897+
898+
#Phase = 1
899+
ActivePowerFlow_line_temp = []
900+
ReactivePowerFlow_line_temp = []
901+
for i in 1:length(sol_eng["nw"])
902+
push!(ActivePowerFlow_line_temp, sum(sol_eng["nw"][string(i)]["line"][line]["pf"][Phase] for Phase in keys(sol_eng["nw"][string(i)]["line"][line]["pf"])) )
903+
push!(ReactivePowerFlow_line_temp, sum(sol_eng["nw"][string(i)]["line"][line]["qf"][Phase] for Phase in keys(sol_eng["nw"][string(i)]["line"][line]["qf"])) )
904+
end
905+
906+
DataLineFlow[1] = round(minimum(ActivePowerFlow_line_temp[:]), digits = 5)
907+
DataLineFlow[2] = round(maximum(ActivePowerFlow_line_temp[:]), digits = 5)
908+
DataLineFlow[3] = round(mean(ActivePowerFlow_line_temp[:]), digits = 5)
909+
DataLineFlow[4] = round(minimum(ReactivePowerFlow_line_temp[:]), digits = 5)
910+
DataLineFlow[5] = round(maximum(ReactivePowerFlow_line_temp[:]), digits = 5)
911+
DataLineFlow[6] = round(mean(ReactivePowerFlow_line_temp[:]), digits = 5)
912+
913+
DataFrame_LineFlow_temp = DataFrame([("Line "*string(line)) DataLineFlow[1] DataLineFlow[2] DataLineFlow[3] DataLineFlow[4] DataLineFlow[5] DataLineFlow[6]], [:LineCode, :Minimum_LineFlow_ActivekW, :Maximum_LineFlow_ActivekW, :Average_LineFlow_ActivekW, :Minimum_LineFlow_ReactivekVAR, :Maximum_LineFlow_ReactivekVAR, :Average_LineFlow_ReactivekVAR])
914+
DataFrame_LineFlow = append!(DataFrame_LineFlow,DataFrame_LineFlow_temp)
915+
916+
# Also create a dictionary of the line power flows
917+
Dictionary_LineFlow_Power_Series_temp = Dict([(line, Dict([
918+
("ActiveLineFlow", ActivePowerFlow_line_temp),
919+
("ReactiveLineFlow", ReactivePowerFlow_line_temp)
920+
]))
921+
])
922+
merge!(Dictionary_LineFlow_Power_Series, Dictionary_LineFlow_Power_Series_temp)
923+
924+
end
925+
926+
return REopt_results, sol_eng, DataDictionaryForEachNodeForOutageSimulator, Dictionary_LineFlow_Power_Series, DataFrame_LineFlow
927+
end
893928

894929
function Check_REopt_PMD_Alignment()
895930
# Compare the REopt and PMD results to ensure the models are linked
@@ -2147,7 +2182,6 @@ end
21472182
function Results_Compilation(results, Outage_Results, Microgrid_Settings, DataFrame_LineFlow_Summary, Dictionary_LineFlow_Power_Series, TimeStamp, ComputationTime_EntireModel; line_upgrade_results = "", transformer_upgrade_results = "")
21482183

21492184
InputsList = Microgrid_Settings.REoptInputsList
2150-
LineFromSubstationToFacilityMeter = Microgrid_Settings.Substation_Node * "-" * Microgrid_Settings.FacilityMeter_Node
21512185

21522186
# Compute system-level outputs
21532187
system_results = Dict{String, Float64}()
@@ -2204,13 +2238,28 @@ function Results_Compilation(results, Outage_Results, Microgrid_Settings, DataFr
22042238
Data = []
22052239

22062240
if Microgrid_Settings.Model_Type == "PowerModelsDistribution"
2207-
MaximumPowerOnSubstationLine = "TBD"
2208-
MinimumPowerOnSubstationLine = "TBD"
2209-
AveragePowerOnSubstationLine = "TBD"
2241+
LineFromSubstationToFacilityMeter = "line"*Microgrid_Settings.Substation_Node * "_" * Microgrid_Settings.FacilityMeter_Node
2242+
2243+
MaximumPowerOnSubstationLine_ActivePower = (round(maximum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["ActiveLineFlow"]), digits = 0))
2244+
MinimumPowerOnSubstationLine_ActivePower = (round(minimum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["ActiveLineFlow"]), digits = 0))
2245+
AveragePowerOnSubstationLine_ActivePower = (round(mean(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["ActiveLineFlow"]), digits = 0))
2246+
2247+
MaximumPowerOnSubstationLine_ReactivePower = (round(maximum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["ReactiveLineFlow"]), digits = 0))
2248+
MinimumPowerOnSubstationLine_ReactivePower = (round(minimum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["ReactiveLineFlow"]), digits = 0))
2249+
AveragePowerOnSubstationLine_ReactivePower = (round(mean(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["ReactiveLineFlow"]), digits = 0))
2250+
22102251
elseif Microgrid_Settings.Model_Type == "BasicLinear"
2211-
MaximumPowerOnSubstationLine = (round(maximum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["NetRealLineFlow"]), digits = 0))
2212-
MinimumPowerOnSubstationLine = (round(minimum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["NetRealLineFlow"]), digits = 0))
2213-
AveragePowerOnSubstationLine = (round(mean(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["NetRealLineFlow"]), digits = 0))
2252+
LineFromSubstationToFacilityMeter = Microgrid_Settings.Substation_Node * "-" * Microgrid_Settings.FacilityMeter_Node
2253+
2254+
MaximumPowerOnSubstationLine_ActivePower = (round(maximum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["NetRealLineFlow"]), digits = 0))
2255+
MinimumPowerOnSubstationLine_ActivePower = (round(minimum(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["NetRealLineFlow"]), digits = 0))
2256+
AveragePowerOnSubstationLine_ActivePower = (round(mean(Dictionary_LineFlow_Power_Series[LineFromSubstationToFacilityMeter]["NetRealLineFlow"]), digits = 0))
2257+
2258+
# Temporarily not recording the reactive power through the lines:
2259+
MaximumPowerOnSubstationLine_ReactivePower = zeros(8760)
2260+
MinimumPowerOnSubstationLine_ReactivePower = zeros(8760)
2261+
AveragePowerOnSubstationLine_ReactivePower = zeros(8760)
2262+
22142263
end
22152264

22162265
# Add system-level results
@@ -2242,13 +2291,20 @@ function Results_Compilation(results, Outage_Results, Microgrid_Settings, DataFr
22422291
push!(DataLabels," Total generator size kw")
22432292
push!(Data, round(system_results["total_generator_size_kw"],digits=0))
22442293

2245-
push!(DataLabels," Maximum power flow on substation line")
2246-
push!(Data, MaximumPowerOnSubstationLine)
2247-
push!(DataLabels," Minimum power flow on substation line")
2248-
push!(Data, MinimumPowerOnSubstationLine)
2249-
push!(DataLabels," Average power flow on substation line")
2250-
push!(Data, AveragePowerOnSubstationLine)
2251-
2294+
push!(DataLabels," Maximum power flow on substation line, Active Power kW")
2295+
push!(Data, MaximumPowerOnSubstationLine_ActivePower)
2296+
push!(DataLabels," Minimum power flow on substation line, Active Power kW")
2297+
push!(Data, MinimumPowerOnSubstationLine_ActivePower)
2298+
push!(DataLabels," Average power flow on substation line, Active Power kW")
2299+
push!(Data, AveragePowerOnSubstationLine_ActivePower)
2300+
2301+
push!(DataLabels," Maximum power flow on substation line, Reactive Power kVAR")
2302+
push!(Data, MaximumPowerOnSubstationLine_ReactivePower)
2303+
push!(DataLabels," Minimum power flow on substation line, Reactive Power kVAR")
2304+
push!(Data, MinimumPowerOnSubstationLine_ReactivePower)
2305+
push!(DataLabels," Average power flow on substation line, Reactive Power kVAR")
2306+
push!(Data, AveragePowerOnSubstationLine_ReactivePower)
2307+
22522308
# Add the microgrid outage results to the dataframe
22532309
push!(DataLabels, "----Microgrid Outage Results----")
22542310
push!(Data, "")
@@ -2350,8 +2406,8 @@ function Results_Compilation(results, Outage_Results, Microgrid_Settings, DataFr
23502406
dataframe_results = DataFrame(Labels = DataLabels, Data = Data)
23512407
CSV.write(Microgrid_Settings.FolderLocation*"/results_"*TimeStamp*"/Results_Summary_"*TimeStamp*".csv", dataframe_results)
23522408

2353-
# Save the Line Flow summary for each line to a different csv
2354-
#CSV.write(Microgrid_Settings.FolderLocation*"/results_"*TimeStamp*"/Results_Line_Flow_Summary_"*TimeStamp*".csv", DataFrame_LineFlow_Summary)
2409+
# Save the Line Flow summary to a different csv
2410+
CSV.write(Microgrid_Settings.FolderLocation*"/results_"*TimeStamp*"/Results_Line_Flow_Summary_"*TimeStamp*".csv", DataFrame_LineFlow_Summary)
23552411

23562412
# Save line upgrade results to a csv
23572413
if Microgrid_Settings.Model_Line_Upgrades
@@ -2374,10 +2430,14 @@ function Results_Compilation(results, Outage_Results, Microgrid_Settings, DataFr
23742430
display(DataFrame_LineFlow_Summary)
23752431

23762432
print("\nSubstation data: ")
2377-
print("\n Maximum power flow from substation: "*string(MaximumPowerOnSubstationLine))
2378-
print("\n Minimum power flow from substation: "*string(MinimumPowerOnSubstationLine))
2379-
print("\n Average power flow from substation: "*string(AveragePowerOnSubstationLine))
2433+
print("\n Maximum active power flow from substation, kW: "*string(MaximumPowerOnSubstationLine_ActivePower))
2434+
print("\n Minimum active power flow from substation, kW: "*string(MinimumPowerOnSubstationLine_ActivePower))
2435+
print("\n Average active power flow from substation, kW: "*string(AveragePowerOnSubstationLine_ActivePower))
23802436

2437+
print("\n Maximum reactive power flow from substation, kVAR: "*string(MaximumPowerOnSubstationLine_ReactivePower))
2438+
print("\n Minimum reactive power flow from substation, kVAR: "*string(MinimumPowerOnSubstationLine_ReactivePower))
2439+
print("\n Average reactive power flow from substation, kVAR: "*string(AveragePowerOnSubstationLine_ReactivePower))
2440+
23812441
# Print results for each node:
23822442
for n in InputsList
23832443
NodeNumberTempB = n["Site"]["node"]
@@ -2550,7 +2610,7 @@ function CreateResultsMap(results, Microgrid_Inputs, TimeStamp)
25502610
end
25512611

25522612
# Function to create additional plots using PlotlyJS
2553-
function Aggregated_PowerFlows_Plot(results, TimeStamp, Microgrid_Inputs)
2613+
function Aggregated_PowerFlows_Plot(results, TimeStamp, Microgrid_Inputs, REoptInputs_Combined, model)
25542614
# Additonal plotting using PlotlyJS
25552615

25562616
OutageStartTimeStep = Microgrid_Inputs.SingleOutageStartTimeStep
@@ -2578,9 +2638,8 @@ function Aggregated_PowerFlows_Plot(results, TimeStamp, Microgrid_Inputs)
25782638
end
25792639
print("\n The nodes with PV are: ")
25802640
print(NodesWithPV)
2581-
2582-
# TODO: account for the situation where one node might be exporting PV and then another node might use that power to charge a battery
25832641

2642+
# TODO: account for the situation where one node might be exporting PV and then another node might use that power to charge a battery
25842643
PVOutput = zeros(8760)
25852644
for NodeNumberTemp in NodesWithPV
25862645
PVOutput = PVOutput + results["REopt_results"][NodeNumberTemp]["PV"]["electric_to_load_series_kw"] + results["REopt_results"][NodeNumberTemp]["PV"]["electric_to_grid_series_kw"]
@@ -2604,14 +2663,36 @@ function Aggregated_PowerFlows_Plot(results, TimeStamp, Microgrid_Inputs)
26042663
NodesWithGenerator = []
26052664
for i in keys(results["REopt_results"])
26062665
if "Generator" in keys(results["REopt_results"][i])
2607-
push!(NodesWithBattery, i)
2666+
push!(NodesWithGenerator, i)
26082667
end
26092668
end
26102669
GeneratorOutput = zeros(8760)
26112670
for NodeNumberTemp in NodesWithGenerator
2612-
GeneratorOutput = GeneratorOutput + results["REopt_results"][NodeNumberTemp]["Generator"]["electric_to_load_series_kw"] + results["REopt_results"][NodeNumberTemp]["Generator"]["electric_to_grid_series_kw"] + results["REopt_results"][NodeNumberTemp]["Generator"]["electric_to_storage_series_kw"]
2671+
GeneratorOutput = GeneratorOutput + results["REopt_results"][NodeNumberTemp]["Generator"]["electric_to_load_series_kw"].data + results["REopt_results"][NodeNumberTemp]["Generator"]["electric_to_grid_series_kw"].data # + results["REopt_results"][NodeNumberTemp]["Generator"]["electric_to_storage_series_kw"].data
26132672
end
2614-
2673+
2674+
#print("\n The generator output is: ")
2675+
#print(GeneratorOutput)
2676+
# Save the REopt Inputs for the site not to a variable
2677+
print("\n The facility meter node REopt inputs are being recorded")
2678+
FacilityMeterNode_REoptInputs = ""
2679+
for p in REoptInputs_Combined
2680+
if string(p.s.site.node) == p.s.settings.facilitymeter_node
2681+
FacilityMeterNode_REoptInputs = p
2682+
end
2683+
end
2684+
print("\n The facility meter node REopt inputs have been recorded")
2685+
2686+
# Save power input from the grid to a variable for plotting
2687+
PowerFromGrid = zeros(8760)
2688+
if Microgrid_Inputs.Model_Type == "PowerModelsDistribution"
2689+
for t in collect(1:8760)
2690+
PowerFromGrid[t] = -( (sum(value.(model[Symbol("dvGridPurchase_"*FacilityMeterNode_REoptInputs.s.settings.facilitymeter_node)]).data[t, tier] for tier in 1:FacilityMeterNode_REoptInputs.s.electric_tariff.n_energy_tiers)) - (sum(value.(model[Symbol("dvProductionToGrid_"*FacilityMeterNode_REoptInputs.s.settings.facilitymeter_node)])["PV", u, t] for u in FacilityMeterNode_REoptInputs.export_bins_by_tech["PV"])))
2691+
end
2692+
elseif Microgrid_Inputs.Model_Type == "BasicLinear"
2693+
PowerFromGrid = results["FromREopt_Dictionary_LineFlow_Power_Series"]["0-15"]["NetRealLineFlow"]
2694+
end
2695+
26152696
#Plot the network-wide power use
26162697

26172698
# Static plot
@@ -2620,7 +2701,7 @@ function Aggregated_PowerFlows_Plot(results, TimeStamp, Microgrid_Inputs)
26202701
Plots.plot!(days, PVOutput, label="Combined PV Output")
26212702
Plots.plot!(days, BatteryOutput, label = "Combined Battery Output")
26222703
Plots.plot!(days, GeneratorOutput, label = "Combined Generator Output")
2623-
#Plots.plot!(days, results["FromREopt_Dictionary_LineFlow_Power_Series"]["0-15"]["NetRealLineFlow"])
2704+
Plots.plot!(days, PowerFromGrid, label = "Grid Power")
26242705
if (OutageStopTimeStep - OutageStartTimeStep) > 0
26252706
OutageStart_Line = OutageStartTimeStep/24
26262707
OutageStop_Line = OutageStopTimeStep/24
@@ -2653,10 +2734,10 @@ function Aggregated_PowerFlows_Plot(results, TimeStamp, Microgrid_Inputs)
26532734
x = days,
26542735
y = GeneratorOutput
26552736
))
2656-
#push!(traces, PlotlyJS.scatter(name = "Power from Substation", showlegend = true, fill = "none", line = PlotlyJS.attr(width = 3),
2657-
# x = days,
2658-
# y = results["FromREopt_Dictionary_LineFlow_Power_Series"]["0-15"]["NetRealLineFlow"]
2659-
#))
2737+
push!(traces, PlotlyJS.scatter(name = "Power from Substation", showlegend = true, fill = "none", line = PlotlyJS.attr(width = 3),
2738+
x = days,
2739+
y = PowerFromGrid
2740+
))
26602741

26612742
if (OutageStopTimeStep - OutageStartTimeStep) > 0
26622743
OutageStart_Line = OutageStartTimeStep/24

0 commit comments

Comments
 (0)