1   /* Copyright 2002-2025 CS GROUP
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.forces.gravity;
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.orekit.bodies.CelestialBodies;
24  import org.orekit.bodies.CelestialBody;
25  import org.orekit.propagation.FieldSpacecraftState;
26  import org.orekit.propagation.SpacecraftState;
27  import org.orekit.utils.ExtendedPositionProvider;
28  
29  /** Third body attraction force model.
30   *
31   * @author Fabien Maussion
32   * @author Véronique Pommier-Maurussane
33   */
34  public class ThirdBodyAttraction extends AbstractBodyAttraction {
35  
36      /** Simple constructor.
37       * @param positionProvider extended position provider for the body to consider
38       * @param name name of the body
39       * @param mu body gravitational constant
40       * @since 13.0
41       */
42      public ThirdBodyAttraction(final ExtendedPositionProvider positionProvider, final String name, final double mu) {
43          super(positionProvider, name, mu);
44      }
45  
46      /** Constructor.
47       * @param body the third body to consider
48       * (ex: {@link CelestialBodies#getSun()} or
49       * {@link CelestialBodies#getMoon()})
50       */
51      public ThirdBodyAttraction(final CelestialBody body) {
52          this(body, body.getName(), body.getGM());
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {
58  
59          final double gm = parameters[0];
60  
61          // compute bodies separation vectors and squared norm
62          final Vector3D centralToBody = getBodyPosition(s.getDate(), s.getFrame());
63          final double r2Central       = centralToBody.getNormSq();
64          final Vector3D satToBody     = centralToBody.subtract(s.getPosition());
65          final double r2Sat           = satToBody.getNormSq();
66  
67          // compute relative acceleration
68          return new Vector3D(gm / (r2Sat * FastMath.sqrt(r2Sat)), satToBody,
69                             -gm / (r2Central * FastMath.sqrt(r2Central)), centralToBody);
70  
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
76                                                                               final T[] parameters) {
77  
78          final T gm = parameters[0];
79  
80          // compute bodies separation vectors and squared norm
81          final FieldVector3D<T> centralToBody = getBodyPosition(s.getDate(), s.getFrame());
82          final T                r2Central     = centralToBody.getNormSq();
83          final FieldVector3D<T> satToBody     = centralToBody.subtract(s.getPosition());
84          final T                r2Sat         = satToBody.getNormSq();
85  
86          // compute relative acceleration
87          return new FieldVector3D<>(r2Sat.multiply(r2Sat.sqrt()).reciprocal().multiply(gm), satToBody,
88                                     r2Central.multiply(r2Central.sqrt()).reciprocal().multiply(gm).negate(), centralToBody);
89  
90      }
91  
92  }