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.chemicals.chem_data import ChemData as chem
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_2"],
extra_js="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.21.2/cytoscape.umd.js")
-> Output will be LOGGED into the file 'reaction_2.log.htm'
# Initialize the system
chem_data = chem(names=["A", "B"]) # NOTE: Diffusion not applicable (just 1 bin)
# Reaction A <-> 3B , with 1st-order kinetics in both directions
chem_data.add_reaction(reactants=["A"], products=[(3,"B",1)], forward_rate=5., reverse_rate=2.)
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.describe_state()
SYSTEM STATE at Time t = 0: 1 bins and 2 species: Species 0 (A). Diff rate: None. Conc: [10.] Species 1 (B). Diff rate: None. Conc: [50.]
chem_data.describe_reactions()
Number of reactions: 1 (at temp. 25 C)
0: A <-> 3 B (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', 'A'}
# 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 | caption | |
|---|---|---|---|---|
| 0 | 0 | 10.0 | 50.0 | Initial state |
# Send the plot to the HTML log file
chem_data.plot_reaction_network("vue_cytoscape_2")
[GRAPHIC ELEMENT SENT TO LOG FILE `reaction_2.log.htm`]
# First step
bio.react(time_step=0.1, n_steps=1, snapshots={"sample_bin": 0})
bio.describe_state()
SYSTEM STATE at Time t = 0.1: 1 bins and 2 species: Species 0 (A). Diff rate: None. Conc: [15.] Species 1 (B). Diff rate: None. Conc: [35.]
Early in the reaction :
[A] = 15. [B] = 35.
bio.get_history()
| SYSTEM TIME | A | B | caption | |
|---|---|---|---|---|
| 0 | 0.0 | 10.0 | 50.0 | Initial state |
| 1 | 0.1 | 15.0 | 35.0 |
# Numerous more steps
bio.react(time_step=0.1, n_steps=10, snapshots={"sample_bin": 0})
bio.describe_state()
SYSTEM STATE at Time t = 1.1: 1 bins and 2 species: Species 0 (A). Diff rate: None. Conc: [14.54545455] Species 1 (B). Diff rate: None. Conc: [36.36363636]
Consistent with the 5/2 ratio of forward/reverse rates (and the 1st order reactions), the systems settles in the equilibrium: [A] = 14.54545455 , [B] = 36.36363636
# Verify that the reaction has reached equilibrium
bio.reaction_dynamics.is_in_equilibrium(conc=bio.bin_snapshot(bin_address = 0))
0: A <-> 3 B
Final concentrations: [A] = 14.55 ; [B] = 36.36
1. Ratio of reactant/product concentrations, adjusted for reaction orders: 2.5
Formula used: [B] / [A]
2. Ratio of forward/reverse reaction rates: 2.5
Discrepancy between the two values: 6.875e-10 %
Reaction IS in equilibrium (within 1% tolerance)
True
bio.get_history()
| SYSTEM TIME | A | B | caption | |
|---|---|---|---|---|
| 0 | 0.0 | 10.000000 | 50.000000 | Initial state |
| 1 | 0.1 | 15.000000 | 35.000000 | |
| 2 | 0.2 | 14.500000 | 36.500000 | |
| 3 | 0.3 | 14.550000 | 36.350000 | |
| 4 | 0.4 | 14.545000 | 36.365000 | |
| 5 | 0.5 | 14.545500 | 36.363500 | |
| 6 | 0.6 | 14.545450 | 36.363650 | |
| 7 | 0.7 | 14.545455 | 36.363635 | |
| 8 | 0.8 | 14.545454 | 36.363637 | |
| 9 | 0.9 | 14.545455 | 36.363636 | |
| 10 | 1.0 | 14.545455 | 36.363636 | |
| 11 | 1.1 | 14.545455 | 36.363636 |
Note how the simulation initially OVERSHOT the equilibrium values; the first step was too large!
fig = px.line(data_frame=bio.get_history(), x="SYSTEM TIME", y=["A", "B"],
title="Changes in concentrations with time",
color_discrete_sequence = ['navy', 'darkorange'],
labels={"value":"concentration", "variable":"Chemical"})
fig.show()
# Same plot, but with smooth line
fig = px.line(data_frame=bio.get_history(), x="SYSTEM TIME", y=["A", "B"],
title="Changes in concentrations with time (smoothed)",
color_discrete_sequence = ['navy', 'darkorange'],
labels={"value":"concentration", "variable":"Chemical"},
line_shape="spline")
fig.show()
The early OVERSHOOTING of the equilibrium values shows prominently in the last plot!