tespy.networks module¶
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:
h_range (list) – List with minimum and maximum values for enthalpy value range.
h_unit (str) – Specify the unit for enthalpy: ‘J / kg’, ‘kJ / kg’, ‘MJ / kg’.
iterinfo (boolean) – Print convergence progress to console.
m_range (list) – List with minimum and maximum values for mass flow value range.
m_unit (str) – Specify the unit for mass flow: ‘kg / s’, ‘t / h’.
p_range (list) – List with minimum and maximum values for pressure value range.
p_unit (str) – Specify the unit for pressure: ‘Pa’, ‘psi’, ‘bar’, ‘MPa’.
s_unit (str) – Specify the unit for specific entropy: ‘J / kgK’, ‘kJ / kgK’, ‘MJ / kgK’.
T_unit (str) – Specify the unit for temperature: ‘K’, ‘C’, ‘F’, ‘R’.
v_unit (str) – Specify the unit for volumetric flow: ‘m3 / s’, ‘m3 / h’, ‘l / s’, ‘l / h’.
vol_unit (str) – Specify the unit for specific volume: ‘m3 / kg’, ‘l / kg’.
x_unit (str) – Specify the unit for steam mass fraction: ‘-’, ‘%’.
Note
Unit specification is optional: If not specified the SI unit (first element in above lists) will be applied!
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. Specifying the fluids is mandatory! Unit systems, fluid property range and iterinfo are optional.
Standard value for iterinfo is
True
. This will print out convergence progress to the console. You can stop the printouts by setting this property toFalse
.>>> from tespy.networks import Network >>> mynetwork = Network(p_unit='bar', T_unit='C') >>> 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 >>> from tespy.connections import Connection, Bus >>> nw = Network(T_unit='C', p_unit='bar', v_unit='m3 / s') >>> so = Source('source') >>> si = Sink('sink') >>> p = Pipe('pipe', Q=0, pr=0.95, printout=False) >>> 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) >>> b = Bus('heat bus') >>> b.add_comps({'comp': p}) >>> nw.add_busses(b) >>> b.set_attr(printout=False) >>> 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_subsys(*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_subsys(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, ...)
.
- 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_connection_properties(c)[source]¶
Check for invalid fluid property values.
- Parameters:
c (tespy.connections.connection.Connection) – Connection to check fluid properties.
- 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_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_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 with the network_reader module. 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, ... Compressor, Turbine, SimpleHeatExchanger) >>> from tespy.connections import Connection, Ref, Bus >>> from tespy.networks import Network >>> import shutil >>> nw = Network(p_unit='bar', T_unit='C', h_unit='kJ / kg', iterinfo=False) >>> air = Source('air') >>> f = Source('fuel') >>> c = Compressor('compressor') >>> comb = CombustionChamber('combustion') >>> t = Turbine('turbine') >>> p = SimpleHeatExchanger('fuel preheater') >>> si = Sink('sink') >>> inc = Connection(air, 'out1', c, 'in1', label='ambient air') >>> cc = Connection(c, 'out1', comb, 'in1') >>> fp = Connection(f, 'out1', p, 'in1') >>> pc = Connection(p, 'out1', comb, 'in2') >>> ct = Connection(comb, 'out1', t, 'in1') >>> outg = Connection(t, 'out1', si, 'in1') >>> nw.add_conns(inc, cc, fp, pc, ct, outg)
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.
>>> c.set_attr(pr=10, eta_s=0.88, design=['eta_s', 'pr'], ... offdesign=['char_map_eta_s', 'char_map_pr']) >>> t.set_attr(eta_s=0.9, design=['eta_s'], ... offdesign=['eta_s_char', 'cone']) >>> comb.set_attr(lamb=2) >>> inc.set_attr(fluid={'N2': 0.7556, 'O2': 0.2315, 'Ar': 0.0129}, T=25, p=1) >>> fp.set_attr(fluid={'CH4': 0.96, 'CO2': 0.04}, T=25, p=40) >>> pc.set_attr(T=25) >>> outg.set_attr(p=Ref(inc, 1, 0)) >>> power = Bus('total power output') >>> power.add_comps({"comp": c, "base": "bus"}, {"comp": t}) >>> nw.add_busses(power)
For a stable start, we specify the fresh air mass flow.
>>> inc.set_attr(m=3) >>> nw.solve('design')
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.>>> comb.set_attr(lamb=None) >>> ct.set_attr(T=1100) >>> inc.set_attr(m=None) >>> power.set_attr(P=-1e6) >>> nw.solve('design') >>> nw.lin_dep False >>> nw.save('design_state.json') >>> _ = nw.export('exported_nwk.json') >>> mass_flow = round(nw.get_conn('ambient air').m.val_SI, 1) >>> c.set_attr(igva='var') >>> nw.solve('offdesign', design_path='design_state.json') >>> round(t.eta_s.val, 1) 0.9 >>> power.set_attr(P=-0.75e6) >>> nw.solve('offdesign', design_path='design_state.json') >>> nw.lin_dep False >>> eta_s_t = round(t.eta_s.val, 3) >>> igva = round(c.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('ambient air').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.busses['total power output'].set_attr(P=-0.75e6) >>> 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 >>> shutil.rmtree('./exported_nwk', ignore_errors=True) >>> shutil.rmtree('./design_state', ignore_errors=True)
- 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.
- init_comp_design_params(c, df)[source]¶
Write design point information to components.
- Parameters:
component (tespy.components.component.Component) – Write design point information to this component.
data (pandas.core.series.Series, pandas.core.frame.DataFrame) – Design point information.
- init_conn_design_params(c, df)[source]¶
Write design point information to connections.
- Parameters:
c (tespy.connections.connection.Connection) – Write design point information to this connection.
df (pandas.core.frame.DataFrame) – Dataframe containing design point information.
- init_conn_params_from_path(c, df)[source]¶
Write parameter information from init_path to connections.
- Parameters:
c (tespy.connections.connection.Connection) – Write init path information to this connection.
df (pandas.core.frame.DataFrame) – Dataframe containing init path information.
- init_count_connections_parameters(c)[source]¶
Count the number of parameters set on a connection.
- Parameters:
c (tespy.connections.connection.Connection) – Connection count parameters of.
- init_design()[source]¶
Initialise a design calculation.
Offdesign parameters are unset, design parameters are set. If
local_offdesign
isTrue
for connections or components, the design point information are read from the .csv-files in the respectivedesign_path
. In this case, the design values are unset, the offdesign values set.
- init_offdesign()[source]¶
Switch components and connections from design to offdesign mode.
Note
components
All parameters stated in the component’s attribute
cp.design
will be unset and all parameters stated in the component’s attributecp.offdesign
will be set instead.Additionally, all component parameters specified as variables are unset and the values from design point are set.
connections
All parameters given in the connection’s attribute
c.design
will be unset and all parameters stated in the connections’s attributecp.offdesign
will be set instead. This does also affect referenced values!
- init_offdesign_params()[source]¶
Read design point information from specified
design_path
.If a
design_path
has been specified individually for components or connections, the data will be read from the specified individual path instead.Note
The methods
tespy.networks.network.Network.init_comp_design_params()
(components) and thetespy.networks.network.Network.init_conn_design_params()
(connections) handle the parameter specification.
- init_precalc_properties(c)[source]¶
Precalculate enthalpy values for connections.
Precalculation is performed only if temperature, vapor mass fraction, temperature difference to boiling point or phase is specified.
- Parameters:
c (tespy.connections.connection.Connection) – Connection to precalculate values for.
- init_properties()[source]¶
Initialise the fluid properties on every connection of the network.
Set generic starting values for mass flow, enthalpy and pressure if not user specified, read from
init_path
or available from previous calculation.For generic starting values precalculate enthalpy value at points of given temperature, vapor mass fraction, temperature difference to boiling point or fluid state.
- 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.
- initialise()[source]¶
Initilialise the network depending on calclation mode.
Design
Generic fluid composition and fluid property initialisation.
Starting values from initialisation path if provided.
Offdesign
Check offdesign path specification.
Set component and connection design point properties.
Switch from design/offdesign parameter specification.
- print_components(c, *args)[source]¶
Get the print values for the component data.
- Parameters:
c (pandas.core.series.Series) – Series containing the component data.
param (str) – Component parameter to print.
colored (bool) – Color the printout.
coloring (dict) – Coloring information for colored printout.
- Returns:
value (str) – String representation of the value to print.
- print_results(colored=True, colors=None, print_results=True)[source]¶
Print the calculations results to prompt.
- 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:
h_range (list) – List with minimum and maximum values for enthalpy value range.
h_unit (str) – Specify the unit for enthalpy: ‘J / kg’, ‘kJ / kg’, ‘MJ / kg’.
iterinfo (boolean) – Print convergence progress to console.
m_range (list) – List with minimum and maximum values for mass flow value range.
m_unit (str) – Specify the unit for mass flow: ‘kg / s’, ‘t / h’.
p_range (list) – List with minimum and maximum values for pressure value range.
p_unit (str) – Specify the unit for pressure: ‘Pa’, ‘psi’, ‘bar’, ‘MPa’.
s_unit (str) – Specify the unit for specific entropy: ‘J / kgK’, ‘kJ / kgK’, ‘MJ / kgK’.
T_unit (str) – Specify the unit for temperature: ‘K’, ‘C’, ‘F’, ‘R’.
v_unit (str) – Specify the unit for volumetric flow: ‘m3 / s’, ‘m3 / h’, ‘l / s’, ‘l / h’.
vol_unit (str) – Specify the unit for specific volume: ‘m3 / kg’, ‘l / kg’.
- 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, prepare_fast_lane=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 property.
- 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 frominit_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 fromdesign_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_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
tespy.networks.network_reader module¶
Module for loading a tespy network from saved state.
Use the method tespy.networks.network_reader.load_network()
for importing
a network from a saved state.
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/network_reader.py
SPDX-License-Identifier: MIT
- tespy.networks.network_reader.load_network(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 with the network_reader module. 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, ... Compressor, Turbine, SimpleHeatExchanger) >>> from tespy.connections import Connection, Ref, Bus >>> from tespy.networks import load_network, Network >>> import shutil >>> nw = Network(p_unit='bar', T_unit='C', h_unit='kJ / kg', iterinfo=False) >>> air = Source('air') >>> f = Source('fuel') >>> c = Compressor('compressor') >>> comb = CombustionChamber('combustion') >>> t = Turbine('turbine') >>> p = SimpleHeatExchanger('fuel preheater') >>> si = Sink('sink') >>> inc = Connection(air, 'out1', c, 'in1', label='ambient air') >>> cc = Connection(c, 'out1', comb, 'in1') >>> fp = Connection(f, 'out1', p, 'in1') >>> pc = Connection(p, 'out1', comb, 'in2') >>> ct = Connection(comb, 'out1', t, 'in1') >>> outg = Connection(t, 'out1', si, 'in1') >>> nw.add_conns(inc, cc, fp, pc, ct, outg)
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.
>>> c.set_attr(pr=10, eta_s=0.88, design=['eta_s', 'pr'], ... offdesign=['char_map_eta_s', 'char_map_pr']) >>> t.set_attr(eta_s=0.9, design=['eta_s'], ... offdesign=['eta_s_char', 'cone']) >>> comb.set_attr(lamb=2) >>> inc.set_attr(fluid={'N2': 0.7556, 'O2': 0.2315, 'Ar': 0.0129}, T=25, p=1) >>> fp.set_attr(fluid={'CH4': 0.96, 'CO2': 0.04}, T=25, p=40) >>> pc.set_attr(T=25) >>> outg.set_attr(p=Ref(inc, 1, 0)) >>> power = Bus('total power output') >>> power.add_comps({"comp": c, "base": "bus"}, {"comp": t}) >>> nw.add_busses(power)
For a stable start, we specify the fresh air mass flow.
>>> inc.set_attr(m=3) >>> nw.solve('design')
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.>>> comb.set_attr(lamb=None) >>> ct.set_attr(T=1100) >>> inc.set_attr(m=None) >>> power.set_attr(P=-1e6) >>> nw.solve('design') >>> nw.lin_dep False >>> nw.save('design_state.json') >>> _ = nw.export('exported_nwk.json') >>> mass_flow = round(nw.get_conn('ambient air').m.val_SI, 1) >>> c.set_attr(igva='var') >>> nw.solve('offdesign', design_path='design_state.json') >>> round(t.eta_s.val, 1) 0.9 >>> power.set_attr(P=-0.75e6) >>> nw.solve('offdesign', design_path='design_state.json') >>> nw.lin_dep False >>> eta_s_t = round(t.eta_s.val, 3) >>> igva = round(c.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 = load_network('exported_nwk.json') >>> imported_nwk.set_attr(iterinfo=False) >>> imported_nwk.solve('design') >>> imported_nwk.lin_dep False >>> round(imported_nwk.get_conn('ambient air').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.busses['total power output'].set_attr(P=-0.75e6) >>> 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 >>> shutil.rmtree('./exported_nwk', ignore_errors=True) >>> shutil.rmtree('./design_state', ignore_errors=True)