Connections

This section provides an overview of the available Connection classes in the tabs below. Beyond that, it gives an introduction on how to parametrize instances. Connections hold the variables that are solved for in the system of equations of all your models.

Class documentation and example: Connection

Table of parameters

Parameter

Description

Quantity

Role

Method

m

mass flow of the fluid (system variable)

mass_flow

variable

None

p

absolute pressure of the fluid (system variable)

pressure

variable

None

h

mass specific enthalpy of the fluid (system variable)

enthalpy

variable

None

T

temperature of the fluid

temperature

specification

T_func

T_bubble

determine pressure based on the provided bubble temperature of the fluid

temperature

specification

None

T_dew

determine pressure based on the provided dew temperature of the fluid

temperature

specification

None

v

volumetric flow of the fluid

volumetric_flow

specification

v_func

x

vapor mass fraction/quality of the two-phase fluid

quality

specification

x_func

td_dew

superheating temperature difference to dew line temperature

temperature_difference

specification

td_dew_func

td_bubble

subcooling temperature difference to bubble line temperature

temperature_difference

specification

td_bubble_func

vol

specific volume of the fluid

specific_volume

result

None

s

specific entropy of the fluid

entropy

result

None

fluid

mass fractions of the fluid composition (system variable)

None

variable

None

fluid_balance

apply an equation which closes the fluid balance with at least two unknown fluid mass fractions

None

specification

fluid_balance_func

e_T

specific thermal exergy

specific_energy

optional result

None

e_M

specific mechanical exergy

specific_energy

optional result

None

e_PH

specific physical exergy

specific_energy

optional result

None

E_T

thermal exergy flow

power

optional result

None

E_M

mechanical exergy flow

power

optional result

None

E_PH

physical exergy flow

power

optional result

None

Class documentation and example: PowerConnection

Table of parameters

Parameter

Description

Quantity

Role

Method

E

energy flow (system variable)

power

variable

None

Class documentation and example: HAConnection

Table of parameters

Parameter

Description

Quantity

Role

Method

fluid

mass fractions of the fluid composition

None

variable

None

m

mass flow of dry air (system variable)

mass_flow

variable

None

mHA

mass flow of humid air

mass_flow

result

None

mH2O

mass flow of liquid or solid water not contained in humid air

mass_flow

result

None

p

absolute pressure of the fluid (system variable)

pressure

variable

None

h

dry air mass specific enthalpy (system variable)

enthalpy

variable

None

w

mass of water per mass of dry air

ratio

specification

None

T

temperature of the fluid

temperature

specification

T_func

v

volumetric flow of the fluid

volumetric_flow

specification

v_func

vol

specific volume of the fluid

specific_volume

result

None

s

specific entropy of the fluid

entropy

result

None

r

relative humidity

ratio

specification

r_func

fluid_balance

apply an equation which closes the fluid balance with at least two unknown fluid mass fractions

None

specification

fluid_balance_func

e_T

specific thermal exergy

specific_energy

optional result

None

e_M

specific mechanical exergy

specific_energy

optional result

None

e_PH

specific physical exergy

specific_energy

optional result

None

E_T

thermal exergy flow

power

optional result

None

E_M

mechanical exergy flow

power

optional result

None

E_PH

physical exergy flow

power

optional result

None

Class documentation and example: SolutionConnection

Table of parameters

Parameter

Description

Quantity

Role

Method

m

mass flow of the fluid (system variable)

mass_flow

variable

None

p

absolute pressure of the fluid (system variable)

pressure

variable

None

h

mass specific enthalpy of the fluid (system variable)

enthalpy

variable

None

T

temperature of the fluid

temperature

specification

T_func

T_bubble

bubble point temperature of the solution

temperature

specification

None

T_dew

dew point temperature of the solution

temperature

specification

None

v

volumetric flow of the fluid

volumetric_flow

specification

v_func

x

vapor mass fraction of the two-phase solution

quality

specification

x_func

td_dew

superheating temperature difference to the dew point

temperature_difference

specification

td_dew_func

td_bubble

subcooling temperature difference to the bubble point

temperature_difference

specification

td_bubble_func

vol

specific volume of the fluid

specific_volume

result

None

s

specific entropy of the fluid

entropy

result

None

fluid

mass fractions of the fluid composition (system variable)

None

variable

None

fluid_balance

apply an equation which closes the fluid balance with at least two unknown fluid mass fractions

None

specification

fluid_balance_func

e_T

specific thermal exergy

specific_energy

optional result

None

e_M

specific mechanical exergy

specific_energy

optional result

None

e_PH

specific physical exergy

specific_energy

optional result

None

E_T

thermal exergy flow

power

optional result

None

E_M

mechanical exergy flow

power

optional result

None

E_PH

physical exergy flow

power

optional result

None

xi

mass fraction of the key component of the working pair

ratio

result

None

Connection Overview

The tables above list the parameters of each class and their role:

  • variable: solved for by the network, e.g. mass flow, pressure, enthalpy and the fluid composition. A specification fixes the variable.

  • specification: set by the user, the parameter brings its own equation or a state of the fluid, e.g. the temperature or the vapor quality.

  • result: calculated after every solve, e.g. the specific entropy s and the specific volume vol, on HAConnection also the humid-air and separated-water mass flows mHA and mH2O.

  • optional result: calculated on request only, e.g. the exergy of a connection in an exergy evaluation, see Exergy analysis. The results table of the network lists these parameters once they have been calculated.

To set and unset values the same logic applies as for components. Results and optional results cannot be specified as inputs or starting values. Passing a value to set_attr, including through a starting-value key such as s0, raises a TypeError.

Setting and unsetting values

We will create a simple problem using a SimpleHeatExchanger to showcase specification options. Further down there is a second simple problem showcasing the PowerConnection.

A Connection always connects two components:

>>> from tespy.networks import Network
>>> from tespy.connections import Connection
>>> from tespy.components import Sink, Source, SimpleHeatExchanger
>>> nw = Network(iterinfo=False)
>>> nw.units.set_defaults(
...     temperature="°C", power="kW", pressure="bar",
...     pressure_difference="bar", enthalpy="kJ/kg"
... )
>>> source = Source('source')
>>> heatexchanger = SimpleHeatExchanger('heat exchanger')
>>> sink = Sink('sink')
>>> c1 = Connection(source, "out1", heatexchanger, "in1", label="c1")
>>> c2 = Connection(heatexchanger, "out1", sink, "in1", label="c2")
>>> nw.add_conns(c1, c2)

It is possible to set simple values and then solve, e.g.:

  • mass flow, pressure and temperature (and fluid) at inlet

  • enthalpy at outlet

  • (pressure drop in heat exchanger)

>>> c1.set_attr(fluid={"R290": 1}, m=5, p=3, T=50)
>>> c2.set_attr(h=500)
>>> heatexchanger.set_attr(dp=0)
>>> nw.solve("design")

Both connections will have all results available, these can be accessed

  • as SI value

  • as value in the network’s default unit of the respective quantity

  • as value with the corresponding quantity (pint.Quantity)

The results include mass flow, pressure, enthalpy, temperature, temperature, vapor quality, specific volume vol and volumetric flow.

>>> round(c1.h.val, 1)  # value in kJ/kg but without unit attached
668.9
>>> print(round(c1.vol.val_with_unit, 3))  # will be in m3/kg
0.195 meter ** 3 / kilogram
>>> round(c2.T.val_SI, 1)  # SI value
259.0
>>> print(round(c2.T.val_with_unit, 1))  # will be in °C
-14.2 degree_Celsius

You can also provide quantities to a specific parameter to individually specify a unit to a parameter, e.g. inlet mass flow. Note, that units are retained when set with individual quantity.

>>> Q = nw.units.ureg.Quantity
>>> c1.set_attr(m=Q(2, "t/h"))
>>> nw.solve("design")
>>> print(c1.m.val_with_unit)
2 metric_ton / hour
>>> print(round(c2.m.val_with_unit, 2))
0.56 kilogram / second

For pure fluids or CoolProp/REFPROP mixtures we can also specify two-phase properties:

  • vapor mass fraction/quality x

  • dew line temperature difference for superheating td_dew

  • bubble line temperature difference for subcooling td_bubble

  • dew line temperature T_dew and bubble line temperature T_bubble to impose the corresponding pressure to the model

We can replace the inlet temperature specification e.g. with superheating. The unit of temperature_difference is different from the unit for temperature. Unsetting a value is simple: Just set it to None.

