FieldHansenTesseralLinear.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.propagation.semianalytical.dsst.utilities.hansen;

  18. import java.lang.reflect.Array;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.Field;
  21. import org.hipparchus.analysis.differentiation.FieldGradient;
  22. import org.hipparchus.analysis.polynomials.PolynomialFunction;
  23. import org.hipparchus.util.FastMath;
  24. import org.hipparchus.util.MathArrays;
  25. import org.orekit.propagation.semianalytical.dsst.utilities.NewcombOperators;

  26. /**
  27.  * Hansen coefficients K(t,n,s) for t!=0 and n < 0.
  28.  * <p>
  29.  * Implements Collins 4-236 or Danielson 2.7.3-(9) for Hansen Coefficients and
  30.  * Collins 4-240 for derivatives. The recursions are transformed into
  31.  * composition of linear transformations to obtain the associated polynomials
  32.  * for coefficients and their derivatives - see Petre's paper
  33.  *
  34.  * @author Petre Bazavan
  35.  * @author Lucian Barbulescu
  36.  * @author Bryan Cazabonne
  37.  * @param <T> type of the field elements
  38.  */
  39. public class FieldHansenTesseralLinear <T extends CalculusFieldElement<T>> {

  40.     /** The number of coefficients that will be computed with a set of roots. */
  41.     private static final int SLICE = 10;

  42.     /**
  43.      * The first vector of polynomials associated to Hansen coefficients and
  44.      * derivatives.
  45.      */
  46.     private PolynomialFunction[][] mpvec;

  47.     /** The second vector of polynomials associated only to derivatives. */
  48.     private PolynomialFunction[][] mpvecDeriv;

  49.     /** The Hansen coefficients used as roots. */
  50.     private final T[][] hansenRoot;

  51.     /** The derivatives of the Hansen coefficients used as roots. */
  52.     private final T[][] hansenDerivRoot;

  53.     /** The minimum value for the order. */
  54.     private final int Nmin;

  55.     /** The index of the initial condition, Petre's paper. */
  56.     private final int N0;

  57.     /** The number of slices needed to compute the coefficients. */
  58.     private final int numSlices;

  59.     /**
  60.      * The offset used to identify the polynomial that corresponds to a negative.
  61.      * value of n in the internal array that starts at 0
  62.      */
  63.     private final int offset;

  64.     /** The objects used to calculate initial data by means of Newcomb operators. */
  65.     private final FieldHansenCoefficientsBySeries<T>[] hansenInit;

  66.     /**
  67.      * Constructor.
  68.      *
  69.      * @param nMax the maximum (absolute) value of n parameter
  70.      * @param s s parameter
  71.      * @param j j parameter
  72.      * @param n0 the minimum (absolute) value of n
  73.      * @param maxHansen maximum power of the eccentricity to use in Hansen coefficient Kernel expansion.
  74.      * @param field field used by default
  75.      */
  76.     public FieldHansenTesseralLinear(final int nMax, final int s, final int j, final int n0,
  77.                                      final int maxHansen, final Field<T> field) {
  78.         //Initialize the fields
  79.         this.offset = nMax + 1;
  80.         this.Nmin = -nMax - 1;
  81.         this.N0 = -n0 - 4;

  82.         final int maxRoots = FastMath.min(4, N0 - Nmin + 4);
  83.         //Ensure that only the needed terms are computed
  84.         this.hansenInit = (FieldHansenCoefficientsBySeries<T>[]) Array.newInstance(FieldHansenCoefficientsBySeries.class, maxRoots);
  85.         for (int i = 0; i < maxRoots; i++) {
  86.             this.hansenInit[i] = new FieldHansenCoefficientsBySeries<>(N0 - i + 3, s, j, maxHansen);
  87.         }

  88.         // The first 4 values are computed with series. No linear combination is needed.
  89.         final int size = N0 - Nmin;
  90.         this.numSlices = (int) FastMath.max(FastMath.ceil(((double) size) / SLICE), 1);
  91.         hansenRoot = MathArrays.buildArray(field, numSlices, 4);
  92.         hansenDerivRoot = MathArrays.buildArray(field, numSlices, 4);
  93.         if (size > 0) {
  94.             mpvec = new PolynomialFunction[size][];
  95.             mpvecDeriv = new PolynomialFunction[size][];

  96.             // Prepare the database of the associated polynomials
  97.             HansenUtilities.generateTesseralPolynomials(N0, Nmin, offset, SLICE, j, s,
  98.                                                         mpvec, mpvecDeriv);
  99.         }

  100.     }

  101.     /**
  102.      * Compute the values for the first four coefficients and their derivatives by means of series.
  103.      *
  104.      * @param e2 e²
  105.      * @param chi &Chi;
  106.      * @param chi2 &Chi;²
  107.      */
  108.     public void computeInitValues(final T e2, final T chi, final T chi2) {
  109.         // compute the values for n, n+1, n+2 and n+3 by series
  110.         // See Danielson 2.7.3-(10)
  111.         //Ensure that only the needed terms are computed
  112.         final int maxRoots = FastMath.min(4, N0 - Nmin + 4);
  113.         for (int i = 0; i < maxRoots; i++) {
  114.             final FieldGradient<T> hansenKernel = hansenInit[i].getValueGradient(e2, chi, chi2);
  115.             this.hansenRoot[0][i] = hansenKernel.getValue();
  116.             this.hansenDerivRoot[0][i] = hansenKernel.getPartialDerivative(0);
  117.         }

  118.         for (int i = 1; i < numSlices; i++) {
  119.             for (int k = 0; k < 4; k++) {
  120.                 final PolynomialFunction[] mv = mpvec[N0 - (i * SLICE) - k + 3 + offset];
  121.                 final PolynomialFunction[] sv = mpvecDeriv[N0 - (i * SLICE) - k + 3 + offset];

  122.                 hansenDerivRoot[i][k] = mv[3].value(chi).multiply(hansenDerivRoot[i - 1][3]).
  123.                                         add(mv[2].value(chi).multiply(hansenDerivRoot[i - 1][2])).
  124.                                         add(mv[1].value(chi).multiply(hansenDerivRoot[i - 1][1])).
  125.                                         add(mv[0].value(chi).multiply(hansenDerivRoot[i - 1][0])).
  126.                                         add(sv[3].value(chi).multiply(hansenRoot[i - 1][3])).
  127.                                         add(sv[2].value(chi).multiply(hansenRoot[i - 1][2])).
  128.                                         add(sv[1].value(chi).multiply(hansenRoot[i - 1][1])).
  129.                                         add(sv[0].value(chi).multiply(hansenRoot[i - 1][0]));

  130.                 hansenRoot[i][k] =  mv[3].value(chi).multiply(hansenRoot[i - 1][3]).
  131.                                     add(mv[2].value(chi).multiply(hansenRoot[i - 1][2])).
  132.                                     add(mv[1].value(chi).multiply(hansenRoot[i - 1][1])).
  133.                                     add(mv[0].value(chi).multiply(hansenRoot[i - 1][0]));
  134.             }
  135.         }
  136.     }

  137.     /**
  138.      * Compute the value of the Hansen coefficient K<sub>j</sub><sup>-n-1, s</sup>.
  139.      *
  140.      * @param mnm1 -n-1
  141.      * @param chi χ
  142.      * @return the coefficient K<sub>j</sub><sup>-n-1, s</sup>
  143.      */
  144.     public T getValue(final int mnm1, final T chi) {
  145.         //Compute n
  146.         final int n = -mnm1 - 1;

  147.         //Compute the potential slice
  148.         int sliceNo = (n + N0 + 4) / SLICE;
  149.         if (sliceNo < numSlices) {
  150.             //Compute the index within the slice
  151.             final int indexInSlice = (n + N0 + 4) % SLICE;

  152.             //Check if a root must be returned
  153.             if (indexInSlice <= 3) {
  154.                 return hansenRoot[sliceNo][indexInSlice];
  155.             }
  156.         } else {
  157.             //the value was a potential root for a slice, but that slice was not required
  158.             //Decrease the slice number
  159.             sliceNo--;
  160.         }

  161.         // Computes the coefficient by linear transformation
  162.         // Danielson 2.7.3-(9) or Collins 4-236 and Petre's paper
  163.         final PolynomialFunction[] v = mpvec[mnm1 + offset];
  164.         return v[3].value(chi).multiply(hansenRoot[sliceNo][3]).
  165.                add(v[2].value(chi).multiply(hansenRoot[sliceNo][2])).
  166.                add(v[1].value(chi).multiply(hansenRoot[sliceNo][1])).
  167.                add(v[0].value(chi).multiply(hansenRoot[sliceNo][0]));

  168.     }

  169.     /**
  170.      * Compute the value of the derivative dK<sub>j</sub><sup>-n-1, s</sup> / de².
  171.      *
  172.      * @param mnm1 -n-1
  173.      * @param chi χ
  174.      * @return the derivative dK<sub>j</sub><sup>-n-1, s</sup> / de²
  175.      */
  176.     public T getDerivative(final int mnm1, final T chi) {

  177.         //Compute n
  178.         final int n = -mnm1 - 1;

  179.         //Compute the potential slice
  180.         int sliceNo = (n + N0 + 4) / SLICE;
  181.         if (sliceNo < numSlices) {
  182.             //Compute the index within the slice
  183.             final int indexInSlice = (n + N0 + 4) % SLICE;

  184.             //Check if a root must be returned
  185.             if (indexInSlice <= 3) {
  186.                 return hansenDerivRoot[sliceNo][indexInSlice];
  187.             }
  188.         } else {
  189.             //the value was a potential root for a slice, but that slice was not required
  190.             //Decrease the slice number
  191.             sliceNo--;
  192.         }

  193.         // Computes the coefficient by linear transformation
  194.         // Danielson 2.7.3-(9) or Collins 4-236 and Petre's paper
  195.         final PolynomialFunction[] v = mpvec[mnm1 + this.offset];
  196.         final PolynomialFunction[] vv = mpvecDeriv[mnm1 + this.offset];

  197.         return v[3].value(chi).multiply(hansenDerivRoot[sliceNo][3]).
  198.                add(v[2].value(chi).multiply(hansenDerivRoot[sliceNo][2])).
  199.                add(v[1].value(chi).multiply(hansenDerivRoot[sliceNo][1])).
  200.                add(v[0].value(chi).multiply(hansenDerivRoot[sliceNo][0])).
  201.                add(vv[3].value(chi).multiply(hansenRoot[sliceNo][3])).
  202.                add(vv[2].value(chi).multiply(hansenRoot[sliceNo][2])).
  203.                add( vv[1].value(chi).multiply(hansenRoot[sliceNo][1])).
  204.                add(vv[0].value(chi).multiply(hansenRoot[sliceNo][0]));

  205.     }

  206.     /**
  207.      * Compute a Hansen coefficient with series.
  208.      * <p>
  209.      * This class implements the computation of the Hansen kernels
  210.      * through a power series in e² and that is using
  211.      * modified Newcomb operators. The reference formulae can be found
  212.      * in Danielson 2.7.3-10 and 3.3-5
  213.      * </p>
  214.      */
  215.     private static class FieldHansenCoefficientsBySeries <T extends CalculusFieldElement<T>> {

  216.         /** -n-1 coefficient. */
  217.         private final int mnm1;

  218.         /** s coefficient. */
  219.         private final int s;

  220.         /** j coefficient. */
  221.         private final int j;

  222.         /** Max power in e² for the Newcomb's series expansion. */
  223.         private final int maxNewcomb;

  224.         /** Polynomial representing the serie. */
  225.         private final PolynomialFunction polynomial;

  226.         /**
  227.          * Class constructor.
  228.          *
  229.          * @param mnm1      -n-1 value
  230.          * @param s         s value
  231.          * @param j         j value
  232.          * @param maxHansen max power of e² in series expansion
  233.          */
  234.         FieldHansenCoefficientsBySeries(final int mnm1, final int s,
  235.                                         final int j, final int maxHansen) {
  236.             this.mnm1 = mnm1;
  237.             this.s = s;
  238.             this.j = j;
  239.             this.maxNewcomb = maxHansen;
  240.             this.polynomial = generatePolynomial();
  241.         }

  242.         /** Computes the value of Hansen kernel and its derivative at e².
  243.          * <p>
  244.          * The formulae applied are described in Danielson 2.7.3-10 and
  245.          * 3.3-5
  246.          * </p>
  247.          * @param e2 e²
  248.          * @param chi &Chi;
  249.          * @param chi2 &Chi;²
  250.          * @return the value of the Hansen coefficient and its derivative for e²
  251.          */
  252.         private FieldGradient<T> getValueGradient(final T e2, final T chi, final T chi2) {

  253.             final T zero = e2.getField().getZero();
  254.             //Estimation of the serie expansion at e2
  255.             final FieldGradient<T> serie = polynomial.value(FieldGradient.variable(1, 0, e2));

  256.             final T value      =  FastMath.pow(chi2, -mnm1 - 1).multiply(serie.getValue()).divide(chi);
  257.             final T coef       = zero.subtract(mnm1 + 1.5);
  258.             final T derivative = coef.multiply(chi2).multiply(value).
  259.                             add(FastMath.pow(chi2, -mnm1 - 1).multiply(serie.getPartialDerivative(0)).divide(chi));
  260.             return new FieldGradient<>(value, derivative);
  261.         }

  262.         /** Generate the serie expansion in e².
  263.          * <p>
  264.          * Generate the series expansion in e² used in the formulation
  265.          * of the Hansen kernel (see Danielson 2.7.3-10):
  266.          * &Sigma; Y<sup>ns</sup><sub>α+a,α+b</sub>
  267.          * *e<sup>2α</sup>
  268.          * </p>
  269.          * @return polynomial representing the power serie expansion
  270.          */
  271.         private PolynomialFunction generatePolynomial() {
  272.             // Initialization
  273.             final int aHT = FastMath.max(j - s, 0);
  274.             final int bHT = FastMath.max(s - j, 0);

  275.             final double[] coefficients = new double[maxNewcomb + 1];

  276.             //Loop for getting the Newcomb operators
  277.             for (int alphaHT = 0; alphaHT <= maxNewcomb; alphaHT++) {
  278.                 coefficients[alphaHT] =
  279.                         NewcombOperators.getValue(alphaHT + aHT, alphaHT + bHT, mnm1, s);
  280.             }

  281.             //Creation of the polynomial
  282.             return new PolynomialFunction(coefficients);
  283.         }
  284.     }

  285. }