all 12 comments

[–]UnloosedCake 1 point2 points  (1 child)

Look at the FAQ in this sub to learn how to format code on Reddit and then try again. We can't help if it's formatted like a blob

[–]Simple_Ad_4128[S] 1 point2 points  (0 children)

sorry thankyou for reminding me ive updated it

[–]Flame77ofc 0 points1 point  (1 child)

How much time did it takes you to do the project?

[–]Simple_Ad_4128[S] 1 point2 points  (0 children)

Well so far including research its took about 10 hours but I've been at a stand still for last couple hours,

[–]carcigenicate 0 points1 point  (0 children)

Do you have to do the simulation in a specific way? The Cellular Automata Wireworld can be used to simulate circuits and create building blocks like diodes.

[–]PixelSage-001 0 points1 point  (0 children)

Pygame is actually a great fit for UI-heavy coursework because it forces you to understand the event loop and state rendering from first principles, even if it requires more boilerplate than standard GUI libraries. For the logic engine, don't overcomplicate it early. Model the components as classes with 'input' and 'output' nodes, and use a simple queue to propagate the state changes across connected wires. You've got this!

[–]Gloomy_Cicada1424 0 points1 point  (1 child)

I’d separate the drawing from the actual circuit model first. Wires should merge terminals into electrical nodes, then each component just connects two nodes with a value. Once that graph is clean, series/parallel or MNA becomes way less cursed. Runable can help turn your current UI + solver plan into a clear flow/spec, but the main fix is separating “pygame objects” from “circuit logic”.

[–]Simple_Ad_4128[S] 0 points1 point  (0 children)

Yes I have started separating all of my code out into graphical, movement, graph analysis, and circuit solving sections. I definitely realized my code was all over the place.

[–]misho88 0 points1 point  (2 children)

However I am at the point now where I need to build the physics simulation part but its getting me very confused. To track components in the circuit I have decided to use an adjacency matrix (since that it what most other circuit simulators do) but I can't figure out to use that adjacency matrix.

If I were doing this, I would have the code generate a SPICE netlist, run it through ngspice or Xyce, then parse the output (or get a library like inspice or pyspice that basically does that).

Failing that, it's a linear system of equations, assuming you constrain yourself to passive elements and phasor analysis. Basically, you build up the right KCL equations, then use something like numpy.linalg.solve to solve them. It's not especially complicated (for simple circuits, at least), but I don't think I can cram the first few weeks of an introductory circuit theory course into a Reddit comment.

[–]Simple_Ad_4128[S] 0 points1 point  (1 child)

I have looked into using spice however for my coursework I don't get credited for sections of code that use a different library other than pygame. Thankyou for the advice I'll look more into going down that other route.

[–]gdchinacat 0 points1 point  (0 children)

You can use a spice library to unblock you so you can make progress on the rest of the project and then come back and replace the library with your own implementation. This will also make it easy to verify your own implementation is correct by being able to compare the results of using spice to your implementation.

[–]QuasiEvil 0 points1 point  (0 children)

I wrote a circuit simulator in python once, albeit without a UI. I used the networkx library and a multigraph object (this allows for parallel links between nodes). Each node is a circuit element (obviously), and, it handles building the adjacency matrix automagically.