|
Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
|
Physical particle with mass, position, velocity, and force accumulation. More...
#include <Particle.hpp>


Protected Attributes | |
| 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_ |
Friends | |
| std::ostream & | operator<< (std::ostream &os, const Particle &p) |
| Stream insertion operator for Particle. | |
Physical particle with mass, position, velocity, and force accumulation.
Particle is the fundamental building block for mechanical simulations. It stores the state (position, velocity, mass) and accumulates forces applied by constraints, springs, or user-defined force functions.
Integration is performed using the velocity Verlet algorithm, which is symplectic and energy-conserving for Hamiltonian systems.
Particles can be marked as fixed (immovable), which is useful for representing anchors or boundary conditions.
Example: auto p = std::make_shared<Particle>(1_kg, Vector3{0_m, 0_m, 0_m}); p->SetComputeForcesFunction([](Particle* p) { // Apply gravity p->ApplyForce({0_N, -1_kg * Consts::g, 0_N}); });
// In simulation loop: p->Integrate(); auto total_energy = p->GetTotalEnergy();
Definition at line 36 of file Particle.hpp.
|
inlinenoexcept |
Construct a particle with mass and initial position.
| mass | Mass of the particle (kg) |
| position | Initial position (m) |
Initial velocity is zero and the particle is not fixed.
Definition at line 46 of file Particle.hpp.
|
virtualdefault |
Accumulate an external force on the particle.
Forces are accumulated during the force computation phase and cleared after each integration step. This allows multiple force sources (gravity, springs, collisions) to contribute.
| force | Force to apply (N) |
Note: If the particle is fixed, this function does nothing.
Definition at line 63 of file Particle.hpp.
|
inlineconstexprnoexcept |
Definition at line 127 of file Particle.hpp.
Definition at line 109 of file Particle.hpp.
Definition at line 103 of file Particle.hpp.
|
inline |
Compute gravitational potential energy relative to a reference height.
| y_ref | Reference height (m), default 0 |
The default reference height is y=0. Positive energy means the particle is above the reference.
Definition at line 160 of file Particle.hpp.
|
inlinenoexcept |
Compute total mechanical energy.
| y_ref | Reference height for potential energy, default 0 |
For conservative systems, this quantity should remain constant. Energy drift can be used as a measure of numerical accuracy.
Definition at line 182 of file Particle.hpp.
Definition at line 115 of file Particle.hpp.
Integrate equations of motion using velocity Verlet.
The velocity Verlet algorithm consists of three steps:
The user-defined force function is called twice per integration step:
External forces are automatically cleared at the start and end. If the particle is fixed, this function does nothing.
Reimplemented in Phyth::Electromagnetics::MagneticDipole, and Phyth::Electromagnetics::PointCharge.
Definition at line 83 of file Particle.hpp.
Definition at line 121 of file Particle.hpp.
|
inlinenoexcept |
Set the user-defined force computation function.
| func | Function that applies forces to the particle via ApplyForce() (Only Apply Force) |
The function is called automatically during Integrate(). The default function does nothing.
Example: p->SetComputeForcesFunction([](Particle* p) { p->ApplyForce({0_N, -1_kg * Consts::g, 0_N}); // gravity });
Definition at line 147 of file Particle.hpp.
|
inlinenoexcept |
Manually set the external force (N)
Definition at line 130 of file Particle.hpp.
Set whether the particle is fixed (immovable)
Definition at line 124 of file Particle.hpp.
Set the particle mass (kg)
Definition at line 112 of file Particle.hpp.
|
inlinenoexcept |
Set the particle position (m)
Definition at line 106 of file Particle.hpp.
|
inlinenoexcept |
Set the particle velocity (m/s)
Definition at line 118 of file Particle.hpp.
Stream insertion operator for Particle.
Output format: "Particle{pos=(x, y, z), mass=value}"
Definition at line 191 of file Particle.hpp.
Definition at line 204 of file Particle.hpp.
Definition at line 201 of file Particle.hpp.
Definition at line 199 of file Particle.hpp.
Definition at line 197 of file Particle.hpp.
Definition at line 202 of file Particle.hpp.
|
protected |
Definition at line 198 of file Particle.hpp.