missing NULL terminator in set_config_x
[geda-gaf.git] / docs / wiki / geda-ngspice_and_gschem.html
blob29083c370360f47be89a1cad99bcec181b792cf9
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html>
4 <head>
5 <link rel="stylesheet" media="screen" type="text/css" href="./style.css" />
6 <link rel="stylesheet" media="screen" type="text/css" href="./design.css" />
7 <link rel="stylesheet" media="print" type="text/css" href="./print.css" />
9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10 </head>
11 <body>
13 <p>
14 <em>Translations of this page are also available in the following languages:</em> <a href="geda-ngspice_and_gschem.ru.html" class="wikilink1" title="geda-ngspice_and_gschem.ru.html">Русский</a>.
15 </p>
17 <h1 class="sectionedit1" id="overview">Overview</h1>
18 <div class="level1">
20 <p>
21 It is possible to use gschem to make circuit diagrams which can then be simulated with ngspice. The advantage is that you can iteratively design and simulate before you commit to fabrication. The purpose of this tutorial is not to teach the details of SPICE but rather to work a very simple example so that the required steps are made clear. More detailed documents on both Spice and gEDA can be found at:
22 </p>
23 <ul>
24 <li class="level1"><div class="li"> <a href="http://ngspice.sourceforge.net/index.html" class="urlextern" title="http://ngspice.sourceforge.net/index.html" rel="nofollow">ngspice</a></div>
25 </li>
26 <li class="level1"><div class="li"> <a href="geda-csygas.html" class="wikilink1" title="geda-csygas.html">geda and Spice</a></div>
27 </li>
28 </ul>
30 <p>
31 There is a <abbr title="Frequently Asked Questions">FAQ</abbr> in the wiki that may be of help: <a href="geda-faq-simulation.html" class="wikilink1" title="geda-faq-simulation.html">faq-simulation</a>
32 </p>
34 <p>
35 This tutorial is based on the example &#039;TwoStageAmp.sch&#039; in the examples which ship in gEDA&#039;s documentation and constructs the first half of that example step by step.
36 </p>
38 </div>
39 <!-- EDIT1 SECTION "Overview" [121-892] -->
40 <h1 class="sectionedit2" id="setup">Setup</h1>
41 <div class="level1">
43 <p>
44 It is assumed that you are able to construct schematics with gschem. If you are unsure how to do this please visit <a href="geda-gsch2pcb_tutorial.html" class="wikilink1" title="geda-gsch2pcb_tutorial.html">gsch2pcb_tutorial</a>. So let&#039;s start by making a project directory &#039;simpleSPICE&#039; and creating a schematic file named
45 &#039;simplespice.sch&#039; in that directory.
46 </p>
48 <p>
49 Use gschem to build up a schematic that looks like:
50 </p>
52 <p>
53 <a href="media/geda/simple1.png" class="media" target="_blank" title="geda:simple1.png"><img src="media/geda/simple1.png" class="media" alt="" /></a>
54 </p>
56 <p>
57 Note that three named nets have been created, “Vin”, “Vout”, and “Vbase”. Although this is not necessary, it is an easy way to keep track of important points in the circuit. Naming nets is done by editing either by right-clicking or by hitting <kbd>E</kbd><kbd>E</kbd> while a net is selected; then just add the attribute &#039;netname&#039; and give it an appropriate name. Although we now have a circuit to simulate, we need to add some SPICE specific information before we can get any results.
58 </p>
60 </div>
61 <!-- EDIT2 SECTION "Setup" [893-1747] -->
62 <h1 class="sectionedit3" id="adding_some_spice_stuff">Adding some SPICE stuff</h1>
63 <div class="level1">
65 <p>
66 There are four things that we will need to add to the schematic to make it suitable for simulation. We must include an input signal, describe the properties of the transistor, explicitly define Vcc, and add some lines to the SPICE file. When all these things are done
67 the schematic will look like:
68 </p>
70 <p>
71 <a href="media/geda/schematicfinished2.png" class="media" target="_blank" title="geda:schematicfinished2.png"><img src="media/geda/schematicfinished2.png" class="media" title="" alt="" /></a>
72 </p>
74 <p>
75 The input signal is added by choosing &#039;SPICE simulation elements→vac-1.sym&#039; from the components menu and, after placing the device, editing its properties to read:
76 </p>
78 <p>
79 <a href="media/geda/dialogvac.png" class="media" target="_blank" title="geda:dialogvac.png"><img src="media/geda/dialogvac.png" class="media" title="" alt="" /></a>
80 </p>
82 <p>
83 Notice that the &#039;value&#039; is a string that will be read by SPICE. You learn about SPICE syntax in the links given above.
84 </p>
86 <p>
87 Vcc is set in basically the same way except we choose &#039;SPICE simulation elements→vdc-1.sym&#039; from the components menu. Its properties should be edited to read:
88 </p>
90 <p>
91 <a href="media/geda/dialogvdc1.png" class="media" target="_blank" title="geda:dialogvdc1.png"><img src="media/geda/dialogvdc1.png" class="media" title="" alt="" /></a>
92 </p>
94 <p>
95 The transistor setup is a bit more complicated. We must first have access to a SPICE model for the transistor of interest. This can usually be found on the manufacturer&#039;s website or the part&#039;s datasheet, but it may need to be modified to work with your favorite simulator. The easiest way to get the model is from <a href="http://www.h-renrew.de/h/spicelib/doc/index.html" class="urlextern" title="http://www.h-renrew.de/h/spicelib/doc/index.html" rel="nofollow"> spicelib</a>. For this example we will consider the ubiquitous 2N3904. Which has the following model:
96 </p>
97 <pre class="code">.model 2N3904 NPN(Is=6.734f Xti=3 Eg=1.11 Vaf=74.03 Bf=416.4 Ne=1.259
98 + Ise=6.734f Ikf=66.78m Xtb=1.5 Br=.7371 Nc=2 Isc=0 Ikr=0 Rc=1
99 + Cjc=3.638p Mjc=.3085 Vjc=.75 Fc=.5 Cje=4.493p Mje=.2593 Vje=.75
100 + Tr=239.5n Tf=301.2p Itf=.4 Vtf=4 Xtf=2 Rb=10)
101 * Fairchild pid=23 case=TO92
102 * 88-09-08 bam creation</pre>
105 Make a sub-folder named &#039;models&#039; in our project folder &#039;simpleSPICE&#039; and make a file named &#039;2N3904.mod&#039; in that folder and fill it with the model text given above. Make sure that there is a newline at the end of the file, this is important so that when the final SPICE file is created everything will be on its own line. Okay, now that we have a model for the transistor we have to apply it. Edit the transistor properties to read:
106 </p>
109 <a href="media/geda/dialogtrans.png" class="media" target="_blank" title="geda:dialogtrans.png"><img src="media/geda/dialogtrans.png" class="media" title="" alt="" /></a>
110 </p>
113 Now we also need to inform gschem of the model file we created. This is done by adding a &#039;spice-model-1.sym&#039; symbol to the schematic.
114 Edit its properties to read:
115 </p>
118 <a href="media/geda/dialogmodel.png" class="media" target="_blank" title="geda:dialogmodel.png"><img src="media/geda/dialogmodel.png" class="media" title="" alt="" /></a>
119 </p>
122 Okay, there are two more symbols to add. The first is &#039;spice-include-1.sym&#039; which will include a text file which describes the SPICE simulation you may want to run and the other is &#039;spice-directive-1.sym&#039; which allows additional lines to be added to the SPICE file.
123 The properties of these symbols can be set as:
124 </p>
127 <a href="media/geda/dialoginclude.png" class="media" target="_blank" title="geda:dialoginclude.png"><img src="media/geda/dialoginclude.png" class="media" alt="" /></a>
128 </p>
131 <a href="media/geda/dialogdirective.png" class="media" target="_blank" title="geda:dialogdirective.png"><img src="media/geda/dialogdirective.png" class="media" alt="" /></a>
132 </p>
135 Congratulations! We now have a schematic that is ready for simulation.
136 </p>
138 </div>
139 <!-- EDIT3 SECTION "Adding some SPICE stuff" [1748-4590] -->
140 <h1 class="sectionedit4" id="simulation">Simulation</h1>
141 <div class="level1">
144 Although the schematic is in place, we still need to write out what the simulation will do. This information will be written in the &#039;./Simulation.cmd&#039; that was referenced in the include symbol added in the last section. For now, create a file &#039;./Simulation.cmd&#039;
145 and make it read:
146 </p>
147 <pre class="code">.OP
148 .AC DEC 20 1Hz 100MegHz
149 * .DC VINPUT 0 5 .01
150 * .DC VINPUT 1 2 .01
151 .plot ac v(Vout) v(Vin)
152 .print ac v(Vout) v(Vin)</pre>
155 Which does one AC analysis while two lines for DC analysis are commented out. Then both Vout and Vin are plotted as well as having their numerical values printed. As mentioned previously, SPICE can be learned from the links given above.
156 </p>
159 Now that we have described the simulation we want done, open a terminal, move to the &#039;simpleSPICE&#039; directory and type the command:
160 </p>
161 <pre class="code">gnetlist -g spice-sdb -o spice.net simplespice.sch</pre>
164 to produce the SPICE file &#039;spice.net&#039;. Now the simulation can be run in batch mode by typing:
165 </p>
166 <pre class="code">ngspice -b spice.net &gt; result.txt</pre>
169 which will spew a great deal of text to &#039;result.txt&#039;. The file should include the numerical output we requested as well as a crude ascii plot of the results. Note that the 10mV input signal has been amplified to about -280mV over a wide frequency range. If you want to view graphical plots ngspice can be operated in interactive mode by removing the -b option. The ngspice website describes this in more detail. In any event, we now have the simulation results in a nice text format suitable for reading with a script, perhaps something written in python or octave, for further graphing or analysis.
170 </p>
172 </div>
173 <!-- EDIT4 SECTION "Simulation" [4591-] --></body>
174 </html>