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.apache.commons.math3.util.FastMath;
  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.errors.PropagationException;
  21. import org.orekit.propagation.SpacecraftState;
  22. import org.orekit.time.AbsoluteDate;

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

  26.  * <p>It mirrors the <code>StepNormalizer</code> interface from <a
  27.  * href="http://commons.apache.org/math/">commons-math</a> but
  28.  * provides a space-dynamics interface to the methods.</p>
  29.  * @author Luc Maisonobe
  30.  */
  31. public class OrekitStepNormalizer implements OrekitStepHandler {

  32.     /** Fixed time step. */
  33.     private double h;

  34.     /** Underlying step handler. */
  35.     private OrekitFixedStepHandler handler;

  36.     /** Last step date. */
  37.     private AbsoluteDate lastDate;

  38.     /** Last State vector. */
  39.     private SpacecraftState lastState;

  40.     /** Integration direction indicator. */
  41.     private boolean forward;

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

  53.     /** Determines whether this handler needs dense output.
  54.      * This handler needs dense output in order to provide data at
  55.      * regularly spaced steps regardless of the steps the propagator
  56.      * uses, so this method always returns true.
  57.      * @return always true
  58.      */
  59.     public boolean requiresDenseOutput() {
  60.         return true;
  61.     }

  62.     /** {@inheritDoc} */
  63.     public void init(final SpacecraftState s0, final AbsoluteDate t)
  64.         throws PropagationException {
  65.         lastDate  = null;
  66.         lastState = null;
  67.         forward   = true;
  68.         handler.init(s0, t);
  69.     }

  70.     /**
  71.      * Handle the last accepted step.
  72.      * @param interpolator interpolator for the last accepted step. For
  73.      * efficiency purposes, the various propagators reuse the same
  74.      * object on each call, so if the instance wants to keep it across
  75.      * all calls (for example to provide at the end of the propagation a
  76.      * continuous model valid throughout the propagation range), it
  77.      * should build a local copy using the clone method and store this
  78.      * copy.
  79.      * @param isLast true if the step is the last one
  80.      * @throws PropagationException this exception is propagated to the
  81.      * caller if the underlying user function triggers one
  82.      */
  83.     public void handleStep(final OrekitStepInterpolator interpolator, final boolean isLast)
  84.         throws PropagationException {
  85.         try {

  86.             if (lastState == null) {
  87.                 // initialize lastState in the first step case

  88.                 lastDate = interpolator.getPreviousDate();
  89.                 interpolator.setInterpolatedDate(lastDate);
  90.                 lastState = interpolator.getInterpolatedState();

  91.                 // take the propagation direction into account
  92.                 forward = interpolator.getCurrentDate().compareTo(lastDate) >= 0;
  93.                 if (!forward) {
  94.                     h = -h;
  95.                 }

  96.             }

  97.             // use the interpolator to push fixed steps events to the underlying handler
  98.             AbsoluteDate nextTime = lastDate.shiftedBy(h);
  99.             boolean nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentDate()) > 0);
  100.             while (nextInStep) {

  101.                 // output the stored previous step
  102.                 handler.handleStep(lastState, false);

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

  107.                 // prepare next iteration
  108.                 nextTime = nextTime.shiftedBy(h);
  109.                 nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentDate()) > 0);

  110.             }

  111.             if (isLast) {
  112.                 // there will be no more steps,
  113.                 // the stored one should be flagged as being the last
  114.                 handler.handleStep(lastState, true);
  115.             }

  116.         } catch (OrekitException oe) {

  117.             // recover a possible embedded PropagationException
  118.             for (Throwable t = oe; t != null; t = t.getCause()) {
  119.                 if (t instanceof PropagationException) {
  120.                     throw (PropagationException) t;
  121.                 }
  122.             }

  123.             throw new PropagationException(oe);

  124.         }
  125.     }

  126. }