<jupyter_start><jupyter_text>Rate Equations $$\frac{d(CD^+)}{dt} = -[(k31) * CD^+ * He^2] + [(kCID1) * CDHe^+ * He] $$ $$\frac{d(CDHe^+)}{dt} = -[(k32) * CDHe^+ * He^2] + [(kCID2) * CDHe2^+ * He] + [(k31) * CD^+ * He^2] - [(kCID1) * CDHe^+ * He] $$ $$\frac{d(CDHe2^+)}{dt} = -[(k33) * CDHe2^+ * He^2] + [(kCID3) * CDHe3^+ * He] + [(k32) * CDHe^+ * He^2] - [(kCID2) * CDHe2^+ * He] $$$$\frac{d(CDHe3^+)}{dt} = -[(k34) * CDHe3^+ * He^2] + [(kCID4) * CDHe4^+ * He] + [(k33) * CDHe2^+ * He^2] - [(kCID3) * CDHe3^+ * He] $$ $$\frac{d(CDHe4^+)}{dt} = [(k34) * CD^+ * He^2] - [(kCID4) * CDHe^+ * He] $$   Rate Constants to fit K31, K32, K33, K34, KCID1, KCID2, KCID3, KCID4<jupyter_code>CD, CDHe, CDHe2, CDHe3, CDHe4, t = variables('CD, CDHe, CDHe2, CDHe3, CDHe4, t')
#k31, k32, k33, k34, kCID1, kCID2, kCID3, kCID4 = parameters('k31, k32, k33, k34, kCID1, kCID2, kCID3, kCID4')

kmin, kcid_min = 1e-30, 1e-15
kmax, kcid_max = 1e-29, 1e-14

print(f'kmin: {kmin} kmax: {kmax}\nkcid_min: {kcid_min} kcid_max: {kcid_max}')

k31 = Parameter('k31', min=kmin, max=kmax)
k32 = Parameter('k32', min=kmin, max=kmax)
k33 = Parameter('k33', min=kmin, max=kmax)
k34 = Parameter('k34', min=kmin, max=kmax)

kCID1 = Parameter('kCID1', min=kcid_min, max=kcid_max)
kCID2 = Parameter('kCID2', min=kcid_min, max=kcid_max)
kCID3 = Parameter('kCID3', min=kcid_min, max=kcid_max)
kCID4 = Parameter('kCID4', min=kcid_min, max=kcid_max)


initial_cond = {
    t: 0,
    CD: cd[0],
    CDHe: 0,
    CDHe2: 0,
    CDHe3: 0,
    CDHe4: 0
}

r31 = k31*CD*He**2
rcid1 = kCID1*CDHe*He

r32 = k32*CDHe*He**2
rcid2 = kCID2*CDHe2*He

r33 = k33*CDHe2*He**2
rcid3 = kCID3*CDHe3*He

r34 = k34*CDHe3*He**2
rcid4 = kCID4*CDHe4*He

rate_model = {
    D(CD, t): -r31+rcid1,
    D(CDHe, t): -r32+rcid2+r31-rcid1,
    D(CDHe2, t): -r33+rcid3+r32-rcid2,
    D(CDHe3, t): -r34+rcid4+r33-rcid3,
    D(CDHe4, t): +r34-rcid4
}

ode_model = ODEModel(rate_model, initial=initial_cond)

# Generate some data
tdata = np.linspace(0, 3, 1000)
guess = InteractiveGuess2D(ode_model, t=time, CD=cd, CDHe=cdHe, CDHe2=cdHe2, CDHe3=cdHe3, CDHe4=cdHe4)
guess.execute()
# with Fit
t0 = start_time()

fit = Fit(ode_model, t=time, CD=cd, CDHe=cdHe, CDHe2=cdHe2, CDHe3=cdHe3, CDHe4=cdHe4)
fit_result = fit.execute()

t1 = start_time()
print(f'Time taken: {t1-t0} s')

CD, CDHe, CDHe2, CDHe3, CDHe4 = ode_model(t=tdata, **fit_result.params)

print(fit_result)
print(f'Parameters: {fit_result.params}' )
print(f'Parameters: {fit_result.params}' )
print(fit_result)
fit_result.model
# Eval the normal way. (without fit)
CD, CDHe, CDHe2, CDHe3, CDHe4 = ode_model(t=tdata, k31=2.7e-30, k32=6e-30, k33=32e-30, k34=15e-28, kCID1=1.9e-15, kCID2=4e-15, kCID3=24e-15, kCID4=15e-14)


# with Fit
#ode_model = ODEModel(rate_model, initial=initial_cond)

#fit = Fit(ode_model, t=time, CD=cd, CDHe-cdHe, CDHe2=cdHe2, CDHe3=cdHe3, CDHe4=cdHe4)
#fit_result = fit.execute()

#CD, CDHe, CDHe2, CDHe3, CDHe4 = ode_model(t=tdata, **fit_result.params)

######################################################################################################################################################################

fig2, ax2 = plt.subplots(figsize = (14, 5), dpi = 150)

ax2.plot(tdata, CD, label = 'CD+')
ax2.plot(time, cd, '.', label = 'CD+_exp')

ax2.plot(tdata, CDHe, label = 'CDHe+')
ax2.plot(time, cdHe, '.', label = 'CDHe+_exp')

ax2.plot(tdata, CDHe2, label = 'CDHe2+')
ax2.plot(time, cdHe2, '.', label = 'CDHe2+_exp')

ax2.plot(tdata, CDHe3, label = 'CDHe3+')
ax2.plot(time, cdHe3, '.', label = 'CDHe3+_exp')

ax2.plot(tdata, CDHe4, label = 'CDHe4+')
ax2.plot(time, cdHe4, '.', label = 'CDHe4+_exp')

ax2.set_yscale('log')
ax2.set_xlabel('Time (s)')
ax2.set_ylabel('Counts')
ax2.set_title('Ternary Reactions rate')
ax2.legend()
ax2.grid()

plt.show()
plt.close()
help(Fit)
import time
t0 = time.time()
time.sleep(1)
t1 = time.time()
t1-t0
time()
help(Parameter)
k31 = Parameter('k31', min=1e-16)<jupyter_output>Help on class Parameter in module symfit.core.argument:

class Parameter(Argument)
 |  Parameter(name=None, *args, **kwargs)
 |  
 |  Parameter objects are used to facilitate bounds on function parameters.
 |  Important change from `symfit>0.4.1`: the name needs to be the first keyword,
 |  followed by the guess value. If no name is provided, the initial value can
 |  be passed as a keyword argument, e.g.: `value=0.1`. A generic name will then
 |  be generated.
 |  
 |  Method resolution order:
 |      Parameter
 |      Argument
 |      sympy.core.symbol.Symbol
 |      sympy.core.expr.AtomicExpr
 |      sympy.core.basic.Atom
 |      sympy.core.expr.Expr
 |      sympy.logic.boolalg.Boolean
 |      sympy.core.basic.Basic
 |      sympy.core.evalf.EvalfMixin
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __call__ = call(self, *values, **named_values)
 |      Call an expression to evaluate it at the given point.
 |      
 |      Future improvements: I would like if func and[...]
{'fraction_non_alphanumeric': 0.21902834008097166, 'fraction_numerical': 0.06538461538461539, 'mean_word_length': 4.496106785317019, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '147132', 'license': 'MIT', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2235, 'n_words': 453, 'path': 'Simulations/kinetics/test/kineticModel.ipynb', 'repo_name': 'aravindhnivas/FELion-Spectrum-Analyser', 'size': '4940'}