Skip to content

Commit

Permalink
add tests for body implementation; slightly edit body.cpp implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jdinovi committed Dec 29, 2023
1 parent 33ce06b commit aad4eaf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ float DENSITY = 4E3;

// Constructor definition
Body::Body(const std::array<double, 3>* initial_position, const std::array<double, 3>* initial_velocity, double mass, float radius)
: Particle(initial_position, initial_velocity, mass), radius(radius == 0 ? pow(3 * mass / (4 * M_PI * DENSITY), 1/3) : radius) {};
: Particle(initial_position, initial_velocity, mass), radius((radius == 0) ? pow(3 * mass / (4 * M_PI * DENSITY), 1./3.) : radius) {};
67 changes: 67 additions & 0 deletions test/test_body.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#define _USE_MATH_DEFINES

#include <array>
#include <cmath>

#include "../include/doctest.h"
#include "../include/body.h"

TEST_CASE("Body Initialization - WITH Radius") {

// Initialize particle specs
double mass = 1;
std::array<double, 3> initial_position1 = {0, 0, 0};
std::array<double, 3> initial_velocity1 = {0, 0, 0};
float radius = 1E5;

// Define the particle
Body body1(&initial_position1, &initial_velocity1, mass, radius);

CHECK(body1.position[0] == 0);
CHECK(body1.position[1] == 0);
CHECK(body1.position[2] == 0);
CHECK(body1.mass == 1);
CHECK(body1.radius == 1E5);
}

TEST_CASE("Body Initialization - WITHOUT Radius") {

// Initialize particle specs
double mass = 1;
std::array<double, 3> initial_position1 = {0, 0, 0};
std::array<double, 3> initial_velocity1 = {0, 0, 0};

// Define the particle
Body body1(&initial_position1, &initial_velocity1, mass);

CHECK(body1.position[0] == 0);
CHECK(body1.position[1] == 0);
CHECK(body1.position[2] == 0);
CHECK(body1.mass == 1);
CHECK((body1.radius - pow(3 * mass / (4 * M_PI * 4E3), 1./3.)) < 0.000001);
}


TEST_CASE("Body Update") {

// Parameters for a timestep
double timestep = 1;

// Initialize particle specs
double mass = 1;
float radius = 1E5;
std::array<double, 3> initial_position1 = {0, 0, 0};
std::array<double, 3> initial_velocity1 = {0, 0, 0};

// Define the force
std::array<double, 3> force = {3, 0, 0};

// Define the particle
Body body1(&initial_position1, &initial_velocity1, mass, radius);

// Update particle
body1.update(&force, timestep);

// Check the update works
CHECK(body1.position[0] == 1.5);
}

0 comments on commit aad4eaf

Please sign in to comment.