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.models.earth;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  import org.orekit.forces.gravity.potential.GravityFieldFactory;
22  import org.orekit.frames.Frame;
23  import org.orekit.frames.FramesFactory;
24  import org.orekit.utils.Constants;
25  
26  import static org.hamcrest.CoreMatchers.is;
27  import static org.hamcrest.MatcherAssert.assertThat;
28  import static org.hipparchus.util.FastMath.PI;
29  import static org.hipparchus.util.FastMath.abs;
30  import static org.orekit.OrekitMatchers.relativelyCloseTo;
31  
32  /**
33   * Unit tests for {@link ReferenceEllipsoid}.
34   *
35   * @author E. Ward
36   * @author G. Prat
37   */
38  public class ReferenceEllipsoidTest {
39  
40  
41      /** Test the constructor and the simple getters. */
42      @Test
43      public void testReferenceEllipsoid() {
44          double a = 1, f = 0.5, gm = 2, omega = 3;
45          ReferenceEllipsoid ellipsoid = new ReferenceEllipsoid(a, f,
46                  FramesFactory.getGCRF(), gm, omega);
47          assertThat(ellipsoid.getEquatorialRadius(), is(a));
48          assertThat(ellipsoid.getFlattening(), is(f));
49          assertThat(ellipsoid.getGM(), is(gm));
50          assertThat(ellipsoid.getSpin(), is(omega));
51      }
52  
53      /**
54       * Gets the WGS84 ellipsoid.
55       *
56       * @return the WGS84 ellipsoid.
57       * @see "Department of Defense World Geodetic System 1984. 2000. NIMA TR
58       * 8350.2 Third Edition, Amendment 1."
59       */
60      private ReferenceEllipsoid getComponent() {
61          /*
62           * use values for WGS84 ellipsoid. From:
63           * http://earth-info.nga.mil/GandG/wgs84/gravitymod/egm96/egm96.html
64           */
65          double a = 6378137.00, f = 1 / 298.257223563, GM = 3.986004418e14, spin = 7292115e-11;
66          return new ReferenceEllipsoid(a, f, FramesFactory.getGCRF(), GM, spin);
67      }
68  
69      /**
70       * Check the computation of normal gravity at several latitudes, allow 1
71       * ulps of error.
72       */
73      @Test
74      public void testGetNormalGravity() {
75          ReferenceEllipsoid ellipsoid = getComponent();
76  
77          // latitudes to evaluate at, in degrees
78          double[] lat = {-90, -45, 0, 45, 90};
79          // expected value of normal gravity at each latitude in lat
80          double[] expected = {9.833276738917813685281,
81                  9.8064684244573317502963, 9.779777598918278489352,
82                  9.8064684244573317502963, 9.833276738917813685281};
83  
84          // run tests
85          Assertions.assertEquals(lat.length, expected.length);
86          for (int i = 0; i < expected.length; i++) {
87              assertThat(ellipsoid.getNormalGravity(lat[i] * PI / 180.),
88                      relativelyCloseTo(expected[i], 1));
89          }
90      }
91  
92      /** Check the J<sub>2</sub> term is correct. */
93      @Test
94      public void testGetC2n0() {
95          ReferenceEllipsoid ellipsoid = getComponent();
96          /*
97           * C2,0 from: Department of Defense World Geodetic System 1984. 2000.
98           * NIMA TR 8350.2 Third Edition, Amendment 1.
99           */
100         double expected = -0.484166774985e-3;
101 
102         // value good to ~ 1e-9
103         Assertions.assertEquals(
104                 ellipsoid.getC2n0(1), expected, 1.31e-9,"J2 term\n");
105 
106         /*
107          * Values from '84, See chapter 3 of DMA TR 8350.2 Table 3.8
108          */
109         double[] expecteds = {7.90304054e-7, -1.687251e-9, 3.461e-12};
110         for (int i = 0; i < expecteds.length; i++) {
111             double C2n = ellipsoid.getC2n0(2 + i);
112             expected = expecteds[i];
113             // expect 4 correct digits
114             Assertions.assertEquals(C2n, expected, abs(2e-4 * expected), "C" + (4 + 2 * i) + ",0" + "\n");
115         }
116     }
117 
118     /** check throws when n=0 */
119     @Test
120     public void testGetC2n0Bad() {
121         Assertions.assertThrows(IllegalArgumentException.class, () -> {
122             getComponent().getC2n0(0);
123         });
124     }
125 
126     /** check {@link ReferenceEllipsoid#getPolarRadius()} */
127     @Test
128     public void testGetPolarRadius() {
129         assertThat(getComponent().getPolarRadius(), is(6356752.314245179));
130     }
131 
132     /**
133      * check {@link ReferenceEllipsoid#getWgs84(Frame)}
134      */
135     @Test
136     public void testGetWgs84() {
137         // setup
138         double c20factor = GravityFieldFactory.getUnnormalizationFactors(2, 0)[2][0];
139         Frame frame = FramesFactory.getGCRF();
140 
141         // action
142         ReferenceEllipsoid wgs84 = ReferenceEllipsoid.getWgs84(frame);
143 
144         // verify
145         Assertions.assertEquals(
146                 wgs84.getC2n0(1), Constants.WGS84_EARTH_C20 / c20factor, 3e-9);
147         assertThat(wgs84.getBodyFrame(), is(frame));
148     }
149 
150     /**
151      * check {@link ReferenceEllipsoid#getGrs80(Frame)}
152      */
153     @Test
154     public void testGetGrs80() {
155         // setup
156         double c20factor = GravityFieldFactory.getUnnormalizationFactors(2, 0)[2][0];
157         Frame frame = FramesFactory.getGCRF();
158 
159         // action
160         ReferenceEllipsoid grs80 = ReferenceEllipsoid.getGrs80(frame);
161 
162         // verify
163         Assertions.assertEquals(
164                 grs80.getC2n0(1), Constants.GRS80_EARTH_C20 / c20factor, 3e-9);
165         assertThat(grs80.getBodyFrame(), is(frame));
166     }
167 
168     /**
169      * check {@link ReferenceEllipsoid#getIers96(Frame)}
170      */
171     @Test
172     public void testGetIers96() {
173         // setup
174         double c20factor = GravityFieldFactory.getUnnormalizationFactors(2, 0)[2][0];
175         Frame frame = FramesFactory.getGCRF();
176 
177         // action
178         ReferenceEllipsoid iers96 = ReferenceEllipsoid.getIers96(frame);
179 
180         // verify
181         Assertions.assertEquals(
182                 iers96.getC2n0(1), Constants.IERS96_EARTH_C20 / c20factor, 3e-9);
183         assertThat(iers96.getBodyFrame(), is(frame));
184     }
185 
186     /**
187      * check {@link ReferenceEllipsoid#getIers2003(Frame)}
188      */
189     @Test
190     public void testGetIers2003() {
191         // setup
192         double c20factor = GravityFieldFactory.getUnnormalizationFactors(2, 0)[2][0];
193         Frame frame = FramesFactory.getGCRF();
194 
195         // action
196         ReferenceEllipsoid iers2003 = ReferenceEllipsoid.getIers2003(frame);
197 
198         // verify
199         Assertions.assertEquals(
200                 iers2003.getC2n0(1), Constants.IERS2003_EARTH_C20 / c20factor, 3e-9);
201         assertThat(iers2003.getBodyFrame(), is(frame));
202     }
203 
204     /**
205      * check {@link ReferenceEllipsoid#getIers2010(Frame)}
206      */
207     @Test
208     public void testGetIers2010() {
209         // setup
210         double c20factor = GravityFieldFactory.getUnnormalizationFactors(2, 0)[2][0];
211         Frame frame = FramesFactory.getGCRF();
212 
213         // action
214         ReferenceEllipsoid iers2010 = ReferenceEllipsoid.getIers2010(frame);
215 
216         // verify
217         Assertions.assertEquals(
218                 iers2010.getC2n0(1), Constants.IERS2010_EARTH_C20 / c20factor, 3e-9);
219         assertThat(iers2010.getBodyFrame(), is(frame));
220     }
221 
222     /** check {@link ReferenceEllipsoid#getEllipsoid()} */
223     @Test
224     public void testGetEllipsoid() {
225         //setup
226         ReferenceEllipsoid ellipsoid = getComponent();
227 
228         //action + verify
229         assertThat(ellipsoid.getEllipsoid(), is(ellipsoid));
230     }
231 }