LAST_REVISED = "June 6, 2025"
LIFE123_VERSION = "1.0.0rc6" # Library version this experiment is based on
#import set_path # Using MyBinder? Uncomment this before running the next cell!
#import sys, os
#os.getcwd()
#sys.path.append("C:/some_path/my_env_or_install") # CHANGE to the folder containing your venv or libraries installation!
# NOTE: If any of the imports below can't find a module, uncomment the lines above, or try: import set_path
from experiments.get_notebook_info import get_notebook_basename
from life123 import ChemData, BioSim1D, check_version
from life123 import GraphicLog
check_version(LIFE123_VERSION)
OK
# Initialize the HTML logging
log_file = get_notebook_basename() + ".log.htm" # Use the notebook base filename for the log file
# Set up the use of some specified graphic (Vue) components
GraphicLog.config(filename=log_file,
components=["vue_cytoscape_2"],
extra_js="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.21.2/cytoscape.umd.js")
-> Output will be LOGGED into the file 'reaction_4.log.htm'
# Initialize the system. NOTE: Diffusion not applicable (using just 1 bin)
chem_data = ChemData(names=["A", "B", "C"], plot_colors=['red', 'darkorange', 'green'])
bio = BioSim1D(n_bins=1, chem_data=chem_data)
bio.set_uniform_concentration(chem_index=0, conc=10.)
bio.set_uniform_concentration(chem_index=1, conc=50.)
bio.set_uniform_concentration(chem_index=2, conc=20.)
bio.describe_state()
SYSTEM STATE at Time t = 0: 1 bins and 3 chemical species
| Species | Diff rate | Bin 0 | |
|---|---|---|---|
| 0 | A | None | 10.0 |
| 1 | B | None | 50.0 |
| 2 | C | None | 20.0 |
# Specify the reaction
reactions = bio.get_reactions()
# Reaction A + B <-> C , with 1st-order kinetics for each species
reactions.add_reaction(reactants=["A" , "B"], products="C",
forward_rate=5., reverse_rate=2.)
reactions.describe_reactions()
Number of reactions: 1 (at temp. 25 C)
0: A + B <-> C (kF = 5 / kR = 2 / delta_G = -2,271.4 / K = 2.5) | 1st order in all reactants & products
Set of chemicals involved in the above reactions: {"B" (darkorange), "C" (green), "A" (red)}
# Send the plot of the reaction network to the HTML log file
reactions.plot_reaction_network("vue_cytoscape_2")
[GRAPHIC ELEMENT SENT TO LOG FILE `reaction_4.log.htm`]
# Let's enable history - by default for all chemicals and all bins
bio.enable_history(take_snapshot=True, caption="Initial state")
History enabled for bins None and chemicals None (None means 'all')
bio.get_bin_history(bin_address=0)
| SYSTEM TIME | A | B | C | caption | |
|---|---|---|---|---|---|
| 0 | 0.0 | 10.0 | 50.0 | 20.0 | Initial state |
# First step
bio.react(time_step=0.002, n_steps=1)
bio.describe_state()
System Time is now: 0.002 SYSTEM STATE at Time t = 0.002: 1 bins and 3 chemical species
| Species | Diff rate | Bin 0 | |
|---|---|---|---|
| 0 | A | None | 5.08 |
| 1 | B | None | 45.08 |
| 2 | C | None | 24.92 |
bio.get_bin_history(bin_address=0)
| SYSTEM TIME | A | B | C | caption | |
|---|---|---|---|---|---|
| 0 | 0.000 | 10.00 | 50.00 | 20.00 | Initial state |
| 1 | 0.002 | 5.08 | 45.08 | 24.92 |
# Numerous more steps
bio.react(time_step=0.002, n_steps=29)
bio.describe_state()
System Time is now: 0.06 SYSTEM STATE at Time t = 0.06: 1 bins and 3 chemical species
| Species | Diff rate | Bin 0 | |
|---|---|---|---|
| 0 | A | None | 0.294878 |
| 1 | B | None | 40.294878 |
| 2 | C | None | 29.705122 |
Consistent with the 5/2 ratio of forward/reverse rates (and the 1st order reactions),
the systems settles in the following equilibrium:
[A] = 0.29487831 , [B] = 40.29487831 , [C] = 29.70512169
# Verify that the reaction has reached equilibrium
bio.get_reaction_handler().is_in_equilibrium(rxn_index=0, conc=bio.bin_snapshot(bin_address = 0))
A + B <-> C
Current concentrations: [A] = 0.2949 ; [B] = 40.29 ; [C] = 29.71
1. Ratio of reactant/product concentrations, adjusted for reaction orders: 2.49999
Formula used: [C] / ([A][B])
2. Ratio of forward/reverse reaction rates: 2.5
Discrepancy between the two values: 0.0003107 %
Reaction IS in equilibrium (within 1% tolerance)
True
bio.get_bin_history(bin_address=0)
| SYSTEM TIME | A | B | C | caption | |
|---|---|---|---|---|---|
| 0 | 0.000 | 10.000000 | 50.000000 | 20.000000 | Initial state |
| 1 | 0.002 | 5.080000 | 45.080000 | 24.920000 | |
| 2 | 0.004 | 2.889616 | 42.889616 | 27.110384 | |
| 3 | 0.006 | 1.758712 | 41.758712 | 28.241288 | |
| 4 | 0.008 | 1.137262 | 41.137262 | 28.862738 | |
| 5 | 0.010 | 0.784874 | 40.784874 | 29.215126 | |
| 6 | 0.012 | 0.581625 | 40.581625 | 29.418375 | |
| 7 | 0.014 | 0.463266 | 40.463266 | 29.536734 | |
| 8 | 0.016 | 0.393960 | 40.393960 | 29.606040 | |
| 9 | 0.018 | 0.353248 | 40.353248 | 29.646752 | |
| 10 | 0.020 | 0.329288 | 40.329288 | 29.670712 | |
| 11 | 0.022 | 0.315171 | 40.315171 | 29.684829 | |
| 12 | 0.024 | 0.306849 | 40.306849 | 29.693151 | |
| 13 | 0.026 | 0.301940 | 40.301940 | 29.698060 | |
| 14 | 0.028 | 0.299045 | 40.299045 | 29.700955 | |
| 15 | 0.030 | 0.297336 | 40.297336 | 29.702664 | |
| 16 | 0.032 | 0.296328 | 40.296328 | 29.703672 | |
| 17 | 0.034 | 0.295734 | 40.295734 | 29.704266 | |
| 18 | 0.036 | 0.295383 | 40.295383 | 29.704617 | |
| 19 | 0.038 | 0.295176 | 40.295176 | 29.704824 | |
| 20 | 0.040 | 0.295053 | 40.295053 | 29.704947 | |
| 21 | 0.042 | 0.294981 | 40.294981 | 29.705019 | |
| 22 | 0.044 | 0.294939 | 40.294939 | 29.705061 | |
| 23 | 0.046 | 0.294914 | 40.294914 | 29.705086 | |
| 24 | 0.048 | 0.294899 | 40.294899 | 29.705101 | |
| 25 | 0.050 | 0.294890 | 40.294890 | 29.705110 | |
| 26 | 0.052 | 0.294885 | 40.294885 | 29.705115 | |
| 27 | 0.054 | 0.294882 | 40.294882 | 29.705118 | |
| 28 | 0.056 | 0.294880 | 40.294880 | 29.705120 | |
| 29 | 0.058 | 0.294879 | 40.294879 | 29.705121 | |
| 30 | 0.060 | 0.294878 | 40.294878 | 29.705122 |
A (now largely depleted) is largely the limiting reagent¶bio.plot_history_single_bin(bin_address=0,
title_prefix="Reaction `A + B <-> C`")