#!/usr/bin/env python # coding: utf-8 # # Diffusion of 1 chemical in 1D # # ## An initial concentration pulse (near the left edge of the system) diffusing out towards equilibrium # # The system starts out with a "concentration pulse" in bin 2 (the 3rd bin from the left) - i.e. that bin is initially the only one with a non-zero concentration of the only chemical species. # Then the system is left undisturbed, and followed to equilibrium. # ### TAGS : "diffusion 1D", "quick-start" # In[1]: LAST_REVISED = "Aug. 15, 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, os #os.getcwd() #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 single non-zero bin concentration of the single chemical `A`, near the left edge of the system # In[5]: chem_data = ChemData(names="A", diffusion_rates=0.1) # If you want to assign a default color, pass arg: plot_colors=["SOME_COLOR_NAME"] 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.describe_state() # In[7]: bio.system_snapshot() # In[8]: bio.visualize_system(title_prefix="Diffusion") # Line curve view # In[9]: bio.system # In[10]: bio.system_heatmaps(title_prefix="Diffusion") # In[ ]: # ## Request history-keeping for some bins # In[11]: # Request to save the concentration history at the bin with the initial concentration injection, # and the bins at the ends of the system bio.enable_history(bins=[0, 2, 9], frequency=4, take_snapshot=True) # In[ ]: # In[ ]: # # Initial Diffusion Step # In[12]: # Advancing to time t=10, with time steps of 0.1 ... # Not sure what value to pass for time steps? If unspecified, a default value is used delta_time = 10. status = bio.diffuse(total_duration=delta_time, time_step=0.1) print(status) bio.describe_state(concise=True) # In[13]: bio.visualize_system(title_prefix="Diffusion") # Line curve view # In[14]: 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[15]: 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[16]: # Mass conservation can also be verified as follows: bio.check_mass_conservation(chem_label="A", 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 was applied # In[18]: bio.plot_history_single_bin(bin_address=2) # In[19]: bio.plot_history_single_bin(bin_address=0, title_prefix="Notice the transient 'overshoot`") # Left "edge" of the 1D system # In[20]: bio.plot_history_single_bin(bin_address=9) # Right "edge" of the 1D system # In[ ]: