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.orbits;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.params.ParameterizedTest;
22  import org.junit.jupiter.params.provider.EnumSource;
23  
24  public class KeplerianAnomalyUtilityTest {
25  
26      @ParameterizedTest
27      @EnumSource(PositionAngleType.class)
28      void testConvertAlphaElliptic(final PositionAngleType inputType) {
29          // GIVEN
30          final double expectedAnomaly = 3.;
31          final double eccentricity = 0.1;
32          final PositionAngleType intermediateType = PositionAngleType.ECCENTRIC;
33          // WHEN
34          final double intermediateAnomaly = KeplerianAnomalyUtility.convertAnomaly(inputType,
35                  expectedAnomaly, eccentricity, intermediateType);
36          final double actualAnomaly = KeplerianAnomalyUtility.convertAnomaly(intermediateType,
37                  intermediateAnomaly, eccentricity, inputType);
38          // THEN
39          Assertions.assertEquals(expectedAnomaly, actualAnomaly, 1e-15);
40      }
41  
42      @ParameterizedTest
43      @EnumSource(PositionAngleType.class)
44      void testConvertAlphaHyperbolic(final PositionAngleType inputType) {
45          // GIVEN
46          final double expectedAnomaly = 3.;
47          final double eccentricity = 2;
48          final PositionAngleType intermediateType = PositionAngleType.TRUE;
49          // WHEN
50          final double intermediateAnomaly = KeplerianAnomalyUtility.convertAnomaly(inputType,
51                  expectedAnomaly, eccentricity, intermediateType);
52          final double actualAnomaly = KeplerianAnomalyUtility.convertAnomaly(intermediateType,
53                  intermediateAnomaly, eccentricity, inputType);
54          // THEN
55          Assertions.assertEquals(expectedAnomaly, actualAnomaly, 1e-10);
56      }
57  
58      @Test
59      public void testEllipticMeanToTrue() {
60          final double e = 0.231;
61          final double M = 2.045;
62          final double v = KeplerianAnomalyUtility.ellipticMeanToTrue(e, M);
63          Assertions.assertEquals(2.4004986679372027, v, 1e-14);
64      }
65  
66      @Test
67      public void testEllipticTrueToMean() {
68          final double e = 0.487;
69          final double v = 1.386;
70          final double M = KeplerianAnomalyUtility.ellipticTrueToMean(e, v);
71          Assertions.assertEquals(0.5238159114936672, M, 1e-14);
72      }
73  
74      @Test
75      public void testEllipticEccentricToTrue() {
76          final double e = 0.687;
77          final double E = 4.639;
78          final double v = KeplerianAnomalyUtility.ellipticEccentricToTrue(e, E);
79          Assertions.assertEquals(3.903008140176819, v, 1e-14);
80      }
81  
82      @Test
83      public void testEllipticTrueToEccentric() {
84          final double e = 0.527;
85          final double v = 0.768;
86          final double E = KeplerianAnomalyUtility.ellipticTrueToEccentric(e, v);
87          Assertions.assertEquals(0.44240462411915754, E, 1e-14);
88      }
89  
90      @Test
91      public void testEllipticMeanToEccentric() {
92          final double e1 = 0.726;
93          final double M1 = 0.;
94          final double E1 = KeplerianAnomalyUtility.ellipticMeanToEccentric(e1, M1);
95          Assertions.assertEquals(0.0, E1, 1e-14);
96  
97          final double e2 = 0.065;
98          final double M2 = 4.586;
99          final double E2 = KeplerianAnomalyUtility.ellipticMeanToEccentric(e2, M2);
100         Assertions.assertEquals(4.522172385101093, E2, 1e-14);
101 
102         final double e3 = 0.403;
103         final double M3 = 0.121;
104         final double E3 = KeplerianAnomalyUtility.ellipticMeanToEccentric(e3, M3);
105         Assertions.assertEquals(0.20175794699115656, E3, 1e-14);
106 
107         final double e4 = 0.999;
108         final double M4 = 0.028;
109         final double E4 = KeplerianAnomalyUtility.ellipticMeanToEccentric(e4, M4);
110         Assertions.assertEquals(0.5511071508829587, E4, 1e-14);
111     }
112 
113     @Test
114     public void testEllipticEccentricToMean() {
115         final double e = 0.192;
116         final double E = 2.052;
117         final double M = KeplerianAnomalyUtility.ellipticEccentricToMean(e, E);
118         Assertions.assertEquals(1.881803817764882, M, 1e-14);
119     }
120 
121     @Test
122     public void testHyperbolicMeanToTrue() {
123         final double e = 1.027;
124         final double M = 1.293;
125         final double v = KeplerianAnomalyUtility.hyperbolicMeanToTrue(e, M);
126         Assertions.assertEquals(2.8254185280004855, v, 1e-14);
127     }
128 
129     @Test
130     public void testHyperbolicTrueToMean() {
131         final double e = 1.161;
132         final double v = -2.469;
133         final double M = KeplerianAnomalyUtility.hyperbolicTrueToMean(e, v);
134         Assertions.assertEquals(-2.5499244818919915, M, 1e-14);
135     }
136 
137     @Test
138     public void testHyperbolicEccentricToTrue() {
139         final double e = 2.161;
140         final double E = -1.204;
141         final double v = KeplerianAnomalyUtility.hyperbolicEccentricToTrue(e, E);
142         Assertions.assertEquals(-1.4528528149658333, v, 1e-14);
143     }
144 
145     @Test
146     public void testHyperbolicTrueToEccentric() {
147         final double e = 1.595;
148         final double v = 0.298;
149         final double E = KeplerianAnomalyUtility.hyperbolicTrueToEccentric(e, v);
150         Assertions.assertEquals(0.1440079208139455, E, 1e-14);
151     }
152 
153     @Test
154     public void testHyperbolicMeanToEccentric() {
155         final double e1 = 1.201;
156         final double M1 = 0.0;
157         final double E1 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e1, M1);
158         Assertions.assertEquals(0.0, E1, 1e-14);
159 
160         final double e2 = 1.127;
161         final double M2 = -3.624;
162         final double E2 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e2, M2);
163         Assertions.assertEquals(-2.3736718687722265, E2, 1e-14);
164 
165         final double e3 = 1.338;
166         final double M3 = -0.290;
167         final double E3 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e3, M3);
168         Assertions.assertEquals(-0.6621795141831807, E3, 1e-14);
169 
170         final double e4 = 1.044;
171         final double M4 = 3.996;
172         final double E4 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e4, M4);
173         Assertions.assertEquals(2.532614977388778, E4, 1e-14);
174 
175         final double e5 = 2.052;
176         final double M5 = 4.329;
177         final double E5 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e5, M5);
178         Assertions.assertEquals(1.816886788278918, E5, 1e-14);
179 
180         final double e6 = 2.963;
181         final double M6 = -1.642;
182         final double E6 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e6, M6);
183         Assertions.assertEquals(-0.7341946491456494, E6, 1e-14);
184 
185         final double e7 = 4.117;
186         final double M7 = -0.286;
187         final double E7 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e7, M7);
188         Assertions.assertEquals(-0.09158570899196887, E7, 1e-14);
189 
190         // Issue 951.
191         final double e8 = 1.251844925917281;
192         final double M8 = 54.70111712786907;
193         final double E8 = KeplerianAnomalyUtility.hyperbolicMeanToEccentric(e8, M8);
194         Assertions.assertEquals(4.550432282228856, E8, 1e-14);
195     }
196 
197     @Test
198     public void testHyperbolicEccentricToMean() {
199         final double e = 1.801;
200         final double E = 3.287;
201         final double M = KeplerianAnomalyUtility.hyperbolicEccentricToMean(e, E);
202         Assertions.assertEquals(20.77894350750361, M, 1e-14);
203     }
204 
205     @Test
206     public void testIssue544() {
207         // Initial parameters
208         // In order to test the issue, we voluntarily set the anomaly at Double.NaN.
209         double e = 0.7311;
210         double anomaly = Double.NaN;
211         // Computes the elliptic eccentric anomaly
212         double E = KeplerianAnomalyUtility.ellipticMeanToEccentric(e, anomaly);
213         // Verify that an infinite loop did not occur
214         Assertions.assertTrue(Double.isNaN(E));
215     }
216 
217 }