Skip to content

Commit

Permalink
everywhere: Move component type sets to Network object
Browse files Browse the repository at this point in the history
All sets pypsa.components.*_components are now found at
network.*_components so they can be dynamically altered by the user.
  • Loading branch information
nworbmot committed Jan 19, 2018
1 parent 2e1b05e commit 364ea17
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 86 deletions.
2 changes: 1 addition & 1 deletion examples/ac-dc-meshed/ac-dc-lopf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
p0 = 0.
p1 = 0.

for c in network.iterate_components(pypsa.components.branch_components):
for c in network.iterate_components(network.branch_components):

bs = (c.df.bus0 == bus)

Expand Down
2 changes: 1 addition & 1 deletion examples/ac-dc-meshed/ac-dc-lpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
p0 = 0.
p1 = 0.

for c in network.iterate_components(pypsa.components.branch_components):
for c in network.iterate_components(network.branch_components):

bs = (c.df.bus0 == bus)

Expand Down
2 changes: 1 addition & 1 deletion examples/opf-storage-hvdc/opf-storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
p0 = 0.
p1 = 0.

for c in network.iterate_components(pypsa.components.branch_components):
for c in network.iterate_components(network.branch_components):

bs = (c.df.bus0 == bus)

Expand Down
64 changes: 25 additions & 39 deletions pypsa/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def __init__(self, import_name=None, name="", ignore_standard_types=False,
def _build_dataframes(self):
"""Function called when network is created to build component pandas.DataFrames."""

for component in all_components:
for component in self.all_components:

attrs = self.components[component]["attrs"]

Expand All @@ -314,7 +314,7 @@ def _build_dataframes(self):

def read_in_default_standard_types(self):

for std_type in standard_types:
for std_type in self.standard_type_components:

list_name = self.components[std_type]["list_name"]

Expand Down Expand Up @@ -383,7 +383,7 @@ def set_snapshots(self,snapshots):
if isinstance(snapshots, pd.DatetimeIndex) and _pd_version < '0.18.0':
snapshots = pd.Index(snapshots.values)

for component in all_components:
for component in self.all_components:
pnl = self.pnl(component)
attrs = self.components[component]["attrs"]

Expand Down Expand Up @@ -615,10 +615,10 @@ def copy(self, with_time=True, ignore_standard_types=False):

network = self.__class__(ignore_standard_types=ignore_standard_types)

for component in self.iterate_components(["Bus", "Carrier"] + sorted(all_components - {"Bus","Carrier"})):
for component in self.iterate_components(["Bus", "Carrier"] + sorted(self.all_components - {"Bus","Carrier"})):
df = component.df
#drop the standard types to avoid them being read in twice
if not ignore_standard_types and component.name in standard_types:
if not ignore_standard_types and component.name in self.standard_type_components:
df = component.df.drop(network.components[component.name]["standard_types"].index)

import_components_from_dataframe(network, df, component.name)
Expand Down Expand Up @@ -676,24 +676,24 @@ def __getitem__(self, key):
)
buses_i = n.buses.index

rest_components = all_components - standard_types - one_port_components - branch_components
rest_components = self.all_components - self.standard_type_components - self.one_port_components - self.branch_components
for c in rest_components - {"Bus", "SubNetwork"}:
n.import_components_from_dataframe(pd.DataFrame(self.df(c)), c)

for c in standard_types:
for c in self.standard_type_components:
df = self.df(c).drop(self.components[c]["standard_types"].index)
n.import_components_from_dataframe(pd.DataFrame(df), c)

for c in one_port_components:
for c in self.one_port_components:
df = self.df(c).loc[lambda df: df.bus.isin(buses_i)]
n.import_components_from_dataframe(pd.DataFrame(df), c)

for c in branch_components:
for c in self.branch_components:
df = self.df(c).loc[lambda df: df.bus0.isin(buses_i) & df.bus1.isin(buses_i)]
n.import_components_from_dataframe(pd.DataFrame(df), c)

