<jupyter_start><jupyter_text>**Two COUPLED reactions:  A + B  C  and  C + D  E , with 1st-order kinetics for each species,taken to equilibrium**LAST REVISED: June 13, 2022<jupyter_code>import set_path
set_path.add_ancestor_dir_to_syspath(3)  # The number of levels to go up 
                                         # to reach the project's home from the folder containing this notebook
from modules.chemicals.chemicals import Chemicals as chem
from modules.reactions.reactions import Reactions
from life_1D.bio_sim_1d import BioSim1D as bio

from modules.html_log.html_log import HtmlLog as log
# Initialize the system
chem_data = chem(diffusion_rates=[.1, .1, .1, .1, .1], names=["A", "B", "C", "D", "E"])  # NOTE: diffusion_rates not used for now

rxn = Reactions(chem_data)

# Reactions A + B <-> C  and  C + D <-> E , with 1st-order kinetics for each species
rxn.add_reaction(reactants=["A", "B"], products=["C"], forward_rate=5., reverse_rate=2.)
rxn.add_reaction(reactants=["C", "D"], products=["E"], forward_rate=8., reverse_rate=4.)

bio.initialize_system(n_bins=1, chem_data=chem_data, reactions=rxn)

bio.set_all_uniform_concentrations( [3., 5., 1., 0.4, 0.1] )

bio.describe_state()
rxn.describe_reactions()
rxn._internal_reactions_data()    # Low-level view of the reactions data
# First step
bio.react(time_step=0.01, n_steps=1)
bio.describe_state()
"""
1 bins and 5 species:
 [[2.27 ]
 [4.27 ]
 [1.702]
 [0.372]
 [0.128]]
"""

# Identical 2nd step
bio.react(time_step=0.01, n_steps=1)
bio.describe_state()
"""
1 bins and 5 species:
 [[1.819395  ]
 [3.819395  ]
 [2.10707348]
 [0.32646848]
 [0.17353152]]
"""

# Numerous more identical steps
bio.react(time_step=0.01, n_steps=200)
bio.describe_state()
"""
1 bins and 5 species:
 [[0.50508029]
 [2.50508029]
 [3.16316668]
 [0.06824696]
 [0.43175304]]
"""


# Do a consistent check with the equilibrium concentrations:

A_eq = bio.bin_concentration(0, 0)
B_eq = bio.bin_concentration(0, 1)
C_eq = bio.bin_concentration(0, 2)
D_eq = bio.bin_concentration(0, 3)
E_eq = bio.bin_concentration(0, 4)

Rf0 = rxn.get_forward_rate(0)
Rb0 = rxn.get_reverse_rate(0)

Rf1 = rxn.get_forward_rate(1)
Rb1 = rxn.get_reverse_rate(1)

equil = -(Rf0 * A_eq * B_eq - Rf1 * C_eq * D_eq) + (Rb0 * C_eq - Rb1 * E_eq)

print("\nAt equilibrium: ", equil, " (this should be close to 0 at equilibrium)")<jupyter_output>At equilibrium:  -2.6645352591003757e-15  (this should be close to 0 at equilibrium)
{'fraction_non_alphanumeric': 0.14063140631406315, 'fraction_numerical': 0.08077080770807708, 'mean_word_length': 4.386313465783664, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '653028', 'license': 'MIT', 'n_tokens_mistral': 1082, 'n_tokens_neox': 954, 'n_words': 239, 'path': 'experiments/life_1D/reaction/reaction_8.ipynb', 'repo_name': 'BrainAnnex/life123', 'size': '2439'}