A library for basic electrotechnical calculations
Project description
Electrotechnics
A Python library for linear circuit analysis using Modified Nodal Analysis (MNA). Supports DC and AC (phasor) analysis.
This repository provides utilities for:
- Building and solving linear circuits with passive elements, voltage and current sources
- Computing Thevenin equivalent impedances between nodes
- Generating detailed reports including voltages, currents, and power analysis
- Interactive phasor diagrams for voltage and current visualization
- Example scripts for frequency response and power flow analysis
Features
Circuit Analysis:
- Solve linear circuits using Modified Nodal Analysis (MNA)
- Support for:
- Passive branches (complex impedances/admittances)
- Independent voltage sources
- Independent current sources
- Ground node selection
- Compute node voltages and branch currents
- Power flow analysis (complex power delivered/absorbed)
- Interactive phasor diagrams with:
- Voltage and current phasors with magnitude/phase tooltips
- Independent voltage/current scaling
- Configurable reference angles
- Toggle controls for visibility
Thevenin Analysis:
- Build nodal Laplacian (admittance) matrices from edge lists (impedance or admittance input).
- Compute equivalent Thevenin impedance between two nodes using the Moore–Penrose pseudoinverse or test-current method.
Reporting:
- Detailed solution reports with:
- Node voltages (magnitude and phase)
- Branch currents and powers
- Source currents and delivered powers
- Power summaries and conservation checks
- Export reports to Excel workbooks
Examples:
- Frequency response analysis
- Power flow visualization
- Complete circuit solving examples
Installation
Install the lastest stable version from the PyPI repository.
pip install electrotechnics -U
On MS Windows you may prefer:
python -m pip install electrotechnics -U
Once installed, run the example in a Python script or console:
from electrotechnics.examples import example1
Requirements
- Python 3.8+
- numpy
- matplotlib (only required for the example plotting)
- pandas (for report generation)
- openpyxl (for Excel export)
Install the required packages (recommended in a virtual environment):
python -m pip install numpy matplotlib pandas openpyxl
Usage
Run the provided example (from the repository root). The example script is inside the src package, so run it on Linux with PYTHONPATH set to src.
env PYTHONPATH=src python -m electrotechnics.examples.example1
This will print the Thevenin impedance at a reference frequency and open two plots (magnitude and phase vs frequency).
You can also import the core functions in your code:
# Example 1: Thevenin impedance
from electrotechnics.circuit import z_th_circuit
# edges: list of (n1, n2, z) where z is complex impedance
edges = [(1, 0, 100.0), (1, 2, 1j*2*np.pi*1000*10e-3)]
Z = z_th_circuit(edges, a=1, b=0)
# Example 2: Full circuit solution with voltage and current sources
from electrotechnics.circuit import solve_circuit, report, export_report_to_excel
# Define circuit elements
edges = [
(1, 0, 100), # R1 = 100 Ω between nodes 1-0
(1, 2, 50), # R2 = 50 Ω between nodes 1-2
(2, 0, 1j*100) # XL = j100 Ω between nodes 2-0
]
# Add sources
voltage_sources = [(1, 0, 10+0j)] # 10 V source between nodes 1-0
current_sources = [(2, 0, 0.1+0j)] # 0.1 A source between nodes 2-0
# Solve circuit
solution = solve_circuit(edges, voltage_sources, current_sources, ground=0)
# Generate detailed report
report_data = report(edges, voltage_sources, current_sources, solution)
# Access results
node_voltages = solution['node_voltages'] # All node voltages
branch_currents = solution['branch_currents'] # Currents through passive branches
source_currents = solution['voltage_source_currents'] # Currents through voltage sources
# Export report to Excel
export_report_to_excel(report_data, 'circuit_solution.xlsx')
# Example 3: Create a phasor diagram
from electrotechnics.plotting import phasor_plot
# After solving the circuit with solve_circuit(), plot phasors:
plot_out = phasor_plot(
solution,
currents=[0, 1], # Plot currents from branches 0 and 1
voltages=[(1, 0), (2, 0)], # Plot voltage differences
voltage_sources=voltage_sources,
current_sources=current_sources,
reference=(1, 0), # Use V1-0 as angle reference
separate_scales=True, # Independent scaling for V/I
hide_axes=True,
show_grid=False,
title='Phasor diagram'
)
License
This project is licensed under the MIT License — see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file electrotechnics-1.5.tar.gz.
File metadata
- Download URL: electrotechnics-1.5.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccc1a68c5d5076f9b61b85c0f75675aa03e11c8dd92de5625a75d9fb3c1387bf
|
|
| MD5 |
c274ac1c2cdd2f1d6a2cb568fd0a257a
|
|
| BLAKE2b-256 |
fcff78ca8e53e95db5c11b7ecf1d6f942124bacf716b87f7ff4649eb6db2cc65
|
File details
Details for the file electrotechnics-1.5-py3-none-any.whl.
File metadata
- Download URL: electrotechnics-1.5-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49c2f6f0f97d9b90d274c07dd7eff8523bce2d8a96815b53520408b02152aa16
|
|
| MD5 |
f510bba343ffb338bf914d0fb9c58e88
|
|
| BLAKE2b-256 |
63203ffa3ecb80f77f910b5dce0b535c521c4867a7196228db9915919aeddab5
|