1   /* Copyright 2022-2025 Romain Serra
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;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.geometry.euclidean.threed.Vector3D;
21  import org.hipparchus.util.Binary64;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  import org.mockito.Mockito;
25  import org.orekit.orbits.CartesianOrbit;
26  import org.orekit.orbits.FieldCartesianOrbit;
27  import org.orekit.time.AbsoluteDate;
28  import org.orekit.utils.AbsolutePVCoordinates;
29  import org.orekit.utils.FieldAbsolutePVCoordinates;
30  import org.orekit.utils.PVCoordinates;
31  import org.orekit.utils.TimeStampedPVCoordinates;
32  
33  import java.util.Arrays;
34  
35  class CartesianToleranceProviderTest {
36  
37      @Test
38      void testOfdP() {
39          // GIVEN
40          final double dP = 1.;
41          final CartesianToleranceProvider toleranceProvider = CartesianToleranceProvider.of(dP);
42          // WHEN
43          final double[][] tolerances = toleranceProvider.getTolerances(Mockito.mock(Vector3D.class),
44                  Mockito.mock(Vector3D.class));
45          // THEN
46          Assertions.assertEquals(dP, tolerances[0][0]);
47          Assertions.assertEquals(dP, tolerances[0][1]);
48          Assertions.assertEquals(dP, tolerances[0][2]);
49          Assertions.assertEquals(CartesianToleranceProvider.DEFAULT_ABSOLUTE_MASS_TOLERANCE, tolerances[0][6]);
50          for (int i = 0; i < tolerances[0].length; ++i) {
51              Assertions.assertTrue(tolerances[1][i] > 0.);
52          }
53      }
54  
55      @Test
56      void testOfdPdVdM() {
57          // GIVEN
58          final double dP = 1.;
59          final double dV = 2.;
60          final double dM = 3.;
61          final CartesianToleranceProvider toleranceProvider = CartesianToleranceProvider.of(dP, dV, dM);
62          final Vector3D unitVector = Vector3D.MINUS_I;
63          // WHEN
64          final double[][] tolerances = toleranceProvider.getTolerances(unitVector, unitVector);
65          // THEN
66          Assertions.assertEquals(dP, tolerances[0][0]);
67          Assertions.assertEquals(dP, tolerances[0][1]);
68          Assertions.assertEquals(dP, tolerances[0][2]);
69          Assertions.assertEquals(dV, tolerances[0][3]);
70          Assertions.assertEquals(dV, tolerances[0][4]);
71          Assertions.assertEquals(dV, tolerances[0][5]);
72          Assertions.assertEquals(dM, tolerances[0][6]);
73          for (int i = 0; i < 6; ++i) {
74              Assertions.assertEquals(tolerances[1][i], tolerances[0][i]);
75          }
76          Assertions.assertTrue(tolerances[1][6] > 0.);
77      }
78  
79      @Test
80      void testGetTolerancesCartesianOrbit() {
81          // GIVEN
82          final double[] absoluteTolerances = new double[7];
83          Arrays.fill(absoluteTolerances, 1.);
84          final double[] relativeTolerances = new double[7];
85          Arrays.fill(relativeTolerances, 2.);
86          final CartesianToleranceProvider mockedProvider = new TestProvider(absoluteTolerances, relativeTolerances);
87          final CartesianOrbit mockedOrbit = mockOrbit();
88          // WHEN
89          final double[][] actualTolerances = mockedProvider.getTolerances(mockedOrbit);
90          // THEN
91          Assertions.assertArrayEquals(absoluteTolerances, actualTolerances[0]);
92          Assertions.assertArrayEquals(relativeTolerances, actualTolerances[1]);
93      }
94  
95      @Test
96      void testGetTolerancesFieldCartesianOrbit() {
97          // GIVEN
98          final double[] absoluteTolerances = new double[7];
99          Arrays.fill(absoluteTolerances, 1.);
100         final double[] relativeTolerances = new double[7];
101         Arrays.fill(relativeTolerances, 2.);
102         final CartesianToleranceProvider mockedProvider = new TestProvider(absoluteTolerances, relativeTolerances);
103         final FieldCartesianOrbit<Binary64> mockedOrbit = mockFieldOrbit();
104         // WHEN
105         final double[][] actualTolerances = mockedProvider.getTolerances(mockedOrbit);
106         // THEN
107         Assertions.assertArrayEquals(absoluteTolerances, actualTolerances[0]);
108         Assertions.assertArrayEquals(relativeTolerances, actualTolerances[1]);
109     }
110 
111     private static CartesianOrbit mockOrbit() {
112         final CartesianOrbit mockedOrbit = Mockito.mock(CartesianOrbit.class);
113         Mockito.when(mockedOrbit.getPosition()).thenReturn(Mockito.mock(Vector3D.class));
114         Mockito.when(mockedOrbit.getPVCoordinates()).thenReturn(new TimeStampedPVCoordinates(AbsoluteDate.ARBITRARY_EPOCH, new PVCoordinates()));
115         return mockedOrbit;
116     }
117 
118     @SuppressWarnings("unchecked")
119     private static <T extends CalculusFieldElement<T>> FieldCartesianOrbit<T> mockFieldOrbit() {
120         final FieldCartesianOrbit<T> mockedFieldOrbit = Mockito.mock(FieldCartesianOrbit.class);
121         final CartesianOrbit mockedOrbit = mockOrbit();
122         Mockito.when(mockedFieldOrbit.toOrbit()).thenReturn(mockedOrbit);
123         return mockedFieldOrbit;
124     }
125 
126     @Test
127     void testGetTolerancesAbsolutePV() {
128         // GIVEN
129         final double[] absoluteTolerances = new double[7];
130         Arrays.fill(absoluteTolerances, 1.);
131         final double[] relativeTolerances = new double[7];
132         Arrays.fill(relativeTolerances, 2.);
133         final CartesianToleranceProvider mockedProvider = new TestProvider(absoluteTolerances, relativeTolerances);
134         final AbsolutePVCoordinates mockedPV = mockPV();
135         // WHEN
136         final double[][] actualTolerances = mockedProvider.getTolerances(mockedPV);
137         // THEN
138         Assertions.assertArrayEquals(absoluteTolerances, actualTolerances[0]);
139         Assertions.assertArrayEquals(relativeTolerances, actualTolerances[1]);
140     }
141 
142     @Test
143     void testGetTolerancesFieldPV() {
144         // GIVEN
145         final double[] absoluteTolerances = new double[7];
146         Arrays.fill(absoluteTolerances, 1.);
147         final double[] relativeTolerances = new double[7];
148         Arrays.fill(relativeTolerances, 2.);
149         final CartesianToleranceProvider mockedProvider = new TestProvider(absoluteTolerances, relativeTolerances);
150         final FieldAbsolutePVCoordinates<Binary64> mockedFieldPV = mockFieldPV();
151         // WHEN
152         final double[][] actualTolerances = mockedProvider.getTolerances(mockedFieldPV);
153         // THEN
154         Assertions.assertArrayEquals(absoluteTolerances, actualTolerances[0]);
155         Assertions.assertArrayEquals(relativeTolerances, actualTolerances[1]);
156     }
157 
158     private static AbsolutePVCoordinates mockPV() {
159         final AbsolutePVCoordinates mockedPV = Mockito.mock(AbsolutePVCoordinates.class);
160         Mockito.when(mockedPV.getPosition()).thenReturn(Mockito.mock(Vector3D.class));
161         Mockito.when(mockedPV.getPVCoordinates()).thenReturn(new TimeStampedPVCoordinates(AbsoluteDate.ARBITRARY_EPOCH, new PVCoordinates()));
162         return mockedPV;
163     }
164 
165     @SuppressWarnings("unchecked")
166     private static <T extends CalculusFieldElement<T>> FieldAbsolutePVCoordinates<T> mockFieldPV() {
167         final FieldAbsolutePVCoordinates<T> mockedFieldPV = Mockito.mock(FieldAbsolutePVCoordinates.class);
168         final AbsolutePVCoordinates mockedPV = mockPV();
169         Mockito.when(mockedFieldPV.toAbsolutePVCoordinates()).thenReturn(mockedPV);
170         return mockedFieldPV;
171     }
172 
173     private static class TestProvider implements CartesianToleranceProvider {
174         private final double[] absoluteTolerances;
175         private final double[] relativeTolerances;
176         TestProvider(final double[] absoluteTolerances, final double[] relativeTolerances) {
177             this.absoluteTolerances = absoluteTolerances;
178             this.relativeTolerances = relativeTolerances;
179         }
180         @Override
181         public double[][] getTolerances(final Vector3D position, final Vector3D velocity) {
182             return new double[][] {
183                 absoluteTolerances, relativeTolerances
184             };
185         }
186     }
187 
188 }