Giter Club home page Giter Club logo

pyvolt's Introduction

pyvolt: a circuit specification library

Pyvolt makes circuit specification and simulation easy.

Installation and import

Install pyvolt using pip install -e . after cloning the repository.

Import pyvolt using import pyvolt as pv and import pyvolt.components as comp.

Tutorial

Create a circuit

circuit = pv.Circuit()

Create components

Create a VoltageSource, Resistor, Diode, Switch, or Transistor and add it to the circuit. Use of Python's walrus (:=) operator is optional, but enables users to create and add components in one line.

circuit.add(voltage_source := comp.VoltageSource(name="V", v=5))
circuit.add(resistor := comp.Resistor(name="r1", ohm=150))
circuit.add(diode := comp.Diode(name="LED", v_f=2))

Connect components

Pyvolt uses the >> operator to connect components instead of defining nodes explicitly:

voltage_source.vplus >> resistor.n1
resistor.n2 >> diode.anode
diode.cathode >> voltage_source.vminus

Connect the circuit ground

The circuit's ground is set by connecting it to any other component terminal. The node connected to the circuit ground is defined as having a voltage of zero.

circuit.gnd >> voltage_source.vminus

Compile the circuit

circuit.compile()

Interpreting the compilation output

Here is the output of circuit.compile() on the circuit defined in this tutorial:

Compiled circuit
----------------
Node 0: v=5.0
    V.vplus: i=0.02
    r1.n1: i=-0.02
Node 1: v=2.0
    LED.anode: i=-0.02
    r1.n2: i=0.02
Node 2: v=0.0
    V.vminus: i=-0.02
    LED.cathode: i=0.02
    Ground.gnd: i=0.0
----------------

We interpret this output as follows:

  • The node with V.vplus and r1.n1 has voltage 5V, with 20 milliamps of current flowing from V.vplus into r1.n1
  • The node with LED.anode and r1.n2 has voltage 2V, with 20 milliamps of current flowing from r1.n2 to LED.anode
  • The node with V.vminus, LED.cathode, and Ground.gnd has voltage 0V, with 20 milliamps of current flowing from LED.cathode to V.vminus

Pyvolt's convention is positive currents flowing out of components and negative currents flowing into components.

Inspecting voltages and currents

After a circuit has been compiled, we can use the inspect_voltage and inspect_current methods:

print("V before diode:", circuit.inspect_voltage(diode.anode))  # V before diode: 2.0
print("I before resistor:", circuit.inspect_current(resistor.n1))  # I before resistor: -0.02

Custom components

We can define custom components by calling the new_node_ref(name) method inside of Component classes for each connection the component has. We can set the target voltage or current of a node or branch by calling set_voltage or set_current on the node reference, respectively.

class Arduino(pv.Component):
    def __init__(self, name: str = "", n_pins: int = 5):
        super().__init__(name)
        self.pin_connections: list[pv.NodeRef] = [self.new_node_ref(f"pin{i}") for i in range(n_pins)]
        self.gnd_connection = self.new_node_ref("gnd")

    def pin(self, pin):
        return self.pin_connections[pin]

    @property
    def gnd(self):
        return self.gnd_connection

    def pin_on(self, pin):
        self.pin_connections[pin].set_voltage(5)

    def pin_off(self, pin):
        self.pin_connections[pin].set_voltage(0)

Then, we can use it just like any other component:

circuit = pv.Circuit()

circuit.add(arduino := Arduino(name="arduino", n_pins=2))
circuit.add(r2 := comp.Resistor(name="r2", ohm=300))

arduino.pin(0) >> r2.n1
# ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.