Skip to content

Commit c6d99fa

Browse files
committed
Add vibe coded file (I didn't test it)
1 parent 3f547bd commit c6d99fa

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

examples/cidnp_kq.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#! /usr/bin/env python
2+
3+
# MARY simulation with simple kinetic rate equations replacing Haberkorn
4+
# superoperators.
5+
6+
import matplotlib.pyplot as plt
7+
import numpy as np
8+
9+
from radicalpy.classical import Rate, RateEquations
10+
from radicalpy.experiments import kine_quantum_mary
11+
from radicalpy.simulation import Molecule, SemiclassicalSimulation
12+
from radicalpy.utils import is_fast_run
13+
14+
15+
def rate_equations():
16+
ke = Rate(1e6, "k_{E}") # radical pair separation to free radical
17+
kr = Rate(2e6, "k_{R}") # reverse electron transfer of RP to groundstate
18+
kfr = Rate(1e5, "k_{FR}") # free radical recombination
19+
20+
FR = "FR"
21+
SS, STp, ST0, STm = "SS", "ST_+", "ST_0", "ST_-"
22+
TpS, TpTp, TpT0, TpTm = "T_+S", "T_+T_+", "T_+T_0", "T_+T_-"
23+
T0S, T0Tp, T0T0, T0Tm = "T_0S", "T_0T_+", "T_0T_0", "T_0T_-"
24+
TmS, TmTp, TmT0, TmTm = "T_-S", "T_-T_+", "T_-T_0", "T_-T_-"
25+
26+
base = {}
27+
base[FR] = {FR: -kfr, SS: ke, TpTp: ke, T0T0: ke, TmTm: ke}
28+
29+
base[SS] = {SS: -(kr + ke)}
30+
base[STp] = {STp: -(kr + ke)}
31+
base[ST0] = {ST0: -(kr + ke)}
32+
base[STm] = {STm: -(kr + ke)}
33+
34+
base[TpS] = {TpS: -(kr + ke)}
35+
base[TpTp] = {TpTp: -ke}
36+
base[TpT0] = {TpT0: -ke}
37+
base[TpTm] = {TpTm: -ke}
38+
39+
base[T0S] = {T0S: -(kr + ke)}
40+
base[T0Tp] = {T0Tp: -ke}
41+
base[T0T0] = {T0T0: -ke}
42+
base[T0Tm] = {T0Tm: -ke}
43+
44+
base[TmS] = {TmS: -(kr + ke)}
45+
base[TmTp] = {TmTp: -ke}
46+
base[TmT0] = {TmT0: -ke}
47+
base[TmTm] = {TmTm: -ke}
48+
49+
rate_eq = RateEquations(base)
50+
return rate_eq, kfr
51+
52+
53+
def main(tmax=5e-6, dt=5e-9, Bmax=20, dB=0.25, num_samples=200):
54+
time = np.arange(0, tmax, dt)
55+
B = np.arange(0, Bmax + dB, dB)
56+
57+
rate_eq, kfr = rate_equations()
58+
mat = np.asarray(rate_eq.matrix.todense(), dtype=complex)
59+
rho0 = np.array(
60+
[
61+
0, # FR
62+
0, # SS
63+
0, # ST+
64+
0, # ST0
65+
0, # ST-
66+
0, # T+S
67+
1 / 3, # T+T+
68+
0, # T+T0
69+
0, # T+T-
70+
0, # T0S
71+
0, # T0T+
72+
1 / 3, # T0T0
73+
0, # T0T-
74+
0, # T-S
75+
0, # T-T+
76+
0, # T-T0
77+
1 / 3, # T-T-
78+
],
79+
dtype=complex,
80+
)
81+
82+
r1 = Molecule.fromisotopes(isotopes=["1H"], hfcs=[0.5])
83+
r2 = Molecule("radical 2")
84+
sim = SemiclassicalSimulation([r1, r2])
85+
86+
results = kine_quantum_mary(
87+
sim,
88+
num_samples=num_samples,
89+
init_state=rho0,
90+
radical_pair=[1, 17],
91+
ts=time,
92+
Bs=B,
93+
D=0,
94+
J=0,
95+
kinetics=mat,
96+
relaxations=[],
97+
)
98+
99+
dt = time[1] - time[0]
100+
free_radical = np.real(results["yield"][:, 0, :])
101+
recombination = kfr.value * free_radical
102+
product_yields = np.cumsum(recombination, axis=0) * dt
103+
product_yield_sums = product_yields[-1, :]
104+
mary = (product_yield_sums - product_yield_sums[0]) / product_yield_sums[0] * 100
105+
lfe = (mary.min() if abs(mary.min()) > abs(mary.max()) else mary.max())
106+
hfe = mary[-1]
107+
108+
plt.plot(B, mary, color="red", linewidth=2)
109+
plt.xlabel("$B_0$ (mT)")
110+
plt.ylabel("MFE (%)")
111+
plt.title("1H radical pair kinetic-quantum MARY")
112+
113+
print(f"LFE = {lfe: .2f} %")
114+
print(f"HFE = {hfe: .2f} %")
115+
116+
path = __file__[:-3] + f"_{0}.png"
117+
plt.savefig(path)
118+
119+
120+
if __name__ == "__main__":
121+
if is_fast_run():
122+
main(tmax=1e-6, dt=2e-8, Bmax=5, dB=1, num_samples=10)
123+
else:
124+
main()

0 commit comments

Comments
 (0)