oemof / tespy

Thermal Engineering Systems in Python (TESPy). This package provides a powerful simulation toolkit for thermal engineering plants such as power plants, district heating systems or heat pumps.
https://tespy.readthedocs.io
MIT License
256 stars 80 forks source link

Include enthalpies of formation to support chemical reactions #341

Closed BenPortner closed 5 months ago

BenPortner commented 2 years ago

Requesting comments by @fwitte before finishing the last todos.

Changes overview

Motivation

Coolprop, the library which is currently used to calculate fluid properties, uses arbitrary reference points for the calculation of species' enthalpies. Furthermore, the enthalpies do not account for the enthalpy of formation (see https://github.com/CoolProp/CoolProp/issues/1360, https://github.com/CoolProp/CoolProp/issues/58). As a consequence, it is currently very tedious to simulate chemical reactions in TESPy. The reaction enthalpy must be accounted for manually, leading to hardcoded values and a limited number of implemented reactions. This PR addresses the issue by:

New component: AdiabaticConstPressureReactor

The new component can be used to model arbitrary chemical gas phase reactions (under adiabatic, constant pressure conditions). The component accepts a reaction formula (e.g. CH4 + 2 O2 -> CO2 + 2 H2O) and a yield parameter X describing how much of the limiting substance is converted (1: complete conversion, 0: no reaction). Example usage:

from tespy.networks import Network
from tespy.components import Sink, Source
from tespy.components.reactors.adiabatic_const_pressure_reactor import AdiabaticConstPressureReactor
from tespy.connections import Connection

nw = Network(fluids=['CH4', 'O2', 'CO2', 'H2O'], m_unit='kg / s', T_unit='C', p_unit='bar', h_unit='kJ / kg')
src = Source('source')
sink = Sink('sink')

reactor = AdiabaticConstPressureReactor("reactor", X=1, formula="CH4 + 2 O2 -> CO2 + 2 H2O")

nw.add_conns(
    Connection(src, 'out1', reactor, 'in1', fluid={'CH4': 0.05, 'O2': 0.2, 'CO2': 0.75, "H2O":0}, p=1, T=20, m=1),
    Connection(reactor, 'out1', sink, 'in1'),
)

nw.solve('design')
nw.print_results()

reactor.outl[0].p.val  # 1 bar
reactor.outl[0].T.val  # 1855°C
reactor.outl[0].fluid.val # OrderedDict([('CH4', 0), ('CO2', 0.8871638890296235), ('H2O', 0.11229505817838446), ('O2', 0.0005410527919919749)])

Possible weak points

Additional dependencies

I had to introduce two new libraries in order to realize the changes:

The libraries are lightweight and should not add too much overhead.

Missing data

Not all species, which exist in Coolprop are also available in pyromat. For non-existent species, the enthalpy of formation is assumed to be zero.

Large negative enthalpies

The new enthalpy values, which now include the enthalpy of formation, are large negative numbers (e.g. -15e6 for water), which seem a bit unintuitive at first. However, large thermal engineering softwares like ASPEN+ also produce such negative numbers. In fact, with the new changes, ASPEN+ and TESPy seem to produce the same enthalpy values (at least for the examples I tried).

Todos

[x] make sure tests run successfully [ ] fix doctests [x] implement tests for new components [ ] update API documentation [ ] update online documentation [ ] update What's New file

pep8speaks commented 2 years ago

Hello @BenPortner! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 36:80: E501 line too long (90 > 79 characters) Line 37:80: E501 line too long (94 > 79 characters) Line 38:80: E501 line too long (101 > 79 characters) Line 39:80: E501 line too long (99 > 79 characters)

Comment last updated at 2022-05-17 16:37:32 UTC
lgtm-com[bot] commented 2 years ago

This pull request introduces 7 alerts when merging a980d8328d7da16abfaa36b677473f6fa3eb4635 into a7fe99e9087c3bfe6e9ead0286aa8983270afa4d - view on LGTM.com

new alerts:

fwitte commented 2 years ago

@BenPortner, thank you so much for your great input! I‘ll have a look at it in the upcoming days.

fwitte commented 2 years ago

Can we have a short call before you put too much work into fixing all doc tests? :D just want so see, if that is necessary or if we can find a different solution. especially for larger systems I fear that the current convergence checks and helpers might not like the changes.

fwitte commented 5 months ago

This had broken with a couple of things, so I am closing it again for now. I like the idea of the generic reactor. And, with the new structure and capabilities of the fluid property module, it might actually be much easier to implement it than before, only have to find the time for it.

Thank you still for putting in the effort!