{ "cells": [ { "cell_type": "markdown", "id": "5cbc8640", "metadata": {}, "source": [ "### A minimalist demonstration for the reaction `A <-> B`,\n", "with 1st-order kinetics in both directions, taken to equilibrium.\n", "\n", "\"No frills!\" For advanced graphics, analysis, diagnostics, fine-tuning, etc, please see other experiments.\n", "\n", "LAST REVISED: Dec. 3, 2023" ] }, { "cell_type": "code", "execution_count": 1, "id": "3792d78d-7429-4221-a263-57b07d77b5bc", "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": "a29db1c7", "metadata": { "tags": [] }, "outputs": [], "source": [ "from src.modules.reactions.reaction_dynamics import ReactionDynamics" ] }, { "cell_type": "code", "execution_count": null, "id": "ac9eea69-174c-43e5-9eed-443cbc5e2ba7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e0529a0c", "metadata": {}, "source": [ "## Initialize the System" ] }, { "cell_type": "code", "execution_count": 3, "id": "78077d8c", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of reactions: 1 (at temp. 25 C)\n", "0: A <-> B (kF = 3 / kR = 2 / delta_G = -1,005.1 / K = 1.5) | 1st order in all reactants & products\n", "Set of chemicals involved in the above reactions: {'B', 'A'}\n" ] } ], "source": [ "# Instantiate the simulator and specify the chemicals\n", "dynamics = ReactionDynamics(names=[\"A\", \"B\"])\n", "\n", "# Reaction A <-> B , with 1st-order kinetics in both directions\n", "dynamics.add_reaction(reactants=\"A\", products=\"B\", \n", " forward_rate=3., reverse_rate=2.)\n", "\n", "dynamics.describe_reactions()" ] }, { "cell_type": "code", "execution_count": 4, "id": "9fc3948d", "metadata": {}, "outputs": [], "source": [ "# Set the initial concentrations of all the chemicals, in their declared index order\n", "dynamics.set_conc([80., 10.])" ] }, { "cell_type": "markdown", "id": "987af2c5", "metadata": { "tags": [] }, "source": [ "## Run the reaction" ] }, { "cell_type": "code", "execution_count": 5, "id": "43735178-313b-48cf-a583-5181238feac3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* INFO: the tentative time step (0.1) 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.04) [Step started at t=0, and will rewind there]\n", "* INFO: the tentative time step (0.04) 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.016) [Step started at t=0, and will rewind there]\n", "* INFO: the tentative time step (0.016) 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.0064) [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", "47 total step(s) taken\n" ] } ], "source": [ "dynamics.single_compartment_react(initial_step=0.1, target_end_time=1.) # Using defaults for all other parameters" ] }, { "cell_type": "code", "execution_count": 6, "id": "2d5df59c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | SYSTEM TIME | \n", "A | \n", "B | \n", "caption | \n", "
|---|---|---|---|---|
| 0 | \n", "0.000000 | \n", "80.000000 | \n", "10.000000 | \n", "Initial state | \n", "
| 1 | \n", "0.006400 | \n", "78.592000 | \n", "11.408000 | \n", "\n", " |
| 2 | \n", "0.009600 | \n", "77.910528 | \n", "12.089472 | \n", "\n", " |
| 3 | \n", "0.013440 | \n", "77.105846 | \n", "12.894154 | \n", "\n", " |
| 4 | \n", "0.018048 | \n", "76.158767 | \n", "13.841233 | \n", "\n", " |
| 5 | \n", "0.023578 | \n", "75.048458 | \n", "14.951542 | \n", "\n", " |
| 6 | \n", "0.029107 | \n", "73.968846 | \n", "16.031154 | \n", "\n", " |
| 7 | \n", "0.034637 | \n", "72.919083 | \n", "17.080917 | \n", "\n", " |
| 8 | \n", "0.040166 | \n", "71.898344 | \n", "18.101656 | \n", "\n", " |
| 9 | \n", "0.045696 | \n", "70.905827 | \n", "19.094173 | \n", "\n", " |
| 10 | \n", "0.052332 | \n", "69.747735 | \n", "20.252265 | \n", "\n", " |
| 11 | \n", "0.058967 | \n", "68.628067 | \n", "21.371933 | \n", "\n", " |
| 12 | \n", "0.065603 | \n", "67.545546 | \n", "22.454454 | \n", "\n", " |
| 13 | \n", "0.072238 | \n", "66.498940 | \n", "23.501060 | \n", "\n", " |
| 14 | \n", "0.078874 | \n", "65.487058 | \n", "24.512942 | \n", "\n", " |
| 15 | \n", "0.085509 | \n", "64.508749 | \n", "25.491251 | \n", "\n", " |
| 16 | \n", "0.093472 | \n", "63.373726 | \n", "26.626274 | \n", "\n", " |
| 17 | \n", "0.101434 | \n", "62.283893 | \n", "27.716107 | \n", "\n", " |
| 18 | \n", "0.109397 | \n", "61.237449 | \n", "28.762551 | \n", "\n", " |
| 19 | \n", "0.117360 | \n", "60.232668 | \n", "29.767332 | \n", "\n", " |
| 20 | \n", "0.125322 | \n", "59.267889 | \n", "30.732111 | \n", "\n", " |
| 21 | \n", "0.134877 | \n", "58.156249 | \n", "31.843751 | \n", "\n", " |
| 22 | \n", "0.144433 | \n", "57.097717 | \n", "32.902283 | \n", "\n", " |
| 23 | \n", "0.153988 | \n", "56.089758 | \n", "33.910242 | \n", "\n", " |
| 24 | \n", "0.163543 | \n", "55.129955 | \n", "34.870045 | \n", "\n", " |
| 25 | \n", "0.175009 | \n", "54.033218 | \n", "35.966782 | \n", "\n", " |
| 26 | \n", "0.186475 | \n", "52.999357 | \n", "37.000643 | \n", "\n", " |
| 27 | \n", "0.197941 | \n", "52.024769 | \n", "37.975231 | \n", "\n", " |
| 28 | \n", "0.211701 | \n", "50.922312 | \n", "39.077688 | \n", "\n", " |
| 29 | \n", "0.225460 | \n", "49.895700 | \n", "40.104300 | \n", "\n", " |
| 30 | \n", "0.239220 | \n", "48.939717 | \n", "41.060283 | \n", "\n", " |
| 31 | \n", "0.255731 | \n", "47.871459 | \n", "42.128541 | \n", "\n", " |
| 32 | \n", "0.272242 | \n", "46.891393 | \n", "43.108607 | \n", "\n", " |
| 33 | \n", "0.292056 | \n", "45.812407 | \n", "44.187593 | \n", "\n", " |
| 34 | \n", "0.311869 | \n", "44.840314 | \n", "45.159686 | \n", "\n", " |
| 35 | \n", "0.335646 | \n", "43.789365 | \n", "46.210635 | \n", "\n", " |
| 36 | \n", "0.359422 | \n", "42.863355 | \n", "47.136645 | \n", "\n", " |
| 37 | \n", "0.387953 | \n", "41.884245 | \n", "48.115755 | \n", "\n", " |
| 38 | \n", "0.422191 | \n", "40.876927 | \n", "49.123073 | \n", "\n", " |
| 39 | \n", "0.456429 | \n", "40.042050 | \n", "49.957950 | \n", "\n", " |
| 40 | \n", "0.497514 | \n", "39.211704 | \n", "50.788296 | \n", "\n", " |
| 41 | \n", "0.546817 | \n", "38.419979 | \n", "51.580021 | \n", "\n", " |
| 42 | \n", "0.605980 | \n", "37.704113 | \n", "52.295887 | \n", "\n", " |
| 43 | \n", "0.676975 | \n", "37.099191 | \n", "52.900809 | \n", "\n", " |
| 44 | \n", "0.762170 | \n", "36.630965 | \n", "53.369035 | \n", "\n", " |
| 45 | \n", "0.864404 | \n", "36.308436 | \n", "53.691564 | \n", "\n", " |
| 46 | \n", "0.987084 | \n", "36.119241 | \n", "53.880759 | \n", "\n", " |
| 47 | \n", "1.134300 | \n", "36.031470 | \n", "53.968530 | \n", "\n", " |