LAST_REVISED = "May 31, 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, PlotlyHelper, check_version
check_version(LIFE123_VERSION)
OK
with two chemicals A and B
chem_data = ChemData(diffusion_rates=[2., 2.], plot_colors=["turquoise", "green"]) # Names "A", "B" automatically assigned
bio = BioSim1D(n_bins=9, chem_data=chem_data)
bio.set_bin_conc(bin_address=4, chem_label="A", conc=10.)
bio.set_bin_conc(bin_address=4, chem_label="B", conc=10.)
bio.set_membranes(membranes=[ (4,5) ]) # By default impermeable
bio.change_permeability("B", 1.) # Permeable to `B` (and only to `B`)
bio.describe_state()
SYSTEM STATE at Time t = 0: 9 bins and 2 chemical species Membranes present: [(4, 5)]
| Species | Diff rate | Bin 0 | Bin 1 | Bin 2 | Bin 3 | Bin 4 | Bin 5 | Bin 6 | Bin 7 | Bin 8 | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | A | 2.0 | 0.0 | 0.0 | 0.0 | 0.0 | 10.0 | 0.0 | 0.0 | 0.0 | 0.0 |
| 1 | B | 2.0 | 0.0 | 0.0 | 0.0 | 0.0 | 10.0 | 0.0 | 0.0 | 0.0 | 0.0 |
bio.system_snapshot()
| A | B | |
|---|---|---|
| 0 | 0.0 | 0.0 |
| 1 | 0.0 | 0.0 |
| 2 | 0.0 | 0.0 |
| 3 | 0.0 | 0.0 |
| 4 | 10.0 | 10.0 |
| 5 | 0.0 | 0.0 |
| 6 | 0.0 | 0.0 |
| 7 | 0.0 | 0.0 |
| 8 | 0.0 | 0.0 |
bio.system_heatmaps()
# Request to save the concentration history at the bin with the initial concentration injection,
# and at a couple of other bins
bio.enable_history(bins=[0, 2, 4], frequency=3, take_snapshot=True)
History enabled for bins [0, 2, 4] and chemicals None (None means 'all')
bio.diffuse(time_step=0.02, n_steps=1)
{'steps': 1, 'system time': '0.02'}
bio.system_snapshot()
| A | B | |
|---|---|---|
| 0 | 0.0 | 0.0 |
| 1 | 0.0 | 0.0 |
| 2 | 0.0 | 0.0 |
| 3 | 0.0 | 0.2 |
| 4 | 10.0 | 9.6 |
| 5 | 0.0 | 0.2 |
| 6 | 0.0 | 0.0 |
| 7 | 0.0 | 0.0 |
| 8 | 0.0 | 0.0 |
bio.system_heatmaps()
bio.visualize_system() # Line curve view
A and B start to differ¶The membranes are impermeable to A but permeable to B
bio.diffuse(time_step=0.02, n_steps=4)
{'steps': 4, 'system time': '0.1'}
bio.system_heatmaps()
bio.visualize_system() # Line curve view
bio.diffuse(time_step=0.02, n_steps=15)
{'steps': 15, 'system time': '0.4'}
bio.system_heatmaps()
bio.visualize_system() # Line curve view
bio.diffuse(time_step=0.02, n_steps=600)
{'steps': 600, 'system time': '12.4'}
bio.system_heatmaps()
bio.visualize_system() # Line curve view
A and B|¶
B, it has simply shifted across bins¶# Let's verify mass conservation
bio.check_mass_conservation(chem_label="A", expected=10.)
True
bio.check_mass_conservation(chem_label="B", expected=10.)
True
A and B at selected bins (bins whose history-keeping we requested prior to running the simulation)¶bio.plot_history_single_bin(bin_address=4, title_prefix="`A` and `B` get separated")
A and B is best seen by plotting the difference of their concentrations, as a function of time¶bin4_hist = bio.conc_history.bin_history(bin_address=4) # The bin where the initial concentration of `A` and `B` was applied
bin4_hist
| SYSTEM TIME | A | B | |
|---|---|---|---|
| 0 | 0.00 | 10.0 | 10.000000 |
| 1 | 0.08 | 10.0 | 8.537110 |
| 2 | 0.16 | 10.0 | 7.383196 |
| 3 | 0.22 | 10.0 | 6.673179 |
| 4 | 0.28 | 10.0 | 6.068917 |
| ... | ... | ... | ... |
| 202 | 12.16 | 10.0 | 1.111178 |
| 203 | 12.22 | 10.0 | 1.111174 |
| 204 | 12.28 | 10.0 | 1.111171 |
| 205 | 12.34 | 10.0 | 1.111168 |
| 206 | 12.40 | 10.0 | 1.111165 |
207 rows × 3 columns
PlotlyHelper.plot_curves(x=bin4_hist['SYSTEM TIME'], y = bin4_hist['A'] - bin4_hist['B'],
x_label="SYSTEM TIME", y_label="[A] - [B]",
colors="purple", title="The separation of `A` and `B`, as seen in central bin 4")
A and B is also seen in other bins...¶bio.plot_history_single_bin(bin_address=0, title_prefix="The separation of `A` and `B` happens at other bins, too...")
bio.plot_history_single_bin(bin_address=2, title_prefix="The separation of `A` and `B` happens at other bins, too...")