1   /* Contributed in the public domain.
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.forces.gravity.potential;
18  
19  import org.hipparchus.util.Precision;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.orekit.forces.gravity.potential.NormalizedSphericalHarmonicsProvider.NormalizedSphericalHarmonics;
24  import org.orekit.time.AbsoluteDate;
25  import org.orekit.utils.Constants;
26  
27  /** @author Evan Ward */
28  public class CachedNormalizedSphericalHarmonicsProviderTest {
29  
30  
31      private static final AbsoluteDate date = AbsoluteDate.J2000_EPOCH;
32      private NormalizedSphericalHarmonicsProvider raw;
33      private static final double step = 60 * 60, slotSpan = Constants.JULIAN_DAY, newSlotInterval = Constants.JULIAN_DAY;
34      private static final int interpolationPoints = 3, maxSlots = 100;
35      private CachedNormalizedSphericalHarmonicsProvider cache;
36  
37      @BeforeEach
38      public void setUp() {
39          raw = new QuadraticProvider(date);
40          cache = new CachedNormalizedSphericalHarmonicsProvider(raw, step, interpolationPoints, maxSlots, slotSpan, newSlotInterval);
41      }
42  
43      @Test
44      void testGetReferenceDate() {
45          AbsoluteDate actualDate = cache.getReferenceDate();
46          Assertions.assertEquals(actualDate, date);
47      }
48  
49      @Test
50      void testLimits() {
51          Assertions.assertEquals(2, cache.getMaxDegree());
52          Assertions.assertEquals(2, cache.getMaxOrder());
53      }
54  
55      @Test
56      void testBody() {
57          Assertions.assertEquals(1, cache.getMu(), 1.0e-15);
58          Assertions.assertEquals(1, cache.getAe(), 1.0e-15);
59      }
60  
61      @Test
62      void testGetTideSystem() {
63          TideSystem actualSystem = cache.getTideSystem();
64          Assertions.assertEquals(actualSystem, TideSystem.UNKNOWN);
65      }
66  
67      @Test
68      void testInterpolation() {
69          //setup
70          //generate points on grid with date as the origin
71          cache.onDate(date);
72          //sample at step/2
73          AbsoluteDate sampleDate = date.shiftedBy(step / 2.0);
74          //expected values
75          NormalizedSphericalHarmonics expected = raw.onDate(sampleDate);
76  
77          //action
78          NormalizedSphericalHarmonics actual = cache.onDate(sampleDate);
79  
80          //verify
81          double tol = Precision.EPSILON;
82          for (int n = 0; n < raw.getMaxDegree(); n++) {
83              for (int m = 0; m < n; m++) {
84                  Assertions.assertEquals(expected.getNormalizedCnm(n, m), actual.getNormalizedCnm(n, m), tol);
85                  Assertions.assertEquals(expected.getNormalizedSnm(n, m), actual.getNormalizedSnm(n, m), tol);
86              }
87          }
88      }
89  
90      @Test
91      void testReverseEntryGeneration() {
92          //setup
93          //generate points on grid with date as the origin
94          cache.onDate(date);
95          //sample before the current cached values
96          AbsoluteDate sampleDate = date.shiftedBy(-step * 3);
97          NormalizedSphericalHarmonics expected = raw.onDate(sampleDate);
98  
99          //action
100         NormalizedSphericalHarmonics actual = cache.onDate(sampleDate);
101 
102         //verify
103         double tol = Precision.EPSILON;
104         for (int n = 0; n < raw.getMaxDegree(); n++) {
105             for (int m = 0; m < n; m++) {
106                 Assertions.assertEquals(expected.getNormalizedCnm(n, m), actual.getNormalizedCnm(n, m), tol);
107                 Assertions.assertEquals(expected.getNormalizedSnm(n, m), actual.getNormalizedSnm(n, m), tol);
108             }
109         }
110     }
111 
112     private static class QuadraticProvider implements NormalizedSphericalHarmonicsProvider {
113 
114         private final AbsoluteDate date;
115 
116         private QuadraticProvider(AbsoluteDate date) {
117             this.date = date;
118         }
119 
120         @Override
121         public NormalizedSphericalHarmonics onDate(final AbsoluteDate date) {
122             final double t = date.durationFrom(this.date);
123             return new NormalizedSphericalHarmonics() {
124                 @Override
125                 public double getNormalizedCnm(int n, int m) {
126                     return n + m + t * t;
127                 }
128 
129                 @Override
130                 public double getNormalizedSnm(int n, int m) {
131                     return n + m + t * t + 1;
132                 }
133 
134                 @Override
135                 public AbsoluteDate getDate() {
136                     return date;
137                 }
138             };
139         }
140 
141         @Override
142         public int getMaxDegree() {
143             return 2;
144         }
145 
146         @Override
147         public int getMaxOrder() {
148             return 2;
149         }
150 
151         @Override
152         public double getMu() {
153             return 1;
154         }
155 
156         @Override
157         public double getAe() {
158             return 1;
159         }
160 
161         @Override
162         public AbsoluteDate getReferenceDate() {
163             return date;
164         }
165 
166        @Override
167         public TideSystem getTideSystem() {
168             return TideSystem.UNKNOWN;
169         }
170 
171     }
172 }