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  
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.util.FastMath;
22  
23  /**
24   * Abstract class for cost with Cartesian coordinates.
25   *
26   * @author Romain Serra
27   * @see CartesianCost
28   * @since 13.0
29   */
30  public abstract class AbstractCartesianCost implements CartesianCost {
31  
32      /** Name of adjoint vector. */
33      private final String name;
34  
35      /** Mass flow rate factor (always positive). */
36      private final double massFlowRateFactor;
37  
38      /** Dimension of adjoint vector. */
39      private final int adjointDimension;
40  
41      /**
42       * Constructor.
43       * @param name name
44       * @param massFlowRateFactor mass flow rate factor
45       */
46      protected AbstractCartesianCost(final String name, final double massFlowRateFactor) {
47          this.name = name;
48          this.massFlowRateFactor = FastMath.abs(massFlowRateFactor);
49          this.adjointDimension = this.massFlowRateFactor == 0. ? 6 : 7;
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      public int getAdjointDimension() {
57          return adjointDimension;
58      }
59  
60      /**
61       * Getter for adjoint vector name.
62       * @return name
63       */
64      @Override
65      public String getAdjointName() {
66          return name;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public double getMassFlowRateFactor() {
72          return massFlowRateFactor;
73      }
74  
75      /**
76       * Computes the Euclidean norm of the adjoint velocity vector.
77       * @param adjointVariables adjoint vector
78       * @return norm of adjoint velocity
79       */
80      protected double getAdjointVelocityNorm(final double[] adjointVariables) {
81          return FastMath.sqrt(adjointVariables[3] * adjointVariables[3] + adjointVariables[4] * adjointVariables[4] + adjointVariables[5] * adjointVariables[5]);
82      }
83  
84      /**
85       * Computes the Euclidean norm of the adjoint velocity vector.
86       * @param adjointVariables adjoint vector
87       * @param <T> field type
88       * @return norm of adjoint velocity
89       */
90      protected <T extends CalculusFieldElement<T>> T getFieldAdjointVelocityNorm(final T[] adjointVariables) {
91          return FastMath.sqrt(adjointVariables[3].square().add(adjointVariables[4].square()).add(adjointVariables[5].square()));
92      }
93  }