Skip to content

Commit

Permalink
various improvements for pypsa and amiris loader
Browse files Browse the repository at this point in the history
  • Loading branch information
maurerle committed Dec 6, 2024
1 parent 7457a21 commit 307df8d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 36 deletions.
9 changes: 9 additions & 0 deletions assume/common/forecasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ def __init__(
index=self.index, value=price_forecast, name="price_forecast"
)

self.data_dict = {}

for key, value in kwargs.items():
self.data_dict[key] = FastSeries(index=self.index, value=value, name=key)



def __getitem__(self, column: str) -> FastSeries:
"""
Retrieves forecasted values.
Expand Down Expand Up @@ -579,5 +586,7 @@ def __getitem__(self, column: str) -> FastSeries:
return self.demand
elif column == "price_EOM":
return self.price_forecast
elif column in self.data_dict.keys():
return self.data_dict[column]
else:
return FastSeries(value=0.0, index=self.index)
55 changes: 28 additions & 27 deletions assume/common/grid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def add_generators(
if "marginal_cost" not in gen_c.columns:
gen_c["marginal_cost"] = p_set

network.madd(
network.add(
"Generator",
names=generators.index,
name=generators.index,
bus=generators["node"], # bus to which the generator is connected to
p_nom=generators[
"max_power"
Expand All @@ -49,16 +49,17 @@ def add_generators(
)
else:
# add generators
network.madd(
#generators.drop(["p_min_pu", "p_max_pu", "marginal_cost"], axis=1, inplace=True)7
#generators["p_min_pu"] = p_set
#generators["p_max_pu"] = p_set + 1
#generators["marginal_cost"] = p_set
network.add(
"Generator",
names=generators.index,
name=generators.index,
bus=generators["node"], # bus to which the generator is connected to
p_nom=generators[
"max_power"
], # Nominal capacity of the powerplant/generator
p_min_pu=p_set,
p_max_pu=p_set + 1,
marginal_cost=p_set,
**generators,
)

Expand All @@ -84,18 +85,18 @@ def add_redispatch_generators(
)

# add generators and their sold capacities as load with reversed sign to have fixed feed in
network.madd(
network.add(
"Load",
names=generators.index,
name=generators.index,
bus=generators["node"], # bus to which the generator is connected to
p_set=p_set,
sign=1,
)

# add upward redispatch generators
network.madd(
network.add(
"Generator",
names=generators.index,
name=generators.index,
suffix="_up",
bus=generators["node"], # bus to which the generator is connected to
p_nom=generators["max_power"], # Nominal capacity of the powerplant/generator
Expand All @@ -105,9 +106,9 @@ def add_redispatch_generators(
)

# add downward redispatch generators
network.madd(
network.add(
"Generator",
names=generators.index,
name=generators.index,
suffix="_down",
bus=generators["node"], # bus to which the generator is connected to
p_nom=generators["max_power"], # Nominal capacity of the powerplant/generator
Expand All @@ -118,18 +119,18 @@ def add_redispatch_generators(
)

# add upward and downward backup generators at each node
network.madd(
network.add(
"Generator",
names=network.buses.index,
name=network.buses.index,
suffix="_backup_up",
bus=network.buses.index, # bus to which the generator is connected to
p_nom=10e4,
marginal_cost=backup_marginal_cost,
)

network.madd(
network.add(
"Generator",
names=network.buses.index,
name=network.buses.index,
suffix="_backup_down",
bus=network.buses.index, # bus to which the generator is connected to
p_nom=10e4,
Expand All @@ -151,9 +152,9 @@ def add_backup_generators(
"""

# add backup generators at each node
network.madd(
network.add(
"Generator",
names=network.buses.index,
name=network.buses.index,
suffix="_backup",
bus=network.buses.index, # bus to which the generator is connected to
p_nom=10e4,
Expand All @@ -174,9 +175,9 @@ def add_loads(
"""

# add loads
network.madd(
network.add(
"Load",
names=loads.index,
name=loads.index,
bus=loads["node"], # bus to which the generator is connected to
**loads,
)
Expand All @@ -201,9 +202,9 @@ def add_redispatch_loads(
del loads_c["sign"]

# add loads with opposite sign (default for loads is -1). This is needed to properly model the redispatch
network.madd(
network.add(
"Load",
names=loads.index,
name=loads.index,
bus=loads["node"], # bus to which the generator is connected to
sign=1,
**loads_c,
Expand Down Expand Up @@ -237,9 +238,9 @@ def add_nodal_loads(
del loads_c["sign"]

# add loads as negative generators
network.madd(
network.add(
"Generator",
names=loads.index,
name=loads.index,
bus=loads["node"], # bus to which the generator is connected to
p_nom=loads["max_power"], # Nominal capacity of the powerplant/generator
p_min_pu=p_set,
Expand All @@ -264,10 +265,10 @@ def read_pypsa_grid(
"""

def add_buses(network: pypsa.Network, buses: pd.DataFrame) -> None:
network.import_components_from_dataframe(buses, "Bus")
network.add("Bus", buses.index, **buses)

def add_lines(network: pypsa.Network, lines: pd.DataFrame) -> None:
network.import_components_from_dataframe(lines, "Line")
network.add("Line", lines.index, **lines)

# setup the network
add_buses(network, grid_dict["buses"])
Expand Down
2 changes: 1 addition & 1 deletion assume/markets/clearing_algorithms/nodal_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,4 @@ def process_dispatch_data(
# rename columns
flows.columns = ["datetime", "line", "flow"]

return flows
return flows.to_dict(orient="records")
2 changes: 1 addition & 1 deletion assume/markets/clearing_algorithms/redispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def clear(
calculate_network_meta(network=redispatch_network, product=product, i=i)
)

# write network flows here if applicable
# TODO write network flows here
flows = []

return accepted_orders, rejected_orders, meta, flows
Expand Down
3 changes: 2 additions & 1 deletion assume/scenario/loader_amiris.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def add_agent_to_world(
"technology": "demand",
"price": load["ValueOfLostLoad"],
},
NaiveForecast(index, demand=demand_series),
# demand_series might contain more values than index
NaiveForecast(index, demand=demand_series[:len(index)]),
)

case "StorageTrader":
Expand Down
10 changes: 6 additions & 4 deletions assume/scenario/loader_pypsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ def load_pypsa(
load_t = network.loads_t["p_set"][load.name]
unit_type = "demand"

kwargs = {load.name: load_t}

world.add_unit(
f"demand_{load.name}",
load.name,
unit_type,
"demand_operator",
{
Expand All @@ -130,7 +132,7 @@ def load_pypsa(
"node": load.node,
"price": 1e3,
},
NaiveForecast(index, demand=load_t),
NaiveForecast(index, demand=load_t, **kwargs),
)

world.add_unit_operator("storage_operator")
Expand Down Expand Up @@ -171,7 +173,7 @@ def load_pypsa(
scenario = "world_pypsa"
study_case = "scigrid_de"
# "pay_as_clear", "redispatch" or "nodal"
market_mechanism = "nodal"
market_mechanism = "pay_as_clear"

match study_case:
case "ac_dc_meshed":
Expand All @@ -184,7 +186,7 @@ def load_pypsa(
logger.info(f"invalid studycase: {study_case}")
network = pd.DataFrame()

study_case += market_mechanism
study_case = f"{study_case}_{market_mechanism}"

start = network.snapshots[0]
end = network.snapshots[-1]
Expand Down
4 changes: 2 additions & 2 deletions assume/units/powerplant.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ def calculate_min_max_power(
min_power = min_power.clip(min=heat_demand)

available_power = self.forecaster.get_availability(self.id).loc[start:end_excl]
max_power = available_power * self.max_power
# check if available power is larger than max_power and raise an error if so
if (available_power > self.max_power).any():
if (max_power > self.max_power).any():
raise ValueError(
f"Available power is larger than max_power for unit {self.id} at time {start}."
)

max_power = available_power * self.max_power
# provide reserve for capacity_pos
max_power = max_power - self.outputs["capacity_pos"].loc[start:end_excl]
# remove what has already been bid
Expand Down
1 change: 1 addition & 0 deletions docker_configs/mqtt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# https://github.com/eclipse/mosquitto/blob/master/mosquitto.conf
listener 1883
set_tcp_nodelay true
allow_anonymous true

max_keepalive 3600

0 comments on commit 307df8d

Please sign in to comment.