1   /* Copyright 2022-2025 Thales Alenia Space
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.time;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.analysis.interpolation.FieldHermiteInterpolator;
21  import org.hipparchus.analysis.interpolation.HermiteInterpolator;
22  import org.hipparchus.util.MathArrays;
23  
24  import java.util.List;
25  
26  /**bHermite interpolator of time stamped clock offsets.
27   * @param <T> type of the field elements
28   * @author Luc Maisonobe
29   * @see HermiteInterpolator
30   * @see TimeInterpolator
31   * @since 12.1
32   */
33  public class FieldClockOffsetHermiteInterpolator<T extends CalculusFieldElement<T>>
34      extends AbstractFieldTimeInterpolator<FieldClockOffset<T>, T> {
35  
36      /**
37       * Constructor with default extrapolation threshold value ({@code DEFAULT_EXTRAPOLATION_THRESHOLD_SEC} s).
38       * <p>
39       * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation
40       * points (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's
41       * phenomenon</a> and numerical problems (including NaN appearing).
42       * </p>
43       * <p>
44       * If the number of interpolation points or derivatives availability is not sufficient,
45       * the rate and acceleration of interpolated offset will be silently set to 0 (i.e.
46       * model will be constant or linear only).
47       * </p>
48       * @param interpolationPoints number of interpolation points
49       */
50      public FieldClockOffsetHermiteInterpolator(final int interpolationPoints) {
51          this(interpolationPoints, DEFAULT_EXTRAPOLATION_THRESHOLD_SEC);
52      }
53  
54      /**
55       * Constructor.
56       * <p>
57       * As this implementation of interpolation is polynomial, it should be used only with small number of interpolation
58       * points (about 10-20 points) in order to avoid <a href="http://en.wikipedia.org/wiki/Runge%27s_phenomenon">Runge's
59       * phenomenon</a> and numerical problems (including NaN appearing).
60       * </p>
61       * <p>
62       * If the number of interpolation points or derivatives availability is not sufficient,
63       * the rate and acceleration of interpolated offset will be silently set to 0 (i.e.
64       * model will be constant or linear only).
65       * </p>
66       * @param interpolationPoints number of interpolation points
67       * @param extrapolationThreshold extrapolation threshold beyond which the propagation will fail
68       */
69      public FieldClockOffsetHermiteInterpolator(final int interpolationPoints, final double extrapolationThreshold) {
70          super(interpolationPoints, extrapolationThreshold);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      protected FieldClockOffset<T> interpolate(final InterpolationData interpolationData) {
76          final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();
77  
78          // Fill interpolator with sample
79          final FieldAbsoluteDate<T>      interpolationDate = interpolationData.getInterpolationDate();
80          final List<FieldClockOffset<T>> neighborList      = interpolationData.getNeighborList();
81          for (FieldClockOffset<T> value : neighborList) {
82              final T   deltaT = value.getDate().durationFrom(interpolationDate);
83              final T[] offset = MathArrays.buildArray(interpolationDate.getField(), 1);
84              offset[0] = value.getOffset();
85              if (value.getRate() == null || value.getRate().isNaN()) {
86                  // no clock rate for this entry
87                  interpolator.addSamplePoint(deltaT, offset);
88              } else {
89                  // clock rate is available
90                  final T[] rate = MathArrays.buildArray(interpolationDate.getField(), 1);
91                  rate[0] = value.getRate();
92                  if (value.getAcceleration() == null || value.getAcceleration().isNaN()) {
93                      // no clock acceleration for this entry
94                      interpolator.addSamplePoint(deltaT, offset, rate);
95                  } else {
96                      // clock acceleration is available
97                      final T[] acceleration = MathArrays.buildArray(interpolationDate.getField(), 1);
98                      acceleration[0] = value.getAcceleration();
99                      interpolator.addSamplePoint(deltaT, offset, rate, acceleration);
100                 }
101             }
102         }
103 
104         final T[][] y = interpolator.derivatives(interpolationDate.getField().getZero(), 2);
105         return new FieldClockOffset<>(interpolationDate, y[0][0], y[1][0], y[2][0]);
106 
107     }
108 
109 }