Author Topic: Simulating circuits  (Read 2129 times)

0 Members and 1 Guest are viewing this topic.

Offline vlad777Topic starter

  • Frequent Contributor
  • **
  • Posts: 350
  • Country: 00
Simulating circuits
« on: March 10, 2019, 03:06:41 am »
https://www.falstad.com/circuit/

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.
-------------------------
 

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21684
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Simulating circuits
« Reply #1 on: March 10, 2019, 03:42:19 am »
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

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21684
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Simulating circuits
« Reply #2 on: March 10, 2019, 03:45:17 am »
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!
 

Offline vlad777Topic starter

  • Frequent Contributor
  • **
  • Posts: 350
  • Country: 00
Re: Simulating circuits
« Reply #3 on: March 10, 2019, 03:59:47 am »
Thanks for the reply!

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.
-------------------------
 

Offline metrologist

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

Offline vlad777Topic starter

  • Frequent Contributor
  • **
  • Posts: 350
  • Country: 00
Re: Simulating circuits
« Reply #5 on: March 10, 2019, 04:27:02 am »
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.
-------------------------
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19520
  • Country: gb
  • 0999
Re: Simulating circuits
« Reply #6 on: March 10, 2019, 10:37:47 am »
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/
 

Online Shock

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

The source to the Falstad one can be found on this page.
https://www.falstad.com/circuit-java/
Soldering/Rework: Pace ADS200, Pace MBT350
Multimeters: Fluke 189, 87V, 117, 112   >>> WANTED STUFF <<<
Oszilloskopen: Lecroy 9314, Phillips PM3065, Tektronix 2215a, 314
 

Offline HalFET

  • Frequent Contributor
  • **
  • Posts: 512
  • Country: 00
Re: Simulating circuits
« Reply #8 on: March 10, 2019, 11:36:51 am »
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
 

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 21684
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Simulating circuits
« Reply #9 on: March 10, 2019, 03:29:54 pm »
Thanks for the reply!

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!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf