Skip to content

Commit

Permalink
Merge pull request #4 from Ben1980/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Ben1980 authored Feb 28, 2019
2 parents 4913f38 + d072c77 commit a0c2e20
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 38 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ target_include_directories(Catch2 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/extern/c

include(CTest)
add_subdirectory(solver solver)
add_subdirectory(solverTest solverTest)
add_subdirectory(solverTest solverTest)

set_property(TARGET solver solverTest PROPERTY CXX_STANDARD 17)
2 changes: 1 addition & 1 deletion solver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(solver
src/solver.cpp include/particle.h include/particleBuilder.h src/particleBuilder.cpp)
src/solver.cpp include/particle.h include/particleBuilder.h src/particleBuilder.cpp include/types.h)

target_include_directories(solver
PUBLIC
Expand Down
9 changes: 9 additions & 0 deletions solver/include/particle.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#ifndef PARTICLE_H
#define PARTICLE_H

#include "types.h"

struct Particle {
double mass;
Vector2D acceleration;
Vector2D velocity;
Vector2D position;

Particle() : mass(0) {}
Particle(double mass, const Vector2D &acceleration, const Vector2D &velocity, const Vector2D &position)
: mass(mass), acceleration(acceleration), velocity(velocity), position(position) {}
};

#endif
20 changes: 15 additions & 5 deletions solver/include/particleBuilder.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
#ifndef PARTICLEBUILDER_H
#define PARTICLEBUILDER_H

#include "types.h"

class Particle;

class ParticleBuilder {
public:
ParticleBuilder * position();
ParticleBuilder * velocity();
ParticleBuilder * acceleration();
ParticleBuilder * mass();
Particle build();
ParticleBuilder() : mMass(0) {}

ParticleBuilder & position(const Vector2D &position);
ParticleBuilder & velocity(const Vector2D &velocity);
ParticleBuilder & acceleration(const Vector2D &acceleration);
ParticleBuilder & mass(double mass);
Particle build() const;

private:
double mMass;
Vector2D mAcceleration;
Vector2D mVelocity;
Vector2D mPosition;
};

#endif
12 changes: 12 additions & 0 deletions solver/include/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef TYPES_H
#define TYPES_H

struct Vector2D {
double x;
double y;

Vector2D() : x(0), y(0) {}
Vector2D(double x, double y) : x(x), y(y) {}
};

#endif
25 changes: 14 additions & 11 deletions solver/src/particleBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
#include "particleBuilder.h"
#include "particle.h"

ParticleBuilder * ParticleBuilder::position()
ParticleBuilder & ParticleBuilder::position(const Vector2D &position)
{
return this;
mPosition = position;
return *this;
}

ParticleBuilder * ParticleBuilder::velocity()
ParticleBuilder & ParticleBuilder::velocity(const Vector2D &velocity)
{
return this;
mVelocity = velocity;
return *this;
}

ParticleBuilder * ParticleBuilder::acceleration()
ParticleBuilder & ParticleBuilder::acceleration(const Vector2D &acceleration)
{
return this;
mAcceleration = acceleration;
return *this;
}

ParticleBuilder * ParticleBuilder::mass()
ParticleBuilder & ParticleBuilder::mass(double mass)
{
return this;
mMass = mass;
return *this;
}

Particle ParticleBuilder::build()
Particle ParticleBuilder::build() const
{
return {};
return {mMass, mAcceleration, mVelocity, mPosition};
}

2 changes: 1 addition & 1 deletion solver/src/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

std::vector<Particle> Solver::solve(const std::vector<Particle> &particles, double epsilon) const
{
return {};
return std::vector<Particle>(2) ;
}
65 changes: 46 additions & 19 deletions solverTest/src/solverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,60 @@
#include "particleBuilder.h"
#include <vector>

TEST_CASE( "Explicit euler algorithm with two mass points", "[euler]" ) {
double Inverse(double value) { return -value; }

Solver solver;
Particle GenerateStandardParticle(double xPosition, double yPosition)
{
ParticleBuilder particleBuilder;

std::unique_ptr<ParticleBuilder> particleBuilder = std::make_unique<ParticleBuilder>();
return particleBuilder
.position({xPosition, yPosition})
.mass(1e10)
.build();
}

Particle particleA = particleBuilder
->position()
->velocity()
->acceleration()
->mass()
->build();
TEST_CASE( "Explicit euler algorithm with two point mass", "[euler]" )
{
Solver solver;

Particle particleB = particleBuilder
->position()
->velocity()
->acceleration()
->mass()
->build();
const std::vector<Particle> particlesX = { GenerateStandardParticle(0.5, 0),
GenerateStandardParticle(-0.5, 0)};

std::vector<Particle> particles = { particleA, particleB };
const std::vector<Particle> particlesY = { GenerateStandardParticle(0, 0.5),
GenerateStandardParticle(0, -0.5)};

const double EPSILON = 1e-3;

SECTION( "Two still standing mass points are attracting each other" ) {
std::vector<Particle> result = solver.solve(particles, EPSILON);
//Solution
const double acceleration = -0.6674079993; //m/s^2
const double velocity = -0.06674079993; //m/s
const double position = 0.48665184; //m

SECTION( "Two still standing point mass are attracting each other in x-direction" ) {
std::vector<Particle> result = solver.solve(particlesX, EPSILON);

Particle &particle = result.front();
REQUIRE(particle.acceleration.x == Approx(acceleration));
REQUIRE(particle.velocity.x == Approx(velocity));
REQUIRE(particle.position.x == Approx(position));

particle = result.back();
REQUIRE(particle.acceleration.x == Approx(Inverse(acceleration)));
REQUIRE(particle.velocity.x == Approx(Inverse(velocity)));
REQUIRE(particle.position.x == Approx(Inverse(position)));
}

SECTION( "Two still standing point mass are attracting each other in y-direction" ) {
std::vector<Particle> result = solver.solve(particlesY, EPSILON);

Particle &particle = result.front();
REQUIRE(particle.acceleration.y == Approx(acceleration));
REQUIRE(particle.velocity.y == Approx(velocity));
REQUIRE(particle.position.y == Approx(position));

REQUIRE(false);
particle = result.back();
REQUIRE(particle.acceleration.y == Approx(Inverse(acceleration)));
REQUIRE(particle.velocity.y == Approx(Inverse(velocity)));
REQUIRE(particle.position.y == Approx(Inverse(position)));
}
}

0 comments on commit a0c2e20

Please sign in to comment.