169 lines
8.2 KiB
Python
169 lines
8.2 KiB
Python
from pathlib import Path
|
||||
|
|
p = Path('/root/dsh_workspace/cloudlab-platform/runner/run_experiment.py')
|
|||
|
|
s = p.read_text(encoding='utf-8')
|
|||
|
|
|
|||
|
|
anchor = "def monod_steady(D, Sin, mueff, Ks, Y):"
|
|||
|
|
assert s.count(anchor) == 1
|
|||
|
|
batch_solver = '''def batch_depletion_time(points, Sin, threshold_frac=1e-6):
|
|||
|
|
"""First time the sampled batch substrate falls below a small threshold.
|
|||
|
|
|
|||
|
|
The time is linearly interpolated between two 0.5 h output samples. It is a
|
|||
|
|
numerical diagnostic, not a measured depletion time.
|
|||
|
|
"""
|
|||
|
|
threshold = max(1e-9, float(Sin) * threshold_frac)
|
|||
|
|
for i in range(1, len(points)):
|
|||
|
|
s0 = points[i - 1][2]
|
|||
|
|
s1 = points[i][2]
|
|||
|
|
t0 = points[i - 1][0]
|
|||
|
|
t1 = points[i][0]
|
|||
|
|
if s1 <= threshold:
|
|||
|
|
if s0 <= threshold:
|
|||
|
|
return t0
|
|||
|
|
frac = (s0 - threshold) / (s0 - s1) if s0 > s1 else 1.0
|
|||
|
|
return t0 + frac * (t1 - t0)
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
|
|||
|
|
def rk45_batch(Sin, mueff, Ks, Y, hours, X0=0.05, rtol=1e-6, atol=1e-9):
|
|||
|
|
"""Dormand-Prince 5(4) integration of a batch Monod culture.
|
|||
|
|
|
|||
|
|
dX/dt = mu(S) X, dS/dt = -mu(S) X / Y, mu(S)=mueff*S/(Ks+S).
|
|||
|
|
There is no dilution, feed, maintenance, death, product formation or
|
|||
|
|
inhibition in this model.
|
|||
|
|
"""
|
|||
|
|
def f(y):
|
|||
|
|
X, S = y
|
|||
|
|
mu = mueff * S / (Ks + S) if S > 0 else 0.0
|
|||
|
|
return [X * mu, -X * mu / Y]
|
|||
|
|
|
|||
|
|
def stage(y, h, ks, coeffs):
|
|||
|
|
return [y[i] + h * sum(c * k[i] for c, k in zip(coeffs, ks)) for i in range(2)]
|
|||
|
|
|
|||
|
|
b5 = [35 / 384, 0, 500 / 1113, 125 / 192, -2187 / 6784, 11 / 84, 0]
|
|||
|
|
b4 = [5179 / 57600, 0, 7571 / 16695, 393 / 640, -92097 / 339200, 187 / 2100, 1 / 40]
|
|||
|
|
y = [float(X0), float(Sin)]
|
|||
|
|
t = 0.0
|
|||
|
|
h = 0.05
|
|||
|
|
points = [[0.0, round(y[0], 6), round(y[1], 6)]]
|
|||
|
|
next_out = 0.5
|
|||
|
|
accepted = rejected = 0
|
|||
|
|
max_rel = 0.0
|
|||
|
|
while t < hours - 1e-12:
|
|||
|
|
h = min(h, hours - t)
|
|||
|
|
y_old = y[:]
|
|||
|
|
t_old = t
|
|||
|
|
k1 = f(y)
|
|||
|
|
k2 = f(stage(y, h, [k1], [1 / 5]))
|
|||
|
|
k3 = f(stage(y, h, [k1, k2], [3 / 40, 9 / 40]))
|
|||
|
|
k4 = f(stage(y, h, [k1, k2, k3], [44 / 45, -56 / 15, 32 / 9]))
|
|||
|
|
k5 = f(stage(y, h, [k1, k2, k3, k4], [19372 / 6561, -25360 / 2187, 64448 / 6561, -212 / 729]))
|
|||
|
|
k6 = f(stage(y, h, [k1, k2, k3, k4, k5], [9017 / 3168, -355 / 33, 46732 / 5247, 49 / 176, -5103 / 18656]))
|
|||
|
|
k7 = f(stage(y, h, [k1, k2, k3, k4, k5, k6], [35 / 384, 0, 500 / 1113, 125 / 192, -2187 / 6784, 11 / 84]))
|
|||
|
|
y5 = stage(y, h, [k1, k2, k3, k4, k5, k6, k7], b5)
|
|||
|
|
y4 = stage(y, h, [k1, k2, k3, k4, k5, k6, k7], b4)
|
|||
|
|
err = max(abs(y5[0] - y4[0]), abs(y5[1] - y4[1]))
|
|||
|
|
scale = atol + rtol * max(abs(y5[0]), abs(y5[1]), 1e-9)
|
|||
|
|
ratio = err / scale if scale else 0.0
|
|||
|
|
if ratio <= 1.0 or h <= 1e-6:
|
|||
|
|
accepted += 1
|
|||
|
|
max_rel = max(max_rel, ratio)
|
|||
|
|
y = [max(0.0, y5[0]), max(0.0, y5[1])]
|
|||
|
|
t = t_old + h
|
|||
|
|
while next_out <= t + 1e-12 and next_out <= hours + 1e-12:
|
|||
|
|
frac = (next_out - t_old) / h if h else 1.0
|
|||
|
|
px = y_old[0] + frac * (y[0] - y_old[0])
|
|||
|
|
ps = y_old[1] + frac * (y[1] - y_old[1])
|
|||
|
|
points.append([round(next_out, 6), round(px, 6), round(ps, 6)])
|
|||
|
|
next_out += 0.5
|
|||
|
|
factor = 5.0 if ratio == 0 else min(5.0, max(0.2, 0.9 * ratio ** -0.2))
|
|||
|
|
else:
|
|||
|
|
rejected += 1
|
|||
|
|
factor = min(1.0, max(0.1, 0.9 * ratio ** -0.2))
|
|||
|
|
h = min(0.5, max(1e-7, h * factor))
|
|||
|
|
if accepted + rejected > 500000:
|
|||
|
|
raise RuntimeError("RK45 超过 500000 步,参数可能过刚")
|
|||
|
|
if points[-1][0] < hours - 1e-9:
|
|||
|
|
points.append([round(hours, 6), round(y[0], 6), round(y[1], 6)])
|
|||
|
|
X, S = y
|
|||
|
|
balance = (X - X0) + (S - Sin) / Y
|
|||
|
|
return points, {"accepted": accepted, "rejected": rejected,
|
|||
|
|
"max_rel_error": max_rel, "mass_balance": balance,
|
|||
|
|
"rtol": rtol, "atol": atol}
|
|||
|
|
|
|||
|
|
|
|||
|
|
'''
|
|||
|
|
s = s.replace(anchor, batch_solver + anchor, 1)
|
|||
|
|
|
|||
|
|
anchor2 = """ mueff = mumax * gamma
|
|||
|
|
base = monod_steady(D, Sin, mueff, Ks, Y)"""
|
|||
|
|
assert s.count(anchor2) == 1
|
|||
|
|
batch_branch = ''' mode = str(params.get("mode", "恒化培养")).strip()
|
|||
|
|
if mode in ("chemostat", "恒化培养"):
|
|||
|
|
mode = "chemostat"
|
|||
|
|
elif mode in ("batch", "分批培养"):
|
|||
|
|
mode = "batch"
|
|||
|
|
else:
|
|||
|
|
raise ValueError("未知培养模式: %s" % mode)
|
|||
|
|
mueff = mumax * gamma
|
|||
|
|
if mode == "batch":
|
|||
|
|
X0 = 0.05
|
|||
|
|
points, diag = rk45_batch(Sin, mueff, Ks, Y, hours, X0=X0)
|
|||
|
|
X, S = points[-1][1], points[-1][2]
|
|||
|
|
mu0 = mueff * Sin / (Ks + Sin) if Sin > 0 else 0.0
|
|||
|
|
t_dep = batch_depletion_time(points, Sin)
|
|||
|
|
upper = X0 + Y * Sin
|
|||
|
|
balance = (X - X0) + (S - Sin) / Y
|
|||
|
|
threshold = max(1e-9, Sin * 1e-6)
|
|||
|
|
status = "底物耗尽" if S <= threshold else "仍在消耗"
|
|||
|
|
metrics = [
|
|||
|
|
metric("模式", "分批"),
|
|||
|
|
metric("γ(T,pH)", "%.3f" % gamma),
|
|||
|
|
metric("γ 温度项", "%.4f" % gamma_t),
|
|||
|
|
metric("γ pH 项", "%.4f" % gamma_p),
|
|||
|
|
metric("μeff", "%.3f" % mueff, "/h"),
|
|||
|
|
metric("初始比生长速率 μ0", "%.4f" % mu0, "/h"),
|
|||
|
|
metric("初始生物量 X0", "%.3f" % X0, "g/L"),
|
|||
|
|
metric("初始底物 S0", "%.3f" % Sin, "g/L"),
|
|||
|
|
metric("%g h 生物量" % hours, "%.3f" % X, "g/L"),
|
|||
|
|
metric("%g h 底物" % hours, "%.4f" % S, "g/L"),
|
|||
|
|
metric("底物耗尽时间", ("%.2f" % t_dep) if t_dep is not None else "未耗尽", "h"),
|
|||
|
|
metric("最终生物量上界", "%.3f" % upper, "g/L"),
|
|||
|
|
metric("质量平衡残差(分批)", "%.2e" % balance, "g/L"),
|
|||
|
|
metric("RK45 接受步", diag["accepted"], "步"),
|
|||
|
|
metric("RK45 拒绝步", diag["rejected"], "步"),
|
|||
|
|
metric("最大局部误差/容差", "%.2f" % diag["max_rel_error"]),
|
|||
|
|
metric("状态", status),
|
|||
|
|
]
|
|||
|
|
tr = [[p[0], p[1], p[2]] for p in points[::max(1, len(points) // 24)]]
|
|||
|
|
rows = [
|
|||
|
|
["初始生物量 X0 (g/L)", "%.6f" % X0],
|
|||
|
|
["初始底物 S0 (g/L)", "%.6f" % Sin],
|
|||
|
|
["结束生物量 X (g/L)", "%.6f" % X],
|
|||
|
|
["结束底物 S (g/L)", "%.6f" % S],
|
|||
|
|
["生物量增量 ΔX (g/L)", "%.6f" % (X - X0)],
|
|||
|
|
["底物消耗 / Y (g/L)", "%.6f" % ((Sin - S) / Y)],
|
|||
|
|
["质量平衡 ΔX − (−ΔS/Y) (g/L)", "%.2e" % balance],
|
|||
|
|
["理论上界 X0 + Y·S0 (g/L)", "%.6f" % upper],
|
|||
|
|
["底物耗尽时间 (h)", ("%.3f" % t_dep) if t_dep is not None else "未耗尽"],
|
|||
|
|
]
|
|||
|
|
tables = [table("时间序列(抽样)", ["t (h)", "X (g/L)", "S (g/L)"], tr),
|
|||
|
|
table("分批物料衡算", ["项目", "值"], rows,
|
|||
|
|
"质量平衡是数值积分自洽性检查;底物耗尽时间由 0.5 h 采样点线性插值。")]
|
|||
|
|
series = [{"title": "分批培养 X(t) 与 S(t)", "xLabel": "时间 (h)", "yLabel": "g/L",
|
|||
|
|
"lines": [{"name": "X", "points": [[p[0], p[1]] for p in points]},
|
|||
|
|
{"name": "S", "points": [[p[0], p[2]] for p in points]}]}]
|
|||
|
|
finish("Monod + RK45 分批培养:Sin=%.3f g/L,T=%.1f ℃,pH=%.1f,γ=%.3f;%g h 生物量 %.3f g/L、底物 %.4f g/L;质量平衡残差 %.2e g/L;%s。" %
|
|||
|
|
(Sin, T, pH, gamma, hours, X, S, balance, status),
|
|||
|
|
metrics, tables, series,
|
|||
|
|
["分批模式:dX/dt=μ(S)X、dS/dt=−μ(S)X/Y;没有稀释、补料、维持能、细胞死亡、产物生成或底物/产物抑制。",
|
|||
|
|
"初始生物量 X0=0.05 g/L 是固定初值,不是实测接种量;μmax 绝对值仍是教学占位。",
|
|||
|
|
"质量平衡是数值积分自洽性检查,不是实验物料衡算;底物耗尽时间由 0.5 h 采样点线性插值,分辨率有限。",
|
|||
|
|
"γ(T,pH)=γT·γpH,由 env.cards 的 Rosso CTMI 计算;边界点 γ=0,不做外推。",
|
|||
|
|
"RK45 使用 Dormand-Prince 5(4),rtol=1e-6、atol=1e-9;分批模式没有解析稳态对照。"],
|
|||
|
|
[{"label": "环境网格", "path": "AtomLab/data/进化实验台.json"}])
|
|||
|
|
return
|
|||
|
|
base = monod_steady(D, Sin, mueff, Ks, Y)'''
|
|||
|
|
s = s.replace(anchor2, batch_branch, 1)
|
|||
|
|
p.write_text(s, encoding='utf-8')
|
|||
|
|
print('patched', len(s))
|