SingleParameterFitter.java

  1. /* Copyright 2022-2025 Luc Maisonobe
  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.frames;

  18. import java.io.Serializable;
  19. import java.util.List;
  20. import java.util.ListIterator;
  21. import java.util.function.ToDoubleFunction;

  22. import org.hipparchus.util.FastMath;
  23. import org.hipparchus.util.MathUtils;
  24. import org.orekit.utils.Constants;
  25. import org.orekit.utils.SecularAndHarmonic;

  26. /** Fitter for one Earth Orientation Parameter.
  27.  * @see PredictedEOPHistory
  28.  * @see EOPFitter
  29.  * @see SecularAndHarmonic
  30.  * @since 12.0
  31.  * @author Luc Maisonobe
  32.  */
  33. public class SingleParameterFitter implements Serializable {

  34.     /** Sun pulsation, one year period. */
  35.     public static final double SUN_PULSATION = MathUtils.TWO_PI / Constants.JULIAN_YEAR;

  36.     /** Moon pulsation (one Moon draconic period). */
  37.     public static final double MOON_DRACONIC_PULSATION = MathUtils.TWO_PI / (27.212221 * Constants.JULIAN_DAY);

  38.     /** Serializable UID. */
  39.     private static final long serialVersionUID = 20230309L;

  40.     /** Time constant of the exponential decay weight. */
  41.     private final double timeConstant;

  42.     /** Convergence on fitted parameter. */
  43.     private final double convergence;

  44.     /** Degree of the polynomial model. */
  45.     private final int degree;

  46.     /** Pulsations of harmonic part (rad/s). */
  47.     private final double[] pulsations;

  48.     /** Simple constructor.
  49.      * @param timeConstant time constant \(\tau\) of the exponential decay weight, point weight is \(e^{\frac{t-t_0}{\tau}}\),
  50.      * i.e. points far in the past before \(t_0\) have smaller weights
  51.      * @param convergence convergence on fitted parameter
  52.      * @param degree degree of the polynomial model
  53.      * @param pulsations pulsations of harmonic part (rad/s)
  54.      * @see #createDefaultDut1FitterShortTermPrediction()
  55.      * @see #createDefaultDut1FitterLongTermPrediction()
  56.      * @see #createDefaultPoleFitterShortTermPrediction()
  57.      * @see #createDefaultPoleFitterLongTermPrediction()
  58.      * @see #createDefaultNutationFitterShortTermPrediction()
  59.      * @see #createDefaultNutationFitterLongTermPrediction()
  60.      * @see SecularAndHarmonic
  61.      * @since 12.0.1
  62.      */
  63.     public SingleParameterFitter(final double timeConstant, final double convergence,
  64.                                  final int degree, final double... pulsations) {
  65.         this.timeConstant    = timeConstant;
  66.         this.convergence     = convergence;
  67.         this.degree          = degree;
  68.         this.pulsations      = pulsations.clone();
  69.     }

  70.     /** Perform secular and harmonic fitting.
  71.      * @param rawHistory EOP history to fit
  72.      * @param extractor extractor for Earth Orientation Parameter
  73.      * @return configured fitter
  74.      */
  75.     public SecularAndHarmonic fit(final EOPHistory rawHistory, final ToDoubleFunction<EOPEntry> extractor) {

  76.         final List<EOPEntry> rawEntries = rawHistory.getEntries();
  77.         final EOPEntry       last       = rawEntries.get(rawEntries.size() - 1);

  78.         // create fitter
  79.         final SecularAndHarmonic sh = new SecularAndHarmonic(degree, pulsations);

  80.         // set up convergence
  81.         sh.setConvergenceRMS(convergence);

  82.         // set up reference date and initial guess to a constant value
  83.         final double[] initialGuess = new double[degree + 1 + 2 * pulsations.length];
  84.         initialGuess[0] = extractor.applyAsDouble(last);
  85.         sh.resetFitting(last.getDate(), initialGuess);

  86.         // sample history
  87.         final ListIterator<EOPEntry> backwardIterator = rawEntries.listIterator(rawEntries.size());
  88.         while (backwardIterator.hasPrevious()) {
  89.             final EOPEntry entry = backwardIterator.previous();
  90.             sh.addWeightedPoint(entry.getDate(), extractor.applyAsDouble(entry),
  91.                                 FastMath.exp(entry.getDate().durationFrom(last.getDate()) / timeConstant));
  92.         }

  93.         // perform fitting
  94.         sh.fit();

  95.         return sh;

  96.     }

  97.     /** Create fitter with default parameters adapted for fitting orientation parameters dUT1 and LOD
  98.      * for short term prediction.
  99.      * <p>
  100.      * The main difference between these settings and {@link #createDefaultDut1FitterLongTermPrediction()
  101.      * the settings for long prediction} is the much smaller \(\tau\). This means more
  102.      * weight is set to the points at the end of the history, hence forcing the fitted prediction
  103.      * model to be closer to these points, hence the prediction error to be smaller just after
  104.      * raw history end. On the other hand, this implies that the model will diverge on long term.
  105.      * These settings are intended when prediction is used for at most 5 days after raw EOP end.
  106.      * </p>
  107.      * <ul>
  108.      *   <li>time constant \(\tau\) of the exponential decay set to 6 {@link Constants#JULIAN_DAY days}</li>
  109.      *   <li>convergence set to 10⁻¹² s</li>
  110.      *   <li>polynomial part set to degree 3</li>
  111.      *   <li>one harmonic term at {@link #SUN_PULSATION}}</li>
  112.      *   <li>one harmonic term at 2 times {@link #SUN_PULSATION}}</li>
  113.      *   <li>one harmonic term at 3 times {@link #SUN_PULSATION}}</li>
  114.      *   <li>one harmonic term at {@link #MOON_DRACONIC_PULSATION}}</li>
  115.      *   <li>one harmonic term at 2 times {@link #MOON_DRACONIC_PULSATION}}</li>
  116.      *   <li>one harmonic term at 3 times {@link #MOON_DRACONIC_PULSATION}}</li>
  117.      * </ul>
  118.      * @return fitter with default configuration for orientation parameters dUT1 and LOD
  119.      * @see #createDefaultDut1FitterShortTermPrediction()
  120.      */
  121.     public static SingleParameterFitter createDefaultDut1FitterShortTermPrediction() {
  122.         return new SingleParameterFitter(6 * Constants.JULIAN_DAY, 1.0e-12, 3,
  123.                                          SUN_PULSATION, 2 * SUN_PULSATION, 3 * SUN_PULSATION,
  124.                                          MOON_DRACONIC_PULSATION, 2 * MOON_DRACONIC_PULSATION, 3 * MOON_DRACONIC_PULSATION);
  125.     }

  126.     /** Create fitter with default parameters adapted for fitting orientation parameters dUT1 and LOD
  127.      * for long term prediction.
  128.      * <p>
  129.      * The main difference between these settings and {@link #createDefaultDut1FitterShortTermPrediction()
  130.      * the settings for short prediction} is the much larger \(\tau\). This means weight
  131.      * is spread throughout history, hence forcing the fitted prediction model to be remain very stable
  132.      * on the long term. On the other hand, this implies that the model will start with already a much
  133.      * larger error just after raw history end.
  134.      * These settings are intended when prediction is used for 5 days after raw EOP end or more.
  135.      * </p>
  136.      * <ul>
  137.      *   <li>time constant \(\tau\) of the exponential decay set to 60 {@link Constants#JULIAN_DAY days}</li>
  138.      *   <li>convergence set to 10⁻¹² s</li>
  139.      *   <li>polynomial part set to degree 3</li>
  140.      *   <li>one harmonic term at {@link #SUN_PULSATION}}</li>
  141.      *   <li>one harmonic term at 2 times {@link #SUN_PULSATION}}</li>
  142.      *   <li>one harmonic term at 3 times {@link #SUN_PULSATION}}</li>
  143.      *   <li>one harmonic term at {@link #MOON_DRACONIC_PULSATION}}</li>
  144.      *   <li>one harmonic term at 2 times {@link #MOON_DRACONIC_PULSATION}}</li>
  145.      *   <li>one harmonic term at 3 times {@link #MOON_DRACONIC_PULSATION}}</li>
  146.      * </ul>
  147.      * @return fitter with default configuration for orientation parameters dUT1 and LOD
  148.      * @see #createDefaultDut1FitterShortTermPrediction()
  149.      */
  150.     public static SingleParameterFitter createDefaultDut1FitterLongTermPrediction() {
  151.         return new SingleParameterFitter(60 * Constants.JULIAN_DAY, 1.0e-12, 3,
  152.                                          SUN_PULSATION, 2 * SUN_PULSATION, 3 * SUN_PULSATION,
  153.                                          MOON_DRACONIC_PULSATION, 2 * MOON_DRACONIC_PULSATION, 3 * MOON_DRACONIC_PULSATION);
  154.     }

  155.     /** Create fitter with default parameters adapted for fitting pole parameters Xp and Yp
  156.      * for long term prediction.
  157.      * <p>
  158.      * The main difference between these settings and {@link #createDefaultPoleFitterLongTermPrediction()
  159.      * the settings for long prediction} is the much smaller \(\tau\). This means more
  160.      * weight is set to the points at the end of the history, hence forcing the fitted prediction
  161.      * model to be closer to these points, hence the prediction error to be smaller just after
  162.      * raw history end. On the other hand, this implies that the model will diverge on long term.
  163.      * These settings are intended when prediction is used for at most 5 days after raw EOP end.
  164.      * </p>
  165.      * <ul>
  166.      *   <li>time constant \(\tau\) of the exponential decay set to 12 {@link Constants#JULIAN_DAY days}</li>
  167.      *   <li>convergence set to 10⁻¹² rad</li>
  168.      *   <li>polynomial part set to degree 3</li>
  169.      *   <li>one harmonic term at {@link #SUN_PULSATION}}</li>
  170.      *   <li>one harmonic term at 2 times {@link #SUN_PULSATION}}</li>
  171.      *   <li>one harmonic term at 3 times {@link #SUN_PULSATION}}</li>
  172.      *   <li>one harmonic term at {@link #MOON_DRACONIC_PULSATION}}</li>
  173.      *   <li>one harmonic term at 2 times {@link #MOON_DRACONIC_PULSATION}}</li>
  174.      *   <li>one harmonic term at 3 times {@link #MOON_DRACONIC_PULSATION}}</li>
  175.      * </ul>
  176.      * @return fitter with default configuration for pole parameters Xp and Yp
  177.      */
  178.     public static SingleParameterFitter createDefaultPoleFitterShortTermPrediction() {
  179.         return new SingleParameterFitter(12 * Constants.JULIAN_DAY, 1.0e-12, 3,
  180.                                          SUN_PULSATION, 2 * SUN_PULSATION, 3 * SUN_PULSATION,
  181.                                          MOON_DRACONIC_PULSATION, 2 * MOON_DRACONIC_PULSATION, 3 * MOON_DRACONIC_PULSATION);
  182.     }

  183.     /** Create fitter with default parameters adapted for fitting pole parameters Xp and Yp
  184.      * for long term prediction.
  185.      * <p>
  186.      * The main difference between these settings and {@link #createDefaultPoleFitterShortTermPrediction()
  187.      * the settings for short prediction} is the much larger \(\tau\). This means weight
  188.      * is spread throughout history, hence forcing the fitted prediction model to be remain very stable
  189.      * on the long term. On the other hand, this implies that the model will start with already a much
  190.      * larger error just after raw history end.
  191.      * These settings are intended when prediction is used for 5 days after raw EOP end or more.
  192.      * </p>
  193.      * <ul>
  194.      *   <li>time constant \(\tau\) of the exponential decay set to 60 {@link Constants#JULIAN_DAY days}</li>
  195.      *   <li>convergence set to 10⁻¹² rad</li>
  196.      *   <li>polynomial part set to degree 3</li>
  197.      *   <li>one harmonic term at {@link #SUN_PULSATION}}</li>
  198.      *   <li>one harmonic term at 2 times {@link #SUN_PULSATION}}</li>
  199.      *   <li>one harmonic term at 3 times {@link #SUN_PULSATION}}</li>
  200.      *   <li>one harmonic term at {@link #MOON_DRACONIC_PULSATION}}</li>
  201.      *   <li>one harmonic term at 2 times {@link #MOON_DRACONIC_PULSATION}}</li>
  202.      *   <li>one harmonic term at 3 times {@link #MOON_DRACONIC_PULSATION}}</li>
  203.      * </ul>
  204.      * @return fitter with default configuration for pole parameters Xp and Yp
  205.      */
  206.     public static SingleParameterFitter createDefaultPoleFitterLongTermPrediction() {
  207.         return new SingleParameterFitter(60 * Constants.JULIAN_DAY, 1.0e-12, 3,
  208.                                          SUN_PULSATION, 2 * SUN_PULSATION, 3 * SUN_PULSATION,
  209.                                          MOON_DRACONIC_PULSATION, 2 * MOON_DRACONIC_PULSATION, 3 * MOON_DRACONIC_PULSATION);
  210.     }

  211.     /** Create fitter with default parameters adapted for fitting nutation parameters dx and dy
  212.      * for long term prediction.
  213.      * <p>
  214.      * The main difference between these settings and {@link #createDefaultNutationFitterLongTermPrediction()
  215.      * the settings for long prediction} is the much smaller \(\tau\). This means more
  216.      * weight is set to the points at the end of the history, hence forcing the fitted prediction
  217.      * model to be closer to these points, hence the prediction error to be smaller just after
  218.      * raw history end. On the other hand, this implies that the model will diverge on long term.
  219.      * These settings are intended when prediction is used for at most 5 days after raw EOP end.
  220.      * </p>
  221.      * <ul>
  222.      *   <li>time constant \(\tau\) of the exponential decay set to 12 {@link Constants#JULIAN_DAY days}</li>
  223.      *   <li>convergence set to 10⁻¹² s</li>
  224.      *   <li>polynomial part set to degree 3</li>
  225.      *   <li>one harmonic term at {@link #SUN_PULSATION}}</li>
  226.      *   <li>one harmonic term at 2 times {@link #SUN_PULSATION}}</li>
  227.      *   <li>one harmonic term at 3 times {@link #SUN_PULSATION}}</li>
  228.      *   <li>one harmonic term at {@link #MOON_DRACONIC_PULSATION}}</li>
  229.      *   <li>one harmonic term at 2 times {@link #MOON_DRACONIC_PULSATION}}</li>
  230.      *   <li>one harmonic term at 3 times {@link #MOON_DRACONIC_PULSATION}}</li>
  231.      * </ul>
  232.      * @return fitter with default configuration for pole nutation parameters dx and dy
  233.      */
  234.     public static SingleParameterFitter createDefaultNutationFitterShortTermPrediction() {
  235.         return new SingleParameterFitter(12 * Constants.JULIAN_DAY, 1.0e-12, 3,
  236.                                          SUN_PULSATION, 2 * SUN_PULSATION, 3 * SUN_PULSATION,
  237.                                          MOON_DRACONIC_PULSATION, 2 * MOON_DRACONIC_PULSATION, 3 * MOON_DRACONIC_PULSATION);
  238.     }

  239.     /** Create fitter with default parameters adapted for fitting nutation parameters dx and dy
  240.      * for long term prediction.
  241.      * <p>
  242.      * The main difference between these settings and {@link #createDefaultNutationFitterShortTermPrediction()
  243.      * the settings for short prediction} is the much larger \(\tau\). This means weight
  244.      * is spread throughout history, hence forcing the fitted prediction model to be remain very stable
  245.      * on the long term. On the other hand, this implies that the model will start with already a much
  246.      * larger error just after raw history end.
  247.      * These settings are intended when prediction is used for 5 days after raw EOP end or more.
  248.      * </p>
  249.      * <ul>
  250.      *   <li>time constant \(\tau\) of the exponential decay set to 60 {@link Constants#JULIAN_DAY days}</li>
  251.      *   <li>convergence set to 10⁻¹² s</li>
  252.      *   <li>polynomial part set to degree 3</li>
  253.      *   <li>one harmonic term at {@link #SUN_PULSATION}}</li>
  254.      *   <li>one harmonic term at 2 times {@link #SUN_PULSATION}}</li>
  255.      *   <li>one harmonic term at 3 times {@link #SUN_PULSATION}}</li>
  256.      *   <li>one harmonic term at {@link #MOON_DRACONIC_PULSATION}}</li>
  257.      *   <li>one harmonic term at 2 times {@link #MOON_DRACONIC_PULSATION}}</li>
  258.      *   <li>one harmonic term at 3 times {@link #MOON_DRACONIC_PULSATION}}</li>
  259.      * </ul>
  260.      * @return fitter with default configuration for pole nutation parameters dx and dy
  261.      */
  262.     public static SingleParameterFitter createDefaultNutationFitterLongTermPrediction() {
  263.         return new SingleParameterFitter(60 * Constants.JULIAN_DAY, 1.0e-12, 3,
  264.                                          SUN_PULSATION, 2 * SUN_PULSATION, 3 * SUN_PULSATION,
  265.                                          MOON_DRACONIC_PULSATION, 2 * MOON_DRACONIC_PULSATION, 3 * MOON_DRACONIC_PULSATION);
  266.     }

  267. }