Difference between revisions of "Team:EPF Lausanne/Software"
Line 364: | Line 364: | ||
<div class="row"> | <div class="row"> | ||
<div class="col-md-10 col-md-offset-1 text-justify"> | <div class="col-md-10 col-md-offset-1 text-justify"> | ||
− | <h3>ODE | + | <h3>ODE Solver</h3> |
+ | <p>Our kinetic model leads to a system of coupled fist-order, nonlinear, ordinary differential equations (ODEs). In order to solve this system we used an explicit Runge-Kutta method of order 4(5) with adaptative step size control and dense output due to Dormand and Prince, implemented by E. Hairer and G. Wanner [1] in the <a href="https://www.scipy.org/">SciPy</a> Python library. To facilitate the use of this integrator, we created an utility class which i suited for our needs.</p> | ||
+ | |||
+ | <p>Tee Solver class needs the function defining the system of ODEs we want to solve, an initial condition and the interval on which we want to integrate. Note that the time step \(\Delta t\) (which is also an argument of the constructor of the Solver class) is not the discretization step, because our algorithm is adaptative: \(\Delta t\) is the maximal allowed step and define the points where the solution of the ODEs system will be computed. </p> | ||
+ | |||
+ | <div class="highlight"><pre><span class="sd">"""</span> | ||
+ | <span class="sd">Copyright (C) 2015 iGEM Team EPF_Lausanne</span> | ||
+ | |||
+ | <span class="sd">This program is free software: you can redistribute it and/or modify</span> | ||
+ | <span class="sd">it under the terms of the GNU General Public License as published by</span> | ||
+ | <span class="sd">the Free Software Foundation, either version 3 of the License, or</span> | ||
+ | <span class="sd">(at your option) any later version.</span> | ||
+ | |||
+ | <span class="sd">This program is distributed in the hope that it will be useful,</span> | ||
+ | <span class="sd">but WITHOUT ANY WARRANTY; without even the implied warranty of</span> | ||
+ | <span class="sd">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> | ||
+ | <span class="sd">GNU General Public License for more details.</span> | ||
+ | |||
+ | <span class="sd">You should have received a copy of the GNU General Public License</span> | ||
+ | <span class="sd">along with this program. If not, see <http://www.gnu.org/licenses/></span> | ||
+ | <span class="sd">"""</span> | ||
+ | |||
+ | <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span> | ||
+ | <span class="kn">from</span> <span class="nn">scipy.integrate</span> <span class="kn">import</span> <span class="o">*</span> | ||
+ | |||
+ | <span class="k">class</span> <span class="nc">Solver</span><span class="p">:</span> | ||
+ | <span class="sd">"""</span> | ||
+ | <span class="sd"> Class that allows the solution of a system of non-linear ODEs. The system is specified by the function fun</span> | ||
+ | |||
+ | <span class="sd"> dy/dt = fun(t,y)</span> | ||
+ | |||
+ | <span class="sd"> where t is a number and y and dy/dt are numpy arrays or lists.</span> | ||
+ | |||
+ | <span class="sd"> The solution is performed with the dopri5 method, an explicit Runge-Kutta method of order (4)5.</span> | ||
+ | <span class="sd"> The method is due to Dormand & Prince, and is implemented by E. Hairer and G. Wanner.</span> | ||
+ | |||
+ | <span class="sd"> See</span> | ||
+ | <span class="sd"> http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.integrate.ode.html</span> | ||
+ | <span class="sd"> for more details.</span> | ||
+ | |||
+ | <span class="sd"> NOTE:</span> | ||
+ | <span class="sd"> Our Solver can take a function of the form</span> | ||
+ | |||
+ | <span class="sd"> f(t,y,pars)</span> | ||
+ | |||
+ | <span class="sd"> where PARS are parameters. PARS can be eventually passed to the constructor of the Solver.</span> | ||
+ | <span class="sd"> """</span> | ||
+ | |||
+ | <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">,</span><span class="n">fun</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">,</span><span class="n">pars</span><span class="o">=</span><span class="p">[]):</span> | ||
+ | <span class="bp">self</span><span class="o">.</span><span class="n">dt</span> <span class="o">=</span> <span class="n">dt</span> <span class="c"># Time step</span> | ||
+ | <span class="bp">self</span><span class="o">.</span><span class="n">fun</span> <span class="o">=</span> <span class="n">fun</span> <span class="c"># Function representing the ODE</span> | ||
+ | <span class="bp">self</span><span class="o">.</span><span class="n">t0</span> <span class="o">=</span> <span class="n">t0</span> <span class="c"># Initial time</span> | ||
+ | <span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">=</span> <span class="n">T</span> <span class="c"># Final time</span> | ||
+ | <span class="bp">self</span><span class="o">.</span><span class="n">y0</span> <span class="o">=</span> <span class="n">y0</span> <span class="c"># Initial condition</span> | ||
+ | |||
+ | <span class="bp">self</span><span class="o">.</span><span class="n">pars</span> <span class="o">=</span> <span class="n">pars</span> <span class="c"># Parameters of the system of ODEs</span> | ||
+ | |||
+ | <span class="k">def</span> <span class="nf">solve</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> | ||
+ | <span class="sd">"""</span> | ||
+ | <span class="sd"> Solve the system of ODEs</span> | ||
+ | |||
+ | <span class="sd"> dy/dt = fun(t,y)</span> | ||
+ | |||
+ | <span class="sd"> on the interval [t0,T], with the initial condition y(0)=y0.</span> | ||
+ | |||
+ | <span class="sd"> Returns two lists, time and solution, containing time points and the solution at these time points.</span> | ||
+ | <span class="sd"> """</span> | ||
+ | |||
+ | <span class="c"># Choose integrator type</span> | ||
+ | <span class="n">r</span> <span class="o">=</span> <span class="n">ode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fun</span><span class="p">)</span><span class="o">.</span><span class="n">set_integrator</span><span class="p">(</span><span class="s">'dopri5'</span><span class="p">)</span> | ||
+ | |||
+ | <span class="c"># Initialize the integrator</span> | ||
+ | <span class="n">r</span><span class="o">.</span><span class="n">set_initial_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">t0</span><span class="p">)</span> | ||
+ | |||
+ | <span class="c"># Set parameters for the ODE function</span> | ||
+ | <span class="n">r</span><span class="o">.</span><span class="n">set_f_params</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">pars</span><span class="p">)</span> | ||
+ | |||
+ | <span class="c"># Initialize solution list and time points list</span> | ||
+ | <span class="n">solution</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y0</span><span class="p">)</span> | ||
+ | <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">t0</span><span class="p">)</span> | ||
+ | |||
+ | <span class="k">while</span> <span class="n">r</span><span class="o">.</span><span class="n">successful</span><span class="p">()</span> <span class="ow">and</span> <span class="n">r</span><span class="o">.</span><span class="n">t</span> <span class="o"><</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span><span class="p">:</span> | ||
+ | <span class="n">r</span><span class="o">.</span><span class="n">integrate</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">t</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dt</span><span class="p">)</span> <span class="c"># Perform one integration step, i.e. obtain the solution y at time t+dt</span> | ||
+ | |||
+ | <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">time</span><span class="p">,</span><span class="n">r</span><span class="o">.</span><span class="n">t</span><span class="p">)</span> <span class="c"># Append the new time</span> | ||
+ | <span class="n">solution</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">solution</span><span class="p">,</span><span class="n">r</span><span class="o">.</span><span class="n">y</span><span class="p">))</span> <span class="c"># Append the new solution</span> | ||
+ | |||
+ | <span class="k">return</span> <span class="n">time</span><span class="p">,</span> <span class="n">solution</span> <span class="c"># Return time and solution vectors</span> | ||
+ | |||
+ | <span class="k">def</span> <span class="nf">solve_for_t</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">t</span><span class="p">):</span> | ||
+ | <span class="sd">"""</span> | ||
+ | <span class="sd"> Solve the system of ODEs</span> | ||
+ | |||
+ | <span class="sd"> dy/dt = fun(t,y)</span> | ||
+ | |||
+ | <span class="sd"> on the interval [t0,T], with the initial condition y(0)=y0.</span> | ||
+ | |||
+ | <span class="sd"> Returns two lists, solution and time, containing time points and the solution at these time points.</span> | ||
+ | |||
+ | <span class="sd"> The solution is computed at the points specified in t, i.e. the time step dt is ignored.</span> | ||
+ | <span class="sd"> """</span> | ||
+ | |||
+ | <span class="c"># Choose integrator type: dopri5 in this case</span> | ||
+ | <span class="n">r</span> <span class="o">=</span> <span class="n">ode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fun</span><span class="p">)</span><span class="o">.</span><span class="n">set_integrator</span><span class="p">(</span><span class="s">'dopri5'</span><span class="p">)</span> | ||
+ | |||
+ | <span class="c"># Initialize the integrator</span> | ||
+ | <span class="n">r</span><span class="o">.</span><span class="n">set_initial_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">t0</span><span class="p">)</span> | ||
+ | |||
+ | <span class="c"># Set parameters for the ODE function</span> | ||
+ | <span class="n">r</span><span class="o">.</span><span class="n">set_f_params</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">pars</span><span class="p">)</span> | ||
+ | |||
+ | <span class="c"># Initialize solution list and time points list</span> | ||
+ | <span class="n">solution</span> <span class="o">=</span> <span class="p">[]</span> | ||
+ | <span class="n">time</span> <span class="o">=</span> <span class="p">[]</span> | ||
+ | |||
+ | <span class="k">for</span> <span class="n">tt</span> <span class="ow">in</span> <span class="n">t</span><span class="p">:</span> | ||
+ | <span class="n">r</span><span class="o">.</span><span class="n">integrate</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span> <span class="c"># Perform one integration step</span> | ||
+ | |||
+ | <span class="n">time</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span> <span class="c"># Append the new time</span> | ||
+ | <span class="n">solution</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">y</span><span class="p">)</span> <span class="c"># Append the new solution</span> | ||
+ | |||
+ | <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">time</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span> <span class="c"># Return time and solution vectors</span> | ||
+ | |||
+ | |||
+ | <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span> | ||
+ | <span class="sd">"""</span> | ||
+ | <span class="sd"> Our test functions:</span> | ||
+ | <span class="sd"> rapid_equilibrium (standard function)</span> | ||
+ | <span class="sd"> rapid_equilibrium_from_string() (returns a function compiled from a string)</span> | ||
+ | <span class="sd"> """</span> | ||
+ | |||
+ | <span class="kn">import</span> <span class="nn">matplotlib.pylab</span> <span class="kn">as</span> <span class="nn">plt</span> | ||
+ | <span class="kn">from</span> <span class="nn">test</span> <span class="kn">import</span> <span class="o">*</span> <span class="c"># Import test functions for the ODE integrator</span> | ||
+ | |||
+ | <span class="n">dt</span> <span class="o">=</span> <span class="mf">0.1</span> | ||
+ | |||
+ | <span class="n">t0</span> <span class="o">=</span> <span class="mi">0</span> | ||
+ | <span class="n">T</span> <span class="o">=</span> <span class="mi">100</span> | ||
+ | <span class="n">y0</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]</span> | ||
+ | |||
+ | <span class="c"># Store the funtion compiled from a string</span> | ||
+ | <span class="n">rapid_equilibrium_s</span> <span class="o">=</span> <span class="n">rapid_equilibrium_from_string</span><span class="p">()</span> | ||
+ | |||
+ | <span class="n">mysolver</span> <span class="o">=</span> <span class="n">Solver</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="n">rapid_equilibrium</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">)</span> | ||
+ | <span class="n">mysolver_string</span> <span class="o">=</span> <span class="n">Solver</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="n">rapid_equilibrium_s</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">)</span> | ||
+ | |||
+ | <span class="n">t</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">mysolver</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span> | ||
+ | <span class="n">tt</span><span class="p">,</span><span class="n">yt</span> <span class="o">=</span> <span class="n">mysolver</span><span class="o">.</span><span class="n">solve_for_t</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span> | ||
+ | <span class="n">ts</span><span class="p">,</span><span class="n">ys</span> <span class="o">=</span> <span class="n">mysolver_string</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span> | ||
+ | |||
+ | <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> | ||
+ | <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">tt</span><span class="p">,</span><span class="n">yt</span><span class="p">,</span><span class="s">'x'</span><span class="p">)</span> | ||
+ | <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ts</span><span class="p">,</span><span class="n">ys</span><span class="p">)</span> | ||
+ | <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> | ||
+ | </pre></div> | ||
+ | |||
+ | |||
+ | |||
+ | <h4>References</h4> | ||
+ | [1] E. Hairer et al., Solving Ordinary Differential Equations, 2nd edition, Springer-Verlag, 1993. | ||
</div> | </div> | ||
</div> | </div> | ||
Line 372: | Line 531: | ||
<div class="row"> | <div class="row"> | ||
<div class="col-md-10 col-md-offset-1 text-justify"> | <div class="col-md-10 col-md-offset-1 text-justify"> | ||
− | <h3>ODE | + | <h3>ODE Fit</h3> |
</div> | </div> |
Revision as of 20:36, 15 September 2015
Name | Description | |
---|---|---|
code2html | Script creating automatically HTML and CSS code from source files in Python, C++ or BASH. | Download |
ODE Solver | Class solving a system of non-linear ODEs given the initial condition. | Download |
ODE Fit | Class fitting the parameter of a system of ODEs to experimental data. | Download |
Human Blaster | Script blasting gRNAs versus the human genome. | Download |
code2html
The following Python script allows to generate HTML (and CSS) code from source files in C++ and Python languages. It is based on Pygment, a Python syntax highlighter. All code in our Wiki is formatted using this script.
This script accepts two command line arguments: the first argument is the name of the file to convert, the second one (optional) is to ask separate HTML and CSS files.
The style is hard coded, but it can be changed easily by modifying the style string. Pygment documentation lists available themes and explains how to create new ones.
"""
Copyright (C) 2015 iGEM Team EPF_Lausanne
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
Command:
######################################
python code2html INPUTFILE [CSS]
######################################
INPUTFILE: name (with path) of the file to convert to html
CSS: write "true" (ot "t", "yes", "y") in order to obtain separate .html and .css files ("false" by default)
"""
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.lexers import CppLexer
from pygments.lexers import BashLexer
from pygments.formatters import HtmlFormatter
# Code formatting style
style = "monokai"
# C++ extensions
cpp = ["cpp","cxx","cc","h"]
# Python extensions
py = ["py"]
# Bash extensions
bash = ["sh","bash"]
def load_file_as_sting(fname):
"""
Open the file FNAME and save all its content in an unformatted string
"""
content = ""
with open(fname,'r') as f: # Open the file (read only)
content = f.read() # Read file and store it in an unformatted string
# The file is automatically closed
return content
def save_string_as_file(fname,string):
"""
Save the unformatted string STRING into the file FNAME
"""
with open(fname,'w') as f: # Open the file (write only)
f.write(string)
# The file is automatically closed
def lexer_formatter(language,css=False):
"""
Return the lexer for the appropriate language and the HTML formatter
"""
L = None
if language in py:
# Python Lexer
L = PythonLexer()
elif language in cpp:
# C++ Lexer
L = CppLexer()
elif language in bash:
# Bash Lexer
L = BashLexer()
else:
raise NameError("Invalid language.")
HF = HtmlFormatter(full=not css,style=style)
return L, HF
def code_to_htmlcss(code,language):
"""
Transform CODE into html and css (separate files)
"""
# Obtain lexer and HtmlFormatter
L, HF = lexer_formatter(language,css=True)
# Create html code
html = highlight(code,L,HF)
# Create css code
css = HF.get_style_defs('.highlight')
return html,css
def code_to_html(code,language):
"""
Transform CODE into html and css (all in the same file)
"""
# Obtain lexer and HtmlFormatter
L, HF = lexer_formatter(language)
# Create fill html code
html = highlight(code,L,HF)
return html
import sys
if __name__ == "__main__":
"""
Command:
######################################
python code2html INPUTFILE [CSS]
######################################
INPUTFILE: name (with path) of the file to convert to html
CSS: write "true" (ot "t", "yes", "y") in order to obtain separate .html and .css files ("false" by default)
"""
# Command line arguments
args = sys.argv
# Check command line arguments
ncla = len(args) # number of command line arguments
if ncla != 2 and ncla != 3 :
raise TypeError("Invalid number of command line arguments.")
css_bool = False
if ncla == 3 and args[-1].lower() in ["true",'t',"yes",'y']:
css_bool = True # Export css separately
# Input file
fname_code = sys.argv[1] # Name of the file containing the code to convert in html
# Input file extension
language = fname_code.split('.')[-1]
# Output files
fname_html = fname_code.split('.')[0] + ".html" # Name of the file where the html code will be stored
fname_css = fname_code.split('.')[0] + ".css" # Name of the file where the css code will be stored
# Save code into a unformatted string
code = load_file_as_sting(fname_code)
if css_bool == False: # Convert to standalone html
html = code_to_html(code,language)
else: # Convert to html and css separately
html,css = code_to_htmlcss(code,language)
# Save html
save_string_as_file(fname_html,html)
if css_bool == True:
# Save css
save_string_as_file(fname_css,css)
ODE Solver
Our kinetic model leads to a system of coupled fist-order, nonlinear, ordinary differential equations (ODEs). In order to solve this system we used an explicit Runge-Kutta method of order 4(5) with adaptative step size control and dense output due to Dormand and Prince, implemented by E. Hairer and G. Wanner [1] in the SciPy Python library. To facilitate the use of this integrator, we created an utility class which i suited for our needs.
Tee Solver class needs the function defining the system of ODEs we want to solve, an initial condition and the interval on which we want to integrate. Note that the time step \(\Delta t\) (which is also an argument of the constructor of the Solver class) is not the discretization step, because our algorithm is adaptative: \(\Delta t\) is the maximal allowed step and define the points where the solution of the ODEs system will be computed.
"""
Copyright (C) 2015 iGEM Team EPF_Lausanne
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
"""
import numpy as np
from scipy.integrate import *
class Solver:
"""
Class that allows the solution of a system of non-linear ODEs. The system is specified by the function fun
dy/dt = fun(t,y)
where t is a number and y and dy/dt are numpy arrays or lists.
The solution is performed with the dopri5 method, an explicit Runge-Kutta method of order (4)5.
The method is due to Dormand & Prince, and is implemented by E. Hairer and G. Wanner.
See
http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.integrate.ode.html
for more details.
NOTE:
Our Solver can take a function of the form
f(t,y,pars)
where PARS are parameters. PARS can be eventually passed to the constructor of the Solver.
"""
def __init__(self,dt,fun,t0,T,y0,pars=[]):
self.dt = dt # Time step
self.fun = fun # Function representing the ODE
self.t0 = t0 # Initial time
self.T = T # Final time
self.y0 = y0 # Initial condition
self.pars = pars # Parameters of the system of ODEs
def solve(self):
"""
Solve the system of ODEs
dy/dt = fun(t,y)
on the interval [t0,T], with the initial condition y(0)=y0.
Returns two lists, time and solution, containing time points and the solution at these time points.
"""
# Choose integrator type
r = ode(self.fun).set_integrator('dopri5')
# Initialize the integrator
r.set_initial_value(self.y0, self.t0)
# Set parameters for the ODE function
r.set_f_params(*self.pars)
# Initialize solution list and time points list
solution = np.asarray(self.y0)
time = np.asarray(self.t0)
while r.successful() and r.t < self.T:
r.integrate(r.t + self.dt) # Perform one integration step, i.e. obtain the solution y at time t+dt
time = np.append(time,r.t) # Append the new time
solution = np.vstack((solution,r.y)) # Append the new solution
return time, solution # Return time and solution vectors
def solve_for_t(self,t):
"""
Solve the system of ODEs
dy/dt = fun(t,y)
on the interval [t0,T], with the initial condition y(0)=y0.
Returns two lists, solution and time, containing time points and the solution at these time points.
The solution is computed at the points specified in t, i.e. the time step dt is ignored.
"""
# Choose integrator type: dopri5 in this case
r = ode(self.fun).set_integrator('dopri5')
# Initialize the integrator
r.set_initial_value(self.y0, self.t0)
# Set parameters for the ODE function
r.set_f_params(*self.pars)
# Initialize solution list and time points list
solution = []
time = []
for tt in t:
r.integrate(tt) # Perform one integration step
time.append(tt) # Append the new time
solution.append(r.y) # Append the new solution
return np.asarray(time), np.asarray(solution) # Return time and solution vectors
if __name__ == "__main__":
"""
Our test functions:
rapid_equilibrium (standard function)
rapid_equilibrium_from_string() (returns a function compiled from a string)
"""
import matplotlib.pylab as plt
from test import * # Import test functions for the ODE integrator
dt = 0.1
t0 = 0
T = 100
y0 = [1,0,0]
# Store the funtion compiled from a string
rapid_equilibrium_s = rapid_equilibrium_from_string()
mysolver = Solver(dt,rapid_equilibrium,t0,T,y0)
mysolver_string = Solver(dt,rapid_equilibrium_s,t0,T,y0)
t,y = mysolver.solve()
tt,yt = mysolver.solve_for_t(np.linspace(t0,T,10))
ts,ys = mysolver_string.solve()
plt.plot(t,y)
plt.plot(tt,yt,'x')
plt.plot(ts,ys)
plt.show()