MultisatStepNormalizer.java

  1. /* Copyright 2002-2024 Luc Maisonobe
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS 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 java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.stream.Collectors;

  21. import org.hipparchus.util.FastMath;
  22. import org.orekit.propagation.SpacecraftState;
  23. import org.orekit.time.AbsoluteDate;

  24. /**
  25.  * This class wraps an object implementing {@link MultiSatFixedStepHandler}
  26.  * into a {@link MultiSatStepHandler}.

  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.  * @since 12.0
  32.  */
  33. public class MultisatStepNormalizer implements MultiSatStepHandler {

  34.     /** Fixed time step. */
  35.     private double h;

  36.     /** Underlying fixed step handler. */
  37.     private MultiSatFixedStepHandler handler;

  38.     /** Last State vectors. */
  39.     private List<SpacecraftState> lastStates;

  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 MultisatStepNormalizer(final double h, final MultiSatFixedStepHandler handler) {
  47.         this.h          = FastMath.abs(h);
  48.         this.handler    = handler;
  49.         this.lastStates = null;
  50.         this.forward    = true;
  51.     }

  52.     /** Get the fixed time step.
  53.      * @return fixed time step
  54.      */
  55.     public double getFixedTimeStep() {
  56.         return h;
  57.     }

  58.     /** Get the underlying fixed step handler.
  59.      * @return underlying fixed step handler
  60.      */
  61.     public MultiSatFixedStepHandler getFixedStepHandler() {
  62.         return handler;
  63.     }

  64.     /** {@inheritDoc} */
  65.     public void init(final List<SpacecraftState> s0, final AbsoluteDate t) {
  66.         lastStates = new ArrayList<>(s0);
  67.         forward    = true;
  68.         handler.init(s0, t, h);
  69.     }

  70.     /** {@inheritDoc} */
  71.     public void handleStep(final List<OrekitStepInterpolator> interpolators) {

  72.         if (lastStates == null) {
  73.             // initialize lastState in the first step case
  74.             lastStates = interpolators.stream().map(i -> i.getPreviousState()).collect(Collectors.toList());
  75.         }

  76.         // take the propagation direction into account
  77.         double step = h;
  78.         forward = interpolators.get(0).isForward();
  79.         if (!forward) {
  80.             step = -h;
  81.         }


  82.         // use the interpolator to push fixed steps events to the underlying handler
  83.         AbsoluteDate nextTime = lastStates.get(0).getDate().shiftedBy(step);
  84.         boolean nextInStep = forward ^ nextTime.compareTo(interpolators.get(0).getCurrentState().getDate()) > 0;
  85.         while (nextInStep) {

  86.             // output the stored previous step
  87.             handler.handleStep(lastStates);

  88.             // store the next step
  89.             final AbsoluteDate time = nextTime;
  90.             lastStates = interpolators.stream().map(i -> i.getInterpolatedState(time)).collect(Collectors.toList());

  91.             // prepare next iteration
  92.             nextTime = nextTime.shiftedBy(step);
  93.             nextInStep = forward ^ nextTime.compareTo(interpolators.get(0).getCurrentState().getDate()) > 0;

  94.         }

  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public void finish(final List<SpacecraftState> finalStates) {

  99.         // there will be no more steps,
  100.         // the stored one should be handled now
  101.         handler.handleStep(lastStates);

  102.         // and the final state handled too
  103.         handler.finish(finalStates);

  104.     }

  105. }