| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #pragma once |
|---|
| 24 | #ifndef __NBODY_HPP20061016161026 |
|---|
| 25 | #define __NBODY_HPP20061016161026 |
|---|
| 26 | |
|---|
| 27 | #include <cmath> |
|---|
| 28 | #include <vector> |
|---|
| 29 | #include "Triplet.hpp" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | class Universe |
|---|
| 35 | { |
|---|
| 36 | public: |
|---|
| 37 | static double gravity() { return double(6.67e-11); } |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | struct Force : public Triplet<double> |
|---|
| 42 | { |
|---|
| 43 | Force(double fx, double fy, double fz) : Triplet<double>(fx, fy, fz) {} |
|---|
| 44 | |
|---|
| 45 | template <class ForceIter> |
|---|
| 46 | static Force superposition(ForceIter begin, ForceIter end) |
|---|
| 47 | { |
|---|
| 48 | Force f; |
|---|
| 49 | for(ForceIter i = begin; i != end; ++i) |
|---|
| 50 | f += *i; |
|---|
| 51 | return f; |
|---|
| 52 | } |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | typedef Triplet<double> Velocity; |
|---|
| 56 | typedef Triplet<double> Acceleration; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | class Particle : public Triplet<double> |
|---|
| 63 | { |
|---|
| 64 | public: |
|---|
| 65 | Particle(double x, double y, double z, double mass) |
|---|
| 66 | : Triplet<double>(x, y, z), _mass(mass) |
|---|
| 67 | {} |
|---|
| 68 | virtual ~Particle() {} |
|---|
| 69 | |
|---|
| 70 | Force gravitationalForce(const Particle& other, double G) const |
|---|
| 71 | { |
|---|
| 72 | Triplet<double> d = *this - other; |
|---|
| 73 | double distSquare = d.x * d.x + d.y * d.y + d.z * d.z; |
|---|
| 74 | double c = G * _mass * other._mass / (distSquare * std::sqrt(distSquare)); |
|---|
| 75 | return Force(c * d.x, c * d.y, c * d.z); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | template <class ParticleIter> |
|---|
| 85 | void updateAcceleration(ParticleIter begin, ParticleIter end, double G) |
|---|
| 86 | { |
|---|
| 87 | std::vector<Force> force; |
|---|
| 88 | for(ParticleIter i = begin; i != end; ++i) |
|---|
| 89 | { |
|---|
| 90 | if(this != i) |
|---|
| 91 | force.push_back(gravitationalForce(*i, G)); |
|---|
| 92 | } |
|---|
| 93 | Force currentForce = Force::superposition(force.begin(), force.end()); |
|---|
| 94 | _acceleration = currentForce / _mass; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void move(double dt) |
|---|
| 98 | { |
|---|
| 99 | _velocity += _acceleration * dt; |
|---|
| 100 | *this += _velocity * dt; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | double getMass() const { return _mass; } |
|---|
| 104 | |
|---|
| 105 | const Velocity& getVelocity() const { return _velocity; } |
|---|
| 106 | void setVelocity(const Velocity& v) { _velocity = v; } |
|---|
| 107 | |
|---|
| 108 | const Acceleration& getAcceleration() const { return _acceleration; } |
|---|
| 109 | void setAcceleration(const Acceleration& a) { _acceleration = a; } |
|---|
| 110 | private: |
|---|
| 111 | double _mass; |
|---|
| 112 | Velocity _velocity; |
|---|
| 113 | Acceleration _acceleration; |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | #endif //__NBODY_HPP20061016161026 |
|---|