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.util.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.params.ParameterizedTest;
22  import org.junit.jupiter.params.provider.ValueSource;
23  
24  class LogarithmicBarrierCartesianFuelTest {
25  
26      private static final String ADJOINT_NAME = "adjoint";
27  
28      @ParameterizedTest
29      @ValueSource(doubles = {0., 0.1, 0.5, 0.9})
30      void testEvaluatePenaltyFunction(final double norm) {
31          // GIVEN
32          final LogarithmicBarrierCartesianFuel penalizedCartesianFuel = new LogarithmicBarrierCartesianFuel(ADJOINT_NAME,
33                  1., 2., 0.5);
34          // WHEN
35          final double actualPenalty = penalizedCartesianFuel.evaluatePenaltyFunction(norm);
36          // THEN
37          Assertions.assertEquals(FastMath.log(norm) + FastMath.log(1 - norm), actualPenalty);
38      }
39  
40      @ParameterizedTest
41      @ValueSource(booleans = {false, true})
42      void testUpdateFieldAdjointDerivatives(final boolean withMass) {
43          // GIVEN
44          final double massFlowRateFactor = withMass ? 1 : 0;
45          final LogarithmicBarrierCartesianFuel cost = new LogarithmicBarrierCartesianFuel("adjoint", massFlowRateFactor, 2, 0.5);
46          final double[] adjoint = new double[withMass ? 7 : 6];
47          adjoint[3] = 1;
48          final double[] derivatives = new double[adjoint.length];
49          // WHEN
50          cost.updateAdjointDerivatives(adjoint, 1, derivatives);
51          // THEN
52          for (int i = 0; i < 6; ++i) {
53              Assertions.assertEquals(0., derivatives[i]);
54          }
55          if (withMass) {
56              Assertions.assertNotEquals(0., derivatives[derivatives.length - 1]);
57          } else {
58              Assertions.assertEquals(0., derivatives[derivatives.length - 1]);
59          }
60      }
61  }