Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if aggregate_generators_weighted:
one_port_components.remove("Generator")
generators, generators_pnl = aggregategenerators(network, busmap, with_time=with_time,
carriers=aggregate_generators_carriers,
custom_strategies=generator_strategies)
io.import_components_from_dataframe(network_c, generators, "Generator")
if with_time:
for attr, df in iteritems(generators_pnl):
if not df.empty:
io.import_series_from_dataframe(network_c, df, "Generator", attr)
for one_port in aggregate_one_ports:
one_port_components.remove(one_port)
new_df, new_pnl = aggregateoneport(network, busmap, component=one_port, with_time=with_time,
custom_strategies=one_port_strategies.get(one_port, {}))
io.import_components_from_dataframe(network_c, new_df, one_port)
for attr, df in iteritems(new_pnl):
io.import_series_from_dataframe(network_c, df, one_port, attr)
##
# Collect remaining one ports
for c in network.iterate_components(one_port_components):
io.import_components_from_dataframe(
network_c,
c.df.assign(bus=c.df.bus.map(busmap)).dropna(subset=['bus']),
c.name
)
if with_time:
for c in network.iterate_components(one_port_components):
kv_u = network.buses.at[u,"v_nom"]
kv_v = network.buses.at[v,"v_nom"]
print(u,v,dist,kv_u,kv_v)
last_scigrid_line = int(network.lines.index[-1])
voltage = max(kv_u,kv_v)*1000
wires = {220000 : 2, 380000 : 4}[voltage]
cables = 6
df = pd.DataFrame([[u,v,length_factor*dist,wires,cables,voltage]],columns=["bus0","bus1","length","wires","cables","voltage"],index=[str(last_scigrid_line+1)])
pypsa.io.import_components_from_dataframe(network,df,"Line")
### Split buses with more than one voltage; add trafos between
#
#This code splits the buses where you have 220 and 380 kV lines landing.
network.lines.voltage.value_counts()
buses_by_voltage = {}
for voltage in network.lines.voltage.value_counts().index:
buses_by_voltage[voltage] = set(network.lines[network.lines.voltage == voltage].bus0)\
| set(network.lines[network.lines.voltage == voltage].bus1)
network.buses.v_nom=380
network.buses.loc[buses_by_voltage[220000],"v_nom"] = 220
def get_clustering_from_busmap(network, busmap, with_time=True, line_length_factor=1.0,
aggregate_generators_weighted=False, aggregate_one_ports={},
aggregate_generators_carriers=None,
bus_strategies=dict(), one_port_strategies=dict(),
generator_strategies=dict()):
buses, linemap, linemap_p, linemap_n, lines = get_buses_linemap_and_lines(network, busmap, line_length_factor, bus_strategies)
network_c = Network()
io.import_components_from_dataframe(network_c, buses, "Bus")
io.import_components_from_dataframe(network_c, lines, "Line")
if with_time:
network_c.snapshot_weightings = network.snapshot_weightings.copy()
network_c.set_snapshots(network.snapshots)
one_port_components = network.one_port_components.copy()
if aggregate_generators_weighted:
one_port_components.remove("Generator")
generators, generators_pnl = aggregategenerators(network, busmap, with_time=with_time,
carriers=aggregate_generators_carriers,
custom_strategies=generator_strategies)
io.import_components_from_dataframe(network_c, generators, "Generator")
if with_time:
for attr, df in iteritems(generators_pnl):
if not df.empty:
io.import_components_from_dataframe(network_c, buses, "Bus")
io.import_components_from_dataframe(network_c, lines, "Line")
if with_time:
network_c.snapshot_weightings = network.snapshot_weightings.copy()
network_c.set_snapshots(network.snapshots)
one_port_components = network.one_port_components.copy()
if aggregate_generators_weighted:
one_port_components.remove("Generator")
generators, generators_pnl = aggregategenerators(network, busmap, with_time=with_time,
carriers=aggregate_generators_carriers,
custom_strategies=generator_strategies)
io.import_components_from_dataframe(network_c, generators, "Generator")
if with_time:
for attr, df in iteritems(generators_pnl):
if not df.empty:
io.import_series_from_dataframe(network_c, df, "Generator", attr)
for one_port in aggregate_one_ports:
one_port_components.remove(one_port)
new_df, new_pnl = aggregateoneport(network, busmap, component=one_port, with_time=with_time,
custom_strategies=one_port_strategies.get(one_port, {}))
io.import_components_from_dataframe(network_c, new_df, one_port)
for attr, df in iteritems(new_pnl):
io.import_series_from_dataframe(network_c, df, one_port, attr)
##
# Collect remaining one ports
io.import_series_from_dataframe(network_c, df, "Generator", attr)
for one_port in aggregate_one_ports:
one_port_components.remove(one_port)
new_df, new_pnl = aggregateoneport(network, busmap, component=one_port, with_time=with_time,
custom_strategies=one_port_strategies.get(one_port, {}))
io.import_components_from_dataframe(network_c, new_df, one_port)
for attr, df in iteritems(new_pnl):
io.import_series_from_dataframe(network_c, df, one_port, attr)
##
# Collect remaining one ports
for c in network.iterate_components(one_port_components):
io.import_components_from_dataframe(
network_c,
c.df.assign(bus=c.df.bus.map(busmap)).dropna(subset=['bus']),
c.name
)
if with_time:
for c in network.iterate_components(one_port_components):
for attr, df in iteritems(c.pnl):
if not df.empty:
io.import_series_from_dataframe(network_c, df, c.name, attr)
new_links = (network.links.assign(bus0=network.links.bus0.map(busmap),
bus1=network.links.bus1.map(busmap))
.dropna(subset=['bus0', 'bus1'])
.loc[lambda df: df.bus0 != df.bus1])
io.import_components_from_dataframe(network_c, new_links, "Link")
links["s_nom"] = [3.**0.5*row["voltage"]/1000.*coeffs.get(row["voltage"],default)["i"]*(row["wires"]/coeffs.get(row["voltage"],default)["wires_typical"])*(row["cables"]/3.) for i,row in links.iterrows()]
print(links["voltage"].value_counts(dropna=False))
print(links[links["length_m"] <=0])
print(links[(links["voltage"] != 220000) & (links["voltage"] != 380000)])
print(links[pd.isnull(links.cables)])
network = pypsa.Network()
pypsa.io.import_components_from_dataframe(network,vertices,"Bus")
pypsa.io.import_components_from_dataframe(network,links,"Line")
network.lines[["b","x","r","b_pu","x_pu","r_pu"]]
network.build_graph()
network.determine_network_topology()
#remove small isolated networks
for sn in network.sub_networks.obj:
buses = sn.buses()
branches = sn.branches()
print(sn,len(buses))
if len(buses) < 5:
print(branches,sn.buses)
for bus in buses.obj:
for attr, df in iteritems(c.pnl):
if not df.empty:
io.import_series_from_dataframe(network_c, df, c.name, attr)
new_links = (network.links.assign(bus0=network.links.bus0.map(busmap),
bus1=network.links.bus1.map(busmap))
.dropna(subset=['bus0', 'bus1'])
.loc[lambda df: df.bus0 != df.bus1])
io.import_components_from_dataframe(network_c, new_links, "Link")
if with_time:
for attr, df in iteritems(network.links_t):
if not df.empty:
io.import_series_from_dataframe(network_c, df, "Link", attr)
io.import_components_from_dataframe(network_c, network.carriers, "Carrier")
network_c.determine_network_topology()
return Clustering(network_c, busmap, linemap, linemap_p, linemap_n)
Returns
--------
network : pypsa.Network
Examples
--------
>>> network_copy = network.copy()
"""
network = Network()
for component in self.iterate_components():
import_components_from_dataframe(network, component.df, component.name)
if with_time:
network.set_snapshots(self.snapshots)
for component in self.iterate_components():
pnl = getattr(network, component.typ.list_name+"_t")
for k in iterkeys(component.pnl):
pnl[k] = component.pnl[k].copy()
#catch all remaining attributes of network
for attr in ["name", "now", "co2_limit", "srid"]:
setattr(network,attr,getattr(self,attr))
return network