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