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.propagation.events.EventDetectionSettings;
26  import org.orekit.propagation.events.EventDetector;
27  import org.orekit.propagation.events.handlers.ResetDerivativesOnEvent;
28  
29  import java.util.List;
30  import java.util.stream.Collectors;
31  import java.util.stream.Stream;
32  
33  class CartesianFuelCostTest {
34  
35      @Test
36      void testConstructor() {
37          // GIVEN
38          final EventDetectionSettings expectedDetectionSettings = EventDetectionSettings.getDefaultEventDetectionSettings();
39          // WHEN
40          final CartesianFuelCost cartesianFuel = new CartesianFuelCost("", 1., 2.);
41          // THEN
42          final EventDetectionSettings actualDetectionSettings = cartesianFuel.getEventDetectionSettings();
43          Assertions.assertEquals(expectedDetectionSettings.getMaxIterationCount(),
44                  actualDetectionSettings.getMaxIterationCount());
45          Assertions.assertEquals(expectedDetectionSettings.getThreshold(), actualDetectionSettings.getThreshold());
46      }
47  
48      @ParameterizedTest
49      @ValueSource(doubles = {-1e1, 1, 1e1})
50      void testUpdateAdjointDerivatives(final double adjointMass) {
51          // GIVEN
52          final double rateFactor = 1.;
53          final double maximumThrust = 2.;
54          final CartesianFuelCost cartesianFuel = new CartesianFuelCost("", rateFactor, maximumThrust);
55          final double[] adjoint = new double[] {0, 0, 0, 1, 0, 0, adjointMass};
56          final double mass = 100;
57          final double[] derivatives = new double[7];
58          // WHEN
59          cartesianFuel.updateAdjointDerivatives(adjoint, mass, derivatives);
60          // THEN
61          if (derivatives[6] != 0) {
62              Assertions.assertEquals(maximumThrust / mass / mass, derivatives[6]);
63          }
64      }
65  
66      @ParameterizedTest
67      @ValueSource(doubles = {-1e1, 1, 1e1})
68      void testGetThrustAccelerationVector(final double adjointMass) {
69          // GIVEN
70          final double rateFactor = 1.;
71          final double maximumThrust = 2.;
72          final CartesianFuelCost cartesianFuel = new CartesianFuelCost("", rateFactor, maximumThrust);
73          final double[] adjoint = new double[] {0, 0, 0, 1, 0, 0, adjointMass};
74          final double mass = 100;
75          // WHEN
76          final Vector3D actual = cartesianFuel.getThrustAccelerationVector(adjoint, mass);
77          // THEN
78          if (actual.getNorm() != 0) {
79              Assertions.assertEquals(Vector3D.PLUS_I.scalarMultiply(maximumThrust / mass), actual);
80          }
81      }
82  
83      @Test
84      void testGetHamiltonianContribution() {
85          // GIVEN
86          final CartesianFuelCost cartesianFuel = Mockito.mock(CartesianFuelCost.class);
87          final double[] adjoint = new double[0];
88          final double mass = 1;
89          final Vector3D accelerationVector = new Vector3D(1, 2, 3);
90          Mockito.when(cartesianFuel.getThrustAccelerationVector(adjoint, mass)).thenReturn(accelerationVector);
91          Mockito.when(cartesianFuel.getHamiltonianContribution(adjoint, mass)).thenCallRealMethod();
92          // WHEN
93          final double actual = cartesianFuel.getHamiltonianContribution(adjoint, mass);
94          // THEN
95          Assertions.assertEquals(accelerationVector.scalarMultiply(mass).getNorm(), -actual);
96      }
97  
98      @Test
99      void testGetEventDetectors() {
100         // GIVEN
101         final EventDetectionSettings mockedDetectionSettings = Mockito.mock(EventDetectionSettings.class);
102         final CartesianFuelCost cartesianFuel = new CartesianFuelCost("", 1., 2.,
103                 mockedDetectionSettings);
104         // WHEN
105         final Stream<EventDetector> detectorStream = cartesianFuel.getEventDetectors();
106         // THEN
107         final List<EventDetector> detectorList = detectorStream.collect(Collectors.toList());
108         Assertions.assertEquals(1, detectorList.size());
109         Assertions.assertEquals(mockedDetectionSettings, detectorList.get(0).getDetectionSettings());
110         Assertions.assertInstanceOf(ResetDerivativesOnEvent.class, detectorList.get(0).getHandler());
111     }
112 
113 }
114