r/howdidtheycodeit • u/smergeolacx • Mar 29 '23
How did they code programs like multisim amd it's online alternative.
Hey guys, i have been working with cadence and multisim this last sem in my university which got me to wondering on how they were coded exactly and the multisim online alternative. Like i wanna tey and make a very crude version of it to try and test myself but like are they just basic formulas to "interactable pictures"..?
31
Upvotes
11
u/Haha71687 Mar 29 '23
OOH this is my wheelhouse. I'm working on something that could be considered "MultiSim or Simulink for games" in Unreal Engine.
Actual Real(TM) circuit simulators probably do some sort of global approach. This is great if accuracy is what you're after, but it can have poor performance and not be very robust for "bad" circuits.
My approach is to treat the entire scene as a graph, with graph islands (simply connected nodes) being particles in the system and the bits that actually "do" something being constraints. For example, a super simple circuit with a battery and light bulb would be 2 particles (the 2 wires) and 2 constraints (the battery and lightbulb).
Each timestep, I LOCALLY solve each constaint (set state of connected particles based on unconstrained state). I run several iterations per timestep and the entire graph converges to a solved state. It's a bit like a cloth simulator. I wouldn't do any engineering work with it, but it's close enough for games and is SUPER performant.
I use it for multidomain stuff, like a hybrid vehicle. Every device in the vehicle (engine, motor, battery, cockpit, fuel tank, gearbox) is a constraint, and they are connected up along domains (drivetrain, electricity, fluids, controls). Each device just does its thing many times per frame, and the entire system converges to a solved state.
5
u/HammerBap Mar 29 '23
Not familiar with multisim but it looks like a circuit simulator. The way I'd approach it is have each component be a node in a graph with inputs and outputs. Now this is pretty straight forward with logic circuits where if your two inputs are 1's you can do the basic operation and output the result for the next component. As you go deeper and need things like gate delays and signal propagation, you begin adding in the actual Sim part of something like - "multisim" - start doing very small time steps and keep track of where a wave has propogated to and its amplitude along the circuit. More complicated and analog circuits will need to be able to handle basic LRC equations / linear algebra and analyze graph cycles to figure out the voltage at different periods of time. All of this builds on each other, so starting simple and adding functionality is definitely the way to go.
3
u/pigeon768 Mar 29 '23
Multisim is a wrapper around the open source library SPICE. Latest version is here. You won't be able to get a short version of the full algorithm here, it's pretty complicated.
There is a simplified version where if you have just linear DC components. (batteries and resistors, but no non-linear components like capacitors, inductors, transistors, diodes, or resistive elements with non-linear resistance like LEDs.) You model each loop, and use Kirchoff's law to tell you what the resistance and voltage is across each loop. You then take all those linear equations, and represent them as a matrix. Solve the matrix and then you have the current through the loops. Then it's just a question of using Ohm's law to calculate voltage drops etc across resistors.
https://www.khanacademy.org/science/electrical-engineering/ee-circuit-analysis-topic/ee-dc-circuit-analysis/a/ee-circuit-analysis-overview
https://www.youtube.com/watch?v=LVDX7T-jX3I
You can use the linear system as a series of stepping stones to sort of numerically integrate a non-linear circuit. Let's say you have a capacitor in your circuit. Use the linear system to calculate the voltage across the capacitor. Now update the capacitor's charge based on its capacitance and the voltage across it. Now treat the capacitor as a (linear) battery, and calculate the linear system again. This will get you get your feet wet in doing non-linear stuff.