diff --git a/examples/ac-dc-meshed/ac-dc-lopf.py b/examples/ac-dc-meshed/ac-dc-lopf.py index 10cc49521..4d27ac7d3 100644 --- a/examples/ac-dc-meshed/ac-dc-lopf.py +++ b/examples/ac-dc-meshed/ac-dc-lopf.py @@ -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) diff --git a/examples/ac-dc-meshed/ac-dc-lpf.py b/examples/ac-dc-meshed/ac-dc-lpf.py index f20fdc71c..56a1f9df9 100644 --- a/examples/ac-dc-meshed/ac-dc-lpf.py +++ b/examples/ac-dc-meshed/ac-dc-lpf.py @@ -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) diff --git a/examples/opf-storage-hvdc/opf-storage.py b/examples/opf-storage-hvdc/opf-storage.py index c842e4dcc..d045156c0 100644 --- a/examples/opf-storage-hvdc/opf-storage.py +++ b/examples/opf-storage-hvdc/opf-storage.py @@ -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) diff --git a/pypsa/components.py b/pypsa/components.py index 064705d0b..77094e0c5 100644 --- a/pypsa/components.py +++ b/pypsa/components.py @@ -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"] @@ -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"] @@ -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"] @@ -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) @@ -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) @@ -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 @@ -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"], @@ -790,13 +790,13 @@ 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: @@ -804,7 +804,7 @@ def consistency_check(self): 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: @@ -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] @@ -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) @@ -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')) @@ -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"} diff --git a/pypsa/contingency.py b/pypsa/contingency.py index d2198a984..ba688a11a 100644 --- a/pypsa/contingency.py +++ b/pypsa/contingency.py @@ -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 @@ -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] diff --git a/pypsa/graph.py b/pypsa/graph.py index d56d134c0..fe4d3baf6 100644 --- a/pypsa/graph.py +++ b/pypsa/graph.py @@ -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") @@ -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: @@ -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: diff --git a/pypsa/io.py b/pypsa/io.py index 5ae00909a..a1f773124 100644 --- a/pypsa/io.py +++ b/pypsa/io.py @@ -288,7 +288,7 @@ 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"] @@ -296,7 +296,7 @@ def _export_to_exporter(network, exporter, basename, export_standard_types=False 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 @@ -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) diff --git a/pypsa/networkclustering.py b/pypsa/networkclustering.py index 0131851ef..3cb784298 100644 --- a/pypsa/networkclustering.py +++ b/pypsa/networkclustering.py @@ -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") diff --git a/pypsa/opf.py b/pypsa/opf.py index 6ef07a5cb..ddf401993 100644 --- a/pypsa/opf.py +++ b/pypsa/opf.py @@ -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"]] @@ -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) @@ -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] @@ -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] diff --git a/pypsa/opt.py b/pypsa/opt.py index 0fe9998b3..fb1a4cc97 100644 --- a/pypsa/opt.py +++ b/pypsa/opt.py @@ -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) diff --git a/pypsa/pf.py b/pypsa/pf.py index 3e4360d07..fc2a4cf18 100644 --- a/pypsa/pf.py +++ b/pypsa/pf.py @@ -208,8 +208,6 @@ def sub_network_pf(sub_network, snapshots=None, skip_pre=False, x_tol=1e-6, use_ # _sub_network_prepare_pf(sub_network, snapshots, skip_pre, calculate_Y) network = sub_network.network - from .components import passive_branch_components, controllable_branch_components, controllable_one_port_components - if not skip_pre: calculate_dependent_values(network) find_bus_controls(sub_network) @@ -225,7 +223,7 @@ def sub_network_pf(sub_network, snapshots=None, skip_pre=False, x_tol=1e-6, use_ for n in ("q", "p"): # allow all one ports to dispatch as set - for c in sub_network.iterate_components(controllable_one_port_components): + for c in sub_network.iterate_components(network.controllable_one_port_components): c_n_set = get_switchable_as_dense(network, c.name, n + '_set', snapshots, c.ind) c.pnl[n].loc[snapshots, c.ind] = c_n_set @@ -234,13 +232,13 @@ def sub_network_pf(sub_network, snapshots=None, skip_pre=False, x_tol=1e-6, use_ sum([((c.pnl[n].loc[snapshots, c.ind] * c.df.loc[c.ind, 'sign']) .groupby(c.df.loc[c.ind, 'bus'], axis=1).sum() .reindex(columns=buses_o, fill_value=0.)) - for c in sub_network.iterate_components(controllable_one_port_components)]) + for c in sub_network.iterate_components(network.controllable_one_port_components)]) if n == "p": network.buses_t[n].loc[snapshots, buses_o] += sum( [(- c.pnl[n+str(i)].loc[snapshots].groupby(c.df["bus"+str(i)], axis=1).sum() .reindex(columns=buses_o, fill_value=0)) - for c in network.iterate_components(controllable_branch_components) + for c in network.iterate_components(network.controllable_branch_components) for i in [0,1]]) def f(guess): @@ -338,7 +336,7 @@ def dfdx(guess): #add voltages to branches buses_indexer = buses_o.get_indexer branch_bus0 = []; branch_bus1 = [] - for c in sub_network.iterate_components(passive_branch_components): + for c in sub_network.iterate_components(network.passive_branch_components): branch_bus0 += list(c.df.loc[c.ind, 'bus0']) branch_bus1 += list(c.df.loc[c.ind, 'bus1']) v0 = V[:,buses_indexer(branch_bus0)] @@ -352,7 +350,7 @@ def dfdx(guess): s0 = pd.DataFrame(v0*np.conj(i0), columns=branches_i, index=snapshots) s1 = pd.DataFrame(v1*np.conj(i1), columns=branches_i, index=snapshots) - for c in sub_network.iterate_components(passive_branch_components): + for c in sub_network.iterate_components(network.passive_branch_components): s0t = s0.loc[:,c.name] s1t = s1.loc[:,c.name] c.pnl.p0.loc[snapshots,s0t.columns] = s0t.values.real @@ -610,8 +608,6 @@ def find_bus_controls(sub_network): def calculate_B_H(sub_network,skip_pre=False): """Calculate B and H matrices for AC or DC sub-networks.""" - from .components import passive_branch_components - network = sub_network.network if not skip_pre: @@ -627,7 +623,7 @@ def calculate_B_H(sub_network,skip_pre=False): #susceptances b = 1./np.concatenate([(c.df.loc[c.ind, attribute]).values \ - for c in sub_network.iterate_components(passive_branch_components)]) + for c in sub_network.iterate_components(network.passive_branch_components)]) if np.isnan(b).any(): @@ -645,7 +641,7 @@ def calculate_B_H(sub_network,skip_pre=False): sub_network.p_branch_shift = -b*np.concatenate([(c.df.loc[c.ind, "phase_shift"]).values*np.pi/180. if c.name == "Transformer" else np.zeros((len(c.ind),)) - for c in sub_network.iterate_components(passive_branch_components)]) + for c in sub_network.iterate_components(network.passive_branch_components)]) sub_network.p_bus_shift = sub_network.K * sub_network.p_branch_shift @@ -916,10 +912,6 @@ def sub_network_lpf(sub_network, snapshots=None, skip_pre=False): logger.info("Performing linear load-flow on %s sub-network %s for snapshot(s) %s", sub_network.network.sub_networks.at[sub_network.name,"carrier"], sub_network, snapshots) - from .components import \ - one_port_components, controllable_one_port_components, \ - passive_branch_components, controllable_branch_components - network = sub_network.network @@ -939,7 +931,7 @@ def sub_network_lpf(sub_network, snapshots=None, skip_pre=False): network.shunt_impedances.g_pu.loc[shunt_impedances_i].values # allow all one ports to dispatch as set - for c in sub_network.iterate_components(controllable_one_port_components): + for c in sub_network.iterate_components(network.controllable_one_port_components): c_p_set = get_switchable_as_dense(network, c.name, 'p_set', snapshots, c.ind) c.pnl.p.loc[snapshots, c.ind] = c_p_set @@ -948,11 +940,11 @@ def sub_network_lpf(sub_network, snapshots=None, skip_pre=False): sum([((c.pnl.p.loc[snapshots, c.ind] * c.df.loc[c.ind, 'sign']) .groupby(c.df.loc[c.ind, 'bus'], axis=1).sum() .reindex(columns=buses_o, fill_value=0.)) - for c in sub_network.iterate_components(one_port_components)] + for c in sub_network.iterate_components(network.one_port_components)] + [(- c.pnl["p"+str(i)].loc[snapshots].groupby(c.df["bus"+str(i)], axis=1).sum() .reindex(columns=buses_o, fill_value=0)) - for c in network.iterate_components(controllable_branch_components) + for c in network.iterate_components(network.controllable_branch_components) for i in [0,1]]) if not skip_pre and len(branches_i) > 0: @@ -965,7 +957,7 @@ def sub_network_lpf(sub_network, snapshots=None, skip_pre=False): flows = pd.DataFrame(v_diff * sub_network.H.T, columns=branches_i, index=snapshots) + sub_network.p_branch_shift - for c in sub_network.iterate_components(passive_branch_components): + for c in sub_network.iterate_components(network.passive_branch_components): f = flows.loc[:, c.name] c.pnl.p0.loc[snapshots, f.columns] = f c.pnl.p1.loc[snapshots, f.columns] = -f diff --git a/test/test_pf_against_pypower.py b/test/test_pf_against_pypower.py index a92c9ca00..f6ebc32e2 100644 --- a/test/test_pf_against_pypower.py +++ b/test/test_pf_against_pypower.py @@ -55,7 +55,7 @@ def test_pypower_case(): network.pf() #compare branch flows - for c in network.iterate_components(pypsa.components.passive_branch_components): + for c in network.iterate_components(network.passive_branch_components): for si in ["p0","p1","q0","q1"]: si_pypsa = getattr(c.pnl,si).loc["now"].values si_pypower = results_df['branch'][si][c.df.original_index].values