1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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.sampling;
18  
19  import org.hipparchus.util.FastMath;
20  import org.orekit.propagation.SpacecraftState;
21  import org.orekit.time.AbsoluteDate;
22  
23  /**
24   * This class wraps an object implementing {@link OrekitFixedStepHandler}
25   * into a {@link OrekitStepHandler}.
26  
27   * <p>It mirrors the <code>StepNormalizer</code> interface from <a
28   * href="https://hipparchus.org/">Hipparchus</a> but
29   * provides a space-dynamics interface to the methods.</p>
30   * @author Luc Maisonobe
31   */
32  public class OrekitStepNormalizer implements OrekitStepHandler {
33  
34      /** Fixed time step. */
35      private double h;
36  
37      /** Underlying fixed step handler. */
38      private OrekitFixedStepHandler handler;
39  
40      /** Last State vector. */
41      private SpacecraftState lastState;
42  
43      /** Integration direction indicator. */
44      private boolean forward;
45  
46      /** Simple constructor.
47       * @param h fixed time step (sign is not used)
48       * @param handler fixed time step handler to wrap
49       */
50      public OrekitStepNormalizer(final double h, final OrekitFixedStepHandler handler) {
51          this.h         = FastMath.abs(h);
52          this.handler   = handler;
53          this.lastState = null;
54          this.forward   = true;
55      }
56  
57      /** Get the fixed time step.
58       * @return fixed time step
59       * @since 11.0
60       */
61      public double getFixedTimeStep() {
62          return h;
63      }
64  
65      /** Get the underlying fixed step handler.
66       * @return underlying fixed step handler
67       * @since 11.0
68       */
69      public OrekitFixedStepHandler getFixedStepHandler() {
70          return handler;
71      }
72  
73      /** {@inheritDoc} */
74      public void init(final SpacecraftState s0, final AbsoluteDate t) {
75          lastState = null;
76          forward   = true;
77          handler.init(s0, t, h);
78      }
79  
80      /**
81       * Handle the last accepted step.
82       * @param interpolator interpolator for the last accepted step. For
83       * efficiency purposes, the various propagators reuse the same
84       * object on each call, so if the instance wants to keep it across
85       * all calls (for example to provide at the end of the propagation a
86       * continuous model valid throughout the propagation range), it
87       * should build a local copy using the clone method and store this
88       * copy.
89       */
90      public void handleStep(final OrekitStepInterpolator interpolator) {
91  
92          if (lastState == null) {
93              // initialize lastState in the first step case
94              lastState = interpolator.getPreviousState();
95          }
96  
97          // take the propagation direction into account
98          double step = h;
99          forward = interpolator.isForward();
100         if (!forward) {
101             step = -h;
102         }
103 
104 
105         // use the interpolator to push fixed steps events to the underlying handler
106         AbsoluteDate nextTime = lastState.getDate().shiftedBy(step);
107         boolean nextInStep = forward ^ nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0;
108         while (nextInStep) {
109 
110             // output the stored previous step
111             handler.handleStep(lastState);
112 
113             // store the next step
114             lastState = interpolator.getInterpolatedState(nextTime);
115 
116             // prepare next iteration
117             nextTime = nextTime.shiftedBy(step);
118             nextInStep = forward ^ nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0;
119 
120         }
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public void finish(final SpacecraftState finalState) {
126 
127         // there will be no more steps,
128         // the stored one should be handled now
129         handler.handleStep(lastState);
130 
131         // and the final state handled too
132         handler.finish(finalState);
133 
134     }
135 
136 }