FieldImpulseManeuver.java

  1. /* Copyright 2002-2024 Exotrail
  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.forces.maneuvers;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  20. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  21. import org.hipparchus.ode.events.Action;
  22. import org.hipparchus.util.FastMath;
  23. import org.orekit.attitudes.AttitudeProvider;
  24. import org.orekit.orbits.FieldCartesianOrbit;
  25. import org.orekit.propagation.FieldSpacecraftState;
  26. import org.orekit.propagation.events.FieldAbstractDetector;
  27. import org.orekit.propagation.events.FieldAdaptableInterval;
  28. import org.orekit.propagation.events.FieldEventDetector;
  29. import org.orekit.propagation.events.handlers.FieldEventHandler;
  30. import org.orekit.time.FieldAbsoluteDate;
  31. import org.orekit.utils.Constants;
  32. import org.orekit.utils.FieldArrayDictionary;
  33. import org.orekit.utils.FieldPVCoordinates;

  34. /** Impulse maneuver model for propagators working with Fields.
  35.  * <p>This class implements an impulse maneuver as a discrete event
  36.  * that can be provided to any {@link org.orekit.propagation.FieldPropagator
  37.  * Propagator} and mirrors the standard version
  38.  * {@link org.orekit.forces.maneuvers.ImpulseManeuver}.</p>
  39.  * <p>The maneuver is triggered when an underlying event generates a
  40.  * {@link Action#STOP STOP} event, in which case this class will generate a {@link
  41.  * Action#RESET_STATE RESET_STATE}
  42.  * event (the stop event from the underlying object is therefore filtered out).
  43.  * In the simple cases, the underlying event detector may be a basic
  44.  * {@link org.orekit.propagation.events.FieldDateDetector date event}, but it
  45.  * can also be a more elaborate {@link
  46.  * org.orekit.propagation.events.FieldApsideDetector apside event} for apogee
  47.  * maneuvers for example.</p>
  48.  * <p>The maneuver is defined by a single velocity increment.
  49.  * If no AttitudeProvider is given, the current attitude of the spacecraft,
  50.  * defined by the current spacecraft state, will be used as the
  51.  * {@link AttitudeProvider} so the velocity increment should be given in
  52.  * the same pseudoinertial frame as the {@link FieldSpacecraftState} used to
  53.  * construct the propagator that will handle the maneuver.
  54.  * If an AttitudeProvider is given, the velocity increment given should be
  55.  * defined appropriately in consideration of that provider. So, a typical
  56.  * case for tangential maneuvers is to provide a {@link org.orekit.attitudes.LofOffset LOF aligned}
  57.  * attitude provider along with a velocity increment defined in accordance with
  58.  * that LOF aligned attitude provider; e.g. if the LOF aligned attitude provider
  59.  * was constructed using LOFType.VNC the velocity increment should be
  60.  * provided in VNC coordinates.</p>
  61.  * <p>The norm through which the delta-V maps to the mass consumption is chosen via the
  62.  * enum {@link Control3DVectorCostType}. Default is Euclidean. </p>
  63.  * <p>Beware that the triggering event detector must behave properly both
  64.  * before and after maneuver. If for example a node detector is used to trigger
  65.  * an inclination maneuver and the maneuver change the orbit to an equatorial one,
  66.  * the node detector will fail just after the maneuver, being unable to find a
  67.  * node on an equatorial orbit! This is a real case that has been encountered
  68.  * during validation ...</p>
  69.  * @see org.orekit.propagation.FieldPropagator#addEventDetector(FieldEventDetector)
  70.  * @see org.orekit.forces.maneuvers.ImpulseManeuver
  71.  * @author Romain Serra
  72.  * @since 12.0
  73.  * @param <D> type of the detector
  74.  * @param <T> type of the field elements
  75.  */
  76. public class FieldImpulseManeuver<D extends FieldEventDetector<T>, T extends CalculusFieldElement<T>>
  77.         extends FieldAbstractDetector<FieldImpulseManeuver<D, T>, T> {

  78.     /** The attitude to override during the maneuver, if set. */
  79.     private final AttitudeProvider attitudeOverride;

  80.     /** Triggering event. */
  81.     private final D trigger;

  82.     /** Velocity increment in satellite frame. */
  83.     private final FieldVector3D<T> deltaVSat;

  84.     /** Specific impulse. */
  85.     private final T isp;

  86.     /** Engine exhaust velocity. */
  87.     private final T vExhaust;

  88.     /** Indicator for forward propagation. */
  89.     private boolean forward;

  90.     /** Type of norm linking delta-V to mass consumption. */
  91.     private final Control3DVectorCostType control3DVectorCostType;

  92.     /** Build a new instance.
  93.      * @param trigger triggering event
  94.      * @param deltaVSat velocity increment in satellite frame
  95.      * @param isp engine specific impulse (s)
  96.      */
  97.     public FieldImpulseManeuver(final D trigger, final FieldVector3D<T> deltaVSat, final T isp) {
  98.         this(trigger, null, deltaVSat, isp);
  99.     }

  100.     /** Build a new instance.
  101.      * @param trigger triggering event
  102.      * @param attitudeOverride the attitude provider to use for the maneuver
  103.      * @param deltaVSat velocity increment in satellite frame
  104.      * @param isp engine specific impulse (s)
  105.      */
  106.     public FieldImpulseManeuver(final D trigger, final AttitudeProvider attitudeOverride,
  107.                                 final FieldVector3D<T> deltaVSat, final T isp) {
  108.         this(trigger.getMaxCheckInterval(), trigger.getThreshold(), trigger.getMaxIterationCount(),
  109.                 new Handler<>(), trigger, attitudeOverride, deltaVSat, isp,
  110.                 Control3DVectorCostType.TWO_NORM);
  111.     }

  112.     /** Build a new instance.
  113.      * @param trigger triggering event
  114.      * @param attitudeOverride the attitude provider to use for the maneuver
  115.      * @param deltaVSat velocity increment in satellite frame
  116.      * @param isp engine specific impulse (s)
  117.      * @param control3DVectorCostType increment's norm for mass consumption
  118.      */
  119.     public FieldImpulseManeuver(final D trigger, final AttitudeProvider attitudeOverride,
  120.                                 final FieldVector3D<T> deltaVSat, final T isp,
  121.                                 final Control3DVectorCostType control3DVectorCostType) {
  122.         this(trigger.getMaxCheckInterval(), trigger.getThreshold(), trigger.getMaxIterationCount(),
  123.                 new Handler<>(), trigger, attitudeOverride, deltaVSat, isp, control3DVectorCostType);
  124.     }

  125.     /** Private constructor with full parameters.
  126.      * <p>
  127.      * This constructor is private as users are expected to use the builder
  128.      * API with the various {@code withXxx()} methods to set up the instance
  129.      * in a readable manner without using a huge amount of parameters.
  130.      * </p>
  131.      * @param maxCheck maximum checking interval
  132.      * @param threshold convergence threshold (s)
  133.      * @param maxIter maximum number of iterations in the event time search
  134.      * @param eventHandler event handler to call at event occurrences
  135.      * @param trigger triggering event
  136.      * @param attitudeOverride the attitude provider to use for the maneuver
  137.      * @param deltaVSat velocity increment in satellite frame
  138.      * @param isp engine specific impulse (s)
  139.      * @param control3DVectorCostType increment's norm for mass consumption
  140.      */
  141.     private FieldImpulseManeuver(final FieldAdaptableInterval<T> maxCheck, final T threshold, final int maxIter,
  142.                                  final FieldEventHandler<T> eventHandler, final D trigger,
  143.                                  final AttitudeProvider attitudeOverride, final FieldVector3D<T> deltaVSat,
  144.                                  final T isp, final Control3DVectorCostType control3DVectorCostType) {
  145.         super(maxCheck, threshold, maxIter, eventHandler);
  146.         this.trigger = trigger;
  147.         this.deltaVSat = deltaVSat;
  148.         this.isp = isp;
  149.         this.attitudeOverride = attitudeOverride;
  150.         this.control3DVectorCostType = control3DVectorCostType;
  151.         this.vExhaust = this.isp.multiply(Constants.G0_STANDARD_GRAVITY);
  152.     }

  153.     /** {@inheritDoc} */
  154.     @Override
  155.     protected FieldImpulseManeuver<D, T> create(final FieldAdaptableInterval<T> newMaxCheck, final T newThreshold,
  156.                                                 final int newMaxIter,
  157.                                                 final FieldEventHandler<T> fieldEventHandler) {
  158.         return new FieldImpulseManeuver<>(newMaxCheck, newThreshold, newMaxIter, fieldEventHandler,
  159.                 trigger, attitudeOverride, deltaVSat, isp, control3DVectorCostType);
  160.     }

  161.     /** {@inheritDoc} */
  162.     @Override
  163.     public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
  164.         forward = t.durationFrom(s0.getDate()).getReal() >= 0;
  165.         // Initialize the triggering event
  166.         trigger.init(s0, t);
  167.     }

  168.     /** {@inheritDoc} */
  169.     @Override
  170.     public T g(final FieldSpacecraftState<T> fieldSpacecraftState) {
  171.         return trigger.g(fieldSpacecraftState);
  172.     }

  173.     /**
  174.      * Get the Attitude Provider to use during maneuver.
  175.      * @return the attitude provider
  176.      */
  177.     public AttitudeProvider getAttitudeOverride() {
  178.         return attitudeOverride;
  179.     }

  180.     /** Get the triggering event.
  181.      * @return triggering event
  182.      */
  183.     public FieldEventDetector<T> getTrigger() {
  184.         return trigger;
  185.     }

  186.     /** Get the velocity increment in satellite frame.
  187.      * @return velocity increment in satellite frame
  188.      */
  189.     public FieldVector3D<T> getDeltaVSat() {
  190.         return deltaVSat;
  191.     }

  192.     /** Get the specific impulse.
  193.      * @return specific impulse
  194.      */
  195.     public T getIsp() {
  196.         return isp;
  197.     }

  198.     /** Get the control vector's cost type.
  199.      * @return control cost type
  200.      * @since 12.0
  201.      */
  202.     public Control3DVectorCostType getControl3DVectorCostType() {
  203.         return control3DVectorCostType;
  204.     }

  205.     /** Local handler. */
  206.     private static class Handler<T extends CalculusFieldElement<T>> implements FieldEventHandler<T> {

  207.         /** {@inheritDoc} */
  208.         @Override
  209.         public Action eventOccurred(final FieldSpacecraftState<T> s,
  210.                                     final FieldEventDetector<T> detector,
  211.                                     final boolean increasing) {
  212.             // filter underlying event
  213.             @SuppressWarnings("unchecked")
  214.             final FieldImpulseManeuver<?, T> im = (FieldImpulseManeuver<?, T>) detector;
  215.             final Action underlyingAction = im.trigger.getHandler().eventOccurred(s, im.trigger,
  216.                     increasing);

  217.             return (underlyingAction == Action.STOP) ? Action.RESET_STATE : Action.CONTINUE;
  218.         }

  219.         /** {@inheritDoc} */
  220.         @Override
  221.         public FieldSpacecraftState<T> resetState(final FieldEventDetector<T> detector,
  222.                                                   final FieldSpacecraftState<T> oldState) {

  223.             @SuppressWarnings("unchecked")
  224.             final FieldImpulseManeuver<?, T> im = (FieldImpulseManeuver<?, T>) detector;
  225.             final FieldAbsoluteDate<T> date = oldState.getDate();
  226.             final FieldRotation<T> rotation;

  227.             if (im.getAttitudeOverride() == null) {
  228.                 rotation = oldState.getAttitude().getRotation();
  229.             } else {
  230.                 rotation = im.attitudeOverride.getAttitudeRotation(oldState.getOrbit(), date,
  231.                         oldState.getFrame());
  232.             }

  233.             // convert velocity increment in inertial frame
  234.             final FieldVector3D<T> deltaV = rotation.applyInverseTo(im.deltaVSat);
  235.             final T one = oldState.getMu().getField().getOne();
  236.             final T sign = (im.forward) ? one : one.negate();

  237.             // apply increment to position/velocity
  238.             final FieldPVCoordinates<T> oldPV = oldState.getPVCoordinates();
  239.             final FieldPVCoordinates<T> newPV =
  240.                     new FieldPVCoordinates<>(oldPV.getPosition(),
  241.                             new FieldVector3D<>(one, oldPV.getVelocity(), sign, deltaV));
  242.             final FieldCartesianOrbit<T> newOrbit =
  243.                     new FieldCartesianOrbit<>(newPV, oldState.getFrame(), date, oldState.getMu());

  244.             // compute new mass
  245.             final T normDeltaV = im.control3DVectorCostType.evaluate(im.deltaVSat);
  246.             final T newMass = oldState.getMass().multiply(FastMath.exp(normDeltaV.multiply(sign.negate()).divide(im.vExhaust)));

  247.             // pack everything in a new state
  248.             FieldSpacecraftState<T> newState = new FieldSpacecraftState<>(oldState.getOrbit().getType().normalize(newOrbit, oldState.getOrbit()),
  249.                     oldState.getAttitude(), newMass);

  250.             for (final FieldArrayDictionary<T>.Entry entry : oldState.getAdditionalStatesValues().getData()) {
  251.                 newState = newState.addAdditionalState(entry.getKey(), entry.getValue());
  252.             }
  253.             for (final FieldArrayDictionary<T>.Entry entry : oldState.getAdditionalStatesDerivatives().getData()) {
  254.                 newState = newState.addAdditionalStateDerivative(entry.getKey(), entry.getValue());
  255.             }

  256.             return newState;
  257.         }

  258.     }
  259. }