| 1 |
Equations |
| 2 |
========= |
| 3 |
|
| 4 |
.. contents:: |
| 5 |
:local: |
| 6 |
:depth: 1 |
| 7 |
|
| 8 |
.. _equation_strings: |
| 9 |
|
| 10 |
Equation strings |
| 11 |
---------------- |
| 12 |
Equations are used both in `NeuronGroup` and `Synapses` to: |
| 13 |
|
| 14 |
* define state variables |
| 15 |
* define continuous-updates on these variables, through differential equations |
| 16 |
|
| 17 |
.. note:: |
| 18 |
|
| 19 |
Brian models are defined by systems of first order ordinary differential |
| 20 |
equations, but you might see the integrated form of synapses in some textbooks |
| 21 |
and papers. See :doc:`converting_from_integrated_form` for details on how |
| 22 |
to convert between these representations. |
| 23 |
|
| 24 |
Equations are defined by multiline strings, where each line takes of one of three forms: |
| 25 |
|
| 26 |
1. ``dx/dt = f : unit (flags)`` (differential equation) |
| 27 |
2. ``x = f : unit (flags)`` (subexpression) |
| 28 |
3. ``x : unit (flags)`` (parameter) |
| 29 |
|
| 30 |
Each of these definitions can take optional ``flags`` in parentheses after the ``unit`` declaration (see :ref:`flags` below). |
| 31 |
|
| 32 |
The first form defines a differential equation that determines how a variable evolves over time. |
| 33 |
The second form defines a subexpression, which is useful to make complex equations more readable, and to have a |
| 34 |
name for expressions that can be recorded with a `StateMonitor`. Such subexpressions are computed |
| 35 |
"on demand" and are not stored. Their use is therefore mostly for convenience and does not affect simulation time |
| 36 |
or memory usage. The third form defines a parameter, which is a value that is unique to each neuron or synapse. |
| 37 |
Its value can either be constant (e.g. to have a heterogeneous population of neurons) or can be a value that gets |
| 38 |
updated by synaptic events, or by `~.Group.run_regularly` operations. |
| 39 |
|
| 40 |
Each definition may be spread out over multiple lines to improve readability, and can include comments after ``#``. |
| 41 |
The ``unit`` definition defines the dimension of the variable. Note that these are always the dimensions of the |
| 42 |
*variable* defined in the line, even in the case of differential equations. Therefore, the unit for the membrane |
| 43 |
potential would be ``volt`` and not ``volt/second`` (the dimensions of its derivative). The ``unit`` always has to |
| 44 |
be a *base unit*, i.e., one must write ``volt``, not ``mV``. This is to make it clear that the values are |
| 45 |
internally always saved in the base units, so no confusion can arise when getting the values out of a `NeuronGroup` |
| 46 |
and discarding the units. Compound units are of course allowed as well (e.g. ``farad/meter**2``). |
| 47 |
There are also three special "units" that can be used: ``1`` denotes a dimensionless floating point variable, |
| 48 |
``boolean`` and ``integer`` denote dimensionless variables of the respective kind. |
| 49 |
|
| 50 |
.. note:: For molar concentration, the base unit that has to be used in the equations is ``mmolar`` (or ``mM``), *not* |
| 51 |
``molar``. This is because 1 molar is 10³ mol/m³ in SI units (i.e., it has a "scale" of 10³), whereas |
| 52 |
1 millimolar corresponds to 1 mol/m³. |
| 53 |
|
| 54 |
Arithmetic operations and functions |
| 55 |
----------------------------------- |
| 56 |
Equation strings can make use of standard arithmetic operations for numerical |
| 57 |
values, using the Python 3 syntax. The supported operations are ``+``, ``-``, |
| 58 |
``*``, ``/`` (floating point division), ``//`` (flooring division), ``%`` |
| 59 |
(remainder), ``**`` (power). For variable assignments, e.g. in reset statements, |
| 60 |
the corresponding in-place assignments such as ``+=`` can be used as well. |
| 61 |
For comparisons, the operations ``==`` (equality), ``!=`` (inequality), ``<``, |
| 62 |
``<=``, ``>``, and ``>=`` are available. Truth values can be combined using |
| 63 |
``and`` and ``or``, or negated using ``not``. Note that Brian does not support |
| 64 |
any operations specific to integers, e.g. "bitwise AND" or shift operations. Importantly, while equations use Python |
| 65 |
syntax, they are not Python code; they are parsed and translated to the target language by Brian, and can therefore |
| 66 |
not use arbitrary Python syntax or functions. They are also written in a "for each neuron/synapse" style, so their |
| 67 |
interpretation depends on the context in which they are used. For example, when a synaptic pre/post statement refers to a |
| 68 |
variable of a pre- or post-synaptic neurons, it only refers to the subset of neurons that spiked. This also means that |
| 69 |
you cannot (and usually don't need to) use Python's indexing syntax to refer to specific elements of a group. |
| 70 |
|
| 71 |
.. warning:: |
| 72 |
|
| 73 |
Brian versions up to 2.1.3.1 did not support ``//`` as the floor division |
| 74 |
operator and potentially used different semantics for the ``/`` operator |
| 75 |
depending on whether Python 2 or 3 was used. To write code that correctly |
| 76 |
and unambiguously works with both newer and older Brian versions, you can |
| 77 |
use expressions such as ``1.0*a/b`` to enforce floating point division (if |
| 78 |
one of the operands is a floating point number, both Python 2 and 3 will use |
| 79 |
floating point division), or ``floor(a/b)`` to enforce flooring division |
| 80 |
Note that the ``floor`` function always returns a floating point value, if |
| 81 |
it is important that the result is an integer value, additionally wrap it |
| 82 |
with the ``int`` function. |
| 83 |
|
| 84 |
Brian also supports standard mathematical functions with the same names as used |
| 85 |
in the ``numpy`` library (e.g. ``exp``, ``sqrt``, ``abs``, ``clip``, ``sin``, |
| 86 |
``cos``, ...) -- for a full list see :ref:`default_functions`. Note that support |
| 87 |
for such functions is provided by Brian itself and the translation to the |
| 88 |
various code generation targets is automatically taken care of. You should |
| 89 |
therefore refer to them directly by name and not as e.g. ``np.sqrt`` or |
| 90 |
``numpy.sqrt``, regardless of the way you |
| 91 |
:doc:`imported Brian or numpy <import>`. This also means that you cannot |
| 92 |
directly refer to arbitrary functions from ``numpy`` or other libraries. For |
| 93 |
details on how to extend the support to non-default functions see |
| 94 |
:ref:`user_functions`. |
| 95 |
|
| 96 |
Special variables |
| 97 |
----------------- |
| 98 |
|
| 99 |
Some special variables are defined, e.g. `t`, `dt` (time) and `xi` (white noise). For a full list see :ref:`special_symbols` below. |
| 100 |
Variable names starting with an underscore and a couple of other names that have special meanings under certain |
| 101 |
circumstances (e.g. names ending in ``_pre`` or ``_post``) are forbidden. |
| 102 |
|
| 103 |
For stochastic equations with several ``xi`` values it is necessary to make clear whether they correspond to the same |
| 104 |
or different noise instantiations. To make this distinction, an arbitrary suffix can be used, e.g. using ``xi_1`` several times |
| 105 |
refers to the same variable, ``xi_2`` (or ``xi_inh``, ``xi_alpha``, etc.) refers to another. An error will be raised if |
| 106 |
you use more than one plain ``xi`` without any suffix. Note that noise is always independent across neurons, you can only work around this |
| 107 |
restriction by defining your noise variable as a shared parameter and update it using a user-defined function (e.g. with `~Group.run_regularly`), |
| 108 |
or create a group that models the noise and link to its variable (see :ref:`linked_variables`). |
| 109 |
|
| 110 |
.. _external-variables: |
| 111 |
|
| 112 |
External references |
| 113 |
------------------- |
| 114 |
Equations defining neuronal or synaptic equations can contain references to |
| 115 |
external constants or functions. These references are looked up at the time |
| 116 |
that the simulation is run. If you don't specify where to look them up, it |
| 117 |
will look in the Python local/global namespace (i.e. the block of code where |
| 118 |
you call `run`). If you want to override this, you can specify an explicit |
| 119 |
"namespace". This is a Python dictionary with keys being variable names as |
| 120 |
they appear in the equations, and values being the desired value of that |
| 121 |
variable. This namespace can be specified either in the creation of the group |
| 122 |
or when you can the `run` function using the ``namespace`` keyword argument. |
| 123 |
|
| 124 |
The following three examples show the different ways of providing external |
| 125 |
variable values, all having the same effect in this case:: |
| 126 |
|
| 127 |
# Explicit argument to the NeuronGroup |
| 128 |
G = NeuronGroup(1, 'dv/dt = -v / tau : 1', namespace={'tau': 10*ms}) |
| 129 |
net = Network(G) |
| 130 |
net.run(10*ms) |
| 131 |
|
| 132 |
# Explicit argument to the run function |
| 133 |
G = NeuronGroup(1, 'dv/dt = -v / tau : 1') |
| 134 |
net = Network(G) |
| 135 |
net.run(10*ms, namespace={'tau': 10*ms}) |
| 136 |
|
| 137 |
# Implicit namespace from the context |
| 138 |
G = NeuronGroup(1, 'dv/dt = -v / tau : 1') |
| 139 |
net = Network(G) |
| 140 |
tau = 10*ms |
| 141 |
net.run(10*ms) |
| 142 |
|
| 143 |
See :doc:`../advanced/namespaces` for more details. |
| 144 |
|
| 145 |
.. admonition:: The following topics are not essential for beginners. |
| 146 |
|
| 147 |
| |
| 148 |
|
| 149 |
.. _flags: |
| 150 |
|
| 151 |
Flags |
| 152 |
----- |
| 153 |
A *flag* is a keyword in parentheses at the end of the line, which |
| 154 |
qualifies the equations. There are several keywords: |
| 155 |
|
| 156 |
*event-driven* |
| 157 |
this is only used in Synapses, and means that the differential equation should be updated |
| 158 |
only at the times of events. This implies that the equation is taken out of the continuous |
| 159 |
state update, and instead a event-based state update statement is generated and inserted into |
| 160 |
event codes (pre and post). |
| 161 |
This can only qualify differential equations of synapses. Currently, only one-dimensional |
| 162 |
linear equations can be handled (see below). |
| 163 |
*unless refractory* |
| 164 |
this means the variable is not updated during the refractory period. |
| 165 |
This can only qualify differential equations of neuron groups. |
| 166 |
*constant* |
| 167 |
this means the parameter will not be changed during a run. This allows |
| 168 |
optimizations in state updaters. This can only qualify parameters. |
| 169 |
*constant over dt* |
| 170 |
this means that the subexpression will be only evaluated once at the beginning |
| 171 |
of the time step. This can be useful to e.g. approximate a non-linear term as |
| 172 |
constant over a time step in order to use the ``linear`` numerical integration |
| 173 |
algorithm. It is also mandatory for subexpressions that refer to stateful |
| 174 |
functions like ``rand()`` to make sure that they are only evaluated once |
| 175 |
(otherwise e.g. recording the value with a `StateMonitor` would re-evaluate it |
| 176 |
and therefore not record the same values that are used in other places). This |
| 177 |
can only qualify subexpressions. |
| 178 |
*shared* |
| 179 |
this means that a parameter or subexpression is not neuron-/synapse-specific |
| 180 |
but rather a single value for the whole `NeuronGroup` or `Synapses`. A shared |
| 181 |
subexpression can only refer to other shared variables. |
| 182 |
*linked* |
| 183 |
this means that a parameter refers to a variable in another `NeuronGroup` |
| 184 |
or `SpatialNeuron`. See :ref:`linked_variables` for more details. |
| 185 |
|
| 186 |
Multiple flags may be specified as follows:: |
| 187 |
|
| 188 |
dx/dt = f : unit (flag1,flag2) |
| 189 |
|
| 190 |
.. _special_symbols: |
| 191 |
|
| 192 |
List of special symbols |
| 193 |
----------------------- |
| 194 |
|
| 195 |
The following lists all of the special symbols that Brian uses in |
| 196 |
equations and code blocks, and their meanings. |
| 197 |
|
| 198 |
dt |
| 199 |
Time step width |
| 200 |
i |
| 201 |
Index of a neuron (`NeuronGroup`) or the pre-synaptic neuron |
| 202 |
of a synapse (`Synapses`) |
| 203 |
j |
| 204 |
Index of a post-synaptic neuron of a synapse |
| 205 |
lastspike |
| 206 |
Last time that the neuron spiked (for refractoriness) |
| 207 |
lastupdate |
| 208 |
Time of the last update of synaptic variables in event-driven |
| 209 |
equations (only defined when event-driven equations are used). |
| 210 |
N |
| 211 |
Number of neurons (`NeuronGroup`) or synapses (`Synapses`). Use |
| 212 |
``N_pre`` or ``N_post`` for the number of presynaptic or |
| 213 |
postsynaptic neurons in the context of `Synapses`. |
| 214 |
not_refractory |
| 215 |
Boolean variable that is normally true, and false if the neuron |
| 216 |
is currently in a refractory state |
| 217 |
t |
| 218 |
Current time |
| 219 |
t_in_timesteps |
| 220 |
Current time measured in time steps |
| 221 |
xi, xi_* |
| 222 |
Stochastic differential in equations |
| 223 |
|
| 224 |
|
| 225 |
Event-driven equations |
| 226 |
---------------------- |
| 227 |
Equations defined as event-driven are completely ignored in the state update. |
| 228 |
They are only defined as variables that can be externally accessed. |
| 229 |
There are additional constraints: |
| 230 |
|
| 231 |
* An event-driven variable cannot be used by any other equation that is not |
| 232 |
also event-driven. |
| 233 |
* An event-driven equation cannot depend on a differential equation that is not |
| 234 |
event-driven (directly, or indirectly through subexpressions). It can depend |
| 235 |
on a constant parameter. |
| 236 |
|
| 237 |
Currently, automatic event-driven updates are only possible for one-dimensional |
| 238 |
linear equations, but this may be extended in the future. |
| 239 |
|
| 240 |
Equation objects |
| 241 |
---------------- |
| 242 |
The model definitions for `NeuronGroup` and `Synapses` can be simple strings or |
| 243 |
`Equations` objects. Such objects can be combined using the add operator:: |
| 244 |
|
| 245 |
eqs = Equations('dx/dt = (y-x)/tau : volt') |
| 246 |
eqs += Equations('dy/dt = -y/tau: volt') |
| 247 |
|
| 248 |
`Equations` allow for the specification of values in the strings, but does this by simple |
| 249 |
string replacement, e.g. you can do:: |
| 250 |
|
| 251 |
eqs = Equations('dx/dt = x/tau : volt', tau=10*ms) |
| 252 |
|
| 253 |
but this is exactly equivalent to:: |
| 254 |
|
| 255 |
eqs = Equations('dx/dt = x/(10*ms) : volt') |
| 256 |
|
| 257 |
The `Equations` object does some basic syntax checking and will raise an error if two equations defining |
| 258 |
the same variable are combined. It does not however do unit checking, checking for unknown identifiers or |
| 259 |
incorrect flags -- all this will be done during the instantiation of a `NeuronGroup` or `Synapses` object. |
| 260 |
|
| 261 |
Examples of `Equation` objects |
| 262 |
------------------------------ |
| 263 |
|
| 264 |
**Concatenating equations** |
| 265 |
|
| 266 |
.. doctest:: |
| 267 |
|
| 268 |
>>> membrane_eqs = Equations('dv/dt = -(v + I)/ tau : volt') |
| 269 |
>>> eqs1 = membrane_eqs + Equations('''I = sin(2*pi*freq*t) : volt |
| 270 |
... freq : Hz''') |
| 271 |
>>> eqs2 = membrane_eqs + Equations('''I : volt''') |
| 272 |
>>> print(eqs1) |
| 273 |
I = sin(2*pi*freq*t) : V |
| 274 |
dv/dt = -(v + I)/ tau : V |
| 275 |
freq : Hz |
| 276 |
>>> print(eqs2) |
| 277 |
dv/dt = -(v + I)/ tau : V |
| 278 |
I : V |
| 279 |
|
| 280 |
**Substituting variable names** |
| 281 |
|
| 282 |
.. doctest:: |
| 283 |
|
| 284 |
>>> general_equation = 'dg/dt = -g / tau : siemens' |
| 285 |
>>> eqs_exc = Equations(general_equation, g='g_e', tau='tau_e') |
| 286 |
>>> eqs_inh = Equations(general_equation, g='g_i', tau='tau_i') |
| 287 |
>>> print(eqs_exc) |
| 288 |
dg_e/dt = -g_e / tau_e : S |
| 289 |
>>> print(eqs_inh) |
| 290 |
dg_i/dt = -g_i / tau_i : S |
| 291 |
|
| 292 |
**Inserting values** |
| 293 |
|
| 294 |
.. doctest:: |
| 295 |
|
| 296 |
>>> eqs = Equations('dv/dt = mu/tau + sigma/tau**.5*xi : volt', |
| 297 |
... mu=-65*mV, sigma=3*mV, tau=10*ms) |
| 298 |
>>> print(eqs) |
| 299 |
dv/dt = (-65. * mvolt)/(10. * msecond) + (3. * mvolt)/(10. * msecond)**.5*xi : V |