Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions physics/Biot-savart-law.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""

Check failure on line 1 in physics/Biot-savart-law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

physics/Biot-savart-law.py:1:1: N999 Invalid module name: 'Biot-savart-law'
Calculate the magnetic field (B) produced by a long straight current-carrying
conductor at a distance r from the wire in free space.

Equation for calculating magnetic field:
B = (μ₀ * I) / (2 * π * r)

where:
B = Magnetic field (Tesla, T)
μ₀ = Permeability of free space (4π × 10⁻⁷ T·m/A)

Check failure on line 10 in physics/Biot-savart-law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

physics/Biot-savart-law.py:10:37: RUF002 Docstring contains ambiguous `×` (MULTIPLICATION SIGN). Did you mean `x` (LATIN SMALL LETTER X)?
I = Current (Amperes, A)
r = Perpendicular distance from the wire (meters, m)

https://en.wikipedia.org/wiki/Biot–Savart_law

Check failure on line 14 in physics/Biot-savart-law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

physics/Biot-savart-law.py:14:35: RUF002 Docstring contains ambiguous `–` (EN DASH). Did you mean `-` (HYPHEN-MINUS)?
"""

import math

# Magnetic constant (Permeability of free space) in T·m/A
mu_0 = 4 * math.pi * 1e-7


def magnetic_field(current: float, distance: float, permeability: float = mu_0) -> float:

Check failure on line 23 in physics/Biot-savart-law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

physics/Biot-savart-law.py:23:89: E501 Line too long (89 > 88)
"""
Args:
current: electric current through the wire (Amperes, A)
distance: perpendicular distance from the wire (meters, m)
permeability: permeability of the medium (T·m/A),
default is free space permeability (μ₀)

returns:
the magnetic field strength at the given point (Tesla, T)

>>> magnetic_field(current=10, distance=0.05)
3.9999999999999996e-05
>>> magnetic_field(current=5, distance=0.1)
1.0e-05
>>> magnetic_field(current=0, distance=0.1)
0.0
>>> magnetic_field(current=-5, distance=0.1)
Traceback (most recent call last):
...
ValueError: Impossible current
>>> magnetic_field(current=5, distance=-0.1)
Traceback (most recent call last):
...
ValueError: Impossible distance
>>> magnetic_field(current=5, distance=0)
Traceback (most recent call last):
...
ZeroDivisionError: Distance cannot be zero
"""

if current < 0:
raise ValueError("Impossible current")
if distance < 0:
raise ValueError("Impossible distance")
if distance == 0:
raise ZeroDivisionError("Distance cannot be zero")

return (permeability * current) / (2 * math.pi * distance)


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading