SPICE Engine Analysis and Circuit Simulation Application Development

Electrical design automation plays an important role in nowadays electronic industry. Various commercial Simulation Program with Integrated Circuit Emphasis (SPICE) packages, such as pSpice Or CAD, have become the standard computer program for electrical simulation, with numerous copies in use worldwide. The customized simulation software with copyright need the understanding and using of SPICE engine which was open-source shortly after its birth. The inner workings of SPICE, including algorithms, data structure and code structure of SPICE were analyzed, and a engine package and application development approach were proposed. The experiments verified its feasibility and accuracy


Introduction
Knowledge of the behavior of electrical circuits requires the simultaneous solution of a number of equations.The model creation [1], circuit simulation [2] and testing [3], and application of analog circuit and VLSI [4] require the assisstance of simulation tools. As a general-purpose circuit-simulation program that solves the network equations for the node voltage for nonlinear DC, nonlinear transient, and linear AC analysis, SPICE in its different versions has been the main computer-aided analysis program used in analog design, and diagnosis for researchers, printing circuit board and electrical device manufacture for engineering and in universities and colleges for student education for over 40 years. This widely used spice-softwares, such as pSpice, hSpice, ngSpice, shares same engine which was re-developed or modified on the base of Berkeley's kernel and to provide various interfaces and functions.
However, this commercial software cannot be used to develop customized software free. In fact, it is well known that SPICE engine is open-source and free use by researchers all over the world. To make customized simulation software, researchers had various customer modification after analysis of the simulation algorithms [5],data structure and code structure. In 1980s, SPICE with version 2 had already rewrited in C code and ported in PC [6]. The 1990s saws various expansion of SPICE with multimedia technology [7] and networks [8,9] with its application in engineer and education. In the later two decades, the simulation algorithms were continously modified to speed the convergence [10,11] and improve the accuracy [12]. In the new centery, various virtual circuit labaroty [13,14] and studies [15][16][17] had been build by the aid of these customized spice-like simulations. To the author's knowledge, there is little information aviable about the computation principle and its simulations of the SPICE engine. Further, the detail information about the re-development of the simulation software with spice engine and C++ is also helpful to programmers.
In this paper, we conducted this study to analyze the inner workings of SPICE engine and customized simulation software development based on the spice kernel. The experiments verified the feasibility and accuracy of the proposed approach. It was born as a class project at the University of California, Berkeley during the mid-1970s and first released in 1971. After its first presented at a conference in 1973, this invaluable program soon evolved to universally used software and then to become the worldwide standard integrated circuit simulator in 1980s. Now SPICE is a general-purpose, open source analog electronic circuit simulator used in integrated circuit and board-level design to check the integrity of circuit designs, to predict circuit behavior, and even to locate the potential errors in circuit board. Figure 1 has given a simplified block diagram of the main SPICE program flow. We can see from Figure 1, when a circuit description was input to SPICE, its initial operating point was calculated first, and the linear companion models was then created for non-linear devices. Then SPICE entered its first kernel process--block 3 and 4, which stand for Nodal Analysis accomplished by formulating the Nodal Matrix and solving the nodal equations for the circuit voltages. SPICE finds the solution for Non-linear circuits in the inner loop (2)(3)(4)(5)(6). It may take many iterations before the calculations converge to a solution. The outer loop (7)(8)(9), together with the inner loop, performs a Transient Analysis creating equivalent linear models for energystorage components for capacitors, inductors, etc. and selecting the best time points.

Nodal Analysis
At the core of the SPICE engine is a basic technique called Nodal Analysis that calculates the voltage at any node given all resistances (conductances) and current sources of the circuit. When a circuit was input, as shown in Figure 2, the SPICE engine calculate the the set of nodal equations according Kirchoff Law. These set of equations will be conveted to matrix form using mathmatical analysis and numeric computing. The frequently used method is Gaussian Elimination and LU Factorization. At last, the equation is solved for the node voltages and this equation is the central mechanism of the SPICE algorithm.

Non-Linear DC Analysis
SPICE can find the voltage at every node in a DC linear circuit by only executing mode 3 and 4. If any non-linear components appear in the circuit, SPICE will first transforming the non-linear components into some equivalence circuit suitable for Nodal Analysis, and then find a solution as if it was a linear circuit, but using a little guessing and a lot more trips. This process covers step 2-5, i.e. the inner loop.
As an example, the circuit shown in Figure 2 includes a very non-linear component, a diode.

Figure 2. A Simple Non-linear Diode Circuit and part of its equivalent circuit
Diode's current versus voltage relationship is described by: Where IS is the saturation current and V t is the thermal voltage. A diode can be modeled using linear components --a parallel combination of a conductance G eq and a current source I eq , where G eq is simply the slope of the tangentat the operating point V do .

exp
(2) And I eq is the point where the tangent slices through the y-axis. ( After this equivalent substition, the nodal equations can be developed as: The whole solution looks like this: 1) Guess the diode's initial trial operating point.

3) Solve Nodal Equations. 4) Convergence?
|V n -V n-1 |<V limit and |I n -I n-1 | <I limit ? 5) No--Use the calculated diode voltage as new trial operating point for another iteration. Start again at Step 2.
6) Yes -Stop iterations and find the solution.
In SPICE, the limits of voltage and currentare actually calculated by [18].
V limit = V n * RELLOT+VNTOL I limit = I n * RELLOT+ABSTOL By default, RELTOL is set to 0.001 or 0.1 percent. So if the expected voltage is 5V, the V limit should be set to 5 mV to reach a solution. However, if the excepted voltage swings near zero, V limit would be ridiculous small and hard to be reached. That's where VNTOL enters the picture. The default of VNTOL is 1 μV to ensures that the limit doesn't get too small if voltages approach 0V. RELTOL and ABSTOL play a similar role for the current change limits,and the default for ABSTOL is 1PA.

Transient Analysis
SPICE executed the outer loop for the linear circuit transient analysis. If there is any energy-storage component, such as capacitor and inductor, SPICE first transformed the energystorage components into their linear companion models, and used Nodal Analysis to find the solution. For example, for the capacitors, SPICE will use backward-Euler(BE) to predict the next approximate value at a future time point t n+1 . The idea of EU is predicting the next voltage with the slope at x n+1 as the following equation: Where ∆ 1 . Graphically, it looks something like Figure 3. The relationships of a capacitor's voltage-current-charge can be described as: Next, apply the BE formula to predict the capacitor's voltage at the next time point.
Finally, we can get an equation in terms of voltages and currents only: At this time, we can transform a capacitor circuit shown as Figure 4 with its Linear companion model, where , .
. With this equivalent substitution, SPICE can develop the nodal equations and find the every node voltages for certain time. Then, SPICE would forward a time step and repeat the same procedure to find the voltages at that time, and so on till the last time.
SPICE can set timestepdynamically to trade offsimulation accuracy and speed. If the change of current or voltage is rapid, SPICE would reduce its step to increase the precise of the simulation, and vice versa.

SPICE Code Strcture
The simplified block diagram of SPICE engine code structure can be shown in Figure 5, where seven Modules are included. 1) Command Parsing: The kernel of this block is comn structure, which includes the name of the commands and their corresponding functions. The program implemented an array of comn structure that defines the commands which SPICE can parse and its corresponding actions. Whenever a command was input, SPICE parses it and finds its match from the set of commands, and executes the corresponding action. 2) Circuit Description: The most important data structure of this block is CKT circuit structure, where contains the device type, simulation type, temperature, and node information, etc. 3) Sparse Matrix: This block defines SMP element structure to represent the non-zero items of sparse matrix where the items are stored with increasing order as they appear in the row or column of The matrix. 4) Numerical Computation: This block contains two typical numerical methods: Newton-Raphson iteration and numerical integration. In SPICE, they were used to transform the set of differential equations into a set of algebraic equations.

Application Interface of SPICE engine
SPICE engine can be package to a number of independent code procedures with prescribed interface. In this way, a block can call certain package to complete given work according to the interface. In addition, any block can be maintained, updated，even rewritten independently.
Basically, SPICE operates like this: 1) SPICE reads in a text circuit description file (".cir" extension) called a netlist, and then sets the corresponding Parameters. 2) SPICE sets the simulation options and performed certain analysis type as given (AC, Transient, DC, Noise, etc.) by calling the interface of analysis procedure. Let's take transient (time) analysis for Linear Circuits as an example to investigate. SPICE would call fuctionDCTran first with the given circuit description. Inside this function, and device information would be found according to every device model. An operating point is required for the initial solution to a Transient Analysis. After the initial operating point was found, this function set the timestep and option parameter call the inner loop to solve the Nodal equation. The results is acquired from the sparse matrix and stored in the structure TRANan, where it can be print or draw by ".PRINT" or".PLOT" command.

Experiments of using SPICE Engine
SPICE engine can interactive with end user by command lines only. Therefore, it's convenient to encapsulate the engine to dynamic link libraries in the integrated development environment like visual studio 2010. These libraries were then package to be a engine class with exposing appropriate interfaces to the caller. For instance, the interface function for the transient analysis described previously may look like "BOOL tran (char* filename, int step, intend_t, intstart_t, intmaxstep)", where the input parameter can be the file name and path of circuit description, timestep, endtime, starttime (with the default value zero), and max timestep allowed. This function returns TURE when it completes the analysis and return FALSE when any error was found.
Then various applications about different circuit analysis and fault simulation can be developed with the SPICE engine that was packaged as a class. Here we build a single document MFC application that calls the SPICE engine. It reads a netlist file as input, and sets analysis type, simulation parameters by a dialogue. Then the application calls the engine to simulate the behavior of the inputting circuit. The simulation results were outputted as text file. Figure 6 shows the application running interface. It displays a operator amplified circuit in the window frame and reads user defined analysis parameters. To verify the feasibility of this approach and investigate the accuracy of SPICE engine, the same circuit was build and analyzed with the same parameters and analysis type in OrCAD Capture, a famous business software. Figure 7 is schematic Of the testing circuit in orCAD Capture.
These two groups of simulation data were exported to Matlab to investigate the difference. Fig 8 is result given by Matlab where the solid line represent the results from OrCAD Capture, and the dot line represent the results from the testing application. It is clear that the results are in accord with each other in most case. There are also some difference due to the possible difference in device models, and maybe integration methods. The most significant difference lie to the maximumValue with only 2.13% difference.

Figure 8. Experiment result
As the result, our approach of using SPICE engine turns out to be a suitable for further software development.

Conclusion
In this paper, the kernel algorithm and code structure of SPICE engine were analyzed, and an software architecture that package SPICE engine as class and called by other applications is proposed. The experiment verified the feasibility and accuracy of the proposed approach. This research can be used to development customized simulation software with fully simulation typies and all customered parameters aviable. Furthermore, it can easily used to develope the fault simulation by simply replace the function model of the devices as a faultinjected model.
Further works involves two parts. One is details of engine, such as its algorithm, its analysis, its parameter setting. Theother is result data processing, such as Fourier transform, wavelet analysis and featureextraction.