Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Spring.hpp
Go to the documentation of this file.
1#ifndef PHYTH_SPRING_H
2#define PHYTH_SPRING_H
3
4#include <stdexcept>
5#include <cmath>
6#include <memory>
7#include <utility>
8
9#include "Particle.hpp"
10
11namespace Phyth::Mechanics {
37 class Spring {
38 public:
53 Spring(std::shared_ptr<Particle> p1, std::shared_ptr<Particle> p2,
56 : p1_(std::move(p1)), p2_(std::move(p2)),
59 rest_length_((p1_->GetPosition() - p2_->GetPosition()).Length()) {
63 }
64
99
101 [[nodiscard]] const std::shared_ptr<Particle> &GetParticle1() const { return p1_; }
102
104 [[nodiscard]] const std::shared_ptr<Particle> &GetParticle2() const { return p2_; }
105
108
111
114
117 return (p1_->GetPosition() - p2_->GetPosition()).Length();
118 }
119
122
133
144
155
162 const auto elongation = GetElongation();
163 return 0.5 * stiffness_ * elongation * elongation;
164 }
165
177
178 private:
179 std::shared_ptr<Particle> p1_;
180 std::shared_ptr<Particle> p2_;
181
185
187 if (const auto len = length.value; !std::isfinite(len) || len <= 0.0) {
188 throw std::invalid_argument("Spring::validateRestLength: rest length must be a positive finite number");
189 }
190 }
191
193 if (stiffness.value < 0.0) {
194 throw std::invalid_argument("Spring::validateStiffness: stiffness must be non-negative");
195 }
196 }
197
199 if (damping.value < 0.0) {
200 throw std::invalid_argument("Spring::validateDamping: damping must be non-negative");
201 }
202 }
203 };
204}
205
206#endif // PHYTH_SPRING_H
Spring constraint between two particles.
Definition Spring.hpp:37
Quantity< Newton > GetEnergyDensity() const
Compute energy per unit length.
Definition Spring.hpp:174
void Constrain() const
Apply spring and damping forces to both particles (Force Constrain)
Definition Spring.hpp:76
const Quantity< Meter > & GetRestLength() const
Definition Spring.hpp:107
Quantity< Meter > GetElongation() const
Definition Spring.hpp:121
Quantity< KilogramPerSecond > damping_
Definition Spring.hpp:183
Quantity< NewtonPerMeter > stiffness_
Definition Spring.hpp:182
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.
Definition Spring.hpp:53
const std::shared_ptr< Particle > & GetParticle1() const
Definition Spring.hpp:101
void SetDamping(const Quantity< KilogramPerSecond > &damping)
Set a new damping coefficient.
Definition Spring.hpp:151
Quantity< Joule > GetElasticEnergy() const
Compute the elastic potential energy stored in the spring.
Definition Spring.hpp:161
Quantity< Meter > rest_length_
Definition Spring.hpp:184
Quantity< Meter > GetCurrentLength() const
Definition Spring.hpp:116
void SetRestLength(const Quantity< Meter > &rest_length)
Set a new rest length.
Definition Spring.hpp:129
std::shared_ptr< Particle > p2_
Definition Spring.hpp:180
const std::shared_ptr< Particle > & GetParticle2() const
Definition Spring.hpp:104
static void validateDamping(const Quantity< KilogramPerSecond > &damping)
Definition Spring.hpp:198
const Quantity< KilogramPerSecond > & GetDamping() const
Definition Spring.hpp:113
const Quantity< NewtonPerMeter > & GetStiffness() const
Definition Spring.hpp:110
void SetStiffness(const Quantity< NewtonPerMeter > &stiffness)
Set a new stiffness.
Definition Spring.hpp:140
static void validateRestLength(const Quantity< Meter > &length)
Definition Spring.hpp:186
static void validateStiffness(const Quantity< NewtonPerMeter > &stiffness)
Definition Spring.hpp:192
std::shared_ptr< Particle > p1_
Definition Spring.hpp:179
constexpr bool is_unit_v
Definition Unit.hpp:79
Compile-time physical dimension as 7 SI base exponents.
Definition Dimension.hpp:25
Physical quantity with a value and a compile-time unit.
Definition Quantity.hpp:27
3D vector with Quantity components
Definition Vector3.hpp:28
Vector3< Scalar > Normalized() const
Normalized vector (unit vector in the same direction)
Definition Vector3.hpp:382