| 1 |
""" |
| 2 |
Example using GSL ODE solvers with a variable time step and comparing it to the |
| 3 |
Brian solver. |
| 4 |
|
| 5 |
For highly accurate simulations, i.e. simulations with a very low desired error, |
| 6 |
the GSL simulation with a variable time step can be faster because it uses a |
| 7 |
low time step only when it is necessary. In biologically detailed models (e.g. |
| 8 |
of the Hodgkin-Huxley type), the relevant time constants are very short around |
| 9 |
an action potential, but much longer when the neuron is near its resting |
| 10 |
potential. The following example uses a very simple neuron model (leaky |
| 11 |
integrate-and-fire), but simulates a change in relevant time constants by |
| 12 |
changing the actual time constant every 10ms, independently for each of 100 |
| 13 |
neurons. To accurately simulate this model with a fixed time step, the time step |
| 14 |
has to be very small, wasting many unnecessary steps for all the neurons where |
| 15 |
the time constant is long. |
| 16 |
|
| 17 |
Note that using the GSL ODE solver is much slower, if both methods use a |
| 18 |
comparable number of steps, i.e. if the desired accuracy is low enough so that |
| 19 |
a single step per "Brian time step" is enough. |
| 20 |
""" |
| 21 |
from brian2 import * |
| 22 |
import time |
| 23 |
|
| 24 |
# Run settings |
| 25 |
start_dt = .1 * ms |
| 26 |
method = 'rk2' |
| 27 |
error = 1.e-6 # requested accuracy |
| 28 |
|
| 29 |
|
| 30 |
def runner(method, dt, options=None): |
| 31 |
seed(0) |
| 32 |
I = 5 |
| 33 |
group = NeuronGroup(100, '''dv/dt = (-v + I)/tau : 1 |
| 34 |
tau : second''', |
| 35 |
method=method, |
| 36 |
method_options=options, |
| 37 |
dt=dt) |
| 38 |
group.run_regularly('''v = rand() |
| 39 |
tau = 0.1*ms + rand()*9.9*ms''', dt=10*ms) |
| 40 |
|
| 41 |
rec_vars = ['v', 'tau'] |
| 42 |
if 'gsl' in method: |
| 43 |
rec_vars += ['_step_count'] |
| 44 |
net = Network(group) |
| 45 |
net.run(0 * ms) |
| 46 |
mon = StateMonitor(group, rec_vars, record=True, dt=start_dt) |
| 47 |
net.add(mon) |
| 48 |
start = time.time() |
| 49 |
net.run(1 * second) |
| 50 |
mon.add_attribute('run_time') |
| 51 |
mon.run_time = time.time() - start |
| 52 |
return mon |
| 53 |
|
| 54 |
|
| 55 |
lin = runner('linear', start_dt) |
| 56 |
method_options = {'save_step_count': True, |
| 57 |
'absolute_error': error, |
| 58 |
'max_steps': 10000} |
| 59 |
gsl = runner('gsl_%s' % method, start_dt, options=method_options) |
| 60 |
|
| 61 |
print("Running with GSL integrator and variable time step:") |
| 62 |
print('Run time: %.3fs' % gsl.run_time) |
| 63 |
|
| 64 |
# check gsl error |
| 65 |
assert np.max(np.abs( |
| 66 |
lin.v - gsl.v)) < error, "Maximum error gsl integration too large: %f" % np.max( |
| 67 |
np.abs(lin.v - gsl.v)) |
| 68 |
print("average step count: %.1f" % np.mean(gsl._step_count)) |
| 69 |
print("average absolute error: %g" % np.mean(np.abs(gsl.v - lin.v))) |
| 70 |
|
| 71 |
print("\nRunning with exact integration and fixed time step:") |
| 72 |
dt = start_dt |
| 73 |
count = 0 |
| 74 |
dts = [] |
| 75 |
avg_errors = [] |
| 76 |
max_errors = [] |
| 77 |
runtimes = [] |
| 78 |
while True: |
| 79 |
print('Using dt: %s' % str(dt)) |
| 80 |
brian = runner(method, dt) |
| 81 |
print('\tRun time: %.3fs' % brian.run_time) |
| 82 |
avg_errors.append(np.mean(np.abs(brian.v - lin.v))) |
| 83 |
max_errors.append(np.max(np.abs(brian.v - lin.v))) |
| 84 |
dts.append(dt) |
| 85 |
runtimes.append(brian.run_time) |
| 86 |
if np.max(np.abs(brian.v - lin.v)) > error: |
| 87 |
print('\tError too high (%g), decreasing dt' % np.max( |
| 88 |
np.abs(brian.v - lin.v))) |
| 89 |
dt *= .5 |
| 90 |
count += 1 |
| 91 |
else: |
| 92 |
break |
| 93 |
print("Desired error level achieved:") |
| 94 |
print("average step count: %.2fs" % (start_dt / dt)) |
| 95 |
print("average absolute error: %g" % np.mean(np.abs(brian.v - lin.v))) |
| 96 |
|
| 97 |
print('Run time: %.3fs' % brian.run_time) |
| 98 |
if brian.run_time > gsl.run_time: |
| 99 |
print("This is %.1f times slower than the simulation with GSL's variable " |
| 100 |
"time step method." % (brian.run_time / gsl.run_time)) |
| 101 |
else: |
| 102 |
print("This is %.1f times faster than the simulation with GSL's variable " |
| 103 |
"time step method." % (gsl.run_time / brian.run_time)) |
| 104 |
|
| 105 |
fig, (ax1, ax2) = plt.subplots(1, 2) |
| 106 |
ax2.axvline(1e-6, color='gray') |
| 107 |
for label, gsl_error, std_errors, ax in [('average absolute error', np.mean(np.abs(gsl.v - lin.v)), avg_errors, ax1), |
| 108 |
('maximum absolute error', np.max(np.abs(gsl.v - lin.v)), max_errors, ax2)]: |
| 109 |
ax.set(xscale='log', yscale='log') |
| 110 |
ax.plot([], [], 'o', color='C0', label='fixed time step') # for the legend entry |
| 111 |
for (error, runtime, dt) in zip(std_errors, runtimes, dts): |
| 112 |
ax.plot(error, runtime, 'o', color='C0') |
| 113 |
ax.annotate('%s' % str(dt), xy=(error, runtime), xytext=(2.5, 5), |
| 114 |
textcoords='offset points', color='C0') |
| 115 |
ax.plot(gsl_error, gsl.run_time, 'o', color='C1', label='variable time step (GSL)') |
| 116 |
ax.set(xlabel=label, xlim=(10**-10, 10**1)) |
| 117 |
ax1.set_ylabel('runtime (s)') |
| 118 |
ax2.legend(loc='lower left') |
| 119 |
|
| 120 |
plt.show() |