Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 22 additions & 1 deletion src/celerity/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# **************************************************************************************


def get_light_travel_distance(time: float) -> float:
"""
Calculate the distance light travels in a given time.
Expand All @@ -19,4 +20,24 @@ def get_light_travel_distance(time: float) -> float:
"""
return SPEED_OF_LIGHT * time

# **************************************************************************************

# **************************************************************************************


def get_photon_frequency(
wavelength: float,
) -> float:
"""
Calculate the frequency of a photon given its wavelength (in m).

:param wavelength: Wavelength in meters
:return: Photon frequency in Hz
:raises ValueError: If wavelength is less than or equal to zero
"""
if wavelength <= 0:
raise ValueError("Wavelength must be a positive number.")

return SPEED_OF_LIGHT / wavelength


# **************************************************************************************
37 changes: 35 additions & 2 deletions tests/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

import unittest

from celerity.light import get_light_travel_distance
from celerity.light import get_light_travel_distance, get_photon_frequency

# **************************************************************************************


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

# **************************************************************************************

# **************************************************************************************


class TestPhotonFrequency(unittest.TestCase):
def test_photon_frequency(self) -> None:
"""
Test the photon frequency calculation.
"""
# Calculate the frequency using the wavelength of 500 nm:
frequency = get_photon_frequency(500e-9)

# Expected approximate frequency in Hz for 500 nm wavelength:
expected_frequency = 6e14

self.assertAlmostEqual(
frequency, expected_frequency, delta=1e-3 * expected_frequency
)

def test_negative_photon_frequency(self) -> None:
"""
Test the photon frequency calculation with a negative wavelength.
"""
with self.assertRaises(ValueError):
get_photon_frequency(-500e-9)


# **************************************************************************************

if __name__ == "__main__":
unittest.main()

# **************************************************************************************