From 6cf21f9ae1b5742dde8742b4add43144125ee0c9 Mon Sep 17 00:00:00 2001 From: Allen Moore Date: Mon, 18 May 2026 18:04:32 -0400 Subject: [PATCH 1/5] new generatorsByBus and storageByBus sets --- gtep/model_library/components.py | 28 ++++++++++++++++++++++++++++ gtep/model_library/dispatch.py | 20 ++++---------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/gtep/model_library/components.py b/gtep/model_library/components.py index 7d6bfdf1..bb15556e 100644 --- a/gtep/model_library/components.py +++ b/gtep/model_library/components.py @@ -70,6 +70,19 @@ def add_model_sets(m, stages, rep_per=["a", "b"], com_per=2, dis_per=2): doc="All generators", ) + m.generatorsByBus = pyo.Set( + m.buses, + initialize={ + bus: + [ + gen + for gen in m.generators + if m.md.data["elements"]["generator"][gen]["bus"] == bus + ] + for bus in m.buses + } + ) + m.thermalGenerators = pyo.Set( within=m.generators, initialize=( @@ -105,6 +118,21 @@ def add_model_sets(m, stages, rep_per=["a", "b"], com_per=2, dis_per=2): doc="Potential storage units", ) + m.storageByBus = pyo.Set( + m.buses, + initialize={ + bus: + [ + batt + for batt in m.storage + if m.md.data["elements"]["storage"][batt]["bus"] == bus + ] + if m.md.data["elements"].get("storage") + else [] + for bus in m.buses + } + ) + def add_model_parameters(m, num_commit, num_dispatch, duration_dispatch): """Creates and labels all the parameters in the GTEP model. This diff --git a/gtep/model_library/dispatch.py b/gtep/model_library/dispatch.py index 2b66c285..656ba846 100644 --- a/gtep/model_library/dispatch.py +++ b/gtep/model_library/dispatch.py @@ -257,29 +257,17 @@ def flow_balance(b, bus): start_points = [ line for line in m.transmission if m.transmission[line]["to_bus"] == bus ] - gens = [ - gen - for gen in m.generators - if m.md.data["elements"]["generator"][gen]["bus"] == bus - ] - batts = [] - if m.config["storage"]: - batts = [ - bat - for bat in m.storage - if m.md.data["elements"]["storage"][bat]["bus"] == bus - ] balance -= sum(b.powerFlow[i] for i in end_points) balance += sum(b.powerFlow[i] for i in start_points) balance += sum( - b.thermalGeneration[g] for g in gens if g in m.thermalGenerators + b.thermalGeneration[g] for g in m.thermalGenerators & m.generatorsByBus[bus] ) balance += sum( - b.renewableGeneration[g] for g in gens if g in m.renewableGenerators + b.renewableGeneration[g] for g in m.renewableGenerators & m.generatorsByBus[bus] ) """ Battery Storage added to flow balance constraint """ - balance += sum(b.storageDischarged[bt] for bt in batts) - balance -= sum(b.storageCharged[bt] for bt in batts) + balance += sum(b.storageDischarged[bt] for bt in m.storageByBus[bus]) + balance -= sum(b.storageCharged[bt] for bt in m.storageByBus[bus]) # Add the loads as a parameter (already includes units). balance -= m.loads[bus] From 9d077bd7ecc6bda15d2e25635895f1300f4145b7 Mon Sep 17 00:00:00 2001 From: Allen Moore Date: Mon, 18 May 2026 18:12:33 -0400 Subject: [PATCH 2/5] black --- gtep/model_library/components.py | 24 ++++++++++++------------ gtep/model_library/dispatch.py | 6 ++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/gtep/model_library/components.py b/gtep/model_library/components.py index bb15556e..275b6671 100644 --- a/gtep/model_library/components.py +++ b/gtep/model_library/components.py @@ -73,14 +73,13 @@ def add_model_sets(m, stages, rep_per=["a", "b"], com_per=2, dis_per=2): m.generatorsByBus = pyo.Set( m.buses, initialize={ - bus: - [ + bus: [ gen for gen in m.generators if m.md.data["elements"]["generator"][gen]["bus"] == bus ] for bus in m.buses - } + }, ) m.thermalGenerators = pyo.Set( @@ -121,16 +120,17 @@ def add_model_sets(m, stages, rep_per=["a", "b"], com_per=2, dis_per=2): m.storageByBus = pyo.Set( m.buses, initialize={ - bus: - [ - batt - for batt in m.storage - if m.md.data["elements"]["storage"][batt]["bus"] == bus - ] - if m.md.data["elements"].get("storage") - else [] + bus: ( + [ + batt + for batt in m.storage + if m.md.data["elements"]["storage"][batt]["bus"] == bus + ] + if m.md.data["elements"].get("storage") + else [] + ) for bus in m.buses - } + }, ) diff --git a/gtep/model_library/dispatch.py b/gtep/model_library/dispatch.py index 656ba846..2733450c 100644 --- a/gtep/model_library/dispatch.py +++ b/gtep/model_library/dispatch.py @@ -260,10 +260,12 @@ def flow_balance(b, bus): balance -= sum(b.powerFlow[i] for i in end_points) balance += sum(b.powerFlow[i] for i in start_points) balance += sum( - b.thermalGeneration[g] for g in m.thermalGenerators & m.generatorsByBus[bus] + b.thermalGeneration[g] + for g in m.thermalGenerators & m.generatorsByBus[bus] ) balance += sum( - b.renewableGeneration[g] for g in m.renewableGenerators & m.generatorsByBus[bus] + b.renewableGeneration[g] + for g in m.renewableGenerators & m.generatorsByBus[bus] ) """ Battery Storage added to flow balance constraint """ balance += sum(b.storageDischarged[bt] for bt in m.storageByBus[bus]) From 6ff07f9f78dbbc54dacfe8c31e009651ce82b0fc Mon Sep 17 00:00:00 2001 From: Allen Moore Date: Tue, 26 May 2026 09:42:15 -0400 Subject: [PATCH 3/5] remove unneeded lines in constraints --- gtep/model_library/dispatch.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/gtep/model_library/dispatch.py b/gtep/model_library/dispatch.py index a4b644a1..f4a4d78a 100644 --- a/gtep/model_library/dispatch.py +++ b/gtep/model_library/dispatch.py @@ -229,16 +229,17 @@ def CP_flow_balance(b): loads = [l for l in b.loads] gens = [gen for gen in m.generators] batts = [bat for bat in m.storage] + # Add generation to constraint balance += sum( b.thermalGeneration[g] for g in gens if g in m.thermalGenerators ) balance += sum( b.renewableGeneration[g] for g in gens if g in m.renewableGenerators ) - """ Battery Storage added to flow balance constraint """ + # Add battery storage to constraint balance += sum(b.storageDischarged[bt] for bt in batts) balance -= sum(b.storageCharged[bt] for bt in batts) - + # Add the loads as a parameter (already includes units). balance -= sum(c_p.loads[l] for l in loads) balance += sum(b.loadShed[bus] for bus in buses) @@ -249,22 +250,12 @@ def CP_flow_balance(b): @b.Constraint(m.buses, doc="Energy balance constraint") def flow_balance(b, bus): balance = 0 + # Add power flow to constraint end_points = [line for line in m.lines if m.from_bus[line] == bus] start_points = [line for line in m.lines if m.to_bus[line] == bus] - gens = [ - gen - for gen in m.generators - if m.md.data["elements"]["generator"][gen]["bus"] == bus - ] - batts = [] - if m.config["storage"]: - batts = [ - bat - for bat in m.storage - if m.md.data["elements"]["storage"][bat]["bus"] == bus - ] balance -= sum(b.powerFlow[i] for i in end_points) balance += sum(b.powerFlow[i] for i in start_points) + # Add generation to constraint balance += sum( b.thermalGeneration[g] for g in m.thermalGenerators & m.generatorsByBus[bus] @@ -273,14 +264,13 @@ def flow_balance(b, bus): b.renewableGeneration[g] for g in m.renewableGenerators & m.generatorsByBus[bus] ) - """ Battery Storage added to flow balance constraint """ + # Add battery storage to constraint balance += sum(b.storageDischarged[bt] for bt in m.storageByBus[bus]) balance -= sum(b.storageCharged[bt] for bt in m.storageByBus[bus]) - # Add the loads as a parameter (already includes units). balance -= c_p.loads[bus] balance += b.loadShed[bus] - return balance == 0 * u.MW + return balance == 0 # NOTE: In comparison to reference [1], this is "per renewable # generator". [TODO: Should we include charging costs from From 1b0d5e656bcce87e045780b7acf4fe53d12dc9af Mon Sep 17 00:00:00 2001 From: Allen Moore Date: Tue, 26 May 2026 09:42:54 -0400 Subject: [PATCH 4/5] fix unit back --- gtep/model_library/dispatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtep/model_library/dispatch.py b/gtep/model_library/dispatch.py index f4a4d78a..7b967c3f 100644 --- a/gtep/model_library/dispatch.py +++ b/gtep/model_library/dispatch.py @@ -270,7 +270,7 @@ def flow_balance(b, bus): # Add the loads as a parameter (already includes units). balance -= c_p.loads[bus] balance += b.loadShed[bus] - return balance == 0 + return balance == 0 * u.MW # NOTE: In comparison to reference [1], this is "per renewable # generator". [TODO: Should we include charging costs from From 47f1328eb1605b32f65b4cca7e758d2d2fd098a2 Mon Sep 17 00:00:00 2001 From: Allen Moore Date: Tue, 26 May 2026 12:33:55 -0400 Subject: [PATCH 5/5] remove space messing up black --- gtep/model_library/dispatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtep/model_library/dispatch.py b/gtep/model_library/dispatch.py index 7b967c3f..1013d789 100644 --- a/gtep/model_library/dispatch.py +++ b/gtep/model_library/dispatch.py @@ -239,7 +239,7 @@ def CP_flow_balance(b): # Add battery storage to constraint balance += sum(b.storageDischarged[bt] for bt in batts) balance -= sum(b.storageCharged[bt] for bt in batts) - # Add the loads as a parameter (already includes units). + # Add the loads as a parameter (already includes units). balance -= sum(c_p.loads[l] for l in loads) balance += sum(b.loadShed[bus] for bus in buses)