Compute the capacitance matrix of N conducting spheres in an arbitrary spatial distribution using the method of mirror images.
This project is based on the numerical implementation I developed during my BSc thesis. It allows for the computation of the mutual capacitance matrix of multiple spherical conductors in a bounded region using a physically grounded and mathematically robust approach.
Install the package using pip:
pip install arbitrary-capacitance-matrix
Or for development:
git clone https://github.com/carlosperezespinar/arbitrary-capacitance-matrix.git
cd arbitrary-capacitance-matrix
pip install -e .
import numpy as np
from capmatrix import compute_capacitance_matrix, Sphere
# Define two spheres
spheres = [
Sphere(center=[0, 0, 0], radius=1e-9), # 1 nm radius at origin
Sphere(center=[3e-9, 0, 0], radius=1e-9) # 1 nm radius, 3 nm away
]
# Compute capacitance matrix
C = compute_capacitance_matrix(spheres)
print("Capacitance matrix (F):")
print(C)
The method of mirror images is a classic electrostatics technique to enforce boundary conditions by introducing fictitious "image charges." For a system of N conducting spheres, we recursively place image charges inside each sphere to satisfy the equipotential condition, then assemble the resulting potentials into the capacitance matrix C:
C_ij = 4 * π * ε₀ * R_i * Q_ij
where Q_ij is the total induced charge on sphere i when sphere j is held at unit potential and all others are grounded.
This approach handles:
- Arbitrary number (N) of spheres
- Random positions & radii
- No assumed symmetries or simplifying approximations
Iterative placement of image charges inside each sphere to enforce constant potential.
Vector-based formula for computing the coordinates and strength of each new image charge from known charge positions.
from capmatrix import compute_capacitance_matrix, Sphere
# Two identical spheres
radius = 1e-9 # 1 nm
separation = 3e-9 # 3 nm center-to-center
spheres = [
Sphere([0, 0, 0], radius),
Sphere([separation, 0, 0], radius)
]
C = compute_capacitance_matrix(spheres)
# Three spheres with different radii
spheres = [
Sphere([-6e-9, 0, 0], 2e-9), # Left sphere
Sphere([0, 0, 0], 1.5e-9), # Center sphere
Sphere([6e-9, 0, 0], 1e-9) # Right sphere
]
C = compute_capacitance_matrix(spheres, tolerance=1e-10, max_iterations=100)
See the examples/
folder for complete working examples:
examples/two_spheres.py
- Two sphere benchmark with analytical comparisonexamples/three_spheres.py
- Three sphere configurationdemo.py
- Simple demonstration script
Represents a conducting sphere.
center
: 3D coordinates [x, y, z]radius
: Sphere radius (must be positive)
Represents a point charge in the system.
Compute the capacitance matrix for a list of spheres.
Parameters:
spheres
: List of Sphere objectstolerance
: Convergence tolerance (default: 1e-8)max_iterations
: Maximum iterations (default: 50)
Returns: NxN numpy array of capacitances in Farads
Run the test suite:
pytest tests/
- Wasshuber, C. (1997). About Single-Electron Devices and Circuits. PhD Thesis, Technische Universität Wien. Online PDF – Chapter on method of images
- Pérez Espinar, C. BSc Thesis (2019), Universitat de Barcelona
This project is licensed under the MIT License - see the LICENSE file for details.