Skip to content

Commit d8395ba

Browse files
Merge pull request #147 from michealroberts/feature/light/get_photon_frequency
feat: add get_photon_frequency to light in celerity module
2 parents 015b8ed + e3a51ae commit d8395ba

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/celerity/light.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# **************************************************************************************
1212

13+
1314
def get_light_travel_distance(time: float) -> float:
1415
"""
1516
Calculate the distance light travels in a given time.
@@ -19,4 +20,24 @@ def get_light_travel_distance(time: float) -> float:
1920
"""
2021
return SPEED_OF_LIGHT * time
2122

22-
# **************************************************************************************
23+
24+
# **************************************************************************************
25+
26+
27+
def get_photon_frequency(
28+
wavelength: float,
29+
) -> float:
30+
"""
31+
Calculate the frequency of a photon given its wavelength (in m).
32+
33+
:param wavelength: Wavelength in meters
34+
:return: Photon frequency in Hz
35+
:raises ValueError: If wavelength is less than or equal to zero
36+
"""
37+
if wavelength <= 0:
38+
raise ValueError("Wavelength must be a positive number.")
39+
40+
return SPEED_OF_LIGHT / wavelength
41+
42+
43+
# **************************************************************************************

tests/test_light.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
import unittest
1010

11-
from celerity.light import get_light_travel_distance
11+
from celerity.light import get_light_travel_distance, get_photon_frequency
1212

1313
# **************************************************************************************
1414

15+
1516
class TestLightTravelDistance(unittest.TestCase):
1617
def test_light_travel_distance(self):
1718
"""
@@ -33,4 +34,36 @@ def test_light_travel_distance(self):
3334
expected_distance = 149896229.0 # Speed of light in m/s * 0.5 seconds
3435
self.assertAlmostEqual(get_light_travel_distance(time), expected_distance)
3536

36-
# **************************************************************************************
37+
38+
# **************************************************************************************
39+
40+
41+
class TestPhotonFrequency(unittest.TestCase):
42+
def test_photon_frequency(self) -> None:
43+
"""
44+
Test the photon frequency calculation.
45+
"""
46+
# Calculate the frequency using the wavelength of 500 nm:
47+
frequency = get_photon_frequency(500e-9)
48+
49+
# Expected approximate frequency in Hz for 500 nm wavelength:
50+
expected_frequency = 6e14
51+
52+
self.assertAlmostEqual(
53+
frequency, expected_frequency, delta=1e-3 * expected_frequency
54+
)
55+
56+
def test_negative_photon_frequency(self) -> None:
57+
"""
58+
Test the photon frequency calculation with a negative wavelength.
59+
"""
60+
with self.assertRaises(ValueError):
61+
get_photon_frequency(-500e-9)
62+
63+
64+
# **************************************************************************************
65+
66+
if __name__ == "__main__":
67+
unittest.main()
68+
69+
# **************************************************************************************

0 commit comments

Comments
 (0)