Skip to content

Commit

Permalink
Use rtol and atol with solve_ivp
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe S. S. Schneider authored and schneiderfelipe committed Nov 27, 2020
1 parent cdae256 commit 74cddd3
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 5 deletions.
Binary file modified docs/_static/drc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/first-order.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/michaelis-menten-dydt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/michaelis-menten-tof.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/michaelis-menten.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/_static/simple-first-order.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion overreact/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def get_drc(
compounds,
y0,
t_span=None,
method="BDF",
method="Radau",
qrrho=True,
scale="l mol-1 s-1",
temperature=298.15,
Expand Down
10 changes: 10 additions & 0 deletions overreact/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def __init__(
qrrho=True, # TODO(schneiderfelipe): change to use_qrrho
temperature=298.15,
bias=0.0,
rtol=1e-5,
atol=1e-9,
box_style=box.SIMPLE,
):
self.model = model
Expand All @@ -82,6 +84,8 @@ def __init__(
self.qrrho = qrrho
self.temperature = temperature
self.bias = bias
self.rtol = rtol
self.atol = atol
self.box_style = box_style

def __rich_console__(self, console, options):
Expand Down Expand Up @@ -542,6 +546,8 @@ def main():
),
action="store_false",
)
parser.add_argument("--rtol", type=float, default=1e-5)
parser.add_argument("--atol", type=float, default=1e-9)
parser.add_argument(
"--plot",
help=(
Expand Down Expand Up @@ -581,6 +587,8 @@ def main():
- QRRHO? = {args.qrrho}
- Temperature = {args.temperature} K
- Pressure = {args.pressure} Pa
- Rel. Tol. = {args.rtol}
- Abs. Tol. = {args.atol}
- Bias = {args.bias / constants.kcal} kcal/mol
Parsing and calculating…
Expand All @@ -604,6 +612,8 @@ def main():
qrrho=args.qrrho,
temperature=args.temperature,
bias=args.bias,
rtol=args.rtol,
atol=args.atol,
)
console.print(report, justify="left")

Expand Down
19 changes: 15 additions & 4 deletions overreact/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
if _found_jax:
import jax.numpy as jnp
from jax import jacfwd
from jax import jit
from jax.config import config

config.update("jax_enable_x64", True)
else:
jnp = np


def get_y(dydt, y0, t_span=None, method="BDF"):
def get_y(dydt, y0, t_span=None, method="Radau", rtol=1e-5, atol=1e-9):
"""Simulate a reaction scheme from its rate function.
This uses scipy's ``solve_ivp`` under the hood.
Expand All @@ -48,6 +47,8 @@ def get_y(dydt, y0, t_span=None, method="BDF"):
Integration method to use. See `scipy.integrade.solve_ivp` for details.
Kinetics problems are very often stiff and, as such, "RK45" is
normally unsuited. "Radau", "BDF" or "LSODA" are good choices.
rtol, atol : array-like
See `scipy.integrade.solve_ivp` for details.
Returns
-------
Expand Down Expand Up @@ -114,7 +115,17 @@ def get_y(dydt, y0, t_span=None, method="BDF"):
if hasattr(dydt, "jac"):
jac = dydt.jac

res = _solve_ivp(dydt, t_span, y0, method=method, dense_output=True, jac=jac)
# TODO(schneiderfelipe): log solve_ivp stuff.
res = _solve_ivp(
dydt,
t_span,
y0,
method=method,
dense_output=True,
rtol=rtol,
atol=atol,
jac=jac,
)
y = res.sol

def r(t):
Expand Down Expand Up @@ -213,7 +224,7 @@ def _dydt(t, y, k=k_adj, M=M):
return jnp.dot(A, r)

if _found_jax:
_dydt = jit(_dydt)
_dydt = _dydt

def _jac(t, y, k=k_adj, M=M):
# _jac(t, y)[i, j] == d f_i / d y_j
Expand Down

0 comments on commit 74cddd3

Please sign in to comment.