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.junit.Assert;
20  import org.hipparchus.util.Precision;
21  import org.junit.Before;
22  import org.junit.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      @Before
38      public void setUp() {
39          raw = new QuadraticProvider(date);
40          cache = new CachedNormalizedSphericalHarmonicsProvider(raw, step, interpolationPoints, maxSlots, slotSpan, newSlotInterval);
41      }
42  
43      @Deprecated
44      @Test
45      public void testGetReferenceDate() {
46          AbsoluteDate actualDate = cache.getReferenceDate();
47          Assert.assertEquals(actualDate, date);
48      }
49  
50      @Test
51      public void testLimits() {
52          Assert.assertEquals(2, cache.getMaxDegree());
53          Assert.assertEquals(2, cache.getMaxOrder());
54      }
55  
56      @Test
57      public void testBody() {
58          Assert.assertEquals(1, cache.getMu(), 1.0e-15);
59          Assert.assertEquals(1, cache.getAe(), 1.0e-15);
60      }
61  
62      @Test
63      public void testGetTideSystem() {
64          TideSystem actualSystem = cache.getTideSystem();
65          Assert.assertEquals(actualSystem, TideSystem.UNKNOWN);
66      }
67  
68      @Deprecated
69      @Test
70      public void testGetOffset() {
71          final double epsilon = 1e-12;
72          AbsoluteDate offsetDate = AbsoluteDate.GALILEO_EPOCH;
73          double targetOffset = offsetDate.durationFrom(date);
74          double actualOffset = cache.getOffset(offsetDate);
75  
76          Assert.assertEquals(targetOffset, actualOffset, epsilon);
77      }
78  
79      @Test
80      public void testInterpolation() {
81          //setup
82          //generate points on grid with date as the origin
83          cache.onDate(date);
84          //sample at step/2
85          AbsoluteDate sampleDate = date.shiftedBy(step / 2.0);
86          //expected values
87          NormalizedSphericalHarmonics expected = raw.onDate(sampleDate);
88  
89          //action
90          NormalizedSphericalHarmonics actual = cache.onDate(sampleDate);
91  
92          //verify
93          double tol = Precision.EPSILON;
94          for (int n = 0; n < raw.getMaxDegree(); n++) {
95              for (int m = 0; m < n; m++) {
96                  Assert.assertEquals(expected.getNormalizedCnm(n, m), actual.getNormalizedCnm(n, m), tol);
97                  Assert.assertEquals(expected.getNormalizedSnm(n, m), actual.getNormalizedSnm(n, m), tol);
98              }
99          }
100     }
101 
102     @Test
103     public void testReverseEntryGeneration() {
104         //setup
105         //generate points on grid with date as the origin
106         cache.onDate(date);
107         //sample before the current cached values
108         AbsoluteDate sampleDate = date.shiftedBy(-step * 3);
109         NormalizedSphericalHarmonics expected = raw.onDate(sampleDate);
110 
111         //action
112         NormalizedSphericalHarmonics actual = cache.onDate(sampleDate);
113 
114         //verify
115         double tol = Precision.EPSILON;
116         for (int n = 0; n < raw.getMaxDegree(); n++) {
117             for (int m = 0; m < n; m++) {
118                 Assert.assertEquals(expected.getNormalizedCnm(n, m), actual.getNormalizedCnm(n, m), tol);
119                 Assert.assertEquals(expected.getNormalizedSnm(n, m), actual.getNormalizedSnm(n, m), tol);
120             }
121         }
122     }
123 
124     private static class QuadraticProvider implements NormalizedSphericalHarmonicsProvider {
125 
126         private final AbsoluteDate date;
127 
128         private QuadraticProvider(AbsoluteDate date) {
129             this.date = date;
130         }
131 
132         @Override
133         public NormalizedSphericalHarmonics onDate(final AbsoluteDate date) {
134             final double t = date.durationFrom(this.date);
135             return new NormalizedSphericalHarmonics() {
136                 @Override
137                 public double getNormalizedCnm(int n, int m) {
138                     return n + m + t * t;
139                 }
140 
141                 @Override
142                 public double getNormalizedSnm(int n, int m) {
143                     return n + m + t * t + 1;
144                 }
145 
146                 @Override
147                 public AbsoluteDate getDate() {
148                     return date;
149                 }
150             };
151         }
152 
153         @Override
154         public int getMaxDegree() {
155             return 2;
156         }
157 
158         @Override
159         public int getMaxOrder() {
160             return 2;
161         }
162 
163         @Override
164         public double getMu() {
165             return 1;
166         }
167 
168         @Override
169         public double getAe() {
170             return 1;
171         }
172 
173         @Override
174         public AbsoluteDate getReferenceDate() {
175             return date;
176         }
177 
178         @Override
179         public double getOffset(AbsoluteDate date) {
180             return date.durationFrom(this.date);
181         }
182 
183         @Override
184         public TideSystem getTideSystem() {
185             return TideSystem.UNKNOWN;
186         }
187 
188     }
189 }