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 src.modules.chemicals.chem_data import ChemData
from src.modules.reactions.reaction_dynamics import ReactionDynamics
# Initialize the system
chem_data = ChemData(names=["A", "B", "E"])
# Reaction A <-> B , with 1st-order kinetics, favorable thermodynamics in the forward direction,
# and a forward rate that is much slower than it would be with the enzyme - as seen in the next reaction, below
chem_data.add_reaction(reactants="A", products="B",
forward_rate=1., delta_G=-3989.73)
# Reaction A + E <-> B + E , with 1st-order kinetics, and a forward rate that is faster than it was without the enzyme
# Thermodynamically, there's no change from the reaction without the enzyme
chem_data.add_reaction(reactants=["A", "E"], products=["B", "E"],
forward_rate=10., delta_G=-3989.73)
chem_data.describe_reactions() # Notice how the enzyme `E` is noted in the printout below
Number of reactions: 2 (at temp. 25 C)
0: A <-> B (kF = 1 / kR = 0.2 / delta_G = -3,989.7 / K = 5) | 1st order in all reactants & products
1: A + E <-> B + E (kF = 10 / kR = 2 / delta_G = -3,989.7 / K = 5) | Enzyme: E | 1st order in all reactants & products
Set of chemicals involved in the above reactions (not counting enzymes): {'B', 'A'}
Set of enzymes involved in the above reactions: {'E'}
dynamics = ReactionDynamics(chem_data=chem_data)
dynamics.set_conc(conc={"A": 20., "B": 0., "E": 0.},
snapshot=True) # Initially, no enzyme `E`
dynamics.describe_state()
SYSTEM STATE at Time t = 0:
3 species:
Species 0 (A). Conc: 20.0
Species 1 (B). Conc: 0.0
Species 2 (E). Conc: 0.0
Set of chemicals involved in reactions (not counting enzymes): {'B', 'A'}
Set of enzymes involved in reactions: {'E'}
dynamics.set_diagnostics() # To save diagnostic information about the call to single_compartment_react()
# All of these settings are currently close to the default values... but subject to change; set for repeatability
dynamics.set_thresholds(norm="norm_A", low=0.5, high=0.8, abort=1.44)
dynamics.set_thresholds(norm="norm_B", low=0.08, high=0.5, abort=1.5)
dynamics.set_step_factors(upshift=1.2, downshift=0.5, abort=0.4)
dynamics.set_error_step_factor(0.25)
# Perform the reactions
dynamics.single_compartment_react(reaction_duration=0.25,
initial_step=0.05, variable_steps=True, explain_variable_steps=False)
Some steps were backtracked and re-done, to prevent negative concentrations or excessively large concentration changes 9 total step(s) taken
#dynamics.explain_time_advance()
dynamics.plot_curves(colors=['darkorange', 'green', 'violet'], show_intervals=True, title_prefix="WITH zero enzyme")
dynamics.set_single_conc(30., species_name="E", snapshot=True) # Plenty of enzyme `E`
dynamics.describe_state()
SYSTEM STATE at Time t = 0.275:
3 species:
Species 0 (A). Conc: 15.232227244425584
Species 1 (B). Conc: 4.767772755574415
Species 2 (E). Conc: 30.0
Set of chemicals involved in reactions (not counting enzymes): {'B', 'A'}
Set of enzymes involved in reactions: {'E'}
dynamics.single_compartment_react(reaction_duration=0.04,
initial_step=0.005, variable_steps=True, explain_variable_steps=False)
*** CAUTION: negative concentration in chemical `A` in step starting at t=0.275. It will be AUTOMATICALLY CORRECTED with a reduction in time step size, as follows:
INFO: the tentative time step (0.005) leads to a NEGATIVE concentration of `A` from reaction A + E <-> B + E (rxn # 1):
Baseline value: 15.232 ; delta conc: -21.418
-> will backtrack, and re-do step with a SMALLER delta time, multiplied by 0.25 (set to 0.00125) [Step started at t=0.275, and will rewind there]
* INFO: the tentative time step (0.00125) leads to a least one norm value > its ABORT threshold:
-> will backtrack, and re-do step with a SMALLER delta time, multiplied by 0.4 (set to 0.0005) [Step started at t=0.275, and will rewind there]
* INFO: the tentative time step (0.0005) leads to a least one norm value > its ABORT threshold:
-> will backtrack, and re-do step with a SMALLER delta time, multiplied by 0.4 (set to 0.0002) [Step started at t=0.275, and will rewind there]
Some steps were backtracked and re-done, to prevent negative concentrations or excessively large concentration changes
30 total step(s) taken
#dynamics.explain_time_advance()
dynamics.plot_curves(colors=['darkorange', 'green', 'violet'], show_intervals=True, title_prefix="WITH enzyme added mid-reaction")
E is added at t = 0.275!¶The reactions simulator automatically switches to small time steps is in order to handle the rapid amount of change
# Verify that the reaction has reached equilibrium
dynamics.is_in_equilibrium()
0: A <-> B
Final concentrations: [B] = 16.67 ; [A] = 3.333
1. Ratio of reactant/product concentrations, adjusted for reaction orders: 5.00003
Formula used: [B] / [A]
2. Ratio of forward/reverse reaction rates: 5.00000519021548
Discrepancy between the two values: 0.0005779 %
Reaction IS in equilibrium (within 1% tolerance)
1: A + E <-> B + E
Final concentrations: [B] = 16.67 ; [E] = 30 ; [A] = 3.333 ; [E] = 30
1. Ratio of reactant/product concentrations, adjusted for reaction orders: 5.00003
Formula used: ([B][E]) / ([A][E])
2. Ratio of forward/reverse reaction rates: 5.00000519021548
Discrepancy between the two values: 0.0005779 %
Reaction IS in equilibrium (within 1% tolerance)
True
dynamics.get_history()
| SYSTEM TIME | A | B | E | caption | |
|---|---|---|---|---|---|
| 0 | 0.000000 | 20.000000 | 0.000000 | 0.0 | Initial state |
| 1 | 0.050000 | 19.000000 | 1.000000 | 0.0 | |
| 2 | 0.100000 | 18.060000 | 1.940000 | 0.0 | |
| 3 | 0.125000 | 17.618200 | 2.381800 | 0.0 | |
| 4 | 0.150000 | 17.189654 | 2.810346 | 0.0 | |
| 5 | 0.175000 | 16.773964 | 3.226036 | 0.0 | |
| 6 | 0.200000 | 16.370745 | 3.629255 | 0.0 | |
| 7 | 0.225000 | 15.979623 | 4.020377 | 0.0 | |
| 8 | 0.250000 | 15.600234 | 4.399766 | 0.0 | |
| 9 | 0.275000 | 15.232227 | 4.767773 | 0.0 | |
| 10 | 0.275000 | 15.232227 | 4.767773 | 30.0 | Set concentration of `E` |
| 11 | 0.275200 | 14.372651 | 5.627349 | 30.0 | |
| 12 | 0.275400 | 13.575171 | 6.424829 | 30.0 | |
| 13 | 0.275600 | 12.835300 | 7.164700 | 30.0 | |
| 14 | 0.275800 | 12.148878 | 7.851122 | 30.0 | |
| 15 | 0.276000 | 11.512043 | 8.487957 | 30.0 | |
| 16 | 0.276200 | 10.921213 | 9.078787 | 30.0 | |
| 17 | 0.276440 | 10.263435 | 9.736565 | 30.0 | |
| 18 | 0.276728 | 9.542527 | 10.457473 | 30.0 | |
| 19 | 0.277074 | 8.767428 | 11.232572 | 30.0 | |
| 20 | 0.277419 | 8.089086 | 11.910914 | 30.0 | |
| 21 | 0.277834 | 7.376689 | 12.623311 | 30.0 | |
| 22 | 0.278249 | 6.771007 | 13.228993 | 30.0 | |
| 23 | 0.278663 | 6.256054 | 13.743946 | 30.0 | |
| 24 | 0.279161 | 5.730676 | 14.269324 | 30.0 | |
| 25 | 0.279659 | 5.299738 | 14.700262 | 30.0 | |
| 26 | 0.280256 | 4.875569 | 15.124431 | 30.0 | |
| 27 | 0.280853 | 4.542897 | 15.457103 | 30.0 | |
| 28 | 0.281570 | 4.229802 | 15.770198 | 30.0 | |
| 29 | 0.282430 | 3.951341 | 16.048659 | 30.0 | |
| 30 | 0.283462 | 3.720982 | 16.279018 | 30.0 | |
| 31 | 0.284700 | 3.547589 | 16.452411 | 30.0 | |
| 32 | 0.286186 | 3.432586 | 16.567414 | 30.0 | |
| 33 | 0.287969 | 3.368656 | 16.631344 | 30.0 | |
| 34 | 0.290109 | 3.341352 | 16.658648 | 30.0 | |
| 35 | 0.292677 | 3.333912 | 16.666088 | 30.0 | |
| 36 | 0.295758 | 3.333265 | 16.666735 | 30.0 | |
| 37 | 0.299456 | 3.333353 | 16.666647 | 30.0 | |
| 38 | 0.303893 | 3.333317 | 16.666683 | 30.0 | |
| 39 | 0.309218 | 3.333343 | 16.666657 | 30.0 | |
| 40 | 0.315608 | 3.333314 | 16.666686 | 30.0 |