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.orbits;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.analysis.polynomials.SmoothStepFactory;
21  import org.orekit.errors.OrekitException;
22  import org.orekit.frames.Frame;
23  import org.orekit.propagation.FieldSpacecraftState;
24  import org.orekit.propagation.analytical.AbstractAnalyticalPropagator;
25  import org.orekit.propagation.analytical.FieldAbstractAnalyticalPropagator;
26  import org.orekit.time.FieldAbsoluteDate;
27  import org.orekit.utils.FieldPVCoordinates;
28  
29  import java.util.List;
30  
31  /**
32   * Orbit blender.
33   * <p>
34   * Its purpose is to interpolate orbit state between tabulated orbit states using the concept of blending, exposed in :
35   * "Efficient Covariance Interpolation using Blending of Approximate State Error Transitions" by Sergei Tanygin, and applying
36   * it to orbit states instead of covariances.
37   * <p>
38   * It propagates tabulated values to the interpolating time using given analytical propagator and then blend each propagated
39   * states using a smoothstep function. It gives especially good results as explained
40   * <a href="https://orekit.org/doc/technical-notes/Implementation_of_covariance_interpolation_in_Orekit.pdf">here</a>
41   * compared to Hermite interpolation when time steps between tabulated values get significant (In LEO, &gt; 10 mn for
42   * example).
43   *
44   * @param <KK> type of field element
45   *
46   * @author Vincent Cucchietti
47   * @see org.hipparchus.analysis.polynomials.SmoothStepFactory
48   * @see org.hipparchus.analysis.polynomials.SmoothStepFactory.FieldSmoothStepFunction
49   */
50  public class FieldOrbitBlender<KK extends CalculusFieldElement<KK>> extends AbstractFieldOrbitInterpolator<KK> {
51  
52      /** Analytical propagator used to propagate tabulated orbits to interpolating time. */
53      private final FieldAbstractAnalyticalPropagator<KK> analyticalPropagator;
54  
55      /** Blending function. */
56      private final SmoothStepFactory.FieldSmoothStepFunction<KK> blendingFunction;
57  
58      /**
59       * Default constructor.
60       *
61       * @param blendingFunction
62       * {@link org.hipparchus.analysis.polynomials.SmoothStepFactory.SmoothStepFunction smoothstep function} used for
63       * blending
64       * @param analyticalPropagator analytical propagator used to propagate tabulated orbits to interpolating time
65       * @param outputInertialFrame output inertial frame
66       *
67       * @throws OrekitException if output frame is not inertial
68       */
69      public FieldOrbitBlender(final SmoothStepFactory.FieldSmoothStepFunction<KK> blendingFunction,
70                               final FieldAbstractAnalyticalPropagator<KK> analyticalPropagator,
71                               final Frame outputInertialFrame) {
72          super(DEFAULT_INTERPOLATION_POINTS, 0., outputInertialFrame);
73          this.blendingFunction     = blendingFunction;
74          this.analyticalPropagator = analyticalPropagator;
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public FieldOrbit<KK> interpolate(final InterpolationData interpolationData) {
80  
81          // Get interpolation date
82          final FieldAbsoluteDate<KK> interpolationDate = interpolationData.getInterpolationDate();
83  
84          // Get first and last entry
85          final List<FieldOrbit<KK>> neighborList  = interpolationData.getNeighborList();
86          final FieldOrbit<KK>       previousOrbit = neighborList.get(0);
87          final FieldOrbit<KK>       nextOrbit     = neighborList.get(1);
88  
89          // Propagate orbits
90          final FieldOrbit<KK> forwardedOrbit  = propagateOrbitAnalytically(previousOrbit, interpolationDate);
91          final FieldOrbit<KK> backwardedOrbit = propagateOrbitAnalytically(nextOrbit, interpolationDate);
92  
93          // Extract position-velocity-acceleration coordinates
94          final FieldPVCoordinates<KK> forwardedPV  = forwardedOrbit.getPVCoordinates(getOutputInertialFrame());
95          final FieldPVCoordinates<KK> backwardedPV = backwardedOrbit.getPVCoordinates(getOutputInertialFrame());
96  
97          // Blend PV coordinates
98          final KK timeParameter = getTimeParameter(interpolationDate, previousOrbit.getDate(), nextOrbit.getDate());
99          final KK blendingValue = blendingFunction.value(timeParameter);
100 
101         final FieldPVCoordinates<KK> blendedPV = forwardedPV.blendArithmeticallyWith(backwardedPV, blendingValue);
102 
103         // Output new blended instance
104         return new FieldCartesianOrbit<>(blendedPV, getOutputInertialFrame(), interpolationDate, previousOrbit.getMu());
105     }
106 
107     /**
108      * Propagate orbit using predefined {@link AbstractAnalyticalPropagator analytical propagator}.
109      *
110      * @param tabulatedOrbit tabulated orbit to propagate
111      * @param propagationDate propagation date
112      *
113      * @return orbit propagated to propagation date
114      */
115     private FieldOrbit<KK> propagateOrbitAnalytically(final FieldOrbit<KK> tabulatedOrbit,
116                                                       final FieldAbsoluteDate<KK> propagationDate) {
117 
118         analyticalPropagator.resetInitialState(new FieldSpacecraftState<>(tabulatedOrbit));
119 
120         return analyticalPropagator.propagate(propagationDate).getOrbit();
121     }
122 }