# Convergency of SPICE simulator
Note: This article is to understand **why my analysis circuit does not converge when I analysis circuit with SPICE**, so we need to understand a little how SPICE works, but how to desing circuit analizer is not point in this article.
[[SPICE] transient GMIN stepping at time=0.00156965](https://geekyengineers.com/transient-gmin-stepping-at-time0-00156965/)
Content:
0. Nutshell: How SPICE Works
1. Newton-Raphson method: find roots
2. Advance of Bias point and DC sweep
3. Transient analysis
## 0. Nutshell: How SPICE Works
* A circuit consist of *Loop*s
* *Loop*s consist of *Node*s and *Branch*s
* 
* SPICE adopt KCL
* 
* I1 ~ I5 meants *branch*
### 0-1. A example

* G = 1/R (conductance)
1. System of equation
* 
2. Converting System of equation to matrix form
* 
3. If `G` included capatance or inductance, then matrix `G` is a *differential equation*
4. *Homogeneous system equation* $->$ Find roots (f(x)=0) $->$ *Newton-Raphson method*
* Note: Matrix form of Newton-Raphson method, refer to [here](https://www.lakeheadu.ca/sites/default/files/uploads/77/Jia.pdf) and [here](https://engcourses-uofa.ca/books/numericalanalysis/nonlinear-systems-of-equations/newton-raphson-method/)
## 1. Newton-Raphson method: find roots
### 1-1. Basic concept of *Newton–Raphson method*
* SPICE use Newton–Raphson method to find a solution for every *node*s of circuit
* 
* 
* $x_{n}$: x_now
* $x_{n+1}$: x_next
* $y_{n}$ or $f(x_{n})$: y_now
* $y_{n+1}$ or $f(x_{n+1})$: y_next = 0
* 
### 1-2. Newton–Raphson method and SPICE
* Can solve non-linear differential eq.
* Semiconductor with capacitor
* The Newton-Raphson algorithm is guaranteed to converge to a solution. However, this guarantee has some conditions:
* The nonlinear equations must have a solution. (1-2-1)
* The equations must be continuous. (1-2-2)
* The algorithm needs the equations derivatives. (1-2-3)
* The initial approximation must be close enough to the solution. (1-2-4)
* SPICE software limited (also can be limited by hardware of PC)
* Voltages and currents in PSpice are limited to $\pm10^{10}$ (or +/-1e10) volts and amps.
* Derivatives in PSpice are limited to $10^{14}$ (or 1e14).
* The arithmetic used in PSpice is double precision and has *15 digits* of accuracy
#### 1-2-1. Is there a solution?
* This simple circuit cannot be solved by SPICE
```
-o---o-
| |
| |
+ |
5V P
- N
| |
| |
-o---o-
```
* The I/V relationship of a diode
* 
* IS: Dark saturation current of Diode
* N=1, KT=25mV(T=300K), V=5V, Is=1e-16
* I = 7.22e70 A
* Maximum current limit is exceeded in SPICE
* Overing I/V limitation of SPICE, which is not hard due to exponential features of diode(semi-conductor)
* Dynamic range($\Delta v$ or $\Delta i$) also can cause no solution (See 1-2-2)
* Beware of **denominators** <- Evil
* Good reference: [Concept about Numeric and float nomber](http://blog.dcview.com/article.php?a=VmhQNVY%2BCzo%3D)
* [Stability of Numeric](https://blog.csdn.net/ACdreamers/article/details/41514071)
#### 1-2-2. Are the Equations Continuous?

Be careful because of the exponential nature of diode I/V ($\Delta v$ or $\Delta i$)
* e.g. 0 to the knee voltage (~0.7V), during which its current
* $\Delta V \rightarrow \infty$ where $slope \rightarrow tan(\pi/2)$
#### 1-2-3. Are the derivatives correct?
* Most realistic models are derivatives
* y=|x| -> is continuous but not derivable
* In realistic circuits, enumerate model which is not differentiable but continuous are very hard
* Not continuous -> Not derivative
#### 1-2-4. Is the initial approximation close enough?
* If *initiail Value of Nodes* leave too long, than SPICE would not find a solution with Newton-Raphson method
* Initial Value(Start-up points): *Bias Point*
* Each analysis starts from a known solution and uses a variable step size to find the next solution
* If the next solution does not converge PSpice reduces the step size, falls back and tries again
##### 1-2-4-1. Bias Point (DC)
* Both $dv(t)/dt$ and $di(t)/dt$ are equal to $0$
* SPICE set power supplied(PS) as 100% output to try to find bias points
* Most of time is successful
* If can't find with 100% PS, then set PS close to almost 0, then *non-linear* features of diode can be zero
* High older terms of diode formula can be eliminated
* [Taylor expand of exponential](https://proofwiki.org/wiki/Power_Series_Expansion_for_Exponential_Function)
* If found, back up 100% PS with variable step size(check before)
##### 1-2-4-2. GMIN STEP
* Optional, default off (NGSPICE is on)
* When enabled, the GMIN stepping algorithm is applied after the circuit fails to converge with the power supplies at 100%
* If GMIN stepping also fails, the supplies are then cut back to almost zero
* GMIN stepping will find a solution by starting the repeating cycle initially 1.0e10 times the nominal value. If a solution is found at this value, next time it will reduce the value by 10.
### 1-3. Example of SPICE simulation output logs
[With and without GMIN](https://gist.github.com/curly-wei/4542c243801e16e575bda3e7bd8d3969)
* Without GMIN: Found soultion \@ 100% PS
* With GMIN: Can't found soultion \@ 100% PS
## 2. Advance of Bias point and DC sweep
## 3. Transient analysis
## Reference
[PSPICE Manual](https://www.seas.upenn.edu/~jan/spice/PSpice_UserguideOrCAD.pdf)
[In a Nutshell: How SPICE Works](https://www.emcs.org/acstrial/newsletters/summer09/HowSpiceWorks.pdf)
[Kirchhoffs Circuit Law](https://www.electronics-tutorials.ws/dccircuits/dcp_4.html)
[Wiki: Slope](https://en.wikipedia.org/wiki/Slope)
[Wiki: Newton-Raphson method](https://en.wikipedia.org/wiki/Newton%27s_method)
[Stability of Numeric](https://blog.csdn.net/ACdreamers/article/details/41514071)
[Concept about Numeric and float nomber](http://blog.dcview.com/article.php?a=VmhQNVY%2BCzo%3D)
###### tags: `TechReport` `DeWei`