Skip to content

Commit

Permalink
allow nested chemical groups
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelcortes committed Oct 18, 2024
1 parent 7b7a8ea commit 69ec92b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
7 changes: 7 additions & 0 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ def test_stream_indexing():
key = ('l', 'Water')
stream.set_flow(1, 'gpm', key)
assert stream.get_flow('gpm', key) == 1.

tmo.settings.chemicals.define_group('Mixture', ['Water', 'Alcohol'], [0.5, 0.5], wt=False)
stream = tmo.Stream(None, Mixture=1.)
assert stream.imol['Alcohol'] == 0.5
assert stream.imol['Water'] == 0.5
assert stream.imol['Ethanol'] == 0.25
assert stream.imol['Methanol'] == 0.25

def test_stream_property_cache():
tmo.settings.set_thermo(['Water', 'Ethanol'], cache=True)
Expand Down
17 changes: 12 additions & 5 deletions thermosteam/_chemicals.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,20 @@ def define_group(self, name, IDs, composition=None, wt=False):
composition = np.ones(len(IDs))
elif len(composition) != len(IDs):
raise ValueError('length of IDs and composition must be the same')
for i in IDs:
if i in self._group_wt_compositions:
raise ValueError(f"'{i}' is a group; cannot define new group using other groups")
index = self.indices(IDs)
new_IDs = []
new_composition = []
group_compositions = self._group_wt_compositions if wt else self._group_mol_compositions
for n, i in enumerate(IDs):
if i in group_compositions:
new_IDs.extend([j.ID for j in self[i]])
new_composition.extend(composition[n] * group_compositions[i])
else:
new_IDs.append(i)
new_composition.append(composition[n])
index = self.indices(new_IDs)
self.__dict__[name] = [self.tuple[i] for i in index]
self._index[name] = index
composition = np.asarray(composition, float)
composition = np.asarray(new_composition, float)
if wt:
composition_wt = composition
composition_mol = composition / self.MW[index]
Expand Down

0 comments on commit 69ec92b

Please sign in to comment.