>>> c1.set_attr(T=None)  # unset the value
>>> nw.units.default["temperature"]
'°C'
>>> nw.units.default["temperature_difference"]
'delta_degC'
>>> c1.set_attr(td_dew=20)
>>> nw.solve("design")
>>> round(c1.T.val, 2)
5.82

Setting starting values

Setting starting values for the variables can be helpful in some situations. You can do this for the following properties:

  • mass flow m0

  • pressure p0

  • enthalpy h0

These specifications are optional!

>>> c1.set_attr(m0=4)
>>> c2.set_attr(h0=300, p0=4)

Referencing specifications

It is also possible to set up linear relationships between parameters between different instances of Connection in your system in the following form:

\[x_0 = a * x_1 + b\]

It is possible to specify these for

  • mass flow, pressure and enthalpy as well as

  • temperature and volumetric flow.

For example, instead of h we can specify a reference to the temperature at c1. The factor is always based on SI value, the delta is in the default unit of the respective property. The starting value for h is required in this context because in the previous calculation the fluid was in two-phase state, meaning the partial derivative of the temperature of c2 with respect to enthalpy would be zero otherwise.

>>> from tespy.connections import Ref
>>> factor = 1
>>> delta = 25
>>> c2.set_attr(h=None, T=Ref(c1, factor, delta), h0=1000)
>>> nw.solve("design")
>>> round(c1.T.val_SI * factor + delta - 273.15, 2)
30.82
>>> round(c2.T.val, 2)
30.82

Instead we could also reference volumetric flow at outlet to find the temperature at outlet.

>>> c2.set_attr(T=None)
>>> factor = 1.2
>>> delta = 0
>>> c2.set_attr(v=Ref(c1, factor, delta))
>>> nw.solve("design")
>>> round(c1.v.val_SI * factor + delta, 2)
0.11
>>> round(c2.v.val_SI, 2)
0.11
>>> round(c2.T.val, 2)
52.91

For more complex (and arbitrary) relationships between variables of the system use the UserDefinedEquation class. Some examples can be found in this section.

Fluid specification

This sections shows some details on the specification of fluids.

# set both elements of the fluid vector
>>> c1.set_attr(fluid={'water': 1})

# set starting values (might be necessary sometimes)
>>> c1.set_attr(fluid0={'water': 1})

# overwrite the existing fluid vector
>>> c1.fluid.is_set
{'water'}
>>> c1.set_attr(fluid={'N2': 0.7, "O2": 0.3})
>>> c1.fluid.is_set == {'N2', 'O2'}
True

# remove a single specification while keeping the other component inside
>>> c1.fluid.is_set.remove('N2')
>>> c1.fluid.is_set
{'O2'}
>>> c1.fluid.val
{'N2': 0.7, 'O2': 0.3}

CoolProp and REFPROP

It is possible to specify the fluid property back end of the fluids by adding the name of the back end in front of the fluid’s name. For incompressible binary mixtures, you can append the water volume/mass fraction to the fluid’s name, for example:

>>> c1.set_attr(fluid={'water': 1})  # HEOS back end
>>> c1.set_attr(fluid={'INCOMP::water': 1})  # incompressible fluid
>>> c1.set_attr(fluid={'BICUBIC::air': 1})  # bicubic back end
>>> c1.set_attr(fluid={'INCOMP::MPG[0.5]|mass': 1})  # binary incompressible mixture

You can also specify REFPROP based fluids, e.g. R513A, which is a mass based mixture of R134a and R1234yf:

>>> c1.set_attr(fluid={'REFPROP::R134A[0.44]&R1234yf[0.56]|mass': 1})  # REFPROP back end

Note

Without further specifications CoolProp will be used as fluid property database. If you do not specify a back end, the default back end HEOS will be used. For an overview of the back ends available please refer to the fluid property section.

Other Backends

You can also change the engine, for example to the iapws library. It is even possible, that you define your own custom engine, e.g. using polynomial equations. Please check out the fluid properties’ section in the docs on how to do this.

>>> from tespy.tools.fluid_properties.wrappers import IAPWSWrapper
>>> c1.set_attr(fluid={'H2O': 1}, fluid_engines={"H2O": IAPWSWrapper})

Please also check out the section on custom fluid properties for more information.

PowerConnection Overview

PowerConnections can be used to represent non-material energy flow, like power or heat. You can make use of generators, motors and buses.

Different use-cases for the implementation of PowerConnection with the respective power components can be:

  • apply motor or generator efficiencies

  • connect multiple turbomachines on a single shaft

  • collect all electricity production and own consumption to calculate net power

The handling of the PowerConnection and the respective components is identical to standard components.

The PowerConnection only holds a single parameter, namely the power flow E (\(\dot E\)), which is measured in Watts. You can create a PowerConnection instance by connecting to a component that has a respective inlet or outlet. For example, consider a turbine generating electricity. First we can set up a system as we are used to do without any PowerConnections:

>>> from tespy.components import Source, Sink, Turbine
>>> from tespy.connections import Connection
>>> from tespy.networks import Network
>>> nw = Network(iterinfo=False)
>>> nw.units.set_defaults(
...     temperature="degC", pressure="bar", pressure_difference="bar",
...     power="kW"
... )
>>> so = Source("source")
>>> turbine = Turbine("turbine")
>>> si = Sink("sink")
>>> c1 = Connection(so, "out1", turbine, "in1", label="c1")
>>> c2 = Connection(turbine, "out1", si, "in1", label="c2")
>>> nw.add_conns(c1, c2)

We can parametrize the model, e.g. consider the turbine part of a gas turbine, which expands hot flue gases:

>>> c1.set_attr(fluid={"air": 1}, p=10, T=1000, m=1)
>>> c2.set_attr(p=1)
>>> turbine.set_attr(eta_s=0.9)
>>> nw.solve("design")
>>> round(turbine.P.val)
-577

We can add a connection between the turbine and the grid. This will add one extra variable to our problem (the energy flow E) but also one extra equation, namely the turbine energy balance. Therefore, after adding the new connection, there is nothing to change to make the model solve.

>>> from tespy.connections import PowerConnection
>>> from tespy.components import PowerSink
>>> grid = PowerSink("grid")
>>> e1 = PowerConnection(turbine, "power", grid, "power", label="e1")
>>> nw.add_conns(e1)
>>> nw.solve("design")
>>> round(e1.E.val)
577

Note

Note that the value of the energy flow of a PowerConnection will always be positive in the defined direction (from one component’s outlet to another component’s inlet).

To learn what power connections are available in each of the component classes see the respective API documentation. There are a couple of example applications available for the PowerConnection in this section.

HAConnection Overview

Attention

HAConnection not yet stable and not fully tested with all kinds of components. You are invited to raise issues on github in case you identify any.

For the simulation of humid air, for example in air conditioning systems, drying processes or cooling towers, use the HAConnection instead of the regular Connection. It works exactly like a Connection but provides additional parameters specific to humid air. The most important ones are:

  • mHA: mass flow of humid air (note, that m is the mass flow of dry air)

  • mH2O: mass flow of liquid or solid water not contained in the humid air

  • w: humidity ratio, i.e. mass of water per mass of dry air

  • r: relative humidity

The fluid composition is always air and water and the mixing rule is fixed to humidair. Specifying the humidity ratio w automatically sets the corresponding fluid composition.

>>> from tespy.connections import HAConnection
>>> from tespy.components import Source, Sink
>>> from tespy.networks import Network
>>> nw_ha = Network(iterinfo=False)
>>> nw_ha.units.set_defaults(
...     temperature="°C", pressure="bar", pressure_difference="bar")
>>> source_ha = Source("source")
>>> sink_ha = Sink("sink")
>>> c_ha = HAConnection(source_ha, "out1", sink_ha, "in1", label="c_ha")
>>> nw_ha.add_conns(c_ha)
>>> c_ha.set_attr(m=1, p=1, T=20, w=0.01)
>>> nw_ha.solve("design")
>>> round(c_ha.r.val, 4)
0.6737
>>> round(c_ha.mHA.val, 2)
1.01

Access from the Network object

You may want to access the network’s connections or powerconnections other than using the variable names, for example in an imported network or connections from a subsystem. It is possible to access these using the connection’s label similar as it is possible for components. By default, the label is generated by this logic:

source:source_id_target:target_id, where

  • source and target are the labels of the components that are connected.

  • source_id and target_id are e.g. out1 and in2 respectively.

>>> conn = nw.get_conn('c1')
>>> conn.label
'c1'
>>> powerconn = nw.get_conn('e1')
>>> powerconn.label
'e1'