Using MacSpice for simulating circuits

This article is a tutorial on how to use MacSpice to run simulations on macOS, how the output would be like.

MacSpice Example – Ljubljana Audio Power Amplifier
1[0]) destroy let counter = counter + 1 end plot impedance vs scale xlabel offset ylabel ‘output impedance at 1khz (magnified)’ title ‘ac analyses’ destroy .endc v1 1 0 dc 0 v2 16 0 dc 25V v3 15 0 dc -25V voutput 0 17 dc 0 ac 1 voffset 18 0 dc -100mV c1 1 2 .22uF c2 5 0 33u c3 7 6 1n c4 6 8 47p c5 9 12 47u r1 2 18 47k r2 3 16 24k r3 4 5 470 r4 4 9 47k r5 6 15 1.5k r6 15 7 100 r7 12 16 470 r8 12 11 4.7k r9 11 10 2.2k r10 10 8 1.5k r11 16 13 100 r12 9 14 100 r13 9 17 4 q1 6 2 3 bc556 q2 15 4 3 bc556 q3 8 6 15 bd139 q4 11 10 8 bc546 q5 13 11 9 bd139 q6 15 8 14 bd140 q7 9 13 16 mj2955 q8 15 14 9 mj2955 d1 9 16 mr811 d2 15 9 mr811 .model bc556 pnp (is=20.3f nf=1 bf=123 vaf=161 ikf=.12 ise=16.4p ne=2 + br=4 nr=1 var=16 ikr=.18 re=.257 rb=1.03 rc=.103 xtb=1.5 + cje=54.3p vje=1.1 mje=.5 cjc=17.5p vjc=.3 mjc=.3 tf=618p tr=429n) .model bc546 npn (is=50.7f nf=1 bf=318 vaf=145 ikf=.3 + ise=15.9p ne=2 br=4 nr=1 var=24 ikr=.45 re=1.54 rb=6.17 + rc=.617 xtb=1.5 cje=20.8p cjc=8.33p tf=611p tr=138n) .model bd139 npn (is=110f nf=1 bf=136 vaf=161 ikf=.5 ise=70.8p ne=2 + br=4 nr=1 var=20 ikr=.75 re=.154 rb=.617 rc=61.7m xtb=1.5 + cje=50.7p vje=.6 mje=.3 cjc=32.9p vjc=.22 mjc=.2 tf=13.2n tr=510n) .model bd140 pnp (is=110f nf=1 bf=130 vaf=161 ikf=.5 ise=74.3p ne=2 + br=4 nr=1 var=20 ikr=.75 re=.154 rb=.617 rc=61.7m xtb=1.5 + cje=111p vje=.6 mje=.3 cjc=72.5p vjc=.22 mjc=.2 tf=13.2n tr=510n) .model mj2955 pnp (is=7.37e-13 nf=1.0 bf=156 vaf=139 ikf=2.0 ise=2.53e-10 + ne=2.0 br=4 nr=1.0 var=28 xtb=1.5 re=1.5e-01 rb=5.9e-01 rc=5.9e-02 + cje=1030p cjc=736p tf=4.0e-08 tr=7.6e-06 mje=.363 vje=.633 mjc=.361 + vjc=.542) .model mr811 d (is=5.95e-08 n=2.180 bv=1.33e+02 rs=2.96e-02 cjo=2.39e-11 + vj=.75 m=.41 tt=5.04e-07) .end” style=”color:#EEFFFF;display:none” aria-label=”Copy” class=”code-block-pro-copy-button”>
MacSpice Example - Ljubljana Audio Power Amplifier

* A rewrite by C.D.H. Williams of poweramp.cir by Puhan et al.
*
* Refer to:
* J. Puhan, T. Tuma and I. Fajfar
* <http://www.fe.uni-lj.si/spice/papers/ev98.pdf>
* SPICE for Windows 95/98/NT
* Electrotechnical Review, Ljubljana Slovenia, 65(5) (1998) 267-271.

.control

* ========================================================
* Plot the effect of dc cross-over compensation
* ========================================================

dc voffset -200mV 200mV 5mV
plot v(8) v(9) v(11) title 'dc analysis'
+    xlabel 'input offset voltage'
+    ylabel 'base and emitter voltages'

* ========================================================
* Plot output impedance vs frequency for 5 bias conditions
* ========================================================

* Start with a clean slate:

unset ListOfPlots

* Use a 'foreach' loop to set each voltage in turn using 'alter' to
* change the dc value of the 'voffset' source. Each pass through the
* loop creates a new plot. The name of each new plot is concatentated
* onto the variable 'ListOfPlots' with a comma separating each name.

foreach offset -100mV -50mV 0mV 50mV 100mV
	alter voffset dc = $offset
	ac dec 25 10Hz 1megHz
	let impedance = v(9)/i(voutput)
	if ($?ListOfPlots)
		set ListOfPlots = $ListOfPlots\,$curplot
	else
		set ListOfPlots = $curplot
	endif
end

* 'ListOfPlots' was carefully constructed above so that
* '{$ListOfPlots}.impedance' will be expanded by the frontend into
* the list of impedance-vs-freq vectors required by the 'plot' command.
* White space characters in the 'ylabel' and 'title' values are
* protected by quoting.

plot {$ListOfPlots}.impedance
+    title 'ac analyses'
+    ylimit 0 1 ylabel 'output impedance'

* Dispose of the plots we have just created and the record of their names.

destroy {$ListOfPlots}
unset ListOfPlots

* ==================================================================
* Plot output impedance at 1kHz for voffset between -300mV and 300mV
* ==================================================================

* create a new plot which will be of type/name 'unknown' then create
* vectors to hold the results

setplot new
let MaxPoints = 61
let impedance = vector(MaxPoints)
let counter = 0
let scale = (10 * vector(MaxPoints) - 300) / 1000
settype voltage scale

* Use a loop to perform an ac analysis at a range of of different
* dc values for the voltage source 'voffset'. The ac analysis creates
* its own plot and, until this destroyed, 'impedance' and 'counter' 
* are no longer in the current plot and must be referenced accordingly.

while counter < MaxPoints
   alter voffset dc = scale[counter]
   ac dec 1 1kHz 10kHz
   let unknown.impedance[unknown.counter] = mag2[0])
   destroy
   let counter = counter + 1
end

plot impedance vs scale xlabel offset ylabel 'output impedance at 1khz (magnified)' title 'ac analyses'
destroy
.endc

v1 1 0 dc 0
v2 16 0 dc 25V
v3 15 0 dc -25V
voutput 0 17 dc 0 ac 1
voffset 18 0 dc -100mV

c1 1 2 .22uF
c2 5 0 33u
c3 7 6 1n
c4 6 8 47p
c5 9 12 47u

r1 2 18 47k
r2 3 16 24k
r3 4 5 470
r4 4 9 47k
r5 6 15 1.5k
r6 15 7 100
r7 12 16 470
r8 12 11 4.7k
r9 11 10 2.2k
r10 10 8 1.5k
r11 16 13 100
r12 9 14 100
r13 9 17 4

q1 6 2 3 bc556
q2 15 4 3 bc556
q3 8 6 15 bd139
q4 11 10 8 bc546
q5 13 11 9 bd139
q6 15 8 14 bd140
q7 9 13 16 mj2955
q8 15 14 9 mj2955

d1 9 16 mr811
d2 15 9 mr811

.model bc556 pnp (is=20.3f nf=1 bf=123 vaf=161 ikf=.12 ise=16.4p ne=2
   + br=4 nr=1 var=16 ikr=.18 re=.257 rb=1.03 rc=.103 xtb=1.5
   + cje=54.3p vje=1.1 mje=.5 cjc=17.5p vjc=.3 mjc=.3 tf=618p tr=429n)
.model bc546 npn (is=50.7f nf=1 bf=318 vaf=145 ikf=.3
   + ise=15.9p ne=2 br=4 nr=1 var=24 ikr=.45 re=1.54 rb=6.17
   + rc=.617 xtb=1.5 cje=20.8p cjc=8.33p tf=611p tr=138n)
.model bd139 npn (is=110f nf=1 bf=136 vaf=161 ikf=.5 ise=70.8p ne=2
   + br=4 nr=1 var=20 ikr=.75 re=.154 rb=.617 rc=61.7m xtb=1.5
   + cje=50.7p vje=.6 mje=.3 cjc=32.9p vjc=.22 mjc=.2 tf=13.2n tr=510n)
.model bd140 pnp (is=110f nf=1 bf=130 vaf=161 ikf=.5 ise=74.3p ne=2
   + br=4 nr=1 var=20 ikr=.75 re=.154 rb=.617 rc=61.7m xtb=1.5
   + cje=111p vje=.6 mje=.3 cjc=72.5p vjc=.22 mjc=.2 tf=13.2n tr=510n)
.model mj2955 pnp (is=7.37e-13 nf=1.0 bf=156 vaf=139 ikf=2.0 ise=2.53e-10
   + ne=2.0 br=4 nr=1.0 var=28 xtb=1.5 re=1.5e-01 rb=5.9e-01 rc=5.9e-02
   + cje=1030p cjc=736p tf=4.0e-08 tr=7.6e-06 mje=.363 vje=.633 mjc=.361
   + vjc=.542)
.model mr811 d (is=5.95e-08 n=2.180 bv=1.33e+02 rs=2.96e-02 cjo=2.39e-11
   + vj=.75 m=.41 tt=5.04e-07)

.end

The output of figures could only be saved as pdfs, which is fine for most of the cases, but for websites, it would be better to use svgs since they are much smaller and have a better visual representations. One could install pdf2svg via brew and convert the pdf files easily to svg.

pdf2svg plot.pdf plot.svg;
  1. v(9)/i(voutput[]
  2. v(9)/i(voutput[]

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *