Skip to content

Commit

Permalink
minor update to adm1 pH solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
joyxyz1994 committed Jun 14, 2024
1 parent 081bf14 commit 48372d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
16 changes: 5 additions & 11 deletions qsdsan/processes/_adm1.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ def solve_pH(state_arr, Ka, unit_conversion):
h = brenth(acid_base_rxn, 1e-14, 1.0,
args=(weak_acids, Ka),
xtol=1e-12, maxiter=100)
nh3 = Ka[1] * weak_acids[2] / (Ka[1] + h)
co2 = weak_acids[3] - Ka[2] * weak_acids[3] / (Ka[2] + h)
return h, nh3, co2

return h
rhos_adm1 = lambda state_arr, params: _rhos_adm1(state_arr, params, h=None)

def _rhos_adm1(state_arr, params, h=None):
Expand Down Expand Up @@ -278,13 +275,10 @@ def _rhos_adm1(state_arr, params, h=None):
if S_va > 0: rhos[7] *= 1/(1+S_bu/S_va)
if S_bu > 0: rhos[8] *= 1/(1+S_va/S_bu)

if h is None:
h, nh3, co2 = solve_pH(state_arr, Ka, unit_conversion)
else:
nh3 = Ka[1] * S_IN * unit_conversion[10] / (Ka[1] + h)
S_IC = state_arr[9] * unit_conversion[9]
co2 = S_IC - Ka[2] * S_IC / (Ka[2] + h)
biogas_S[-1] = co2 / unit_conversion[9]
if h is None: h = solve_pH(state_arr, Ka, unit_conversion)
nh3 = S_IN * unit_conversion[10] * Ka[1] / (Ka[1] + h)
co2 = state_arr[9] * h / (Ka[2] + h)
biogas_S[-1] = co2

Iph = Hill_inhibit(h, pH_ULs, pH_LLs)
Iin = substr_inhibit(S_IN, KS_IN)
Expand Down
15 changes: 5 additions & 10 deletions qsdsan/processes/_adm1_p_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ def solve_pH(state_arr, Ka, unit_conversion):
h = brenth(acid_base_rxn, 1e-14, 1.0,
args=(weak_acids, Ka),
xtol=1e-12, maxiter=100)
nh3 = Ka[1] * weak_acids[4] / (Ka[1] + h)
co2 = weak_acids[6] - Ka[3] * weak_acids[6] / (Ka[3] + h)
return h, nh3, co2
return h

rhos_adm1_p_extension = lambda state_arr, params: _rhos_adm1_p_extension(state_arr, params, h=None)

Expand Down Expand Up @@ -219,13 +217,10 @@ def _rhos_adm1_p_extension(state_arr, params, h=None):
biogas_S = state_arr[7:10].copy()
biogas_p = R * T_op * state_arr[34:37]

if h is None:
h, nh3, co2 = solve_pH(state_arr, Ka, unit_conversion)
else:
nh3 = Ka[1] * S_IN * unit_conversion[10] / (Ka[1] + h)
S_IC = state_arr[9] * unit_conversion[9]
co2 = S_IC - Ka[3] * S_IC / (Ka[3] + h)
biogas_S[-1] = co2 / unit_conversion[9]
if h is None: h = solve_pH(state_arr, Ka, unit_conversion)
nh3 = S_IN * unit_conversion[10] * Ka[1] / (Ka[1] + h)
co2 = state_arr[9] * h / (Ka[3] + h)
biogas_S[-1] = co2

Iph = Hill_inhibit(h, pH_ULs, pH_LLs)
Iin = substr_inhibit(S_IN, KS_IN)
Expand Down
2 changes: 1 addition & 1 deletion qsdsan/sanunits/_anaerobic_reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def h2_stoichio(state_arr):
grad_dydt_Sh2_AD = self.model.grad_dydt_Sh2_AD
def solve_h2(QC, S_in, T):
Ka = params['Ka_base'] * T_correction_factor(params['T_base'], T, params['Ka_dH'])
h, nh3, co2 = solve_pH(QC, Ka, unit_conversion)
h = solve_pH(QC, Ka, unit_conversion)
S_h2_0 = QC[h2_idx]
S_h2_in = S_in[h2_idx]
S_h2 = newton(dydt_Sh2_AD, S_h2_0, grad_dydt_Sh2_AD,
Expand Down

0 comments on commit 48372d2

Please sign in to comment.