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.CalculusFieldElement;
20  import org.orekit.propagation.FieldSpacecraftState;
21  import org.orekit.time.FieldAbsoluteDate;
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="http://commons.apache.org/math/">commons-math</a> but
29   * provides a space-dynamics interface to the methods.</p>
30   * @author Luc Maisonobe
31   * @param <T> type of the field elements
32   */
33  public class FieldOrekitStepNormalizer <T extends CalculusFieldElement<T>> implements FieldOrekitStepHandler<T> {
34  
35      /** Fixed time step. */
36      private T h;
37  
38      /** Underlying step handler. */
39      private FieldOrekitFixedStepHandler<T> handler;
40  
41      /** Last State vector. */
42      private FieldSpacecraftState<T> lastState;
43  
44      /** Integration direction indicator. */
45      private boolean forward;
46  
47      /** Simple constructor.
48       * @param h fixed time step (sign is not used)
49       * @param handler fixed time step handler to wrap
50       */
51      public FieldOrekitStepNormalizer(final T h, final FieldOrekitFixedStepHandler<T> handler) {
52          this.h       = h.abs();
53          this.handler = handler;
54          lastState = null;
55          forward   = true;
56      }
57  
58      /** Get the fixed time step.
59       * @return fixed time step
60       * @since 11.0
61       */
62      public T getFixedTimeStep() {
63          return h;
64      }
65  
66      /** Get the underlying fixed step handler.
67       * @return underlying fixed step handler
68       * @since 11.0
69       */
70      public FieldOrekitFixedStepHandler<T> getFixedStepHandler() {
71          return handler;
72      }
73  
74      /** Determines whether this handler needs dense output.
75       * This handler needs dense output in order to provide data at
76       * regularly spaced steps regardless of the steps the propagator
77       * uses, so this method always returns true.
78       * @return always true
79       */
80      public boolean requiresDenseOutput() {
81          return true;
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
87          lastState = null;
88          forward   = true;
89          handler.init(s0, t, h);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public void handleStep(final FieldOrekitStepInterpolator<T> interpolator) {
95  
96          if (lastState == null) {
97              // initialize lastState in the first step case
98              lastState = interpolator.getPreviousState();
99          }
100         // take the propagation direction into account
101         T step = h;
102         forward = interpolator.isForward();
103         if (!forward) {
104             step = h.multiply(-1);
105         }
106 
107 
108         // use the interpolator to push fixed steps events to the underlying handler
109         FieldAbsoluteDate<T> nextTime = lastState.getDate().shiftedBy(step);
110         boolean nextInStep = forward ^ nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0;
111         while (nextInStep) {
112 
113             // output the stored previous step
114             handler.handleStep(lastState);
115 
116             // store the next step
117             lastState = interpolator.getInterpolatedState(nextTime);
118 
119             // prepare next iteration
120             nextTime = nextTime.shiftedBy(step);
121             nextInStep = forward ^ nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0;
122 
123         }
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public void finish(final FieldSpacecraftState<T> finalState) {
129 
130         // there will be no more steps,
131         // the stored one should be handled now
132         handler.handleStep(lastState);
133 
134         // and the final state handled too
135         handler.finish(finalState);
136 
137     }
138 
139 }