1   /* Copyright 2022-2026 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 java.util.function.Function;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.util.FastMath;
24  import org.orekit.propagation.FieldSpacecraftState;
25  import org.orekit.propagation.events.FieldEventDetectionSettings;
26  import org.orekit.propagation.events.FieldEventDetector;
27  import org.orekit.propagation.events.functions.EventFunction;
28  import org.orekit.propagation.events.functions.EventFunctionModifier;
29  import org.orekit.propagation.events.handlers.FieldResetDerivativesOnEvent;
30  
31  /**
32   * Abstract class for cost with Cartesian coordinates.
33   *
34   * @param <T> field type
35   * @author Romain Serra
36   * @see CartesianCost
37   * @since 13.0
38   */
39  public abstract class FieldAbstractCartesianCost<T extends CalculusFieldElement<T>> implements FieldCartesianCost<T> {
40  
41      /** Name of adjoint vector. */
42      private final String name;
43  
44      /** Mass flow rate factor (always positive). */
45      private final T massFlowRateFactor;
46  
47      /** Dimension of adjoint vector. */
48      private final int adjointDimension;
49  
50      /**
51       * Constructor.
52       * @param name name
53       * @param massFlowRateFactor mass flow rate factor
54       */
55      protected FieldAbstractCartesianCost(final String name, final T massFlowRateFactor) {
56          this.name = name;
57          this.massFlowRateFactor = FastMath.abs(massFlowRateFactor);
58          this.adjointDimension = this.massFlowRateFactor.isZero() ? 6 : 7;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public int getAdjointDimension() {
64          return adjointDimension;
65      }
66  
67      /**
68       * Getter for adjoint vector name.
69       * @return name
70       */
71      @Override
72      public String getAdjointName() {
73          return name;
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public T getMassFlowRateFactor() {
79          return massFlowRateFactor;
80      }
81  
82      /**
83       * Computes the Euclidean norm of the adjoint velocity vector.
84       * @param adjointVariables adjoint vector
85       * @return norm of adjoint velocity
86       */
87      protected T getFieldAdjointVelocityNorm(final T[] adjointVariables) {
88          return FastMath.sqrt(adjointVariables[3].square().add(adjointVariables[4].square()).add(adjointVariables[5].square()));
89      }
90  
91      /**
92       * Build event detector.
93       * @param eventFunction event function
94       * @param fieldEventDetectionSettings detection settings
95       * @return event detector
96       * @since 14.0
97       */
98      protected FieldEventDetector<T> buildSwitchDetector(final FieldSwitchFunction eventFunction,
99                                                          final FieldEventDetectionSettings<T> fieldEventDetectionSettings) {
100         return FieldEventDetector.of(eventFunction, new FieldResetDerivativesOnEvent<>(), fieldEventDetectionSettings);
101     }
102 
103     /**
104      * Class for control switch function involving Field.
105      */
106     public class FieldSwitchFunction implements EventFunctionModifier {
107 
108         /** Wrapped event function. */
109         private final EventFunction baseFunction;
110 
111         protected FieldSwitchFunction(final Function<FieldSpacecraftState<T>, T> fieldFunction) {
112             this.baseFunction = EventFunction.of(getMassFlowRateFactor().getField(), fieldFunction);
113         }
114 
115         @Override
116         public EventFunction getBaseFunction() {
117             return baseFunction;
118         }
119 
120         @Override
121         public boolean dependsOnMainVariablesOnly() {
122             return false;
123         }
124     }
125 }