Browse Source
examples for loops.
examples for loops.
The syntax is listed in the ngspice manual, chapter 17.6 Control Structures. Practical examples using a simple voltage divider circuit are given here.pre-master-46
9 changed files with 270 additions and 0 deletions
-
48examples/loops/instance-loop.cir
-
48examples/loops/loop-instance.cir
-
52examples/loops/loop-model.cir
-
53examples/loops/loop-param.cir
-
19examples/loops/loop_IfThenElse.cir
-
12examples/loops/loop_dowhile.cir
-
15examples/loops/loop_foreach.cir
-
11examples/loops/loop_repeat.cir
-
12examples/loops/loop_while.cir
@ -0,0 +1,48 @@ |
|||
* dc loop with element (instance) parameter |
|||
* We alter resistor R2 |
|||
|
|||
* The circuit |
|||
R1 n1 0 1k |
|||
R2 n2 n1 1k |
|||
|
|||
V1 n2 0 1 |
|||
|
|||
|
|||
* start and end values for R2 |
|||
.csparam start = 1k |
|||
.csparam end = 0.1k |
|||
.csparam delta = 0.05k |
|||
|
|||
* control script |
|||
.control |
|||
* create a new plot for storing the measurements |
|||
set curplot = new ; create a new plot |
|||
set curplottitle = "OutputData" |
|||
set plot_out = $curplot ; store its name to 'plot_out' |
|||
if (end < start) |
|||
let delta = -abs(delta) |
|||
else |
|||
let delta = abs(delta) |
|||
end |
|||
let op_runs = floor((end - start)/delta) + 1 ; number of runs for simulation |
|||
let run = 0 ; number of actual run |
|||
let vnode2 = unitvec(op_runs) ; vector for all n1 voltages |
|||
let r2val = unitvec(op_runs) ; vector for all resistor values |
|||
let rcur = start ; set the start value for R2 |
|||
* the loop |
|||
dowhile run < op_runs |
|||
alter R2 $&rcur ; instance parameter resistance for R2 is changed to rcur |
|||
op ; simulate operating point, plot op1 is created |
|||
setplot $plot_out ; go to the output plot |
|||
let vnode2[run] = op1.v(n1) ; store the current n1 voltage value |
|||
let r2val[run] = rcur ; store the current R2 resistance value |
|||
destroy op1 |
|||
let rcur = rcur + delta |
|||
let run = run + 1 |
|||
end |
|||
settype impedance r2val |
|||
settype voltage vnode2 |
|||
plot vnode2 vs r2val |
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,48 @@ |
|||
* dc loop with element (instance) parameter |
|||
* We alter resistor R2 |
|||
|
|||
* The circuit |
|||
R1 n1 0 1k |
|||
R2 n2 n1 1k |
|||
|
|||
V1 n2 0 1 |
|||
|
|||
|
|||
* start and end values for R2 |
|||
.csparam start = 1k |
|||
.csparam end = 0.1k |
|||
.csparam delta = 0.05k |
|||
|
|||
* control script |
|||
.control |
|||
* create a new plot for storing the measurements |
|||
set curplot = new ; create a new plot |
|||
set curplottitle = "OutputData" |
|||
set plot_out = $curplot ; store its name to 'plot_out' |
|||
if (end < start) ; find appropriate sign for delta |
|||
let delta = -abs(delta) |
|||
else |
|||
let delta = abs(delta) |
|||
end |
|||
let op_runs = floor((end - start)/delta) + 1 ; number of runs for simulation |
|||
let run = 0 ; number of actual run |
|||
let vnode2 = unitvec(op_runs) ; vector for all n1 voltages |
|||
let r2val = unitvec(op_runs) ; vector for all resistor values |
|||
let rcur = start ; set the start value for R2 |
|||
* the loop |
|||
dowhile run < op_runs |
|||
alter R2 $&rcur ; instance parameter resistance for R2 is changed to rcur |
|||
op ; simulate operating point, plot op1 is created |
|||
setplot $plot_out ; go to the output plot |
|||
let vnode2[run] = op1.v(n1) ; store the current n1 voltage value |
|||
let r2val[run] = rcur ; store the current R2 resistance value |
|||
destroy op1 |
|||
let rcur = rcur + delta |
|||
let run = run + 1 |
|||
end |
|||
settype impedance r2val |
|||
settype voltage vnode2 |
|||
plot vnode2 vs r2val |
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,52 @@ |
|||
* dc loop with model parameter |
|||
* We alter resistor R2 |
|||
|
|||
* The circuit |
|||
R1 n1 0 1k |
|||
R2 n2 n1 rmod |
|||
.model rmod r (r=1k) |
|||
|
|||
V1 n2 0 1 |
|||
|
|||
|
|||
* start and end values for R2 |
|||
.csparam start = 1k |
|||
.csparam end = 0.1k |
|||
.csparam delta = 0.05k |
|||
|
|||
* control script |
|||
.control |
|||
* create a new plot for storing the measurements |
|||
set curplot = new ; create a new plot |
|||
set curplottitle = "OutputData" |
|||
set plot_out = $curplot ; store its name to 'plot_out' |
|||
if (end < start) ; find appropriate sign for delta |
|||
let delta = -abs(delta) |
|||
else |
|||
let delta = abs(delta) |
|||
end |
|||
let op_runs = floor((end - start)/delta) + 1 ; number of runs for simulation |
|||
let run = 0 ; number of actual run |
|||
let vnode2 = unitvec(op_runs) ; vector for all n1 voltages |
|||
let r2val = unitvec(op_runs) ; vector for all resistor values |
|||
let rcur = start ; set the start value for R2 |
|||
* the loop |
|||
dowhile run < op_runs |
|||
echo run no. $&run with R2 = $&rcur Ohms |
|||
altermod R2 r = $&rcur ; model parameter r of model rmod for R2 is changed to rcur |
|||
op ; simulate operating point, plot op1 is created |
|||
setplot $plot_out ; go to the output plot |
|||
let vn1 = op1.v(n1) |
|||
echo voltage at node n1 is $&vn1 |
|||
let vnode2[run] = op1.v(n1) ; store the current n1 voltage value |
|||
let r2val[run] = rcur ; store the current R2 resistance value |
|||
destroy op1 |
|||
let rcur = rcur + delta |
|||
let run = run + 1 |
|||
end |
|||
settype impedance r2val |
|||
settype voltage vnode2 |
|||
plot vnode2 vs r2val |
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,53 @@ |
|||
* dc loop with .param change |
|||
* We alter resistor R2 |
|||
|
|||
.param pr2 = 1k |
|||
|
|||
* The circuit |
|||
R1 n1 0 1k |
|||
R2 n2 n1 {pr2} |
|||
|
|||
V1 n2 0 1 |
|||
|
|||
|
|||
* start and end values for R2 |
|||
.csparam start = {pr2} |
|||
.csparam end = 0.1k |
|||
.csparam delta = 0.05k |
|||
|
|||
* control script |
|||
.control |
|||
* create a new plot for storing the measurements |
|||
set curplot = new ; create a new plot |
|||
set curplottitle = "OutputData" |
|||
set plot_out = $curplot ; store its name to 'plot_out' |
|||
|
|||
if (end < start) ; find appropriate sign for delta |
|||
let delta = -abs(delta) |
|||
else |
|||
let delta = abs(delta) |
|||
end |
|||
let ldelta = delta ; loop delta, original delta will be restored to csparam by 'reset' |
|||
let op_runs = floor((end - start)/delta) + 1 ; number of runs for simulation |
|||
let run = 0 ; number of actual run |
|||
let vnode2 = unitvec(op_runs) ; vector for all n1 voltages |
|||
let r2val = unitvec(op_runs) ; vector for all resistor values |
|||
let rcur = start ; set the start value for R2 |
|||
* the loop |
|||
dowhile run < op_runs |
|||
alterparam pr2 = $&rcur ; instance parameter resistance for R2 is changed to rcur |
|||
reset ; make .param change effective |
|||
op ; simulate operating point, plot op1 is created |
|||
setplot $plot_out ; go to the output plot |
|||
let vnode2[run] = op1.v(n1) ; store the current n1 voltage value |
|||
let r2val[run] = rcur ; store the current R2 resistance value |
|||
destroy op1 |
|||
let rcur = rcur + ldelta |
|||
let run = run + 1 |
|||
end |
|||
settype impedance r2val |
|||
settype voltage vnode2 |
|||
plot vnode2 vs r2val |
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,19 @@ |
|||
example if then else loop |
|||
.control |
|||
|
|||
foreach val -40 -20 0 20 40 |
|||
if $val < 0 |
|||
echo variable $val is less than 0 |
|||
else |
|||
echo variable $val is greater than or equal to 0 |
|||
end |
|||
end |
|||
|
|||
let vec = 1 |
|||
if vec = 1 ; $&vec = 1 is possible as well |
|||
echo vec is $&vec |
|||
end |
|||
|
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,12 @@ |
|||
example dowhile loop |
|||
.control |
|||
|
|||
let loopindex = 0 |
|||
dowhile loopindex <> 5 |
|||
echo index is $&loopindex |
|||
let loopindex = loopindex + 1 |
|||
end |
|||
|
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,15 @@ |
|||
example foreach loop |
|||
.control |
|||
|
|||
foreach val -40 -20 0 20 40 |
|||
echo var is $val |
|||
end |
|||
echo |
|||
set myvariable = ( -4 -2 0 2 4 ) |
|||
foreach var $myvariable |
|||
echo var is $var |
|||
end |
|||
|
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,11 @@ |
|||
example repeat loop |
|||
.control |
|||
|
|||
set loops = 7 |
|||
repeat $loops |
|||
echo How many loops? $loops |
|||
end |
|||
|
|||
.endc |
|||
|
|||
.end |
|||
@ -0,0 +1,12 @@ |
|||
example while loop |
|||
.control |
|||
|
|||
let loopindex = 0 |
|||
while loopindex < 5 |
|||
echo index is $&loopindex |
|||
let loopindex = loopindex + 1 |
|||
end |
|||
|
|||
.endc |
|||
|
|||
.end |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue