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 7cbfbf7 commit eae900d
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 25 deletions.
6 changes: 6 additions & 0 deletions biosteam/_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ def __init__(self,
self._set_facility_recycle(facility_recycle)
self._register(ID)
self._save_configuration()
self._load_stream_links()
self._state = None
self._state_idx = None
self._state_header = None
Expand Down Expand Up @@ -1201,6 +1202,7 @@ def __exit__(self, type, exception, traceback):
raise RuntimeError('system cannot be modified before exiting `with` statement')
else:
self.update_configuration(dump)
self._load_stream_links()
self.set_tolerance(
algorithm=self._algorithm,
method=self._method,
Expand Down Expand Up @@ -1346,6 +1348,9 @@ def set_tolerance(self, mol: Optional[float]=None, rmol: Optional[float]=None,
set_outlet = MockSystem.set_outlet
_load_flowsheet = MockSystem._load_flowsheet

def _load_stream_links(self):
for u in self.units: u._load_stream_links()

@property
def TEA(self) -> TEA:
"""TEA object linked to the system."""
Expand Down Expand Up @@ -2334,6 +2339,7 @@ def _setup(self, update_configuration=False, units=None, load_configuration=True
self._setup_units()
self._remove_temporary_units()
self._save_configuration()
self._load_stream_links()
else:
if load_configuration: self._load_configuration()
self._create_temporary_connections()
Expand Down
8 changes: 8 additions & 0 deletions biosteam/_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ def __init_subclass__(cls,
#: flexible operation (e.g., filtration membranes).
_materials_and_maintenance: frozenset[str] = frozenset()

#: **class-attribute** Wether to link inlet and outlet streams.
_link_streams: bool = False

#: **class-attribute** Lifetime of equipment. Defaults to lifetime of
#: production venture. Use an integer to specify the lifetime for all
#: items in the unit purchase costs. Use a dictionary to specify the
Expand Down Expand Up @@ -317,6 +320,7 @@ def _create_material_balance_equations(self, composition_sensitive):
list[tuple[dict, array]] Create material balance equations for
phenomena-oriented simulation.
"""
if self._link_streams: return []
fresh_inlets, process_inlets, equations = self._begin_equations(composition_sensitive)
outs = self.flat_outs
N = self.chemicals.size
Expand Down Expand Up @@ -1153,6 +1157,9 @@ def reset_cache(self, isdynamic=None):
def _get_design_info(self):
return ()

def _load_stream_links(self):
if self._link_streams: self._outs[0].link_with(self._ins[0])

def _reevaluate(self):
"""Reevaluate design/cost/LCA results."""
self._setup()
Expand Down Expand Up @@ -1294,6 +1301,7 @@ def simulate(self,
self._check_setup()
if run is None or run:
for ps in self._specifications: ps.compile_path(self)
self._load_stream_links()
self.run()
self._summary(design_kwargs, cost_kwargs)

Expand Down
2 changes: 2 additions & 0 deletions biosteam/units/_duplicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ def _load_stream_links(self):
duplicated_outs = self.outs
for stream in duplicated_outs:
stream.link_with(feed)

def _run(self): pass
6 changes: 5 additions & 1 deletion biosteam/units/_flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,11 @@ def _run(self):
elif V > 1:
vapor.imol[chemical_ID] = f
liquid.imol[chemical_ID] = 0
vapor.H = H
try:
vapor.H = H
except RuntimeError: # Extrapolation failed
vapor.phase = 'g'
vapor.T = vapor.chemicals[chemical_ID].Tc
else:
vapor.imol[chemical_ID] = f * V
liquid.imol[chemical_ID] = (1 - V) * f
Expand Down
11 changes: 4 additions & 7 deletions biosteam/units/aerated_bioreactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,7 @@ def load_flow_rates(F_feeds):
vent.empty()
self._run_vent(vent, effluent)

try:
baseline_feed = bst.Stream.sum(self.normal_gas_feeds, energy_balance=False)
except:
breakpoint()
bst.Stream.sum(self.normal_gas_feeds, energy_balance=False)
baseline_feed = bst.Stream.sum(self.normal_gas_feeds, energy_balance=False)
baseline_flows = baseline_feed.get_flow('mol/s', self.gas_substrates)
bounds = np.array([[max(1.01 * SURs[i] - baseline_flows[i], 0), 10 * SURs[i]] for i in index])
if self.optimize_power:
Expand All @@ -614,8 +610,9 @@ def gas_flow_rate_objective(F_substrates):
mask = STRs - F_ins > 0
STRs[mask] = F_ins[mask]
diff = SURs - STRs
diff[diff > 0] *= 1e6 # Force transfer rate to meet uptake rate
return (diff * diff).sum()
diff[diff > 0] *= 1e3 # Force transfer rate to meet uptake rate
SE = (diff * diff).sum()
return SE

f = gas_flow_rate_objective
with catch_warnings():
Expand Down
6 changes: 3 additions & 3 deletions biosteam/units/heat_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,9 @@ def total_heat_transfer(self):
Q = total_heat_transfer # Alias for backward compatibility

def simulate_as_auxiliary_exchanger(self,
ins, outs=None, duty=None, vle=True, scale=None, hxn_ok=True,
P_in=None, P_out=None, update=False,
):
ins, outs=None, duty=None, vle=True, scale=None, hxn_ok=True,
P_in=None, P_out=None, update=False,
):
inlet = self.ins[0]
outlet = self.outs[0]
if not inlet:
Expand Down
21 changes: 12 additions & 9 deletions biosteam/utils/stream_link_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,32 @@
# for license details.
"""
"""
from collections import namedtuple

__all__ = ('static',
'static_flow_and_phase')

# %% Linking options


def _run_static(self):
self._outs[0].copy_like(self._ins[0])

def _run_static_flow_and_phase(self):
def static(cls=None):
if cls is None: return lambda cls: static(cls)
cls._link_streams = True
cls._run = _run_static
return cls

def _run_static_flow(self):
outlet = self._outs[0]
inlet = self._ins[0]
outlet.phases = inlet.phases
outlet.copy_flow(inlet)

def static(cls=None):
if cls is None: return lambda cls: static(cls)
if '_run' not in cls.__dict__:
cls._run = _run_static
return cls

def static_flow_and_phase(cls):
cls._N_ins = cls._N_outs = 1
if '_run' not in cls.__dict__:
cls._run = _run_static_flow_and_phase
if '_run' not in cls.__dict__: cls._run = cls._run_static_flow
return cls

del namedtuple
11 changes: 7 additions & 4 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ def adjust_flow_rate():
H1.add_specification(lambda: None, run=True, impacted_units=[T2])
H2.add_specification(lambda: None, run=True, impacted_units=[T1])
sys.simulate()
assert sys.unit_path.index(H1) < sys.unit_path.index(T2)
assert H1.specifications[0].path == []
assert H2.specifications[0].path == [T1, H1, T2]
# Net simulation order is ..., T2, H2, T1, H1, T2, H2, ...
if sys.unit_path.index(H1) < sys.unit_path.index(T2):
assert H1.specifications[0].path == []
assert H2.specifications[0].path == [T1, H1, T2]
# Net simulation order is ..., T2, H2, T1, H1, T2, H2, ...
else:
assert H1.specifications[0].path == [T2, H2, T1]
assert H2.specifications[0].path == []

def test_process_specifications_with_recycles():
bst.F.set_flowsheet('bifurcated_recycle_loops')
Expand Down
2 changes: 1 addition & 1 deletion thermosteam

0 comments on commit eae900d

Please sign in to comment.