Skip to content

Commit 027aaf1

Browse files
misc: Curve fitting to biexponential function
Removing B in lieu of 1-A gets rid of "bad" results (since B can be 0, and therefore tau2 could have huge/bad values, leading to a huge / nonsensical value of the lifetime) Co-authored-by: Moritz Sallermann <[email protected]>
1 parent a6bd557 commit 027aaf1

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

examples/hydrogen_bond_tcf/lineplot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,10 @@ def insert_inset(image_path, axis, rel_height,
9393
# ---------------------------------------------
9494
# Curve fitting
9595
# initial guess A, tau1, B, tau2
96-
params, fit_t, fit_ac, lifetime = fit_biexponential(tau_val, tcf_val, [0.5, 1, 1, 2])
96+
params, fit_t, fit_ac, lifetime = fit_biexponential(tau_val, tcf_val, [0.5, 1, 2])
9797
print("Lifetime is ", lifetime, "ps \n")
98-
A, tau1, B, tau2 = params
98+
A, tau1, tau2 = params
9999
# If A+B is around 1.01, the fit is bad even if it looks okay
100-
print(f"Sum A + B = {A+B} and should be 1.0\n")
101100
print(f"The time constants are {tau1} ps and {tau2} ps\n")
102101
# ---------------------------------------------
103102
ax1 = fig.add_subplot(gs[0,0])
@@ -125,6 +124,7 @@ def insert_inset(image_path, axis, rel_height,
125124
# xtick_vec = np.append(xtick_vec, 2.25)
126125
# ax1.set_xticks(xtick_vec)
127126
# ax1.set_ylim([0.0,25])
127+
# ax1.set_yscale("log")
128128
ax1.set_xlim([0.0,10])
129129

130130
# ---------------------------------------------------------------------

soluanalysis/misc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def biexponential_model(
11-
t: float, A: float, tau1: float, B: float, tau2: float
11+
t: float, A: float, tau1: float, tau2: float
1212
) -> float:
1313
"""Fit data to a biexponential function (sum of two exponential decays). A and B should sum to 1.0
1414
C(t) = A*exp(-t/tau1) + B*exp(-t/tau2)
@@ -23,13 +23,13 @@ def biexponential_model(
2323
Returns:
2424
float: _description_
2525
"""
26-
return A * np.exp(-t / tau1) + B * np.exp(-t / tau2)
26+
return A * np.exp(-t / tau1) + (1-A) * np.exp(-t / tau2)
2727

2828

2929
def fit_biexponential(
3030
tau_timeseries: Union[List[float], npt.NDArray],
3131
tcf_timeseries: Union[List[float], npt.NDArray],
32-
initial_guess: Union[List[float], npt.NDArray] = [0.5, 1, 1, 2],
32+
initial_guess: Union[List[float], npt.NDArray] = [0.5, 1, 2],
3333
) -> Tuple[Tuple[float, float, float], npt.NDArray, npt.NDArray, float]:
3434
"""Fit a biexponential function of the form (A*exp(-t/tau1) + B*exp(-t/tau2)) to the tau values and time autocorrelation function values (using the continuous bond definition).
3535
Using the relation from Gowers et al. (2015).
@@ -59,9 +59,9 @@ def fit_biexponential(
5959
fit_ac = biexponential_model(fit_t, *params)
6060

6161
# Extract the parameters
62-
A, tau1, B, tau2 = params
62+
A, tau1, tau2 = params
6363

6464
# Compute the lifetime
65-
lifetime = A * tau1 + B * tau2
65+
lifetime = A * tau1 + (1-A) * tau2
6666

6767
return params, fit_t, fit_ac, lifetime

0 commit comments

Comments
 (0)