import set_path # Importing this module will add the project's home directory to sys.path
Added 'D:\Docs\- MY CODE\BioSimulations\life123-Win7' to sys.path
from experiments.get_notebook_info import get_notebook_basename
from life123 import BioSim1D
import plotly.express as px
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
GraphicLog.config(filename=log_file,
components=["vue_heatmap_11", "vue_curves_3"])
-> Output will be LOGGED into the file 'gradient_1.log.htm'
# Set the heatmap parameters
heatmap_pars = {"range": [75, 125],
"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": [75, 125],
"outer_width": 850, "outer_height": 250,
"margins": {"top": 30, "right": 30, "bottom": 30, "left": 55}
}
# Initialize the system with a uniform concentration (of the only species)
chem_data = chem(names=["A"], diffusion_rates=[0.6])
bio = BioSim1D(n_bins=9, chem_data=chem_data)
bio.set_uniform_concentration(species_index=0, conc=100.)
bio.describe_state()
SYSTEM STATE at Time t = 0: 9 bins and 1 species: Species 0 (A). Diff rate: 0.6. Conc: [100. 100. 100. 100. 100. 100. 100. 100. 100.]
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(caption="Diffusion")
# Show as heatmap
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()
# Output to the log file
log.write("Creation of a gradient", style=log.h3)
log.write(f"System state at time t={bio.system_time}:", blanks_before=2, style=log.bold)
# 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")
Creation of a gradient System state at time t=0: [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`]
delta_time = 1.
for i in range(501):
# Inject to the leftmost bin
bio.inject_conc_to_bin(bin_address=0, species_index=0, delta_conc=4, zero_clip = False)
# Drain from the rightmost bin
bio.inject_conc_to_bin(bin_address=8, species_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
bio.visualize_system(caption="Diffusion")
# Show as heatmap
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 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")
SYSTEM STATE at Time t = 1: [[102.50172719 101.14444998 100.29805415 100.04921258 100. 99.95078742 99.70194585 98.85555002 97.49827281]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 4: [[107.42285216 104.74375636 102.29241276 100.85388504 100. 99.14611496 97.70758724 95.25624364 92.57714784]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 7: [[110.88507422 107.69013213 104.36899081 101.90793997 100. 98.09206003 95.63100919 92.30986787 89.11492578]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 10: [[113.59743191 110.06014065 106.11080705 102.82666877 100. 97.17333123 93.88919295 89.93985935 86.40256809]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 13: [[115.76754 111.96612839 107.52278434 103.57670355 100. 96.42329645 92.47721566 88.03387161 84.23246 ]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 101: [[124.65573484 119.78183626 113.32331552 106.66286788 100. 93.33713212 86.67668448 80.21816374 75.34426516]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 201: [[124.67064114 119.79494464 113.33304492 106.66804479 100. 93.33195521 86.66695508 80.20505536 75.32935886]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 301: [[124.67065158 119.79495382 113.33305173 106.66804841 100. 93.33195159 86.66694827 80.20504618 75.32934842]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 401: [[124.67065159 119.79495383 113.33305174 106.66804842 100. 93.33195158 86.66694826 80.20504617 75.32934841]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] SYSTEM STATE at Time t = 501: [[124.67065159 119.79495383 113.33305174 106.66804842 100. 93.33195158 86.66694826 80.20504617 75.32934841]]
[GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`] [GRAPHIC ELEMENT SENT TO LOG FILE `gradient_1.log.htm`]
Note: if the drain is too large, relative to the diffusion rate, the smaller concentration could "saturate" at zero