Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Particle.hpp
Go to the documentation of this file.
1#ifndef PHYTH_PARTICLE_H
2#define PHYTH_PARTICLE_H
3
4#include <functional>
5
10
11namespace Phyth::Mechanics {
36 class Particle {
37 public:
49
50 virtual ~Particle() = default;
51
64 if (fixed_) return;
66 }
67
101
103 [[nodiscard]] constexpr const auto &GetPosition() const noexcept { return position_; }
104
107
109 [[nodiscard]] constexpr auto GetMass() const noexcept { return mass_; }
110
112 void SetMass(const Quantity<Kilogram> &mass) noexcept { mass_ = mass; }
113
115 [[nodiscard]] constexpr const auto &GetVelocity() const noexcept { return velocity_; }
116
119
121 [[nodiscard]] constexpr bool IsFixed() const noexcept { return fixed_; }
122
124 void SetFixed(const bool fixed) noexcept { fixed_ = fixed; }
125
127 [[nodiscard]] constexpr const auto &GetExternalForce() const noexcept { return external_force_; }
128
133
147 void SetComputeForcesFunction(const std::function<void(Particle *)> &func) noexcept {
149 }
150
163
170 return 0.5 * mass_ * velocity_.LengthSquared();
171 }
172
185
191 friend std::ostream &operator<<(std::ostream &os, const Particle &p) {
192 os << "Particle{pos=" << p.position_ << ", mass=" << p.mass_ << "}";
193 return os;
194 }
195
196 protected:
199 bool fixed_{false};
200
203
204 std::function<void(Particle *)> compute_forces_func_ = [](Particle *) {
205 };
206 };
207}
208
209#endif // PHYTH_PARTICLE_H
Physical particle with mass, position, velocity, and force accumulation.
Definition Particle.hpp:36
constexpr const auto & GetVelocity() const noexcept
Definition Particle.hpp:115
Quantity< Kilogram > mass_
Definition Particle.hpp:197
Vector3< Quantity< MeterPerSecond > > velocity_
Definition Particle.hpp:198
Vector3< Quantity< Meter > > position_
Definition Particle.hpp:202
Quantity< Joule > GetPotentialEnergy(const Quantity< Meter > y_ref=0) const
Compute gravitational potential energy relative to a reference height.
Definition Particle.hpp:160
constexpr bool IsFixed() const noexcept
Definition Particle.hpp:121
void SetExternalForce(const Vector3< Quantity< Newton > > &external_force) noexcept
Manually set the external force (N)
Definition Particle.hpp:130
Quantity< Joule > GetKineticEnergy() const noexcept
Compute kinetic energy.
Definition Particle.hpp:169
Vector3< Quantity< Newton > > external_force_
Definition Particle.hpp:201
void SetMass(const Quantity< Kilogram > &mass) noexcept
Set the particle mass (kg)
Definition Particle.hpp:112
void SetPosition(const Vector3< Quantity< Meter > > &position) noexcept
Set the particle position (m)
Definition Particle.hpp:106
constexpr const auto & GetPosition() const noexcept
Definition Particle.hpp:103
void SetVelocity(const Vector3< Quantity< MeterPerSecond > > &velocity) noexcept
Set the particle velocity (m/s)
Definition Particle.hpp:118
void SetFixed(const bool fixed) noexcept
Set whether the particle is fixed (immovable)
Definition Particle.hpp:124
Particle(const Quantity< Kilogram > &mass, const Vector3< Quantity< Meter > > &position) noexcept
Construct a particle with mass and initial position.
Definition Particle.hpp:46
friend std::ostream & operator<<(std::ostream &os, const Particle &p)
Stream insertion operator for Particle.
Definition Particle.hpp:191
std::function< void(Particle *)> compute_forces_func_
Definition Particle.hpp:204
virtual void Integrate() noexcept
Integrate equations of motion using velocity Verlet.
Definition Particle.hpp:83
void ApplyForce(const Vector3< Quantity< Newton > > &force)
Accumulate an external force on the particle.
Definition Particle.hpp:63
Quantity< Joule > GetTotalEnergy(const Quantity< Meter > y_ref=0) const noexcept
Compute total mechanical energy.
Definition Particle.hpp:182
virtual ~Particle()=default
constexpr const auto & GetExternalForce() const noexcept
Definition Particle.hpp:127
void SetComputeForcesFunction(const std::function< void(Particle *)> &func) noexcept
Set the user-defined force computation function.
Definition Particle.hpp:147
constexpr auto GetMass() const noexcept
Definition Particle.hpp:109
constexpr auto g
constexpr bool is_unit_v
Definition Unit.hpp:79
Physical quantity with a value and a compile-time unit.
Definition Quantity.hpp:27
3D vector with Quantity components
Definition Vector3.hpp:28