[pre-pre-ANN] Circuits.jl

Warning: Unregistered hobby project. I just want to see if this is something anyone else has been thinking about / wants / has input on.

Circuits.jl

An electrical circuit analysis tool in Julia. I’ll just show the examples from my (currently-only-manually-compiling) docs to show it off:

Examples

c = @circuit a:(0,0) --> Inductor(9e-3) --> b:(0,1) --> Resistor(11) --> c:(1,1) --> Capacitor(1.455e-6) --> d:(1,0)
latexstandalone(c, "example1.png")

# One day this will be `p = Network(c, :a, :d)`, but that's not implemented yet.
# `A --> B --> C` currently doesn't return a single `Series`. That's a bug.
p = Series(Inductor(9e-3), Resistor(11), Capacitor(1.455e-6)) # H, Ω, F
s = range(0, 20000, length=1000) # Hz
plot(s, map(ω->abs(5*voltageDivision(p, im*ω)[3]), s); # `[3]` here means "voltage over the capacitor"
        xlabel="\$\\nu / \\mathrm{Hz}\$", ylabel="\$U / \\mathrm{V}\$",
    )
vline!([1/sqrt(9e-3*1.455e-6)]) # theoretical resonance frequency of LC circuit
savefig("example2.png")

example2

Features

  • Displays circuits using circuitikz
  • Simplifies circuits by merging components (Resistor(a) --> Resistor(b) == Resistor(a+b), Inductor(a) // Inductor(b) == Inductor(inv(inv(a)+inv(b))) etc)
  • Interfaces well with Unitful.jl, and hopefully some symbolic packages (there are so many choices, I haven’t really had the time to choose – ideally you get one that can perform inverse laplace transforms)
  • Performs voltage and current division

Loose ends

  • Some uncertainty as to the julia version (I think this only runs on 1.6 because it uses infix -->, if someone has a good idea for an alternative infix operator for “series”, let me know)
  • Should probably use LightGraphs.jl or something to store circuits. I tried writing a version, but I really don’t know the theory or the package well enough. Especially for operations like unfurling a Circuit between two nodes as a Network, some kind of graph crawler will probably be necessary
  • Voltage and current sources don’t quite work right yet
  • Doesn’t yet know how to set up proper node analysis matrices (hinges on the previous two points)
  • Some bugs in things like merging Seriess containing Seriess and Parallels containing Parallels
  • The nomenclature is probably stupid, I’m not an electrical engineer but a lowly physicist

Conclusion

I’m happy for any ideas or discouraging words!

27 Likes

Nice! I always wanted to write something like this but can’t find the time.

What are your plans regarding non-linear circuit components?

Nice! How does it compare to https://github.com/HSU-ANT/ACME.jl ?

1 Like

Half putting my head in the sand about those. I felt like it would make sense to get something up and working for linear components and sources, trying to stay somewhat general. As far as I remember you can actually add nonlinear components to analysis matrices systematically if you’re careful. But yeah, one day maybe.

1 Like

Annoyingly I hadn’t found that one (this is why the Julia naming scheme discourages acronyms). It’s probably a lot more refined than mine, but there is probably also some meaningful differences. From a cursory overview, I’d say this is more analytical and that is more numerical. And they don’t do the circuitikz thing :stuck_out_tongue:

That’s pretty cool! Nice congratulations on building it. Displaying it is also a really nice touch.

I’m not sure if you’re familar with this example from an existing package. But just in case you haven’t seen it yet there’s also the ModelingToolkit.jl that has an example of simulating a resistor-capacitor circuit by defining components and their equations and connecting them together.

https://mtk.sciml.ai/dev/tutorials/acausal_components/

I don’t fully understand how the tutorial works but it could also serve as your backend to help tackle simulating ODEs and solving circuits in the time domain.

1 Like

Nice idea and original title.

PS: running it in Julia produces:

julia> pre=true; ANN=false; [pre-pre-ANN]
1-element Array{Int64,1}:
 0

:wink:

4 Likes

This is great! but the one that makes us better are critics, just kidding!

It can be used to calculate electricity generated and used by smart grid in the future.

A physicist created this, wonder where the electrical engineer at this time.