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;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
21  import org.hipparchus.geometry.euclidean.threed.Vector3D;
22  import org.hipparchus.util.FastMath;
23  import org.hipparchus.util.MathArrays;
24  import org.orekit.frames.Frame;
25  import org.orekit.time.AbsoluteDate;
26  import org.orekit.time.FieldAbsoluteDate;
27  import org.orekit.utils.ExtendedPositionProvider;
28  
29  /**
30   * Class defining the contributions of a point-mass, third body in the adjoint equations for Cartesian coordinates.
31   * If present, then the propagator should also include a {@link org.orekit.forces.gravity.ThirdBodyAttraction}.
32   * @author Romain Serra
33   * @see CartesianAdjointEquationTerm
34   * @see org.orekit.forces.gravity.ThirdBodyAttraction
35   * @since 12.2
36   */
37  public class CartesianAdjointThirdBodyTerm extends AbstractCartesianAdjointNonCentralBodyTerm {
38  
39      /**
40       * Constructor.
41       * @param mu body gravitational parameter.
42       * @param bodyPositionProvider body position provider
43       */
44      public CartesianAdjointThirdBodyTerm(final double mu, final ExtendedPositionProvider bodyPositionProvider) {
45          super(mu, bodyPositionProvider);
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public Vector3D getAcceleration(final AbsoluteDate date, final double[] stateVariables,
51                                      final Frame frame) {
52          final Vector3D bodyPosition = getBodyPosition(date, frame);
53          final double x = stateVariables[0] - bodyPosition.getX();
54          final double y = stateVariables[1] - bodyPosition.getY();
55          final double z = stateVariables[2] - bodyPosition.getZ();
56          final Vector3D newtonianAcceleration = getNewtonianAcceleration(new double[] {x, y, z});
57          final double rBody2 = bodyPosition.getNormSq();
58          final Vector3D bodyCentralAcceleration = bodyPosition.scalarMultiply(getMu() / (rBody2 * FastMath.sqrt(rBody2)));
59          return newtonianAcceleration.subtract(bodyCentralAcceleration);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public <T extends CalculusFieldElement<T>> FieldVector3D<T> getFieldAcceleration(final FieldAbsoluteDate<T> date,
65                                                                                       final T[] stateVariables,
66                                                                                       final Frame frame) {
67          final FieldVector3D<T> bodyPosition = getFieldBodyPosition(date, frame);
68          final T x = stateVariables[0].subtract(bodyPosition.getX());
69          final T y = stateVariables[1].subtract(bodyPosition.getY());
70          final T z = stateVariables[2].subtract(bodyPosition.getZ());
71          final T[] relativePosition = MathArrays.buildArray(date.getField(), 3);
72          relativePosition[0] = x;
73          relativePosition[1] = y;
74          relativePosition[2] = z;
75          final FieldVector3D<T> newtonianAcceleration = getFieldNewtonianAcceleration(relativePosition);
76          final T rBody2 = bodyPosition.getNormSq();
77          final T factor = rBody2.multiply(rBody2.sqrt()).reciprocal().multiply(getMu());
78          final FieldVector3D<T> bodyCentralAcceleration = bodyPosition.scalarMultiply(factor);
79          return newtonianAcceleration.subtract(bodyCentralAcceleration);
80      }
81  }