/**
  * Simple constructor.
  *
  * <p>The {@code applyBefore} parameter is mainly used when the differential effect is associated
  * with a maneuver. In this case, the parameter must be set to {@code false}.
  *
  * @param orbit0 original orbit at reference date
  * @param orbit1 shifted orbit at reference date
  * @param applyBefore if true, effect is applied both before and after reference date, if false it
  *     is only applied after reference date
  * @param gravityField gravity field to use
  * @exception OrekitException if gravity field does not contain J2 coefficient
  */
 public J2DifferentialEffect(
     final Orbit orbit0,
     final Orbit orbit1,
     final boolean applyBefore,
     final UnnormalizedSphericalHarmonicsProvider gravityField)
     throws OrekitException {
   this(
       orbit0,
       orbit1,
       applyBefore,
       gravityField.getAe(),
       gravityField.getMu(),
       -gravityField.onDate(orbit0.getDate()).getUnnormalizedCnm(2, 0));
 }
 /**
  * Simple constructor.
  *
  * <p>The {@code applyBefore} parameter is mainly used when the differential effect is associated
  * with a maneuver. In this case, the parameter must be set to {@code false}.
  *
  * @param original original state at reference date
  * @param directEffect direct effect changing the orbit
  * @param applyBefore if true, effect is applied both before and after reference date, if false it
  *     is only applied after reference date
  * @param gravityField gravity field to use
  * @exception OrekitException if gravity field does not contain J2 coefficient
  */
 public J2DifferentialEffect(
     final SpacecraftState original,
     final AdapterPropagator.DifferentialEffect directEffect,
     final boolean applyBefore,
     final UnnormalizedSphericalHarmonicsProvider gravityField)
     throws OrekitException {
   this(
       original,
       directEffect,
       applyBefore,
       gravityField.getAe(),
       gravityField.getMu(),
       -gravityField.onDate(original.getDate()).getUnnormalizedCnm(2, 0));
 }
  /** {@inheritDoc} */
  @Override
  public double[] getShortPeriodicVariations(final AbsoluteDate date, final double[] meanElements)
      throws OrekitException {

    // Initialise the short periodic variations
    final double[] shortPeriodicVariation = new double[] {0., 0., 0., 0., 0., 0.};

    // Compute only if there is at least one non-resonant tesseral or
    // only the m-daily tesseral should be taken into account
    if (!nonResOrders.isEmpty() || mDailiesOnly) {

      // Build an Orbit object from the mean elements
      final Orbit meanOrbit =
          OrbitType.EQUINOCTIAL.mapArrayToOrbit(
              meanElements, PositionAngle.MEAN, date, provider.getMu(), this.frame);

      // Build an auxiliary object
      final AuxiliaryElements aux = new AuxiliaryElements(meanOrbit, I);

      // Central body rotation angle from equation 2.7.1-(3)(4).
      final Transform t = bodyFrame.getTransformTo(aux.getFrame(), aux.getDate());
      final Vector3D xB = t.transformVector(Vector3D.PLUS_I);
      final Vector3D yB = t.transformVector(Vector3D.PLUS_J);
      final double currentTheta =
          FastMath.atan2(
              -f.dotProduct(yB) + I * g.dotProduct(xB), f.dotProduct(xB) + I * g.dotProduct(yB));

      // Add the m-daily contribution
      for (int m = 1; m <= maxOrderMdailyTesseralSP; m++) {
        // Phase angle
        final double jlMmt = -m * currentTheta;
        final double sinPhi = FastMath.sin(jlMmt);
        final double cosPhi = FastMath.cos(jlMmt);

        // compute contribution for each element
        for (int i = 0; i < 6; i++) {
          shortPeriodicVariation[i] +=
              tesseralSPCoefs.getCijm(i, 0, m, date) * cosPhi
                  + tesseralSPCoefs.getSijm(i, 0, m, date) * sinPhi;
        }
      }

      // loop through all non-resonant (j,m) pairs
      for (final Map.Entry<Integer, List<Integer>> entry : nonResOrders.entrySet()) {
        final int m = entry.getKey();
        final List<Integer> listJ = entry.getValue();

        for (int j : listJ) {
          // Phase angle
          final double jlMmt = j * meanElements[5] - m * currentTheta;
          final double sinPhi = FastMath.sin(jlMmt);
          final double cosPhi = FastMath.cos(jlMmt);

          // compute contribution for each element
          for (int i = 0; i < 6; i++) {
            shortPeriodicVariation[i] +=
                tesseralSPCoefs.getCijm(i, j, m, date) * cosPhi
                    + tesseralSPCoefs.getSijm(i, j, m, date) * sinPhi;
          }
        }
      }
    }

    return shortPeriodicVariation;
  }
  /** {@inheritDoc} */
  @Override
  public void initializeStep(final AuxiliaryElements aux) throws OrekitException {

    // Equinoctial elements
    a = aux.getSma();
    k = aux.getK();
    h = aux.getH();
    q = aux.getQ();
    p = aux.getP();
    lm = aux.getLM();

    // Eccentricity
    ecc = aux.getEcc();
    e2 = ecc * ecc;

    // Retrograde factor
    I = aux.getRetrogradeFactor();

    // Equinoctial frame vectors
    f = aux.getVectorF();
    g = aux.getVectorG();

    // Central body rotation angle from equation 2.7.1-(3)(4).
    final Transform t = bodyFrame.getTransformTo(aux.getFrame(), aux.getDate());
    final Vector3D xB = t.transformVector(Vector3D.PLUS_I);
    final Vector3D yB = t.transformVector(Vector3D.PLUS_J);
    theta =
        FastMath.atan2(
            -f.dotProduct(yB) + I * g.dotProduct(xB), f.dotProduct(xB) + I * g.dotProduct(yB));

    // Direction cosines
    alpha = aux.getAlpha();
    beta = aux.getBeta();
    gamma = aux.getGamma();

    // Equinoctial coefficients
    // A = sqrt(μ * a)
    final double A = aux.getA();
    // B = sqrt(1 - h² - k²)
    final double B = aux.getB();
    // C = 1 + p² + q²
    final double C = aux.getC();
    // Common factors from equinoctial coefficients
    // 2 * a / A
    ax2oA = 2. * a / A;
    // B / A
    BoA = B / A;
    // 1 / AB
    ooAB = 1. / (A * B);
    // C / 2AB
    Co2AB = C * ooAB / 2.;
    // B / (A * (1 + B))
    BoABpo = BoA / (1. + B);
    // &mu / a
    moa = provider.getMu() / a;
    // R / a
    roa = provider.getAe() / a;

    // &Chi; = 1 / B
    chi = 1. / B;
    chi2 = chi * chi;

    // mean motion n
    meanMotion = aux.getMeanMotion();
  }