BioSTEAMDevelopmentGroup / thermosteam

BioSTEAM's Premier Thermodynamic Engine
Other
58 stars 12 forks source link

Creating a Stream object with mass flows for isomer chemicals (acetic acid & glycolaldehyde) #94

Closed Ali-Nazarik closed 5 months ago

Ali-Nazarik commented 5 months ago

Describe the bug

An index error arises when attempting to add two chemicals that are isomers, i.e., they have the same atomic composition. Specifically, this error occurs when adding acetic acid and glycolaldehyde, both of which have the molecular formula C2H4O2, resulting in a KeyError for ‘C2H4O2’. Interestingly, the error does not occur if either of these chemicals is removed from the script provided below.

To Reproduce

The following Python script can be used to reproduce the error:

from biosteam import units, settings, Stream import thermosteam as tmo

ID = ['acetic acid', 'formic acid', 'methanol', 'ethanol', 'methanediol', 'hydroxyacetone', 'leucoglucosan', 'glycolaldehyde', 'lactic acid', 'acetone', 'phenol', 'D-Glucose', 'sucrose', 'water']

chemicals = tmo.Chemicals(ID, cache=False) settings.set_thermo(chemicals)

feed = Stream('feed', T=273.15+25, P=100000, units='kg/hr', C2H4O2=6.59360730593607, CH2O2=2.08219178082192, CH4O=0.607305936073059, C2H6O=0.17351598173516, CH4O2=1.04109589041096, C3H6O2=3.81735159817352, C6H10O5=5.86027397260274, glycolaldehyde=3.96986301369863, C3H6O3=0.913698630136986, C3H6O=0.504109589041096, C6H6O=0.148082191780822, C6H12O6=3.05616438356164, C12H22O11=17.0452054794521, H2O=54.2465753424658)

A = feed.get_flow(units='kg/hr')

Expected behavior It is expected that creation of streams with isomers should be possible.

Actual behavior It is not possible to create streams with isomers using the above method.

Version

Additional context

Encountered error:

Traceback (most recent call last): File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam_chemicals.py", line 1073, in _get_index_and_kind return index_cache[key]


KeyError: ('C2H4O2', 'CH2O2', 'CH4O', 'C2H6O', 'CH4O2', 'C3H6O2', 'C6H10O5', 'glycolaldehyde', 'C3H6O3', 'C3H6O', 'C6H6O', 'C6H12O6', 'C12H22O11', 'H2O')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam\_chemicals.py", line 989, in indices
    return [dct[i] for i in IDs]
            ~~~^^^
KeyError: 'C2H4O2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\AliNazariKhoorasgani\OneDrive - BTG BioLiquids BV\Desktop\PS (evaluate 1st evaporator)\PS+WO mixture (1st Evaporator)\2 stage condensation.py", line 88, in <module>
    feed = Stream('feed',T=273.15+50,P=10000,units='kg/hr',
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam\_stream.py", line 318, in __init__
    self._init_indexer(flow, phase, chemicals, chemical_flows)
  File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam\_stream.py", line 716, in _init_indexer
    imol = indexer.ChemicalMolarFlowIndexer(phase, chemicals=chemicals, **chemical_flows)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam\indexer.py", line 515, in __new__
    self[IDs] = values
    ~~~~^^^^^
  File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam\indexer.py", line 542, in __setitem__
    self.data, *self._chemicals._get_index_and_kind(key), 
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam\_chemicals.py", line 1086, in _get_index_and_kind
    index = self.indices(key)
            ^^^^^^^^^^^^^^^^^
  File "C:\Users\AliNazariKhoorasgani\AppData\Local\Programs\Python\Python312\Lib\site-packages\thermosteam\_chemicals.py", line 991, in indices
    raise UndefinedChemicalAlias(key_error.args[0])
thermosteam.exceptions.UndefinedChemicalAlias: 'C2H4O2'
yoelcortes commented 5 months ago

@Ali-Nazarik, the aliases are set when compiling chemicals in set_thermo. Any conflicting aliases are removed to avoid any ambiguity (so that BioSTEAM only does what can be expected). You can modify aliases after set_thermo as follows:

from biosteam import units, settings, Stream
import thermosteam as tmo

IDs = ['acetic acid', 'formic acid', 'methanol', 'ethanol', 'methanediol',
'hydroxyacetone', 'leucoglucosan', 'glycolaldehyde', 'lactic acid',
'acetone', 'phenol', 'D-Glucose', 'sucrose', 'water']

chemicals = tmo.Chemicals(IDs, cache=False)
settings.set_thermo(chemicals)
chemicals.set_alias('acetic acid', chemicals['acetic acid'].formula) # Set alias after set_thermo
feed = Stream('feed', T=273.15+25, P=100000, units='kg/hr',
C2H4O2=6.59360730593607, CH2O2=2.08219178082192,
CH4O=0.607305936073059, C2H6O=0.17351598173516,
CH4O2=1.04109589041096, C3H6O2=3.81735159817352,
C6H10O5=5.86027397260274, glycolaldehyde=3.96986301369863,
C3H6O3=0.913698630136986, C3H6O=0.504109589041096,
C6H6O=0.148082191780822, C6H12O6=3.05616438356164,
C12H22O11=17.0452054794521, H2O=54.2465753424658)

A = feed.get_flow(units='kg/hr')

But I wouldn't recommend using the formula as an alias for isomers because it can be confusing for bigger projects. Thanks,