#!/usr/bin/env python # coding: utf-8 # # Attaining a Concentration Gradient # ### by continuosly injecting and draining, at opposite ends # # The system starts out with a uniform concentration. # Then identical concentrations are repeatedly *injected to the left* and *drained from the right* # Diffusion turn the forced concentration imbalance into a smooth gradient. # ### TAGS : "diffusion 1D" # In[1]: LAST_REVISED = "Apr. 29, 2025" LIFE123_VERSION = "1.0.0rc3" # 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[ ]: # In[5]: # Initialize the system with a uniform concentration (of the only species) chem_data = ChemData(names="A", diffusion_rates=0.6) bio = BioSim1D(n_bins=9, chem_data=chem_data) bio.set_uniform_concentration(chem_label="A", conc=100.) bio.describe_state() # In[6]: bio.show_system_snapshot() # In[7]: # Visualize the system's initial state bio.visualize_system(title_prefix="Diffusion") # In[8]: bio.system_heatmaps(title_prefix="Diffusion", text_format=".0f") # In[ ]: # # Start the simulation steps # In[9]: delta_time = 1. # In[10]: for i in range(501): # Inject to the leftmost bin bio.inject_conc_to_bin(bin_address=0, chem_index=0, delta_conc=4, zero_clip = False) # Drain from the rightmost bin bio.inject_conc_to_bin(bin_address=8, chem_index=0, delta_conc=-4, zero_clip = False) # Note: the NET GAIN of moles of A in the system is zero! # Diffuse for the time span delta_time status = bio.diffuse(total_duration=delta_time, time_step=0.1) if (i <= 12 and i%3 == 0) or (i%100 == 0): # Display more frequently initially print() bio.describe_state(concise=True) # Show the system state as a line plot fig = bio.visualize_system(title_prefix="Diffusion") fig.show() # Show as heatmap fig = bio.system_heatmaps(title_prefix="Diffusion", text_format=".3g") fig.show() # ### By now, the gradient has stabilized with # ### [A] = 124.67065159 on the left and [A] = 75.32934841 on the right # Note: if the drain is too large, relative to the diffusion rate, the smaller concentration could "saturate" at zero # In[ ]: