Debug your models efficiently

This tutorial gives insights on how you can debug tespy models. The user interface of the current implementation might still need some refinement, so you are invited to raise issues in the github repository. We will change it based on the feedback. The outputs shown here are based on the following version of tespy:

from tespy import __version__
__version__
'0.9.11.dev0'

Simple model debugging

This tutorial will show a couple of things

  1. How to extract the variables of the problem

    • before presolving step

    • after presolving step and identify the presolved variables

  2. How to extract the applied equations of the problem

    • before presolving step

    • after presolving step and identify the presolved equations

  3. How to read and fix the errors that are raised during presolving

  4. How to debug a model in case of linear dependency by inspecting the error message, incidence matrix and Jacobian

  5. How to interpret/deal with a couple of warnings/errors that might pop up during postprocessing

Model overview

The model we implement is a very simple heat pump model, just as implemented in the introductory Heat Pump tutorial.

../_images/heat_pump.svg ../_images/heat_pump_darkmode.svg

Model code

from tespy.components import CycleCloser, SimpleHeatExchanger, Compressor, Valve, Motor, PowerSource
from tespy.connections import Connection, PowerConnection
from tespy.networks import Network
nw = Network()
nw.units.set_defaults(
    temperature="°C",
    pressure="bar",
    power="kW",
    heat="kW",
    enthalpy="kJ/kg"
)
grid = PowerSource("grid")
motor = Motor("motor")

cc = CycleCloser("cycle closer")
valve = Valve("valve")
evaporator = SimpleHeatExchanger("evaporator")
compressor = Compressor("compressor")
condenser = SimpleHeatExchanger("condenser")

c1 = Connection(cc, "out1", evaporator, "in1", label="c1")
c2 = Connection(evaporator, "out1", compressor, "in1", label="c2")
c3 = Connection(compressor, "out1", condenser, "in1", label="c3")
c4 = Connection(condenser, "out1", valve, "in1", label="c4")
c0 = Connection(valve, "out1", cc, "in1", label="c0")


nw.add_conns(c1, c2, c3, c4, c0)

e1 = PowerConnection(grid, "power", motor, "power_in", label="e1")
e2 = PowerConnection(motor, "power_out", compressor, "power", label="e2")

nw.add_conns(e1, e2)

Debug the model

Variable and equation identification

With nothing specified and trying to solve we will get an information, that the network is lacking fluid information.

nw.solve("design", init_only=True)
---------------------------------------------------------------------------
TESPyNetworkError                         Traceback (most recent call last)
Cell In[5], line 1
----> 1 nw.solve("design", init_only=True)

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:2476, in Network.solve(self, mode, init_path, design_path, max_iter, min_iter, init_only, init_previous, use_cuda, print_results, robust_relax)
   2468 msg = (
   2469     "Network information:\n"
   2470     f" - Number of components: {len(self.comps)}\n"
   2471     f" - Number of connections: {len(self.conns)}\n"
   2472     f" - Number of busses: {len(self.busses)}"
   2473 )
   2474 logger.debug(msg)
-> 2476 self._prepare_problem()
   2478 if init_only:
   2479     return

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:889, in Network._prepare_problem(self)
    886         self._create_fluid_wrapper_branches()
    887     continue
--> 889 self._propagate_fluid_wrappers()
    890 self._init_connection_result_datastructure()
    891 self._prepare_solve_mode()

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:960, in Network._propagate_fluid_wrappers(self)
    954 if num_potential_fluids == 0:
    955     msg = (
    956         "The follwing connections of your network are missing any "
    957         "kind of fluid composition information:"
    958         f"{', '.join([c.label for c in all_connections])}."
    959     )
--> 960     raise hlp.TESPyNetworkError(msg)
    962 for c in all_connections:
    963     c.mixing_rule = mixing_rule

TESPyNetworkError: The follwing connections of your network are missing any kind of fluid composition information:c1, c2, c3, c4, c0.

Then let’s specify the fluid and try to preprocess the network. With solve_determination we can check, how many parameters are specified and how many are missing. When running with init_only no error is raised, so we can use that starting point in an interactive python environment to get started with debugging.

c1.set_attr(fluid={"R290": 1})
nw.solve("design", init_only=True)
# this would be executed as next step for actual solve call
nw.solve_determination()
You have not provided enough parameters: 10 required, 1 supplied. Aborting calculation!
---------------------------------------------------------------------------
TESPyNetworkError                         Traceback (most recent call last)
Cell In[6], line 4
      2 nw.solve("design", init_only=True)
      3 # this would be executed as next step for actual solve call
----> 4 nw.solve_determination()

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:2609, in Network.solve_determination(self)
   2607 logger.error(msg)
   2608 self.status = 11
-> 2609 raise hlp.TESPyNetworkError(msg)

TESPyNetworkError: You have not provided enough parameters: 10 required, 1 supplied. Aborting calculation!

Now we can check the following information:

  • Which are the original variables of our model

  • Which of those variables have already been determined by the presolving

  • Which ones are the actual variables, that the model has to solve and which original variables of the model these variables represent

The original variables:

nw.get_variables_before_presolve()
[('c0', 'm'),
 ('c0', 'p'),
 ('c0', 'h'),
 ('c0', 'fluid'),
 ('c1', 'm'),
 ('c1', 'p'),
 ('c1', 'h'),
 ('c1', 'fluid'),
 ('c2', 'm'),
 ('c2', 'p'),
 ('c2', 'h'),
 ('c2', 'fluid'),
 ('c3', 'm'),
 ('c3', 'p'),
 ('c3', 'h'),
 ('c3', 'fluid'),
 ('c4', 'm'),
 ('c4', 'p'),
 ('c4', 'h'),
 ('c4', 'fluid'),
 ('e1', 'E'),
 ('e2', 'E')]

The variables solved already by the presolving step:

nw.get_presolved_variables()
[('c0', 'fluid'),
 ('c1', 'fluid'),
 ('c2', 'fluid'),
 ('c3', 'fluid'),
 ('c4', 'fluid')]

The actual variables of the problem as a dictionary:

  • keys: tuple with variable number as first element, variable type as second element (mass flow, pressure, enthalpy, fluid, …)

  • values: list of the original variables this variable represents. The list again contains tuples with

    • the label of the component/connection from which the variable originated

    • the type of variable as second element

nw.get_variables()
{(0, 'p'): [('c2', 'p')],
 (1, 'h'): [('c2', 'h')],
 (2, 'p'): [('c3', 'p')],
 (3, 'h'): [('c3', 'h')],
 (4, 'p'): [('c4', 'p')],
 (5, 'E'): [('e1', 'E')],
 (6, 'E'): [('e2', 'E')],
 (7, 'm'): [('c2', 'm'), ('c3', 'm'), ('c1', 'm'), ('c4', 'm'), ('c0', 'm')],
 (8, 'p'): [('c0', 'p'), ('c1', 'p')],
 (9, 'h'): [('c0', 'h'), ('c1', 'h'), ('c4', 'h')]}

We can also check which equations of the model have been presolved in order to retrieve the dependencies between the variables. E.g. the mass flow variable just before represents all the mass flows in this model. We can see, that the mass_flow_constraints have been solved for all components. The equations indicated here can be inspected in the tables of the documentation on the components and connections.

nw.get_presolved_equations()
[('compressor', 'mass_flow_constraints'),
 ('compressor', 'fluid_constraints'),
 ('condenser', 'mass_flow_constraints'),
 ('condenser', 'fluid_constraints'),
 ('cycle closer', 'pressure_equality_constraint'),
 ('cycle closer', 'enthalpy_equality_constraint'),
 ('evaporator', 'mass_flow_constraints'),
 ('evaporator', 'fluid_constraints'),
 ('valve', 'mass_flow_constraints'),
 ('valve', 'fluid_constraints'),
 ('valve', 'enthalpy_constraints')]

There is not yet an easy way to identify which variable was presolved by which. Next to the presolved equations we can also inspect, which equations are present in the actual model that needs to be solved iteratively.

nw.get_equations()
{0: ('compressor', ('energy_connector_balance', 0))}

With the equations we can also extract the variables these depend on.

nw.get_equations_with_dependents()
{('compressor', ('energy_connector_balance', 0)): [(1, 'h'),
  (3, 'h'),
  (6, 'E'),
  (7, 'm')]}

Impose parameters and check again

Now let’s impose a couple of boundary conditions:

  • No pressure drop in heat exchanges

  • Compressor efficiency

  • Motor efficiency

