FieldOrekitStepNormalizer.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.RealFieldElement;
  19. import org.orekit.propagation.FieldSpacecraftState;
  20. import org.orekit.time.FieldAbsoluteDate;

  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="http://commons.apache.org/math/">commons-math</a> but
  26.  * provides a space-dynamics interface to the methods.</p>
  27.  * @author Luc Maisonobe
  28.  */
  29. public class FieldOrekitStepNormalizer <T extends RealFieldElement<T>> implements FieldOrekitStepHandler<T> {

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

  32.     /** Underlying step handler. */
  33.     private FieldOrekitFixedStepHandler<T> handler;

  34.     /** Last State vector. */
  35.     private FieldSpacecraftState<T> 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 FieldOrekitStepNormalizer(final T h, final FieldOrekitFixedStepHandler<T> handler) {
  43.         this.h       = h.abs();
  44.         this.handler = handler;
  45.         lastState = null;
  46.         forward   = true;
  47.     }

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

  57.     /** {@inheritDoc} */
  58.     public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
  59.         lastState = null;
  60.         forward   = true;
  61.         handler.init(s0, t, h);
  62.     }

  63.     /**
  64.      * Handle the last accepted step.
  65.      * @param interpolator interpolator for the last accepted step. For
  66.      * efficiency purposes, the various propagators reuse the same
  67.      * object on each call, so if the instance wants to keep it across
  68.      * all calls (for example to provide at the end of the propagation a
  69.      * continuous model valid throughout the propagation range), it
  70.      * should build a local copy using the clone method and store this
  71.      * copy.
  72.      * @param isLast true if the step is the last one
  73.      */
  74.     public void handleStep(final FieldOrekitStepInterpolator<T> interpolator, final boolean isLast) {

  75.         if (lastState == null) {
  76.             // initialize lastState in the first step case
  77.             lastState = interpolator.getPreviousState();
  78.         }
  79.         // take the propagation direction into account
  80.         T step = h;
  81.         forward = interpolator.isForward();
  82.         if (!forward) {
  83.             step = h.multiply(-1);
  84.         }


  85.         // use the interpolator to push fixed steps events to the underlying handler
  86.         FieldAbsoluteDate<T> nextTime = lastState.getDate().shiftedBy(step);
  87.         boolean nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
  88.         while (nextInStep) {

  89.             // output the stored previous step
  90.             handler.handleStep(lastState, false);

  91.             // store the next step
  92.             lastState = interpolator.getInterpolatedState(nextTime);

  93.             // prepare next iteration
  94.             nextTime = nextTime.shiftedBy(step);
  95.             nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);

  96.         }

  97.         if (isLast) {
  98.             // there will be no more steps,
  99.             // the stored one should be flagged as being the last
  100.             handler.handleStep(lastState, true);
  101.         }

  102.     }

  103. }