OrekitStepNormalizer.java

  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. import org.hipparchus.util.FastMath;
  19. import org.orekit.propagation.SpacecraftState;
  20. import org.orekit.time.AbsoluteDate;

  21. /**
  22.  * This class wraps an object implementing {@link OrekitFixedStepHandler}
  23.  * into a {@link OrekitStepHandler}.

  24.  * <p>It mirrors the <code>StepNormalizer</code> interface from <a
  25.  * href="https://hipparchus.org/">Hipparchus</a> but
  26.  * provides a space-dynamics interface to the methods.</p>
  27.  * @author Luc Maisonobe
  28.  */
  29. public class OrekitStepNormalizer implements OrekitStepHandler {

  30.     /** Fixed time step. */
  31.     private double h;

  32.     /** Underlying fixed step handler. */
  33.     private OrekitFixedStepHandler handler;

  34.     /** Last State vector. */
  35.     private SpacecraftState lastState;

  36.     /** Integration direction indicator. */
  37.     private boolean forward;

  38.     /** Simple constructor.
  39.      * @param h fixed time step (sign is not used)
  40.      * @param handler fixed time step handler to wrap
  41.      */
  42.     public OrekitStepNormalizer(final double h, final OrekitFixedStepHandler handler) {
  43.         this.h         = FastMath.abs(h);
  44.         this.handler   = handler;
  45.         this.lastState = null;
  46.         this.forward   = true;
  47.     }

  48.     /** Get the fixed time step.
  49.      * @return fixed time step
  50.      * @since 11.0
  51.      */
  52.     public double getFixedTimeStep() {
  53.         return h;
  54.     }

  55.     /** Get the underlying fixed step handler.
  56.      * @return underlying fixed step handler
  57.      * @since 11.0
  58.      */
  59.     public OrekitFixedStepHandler getFixedStepHandler() {
  60.         return handler;
  61.     }

  62.     /** Determines whether this handler needs dense output.
  63.      * This handler needs dense output in order to provide data at
  64.      * regularly spaced steps regardless of the steps the propagator
  65.      * uses, so this method always returns true.
  66.      * @return always true
  67.      */
  68.     public boolean requiresDenseOutput() {
  69.         return true;
  70.     }

  71.     /** {@inheritDoc} */
  72.     public void init(final SpacecraftState s0, final AbsoluteDate t) {
  73.         lastState = null;
  74.         forward   = true;
  75.         handler.init(s0, t, h);
  76.     }

  77.     /**
  78.      * Handle the last accepted step.
  79.      * @param interpolator interpolator for the last accepted step. For
  80.      * efficiency purposes, the various propagators reuse the same
  81.      * object on each call, so if the instance wants to keep it across
  82.      * all calls (for example to provide at the end of the propagation a
  83.      * continuous model valid throughout the propagation range), it
  84.      * should build a local copy using the clone method and store this
  85.      * copy.
  86.      */
  87.     public void handleStep(final OrekitStepInterpolator interpolator) {

  88.         if (lastState == null) {
  89.             // initialize lastState in the first step case
  90.             lastState = interpolator.getPreviousState();
  91.         }

  92.         // take the propagation direction into account
  93.         double step = h;
  94.         forward = interpolator.isForward();
  95.         if (!forward) {
  96.             step = -h;
  97.         }


  98.         // use the interpolator to push fixed steps events to the underlying handler
  99.         AbsoluteDate nextTime = lastState.getDate().shiftedBy(step);
  100.         boolean nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
  101.         while (nextInStep) {

  102.             // output the stored previous step
  103.             handler.handleStep(lastState);

  104.             // store the next step
  105.             lastState = interpolator.getInterpolatedState(nextTime);

  106.             // prepare next iteration
  107.             nextTime = nextTime.shiftedBy(step);
  108.             nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);

  109.         }
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     public void finish(final SpacecraftState finalState) {

  114.         // there will be no more steps,
  115.         // the stored one should be handled now
  116.         handler.handleStep(lastState);

  117.         // and the final state handled too
  118.         handler.finish(finalState);

  119.     }

  120. }