{
"cells": [
{
"cell_type": "markdown",
"id": "3bbe8002-bdf3-490c-bde0-80dd3713a3d0",
"metadata": {},
"source": [
"## A complex (multistep) reaction `A <-> C` derived from 2 coupled elementary reactions: \n",
"## `A <-> B` and `B <-> C` \n",
"We are given the time evolution of the complex reaction, \n",
"and want to determine whether it can be modeled as an elementary reaction. \n",
"\n",
"In PART 1, a time evolution of [A], [B] and [C] is obtained by simulation \n",
"In PART 2, the time functions generated in Part 1 are taken as a _starting point,_ to explore how to model the composite reaction `A <-> C` \n",
"\n",
"**Background**: please see experiments `cascade_1` and `mystery_reaction_1`\n",
"\n",
"LAST REVISED: Dec. 6, 2023"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4d9b4808-b2ec-4b90-a604-f7fa69af39b8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added 'D:\\Docs\\- MY CODE\\BioSimulations\\life123-Win7' to sys.path\n"
]
}
],
"source": [
"import set_path # Importing this module will add the project's home directory to sys.path"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3924c013",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from experiments.get_notebook_info import get_notebook_basename\n",
"\n",
"from src.modules.reactions.reaction_dynamics import ReactionDynamics\n",
"from src.modules.visualization.plotly_helper import PlotlyHelper"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75411c8b-f0c5-411d-9e12-1eaa423449f9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "9329208b-070f-4902-8f37-0f11ddf75ed6",
"metadata": {},
"source": [
"# PART 1 - We'll generate the time evolution of [A] and [C] by simulating coupled elementary reactions of KNOWN rate constants...\n",
"## but pretend you don't see this section! (because we later assume that those time evolutions are just GIVEN to us)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "72b4245c-de4e-480d-a501-3495b7ed8bc4",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of reactions: 2 (at temp. 25 C)\n",
"0: A <-> B (kF = 8 / kR = 2 / delta_G = -3,436.6 / K = 4) | 1st order in all reactants & products\n",
"1: B <-> C (kF = 12 / kR = 1 / delta_G = -6,160 / K = 12) | 1st order in all reactants & products\n",
"Set of chemicals involved in the above reactions: {'A', 'C', 'B'}\n"
]
}
],
"source": [
"# Instantiate the simulator and specify the chemicals\n",
"dynamics = ReactionDynamics(names=[\"A\", \"B\", \"C\"])\n",
"\n",
"# Reaction A <-> B (slower, and with a smaller K)\n",
"dynamics.add_reaction(reactants=\"A\", products=\"B\",\n",
" forward_rate=8., reverse_rate=2.) \n",
"\n",
"# Reaction B <-> C (faster, and with a larger K)\n",
"dynamics.add_reaction(reactants=\"B\", products=\"C\",\n",
" forward_rate=12., reverse_rate=1.) \n",
" \n",
"dynamics.describe_reactions()"
]
},
{
"cell_type": "markdown",
"id": "98a9fbe5-2090-4d38-9c5f-94fbf7c3eae2",
"metadata": {},
"source": [
"### Run the simulation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ae304704-c8d9-4cef-9e0b-2587bb3909ef",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SYSTEM STATE at Time t = 0:\n",
"3 species:\n",
" Species 0 (A). Conc: 50.0\n",
" Species 1 (B). Conc: 0.0\n",
" Species 2 (C). Conc: 0.0\n",
"Set of chemicals involved in reactions: {'A', 'C', 'B'}\n"
]
}
],
"source": [
"dynamics.set_conc([50., 0., 0.], snapshot=True) # Set the initial concentrations of all the chemicals, in their index order\n",
"dynamics.describe_state()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2502cd11-0df9-4303-8895-98401a1df7b8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* INFO: the tentative time step (0.01) leads to a least one norm value > its ABORT threshold:\n",
" -> will backtrack, and re-do step with a SMALLER delta time, multiplied by 0.4 (set to 0.004) [Step started at t=0, and will rewind there]\n",
"Some steps were backtracked and re-done, to prevent negative concentrations or excessively large concentration changes\n",
"85 total step(s) taken\n"
]
}
],
"source": [
"# These settings can be tweaked to make the time resolution finer or coarser. \n",
"# Here we use a \"mid\" heuristic: neither too fast nor too prudent\n",
"dynamics.use_adaptive_preset(preset=\"mid\")\n",
"\n",
"dynamics.single_compartment_react(initial_step=0.01, reaction_duration=0.8,\n",
" snapshots={\"initial_caption\": \"1st reaction step\",\n",
" \"final_caption\": \"last reaction step\"},\n",
" variable_steps=True, explain_variable_steps=False)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a2c0e793-5457-46a5-9150-388c9f562cf0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hovertemplate": "Chemical=A
SYSTEM TIME=%{x}
concentration=%{y}
SYSTEM TIME=%{x}
concentration=%{y}
SYSTEM TIME=%{x}
concentration=%{y}
| \n", " | SYSTEM TIME | \n", "A | \n", "C | \n", "caption | \n", "
|---|---|---|---|---|
| 0 | \n", "0.000000 | \n", "50.000000 | \n", "0.000000 | \n", "Initial state | \n", "
| 1 | \n", "0.004000 | \n", "48.400000 | \n", "0.000000 | \n", "1st reaction step | \n", "
| 2 | \n", "0.008000 | \n", "46.864000 | \n", "0.076800 | \n", "\n", " |
| 3 | \n", "0.010000 | \n", "46.126413 | \n", "0.150067 | \n", "\n", " |
| 4 | \n", "0.011000 | \n", "45.764849 | \n", "0.194599 | \n", "\n", " |
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 81 | \n", "0.638365 | \n", "1.497612 | \n", "44.279102 | \n", "\n", " |
| 82 | \n", "0.670313 | \n", "1.384698 | \n", "44.483579 | \n", "\n", " |
| 83 | \n", "0.708651 | \n", "1.276811 | \n", "44.678990 | \n", "\n", " |
| 84 | \n", "0.754656 | \n", "1.179000 | \n", "44.856174 | \n", "\n", " |
| 85 | \n", "0.809862 | \n", "1.096061 | \n", "45.006431 | \n", "last reaction step | \n", "
86 rows × 4 columns
\n", "| \n", " | search_value | \n", "SYSTEM TIME | \n", "A | \n", "B | \n", "C | \n", "caption | \n", "
|---|---|---|---|---|---|---|
| 47 | \n", "0.1 | \n", "0.098644 | \n", "24.020441 | \n", "14.383519 | \n", "11.59604 | \n", "\n", " |