Nested loops in ngspice

ngspicesimulationsoftwarespice

I am having some problems regarding using loops in the control section of ngspice. Specifically in this example:

VD0 d 0 0 
VG0 g 0 0 
VS0 s 0 0 
JDS d g s JM
.MODEL JM NJF (VTO=-1 BETA=1)

.CONTROL
let start_d = 0
let delta_d = 0.5
let stop_d = 1.81
let start_g = -3
let delta_g = 0.5
let stop_g = 0.01
let d_act = start_d
let g_act = start_g
while d_act le stop_d
 alter vd0 dc d_act
 while g_act < stop_g
  alter vg0 dc g_act
  op
  print v(d) v(g) v(s)
  let g_act = g_act + delta_g
 end
 let d_act = d_act + delta_d
end
.ENDC
.END

It should go through all the combinations of vd and vg (0,-3) (0,-2.5)… (0 0) (0.5 -3)…

However it does not step vd at all. It keeps vd=0 and steps only vg.

Anybody can help?

Best Answer

Your loop is wrong - you are not resetting g_act to g_start in the inner loop. Try this: (I've commented out the alter and op statements, you can add them back in with your circuit)

 *Loop Test
 .CONTROL
 let start_d = 0
 let delta_d = 0.5
 let stop_d = 1.81
 let start_g = -3
 let delta_g = 0.5
 let stop_g = 0.01
 let d_act = start_d
 let g_act = start_g

 while d_act le stop_d
      *alter vd0 dc d_act
     let g_act = start_g
      while g_act < stop_g
         *alter vg0 dc g_act
         *op
         echo run sim with d_act:"$&d_act" and g_act:"$&g_act"
         let g_act = g_act + delta_g
         end
 let d_act = d_act + delta_d
 end
 .ENDC
 .END