C
C#2mo ago
Gax

Simulating electricity flow in wire/component classes in a linked list like manner

While this involves physics, I don't need help with that part, but instead with passing data from chains of wires that may have multiple outputs Currently working on a circuit simulation program. I have all the math done (for a prototype at least) working but with a few issues that I'm struggling to fix. Here's my base wire class. It is inherited by other components, such as Resistors, LEDs a Voltage Source, etc with overriden flows, however they still pretty much use DefaultFlow();. The wire state has to be reset before every flow https://paste.mod.gg/jcdunwrniejd/0 Assuming a simple circuit with only one source My issue When using only one voltage source, the circuit works wonders. However issues arise when chaining two or more of them, because sadly the order where flow is computed will matter. For example assume this setup:
Source2 -> Source1 -> Wire
Source2 -> Source1 -> Wire
- Assuming wire does not connect back to Source2. - Ignore resistance. - Source2 has 5V and Source1 has 3V - Source2 was instantiated after Source1, which means it would be at an index after Source1 in a wire list However, when iterating over the sources to start the flow, the issue starts: The wire only gets Source1's voltage. Which means it'll have 3V instead of the expected 8V. I have thought of finding the source that has the longest "chain" of wires/components, however this becomes flawed when Wire connects back to Source2, just like an expected circuit, since all sources could have the same "chain length" (imagine a 4th wire connecting back to Source2) Another (very bad) idea was recursively calculating the expected values for the circuit all the way up to that specific wire, but that brought a lot of issues, mainly performance ones considering these calculations happen every frame. I'm not entirely sure what the best solution for this is, and am just looking for ideas on how to proceed or maybe find a solution using a feature I'm unaware of.
BlazeBin - jcdunwrniejd
A tool for sharing your source code with the world!
6 Replies
ParaLogia
ParaLogia2mo ago
I think some of us need help with the physics part in order to understand this... In your example, if the wire did connect back to Source2, what would the expected voltage be? Still 8V?
MutableString
MutableString2mo ago
why is the wire a "thing", or rephrasing, why don't you have wires and nodes? (where sources would be two nodes, so to form a usual graph) then: it seems like you are trying to solve this "statically" instead of building the electrical network with just data and then examining with an algorithm to determine/solve all the values, this would explain why you have problems with order of instantiation also are you saying that a resistor class would inherit from a wire class? because that seem pretty incorrect to me anyway there are programs that simulate these kind of networks if they can be of any help
Gax
Gax2mo ago
In this case still 8 Right now you can assume if nothing consumes it like a LED or something, it would still be the same I have looked into them, a small amount are open source and while they use a slightly similar approach, the ones I found seem like their own implementation of it The reason I have Wire is for more easily implementing the components without having to repeat a lot of behaviour, and having the "universal" changes apply to them (like the normal flow of electricity and such) While a resistor inheriting from a wire might sound weird, all that would mean is that it would inherit the same properties like position and colors and such, while also following the basic flow that wires do, with the added change of passing less voltage through
MutableString
MutableString2mo ago
im not against code reuse (or rather, logic reuse), but stil a resistor is not a wire... how easy would this conceptualization be to understand from some who reads your code? it's true that both the wire and a component like a resistor share some electrical properties, and i'm not saying to overdo it (you find the granularity in the model that you need), but still they're two different things even in this discipline so rather i would use a bit of composition, both a wire and a transistor have a position, so you can think of an interface, for example, that represents what a position is/what it's used for and both can inherit that
Gax
Gax2mo ago
I'm mostly treating the wire class as both that and a base component class. Even when rewriting to fit that idea, I'd still be faced with the same issue: I still would need to figure a way to examine and find where the "beginning" of the circuit is Unless I'm having a bad case of tunnel vision I can't see how refactoring to use nodes and wires would help finding that Sadly I'm also limited to using this format by the project manager/lead, which is a whole new barrier that id need to deal with when refactoring I was asked to use a base class for all components so that creating new components as they need is easy, since I'm only responsible for the main "flow from A to B correctly" part of it
MutableString
MutableString2mo ago
as a person maintaining long lived projects i had to take these choices and live with it, so my suggestion is - apart from the things that naming is hard - just that you really want that the name you give an object really reflects what it is used for i don't have full knowledge of the problem, the separation into wire and node it could be that it won't be that useful for this in the end but i would usually start from that, because they are the two most basic element you could deconstruct this into, and then simplify what is not needed the matter of examining the circuit instead for me is abot the division of data and behavior, rather than naming or determining the building blocks of the network, so if you move the system that analyzes the network after it has been created (if you can) then part of your problems should just vanish, and you can examine the network how many times you want
Want results from more Discord servers?
Add your server