1   /* Copyright 2002-2022 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.conversion;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.hipparchus.geometry.euclidean.threed.Vector3D;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.orekit.Utils;
27  import org.orekit.errors.OrekitException;
28  import org.orekit.frames.FramesFactory;
29  import org.orekit.orbits.EquinoctialOrbit;
30  import org.orekit.orbits.Orbit;
31  import org.orekit.orbits.OrbitType;
32  import org.orekit.orbits.PositionAngle;
33  import org.orekit.propagation.Propagator;
34  import org.orekit.propagation.SpacecraftState;
35  import org.orekit.propagation.analytical.KeplerianPropagator;
36  import org.orekit.time.AbsoluteDate;
37  import org.orekit.utils.PVCoordinates;
38  
39  public class KeplerianConverterTest {
40  
41      private Orbit orbit;
42  
43      private final static Vector3D position = new Vector3D(7.0e6, 1.0e6, 4.0e6);
44      private final static Vector3D velocity = new Vector3D(-500.0, 8000.0, 1000.0);
45      private final static double mu = 3.9860047e14;
46  
47      @Test
48      public void testConversionPositionVelocity() {
49          checkFit(orbit, 86400, 300, 1.0e-3, false, 1.901e-8);
50      }
51  
52      @Test
53      public void testConversionPositionOnly() {
54          checkFit(orbit, 86400, 300, 1.0e-3, true, 2.691e-8);
55      }
56  
57      @Test(expected = OrekitException.class)
58      public void testConversionWithFreeParameter() {
59          checkFit(orbit, 86400, 300, 1.0e-3, true, 2.65e-8, "toto");
60      }
61  
62      protected void checkFit(final Orbit orbit,
63                              final double duration,
64                              final double stepSize,
65                              final double threshold,
66                              final boolean positionOnly,
67                              final double expectedRMS,
68                              final String... freeParameters)
69          {
70  
71          Propagator p = new KeplerianPropagator(orbit);
72          List<SpacecraftState> sample = new ArrayList<SpacecraftState>();
73          for (double dt = 0; dt < duration; dt += stepSize) {
74              sample.add(p.propagate(orbit.getDate().shiftedBy(dt)));
75          }
76  
77          PropagatorBuilder builder = new KeplerianPropagatorBuilder(OrbitType.KEPLERIAN.convertType(orbit),
78                                                                     PositionAngle.MEAN,
79                                                                     1.0);
80  
81          FiniteDifferencePropagatorConverter fitter = new FiniteDifferencePropagatorConverter(builder, threshold, 1000);
82  
83          fitter.convert(sample, positionOnly, freeParameters);
84  
85          Assert.assertEquals(expectedRMS, fitter.getRMS(), 0.01 * expectedRMS);
86  
87          KeplerianPropagator prop = (KeplerianPropagator)fitter.getAdaptedPropagator();
88          Orbit fitted = prop.getInitialState().getOrbit();
89  
90          final double eps = 1.0e-12;
91          Assert.assertEquals(orbit.getPVCoordinates().getPosition().getX(),
92                              fitted.getPVCoordinates().getPosition().getX(),
93                              eps * orbit.getPVCoordinates().getPosition().getX());
94          Assert.assertEquals(orbit.getPVCoordinates().getPosition().getY(),
95                              fitted.getPVCoordinates().getPosition().getY(),
96                              eps * orbit.getPVCoordinates().getPosition().getY());
97          Assert.assertEquals(orbit.getPVCoordinates().getPosition().getZ(),
98                              fitted.getPVCoordinates().getPosition().getZ(),
99                              eps * orbit.getPVCoordinates().getPosition().getZ());
100 
101         Assert.assertEquals(orbit.getPVCoordinates().getVelocity().getX(),
102                             fitted.getPVCoordinates().getVelocity().getX(),
103                             -eps * orbit.getPVCoordinates().getVelocity().getX());
104         Assert.assertEquals(orbit.getPVCoordinates().getVelocity().getY(),
105                             fitted.getPVCoordinates().getVelocity().getY(),
106                             eps * orbit.getPVCoordinates().getVelocity().getY());
107         Assert.assertEquals(orbit.getPVCoordinates().getVelocity().getZ(),
108                             fitted.getPVCoordinates().getVelocity().getZ(),
109                             eps * orbit.getPVCoordinates().getVelocity().getZ());
110 
111     }
112 
113     @Before
114     public void setUp() {
115         Utils.setDataRoot("regular-data");
116 
117         AbsoluteDate initDate = AbsoluteDate.J2000_EPOCH.shiftedBy(584.);
118         orbit = new EquinoctialOrbit(new PVCoordinates(position, velocity),
119                                      FramesFactory.getEME2000(), initDate, mu);
120     }
121 
122 }
123