geem-lab / overreact

⚛️📈 Create and analyze chemical microkinetic models built from computational chemistry data. Crafted at the @geem-lab.
https://geem-lab.github.io/overreact-guide/
MIT License
48 stars 9 forks source link

Module for high-level API #30

Closed schneiderfelipe closed 2 years ago

schneiderfelipe commented 4 years ago

We could benefit from a high-level interface, defined as an interface that deals with logfile and model objects directly. All functions would be higher-level versions of internal procedures. This is related to the discussion in #28.

from overreact import api, io, simulate
import matplotlib.pyplot as plt

model = io.parse_model("my-model.jk")
for temperature in [200.0, 300.0, 400.0]:
    enthalpies = api.get_enthalpies(model.compounds, temperature=temperature)
    entropies = api.get_entropies(model.compounds, temperature=temperature)
    freeenenergies = enthalpies - temperature * entropies

    delta_freeenergies = api.get_delta(model.scheme.B, freeenergies)
    k = api.get_k(delta_freeenergies)
    kappa = api.get_kappa(delta_freeenergies, model.scheme)
    k = kappa * k

    y0 = [1.0, 0.0]
    dydt = simulate.get_dydt(scheme, k)
    t, y = simulate.get_y(dydt, y0=y0, method="Radau")
    plt.plot(t, y[1], label=f"P @{temperature}K")

plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("Concentration (M)")
plt.show()

Furthermore, other related things are proposed:

...
def factory(freeenergies, scheme, y0, t):
    delta_freeenergies = api.get_delta(scheme.B, freeenergies)
    k = api.get_k(delta_freeenergies)
    kappa = api.get_kappa(delta_freeenergies, scheme)
    k = kappa * k

    dydt = simulate.get_dydt(scheme, k)
    t, y = simulate.get_y(dydt, y0, [0.0, t], method="Radau")
    return dydt(t[-1], y.T)

t = 60.0
y0 = [1.0, 0.0]
drc = get_drc(factory, args=(model.scheme, y0, t))

# degree of rate control of the formation of 1 (P) with respect to all compounds
print(drc[1, :])

The (square) array-like object drc would contain the derivatives of equation 1 of Campbell (2020) for the rate of formation of all compounds with respect to the free energies of all compounds.

schneiderfelipe commented 4 years ago

Projects and issues related to the degree of rate control were moved to milestone 1.2.

schneiderfelipe commented 3 years ago

The ideas on rate control are still planned for 1.2, but all other suggestions are expected to be ready in 1.1.

schneiderfelipe commented 3 years ago

This is important to have as we go public as we want to publish tutorials as Jupyter Notebooks (see #49).

schneiderfelipe commented 3 years ago

There is a demand for an API in Python for thermochemistry; see bobbypaton/GoodVibes#35.

schneiderfelipe commented 3 years ago

We could benefit from a high-level interface, defined as an interface that deals with logfile and model objects directly. All functions would be higher-level versions of internal procedures. This is related to the discussion in #28.

* [x]  As such, the user would be able to do something similar to:

The current status has improved the example above a lot. A draft of how we can currently accomplish the same thing now:

import numpy as np
import matplotlib.pyplot as plt

import overreact as rx

model = rx.parse_model("my-model.k")
for temperature in [200.0, 300.0, 400.0]:
    # already multiplies contains transmission coefficients
    k = rx.get_k(model.scheme, model.compounds)
    dydt = rx.get_dydt(model.scheme, k)

    y0 = [1.0, 0.0]
    y, r = simulate.get_y(dydt, y0)

    t = np.linspace(y.t_min, y.t_max)
    plt.plot(t, y(t)[1], label=f"P @{temperature}K")

plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("Concentration (M)")
plt.show()

Furthermore, other related things are proposed:

* [ ]  Calculate the degree of rate control with a factory function that receives the free energies as first parameter and returns one reaction rate:

I'm postponing this for now as I currently don't prioritize this anymore. The example above is good though. As such, this is going to 1.2 again.

schneiderfelipe commented 2 years ago

I'm postponing the development on the degree of rate control and sensitivity analysis in general. The rest of this issue is there already.