Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Phyth::Electromagnetics::PointCharge Class Reference

Point charge with full electromagnetic field and dynamics. More...

#include <PointCharge.hpp>

Inheritance diagram for Phyth::Electromagnetics::PointCharge:
Inheritance graph
[legend]
Collaboration diagram for Phyth::Electromagnetics::PointCharge:
Collaboration graph
[legend]

Public Member Functions

 PointCharge (const Quantity< Kilogram > mass, const Vector3< Quantity< Meter > > &position, const Quantity< Coulomb > charge_value)
 Construct a point charge.
 
void SetComputeForcesFunction (const std::function< void(PointCharge *)> &func) noexcept
 Set the force computation function.
 
Vector3< Quantity< NewtonPerCoulomb > > GetElectricFieldAt (const Vector3< Quantity< Meter > > &point) const override
 Get the electric field at a point using Liénard-Wiechert formulas.
 
Vector3< Quantity< Tesla > > GetMagneticFieldAt (const Vector3< Quantity< Meter > > &point) const override
 Get the magnetic field at a point using Liénard-Wiechert formulas.
 
Quantity< Volt > GetElectricPotentialAt (const Vector3< Quantity< Meter > > &point) const override
 Get the electric potential at a point.
 
void ApplyElectricFieldForce (const Vector3< Quantity< NewtonPerCoulomb > > &field)
 Apply force from an electric field: F = q * E.
 
void ApplyMagneticFieldForce (const Vector3< Quantity< Tesla > > &field)
 Apply force from a magnetic field: F = q * v cross B.
 
void ApplyLorentzForce (const Vector3< Quantity< NewtonPerCoulomb > > &electric_field, const Vector3< Quantity< Tesla > > &magnetic_field)
 Apply the full Lorentz force: F = q * (E + v cross B)
 
Quantity< Coulomb > GetTotalChargeValue () const override
 Get the total charge value.
 
Quantity< Meter > GetMinimumDistanceTo (const Vector3< Quantity< Meter > > &point) const override
 Get the minimum distance from a point to the charge location.
 
void Integrate () noexcept override
 Integrate dynamics using velocity Verlet.
 
- Public Member Functions inherited from Phyth::Mechanics::Particle
 Particle (const Quantity< Kilogram > &mass, const Vector3< Quantity< Meter > > &position) noexcept
 Construct a particle with mass and initial position.
 
virtual ~Particle ()=default
 
void ApplyForce (const Vector3< Quantity< Newton > > &force)
 Accumulate an external force on the particle.
 
constexpr const auto & GetPosition () const noexcept
 
void SetPosition (const Vector3< Quantity< Meter > > &position) noexcept
 Set the particle position (m)
 
constexpr auto GetMass () const noexcept
 
void SetMass (const Quantity< Kilogram > &mass) noexcept
 Set the particle mass (kg)
 
constexpr const auto & GetVelocity () const noexcept
 
void SetVelocity (const Vector3< Quantity< MeterPerSecond > > &velocity) noexcept
 Set the particle velocity (m/s)
 
constexpr bool IsFixed () const noexcept
 
void SetFixed (const bool fixed) noexcept
 Set whether the particle is fixed (immovable)
 
constexpr const auto & GetExternalForce () const noexcept
 
void SetExternalForce (const Vector3< Quantity< Newton > > &external_force) noexcept
 Manually set the external force (N)
 
void SetComputeForcesFunction (const std::function< void(Particle *)> &func) noexcept
 Set the user-defined force computation function.
 
Quantity< Joule > GetPotentialEnergy (const Quantity< Meter > y_ref=0) const
 Compute gravitational potential energy relative to a reference height.
 
Quantity< Joule > GetKineticEnergy () const noexcept
 Compute kinetic energy.
 
Quantity< Joule > GetTotalEnergy (const Quantity< Meter > y_ref=0) const noexcept
 Compute total mechanical energy.
 
