Skip to content

Commit

Permalink
Tidy Star class
Browse files Browse the repository at this point in the history
  • Loading branch information
ConniBug committed Mar 5, 2024
1 parent 73749a5 commit 20a343c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
30 changes: 29 additions & 1 deletion Star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ double Star::acceleration_update_stars_in_region(bool clear_accel) {
long double r_in_km = r * parsec_to_km;
long double accel_from_star = (gravitationalConstantFinal * star->mass)/(pow(r_in_km, 2)); // Now gives acceleration in pc/year^2

this->acceleration += ((star->position - this->position) / r) * accel_from_star;
auto position_delta = star->position - this->position;
position_delta * r;
this->acceleration += (position_delta / r) * accel_from_star;
}
}
return std::chrono::duration_cast<std::chrono::milliseconds>((std::chrono::high_resolution_clock::now())-accelerationStartTime).count();
Expand Down Expand Up @@ -236,4 +238,30 @@ void Star::position_update(long double override_timestep) {
this->history_tmp = history_record_t();

this->regions_we_are_in.clear();
}

bool Star::is_static() const {
return this->flags & (int)STAR_FLAGS::STATIC;
}

long double Star::kinetic_energy(int history_index, bool reverse) {
history_index -= 1;

// Return the live value of the star
if(history_index == -1)
return (0.5 * this->mass) * this->velocity.magnitude_squared();

history_index = reverse ? this->history.size() - history_index : history_index;

auto tmp_mass = 0.5 * this->mass;
auto tmp_velocity = this->history[history_index].velocity; // in ps/year

// logging::info("Velocity ps/year - ", tmp_velocity);
tmp_velocity = tmp_velocity * parsecsPerYear_to_metersPerSecond; // convert to m/s
// logging::info("Velocity m/s - ", tmp_velocity);

auto energy = tmp_mass * tmp_velocity.magnitude_squared(); // in J
// logging::info("Energy J - ", energy);

return energy;
}
27 changes: 2 additions & 25 deletions Star.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,9 @@ class Star {

void position_update(long double override_timestep = 0);

bool is_static() {
return this->flags & (int)STAR_FLAGS::STATIC;
}

long double kinetic_energy(int history_index = -1, bool reverse = false) {
history_index = history_index - 1;

// Return the live value of the star
if(history_index == -1)
return (0.5 * this->mass) * this->velocity.magnitude_squared();
else {
history_index = reverse ? this->history.size() - history_index : history_index;

auto tmp_mass = 0.5 * this->mass;
auto tmp_velocity = this->history[history_index].velocity; // in ps/year
bool is_static() const;

// logging::info("Velocity ps/year - ", tmp_velocity);
tmp_velocity = tmp_velocity * parsecsPerYear_to_metersPerSecond; // convert to m/s
// logging::info("Velocity m/s - ", tmp_velocity);

auto energy = tmp_mass * tmp_velocity.magnitude_squared(); // in J
// logging::info("Energy J - ", energy);

return energy;
}
}
long double kinetic_energy(int history_index = -1, bool reverse = false);
};

#endif //C_VERSION_STAR_H

0 comments on commit 20a343c

Please sign in to comment.