A general-purpose mesh analysis solver for planar electrical circuits. It computes mesh currents by automatically constructing and solving Kirchhoff's Voltage Law (KVL) equations using symbolic algebra.
Originally developed during B.Tech 1st Year as a foundational circuits project, then refactored and improved in 3rd Year with clean architecture, proper naming conventions, docstrings, and modular function design.
- Features
- How Mesh Analysis Works
- Algorithm Overview
- Requirements
- Installation
- How to Run
- Input Instructions
- Examples
- Project Structure
- Future Improvements
- Author
- License
| Capability | Description |
|---|---|
| Unlimited meshes | Supports any number of meshes (practical limit: 9 for common-resistor labelling) |
| Shared resistors | Handles common resistors between any pair of meshes |
| Voltage sources | Supports multiple voltage sources per mesh with sign convention |
| Current sources | Supports current source constraints on individual meshes |
| Automatic KVL equations | Equation terms are generated programmatically from user input |
| Symbolic solving | Uses SymPy's symbolic engine -- exact rational/algebraic solutions |
| Direction detection | Reports whether each mesh current flows Clockwise (CW) or Anti-Clockwise (A.CW) |
| Common resistor currents | Calculates and displays current through every shared branch with flow direction |
Mesh analysis is a systematic method for solving planar circuits. It is based on two principles:
- Kirchhoff's Voltage Law (KVL): The algebraic sum of all voltages around any closed loop (mesh) is zero.
- Mesh currents: A fictitious current variable is assigned to each mesh, assumed to flow in the clockwise direction by convention.
- Assign a mesh current (
$i_1, i_2, \ldots, i_n$ ) to each independent loop. - For each mesh, write a KVL equation:
- Resistors exclusive to the mesh contribute
$R \cdot i_k$ . - Shared (common) resistors contribute a coupling term
$R_c \cdot (i_k - i_j)$ between adjacent meshes$k$ and$j$ . - Voltage sources add or subtract their value depending on polarity relative to the assumed current direction.
- Resistors exclusive to the mesh contribute
- If a mesh contains a current source, the mesh current on that loop is directly constrained by the source value instead of a KVL equation.
- Solve the resulting system of
$n$ linear equations for$n$ unknowns.
A negative solution for a mesh current indicates the actual current flows anti-clockwise (opposite to the assumed direction).
START
|
v
[1] Read number of meshes (n) and number of current sources
|
v
[2] For each mesh:
- Collect resistor values --> store in mesh_elements
- If current source exists, record constraint
|
v
[3] For each mesh:
- Collect voltage source values --> store in mesh_elements
|
v
[4] If n > 1:
- Collect common (shared) resistor values between mesh pairs
- Store negative coupling coefficients in mesh_elements
|
v
[5] Build KVL equation terms for each mesh:
- Self-resistance terms: R * i_k
- Voltage terms: V (constant)
- Coupling terms: -R_common * i_adjacent
|
v
[6] Convert string terms to SymPy symbolic expressions
|
v
[7] Construct equation system:
- Standard mesh: sum_of_terms = 0 (KVL)
- Current source: i_k = I_source (constraint)
|
v
[8] Solve using sympy.solve()
|
v
[9] Compute current through each common resistor:
I_branch = i_k - i_j
|
v
[10] Remove duplicate branch pairs (R12 == R21)
|
v
[11] Display results with direction indicators
|
END
| Dependency | Version | Purpose |
|---|---|---|
| Python | 3.8+ | Runtime |
| SymPy | >= 1.12 | Symbolic equation construction and linear system solving |
| Package | Purpose |
|---|---|
| colorama | Enables ANSI color support on older Windows terminals (CMD on Windows 8/7). Not needed on Windows 10+, Windows Terminal, PowerShell 7, or Linux/macOS. |
The solver uses raw ANSI escape codes for colored terminal output. These work natively on all modern terminals. Install colorama only if you see garbled output on a legacy Windows console.
git clone https://github.com/<your-username>/mesh_analysis.git
cd mesh_analysispython -m venv venvActivate it:
| OS | Command |
|---|---|
| Windows (PowerShell) | .\venv\Scripts\Activate.ps1 |
| Windows (CMD) | .\venv\Scripts\activate.bat |
| Linux / macOS | source venv/bin/activate |
Using the requirements file:
pip install -r requirements.txtOr install manually:
pip install sympyTo verify installation:
python -c "import sympy; print(sympy.__version__)"python Mesh_solver.pyThe program launches an interactive terminal session. Follow the on-screen prompts to enter your circuit data. Results are displayed immediately after solving.
| Parameter | Format | Notes |
|---|---|---|
| Number of meshes | Integer (e.g., 2) |
Total independent loops in the circuit |
| Current sources | Integer count, then value per mesh | Enter 0 if mesh has no current source. Positive = CW, Negative = A.CW |
| Resistor values | Comma-separated (e.g., 2,5,10) |
Enter all resistors in the mesh, including any shared resistor |
| Voltage sources | Comma-separated with sign (e.g., -5,10) |
Positive = voltage rise in CW direction, Negative = voltage drop |
| Common resistors | One value per mesh pair | The shared resistance between Mesh i and Mesh j |
All values follow the clockwise (CW) positive convention:
- A voltage source whose positive terminal is encountered first when traversing the mesh clockwise is positive.
- A voltage source whose negative terminal is encountered first is negative.
- A current source flowing in the CW direction of the mesh is positive; A.CW is negative.
- Common resistors must also be included in each mesh's individual resistor list.
- Enter
0for current source value if the mesh does not contain one. - Units: Resistance in Ohms, Voltage in Volts, Current in Amperes.
Circuit: A single loop with a 10V voltage source and two resistors (2 Ohm, 3 Ohm) in series.
+---[ 2 Ohm ]---[ 3 Ohm ]---+
| |
(+) |
10V |
(-) |
| |
+----------------------------+
Mesh 1 (i1 -->)
Input:
Enter no of Meshes : 1
Enter the total no of Current sources in the circuit (if no current source enter 0(zero)) : 0
Mesh 1
Enter the values of all resistors(Ex: 2,5,10) : 2,3
Mesh1-R1 = 2 Ohm
Mesh1-R2 = 3 Ohm
Mesh 1
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 10
Mesh1-V1 = 10 V
Output:
Mesh Currents :
Mesh 1 --> i1 = 2.0 A in (CW)
Verification:
Circuit: Two meshes sharing a 3 Ohm resistor. Mesh 1 has a 10V source and resistors 2 Ohm, 3 Ohm. Mesh 2 has a 5V source and resistors 3 Ohm, 4 Ohm.
+--[ 2 Ohm ]--+--[ 4 Ohm ]--+
| | |
(+) [ 3 Ohm ] (+)
10V (shared) 5V
(-) | (-)
| | |
+--------------+--------------+
Mesh 1 (i1) Mesh 2 (i2)
Input:
Enter no of Meshes : 2
Enter the total no of Current sources in the circuit (if no current source enter 0(zero)) : 0
Mesh 1
Enter the values of all resistors(Ex: 2,5,10) : 2,3
Mesh 2
Enter the values of all resistors(Ex: 2,5,10) : 3,4
Mesh 1
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 10
Mesh 2
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 5
Enter the no.of Common resistors B/W Meshes : 1
Enter Common resistance values B/W meshes :
Mesh 1 and Mesh 2 = 3
Output:
Mesh Currents :
Mesh 1 --> i1 = 2.83 A in (CW)
Mesh 2 --> i2 = 1.93 A in (CW)
Current through common resistors :
I-R12(3 Ohm) = 0.9 A (in the direction of i1)
Verification (KVL):
Mesh 1:
Mesh 2:
Solving:
Current through shared resistor:
Circuit: Three meshes. Mesh 1 and Mesh 2 share a 4 Ohm resistor. Mesh 2 and Mesh 3 share a 6 Ohm resistor. Each mesh has its own voltage source.
+--[3 Ohm]--+--[5 Ohm]--+--[2 Ohm]--+
| | | |
(+) [4 Ohm] [6 Ohm] (+)
20V shared shared 10V
(-) | | (-)
| | | |
+------------+--[8 Ohm]--+------------+
Mesh 1 Mesh 2 Mesh 3
Input:
Enter no of Meshes : 3
Enter the total no of Current sources in the circuit (if no current source enter 0(zero)) : 0
Mesh 1
Enter the values of all resistors(Ex: 2,5,10) : 3,4
Mesh 2
Enter the values of all resistors(Ex: 2,5,10) : 4,5,6,8
Mesh 3
Enter the values of all resistors(Ex: 2,5,10) : 6,2
Mesh 1
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 20
Mesh 2
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : -15
Mesh 3
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 10
Enter the no.of Common resistors B/W Meshes : 2
Enter Common resistance values B/W meshes :
Mesh 1 and Mesh 2 = 4
Mesh 1 and Mesh 3 = 0
Mesh 2 and Mesh 3 = 6
Output:
Mesh Currents :
Mesh 1 --> i1 = 2.82 A in (CW)
Mesh 2 --> i2 = 0.07 A in (A.CW)
Mesh 3 --> i3 = 1.30 A in (CW)
Current through common resistors :
I-R12(4 Ohm) = 2.89 A (in the direction of i1)
I-R23(6 Ohm) = 1.37 A (in the direction of i3)
Note: Enter 0 for the common resistance between Mesh 1 and Mesh 3 since they do not share a resistor.
Circuit: Two meshes with multiple voltage sources per mesh. Mesh 1 has two voltage sources (20V and -5V). Mesh 2 has one voltage source (10V). The meshes share a 5 Ohm resistor.
+--[3 Ohm]---(+20V-)---+---(-5V+)--[7 Ohm]--+
| | |
| [5 Ohm] (+)
| shared 10V
| | (-)
| | |
+---[2 Ohm]------------+----------------------+
Mesh 1 Mesh 2
Input:
Enter no of Meshes : 2
Enter the total no of Current sources in the circuit (if no current source enter 0(zero)) : 0
Mesh 1
Enter the values of all resistors(Ex: 2,5,10) : 3,5,2
Mesh 2
Enter the values of all resistors(Ex: 2,5,10) : 5,7
Mesh 1
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 20,-5
Mesh 2
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 10
Enter the no.of Common resistors B/W Meshes : 1
Enter Common resistance values B/W meshes :
Mesh 1 and Mesh 2 = 5
Output:
Mesh Currents :
Mesh 1 --> i1 = 1.97 A in (CW)
Mesh 2 --> i2 = 1.65 A in (CW)
Current through common resistors :
I-R12(5 Ohm) = 0.32 A (in the direction of i1)
Verification (KVL):
Mesh 1:
Mesh 2:
Solving:
Circuit: Two meshes sharing a 4 Ohm resistor. Mesh 1 has a 20V voltage source and resistors 2 Ohm, 4 Ohm. Mesh 2 contains a 5A current source (flowing clockwise) and a 6 Ohm resistor along with the shared 4 Ohm resistor.
When a mesh contains a current source, the mesh current for that loop is directly constrained by the source value. No KVL equation is written for that mesh; instead
+---[ 2 Ohm ]---+---[ 6 Ohm ]---+
| | |
(+) [ 4 Ohm ] (5A source)
20V (shared) CW
(-) | |
| | |
+----------------+----------------+
Mesh 1 (i1) Mesh 2 (i2)
Input:
Enter no of Meshes : 2
Enter the total no of Current sources in the circuit (if no current source enter 0(zero)) : 1
Mesh 1
Enter the value of current source in Mesh 1 (in C.W) : 0
Enter the values of all resistors(Ex: 2,5,10) : 2,4
Mesh 2
Enter the value of current source in Mesh 2 (in C.W) : 5
Enter the values of all resistors(Ex: 2,5,10) : 4,6
Mesh 1
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 20
Mesh 2
Enter the values of all voltage sources(in C.W)[Ex: -5,10,20] : 0
Enter the no.of Common resistors B/W Meshes : 1
Enter Common resistance values B/W meshes :
Mesh 1 and Mesh 2 = 4
Output:
Mesh Currents :
Mesh 1 --> i1 = 6.67 A in (CW)
Mesh 2 --> i2 = 5.0 A in (CW)
Current through common resistors :
I-R12(4 Ohm) = 1.67 A (in the direction of i1)
Verification:
Mesh 2 is constrained:
Mesh 1 KVL:
Current through shared resistor:
mesh_analysis/
|-- Mesh_solver.py # Main solver script (single-file application)
|-- requirements.txt # Python dependency list
|-- README.md # Project documentation
|-- .gitignore # Git ignore rules
| Section | Description |
|---|---|
| Constants | ANSI color codes, unit symbols |
| Global Data Stores | Resistor/voltage/current tracking lists and dictionaries |
collect_resistors() |
Prompts and stores resistor values per mesh |
collect_voltages() |
Prompts and stores voltage source values per mesh |
collect_common_resistors() |
Prompts and stores shared resistor values between mesh pairs |
build_kvl_equation_terms() |
Constructs KVL string terms from mesh_elements |
solve_mesh_equations() |
Converts terms to SymPy equations and solves the linear system |
compute_common_resistor_currents() |
Calculates branch currents through shared resistors |
remove_duplicate_pairs() |
Filters duplicate common-resistor entries (e.g., R12 vs R21) |
display_instructions() |
Prints formatted usage instructions |
display_mesh_currents() |
Prints solved mesh currents with CW/A.CW direction |
display_common_resistor_currents() |
Prints current through shared branches with flow direction |
| Main Program | Orchestrates input, solving, and output in four sequential steps |
- Web interface -- Flask/Streamlit front-end for browser-based circuit input and visualization.
- Graphical circuit diagrams -- Auto-generate circuit schematics from input data using
matplotliborschemdraw. - Matrix display -- Show the assembled resistance matrix and voltage vector before solving.
- Export results -- Output solutions to CSV, JSON, or PDF report format.
- Input validation -- Robust error handling for malformed or inconsistent circuit data.
- Supermesh support -- Automatic supermesh detection when a current source is shared between two meshes.
- Unit tests -- Automated test suite with known circuit solutions for regression testing.
M. Abhiram
B.Tech Engineering Student
| Originally Developed | 1st Year B.Tech |
| Refactored & Improved | 3rd Year B.Tech |
MIT License
Copyright (c) 2026 Abhiram M
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.