Skip to content

Commit

Permalink
Merge pull request #311 from Breakthrough-Energy/daniel/bus_demand
Browse files Browse the repository at this point in the history
Add get_bus_demand method
  • Loading branch information
danielolsen authored Oct 8, 2020
2 parents 9d290b6 + 90eaf9b commit 28fce98
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
15 changes: 14 additions & 1 deletion powersimdata/scenario/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from powersimdata.input.input_data import InputData
from powersimdata.input.transform_profile import TransformProfile
from powersimdata.output.output_data import OutputData, construct_load_shed
from powersimdata.scenario.helpers import calculate_bus_demand
from powersimdata.scenario.state import State
from powersimdata.utility import server_setup

Expand Down Expand Up @@ -242,7 +243,7 @@ def get_demand(self, original=True):
:param bool original: should the original demand profile or the
potentially modified one be returned.
:return: (*pandas.DataFrame*) -- data frame of demand.
:return: (*pandas.DataFrame*) -- data frame of demand (hour, zone).
"""
profile = TransformProfile(
self._ssh, self._scenario_info, self.get_grid(), self.get_ct()
Expand Down Expand Up @@ -272,6 +273,18 @@ def get_demand(self, original=True):
demand[start:end] *= 1.0 - value / 100.0
return demand

def get_bus_demand(self):
"""Returns demand profiles, by bus.
:return: (*pandas.DataFrame*) -- data frame of demand (hour, bus).
"""
profile = TransformProfile(
self._ssh, self._scenario_info, self.get_grid(), self.get_ct()
)
demand = profile.get_profile("demand")
grid = self.get_grid()
return calculate_bus_demand(grid.bus, demand)

def get_hydro(self):
"""Returns hydro profile
Expand Down
15 changes: 14 additions & 1 deletion powersimdata/scenario/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from powersimdata.input.transform_grid import TransformGrid
from powersimdata.input.transform_profile import TransformProfile
from powersimdata.scenario.execute import Execute
from powersimdata.scenario.helpers import check_interconnect, interconnect2name
from powersimdata.scenario.helpers import (
calculate_bus_demand,
check_interconnect,
interconnect2name,
)
from powersimdata.scenario.state import State
from powersimdata.utility import server_setup
from powersimdata.utility.transfer_data import upload
Expand Down Expand Up @@ -149,6 +153,15 @@ def get_demand(self):
"""
return self.get_profile("demand")

def get_bus_demand(self):
"""Returns demand profiles, by bus.
:return: (*pandas.DataFrame*) -- data frame of demand (hour, bus).
"""
demand = self.get_profile("demand")
grid = self.get_grid()
return calculate_bus_demand(grid.bus, demand)

def get_hydro(self):
"""Returns hydro profile.
Expand Down
26 changes: 26 additions & 0 deletions powersimdata/scenario/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import numpy as np
import pandas as pd


def check_interconnect(interconnect):
"""Sets interconnect.
Expand Down Expand Up @@ -42,3 +46,25 @@ def interconnect2name(interconnect):
return "easternwestern"
else:
return "usa"


def calculate_bus_demand(bus, demand):
"""Calculates bus-level demand from zone-level demand.
:param pandas.DataFrame bus: bus data frame.
:param pandas.DataFrame demand: demand data frame.
:return: (*pandas.DataFrame*) -- dataframe of (hour, bus) demand.
"""
zone_demand = bus.groupby("zone_id").sum().Pd
bus_zone_share = bus.apply(lambda x: x.Pd / zone_demand.loc[x.zone_id], axis=1)
bus_to_zone = np.zeros((len(zone_demand), len(bus)))
bus_idx_lookup = {b: i for i, b in enumerate(bus.index)}
zone_idx_lookup = {z: i for i, z in enumerate(zone_demand.index)}
for b in bus.index:
bus_idx = bus_idx_lookup[b]
zone_idx = zone_idx_lookup[bus.loc[b, "zone_id"]]
bus_to_zone[zone_idx, bus_idx] = bus_zone_share.loc[b]
bus_demand = pd.DataFrame(
(demand.to_numpy() @ bus_to_zone), index=demand.index, columns=bus.index
)
return bus_demand

0 comments on commit 28fce98

Please sign in to comment.