<commit_before>﻿
#pragma once

#include "integrators/integrators.hpp"

#include <limits>
#include <vector>
#include <string>

#include "base/macros.hpp"
#include "integrators/embedded_explicit_generalized_runge_kutta_nyström_integrator.hpp"
#include "integrators/embedded_explicit_runge_kutta_nyström_integrator.hpp"
#include "integrators/methods.hpp"
#include "integrators/symmetric_linear_multistep_integrator.hpp"
#include "integrators/symplectic_runge_kutta_nyström_integrator.hpp"

// A case branch in a switch on the serialized integrator |kind|.  It determines
// the |method| type from the |kind| and calls |action| on it.  |action| must be
// a 1-argument macro.
#define PRINCIPIA_INTEGRATOR_CASE(kind, method, action) \
  case serialization::FixedStepSizeIntegrator::kind: {  \
    action(method);                                     \
    break;                                              \
  }

// All the case branches in a switch on the serialized integrator kind.
// Depending on the nature of the integrator, each case branch calls one of the
// given actions, which must be 1-argument macros.
#define PRINCIPIA_INTEGRATOR_CASES(slms_action, sprk_action, srkn_action) \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_S6,                          \
                            BlanesMoan2002S6,                             \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_S10,                         \
                            BlanesMoan2002S10,                            \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_SRKN_6B,                     \
                            BlanesMoan2002SRKN6B,                         \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_SRKN_11B,                    \
                            BlanesMoan2002SRKN11B,                        \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_SRKN_14A,                    \
                            BlanesMoan2002SRKN14A,                        \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(CANDY_ROZMUS_1991_FOREST_RUTH_1990,           \
                            CandyRozmus1991ForestRuth1990,                \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_S2,                            \
                            McLachlan1995S2,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_S4,                            \
                            McLachlan1995S4,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_S5,                            \
                            McLachlan1995S5,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SB3A_4,                        \
                            McLachlan1995SB3A4,                           \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SB3A_5,                        \
                            McLachlan1995SB3A5,                           \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS5,                           \
                            McLachlan1995SS5,                             \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS9,                           \
                            McLachlan1995SS9,                             \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS15,                          \
                            McLachlan1995SS15,                            \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS17,                          \
                            McLachlan1995SS17,                            \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_2_OPTIMAL,         \
                            McLachlanAtela1992Order2Optimal,              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_3_OPTIMAL,         \
                            McLachlanAtela1992Order3Optimal,              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_4_OPTIMAL,         \
                            McLachlanAtela1992Order4Optimal,              \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_5_OPTIMAL,         \
                            McLachlanAtela1992Order5Optimal,              \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(NEWTON_DELAMBRE_STORMER_VERLET_LEAPFROG,      \
                            NewtonDelambreStørmerVerletLeapfrog,          \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(OKUNBOR_SKEEL_1994_ORDER_6_METHOD_13,         \
                            OkunborSkeel1994Order6Method13,               \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_1999_ORDER_8A,                        \
                            Quinlan1999Order8A,                           \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_1999_ORDER_8B,                        \
                            Quinlan1999Order8B,                           \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_8,                \
                            QuinlanTremaine1990Order8,                    \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_10,               \
                            QuinlanTremaine1990Order10,                   \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_12,               \
                            QuinlanTremaine1990Order12,                   \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_14,               \
                            QuinlanTremaine1990Order14,                   \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(RUTH_1983,                                    \
                            Ruth1983,                                     \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(SUZUKI_1990,                                  \
                            鈴木1990,                                     \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_6A,                        \
                            吉田1990Order6A,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_6B,                        \
                            吉田1990Order6B,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_6C,                        \
                            吉田1990Order6C,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8A,                        \
                            吉田1990Order8A,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8B,                        \
                            吉田1990Order8B,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8C,                        \
                            吉田1990Order8C,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8D,                        \
                            吉田1990Order8D,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8E,                        \
                            吉田1990Order8E,                              \
                            sprk_action)

namespace principia {
namespace integrators {
namespace internal_integrators {

template<typename Integrator>
not_null<std::unique_ptr<typename Integrator::Instance>>
ReadSlmsInstanceFromMessage(
    serialization::FixedStepSizeIntegratorInstance const& message,
    IntegrationProblem<typename Integrator::ODE> const& problem,
    typename Integrator::AppendState const& append_state,
    Time const& step,
    Integrator const& integrator) {
  CHECK(message.HasExtension(
      serialization::SymmetricLinearMultistepIntegratorInstance::extension))
      << message.DebugString();
  auto const& extension = message.GetExtension(
      serialization::SymmetricLinearMultistepIntegratorInstance::extension);
  return typename Integrator::Instance::ReadFromMessage(extension,
                                                        problem,
                                                        append_state,
                                                        step,
                                                        integrator);
}

template<typename Integrator>
not_null<std::unique_ptr<typename Integrator::Instance>>
ReadSrknInstanceFromMessage(
    serialization::FixedStepSizeIntegratorInstance const& message,
    IntegrationProblem<typename Integrator::ODE> const& problem,
    typename Integrator::AppendState const& append_state,
    Time const& step,
    Integrator const& integrator) {
  CHECK(message.HasExtension(
      serialization::SymplecticRungeKuttaNystromIntegratorInstance::extension))
      << message.DebugString();
  auto const& extension = message.GetExtension(
      serialization::SymplecticRungeKuttaNystromIntegratorInstance::extension);
  return typename Integrator::Instance::ReadFromMessage(extension,
                                                        problem,
                                                        append_state,
                                                        step,
                                                        integrator);
}

// We do not deserialize an SPRK per se, but only when it is converted to an
// SRKN.  The reason is that an SPRK is for a different kind of equation than
// an SRKN, so the two would return different types.  We avoid replicating code
// at the expense of some template complexity.
template<typename Integrator, typename Result>
struct SprkAsSrknConstructor;

template<typename Integrator, typename ODE>
struct SprkAsSrknConstructor<Integrator, FixedStepSizeIntegrator<ODE> const&> {
  static FixedStepSizeIntegrator<ODE> const& Make(
      serialization::FixedStepSizeIntegratorInstance const& message,
      Integrator const& integrator) {<commit_msg>Fix a Clang compilation error.<commit_after>﻿
#pragma once

#include "integrators/integrators.hpp"

#include <limits>
#include <vector>
#include <string>

#include "base/macros.hpp"
#include "integrators/embedded_explicit_generalized_runge_kutta_nyström_integrator.hpp"
#include "integrators/embedded_explicit_runge_kutta_nyström_integrator.hpp"
#include "integrators/methods.hpp"
#include "integrators/symmetric_linear_multistep_integrator.hpp"
#include "integrators/symplectic_runge_kutta_nyström_integrator.hpp"

// A case branch in a switch on the serialized integrator |kind|.  It determines
// the |method| type from the |kind| and calls |action| on it.  |action| must be
// a 1-argument macro.
#define PRINCIPIA_INTEGRATOR_CASE(kind, method, action) \
  case serialization::FixedStepSizeIntegrator::kind: {  \
    action(method);                                     \
    break;                                              \
  }

// All the case branches in a switch on the serialized integrator kind.
// Depending on the nature of the integrator, each case branch calls one of the
// given actions, which must be 1-argument macros.
#define PRINCIPIA_INTEGRATOR_CASES(slms_action, sprk_action, srkn_action) \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_S6,                          \
                            BlanesMoan2002S6,                             \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_S10,                         \
                            BlanesMoan2002S10,                            \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_SRKN_6B,                     \
                            BlanesMoan2002SRKN6B,                         \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_SRKN_11B,                    \
                            BlanesMoan2002SRKN11B,                        \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(BLANES_MOAN_2002_SRKN_14A,                    \
                            BlanesMoan2002SRKN14A,                        \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(CANDY_ROZMUS_1991_FOREST_RUTH_1990,           \
                            CandyRozmus1991ForestRuth1990,                \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_S2,                            \
                            McLachlan1995S2,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_S4,                            \
                            McLachlan1995S4,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_S5,                            \
                            McLachlan1995S5,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SB3A_4,                        \
                            McLachlan1995SB3A4,                           \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SB3A_5,                        \
                            McLachlan1995SB3A5,                           \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS5,                           \
                            McLachlan1995SS5,                             \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS9,                           \
                            McLachlan1995SS9,                             \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS15,                          \
                            McLachlan1995SS15,                            \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_1995_SS17,                          \
                            McLachlan1995SS17,                            \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_2_OPTIMAL,         \
                            McLachlanAtela1992Order2Optimal,              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_3_OPTIMAL,         \
                            McLachlanAtela1992Order3Optimal,              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_4_OPTIMAL,         \
                            McLachlanAtela1992Order4Optimal,              \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(MCLACHLAN_ATELA_1992_ORDER_5_OPTIMAL,         \
                            McLachlanAtela1992Order5Optimal,              \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(NEWTON_DELAMBRE_STORMER_VERLET_LEAPFROG,      \
                            NewtonDelambreStørmerVerletLeapfrog,          \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(OKUNBOR_SKEEL_1994_ORDER_6_METHOD_13,         \
                            OkunborSkeel1994Order6Method13,               \
                            srkn_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_1999_ORDER_8A,                        \
                            Quinlan1999Order8A,                           \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_1999_ORDER_8B,                        \
                            Quinlan1999Order8B,                           \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_8,                \
                            QuinlanTremaine1990Order8,                    \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_10,               \
                            QuinlanTremaine1990Order10,                   \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_12,               \
                            QuinlanTremaine1990Order12,                   \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(QUINLAN_TREMAINE_1990_ORDER_14,               \
                            QuinlanTremaine1990Order14,                   \
                            slms_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(RUTH_1983,                                    \
                            Ruth1983,                                     \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(SUZUKI_1990,                                  \
                            鈴木1990,                                     \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_6A,                        \
                            吉田1990Order6A,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_6B,                        \
                            吉田1990Order6B,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_6C,                        \
                            吉田1990Order6C,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8A,                        \
                            吉田1990Order8A,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8B,                        \
                            吉田1990Order8B,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8C,                        \
                            吉田1990Order8C,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8D,                        \
                            吉田1990Order8D,                              \
                            sprk_action)                                  \
  PRINCIPIA_INTEGRATOR_CASE(YOSHIDA_1990_ORDER_8E,                        \
                            吉田1990Order8E,                              \
                            sprk_action)

namespace principia {
namespace integrators {
namespace internal_integrators {

template<typename Integrator>
not_null<std::unique_ptr<typename Integrator::Instance>>
ReadSlmsInstanceFromMessage(
    serialization::FixedStepSizeIntegratorInstance const& message,
    IntegrationProblem<typename Integrator::ODE> const& problem,
    typename Integrator::AppendState const& append_state,
    Time const& step,
    Integrator const& integrator) {
  CHECK(message.HasExtension(
      serialization::SymmetricLinearMultistepIntegratorInstance::extension))
      << message.DebugString();
  auto const& extension = message.GetExtension(
      serialization::SymmetricLinearMultistepIntegratorInstance::extension);
  return Integrator::Instance::ReadFromMessage(extension,
                                               problem,
                                               append_state,
                                               step,
                                               integrator);
}

template<typename Integrator>
not_null<std::unique_ptr<typename Integrator::Instance>>
ReadSrknInstanceFromMessage(
    serialization::FixedStepSizeIntegratorInstance const& message,
    IntegrationProblem<typename Integrator::ODE> const& problem,
    typename Integrator::AppendState const& append_state,
    Time const& step,
    Integrator const& integrator) {
  CHECK(message.HasExtension(
      serialization::SymplecticRungeKuttaNystromIntegratorInstance::extension))
      << message.DebugString();
  auto const& extension = message.GetExtension(
      serialization::SymplecticRungeKuttaNystromIntegratorInstance::extension);
  return Integrator::Instance::ReadFromMessage(extension,
                                               problem,
                                               append_state,
                                               step,
                                               integrator);
}

// We do not deserialize an SPRK per se, but only when it is converted to an
// SRKN.  The reason is that an SPRK is for a different kind of equation than
// an SRKN, so the two would return different types.  We avoid replicating code
// at the expense of some template complexity.
template<typename Integrator, typename Result>
struct SprkAsSrknConstructor;

template<typename Integrator, typename ODE>
struct SprkAsSrknConstructor<Integrator, FixedStepSizeIntegrator<ODE> const&> {
  static FixedStepSizeIntegrator<ODE> const& Make(
      serialization::FixedStepSizeIntegratorInstance const& message,
      Integrator const& integrator) {
{'fraction_non_alphanumeric': 0.04421225158778939, 'fraction_numerical': 0.03179676295841016, 'mean_word_length': 0.94780526735834, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '499980', 'n_tokens_mistral': 6678, 'n_tokens_neox': 6009, 'n_words': 730}