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.semianalytical.dsst;
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, DSSTStateTransitionMatrixGenerator.DSSTPartialsObserver {
30  
31      private final DSSTPropagator propagator;
32      private final MatricesHarvester harvester;
33      private final AbsoluteDate pickUpDate;
34      private final String accParamName;
35      private final String columnName;
36      private DSSTStateTransitionMatrixGenerator stmGenerator;
37      private SpacecraftState s0;
38      private RealMatrix dYdY0;
39      private RealMatrix dYdP;
40      private double[] accPartial;
41  
42      public PickUpHandler(final DSSTPropagator propagator, final AbsoluteDate pickUpDate,
43                           final String accParamName, final String columnName) {
44          this.propagator   = propagator;
45          this.harvester    = propagator.setupMatricesComputation("stm", null, null);
46          initializeShortPeriod();
47          this.pickUpDate   = pickUpDate;
48          this.accParamName = accParamName;
49          this.columnName   = columnName;
50          this.s0           = null;
51          this.accPartial   = null;
52      }
53  
54      public SpacecraftState getState() {
55          return s0;
56      }
57  
58      public RealMatrix getStm() {
59          return dYdY0;
60      }
61  
62      public RealMatrix getdYdP() {
63          return dYdP;
64      }
65  
66      public double[] getAccPartial() {
67          return accPartial.clone();
68      }
69  
70      public void init(SpacecraftState s0, AbsoluteDate t) {
71          // as the generators are only created on the fly at propagation start
72          // we retrieve the STM generator here
73          for (final AdditionalDerivativesProvider provider : propagator.getAdditionalDerivativesProviders()) {
74              if (provider instanceof DSSTStateTransitionMatrixGenerator) {
75                  stmGenerator = (DSSTStateTransitionMatrixGenerator) provider;
76                  stmGenerator.addObserver(accParamName, this);
77              }
78          }
79      }
80  
81      public void handleStep(OrekitStepInterpolator interpolator) {
82          if (pickUpDate != null) {
83              // we want to pick up some intermediate Jacobians
84              double dt0 = pickUpDate.durationFrom(interpolator.getPreviousState().getDate());
85              double dt1 = pickUpDate.durationFrom(interpolator.getCurrentState().getDate());
86              if (dt0 * dt1 > 0) {
87                  // the current step does not cover the pickup date
88                  return;
89              } else {
90                  checkState(interpolator.getInterpolatedState(pickUpDate));
91              }
92          }
93      }
94  
95      public void finish(SpacecraftState finalState) {
96          if (s0 == null) {
97              checkState(finalState);
98          }
99      }
100 
101     public void partialsComputed(final SpacecraftState state, final RealMatrix newFactor, final double[] newAccelerationPartials) {
102         if (accParamName != null &&
103             (pickUpDate == null || FastMath.abs(pickUpDate.durationFrom(state.getDate())) < 1.0e-6)) {
104             accPartial = newAccelerationPartials.clone();
105         }
106     }
107 
108     private void checkState(final SpacecraftState state) {
109         stmGenerator.combinedDerivatives(state); // just for the side effect of calling partialsComputed
110         Assertions.assertEquals(columnName == null ? 1 : 2, state.getAdditionalDataValues().size());
111         dYdY0 = harvester.getStateTransitionMatrix(state);
112         dYdP  = harvester.getParametersJacobian(state); // may be null
113         s0    = state;
114     }
115 
116 
117     private void initializeShortPeriod() {
118         // Mean orbit
119         final SpacecraftState initial = propagator.initialIsOsculating() ?
120                        DSSTPropagator.computeMeanState(propagator.getInitialState(), propagator.getAttitudeProvider(), propagator.getAllForceModels()) :
121                            propagator.getInitialState();
122         ((DSSTHarvester) harvester).initializeFieldShortPeriodTerms(initial); // Initial state is MEAN
123     }
124 
125 }