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.ode.events.Action;
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.orekit.propagation.events.EventDetector;
25  
26  import java.util.List;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  
30  class BoundedCartesianEnergyTest {
31  
32      @Test
33      void getEventDetectorsSizeAndActionTest() {
34          // GIVEN
35          final double maximumThrustMagnitude = 1.;
36          final double massFlowRateFactor = 2.;
37          final BoundedCartesianEnergy boundedCartesianEnergy = new BoundedCartesianEnergy("", massFlowRateFactor,
38                  maximumThrustMagnitude);
39          // WHEN
40          final Stream<EventDetector> eventDetectorStream = boundedCartesianEnergy.getEventDetectors();
41          // THEN
42          final List<EventDetector> eventDetectors = eventDetectorStream.collect(Collectors.toList());
43          Assertions.assertEquals(2, eventDetectors.size());
44          for (final EventDetector eventDetector : eventDetectors) {
45              Assertions.assertInstanceOf(CartesianEnergyConsideringMass.SingularityDetector.class, eventDetector);
46              final CartesianEnergyConsideringMass.SingularityDetector singularityDetector =
47                      (CartesianEnergyConsideringMass.SingularityDetector) eventDetector;
48              Assertions.assertEquals(Action.RESET_DERIVATIVES, singularityDetector.getHandler().eventOccurred(null, null, true));
49          }
50      }
51  
52      @ParameterizedTest
53      @ValueSource(booleans = {false, true})
54      void testUpdateFieldAdjointDerivatives(final boolean withMass) {
55          // GIVEN
56          final double massFlowRateFactor = withMass ? 1 : 0;
57          final BoundedCartesianEnergy cost = new BoundedCartesianEnergy("adjoint", massFlowRateFactor, 2);
58          final double[] adjoint = new double[withMass ? 7 : 6];
59          adjoint[3] = 1;
60          final double[] derivatives = new double[adjoint.length];
61          // WHEN
62          cost.updateAdjointDerivatives(adjoint, 1, derivatives);
63          // THEN
64          for (int i = 0; i < 6; ++i) {
65              Assertions.assertEquals(0., derivatives[i]);
66          }
67          if (withMass) {
68              Assertions.assertNotEquals(0., derivatives[derivatives.length - 1]);
69          } else {
70              Assertions.assertEquals(0., derivatives[derivatives.length - 1]);
71          }
72      }
73  }