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.geometry.euclidean.threed.FieldVector3D;
20  import org.hipparchus.util.Binary64;
21  import org.hipparchus.util.Binary64Field;
22  import org.hipparchus.util.MathArrays;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.ValueSource;
27  
28  import java.util.Arrays;
29  
30  class FieldCartesianFlightDurationCostTest {
31  
32      private static final String ADJOINT_NAME = "adjoint";
33  
34      @Test
35      void testToCartesianCost() {
36          // GIVEN
37          final FieldCartesianFlightDurationCost<Binary64> fieldCost = new FieldCartesianFlightDurationCost<>(ADJOINT_NAME, Binary64.ONE,
38                  Binary64.PI);
39          // WHEN
40          final CartesianFlightDurationCost cost = fieldCost.toCartesianCost();
41          // THEN
42          Assertions.assertEquals(cost.getAdjointDimension(), fieldCost.getAdjointDimension());
43          Assertions.assertEquals(cost.getAdjointName(), fieldCost.getAdjointName());
44          Assertions.assertEquals(cost.getMaximumThrustMagnitude(), fieldCost.getMaximumThrustMagnitude().getReal());
45          Assertions.assertEquals(cost.getMassFlowRateFactor(), fieldCost.getMassFlowRateFactor().getReal());
46      }
47  
48      @Test
49      void testGetHamiltonianContribution() {
50          // GIVEN
51          final FieldCartesianFlightDurationCost<Binary64> cost = new FieldCartesianFlightDurationCost<>(ADJOINT_NAME, Binary64.ONE,
52                  Binary64.PI);
53          // WHEN
54          final Binary64 contribution = cost.getFieldHamiltonianContribution(null, Binary64.ZERO);
55          // THEN
56          Assertions.assertEquals(Binary64.ONE.negate(), contribution);
57      }
58  
59      @ParameterizedTest
60      @ValueSource(booleans = {false, true})
61      void testUpdateFieldAdjointDerivatives(final boolean withMass) {
62          // GIVEN
63          final Binary64 massFlowRateFactor = withMass ? Binary64.ONE : Binary64.ZERO;
64          final FieldCartesianFlightDurationCost<Binary64> cost = new FieldCartesianFlightDurationCost<>(ADJOINT_NAME, massFlowRateFactor, Binary64.PI);
65          final Binary64[] adjoint = MathArrays.buildArray(Binary64Field.getInstance(), withMass ? 7 : 6);
66          final Binary64[] derivatives = adjoint.clone();
67          adjoint[3] = Binary64.ONE;
68          // WHEN
69          cost.updateFieldAdjointDerivatives(adjoint, Binary64.ONE, derivatives);
70          // THEN
71          final Binary64 zero = Binary64.ZERO;
72          for (int i = 0; i < 6; ++i) {
73              Assertions.assertEquals(zero, derivatives[i]);
74          }
75          if (withMass) {
76              Assertions.assertNotEquals(zero, derivatives[derivatives.length - 1]);
77          } else {
78              Assertions.assertEquals(zero, derivatives[derivatives.length - 1]);
79          }
80      }
81  
82      @Test
83      void testGetThrustAccelerationVector() {
84          // GIVEN
85          final FieldCartesianFlightDurationCost<Binary64> cost = new FieldCartesianFlightDurationCost<>(ADJOINT_NAME, Binary64.ONE, Binary64.PI);
86          // WHEN
87          final Binary64[] adjoint = MathArrays.buildArray(Binary64Field.getInstance(), 6);
88          adjoint[3] = new Binary64(1);
89          adjoint[4] = new Binary64(2);
90          adjoint[5] = new Binary64(3);
91          final FieldVector3D<Binary64> contribution = cost.getFieldThrustAccelerationVector(adjoint, Binary64.ZERO);
92          // THEN
93          final FieldVector3D<Binary64> fieldVector3D = new FieldVector3D<>(Arrays.copyOfRange(adjoint, 3, 6));
94          Assertions.assertEquals(fieldVector3D.normalize().scalarMultiply(cost.getMaximumThrustMagnitude()),
95                  contribution);
96      }
97  }