#!/usr/bin/env python # coding: utf-8 # ## Accurate results in the simulation of the coupled reactions `2 S <-> U` and `S <-> X` # Both mostly forward. 1st-order kinetics throughout. # # Same as `substeps_1`, but with fixed steps: a lot of tiny steps - as a proxy for the "exact value" # # LAST REVISED: May 25, 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.reactions.reaction_data import ReactionData as chem from src.modules.reactions.reaction_dynamics import ReactionDynamics import numpy as np import plotly.express as px 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") # ### Initialize the system # In[4]: # Initialize the system chem_data = chem(names=["U", "X", "S"]) # Reaction 2 S <-> U , with 1st-order kinetics for all species (mostly forward) chem_data.add_reaction(reactants=[(2, "S")], products="U", forward_rate=8., reverse_rate=2.) # Reaction S <-> X , with 1st-order kinetics for all species (mostly forward) chem_data.add_reaction(reactants="S", products="X", forward_rate=6., reverse_rate=3.) chem_data.describe_reactions() # Send the plot of the reaction network to the HTML log file graph_data = chem_data.prepare_graph_network() GraphicLog.export_plot(graph_data, "vue_cytoscape_1") # ### Set the initial concentrations of all the chemicals # In[5]: dynamics = ReactionDynamics(reaction_data=chem_data) dynamics.set_conc(conc={"U": 50., "X": 100., "S": 0.}) dynamics.describe_state() # In[6]: dynamics.set_diagnostics() # To save diagnostic information about the call to single_compartment_react() dynamics.single_compartment_react(initial_step=0.0001, target_end_time=0.3, variable_steps=False) df = dynamics.get_history() df # In[7]: df.iloc[400] # In[8]: df.iloc[1850] # This data point is used in experiment `substeps_1` # In[9]: (transition_times, step_sizes) = dynamics.explain_time_advance(return_times=True) # In[10]: np.array(step_sizes) # In[11]: np.array(transition_times) # Note: there will be one more transition time (the end time) than step sizes # ## Plots of changes of concentration with time # In[12]: dynamics.plot_curves(colors=['green', 'orange', 'blue']) # In[ ]: