Debian Codegraph @

docs_sphinx/advanced/state_update.rst

raw
1 State update
2 ============
3
4 In Brian, a state updater transforms a set of equations into an abstract
5 state update code (and therefore is automatically target-independent). In
6 general, any function (or callable object) that takes an `Equations` object
7 and returns abstract code (as a string) can be used as a state updater and
8 passed to the `NeuronGroup` constructor as a ``method`` argument.
9
10 The more common use case is to specify no state updater at all or chose one by
11 name, see `Choice of state updaters`_ below.
12
13
14 Explicit state update
15 ---------------------
16 Explicit state update schemes can be specified in mathematical notation, using
17 the `ExplicitStateUpdater` class. A state updater scheme contains a series
18 of statements, defining temporary variables and a final line (starting with
19 ``x_new =``), giving the updated value for the state variable. The description
20 can make reference to ``t`` (the current time), ``dt`` (the size of the time
21 step), ``x`` (value of the state variable), and ``f(x, t)`` (the definition of
22 the state variable ``x``, assuming ``dx/dt = f(x, t)``. In addition, state
23 updaters supporting stochastic equations additionally make use of ``dW`` (a
24 normal distributed random variable with variance ``dt``) and ``g(x, t)``, the
25 factor multiplied with the noise variable, assuming
26 ``dx/dt = f(x, t) + g(x, t) * xi``.
27
28 Using this notation, simple forward Euler integration is specified as::
29
30 x_new = x + dt * f(x, t)
31
32 A Runge-Kutta 2 (midpoint) method is specified as::
33
34 k = dt * f(x,t)
35 x_new = x + dt * f(x + k/2, t + dt/2)
36
37 When creating a new state updater using `ExplicitStateUpdater`, you can
38 specify the ``stochastic`` keyword argument, determining whether this state
39 updater does not support any stochastic equations (``None``, the default),
40 stochastic equations with additive noise only (``'additive'``), or
41 arbitrary stochastic equations (``'multiplicative'``). The provided state
42 updaters use the Stratonovich interpretation for stochastic equations (which
43 is the correct interpretation if the white noise source is seen as the limit
44 of a coloured noise source with a short time constant). As a result of this,
45 the simple Euler-Maruyama scheme (``x_new = x + dt*f(x, t) + dW*g(x, t)``) will
46 only be used for additive noise.
47
48 An example for a general state updater that handles arbitrary multiplicative
49 noise (under Stratonovich interpretation) is the derivative-free Milstein
50 method::
51
52 x_support = x + dt*f(x, t) + dt**.5 * g(x, t)
53 g_support = g(x_support, t)
54 k = 1/(2*dt**.5)*(g_support - g(x, t))*(dW**2)
55 x_new = x + dt*f(x,t) + g(x, t) * dW + k
56
57 Note that a single line in these descriptions is only allowed to mention
58 ``g(x, t)``, respectively ``f(x, t)`` only once (and you are not allowed to
59 write, for example, ``g(f(x, t), t)``). You can work around these restrictions
60 by using intermediate steps, defining temporary variables, as in the above
61 examples for `milstein` and `rk2`.
62
63
64 Choice of state updaters
65 ------------------------
66 As mentioned in the beginning, you can pass arbitrary callables to the
67 method argument of a `NeuronGroup`, as long as this callable converts an
68 `Equations` object into abstract code. The best way to add a new state updater,
69 however, is to register it with brian and provide a method to determine whether
70 it is appropriate for a given set of equations. This way, it can be
71 automatically chosen when no method is specified and it can be referred to with
72 a name (i.e. you can pass a string like ``'euler'`` to the method argument
73 instead of importing `euler` and passing a reference to the object itself).
74
75 If you create a new state updater using the `ExplicitStateUpdater` class, you
76 have to specify what kind of stochastic equations it supports. The keyword
77 argument ``stochastic`` takes the values ``None`` (no stochastic equation
78 support, the default), ``'additive'`` (support for stochastic equations with
79 additive noise), ``'multiplicative'`` (support for arbitrary stochastic
80 equations).
81
82 After creating the state updater, it has to be registered with
83 `StateUpdateMethod`::
84
85 new_state_updater = ExplicitStateUpdater('...', stochastic='additive')
86 StateUpdateMethod.register('mymethod', new_state_updater)
87
88 The preferred way to do write new general state updaters (i.e. state updaters
89 that cannot be described using the explicit syntax described above) is to
90 extend the `StateUpdateMethod` class (but this is not strictly necessary, all
91 that is needed is an object that implements a ``__call__`` method that
92 operates on an `Equations` object and a dictionary of variables). Optionally,
93 the state updater can be registered with `StateUpdateMethod` as shown above.
94
95 Implicit state updates
96 ----------------------
97
98 .. note::
99
100 All of the following is just here for future reference, it's not
101 implemented yet.
102
103
104 Implicit schemes often use Newton-Raphson or fixed point iterations.
105 These can also be defined by mathematical statements, but the number of iterations
106 is dynamic and therefore not easily vectorised. However, this might not be
107 a big issue in C, GPU or even with Numba.
108
109 Backward Euler
110 ^^^^^^^^^^^^^^
111 Backward Euler is defined as follows::
112
113 x(t+dt)=x(t)+dt*f(x(t+dt),t+dt)
114
115 This is not a executable statement because the RHS depends on the future.
116 A simple way is to perform fixed point iterations::
117
118 x(t+dt)=x(t)
119 x(t+dt)=x(t)+dt*dx=f(x(t+dt),t+dt) until increment<tolerance
120
121 This includes a loop with a different number of iterations depending on the
122 neuron.