How to cut the simulation in-between into two runs?

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
gaoyy18
Posts: 11
Joined: Tue Mar 14, 2023 11:33 am

How to cut the simulation in-between into two runs?

Post by gaoyy18 »

I have a neuron model that needs to simulate 1000ms, is there any method in python to cut the simulation in-between into two runs?
For example, first simulate for 300ms, then I want to change some parameters of the neuron model, and continue the simulation for the rest 700ms. And the end of the first simulation state is the beginning state of the second simulation state.

I tried:

Code: Select all

# 1st simulation
h.finitialize(-65 * mV)
h.continuerun(300 * ms)
# some change
...
#  2nd simulation
h.continuerun(700 * ms)
But it did not work. How can I do that in python?
Many thanks for advance!
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to cut the simulation in-between into two runs?

Post by ted »

See
Using events to implement parameter changes during a run
in the Hot tips area of the Forum.

Yes, that example is written in hoc, but it's very simple, and by referring to NEURON's Python documentation at nrn.readthedocs.io I'm sure you'll be able to figure out what to do.
gaoyy18
Posts: 11
Joined: Tue Mar 14, 2023 11:33 am

Re: How to cut the simulation in-between into two runs?

Post by gaoyy18 »

ted wrote: Mon May 22, 2023 2:59 pm See
Using events to implement parameter changes during a run
in the Hot tips area of the Forum.

Yes, that example is written in hoc, but it's very simple, and by referring to NEURON's Python documentation at nrn.readthedocs.io I'm sure you'll be able to figure out what to do.
Thank you for your guidance! But it seems that I need to know and design the changes all before the simulation in the example you referred. Can I just run first simulation, pulse it and observe the intermediate results, choose changes according to the results, then run second simulation?
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to cut the simulation in-between into two runs?

Post by ted »

Easy. Launch the first simulation. After that stops, make whatever parameter changes you like. Then execute

Code: Select all

if (cvode.active()) {
  cvode.re_init()
} else {
  fcurrent()
}
or, if you prefer to use Python, execute the equivalent Python statements, which should be something like

Code: Select all

if (h.cvode.active()==TRUE):
  h.cvode.re_init()
else:
  h.fcurrent()

h.continuerun(newtstop)
where "newtstop" is the next time at which you want the simulation to stop (should be > the tstop you used for the first run).
gaoyy18
Posts: 11
Joined: Tue Mar 14, 2023 11:33 am

Re: How to cut the simulation in-between into two runs?

Post by gaoyy18 »

ted wrote: Tue May 23, 2023 10:53 am Easy. Launch the first simulation. After that stops, make whatever parameter changes you like. Then execute

Code: Select all

if (cvode.active()) {
  cvode.re_init()
} else {
  fcurrent()
}
or, if you prefer to use Python, execute the equivalent Python statements, which should be something like

Code: Select all

if (h.cvode.active()==TRUE):
  h.cvode.re_init()
else:
  h.fcurrent()

h.continuerun(newtstop)
where "newtstop" is the next time at which you want the simulation to stop (should be > the tstop you used for the first run).
Many thanks! I tried it and it works. However, when I tried to record the second simulation, I failed. The code is below:

Code: Select all

# record for 1st simulation
soma_v = h.Vector().record(my_cell.soma(0.5)._ref_v)
t = h.Vector().record(h._ref_t)

# 1st simulation
h.finitialize(-65 * mV)
h.continuerun(300 * ms)

# stop and make some changes
if (h.cvode.active()==1):
  h.cvode.re_init()
else:
  h.fcurrent()
......

# record for 2nd simulation
soma_v_2 = h.Vector().record(my_cell.soma(0.5)._ref_v)
t_2 = h.Vector().record(h._ref_t)

# 2nd simulation
h.continuerun(1000 * ms)
The record for 1st simulation, i.e. variables"soma_v" and "t" return record for the first 300ms.
But the record for 2nd simulation, i.e. variables"soma_v_2" and "t_2" return nothing. May I ask how to record the 2nd simulation correctly?
Thanks for advance.
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to cut the simulation in-between into two runs?

Post by ted »

But the record for 2nd simulation, i.e. variables"soma_v_2" and "t_2" return nothing.
I didn't tell you to do this

Code: Select all

# record for 2nd simulation
soma_v_2 = h.Vector().record(my_cell.soma(0.5)._ref_v)
t_2 = h.Vector().record(h._ref_t)
did I?

Omit those two statements, and at the end of the second run your soma_v and t vectors will contain the values calculated at all times from t==0 to the end of the second run, and you can do whatever you like with those data after the end of the second run.

If you don't want to save the results generated before you make your parameter changes, then execute
h.frecord_init()
just before your
h.continuerun(1000)
statement, and the soma_v and t vectors will contain only the results generated during the second run.

If you want to know more about segmenting simulations and using h.frecord_init(), search the Forum for frecord_init, and in paricular read this post viewtopic.php?p=15895#p15895


If you use the same first simulation many times, you might be able to save some run time by using SaveState. Read about it in NEURON's Python documentation at nrn.readthedocs.io, and check the Forum for examples.
ramcdougal
Posts: 267
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: How to cut the simulation in-between into two runs?

Post by ramcdougal »

Just to add one point, the underlying issue here is that Vector.record has to be defined before the simulation is initialized... but yeah, no reason to create a new variable for the second half.
Post Reply