{ "cells": [ { "cell_type": "markdown", "id": "3bbe8002-bdf3-490c-bde0-80dd3713a3d0", "metadata": {}, "source": [ "## Reversible Synthesis elementary reaction `A + B <-> C`\n", "#### taken to equilibrium.\n", "#### Comparison of 2 approximate solutions, and the exact solutions " ] }, { "cell_type": "markdown", "id": "1b7d393d-a9b5-4437-80ae-c2b3effd9ca8", "metadata": {}, "source": [ "### TAGS : \"uniform compartment\", \"numerical\"" ] }, { "cell_type": "code", "execution_count": 1, "id": "5dafa9a4-85de-49a5-925d-e268ff1402a1", "metadata": {}, "outputs": [], "source": [ "LAST_REVISED = \"Jan. 4, 2026\"\n", "LIFE123_VERSION = \"1.0.0rc7\" # Library version this experiment is based on" ] }, { "cell_type": "code", "execution_count": 2, "id": "82191b23-e6f5-4638-a26d-3716fc728055", "metadata": {}, "outputs": [], "source": [ "#import set_path # Using MyBinder? Uncomment this before running the next cell!" ] }, { "cell_type": "code", "execution_count": 3, "id": "4bbaa8f3", "metadata": { "tags": [] }, "outputs": [], "source": [ "#import sys\n", "#sys.path.append(\"C:/some_path/my_env_or_install\") # CHANGE to the folder containing your venv or libraries installation!\n", "# NOTE: If any of the imports below can't find a module, uncomment the lines above, or try: import set_path \n", "\n", "import pandas as pd\n", "from life123 import check_version, UniformCompartment,PlotlyHelper" ] }, { "cell_type": "code", "execution_count": 4, "id": "4f91f11c-caf8-4ce6-a2c4-dc2a7cab7396", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OK\n" ] } ], "source": [ "check_version(LIFE123_VERSION)" ] }, { "cell_type": "code", "execution_count": null, "id": "22559b04-73d8-4bd2-8c8d-c95dbdb0e862", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3db339d2-4ee0-46b8-a596-419a2b075cc2", "metadata": {}, "source": [ "# 1. Run a simulation with low accuracy" ] }, { "cell_type": "markdown", "id": "9329208b-070f-4902-8f37-0f11ddf75ed6", "metadata": {}, "source": [ "### Initialize the System\n", "Specify the chemicals, the reactions, and the initial concentrations" ] }, { "cell_type": "code", "execution_count": 5, "id": "e328c104-126d-400b-89fe-c4bbe1dc2212", "metadata": {}, "outputs": [], "source": [ "# Instantiate the simulator and specify the chemicals\n", "# Here we use the \"fast\" preset for the variable steps, which leads to fewer steps, but generally less-accurate results\n", "uc_fast = UniformCompartment(preset=\"fast\") " ] }, { "cell_type": "code", "execution_count": 6, "id": "1521d60a-b189-40d1-be43-013a6f5ac510", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "add_reaction(): detected reaction type `ReactionSynthesis`\n", "Number of reactions: 1\n", "0: A + B <-> C Elementary Synthesis reaction (kF = 5 / kR = 2 / delta_G = -2,271.4 / K = 2.5 / Temp = 25 C)\n", "Chemicals involved in the above reactions: ['A', 'B', 'C']\n" ] } ], "source": [ "# Elementary reaction A + B <-> C\n", "uc_fast.add_reaction(reactants=[\"A\" , \"B\"], products=\"C\", kF=5., kR=2.)\n", "\n", "uc_fast.describe_reactions()" ] }, { "cell_type": "code", "execution_count": 7, "id": "ae304704-c8d9-4cef-9e0b-2587bb3909ef", "metadata": {}, "outputs": [], "source": [ "# Set the initial concentrations of all the chemicals\n", "uc_fast.set_conc({\"A\": 10., \"B\": 50., \"C\": 20.})" ] }, { "cell_type": "code", "execution_count": null, "id": "42f757ce-98bb-4d3f-871d-78d059cba2a9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "fc516ca2-e62d-4784-b826-5372ff7f4c75", "metadata": { "tags": [] }, "source": [ "### Run the reaction" ] }, { "cell_type": "code", "execution_count": 8, "id": "2502cd11-0df9-4303-8895-98401a1df7b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "23 total variable step(s) taken in 0.046 sec\n", "Number of step re-do's because of elective soft aborts: 3\n", "Norm usage: {'norm_A': 13, 'norm_B': 9, 'norm_C': 9, 'norm_D': 9}\n", "System Time is now: 0.0674\n" ] } ], "source": [ "uc_fast.single_compartment_react(initial_step=0.004, duration=0.06, variable_steps=True)" ] }, { "cell_type": "code", "execution_count": 9, "id": "80fbaee3-bd6f-4197-9270-23374d46a4a7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | SYSTEM TIME | \n", "A | \n", "B | \n", "C | \n", "step | \n", "caption | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "0.000000 | \n", "10.000000 | \n", "50.000000 | \n", "20.000000 | \n", "\n", " | Set concentration | \n", "
| 1 | \n", "0.000864 | \n", "7.874560 | \n", "47.874560 | \n", "22.125440 | \n", "1 | \n", "1st reaction step | \n", "
| 2 | \n", "0.001555 | \n", "6.602265 | \n", "46.602265 | \n", "23.397735 | \n", "2 | \n", "\n", " |
| 3 | \n", "0.002246 | \n", "5.571266 | \n", "45.571266 | \n", "24.428734 | \n", "3 | \n", "\n", " |
| 4 | \n", "0.002938 | \n", "4.727594 | \n", "44.727594 | \n", "25.272406 | \n", "4 | \n", "\n", " |
| 5 | \n", "0.003629 | \n", "4.031746 | \n", "44.031746 | \n", "25.968254 | \n", "5 | \n", "\n", " |
| 6 | \n", "0.004666 | \n", "3.165305 | \n", "43.165305 | \n", "26.834695 | \n", "6 | \n", "\n", " |
| 7 | \n", "0.005702 | \n", "2.512652 | \n", "42.512652 | \n", "27.487348 | \n", "7 | \n", "\n", " |
| 8 | \n", "0.006739 | \n", "2.015898 | \n", "42.015898 | \n", "27.984102 | \n", "8 | \n", "\n", " |
| 9 | \n", "0.007776 | \n", "1.634842 | \n", "41.634842 | \n", "28.365158 | \n", "9 | \n", "\n", " |
| 10 | \n", "0.008813 | \n", "1.340804 | \n", "41.340804 | \n", "28.659196 | \n", "10 | \n", "\n", " |
| 11 | \n", "0.009850 | \n", "1.112883 | \n", "41.112883 | \n", "28.887117 | \n", "11 | \n", "\n", " |
| 12 | \n", "0.010886 | \n", "0.935595 | \n", "40.935595 | \n", "29.064405 | \n", "12 | \n", "\n", " |
| 13 | \n", "0.011923 | \n", "0.797321 | \n", "40.797321 | \n", "29.202679 | \n", "13 | \n", "\n", " |
| 14 | \n", "0.013478 | \n", "0.635211 | \n", "40.635211 | \n", "29.364789 | \n", "14 | \n", "\n", " |
| 15 | \n", "0.015034 | \n", "0.525833 | \n", "40.525833 | \n", "29.474167 | \n", "15 | \n", "\n", " |
| 16 | \n", "0.016589 | \n", "0.451805 | \n", "40.451805 | \n", "29.548195 | \n", "16 | \n", "\n", " |
| 17 | \n", "0.018922 | \n", "0.376490 | \n", "40.376490 | \n", "29.623510 | \n", "17 | \n", "\n", " |
| 18 | \n", "0.021254 | \n", "0.337393 | \n", "40.337393 | \n", "29.662607 | \n", "18 | \n", "\n", " |
| 19 | \n", "0.024754 | \n", "0.306871 | \n", "40.306871 | \n", "29.693129 | \n", "19 | \n", "\n", " |
| 20 | \n", "0.030002 | \n", "0.293965 | \n", "40.293965 | \n", "29.706035 | \n", "20 | \n", "\n", " |
| 21 | \n", "0.037876 | \n", "0.295437 | \n", "40.295437 | \n", "29.704563 | \n", "21 | \n", "\n", " |
| 22 | \n", "0.049685 | \n", "0.294082 | \n", "40.294082 | \n", "29.705918 | \n", "22 | \n", "\n", " |
| 23 | \n", "0.067400 | \n", "0.296969 | \n", "40.296969 | \n", "29.703031 | \n", "23 | \n", "last reaction step | \n", "
| \n", " | SYSTEM TIME | \n", "A | \n", "B | \n", "C | \n", "step | \n", "caption | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "0.000000 | \n", "10.000000 | \n", "50.000000 | \n", "20.000000 | \n", "\n", " | Set concentration | \n", "
| 1 | \n", "0.000160 | \n", "9.606400 | \n", "49.606400 | \n", "20.393600 | \n", "1 | \n", "1st reaction step | \n", "
| 2 | \n", "0.000336 | \n", "9.194224 | \n", "49.194224 | \n", "20.805776 | \n", "2 | \n", "\n", " |
| 3 | \n", "0.000530 | \n", "8.764451 | \n", "48.764451 | \n", "21.235549 | \n", "3 | \n", "\n", " |
| 4 | \n", "0.000743 | \n", "8.318407 | \n", "48.318407 | \n", "21.681593 | \n", "4 | \n", "\n", " |
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 76 | \n", "0.045329 | \n", "0.294997 | \n", "40.294997 | \n", "29.705003 | \n", "76 | \n", "\n", " |
| 77 | \n", "0.048708 | \n", "0.294914 | \n", "40.294914 | \n", "29.705086 | \n", "77 | \n", "\n", " |
| 78 | \n", "0.052424 | \n", "0.294886 | \n", "40.294886 | \n", "29.705114 | \n", "78 | \n", "\n", " |
| 79 | \n", "0.056511 | \n", "0.294879 | \n", "40.294879 | \n", "29.705121 | \n", "79 | \n", "\n", " |
| 80 | \n", "0.061008 | \n", "0.294878 | \n", "40.294878 | \n", "29.705122 | \n", "80 | \n", "last reaction step | \n", "
81 rows × 6 columns
\n", "| \n", " | SYSTEM TIME | \n", "A | \n", "B | \n", "C | \n", "step | \n", "caption | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "0.000000 | \n", "10.000000 | \n", "50.000000 | \n", "20.000000 | \n", "\n", " | Set concentration | \n", "
| 1 | \n", "0.000160 | \n", "9.615719 | \n", "49.615719 | \n", "20.384281 | \n", "1 | \n", "1st reaction step | \n", "
| 2 | \n", "0.000336 | \n", "9.213647 | \n", "49.213647 | \n", "20.786353 | \n", "2 | \n", "\n", " |
| 3 | \n", "0.000530 | \n", "8.794750 | \n", "48.794750 | \n", "21.205250 | \n", "3 | \n", "\n", " |
| 4 | \n", "0.000743 | \n", "8.360318 | \n", "48.360318 | \n", "21.639682 | \n", "4 | \n", "\n", " |
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 75 | \n", "0.045358 | \n", "0.295598 | \n", "40.295598 | \n", "29.704402 | \n", "75 | \n", "\n", " |
| 76 | \n", "0.048737 | \n", "0.295238 | \n", "40.295238 | \n", "29.704762 | \n", "76 | \n", "\n", " |
| 77 | \n", "0.052453 | \n", "0.295046 | \n", "40.295046 | \n", "29.704954 | \n", "77 | \n", "\n", " |
| 78 | \n", "0.056540 | \n", "0.294950 | \n", "40.294950 | \n", "29.705050 | \n", "78 | \n", "\n", " |
| 79 | \n", "0.061037 | \n", "0.294906 | \n", "40.294906 | \n", "29.705094 | \n", "79 | \n", "last reaction step | \n", "
80 rows × 6 columns
\n", "