// Tests that serialization and deserialization work.
TEST_P(SymmetricLinearMultistepIntegratorTest, Serialization) {
  LOG(INFO) << GetParam();
  Length const q_initial = 1 * Metre;
  Speed const v_initial = 0 * Metre / Second;
  Instant const t_initial;
  Time const step = 0.2 * Second;

  Mass const m = 1 * Kilogram;
  Stiffness const k = SIUnit<Stiffness>();
  Energy const initial_energy =
      0.5 * m * Pow<2>(v_initial) + 0.5 * k * Pow<2>(q_initial);

  std::vector<ODE::SystemState> solution;
  ODE harmonic_oscillator;
  harmonic_oscillator.compute_acceleration =
      std::bind(ComputeHarmonicOscillatorAcceleration1D,
                _1, _2, _3, /*evaluations=*/nullptr);
  IntegrationProblem<ODE> problem;
  problem.equation = harmonic_oscillator;
  problem.initial_state = {{q_initial}, {v_initial}, t_initial};
  auto const append_state = [&solution](ODE::SystemState const& state) {
    solution.push_back(state);
  };

  auto const instance1 =
      GetParam().integrator.NewInstance(problem, append_state, step);
  serialization::IntegratorInstance message1;
  instance1->WriteToMessage(&message1);
  auto const instance2 =
      FixedStepSizeIntegrator<ODE>::Instance::ReadFromMessage(
          message1, harmonic_oscillator, append_state);
  serialization::IntegratorInstance message2;
  instance2->WriteToMessage(&message2);
  EXPECT_THAT(message1, EqualsProto(message2));
}
TEST_F(EmbeddedExplicitRungeKuttaNyströmIntegratorTest, Serialization) {
  AdaptiveStepSizeIntegrator<ODE> const& integrator =
      EmbeddedExplicitRungeKuttaNyströmIntegrator<
          methods::DormandالمكاوىPrince1986RKN434FM,
          Length>();
  Length const x_initial = 1 * Metre;
  Speed const v_initial = 0 * Metre / Second;
  Time const period = 2 * π * Second;
  Instant const t_initial;
  Instant const t_final = t_initial + 10 * period;
  Length const length_tolerance = 1 * Milli(Metre);
  Speed const speed_tolerance = 1 * Milli(Metre) / Second;

  auto const step_size_callback = [](bool tolerable) {};

  std::vector<ODE::SystemState> solution;
  ODE harmonic_oscillator;
  harmonic_oscillator.compute_acceleration =
      std::bind(ComputeHarmonicOscillatorAcceleration1D,
                _1, _2, _3, /*evaluations=*/nullptr);
  IntegrationProblem<ODE> problem;
  problem.equation = harmonic_oscillator;
  problem.initial_state = {{x_initial}, {v_initial}, t_initial};
  auto const append_state = [&solution](ODE::SystemState const& state) {
    solution.push_back(state);
  };
  AdaptiveStepSizeIntegrator<ODE>::Parameters const parameters(
      /*first_time_step=*/t_final - t_initial,
      /*safety_factor=*/0.9);
  auto const tolerance_to_error_ratio =
      std::bind(HarmonicOscillatorToleranceRatio,
                _1, _2,
                length_tolerance,
                speed_tolerance,
                step_size_callback);

  auto const instance1 = integrator.NewInstance(problem,
                                                append_state,
                                                tolerance_to_error_ratio,
                                                parameters);
  serialization::IntegratorInstance message1;
  instance1->WriteToMessage(&message1);
  auto const instance2 =
      AdaptiveStepSizeIntegrator<ODE>::Instance::ReadFromMessage(
          message1,
          harmonic_oscillator,
          append_state,
          tolerance_to_error_ratio);
  serialization::IntegratorInstance message2;
  instance2->WriteToMessage(&message2);
  EXPECT_THAT(message1, EqualsProto(message2));
}
Beispiel #3
0
  void TestRotatingBody() {
    using F = Frame<Tag, tag, true>;

    AngularVelocity<F> const angular_velocity =
        AngularVelocity<F>({-1 * Radian / Second,
                            2 * Radian / Second,
                            5 * Radian / Second});
    auto const rotating_body =
        RotatingBody<F>(17 * SIUnit<GravitationalParameter>(),
                        typename RotatingBody<F>::Parameters(
                            3 * Radian,
                            Instant() + 4 * Second,
                            angular_velocity));

    serialization::Body message;
    RotatingBody<F> const* cast_rotating_body;
    rotating_body.WriteToMessage(&message);
    EXPECT_TRUE(message.has_massive_body());
    EXPECT_FALSE(message.has_massless_body());
    EXPECT_TRUE(message.massive_body().HasExtension(
                    serialization::RotatingBody::rotating_body));

    not_null<std::unique_ptr<MassiveBody const>> const massive_body =
        MassiveBody::ReadFromMessage(message);
    EXPECT_EQ(rotating_body.gravitational_parameter(),
              massive_body->gravitational_parameter());
    cast_rotating_body = dynamic_cast<RotatingBody<F> const*>(&*massive_body);
    EXPECT_THAT(cast_rotating_body, NotNull());
  }
TEST_F(PluginCompatibilityTest, PreBourbaki) {
  // Read the entire hex data.
  std::fstream file = std::fstream(
      SOLUTION_DIR / "ksp_plugin_test" / "pre_bourbaki.proto.hex");
  CHECK(file.good());
  std::string hex;
  while (!file.eof()) {
    std::string line;
    std::getline(file, line);
    for (auto const c : line) {
      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
        hex.append(1, c);
      }
    }
  }
  file.close();

  // Parse it and convert to binary data.
  UniqueBytes bin(hex.size() / 2);
  HexadecimalDecode(Array<std::uint8_t const>(
                        reinterpret_cast<const std::uint8_t*>(hex.c_str()),
                        hex.size()),
                    bin.get());

  // Construct a protocol buffer from the binary data.
  google::protobuf::io::CodedInputStream coded_input_stream(
      bin.data.get(), static_cast<int>(bin.size));
  serialization::Plugin pre_bourbaki_serialized_plugin;
  CHECK(pre_bourbaki_serialized_plugin.MergeFromCodedStream(
            &coded_input_stream));

  // Construct a plugin from the protocol buffer.
  auto plugin = TestablePlugin::ReadFromMessage(pre_bourbaki_serialized_plugin);

  // Do some operations on the plugin.
  plugin->KeepAllVessels();
  plugin->AdvanceTime(plugin->current_time() + 1 * Second, 2 * Radian);
  plugin->AdvanceTime(plugin->current_time() + 1 * Hour, 3 * Radian);

  // Serialize and deserialize it in the new format.
  serialization::Plugin post_bourbaki_serialized_plugin;
  plugin->WriteToMessage(&post_bourbaki_serialized_plugin);
  plugin = TestablePlugin::ReadFromMessage(post_bourbaki_serialized_plugin);
}
Beispiel #5
0
void Identity<FromFrame, ToFrame>::WriteToMessage(
    not_null<serialization::LinearMap*> const message) const {
  LinearMap<FromFrame, ToFrame>::WriteToMessage(message);
  WriteToMessage(message->MutableExtension(serialization::Identity::identity));
}
inline void MassiveBody::WriteToMessage(
    not_null<serialization::Body*> const message) const {
  WriteToMessage(message->mutable_massive_body());
}
void Permutation<FromFrame, ToFrame>::WriteToMessage(
      not_null<serialization::LinearMap*> const message) const {
  LinearMap<FromFrame, ToFrame>::WriteToMessage(message);
  WriteToMessage(
      message->MutableExtension(serialization::Permutation::extension));
}