| 1 |
.. _numerical_integration: |
| 2 |
|
| 3 |
Numerical integration |
| 4 |
===================== |
| 5 |
|
| 6 |
By default, Brian |
| 7 |
chooses an integration method automatically, trying to solve the equations |
| 8 |
exactly first (for linear equations) and then resorting to numerical algorithms. |
| 9 |
It will also take care of integrating stochastic differential equations |
| 10 |
appropriately. |
| 11 |
|
| 12 |
Note that in some cases, the automatic choice of integration method will not be |
| 13 |
appropriate, because of a choice of parameters that couldn't be determined in |
| 14 |
advance. In this case, typically you will get nan (not a number) values in the |
| 15 |
results, or large oscillations. In this case, Brian will generate a warning to |
| 16 |
let you know, but will not raise an error. |
| 17 |
|
| 18 |
Method choice |
| 19 |
------------- |
| 20 |
|
| 21 |
You will get an ``INFO`` message telling you which integration method Brian decided to use, |
| 22 |
together with information about how much time it took to apply the integration method |
| 23 |
to your equations. If other methods have been tried but were not applicable, you will |
| 24 |
also see the time it took to try out those other methods. In some cases, checking |
| 25 |
other methods (in particular the ``'exact'`` method which attempts to solve the |
| 26 |
equations analytically) can take a considerable amount of time -- to avoid wasting |
| 27 |
this time, you can always chose the integration method manually (see below). You |
| 28 |
can also suppress the message by raising the log level or by explicitly suppressing |
| 29 |
``'method_choice'`` log messages -- for details, see :doc:`../advanced/logging`. |
| 30 |
|
| 31 |
If you prefer to chose an integration algorithm yourself, you can do so using |
| 32 |
the ``method`` keyword for `NeuronGroup`, `Synapses`, or `SpatialNeuron`. |
| 33 |
The complete list of available methods is the following: |
| 34 |
|
| 35 |
* ``'exact'``: exact integration for linear equations (alternative name: ``'linear'``) |
| 36 |
* ``'exponential_euler'``: exponential Euler integration for conditionally |
| 37 |
linear equations |
| 38 |
* ``'euler'``: forward Euler integration (for additive stochastic |
| 39 |
differential equations using the Euler-Maruyama method) |
| 40 |
* ``'rk2'``: second order Runge-Kutta method (midpoint method) |
| 41 |
* ``'rk4'``: classical Runge-Kutta method (RK4) |
| 42 |
* ``'heun'``: stochastic Heun method for solving Stratonovich stochastic |
| 43 |
differential equations with non-diagonal multiplicative noise. |
| 44 |
* ``'milstein'``: derivative-free Milstein method for solving stochastic |
| 45 |
differential equations with diagonal multiplicative noise |
| 46 |
|
| 47 |
.. note:: |
| 48 |
|
| 49 |
The ``'independent'`` integration method (exact integration for a system of |
| 50 |
independent equations, where all the equations can be analytically solved |
| 51 |
independently) should no longer be used and might be removed in future |
| 52 |
versions of Brian. |
| 53 |
|
| 54 |
.. note:: The following methods are still considered experimental |
| 55 |
|
| 56 |
* ``'gsl'``: default integrator when choosing to integrate equations with |
| 57 |
the GNU Scientific Library ODE solver: the rkf45 method. Uses an adaptable |
| 58 |
time step by default. |
| 59 |
* ``'gsl_rkf45'``: Runge-Kutta-Fehlberg method. |
| 60 |
A good general-purpose integrator according to the GSL documentation. Uses an |
| 61 |
adaptable time step by default. |
| 62 |
* ``'gsl_rk2'``: Second order Runge-Kutta method using GSL. Uses an adaptable |
| 63 |
time step by default. |
| 64 |
* ``'gsl_rk4'``: Fourth order Runge-Kutta method using GSL. Uses an adaptable |
| 65 |
time step by default. |
| 66 |
* ``'gsl_rkck'``: Runge-Kutta Cash-Karp method using GSL. Uses an adaptable |
| 67 |
time step by default. |
| 68 |
* ``'gsl_rk8pd'``: Runge-Kutta Prince-Dormand method using GSL. Uses an adaptable |
| 69 |
time step by default. |
| 70 |
|
| 71 |
.. admonition:: The following topics are not essential for beginners. |
| 72 |
|
| 73 |
| |
| 74 |
|
| 75 |
Technical notes |
| 76 |
--------------- |
| 77 |
|
| 78 |
Each class defines its own list of algorithms it tries to |
| 79 |
apply, `NeuronGroup` and `Synapses` will use the first suitable method out of |
| 80 |
the methods ``'exact'``, ``'euler'`` and ``'heun'`` while `SpatialNeuron` |
| 81 |
objects will use ``'exact'``, ``'exponential_euler'``, ``'rk2'`` or ``'heun'``. |
| 82 |
|
| 83 |
You can also define your own numerical integrators, see |
| 84 |
:doc:`../advanced/state_update` for details. |
| 85 |
|
| 86 |
.. _gsl_integration: |
| 87 |
|
| 88 |
GSL stateupdaters |
| 89 |
----------------- |
| 90 |
The stateupdaters preceded with the gsl tag use ODE solvers defined in the GNU |
| 91 |
Scientific Library. The benefit of using these integrators over the ones written |
| 92 |
by Brian internally, is that they are implemented with an adaptable timestep. |
| 93 |
Integrating with an adaptable timestep comes with two advantages: |
| 94 |
|
| 95 |
* These methods check whether the estimated error of the solutions returned fall |
| 96 |
within a certain error bound. For the non-gsl integrators there is currently no |
| 97 |
such check. |
| 98 |
* Systems no longer need to be simulated with just one time step. That is, a bigger |
| 99 |
timestep can be chosen and the integrator will reduce the timestep when increased |
| 100 |
accuracy is required. This is particularly useful for systems where both slow and |
| 101 |
fast time constants coexist, as is the case with for example (networks of neurons |
| 102 |
with) Hodgkin-Huxley equations. Note that Brian's timestep still determines the |
| 103 |
resolution for monitors, spike timing, spike propagation etc. Hence, in a network, |
| 104 |
the simulation error will therefore still be on the order of ``dt``. The benefit |
| 105 |
is that short time constants occurring in equations no longer dictate the network |
| 106 |
time step. |
| 107 |
|
| 108 |
In addition to a choice between different integration methods, there are a few more |
| 109 |
options that can be specified when using GSL. These options can be specified by |
| 110 |
sending a dictionary as the ``method_options`` key upon initialization of the object |
| 111 |
using the integrator (`NeuronGroup`, `Synapses` or `SpatialNeuron`). |
| 112 |
The available method options are: |
| 113 |
|
| 114 |
* ``'adaptable_timestep'``: whether or not to let GSL reduce the timestep to |
| 115 |
achieve the accuracy defined with the ``'absolute_error'`` and |
| 116 |
``'absolute_error_per_variable'`` options described below. If this is set to ``False``, |
| 117 |
the timestep is determined by Brian (i.e. the ``dt`` of the respective clock is used, see :ref:`scheduling`). |
| 118 |
If the resulted estimated error exceeds the set error bounds, the simulation |
| 119 |
is aborted. When using cython this is reported with an `IntegrationError`. |
| 120 |
Defaults to ``True``. |
| 121 |
* ``'absolute_error'``: each of the methods has a way of estimating the error that |
| 122 |
is the result of using numerical integration. You can specify the maximum size of this |
| 123 |
error to be allowed for any of the to-be-integrated variables in base units with this |
| 124 |
keyword. Note that giving very small values makes the simulation slow and might result |
| 125 |
in unsuccessful integration. In the case of using the ``'absolute_error_per_variable'`` |
| 126 |
option, this is the error for variables that were not specified individually. |
| 127 |
Defaults to 1e-6. |
| 128 |
* ``'absolute_error_per_variable'``: specify the absolute error per variable in its own |
| 129 |
units. Variables for which the error is not specified use the error set with the |
| 130 |
``'absolute_error'`` option. |
| 131 |
Defaults to None. |
| 132 |
* ``'max_steps'``: The maximal number of steps that the integrator will take within a |
| 133 |
single "Brian timestep" in order to reach the given error criterion. Can be set to |
| 134 |
0 to not set any limits. Note that without limits, it can take a very long time |
| 135 |
until the integrator figures out that it cannot reach the desired error level. This |
| 136 |
will manifest as a simulation that appears to be stuck. |
| 137 |
Defaults to 100. |
| 138 |
* ``'use_last_timestep'``: with the ``'adaptable_timestep'`` option set to True, GSL tries |
| 139 |
different time steps to find a solution that satisfies the set error bounds. |
| 140 |
It is likely that for Brian's next time step the GSL time step |
| 141 |
will be somewhat similar per neuron (e.g. active neurons will have a shorter GSL time step |
| 142 |
than inactive neurons). With this option set to True, the time step GSL found to satisfy |
| 143 |
the set error bounds is saved per neuron and given to GSL again in Brian's next time step. |
| 144 |
This also means that the final time steps are saved in Brian's memory and can thus |
| 145 |
be recorded with the `StateMonitor`: it can be accessed under ``'_last_timestep'``. |
| 146 |
Note that some extra memory is required to keep track of the last time steps. |
| 147 |
Defaults to True. |
| 148 |
* ``'save_failed_steps'``: if ``'adaptable_timestep'`` is set to True, |
| 149 |
each time GSL tries a time step and it results in an estimated |
| 150 |
error that exceeds the set bounds, one is added to the ``'_failed_steps'`` variable. For |
| 151 |
purposes of investigating what happens within GSL during an integration step, we offer |
| 152 |
the option of saving this variable. |
| 153 |
Defaults to False. |
| 154 |
* ``'save_step_count'``: the same goes for the total number of GSL steps taken in a single |
| 155 |
Brian time step: this is optionally saved in the ``'_step_count'`` variable. |
| 156 |
Defaults to False. |
| 157 |
|
| 158 |
Note that at the moment recording ``'_last_timestep'``, ``'_failed_steps'``, or ``'_step_count'`` |
| 159 |
requires a call to `run` (e.g. with 0 ms) to trigger the code generation process, before the |
| 160 |
call to `StateMonitor`. |
| 161 |
|
| 162 |
More information on the GSL ODE solver itself can be found in its |
| 163 |
`documentation <https://www.gnu.org/software/gsl/manual/html_node/Ordinary-Differential-Equations.html>`_. |