1   /* Copyright 2002-2025 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.Test;
22  import org.orekit.estimation.Context;
23  import org.orekit.estimation.EstimationTestUtils;
24  import org.orekit.estimation.leastsquares.AbstractBatchLSModel;
25  import org.orekit.estimation.leastsquares.ModelObserver;
26  import org.orekit.estimation.measurements.ObservedMeasurement;
27  import org.orekit.frames.FramesFactory;
28  import org.orekit.orbits.CartesianOrbit;
29  import org.orekit.orbits.Orbit;
30  import org.orekit.orbits.PositionAngleType;
31  import org.orekit.propagation.analytical.KeplerianPropagator;
32  import org.orekit.time.AbsoluteDate;
33  import org.orekit.utils.ParameterDriversList;
34  import org.orekit.utils.ParameterDriversList.DelegatingDriver;
35  import org.orekit.utils.TimeStampedPVCoordinates;
36  
37  import java.util.List;
38  
39  import static org.orekit.Utils.assertParametersDriversValues;
40  
41  public class AbstractPropagatorBuilderTest {
42  
43      /** Test method resetOrbit. */
44      @Test
45      public void testResetOrbit() {
46          // Load a context
47          Context context = EstimationTestUtils.eccentricContext("regular-data:potential:tides");
48  
49          // Use a Cartesian orbit so the parameters are changed sufficiently when shifting the orbit of a minute
50          final Orbit initialOrbit = new CartesianOrbit(context.initialOrbit);
51  
52          final AbstractPropagatorBuilder<KeplerianPropagator> propagatorBuilder = new AbstractPropagatorBuilder(initialOrbit, PositionAngleType.TRUE, 10., true) {
53  
54              @Override
55              public KeplerianPropagator buildPropagator(double[] normalizedParameters) {
56                  // Dummy function "buildPropagator", copied from KeplerianPropagatorBuilder
57                  setParameters(normalizedParameters);
58                  return new KeplerianPropagator(createInitialOrbit());
59              }
60  
61              @Override
62              public AbstractBatchLSModel buildLeastSquaresModel(PropagatorBuilder[] builders,
63                                                                 List<ObservedMeasurement<?>> measurements,
64                                                                 ParameterDriversList estimatedMeasurementsParameters,
65                                                                 ModelObserver observer) {
66                  // The test don't use orbit determination. So, the method can return null
67                  return null;
68              }
69          };
70  
71          // Shift the orbit of a minute
72          // Reset the builder and check the orbits value
73          final Orbit newOrbit = initialOrbit.shiftedBy(60.).inFrame(FramesFactory.getTOD(true));
74          propagatorBuilder.resetOrbit(newOrbit);
75  
76          // Check that the new orbit was properly set in the builder and
77          Assertions.assertEquals(0., propagatorBuilder.getInitialOrbitDate().durationFrom(newOrbit.getDate()), 0.);
78          final double[] stateVector = new double[6];
79          propagatorBuilder.getOrbitType().mapOrbitToArray(newOrbit.inFrame(context.initialOrbit.getFrame()),
80                  PositionAngleType.TRUE, stateVector, null);
81          int i = 0;
82          for (DelegatingDriver driver : propagatorBuilder.getOrbitalParametersDrivers().getDrivers()) {
83              final double expectedValue = stateVector[i++];
84              Assertions.assertEquals(expectedValue, driver.getValue(), 0.);
85              Assertions.assertEquals(expectedValue, driver.getReferenceValue(), 0.);
86          }
87      }
88  
89      /**
90       * Assert that actual {@link PropagatorBuilder} instance et is a copy of expected instance.
91       *
92       * @param expected expected instance to compare to
93       * @param actual actual instance to be compared
94       * @param <B> type of the propagator builder
95       */
96      public static <B extends AbstractPropagatorBuilder> void assertPropagatorBuilderIsACopy(final B expected, final B actual){
97  
98          // They should not be the same instance
99          Assertions.assertNotEquals(expected, actual);
100 
101         Assertions.assertArrayEquals(expected.getSelectedNormalizedParameters(),
102                                      actual.getSelectedNormalizedParameters());
103 
104         assertParametersDriversValues(expected.getOrbitalParametersDrivers(),
105                                        actual.getOrbitalParametersDrivers());
106 
107         Assertions.assertEquals(expected.getFrame(), actual.getFrame());
108         Assertions.assertEquals(expected.getMu(), actual.getMu());
109         Assertions.assertEquals(expected.getAttitudeProvider(), actual.getAttitudeProvider());
110         Assertions.assertEquals(expected.getOrbitType(), actual.getOrbitType());
111         Assertions.assertEquals(expected.getPositionAngleType(), actual.getPositionAngleType());
112         Assertions.assertEquals(expected.getPositionScale(), actual.getPositionScale());
113         Assertions.assertEquals(expected.getInitialOrbitDate(), actual.getInitialOrbitDate());
114         Assertions.assertEquals(expected.getAdditionalDerivativesProviders(), actual.getAdditionalDerivativesProviders());
115 
116         // Verify that the propagations give the same results
117         AbsoluteDate targetEpoch = expected.getInitialOrbitDate().shiftedBy(7200.0);
118         TimeStampedPVCoordinates expectedCoordinates = expected.buildPropagator().propagate(targetEpoch).getPVCoordinates();
119         TimeStampedPVCoordinates actualCoordinates   = actual.buildPropagator().propagate(targetEpoch).getPVCoordinates();
120         Assertions.assertEquals(0.0, Vector3D.distance(expectedCoordinates.getPosition(), actualCoordinates.getPosition()));
121         Assertions.assertEquals(0.0, Vector3D.distance(expectedCoordinates.getVelocity(), actualCoordinates.getVelocity()));
122 
123     }
124 }
125