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   */
32  public class FieldOrekitStepNormalizer <T extends CalculusFieldElement<T>> implements FieldOrekitStepHandler<T> {
33  
34      /** Fixed time step. */
35      private T h;
36  
37      /** Underlying step handler. */
38      private FieldOrekitFixedStepHandler<T> handler;
39  
40      /** Last State vector. */
41      private FieldSpacecraftState<T> 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 FieldOrekitStepNormalizer(final T h, final FieldOrekitFixedStepHandler<T> handler) {
51          this.h       = h.abs();
52          this.handler = handler;
53          lastState = null;
54          forward   = true;
55      }
56  
57      /** Get the fixed time step.
58       * @return fixed time step
59       * @since 11.0
60       */
61      public T 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 FieldOrekitFixedStepHandler<T> getFixedStepHandler() {
70          return handler;
71      }
72  
73      /** Determines whether this handler needs dense output.
74       * This handler needs dense output in order to provide data at
75       * regularly spaced steps regardless of the steps the propagator
76       * uses, so this method always returns true.
77       * @return always true
78       */
79      public boolean requiresDenseOutput() {
80          return true;
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
86          lastState = null;
87          forward   = true;
88          handler.init(s0, t, h);
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public void handleStep(final FieldOrekitStepInterpolator<T> interpolator) {
94  
95          if (lastState == null) {
96              // initialize lastState in the first step case
97              lastState = interpolator.getPreviousState();
98          }
99          // take the propagation direction into account
100         T step = h;
101         forward = interpolator.isForward();
102         if (!forward) {
103             step = h.multiply(-1);
104         }
105 
106 
107         // use the interpolator to push fixed steps events to the underlying handler
108         FieldAbsoluteDate<T> nextTime = lastState.getDate().shiftedBy(step);
109         boolean nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
110         while (nextInStep) {
111 
112             // output the stored previous step
113             handler.handleStep(lastState);
114 
115             // store the next step
116             lastState = interpolator.getInterpolatedState(nextTime);
117 
118             // prepare next iteration
119             nextTime = nextTime.shiftedBy(step);
120             nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
121 
122         }
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public void finish(final FieldSpacecraftState<T> finalState) {
128 
129         // there will be no more steps,
130         // the stored one should be handled now
131         handler.handleStep(lastState);
132 
133         // and the final state handled too
134         handler.finish(finalState);
135 
136     }
137 
138 }