Skip to content

Commit 7460253

Browse files
committed
add test fun for calc_km
1 parent f5b3122 commit 7460253

2 files changed

Lines changed: 90 additions & 15 deletions

File tree

navicat_mikimo/kinetic_solver.py

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def eyring(
3636
"""
3737
if temperature == 0:
3838
raise ValueError("Temperature cannot be zero.")
39-
return kb_h * temperature * np.exp(-np.atleast_1d(dG_ddag) / (R_ * temperature))
39+
return kb_h * temperature * \
40+
np.exp(-np.atleast_1d(dG_ddag) / (R_ * temperature))
4041

4142

4243
def get_dG_ddag(energy_profile, dgr, coeff_TS):
@@ -110,7 +111,8 @@ def get_k(
110111
energy_profile_reverse = energy_profile_reverse[:-1]
111112
energy_profile_reverse = energy_profile_reverse - dgr
112113
energy_profile_reverse = np.insert(energy_profile_reverse, 0, 0)
113-
dG_ddag_reverse = get_dG_ddag(energy_profile_reverse, -dgr, coeff_TS_reverse)
114+
dG_ddag_reverse = get_dG_ddag(
115+
energy_profile_reverse, -dgr, coeff_TS_reverse)
114116

115117
k_forward = eyring(dG_ddag_forward, temperature)
116118
k_reverse = np.flip(eyring(dG_ddag_reverse, temperature))
@@ -144,14 +146,14 @@ def calc_k(
144146
]
145147
if not isinstance(dgr_all, np.ndarray):
146148
dgr_all = np.asarray(dgr_all)
147-
coeff_TS_all = [
148-
np.asarray(e) if not isinstance(e, np.ndarray) else e for e in coeff_TS_all
149-
]
149+
coeff_TS_all = [np.asarray(e) if not isinstance(
150+
e, np.ndarray) else e for e in coeff_TS_all]
150151

151152
k_forward_all = np.empty(0)
152153
k_reverse_all = np.empty(0)
153154

154-
for energy_profile, dgr, coeff_TS in zip(energy_profile_all, dgr_all, coeff_TS_all):
155+
for energy_profile, dgr, coeff_TS in zip(
156+
energy_profile_all, dgr_all, coeff_TS_all):
155157
k_forward, k_reverse = get_k(
156158
energy_profile, dgr, coeff_TS, temperature=temperature
157159
)
@@ -253,8 +255,18 @@ def system_KE_DE(
253255

254256
boundary = np.zeros((initial_conc.shape[0], 2))
255257
TOLERANCE = 1
256-
r_idx = np.where(char.startswith(states, "R") & ~char.startswith(states, "INT"))[0]
257-
p_idx = np.where(char.startswith(states, "P") & ~char.startswith(states, "INT"))[0]
258+
r_idx = np.where(
259+
char.startswith(
260+
states,
261+
"R") & ~char.startswith(
262+
states,
263+
"INT"))[0]
264+
p_idx = np.where(
265+
char.startswith(
266+
states,
267+
"P") & ~char.startswith(
268+
states,
269+
"INT"))[0]
258270
int_idx = np.setdiff1d(
259271
np.arange(1, initial_conc.shape[0]), np.concatenate([r_idx, p_idx])
260272
)
@@ -494,7 +506,8 @@ def calc_km(
494506
)
495507

496508
try:
497-
c_target_t = np.array([result_solve_ivp.y[i][-1] for i in idx_target_all])
509+
c_target_t = np.array([result_solve_ivp.y[i][-1]
510+
for i in idx_target_all])
498511

499512
r_idx = [
500513
i
@@ -533,8 +546,10 @@ def test_get_k():
533546
coeff_TS = np.array([0, 1, 0, 1, 0, 1])
534547
temperature = 298.15
535548

536-
expected_k_forward = np.array([1.23420642e02, 2.66908478e-02, 6.51255161e-04])
537-
expected_k_reverse = np.array([2.87005583e02, 6.51255161e-04, 4.70404010e-01])
549+
expected_k_forward = np.array(
550+
[1.23420642e02, 2.66908478e-02, 6.51255161e-04])
551+
expected_k_reverse = np.array(
552+
[2.87005583e02, 6.51255161e-04, 4.70404010e-01])
538553

539554
k_forward, k_reverse = get_k(energy_profile, dgr, coeff_TS, temperature)
540555

@@ -806,9 +821,8 @@ def test_system_KE_DE():
806821
idx_target_all = [states.index(i) for i in states if "*" in i]
807822
c_target_t = np.array([result_solve_ivp.y[i][-1] for i in idx_target_all])
808823

809-
R_idx = [
810-
i for i, s in enumerate(states) if s.lower().startswith("r") and "INT" not in s
811-
]
824+
R_idx = [i for i, s in enumerate(
825+
states) if s.lower().startswith("r") and "INT" not in s]
812826
Rp = rxn_network_all[:, R_idx]
813827
Rp_ = []
814828
for col in range(Rp.shape[1]):
@@ -823,6 +837,63 @@ def test_system_KE_DE():
823837
assert_allclose(c_target_t, expected_c_target_t)
824838

825839

840+
def test_calc_km():
841+
energy_profile_all = [np.array([0., -19.82, -4.65, -24.45, -39.79, -22.53, -32.12, -38.17,
842+
-49.23, -36.45], dtype=float),
843+
np.array([-19.82, -4.36, -19.97, -38.49, -15.51, -29.72, -35.33, -48.19,
844+
-36.37], dtype=float)]
845+
dgr_all = [-28.71, -30.07]
846+
coeff_TS_all = [np.array([0, 0, 1, 0, 0, 1, 0, 1, 0, 1]),
847+
np.array([0, 1, 0, 0, 1, 0, 1, 0, 1])]
848+
rxn_network_all = np.array([
849+
[-1, 1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0],
850+
[0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
851+
[0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0],
852+
[0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
853+
[0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0],
854+
[1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 0],
855+
[0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
856+
[0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, -1, 0, 0, 0],
857+
[0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0],
858+
[0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, -1, 0, 0],
859+
[1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1]])
860+
temperature = 393.15
861+
t_span = (0, 86400)
862+
initial_conc = np.array(
863+
[0.01, 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0.], dtype=float)
864+
states = [
865+
'INT2',
866+
'INT3',
867+
'INT4L',
868+
'INT5L',
869+
'INT6L',
870+
'INT7L',
871+
'INT4B',
872+
'INT5B',
873+
'INT6B',
874+
'INT7B',
875+
'R-alkene',
876+
'R-CO',
877+
'R-H$_2$',
878+
'P-L*',
879+
'P-B*']
880+
expected_res = np.array([0.14785136, 0.84215103])
881+
res, _ = calc_km(
882+
energy_profile_all,
883+
dgr_all,
884+
coeff_TS_all,
885+
rxn_network_all,
886+
temperature,
887+
t_span,
888+
initial_conc,
889+
states,
890+
report_as_yield=False,
891+
quality=0,
892+
ks=None,
893+
)
894+
assert_allclose(expected_res, np.array(res))
895+
896+
826897
def main():
827898
(
828899
dg,
@@ -842,7 +913,8 @@ def main():
842913
initial_conc = np.array([])
843914
last_row_index = df_network.index[-1]
844915
if isinstance(last_row_index, str):
845-
if last_row_index.lower() in ["initial_conc", "c0", "initial conc"]:
916+
if last_row_index.lower() in [
917+
"initial_conc", "c0", "initial conc"]:
846918
initial_conc = df_network.iloc[-1:].to_numpy()[0]
847919
df_network = df_network.drop(df_network.index[-1])
848920
rxn_network_all = df_network.to_numpy()[:, :]

navicat_mikimo/test_module.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
test_calc_dX_dt,
55
test_get_k,
66
test_system_KE_DE,
7+
test_calc_km
78
)
89

910
test_add_rate()
1011
test_calc_dX_dt()
1112
test_get_k()
1213
test_process_data_mkm()
1314
test_system_KE_DE()
15+
test_calc_km()
1416

1517
if __name__ == "__main__":
1618
test_add_rate()
1719
test_calc_dX_dt()
1820
test_get_k()
1921
test_process_data_mkm()
2022
test_system_KE_DE()
23+
test_calc_km()

0 commit comments

Comments
 (0)