#!/usr/bin/env python # coding: utf-8 # ## One-bin reaction `2A <-> B` # ### COMPARING 1st-order and 2nd-order kinetics in *forward* direction; reverse direction always 1-st order # # Diffusion not applicable (just 1 bin) # # See also the experiment _"reactions_single_compartment/react_4"_ # ### TAGS : "reactions 1D", "under-the-hood" # In[1]: LAST_REVISED = "June 5, 2025" LIFE123_VERSION = "1.0.0rc6" # Library version this experiment is based on # In[2]: #import set_path # Using MyBinder? Uncomment this before running the next cell! # In[3]: #import sys #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 BioSim1D, ChemData, check_version from life123 import HtmlLog as log from life123 import GraphicLog # In[4]: check_version(LIFE123_VERSION) # In[ ]: # In[5]: # 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") # In[ ]: # # INITIALLY, with 1st-order kinetics in both directions # In[6]: # Initialize the system; NOTE: Diffusion not applicable (just 1 bin) chem_data = ChemData(names=["A", "B"], plot_colors=['turquoise', 'green']) bio = BioSim1D(n_bins=1, chem_data=chem_data) bio.set_all_uniform_concentrations( [3., 5.] ) bio.describe_state() # In[7]: # Specify the reaction reactions = bio.get_reactions() # Reaction 2A <-> B , FOR NOW with 1st-order kinetics in both directions reactions.add_reaction(reactants=[(2, "A", 1)], products="B", forward_rate=5., reverse_rate=2.) reactions.describe_reactions() # In[8]: # Send a header and a plot to the HTML log file log.write("Reaction 2A <-> B is 1st order in all species:", style=log.h2) reactions.plot_reaction_network("vue_cytoscape_2") # In[ ]: # In[9]: # Let's enable history - by default for all chemicals and all bins bio.enable_history(take_snapshot=True, caption="Initial state") # In[10]: bio.get_bin_history(bin_address=0) # In[ ]: # In[ ]: # ### First step # In[11]: bio.get_reaction_handler().enable_diagnostics() # To save diagnostic information for the simulation run, below # In[12]: # First step bio.react(time_step=0.02, n_steps=1) bio.describe_state() # Small conc. changes so far: [A] = 2.8 , [B] = 5.1 # In[13]: bio.get_bin_history(bin_address=0) # In[14]: # Numerous more steps, to equilibrium bio.react(time_step=0.02, n_steps=20) bio.describe_state() # Consistent with the 5/2 ratio of forward/reverse rates (and the *1st order* reactions), # the systems settles in the following equilibrium: # [A] = 2.16928427 , [B] = 5.41535786 # In[15]: # Verify that the reaction has reached equilibrium bio.get_reaction_handler().is_in_equilibrium(rxn_index=0, conc=bio.bin_snapshot(bin_address = 0)) # In[16]: df = bio.get_bin_history(bin_address=0) df # In[17]: bio.plot_history_single_bin(bin_address=0, title_prefix="Reaction `2A <-> B`") # A gets depleted, while B gets produced. # #### Let's verify that the stoichiometry is being respected # In[18]: # We'll check the first two arrays of concentrations, from the run's history arr0 = bio.get_reaction_handler().get_historical_concentrations(row=0, df=df) arr1 = bio.get_reaction_handler().get_historical_concentrations(row=1, df=df) arr0, arr1 # In[19]: bio.get_reaction_handler().get_diagnostics().stoichiometry_checker(rxn_index=0, conc_arr_before = arr0, conc_arr_after = arr1) # In[ ]: # In[ ]: # # STARTING OVER, this time with 2nd-order kinetics in the forward reaction # In[20]: reactions = bio.get_reactions() # In[21]: reactions.clear_reactions_data() # In[22]: # Reaction 2A <-> B , NOW WITH 2nd-order kinetics in the forward direction reactions.add_reaction(reactants=[(2, "A", 2)], products="B", forward_rate=5., reverse_rate=2.) # In[23]: reactions.describe_reactions() # In[24]: # RESET the concentrations to their original values bio.set_all_uniform_concentrations( [3., 5.] ) bio.describe_state() # In[25]: # Save this manual concentration change into the ongoing history bio.capture_snapshot(caption="RESET all concentrations to initial values") bio.get_bin_history(bin_address=0) # In[26]: # Send a header and a plot to the HTML log file log.write("Reaction 2A <-> B is 2nd order in A, and 1st order in B:", style=log.h2) reactions.plot_reaction_network("vue_cytoscape_2") # In[ ]: # In[27]: # First step bio.react(time_step=0.02, n_steps=1) bio.describe_state() # [A] = 1.6 , [B] = 5.7 # _(Contrast with the counterpart in the 1st order kinetics: [A] = 2.8 , [B] = 5.1)_ # In[28]: bio.get_bin_history(bin_address=0) # In[ ]: # In[29]: # Numerous more steps bio.react(time_step=0.02, n_steps=20) bio.describe_state() # The systems settles in the following equilibrium: [A] = 1.51554944 , [B] = 5.74222528 # In[30]: # Verify that the reaction has reached equilibrium bio.get_reaction_handler().is_in_equilibrium(rxn_index=0, conc=bio.bin_snapshot(bin_address = 0)) # In[31]: bio.plot_history_single_bin(bin_address=0, title_prefix="Reaction `2A <-> B` (the jump at 0.42 is the concentration reset)", vertical_lines_to_add=[0.42]) # In[32]: df2 = bio.get_bin_history(bin_address=0) df2 # **Compared to first-order kinetics in A**, the (2nd order in A) reaction now takes place much more quickly, and proceeds to almost complete depletion of A # #### Let's verify that the stoichiometry is still being respected # In[33]: # We'll check the first two arrays of concentrations, from the run's history arr0 = bio.get_reaction_handler().get_historical_concentrations(row=22, df=df2) # Row 22 is the conc. reset arr1 = bio.get_reaction_handler().get_historical_concentrations(row=23, df=df2) arr0, arr1 # In[34]: bio.get_reaction_handler().get_diagnostics().stoichiometry_checker(rxn_index=0, conc_arr_before = arr0, conc_arr_after = arr1) # In[ ]: