Tutorial: Making NEURON Vector.play() + SaveState.restore() + CVODE work

Here is a working example of running a NEURON simulation of a cell to steady state, saving that state, restoring it in a new simulation, and using the Vector.play method to stimulate the cell with an arbitrary waveform. All this while using the variable integration method CVODE.

First, build the cell, and run it to steady state, and save the result:

neuron import h, gui

soma = h.Section()
soma.insert('pas')
soma.insert('hh')

ic = h.IClamp(0.5, sec=soma)
ic.delay = 0
ic.dur = 1e9
ic.amp = 0

h.tstop = 500
h.cvode_active(1)
h.run()

ss = h.SaveState()
ss.save()

sf = h.File('state.bin')
ss.fwrite(sf)

print(h.t)
print(soma.v)

Then, in a new python session, run the following to restore the saved state and resume simulation from that point:

from neuron import h, gui

soma = h.Section()
soma.insert('pas')
soma.insert('hh')

ic = h.IClamp(0.5, sec=soma)
ic.delay = 0
ic.dur = 1e9
ic.amp = 0

rv = h.Vector([0, 0, 10, 0])
tv = h.Vector([0, 500, 600, 600])
rv.play(ic._ref_amp, tv, 1)

h.stdinit()

ns = h.SaveState()
sf = h.File('state.bin')
ns.fread(sf)
ns.restore(1)

h.cvode_active(1)

h.nrncontrolmenu()
h.newPlotV()

h.continuerun(700)

You should see something like this in the GUI:

neuron-ramp

Specifically:

  • The simulation should not have run 0-500 ms
  • It should have resumed from the steady state saved in the first session
  • You should see the ramping voltage starting at 500ms

Notes

  • Make sure the current clamp is present in the initial and the new session
  • After vector.play(), make sure to run h.stdinit(). This creates the events in the queue
  • Restore the session after h.stdinit(), and use the “1” argument to preserve the queued vector.play() events
  • After restoring, make sure to use the h.cvode_active(1) (not h.cvode.active) to properly initialize the CVODE integrator