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.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.Utils;
23  import org.orekit.errors.OrekitException;
24  import org.orekit.forces.gravity.potential.NormalizedSphericalHarmonicsProvider.NormalizedSphericalHarmonics;
25  import org.orekit.forces.gravity.potential.UnnormalizedSphericalHarmonicsProvider.UnnormalizedSphericalHarmonics;
26  import org.orekit.time.AbsoluteDate;
27  
28  public class SHAFormatReaderTest {
29  
30      @Test
31      void testReadNormalized() {
32          Utils.setDataRoot("potential");
33          GravityFieldFactory.addPotentialCoefficientsReader(new SHAFormatReader("sha.grgm1200b_sigma_truncated_5x5", true));
34          NormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getNormalizedProvider(5, 5);
35          NormalizedSphericalHarmonics harmonics = provider.onDate(AbsoluteDate.FUTURE_INFINITY);
36          Assertions.assertEquals(TideSystem.UNKNOWN, provider.getTideSystem());
37          Assertions.assertEquals(-3.1973502105869101E-06, harmonics.getNormalizedCnm(3, 0), 1.0e-15);
38          Assertions.assertEquals( 3.1105527966439498E-06, harmonics.getNormalizedCnm(5, 5), 1.0e-15);
39          Assertions.assertEquals( 0.0, harmonics.getNormalizedSnm(4, 0), 1.0e-15);
40          Assertions.assertEquals( 3.9263792903879803E-06, harmonics.getNormalizedSnm(4, 4), 1.0e-15);
41          Assertions.assertEquals(2.7542657233402899E-06, harmonics.getNormalizedCnm(5, 4), 1.0e-15);
42      }
43  
44      @Test
45      void testReadUnnormalized() {
46          Utils.setDataRoot("potential");
47          GravityFieldFactory.addPotentialCoefficientsReader(new SHAFormatReader("sha.grgm1200b_sigma_truncated_5x5", true));
48          UnnormalizedSphericalHarmonicsProvider provider = GravityFieldFactory.getUnnormalizedProvider(5, 5);
49          UnnormalizedSphericalHarmonics harmonics = provider.onDate(AbsoluteDate.FUTURE_INFINITY);
50          Assertions.assertEquals(TideSystem.UNKNOWN, provider.getTideSystem());
51          int maxUlps = 1;
52          checkValue(harmonics.getUnnormalizedCnm(3, 0), 3, 0, -3.1973502105869101E-06, maxUlps);
53          checkValue(harmonics.getUnnormalizedCnm(5, 5), 5, 5, 3.1105527966439498E-06, maxUlps);
54          checkValue(harmonics.getUnnormalizedSnm(4, 0), 4, 0, 0.0,                maxUlps);
55          checkValue(harmonics.getUnnormalizedSnm(4, 4), 4, 4, 3.9263792903879803E-06, maxUlps);
56  
57          double a = (2.7542657233402899E-06);
58          double b = 9*8*7*6*5*4*3*2;
59          double c = 2*11/b;
60          double result = a*FastMath.sqrt(c);
61  
62          Assertions.assertEquals(result, harmonics.getUnnormalizedCnm(5, 4), 1.0e-20);
63  
64          a = -6.0069538669876603E-06;
65          b = 8*7*6*5*4*3*2;
66          c=2*9/b;
67          result = a*FastMath.sqrt(c);
68          Assertions.assertEquals(result, harmonics.getUnnormalizedCnm(4, 4), 1.0e-20);
69  
70          Assertions.assertEquals(2.0321922328195912e-4, -harmonics.getUnnormalizedCnm(2, 0), 1.0e-20);
71      }
72  
73      @Test
74      void testCorruptedFile1() {
75          Assertions.assertThrows(OrekitException.class, () -> {
76              Utils.setDataRoot("potential");
77              GravityFieldFactory.addPotentialCoefficientsReader(new SHAFormatReader("corrupted-1_sha.grgm1200b_sigma_truncated_5x5", false));
78              GravityFieldFactory.getUnnormalizedProvider(5, 5);
79          });
80      }
81  
82      @Test
83      void testCorruptedFile2() {
84          Assertions.assertThrows(OrekitException.class, () -> {
85              Utils.setDataRoot("potential");
86              GravityFieldFactory.addPotentialCoefficientsReader(new SHAFormatReader("corrupted-2_sha.grgm1200b_sigma_truncated_5x5", false));
87              GravityFieldFactory.getUnnormalizedProvider(5, 5);
88          });
89      }
90  
91      @Test
92      void testCorruptedFile3() {
93          Assertions.assertThrows(OrekitException.class, () -> {
94              Utils.setDataRoot("potential");
95              GravityFieldFactory.addPotentialCoefficientsReader(new SHAFormatReader("corrupted-3_sha.grgm1200b_sigma_truncated_5x5", false));
96              GravityFieldFactory.getUnnormalizedProvider(5, 5);
97          });
98      }
99  
100     @Test
101     void testCannotParseHeader() {
102         Assertions.assertThrows(OrekitException.class, () -> {
103             Utils.setDataRoot("potential");
104             GravityFieldFactory.addPotentialCoefficientsReader(new SHAFormatReader("corrupted-4_sha.grgm1200b_sigma_truncated_5x5", false));
105             GravityFieldFactory.getUnnormalizedProvider(2, 2);
106         });
107     }
108 
109     private void checkValue(final double value, final int n, final int m,
110                             final double constant, final int maxUlps)
111         {
112         double factor = GravityFieldFactory.getUnnormalizationFactors(n, m)[n][m];
113         double normalized = factor * constant;
114         double epsilon = maxUlps * FastMath.ulp(normalized);
115         Assertions.assertEquals(normalized, value, epsilon);
116     }
117 }