#!/usr/bin/env python # coding: utf-8 # # Exploring the change of delta_x (spatial resolution) in diffusion accuracy. # #### From the same initial setup, diffusion is carried out over a fixed time span, # #### at different spatial resolutions - and then the respective results are compared # ### TAGS : "diffusion 1D", "under-the-hood" # In[1]: LAST_REVISED = "May 3, 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 #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, Numerical, check_version # In[4]: check_version(LIFE123_VERSION) # In[ ]: # ## Prepare the initial system # In[5]: chem_data = ChemData(names="A", diffusion_rates=0.1) conc_list=[10,13,17,21,25,28,30,38,42,55,65,47,35,32,27,23,20,17,14,8,3,10,16,18, 20,25,30,35,40,65,85,115,150,92,73,69,65,50,42,36,20,45,50,55,69,82,95, 77,60,43,37,31,25,22,20,18,15,11,9, 8] bio = BioSim1D(n_bins=len(conc_list), chem_data=chem_data) bio.set_species_conc(chem_label="A", conc_list=conc_list) bio.describe_state() # In[6]: # Visualize the initial system state bio.visualize_system(title_prefix="Diffusion") # In[7]: # Show as heatmap bio.system_heatmaps(title_prefix="Diffusion") # In[8]: bio.describe_state(concise=True) # In[ ]: # ### Populate the data set with more bins, using interpolated concentration values # ### IMPORTANT: we're **NOT** changing spacial resolution here; we're just creating a less ragged dataset, as *our initial system state* # In[9]: bio.smooth_spatial_resolution() bio.describe_state() # In[10]: bio.n_bins # In[ ]: # # The STARTING POINT # ### This system setup will be our starting point in exploring diffusion using different spacial resolutions # In[11]: original_state = bio.save_system() # SAVE a copy of the system state, to do multiple runs starting from it # In[12]: # Line plot bio.visualize_system(title_prefix="Diffusion") # In[13]: # Show as heatmap bio.system_heatmaps(title_prefix="Diffusion") # In[ ]: # # Initial Diffusions with delta_x = 1 # In[14]: bio.describe_state(concise=True) # Our initial state # In[15]: bio.diffuse(total_duration=7, time_step=0.0005) bio.describe_state(concise=True) # In[16]: # SAVE the above system data (a matrix of dimension n_species x n_bins): this is the result of diffusion with delta_x = 1 diffuse_dx_1 = bio.system # In[17]: # Line plot bio.visualize_system(title_prefix="Diffusion") # ### Enough time has proceeded to result in some smoothing, and non-puny changes in most values - but still nowhere near equilibrium # # Now restore the system to its initial (pre-diffusion) state # ### and then perform a diffusion over the same time span, but with DOUBLE the spacial resolution # #### delta_x will be be 1/2 instead of the original default 1 # In[18]: bio.restore_system(original_state) # In[19]: bio.describe_state() # In[20]: # Double the spacial resolution bio.increase_spatial_resolution(2) bio.describe_state() # In[21]: # Now repeat the idential diffusion process as before, but with half the delta_x bio.diffuse(total_duration=7, time_step=0.0005, delta_x=0.5) # In[22]: # Finally, halve the resolution, to return to the original number of bins bio.decrease_spatial_resolution(2) # In[23]: bio.describe_state(concise=True) # In[24]: # SAVE the above system data: this is the result of diffusion with delta_x of 1/2 diffuse_dx_1_2 = bio.system # ### Compare the last 2 runs (with dx=1 and dx=1/2) # In[25]: Numerical .compare_states(diffuse_dx_1 , diffuse_dx_1_2, verbose=True) # # Again, restore the system to its initial (pre-diffusion) state # ### and then perform a diffusion over the same time span, but with QUADRUPLE the spacial resolution # ### delta_x will be be 1/4 instead of the original default 1 # In[26]: bio.restore_system(original_state) # In[27]: bio.describe_state() # In[28]: # Quadruple the spacial resolution bio.increase_spatial_resolution(4) bio.describe_state() # In[29]: # Now repeat the idential diffusion process as before, but with 1/4 the delta_x bio.diffuse(total_duration=7, time_step=0.0005, delta_x=0.25) # In[30]: # Finally, reduce the resolution by a factor 4, to return to the original number of bins bio.decrease_spatial_resolution(4) bio.describe_state(concise=True) # In[31]: # SAVE the above system data: this is the result of diffusion with delta_x of 1/4 diffuse_dx_1_4 = bio.system # ### Compare the latest 2 runs (with dx=1/2 and dx=1/4) # In[32]: Numerical .compare_states(diffuse_dx_1_2 , diffuse_dx_1_4) # ### Notice how the discrepancies have gone down # # One last time, restore the system to its initial (pre-diffusion) state # ### and then perform a diffusion over the same time span, but with 10x the spacial resolution # ### delta_x will be be 1/10 instead of the original default 1 # In[33]: bio.restore_system(original_state) # Increase by a factor 10 the spacial resolution bio.increase_spatial_resolution(10) bio.n_bins # In[34]: # Now repeat the idential diffusion process as before, but with 1/10 the delta_x bio.diffuse(total_duration=7, time_step=0.0005, delta_x=0.1) # In[35]: # Finally, reduce the resolution by a factor 10, to return to the original number of bins bio.decrease_spatial_resolution(10) bio.describe_state(concise=True) # In[36]: # SAVE the above system data: this is the result of diffusion with delta_x of 1/10 diffuse_dx_1_10 = bio.system # ### Again, compare the latest 2 runs (with dx=1/4 and dx=1/10) # In[37]: Numerical.compare_states(diffuse_dx_1_4 , diffuse_dx_1_10) # ### Notice how the discrepancies have gone down even more # ### This matches expectations that we're getting closer and closer to a "true" (very high precision) value, as we keep increasing the spacial resolution # In[ ]: