Skip to content

Commit

Permalink
improvement to PO-sim
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelcortes committed Jan 30, 2024
1 parent b35679c commit 391ffa4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
33 changes: 19 additions & 14 deletions biosteam/_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,29 @@ def solve(self):
values.append(value)
return objs, np.array(values)
A, objs = dictionaries2array(self.A)
b = np.array(b).T
if A.ndim == 2:
if A.ndim == 3:
A_ = A
b_ = np.array(b).T
objs_ = objs
values = []
for A, b in zip(A_, b_):
rows = A.any(axis=0)
cols = A.any(axis=1)
b = [j for (i, j) in zip(rows, b)]
A = A[rows][:, cols]
objs = [j for (i, j) in zip(cols, objs_) if i]
values.append(solve(A, b).T)
values = np.array(values).T
else:
rows = A.any(axis=0)
cols = A.any(axis=1)
b = np.array(b).T
b = [j for (i, j) in zip(rows, b)]
A = A[rows][:, cols]
objs = [j for (i, j) in zip(cols, objs) if i]
else:
# TODO: A.ndim == 3
pass
try:
values = solve(A, b).T
except Exception as e:
# for i in self.A:
# print('--')
# for i, j in i.items():
# print(i.ID, j)
raise e
if np.isnan(values).any():
raise RuntimeError('nan value in variables')
for obj, value in zip(objs, values):
obj._update_decoupled_variable(variable, value)
if variable in ('mol', 'mol-LLE'):
Expand Down Expand Up @@ -685,7 +690,7 @@ class System:
available_methods: Methods[str, tuple[Callable, bool, dict]] = Methods()

#: Variable solution priority for phenomena oriented simulation.
variable_priority: list[str] = ['mol', ('mol-LLE', 'K-pseudo'), 'K', 'T', 'L', 'B']
variable_priority: list[str] = ['mol', ('mol-LLE', 'K-pseudo'), 'K', 'B', 'mol', 'T', 'L']

@classmethod
def register_method(cls, name, solver, conditional=False, **kwargs):
Expand Down Expand Up @@ -2222,7 +2227,7 @@ def run(self):
self.run_phenomena()
except:
warn('phenomena-oriented simulation failed; '
'attempting one sequential-modular loop', RuntimeWarning)
'attempting one sequential-modular loop', RuntimeWarning)
for i in self.unit_path: i.run()
else:
raise RuntimeError(f'unknown algorithm {algorithm!r}')
Expand Down
4 changes: 2 additions & 2 deletions biosteam/units/phase_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def _get_arrays(self):

def _set_arrays(self, IDs, **kwargs):
IDs_last = self.IDs
if IDs_last and IDs_last != IDs:
if IDs_last and IDs_last != IDs and len(IDs_last) > len(IDs):
size = len(IDs_last)
index = [IDs_last.index(i) for i in IDs]
for name, array in kwargs.items():
Expand Down Expand Up @@ -668,7 +668,7 @@ def _run_vle(self, P=None, update=True):
y_mol = 0
K_new = y_mol / x_mol
if B is None:
if V_total and not L_total:
if not L_total:
self.B = inf
else:
self.B = V_total / L_total
Expand Down
3 changes: 2 additions & 1 deletion tests/test_phenomena_oriented_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def fresh_solvent_flow_rate():
for i in range(1): sm_sys.simulate()
t1 = time.toc()

assert t0 < 0.75 * t1, 'phenomena-oriented simulation speed should be faster than sequential-modular'
assert t0 < 0.2 * t1, 'phenomena-oriented simulation speed should be faster than sequential-modular'

for s_sm, s_dp in zip(sm_sys.streams, dp_sys.streams):
actual = s_sm.mol
Expand Down Expand Up @@ -296,6 +296,7 @@ def adjust_fresh_solvent_flow_rate():

time.tic()
sm = create_system('sequential modular')
sm.flatten()
sm.set_tolerance(rmol=1e-6, mol=1e-6, subsystems=True, method='fixed-point')
sm.simulate()
t_sequential = time.toc()
Expand Down

0 comments on commit 391ffa4

Please sign in to comment.