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

Convert FlightPlan and its test to DiscreteTraject0ry #3185

Merged
merged 7 commits into from
Oct 31, 2021
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
61 changes: 29 additions & 32 deletions ksp_plugin/flight_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ FlightPlan::FlightPlan(
initial_time_(initial_time),
initial_degrees_of_freedom_(std::move(initial_degrees_of_freedom)),
desired_final_time_(desired_final_time),
root_(make_not_null_unique<DiscreteTrajectory<Barycentric>>()),
ephemeris_(ephemeris),
adaptive_step_parameters_(std::move(adaptive_step_parameters)),
generalized_adaptive_step_parameters_(
std::move(generalized_adaptive_step_parameters)) {
CHECK(desired_final_time_ >= initial_time_);

// Set the (single) point of the root.
root_->Append(initial_time_, initial_degrees_of_freedom_);
// Set the first point of the first coasting trajectory.
trajectory_.Append(initial_time_, initial_degrees_of_freedom_);
segments_.emplace_back(trajectory_.segments().begin());

// Create a fork for the first coasting trajectory.
segments_.emplace_back(root_->NewForkWithoutCopy(initial_time_));
coast_analysers_.push_back(make_not_null_unique<OrbitAnalyser>(
ephemeris_, DefaultHistoryParameters()));
CHECK(manœuvres_.empty());
Expand Down Expand Up @@ -202,18 +200,18 @@ int FlightPlan::number_of_segments() const {

void FlightPlan::GetSegment(
int const index,
DiscreteTrajectory<Barycentric>::Iterator& begin,
DiscreteTrajectory<Barycentric>::Iterator& end) const {
DiscreteTraject0ry<Barycentric>::iterator& begin,
DiscreteTraject0ry<Barycentric>::iterator& end) const {
CHECK_LE(0, index);
CHECK_LT(index, number_of_segments());
begin = segments_[index]->Fork();
begin = segments_[index]->begin();
end = segments_[index]->end();
}

void FlightPlan::GetAllSegments(
DiscreteTrajectory<Barycentric>::Iterator& begin,
DiscreteTrajectory<Barycentric>::Iterator& end) const {
begin = segments_.back()->Find(segments_.front()->Fork()->time);
DiscreteTraject0ry<Barycentric>::iterator& begin,
DiscreteTraject0ry<Barycentric>::iterator& end) const {
begin = segments_.front()->begin();
end = segments_.back()->end();
CHECK(begin != end);
}
Expand Down Expand Up @@ -314,7 +312,6 @@ std::unique_ptr<FlightPlan> FlightPlan::ReadFromMessage(

FlightPlan::FlightPlan()
: initial_degrees_of_freedom_(Barycentric::origin, Barycentric::unmoving),
root_(make_not_null_unique<DiscreteTrajectory<Barycentric>>()),
ephemeris_(testing_utilities::make_not_null<Ephemeris<Barycentric>*>()),
adaptive_step_parameters_(
EmbeddedExplicitRungeKuttaNyströmIntegrator<
Expand Down Expand Up @@ -343,7 +340,7 @@ absl::Status FlightPlan::RecomputeAllSegments() {

absl::Status FlightPlan::BurnSegment(
NavigationManœuvre const& manœuvre,
not_null<DiscreteTrajectory<Barycentric>*> const segment) {
DiscreteTrajectorySegmentIterator<Barycentric> const segment) {
Instant const final_time = manœuvre.final_time();
if (manœuvre.initial_time() < final_time) {
// Make sure that the ephemeris covers the entire segment, reanimating and
Expand All @@ -356,14 +353,14 @@ absl::Status FlightPlan::BurnSegment(

if (manœuvre.is_inertially_fixed()) {
return ephemeris_->FlowWithAdaptiveStep(
segment,
&trajectory_,
manœuvre.InertialIntrinsicAcceleration(),
final_time,
adaptive_step_parameters_,
max_ephemeris_steps_per_frame);
} else {
return ephemeris_->FlowWithAdaptiveStep(
segment,
&trajectory_,
manœuvre.FrenetIntrinsicAcceleration(),
final_time,
generalized_adaptive_step_parameters_,
Expand All @@ -376,7 +373,7 @@ absl::Status FlightPlan::BurnSegment(

absl::Status FlightPlan::CoastSegment(
Instant const& desired_final_time,
not_null<DiscreteTrajectory<Barycentric>*> const segment) {
DiscreteTrajectorySegmentIterator<Barycentric> const segment) {
// Make sure that the ephemeris covers the entire segment, reanimating and
// waiting if necessary.
Instant const starting_time = segment->back().time;
Expand All @@ -386,7 +383,7 @@ absl::Status FlightPlan::CoastSegment(
}

return ephemeris_->FlowWithAdaptiveStep(
segment,
&trajectory_,
Ephemeris<Barycentric>::NoIntrinsicAcceleration,
desired_final_time,
adaptive_step_parameters_,
Expand All @@ -413,12 +410,12 @@ absl::Status FlightPlan::ComputeSegments(
anomalous_segments_ = 1;
anomalous_status_ = status;
}
auto const& [first_time, first_degrees_of_freedom] = coast->front();
coast_analysers_[it - manœuvres_.begin()]->RequestAnalysis(
{.first_time = coast->Fork()->time,
.first_degrees_of_freedom = coast->Fork()->degrees_of_freedom,
.mission_duration = coast->back().time - coast->Fork()->time,
.extended_mission_duration =
desired_final_time_ - coast->Fork()->time});
{.first_time = first_time,
.first_degrees_of_freedom = first_degrees_of_freedom,
.mission_duration = coast->back().time - first_time,
.extended_mission_duration = desired_final_time_ - first_time});
}

AddLastSegment();
Expand All @@ -442,12 +439,12 @@ absl::Status FlightPlan::ComputeSegments(
// having to extend the flight plan by hand.
desired_final_time_ =
std::max(desired_final_time_, segments_.back()->t_max());
auto const& [first_time, first_degrees_of_freedom] =
segments_.back()->front();
coast_analysers_.back()->RequestAnalysis(
{.first_time = segments_.back()->Fork()->time,
.first_degrees_of_freedom =
segments_.back()->Fork()->degrees_of_freedom,
.mission_duration =
desired_final_time_ - segments_.back()->Fork()->time});
{.first_time = first_time,
.first_degrees_of_freedom = first_degrees_of_freedom,
.mission_duration = desired_final_time_ - first_time});
absl::Status const status =
CoastSegment(desired_final_time_, segments_.back());
if (!status.ok()) {
Expand All @@ -460,23 +457,23 @@ absl::Status FlightPlan::ComputeSegments(
}

void FlightPlan::AddLastSegment() {
segments_.emplace_back(segments_.back()->NewForkAtLast());
segments_.emplace_back(trajectory_.NewSegment());
if (anomalous_segments_ > 0) {
++anomalous_segments_;
}
}

void FlightPlan::ResetLastSegment() {
segments_.back()->ForgetAfter(segments_.back()->Fork()->time);
auto const last_segment = segments_.back();
trajectory_.ForgetAfter(std::next(last_segment->begin()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it has different semantics with respect to forgetting the given point, I think ForgetAfter should be renamed to something like ForgetFrom: the name ForgetAfter is misleading, I would have expected no next.
We can do that after migrating to DiscreteTraject0ry if that’s easier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some of the identifiers will need adjusting, but we'll do that at the end.

if (anomalous_segments_ == 1) {
anomalous_segments_ = 0;
}
}

void FlightPlan::PopLastSegment() {
DiscreteTrajectory<Barycentric>* trajectory = segments_.back();
CHECK(!trajectory->is_root());
trajectory->parent()->DeleteFork(trajectory);
auto last_segment = segments_.back();
trajectory_.DeleteSegments(last_segment);
segments_.pop_back();
if (anomalous_segments_ > 0) {
--anomalous_segments_;
Expand Down
36 changes: 20 additions & 16 deletions ksp_plugin/flight_plan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include "ksp_plugin/manœuvre.hpp"
#include "ksp_plugin/orbit_analyser.hpp"
#include "physics/degrees_of_freedom.hpp"
#include "physics/discrete_trajectory.hpp"
#include "physics/discrete_traject0ry.hpp"
#include "physics/discrete_trajectory_segment_iterator.hpp"
#include "physics/ephemeris.hpp"
#include "quantities/named_quantities.hpp"
#include "quantities/quantities.hpp"
Expand All @@ -25,7 +26,8 @@ using base::not_null;
using geometry::Instant;
using integrators::AdaptiveStepSizeIntegrator;
using physics::DegreesOfFreedom;
using physics::DiscreteTrajectory;
using physics::DiscreteTraject0ry;
using physics::DiscreteTrajectorySegmentIterator;
using physics::Ephemeris;
using quantities::Length;
using quantities::Mass;
Expand Down Expand Up @@ -113,11 +115,11 @@ class FlightPlan {
// |index| must be in [0, number_of_segments()[. Sets the iterators to denote
// the given trajectory.
virtual void GetSegment(int index,
DiscreteTrajectory<Barycentric>::Iterator& begin,
DiscreteTrajectory<Barycentric>::Iterator& end) const;
DiscreteTraject0ry<Barycentric>::iterator& begin,
DiscreteTraject0ry<Barycentric>::iterator& end) const;
virtual void GetAllSegments(
DiscreteTrajectory<Barycentric>::Iterator& begin,
DiscreteTrajectory<Barycentric>::Iterator& end) const;
DiscreteTraject0ry<Barycentric>::iterator& begin,
DiscreteTraject0ry<Barycentric>::iterator& end) const;

// |coast_index| must be in [0, number_of_manœuvres()].
virtual OrbitAnalyser::Analysis* analysis(int coast_index);
Expand Down Expand Up @@ -150,13 +152,15 @@ class FlightPlan {

// Flows the given |segment| for the duration of |manœuvre| using its
// intrinsic acceleration.
absl::Status BurnSegment(NavigationManœuvre const& manœuvre,
not_null<DiscreteTrajectory<Barycentric>*> segment);
absl::Status BurnSegment(
NavigationManœuvre const& manœuvre,
DiscreteTrajectorySegmentIterator<Barycentric> segment);

// Flows the given |segment| until |desired_final_time| with no intrinsic
// acceleration.
absl::Status CoastSegment(Instant const& desired_final_time,
not_null<DiscreteTrajectory<Barycentric>*> segment);
absl::Status CoastSegment(
Instant const& desired_final_time,
DiscreteTrajectorySegmentIterator<Barycentric> segment);

// Computes new trajectories and appends them to |segments_|. This updates
// the last coast of |segments_| and then appends one coast and one burn for
Expand Down Expand Up @@ -202,14 +206,14 @@ class FlightPlan {
Instant initial_time_;
DegreesOfFreedom<Barycentric> initial_degrees_of_freedom_;
Instant desired_final_time_;
// The root of the flight plan. Contains a single point, not part of
// |segments_|. Owns all the |segments_|.
not_null<std::unique_ptr<DiscreteTrajectory<Barycentric>>> root_;

// The trajectory of the part, composed of any number of segments,
// alternatively coasts and burns.
DiscreteTraject0ry<Barycentric> trajectory_;

// Never empty; Starts and ends with a coast; coasts and burns alternate.
// Each trajectory is a fork of the previous one.
std::vector<not_null<DiscreteTrajectory<Barycentric>*>> segments_;
// The last |anomalous_segments_| of |segments_| are anomalous, i.e. they
std::vector<DiscreteTrajectorySegmentIterator<Barycentric>> segments_;
// The last |anomalous_segments_| of |segments_| are anomalous, i.e., they
// either end prematurely or follow an anomalous trajectory; in the latter
// case they are empty.
int anomalous_segments_ = 0;
Expand Down
39 changes: 24 additions & 15 deletions ksp_plugin_test/flight_plan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <vector>

#include "astronomy/epoch.hpp"
#include "base/not_null.hpp"
#include "geometry/named_quantities.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "integrators/embedded_explicit_generalized_runge_kutta_nyström_integrator.hpp"
Expand All @@ -13,9 +15,12 @@
#include "integrators/symmetric_linear_multistep_integrator.hpp"
#include "ksp_plugin/integrators.hpp"
#include "physics/degrees_of_freedom.hpp"
#include "physics/discrete_trajectory.hpp"
#include "physics/discrete_traject0ry.hpp"
#include "physics/ephemeris.hpp"
#include "physics/massive_body.hpp"
#include "physics/rotating_body.hpp"
#include "quantities/named_quantities.hpp"
#include "quantities/quantities.hpp"
#include "serialization/ksp_plugin.pb.h"
#include "testing_utilities/almost_equals.hpp"
#include "testing_utilities/approximate_quantity.hpp"
Expand All @@ -25,13 +30,14 @@

namespace principia {
namespace ksp_plugin {
namespace internal_flight_plan {

using astronomy::J2000;
using base::not_null;
using base::make_not_null_shared;
using base::make_not_null_unique;
using geometry::Barycentre;
using geometry::Displacement;
using geometry::Instant;
using geometry::Position;
using geometry::Velocity;
using integrators::EmbeddedExplicitGeneralizedRungeKuttaNyströmIntegrator;
Expand All @@ -42,13 +48,17 @@ using integrators::methods::Fine1987RKNG34;
using integrators::methods::QuinlanTremaine1990Order12;
using physics::BodyCentredNonRotatingDynamicFrame;
using physics::DegreesOfFreedom;
using physics::DiscreteTrajectory;
using physics::DiscreteTraject0ry;
using physics::Ephemeris;
using physics::Frenet;
using physics::MassiveBody;
using physics::RotatingBody;
using quantities::Force;
using quantities::Length;
using quantities::Mass;
using quantities::Pow;
using quantities::SpecificImpulse;
using quantities::Speed;
using quantities::Sqrt;
using quantities::si::Kilogram;
using quantities::si::Metre;
Expand Down Expand Up @@ -185,7 +195,7 @@ class FlightPlanTest : public testing::Test {
Instant const t0_;
std::unique_ptr<TestNavigationFrame> navigation_frame_;
std::unique_ptr<Ephemeris<Barycentric>> ephemeris_;
DiscreteTrajectory<Barycentric> root_;
DiscreteTraject0ry<Barycentric> root_;
std::unique_ptr<FlightPlan> flight_plan_;
};

Expand Down Expand Up @@ -232,10 +242,10 @@ TEST_F(FlightPlanTest, Singular) {
/*max_steps=*/1,
/*length_integration_tolerance=*/1 * Metre,
/*speed_integration_tolerance=*/1 * Metre / Second));
DiscreteTrajectory<Barycentric>::Iterator begin;
DiscreteTrajectory<Barycentric>::Iterator end;
DiscreteTraject0ry<Barycentric>::iterator begin;
DiscreteTraject0ry<Barycentric>::iterator end;
flight_plan_->GetSegment(0, begin, end);
DiscreteTrajectory<Barycentric>::Iterator back = end;
DiscreteTraject0ry<Barycentric>::iterator back = end;
--back;
EXPECT_THAT(AbsoluteError(singularity, back->time), Lt(1e-4 * Second));
// Attempting to put a burn past the singularity fails.
Expand Down Expand Up @@ -370,8 +380,8 @@ TEST_F(FlightPlanTest, Segments) {
EXPECT_EQ(5, flight_plan_->number_of_segments());

std::vector<Instant> times;
DiscreteTrajectory<Barycentric>::Iterator begin;
DiscreteTrajectory<Barycentric>::Iterator end;
DiscreteTraject0ry<Barycentric>::iterator begin;
DiscreteTraject0ry<Barycentric>::iterator end;

int last_times_size = times.size();
Instant last_t = t0_ - 2 * π * Second;
Expand All @@ -389,8 +399,8 @@ TEST_F(FlightPlanTest, Segments) {
}

TEST_F(FlightPlanTest, SetAdaptiveStepParameter) {
DiscreteTrajectory<Barycentric>::Iterator begin;
DiscreteTrajectory<Barycentric>::Iterator end;
DiscreteTraject0ry<Barycentric>::iterator begin;
DiscreteTraject0ry<Barycentric>::iterator end;
flight_plan_->SetDesiredFinalTime(t0_ + 42 * Second);
EXPECT_OK(flight_plan_->Insert(MakeFirstBurn(), 0));
EXPECT_OK(flight_plan_->Insert(MakeSecondBurn(), 1));
Expand Down Expand Up @@ -461,9 +471,9 @@ TEST_F(FlightPlanTest, GuidedBurn) {
auto unguided_burn = MakeFirstBurn();
unguided_burn.thrust /= 10;
EXPECT_OK(flight_plan_->Insert(std::move(unguided_burn), 0));
DiscreteTrajectory<Barycentric>::Iterator begin;
DiscreteTrajectory<Barycentric>::Iterator end;
DiscreteTrajectory<Barycentric>::Iterator last;
DiscreteTraject0ry<Barycentric>::iterator begin;
DiscreteTraject0ry<Barycentric>::iterator end;
DiscreteTraject0ry<Barycentric>::iterator last;
flight_plan_->GetAllSegments(begin, end);
last = --end;
Speed const unguided_final_speed =
Expand Down Expand Up @@ -600,6 +610,5 @@ TEST_F(FlightPlanTest, Insertion) {
EXPECT_THAT(inserted_out_of_order, EqualsProto(inserted_in_order));
}

} // namespace internal_flight_plan
} // namespace ksp_plugin
} // namespace principia