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  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.util.FastMath;
22  import org.orekit.propagation.events.EventDetectionSettings;
23  import org.orekit.propagation.events.FieldEventDetectionSettings;
24  import org.orekit.propagation.events.FieldEventDetector;
25  
26  import java.util.stream.Stream;
27  
28  /**
29   * Class for bounded energy cost with Cartesian coordinates.
30   * An energy cost is proportional to the integral over time of the squared Euclidean norm of the control vector, often scaled with 1/2.
31   * 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.
32   * Here, the control vector is chosen as the thrust force divided by the maximum thrust magnitude and expressed in the propagation frame.
33   *
34   * @param <T> field type
35   * @author Romain Serra
36   * @see FieldUnboundedCartesianEnergy
37   * @see BoundedCartesianEnergy
38   * @since 13.0
39   */
40  public class FieldBoundedCartesianEnergy<T extends CalculusFieldElement<T>> extends FieldCartesianEnergyConsideringMass<T> {
41  
42      /** Maximum value of thrust force Euclidean norm. */
43      private final T maximumThrustMagnitude;
44  
45      /**
46       * Constructor.
47       * @param name name
48       * @param massFlowRateFactor mass flow rate factor
49       * @param maximumThrustMagnitude maximum thrust magnitude
50       * @param eventDetectionSettings singularity event detection settings
51       */
52      public FieldBoundedCartesianEnergy(final String name, final T massFlowRateFactor,
53                                         final T maximumThrustMagnitude,
54                                         final FieldEventDetectionSettings<T> eventDetectionSettings) {
55          super(name, massFlowRateFactor, eventDetectionSettings);
56          this.maximumThrustMagnitude = FastMath.abs(maximumThrustMagnitude);
57      }
58  
59      /**
60       * Constructor.
61       * @param name name
62       * @param massFlowRateFactor mass flow rate factor
63       * @param maximumThrustMagnitude maximum thrust magnitude
64       */
65      public FieldBoundedCartesianEnergy(final String name, final T massFlowRateFactor,
66                                         final T maximumThrustMagnitude) {
67          this(name, massFlowRateFactor, maximumThrustMagnitude, new FieldEventDetectionSettings<>(massFlowRateFactor.getField(),
68                  EventDetectionSettings.getDefaultEventDetectionSettings()));
69      }
70  
71      /** Getter for maximum thrust magnitude.
72       * @return maximum thrust
73       */
74      public T getMaximumThrustMagnitude() {
75          return maximumThrustMagnitude;
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      protected T getFieldThrustForceNorm(final T[] adjointVariables, final T mass) {
81          final T adjointVelocityNorm = getFieldAdjointVelocityNorm(adjointVariables);
82          T factor = adjointVelocityNorm.divide(mass);
83          if (getAdjointDimension() > 6) {
84              factor = factor.subtract(adjointVariables[6].multiply(getMassFlowRateFactor()));
85          }
86          final double factorReal = factor.getReal();
87          final T zero = mass.getField().getZero();
88          if (factorReal > maximumThrustMagnitude.getReal()) {
89              return maximumThrustMagnitude;
90          } else if (factorReal < 0.) {
91              return zero;
92          } else {
93              return factor;
94          }
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public Stream<FieldEventDetector<T>> getFieldEventDetectors(final Field<T> field) {
100         final T zero = field.getZero();
101         return Stream.of(new FieldSingularityDetector(getEventDetectionSettings(), zero),
102                 new FieldSingularityDetector(getEventDetectionSettings(), maximumThrustMagnitude));
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public BoundedCartesianEnergy toCartesianCost() {
108         return new BoundedCartesianEnergy(getAdjointName(), getMassFlowRateFactor().getReal(),
109                 maximumThrustMagnitude.getReal(), getEventDetectionSettings().toEventDetectionSettings());
110     }
111 
112 }