From 235025f2df40e3f9ad2de7c0c40ee340b0d9129c Mon Sep 17 00:00:00 2001 From: Joy Zhang Date: Wed, 21 Feb 2024 13:51:52 -0800 Subject: [PATCH] attempt to reduce rounding error --- qsdsan/_process.py | 5 ++++- qsdsan/processes/_adm1.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/qsdsan/_process.py b/qsdsan/_process.py index d795d362..10f07817 100644 --- a/qsdsan/_process.py +++ b/qsdsan/_process.py @@ -1192,7 +1192,10 @@ def stoichiometry(self): stoichio_vals = [] for row in stoichio: stoichio_vals.append([v.subs(v_params) if not isa(v, (float, int)) else v for v in row]) - try: stoichio_vals = np.asarray(stoichio_vals, dtype=float) + try: + stoichio_vals = np.asarray(stoichio_vals, dtype=float) + #!!! round-off error + stoichio_vals[np.abs(stoichio_vals) < 2.22044604925e-16] = 0.0 except TypeError: pass return pd.DataFrame(stoichio_vals, index=self.IDs, columns=self._components.IDs) else: return pd.DataFrame(stoichio, index=self.IDs, columns=self._components.IDs) diff --git a/qsdsan/processes/_adm1.py b/qsdsan/processes/_adm1.py index 161f9cfb..4ed7ca58 100644 --- a/qsdsan/processes/_adm1.py +++ b/qsdsan/processes/_adm1.py @@ -569,9 +569,9 @@ def __new__(cls, components=None, path=None, N_xc=2.686e-3, N_I=4.286e-3, N_aa=7 **kwargs): cmps = _load_components(components) - cmps.X_c.i_N = N_xc * N_mw - cmps.X_I.i_N = cmps.S_I.i_N = N_I * N_mw - cmps.S_aa.i_N = cmps.X_pr.i_N = N_aa * N_mw + cmps.X_c.i_N = round(N_xc * N_mw, 4) + cmps.X_I.i_N = cmps.S_I.i_N = round(N_I * N_mw, 4) + cmps.S_aa.i_N = cmps.X_pr.i_N = round(N_aa * N_mw, 4) if not path: path = _path self = Processes.load_from_file(path, @@ -591,11 +591,11 @@ def __new__(cls, components=None, path=None, N_xc=2.686e-3, N_I=4.286e-3, N_aa=7 self.extend(gas_transfer) self.compile(to_class=cls) - stoichio_vals = (f_ch_xc, f_pr_xc, f_li_xc, f_xI_xc, 1-f_ch_xc-f_pr_xc-f_li_xc-f_xI_xc, - f_fa_li, f_bu_su, f_pro_su, f_ac_su, 1-f_bu_su-f_pro_su-f_ac_su, - f_va_aa, f_bu_aa, f_pro_aa, f_ac_aa, 1-f_va_aa-f_bu_aa-f_pro_aa-f_ac_aa, - f_ac_fa, 1-f_ac_fa, f_pro_va, f_ac_va, 1-f_pro_va-f_ac_va, - f_ac_bu, 1-f_ac_bu, f_ac_pro, 1-f_ac_pro, + stoichio_vals = (f_ch_xc, f_pr_xc, f_li_xc, f_xI_xc, 1.0-f_ch_xc-f_pr_xc-f_li_xc-f_xI_xc, + f_fa_li, f_bu_su, f_pro_su, f_ac_su, 1.0-f_bu_su-f_pro_su-f_ac_su, + f_va_aa, f_bu_aa, f_pro_aa, f_ac_aa, 1.0-f_va_aa-f_bu_aa-f_pro_aa-f_ac_aa, + f_ac_fa, 1.0-f_ac_fa, f_pro_va, f_ac_va, 1.0-f_pro_va-f_ac_va, + f_ac_bu, 1.0-f_ac_bu, f_ac_pro, 1.0-f_ac_pro, Y_su, Y_aa, Y_fa, Y_c4, Y_pro, Y_ac, Y_h2) pH_LLs = np.array([pH_limits_aa[0]]*6 + [pH_limits_ac[0], pH_limits_h2[0]]) pH_ULs = np.array([pH_limits_aa[1]]*6 + [pH_limits_ac[1], pH_limits_h2[1]])