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.Vector3D;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  import org.junit.jupiter.params.ParameterizedTest;
23  import org.junit.jupiter.params.provider.ValueSource;
24  import org.mockito.Mockito;
25  import org.orekit.TestUtils;
26  import org.orekit.propagation.SpacecraftState;
27  import org.orekit.propagation.integration.AdditionalDerivativesProvider;
28  import org.orekit.propagation.integration.CombinedDerivatives;
29  import org.orekit.time.AbsoluteDate;
30  
31  class CartesianCostTest {
32  
33      @Test
34      void getEventDetectorsTest() {
35          // GIVEN
36          final TestCost cost = new TestCost();
37          // WHEN & THEN
38          Assertions.assertEquals(0., cost.getEventDetectors().count());
39      }
40  
41      @ParameterizedTest
42      @ValueSource(booleans = {true, false})
43      void getCostDerivativeProviderTest(final boolean yields) {
44          // GIVEN
45          final TestCost cost = new TestCost();
46          final String expectedName = "a";
47          final SpacecraftState mockedState = Mockito.mock();
48          final String adjointName = cost.getAdjointName();
49          Mockito.when(mockedState.hasAdditionalData(adjointName)).thenReturn(yields);
50          // WHEN
51          final AdditionalDerivativesProvider costDerivative = cost.getCostDerivativeProvider(expectedName);
52          // THEN
53          Assertions.assertEquals(expectedName, costDerivative.getName());
54          Assertions.assertEquals(1, costDerivative.getDimension());
55          Assertions.assertNotEquals(yields, costDerivative.yields(mockedState));
56      }
57  
58      @Test
59      void getCostDerivativeProviderCombinedDerivativesTest() {
60          // GIVEN
61          final CartesianCost cost = new TestCost();
62          final String name = "a";
63          final double[] adjoint = new double[] {1, 2, 3, 4, 5, 6};
64          final SpacecraftState state = new SpacecraftState(TestUtils.getDefaultOrbit(AbsoluteDate.ARBITRARY_EPOCH))
65                  .addAdditionalData(cost.getAdjointName(), adjoint);
66          // WHEN
67          final AdditionalDerivativesProvider costDerivative = cost.getCostDerivativeProvider(name);
68          // THEN
69          final CombinedDerivatives combinedDerivatives = costDerivative.combinedDerivatives(state);
70          Assertions.assertNull(combinedDerivatives.getMainStateDerivativesIncrements());
71          Assertions.assertEquals(1, combinedDerivatives.getAdditionalDerivatives()[0]);
72      }
73  
74      private static class TestCost implements CartesianCost {
75  
76          @Override
77          public String getAdjointName() {
78              return "adjoint";
79          }
80  
81          @Override
82          public int getAdjointDimension() {
83              return 6;
84          }
85  
86          @Override
87          public double getMassFlowRateFactor() {
88              return 0;
89          }
90  
91          @Override
92          public Vector3D getThrustAccelerationVector(final double[] adjointVariables, final double mass) {
93              return null;
94          }
95  
96          @Override
97          public void updateAdjointDerivatives(final double[] adjointVariables, final double mass,
98                                               final double[] adjointDerivatives) {
99  
100         }
101 
102         @Override
103         public double getHamiltonianContribution(final double[] adjointVariables, final double mass) {
104             return -1;
105         }
106     }
107 
108 }