import set_path # Importing this module will add the project's home directory to sys.path
Added 'D:\Docs\- MY CODE\BioSimulations\life123-Win7' to sys.path
from experiments.get_notebook_info import get_notebook_basename
from src.modules.reactions.reaction_data import ReactionData as chem
from src.modules.reactions.reaction_dynamics import ReactionDynamics
from src.life_1D.bio_sim_1d import BioSim1D
import plotly.express as px
from src.modules.visualization.graphic_log import GraphicLog
# 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_1"],
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'
# Specify the chemicals
chem_data = chem(names=["A", "B", "C"]) # NOTE: Diffusion not applicable (using just 1 bin)
# Reaction A + B <-> C , with 1st-order kinetics for each species
chem_data.add_reaction(reactants=["A" , "B"], products=["C"],
forward_rate=5., reverse_rate=2.)
chem_data.describe_reactions()
Number of reactions: 1 (at temp. 25 C) 0: A + B <-> C (kF = 5 / kR = 2 / Delta_G = -2,271.45 / K = 2.5) | 1st order in all reactants & products
# Initialize the system
bio = BioSim1D(n_bins=1, chem_data=chem_data)
bio.set_uniform_concentration(species_index=0, conc=10.)
bio.set_uniform_concentration(species_index=1, conc=50.)
bio.set_uniform_concentration(species_index=2, conc=20.)
bio.describe_state()
SYSTEM STATE at Time t = 0: 1 bins and 3 species: Species 0 (A). Diff rate: None. Conc: [10.] Species 1 (B). Diff rate: None. Conc: [50.] Species 2 (C). Diff rate: None. Conc: [20.]
# Save the state of the concentrations of all species at bin 0
bio.add_snapshot(bio.bin_snapshot(bin_address = 0), caption="Initial state")
bio.get_history()
| SYSTEM TIME | A | B | C | caption | |
|---|---|---|---|---|---|
| 0 | 0 | 10.0 | 50.0 | 20.0 | Initial state |
# Send the plot to the HTML log file
graph_data = chem_data.prepare_graph_network()
GraphicLog.export_plot(graph_data, "vue_cytoscape_1")
[GRAPHIC ELEMENT SENT TO LOG FILE `reaction_4.log.htm`]
# First step
bio.react(time_step=0.002, n_steps=1)
bio.describe_state()
SYSTEM STATE at Time t = 0.002: 1 bins and 3 species: Species 0 (A). Diff rate: None. Conc: [5.08] Species 1 (B). Diff rate: None. Conc: [45.08] Species 2 (C). Diff rate: None. Conc: [24.92]
Early in the reaction : [A] = 5.08 , [B] = 45.08 , [C] = [24.92]
# Save the state of the concentrations of all species at bin 0
bio.add_snapshot(bio.bin_snapshot(bin_address = 0))
bio.get_history()
| 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, snapshots={"sample_bin": 0})
bio.describe_state()
SYSTEM STATE at Time t = 0.06: 1 bins and 3 species: Species 0 (A). Diff rate: None. Conc: [0.29487831] Species 1 (B). Diff rate: None. Conc: [40.29487831] Species 2 (C). Diff rate: None. Conc: [29.70512169]
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.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc=bio.bin_snapshot(bin_address = 0))
A + B <-> C
Final concentrations: [C] = 29.71 ; [A] = 0.2949 ; [B] = 40.29
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
# Save the state of the concentrations of all species at bin 0
bio.get_history()
| 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 |
fig = px.line(data_frame=bio.get_history(), x="SYSTEM TIME", y=["A", "B", "C"],
title="Reaction A + B <-> C . Changes in concentrations with time",
color_discrete_sequence = ['red', 'violet', 'green'],
labels={"value":"concentration", "variable":"Chemical"})
fig.show()