- Public Member Functions inherited from Phyth::Electromagnetics::ChargeSource
virtual ~ChargeSource ()=default
 
virtual Quantity< CoulombPerCubicMeter > GetChargeDensityAt (const Vector3< Quantity< Meter > > &point) const
 Get the charge density at a point.
 
- Public Member Functions inherited from Phyth::Electromagnetics::MagneticSource
virtual ~MagneticSource ()=default
 

Private Member Functions

Vector3< Quantity< Meter > > GetPositionAtOffset (const Quantity< Second > &offset) const
 Get position at a time offset in the past.
 
Vector3< Quantity< MeterPerSecond > > GetVelocityAtOffset (const Quantity< Second > &offset) const
 Get velocity at a time offset in the past.
 
Vector3< Quantity< MeterPerSecondSquared > > GetAccelerationAtOffset (const Quantity< Second > &offset) const
 Get acceleration at a time offset in the past.
 
Quantity< Second > SolveRetardedTimeOffset (const Vector3< Quantity< Meter > > &point) const
 Solve for the retarded time offset using Newton's method.
 
Quantity< Second > SolveRetardedTimeOffsetBisection (const Vector3< Quantity< Meter > > &point, Quantity< Second > dt_low, Quantity< Second > dt_high) const
 Solve for the retarded time offset using bisection method.
 

Private Attributes

Quantity< Coulomb > charge_value_
 
TimeHistory< Vector3< Quantity< Meter > > > position_history_ {}
 
TimeHistory< Vector3< Quantity< MeterPerSecond > > > velocity_history_ {}
 
TimeHistory< Vector3< Quantity< MeterPerSecondSquared > > > acceleration_history_ {}
 

Additional Inherited Members

- Protected Attributes inherited from Phyth::Mechanics::Particle
Quantity< Kilogram > mass_
 
Vector3< Quantity< MeterPerSecond > > velocity_ {}
 
bool fixed_ {false}
 
Vector3< Quantity< Newton > > external_force_ {}
 
Vector3< Quantity< Meter > > position_
 
std::function< void(Particle *)> compute_forces_func_
 

Detailed Description

Point charge with full electromagnetic field and dynamics.

PointCharge represents a moving charged particle that produces electric and magnetic fields via the Liénard-Wiechert potentials. It inherits from Particle for dynamics and implements both ChargeSource and MagneticSource interfaces.

The electric field consists of two components:

Example: auto charge = std::make_shared<PointCharge>( 1.0_kg, Vector3<Quantity<Meter>>(0_m, 0_m, 0_m), 1.0_C );

// Add a force function (e.g., Lorentz force) charge->SetComputeForcesFunction([](PointCharge* q) { Vector3<Quantity<NewtonPerCoulomb>> E(0_V/m, 0_V/m, -1.0_V/m); q->ApplyElectricFieldForce(E); });

// In simulation loop: charge->Integrate(); auto E = charge->GetElectricFieldAt(point); auto B = charge->GetMagneticFieldAt(point);

Note: The field and potential diverge at the charge location. Querying at the charge position throws an exception.

Definition at line 43 of file PointCharge.hpp.

Constructor & Destructor Documentation

◆ PointCharge()

Phyth::Electromagnetics::PointCharge::PointCharge ( const Quantity< Kilogram >  mass,
const Vector3< Quantity< Meter > > &  position,
const Quantity< Coulomb >  charge_value 
)
inlineexplicit

Construct a point charge.

Parameters
massMass of the particle (kg)
positionInitial position (m)
charge_valueElectric charge (Coulombs)

Definition at line 54 of file PointCharge.hpp.

Member Function Documentation

◆ ApplyElectricFieldForce()

void Phyth::Electromagnetics::PointCharge::ApplyElectricFieldForce ( const Vector3< Quantity< NewtonPerCoulomb > > &  field)
inline

Apply force from an electric field: F = q * E.

Parameters
fieldElectric field intensity at the charge position

Definition at line 223 of file PointCharge.hpp.

◆ ApplyLorentzForce()

void Phyth::Electromagnetics::PointCharge::ApplyLorentzForce ( const Vector3< Quantity< NewtonPerCoulomb > > &  electric_field,
const Vector3< Quantity< Tesla > > &  magnetic_field 
)
inline

Apply the full Lorentz force: F = q * (E + v cross B)

Parameters
electric_fieldElectric field intensity (N/C)
magnetic_fieldMagnetic flux density (Tesla)

Definition at line 242 of file PointCharge.hpp.

◆ ApplyMagneticFieldForce()

void Phyth::Electromagnetics::PointCharge::ApplyMagneticFieldForce ( const Vector3< Quantity< Tesla > > &  field)
inline

Apply force from a magnetic field: F = q * v cross B.

Parameters
fieldMagnetic flux density at the charge position

Definition at line 232 of file PointCharge.hpp.

◆ GetAccelerationAtOffset()

Vector3< Quantity< MeterPerSecondSquared > > Phyth::Electromagnetics::PointCharge::GetAccelerationAtOffset ( const Quantity< Second > &  offset) const
inlineprivate

Get acceleration at a time offset in the past.

Definition at line 309 of file PointCharge.hpp.

◆ GetElectricFieldAt()

Vector3< Quantity< NewtonPerCoulomb > > Phyth::Electromagnetics::PointCharge::GetElectricFieldAt ( const Vector3< Quantity< Meter > > &  point) const
inlineoverridevirtual

Get the electric field at a point using Liénard-Wiechert formulas.

The electric field is: E = k_E * q * (hat_R - beta) / (gamma^2 * R^2 * (1 - hat_R dot beta)^3)

  • k_E * q * (hat_R cross ((hat_R - beta) cross beta_dot)) / (c * R * (1 - hat_R dot beta)^3)

The first term is the velocity field (1/r^2), the second is the acceleration field (radiation, 1/r).

Parameters
pointPosition to evaluate the field (meters)
Returns
Electric field intensity E (N/C)
Exceptions
std::runtime_errorIf point is at the charge location

Implements Phyth::Electromagnetics::ChargeSource.

Definition at line 95 of file PointCharge.hpp.

◆ GetElectricPotentialAt()

Quantity< Volt > Phyth::Electromagnetics::PointCharge::GetElectricPotentialAt ( const Vector3< Quantity< Meter > > &  point) const
inlineoverridevirtual

Get the electric potential at a point.

The Liénard-Wiechert potential is: V = k_E * q / (R - hat_R dot beta * R)

Parameters
pointPosition to evaluate the potential (meters)
Returns
Electric potential V (Volts)
Exceptions
std::runtime_errorIf point is at the charge location or the denominator factor approaches zero

Implements Phyth::Electromagnetics::ChargeSource.

Definition at line 182 of file PointCharge.hpp.

◆ GetMagneticFieldAt()

Vector3< Quantity< Tesla > > Phyth::Electromagnetics::PointCharge::GetMagneticFieldAt ( const Vector3< Quantity< Meter > > &  point) const
inlineoverridevirtual

Get the magnetic field at a point using Liénard-Wiechert formulas.

The magnetic field is related to the electric field by: B = (hat_R / c) cross E

Parameters
pointPosition to evaluate the field (meters)
Returns
Magnetic flux density B (Tesla)
Exceptions
std::runtime_errorIf point is at the charge location

Implements Phyth::Electromagnetics::MagneticSource.

Definition at line 151 of file PointCharge.hpp.

◆ GetMinimumDistanceTo()

Quantity< Meter > Phyth::Electromagnetics::PointCharge::GetMinimumDistanceTo ( const Vector3< Quantity< Meter > > &  point) const
inlineoverridevirtual

Get the minimum distance from a point to the charge location.

Parameters
pointPosition to compute distance from
Returns
Distance to the charge location (meters)

Implements Phyth::Electromagnetics::ChargeSource.

Definition at line 264 of file PointCharge.hpp.

◆ GetPositionAtOffset()

Vector3< Quantity< Meter > > Phyth::Electromagnetics::PointCharge::GetPositionAtOffset ( const Quantity< Second > &  offset) const
inlineprivate

Get position at a time offset in the past.

Definition at line 291 of file PointCharge.hpp.

◆ GetTotalChargeValue()

Quantity< Coulomb > Phyth::Electromagnetics::PointCharge::GetTotalChargeValue ( ) const
inlineoverridevirtual

Get the total charge value.

Returns
The charge of the particle (Coulombs)

Implements Phyth::Electromagnetics::ChargeSource.

Definition at line 253 of file PointCharge.hpp.

◆ GetVelocityAtOffset()

Vector3< Quantity< MeterPerSecond > > Phyth::Electromagnetics::PointCharge::GetVelocityAtOffset ( const Quantity< Second > &  offset) const
inlineprivate

Get velocity at a time offset in the past.

Definition at line 300 of file PointCharge.hpp.

◆ Integrate()

void Phyth::Electromagnetics::PointCharge::Integrate ( )
inlineoverridevirtualnoexcept

Integrate dynamics using velocity Verlet.

Overrides Particle::Integrate to also update history buffers for retarded field computations.

Reimplemented from Phyth::Mechanics::Particle.

Definition at line 274 of file PointCharge.hpp.

◆ SetComputeForcesFunction()

void Phyth::Electromagnetics::PointCharge::SetComputeForcesFunction ( const std::function< void(PointCharge *)> &  func)
inlinenoexcept

Set the force computation function.

Parameters
funcFunction that applies forces to the charge via ApplyForce()

This wraps the Particle::SetComputeForcesFunction to accept PointCharge pointers directly.

Definition at line 72 of file PointCharge.hpp.

◆ SolveRetardedTimeOffset()

Quantity< Second > Phyth::Electromagnetics::PointCharge::SolveRetardedTimeOffset ( const Vector3< Quantity< Meter > > &  point) const
inlineprivate

Solve for the retarded time offset using Newton's method.

Solves R(t_ret) = c * (t - t_ret) for t_ret. Falls back to bisection if Newton fails.

Definition at line 323 of file PointCharge.hpp.

◆ SolveRetardedTimeOffsetBisection()

Quantity< Second > Phyth::Electromagnetics::PointCharge::SolveRetardedTimeOffsetBisection ( const Vector3< Quantity< Meter > > &  point,
Quantity< Second >  dt_low,
Quantity< Second >  dt_high 
) const
inlineprivate

Solve for the retarded time offset using bisection method.

Fallback when Newton's method fails to converge.

Exceptions
std::runtime_errorIf no solution exists in the interval

Definition at line 375 of file PointCharge.hpp.

Member Data Documentation

◆ acceleration_history_

TimeHistory<Vector3<Quantity<MeterPerSecondSquared> > > Phyth::Electromagnetics::PointCharge::acceleration_history_ {}
private

Definition at line 287 of file PointCharge.hpp.

◆ charge_value_

Quantity<Coulomb> Phyth::Electromagnetics::PointCharge::charge_value_
private

Definition at line 283 of file PointCharge.hpp.

◆ position_history_

TimeHistory<Vector3<Quantity<Meter> > > Phyth::Electromagnetics::PointCharge::position_history_ {}
private

Definition at line 285 of file PointCharge.hpp.

◆ velocity_history_

TimeHistory<Vector3<Quantity<MeterPerSecond> > > Phyth::Electromagnetics::PointCharge::velocity_history_ {}
private

Definition at line 286 of file PointCharge.hpp.


The documentation for this class was generated from the following file: