-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Ben1980/develop
Develop
- Loading branch information
Showing
13 changed files
with
444 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "particle.h" | ||
#include <random> | ||
#include <functional> | ||
#include <chrono> | ||
#include <cassert> | ||
|
||
size_t Particle::IDCounter = 0; | ||
|
||
Particle::Particle() | ||
: mass(0) { | ||
id = IDCounter++; | ||
} | ||
|
||
Particle::Particle(double mass, const Vector2D &acceleration, const Vector2D &velocity, const Vector2D &position) | ||
: mass(mass), acceleration(acceleration), velocity(velocity), position(position) { | ||
assert(mass > 0); | ||
id = IDCounter++; | ||
} | ||
|
||
bool Particle::operator==(const Particle &rhs) const { | ||
return id == rhs.id; | ||
} | ||
|
||
bool Particle::operator!=(const Particle &rhs) const { | ||
return !(rhs == *this); | ||
} | ||
|
||
const Vector2D &Particle::getAcceleration() const { | ||
return acceleration; | ||
} | ||
|
||
void Particle::setAcceleration(const Vector2D &acceleration) { | ||
Particle::acceleration = acceleration; | ||
} | ||
|
||
const Vector2D &Particle::getVelocity() const { | ||
return velocity; | ||
} | ||
|
||
void Particle::setVelocity(const Vector2D &velocity) { | ||
Particle::velocity = velocity; | ||
} | ||
|
||
const Vector2D &Particle::getPosition() const { | ||
return position; | ||
} | ||
|
||
void Particle::setPosition(const Vector2D &position) { | ||
Particle::position = position; | ||
} | ||
|
||
double Particle::getMass() const { | ||
return mass; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,85 @@ | ||
#include "solver.h" | ||
#include "particle.h" | ||
#include <cmath> | ||
#include <algorithm> | ||
#include <solver.h> | ||
#include <cassert> | ||
|
||
std::vector<Particle> Solver::solve(const std::vector<Particle> &particles, double epsilon) const | ||
{ | ||
return std::vector<Particle>(2) ; | ||
} | ||
|
||
const double Solver::G = 6.67408e-11; | ||
const double Solver::EPSILON = 1e-3; | ||
|
||
Solver::Solver(double mEpsilon) : mEpsilon(mEpsilon) {} | ||
|
||
std::vector<Particle> Solver::solve(const std::vector<Particle> &particles) const { | ||
std::vector<Particle> solution = calculateAcceleration(particles); | ||
solution = calculateVelocity(solution); | ||
solution = calculatePosition(solution); | ||
|
||
return solution; | ||
} | ||
|
||
std::vector<Particle> Solver::calculateAcceleration(const std::vector<Particle> &particles) const { | ||
std::vector<Particle> solution(particles.size()); | ||
|
||
std::transform(begin(particles), end(particles), begin(solution), [&particles](const Particle &particle) { | ||
return AccumulateAcceleration(particles, particle); | ||
}); | ||
|
||
return solution; | ||
} | ||
|
||
std::vector<Particle> Solver::calculateVelocity(const std::vector<Particle> &particles) const { | ||
std::vector<Particle> solution(particles.size()); | ||
|
||
std::transform(begin(particles), end(particles), begin(solution), [epsilon = mEpsilon](Particle particle) { | ||
const Vector2D v0 = particle.getVelocity(); | ||
particle.setVelocity(v0 + particle.getAcceleration()*epsilon); | ||
|
||
return particle; | ||
}); | ||
|
||
return solution; | ||
} | ||
|
||
std::vector<Particle> Solver::calculatePosition(const std::vector<Particle> &particles) const { | ||
std::vector<Particle> solution(particles.size()); | ||
|
||
std::transform(begin(particles), end(particles), begin(solution), [epsilon = mEpsilon](Particle particle) { | ||
const Vector2D v = particle.getVelocity(); | ||
const Vector2D r0 = particle.getPosition(); | ||
particle.setPosition(r0 + v*epsilon + particle.getAcceleration()*epsilon*epsilon); | ||
|
||
return particle; | ||
}); | ||
|
||
return solution; | ||
} | ||
|
||
Particle Solver::AccumulateAcceleration(const std::vector<Particle> &particles, const Particle &particle) { | ||
Particle particleA = particle; | ||
const double e3 = EPSILON*EPSILON*EPSILON; | ||
|
||
std::for_each(begin(particles), end(particles), [&particleA, e3](const Particle &particleB) { | ||
if(particleA != particleB) { | ||
const double M = CalculateEquivalentMass(particleA, particleB); | ||
const Vector2D r = particleB.getPosition() - particleA.getPosition(); | ||
const double rLength = r.length(); | ||
const double r3 = fabs(rLength*rLength*rLength); | ||
|
||
const Vector2D a0 = particleA.getAcceleration(); | ||
particleA.setAcceleration(a0 + G*M*r/(r3 + e3)); | ||
} | ||
}); | ||
|
||
return particleA; | ||
} | ||
|
||
double Solver::CalculateEquivalentMass(const Particle &particleA, const Particle &particleB) { | ||
const double massA = particleA.getMass(); | ||
assert(massA > 0); | ||
|
||
const double massB = particleB.getMass(); | ||
|
||
return massA*massB/massA; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "types.h" | ||
|
||
#include <cmath> | ||
#include <types.h> | ||
#include <cassert> | ||
#include <limits> | ||
|
||
bool Vector2D::operator==(const Vector2D &rhs) const { | ||
auto equalZero = std::numeric_limits<double>::min(); | ||
|
||
return fabs(x - rhs.x) <= equalZero && | ||
fabs(y - rhs.y) <= equalZero; | ||
} | ||
|
||
bool Vector2D::operator!=(const Vector2D &rhs) const { | ||
return !(rhs == *this); | ||
} | ||
|
||
double Vector2D::length() const { | ||
return sqrt(x*x + y*y); | ||
} | ||
|
||
Vector2D& Vector2D::operator-=(const Vector2D& rhs) { | ||
*this = *this - rhs; | ||
return *this; | ||
} | ||
|
||
Vector2D& Vector2D::operator*=(const double& rhs) { | ||
*this = *this * rhs; | ||
return *this; | ||
} | ||
|
||
Vector2D& Vector2D::operator/=(const double& rhs) { | ||
*this = *this / rhs; | ||
return *this; | ||
} | ||
|
||
Vector2D &Vector2D::operator+=(const Vector2D &rhs) { | ||
*this = *this + rhs; | ||
return *this; | ||
} | ||
|
||
Vector2D operator-(const Vector2D &lhs, const Vector2D &rhs) { | ||
return { lhs.x - rhs.x, | ||
lhs.y - rhs.y }; | ||
} | ||
|
||
Vector2D operator+(const Vector2D &lhs, const Vector2D &rhs) { | ||
return { lhs.x + rhs.x, | ||
lhs.y + rhs.y }; | ||
} | ||
|
||
Vector2D operator*(const Vector2D &lhs, const double &rhs) { | ||
return { lhs.x * rhs, | ||
lhs.y * rhs }; | ||
} | ||
|
||
Vector2D operator*(const double &lhs, const Vector2D &rhs) { | ||
return rhs * lhs; | ||
} | ||
|
||
Vector2D operator/(const Vector2D &lhs, const double &rhs) { | ||
assert(fabs(rhs) > 0); | ||
|
||
return { lhs.x / rhs, | ||
lhs.y / rhs }; | ||
} |
Oops, something went wrong.