Is a graph the right structure to represent an SPICE electronic circuit??

Hello. I am a student who is figuring out how to build an SPICE simulator. I don't have dollars to spend buying a book from Amazon, at least not yet. By that reason i start to navigate on the web and i found a bit of theory about Electronics Circuit SPICE simulator.

I need is an advise to know if the program should be implemented as a graph, and how to evaluate the Kirchoff Law at a given node. Example, if i update a node value, and then it receives more current from other side of circuit how this can be solved?? Please, tell me how to solve it.

I am looking for theory of graphs now. I am a physics student and i have not seen this yet.
I don't know if I understand. Wikipedia says
SPICE ("Simulation Program with Integrated Circuit Emphasis") is a general-purpose, open-source analog electronic circuit simulator.
Do you want to write a simulator that behaves like the program SPICE (for educational purposes?), or does SPICE mean something else?
I am trying to understand how works SPICE. I have a little of mess understanding source code, i am figuring ideas from some PDFs. By that reason i am using instead an approach based on reading PDFs. I am trying to read source code but it makes hard for me.

I am mainly recreating itself.
Last edited on
design may be premature ^^^^ see above but going with what you said:

it may be graph like.
it looks like you have a node that has links (in and out) and a local {state or value type thing}?
just note that links here can't be raw pointers, because they also have a value on them.
so you have some sort of node object with a container of input links and a container of output links, where link is also an object.

this graph isnt going to be a PhD study in graph theory. Its just a way to store data. You need a way to traverse it, and being a circuit, there are likely distinct hard points ( circuits have a conceptual start and end point, or in a system, maybe a few of these) that you can hold onto that assist with this... unlike a generic cloud of data with random connections, yours has properties that you can exploit making it easier to deal with than 'generic graph theory' code. Honestly, you may just have some sort 'skip list' type structure (which is a graph... just as any list or tree is a graph) when its all said and done (?).
Is a graph the right structure to represent an SPICE electronic circuit??


Up to a point. If it's simply a resistive circuit then, yes. Each node has a voltage (which may or may not be imposed), each edge has a weight (resistance) and you need a function that will ensure that net current out of a node is zero and the sum of applied voltages matches the sum of IR losses round each closed loop.


Example, if i update a node value, and then it receives more current from other side of circuit how this can be solved?? Please, tell me how to solve it.

Update the nodal voltages iteratively to tend to zero net current from each node; the difference in voltage between the two ends of a wire and the resistance in that wire fix the current through it (Ohm's Law).

You do exactly the same in a water pipe network, except that the voltage is replaced by pressure (or fluid head), current is replaced by volume flow rate, and the resistance law tends to be quadratic, not linear.


I am trying to read source code but it makes hard for me.

You should understand the theory first and do a few simple circuits by hand - not try to learn electric circuit theory by reading c++ source code.



Start adding capacitors, inductors and transistors, however, and you want a rather more complicated class of object.
Last edited on
Thanks for your help. I will to put it in practice. Just let me try.
Topic archived. No new replies allowed.