Page 1 of 1

How to change parameters during a simulation

Posted: Sun Sep 04, 2005 1:09 pm
by ted
How to change a model's parameters in the course of a simulation is a common problem.

One way is to use the Vector class's play method. This example uses Vector play with interpolation to make celsius change "continuously" from 6.3 to 20 deg C over the course of 100 ms. Yes, that's unrealistically fast, but this is just a toy example.

Code: Select all

objref tempvec, tvec
tempvec = new Vector(3)
tvec = new Vector(3)
tempvec.x[0] = 6.3  // deg C
tvec.x[0] = 0
tempvec.x[1] = 20   // deg C
tvec.x[1] = 100
tempvec.x[2] = 20   // deg C
tvec.x[2] = 1e9

// third argument == 1 means play with interpolation, tvec, 1)
Be sure to read the Programmer's Reference documentation of Vector play.

The following example uses the event delivery system's FInitializeHandler and cvode.event to force abrupt parameter changes during a simulation: an IClamp is made to deliver a series of current pulses. In this example, the pulses repeat until the end of the simulation. It would be easy to add a counter that controls how many pulses are generated. A similar strategy can be used to drive one or more discontinuous changes in any parameter.

Code: Select all

1.  During simulation initialization, the stimulus current is set to 0,
and an FInitializeHandler is used to launch an event that will arrive
at the time when we want the first jump of stim.amp to occur.
2.  Arrival of this event causes proc seti() to be called.
3.  seti() assigns a new value to stim.amp, and uses the CVode class's
event() method to launch two new events.  The first of these will come
back in the future to turn off the stimulus.  The second will come back
a bit later, to turn it back on again, and start a new cycle.

objref stim
stim = new IClamp(0.5)

DUR = 0.1  // ms, duration of each pulse
AMP = 0.1  // nA
START = 5  // ms, time of first pulse
INTERVAL = 25  // ms, interval between pulse starts
// assumes DUR < INTERVAL

objref fih
fih = new FInitializeHandler("initi()")


proc initi() {
  STIMON = 0
  stim.del = 0    // we want to exert control over amp starting at 0 ms
  stim.dur = 1e9  // if we're going to change amp,
                  // dur must be long enough to span all our changes
  cvode.event(START, "seti()")

proc seti() {
//  print "time is ", t
  if (STIMON==0) {
    STIMON = 1
    stim.amp = AMP
    cvode.event(t + DUR, "seti()")
    cvode.event(t + INTERVAL, "seti()")
  } else {
    STIMON = 0
    stim.amp = 0
  // we've changed a parameter abruptly
  // so we really should re-initialize cvode
  if ( {
  } else {
As always, be sure to read the Programmer's Reference documentation of anything with which you are unfamiliar.

Re: How to change parameters during a simulation

Posted: Tue Mar 03, 2015 11:35 am
by ted
The above post was revised on 20150303