n.set_snapshots(self.snapshots[time_i])
for c in all_components:
for c in self.all_components:
i = n.df(c).index
try:
npnl = n.pnl(c)
Expand All @@ -716,23 +716,23 @@ def __getitem__(self, key):
#beware, this turns bools like s_nom_extendable into objects because of
#presence of links without s_nom_extendable
def branches(self):
return pd.concat((self.df(c) for c in branch_components),
keys=branch_components)
return pd.concat((self.df(c) for c in self.branch_components),
keys=self.branch_components)

def passive_branches(self):
return pd.concat((self.df(c) for c in passive_branch_components),
keys=passive_branch_components)
return pd.concat((self.df(c) for c in self.passive_branch_components),
keys=self.passive_branch_components)

def controllable_branches(self):
return pd.concat((self.df(c) for c in controllable_branch_components),
keys=controllable_branch_components)
return pd.concat((self.df(c) for c in self.controllable_branch_components),
keys=self.controllable_branch_components)

def determine_network_topology(self):
"""
Build sub_networks from topology.
"""

adjacency_matrix = self.adjacency_matrix(passive_branch_components)
adjacency_matrix = self.adjacency_matrix(self.passive_branch_components)
n_components, labels = csgraph.connected_components(adjacency_matrix, directed=False)

# remove all old sub_networks
Expand Down Expand Up @@ -761,12 +761,12 @@ def determine_network_topology(self):

self.buses.loc[:, "sub_network"] = labels.astype(str)

for c in self.iterate_components(passive_branch_components):
for c in self.iterate_components(self.passive_branch_components):
c.df["sub_network"] = c.df.bus0.map(self.buses["sub_network"])

def iterate_components(self, components=None, skip_empty=True):
if components is None:
components = all_components
components = self.all_components