condenser.set_attr(dp=0)
evaporator.set_attr(dp=0)
compressor.set_attr(eta_s=0.8)
motor.set_attr(eta=0.97)
nw.solve("design", init_only=True)

Again, we can inspect, which variables have been presolved now. It does not change, because we did not impose any boundary conditions, where any of the variables can be directly determined from.

nw.get_presolved_variables()
[('c0', 'fluid'),
 ('c1', 'fluid'),
 ('c2', 'fluid'),
 ('c3', 'fluid'),
 ('c4', 'fluid')]

But if we check the actual variables of the system, we see that the number has been reduced. The two energy flows are now mapped to a single variable, and the pressure values before and after the heat exchangers have been also mapped to a single variable respectively.

nw.get_variables()
{(0, 'h'): [('c2', 'h')],
 (1, 'h'): [('c3', 'h')],
 (2, 'm'): [('c2', 'm'), ('c3', 'm'), ('c1', 'm'), ('c4', 'm'), ('c0', 'm')],
 (3, 'p'): [('c3', 'p'), ('c4', 'p')],
 (4, 'p'): [('c0', 'p'), ('c1', 'p'), ('c2', 'p')],
 (5, 'h'): [('c0', 'h'), ('c1', 'h'), ('c4', 'h')],
 (6, 'E'): [('e1', 'E'), ('e2', 'E')]}

The reason for that can be seen in the presolved equations, where now we have three additional entries.

nw.get_presolved_equations()
[('compressor', 'mass_flow_constraints'),
 ('compressor', 'fluid_constraints'),
 ('condenser', 'mass_flow_constraints'),
 ('condenser', 'fluid_constraints'),
 ('condenser', 'dp'),
 ('cycle closer', 'pressure_equality_constraint'),
 ('cycle closer', 'enthalpy_equality_constraint'),
 ('evaporator', 'mass_flow_constraints'),
 ('evaporator', 'fluid_constraints'),
 ('evaporator', 'dp'),
 ('motor', 'eta'),
 ('valve', 'mass_flow_constraints'),
 ('valve', 'fluid_constraints'),
 ('valve', 'enthalpy_constraints')]

And we also get one more equation in our model equations, that needs to be solved numerically: the compressor efficiency.

nw.get_equations_with_dependents()
{('compressor', ('energy_connector_balance', 0)): [(0, 'h'),
  (1, 'h'),
  (2, 'm'),
  (6, 'E')],
 ('compressor', ('eta_s', 0)): [(0, 'h'), (1, 'h'), (3, 'p'), (4, 'p')]}

Let’s add more boundary conditions, because we are still missing a couple:

  • evaporation temperature level and superheating

c2.set_attr(T_dew=10, td_dew=10)
nw.solve("design", init_only=True)

Now we can see that the number of variables has been reduced by two. The reason for this is, that the presolver was able to identify pressure and enthalpy at the compressor inlet with the given boundary conditions.

nw.get_variables()
{(0, 'h'): [('c3', 'h')],
 (1, 'm'): [('c2', 'm'), ('c3', 'm'), ('c1', 'm'), ('c4', 'm'), ('c0', 'm')],
 (2, 'p'): [('c3', 'p'), ('c4', 'p')],
 (3, 'h'): [('c0', 'h'), ('c1', 'h'), ('c4', 'h')],
 (4, 'E'): [('e1', 'E'), ('e2', 'E')]}

Since these two variables have now been presolved, the equation of the compressor has less dependents, as it is not necessary to solve for the respective variables anymore.

nw.get_equations_with_dependents()
{('compressor', ('energy_connector_balance', 0)): [(0, 'h'),
  (1, 'm'),
  (4, 'E')],
 ('compressor', ('eta_s', 0)): [(0, 'h'), (2, 'p')]}

We are still missing 3 equations as we have 5 variables in the problem and only 2 equations at the moment, so let’s add the missing specifications:

  • electrical power input

  • condensing temperature level and subcooling

c4.set_attr(T_bubble=60, td_bubble=0)
e1.set_attr(E=100)  # 100 kW
nw.solve("design")
 iter  | residual   | progress   | massflow   | pressure   | enthalpy   | fluid      | component  
-------+------------+------------+------------+------------+------------+------------+------------
 1     | 2.11e+05   | 7 %        | 6.45e-01   | 0.00e+00   | 1.25e+05   | 0.00e+00   | 0.00e+00   
 2     | 8.04e+04   | 12 %       | 1.09e+00   | 0.00e+00   | 1.83e-10   | 0.00e+00   | 0.00e+00   
 3     | 3.75e-09   | 100 %      | 4.97e-14   | 0.00e+00   | 6.37e-11   | 0.00e+00   | 0.00e+00   
 4     | 1.14e-10   | 100 %      | 2.47e-16   | 0.00e+00   | 6.37e-11   | 0.00e+00   | 0.00e+00   
Total iterations: 4, Calculation time: 0.00 s, Iterations per second: 2076.90

Handle errors during presolving

Some errors can occur during presolving, for example:

You specify a linear change of specific variable while specifying both values simultaneously. In this case, the error message directly tells you which variables are linear dependent and that you specified more than a single value in that set (points to the labels of the connections/components).

e2.set_attr(E=97)
nw.solve("design")
---------------------------------------------------------------------------
TESPyNetworkError                         Traceback (most recent call last)
Cell In[23], line 2
      1 e2.set_attr(E=97)
----> 2 nw.solve("design")

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:2476, in Network.solve(self, mode, init_path, design_path, max_iter, min_iter, init_only, init_previous, use_cuda, print_results, robust_relax)
   2468 msg = (
   2469     "Network information:\n"
   2470     f" - Number of components: {len(self.comps)}\n"
   2471     f" - Number of connections: {len(self.conns)}\n"
   2472     f" - Number of busses: {len(self.busses)}"
   2473 )
   2474 logger.debug(msg)
-> 2476 self._prepare_problem()
   2478 if init_only:
   2479     return

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:897, in Network._prepare_problem(self)
    894 self._init_set_properties()
    895 self._create_structure_matrix()
--> 897 self._presolve()
    898 self._prepare_for_solver()
    900 # generic fluid property initialisation

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1519, in Network._presolve(self)
   1516 for c in self.conns['object']:
   1517     self._presolved_equations += c._presolve()
-> 1519 self._presolve_linear_dependents()
   1521 # iteratively check presolvable fluid properties
   1522 # and distribute presolved variables to all linear dependents
   1523 # until the number of variables does not change anymore
   1524 number_variables = sum([
   1525     variable.is_var
   1526     for conn in self.conns['object']
   1527     for variable in conn.get_variables().values()
   1528 ])

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1627, in Network._presolve_linear_dependents(self)
   1618     variables_properties = [
   1619         f"({self._variable_lookup[var]['object'].label}: "
   1620         f"{self._variable_lookup[var]['property']})"
   1621         for var in linear_dependents["variables"]
   1622     ]
   1623     msg = (
   1624         "You specified more than one variable of the linear "
   1625         f"dependent variables: {', '.join(variables_properties)}."
   1626     )
-> 1627     raise hlp.TESPyNetworkError(msg)
   1628 elif number_specifications == 1:
   1629     reference_data = self._variable_lookup[reference]

TESPyNetworkError: You specified more than one variable of the linear dependent variables: (e1: E), (e2: E).

We can see the same problem if we were to specify compressor pressure ratio: The compressor inlet pressure is determined from the compressor inlet state, the condenser outlet pressure is determined from the condenser outlet state and the condenser pressure drop is specified. By that also the compressor outlet pressure is known and you cannot specify the outlet pressure.

e2.set_attr(E=None)
compressor.set_attr(pr=4)
nw.solve("design", init_only=True)
---------------------------------------------------------------------------
TESPyNetworkError                         Traceback (most recent call last)
Cell In[24], line 3
      1 e2.set_attr(E=None)
      2 compressor.set_attr(pr=4)
----> 3 nw.solve("design", init_only=True)

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:2476, in Network.solve(self, mode, init_path, design_path, max_iter, min_iter, init_only, init_previous, use_cuda, print_results, robust_relax)
   2468 msg = (
   2469     "Network information:\n"
   2470     f" - Number of components: {len(self.comps)}\n"
   2471     f" - Number of connections: {len(self.conns)}\n"
   2472     f" - Number of busses: {len(self.busses)}"
   2473 )
   2474 logger.debug(msg)
-> 2476 self._prepare_problem()
   2478 if init_only:
   2479     return

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:897, in Network._prepare_problem(self)
    894 self._init_set_properties()
    895 self._create_structure_matrix()
--> 897 self._presolve()
    898 self._prepare_for_solver()
    900 # generic fluid property initialisation

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1519, in Network._presolve(self)
   1516 for c in self.conns['object']:
   1517     self._presolved_equations += c._presolve()
-> 1519 self._presolve_linear_dependents()
   1521 # iteratively check presolvable fluid properties
   1522 # and distribute presolved variables to all linear dependents
   1523 # until the number of variables does not change anymore
   1524 number_variables = sum([
   1525     variable.is_var
   1526     for conn in self.conns['object']
   1527     for variable in conn.get_variables().values()
   1528 ])

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1627, in Network._presolve_linear_dependents(self)
   1618     variables_properties = [
   1619         f"({self._variable_lookup[var]['object'].label}: "
   1620         f"{self._variable_lookup[var]['property']})"
   1621         for var in linear_dependents["variables"]
   1622     ]
   1623     msg = (
   1624         "You specified more than one variable of the linear "
   1625         f"dependent variables: {', '.join(variables_properties)}."
   1626     )
-> 1627     raise hlp.TESPyNetworkError(msg)
   1628 elif number_specifications == 1:
   1629     reference_data = self._variable_lookup[reference]

TESPyNetworkError: You specified more than one variable of the linear dependent variables: (c2: p), (c3: p), (c1: p), (c0: p), (c4: p).

You can also think of creating a circular dependency. For example, if you specify a relationship of mass flow in front and behind the cycle closer (or if you were to remove the cycle closer). Then the mass flow would form a circular dependency. As output of the error message you get the variables which are part of the circular dependency and the equations responsible for that.

compressor.set_attr(pr=None)


from tespy.connections import Ref

c1.set_attr(m=Ref(c0, 1, 0))
nw.solve("design", init_only=True)
---------------------------------------------------------------------------
TESPyNetworkError                         Traceback (most recent call last)
Cell In[25], line 7
      4 from tespy.connections import Ref
      6 c1.set_attr(m=Ref(c0, 1, 0))
----> 7 nw.solve("design", init_only=True)

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:2476, in Network.solve(self, mode, init_path, design_path, max_iter, min_iter, init_only, init_previous, use_cuda, print_results, robust_relax)
   2468 msg = (
   2469     "Network information:\n"
   2470     f" - Number of components: {len(self.comps)}\n"
   2471     f" - Number of connections: {len(self.conns)}\n"
   2472     f" - Number of busses: {len(self.busses)}"
   2473 )
   2474 logger.debug(msg)
-> 2476 self._prepare_problem()
   2478 if init_only:
   2479     return

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:895, in Network._prepare_problem(self)
    892 # this method will distribute units and set SI values from given values
    893 # and units
    894 self._init_set_properties()
--> 895 self._create_structure_matrix()
    897 self._presolve()
    898 self._prepare_for_solver()

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1152, in Network._create_structure_matrix(self)
   1149 sum_eq = self._preprocess_network_parts(self.comps["object"], sum_eq)
   1150 sum_eq = self._preprocess_network_parts(self.user_defined_eq.values(), sum_eq)
