PosVelChebyshev.java

  1. /* Copyright 2002-2025 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 org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.orekit.time.AbsoluteDate;
  22. import org.orekit.time.FieldAbsoluteDate;
  23. import org.orekit.time.TimeScale;
  24. import org.orekit.time.TimeStamped;
  25. import org.orekit.utils.FieldPVCoordinates;
  26. import org.orekit.utils.PVCoordinates;


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

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

  36.     /** Start of the validity range of the instance. */
  37.     private final AbsoluteDate start;

  38.     /** Duration of validity range of the instance. */
  39.     private final double duration;

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

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

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

  46.     /** Velocity scale for internal use. */
  47.     private final double vScale;
  48.     /** Acceleration scale for internal use. */
  49.     private final double aScale;

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

  72.     /** {@inheritDoc} */
  73.     public AbsoluteDate getDate() {
  74.         return start;
  75.     }

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

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

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

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

  104.         // normalize date
  105.         final double t = computeValueIndependentVariable(date);
  106.         final double twoT = 2 * t;

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

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

  115.             // consider last computed polynomials on position
  116.             xP += xCoeffs[k] * pK;
  117.             yP += yCoeffs[k] * pK;
  118.             zP += zCoeffs[k] * pK;

  119.             // compute next Chebyshev polynomial value
  120.             final double pKm2 = pKm1;
  121.             pKm1 = pK;
  122.             pK   = twoT * pKm1 - pKm2;

  123.         }

  124.         return new Vector3D(xP, yP, zP);
  125.     }

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

  132.         final T zero = date.getField().getZero();
  133.         final T one  = date.getField().getOne();

  134.         // normalize date
  135.         final T t = computeValueIndependentVariable(date);
  136.         final T twoT = t.add(t);

  137.         // initialize Chebyshev polynomials recursion
  138.         T pKm1 = one;
  139.         T pK   = t;
  140.         T xP   = zero.newInstance(xCoeffs[0]);
  141.         T yP   = zero.newInstance(yCoeffs[0]);
  142.         T zP   = zero.newInstance(zCoeffs[0]);

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

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

  149.             // compute next Chebyshev polynomial value
  150.             final T pKm2 = pKm1;
  151.             pKm1 = pK;
  152.             pK   = twoT.multiply(pKm1).subtract(pKm2);

  153.         }

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

  155.     }

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

  161.         // normalize date
  162.         final double t = computeValueIndependentVariable(date);
  163.         final double twoT = 2 * t;

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

  170.         // initialize Chebyshev polynomials derivatives recursion
  171.         double qKm1 = 0;
  172.         double qK   = 1;
  173.         double xV   = 0;
  174.         double yV   = 0;
  175.         double zV   = 0;

  176.         // initialize Chebyshev polynomials second derivatives recursion
  177.         double rKm1 = 0;
  178.         double rK   = 0;
  179.         double xA   = 0;
  180.         double yA   = 0;
  181.         double zA   = 0;

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

  184.             // consider last computed polynomials on position
  185.             xP += xCoeffs[k] * pK;
  186.             yP += yCoeffs[k] * pK;
  187.             zP += zCoeffs[k] * pK;

  188.             // consider last computed polynomials on velocity
  189.             xV += xCoeffs[k] * qK;
  190.             yV += yCoeffs[k] * qK;
  191.             zV += zCoeffs[k] * qK;

  192.             // consider last computed polynomials on acceleration
  193.             xA += xCoeffs[k] * rK;
  194.             yA += yCoeffs[k] * rK;
  195.             zA += zCoeffs[k] * rK;

  196.             // compute next Chebyshev polynomial value
  197.             final double pKm2 = pKm1;
  198.             pKm1 = pK;
  199.             pK   = twoT * pKm1 - pKm2;

  200.             // compute next Chebyshev polynomial derivative
  201.             final double qKm2 = qKm1;
  202.             qKm1 = qK;
  203.             qK   = twoT * qKm1 + 2 * pKm1 - qKm2;

  204.             // compute next Chebyshev polynomial second derivative
  205.             final double rKm2 = rKm1;
  206.             rKm1 = rK;
  207.             rK   = twoT * rKm1 + 4 * qKm1 - rKm2;

  208.         }

  209.         return new PVCoordinates(new Vector3D(xP, yP, zP),
  210.                                  new Vector3D(xV * vScale, yV * vScale, zV * vScale),
  211.                                  new Vector3D(xA * aScale, yA * aScale, zA * aScale));

  212.     }

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

  219.         final T zero = date.getField().getZero();
  220.         final T one  = date.getField().getOne();

  221.         // normalize date
  222.         final T t = computeValueIndependentVariable(date);
  223.         final T twoT = t.add(t);

  224.         // initialize Chebyshev polynomials recursion
  225.         T pKm1 = one;
  226.         T pK   = t;
  227.         T xP   = zero.newInstance(xCoeffs[0]);
  228.         T yP   = zero.newInstance(yCoeffs[0]);
  229.         T zP   = zero.newInstance(zCoeffs[0]);

  230.         // initialize Chebyshev polynomials derivatives recursion
  231.         T qKm1 = zero;
  232.         T qK   = one;
  233.         T xV   = zero;
  234.         T yV   = zero;
  235.         T zV   = zero;

  236.         // initialize Chebyshev polynomials second derivatives recursion
  237.         T rKm1 = zero;
  238.         T rK   = zero;
  239.         T xA   = zero;
  240.         T yA   = zero;
  241.         T zA   = zero;

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

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

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

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

  256.             // compute next Chebyshev polynomial value
  257.             final T pKm2 = pKm1;
  258.             pKm1 = pK;
  259.             pK   = twoT.multiply(pKm1).subtract(pKm2);

  260.             // compute next Chebyshev polynomial derivative
  261.             final T qKm2 = qKm1;
  262.             qKm1 = qK;
  263.             qK   = twoT.multiply(qKm1).add(pKm1.multiply(2)).subtract(qKm2);

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

  268.         }

  269.         return new FieldPVCoordinates<>(new FieldVector3D<>(xP, yP, zP),
  270.                                         new FieldVector3D<>(xV.multiply(vScale), yV.multiply(vScale), zV.multiply(vScale)),
  271.                                         new FieldVector3D<>(xA.multiply(aScale), yA.multiply(aScale), zA.multiply(aScale)));

  272.     }

  273. }