Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the "apocalypse" bug that was causing celestials to be suddenly moved to random locations #612

Merged
merged 7 commits into from
Aug 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions physics/continuous_trajectory_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <limits>
#include <vector>

#include "glog/stl_logging.h"
#include "physics/continuous_trajectory.hpp"
#include "quantities/si.hpp"
#include "testing_utilities/numerics.hpp"
Expand Down Expand Up @@ -269,11 +270,13 @@ void ContinuousTrajectory<Frame>::ComputeBestNewhallApproximation(
std::vector<Velocity<Frame>> const& v,
Instant const& t_min,
Instant const& t_max)) {
Length const previous_adjusted_tolerance = adjusted_tolerance_;

// If the degree is too old, restart from the lowest degree. This ensures
// that we use the lowest possible degree at a small computational cost.
if (degree_age_ >= kMaxDegreeAge) {
VLOG(1) << "Lowering degree from " << degree_ << " to " << kMinDegree
<< " because the approximation is too old";
VLOG(1) << "Lowering degree for " << this << " from " << degree_
<< " to " << kMinDegree << " because the approximation is too old";
is_unstable_ = false;
adjusted_tolerance_ = tolerance_;
degree_ = kMinDegree;
Expand All @@ -292,7 +295,8 @@ void ContinuousTrajectory<Frame>::ComputeBestNewhallApproximation(
// If we are in the zone of numerical instabilities and we exceeded the
// tolerance, restart from the lowest degree.
if (is_unstable_ && error_estimate > adjusted_tolerance_) {
VLOG(1) << "Lowering degree from " << degree_ << " to " << kMinDegree
VLOG(1) << "Lowering degree for " << this << " from " << degree_
<< " to " << kMinDegree
<< " because error estimate " << error_estimate
<< " exceeds adjusted tolerance " << adjusted_tolerance_
<< " and computations are unstable";
Expand Down Expand Up @@ -338,6 +342,12 @@ void ContinuousTrajectory<Frame>::ComputeBestNewhallApproximation(
<< " with error estimate " << error_estimate;
}

// A check that the tolerance did not explode.
CHECK_LT(adjusted_tolerance_, 1E6 * previous_adjusted_tolerance)
<< "Apocalypse occurred at " << time
<< ", displacements are: " << q
<< ", velocities are: " << v;

++degree_age_;
}

Expand Down
10 changes: 6 additions & 4 deletions physics/ephemeris_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ Ephemeris<Frame>::Ephemeris(
inserted.first->second.get();
trajectory->Append(initial_time, degrees_of_freedom);

VLOG(1) << "Constructed trajectory " << trajectory
<< " for body with mass " << body->mass();

if (body->is_oblate()) {
// Inserting at the beginning of the vectors is O(N).
oblate_bodies_.insert(oblate_bodies_.begin(), body.get());
Expand Down Expand Up @@ -210,9 +213,8 @@ void Ephemeris<Frame>::ForgetAfter(Instant const & t) {
CHECK_LE(t, it->time.value);

int index = 0;
for (auto& pair : bodies_to_trajectories_) {
ContinuousTrajectory<Frame>& trajectory = *pair.second;
trajectory.ForgetAfter(
for (auto const& trajectory : trajectories_) {
trajectory->ForgetAfter(
it->time.value,
DegreesOfFreedom<Frame>(it->positions[index].value,
it->velocities[index].value));
Expand Down Expand Up @@ -309,7 +311,7 @@ void Ephemeris<Frame>::FlowWithFixedStep(
std::vector<not_null<Trajectory<Frame>*>> const& trajectories,
Time const& step,
Instant const& t) {
LOG(INFO) << __FUNCTION__;
VLOG(1) << __FUNCTION__ << " " << NAMED(step) << " " << NAMED(t);
if (empty() || t > t_max()) {
Prolong(t);
}
Expand Down
4 changes: 3 additions & 1 deletion physics/ephemeris_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ class EphemerisTest : public testing::Test {
initial_state,
not_null<Position<EarthMoonOrbitPlane>*> const centre_of_mass,
not_null<Time*> const period) {
auto earth = std::make_unique<MassiveBody const>(6E24 * Kilogram);
// Create the Moon before the Earth to exercise a bug caused by the order of
// pointers differing from the order of bodies (don't ask).
auto moon = std::make_unique<MassiveBody const>(7E22 * Kilogram);
auto earth = std::make_unique<MassiveBody const>(6E24 * Kilogram);

// The Earth-Moon system, roughly, with a circular orbit with velocities
// in the centre-of-mass frame.
Expand Down