1

I'm making a simple electric circuit simulator. It will (at least initially) only feature batteries, wires and resistors in series and parallel. However, I'm at a loss how best to simulate said circuit in a good way.

Specifically, I will have batteries and resistors with two contact points each, and wires that go between two contact points. I assume that each component will have a field for its resistance, the current through it and the voltage across it (current and voltage will, of course, be signed). Each component is given a resistance, and the batteries are given a voltage. The goal of the simulation is to assign correct values to all the other fields in real time as the player connects and disconnects components and wires.

These are the requirements:

  • It must be correct, including Ohm's and Kirchhoff's laws (I'm modeling real world circuits, and there is little point if the model does something completely different)
  • It must be numerically stable (we can't have uncontrolled oscillations or something just because two neighbouring resistors can't make up their minds together)
  • It should stabilize relatively quickly for, let's say, fewer than 30 components (having to wait a few seconds before the values are correct doesn't really satisfy "real time", but I really don't plan on using it for more than 10 or maybe 20 components)
  • The optimal formulation for me (how I envision this in my head) would be if I could assign a script to each component that took care of that component only, possibly by communicating field values with neighbouring components, and each component script works in parallel and adjusts as is needed

I only see problems here and no solutions. The biggest problem, I think, is Kirchhoff's voltage law (going around any sub-circuit, the voltage across all components, including signs, add up to 0), because that's a global law (it says somehting about a whole circuit and not just a single component / connection point). There is a mathematical reformulation saying that there exists a potential function on the points in the circuit (for instance, the voltage measured against the + pole of the battery), which is a bit more local, but I still don't see how to let a component know how much the voltage / potential drops across it.

Kirchhoff's current law (the net current flow into an intersection is 0) might also be trouble. It seems to force me to make intersections into separate objects to enforce it. I originally thought that I could just let each component have two lists (a left list and a right list) containing every other component that is connected to it at that point, but that might not make KCL easily enforcable.

I know there are circuit simulators out there, and they must have solved this exact problem somehow. I just can't find an explanation because if I try googling it, I only find the already made simulators and no explanations anywhere.

Arthur
  • 653
  • 2
  • 6
  • 21
  • Your problem results in a linear system which is sparse. So you will need a matrix-vector library and use its solver, preferably using sparse data structures and algorithms. -- If you include more active elements then you get a differential-algebraic system. Study Modelica for one approach to that. – Lutz Lehmann Mar 24 '17 at 08:29
  • @LutzL That might work. With few enough components, I probably wouldn't even need dedicated sparse structures and algorithms. However, it is not the kind of solution I primarily look for. To me, the interesting part of this problem is how to simulate a single component correctly, and apply that to each component. – Arthur Mar 24 '17 at 09:40
  • So you want to simulate the electrical field that travels with the speed of light (specific to the medium) or some approximation thereof to simulate the change from the steady state "switched off" to the steady state "switched on"? – Lutz Lehmann Mar 24 '17 at 12:29
  • @LutzL Well, it won't travel at the speed of light in that case, unless you mean "light speed" as in Conway's game of life or something (i.e. the change propagates one component per update). In other words, what I would _love_ to do is to make it a cellular automaton (if that is an appropriate phrase... maybe "componental automaton"). When I think about it that way, it is kind of obvious that it will stabilize slowly... So I might just swallow my pride and do it the linear equation way after all. – Arthur Mar 24 '17 at 12:48
  • No, I mean "light" light, as in electromagnetical waves like a changing current travel at the speed of light. Which is different in copper than in vacuum. But then if you do that seriously we would be talking about solving wave equations, which is overkill for this topic. -- And yes, there are non-physical ways to iterate towards an equilibrium solution. But guaranteeing contractivity of the method might be harder than solving the linear system. – Lutz Lehmann Mar 24 '17 at 13:21

0 Answers0