CartesianCost.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.geometry.euclidean.threed.Vector3D;
  19. import org.orekit.control.indirect.adjoint.CartesianAdjointDerivativesProvider;
  20. import org.orekit.propagation.SpacecraftState;
  21. import org.orekit.propagation.events.EventDetector;
  22. import org.orekit.propagation.integration.AdditionalDerivativesProvider;
  23. import org.orekit.propagation.integration.CombinedDerivatives;

  24. import java.util.stream.Stream;

  25. /**
  26.  * Interface to definite cost function in the frame of Pontryagin's Maximum Principle using Cartesian coordinates.
  27.  * It provides the link between the optimal control and the adjoint variables. This relationship is obtained by maximizing the Hamiltonian.
  28.  * The choice of control vector impacts on it.
  29.  * Both standard (double type) and (Calculus)Field versions are to be implemented by inheritors.
  30.  * @author Romain Serra
  31.  * @see CartesianAdjointDerivativesProvider
  32.  * @since 12.2
  33.  */
  34. public interface CartesianCost {

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

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

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

  48.     /**
  49.      * Computes the thrust acceleration vector in propagation frame from the adjoint variables and the mass.
  50.      * @param adjointVariables adjoint vector
  51.      * @param mass mass
  52.      * @return thrust vector
  53.      */
  54.     Vector3D getThrustAccelerationVector(double[] adjointVariables, double mass);

  55.     /**
  56.      * Update the adjoint derivatives if necessary.
  57.      *
  58.      * @param adjointVariables   adjoint vector
  59.      * @param mass               mass
  60.      * @param adjointDerivatives derivatives to update
  61.      */
  62.     void updateAdjointDerivatives(double[] adjointVariables, double mass, double[] adjointDerivatives);

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

  71.     /**
  72.      * Get the detectors needed for propagation.
  73.      * @return event detectors
  74.      */
  75.     default Stream<EventDetector> getEventDetectors() {
  76.         return Stream.of();
  77.     }

  78.     /**
  79.      * Get the derivatives provider to be able to integrate the cost function.
  80.      * @param name name of cost as additional state variable
  81.      * @return derivatives provider
  82.      * @since 13.0
  83.      */
  84.     default AdditionalDerivativesProvider getCostDerivativeProvider(final String name) {
  85.         return new AdditionalDerivativesProvider() {
  86.             @Override
  87.             public String getName() {
  88.                 return name;
  89.             }

  90.             @Override
  91.             public int getDimension() {
  92.                 return 1;
  93.             }

  94.             @Override
  95.             public boolean yields(final SpacecraftState state) {
  96.                 return !state.hasAdditionalData(getAdjointName());
  97.             }

  98.             @Override
  99.             public CombinedDerivatives combinedDerivatives(final SpacecraftState s) {
  100.                 final double[] adjoint = s.getAdditionalState(getAdjointName());
  101.                 final double hamiltonianContribution = getHamiltonianContribution(adjoint, s.getMass());
  102.                 return new CombinedDerivatives(new double[] { -hamiltonianContribution }, null);
  103.             }
  104.         };
  105.     }
  106. }