etrago.tools package

Submodules

etrago.tools.io module

io.py

Input/output operations between powerflow schema in the oedb and PyPSA. Additionally oedb wrapper classes to instantiate PyPSA network objects.

etrago.tools.io.packagename

Package containing orm class definitions

Type:str
etrago.tools.io.temp_ormclass

Orm class name of table with temporal resolution

Type:str
etrago.tools.io.carr_ormclass

Orm class name of table with carrier id to carrier name datasets

Type:str

Notes

A configuration file connecting the chosen optimization method with components to be queried is needed for NetworkScenario class.

etrago.tools.io.load_config_file(filename='config.json')[source]
class etrago.tools.io.ScenarioBase(session, method='lopf', configpath='config.json', version=None, prefix='EgoGridPfHv')[source]

Bases: object

Base class to address the dynamic provision of orm classes representing powerflow components from egoio based on a configuration file

Parameters:
  • config (OrderedDict) – Dictionary with orm class names that should be accessable via _mapped.
  • session (sqla.orm.session.Session) – Handles conversations with the database.
  • version (str) – Version number of data version control in grid schema of the oedb.
  • prefix (str) – Common prefix of component orm classnames in egoio.

Methods

map_ormclass(name)[source]

Populate _mapped attribute with orm class

Parameters:name (str) – Component part of orm class name. Concatenated with _prefix.
class etrago.tools.io.NetworkScenario(session, scn_name='Status Quo', method='lopf', start_snapshot=1, end_snapshot=20, temp_id=1, **kwargs)[source]

Bases: etrago.tools.io.ScenarioBase

Adapter class between oedb powerflow data and PyPSA. Provides the method build_network to generate a pypsa.Network.

Parameters:
  • scn_name (str) – Scenario name.
  • method (str) – Objective function.
  • start_snapshot (int) – First snapshot or timestep.
  • end_snapshot (int) – Last timestep.
  • temp_id (int) – Nummer of temporal resolution.

Methods

configure_timeindex()[source]

Construct a DateTimeIndex with the queried temporal resolution, start- and end_snapshot.

id_to_source()[source]
fetch_by_relname(name)[source]

Construct DataFrame with component data from filtered table data.

Parameters:name (str) – Component name.
Returns:Component data.
Return type:pd.DataFrame
series_fetch_by_relname(name, column)[source]

Construct DataFrame with component timeseries data from filtered table data.

Parameters:
  • name (str) – Component name.
  • column (str) – Component field with timevarying data.
Returns:

Component data.

Return type:

pd.DataFrame

build_network(network=None, *args, **kwargs)[source]

Core method to construct PyPSA Network object.

etrago.tools.io.clear_results_db(session)[source]

Used to clear the result tables in the OEDB. Caution! This deletes EVERY RESULT SET!

etrago.tools.io.results_to_oedb(session, network, args, grid='hv', safe_results=False)[source]

Return results obtained from PyPSA to oedb

Parameters:
  • session
  • network (PyPSA network container) – Holds topology of grid including results from powerflow analysis
  • args (dict) – Settings from appl.py
  • grid (str) – Choose voltage-level, currently only ‘hv’ implemented
  • safe_results (boolean) – If it is set to ‘True’ the result set will be saved to the versioned grid schema eventually apart from being saved to the model_draft by a SQL-script. ONLY set to True if you know what you are doing.
etrago.tools.io.run_sql_script(conn, scriptname='results_md2grid.sql')[source]

This function runs .sql scripts in the folder ‘sql_scripts’

etrago.tools.io.extension(network, session, version, scn_extension, start_snapshot, end_snapshot, **kwargs)[source]

Function that adds an additional network to the existing network container. The new network can include every PyPSA-component (e.g. buses, lines, links). To connect it to the existing network, transformers are needed.

All components and its timeseries of the additional scenario need to be inserted in the fitting ‘model_draft.ego_grid_pf_hv_extension_’ table. The scn_name in the tables have to be labled with ‘extension_’ + scn_name (e.g. ‘extension_nep2035’).

Until now, the tables include three additional scenarios: ‘nep2035_confirmed’: all new lines and needed transformers planed in the ‘Netzentwicklungsplan 2035’ (NEP2035) that have been confirmed by the Bundesnetzagentur (BNetzA)

‘nep2035_b2’: all new lines and needed transformers planned in the NEP 2035 in the scenario 2035 B2

‘BE_NO_NEP 2035’: DC-lines and transformers to connect the upcomming electrical-neighbours Belgium and Norway

Generation, loads and its timeseries in Belgium and Norway for scenario ‘NEP 2035’

network : The existing network container (e.g. scenario ‘NEP 2035’) session : session-data overlay_scn_name : Name of the additional scenario (WITHOUT ‘extension_’) start_snapshot, end_snapshot: Simulation time
Returns:network
Return type:Network container including existing and additional network
etrago.tools.io.decommissioning(network, session, args, **kwargs)[source]

Function that removes components in a decommissioning-scenario from the existing network container. Currently, only lines can be decommissioned.

All components of the decommissioning scenario need to be inserted in the fitting ‘model_draft.ego_grid_pf_hv_extension_’ table. The scn_name in the tables have to be labled with ‘decommissioning_’ + scn_name (e.g. ‘decommissioning_nep2035’).

Parameters:
  • network (The existing network container (e.g. scenario 'NEP 2035')) –
  • session (session-data) –
  • overlay_scn_name (Name of the decommissioning scenario) –
Returns:

network

Return type:

Network container including decommissioning

etrago.tools.io.distance(x0, x1, y0, y1)[source]

Function that calculates the square of the distance between two points.

Parameters:
  • x0 (x - coordinate of point 0) –
  • x1 (x - coordinate of point 1) –
  • y0 (y - coordinate of point 0) –
  • y1 (y - coordinate of point 1) –
Returns:

  • distance (float)
  • square of distance

etrago.tools.io.calc_nearest_point(bus1, network)[source]

Function that finds the geographical nearest point in a network from a given bus.

Parameters:
  • bus1 (float) –
  • of bus (id) –
  • network (Pypsa network container) –
  • including the comparable buses (network) –
Returns:

  • bus0 (float)
  • bus_id of nearest point

etrago.tools.extendable module

Extendable.py defines function to set PyPSA-components extendable.

etrago.tools.extendable.extendable(network, args, line_max)[source]

Function that sets selected components extendable

‘network’ for all lines, links and transformers ‘german_network’ for all lines, links and transformers located in Germany ‘foreign_network’ for all foreign lines, links and transformers ‘transformers’ for all transformers ‘storages’ for extendable storages ‘overlay_network’ for lines, links and trafos in extension scenerio(s)

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict) – Arguments set in appl.py
Returns:

network – Overall container of PyPSA

Return type:

:class:`pypsa.Network

etrago.tools.extendable.extension_preselection(network, args, method, days=3)[source]

Function that preselects lines which are extendend in snapshots leading to overloading to reduce nubmer of extension variables.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict) – Arguments set in appl.py
  • method (str) – Choose method of selection: ‘extreme_situations’ for remarkable timsteps (e.g. minimal resiudual load) ‘snapshot_clustering’ for snapshot clustering with number of days
  • days (int) – Number of clustered days, only used when method = ‘snapshot_clustering’
Returns:

network – Overall container of PyPSA

Return type:

:class:`pypsa.Network

etrago.tools.extendable.print_expansion_costs(network, args)[source]

etrago.tools.plot module

etrago.tools.snapshot_clustering module

etrago.tools.utilities module

Utilities.py includes a wide range of useful functions.

etrago.tools.utilities.buses_of_vlvl(network, voltage_level)[source]

Get bus-ids of given voltage level(s).

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • voltage_level (list) –
Returns:

List containing bus-ids.

Return type:

list

etrago.tools.utilities.buses_grid_linked(network, voltage_level)[source]

Get bus-ids of a given voltage level connected to the grid.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • voltage_level (list) –
Returns:

List containing bus-ids.

Return type:

list

etrago.tools.utilities.geolocation_buses(network, session)[source]

If geopandas is installed: Use Geometries of buses x/y(lon/lat) and Polygons of Countries from RenpassGisParameterRegion in order to locate the buses

Else: Use coordinats of buses to locate foreign buses, which is less accurate.

Parameters:
  • network_etrago (: class: etrago.tools.io.NetworkScenario) – eTraGo network object compiled by: meth: etrago.appl.etrago
  • session (: sqlalchemy: sqlalchemy.orm.session.Session < orm/session_basics.html >) – SQLAlchemy session to the OEDB
etrago.tools.utilities.buses_by_country(network)[source]

Find buses of foreign countries using coordinates and return them as Pandas Series

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
Returns:foreign_buses
Return type:Series containing buses by country
etrago.tools.utilities.clip_foreign(network)[source]

Delete all components and timelines located outside of Germany. Add transborder flows divided by country of origin as network.foreign_trade.

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
Returns:network – Overall container of PyPSA
Return type::class:`pypsa.Network

Change transmission technology of foreign lines from AC to DC (links).

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
Returns:network – Overall container of PyPSA
Return type::class:`pypsa.Network
etrago.tools.utilities.set_q_foreign_loads(network, cos_phi=1)[source]

Set reative power timeseries of loads in neighbouring countries

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • cos_phi (float) – Choose ration of active and reactive power of foreign loads
Returns:

network – Overall container of PyPSA

Return type:

:class:`pypsa.Network

etrago.tools.utilities.connected_grid_lines(network, busids)[source]

Get grid lines connected to given buses.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • busids (list) – List containing bus-ids.
Returns:

PyPSA lines.

Return type:

class:`pandas.DataFrame

etrago.tools.utilities.connected_transformer(network, busids)[source]

Get transformer connected to given buses.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • busids (list) – List containing bus-ids.
Returns:

PyPSA transformer.

Return type:

class:`pandas.DataFrame

etrago.tools.utilities.load_shedding(network, **kwargs)[source]

Implement load shedding in existing network to identify feasibility problems

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • marginal_cost (int) – Marginal costs for load shedding
  • p_nom (int) – Installed capacity of load shedding generator
etrago.tools.utilities.data_manipulation_sh(network)[source]

Adds missing components to run calculations with SH scenarios.

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
etrago.tools.utilities.results_to_csv(network, args, path)[source]

Function the writes the calaculation results in csv-files in the desired directory.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict) – Contains calculation settings of appl.py
  • path (str) – Choose path for csv-files
etrago.tools.utilities.parallelisation(network, args, group_size, extra_functionality=None)[source]

Function that splits problem in selected number of snapshot groups and runs optimization successive for each group.

Not useful for calculations with storage untis or extension.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict) – Contains calculation settings of appl.py
Returns:

network – Overall container of PyPSA

Return type:

:class:`pypsa.Network

etrago.tools.utilities.set_slack(network)[source]

Function that chosses the bus with the maximum installed power as slack

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
Returns:network – Overall container of PyPSA
Return type::class:`pypsa.Network
etrago.tools.utilities.pf_post_lopf(network, args, add_foreign_lopf, q_allocation, calc_losses)[source]

Function that prepares and runs non-linar load flow using PyPSA pf.

If network has been extendable, a second lopf with reactances adapted to new s_nom is needed.

If crossborder lines are DC-links, pf is only applied on german network. Crossborder flows are still considerd due to the active behavior of links. To return a network containing the whole grid, the optimised solution of the foreign components can be added afterwards.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict) – Contains calculation settings of appl.py
  • add_foreign_lopf (boolean) – Choose if foreign results of lopf should be added to the network when foreign lines are DC
  • q_allocation (str) – Choose allocation of reactive power. Possible settings are listed in distribute_q function.
  • calc_losses (bolean) – Choose if line losses will be calculated.
etrago.tools.utilities.distribute_q(network, allocation='p_nom')[source]

Function that distributes reactive power at bus to all installed generators and storages.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • allocation (str) – Choose key to distribute reactive power: ‘p_nom’ to dirstribute via p_nom ‘p’ to distribute via p_set
etrago.tools.utilities.calc_line_losses(network)[source]

Calculate losses per line with PF result data

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • s0 (series) – apparent power of line
  • i0 (series) – current of line
  • -------
etrago.tools.utilities.loading_minimization(network, snapshots)[source]
etrago.tools.utilities.group_parallel_lines(network)[source]
etrago.tools.utilities.set_line_costs(network, args, cost110=230, cost220=290, cost380=85, costDC=375)[source]

Set capital costs for extendable lines in respect to PyPSA [€/MVA]

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict containing settings from appl.py) –
  • cost110 (capital costs per km for 110kV lines and cables) – default: 230€/MVA/km, source: costs for extra circuit in dena Verteilnetzstudie, p. 146)
  • cost220 (capital costs per km for 220kV lines and cables) – default: 280€/MVA/km, source: costs for extra circuit in NEP 2025, capactity from most used 220 kV lines in model
  • cost380 (capital costs per km for 380kV lines and cables) – default: 85€/MVA/km, source: costs for extra circuit in NEP 2025, capactity from most used 380 kV lines in NEP
  • costDC (capital costs per km for DC-lines) – default: 375€/MVA/km, source: costs for DC transmission line in NEP 2035
  • -------
etrago.tools.utilities.set_trafo_costs(network, args, cost110_220=7500, cost110_380=17333, cost220_380=14166)[source]

Set capital costs for extendable transformers in respect to PyPSA [€/MVA]

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • cost110_220 (capital costs for 110/220kV transformer) – default: 7500€/MVA, source: costs for extra trafo in dena Verteilnetzstudie, p. 146; S of trafo used in osmTGmod
  • cost110_380 (capital costs for 110/380kV transformer) – default: 17333€/MVA, source: NEP 2025
  • cost220_380 (capital costs for 220/380kV transformer) – default: 14166€/MVA, source: NEP 2025
etrago.tools.utilities.add_missing_components(network)[source]

Add missing transformer at Heizkraftwerk Nord in Munich and missing transformer in Stuttgart

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
Returns:network – Overall container of PyPSA
Return type::class:`pypsa.Network
etrago.tools.utilities.convert_capital_costs(network, start_snapshot, end_snapshot, p=0.05, T=40)[source]

