FieldCartesianEnergyConsideringMass.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.geometry.euclidean.threed.FieldVector3D;
  20. import org.orekit.propagation.FieldSpacecraftState;
  21. import org.orekit.propagation.events.FieldEventDetectionSettings;

  22. /**
  23.  * Abstract class for energy cost with Cartesian coordinates and non-zero mass flow rate.
  24.  * An energy cost is proportional to the integral over time of the squared Euclidean norm of the control vector, often scaled with 1/2.
  25.  * This type of cost is not optimal in terms of mass consumption, however its solutions showcase a smoother behavior favorable for convergence in shooting techniques.
  26.  *
  27.  * @param <T> field type
  28.  * @author Romain Serra
  29.  * @see FieldCartesianCost
  30.  * @see CartesianEnergyConsideringMass
  31.  * @since 13.0
  32.  */
  33. abstract class FieldCartesianEnergyConsideringMass<T extends CalculusFieldElement<T>> extends FieldAbstractCartesianCost<T> {

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

  36.     /**
  37.      * Constructor.
  38.      * @param name name
  39.      * @param massFlowRateFactor mass flow rate factor
  40.      * @param eventDetectionSettings settings for singularity detections
  41.      */
  42.     protected FieldCartesianEnergyConsideringMass(final String name, final T massFlowRateFactor,
  43.                                                   final FieldEventDetectionSettings<T> eventDetectionSettings) {
  44.         super(name, massFlowRateFactor);
  45.         this.eventDetectionSettings = eventDetectionSettings;
  46.     }

  47.     /**
  48.      * Getter for event detection settings.
  49.      * @return detection settings.
  50.      */
  51.     public FieldEventDetectionSettings<T> getEventDetectionSettings() {
  52.         return eventDetectionSettings;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public FieldVector3D<T> getFieldThrustAccelerationVector(final T[] adjointVariables, final T mass) {
  57.         return getFieldThrustDirection(adjointVariables).scalarMultiply(getFieldThrustForceNorm(adjointVariables, mass).divide(mass));
  58.     }

  59.     /**
  60.      * Computes the direction of thrust.
  61.      * @param adjointVariables adjoint vector
  62.      * @return thrust direction
  63.      */
  64.     protected FieldVector3D<T> getFieldThrustDirection(final T[] adjointVariables) {
  65.         return new FieldVector3D<>(adjointVariables[3], adjointVariables[4], adjointVariables[5]).normalize();
  66.     }

  67.     /**
  68.      * Computes the Euclidean norm of the thrust force.
  69.      * @param adjointVariables adjoint vector
  70.      * @param mass mass
  71.      * @return norm of thrust
  72.      */
  73.     protected abstract T getFieldThrustForceNorm(T[] adjointVariables, T mass);

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public void updateFieldAdjointDerivatives(final T[] adjointVariables, final T mass, final T[] adjointDerivatives) {
  77.         if (getAdjointDimension() > 6) {
  78.             adjointDerivatives[6] = adjointDerivatives[6].add(getFieldThrustForceNorm(adjointVariables, mass)
  79.                     .multiply(getFieldAdjointVelocityNorm(adjointVariables)).divide(mass.square()));
  80.         }
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public T getFieldHamiltonianContribution(final T[] adjointVariables, final T mass) {
  85.         final FieldVector3D<T> thrustForce = getFieldThrustAccelerationVector(adjointVariables, mass).scalarMultiply(mass);
  86.         return thrustForce.getNormSq().multiply(-1. / 2.);
  87.     }

  88.     /**
  89.      * Field event detector for singularities in adjoint dynamics.
  90.      */
  91.     class FieldSingularityDetector extends FieldControlSwitchDetector<T> {

  92.         /** Value to detect. */
  93.         private final T detectionValue;

  94.         /**
  95.          * Constructor.
  96.          * @param detectionSettings detection settings
  97.          * @param detectionValue value to detect
  98.          */
  99.         FieldSingularityDetector(final FieldEventDetectionSettings<T> detectionSettings, final T detectionValue) {
  100.             super(detectionSettings);
  101.             this.detectionValue = detectionValue;
  102.         }

  103.         /** {@inheritDoc} */
  104.         @Override
  105.         public T g(final FieldSpacecraftState<T> state) {
  106.             final T[] adjoint = state.getAdditionalState(getAdjointName());
  107.             return evaluateVariablePart(adjoint, state.getMass()).subtract(detectionValue);
  108.         }

  109.         /**
  110.          * Evaluate variable part of singularity function.
  111.          * @param adjointVariables adjoint vector
  112.          * @param mass mass
  113.          * @return singularity function without the constant part
  114.          */
  115.         private T evaluateVariablePart(final T[] adjointVariables, final T mass) {
  116.             final T adjointVelocityNorm = getFieldAdjointVelocityNorm(adjointVariables);
  117.             T variablePart = adjointVelocityNorm.divide(mass);
  118.             if (getAdjointDimension() > 6) {
  119.                 variablePart = variablePart.subtract(adjointVariables[6].multiply(getMassFlowRateFactor()));
  120.             }
  121.             return variablePart;
  122.         }

  123.     }
  124. }