Subscript out of range and Segment ation violation error?

Anything that doesn't fit elsewhere.
Post Reply
ahajj
Posts: 8
Joined: Fri Jun 16, 2006 10:40 am
Location: Ottawa

Subscript out of range and Segment ation violation error?

Post by ahajj »

Hi, I found this fallowing code in the forum and I am having problems. Here's a part of my code

Code: Select all

// Insert stimulation

objref stim[ncells]
for i=0, ncells-1 sIN[i].soma {
	stim[i] = 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()")

STIMON = 0

proc initi() {
  STIMON = 0
  stim[i].del = 0    // we want to exert control over amp starting at 0 ms
  stim[i].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[i].amp = AMP
    cvode.event(t + DUR, "seti()")
    cvode.event(t + INTERVAL, "seti()")
  } else {
    STIMON = 0
    stim[i].amp = 0
  }
  // we've changed a parameter abruptly
  // so we really should re-initialize cvode
  if (cvode.active()) {
    cvode.re_init()
  } else {
    fcurrent()
  }
}



strdef gtxt


for i=0,ncells-1 {
	sprint(gtxt,"IN[%d].soma.v(0.5)",i)
	addgraph(gtxt,-120,40)
}
access IN[0].soma   //Set In as default cell

// Initiale stimulation
init()
run()
I get this message when I run my program:

/usr/bin/nrniv: subscript out of range stim in FS-1elect.hoc near line 158
{initi()}
^
initi()
finitialize(-70)
init()
/usr/bin/nrniv: Segmentation violation See $NEURONHOME/lib/help/oc.help in FS-1e lect.hoc near line 158
Line 158 is the final line on this part of the code

Thanks
Here's the link to the code I have found on this forum https://www.neuron.yale.edu/phpBB2/view ... light=ramp
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Hanze University of Applied Sciences
Contact:

Post by Raj »

Change

Code: Select all

proc initi() { 
to

Code: Select all

proc initi() { local i
i=$1

Change

Code: Select all

proc seti()  { 
to

Code: Select all

proc seti() { local i
i=$1
and some wizardry:
replace

Code: Select all

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

Code: Select all

objref fih[ncells]
strdef myInitializeStatement
for i=0, ncells-1 {
    sprint(myInitializeStatement,"fih[%d] = new FInitializeHandler(\"initi(%d)\")",i,i)
    execute(myInitializeStatement)
}
 
and you have a chance of a working application. Unfortunately I had no time to test it.

I almost forgot:
replace

Code: Select all

cvode.event(START, "seti()") 
with

Code: Select all

strdef myCVodeEventString
sprint( myCVodeEventString,"cvode.event(START, \"seti(%d)\")",i))
execute(myCVodeEventString)
where the strdef part should be before the function and the rest at the position of the original line.
Last edited by Raj on Thu Jun 22, 2006 1:25 pm, edited 1 time in total.
ahajj
Posts: 8
Joined: Fri Jun 16, 2006 10:40 am
Location: Ottawa

Post by ahajj »

Raj, dank u voor het snelle antwoord (in dutch)

But I get an parse error on this line

Code: Select all

sprint(myCVodeEventString.("cvode.event(START,\"seti(%d)\")",i))
Thank you in advance
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Hanze University of Applied Sciences
Contact:

Post by Raj »

Those pesky brackets again, I think the second bracket should go. After that you should make sure the other brackets are correct.

By the way you will have to use the same construction with variations for all the cvode.event statements.
ahajj
Posts: 8
Joined: Fri Jun 16, 2006 10:40 am
Location: Ottawa

Post by ahajj »

Sorry to be annoying Raj, When you said that I must use the same construction with variations for all the cvode.event statements, you mean that i should change all the cvode.event too the fallowing construction
strdef .....
sprint....
execute.....
Where can i find a tutorial about this contruction (sorry, I am new to Neuron)

Thx again
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Hanze University of Applied Sciences
Contact:

Post by Raj »

In the end one never gets away with giving code without explanations.

The problem with your original code was that you were making reference to a variable i, in constructions like stim. However this variable was out of range, because you had executed a for loop over the range, and when the for loop is finished the variable used in the loop is out of range. Hence your error message.


Normally you fix that by starting a new for loop which iterates over the range again, but the functions you are using do not allow that. In neuron many functions take strings as arguments, strings that are actually commands, functions, pieces of code. If you use a variable in such a command you don't know what it will be at execution time. To fix this we use the construction (an example from above):

Code: Select all

sprint(myInitializeStatement,"fih[%d] = new FInitializeHandler(\"initi(%d)\")",i,i)
execute(myInitializeStatement)
 
After executing this code for i=1 and i=2 fih[1] contains a reference to the string: initi(1) and fih[2] has a reference to the string: initi(2). When they now each use their respective strings to initialize the next simulation run they will both call initi with the right argument 1 and 2, respectively.

With this knowledge in mind you should be able to work out how to apply this construction to the other cases. Have a good look at the quick reference to see how the sprint and execute statements work. Start simple
replace the code in initi with for example

Code: Select all

 print "This is initi i = ", i 
and then work your way towards your endgoal.
ted
Site Admin
Posts: 5793
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Raj is correct about the cause of the problem and the clue to a workaround.
But you know how many stim you need to configure, what params you
want them to have, and the times at which you want to stuff events, so
why not do all this at one time? Change proc initi() so that it iterates over
all of the stim, one at a time, setting their params and setting up the events.
No need to construct and execute command strings, no susceptibility to
leftover values of iterator indices, and the code is much simpler and easier
to write and maintain.
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Hanze University of Applied Sciences
Contact:

Post by Raj »

Ted is right.

This time I was focusing on making broken code work. I did not take an extra step back to consider the intention of your code.

You will need the string construction here only if you start differentiating between the different IClamp's. As long as they are switched on and off synchronously you should be able to explicitly add the for-loops to the functions.
ted
Site Admin
Posts: 5793
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Raj wrote:As long as they are switched on and off synchronously
That isn't to say that their current pulses must turn on and off at the same
time. Offhand I can't imagine a situation in which it would be useful to _not_
set up their parameters and events in a single procedure.
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Hanze University of Applied Sciences
Contact:

Post by Raj »

Let me clarify my point of view, in this admittedly increasingly hypothetical case.

Suppose you want to use pulsed stimuli with different intervals and starting times. The first thing to notice is that STIMON should become an array, to keep track of the different states. The second thing is that also INTERVAL, and START should be arrays and maybe even DUR, AMP to allow for different stimulus strengths.

Now when an event is created which calls seti() we will need to know in this function which stimulus is actually switched. This is most easily achieved by passing the index of the stimulus to seti(), and for that we need the string construction during our call to CVode.event to avoid the use of a variable i with unknown value at execution time.
Post Reply