### Author Topic: Simulating circuits  (Read 297 times)

0 Members and 1 Guest are viewing this topic.

• Frequent Contributor
• Posts: 295
• Country:
##### Simulating circuits
« on: March 10, 2019, 02:06:41 pm »

This page I believe is one guy's DIY project. I don't know It just looks like that.

So how do you simulate a circuit?
Do you build a incidence matrix of differential equations, then use Monte-Carlo to solve integrals, and Gauss-Jordan to invert matrices

I believe that basic LCR simulation (with various source frequencies ) is not too hard.

Do you have a video or link to recommend?
I had hopes in this one ,but in the end he says nothing.

Mind over matter. Pain over mind. Boss over pain.
-------------------------

#### T3sl4co1l

• Super Contributor
• Posts: 12452
• Country:
• Expert, Analog Electronics, PCB Layout, EMC
##### Re: Simulating circuits
« Reply #1 on: March 10, 2019, 02:42:19 pm »
It's a numerical solution of differential equations.

The difference equations arise from the linear components:
V = R * I
V = L * dI/dt
I = C * dV/dt
For each node-to-node voltage V and branch current I, of each component respectively (R, L, C).

You build a matrix of node voltages and branch currents, and invert through whatever methods.  SPICE uses LU reduction I think.  Finally, you get the node voltages for a given timestep, add the differences, and repeat.

That solves general, linear circuits for transient results, but these aren't so interesting, as analytical and frequency-domain results are available much faster (and often much more useful).

To do that, to solve in the frequency domain, use the Fourier or Laplace transform of the RLC components (e.g., V = j*omega*L*I), solve the node equations, and you're done.  For arbitrary sources, transform them as well, and use superposition to solve for the result in the circuit.  Finally, if time domain results are desired, apply inverse transform and solve for initial conditions.

This is SPICE's AC analysis, where the node equations are solved at a single (given) frequency.  Usually a sweep of frequencies.  The inverse transform is not used.

That's the easy part.  The hard part is considering nonlinear elements: diode junctions, transistors, that sort of thing.

SPICE handles this by finding an operating point, solving for the incremental resistance of each element at that point (i.e., R_incr = dV/dI), and iterating until the error falls below the various tolerances (RELTOL, VTOL, CHGTOL..).  This solves for the node voltages and currents in the timestep.  The next timestep is calculated, and the process repeats.  If the error isn't falling (convergence failure), timestep is reduced and iteration is attempted again.  This makes the points much more dense around regions of rapid change.  There are some other stability tricks, source and Gmin stepping for example, and other integration methods (trapezoidal (~Newton's method), and Runge-Kutta (order 2+)) also used, or available.

Falstad simulator, as far as I know, uses a fixed timestep and Newton integration, which is very easy to code, reasonably stable for simple circuits, and still pretty good with some badly behaved systems (of note, integrating an event-driven digital simulator into it isn't too bad; not sure if that's what's used, but it seems likely).  It's also that much more fragile when it comes to numerical stability.  It's fairly easy to create a circuit which diverges, producing exponential outputs (which the viewer is more than happy to show climbing to ridiculous values for you).

Two commonly seen convergence issues arise from this:
- If there are undefined nodes (e.g., capacitors or current sources in series), or loops (inductors or voltage sources in parallel), the fact that that variable is undefined, manifests as a singular matrix.  Attempting to invert a singular matrix is analogous to dividing by zero.  Matrices are just richer in the ways they can behave like zero, and this is one such case.  Solution: add a leakage resistor from the node to a nearby node, or ESR to the loop.  SPICE has the RSHUNT parameter to do this automatically; but if you use ~safe large values (e.g., 1GΩ), it may converge but go really slowly.
- If the nonlinearity just can't be fitted (by reducing timestep), it will eventually give up and say "timestep too small".  (Or it won't at all, as LTSpice tends to do, IIRC.)  Try loosening tolerances, or adding parasitics to the circuit (you may be attempting to simulate a nonphysical circuit -- this is especially important, say, in switching power supplies).  Avoid discontinuous or piecewise functions (e.g., switches, IF or TABLE expressions, EXP without LIMIT, etc.).

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!

The following users thanked this post: GlennSprigg

#### T3sl4co1l

• Super Contributor
• Posts: 12452
• Country:
• Expert, Analog Electronics, PCB Layout, EMC
##### Re: Simulating circuits
« Reply #2 on: March 10, 2019, 02:45:17 pm »
You may find this of interest,
https://www.seventransistorlabs.com/Calc/Filter1~.html
it's an AC steady state solution (just like SPICE's AC analysis), for a linear network (like a filter ladder), using 2-port transmission (ABCD) matrices.  The source is right there, it's HTML+CSS+JS.

Been meaning to fix a number of things on there... also noticed the tooltips are broken in newer Chrome.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!

• Frequent Contributor
• Posts: 295
• Country:
##### Re: Simulating circuits
« Reply #3 on: March 10, 2019, 02:59:47 pm »

For the beginning I would be satisfied with transient analysis.

How do you "build a matrix of node voltages and branch currents"
and why do you invert?
Mind over matter. Pain over mind. Boss over pain.
-------------------------

#### metrologist

• Super Contributor
• Posts: 1724
• Country:
##### Re: Simulating circuits
« Reply #4 on: March 10, 2019, 03:22:10 pm »
Isn't that math stuff for the interns? I use LTspice.

• Frequent Contributor
• Posts: 295
• Country:
##### Re: Simulating circuits
« Reply #5 on: March 10, 2019, 03:27:02 pm »
Isn't that math stuff for the interns? I use LTspice.

Sorry I am not talking about using simulators.
My goal is programing something like but simpler, like this  https://www.falstad.com/circuit/
Mind over matter. Pain over mind. Boss over pain.
-------------------------

#### Zero999

• Super Contributor
• Posts: 11869
• Country:
• Hero999
##### Re: Simulating circuits
« Reply #6 on: March 10, 2019, 09:37:47 pm »
Isn't that math stuff for the interns? I use LTspice.

Sorry I am not talking about using simulators.
My goal is programing something like but simpler, like this  https://www.falstad.com/circuit/
I'd suggest learning how to use a simulator, so you can understand how it works, before trying to write one.

I doubt it's worth wring your own simulator, unless it's purely for educational purposes. If you have a requirement for a simulation engine in some software you're developing then use the free SPICE engine.
http://bwrcs.eecs.berkeley.edu/Classes/IcBook/SPICE/

#### Shock

• Super Contributor
• Posts: 2449
• Country:
##### Re: Simulating circuits
« Reply #7 on: March 10, 2019, 09:40:47 pm »
Sorry I am not talking about using simulators.
My goal is programing something like but simpler, like this  https://www.falstad.com/circuit/

Multimeters: Fluke 87V, 117, 27/FM
Oscilloscopes: Rigol DS1054Z, Phillips PM3065

#### HalFET

• Frequent Contributor
• Posts: 451
• Country:
##### Re: Simulating circuits
« Reply #8 on: March 10, 2019, 10:36:51 pm »
Writing your own is great fun though, no need to discourage people if they're not doing this for a professional reason.

The lumped element passives are trivial, you can simply generate the equations and solve it using any direct linear solver. You can also add linear controlled sources without really digging into the realm of numerical solvers, but then you have to be careful with how you put generate your matrices to ensure you can still solve them. The issue is that once you accurately wish to represent a circuit you really need more complex models, the academic papers written about CANCER and SPICE are a pretty good starting point to find out about those:
https://ieeexplore.ieee.org/document/1050166
https://www2.eecs.berkeley.edu/Pubs/TechRpts/1973/ERL-382.pdf

#### T3sl4co1l

• Super Contributor
• Posts: 12452
• Country:
• Expert, Analog Electronics, PCB Layout, EMC
##### Re: Simulating circuits
« Reply #9 on: March 11, 2019, 02:29:54 am »

For the beginning I would be satisfied with transient analysis.

How do you "build a matrix of node voltages and branch currents"
and why do you invert?

You start with a circuit representation, of some sort or another.  A schematic is a good graphical representation, and a netlist is a good textual representation.  Creating a schematic entry is a task all its own!

SPICE's netlisting also allows recursive (hmm, to a point?) (but not self-recursive) structure, by way of subcircuits.  Internally, this is expanded and flattened into a single netlist, which is turned into the matrix.

The matrix consists of row/column indices corresponding to nets in the netlist, probably sequential based on order of appearance; order doesn't matter, as long as you retain a list of which ones are which.

The value at each cell of the matrix is the current flow through that branch, i.e., from the row net to the column net (or vice versa, whichever way it is).  Thus, most nets do not share connections, so most cells are zero, i.e., the matrix is usually sparse.  (This allows other optimizations when dealing with large (100s square) matrices.)

What's being solved is the problem A*x = b, where b is the vector of estimated node voltages, and x is the improved estimate, or the next timestep -- or something like that, I forget exactly.  In any case, a linear algebra solution is needed, and matrix inversion is the way to solve this.

The matrix is typically symmetrical, corresponding to the condition of reciprocity.  Some elements are nonreciprocal, for example dependent sources and transistors.  Dependent sources are explicit connections from one net (or many) to another; transistors are similar, but with more tweaks added for realism (like capacitance).

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!

Smf