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.propagation.semianalytical.dsst.utilities.hansen;
18  
19  import org.hipparchus.util.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  
23  public class HansenThirdBodyLinearTest {
24  
25      private static double hansen(int n, int s, double chi) {
26          if (n == 0 && s == 0) {
27              return 1.0;
28          } else if (n == 0 && s == 1) {
29              return -1.0;
30          } else if (n == 1 && s == 0) {
31              return 1 + 0.5 * (1 - 1.0 / (chi * chi));
32          } else if (n >= 1 && n == s - 1) {
33              return (-(2 * (double)s - 1) / (double)s ) * hansen(s - 2, s - 1, chi);
34          } else if (n >= 1 && n == s) {
35              return ((2 * (double)s + 1) / ((double)s + 1) ) * hansen(s - 1, s, chi);
36          } else {
37              return ((2 * (double)n + 1) / ((double)n + 1)) * hansen(n - 1, s, chi) -
38                     ((((double)n + (double)s) * ((double)n - (double)s)) / ((double)n * ((double)n + 1) * chi * chi)) * hansen(n - 2, s, chi);
39          }
40      }
41  
42      @Test
43      public void testLinearVsRecursive00() {
44          doTestLinearVsRecursive(0.0, 1.1e-12);
45      }
46  
47      @Test
48      public void testLinearVsRecursive01() {
49          doTestLinearVsRecursive(0.1, 2.7e-13);
50      }
51  
52      @Test
53      public void testLinearVsRecursive02() {
54          doTestLinearVsRecursive(0.2, 9.5e-14);
55      }
56  
57      @Test
58      public void testLinearVsRecursive03() {
59          doTestLinearVsRecursive(0.3, 5.6e-14);
60      }
61  
62      @Test
63      public void testLinearVsRecursive04() {
64          doTestLinearVsRecursive(0.4, 1.5e-14);
65      }
66  
67      @Test
68      public void testLinearVsRecursive05() {
69          doTestLinearVsRecursive(0.5, 5.9e-15);
70      }
71  
72      @Test
73      public void testLinearVsRecursive06() {
74          doTestLinearVsRecursive(0.6, 3.7e-15);
75      }
76  
77      @Test
78      public void testLinearVsRecursive07() {
79          doTestLinearVsRecursive(0.7, 1.7e-15);
80      }
81  
82      @Test
83      public void testLinearVsRecursive08() {
84          doTestLinearVsRecursive(0.8, 9.5e-16);
85      }
86  
87      @Test
88      public void testLinearVsRecursive09() {
89          doTestLinearVsRecursive(0.9, 8.3e-16);
90      }
91  
92      private void doTestLinearVsRecursive(final double ecc, final double tol) {
93          final int N = 22;
94          final double chi = 1.0 / FastMath.sqrt(1 - ecc * ecc);
95          final HansenThirdBodyLinear[] htbl = new HansenThirdBodyLinear[N + 1];
96  
97          for (int s = 0; s <= N; s++) {
98              htbl[s] = new HansenThirdBodyLinear(N, s);
99              htbl[s].computeInitValues(1 / chi, 1 / (chi * chi), 1 / (chi * chi * chi));
100         }
101 
102         double maxRelativeError = 0;
103         for (int s = 0; s <= N; s++) {
104             for (int n = FastMath.max(2, s); n <= N; n++) {
105                 final double hansenRec = hansen(n, s, chi);
106                 final double hansenLin = htbl[s].getValue(n, 1 / chi);
107                 final double relativeError = FastMath.abs((hansenLin - hansenRec) / hansenRec);
108                 maxRelativeError = FastMath.max(maxRelativeError, relativeError);
109             }
110         }
111 
112         Assertions.assertEquals(0.0, maxRelativeError, tol);
113 
114     }
115 
116 
117 }