FieldCartesianCost.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.hipparchus.util.MathArrays;
  22. import org.orekit.control.indirect.adjoint.CartesianAdjointDerivativesProvider;
  23. import org.orekit.propagation.FieldSpacecraftState;
  24. import org.orekit.propagation.events.FieldEventDetector;
  25. import org.orekit.propagation.integration.FieldAdditionalDerivativesProvider;
  26. import org.orekit.propagation.integration.FieldCombinedDerivatives;

  27. import java.util.stream.Stream;

  28. /**
  29.  * Interface to definite cost function in the frame of Pontryagin's Maximum Principle using Cartesian coordinates.
  30.  * It provides the link between the optimal control and the adjoint variables. This relationship is obtained by maximizing the Hamiltonian.
  31.  * The choice of control vector impacts on it.
  32.  * @author Romain Serra
  33.  * @see CartesianAdjointDerivativesProvider
  34.  * @since 13.0
  35.  */
  36. public interface FieldCartesianCost<T extends CalculusFieldElement<T>> {

  37.     /** Getter for adjoint vector name.
  38.      * @return adjoint vector name
  39.      */
  40.     String getAdjointName();

  41.     /** Getter for adjoint vector dimension.
  42.      * @return adjoint dimension
  43.      */
  44.     int getAdjointDimension();

  45.     /** Getter for mass flow rate factor. It is negated and multiplied by the thrust force magnitude to obtain the mass time derivative.
  46.      * The fact that it is a constant means that the exhaust speed is assumed to be independent of time.
  47.      * @return mass flow rate factor
  48.      */
  49.     T getMassFlowRateFactor();

  50.     /**
  51.      * Computes the thrust acceleration vector in propagation frame from the adjoint variables and the mass.
  52.      * @param adjointVariables adjoint vector
  53.      * @param mass mass
  54.      * @return thrust vector
  55.      */
  56.     FieldVector3D<T> getFieldThrustAccelerationVector(T[] adjointVariables, T mass);

  57.     /**
  58.      * Update the adjoint derivatives if necessary.
  59.      *
  60.      * @param adjointVariables   adjoint vector
  61.      * @param mass               mass
  62.      * @param adjointDerivatives derivatives to update
  63.      */
  64.     void updateFieldAdjointDerivatives(T[] adjointVariables, T mass, T[] adjointDerivatives);

  65.     /**
  66.      * Computes the Hamiltonian contribution to the cost function.
  67.      * It equals the Lagrange-form integrand multiplied by -1.
  68.      * @param adjointVariables adjoint vector
  69.      * @param mass mass
  70.      * @return contribution to Hamiltonian
  71.      */
  72.     T getFieldHamiltonianContribution(T[] adjointVariables, T mass);

  73.     /**
  74.      * Get the detectors needed for propagation.
  75.      * @param field field
  76.      * @return event detectors
  77.      */
  78.     default Stream<FieldEventDetector<T>> getFieldEventDetectors(final Field<T> field) {
  79.         return Stream.of();
  80.     }

  81.     /**
  82.      * Get the derivatives provider to be able to integrate the cost function.
  83.      * @param name name of cost as additional state variable
  84.      * @return derivatives provider
  85.      * @since 13.0
  86.      */
  87.     default FieldAdditionalDerivativesProvider<T> getCostDerivativeProvider(final String name) {
  88.         return new FieldAdditionalDerivativesProvider<T>() {

  89.             @Override
  90.             public String getName() {
  91.                 return name;
  92.             }

  93.             @Override
  94.             public int getDimension() {
  95.                 return 1;
  96.             }

  97.             @Override
  98.             public boolean yields(final FieldSpacecraftState<T> state) {
  99.                 return !state.hasAdditionalData(getAdjointName());
  100.             }

  101.             @Override
  102.             public FieldCombinedDerivatives<T> combinedDerivatives(final FieldSpacecraftState<T> s) {
  103.                 final T mass = s.getMass();
  104.                 final T[] derivatives = MathArrays.buildArray(mass.getField(), 1);
  105.                 final T[] adjoint = s.getAdditionalState(getAdjointName());
  106.                 final T hamiltonianContribution = getFieldHamiltonianContribution(adjoint, s.getMass());
  107.                 derivatives[0] = hamiltonianContribution.negate();
  108.                 return new FieldCombinedDerivatives<>(derivatives, null);
  109.             }
  110.         };
  111.     }

  112.     /**
  113.      * Method returning equivalent in non-Field.
  114.      * @return cost function for non-Field applications
  115.      */
  116.     CartesianCost toCartesianCost();
  117. }