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_2D.bio_sim_2d import BioSim2D
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_1.log.htm'
# Initialize the system
chem_data = chem(names=["A", "B"]) # NOTE: Diffusion not done
# Reaction A <-> B , with 1st-order kinetics in both directions
chem_data.add_reaction(reactants=["A"], products=["B"], forward_rate=3., reverse_rate=2.)
bio = BioSim2D(n_bins=(3,4), chem_data=chem_data)
bio.set_bin_conc_all_species(bin_x=0, bin_y=0, conc_list=[10.,50.])
bio.set_bin_conc_all_species(bin_x=0, bin_y=1, conc_list=[20.,35.])
bio.set_bin_conc_all_species(bin_x=2, bin_y=3, conc_list=[5.,100.])
bio.describe_state()
SYSTEM STATE at Time t = 0:
Species `A`:
0 1 2 3
0 10.0 20.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 5.0
Species `B`:
0 1 2 3
0 50.0 35.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 100.0
chem_data.describe_reactions()
Number of reactions: 1 (at temp. 25 C) 0: A <-> B (kF = 3 / kR = 2 / Delta_G = -1,005.13 / K = 1.5) | 1st order in all reactants & products
# 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_1.log.htm`]
# First step (NOTE: here we're using a lower-level function that doesn't update the system state;
# it only computes the delta_reactions array)
bio.reaction_step(delta_time=0.1)
print("bio.delta_reactions:\n", bio.delta_reactions)
bio.delta_reactions: [[[ 7. 1. 0. 0. ] [ 0. 0. 0. 0. ] [ 0. 0. 0. 18.5]] [[ -7. -1. 0. 0. ] [ 0. 0. 0. 0. ] [ 0. 0. 0. -18.5]]]
bio.system += bio.delta_reactions # Matrix operation to update all the concentrations
bio.system_time += 0.1
bio.describe_state()
SYSTEM STATE at Time t = 0.1:
Species `A`:
0 1 2 3
0 17.0 21.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 23.5
Species `B`:
0 1 2 3
0 43.0 34.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 81.5
# NOTE: now, we're using a highel-level function that also updates the system state
bio.react(time_step=0.1, n_steps=1)
bio.describe_state()
SYSTEM STATE at Time t = 0.2:
Species `A`:
0 1 2 3
0 20.5 21.5 0.0 0.00
1 0.0 0.0 0.0 0.00
2 0.0 0.0 0.0 32.75
Species `B`:
0 1 2 3
0 39.5 33.5 0.0 0.00
1 0.0 0.0 0.0 0.00
2 0.0 0.0 0.0 72.25
bio.react(time_step=0.1, n_steps=8)
bio.describe_state()
SYSTEM STATE at Time t = 0.9999999999999999:
Species `A`:
0 1 2 3
0 23.986328 21.998047 0.0 0.000000
1 0.000000 0.000000 0.0 0.000000
2 0.000000 0.000000 0.0 41.963867
Species `B`:
0 1 2 3
0 36.013672 33.001953 0.0 0.000000
1 0.000000 0.000000 0.0 0.000000
2 0.000000 0.000000 0.0 63.036133
bio.react(time_step=0.1, n_steps=10)
bio.describe_state()
SYSTEM STATE at Time t = 2.0000000000000004:
Species `A`:
0 1 2 3
0 23.999987 21.999998 0.0 0.000000
1 0.000000 0.000000 0.0 0.000000
2 0.000000 0.000000 0.0 41.999965
Species `B`:
0 1 2 3
0 36.000013 33.000002 0.0 0.000000
1 0.000000 0.000000 0.0 0.000000
2 0.000000 0.000000 0.0 63.000035
Verify the equilibrium in each of the active bins
bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={"A": 23.99998665, "B": 36.00001335})
A <-> B
Final concentrations: [B] = 36 ; [A] = 24
1. Ratio of reactant/product concentrations, adjusted for reaction orders: 1.5
Formula used: [B] / [A]
2. Ratio of forward/reverse reaction rates: 1.5
Discrepancy between the two values: 9.271e-05 %
Reaction IS in equilibrium (within 1% tolerance)
True
bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={"A": 21.99999809, "B": 33.00000191})
A <-> B
Final concentrations: [B] = 33 ; [A] = 22
1. Ratio of reactant/product concentrations, adjusted for reaction orders: 1.5
Formula used: [B] / [A]
2. Ratio of forward/reverse reaction rates: 1.5
Discrepancy between the two values: 1.447e-05 %
Reaction IS in equilibrium (within 1% tolerance)
True
bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={"A": 41.99996471, "B": 63.00003529}, explain=False)
True