pypsa.Network.add#
- Network.add(class_name: str, name: str | int | Sequence[int | str], suffix: str = '', overwrite: bool = False, **kwargs: Any) Index#
Add components to the network.
Handles addition of single and multiple components along with their attributes. Pass a list of names to add multiple components at once or pass a single name to add a single component.
When a single component is added, all non-scalar attributes are assumed to be time-varying and indexed by snapshots. When multiple components are added, all non-scalar attributes are assumed to be static and indexed by names. If you want to add time-varying attributes to multiple components, you can pass a 2D array/ DataFrame where the first dimension is snapshots and the second dimension is names.
Any attributes which are not specified will be given the default value from Components.
- Parameters:
class_name (str) – Component class name in (“Bus”, “Generator”, “Load”, “StorageUnit”, “Store”, “ShuntImpedance”, “Line”, “Transformer”, “Link”).
name (str or int or list of str or list of int) – Component name(s)
suffix (str, default "") – All components are named after name with this added suffix.
overwrite (bool, default False) – If True, existing components with the same names as in name will be overwritten. Otherwise only new components will be added and others will be ignored.
kwargs (Any) – Component attributes, e.g. x=[0.1, 0.2], can be list, pandas.Series of pandas.DataFrame for time-varying
- Returns:
new_names – Names of new components (including suffix)
- Return type:
pandas.index
Examples
Add a single component:
>>> n.add("Bus", "my_bus_0") >>> n.add("Bus", "my_bus_1", v_nom=380) >>> n.add("Line", "my_line_name", bus0="my_bus_0", bus1="my_bus_1", length=34, r=2, x=4)
Add multiple components with static attributes:
>>> n.add("Load", ["load 1", "load 2"], ... bus=["1", "2"], ... p_set=np.random.rand(len(n.snapshots), 2))
Add multiple components with time-varying attributes:
>>> import pandas as pd, numpy as np >>> buses = range(13) >>> snapshots = range(7) >>> n = pypsa.Network() >>> n.set_snapshots(snapshots) >>> n.add("Bus", buses) >>> # add load as numpy array >>> n.add("Load", ... n.buses.index + " load", ... bus=buses, ... p_set=np.random.rand(len(snapshots), len(buses))) >>> # add wind availability as pandas DataFrame >>> wind = pd.DataFrame(np.random.rand(len(snapshots), len(buses)), ... index=n.snapshots, ... columns=buses) >>> #use a suffix to avoid boilerplate to rename everything >>> n.add("Generator", ... buses, ... suffix=' wind', ... bus=buses, ... p_nom_extendable=True, ... capital_cost=1e5, ... p_max_pu=wind)