#!/usr/bin/env python # coding: utf-8 # # Diffusion of 2 chemicals in 1D # # ## Two symmetric initial concentration pulses (near the opposite edges of the system) diffusing out towards equilibrium, with identical diffusion rates # # Symmetry is observed throughout # ### TAGS : "diffusion 1D", "basic" # 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 life123 import BioSim1D, ChemData, check_version # In[4]: check_version(LIFE123_VERSION) # In[ ]: # ## Prepare the initial system # with a non-zero bin concentration of the chemical `A`, near the left edge of the system, # and an identical bin concentration of the chemical `B`, near the left edge # In[5]: chem_data = ChemData(names=["A", "B"], diffusion_rates=[0.1, 0.1], plot_colors=["turquoise", "green"]) bio = BioSim1D(n_bins=10, chem_data=chem_data) # In[6]: bio.inject_conc_to_bin(bin_address=2, chem_label="A", delta_conc=10.) bio.inject_conc_to_bin(bin_address=7, chem_label="B", delta_conc=10.) bio.describe_state() # In[7]: bio.system_snapshot() # In[8]: bio.visualize_system(title_prefix="Diffusion") # Line curve view # In[9]: bio.system_heatmaps(title_prefix="Diffusion") # In[ ]: # ## Request history-keeping for some bins # In[10]: # Request to save the concentration history at the bins with the initial concentration injection, # and the bins at the ends of the system bio.enable_history(bins=[0, 2, 7, 9], frequency=3, take_snapshot=True) # In[ ]: # In[ ]: # # Initial Diffusion Step # In[11]: # Advancing to time t=10, with time steps of 0.1 ... delta_time = 10. status = bio.diffuse(total_duration=delta_time, time_step=0.1) print(status) bio.describe_state(concise=True) # In[12]: bio.visualize_system(title_prefix="Diffusion") # Line curve view # In[13]: bio.system_heatmaps(title_prefix="Diffusion") # In[ ]: # ## This is still an early stage in the diffusion process; let's advance it more... # (Visualization from results shown at selected times) # In[14]: for i in range(50): status = bio.diffuse(total_duration=delta_time, time_step=0.1) if i<2 or i==6 or i>=49: bio.describe_state(concise=True) # Line curve view fig = bio.visualize_system(title_prefix="Diffusion") # Line curve view fig.show() # Heatmap view fig = bio.system_heatmaps() fig.show() # ## All bins now have essentially uniform concentration # # **Mass conservations**: The initial "10 units of concentration" are now uniformly spread across the 10 bins, leading to a near-constant concentration of 10/10 = **1.0** # In[15]: # Mass conservation can also be verified as follows: bio.check_mass_conservation(chem_label="A", expected=10.) # In[16]: bio.check_mass_conservation(chem_label="B", expected=10.) # In[ ]: # In[ ]: # ## Visualization of time changes at particular bins # #### Instead of visualizing the entire system at a moment of time, like in the previous heatmaps, let's now look at the time evolution of the (only) chemical `A` at either of the bins whose history we requested prior to running the simulation # In[17]: bio.conc_history.bin_history(bin_address=2) # The bin where the initial concentration of `A` was applied # In[18]: bio.plot_history_single_bin(bin_address=2) # In[19]: bio.plot_history_single_bin(bin_address=7) # The bin where the initial concentration of `B` was applied # Notice the symmetery between bins 2 and 7 (if you switch `A` and `B`) # In[ ]: # In[20]: bio.plot_history_single_bin(bin_address=0) # Left "edge" of the 1D system # In[21]: bio.plot_history_single_bin(bin_address=9) # Right "edge" of the 1D system # Notice the symmetery between bins 0 and 9 (if you switch `A` and `B`) # In[ ]: