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.numerical;
18  
19  import org.hipparchus.linear.RealMatrix;
20  import org.hipparchus.util.FastMath;
21  import org.junit.jupiter.api.Assertions;
22  import org.orekit.propagation.MatricesHarvester;
23  import org.orekit.propagation.SpacecraftState;
24  import org.orekit.propagation.integration.AdditionalDerivativesProvider;
25  import org.orekit.propagation.sampling.OrekitStepHandler;
26  import org.orekit.propagation.sampling.OrekitStepInterpolator;
27  import org.orekit.time.AbsoluteDate;
28  
29  class PickUpHandler implements OrekitStepHandler, StateTransitionMatrixGenerator.PartialsObserver {
30  
31      private final NumericalPropagator propagator;
32      private final MatricesHarvester harvester;
33      private final AbsoluteDate pickUpDate;
34      private final String accParamName;
35      private final String columnName;
36      private StateTransitionMatrixGenerator stmGenerator;
37      private SpacecraftState s0;
38      private RealMatrix dYdY0;
39      private RealMatrix dYdP;
40      private double[] accPartial;
41  
42      public PickUpHandler(final NumericalPropagator propagator, final AbsoluteDate pickUpDate,
43                           final String accParamName, final String columnName) {
44          this.propagator   = propagator;
45          this.harvester    = propagator.setupMatricesComputation("stm", null, null);
46          this.pickUpDate   = pickUpDate;
47          this.accParamName = accParamName;
48          this.columnName   = columnName;
49          this.s0           = null;
50          this.accPartial   = null;
51          if (columnName == null) {
52              Assertions.assertTrue(harvester.getJacobiansColumnsNames().isEmpty());
53          } else {
54              Assertions.assertEquals(1, harvester.getJacobiansColumnsNames().size());
55              Assertions.assertEquals(columnName, harvester.getJacobiansColumnsNames().get(0));
56          }
57      }
58  
59      public SpacecraftState getState() {
60          return s0;
61      }
62  
63      public RealMatrix getStm() {
64          return dYdY0;
65      }
66  
67      public RealMatrix getdYdP() {
68          return dYdP;
69      }
70  
71      public double[] getAccPartial() {
72          return accPartial.clone();
73      }
74  
75      public void init(SpacecraftState s0, AbsoluteDate t) {
76          // as the generators are only created on the fly at propagation start
77          // we retrieve the STM generator here
78          for (final AdditionalDerivativesProvider provider : propagator.getAdditionalDerivativesProviders()) {
79              if (provider instanceof StateTransitionMatrixGenerator) {
80                  stmGenerator = (StateTransitionMatrixGenerator) provider;
81                  stmGenerator.addObserver(accParamName, this);
82              }
83          }
84      }
85  
86      public void handleStep(OrekitStepInterpolator interpolator) {
87          if (pickUpDate != null) {
88              // we want to pick up some intermediate Jacobians
89              double dt0 = pickUpDate.durationFrom(interpolator.getPreviousState().getDate());
90              double dt1 = pickUpDate.durationFrom(interpolator.getCurrentState().getDate());
91              if (dt0 * dt1 > 0) {
92                  // the current step does not cover the pickup date
93                  return;
94              } else {
95                  checkState(interpolator.getInterpolatedState(pickUpDate));
96              }
97          }
98      }
99  
100     public void finish(SpacecraftState finalState) {
101         if (s0 == null) {
102             checkState(finalState);
103         }
104     }
105 
106     public void partialsComputed(final SpacecraftState state, final double[] newFactor, final double[] newAccelerationPartials) {
107         if (accParamName != null &&
108             (pickUpDate == null || FastMath.abs(pickUpDate.durationFrom(state.getDate())) < 1.0e-6)) {
109             accPartial = newAccelerationPartials.clone();
110         }
111     }
112 
113     private void checkState(final SpacecraftState state) {
114         stmGenerator.combinedDerivatives(state); // just for the side effect of calling partialsComputed
115         Assertions.assertEquals(columnName == null ? 1 : 2, state.getAdditionalDataValues().size());
116         dYdY0 = harvester.getStateTransitionMatrix(state);
117         dYdP  = harvester.getParametersJacobian(state); // may be null
118         s0    = state;
119     }
120 
121 }