Monday, December 16, 2024

Looking at particles from an object oriented programming view

This program is designed to implement classes for particles. The normal method for programmers is to look at how things are the same and then subclass the differences.  In this case a photon is just a particle and matter just adds rest mass and overrides like two functions.  
 

Photon Properties:

Frequency (Hz): 1

Wavelength (m): 299792458.0

Rotation (radians): 6.283185307179586

Energy (J): 6.62607015e-34

Momentum (kg·m/s): 2.2102190943042335e-42

Velocity (m/s): 299792457.99999994

Relativistic Mass (kg): 7.372497323812708e-51


Matter Particle Properties:

Frequency (Hz): 1

Wavelength (m): 299792458.0

Rotation (radians): 6.283185307179586

Energy (J): 8.187659678292408e-14

Momentum (kg·m/s): 2.2102190943042335e-42

Velocity (m/s): 2.4261460969311017e-12

Relativistic Mass (kg): 9.11e-31

Rest Mass (kg): 9.11e-31

 

 

import math


class Particle:

    """Unified base class for all particles."""

    def __init__(self, frequency=None, wavelength=None, medium_refractive_index=1.0):

        self.c = 299792458  # Speed of light in vacuum (m/s)

        if frequency is None and wavelength is None:

            raise ValueError("Either frequency or wavelength must be provided.")

        

        if frequency is not None:

            self.frequency = frequency  # (Hz)

            self.wavelength = self.c / frequency  # Calculate wavelength from frequency

        else:

            self.wavelength = wavelength  # (m)

            self.frequency = self.c / wavelength  # Calculate frequency from wavelength

            

        self.h = 6.62607015e-34  # Planck's constant (J·s)

        self.n = medium_refractive_index  # Refractive index of medium


    @property

    def speed_of_light(self):

        """Speed of light in the medium: c / n """

        return self.c / self.n


    def calculate_rotation(self):

        """Rotation: 2πf radians """

        return 2 * math.pi * self.frequency


    def calculate_energy(self):

        """Total energy: E = hf """

        return self.h * self.frequency


    def calculate_momentum(self):

        """Momentum: p = E / v = h / λ """

        return self.h / self.wavelength


    def calculate_wavelength(self):

        """Wavelength: λ = c / f """

        return self.speed_of_light / self.frequency


    def calculate_relativistic_mass(self):

        """Relativistic mass: E / c² """

        return self.calculate_energy() / self.speed_of_light**2


    def calculate_velocity(self):

        """ Velocity: v = p / m  """

        return self.calculate_momentum() / self.calculate_relativistic_mass()


    def get_properties(self):

        """Unified output for all key properties."""

        return {

            "Frequency (Hz)": self.frequency,

            "Wavelength (m)": self.calculate_wavelength(),

            "Rotation (radians)": self.calculate_rotation(),

            "Energy (J)": self.calculate_energy(),

            "Momentum (kg·m/s)": self.calculate_momentum(),

            "Velocity (m/s)": self.calculate_velocity(),

            "Relativistic Mass (kg)": self.calculate_relativistic_mass()

        }



class Photon(Particle):

    def __init__(self, frequency, medium_refractive_index=1.0):

        super().__init__(frequency, medium_refractive_index=medium_refractive_index)



class Matter(Particle):

    def __init__(self, frequency, rest_mass, medium_refractive_index=1.0):

        super().__init__(frequency, medium_refractive_index)

        self.rest_mass = rest_mass  # Rest mass (kg)


    def calculate_energy(self):

        """Total energy for matter: E² = (pc)² + (m₀c²)²."""

        rest_energy = self.rest_mass * self.c**2

        momentum_energy = (self.calculate_momentum() * self.c)**2

        return math.sqrt(rest_energy**2 + momentum_energy)


    def calculate_relativistic_mass(self):

        """Relativistic mass: E / c²."""

        return self.calculate_energy() / self.speed_of_light**2


    def get_properties(self):

        """Returns all properties for a matter particle."""

        properties = super().get_properties()

        properties.update({

            "Rest Mass (kg)": self.rest_mass

        })

        return properties


# Create a Photon with a given frequency

photon = Photon(frequency=1)  

photon_properties = photon.get_properties()


# Print the properties of the photon

print("Photon Properties:")

for key, value in photon_properties.items():

    print(f"{key}: {value}")



# Create a Matter particle with a given frequency and rest mass

matter_particle = Matter(frequency=1, rest_mass=9.11e-31) 

matter_properties = matter_particle.get_properties()


# Print the properties of the matter particle

print("\nMatter Particle Properties:")

for key, value in matter_properties.items():

    print(f"{key}: {value}")


No comments:

Post a Comment