-> 1152 _linear_dependencies = self._find_linear_dependent_variables(
   1153     self._structure_matrix, self._rhs
   1154 )
   1155 _linear_dependent_variables = [
   1156     var for linear_dependents in _linear_dependencies
   1157     for var in linear_dependents["variables"]
   1158 ]
   1159 _missing_variables = [
   1160     {
   1161         "variables": [var],
   (...)
   1167     for var in set(range(num_vars)) - set(_linear_dependent_variables)
   1168 ]

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1315, in Network._find_linear_dependent_variables(self, sparse_matrix, rhs)
   1311 cycle = self._find_cycles_in_graph(
   1312     {k: [x[0] for x in v] for k, v in adjacency_list.items()}
   1313 )
   1314 if cycle is not None:
-> 1315     self._raise_error_if_cycle(cycle, edges_with_factors, eq_idx)
   1317 # Find connected components and compute factors/offsets
   1318 visited = set()

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1476, in Network._raise_error_if_cycle(self, cycle, edges_with_factors, eq_idx)
   1468 equations = self._get_equation_sets_by_eq_set_number(cycling_eqs)
   1469 msg = (
   1470     "A circular dependency between the variables "
   1471     f"{', '.join([str(v) for v in variable_names])} "
   (...)
   1474     "detected. This overdetermines the problem."
   1475 )
-> 1476 raise hlp.TESPyNetworkError(msg)

TESPyNetworkError: A circular dependency between the variables ('c0', 'm'), ('c1', 'm'), ('c2', 'm'), ('c3', 'm'), ('c4', 'm') caused by the equations ('c1', 'm_ref'), ('compressor', 'mass_flow_constraints'), ('condenser', 'mass_flow_constraints'), ('evaporator', 'mass_flow_constraints'), ('valve', 'mass_flow_constraints') has been detected. This overdetermines the problem.

A last error that might occur is specification of properties that determine the same variable, e.g. the c2 pressure as the evaporation pressure is already determined from the saturation temperature and superheating.

c1.set_attr(m=None)
c2.set_attr(p=10)  # p has already been set!
nw.solve("design")
---------------------------------------------------------------------------
TESPyNetworkError                         Traceback (most recent call last)
Cell In[26], line 3
      1 c1.set_attr(m=None)
      2 c2.set_attr(p=10)  # p has already been set!
----> 3 nw.solve("design")

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:2476, in Network.solve(self, mode, init_path, design_path, max_iter, min_iter, init_only, init_previous, use_cuda, print_results, robust_relax)
   2468 msg = (
   2469     "Network information:\n"
   2470     f" - Number of components: {len(self.comps)}\n"
   2471     f" - Number of connections: {len(self.conns)}\n"
   2472     f" - Number of busses: {len(self.busses)}"
   2473 )
   2474 logger.debug(msg)
-> 2476 self._prepare_problem()
   2478 if init_only:
   2479     return

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:897, in Network._prepare_problem(self)
    894 self._init_set_properties()
    895 self._create_structure_matrix()
--> 897 self._presolve()
    898 self._prepare_for_solver()
    900 # generic fluid property initialisation

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/networks/network.py:1517, in Network._presolve(self)
   1514 # set up the actual list of equations for connections, components,
   1516 for c in self.conns['object']:
-> 1517     self._presolved_equations += c._presolve()
   1519 self._presolve_linear_dependents()
   1521 # iteratively check presolvable fluid properties
   1522 # and distribute presolved variables to all linear dependents
   1523 # until the number of variables does not change anymore

File ~/checkouts/readthedocs.org/user_builds/tespy/envs/820/lib/python3.10/site-packages/tespy/connections/connection.py:926, in Connection._presolve(self)
    919 if num_specs > 2:
    920     msg = (
    921         "You have specified more than 2 parameters for the connection "
    922         f"{self.label} with a known fluid composition: "
    923         f"{', '.join(specifications)}. This overdetermines the state "
    924         "of the fluid."
    925     )
--> 926     raise TESPyNetworkError(msg)
    928 presolved_equations = []
    929 if self.p.is_set:

TESPyNetworkError: You have specified more than 2 parameters for the connection c2 with a known fluid composition: p, T_dew, td_dew. This overdetermines the state of the fluid.
c2.set_attr(p=None)

Inspect reasons for linear dependency

You can also inspect the network after crashing due to a linear dependency in the Jacobian. For this, we will construct a case where, we get this exact issue:

  • We fix heat output of condenser (having fixed motor electrical power already)

  • no specification of evaporator delta p

condenser.set_attr(Q=-350)
evaporator.set_attr(dp=None)
nw.solve("design")
Detected singularity in Jacobian matrix. This singularity is most likely caused by the parametrization of your problem and NOT a numerical issue. Double check your setup.
The following variables of your problem are not in connection with any equation: (2, 'p')
 iter  | residual   | progress   | massflow   | pressure   | enthalpy   | fluid      | component  
-------+------------+------------+------------+------------+------------+------------+------------
 1     | 5.67e+04   | 13 %       | NaN        | NaN        | NaN        | NaN        | NaN        
Total iterations: 1, Calculation time: 0.00 s, Iterations per second: 1555.75

The solver now tells us:

  • The problem is likely a problem in the setup

  • The reason is that a variable is not associated with any equation

We can then retrieve, which unknowns the variable represents to understand, the issue behind it.

nw.get_variables()
{(0, 'h'): [('c3', 'h')],
 (1, 'm'): [('c2', 'm'), ('c3', 'm'), ('c1', 'm'), ('c4', 'm'), ('c0', 'm')],
 (2, 'p'): [('c0', 'p'), ('c1', 'p')]}

And we can identify, that there is no equation, that depends on that variable:

nw.get_equations_with_dependents()
{('compressor', ('energy_connector_balance', 0)): [(0, 'h'), (1, 'm')],
 ('compressor', ('eta_s', 0)): [(0, 'h')],
 ('condenser', ('Q', 0)): [(0, 'h'), (1, 'm')]}

If the problem is not a setup based problem but a numerical one due to partial derivatives in the Jacobian becoming zero where they should not, it is also possible to inspect the issue. For this we will change the model setup. Just a normal HeatExchanger is sufficient for that:

from tespy.components import Source, Sink, HeatExchanger


nw = Network()
nw.units.set_defaults(
    temperature="°C",
    pressure="bar"
)

so1 = Source("source 1")
so2 = Source("source 2")

si1 = Sink("sink 1")
si2 = Sink("sink 2")

heatex = HeatExchanger("heatexchanger")

c1 = Connection(so1, "out1", heatex, "in1", label="c1")
c2 = Connection(heatex, "out1", si1, "in1", label="c2")
d1 = Connection(so2, "out1", heatex, "in2", label="d1")
d2 = Connection(heatex, "out2", si2, "in1", label="d2")

nw.add_conns(c1, c2, d1, d2)

Now we could make a specification that is impossible but hard to catch as being a setup problem: We set a minimum terminal temperature difference of 25 K but at the same time fix the temperature at hot side outlet and cold side inlet (leading to a temperature difference of 20 K).

c1.set_attr(fluid={"air": 1}, T=200, p=1, m=5)
c2.set_attr(T=110)
d1.set_attr(fluid={"water": 1}, T=90, p=1)
heatex.set_attr(dp1=0, dp2=0, ttd_min=25)
nw.solve("design")
Found singularity in Jacobian matrix, calculation aborted! The setup of you problem seems to be solvable. It failed due to partial derivatives in the Jacobian being zero, which were expected not to be zero, or the other way around. The reason for this usually lies in starting value selection or bad convergence. The following equations (key of outer dict) may have an unexpected zero/non-zero in the partial derivative towards the variable (value of outer dict) and be the root of evil: {1: ('heatexchanger', ('ttd_min', 0))}: {(0, 'h'): [('d2', 'h')]}The following equations of your problem do not depend on any variable: ('heatexchanger', ('ttd_min', 0))
 iter  | residual   | progress   | massflow   | pressure   | enthalpy   | fluid      | component  
-------+------------+------------+------------+------------+------------+------------+------------
 1     | 4.45e+06   | 0 %        | 1.61e+00   | 0.00e+00   | 1.48e+05   | 0.00e+00   | 0.00e+00   
 2     | 2.37e+05   | 6 %        | NaN        | NaN        | NaN        | NaN        | NaN        
Total iterations: 2, Calculation time: 0.00 s, Iterations per second: 456.42

We can see:

  • The solver tells us potentially numerical issue (not always correct!).

  • The heat exchanger ttd_min function should depend on variable (0, “h”) representing “h” of connection “d2”.

  • There is a zero in the Jacobian of that function towards that number while there should be a non-zero entry.

  • An extra piece of information: all entries in the Jacobian of the equation became zero.

Next we can also extract the incidence matrix and the Jacobian to identify issue if we want. For that we have a look at the equations and their dependents first.

nw.get_equations_with_dependents()
{('heatexchanger', ('energy_balance_constraints', 0)): [(0, 'h'), (1, 'm')],
 ('heatexchanger', ('ttd_min', 0)): [(0, 'h')]}

Then we can use the incidence_matrix and the Jacobian. Here we see for the problematic equation number (1) and variable number (0):

  • The incidence matrix indicates which entries (rows = equations, columns = variables) of the Jacobian are supposed to be zero or non-zero.

  • The actual values of the Jacobian.

  • And, that a non-zero is expected for (1, 0) but there is a zero instead.

nw._incidence_matrix_dense
array([[1., 1.],
       [1., 0.]])
nw.jacobian
array([[2.84098354e-01, 2.44827107e+06],
       [0.00000000e+00, 0.00000000e+00]])