1   /* Copyright 2002-2021 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 java.util.Collections;
20  import java.util.List;
21  import java.util.stream.Stream;
22  
23  import org.hipparchus.Field;
24  import org.hipparchus.CalculusFieldElement;
25  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.hipparchus.util.FastMath;
28  import org.orekit.bodies.CelestialBodies;
29  import org.orekit.bodies.CelestialBody;
30  import org.orekit.forces.AbstractForceModel;
31  import org.orekit.propagation.FieldSpacecraftState;
32  import org.orekit.propagation.SpacecraftState;
33  import org.orekit.propagation.events.EventDetector;
34  import org.orekit.propagation.events.FieldEventDetector;
35  import org.orekit.utils.ParameterDriver;
36  
37  /** Third body attraction force model.
38   *
39   * @author Fabien Maussion
40   * @author Véronique Pommier-Maurussane
41   */
42  public class ThirdBodyAttraction extends AbstractForceModel {
43  
44      /** Suffix for parameter name for attraction coefficient enabling Jacobian processing. */
45      public static final String ATTRACTION_COEFFICIENT_SUFFIX = " attraction coefficient";
46  
47      /** Central attraction scaling factor.
48       * <p>
49       * We use a power of 2 to avoid numeric noise introduction
50       * in the multiplications/divisions sequences.
51       * </p>
52       */
53      private static final double MU_SCALE = FastMath.scalb(1.0, 32);
54  
55      /** Drivers for third body attraction coefficient. */
56      private final ParameterDriver gmParameterDriver;
57  
58      /** The body to consider. */
59      private final CelestialBody body;
60  
61      /** Simple constructor.
62       * @param body the third body to consider
63       * (ex: {@link CelestialBodies#getSun()} or
64       * {@link CelestialBodies#getMoon()})
65       */
66      public ThirdBodyAttraction(final CelestialBody body) {
67          gmParameterDriver = new ParameterDriver(body.getName() + ATTRACTION_COEFFICIENT_SUFFIX,
68                                                  body.getGM(), MU_SCALE,
69                                                  0.0, Double.POSITIVE_INFINITY);
70  
71          this.body = body;
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public boolean dependsOnPositionOnly() {
77          return true;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {
83  
84          final double gm = parameters[0];
85  
86          // compute bodies separation vectors and squared norm
87          final Vector3D centralToBody = body.getPVCoordinates(s.getDate(), s.getFrame()).getPosition();
88          final double r2Central       = centralToBody.getNormSq();
89          final Vector3D satToBody     = centralToBody.subtract(s.getPVCoordinates().getPosition());
90          final double r2Sat           = satToBody.getNormSq();
91  
92          // compute relative acceleration
93          return new Vector3D(gm / (r2Sat * FastMath.sqrt(r2Sat)), satToBody,
94                             -gm / (r2Central * FastMath.sqrt(r2Central)), centralToBody);
95  
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
101                                                                          final T[] parameters) {
102 
103         final T gm = parameters[0];
104 
105         // compute bodies separation vectors and squared norm
106         final FieldVector3D<T> centralToBody = new FieldVector3D<>(s.getA().getField(),
107                                                                    body.getPVCoordinates(s.getDate().toAbsoluteDate(), s.getFrame()).getPosition());
108         final T                r2Central     = centralToBody.getNormSq();
109         final FieldVector3D<T> satToBody     = centralToBody.subtract(s.getPVCoordinates().getPosition());
110         final T                r2Sat         = satToBody.getNormSq();
111 
112         // compute relative acceleration
113         return new FieldVector3D<>(r2Sat.multiply(r2Sat.sqrt()).reciprocal().multiply(gm), satToBody,
114                                    r2Central.multiply(r2Central.sqrt()).reciprocal().multiply(gm).negate(), centralToBody);
115 
116     }
117 
118     /** {@inheritDoc} */
119     public Stream<EventDetector> getEventsDetectors() {
120         return Stream.empty();
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventsDetectors(final Field<T> field) {
126         return Stream.empty();
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public List<ParameterDriver> getParametersDrivers() {
132         return Collections.singletonList(gmParameterDriver);
133     }
134 
135 }