root/sandbox/trunk/pdp/src/N-body.hpp @ 936

Revision 936, 3.4 kB (checked in by leo, 7 years ago)

add n-body simulation project

Line 
1/*--------------------------------------------------------------------------
2 *  Copyright 2006 Taro L. Saito
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 *--------------------------------------------------------------------------*/
16//--------------------------------------------------
17// N-body.hpp
18// Since  2006/10/16 16:06:36
19//
20// $Date$
21// $Author$
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 * The class Universe defines global constant values;
33 */ 
34class Universe
35{
36public:
37        static double gravity() { return double(6.67e-11); }
38};
39
40
41struct 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
55typedef Triplet<double> Velocity;
56typedef Triplet<double> Acceleration;
57
58
59/**
60* Particle class represents a two-dimensional point in the universe.
61*/
62class Particle : public Triplet<double>
63{
64public:
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); // return a projection of F into each of x, y, z-plane.
76
77                // In precise, the above calculation process is as follows:
78                //
79                // double r = std::sqrt(distSquare);
80                // double F = (Universe::gravity() * _mass * other._mass) / (r * r);
81                // return Force(F * (dx / r), F * (dy / r), F * (dz / r));     
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;   // F = ma
95        }
96
97        void move(double dt)
98        {
99                _velocity += _acceleration * dt;        // V' = V + A * dt
100                *this += _velocity * dt;                        // P' = P + V * 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; }
110private:
111        double _mass;
112        Velocity _velocity;
113        Acceleration _acceleration;
114};
115
116
117#endif //__NBODY_HPP20061016161026
Note: See TracBrowser for help on using the browser.