Simulating circuits - Page 1
Forum section: electronics / general

author: vlad777
author_rank: Frequent Contributor
timestamp: March 10, 2019, 03:06:41 am
content: 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 matricesI 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.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: March 10, 2019, 03:42:19 am
content: It's a numerical solution of differential equations.The difference equations arise from the linear components:V = R * IV = L * dI/dtI = C * dV/dtFor 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

author: T3sl4co1l
author_rank: Super Contributor
timestamp: March 10, 2019, 03:45:17 am
content: You may find this of interest,https://www.seventransistorlabs.com/Calc/Filter1~.htmlit'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

author: vlad777
author_rank: Frequent Contributor
timestamp: March 10, 2019, 03:59:47 am
content: 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?

author: metrologist
author_rank: Super Contributor
timestamp: March 10, 2019, 04:22:10 am
content: Isn't that math stuff for the interns? I use LTspice.

author: vlad777
author_rank: Frequent Contributor
timestamp: March 10, 2019, 04:27:02 am
content: Quote from: metrologist on March 10, 2019, 04:22:10 amSorry I am not talking about using simulators.My goal is programing something like but simpler, like thishttps://www.falstad.com/circuit/

author: Zero999
author_rank: Super Contributor
timestamp: March 10, 2019, 10:37:47 am
content: Quote from: vlad777 on March 10, 2019, 04:27:02 amI'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/

author: Shock
author_rank: Super Contributor
timestamp: March 10, 2019, 10:40:47 am
content: Quote from: vlad777 on March 10, 2019, 04:27:02 amThe source to the Falstad one can be found on this page.https://www.falstad.com/circuit-java/

author: HalFET
author_rank: Frequent Contributor
timestamp: March 10, 2019, 11:36:51 am
content: 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/1050166https://www2.eecs.berkeley.edu/Pubs/TechRpts/1973/ERL-382.pdf

author: T3sl4co1l
author_rank: Super Contributor
timestamp: March 10, 2019, 03:29:54 pm
content: Quote from: vlad777 on March 10, 2019, 03:59:47 amYou 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