```python
import sympy as sp
import matplotlib.pyplot as plt
from chempy import ReactionSystem
from chempy.kinetics.ode import get_odesys
sp.init_printing()
%matplotlib inline
```


```python
ReactionSystem?
```


```python
rsys = ReactionSystem.from_string("""
-> H; 'p'
2 H -> H2; 'k2'
""", checks=('substance_keys', 'duplicate', 'duplicate_names'))
rsys
```


```python
H, k, p, c1, t, H0  = sp.symbols('H k p c1 t H0', real=True, positive=True)
```


```python
dHdt = p - 2*k*H**2
dHdt
```


```python
analytic = sp.sqrt(p/k/2)*sp.tanh((c1 + t)*sp.sqrt(2*k*p))
analytic
```


```python
(analytic.diff(t) - dHdt.subs(H, analytic)).simplify()
```


```python
sol1, sol2 = sp.solve(analytic.subs(t, 0) - H0, c1)
```


```python
res1 = analytic.subs(c1, sol1).simplify()
res1
```


```python
res2 = analytic.subs(c1, sol2).simplify()
res2
```


```python
def get_python_code(expr, be='np'):
    cses, new_expr = sp.cse(expr)
    snippet = '\n'.join(['%s = %s' % cse for cse in cses] + ['return %s' % new_expr[0]]).replace(
        'sqrt', '%s.sqrt' % be).replace(
        'log', '%s.log' % be).replace(
        'tanh', '%s.tanh' % be).replace(
        'exp', '%s.exp' % be)
    return snippet
```


```python
print(get_python_code(res2))
```


```python
print(get_python_code(res2.rewrite('exp'), ))
```


```python
f1 = sp.lambdify([t, p, k, H0], res1)
f2 = sp.lambdify([t, p, k, H0], res2)
```


```python
odesys, extra = get_odesys(rsys, include_params=False)
```


```python
c0 = {'H': 42e-6, 'H2': 17e-6}
params = {'p': 314/3600*.998*2*1.036426856e-7, 'k2': 53/60}
result = odesys.integrate(7*3600, c0, params,
                          integrator='cvode')
result.plot()
plt.plot(result.xout, f2(result.xout, params['p'], params['k2'], c0['H']), ls=':')
plt.gca().set_ylim([0, 300e-6])
```


```python
result.params
```


```python

```

cafb1a121c4a349123c93fdca9414a9eea22cb5a
ipynb
Jupyter Notebook
examples/_kinetics_radiolysis_analytic.ipynb
bertiewooster/chempy
115adc1d570aa1631baff4374f3128ce23fa7776
BSD-2-Clause
2015-10-30T03:41:05.000Z
2022-03-31T05:01:17.000Z
examples/_kinetics_radiolysis_analytic.ipynb
bertiewooster/chempy
115adc1d570aa1631baff4374f3128ce23fa7776
BSD-2-Clause
2015-11-03T13:31:23.000Z
2022-03-31T16:46:19.000Z
examples/_kinetics_radiolysis_analytic.ipynb
bertiewooster/chempy
115adc1d570aa1631baff4374f3128ce23fa7776
BSD-2-Clause
2016-06-06T19:55:48.000Z
2022-03-19T23:39:13.000Z
Qwen/Qwen-72B
1. YES
2. YES