Skip to content

Commit

Permalink
finish new indexer internals
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelcortes committed Nov 11, 2024
1 parent e1a9ef5 commit f35b714
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
81 changes: 78 additions & 3 deletions thermosteam/_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def __init__(self, stream, flow, T, P, phase):

def __enter__(self):
stream = self.stream
self.data = stream.get_data()
if self.flow is not None: stream.imol.data[:] = self.flow
if self.T is not None: stream.T = self.T
if self.P is not None: stream.P = self.P
Expand Down Expand Up @@ -1636,7 +1635,7 @@ def copy_thermal_condition(self, other):
def copy_phase(self, other):
"""Copy phase from another stream."""
try:
self._imol._phase = other._imol._phase
self._imol.phase = other._imol._phase
except AttributeError as e:
if isinstance(other, tmo.MultiStream):
raise ValueError('cannot copy phase from stream with multiple phases')
Expand Down Expand Up @@ -1826,15 +1825,91 @@ def copy(self, ID=None, thermo=None):
new.reset_cache()
new.price = 0
new.ID = ID
return new
return new
__copy__ = copy

def link_with(self, other: Stream):
"""
Link with another stream.
Parameters
----------
other :
See Also
--------
:obj:`~Stream.flow_proxy`
:obj:`~Stream.proxy`
Examples
--------
>>> import thermosteam as tmo
>>> tmo.settings.set_thermo(['Water', 'Ethanol'], cache=True)
>>> s1 = tmo.Stream('s1', Water=20, Ethanol=10, units='kg/hr')
>>> s2 = tmo.Stream('s2')
>>> s2.link_with(s1)
>>> s1.mol is s2.mol
True
>>> s2.thermal_condition is s1.thermal_condition
True
>>> s1.phase = 'g'
>>> s2.phase
'g'
"""
if not isinstance(other._imol, self._imol.__class__):
at_unit = f" at unit {self.source}" if self.source is other.sink else ""
raise RuntimeError(f"stream {self} cannot link with stream {other}" + at_unit
+ "; streams must have the same class to link")
self._imol = other._imol
self._thermal_condition = other._thermal_condition

def unlink(self):
"""
Unlink stream from other streams.
Examples
--------
>>> import thermosteam as tmo
>>> tmo.settings.set_thermo(['Water', 'Ethanol'], cache=True)
>>> s1 = tmo.Stream('s1', Water=20, Ethanol=10, units='kg/hr')
>>> s2 = tmo.Stream('s2')
>>> s2.link_with(s1)
>>> s1.unlink()
>>> s2.mol is s1.mol
False
>>> s1.phases = s2.phases = ('l', 'g')
>>> s2.link_with(s1)
>>> s1.imol.data is s2.imol.data
True
>>> s1.unlink()
>>> s1.imol.data is s2.imol.data
False
MultiStream phases cannot be unlinked:
>>> s1 = tmo.MultiStream(None, phases=('l', 'g'))
>>> s1['g'].unlink()
Traceback (most recent call last):
RuntimeError: phase is locked; stream cannot be unlinked
"""
imol = self._imol
if hasattr(imol, '_phase'):
if imol._lock_phase:
raise RuntimeError('phase is locked; stream cannot be unlinked')
self._imol = imol.full_copy()
self._thermal_condition = self._thermal_condition.copy()
self.reset_cache()

def flow_proxy(self, ID=None):
"""
Return a new stream that shares flow rate data with this one.
See Also
--------
:obj:`~Stream.link_with`
:obj:`~Stream.proxy`
Examples
Expand Down
8 changes: 4 additions & 4 deletions thermosteam/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
def set_main_phase(main_indexer, indexers):
other_indexer, *indexers = indexers
try:
phase = other_indexer._phase.phase
phase = other_indexer._phase
for i in indexers:
if phase != i._phase.phase: return
if phase != i._phase: return
main_indexer.phase = phase
except: pass

Expand Down Expand Up @@ -1036,10 +1036,10 @@ def to_chemical_indexer(self, phase):
dct = sum_sparse_vectors(rows)
data.dct.clear()
data.dct.update(dct)
return self._ChemicalIndexer.blank(phase, self._chemicals, self._parent)
return self._ChemicalIndexer.blank(phase, self._chemicals, self._parent or self)

def to_material_indexer(self, phases):
material_indexer = self.__class__.blank(phases, self._chemicals, self._parent)
material_indexer = self.__class__.blank(phases, self._chemicals, self._parent or self)
index = material_indexer._phase_indexer
rows = material_indexer.data.rows
for phase, data in self:
Expand Down

0 comments on commit f35b714

Please sign in to comment.