{ "cells": [ { "cell_type": "markdown", "id": "d0259ef8", "metadata": { "lines_to_next_cell": 0 }, "source": [ "## `A <-> B` reaction, with 1st-order kinetics in both directions,\n", "### taken to equilibrium\n", "\n", "Diffusion NOT done" ] }, { "cell_type": "markdown", "id": "63cc2f2f-d73c-4f47-b9d8-bf6545e0d24d", "metadata": {}, "source": [ "### TAGS : \"reactions 2D\"" ] }, { "cell_type": "code", "execution_count": 1, "id": "514caa8d-0b1b-4526-af21-96f7cbcb0fe6", "metadata": {}, "outputs": [], "source": [ "LAST_REVISED = \"Dec. 16, 2024\"\n", "LIFE123_VERSION = \"1.0-rc.1\" # Library version this experiment is based on" ] }, { "cell_type": "code", "execution_count": 2, "id": "7da2e6ab-2b18-4952-b2eb-1e2ce61bfb36", "metadata": {}, "outputs": [], "source": [ "#import set_path # Using MyBinder? Uncomment this before running the next cell!" ] }, { "cell_type": "code", "execution_count": 3, "id": "922f1ee7", "metadata": {}, "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", "from experiments.get_notebook_info import get_notebook_basename\n", "\n", "from life123 import UniformCompartment, BioSim2D, GraphicLog" ] }, { "cell_type": "code", "execution_count": 4, "id": "48630be5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-> Output will be LOGGED into the file 'reaction_1.log.htm'\n" ] } ], "source": [ "# Initialize the HTML logging\n", "log_file = get_notebook_basename() + \".log.htm\" # Use the notebook base filename for the log file\n", "# Set up the use of some specified graphic (Vue) components\n", "GraphicLog.config(filename=log_file,\n", " components=[\"vue_cytoscape_2\"],\n", " extra_js=\"https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.21.2/cytoscape.umd.js\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "12ad037c-fd9b-43e5-b59d-22f3010951f0", "metadata": {}, "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": [ "# Initialize the system. NOTE: Diffusion not done\n", "uc = UniformCompartment(names=[\"A\", \"B\"])\n", "\n", "# Reaction A <-> B , with 1st-order kinetics in both directions\n", "uc.add_reaction(reactants=\"A\", products=\"B\", forward_rate=3., reverse_rate=2.)\n", "\n", "uc.describe_reactions()" ] }, { "cell_type": "code", "execution_count": 6, "id": "bad0134c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[GRAPHIC ELEMENT SENT TO LOG FILE `reaction_1.log.htm`]\n" ] } ], "source": [ "# Send the plot to the HTML log file\n", "uc.plot_reaction_network(\"vue_cytoscape_2\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "e927653f-e165-4203-8f0f-c8f2d8876265", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SYSTEM STATE at Time t = 0:\n", "Species `A`:\n", " 0 1 2 3\n", "0 10.0 20.0 0.0 0.0\n", "1 0.0 0.0 0.0 0.0\n", "2 0.0 0.0 0.0 5.0\n", "Species `B`:\n", " 0 1 2 3\n", "0 50.0 35.0 0.0 0.0\n", "1 0.0 0.0 0.0 0.0\n", "2 0.0 0.0 0.0 100.0\n" ] } ], "source": [ "bio = BioSim2D(n_bins=(3,4), reaction_handler=uc)\n", "\n", "bio.set_bin_conc_all_species(bin_x=0, bin_y=0, conc_list=[10.,50.])\n", "bio.set_bin_conc_all_species(bin_x=0, bin_y=1, conc_list=[20.,35.])\n", "bio.set_bin_conc_all_species(bin_x=2, bin_y=3, conc_list=[5.,100.])\n", "\n", "bio.describe_state()" ] }, { "cell_type": "code", "execution_count": null, "id": "23137f05-bdc3-46a6-9101-c0f0d98071a5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "0b8318bf", "metadata": { "tags": [] }, "source": [ "## First step" ] }, { "cell_type": "code", "execution_count": 8, "id": "97f6ad20", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bio.delta_reactions:\n", " [[[ 7. 1. 0. 0. ]\n", " [ 0. 0. 0. 0. ]\n", " [ 0. 0. 0. 18.5]]\n", "\n", " [[ -7. -1. 0. 0. ]\n", " [ 0. 0. 0. 0. ]\n", " [ 0. 0. 0. -18.5]]]\n" ] } ], "source": [ "# First step (NOTE: here we're using a lower-level function that doesn't update the system state;\n", "# it only computes the delta_reactions array)\n", "bio.reaction_step(delta_time=0.1)\n", "print(\"bio.delta_reactions:\\n\", bio.delta_reactions)" ] }, { "cell_type": "code", "execution_count": 9, "id": "93b53e77", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SYSTEM STATE at Time t = 0.1:\n", "Species `A`:\n", " 0 1 2 3\n", "0 17.0 21.0 0.0 0.0\n", "1 0.0 0.0 0.0 0.0\n", "2 0.0 0.0 0.0 23.5\n", "Species `B`:\n", " 0 1 2 3\n", "0 43.0 34.0 0.0 0.0\n", "1 0.0 0.0 0.0 0.0\n", "2 0.0 0.0 0.0 81.5\n" ] } ], "source": [ "bio.system += bio.delta_reactions # Matrix operation to update all the concentrations\n", "bio.system_time += 0.1\n", "\n", "bio.describe_state()" ] }, { "cell_type": "markdown", "id": "1dfcfa0b-ae89-4fdc-af96-54ada6f7f555", "metadata": { "tags": [] }, "source": [ "## Second step" ] }, { "cell_type": "code", "execution_count": 10, "id": "4a32fd45-f20d-4bed-99cb-3b611656e1be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SYSTEM STATE at Time t = 0.2:\n", "Species `A`:\n", " 0 1 2 3\n", "0 20.5 21.5 0.0 0.00\n", "1 0.0 0.0 0.0 0.00\n", "2 0.0 0.0 0.0 32.75\n", "Species `B`:\n", " 0 1 2 3\n", "0 39.5 33.5 0.0 0.00\n", "1 0.0 0.0 0.0 0.00\n", "2 0.0 0.0 0.0 72.25\n" ] } ], "source": [ "# NOTE: now, we're using a highel-level function that also updates the system state\n", "bio.react(time_step=0.1, n_steps=1)\n", "bio.describe_state()" ] }, { "cell_type": "markdown", "id": "c8f2dcc9-7343-47b0-9e2c-4157add49e32", "metadata": {}, "source": [ "## Many more steps, to equilibrium" ] }, { "cell_type": "code", "execution_count": 11, "id": "42be106a-095e-4d0e-8c24-a8d03370eff7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SYSTEM STATE at Time t = 0.9999999999999999:\n", "Species `A`:\n", " 0 1 2 3\n", "0 23.986328 21.998047 0.0 0.000000\n", "1 0.000000 0.000000 0.0 0.000000\n", "2 0.000000 0.000000 0.0 41.963867\n", "Species `B`:\n", " 0 1 2 3\n", "0 36.013672 33.001953 0.0 0.000000\n", "1 0.000000 0.000000 0.0 0.000000\n", "2 0.000000 0.000000 0.0 63.036133\n" ] } ], "source": [ "bio.react(time_step=0.1, n_steps=8)\n", "bio.describe_state()" ] }, { "cell_type": "code", "execution_count": 12, "id": "51fefb11-5382-4e2e-87e4-f175884e9fcf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SYSTEM STATE at Time t = 2.0000000000000004:\n", "Species `A`:\n", " 0 1 2 3\n", "0 23.999987 21.999998 0.0 0.000000\n", "1 0.000000 0.000000 0.0 0.000000\n", "2 0.000000 0.000000 0.0 41.999965\n", "Species `B`:\n", " 0 1 2 3\n", "0 36.000013 33.000002 0.0 0.000000\n", "1 0.000000 0.000000 0.0 0.000000\n", "2 0.000000 0.000000 0.0 63.000035\n" ] } ], "source": [ "bio.react(time_step=0.1, n_steps=10)\n", "bio.describe_state()" ] }, { "cell_type": "markdown", "id": "9b66fc29-20a2-4be9-9248-5fff187aa773", "metadata": {}, "source": [ "### The system has now reached equilibrium\n", "### in individual bins, which remain separate because we're NOT doing diffusion in this experiment" ] }, { "cell_type": "markdown", "id": "4458f3e5-3cd5-4f88-a79f-0dc134bcf309", "metadata": {}, "source": [ "Verify the equilibrium in each of the active bins" ] }, { "cell_type": "code", "execution_count": 13, "id": "a0d8cc78-870c-45af-90dc-10f50bf3bc40", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A <-> B\n", "Final concentrations: [A] = 24 ; [B] = 36\n", "1. Ratio of reactant/product concentrations, adjusted for reaction orders: 1.5\n", " Formula used: [B] / [A]\n", "2. Ratio of forward/reverse reaction rates: 1.5\n", "Discrepancy between the two values: 9.271e-05 %\n", "Reaction IS in equilibrium (within 1% tolerance)\n", "\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={\"A\": 23.99998665, \"B\": 36.00001335})" ] }, { "cell_type": "code", "execution_count": 14, "id": "46a41b53-b4c5-4de3-84d5-3fd1024c2652", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A <-> B\n", "Final concentrations: [A] = 22 ; [B] = 33\n", "1. Ratio of reactant/product concentrations, adjusted for reaction orders: 1.5\n", " Formula used: [B] / [A]\n", "2. Ratio of forward/reverse reaction rates: 1.5\n", "Discrepancy between the two values: 1.447e-05 %\n", "Reaction IS in equilibrium (within 1% tolerance)\n", "\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={\"A\": 21.99999809, \"B\": 33.00000191})" ] }, { "cell_type": "code", "execution_count": 15, "id": "91e85f0a-a186-408f-864e-08bc6acb9591", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={\"A\": 41.99996471, \"B\": 63.00003529}, explain=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "6c75c6ba", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }