Setting stims and time after SaveState.restore()
Posted: Wed Aug 13, 2014 2:16 pm
After a SaveState.restore() , modifications are difficult and seem to depend on doing things at the correct time in the finitialize() sequence. I don't yet fully understand the reasons at each step but will post further as I better understand it.
Here is the fairly simple sim I was working with:
mss = h.SaveState(); myfile = h.File()
lsy, lnc, lns, lfih = [], [], [], [] # list of synapse, netcon, netstim, finithandlers
dend = h.Section(); dend.insert('pas'); dend.insert('hh')
vvec= h.Vector(); vvec.record(dend(0.1)._ref_v) # record voltage
tvec=h.Vector(); tvec.record(h._ref_t) # record time
syn = h.Exp2Syn(0.5,sec=dend)
syn.tau1 = 0.05;syn.tau2 = 5.3;syn.e = 0 # AMPA
ns = h.NetStim();ns.number = 1;ns.start = 750;
nc = h.NetCon(ns,syn); nc.weight[0] = 0.1; nc.active(1)
lsy.append(syn); lns.append(ns); lnc.append(nc)
Note that when saving/restoring the model structure must be identical -- hence the netstim should be in place prior to the save. The save was done as a cvode.event() thru an finitializehandler as follows.
def sssave (): print "Saving at t=%g"%(h.t); mss.save()
def setsave (): h.cvode.event(509,sssave)
fihl=h.FInitializeHandler(0,setsave)
after writing to a file and restoring the following handlers were set up to manipulate the time and netstim before running a new simulation using fihset() (see at bottom)
fihset((3,begsets),(1,ssrestore),(2,finalsets))
def begsets (): # fih3 at very beginning
lns[0].start=600 # hidden -- will no longer see this when print out at fih3
def ssrestore (): # fih1
mss.restore(1) # restore takes place here
lns[0].number=3; lns[0].interval=50
def finalsets (): # fih2 at very end
h.cvode.re_init() # needed to get recording to continue from old t
It seems odd that the NetStim had to be modified at 2 different locations.
The use of re_init() to get t correct also was mysterious and was something I attempted to examine further.
Without this, when recording under cvode (tvec.record(h._ref_t) # record time), the time in tvec[0] was 0
In order to fix this it appears necessary to do cvode.re_init() at the very end of finitialize()
This was effected by using flag 2 to an FInitializeHandler() -- this takes place then just before the return
doing this at flag 1 or using frecord_init() does not seem to work
It is not clear to me why this would be the case since it appears that re_init is already being called after handler1 in the finitialize() sequence. The 't' is preserved at the end of finitialize() regardless of whether this additional call is made.
nrncvode_set_t(t);
,,,,
nrn_fihexec(1); /* after INITIAL blocks, before fcurrent*/
,,,,
cvode_finitialize(); # calls re_init (/usr/site/nrniv/nrn/src/nrncvode/cvodestb.cpp:117:577)
nrn_record_init();
,,,,
nrn_fihexec(2); /* just before return */
def fihset (*args):
'set using tuple of number and routine'
global fihl
try: fihl
except: fihl=[0,0,0,0]
for i,rtn in args:
fihl=h.FInitializeHandler(i,rtn)
Here is the fairly simple sim I was working with:
mss = h.SaveState(); myfile = h.File()
lsy, lnc, lns, lfih = [], [], [], [] # list of synapse, netcon, netstim, finithandlers
dend = h.Section(); dend.insert('pas'); dend.insert('hh')
vvec= h.Vector(); vvec.record(dend(0.1)._ref_v) # record voltage
tvec=h.Vector(); tvec.record(h._ref_t) # record time
syn = h.Exp2Syn(0.5,sec=dend)
syn.tau1 = 0.05;syn.tau2 = 5.3;syn.e = 0 # AMPA
ns = h.NetStim();ns.number = 1;ns.start = 750;
nc = h.NetCon(ns,syn); nc.weight[0] = 0.1; nc.active(1)
lsy.append(syn); lns.append(ns); lnc.append(nc)
Note that when saving/restoring the model structure must be identical -- hence the netstim should be in place prior to the save. The save was done as a cvode.event() thru an finitializehandler as follows.
def sssave (): print "Saving at t=%g"%(h.t); mss.save()
def setsave (): h.cvode.event(509,sssave)
fihl=h.FInitializeHandler(0,setsave)
after writing to a file and restoring the following handlers were set up to manipulate the time and netstim before running a new simulation using fihset() (see at bottom)
fihset((3,begsets),(1,ssrestore),(2,finalsets))
def begsets (): # fih3 at very beginning
lns[0].start=600 # hidden -- will no longer see this when print out at fih3
def ssrestore (): # fih1
mss.restore(1) # restore takes place here
lns[0].number=3; lns[0].interval=50
def finalsets (): # fih2 at very end
h.cvode.re_init() # needed to get recording to continue from old t
It seems odd that the NetStim had to be modified at 2 different locations.
The use of re_init() to get t correct also was mysterious and was something I attempted to examine further.
Without this, when recording under cvode (tvec.record(h._ref_t) # record time), the time in tvec[0] was 0
In order to fix this it appears necessary to do cvode.re_init() at the very end of finitialize()
This was effected by using flag 2 to an FInitializeHandler() -- this takes place then just before the return
doing this at flag 1 or using frecord_init() does not seem to work
It is not clear to me why this would be the case since it appears that re_init is already being called after handler1 in the finitialize() sequence. The 't' is preserved at the end of finitialize() regardless of whether this additional call is made.
nrncvode_set_t(t);
,,,,
nrn_fihexec(1); /* after INITIAL blocks, before fcurrent*/
,,,,
cvode_finitialize(); # calls re_init (/usr/site/nrniv/nrn/src/nrncvode/cvodestb.cpp:117:577)
nrn_record_init();
,,,,
nrn_fihexec(2); /* just before return */
def fihset (*args):
'set using tuple of number and routine'
global fihl
try: fihl
except: fihl=[0,0,0,0]
for i,rtn in args:
fihl=h.FInitializeHandler(i,rtn)