Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example rework #737

Merged
merged 8 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 71 additions & 124 deletions examples/AdvancedFeatures/CLE_examples_convergence.ipynb

Large diffs are not rendered by default.

55 changes: 25 additions & 30 deletions examples/AdvancedFeatures/Events.ipynb

Large diffs are not rendered by default.

3,184 changes: 1,595 additions & 1,589 deletions examples/AdvancedFeatures/KineticModelForStyrenePolymerization.ipynb

Large diffs are not rendered by default.

265 changes: 265 additions & 0 deletions examples/AdvancedFeatures/Live Output with C Solver.ipynb

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions examples/AdvancedFeatures/Live Output with Python Solvers.ipynb

Large diffs are not rendered by default.

169 changes: 169 additions & 0 deletions examples/AdvancedFeatures/Model_Pre_Compilation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solver Pre-Compilation\n",
"Most of the time, using the 'algorithm =' argument within model.run() is the best practice for assigning a particular algorithm to be used when a particular model is ran. This however will create issues when the model is ran within a loop body as each iteration will take almost a full five seconds in make alone, dramatically increasing the time taken to run each iteration. This is why it is best to precompile any model within a loop body. Below is a quick example of how to do so. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"import time\n",
"sys.path.insert(1, '../../')\n",
"import numpy\n",
"import matplotlib.pyplot as plt\n",
"import gillespy2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create a model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class MichaelisMenten(gillespy2.Model):\n",
" def __init__(self, parameter_values=None):\n",
" #initialize Model\n",
" gillespy2.Model.__init__(self, name=\"Michaelis_Menten\")\n",
" \n",
" #parameters\n",
" rate1 = gillespy2.Parameter(name='rate1', expression= 0.0017)\n",
" rate2 = gillespy2.Parameter(name='rate2', expression= 0.5)\n",
" rate3 = gillespy2.Parameter(name='rate3', expression = 0.1)\n",
" self.add_parameter([rate1,rate2,rate3])\n",
" \n",
" #Species\n",
" A = gillespy2.Species(name='Substrate', initial_value=301)\n",
" B = gillespy2.Species(name='Enzyme', initial_value=120)\n",
" C = gillespy2.Species(name='Enzyme_Substrate_Complex', initial_value=0)\n",
" D = gillespy2.Species(name='Product', initial_value=0)\n",
" self.add_species([A, B, C, D])\n",
" \n",
" #reactions\n",
" r1 = gillespy2.Reaction(name=\"r1\",reactants={A:1,B:1}, products={C:1},\n",
" rate=rate1)\n",
" \n",
" r2 = gillespy2.Reaction(name=\"r2\",reactants={C:1}, products={A:1,B:1},\n",
" rate=rate2)\n",
" \n",
" r3 = gillespy2.Reaction(name=\"r3\",reactants={C:1}, products={B:1,D:1},\n",
" rate=rate3)\n",
" self.add_reaction([r1,r2,r3])\n",
" self.timespan(numpy.linspace(0,100,101))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Instantiate the model without a solver or algorithm"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"model = MichaelisMenten()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Call get_best_solver_algo on model object and pass the desired algorithm name like you would with 'algorithm ='. Also pass the model object in a seperate bracket. This will Precompile the model."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"PrecompSolver = model.get_best_solver_algo(\"Tau-Hybrid\")(model)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Below is an example of why pre-compilation is neccsary when running a model in a loop body"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"start = time.perf_counter()\n",
"for i in range(20):\n",
" ode_results = model.run(solver = PrecompSolver)\n",
"end = time.perf_counter()\n",
"Precomp = end - start\n",
"\n",
"start = time.perf_counter()\n",
"for i in range(20):\n",
" hybrid_results = model.run(algorithm = \"Tau-Hybrid\")\n",
"end = time.perf_counter() \n",
"noPrecomp = end - start\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time to complete 20 runs using pre compiled model: 0.35088030000000003\n",
"Time to complete 20 runs without using pre compiled model: 198.7868176\n"
]
}
],
"source": [
"print(\"Time to complete 20 runs using pre compiled model: \" + str(Precomp))\n",
"print(\"Time to complete 20 runs without using pre compiled model: \" + str(noPrecomp))"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading