5
\$\begingroup\$

I wonder if someone can help me. How will one go about to create a simple circuit simulator? Similar to Multisim, just a lot simpler!

Basically, I only need resistors, capacitors, inductors and voltage sources.

Is there a tutorial I can follow, to create this using C# and Visual Studio?

\$\endgroup\$
  • 1
    \$\begingroup\$ Must you have AC and DC and oppoint and thermal noise? \$\endgroup\$ – analogsystemsrf Sep 3 '18 at 5:09
  • 1
    \$\begingroup\$ AC and DC yes, thermal noise not so important to me now. \$\endgroup\$ – fitzchivalry Sep 3 '18 at 5:22
28
\$\begingroup\$

I wrote the simulation engine that powers CircuitLab from scratch: from the sparse matrix library up through component models and simulation modes. My co-founder wrote the front-end. It ended up being an unbelievably huge programming project, but one I'm quite proud of. If you're up for the challenge, writing a circuit simulator may be one of the most rewarding programming projects you'll ever tackle.

At a high level, you just need to:

  1. Turn a network of components into a system of equations (non-linear differential equations).
  2. Numerically solve the system of equations (using sparse matrix techniques).

I don't know of an online tutorial, but I've tried to document a lot of this as I write the "Ultimate Electronics" textbook, especially in Chapter 2. There are also a number of 1990s-era books on the topic of circuit simulation, though I don't have them handy at the moment.

My suggestion would be to start from only voltage sources and resistors, and continue building from there. Good luck.

\$\endgroup\$
  • \$\begingroup\$ Thank you for this answer and book! Out of curiosity, as an estimation, can you tell how many lines of code it ended up being? \$\endgroup\$ – user1584421 Sep 3 '18 at 18:14
2
\$\begingroup\$

I doubt there are online tutorials because it's something pretty specific.

However, one source of information you can definitely use is open source code. One I know of is SpicePy - it's written in Python, but it's very well documented, although the Python language is very descriptive just by itself. You can use such library in your Python code or though the Telegram Bot.

What you'll need is some sort of way to describe the topology of your circuit. One common approach is the use of netlists, which are essentially text that describe each component in the circuit and how it's connect to the other ones (e.g. through node numbers). You can use this strategy or whatever one seems easier for you to take; parsing it and making it an actual graph (i.e. is it meaningful?) out of it might take you some time.

After that, one common way to analyze circuits in simulators is nodal analysis; then resort to some linear algebra library to solve the system of equations (which will surely be linear), such as Math.Net.

\$\endgroup\$
0
\$\begingroup\$

I wrote a Spice-based circuit simulator for .NET and Mono called Spice#. The main difference with most simulators you will find out there are:

  • It is open source.
  • It is a library and not a standalone executable.
  • It is developed to be customized and extended.

The project also has documentation containing some information about how the simulator (like Spice) performs Modified Nodal Analysis (MNA). So I believe this topic might be of interest to you.

\$\endgroup\$
0
\$\begingroup\$

I wrote a Python-based tool that can simulate AC signals and noise using the sparse matrix solvers provided by SciPy. It is an open source project based on a closed source tool in the gravitational wave community called LISO.

I have structured the code in a way that you can build different solving libraries or create different analyses that use the same circuit definition. Both analysis types that exist as of writing (signal and noise analyses) build a matrix from the circuit components and then solve it for a given excitation, e.g. an input, over a given frequency range.

It is similar to the way SPICE and other nodal analysis circuit simulators work. I hope the code is relatively easy to understand.

There is some basic documentation here, but it is still in development. Let me know if you end up using it and have questions about how it works.

Here is an example of LISO syntax that it supports (but you can also define circuits purely in Python). It is a little like a SPICE netlist:

r r1 100 nin nsum
r r3 1.075k no nsum
r r4 42.2 nsum nm
r r6 65 nin gnd
c c2 4.7n nsum gnd
c c5 122p no nm
op op1 op27 gnd nm no

freq log 1 1M 101

uinput nin nsum 50
uoutput no nsum

The above code produces a plot like this:

floating voltage op-amp circuit

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.