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 = "Oct. 6, 2024"
LIFE123_VERSION = "1.0.0.beta.39" # Library version this experiment is based on
#import set_path # Using MyBinder? Uncomment this before running the next cell!
#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 experiments.get_notebook_info import get_notebook_basename
from life123 import BioSim1D
import plotly.express as px
import plotly.graph_objects as go
from life123 import ChemData as chem
from life123 import HtmlLog as log
from life123 import GraphicLog
# Initialize the HTML logging
log_file = get_notebook_basename() + ".log.htm" # Use the notebook base filename for the log file
# Set up the use of some specified graphic (Vue) components
GraphicLog.config(filename=log_file,
components=["vue_heatmap_11", "vue_curves_3"])
-> Output will be LOGGED into the file 'diffusion_1.log.htm'
# Set the heatmap parameters (for the log file)
heatmap_pars = {"range": [0, 2.5],
"outer_width": 850, "outer_height": 150,
"margins": {"top": 30, "right": 30, "bottom": 30, "left": 55}
}
# Set the parameters of the line plots
lineplot_pars = {"range": [0, 10],
"outer_width": 850, "outer_height": 250,
"margins": {"top": 30, "right": 30, "bottom": 30, "left": 55}
}
# Prepare the initial system, with a single non-zero bin, near the left edge of the system
chem_data = chem(names=["A"], diffusion_rates=[0.1])
bio = BioSim1D(n_bins=10, chem_data=chem_data)
bio.inject_conc_to_bin(bin_address=2, species_index=0, delta_conc=10.)
bio.describe_state()
SYSTEM STATE at Time t = 0: 10 bins and 1 species: Species 0 (A). Diff rate: 0.1. Conc: [ 0. 0. 10. 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 |
# Line curve view
fig = px.line(data_frame=bio.system_snapshot(), y=["A"],
title= f"Diffusion. System snapshot at time t={bio.system_time}",
color_discrete_sequence = ['red'],
labels={"value":"concentration", "variable":"Chemical", "index":"Bin number"})
fig.show()
# ONE APPROACH TO CREATE A PLOTLY HEATMAP, using imshow() from plotly.express
fig = px.imshow(bio.system_snapshot().T,
title= f"Diffusion. System snapshot as a heatmap at time t={bio.system_time}",
labels=dict(x="Bin number", y="Chem. species", color="Concentration"),
text_auto=True, color_continuous_scale="gray_r") # text_auto=’.2f’
fig.data[0].xgap=4
fig.data[0].ygap=4
fig.show()
# ANOTHER APPROACH TO CREATE A PLOTLY HEATMAP, using Heatmap() from plotly.graph_objects
data = go.Heatmap(z=bio.system_snapshot().T,
y=['A'],
colorscale='gray_r', colorbar={'title': 'Concentration'},
xgap=4, ygap=4, texttemplate = '%{z}', hovertemplate= 'Bin number: %{x}<br>Chem. species: %{y}<br>Concentration: %{z}<extra></extra>')
fig = go.Figure(data,
layout=go.Layout(title=f"Diffusion. System snapshot as a heatmap at time t={bio.system_time}",
xaxis={'title': 'Bin number'}, yaxis={'title': 'Chem. species'}
)
)
fig.show()
log.write("1-D diffusion to equilibrium of a single species, with Diffusion rate 0.1",
style=log.h2)
log.write("Initial system state at time t=0:", blanks_before=2, style=log.bold)
# Output a heatmap to the log file
bio.single_species_heatmap(species_index=0, heatmap_pars=heatmap_pars, graphic_component="vue_heatmap_11")
# Output a line plot the log file
bio.single_species_line_plot(species_index=0, plot_pars=lineplot_pars, graphic_component="vue_curves_3")
1-D diffusion to equilibrium of a single species, with Diffusion rate 0.1 Initial system state at time t=0: [GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`]
log.write("Advancing to time t=10, with time steps of 0.1 ... ", blanks_before=2, newline=False)
Advancing to time t=10, with time steps of 0.1 ...
delta_time = 10.
status = bio.diffuse(total_duration=delta_time, time_step=0.1)
print("\n", status)
log.write(f"After delta time {delta_time}. TOTAL TIME {bio.system_time} ({status['steps']} steps taken):")
bio.describe_state(concise=True)
{'steps': 100}
After delta time 10.0. TOTAL TIME 9.99999999999998 (100 steps taken):
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]]
# Line curve view
fig = px.line(data_frame=bio.system_snapshot(), y=["A"],
title= f"Diffusion. System snapshot at time t={bio.system_time}",
color_discrete_sequence = ['red'],
labels={"value":"concentration", "variable":"Chemical", "index":"Bin number"})
fig.show()
# Heatmap view
fig = px.imshow(bio.system_snapshot().T,
title= f"Diffusion. System snapshot as a heatmap at time t={bio.system_time}",
labels=dict(x="Bin number", y="Chem. species", color="Concentration"),
text_auto='.3f', color_continuous_scale="gray_r")
fig.data[0].xgap=4
fig.data[0].ygap=4
fig.show()
# Output a heatmap into the log file
bio.single_species_heatmap(species_index=0, heatmap_pars=heatmap_pars, graphic_component="vue_heatmap_11")
# Output a line plot the log file
bio.single_species_line_plot(species_index=0, plot_pars=lineplot_pars, graphic_component="vue_curves_3")
[GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`]
for i in range(50):
status = bio.diffuse(total_duration=delta_time, time_step=0.1)
print(f"\nAfter Delta time {delta_time}. TOTAL TIME {bio.system_time} ({status['steps']} steps taken):")
bio.describe_state(concise=True)
if i<2 or i==6 or i>=49:
# Line curve view
fig = px.line(data_frame=bio.system_snapshot(), y=["A"],
title= f"Diffusion. System snapshot at time t={bio.system_time}",
color_discrete_sequence = ['red'],
labels={"value":"concentration", "variable":"Chemical", "index":"Bin number"})
fig.show()
# Heatmap view
fig = px.imshow(bio.system_snapshot().T,
title= f"Diffusion. System snapshot as a heatmap at time t={bio.system_time}",
labels=dict(x="Bin number", y="Chem. species", color="Concentration"),
text_auto='.2f', color_continuous_scale="gray_r")
fig.data[0].xgap=4
fig.data[0].ygap=4
fig.show()
# Output a heatmap to the log file
bio.single_species_heatmap(species_index=0, heatmap_pars=heatmap_pars, header=f"Time {bio.system_time} :\n", graphic_component="vue_heatmap_11")
# Output a line plot the log file
bio.single_species_line_plot(species_index=0, plot_pars=lineplot_pars, graphic_component="vue_curves_3")
After Delta time 10.0. TOTAL TIME 20.000000000000014 (100 steps taken): 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]]
[GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] After Delta time 10.0. TOTAL TIME 30.000000000000156 (100 steps taken): 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]]
[GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] After Delta time 10.0. TOTAL TIME 40.0000000000003 (100 steps taken): SYSTEM STATE at Time t = 40: [[1.89162641 1.84625985 1.72030668 1.48693078 1.1664388 0.82118645 0.51779486 0.29499154 0.15846579 0.09599884]] After Delta time 10.0. TOTAL TIME 50.00000000000044 (100 steps taken): SYSTEM STATE at Time t = 50: [[1.83433746 1.76930884 1.63070397 1.41626323 1.14422761 0.85269498 0.58491221 0.37309998 0.2318666 0.16258513]] After Delta time 10.0. TOTAL TIME 60.00000000000058 (100 steps taken): SYSTEM STATE at Time t = 60: [[1.76697624 1.69877596 1.56309588 1.36661912 1.12701103 0.87222369 0.63367319 0.43785006 0.30151635 0.23225847]] After Delta time 10.0. TOTAL TIME 70.0000000000003 (100 steps taken): SYSTEM STATE at Time t = 70: [[1.69980096 1.63420894 1.50697719 1.32775575 1.11331789 0.8864918 0.67231694 0.49325804 0.36586375 0.30000873]] After Delta time 10.0. TOTAL TIME 79.99999999999973 (100 steps taken): 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]]
[GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] After Delta time 10.0. TOTAL TIME 89.99999999999916 (100 steps taken): SYSTEM STATE at Time t = 90: [[1.57781285 1.52179206 1.41478512 1.26674457 1.09200065 0.90798758 0.73325992 0.58522943 0.47821243 0.42217538]] After Delta time 10.0. TOTAL TIME 99.9999999999986 (100 steps taken): SYSTEM STATE at Time t = 100: [[1.5242553 1.47317189 1.37581121 1.2414767 1.08324636 0.91675072 0.75852442 0.6241924 0.52682923 0.47574177]] After Delta time 10.0. TOTAL TIME 109.99999999999802 (100 steps taken): SYSTEM STATE at Time t = 110: [[1.47549769 1.42905472 1.34063054 1.21878187 1.07540452 0.92459475 0.78121841 0.65937036 0.57094556 0.52450158]] After Delta time 10.0. TOTAL TIME 119.99999999999746 (100 steps taken): SYSTEM STATE at Time t = 120: [[1.43120344 1.38903875 1.30879999 1.19829836 1.06833665 0.93166317 0.80170171 0.69120023 0.61096132 0.56879638]] After Delta time 10.0. TOTAL TIME 129.9999999999969 (100 steps taken): SYSTEM STATE at Time t = 130: [[1.39100434 1.35274953 1.27996855 1.17976686 1.06194682 0.93805314 0.82023316 0.72003151 0.64725048 0.60899562]] After Delta time 10.0. TOTAL TIME 139.99999999999633 (100 steps taken): SYSTEM STATE at Time t = 140: [[1.35453928 1.31984299 1.25383973 1.16298212 1.05616129 0.94383869 0.83701788 0.74616029 0.68015701 0.6454607 ]] After Delta time 10.0. TOTAL TIME 149.99999999999577 (100 steps taken): SYSTEM STATE at Time t = 150: [[1.32146906 1.29000514 1.23015414 1.14777111 1.0509191 0.9490809 0.8522289 0.76984587 0.70999486 0.67853094]] After Delta time 10.0. TOTAL TIME 159.9999999999952 (100 steps taken): SYSTEM STATE at Time t = 160: [[1.29148094 1.26295037 1.20868068 1.13398258 1.04616753 0.95383247 0.86601742 0.79131932 0.73704963 0.70851906]] After Delta time 10.0. TOTAL TIME 169.99999999999463 (100 steps taken): SYSTEM STATE at Time t = 170: [[1.26428912 1.23841937 1.18921159 1.1214819 1.04185994 0.95814006 0.8785181 0.81078841 0.76158063 0.73571088]] After Delta time 10.0. TOTAL TIME 179.99999999999406 (100 steps taken): SYSTEM STATE at Time t = 180: [[1.2396335 1.21617681 1.17155929 1.1101481 1.0379545 0.9620455 0.8898519 0.82844071 0.78382319 0.7603665 ]] After Delta time 10.0. TOTAL TIME 189.9999999999935 (100 steps taken): SYSTEM STATE at Time t = 190: [[1.21727779 1.19600926 1.15555401 1.09987193 1.03441355 0.96558645 0.90012807 0.84444599 0.80399074 0.78272221]] After Delta time 10.0. TOTAL TIME 199.99999999999292 (100 steps taken): SYSTEM STATE at Time t = 200: [[1.19700758 1.17772317 1.14104198 1.09055457 1.03120299 0.96879701 0.90944543 0.85895802 0.82227683 0.80299242]] After Delta time 10.0. TOTAL TIME 209.99999999999235 (100 steps taken): SYSTEM STATE at Time t = 210: [[1.17862837 1.16114301 1.12788385 1.08210651 1.02829198 0.97170802 0.91789349 0.87211615 0.83885699 0.82137163]] After Delta time 10.0. TOTAL TIME 219.9999999999918 (100 steps taken): SYSTEM STATE at Time t = 220: [[1.16196378 1.14610965 1.11595329 1.0744466 1.02565255 0.97434745 0.9255534 0.88404671 0.85389035 0.83803622]] After Delta time 10.0. TOTAL TIME 229.99999999999122 (100 steps taken): SYSTEM STATE at Time t = 230: [[1.14685385 1.13247878 1.10513576 1.06750132 1.02325937 0.97674063 0.93249868 0.89486424 0.86752122 0.85314615]] After Delta time 10.0. TOTAL TIME 239.99999999999065 (100 steps taken): SYSTEM STATE at Time t = 240: [[1.13315355 1.12011956 1.09532742 1.06120397 1.02108945 0.97891055 0.93879603 0.90467258 0.87988044 0.86684645]] After Delta time 10.0. TOTAL TIME 249.99999999999008 (100 steps taken): SYSTEM STATE at Time t = 250: [[1.12073138 1.10891335 1.08643413 1.05549413 1.01912197 0.98087803 0.94450587 0.91356587 0.89108665 0.87926862]] After Delta time 10.0. TOTAL TIME 259.9999999999906 (100 steps taken): SYSTEM STATE at Time t = 260: [[1.1094681 1.0987526 1.07837051 1.05031696 1.01733804 0.98266196 0.94968304 0.92162949 0.9012474 0.8905319 ]] After Delta time 10.0. TOTAL TIME 269.9999999999929 (100 steps taken): SYSTEM STATE at Time t = 270: [[1.09925559 1.08953976 1.07105916 1.04562279 1.01572054 0.98427946 0.95437721 0.92894084 0.91046024 0.90074441]] After Delta time 10.0. TOTAL TIME 279.99999999999517 (100 steps taken): SYSTEM STATE at Time t = 280: [[1.08999583 1.08118641 1.0644299 1.04136654 1.01425394 0.98574606 0.95863346 0.9355701 0.91881359 0.91000417]] After Delta time 10.0. TOTAL TIME 289.99999999999744 (100 steps taken): SYSTEM STATE at Time t = 290: [[1.08159993 1.07361236 1.0584191 1.03750737 1.01292416 0.98707584 0.96249263 0.9415809 0.92638764 0.91840007]] After Delta time 10.0. TOTAL TIME 299.9999999999997 (100 steps taken): SYSTEM STATE at Time t = 300: [[1.07398731 1.06674491 1.05296906 1.03400823 1.01171844 0.98828156 0.96599177 0.94703094 0.93325509 0.92601269]] After Delta time 10.0. TOTAL TIME 310.000000000002 (100 steps taken): SYSTEM STATE at Time t = 310: [[1.06708488 1.06051814 1.04802747 1.03083553 1.0106252 0.9893748 0.96916447 0.95197253 0.93948186 0.93291512]] After Delta time 10.0. TOTAL TIME 320.00000000000426 (100 steps taken): SYSTEM STATE at Time t = 320: [[1.06082639 1.05487228 1.04354689 1.02795882 1.00963395 0.99036605 0.97204118 0.95645311 0.94512772 0.93917361]] After Delta time 10.0. TOTAL TIME 330.00000000000654 (100 steps taken): SYSTEM STATE at Time t = 330: [[1.05515177 1.04975313 1.03948431 1.02535049 1.00873518 0.99126482 0.97464951 0.96051569 0.95024687 0.94484823]] After Delta time 10.0. TOTAL TIME 340.0000000000088 (100 steps taken): SYSTEM STATE at Time t = 340: [[1.05000655 1.04511156 1.03580073 1.02298549 1.00792026 0.99207974 0.97701451 0.96419927 0.95488844 0.94999345]] After Delta time 10.0. TOTAL TIME 350.0000000000111 (100 steps taken): SYSTEM STATE at Time t = 350: [[1.04534133 1.04090301 1.03246081 1.02084112 1.00718136 0.99281864 0.97915888 0.96753919 0.95909699 0.95465867]] After Delta time 10.0. TOTAL TIME 360.00000000001336 (100 steps taken): SYSTEM STATE at Time t = 360: [[1.04111134 1.03708708 1.02943247 1.01889681 1.0065114 0.9934886 0.98110319 0.97056753 0.96291292 0.95888866]] After Delta time 10.0. TOTAL TIME 370.00000000001563 (100 steps taken): SYSTEM STATE at Time t = 370: [[1.03727598 1.03362715 1.02668666 1.01713389 1.00590394 0.99409606 0.98286611 0.97331334 0.96637285 0.96272402]] After Delta time 10.0. TOTAL TIME 380.0000000000179 (100 steps taken): SYSTEM STATE at Time t = 380: [[1.03379843 1.03049 1.024197 1.01553543 1.00535315 0.99464685 0.98446457 0.975803 0.96951 0.96620157]] After Delta time 10.0. TOTAL TIME 390.0000000000202 (100 steps taken): SYSTEM STATE at Time t = 390: [[1.0306453 1.02764553 1.02193961 1.0140861 1.00485374 0.99514626 0.9859139 0.97806039 0.97235447 0.9693547 ]] After Delta time 10.0. TOTAL TIME 400.00000000002245 (100 steps taken): SYSTEM STATE at Time t = 400: [[1.02778634 1.02506642 1.01989282 1.01277198 1.00440092 0.99559908 0.98722802 0.98010718 0.97493358 0.97221366]] After Delta time 10.0. TOTAL TIME 410.0000000000247 (100 steps taken): SYSTEM STATE at Time t = 410: [[1.02519409 1.02272792 1.01803698 1.01158045 1.00399035 0.99600965 0.98841955 0.98196302 0.97727208 0.97480591]] After Delta time 10.0. TOTAL TIME 420.000000000027 (100 steps taken): SYSTEM STATE at Time t = 420: [[1.02284368 1.02060758 1.01635427 1.01050009 1.00361808 0.99638192 0.98949991 0.98364573 0.97939242 0.97715632]] After Delta time 10.0. TOTAL TIME 430.0000000000293 (100 steps taken): SYSTEM STATE at Time t = 430: [[1.02071255 1.01868506 1.01482855 1.00952051 1.00328055 0.99671945 0.99047949 0.98517145 0.98131494 0.97928745]] After Delta time 10.0. TOTAL TIME 440.00000000003155 (100 steps taken): SYSTEM STATE at Time t = 440: [[1.01878023 1.01694189 1.01344516 1.00863233 1.0029745 0.9970255 0.99136767 0.98655484 0.98305811 0.98121977]] After Delta time 10.0. TOTAL TIME 450.0000000000338 (100 steps taken): SYSTEM STATE at Time t = 450: [[1.01702819 1.01536135 1.01219083 1.007827 1.002697 0.997303 0.992173 0.98780917 0.98463865 0.98297181]] After Delta time 10.0. TOTAL TIME 460.0000000000361 (100 steps taken): SYSTEM STATE at Time t = 460: [[1.01543959 1.01392826 1.01105353 1.0070968 1.00244539 0.99755461 0.9929032 0.98894647 0.98607174 0.98456041]] After Delta time 10.0. TOTAL TIME 470.00000000003837 (100 steps taken): SYSTEM STATE at Time t = 470: [[1.0139992 1.01262886 1.01002232 1.00643473 1.00221726 0.99778274 0.99356527 0.98997768 0.98737114 0.9860008 ]] After Delta time 10.0. TOTAL TIME 480.00000000004064 (100 steps taken): SYSTEM STATE at Time t = 480: [[1.01269318 1.01145069 1.00908732 1.00583442 1.0020104 0.9979896 0.99416558 0.99091268 0.98854931 0.98730682]] After Delta time 10.0. TOTAL TIME 490.0000000000429 (100 steps taken): SYSTEM STATE at Time t = 490: [[1.01150901 1.01038243 1.00823954 1.00529011 1.00182285 0.99817715 0.99470989 0.99176046 0.98961757 0.98849099]] After Delta time 10.0. TOTAL TIME 500.0000000000452 (100 steps taken): SYSTEM STATE at Time t = 500: [[1.01043531 1.00941383 1.00747086 1.00479659 1.00165279 0.99834721 0.99520341 0.99252914 0.99058617 0.98956469]] After Delta time 10.0. TOTAL TIME 510.00000000004746 (100 steps taken): 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]]
[GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `diffusion_1.log.htm`]
Mass conservations: The "10 units of concentration" are now uniformly spread across the 10 bins, leading to a near-constant concentration of 10/10 = 1.0