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 /** 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 public void init(final SpacecraftState s0, final AbsoluteDate t) {
85 lastState = null;
86 forward = true;
87 handler.init(s0, t, h);
88 }
89
90 /**
91 * Handle the last accepted step.
92 * @param interpolator interpolator for the last accepted step. For
93 * efficiency purposes, the various propagators reuse the same
94 * object on each call, so if the instance wants to keep it across
95 * all calls (for example to provide at the end of the propagation a
96 * continuous model valid throughout the propagation range), it
97 * should build a local copy using the clone method and store this
98 * copy.
99 */
100 public void handleStep(final OrekitStepInterpolator interpolator) {
101
102 if (lastState == null) {
103 // initialize lastState in the first step case
104 lastState = interpolator.getPreviousState();
105 }
106
107 // take the propagation direction into account
108 double step = h;
109 forward = interpolator.isForward();
110 if (!forward) {
111 step = -h;
112 }
113
114
115 // use the interpolator to push fixed steps events to the underlying handler
116 AbsoluteDate nextTime = lastState.getDate().shiftedBy(step);
117 boolean nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
118 while (nextInStep) {
119
120 // output the stored previous step
121 handler.handleStep(lastState);
122
123 // store the next step
124 lastState = interpolator.getInterpolatedState(nextTime);
125
126 // prepare next iteration
127 nextTime = nextTime.shiftedBy(step);
128 nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
129
130 }
131 }
132
133 /** {@inheritDoc} */
134 @Override
135 public void finish(final SpacecraftState finalState) {
136
137 // there will be no more steps,
138 // the stored one should be handled now
139 handler.handleStep(lastState);
140
141 // and the final state handled too
142 handler.finish(finalState);
143
144 }
145
146 }