spice - circuit simulation usage of standard spice, and internals of circuit analysis and simulation Entry: Writing a circuit simulator Date: Thu Oct 16 16:20:05 EDT 2014 Mostly to gain some insight into discretization + some practical incentive to do "boutique" analog design. - Set point analysis - Linearized impedance / transfer functions How to make this work? One thing that seems important is to figure out how to remove infinities due to ideal components. E.g. opamps with finite gain and GBW. Can this be done through abstract interpretation? Entry: Circuit simulation Date: Tue Oct 21 11:04:44 EDT 2014 Been thinking about impedance and circuits. Some conclusions: - It's possible to move nonlinearities into the temporal domain as long as they are smooth enough. I.e. a diode becomes (d = unit delay): I[k]/I_s = e(V[k-1]/V_T) {1 + (V[k]-V[k-1])/V_T} I.e. the nonlinear relation is past->now, not now<->now. - For circuits, nonlinearity is essential (cannot be decoupled). I.e. we need to keep that matrix and solve it. Open questions: - In what form should equations be specified? - How to translate equations into matrix form, i.e. separate variables from coefficients and linearize nonlinear components. - Should voltages be floating or is a common ground assumed? -> NO Start with abstraction! Equations should be specified as n-ports. A system that is solvable is a 0-port. E.g. system of interest + input source. Entry: Circuit editor Date: Tue Oct 21 18:47:10 EDT 2014 It would be so cool to make this into a real-time circuit editor that produces sound on the fly. Entry: Specification language Date: Tue Oct 21 20:19:11 EDT 2014 A first draft: an RC filter. All nodes are explicit. It is up to the user to not connect them. A non-connected node is a node connected to a 0 current source. Current flows into the node from the outside of the n-port. n-ports do not have hidden supply nodes, so \sum I = 0 always holds. Does it need to be explicit? (1) (nport (terminals (v_in i_in) (v_out i_out) (v_gnd i_gnd)) (parameters (R C)) (equations (+ i_gnd i_in i_out) ;; (1) (+ i_gnd (* C (d/dt (- v_out v_gnd)))) (- i_in (* R (- v_in v_out))) )) 6 unknowns, 3 degrees of freedom left. A full connection would be: v_in = x input voltage v_gnd = 0 common ground i_out = 0 no output load TODO: identify state variables. A problem is expressions like (d/dt (- v1 v2)). Those need to be made explicit, probably by adding extra unknowns. Entry: Serializing equations Date: Wed Oct 22 09:16:10 EDT 2014 Given a set of equations: - For each d/dt, make sure the variable is naked. Otherwise add new variables and an extra equation. - Equations have a set of inputs. Separate parameters from variables. - Solve set of equations - Repeat Looks like this will need some appropriate data structure traversals. Tree processing is always the big hurdle, so let's do it right this time. mapAccum[1] seems appropriate: process datastructure + update accumulator. however, when this is a tree, there seems to be a conflict between substitution and traversal. i.e. when a node processing function prunes a subtree, should the old one be traversed still? Let's bypass the d/dt rewriting and just assume d/dt operates on naked variables first. [1] http://www.haskell.org/hoogle/?hoogle=mapAccum Entry: Simulating circuits Date: Sun Jun 1 14:57:16 EDT 2014 - DC analysis - AC analysis - Full step simulation What is most useful? DC/AC as it determines design. One of the difficulties I experience is fixing impedance of a circuit. Due to dependencies it usually requires a couple of iterations. As for writing somethings myself, the question is: is this useful? PRO: - something to learn - allows DSP modeling of analog circuits CON: - takes time - will not be as good as spice - learning spice might be a good investment Is DC analysis just relaxed full simulation? Entry: Porting analog Date: Fri Jun 13 09:16:12 EDT 2014 Been working on analog circuits for a bit. Very interesting, but also very messy. Slow process. I'd like to find a way to boost the process by formalizing at least the small-signal portion of a circuit. I wanted to get into spice but really it seems a bit awkward to me. Better to find a good way to express circuits as code/functions and then have it generate spice input? Entry: Splitting d/dt Date: Fri Oct 24 13:36:21 EDT 2014 This equation expression (+ i_gnd (* C (d/dt v_c))) ;; = 0 needs to be transformed into an update. The update comes straight from the naked expression for the derivative so to normalize, expand everything to sum of products and bring the d/dt term to the other side. to solve the other equations, simply do not take this into account This is what comes out atm racket@test-impedance.rkt> (info p) ((variables (i_out v_in i_in i_gnd v_gnd v_out v_c)) (derivatives (v_c)) (equations ((algebraic ((- v_c (- v_out v_gnd)) (- i_in (* R (- v_in v_out))) (+ i_in i_out i_gnd))) (diff ((+ i_gnd (* C (d/dt v_c)))))))) So.. for the algebraic instantaneous equations, the variables that have differentials are actually parameters. Now it might be easier to start with normal forms. Later maybe add sugar on top but that's probably not necessary if the NFs are clear. Something like this dif: (v e) -> (= (d/dt v) e) ;; e is any function of vars, params alg: (c (c1 x1) ...)) -> (= c (+ (* c1 x1) ...)) ;; c, c1, ... are coefficients (can not depend on x1 ...) (nport (terminals (v_in i_in) (v_out i_out) (v_gnd i_gnd)) (parameters R C) (differential (v_c (/ (- i_gnd) C))) (algebraic (0 (-1 v_c) (1 v_out) (-1 v_gnd)) (0 (-1 i_in) ((* -1 R) v_in) (* R v_out)) (0 (1 i_in) (1 i_out) (i_gnd)))) So basic idea is that: - all linear variables are exposed - all differential variables are exposed For non-linearity, it seems that just computing the coefficients through automatic differentiation is already enough to perform linearized approximation. I wonder if the whole thing can then be solved using iterative methods as well, since the solution doesn't change much from point to point. I.e. no matrix inversion. Entry: Circuit simulation cont.. Date: Fri Oct 24 22:46:25 EDT 2014 So if nonlinearities are approximated linearly and time steps are small, the essential step (= solving the connected matrix equation) will be of an update nature as well. So why not use an interative method[1][2] to solve the matrix inversion? Since the system will be sparse, this will probably be quite efficient. It might be necessary start from a true solution but tracking should be straightforward.. [1] http://en.wikipedia.org/wiki/Biconjugate_gradient_method [2] http://en.wikipedia.org/wiki/Iterative_method#Linear_systems Entry: About circuit simulation Date: Fri Oct 24 23:07:13 EDT 2014 What method am I re-inventing? The problem is a set of non-linear differential equations with non-linear (equality) constraints. Something like this[1]. Numerical Solution of Nonlinear Differential Equations with Algebraic Constraints.. EDIT: The question to ask (or to find the right formulation for) is what is so different about nonlinear ODEs when you couple them with linear or nonlinear algebraic equations as compared to algebraic equations or straight up ODEs where all unknowns have derivatives? Maybe trying to re-invent isn't the best idea here. But I'd love to gain more insight by taking an autodiff approach to this problem. Tools are readily available in rai. Some faint bell... In [3] it is mentioned that equtions built up from KCL can't be solved linearly if there are voltage sources. Therefore Norton equivalent current sources are used. ( See next post ). This hints at me naive in placing I and V on the same footing, hoping for linear equations. Is this so? I didn't actually check that. ( EDIT: There is no problem: equations are linear in I and V. ) The other interesting question: where does the asymmetry come from? Any topological reason there why it's not neatly dual? Following hunches here, but it looks like because spice is so specific, the somewhat general approach I'm taking is not going to cut it. Maybe first it's best to implement a minimal spice? Ok, wait [3] is just for DC and AC analysis.. Transient analysis is different. [1] http://epubs.siam.org/doi/abs/10.1137/0907049 [2] https://ccrma.stanford.edu/~dtyeh/papers/yeh07_dafx_distortion.pdf [3] http://www3.imperial.ac.uk/pls/portallive/docs/1/7292571.PDF [4] http://books.google.com/books/about/Inside_SPICE.html?id=oOVSAAAAMAAJ Entry: Spice DC analysis, voltages sources and Thevenin equivalent circuit Date: Mon Oct 27 11:20:20 EDT 2014 In [1] it is mentioned that - Current sources and resistors mix very well as they give simple linear systems in terms of node voltages when Kirchhoff's Current Law (KCL) is used for every node. - The presence of a voltage source messes up this pretty picture. See page 10 in [1]. I vaguely remember running into this in class, but thinking about it, the statement "we cannot form the matrix equations" seems a bit hard to believe. What exactly is the limitation? Surely there are 3 linear equations in 3 unknowns (I1, V2, V2) in Figure 2. I guess the point is here that when all voltage sources are replaced with current sources, the problem is a lot easier to state since all node voltages are variables and for each node there is a KCL equation. No need to keep track of which voltages are variables and which are parameters. [1] http://www3.imperial.ac.uk/pls/portallive/docs/1/7292571.PDF Entry: Loops and nodes Date: Mon Oct 27 11:39:32 EDT 2014 Kirchhoff's laws: - current: sum I = 0 at node - voltage: sum V = 0 over loop Circuit topology: is #loops = #nodes ? No, they are completely independent. How does this relate exactly to circuit duality? Entry: Circuit simulation: current status Date: Mon Oct 27 12:01:13 EDT 2014 Ordered the spice book[1] for some inspiration. I find it strange I cant find a canned solution to how to solve nonlinear ODEs with nonlinear algebraic constraints. Currently I am a little frustrated to not find a good way to look at this, so probably some code needs to be written first to bring the details into focus.. [1] http://books.google.com/books/about/Inside_SPICE.html?id=oOVSAAAAMAAJ Entry: Resistors and current sources Date: Tue Nov 4 22:50:44 EST 2014 Spice book arrived. Let's have a closer look at this resistors + current sources approach. Reading chapter 2, all matrices that appear are symmetric. This can't be a coincidence. See next post. Reading on, it talks about spice linearizing diodes as current sources in parallel with resistors. I=I0+GV Same for dynamic elements, which I find surprising. This seems to be a clear difference from the approach I was thinking about. Or maybe not? See figure 2.14 Actually it is not that different, except for: - change of variables (GV = I) - multiple iterations for a single time point for NL els - several integration methods The GV=I form seems better as it reduces the number of variables: only node voltages. Currents are derived from voltages. The idea to "rotate" nonlinear elements into time should still work, making the computation at a particular time step linear. Since the circuit is symmetric and sparse, CG can be used if it's positive definite (probably is.. indeed i found some mention online. but why?) It might be good to find out if using V only (instead of both V and I) creates bad numerical condition. The spice book already mentions diodes with high currents (small dV -> large dI). Maybe in such cases a change of variables makes sense? ( eigencircuit? see next posts ) [1] http://books.google.com/books/about/Inside_SPICE.html?id=oOVSAAAAMAAJ Entry: Symmetric conductivity matrix Date: Wed Nov 5 00:05:11 EST 2014 Trying for a maximally connected 3-node network 0,1,2. Node 0 is the ground node, giving two variables V10, V20. The effect of a current source connected between node 1 and 2 can be absorbed into current sources from ground to nodes 1,2. This gives the following equations: G V = I (G10 + G12) -G12 V10 I01 -G12 (G20 + G12) V20 I02 With Gxy conductivity between node x and y. Ixy current flowing from x to y Vxy voltage drop from x->y Where does this symmetry come from? Essentially from the symmetric relation "connected to". I.e. a resistor doesn't have any sense of direction. This generalizes to a_xx = Gx0 + \sum_y!=x Gxy a_xy = -Gxy (x != y) Entry: Why is a conductance matrix positive definite? Date: Wed Nov 5 01:32:50 EST 2014 G V = I or V = G^-1 I Given a network of resistors and current sources, compute the node voltages. The inverse problem is: given a network of resistors and voltage sources, compute the currents drawn from the voltage sources applied to each node. The matrix G is positive definite if V^T G V > 0 for each V, meaning V^T I > 0 or the total power dissipation is positive, which is clear from physical properties. So the this also answers a previous question: The quadratic form associated with the symmetric conductance is the power dissipation of the resistor network. Does it work in the other direction? Can a positive definite quadratic form be written as a conductance matrix? Probably not as there is a structural constraint. A more interesting question to ask is probably: what are the eigenvectors / values? Also, once transconductances are introduced, the symmetry breaks. Entry: Changing variables Date: Wed Nov 5 02:13:59 EST 2014 G V = I Suppose for a circuit we're not just using current inputs, but a couple of the nodes will have set voltages, how to change variables? In SPICE this is done using Norton equivalent transformation. It should be straightforward to make this change of variables, but what about the resulting matrix? How to express that transformation? Is the matrix still pos def? Entry: Rethinking circuit representation Date: Wed Nov 5 10:25:45 EST 2014 I'm starting to understand the spice rationale[1]. The basic model is that of a resistive network with one node dedicated as ground node. Equations are set up using KCL for each non-ground node. This gives a relation between node voltages and currents flowing into each node: G V = I This is just a relation. It can be interpreted as currents drawn from voltage sources, or node voltages caused by current sources. The matrix G is symmetric, positive definite. - symmetric: a resistor has no directionality - positive definite: V^T G V = V^T I = P, total power is always > 0 Starting from this basic model, SPICE linearizes the equations and performs NR iterations at a particular time instance, and uses one of several integration methods to move forward in time. The structure of G allows for Krylov subspace methods (conjugate gradient, lanczos) [1] entry://../math/20141105-000511 Entry: Circuit: simulation and impedance tuning Date: Tue Jan 5 16:39:00 CET 2016 I'd like to approach circuit design as a programming problem. Meaning: - Formalize circuit capture in a data structure (initial) or program (final). - Perform DC / AC analysis - Find a way to tune for impedance First step is to compute the conductivity matrix, as is used in SPICE. Compute it as code, to perform simulation through automatic differentiation. Questions: - Why does spice use constant current source, conductivity matrix, node voltages as unknown, instead of the dual? - Does it really matter? Does it make sense to use a representation that has both primal and dual representations? What is a circuit? It is related to a nonlinear manifold. Its affine approximation is either: - conductivity matrix + constant current sources - resistance matrix + constant voltage sources Until it is clear what the reason is to pick one of these, work with the dual representation. Is there a mathematical object that uses this? Projective geometry? Entry: Projective geometry Date: Tue Jan 5 16:51:04 CET 2016 Is there a point in looking at circuits through the lens of projective geometry, to avoid infinities that would otherwise appear? E.g. infinite conductivity (short), infinite resistance (open)? The affine view (one inverse of the other): - G V = I - R I = V Trouble is that some of these might be singular, caused by shorts or open. In the standard approach, those shorts and opens are structurally removed. Does it actually make sense to have infinite current and voltage (compare to point at infinity in computer graphics, projective geometry)? No, but it does make sense to have infinite derivatives. Is there a way to represent this while the coefficients are elements of a projective space? E.g. the derivative (actuall, first order approximation) is a projective space. Actually, this problem goes away when looking at the manifold in an abstract way. It only appears in representation. Is that right? The tangent space of a manifold is a vector space, but is there a "condition" problem there? Is it possible that the actual space is degenerate? It seems that the problem is present at the abstract manifold level. Is this in any way related to sympectic manifolds? E.g. do current and voltage (or some kind of composite object) need to appear both as unknowns? What is the integral of a projective space? Entry: Projective view of a circuit node Date: Tue Jan 5 17:17:49 CET 2016 A dual representation of a circuit node has coordinates: - V : voltage of the node (wrt to ground) - I : current imbalance Either of the two dual views will see this either in terms of - voltage sources: setting the voltage, absorbing the current - current sources: setting the current, absorbing the voltage Is there a way to look projectively at a source? Instead of picking a view, is there a way to keep the symmetry and look at it as an entitiy in itself? Again, it is not about the V,I but about the conductivity/resistance. One thing though: most circuits are 2/3 terminals. These have inverses that are easier to express, e.g. to maintain a dual representation. Can this be used at all? It seems a lot of structure is lost when using full matrices. Circuit matrices are fairly sparse. Actually that's not relevant: sparsity is related to number of connections to a single node, not the properties of edges. Entry: Circuit dualities Date: Tue Jan 5 18:05:37 CET 2016 Before finding a good representation that can switch between dualities, first name all symmetries present in a circuit (simulation): - Conductance / Resistance (voltage / current) - Node / edge duality - s and 1/s duality Entry: Spice and G V = I Date: Tue Jan 5 18:14:32 CET 2016 See prev post. (Nov 4). Regarding projective view: it doubles the number of variables. This might not be wise w.r.t. computation. Still, depending on the "mode" an algorithm is in, it might still be wise to allow both G and R to be computed without actually computing inverses. The best representation of a matrix is as code. (Piponi on Twitter?) If the local "clustering" can somehow be maintained, this it might be possible to do primal/dual approaches for computing CG updates. Entry: Modeling caps as current sources Date: Fri Jan 8 12:38:32 CET 2016 Dynamic elements modeled as current sources in parallel with resistors. How can this work? For an inductor, it makes sense. For a capacitor, it is necessary to use norton/thevenin translation. This seems inherently very ill-conditioned. This worries me especially, since most of the circuits I'm interested in have caps instead of inductors. Why was this choice made in the first place? (G,I_0) vs (R,V_0). To understand this, it is necessary to look at the condition of the equations, possibly transform them to an inherently more stable form. Entry: Spice: taking a stab at working with equations Date: Tue Feb 16 18:19:12 EST 2016 Two things need to be distinguished: - Primitive equations that relate currents, voltages and time - Spice's GV=I approach. I'm currently more interested in building a good representation of the equations, and then find mappings of that that will answer questions. E.g. sensitivities: effect of circuit parameter on some computable property. The full equation is a first order, non-linear differential equation. The state variables can be currents and voltages. However, they generally are voltages, as inductors are rare and can be omitted on the first try. ( And could be replaced by gyrators? ) ( Re-reading from october 2015. Essentially going through the same thing. ) Entry: circuit simulation Date: Wed Feb 17 11:22:14 EST 2016 TODO: - Equations: Look at previous posts. There are some good ideas there + some code. - Make ngspice/gnucap work (add models) Entry: Got lost in thinking about graphs... Date: Wed Feb 17 18:04:40 EST 2016 Not easy. I'm missing a lot of intuition. It's not clear how to think of circuits graphs, since there are terminals and nets, which have more structure than graphs (nodes, edges). Circuits are terminals and nets. It's helpful to draw a fully connected circuit and replacing electrical symbols with "blank" 2 and 3 terminals. 2-terminals can be represented by edges. Such a graph is not simple (i.e. parallel connections possible). However, parallel connections can be eliminated by using composite objects. The result is then a simple graph. Using norton/thevenin transformation, even further reduction is possible (eliminate serial connections) until a single norton/thevenin circuit remains. When 3-terminals enter the picture, this is no longer possible. Modeling 3-terminals as a collection of 2-terminals and some extra equations (gyrators?) is possible but will "halt" the ability to construct equivalent circuits, as that is only possible for linears.. Are circuits planar graphs[1]? Not necessarily. https://en.wikipedia.org/wiki/Planar_graph For circuit analysis, a problem that arises is which loops to pick to determine independent KVL equations. Planar graphs seem to make this easier (as it is easier to construct the dual?) Circuits (networks) are n-terminals connected to nets. They are not graphs per se (in the mathematical sense; objects made of nodes and links). Circuits of linear 2-terminals can be combined to a single 2-terminal in the Laplace domain. Circuits of non-reactive nonlinear 2-terminals can be combined in a similar way into a single current/voltage relation. Once nonlinear and active are combined, the behavior becomes more complex and cannot be condensed into functions: systems of nonlinear differential equations arise. N-ports, N>2 complicate matters. However in the application of interest (analysis of diode + bjt circuits), these can be decomposed into networks of 2-terminal devices with extra current/voltage relations (Ebers-Moll, with 2 current relations). So the objects we're working with are: - networks of 2-terminals - KCL, KVL - passive static (R) and dynamic (C,H) relations - current/voltage controlled current/voltage sources Such a circuit is a non-simple, directed graph, where every edge represents a 2-terminal. Entry: Drawing equivalent circuits. Date: Wed Feb 17 19:23:23 EST 2016 Since equivalent circuits are all 2-terminals, they are proper non-simple graphs, so might be drawn with graphviz. To make this pretty, distinguish nodes and 2-terminals, and draw 2-terminal edges as edge->box->edge. http://stackoverflow.com/questions/29768453/how-to-use-graphviz-and-racket Entry: KVL vs. KCL Date: Wed Feb 17 19:35:44 EST 2016 I wonder, is KVL actually necessary? By assigning a voltage to every node, there is no way to make that not work. So it seems spice uses KCL, and not KVL. The reason? Likely because it is easy to build equations from nodes (that information is right there in the netlist, as opposed to having to analyze the graph to find loops). At least that is what is inferred here [1]. A consequence of that is that it is not possible to have isolated voltage sources: they always need to be translated to current source + series resistor. Leads to the question: is it possible to partially invert this? E.g. use a graph transformation to partially dualize? ( I went here before... ) G v = i Where v are the node voltages, and i are the node currents injected into each node. It seems that this is just a re-organization of the equations: [ G -I ] [ v i ]^T = 0 Into [ A B ] [x c]^T = 0 or Ax + Bc = 0 Where x denotes the unknowns and c denotes the known current/voltage sources. So essentially a simple problem. [1] http://electronics.stackexchange.com/questions/19198/kcl-vs-kvl-in-circuit-analysis [2] http://ltwiki.org/?title=Pure_Inductor_and_Voltage_Source_Modelling_%28and_how_to_speed_up_simulations_with_them%29_using_LTspice Entry: Summary: why spice looks like it does Date: Wed Feb 17 21:50:26 EST 2016 - KCL gives an easy way to dervice equations from a netlist: one equation per node, except ground node. - This expresses connectivity as an admittance (conductance) matrix G, each element corresponding to the conductance of a single resistor. - To deal with ideal voltage sources, the equations can be re-arranged from Gv=i, [G|-1][v|i]^T=0 form to Ap+Bs=0, where p contains all variabiable voltages/currents and s contains all voltage/current sources. - If G represents the connectivity of the network, each element in G, if not a static resistor, is a current linear approximation of dynamic and nonlinear effects. Offsets are determined by voltage, current sources. Entry: Spice companion models for reactive elements Date: Wed Feb 17 22:42:58 EST 2016 Capacitor = current source (par) resistor Inductor = voltage source (ser) resistor Weird, because I would assume the other way around. Why? I = C dV/dt Aproximated, with T timestep. I(k) = (C/T) V(k)-V(k-1) = C/T V(k) - V(k-1)C/T = g(k) V(k) - i(k) So clearly resistor (par) current source. Entry: Analysis help Date: Wed Feb 17 23:02:45 EST 2016 So last two days were fun, but let's get into the point of the matter. What am I trying to accomplish? 1. Short term: assist with designing hardware analog circuits 2. Longer term: find a strategy to convert circuits to executable, real-time C code. Leaving the longer term goal for later when more insight has been gained, what would be an analysis step to perform? For the SPUT VCF circuit, some important things are: - Verify principal correctness - Tune Impedances - Tune gains - Tune DC setpoints