{ "cells": [ { "cell_type": "markdown", "id": "d31149d6-a2ab-45e8-a465-6428937e2086", "metadata": {}, "source": [ "### Exploring effect of Time Resolution on accuracy\n", "\n", "In the examples below, the _time advance_ always remains constant,\n", "but the _number of steps_ used to arrive there vary" ] }, { "cell_type": "markdown", "id": "4e59b390-6f36-4548-92e3-f1601ec4a111", "metadata": {}, "source": [ "### TAGS : \"diffusion 1D\", \"under-the-hood\"" ] }, { "cell_type": "code", "execution_count": 1, "id": "27c384de-cb14-4a12-9c3d-5b2a75e6f4f8", "metadata": {}, "outputs": [], "source": [ "LAST_REVISED = \"May 3, 2025\"\n", "LIFE123_VERSION = \"1.0.0rc3\" # Library version this experiment is based on" ] }, { "cell_type": "code", "execution_count": 2, "id": "f594d956", "metadata": {}, "outputs": [], "source": [ "#import set_path # Using MyBinder? Uncomment this before running the next cell!" ] }, { "cell_type": "code", "execution_count": 3, "id": "5755befb-8156-43cc-bef9-c742a3498e6e", "metadata": {}, "outputs": [], "source": [ "#import sys\n", "#sys.path.append(\"C:/some_path/my_env_or_install\") # CHANGE to the folder containing your venv or libraries installation!\n", "# NOTE: If any of the imports below can't find a module, uncomment the lines above, or try: import set_path \n", "\n", "from life123 import BioSim1D, ChemData, check_version" ] }, { "cell_type": "code", "execution_count": 4, "id": "dca5fbbd-3a21-409e-a2f2-0d866bb5988b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OK\n" ] } ], "source": [ "check_version(LIFE123_VERSION)" ] }, { "cell_type": "code", "execution_count": null, "id": "bf15a069-5683-4a2b-ac3e-31397d069e17", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 5, "id": "baf827af-6cad-4ffb-a7a8-af415113345d", "metadata": {}, "outputs": [], "source": [ "def set_initial_condition():\n", " # Set or reset the initial concentrations\n", " bio.set_uniform_concentration(chem_index=0, conc=0.)\n", " bio.inject_conc_to_bin(bin_address=2, chem_index=0, delta_conc=10.)" ] }, { "cell_type": "code", "execution_count": 6, "id": "e20f3dd6-f5b3-47b7-8846-122ae250640e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SYSTEM STATE at Time t = 0:\n", "10 bins and 1 chemical species:\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SpeciesDiff rateBin 0Bin 1Bin 2Bin 3Bin 4Bin 5Bin 6Bin 7Bin 8Bin 9
0Chemical 10.10.00.010.00.00.00.00.00.00.00.0
\n", "
" ], "text/plain": [ " Species Diff rate Bin 0 Bin 1 Bin 2 Bin 3 Bin 4 Bin 5 Bin 6 \\\n", "0 Chemical 1 0.1 0.0 0.0 10.0 0.0 0.0 0.0 0.0 \n", "\n", " Bin 7 Bin 8 Bin 9 \n", "0 0.0 0.0 0.0 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chem_data = ChemData(diffusion_rates=0.1)\n", "bio = BioSim1D(n_bins=10, chem_data=chem_data)\n", "\n", "set_initial_condition()\n", "\n", "bio.describe_state()" ] }, { "cell_type": "code", "execution_count": 7, "id": "7dc6d6a4-a607-4660-87b5-7612501f0f00", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 8 ...\n", " Performing diffusion step 9 ...\n", "\n", "System after Delta time 33.3, at end of 10 steps of size 3.33:\n", "SYSTEM STATE at Time t = 33.3:\n", "[[1.94849832 1.89701268 1.76252648 1.51861039 1.18134438 0.81185766\n", " 0.48398585 0.24587699 0.10558355 0.04470372]]\n", "\n", " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 18 ...\n", " Performing diffusion step 19 ...\n", "\n", "System after Delta time 33.3, at end of 20 steps of size 1.665:\n", "SYSTEM STATE at Time t = 66.6:\n", "[[1.93083096 1.89918952 1.78309085 1.53809527 1.18379901 0.79887357\n", " 0.46852318 0.23832707 0.10742893 0.05184163]]\n", "\n", " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 28 ...\n", " Performing diffusion step 29 ...\n", "\n", "System after Delta time 33.3, at end of 30 steps of size 1.11:\n", "SYSTEM STATE at Time t = 99.9:\n", "[[1.92418469 1.90022582 1.79079709 1.54473636 1.18398957 0.79411327\n", " 0.46362705 0.23627042 0.10813759 0.05391814]]\n", "\n", " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 48 ...\n", " Performing diffusion step 49 ...\n", "\n", "System after Delta time 33.3, at end of 50 steps of size 0.666:\n", "SYSTEM STATE at Time t = 133.2:\n", "[[1.91860809 1.90117398 1.79725968 1.55006867 1.18390667 0.79018989\n", " 0.45982272 0.23476444 0.10871741 0.05548847]]\n", "\n", " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 98 ...\n", " Performing diffusion step 99 ...\n", "\n", "System after Delta time 33.3, at end of 100 steps of size 0.333:\n", "SYSTEM STATE at Time t = 166.5:\n", "[[1.91428037 1.90195278 1.80227809 1.55407026 1.18370653 0.78719167\n", " 0.45703829 0.2337084 0.10915577 0.05661783]]\n", "\n", " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 998 ...\n", " Performing diffusion step 999 ...\n", "\n", "System after Delta time 33.3, at end of 1000 steps of size 0.0333:\n", "SYSTEM STATE at Time t = 199.8:\n", "[[1.9102833 1.90270095 1.80691952 1.55766816 1.18342551 0.78445907\n", " 0.45458379 0.23280741 0.10955101 0.05760128]]\n", "\n", " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 9998 ...\n", " Performing diffusion step 9999 ...\n", "\n", "System after Delta time 33.3, at end of 10000 steps of size 0.00333:\n", "SYSTEM STATE at Time t = 233.1:\n", "[[1.90987842 1.90277812 1.80739016 1.5580276 1.18339217 0.78418423\n", " 0.45434105 0.23271976 0.10959052 0.05769798]]\n", "\n", " Performing diffusion step 0 ...\n", " Performing diffusion step 1 ...\n", " ...\n", " Performing diffusion step 99998 ...\n", " Performing diffusion step 99999 ...\n", "\n", "System after Delta time 33.3, at end of 100000 steps of size 0.000333:\n", "SYSTEM STATE at Time t = 266.4:\n", "[[1.90983788 1.90278586 1.80743729 1.55806354 1.18338878 0.78415673\n", " 0.4543168 0.23271101 0.10959447 0.05770763]]\n", "\n" ] }, { "data": { "text/plain": [ "{'steps': 100000}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t_final = 33.3\n", "\n", "bio.debug = True\n", "\n", "bio.diffuse(total_duration=t_final, n_steps=10)\n", "\n", "set_initial_condition() # Reset the concentrations\n", "bio.diffuse(total_duration=t_final, n_steps=20)\n", "\n", "set_initial_condition() # Reset the concentrations\n", "bio.diffuse(total_duration=t_final, n_steps=30)\n", "\n", "set_initial_condition() # Reset the concentrations\n", "bio.diffuse(total_duration=t_final, n_steps=50)\n", "\n", "set_initial_condition() # Reset the concentrations\n", "bio.diffuse(total_duration=t_final, n_steps=100)\n", "\n", "set_initial_condition() # Reset the concentrations\n", "bio.diffuse(total_duration=t_final, n_steps=1000)\n", "\n", "set_initial_condition() # Reset the concentrations\n", "bio.diffuse(total_duration=t_final, n_steps=10000)\n", "\n", "set_initial_condition() # Reset the concentrations\n", "bio.diffuse(total_duration=t_final, n_steps=100000)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }