#!/usr/bin/env python # coding: utf-8 # ## `A <-> B` reaction, with 1st-order kinetics in both directions, # ### taken to equilibrium # # Diffusion not done # # LAST REVISED: July 14, 2023 # In[1]: import set_path # Importing this module will add the project's home directory to sys.path # In[2]: 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 # In[3]: # 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") # In[4]: # 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() # In[5]: chem_data.describe_reactions() # In[6]: # Send the plot to the HTML log file graph_data = chem_data.prepare_graph_network() GraphicLog.export_plot(graph_data, "vue_cytoscape_1") # ## First step # In[7]: # 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) # In[8]: bio.system += bio.delta_reactions # Matrix operation to update all the concentrations bio.system_time += 0.1 bio.describe_state() # ## Second step # In[9]: # 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() # ## Many more steps, to equilibrium # In[10]: bio.react(time_step=0.1, n_steps=8) bio.describe_state() # In[11]: bio.react(time_step=0.1, n_steps=10) bio.describe_state() # ### The system has now reached equilibrium # ### in individual bins, which remain separate because we're NOT doing diffusion in this experiment # Verify the equilibrium in each of the active bins # In[12]: bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={"A": 23.99998665, "B": 36.00001335}) # In[13]: bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={"A": 21.99999809, "B": 33.00000191}) # In[14]: bio.reaction_dynamics.is_in_equilibrium(rxn_index=0, conc={"A": 41.99996471, "B": 63.00003529}, explain=False) # In[ ]: