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(**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", "temperature": "degC"
... })
>>> mynetwork.set_attr(p_range=[1, 10])
>>> type(mynetwork)
<class 'tespy.networks.network.Network'>
>>> mynetwork.set_attr(iterinfo=False)
>>> mynetwork.iterinfo
False
>>> mynetwork.set_attr(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 heat bus. Therefore the .print_results() method should not print any results.

>>> from tespy.networks import Network
>>> from tespy.components import Source, Sink, Pipe, PowerSink
>>> from tespy.connections import Connection, PowerConnection
>>> nw = Network()
>>> nw.units.set_defaults(**{
...     "pressure": "bar", "temperature": "degC"
... })
>>> so = Source('source')
>>> si = Sink('sink')
>>> p = Pipe('pipe', Q=0, pr=0.95, printout=False, power_connector_location="outlet")
>>> h = PowerSink('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 = PowerConnection(p, 'heat', h, 'power', printout=False)
>>> nw.add_conns(e)
>>> nw.set_attr(iterinfo=False)
>>> nw.solve('design')
>>> nw.print_results()
add_busses(*args)[source]

Add one or more busses to the network.

Parameters:

b (tespy.connections.bus.Bus) – The bus to be added to the network, bus objects bi add_busses(b1, b2, b3, ...).

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_busses(b)[source]

Checksthe busses to be added for type, duplicates and identical labels.

Parameters:

b (tespy.connections.bus.Bus) – The bus to be checked.

check_topology()[source]

Check if components are connected properly within the network.

check_variable_bounds()[source]
property converged
del_busses(*args)[source]

Remove one or more busses from the network.

Parameters:

b (tespy.connections.bus.Bus) – The bus to be removed from the network, bus objects bi add_busses(b1, b2, b3, ...).

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

  • Bus.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, connections and busses 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(iterinfo=False)
>>> nw.units.set_defaults(**{
...     "pressure": "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. The documentation example in class tespy.connections.bus.Bus provides more information on efficiencies of generators, for instance.

>>> 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()
>>> nw.save('design_state.json')
>>> _ = 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.json')
>>> round(turbine.eta_s.val, 1)
0.9
>>> e4.set_attr(E=0.75)
>>> nw.solve('offdesign', design_path='design_state.json')
>>> 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.set_attr(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.json')
>>> 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.json')
>>> 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')
>>> os.remove('design_state.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_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_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

init_val0(c, key)[source]

Set starting values for fluid properties.

The component classes provide generic starting values for their inlets and outlets.

Parameters:

c (tespy.connections.connection.Connection) – Connection to initialise.

iterinfo_body(print_results=True)[source]

Print convergence progress.

iterinfo_head(print_results=True)[source]

Print head of convergence progress.

iterinfo_tail(print_results=True)[source]

Print tail of convergence progress.

postprocessing()[source]

Calculate connection, bus and component parameters.

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

Print the calculations results to prompt.

process_busses()[source]

Process the bus results.

process_components()[source]

Process the component results.

process_connections()[source]

Process the Connection results.

save(json_file_path)[source]

Dump the results to a json style output.

Parameters:

json_file_path (str) – Filename to dump results into.

Note

Results will be saved to specified file path

save_csv(folder_path)[source]

Export the results in multiple csv files in a folder structure

  • Connection.csv

  • Component/ - Compressor.csv - ….

  • Bus/ - power input bus.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.

set_defaults()[source]

Set default network properties.

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)[source]

Solve the network.

  • Check network consistency.

  • Initialise calculation and preprocessing.

  • Perform actual calculation.

  • Postprocessing.

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

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

  • init_path (str) – Path to the folder, where your network was saved to, e.g. saving to nw.save('myplant/test.json') would require loading from init_path='myplant/test.json'.

  • design_path (str) – Path to the folder, where your network’s design case was saved to, e.g. saving to nw.save('myplant/test.json') would require loading from design_path='myplant/test.json'.

  • 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”.

solve_busses()[source]

Calculate the equations and the partial derivatives for the busses.

solve_control()[source]

Control iteration step of the newton algorithm.

  • Calculate the residual value for each equation

  • Calculate the jacobian matrix

  • Calculate new values for variables

  • Restrict fluid properties to value ranges

  • Check component parameters for consistency

solve_determination()[source]

Check, if the number of supplied parameters is sufficient.

solve_equations()[source]

Calculate the residual and derivatives of all equations.

solve_loop(print_results=True)[source]

Loop of the newton algorithm.

unload_variables()[source]
tespy.networks.network.v07_to_v08_export(path)[source]

Transform the v0.7 network export to a dictionary compatible to v0.8.

Parameters:

path (str) – Path to the export structure

Returns:

dict – Dictionary of the v0.8 network export

tespy.networks.network.v07_to_v08_save(path)[source]

Transform the v0.7 network save to a dictionary compatible to v0.8.

Parameters:

path (str) – Path to the save structure

Returns:

dict – Dictionary of the v0.8 network save