PosVelChebyshev.java

  1. /* Copyright 2002-2023 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.bodies;

  18. import java.io.Serializable;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  21. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  22. import org.orekit.time.AbsoluteDate;
  23. import org.orekit.time.FieldAbsoluteDate;
  24. import org.orekit.time.TimeScale;
  25. import org.orekit.time.TimeStamped;
  26. import org.orekit.utils.FieldPVCoordinates;
  27. import org.orekit.utils.PVCoordinates;


  28. /** Position-Velocity model based on Chebyshev polynomials.
  29.  * <p>This class represent the most basic element of the piecewise ephemerides
  30.  * for solar system bodies like JPL DE 405 ephemerides.</p>
  31.  * @see JPLEphemeridesLoader
  32.  * @author Luc Maisonobe
  33.  */
  34. class PosVelChebyshev implements TimeStamped, Serializable {

  35.     /** Serializable UID. */
  36.     private static final long serialVersionUID = 20151023L;

  37.     /** Time scale in which the ephemeris is defined. */
  38.     private final TimeScale timeScale;

  39.     /** Start of the validity range of the instance. */
  40.     private final AbsoluteDate start;

  41.     /** Duration of validity range of the instance. */
  42.     private final double duration;

  43.     /** Chebyshev polynomials coefficients for the X component. */
  44.     private final double[] xCoeffs;

  45.     /** Chebyshev polynomials coefficients for the Y component. */
  46.     private final double[] yCoeffs;

  47.     /** Chebyshev polynomials coefficients for the Z component. */
  48.     private final double[] zCoeffs;

  49.     /** Velocity scale for internal use. */
  50.     private final double vScale;
  51.     /** Acceleration scale for internal use. */
  52.     private final double aScale;

  53.     /** Simple constructor.
  54.      * @param start start of the validity range of the instance
  55.      * @param timeScale time scale in which the ephemeris is defined
  56.      * @param duration duration of the validity range of the instance
  57.      * @param xCoeffs Chebyshev polynomials coefficients for the X component
  58.      * (a reference to the array will be stored in the instance)
  59.      * @param yCoeffs Chebyshev polynomials coefficients for the Y component
  60.      * (a reference to the array will be stored in the instance)
  61.      * @param zCoeffs Chebyshev polynomials coefficients for the Z component
  62.      * (a reference to the array will be stored in the instance)
  63.      */
  64.     PosVelChebyshev(final AbsoluteDate start, final TimeScale timeScale, final double duration,
  65.                     final double[] xCoeffs, final double[] yCoeffs, final double[] zCoeffs) {
  66.         this.start     = start;
  67.         this.timeScale = timeScale;
  68.         this.duration  = duration;
  69.         this.xCoeffs   = xCoeffs;
  70.         this.yCoeffs   = yCoeffs;
  71.         this.zCoeffs   = zCoeffs;
  72.         this.vScale = 2 / duration;
  73.         this.aScale = this.vScale * this.vScale;
  74.     }

  75.     /** {@inheritDoc} */
  76.     public AbsoluteDate getDate() {
  77.         return start;
  78.     }

  79.     /** Compute value of Chebyshev's polynomial independent variable.
  80.      * @param date date
  81.      * @return double independent variable value
  82.      */
  83.     private double computeValueIndependentVariable(final AbsoluteDate date) {
  84.         return (2 * date.offsetFrom(start, timeScale) - duration) / duration;
  85.     }

  86.     /** Compute value of Chebyshev's polynomial independent variable.
  87.      * @param date date
  88.      * @param <T> type of the field elements
  89.      * @return <T> independent variable value
  90.      */
  91.     private <T extends CalculusFieldElement<T>> T computeValueIndependentVariable(final FieldAbsoluteDate<T> date) {
  92.         return date.offsetFrom(new FieldAbsoluteDate<>(date.getField(), start), timeScale).multiply(2).subtract(duration).divide(duration);
  93.     }

  94.     /** Check if a date is in validity range.
  95.      * @param date date to check
  96.      * @return true if date is in validity range
  97.      */
  98.     public boolean inRange(final AbsoluteDate date) {
  99.         final double dt = date.offsetFrom(start, timeScale);
  100.         return dt >= -0.001 && dt <= duration + 0.001;
  101.     }

  102.     /** Get the position at a specified date.
  103.      * @param date date at which position is requested
  104.      * @return position at specified date
  105.      */
  106.     Vector3D getPosition(final AbsoluteDate date) {

  107.         // normalize date
  108.         final double t = computeValueIndependentVariable(date);
  109.         final double twoT = 2 * t;

  110.         // initialize Chebyshev polynomials recursion
  111.         double pKm1 = 1;
  112.         double pK   = t;
  113.         double xP   = xCoeffs[0];
  114.         double yP   = yCoeffs[0];
  115.         double zP   = zCoeffs[0];

  116.         // combine polynomials by applying coefficients
  117.         for (int k = 1; k < xCoeffs.length; ++k) {

  118.             // consider last computed polynomials on position
  119.             xP += xCoeffs[k] * pK;
  120.             yP += yCoeffs[k] * pK;
  121.             zP += zCoeffs[k] * pK;

  122.             // compute next Chebyshev polynomial value
  123.             final double pKm2 = pKm1;
  124.             pKm1 = pK;
  125.             pK   = twoT * pKm1 - pKm2;

  126.         }

  127.         return new Vector3D(xP, yP, zP);
  128.     }

  129.     /** Get the position at a specified date.
  130.      * @param date date at which position is requested
  131.      * @param <T> type of the field elements
  132.      * @return position at specified date
  133.      */
  134.     <T extends CalculusFieldElement<T>> FieldVector3D<T> getPosition(final FieldAbsoluteDate<T> date) {

  135.         final T zero = date.getField().getZero();
  136.         final T one  = date.getField().getOne();

  137.         // normalize date
  138.         final T t = computeValueIndependentVariable(date);
  139.         final T twoT = t.add(t);

  140.         // initialize Chebyshev polynomials recursion
  141.         T pKm1 = one;
  142.         T pK   = t;
  143.         T xP   = zero.add(xCoeffs[0]);
  144.         T yP   = zero.add(yCoeffs[0]);
  145.         T zP   = zero.add(zCoeffs[0]);

  146.         // combine polynomials by applying coefficients
  147.         for (int k = 1; k < xCoeffs.length; ++k) {

  148.             // consider last computed polynomials on position
  149.             xP = xP.add(pK.multiply(xCoeffs[k]));
  150.             yP = yP.add(pK.multiply(yCoeffs[k]));
  151.             zP = zP.add(pK.multiply(zCoeffs[k]));

  152.             // compute next Chebyshev polynomial value
  153.             final T pKm2 = pKm1;
  154.             pKm1 = pK;
  155.             pK   = twoT.multiply(pKm1).subtract(pKm2);

  156.         }

  157.         return new FieldVector3D<>(xP, yP, zP);

  158.     }

  159.     /** Get the position-velocity-acceleration at a specified date.
  160.      * @param date date at which position-velocity-acceleration is requested
  161.      * @return position-velocity-acceleration at specified date
  162.      */
  163.     PVCoordinates getPositionVelocityAcceleration(final AbsoluteDate date) {

  164.         // normalize date
  165.         final double t = computeValueIndependentVariable(date);
  166.         final double twoT = 2 * t;

  167.         // initialize Chebyshev polynomials recursion
  168.         double pKm1 = 1;
  169.         double pK   = t;
  170.         double xP   = xCoeffs[0];
  171.         double yP   = yCoeffs[0];
  172.         double zP   = zCoeffs[0];

  173.         // initialize Chebyshev polynomials derivatives recursion
  174.         double qKm1 = 0;
  175.         double qK   = 1;
  176.         double xV   = 0;
  177.         double yV   = 0;
  178.         double zV   = 0;

  179.         // initialize Chebyshev polynomials second derivatives recursion
  180.         double rKm1 = 0;
  181.         double rK   = 0;
  182.         double xA   = 0;
  183.         double yA   = 0;
  184.         double zA   = 0;

  185.         // combine polynomials by applying coefficients
  186.         for (int k = 1; k < xCoeffs.length; ++k) {

  187.             // consider last computed polynomials on position
  188.             xP += xCoeffs[k] * pK;
  189.             yP += yCoeffs[k] * pK;
  190.             zP += zCoeffs[k] * pK;

  191.             // consider last computed polynomials on velocity
  192.             xV += xCoeffs[k] * qK;
  193.             yV += yCoeffs[k] * qK;
  194.             zV += zCoeffs[k] * qK;

  195.             // consider last computed polynomials on acceleration
  196.             xA += xCoeffs[k] * rK;
  197.             yA += yCoeffs[k] * rK;
  198.             zA += zCoeffs[k] * rK;

  199.             // compute next Chebyshev polynomial value
  200.             final double pKm2 = pKm1;
  201.             pKm1 = pK;
  202.             pK   = twoT * pKm1 - pKm2;

  203.             // compute next Chebyshev polynomial derivative
  204.             final double qKm2 = qKm1;
  205.             qKm1 = qK;
  206.             qK   = twoT * qKm1 + 2 * pKm1 - qKm2;

  207.             // compute next Chebyshev polynomial second derivative
  208.             final double rKm2 = rKm1;
  209.             rKm1 = rK;
  210.             rK   = twoT * rKm1 + 4 * qKm1 - rKm2;

  211.         }

  212.         return new PVCoordinates(new Vector3D(xP, yP, zP),
  213.                                  new Vector3D(xV * vScale, yV * vScale, zV * vScale),
  214.                                  new Vector3D(xA * aScale, yA * aScale, zA * aScale));

  215.     }

  216.     /** Get the position-velocity-acceleration at a specified date.
  217.      * @param date date at which position-velocity-acceleration is requested
  218.      * @param <T> type of the field elements
  219.      * @return position-velocity-acceleration at specified date
  220.      */
  221.     <T extends CalculusFieldElement<T>> FieldPVCoordinates<T> getPositionVelocityAcceleration(final FieldAbsoluteDate<T> date) {

  222.         final T zero = date.getField().getZero();
  223.         final T one  = date.getField().getOne();

  224.         // normalize date
  225.         final T t = computeValueIndependentVariable(date);
  226.         final T twoT = t.add(t);

  227.         // initialize Chebyshev polynomials recursion
  228.         T pKm1 = one;
  229.         T pK   = t;
  230.         T xP   = zero.add(xCoeffs[0]);
  231.         T yP   = zero.add(yCoeffs[0]);
  232.         T zP   = zero.add(zCoeffs[0]);

  233.         // initialize Chebyshev polynomials derivatives recursion
  234.         T qKm1 = zero;
  235.         T qK   = one;
  236.         T xV   = zero;
  237.         T yV   = zero;
  238.         T zV   = zero;

  239.         // initialize Chebyshev polynomials second derivatives recursion
  240.         T rKm1 = zero;
  241.         T rK   = zero;
  242.         T xA   = zero;
  243.         T yA   = zero;
  244.         T zA   = zero;

  245.         // combine polynomials by applying coefficients
  246.         for (int k = 1; k < xCoeffs.length; ++k) {

  247.             // consider last computed polynomials on position
  248.             xP = xP.add(pK.multiply(xCoeffs[k]));
  249.             yP = yP.add(pK.multiply(yCoeffs[k]));
  250.             zP = zP.add(pK.multiply(zCoeffs[k]));

  251.             // consider last computed polynomials on velocity
  252.             xV = xV.add(qK.multiply(xCoeffs[k]));
  253.             yV = yV.add(qK.multiply(yCoeffs[k]));
  254.             zV = zV.add(qK.multiply(zCoeffs[k]));

  255.             // consider last computed polynomials on acceleration
  256.             xA = xA.add(rK.multiply(xCoeffs[k]));
  257.             yA = yA.add(rK.multiply(yCoeffs[k]));
  258.             zA = zA.add(rK.multiply(zCoeffs[k]));

  259.             // compute next Chebyshev polynomial value
  260.             final T pKm2 = pKm1;
  261.             pKm1 = pK;
  262.             pK   = twoT.multiply(pKm1).subtract(pKm2);

  263.             // compute next Chebyshev polynomial derivative
  264.             final T qKm2 = qKm1;
  265.             qKm1 = qK;
  266.             qK   = twoT.multiply(qKm1).add(pKm1.multiply(2)).subtract(qKm2);

  267.             // compute next Chebyshev polynomial second derivative
  268.             final T rKm2 = rKm1;
  269.             rKm1 = rK;
  270.             rK   = twoT.multiply(rKm1).add(qKm1.multiply(4)).subtract(rKm2);

  271.         }

  272.         return new FieldPVCoordinates<>(new FieldVector3D<>(xP, yP, zP),
  273.                                         new FieldVector3D<>(xV.multiply(vScale), yV.multiply(vScale), zV.multiply(vScale)),
  274.                                         new FieldVector3D<>(xA.multiply(aScale), yA.multiply(aScale), zA.multiply(aScale)));

  275.     }

  276. }