return (Component(name=c,
list_name=self.components[c]["list_name"],
Expand All @@ -790,21 +790,21 @@ def consistency_check(self):
"""


for c in self.iterate_components(one_port_components):
for c in self.iterate_components(self.one_port_components):
missing = c.df.index[~c.df.bus.isin(self.buses.index)]
if len(missing) > 0:
logger.warning("The following %s have buses which are not defined:\n%s",
c.list_name, missing)

for c in self.iterate_components(branch_components):
for c in self.iterate_components(self.branch_components):
for attr in ["bus0","bus1"]:
missing = c.df.index[~c.df[attr].isin(self.buses.index)]
if len(missing) > 0:
logger.warning("The following %s have %s which are not defined:\n%s",
c.list_name, attr, missing)


for c in self.iterate_components(passive_branch_components):
for c in self.iterate_components(self.passive_branch_components):
for attr in ["x","r"]:
bad = c.df.index[c.df[attr] == 0.]
if len(bad) > 0:
Expand All @@ -824,7 +824,7 @@ def consistency_check(self):
c.list_name, bad)


for c in self.iterate_components(all_components):
for c in self.iterate_components(self.all_components):
for attr in c.attrs.index[c.attrs.varying & c.attrs.static]:
attr_df = c.pnl[attr]

Expand All @@ -845,7 +845,7 @@ def consistency_check(self):

static_attrs = ['p_nom', 's_nom', 'e_nom']
varying_attrs = ['p_max_pu', 'e_max_pu']
for c in self.iterate_components(all_components - {'TransformerType'}):
for c in self.iterate_components(self.all_components - {'TransformerType'}):
varying_attr = c.attrs.index[c.attrs.varying].intersection(varying_attrs)
static_attr = c.attrs.index[c.attrs.static].intersection(static_attrs)

Expand Down Expand Up @@ -958,7 +958,7 @@ def transformers_i(self):
def branches_i(self):
types = []
names = []
for c in self.iterate_components(passive_branch_components):
for c in self.iterate_components(self.network.passive_branch_components):
types += len(c.ind) * [c.name]
names += list(c.ind)
return pd.MultiIndex.from_arrays([types, names], names=('type', 'name'))
Expand Down Expand Up @@ -1008,17 +1008,3 @@ def iterate_components(self, components=None, skip_empty=True):
c = Component(*c[:-1], ind=getattr(self, c.list_name + '_i')())
if not (skip_empty and len(c.ind) == 0):
yield c


standard_types = {"LineType", "TransformerType"}

passive_one_port_components = {"ShuntImpedance"}
controllable_one_port_components = {"Load", "Generator", "StorageUnit", "Store"}
one_port_components = passive_one_port_components|controllable_one_port_components

passive_branch_components = {"Line", "Transformer"}
controllable_branch_components = {"Link"}
branch_components = passive_branch_components|controllable_branch_components

#i.e. everything except "Network"
all_components = branch_components|one_port_components|standard_types|{"Bus", "SubNetwork", "Carrier", "GlobalConstraint"}
4 changes: 1 addition & 3 deletions pypsa/contingency.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ def network_lpf_contingency(network, snapshots=None, branch_outages=None):
"""

from .components import passive_branch_components

if snapshots is None:
snapshots = network.snapshots

Expand All @@ -126,7 +124,7 @@ def network_lpf_contingency(network, snapshots=None, branch_outages=None):

p0_base = pd.Series(index=passive_branches.index)

for c in passive_branch_components:
for c in network.passive_branch_components:
pnl = network.pnl(c)
p0_base[c] = pnl.p0.loc[snapshot]

Expand Down
12 changes: 6 additions & 6 deletions pypsa/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def graph(network, branch_components=None, weight=None):

if isinstance(network, components.Network):
if branch_components is None:
branch_components = components.branch_components
branch_components = network.branch_components
buses_i = network.buses.index
elif isinstance(network, components.SubNetwork):
if branch_components is None:
branch_components = components.passive_branch_components
branch_components = network.network.passive_branch_components
buses_i = network.buses_i()
else:
raise TypeError("build_graph must be called with a Network or a SubNetwork")
Expand Down Expand Up @@ -85,12 +85,12 @@ def adjacency_matrix(network, branch_components=None, busorder=None, weights=Non

if isinstance(network, components.Network):
if branch_components is None:
branch_components = components.branch_components
branch_components = network.branch_components
if busorder is None:
busorder = network.buses.index
elif isinstance(network, components.SubNetwork):
if branch_components is None:
branch_components = components.passive_branch_components
branch_components = network.network.passive_branch_components
if busorder is None:
busorder = network.buses_i()
else:
Expand Down Expand Up @@ -146,12 +146,12 @@ def incidence_matrix(network, branch_components=None, busorder=None):

if isinstance(network, components.Network):
if branch_components is None:
branch_components = components.branch_components
branch_components = network.branch_components
if busorder is None:
busorder = network.buses.index
elif isinstance(network, components.SubNetwork):
if branch_components is None:
branch_components = components.passive_branch_components
branch_components = network.network.passive_branch_components
if busorder is None:
busorder = network.buses_i()
else:
Expand Down
6 changes: 3 additions & 3 deletions pypsa/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ def _export_to_exporter(network, exporter, basename, export_standard_types=False
exporter.save_snapshots(snapshots)

exported_components = []
for component in pypsa.components.all_components - {"SubNetwork"}:
for component in network.all_components - {"SubNetwork"}:

list_name = network.components[component]["list_name"]
attrs = network.components[component]["attrs"]

df = network.df(component)
pnl = network.pnl(component)

if not export_standard_types and component in pypsa.components.standard_types:
if not export_standard_types and component in network.standard_type_components:
df = df.drop(network.components[component]["standard_types"].index)

# first do static attributes
Expand Down Expand Up @@ -538,7 +538,7 @@ def _import_from_importer(network, importer, basename, skip_time=False):
imported_components = []

# now read in other components; make sure buses and carriers come first
for component in ["Bus", "Carrier"] + sorted(pypsa.components.all_components - {"Bus", "Carrier", "SubNetwork"}):
for component in ["Bus", "Carrier"] + sorted(network.all_components - {"Bus", "Carrier", "SubNetwork"}):
list_name = network.components[component]["list_name"]

df = importer.get_static(list_name)
Expand Down
2 changes: 1 addition & 1 deletion pypsa/networkclustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def get_clustering_from_busmap(network, busmap, with_time=True, line_length_fact
if with_time:
network_c.set_snapshots(network.snapshots)

one_port_components = components.one_port_components.copy()
one_port_components = network.one_port_components.copy()

if aggregate_generators_weighted:
one_port_components.remove("Generator")
Expand Down
13 changes: 4 additions & 9 deletions pypsa/opf.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,12 @@ def define_passive_branch_flows_with_kirchhoff(network,snapshots,skip_vars=False

def define_passive_branch_constraints(network,snapshots):

from .components import passive_branch_components

passive_branches = network.passive_branches()
extendable_branches = passive_branches[passive_branches.s_nom_extendable]
fixed_branches = passive_branches[~ passive_branches.s_nom_extendable]

s_max_pu = pd.concat({c : get_switchable_as_dense(network,c,'s_max_pu')
for c in passive_branch_components}, axis=1)
for c in network.passive_branch_components}, axis=1)

flow_upper = {(b[0],b[1],sn) : [[(1,network.model.passive_branch_p[b[0],b[1],sn])],
"<=", s_max_pu.at[sn,b]*fixed_branches.at[b,"s_nom"]]
Expand Down Expand Up @@ -1123,9 +1121,6 @@ def define_linear_objective(network,snapshots):

def extract_optimisation_results(network, snapshots, formulation="angles"):

from .components import \
passive_branch_components, branch_components, controllable_one_port_components

if isinstance(snapshots, pd.DatetimeIndex) and _pd_version < '0.18.0':
# Work around pandas bug #12050 (https://github.com/pydata/pandas/issues/12050)
snapshots = pd.Index(snapshots.values)
Expand Down Expand Up @@ -1181,14 +1176,14 @@ def set_from_series(df, series):
pd.concat({c.name:
c.pnl.p.loc[snapshots].multiply(c.df.sign, axis=1)
.groupby(c.df.bus, axis=1).sum()
for c in network.iterate_components(controllable_one_port_components)}) \
for c in network.iterate_components(network.controllable_one_port_components)}) \
.sum(level=1) \
.reindex(columns=network.buses_t.p.columns, fill_value=0.)


# passive branches
passive_branches = as_series(model.passive_branch_p)
for c in network.iterate_components(passive_branch_components):
for c in network.iterate_components(network.passive_branch_components):
set_from_series(c.pnl.p0, passive_branches.loc[c.name])
c.pnl.p1.loc[snapshots] = - c.pnl.p0.loc[snapshots]

Expand Down Expand Up @@ -1262,7 +1257,7 @@ def set_from_series(df, series):


s_nom_extendable_passive_branches = as_series(model.passive_branch_s_nom)
for c in network.iterate_components(passive_branch_components):
for c in network.iterate_components(network.passive_branch_components):
c.df['s_nom_opt'] = c.df.s_nom
if c.df.s_nom_extendable.any():
c.df.loc[c.df.s_nom_extendable, 's_nom_opt'] = s_nom_extendable_passive_branches.loc[c.name]
Expand Down
3 changes: 1 addition & 2 deletions pypsa/opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,9 @@ def empty_model(model):
@contextmanager
def empty_network(network):
logger.debug("Storing pypsa timeseries to disk")
from .components import all_components

panels = {}
for c in all_components:
for c in network.all_components:
attr = network.components[c]["list_name"] + "_t"
panels[attr] = getattr(network, attr)
setattr(network, attr, None)
Expand Down
Loading

0 comments on commit 364ea17

Please sign in to comment.