tespy.networks package

tespy.networks.network module

Module for tespy network class.

The network is the container for every TESPy simulation. The network class automatically creates the system of equations describing topology and parametrization of a specific model and solves it.

This file is part of project TESPy (github.com/oemof/tespy). It’s copyrighted by the contributors recorded in the version control history of the file, available from its original location tespy/networks/networks.py

SPDX-License-Identifier: MIT

class tespy.networks.network.Network(iterinfo=True, units=None, m_range=None, p_range=None, h_range=None, **kwargs)[source]

Bases: object

Class component is the base class of all TESPy components.

Parameters:
  • iterinfo (boolean) – Print convergence progress to console.

  • h_range (list) – List with minimum and maximum values for enthalpy value range.

  • m_range (list) – List with minimum and maximum values for mass flow value range.

  • p_range (list) – List with minimum and maximum values for pressure value range.

Note

Units are specified via the Network.units.set_defaults interface. The specification is optional and will use SI units by default.

Range specification is optional, too. The value range is used to stabilize the newton algorithm. For more information see the “getting started” section in the online-documentation.

Example

Basic example for a setting up a tespy.networks.network.Network object.

Standard value for iterinfo is True. This will print out convergence progress to the console. You can stop the printouts by setting this property to False.

>>> from tespy.networks import Network
>>> mynetwork = Network()
>>> mynetwork.units.set_defaults(**{
...     "pressure": "bar", "pressure_difference": "bar",
...     "temperature": "degC"
... })
>>> mynetwork.p_range = [1, 10]
>>> type(mynetwork)
<class 'tespy.networks.network.Network'>
>>> mynetwork.iterinfo = False
>>> mynetwork.iterinfo
False
>>> mynetwork.iterinfo = True
>>> mynetwork.iterinfo
True

A simple network consisting of a source, a pipe and a sink. This example shows how the printout parameter can be used. We specify printout=False for both connections, the pipe as well as the power connection. Therefore the .print_results() method should not print any results.

>>> from tespy.networks import Network
>>> from tespy.components import Source, Sink, Pipe, HeatSink
>>> from tespy.connections import Connection, HeatConnection
>>> nw = Network()
>>> nw.units.set_defaults(**{
...     "pressure": "bar", "pressure_difference": "bar",
...     "temperature": "degC"
... })
>>> so = Source('source')
>>> si = Sink('sink')
>>> p = Pipe('pipe', Q=0, pr=0.95, printout=False)
>>> h = HeatSink('heat to ambient')
>>> a = Connection(so, 'out1', p, 'in1')
>>> b = Connection(p, 'out1', si, 'in1')
>>> nw.add_conns(a, b)
>>> a.set_attr(fluid={'CH4': 1}, T=30, p=10, m=10, printout=False)
>>> b.set_attr(printout=False)
>>> e = HeatConnection(p, 'heat', h, 'heat', printout=False)
>>> nw.add_conns(e)
>>> nw.iterinfo = False
>>> nw.solve('design')
>>> nw.print_results()
add_conns(*args)[source]

Add one or more connections to the network.

Parameters:

c (tespy.connections.connection.Connection) – The connection to be added to the network, connections objects ci add_conns(c1, c2, c3, ...).

add_subsystems(*args)[source]

Add one or more subsystems to the network.

Parameters:

c (tespy.components.subsystem.Subsystem) – The subsystem to be added to the network, subsystem objects si network.add_subsystems(s1, s2, s3, ...).

add_ude(*args)[source]

Add a user defined function to the network.

Parameters:

c (tespy.tools.helpers.UserDefinedEquation) – The objects to be added to the network, UserDefinedEquation objects ci add_ude(c1, c2, c3, ...).

assert_convergence()[source]

Check convergence status of a simulation.

check_topology()[source]

Check if components are connected properly within the network.

property converged
del_conns(*args)[source]

Remove one or more connections from the network.

Parameters:

c (tespy.connections.connection.Connection) – The connection to be removed from the network, connections objects ci del_conns(c1, c2, c3, ...).

del_subsystems(*args)[source]

Delete one or more subsystems from the network.

Parameters:

c (tespy.components.subsystem.Subsystem) – The subsystem to be deleted from the network, subsystem objects si network.del_subsystems(s1, s2, s3, ...).

del_ude(*args)[source]

Remove a user defined function from the network.

Parameters:

c (tespy.tools.helpers.UserDefinedEquation) – The objects to be deleted from the network, UserDefinedEquation objects ci del_ude(c1, c2, c3, ...).

export(json_file_path=None)[source]

Export the parametrization and structure of the Network instance

Parameters:

json_file_path (str, optional) – Path for exporting to filesystem. If path is None, the data are only returned and not written to the filesystem, by default None.

Returns:

dict – Parametrization and structure of the Network instance.

classmethod from_dict(network_data)[source]
classmethod from_json(json_file_path)[source]

Load a network from a base path.

Parameters:

path (str) – The path to the network data.

Returns:

nw (tespy.networks.network.Network) – TESPy networks object.

Note

If you export the network structure of an existing TESPy network, it will be saved to the path you specified. The structure of the saved data in that path is the structure you need to provide in the path for loading the network.

The structure of the path must be as follows:

  • Folder: path (e.g. ‘mynetwork’)

  • Component.json

  • Connection.json

  • Network.json

Example

Create a network and export it. This is followed by loading the network from the exported json file. All network information stored will be passed to a new network object. Components and connections will be accessible by label. The following example setup is simple gas turbine setup with compressor, combustion chamber and turbine. The fuel is fed from a pipeline and throttled to the required pressure while keeping the temperature at a constant value.

>>> from tespy.components import (
...     Sink, Source, CombustionChamber, TurboCompressor, Turbine,
...     SimpleHeatExchanger, PowerBus, PowerSink, Generator
... )
>>> from tespy.connections import Connection, Ref, PowerConnection
>>> from tespy.networks import Network
>>> import os
>>> nw = Network()
>>> nw.iterinfo = False
>>> nw.units.set_defaults(**{
...     "pressure": "bar", "pressure_difference": "bar",
...     "temperature": "degC", "enthalpy": "kJ/kg",
...     "power": "MW"
... })
>>> air = Source('air')
>>> f = Source('fuel')
>>> compressor = TurboCompressor('compressor')
>>> combustion = CombustionChamber('combustion')
>>> turbine = Turbine('turbine')
>>> preheater = SimpleHeatExchanger('fuel preheater')
>>> si = Sink('sink')
>>> shaft = PowerBus('shaft', num_in=1, num_out=2)
>>> generator = Generator('generator')
>>> grid = PowerSink('grid')
>>> c1 = Connection(air, 'out1', compressor, 'in1', label='c01')
>>> c2 = Connection(compressor, 'out1', combustion, 'in1', label='c02')
>>> c11 = Connection(f, 'out1', preheater, 'in1', label='c11')
>>> c12 = Connection(preheater, 'out1', combustion, 'in2', label='c12')
>>> c3 = Connection(combustion, 'out1', turbine, 'in1', label='c03')
>>> c4 = Connection(turbine, 'out1', si, 'in1', label='c04')
>>> nw.add_conns(c1, c2, c11, c12, c3, c4)
>>> e1 = PowerConnection(turbine, 'power', shaft, 'power_in1', label='e1')
>>> e2 = PowerConnection(shaft, 'power_out1', compressor, 'power', label='e2')
>>> e3 = PowerConnection(shaft, 'power_out2', generator, 'power_in', label='e3')
>>> e4 = PowerConnection(generator, 'power_out', grid, 'power', label='e4')
>>> nw.add_conns(e1, e2, e3, e4)

Specify component and connection properties. The intlet pressure at the compressor and the outlet pressure after the turbine are identical. For the compressor, the pressure ratio and isentropic efficiency are design parameters. A compressor map (efficiency vs. mass flow and pressure rise vs. mass flow) is selected for the compressor. Fuel is Methane.

>>> compressor.set_attr(
...     pr=10, eta_s=0.88, design=['eta_s', 'pr'],
...     offdesign=['char_map_eta_s', 'char_map_pr']
... )
>>> turbine.set_attr(
...     eta_s=0.9, design=['eta_s'],
...     offdesign=['eta_s_char', 'cone']
... )
>>> combustion.set_attr(lamb=2)
>>> c1.set_attr(
...     fluid={'N2': 0.7556, 'O2': 0.2315, 'Ar': 0.0129}, T=25, p=1
... )
>>> c11.set_attr(fluid={'CH4': 0.96, 'CO2': 0.04}, T=25, p=40)
>>> c12.set_attr(T=25)
>>> c4.set_attr(p=Ref(c1, 1, 0))
>>> generator.set_attr(eta=1)

