#!/usr/bin/env python # coding: utf-8 # # Membranes in 1D : Diffusion and Passive Transport across Membranes # # #### Simple scenarios with 1 chemical # ### TAGS : "membranes 1D", "basic", "quick-start", "diffusion 1D", "under-the-hood" # In[1]: LAST_REVISED = "May 19, 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[ ]: # ## Prepare the initial system # with a single chemical `A` # In[5]: chem_data = ChemData(diffusion_rates=10.) # Name "A" automatically assigned to single chemical bio = BioSim1D(n_bins=4, chem_data=chem_data) # In[6]: initial_conc = [20, 50, 150, 250] # In[7]: bio.set_species_conc(conc_list=initial_conc, chem_index=0) bio.describe_state() # In[8]: bio.system_snapshot() # In[9]: bio.visualize_system() # Line curve view # In[10]: bio.system_heatmaps() # In[ ]: # # Add Membranes # #### The bins would normally mix up by diffusion, but we'll prevent that by inserting impermeable membranes # In[11]: bio.set_membranes(membranes=[ (1,2) ]) # By default impermeable # In[12]: bio.membranes_obj # In[13]: bio.describe_state() # In[14]: bio.system_heatmaps() # In[ ]: # ## Now, let's start the diffusion # In[15]: bio.diffuse(time_step=0.02, n_steps=1) # In[16]: bio.system_snapshot() # In[17]: bio.system_heatmaps() # ### Nothing has changed in the 2 leftmost bins, as a result of the impermeable membranes # By contrast, diffusion is progressing between the 2 rightmost bins # # *Technical side note:* since we're using, by default, simple # 3-1 stencils for the diffusion step, the concentration increment in bin 3, and its corresponding decrement in bin 2, is: # In[18]: 0.02 * 100 * 10 / (1*1) # (Time step) * (Delta concentration) * (Diffusion rate const) / (bin length squared) # The 150 initial value in bin 2 increased by 20 to 170, and the 250 value in bin 3 correspondingly decreased to 230 # In[ ]: # In[ ]: # ### Let's further advance the diffusion, by many more steps # In[19]: bio.diffuse(time_step=0.02, n_steps=14) # In[20]: bio.system_heatmaps() # In[ ]: # ## Now, let's assign a small permeability to our single chemical `A` to the membranes (by passive transport) # In[21]: bio.change_permeability("A", 1.) # In[22]: bio.diffuse(time_step=0.02, n_steps=1) # In[23]: bio.system_heatmaps() # ### Passive transport across membranes is now taking place both out of bin 1 (to its left neighbor) and into it (from its right neighbors) # Diffusion is continuing normally between the 2 rightmost bins # *Technical side note -* Passive membrane transport out of bin 1 (conc. 50) into bin 0 (conc. 20) in this single step was: # In[24]: 30 * 1 * 0.02 # (Delta conc) * Permeability * (Time step) , since all bin sizes are 1 # The concentration of bin 0 has increased from 20 to 20.6 during this single step. # The concentration of bin 1 is also increasing, because its loss to bin 0 is more than compensated by its gain from bin 2 (bigger conc. difference) # In[ ]: # ### Let's continue to advance the diffusion # In[25]: bio.diffuse(time_step=0.02, n_steps=4) # In[26]: bio.system_heatmaps() # In[27]: bio.diffuse(time_step=0.02, n_steps=45) # In[28]: bio.system_heatmaps() # In[29]: bio.diffuse(time_step=0.02, n_steps=500) # In[30]: bio.system_heatmaps() # #### Verify that the total amount of our chemical present hasn't changed; it has simply shifted across bins # In[31]: bio.check_mass_conservation(expected=sum(initial_conc), chem_index=0) # In[ ]: