The system starts out with a "concentration pulse" in bin 2 (the 3rd bin from the left) - 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.
LAST_REVISED = "Aug. 15, 2025"
LIFE123_VERSION = "1.0.0rc6" # 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
with a single non-zero bin concentration of the single chemical A, near the left edge of the system
chem_data = ChemData(names="A", diffusion_rates=0.1) # If you want to assign a default color, pass arg: plot_colors=["SOME_COLOR_NAME"]
bio = BioSim1D(n_bins=10, chem_data=chem_data)
bio.inject_conc_to_bin(bin_address=2, chem_label="A", delta_conc=10.)
bio.describe_state()
SYSTEM STATE at Time t = 0: 10 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 | Bin 9 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | A | 0.1 | 0.0 | 0.0 | 10.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
bio.system_snapshot()
| A | |
|---|---|
| 0 | 0.0 |
| 1 | 0.0 |
| 2 | 10.0 |
| 3 | 0.0 |
| 4 | 0.0 |
| 5 | 0.0 |
| 6 | 0.0 |
| 7 | 0.0 |
| 8 | 0.0 |
| 9 | 0.0 |
bio.visualize_system(title_prefix="Diffusion") # Line curve view
bio.system
array([[ 0., 0., 10., 0., 0., 0., 0., 0., 0., 0.]])
bio.system_heatmaps(title_prefix="Diffusion")
# Request to save the concentration history at the bin with the initial concentration injection,
# and the bins at the ends of the system
bio.enable_history(bins=[0, 2, 9], frequency=4, take_snapshot=True)
History enabled for bins [0, 2, 9], for ALL chemicals
# Advancing to time t=10, with time steps of 0.1 ...
# Not sure what value to pass for time steps? If unspecified, a default value is used
delta_time = 10.
status = bio.diffuse(total_duration=delta_time, time_step=0.1)
print(status)
bio.describe_state(concise=True)
{'steps': 100, 'system time': '10', 'time_step': 0.1}
SYSTEM STATE at Time t = 10:
[[1.22598070e+00 2.22414009e+00 3.08221111e+00 2.15823525e+00
9.37782076e-01 2.88503658e-01 6.79378836e-02 1.28711509e-02
2.03304706e-03 3.05037621e-04]]
bio.visualize_system(title_prefix="Diffusion") # Line curve view
bio.system_heatmaps(title_prefix="Diffusion")
(Visualization from results shown at selected times)
for i in range(50):
status = bio.diffuse(total_duration=delta_time, time_step=0.1)
if i<2 or i==6 or i>=49:
bio.describe_state(concise=True)
# Line curve view
fig = bio.visualize_system(title_prefix="Diffusion") # Line curve view
fig.show()
# Heatmap view
fig = bio.system_heatmaps()
fig.show()
SYSTEM STATE at Time t = 20: [[1.79154498 2.04604996 2.15752876 1.81408657 1.18572897 0.61493163 0.26031377 0.09234937 0.02835038 0.00911562]]
SYSTEM STATE at Time t = 30: [[1.908894 1.93254508 1.86205856 1.60230147 1.1912129 0.75904212 0.41665574 0.19951697 0.08641213 0.04136102]]
SYSTEM STATE at Time t = 80: [[1.63637931 1.57533401 1.45817295 1.29515888 1.10189171 0.89806096 0.7048592 0.54188555 0.42468407 0.36357336]]
SYSTEM STATE at Time t = 510: [[1.00946178 1.00853559 1.00677389 1.0043491 1.0014986 0.9985014 0.9956509 0.99322611 0.99146441 0.99053822]]
Mass conservations: The initial "10 units of concentration" are now uniformly spread across the 10 bins, leading to a near-constant concentration of 10/10 = 1.0
# Mass conservation can also be verified as follows:
bio.check_mass_conservation(chem_label="A", expected=10.)
True
A at either of the bins whose history we requested prior to running the simulation¶bio.conc_history.bin_history(bin_address=2) # The bin where the initial concentration was applied
| SYSTEM TIME | A | |
|---|---|---|
| 0 | 0.0 | 10.000000 |
| 1 | 0.4 | 9.235207 |
| 2 | 0.8 | 8.557276 |
| 3 | 1.2 | 7.955274 |
| 4 | 1.6 | 7.419713 |
| ... | ... | ... |
| 1271 | 508.4 | 1.006881 |
| 1272 | 508.8 | 1.006854 |
| 1273 | 509.2 | 1.006827 |
| 1274 | 509.6 | 1.006800 |
| 1275 | 510.0 | 1.006774 |
1276 rows × 2 columns
bio.plot_history_single_bin(bin_address=2)
bio.plot_history_single_bin(bin_address=0, title_prefix="Notice the transient 'overshoot`") # Left "edge" of the 1D system
bio.plot_history_single_bin(bin_address=9) # Right "edge" of the 1D system