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. A single value sequence is treated as scalar and broadcasted to all components. It is recommended to explicitly pass a scalar instead. 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
>>> import pypsa >>> n = pypsa.examples.ac_dc_meshed()
Add a single component:
>>> n.add("Bus", "my_bus_0") Index(['my_bus_0'], dtype='object') >>> n.add("Bus", "my_bus_1", v_nom=380) Index(['my_bus_1'], dtype='object') >>> n.add("Line", "my_line_name", bus0="my_bus_0", bus1="my_bus_1", length=34, r=2, x=4) Index(['my_line_name'], dtype='object')
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)) Index(['load 1', 'load 2'], dtype='object')
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) Index(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'], dtype='object') >>> # add load as numpy array >>> n.add("Load", ... n.buses.index + " load", ... bus=buses, ... p_set=np.random.rand(len(snapshots), len(buses))) Index(['0 load', '1 load', '2 load', '3 load', '4 load', '5 load', '6 load', '7 load', '8 load', '9 load', '10 load', '11 load', '12 load'], dtype='object', name='Bus') >>> # 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) Index(['0 wind', '1 wind', '2 wind', '3 wind', '4 wind', '5 wind', '6 wind', '7 wind', '8 wind', '9 wind', '10 wind', '11 wind', '12 wind'], dtype='object')