#!/usr/bin/env python # coding: utf-8 # # Diffusion of 1 chemical in 2D # # ## An initial concentration pulse of a single chemical (near the left edge of the system, and halfway vertically), diffusing towards equilibrium # # The system starts out with a "concentration pulse" in just one bin - 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. # # (Note: this is the 2D counterpart of the 1D experiment by the same name) # ### TAGS : "diffusion 2D", "quick-start" # In[1]: LAST_REVISED = "Jan. 22, 2025" LIFE123_VERSION = "1.0.0rc2" # 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 BioSim2D, ChemData, check_version # In[4]: check_version(LIFE123_VERSION) # In[ ]: # In[5]: # Prepare the initial system, with a single non-zero bin, near the left edge of the system, positioned halfway vertically chem_data = ChemData(names="A", diffusion_rates=0.02) bio = BioSim2D(x_bins=8, y_bins=5, chem_data=chem_data) # In[6]: bio.describe_state() # In[ ]: # In[ ]: # ## Request history-keeping for some bins # In[7]: bio.enable_history(bins=[(1,2), (7,4)], frequency=3) # Request to save the concentration history at those bins # (the one with the initial injection, and one far away in a corner) # In[ ]: # ## Apply the initial concentration pulse # In[8]: bio.set_bin_conc(bin_address = (1,2), chem_label="A", conc=10.) bio.describe_state() # In[9]: bio.system_snapshot(chem_label="A") # In[10]: bio.heatmap_single_chem(chem_label="A", title_prefix="Diffusion") # In[ ]: # In[ ]: # # Initial Diffusion Step # In[11]: delta_time = 10. status = bio.diffuse(total_duration=delta_time, time_step=0.1) print("\n", status) bio.describe_state() # In[12]: bio.heatmap_single_chem(chem_label="A", title_prefix="Diffusion") # In[13]: # MASS-CONSERVATION CHECK. Verify that that sum of all the entries in the above matrix is still the initial 10. bio.check_mass_conservation(chem_label="A", expected=10.) # 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(180): status = bio.diffuse(total_duration=delta_time, time_step=0.1) if i<2 or i==6 or i>=179: bio.describe_state() fig = bio.heatmap_single_chem(chem_label="A", title_prefix="Diffusion", height=400) fig.show() # # All bins now have essentially uniform concentration. The diffusion has reached equilibrium # # Notice, throughout the simulation, the continued symmetry across the mid-row (ybin 2). # **Mass conservations**: the initial "10. units of concentration" are now uniformly spread across the 40 (5x8) bins, leading to a near-constant concentration of 10./40 # In[15]: 10./40 # In[16]: # Mass conservation can also be verified as follows: bio.check_mass_conservation(chem_label="A", expected=10.) # 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=(1,2)) # The bin where the initial concentration was applied # In[18]: bio.plot_history_single_bin(bin_address=(1,2)) # In[19]: bio.plot_history_single_bin(bin_address=(7,4)) # A bin in a far-away corner from the initial concentration injection # In[ ]: