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

Spring constraint between two particles. More...

#include <Spring.hpp>

Collaboration diagram for Phyth::Mechanics::Spring:
Collaboration graph
[legend]

Public Member Functions

 Spring (std::shared_ptr< Particle > p1, std::shared_ptr< Particle > p2, const Quantity< NewtonPerMeter > &stiffness, const Quantity< KilogramPerSecond > &damping=Quantity< KilogramPerSecond >{0})
 Construct a spring between two particles.
 
void Constrain () const
 Apply spring and damping forces to both particles (Force Constrain)
 
const std::shared_ptr< Particle > & GetParticle1 () const
 
const std::shared_ptr< Particle > & GetParticle2 () const
 
const Quantity< Meter > & GetRestLength () const
 
const Quantity< NewtonPerMeter > & GetStiffness () const
 
const Quantity< KilogramPerSecond > & GetDamping () const
 
Quantity< Meter > GetCurrentLength () const
 
Quantity< Meter > GetElongation () const
 
void SetRestLength (const Quantity< Meter > &rest_length)
 Set a new rest length.
 
void SetStiffness (const Quantity< NewtonPerMeter > &stiffness)
 Set a new stiffness.
 
void SetDamping (const Quantity< KilogramPerSecond > &damping)
 Set a new damping coefficient.
 
Quantity< Joule > GetElasticEnergy () const
 Compute the elastic potential energy stored in the spring.
 
Quantity< Newton > GetEnergyDensity () const
 Compute energy per unit length.
 

Static Private Member Functions

static void validateRestLength (const Quantity< Meter > &length)
 
static void validateStiffness (const Quantity< NewtonPerMeter > &stiffness)
 
static void validateDamping (const Quantity< KilogramPerSecond > &damping)
 

Private Attributes

std::shared_ptr< Particle > p1_
 
std::shared_ptr< Particle > p2_
 
Quantity< NewtonPerMeter > stiffness_
 
Quantity< KilogramPerSecond > damping_
 
Quantity< Meter > rest_length_
 

Detailed Description

Spring constraint between two particles.

Spring applies a Hooke's law force between two particles: F = -k * (current_length - rest_length) * direction

Additionally, a damping force proportional to the relative velocity along the spring axis can be added: F_damp = -c * (v1 - v2) · direction

The spring is bi-directional: equal and opposite forces are applied to both particles. If one particle is fixed, the other still moves correctly.

Example: auto p1 = std::make_shared<Particle>(1_kg, Vector3{0_m, 0_m, 0_m}); auto p2 = std::make_shared<Particle>(1_kg, Vector3{1_m, 0_m, 0_m});

Spring spring(p1, p2, 10.0_N/m, 0.5_kgps);

// In simulation loop: spring.Constrain();

auto energy = spring.GetElasticEnergy(); // 1/2 * k * elongation^2

Definition at line 37 of file Spring.hpp.

Constructor & Destructor Documentation

◆ Spring()

Phyth::Mechanics::Spring::Spring ( std::shared_ptr< Particle >  p1,
std::shared_ptr< Particle >  p2,
const Quantity< NewtonPerMeter > &  stiffness,
const Quantity< KilogramPerSecond > &  damping = Quantity<KilogramPerSecond>{0} 
)
inline

Construct a spring between two particles.

Parameters
p1First particle (force applied in direction p2->p1)
p2Second particle (force applied in direction p1->p2)
stiffnessSpring constant k (N/m)
dampingDamping coefficient c (kg/s), default 0

The rest length is automatically set to the initial distance between the two particles.

Exceptions
std::invalid_argumentIf rest length <= 0, stiffness < 0, or damping < 0

Definition at line 53 of file Spring.hpp.

Member Function Documentation

◆ Constrain()

void Phyth::Mechanics::Spring::Constrain ( ) const
inline

Apply spring and damping forces to both particles (Force Constrain)

The spring force is: F = k * (current_length - rest_length) * direction

If damping > 0, an additional damping force is applied: F_damp = c * (v1 - v2) · direction

Both forces are applied in opposite directions to p1 and p2.

Definition at line 76 of file Spring.hpp.

◆ GetCurrentLength()

Quantity< Meter > Phyth::Mechanics::Spring::GetCurrentLength ( ) const
inline
Returns
Current distance between the two particles

Definition at line 116 of file Spring.hpp.

◆ GetDamping()

const Quantity< KilogramPerSecond > & Phyth::Mechanics::Spring::GetDamping ( ) const
inline
Returns
Damping coefficient c (kg/s)

Definition at line 113 of file Spring.hpp.

◆ GetElasticEnergy()

Quantity< Joule > Phyth::Mechanics::Spring::GetElasticEnergy ( ) const
inline

Compute the elastic potential energy stored in the spring.

Returns
1/2 * k * elongation^2 (Joules)

Definition at line 161 of file Spring.hpp.

◆ GetElongation()

Quantity< Meter > Phyth::Mechanics::Spring::GetElongation ( ) const
inline
Returns
Current elongation (current_length - rest_length)

Definition at line 121 of file Spring.hpp.

◆ GetEnergyDensity()

Quantity< Newton > Phyth::Mechanics::Spring::GetEnergyDensity ( ) const
inline

Compute energy per unit length.

Returns
ElasticEnergy / CurrentLength (Newtons)

Note: If current_length is zero, this will produce a division by zero. Caller should ensure the particles are separated.

Definition at line 174 of file Spring.hpp.

◆ GetParticle1()

const std::shared_ptr< Particle > & Phyth::Mechanics::Spring::GetParticle1 ( ) const
inline
Returns
Shared pointer to the first particle

Definition at line 101 of file Spring.hpp.

◆ GetParticle2()

const std::shared_ptr< Particle > & Phyth::Mechanics::Spring::GetParticle2 ( ) const
inline
Returns
Shared pointer to the second particle

Definition at line 104 of file Spring.hpp.

◆ GetRestLength()

const Quantity< Meter > & Phyth::Mechanics::Spring::GetRestLength ( ) const
inline
Returns
Current rest length (constant unless modified)

Definition at line 107 of file Spring.hpp.

◆ GetStiffness()

const Quantity< NewtonPerMeter > & Phyth::Mechanics::Spring::GetStiffness ( ) const
inline
Returns
Spring stiffness constant k (N/m)

Definition at line 110 of file Spring.hpp.

◆ SetDamping()

void Phyth::Mechanics::Spring::SetDamping ( const Quantity< KilogramPerSecond > &  damping)
inline

Set a new damping coefficient.

Parameters
dampingNew damping coefficient (must be non-negative)
Exceptions
std::invalid_argumentIf damping < 0

Definition at line 151 of file Spring.hpp.

◆ SetRestLength()

void Phyth::Mechanics::Spring::SetRestLength ( const Quantity< Meter > &  rest_length)
inline

Set a new rest length.

Parameters
rest_lengthNew rest length (must be positive finite)
Exceptions
std::invalid_argumentIf rest_length <= 0

Definition at line 129 of file Spring.hpp.

◆ SetStiffness()

void Phyth::Mechanics::Spring::SetStiffness ( const Quantity< NewtonPerMeter > &  stiffness)
inline

Set a new stiffness.

Parameters
stiffnessNew stiffness (must be non-negative)
Exceptions
std::invalid_argumentIf stiffness < 0

Definition at line 140 of file Spring.hpp.

◆ validateDamping()

static void Phyth::Mechanics::Spring::validateDamping ( const Quantity< KilogramPerSecond > &  damping)
inlinestaticprivate

Definition at line 198 of file Spring.hpp.

◆ validateRestLength()

static void Phyth::Mechanics::Spring::validateRestLength ( const Quantity< Meter > &  length)
inlinestaticprivate

Definition at line 186 of file Spring.hpp.

◆ validateStiffness()

static void Phyth::Mechanics::Spring::validateStiffness ( const Quantity< NewtonPerMeter > &  stiffness)
inlinestaticprivate

Definition at line 192 of file Spring.hpp.

Member Data Documentation

◆ damping_

Quantity<KilogramPerSecond> Phyth::Mechanics::Spring::damping_
private

Definition at line 183 of file Spring.hpp.

◆ p1_

std::shared_ptr<Particle> Phyth::Mechanics::Spring::p1_
private

Definition at line 179 of file Spring.hpp.

◆ p2_

std::shared_ptr<Particle> Phyth::Mechanics::Spring::p2_
private

Definition at line 180 of file Spring.hpp.

◆ rest_length_

Quantity<Meter> Phyth::Mechanics::Spring::rest_length_
private

Definition at line 184 of file Spring.hpp.

◆ stiffness_

Quantity<NewtonPerMeter> Phyth::Mechanics::Spring::stiffness_
private

Definition at line 182 of file Spring.hpp.


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