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.
LAST_REVISED = "Apr. 29, 2025"
LIFE123_VERSION = "1.0.0rc3" # Library version this experiment is based on
#import set_path # Using MyBinder? Uncomment this before running the next cell!
#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
check_version(LIFE123_VERSION)
OK
# 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()
SYSTEM STATE at Time t = 0: 9 bins and 1 chemical species:
| Species | Diff rate | Bin 0 | Bin 1 | Bin 2 | Bin 3 | Bin 4 | Bin 5 | Bin 6 | Bin 7 | Bin 8 | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | A | 0.6 | 100.0 | 100.0 | 100.0 | 100.0 | 100.0 | 100.0 | 100.0 | 100.0 | 100.0 |
bio.show_system_snapshot()
SYSTEM SNAPSHOT at time 0:
| A | |
|---|---|
| 0 | 100.0 |
| 1 | 100.0 |
| 2 | 100.0 |
| 3 | 100.0 |
| 4 | 100.0 |
| 5 | 100.0 |
| 6 | 100.0 |
| 7 | 100.0 |
| 8 | 100.0 |
# Visualize the system's initial state
bio.visualize_system(title_prefix="Diffusion")
bio.system_heatmaps(title_prefix="Diffusion", text_format=".0f")
delta_time = 1.
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()
SYSTEM STATE at Time t = 1: [[102.50172719 101.14444998 100.29805415 100.04921258 100. 99.95078742 99.70194585 98.85555002 97.49827281]]
SYSTEM STATE at Time t = 4: [[107.42285216 104.74375636 102.29241276 100.85388504 100. 99.14611496 97.70758724 95.25624364 92.57714784]]
SYSTEM STATE at Time t = 7: [[110.88507422 107.69013213 104.36899081 101.90793997 100. 98.09206003 95.63100919 92.30986787 89.11492578]]
SYSTEM STATE at Time t = 10: [[113.59743191 110.06014065 106.11080705 102.82666877 100. 97.17333123 93.88919295 89.93985935 86.40256809]]
SYSTEM STATE at Time t = 13: [[115.76754 111.96612839 107.52278434 103.57670355 100. 96.42329645 92.47721566 88.03387161 84.23246 ]]
SYSTEM STATE at Time t = 101: [[124.65573484 119.78183626 113.32331552 106.66286788 100. 93.33713212 86.67668448 80.21816374 75.34426516]]
SYSTEM STATE at Time t = 201: [[124.67064114 119.79494464 113.33304492 106.66804479 100. 93.33195521 86.66695508 80.20505536 75.32935886]]
SYSTEM STATE at Time t = 301: [[124.67065158 119.79495382 113.33305173 106.66804841 100. 93.33195159 86.66694827 80.20504618 75.32934842]]
SYSTEM STATE at Time t = 401: [[124.67065159 119.79495383 113.33305174 106.66804842 100. 93.33195158 86.66694826 80.20504617 75.32934841]]
SYSTEM STATE at Time t = 501: [[124.67065159 119.79495383 113.33305174 106.66804842 100. 93.33195158 86.66694826 80.20504617 75.32934841]]
Note: if the drain is too large, relative to the diffusion rate, the smaller concentration could "saturate" at zero