For a stable start, we specify the fresh air mass flow.

>>> c1.set_attr(m=3)
>>> nw.solve('design')
>>> nw.assert_convergence()

The total power output is set to 1 MW, electrical or mechanical efficiencies are not considered in this example. See tespy.components.power.motor.Motor and tespy.components.power.generator.Generator for modelling conversion efficiencies between mechanical and electrical power.

>>> combustion.set_attr(lamb=None)
>>> c3.set_attr(T=1100)
>>> c1.set_attr(m=None)
>>> e4.set_attr(E=1)
>>> nw.solve('design')
>>> nw.assert_convergence()
>>> design_state = nw.save(as_dict=True)
>>> _ = nw.export('exported_nwk.json')
>>> mass_flow = round(nw.get_conn('c01').m.val_SI, 1)
>>> compressor.set_attr(igva='var')
>>> nw.solve('offdesign', design_path=design_state)
>>> round(turbine.eta_s.val, 1)
0.9
>>> e4.set_attr(E=0.75)
>>> nw.solve('offdesign', design_path=design_state)
>>> nw.assert_convergence()
>>> eta_s_t = round(turbine.eta_s.val, 3)
>>> igva = round(compressor.igva.val, 3)
>>> eta_s_t
0.898
>>> igva
20.138

The designed network is exported to the path ‘exported_nwk’. Now import the network and recalculate. Check if the results match with the previous calculation in design and offdesign case.

>>> imported_nwk = Network.from_json('exported_nwk.json')
>>> imported_nwk.iterinfo = False
>>> imported_nwk.solve('design')
>>> imported_nwk.lin_dep
False
>>> round(imported_nwk.get_conn('c01').m.val_SI, 1) == mass_flow
True
>>> round(imported_nwk.get_comp('turbine').eta_s.val, 3)
0.9
>>> imported_nwk.get_comp('compressor').set_attr(igva='var')
>>> imported_nwk.solve('offdesign', design_path=design_state)
>>> round(imported_nwk.get_comp('turbine').eta_s.val, 3)
0.9
>>> imported_nwk.get_conn('e4').set_attr(E=0.75)
>>> imported_nwk.solve('offdesign', design_path=design_state)
>>> round(imported_nwk.get_comp('turbine').eta_s.val, 3) == eta_s_t
True
>>> round(imported_nwk.get_comp('compressor').igva.val, 3) == igva
True
>>> os.remove('exported_nwk.json')
get_attr(key)[source]

Get the value of a network attribute.

Parameters:

key (str) – The attribute you want to retrieve.

Returns:

out – Specified attribute.

get_comp(label)[source]

Get Component via label.

Parameters:

label (str) – Label of the Component object.

Returns:

c (tespy.components.component.Component) – Component object with specified label, None if no Component of the network has this label.

get_conn(label)[source]

Get Connection via label.

Parameters:

label (str) – Label of the Connection object.

Returns:

c (tespy.connections.connection.Connection) – Connection object with specified label, None if no Connection of the network has this label.

get_equations() dict[source]

Get the actual equations after presolving the problem

Returns:

dict – Lookup with equation number as index and tuple of label and parameter defining the equation. In case one parameter defines multiple equations, the same equation is repeated.

get_equations_with_dependents() dict[source]

Get the equations together with the variables they depend on.

Returns:

dict – Lookup with equation (component, (parameter_label, number)) and the variables it depends on as a list (variable number, variable type)

get_linear_dependent_variables() list[source]

Get a list with sublists containing linear dependent variables

Returns:

list – List of lists of linear dependent variables

get_linear_dependents_by_object(obj, prop) list[source]

Get the list of linear dependent variables for a specified variable

Parameters:
  • obj (object) – Parent object holding a variable

  • prop (str) – Name of the variable (e.g. ‘m’ or ‘h’)

Returns:

list – list of linear dependent variables

Raises:
  • KeyError – In case the object does not have any variables

  • KeyError – In case the specified property is not a variable

get_presolved_equations() list[source]

Get the list of equations, that has been presolved with their respective parent object

Returns:

list – list of presolved equations

get_presolved_variables() list[source]

Get the list of presolved variables with their respective parent object and property.

Returns:

list – list of presolved variables

get_sorted_residual_index() list[int][source]

Get the sorted array of residual indices.

Returns:

list[int] – List of variable numbers, the index values.

get_subsystem(label)[source]

Get Subsystem via label.

Parameters:

label (str) – Label of the Subsystem object.

Returns:

tespy.components.subsystem.Subsystem – Subsystem objectt with specified label, None if no Subsystem of the network has this label.

get_ude(label)[source]

Get UserDefinedEquation via label.

Parameters:

label (str) – Label of the UserDefinedEquation object.

Returns:

c (tespy.tools.helpers.UserDefinedEquation) – UserDefinedEquation object with specified label, None if no UserDefinedEquation of the network has this label.

get_variables() dict[source]

Get all variables of the presolved problem with their respective represented original variables.

Returns:

dict – variable number and property with the list of represented variables

get_variables_before_presolve() list[source]

Get the list of variables before presolving.

Returns:

list – list of original variables

property h_range
property iterinfo
property m_range
property p_range
print_equations()[source]

Print a formatted table of equations after presolving.

print_equations_with_dependents()[source]

Print a formatted table of equations and the variables they depend on.

print_incidence_matrix()[source]

Print the incidence matrix with equation rows and variable columns.

print_presolved_equations()[source]

Print a formatted table of presolved equations.

print_presolved_variables()[source]

Print a formatted table of presolved variables.

print_residuals()[source]

Print a formatted table of equation residuals, sorted by magnitude.

print_results(colored=True, colors=None, print_results=True, subsystem=None)[source]

Print the calculations results to prompt.

print_variables()[source]

Print a formatted table of variables after presolving.

print_variables_before_presolve()[source]

Print a formatted table of all variables before presolving.

save(json_file_path: str | Path | None = None, as_dict: bool = False) None | dict | str[source]

Dump the results to a json style output.

Parameters:
  • json_file_path (str | Path | None) – Filename to dump results into. If None, the state is returned in-memory (as dict when as_dict=True, otherwise as JSON string).

  • as_dict (bool) – If True and json_file_path is None, return the state as a dict that can be passed directly as design_path or init_path in a subsequent solve() call. Default False; the False behaviour (returning a JSON string) is deprecated and will be removed in a future release.

Returns:

  • None – If a file path is provided, results are saved to file.

  • dict – If json_file_path is None and as_dict=True.

  • str – If json_file_path is None and as_dict=False (deprecated).

save_csv(folder_path)[source]

Export the results in multiple csv files in a folder structure

  • Connection.csv

  • Component/ - Compressor.csv - ….

Parameters:

folder_path (str) – Path to dump results to

set_attr(**kwargs)[source]

Set, resets or unsets attributes of a network.

Parameters:
  • iterinfo (boolean) – Print convergence progress to console.

  • h_range (list) – List with minimum and maximum values for enthalpy value range.

  • m_range (list) – List with minimum and maximum values for mass flow value range.

  • p_range (list) – List with minimum and maximum values for pressure value range.

solve(mode, init_path=None, design_path=None, max_iter=50, min_iter=4, init_only=False, init_previous=True, use_cuda=False, print_results=True, robust_relax=False, skip_postprocess=False)[source]

Solve the network.

  • Check network consistency.

  • Initialise calculation and preprocessing.

  • Perform actual calculation.

  • Postprocessing.

It is possible to check programmatically, if a network was solved successfully with the .converged attribute.

Parameters:
  • mode (str) – Choose from ‘design’ and ‘offdesign’.

  • init_path (str | Path | dict) – Path to a previously saved network state (e.g. nw.save('myplant/test.json')), or the dict returned by nw.save(as_dict=True).

  • design_path (str | Path | dict) – Path to the saved design-case state (e.g. nw.save('myplant/test.json')), or the dict returned by nw.save(as_dict=True).

  • max_iter (int) – Maximum number of iterations before calculation stops, default: 50.

  • min_iter (int) – Minimum number of iterations before calculation stops, default: 4.

  • init_only (boolean) – Perform initialisation only, default: False.

  • init_previous (boolean) – Initialise the calculation with values from the previous calculation, default: True.

  • use_cuda (boolean) – Use cuda instead of numpy for matrix inversion, default: False.

Note

For more information on the solution process have a look at the online documentation at tespy.readthedocs.io in the section “TESPy modules”.

property units