FieldCartesianFuelCost.java

  1. /* Copyright 2022-2025 Romain Serra
  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.control.indirect.adjoint.cost;


  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  21. import org.orekit.propagation.FieldSpacecraftState;
  22. import org.orekit.propagation.events.EventDetectionSettings;
  23. import org.orekit.propagation.events.FieldEventDetectionSettings;
  24. import org.orekit.propagation.events.FieldEventDetector;

  25. import java.util.stream.Stream;

  26. /**
  27.  * Class for fuel cost with Cartesian coordinates.
  28.  * It is the integral over time of the Euclidean norm of the thrust vector.
  29.  *
  30.  * @author Romain Serra
  31.  * @see CartesianCost
  32.  * @since 13.0
  33.  */
  34. public class FieldCartesianFuelCost<T extends CalculusFieldElement<T>> extends FieldAbstractCartesianCost<T> {

  35.     /** Maximum value of thrust force Euclidean norm. */
  36.     private final T maximumThrustMagnitude;

  37.     /** Detection settings for singularity detection. */
  38.     private final FieldEventDetectionSettings<T> eventDetectionSettings;

  39.     /**
  40.      * Constructor with default detection settings.
  41.      * @param name name
  42.      * @param massFlowRateFactor mass flow rate factor
  43.      * @param maximumThrustMagnitude maximum thrust magnitude
  44.      */
  45.     public FieldCartesianFuelCost(final String name, final T massFlowRateFactor, final T maximumThrustMagnitude) {
  46.         this(name, massFlowRateFactor, maximumThrustMagnitude, new FieldEventDetectionSettings<>(massFlowRateFactor.getField(),
  47.                 EventDetectionSettings.getDefaultEventDetectionSettings()));
  48.     }

  49.     /**
  50.      * Constructor.
  51.      * @param name name
  52.      * @param massFlowRateFactor mass flow rate factor
  53.      * @param maximumThrustMagnitude maximum thrust magnitude
  54.      * @param eventDetectionSettings singularity event detection settings
  55.      */
  56.     public FieldCartesianFuelCost(final String name, final T massFlowRateFactor, final T maximumThrustMagnitude,
  57.                                   final FieldEventDetectionSettings<T> eventDetectionSettings) {
  58.         super(name, massFlowRateFactor);
  59.         this.maximumThrustMagnitude = maximumThrustMagnitude;
  60.         this.eventDetectionSettings = eventDetectionSettings;
  61.     }

  62.     /**
  63.      * Getter for event detection settings.
  64.      * @return detection settings.
  65.      */
  66.     public FieldEventDetectionSettings<T> getEventDetectionSettings() {
  67.         return eventDetectionSettings;
  68.     }

  69.     /** Getter for maximum thrust magnitude.
  70.      * @return maximum thrust
  71.      */
  72.     public T getMaximumThrustMagnitude() {
  73.         return maximumThrustMagnitude;
  74.     }

  75.     /**
  76.      * Evaluate switching function (whose sign determines the bang-bang control profile).
  77.      * @param adjointVariables adjoint vector
  78.      * @param mass mass
  79.      * @return value of switch function
  80.      */
  81.     private T evaluateFieldSwitchFunction(final T[] adjointVariables, final T mass) {
  82.         T switchFunction = getFieldAdjointVelocityNorm(adjointVariables).divide(mass).subtract(1.);
  83.         if (getAdjointDimension() > 6) {
  84.             switchFunction = switchFunction.subtract(adjointVariables[6].multiply(getMassFlowRateFactor()));
  85.         }
  86.         return switchFunction;
  87.     }

  88.     /**
  89.      * Computes the direction of thrust.
  90.      * @param adjointVariables adjoint vector
  91.      * @return thrust direction
  92.      */
  93.     private FieldVector3D<T> getFieldThrustDirection(final T[] adjointVariables) {
  94.         return new FieldVector3D<>(adjointVariables[3], adjointVariables[4], adjointVariables[5]).normalize();
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public FieldVector3D<T> getFieldThrustAccelerationVector(final T[] adjointVariables, final T mass) {
  99.         final T switchFunction = evaluateFieldSwitchFunction(adjointVariables, mass);
  100.         if (switchFunction.getReal() > 0.) {
  101.             return getFieldThrustDirection(adjointVariables).scalarMultiply(mass.reciprocal().multiply(maximumThrustMagnitude));
  102.         } else {
  103.             return FieldVector3D.getZero(mass.getField());
  104.         }
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     public void updateFieldAdjointDerivatives(final T[] adjointVariables, final T mass,
  109.                                               final T[] adjointDerivatives) {
  110.         if (getAdjointDimension() > 6) {
  111.             final T switchFunction = evaluateFieldSwitchFunction(adjointVariables, mass);
  112.             if (switchFunction.getReal() > 0.) {
  113.                 adjointDerivatives[6] = adjointDerivatives[6].add(getFieldAdjointVelocityNorm(adjointVariables)
  114.                         .multiply(maximumThrustMagnitude).divide(mass.square()));
  115.             }
  116.         }
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public T getFieldHamiltonianContribution(final T[] adjointVariables, final T mass) {
  121.         final FieldVector3D<T> thrustForce = getFieldThrustAccelerationVector(adjointVariables, mass).scalarMultiply(mass);
  122.         return thrustForce.getNorm().negate();
  123.     }

  124.     /** {@inheritDoc} */
  125.     @Override
  126.     public Stream<FieldEventDetector<T>> getFieldEventDetectors(final Field<T> field) {
  127.         return Stream.of(new FieldSwitchDetector(getEventDetectionSettings()));
  128.     }

  129.     @Override
  130.     public CartesianFuelCost toCartesianCost() {
  131.         return new CartesianFuelCost(getAdjointName(), getMassFlowRateFactor().getReal(), maximumThrustMagnitude.getReal(),
  132.                 getEventDetectionSettings().toEventDetectionSettings());
  133.     }

  134.     /**
  135.      * Field event detector for bang-bang switches.
  136.      */
  137.     class FieldSwitchDetector extends FieldControlSwitchDetector<T> {

  138.         FieldSwitchDetector(final FieldEventDetectionSettings<T> detectionSettings) {
  139.             super(detectionSettings);
  140.         }

  141.         /** {@inheritDoc} */
  142.         @Override
  143.         public T g(final FieldSpacecraftState<T> state) {
  144.             final T[] adjoint = state.getAdditionalState(getAdjointName());
  145.             return evaluateFieldSwitchFunction(adjoint, state.getMass());
  146.         }

  147.     }
  148. }