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.Field;
21  import org.hipparchus.analysis.polynomials.PolynomialFunction;
22  import org.hipparchus.util.Binary64Field;
23  import org.hipparchus.util.FastMath;
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  import org.orekit.Utils;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  public class SampledClockModelTest {
33  
34      @Test
35      public void testCubicNoRate() {
36          final PolynomialFunction c       = new PolynomialFunction(FastMath.scalb(1.0, -6),
37                                                                    FastMath.scalb(1.0, -7),
38                                                                    FastMath.scalb(1.0, -8),
39                                                                    FastMath.scalb(1.0, -9));
40          final PolynomialFunction cDot    = c.polynomialDerivative();
41          final PolynomialFunction cDotDot = cDot.polynomialDerivative();
42          final AbsoluteDate       t0      = new AbsoluteDate(2020, 4, 1, TimeScalesFactory.getUTC());
43          final List<ClockOffset> sample = new ArrayList<>();
44          for (double dt = 0; dt < 10; dt += FastMath.scalb(1, -4)) {
45              sample.add(new ClockOffset(t0.shiftedBy(dt), c.value(dt), Double.NaN, Double.NaN));
46          }
47          final SampledClockModel clockModel = new SampledClockModel(sample, 4);
48          for (double dt = 0.02; dt < 0.98; dt += 0.02) {
49              final ClockOffset co = clockModel.getOffset(t0.shiftedBy(dt));
50              Assertions.assertEquals(dt,                co.getDate().durationFrom(t0), 1.0e-15);
51              Assertions.assertEquals(c.value(dt),       co.getOffset(),                1.0e-15);
52              Assertions.assertEquals(cDot.value(dt),    co.getRate(),                  1.0e-15);
53              Assertions.assertEquals(cDotDot.value(dt), co.getAcceleration(),          1.0e-15);
54          }
55  
56          Assertions.assertEquals(t0, clockModel.getCache().getEarliest().getDate());
57          Assertions.assertEquals(t0.shiftedBy(9.9375), clockModel.getCache().getLatest().getDate());
58          Assertions.assertEquals(4, clockModel.getCache().getMaxNeighborsSize());
59          Assertions.assertEquals(160, clockModel.getCache().getAll().size());
60  
61      }
62  
63      @Test
64      public void testCubicNoAcceleration() {
65          final PolynomialFunction c       = new PolynomialFunction(FastMath.scalb(1.0, -6),
66                                                                    FastMath.scalb(1.0, -7),
67                                                                    FastMath.scalb(1.0, -8),
68                                                                    FastMath.scalb(1.0, -9));
69          final PolynomialFunction cDot    = c.polynomialDerivative();
70          final PolynomialFunction cDotDot = cDot.polynomialDerivative();
71          final AbsoluteDate       t0      = new AbsoluteDate(2020, 4, 1, TimeScalesFactory.getUTC());
72          final List<ClockOffset> sample = new ArrayList<>();
73          for (double dt = 0; dt < 10; dt += FastMath.scalb(1, -4)) {
74              sample.add(new ClockOffset(t0.shiftedBy(dt), c.value(dt), cDot.value(dt), Double.NaN));
75          }
76          final SampledClockModel clockModel = new SampledClockModel(sample, 2);
77          for (double dt = 0.02; dt < 0.98; dt += 0.02) {
78              final ClockOffset co = clockModel.getOffset(t0.shiftedBy(dt));
79              Assertions.assertEquals(dt,                co.getDate().durationFrom(t0), 1.0e-15);
80              Assertions.assertEquals(c.value(dt),       co.getOffset(),                1.0e-15);
81              Assertions.assertEquals(cDot.value(dt),    co.getRate(),                  1.0e-15);
82              Assertions.assertEquals(cDotDot.value(dt), co.getAcceleration(),          1.0e-15);
83          }
84      }
85  
86      @Test
87      public void testCubicNoRateField() {
88          doTestCubicNoRate(Binary64Field.getInstance());
89      }
90  
91      public <T extends CalculusFieldElement<T>> void doTestCubicNoRate(final Field<T> field) {
92          final PolynomialFunction c       = new PolynomialFunction(FastMath.scalb(1.0, -6),
93                                                                    FastMath.scalb(1.0, -7),
94                                                                    FastMath.scalb(1.0, -8),
95                                                                    FastMath.scalb(1.0, -9));
96          final PolynomialFunction cDot    = c.polynomialDerivative();
97          final PolynomialFunction cDotDot = cDot.polynomialDerivative();
98          final AbsoluteDate       t0      = new AbsoluteDate(2020, 4, 1, TimeScalesFactory.getUTC());
99          final FieldAbsoluteDate<T> t0F = new FieldAbsoluteDate<>(field, t0);
100         final List<ClockOffset> sample = new ArrayList<>();
101         for (double dt = 0; dt < 10; dt += FastMath.scalb(1, -4)) {
102             sample.add(new ClockOffset(t0.shiftedBy(dt), c.value(dt), Double.NaN, Double.NaN));
103         }
104         final SampledClockModel clockModel = new SampledClockModel(sample, 4);
105         for (double dt = 0.02; dt < 0.98; dt += 0.02) {
106             final T dtF = field.getZero().newInstance(dt);
107             final FieldClockOffset<T> co = clockModel.getOffset(t0F.shiftedBy(dtF));
108             Assertions.assertEquals(dt, co.getDate().durationFrom(t0).getReal(),                  1.0e-15);
109             Assertions.assertEquals(c.value(dtF).getReal(),       co.getOffset().getReal(),       1.0e-15);
110             Assertions.assertEquals(cDot.value(dtF).getReal(),    co.getRate().getReal(),         1.0e-15);
111             Assertions.assertEquals(cDotDot.value(dtF).getReal(), co.getAcceleration().getReal(), 1.0e-15);
112         }
113     }
114 
115     @Test
116     public void testCubicNoAccelerationField() {
117         doTestCubicNoAcceleration(Binary64Field.getInstance());
118     }
119 
120     public <T extends CalculusFieldElement<T>> void doTestCubicNoAcceleration(final Field<T> field) {
121         final PolynomialFunction c       = new PolynomialFunction(FastMath.scalb(1.0, -6),
122                                                                   FastMath.scalb(1.0, -7),
123                                                                   FastMath.scalb(1.0, -8),
124                                                                   FastMath.scalb(1.0, -9));
125         final PolynomialFunction cDot    = c.polynomialDerivative();
126         final PolynomialFunction cDotDot = cDot.polynomialDerivative();
127         final AbsoluteDate       t0      = new AbsoluteDate(2020, 4, 1, TimeScalesFactory.getUTC());
128         final FieldAbsoluteDate<T> t0F = new FieldAbsoluteDate<>(field, t0);
129         final List<ClockOffset> sample = new ArrayList<>();
130         for (double dt = 0; dt < 10; dt += FastMath.scalb(1, -4)) {
131             sample.add(new ClockOffset(t0.shiftedBy(dt), c.value(dt), cDot.value(dt), Double.NaN));
132         }
133         final SampledClockModel clockModel = new SampledClockModel(sample, 2);
134         for (double dt = 0.02; dt < 0.98; dt += 0.02) {
135             final T dtF = field.getZero().newInstance(dt);
136             final FieldClockOffset<T> co = clockModel.getOffset(t0F.shiftedBy(dtF));
137             Assertions.assertEquals(dt, co.getDate().durationFrom(t0).getReal(),                  1.0e-15);
138             Assertions.assertEquals(c.value(dtF).getReal(),       co.getOffset().getReal(),       1.0e-15);
139             Assertions.assertEquals(cDot.value(dtF).getReal(),    co.getRate().getReal(),         1.0e-15);
140             Assertions.assertEquals(cDotDot.value(dtF).getReal(), co.getAcceleration().getReal(), 1.0e-15);
141         }
142     }
143 
144     @BeforeEach
145     public void setUp() throws Exception {
146         Utils.setDataRoot("regular-data");
147     }
148 
149 }