Pages

Sunday, 10 March 2013

Hello world in NgSpice!

Let's start with electronic circuit simulation in Linux! I want to start with a very simple example. I'm going to do a simulation of a voltage divider in Ngspice.
This is the first of a series of examples in Ngspice, in a linux machine.
Ngspice, as it is written in the manual, is a general-purpose circuit simulation program for nonlinear and linear analyses. It works on almost on every type of Operative systems like Linux distributions, MS Windows and Mac OS, and even on tablet and smartphone through a remote service online. It is completely free. For installation guide and other useful information please look in the website.


Here the circuit description file:

voltage_divider.sp

*A simple Voltage divider

V1 1 0 DC 15
R1 1 2 10k
R2 2 0 5k

.OP
.PRINT OP v(2)

.END


Now le us analyze the code.
The first line is the "name of the circuit", so we can write anything and it can not be seen by ngspice as a line of the script but everytime as the title! It is a common error start to write the code from the first line in spice.
To follow we have the statements of a voltage source of 15 volt between the node 1 (positive) and 0 (negative-ground), then the statements of the two resistor, that form the voltage divider. After that there are command to execute a DC simulation to calculate the operating point of the circuit and the showing of the result, then script .end!
To execute it and get the result of this simple circuit, we must enter in the terminal, from the same directory where the file is saved:
ngspice -b -o output.txt voltage_divider.sp

So the result of Operating Point simulation and the value of v(2), will be written in the file output.txt. In this file we can find more information compared to those we looking for. Such the voltages of all the nodes, the model of all the devices in the circuit (Resistor and Voltage source) and other information about the simulation process.

Another method to get only the result of the voltage in the node 2 is tu use the interactive interpreter. You can write the interactive command to get v(2) directly in ngspice, or write them in the script (voltage_divider2.sp) to execute like that:

voltage_divider2.sp

A simple Voltage divider

V1  1   0   DC 15
R1  1   2   10k
R2  2   0   5k

.CONTROL
   OP
   print v(2)
   quit
.ENDC

.END


In this way ngspice will print only the desired information:
ngspice voltage_divider2.sp
The result will be, of course: "v(2) = 5.000000e+00".

The first example is finished! I will continue in the next post!

No comments:

Post a Comment