Convert capital_costs to fit to pypsa and caluculated time

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • p (interest rate, default 0.05) –
  • T (number of periods, default 40 years (source: StromNEV Anlage 1)) –
  • -------
etrago.tools.utilities.find_snapshots(network, carrier, maximum=True, minimum=True, n=3)[source]

Function that returns snapshots with maximum and/or minimum feed-in of selected carrier.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • carrier (str) – Selected carrier of generators
  • maximum (bool) – Choose if timestep of maximal feed-in is returned.
  • minimum (bool) – Choose if timestep of minimal feed-in is returned.
  • n (int) – Number of maximal/minimal snapshots
Returns:

calc_snapshots – List containing snapshots

Return type:

‘pandas.core.indexes.datetimes.DatetimeIndex’

etrago.tools.utilities.ramp_limits(network)[source]

Add ramping constraints to thermal power plants.

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
etrago.tools.utilities.get_args_setting(args, jsonpath='scenario_setting.json')[source]

Get and open json file with scenaio settings of eTraGo args. The settings incluedes all eTraGo specific settings of arguments and parameters for a reproducible calculation.

Parameters:json_file (str) – Default: scenario_setting.json Name of scenario setting json file
Returns:args – Dictionary of json file
Return type:dict
etrago.tools.utilities.set_random_noise(network, seed, sigma=0.01)[source]

Sets random noise to marginal cost of each generator.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • seed (int) – seed number, needed to reproduce results
  • sigma (float) – Default: 0.01 standard deviation, small values reduce impact on dispatch but might lead to numerical instability
etrago.tools.utilities.set_line_country_tags(network)[source]

Set country tag for AC- and DC-lines.

Parameters:network (:class:`pypsa.Network) – Overall container of PyPSA
etrago.tools.utilities.crossborder_capacity(network, method, capacity_factor)[source]

Adjust interconnector capacties.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • method (string) – Method of correction. Options are ‘ntc_acer’ and ‘thermal_acer’. ‘ntc_acer’ corrects all capacities according to values published by the ACER in 2016. ‘thermal_acer’ corrects certain capacities where our dataset most likely overestimates the thermal capacity.
  • capacity_factor (float) – branch capacity factor. Reduction by branch-capacity factor is applied afterwards and shouln’t effect ntc-values, which already include (n-1)-security. To exclude the ntc-capacities from the capacity factor, the crossborder-capacities are diveded by the factor in this function. For thermal-acer this is excluded by setting branch capacity factors to one.
etrago.tools.utilities.set_branch_capacity(network, args)[source]

Set branch capacity factor of lines and transformers, different factors for HV (110kV) and eHV (220kV, 380kV).

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict) – Settings in appl.py
etrago.tools.utilities.update_electrical_parameters(network, l_snom_pre, t_snom_pre)[source]

Update electrical parameters of active branch components considering s_nom of previous iteration

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • l_snom_pre (pandas.Series) – s_nom of ac-lines in previous iteration
  • t_snom_pre (pandas.Series) – s_nom of transformers in previous iteration
etrago.tools.utilities.iterate_lopf(network, args, extra_functionality, method={'n_iter': 4}, delta_s_max=0.1)[source]

Run optimization of lopf. If network extension is included, the specified number of iterations is calculated to consider reactance changes.

Parameters:
  • network (:class:`pypsa.Network) – Overall container of PyPSA
  • args (dict) – Settings in appl.py
  • extra_functionality (str) – Define extra constranits.
  • method (dict) – Choose ‘n_iter’ and integer for fixed number of iterations or ‘threshold’ and derivation of objective in percent for variable number of iteration until the threshold of the objective function is reached
  • delta_s_max (float) – Increase of maximal extension of each line in p.u. Currently only working with method n_iter

Module contents