From 7a50c4b84a1bd96b576125b2f3bbad6788f30d29 Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Wed, 6 Apr 2022 10:32:18 +0200 Subject: [PATCH] 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. --- examples/loops/instance-loop.cir | 48 +++++++++++++++++++++++++++ examples/loops/loop-instance.cir | 48 +++++++++++++++++++++++++++ examples/loops/loop-model.cir | 52 +++++++++++++++++++++++++++++ examples/loops/loop-param.cir | 53 ++++++++++++++++++++++++++++++ examples/loops/loop_IfThenElse.cir | 19 +++++++++++ examples/loops/loop_dowhile.cir | 12 +++++++ examples/loops/loop_foreach.cir | 15 +++++++++ examples/loops/loop_repeat.cir | 11 +++++++ examples/loops/loop_while.cir | 12 +++++++ 9 files changed, 270 insertions(+) create mode 100644 examples/loops/instance-loop.cir create mode 100644 examples/loops/loop-instance.cir create mode 100644 examples/loops/loop-model.cir create mode 100644 examples/loops/loop-param.cir create mode 100644 examples/loops/loop_IfThenElse.cir create mode 100644 examples/loops/loop_dowhile.cir create mode 100644 examples/loops/loop_foreach.cir create mode 100644 examples/loops/loop_repeat.cir create mode 100644 examples/loops/loop_while.cir diff --git a/examples/loops/instance-loop.cir b/examples/loops/instance-loop.cir new file mode 100644 index 000000000..e3c1f4459 --- /dev/null +++ b/examples/loops/instance-loop.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 \ No newline at end of file diff --git a/examples/loops/loop-instance.cir b/examples/loops/loop-instance.cir new file mode 100644 index 000000000..b71f28613 --- /dev/null +++ b/examples/loops/loop-instance.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) ; 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 \ No newline at end of file diff --git a/examples/loops/loop-model.cir b/examples/loops/loop-model.cir new file mode 100644 index 000000000..6ba53822b --- /dev/null +++ b/examples/loops/loop-model.cir @@ -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 \ No newline at end of file diff --git a/examples/loops/loop-param.cir b/examples/loops/loop-param.cir new file mode 100644 index 000000000..2153d0359 --- /dev/null +++ b/examples/loops/loop-param.cir @@ -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 \ No newline at end of file diff --git a/examples/loops/loop_IfThenElse.cir b/examples/loops/loop_IfThenElse.cir new file mode 100644 index 000000000..de4d03a93 --- /dev/null +++ b/examples/loops/loop_IfThenElse.cir @@ -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 diff --git a/examples/loops/loop_dowhile.cir b/examples/loops/loop_dowhile.cir new file mode 100644 index 000000000..93e93471d --- /dev/null +++ b/examples/loops/loop_dowhile.cir @@ -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 diff --git a/examples/loops/loop_foreach.cir b/examples/loops/loop_foreach.cir new file mode 100644 index 000000000..1195c07b7 --- /dev/null +++ b/examples/loops/loop_foreach.cir @@ -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 diff --git a/examples/loops/loop_repeat.cir b/examples/loops/loop_repeat.cir new file mode 100644 index 000000000..eb10974fe --- /dev/null +++ b/examples/loops/loop_repeat.cir @@ -0,0 +1,11 @@ +example repeat loop +.control + +set loops = 7 +repeat $loops + echo How many loops? $loops +end + +.endc + +.end diff --git a/examples/loops/loop_while.cir b/examples/loops/loop_while.cir new file mode 100644 index 000000000..1fc24dc7a --- /dev/null +++ b/examples/loops/loop_while.cir @@ -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