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.     /** {@inheritDoc} */
  63.     public void init(final SpacecraftState s0, final AbsoluteDate t) {
  64.         lastState = null;
  65.         forward   = true;
  66.         handler.init(s0, t, h);
  67.     }

  68.     /**
  69.      * Handle the last accepted step.
  70.      * @param interpolator interpolator for the last accepted step. For
  71.      * efficiency purposes, the various propagators reuse the same
  72.      * object on each call, so if the instance wants to keep it across
  73.      * all calls (for example to provide at the end of the propagation a
  74.      * continuous model valid throughout the propagation range), it
  75.      * should build a local copy using the clone method and store this
  76.      * copy.
  77.      */
  78.     public void handleStep(final OrekitStepInterpolator interpolator) {

  79.         if (lastState == null) {
  80.             // initialize lastState in the first step case
  81.             lastState = interpolator.getPreviousState();
  82.         }

  83.         // take the propagation direction into account
  84.         double step = h;
  85.         forward = interpolator.isForward();
  86.         if (!forward) {
  87.             step = -h;
  88.         }


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

  93.             // output the stored previous step
  94.             handler.handleStep(lastState);

  95.             // store the next step
  96.             lastState = interpolator.getInterpolatedState(nextTime);

  97.             // prepare next iteration
  98.             nextTime = nextTime.shiftedBy(step);
  99.             nextInStep = forward ^ nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0;

  100.         }
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public void finish(final SpacecraftState finalState) {

  105.         // there will be no more steps,
  106.         // the stored one should be handled now
  107.         handler.handleStep(lastState);

  108.         // and the final state handled too
  109.         handler.finish(finalState);

  110.     }

  111. }