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.FieldPVCoordinates;
36  import org.orekit.utils.PVCoordinates;
37  import org.orekit.utils.ParameterDriver;
38  
39  /** Body attraction force model computed as relative acceleration towards frame center.
40   * @author Luc Maisonabe
41   * @author Julio Hernanz
42   */
43  public class SingleBodyRelativeAttraction extends AbstractForceModel {
44  
45      /** Suffix for parameter name for attraction coefficient enabling Jacobian processing. */
46      public static final String ATTRACTION_COEFFICIENT_SUFFIX = " attraction coefficient";
47  
48      /** Central attraction scaling factor.
49       * <p>
50       * We use a power of 2 to avoid numeric noise introduction
51       * in the multiplications/divisions sequences.
52       * </p>
53       */
54      private static final double MU_SCALE = FastMath.scalb(1.0, 32);
55  
56      /** Drivers for body attraction coefficient. */
57      private final ParameterDriver gmDriver;
58  
59      /** The body to consider. */
60      private final CelestialBody body;
61  
62      /** Simple constructor.
63       * @param body the body to consider
64       * (ex: {@link CelestialBodies#getSun()} or
65       * {@link CelestialBodies#getMoon()})
66       */
67      public SingleBodyRelativeAttraction(final CelestialBody body) {
68          gmDriver = new ParameterDriver(body.getName() + ATTRACTION_COEFFICIENT_SUFFIX,
69                                         body.getGM(), MU_SCALE,
70                                         0.0, Double.POSITIVE_INFINITY);
71  
72          this.body = body;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public boolean dependsOnPositionOnly() {
78          return true;
79      }
80  
81      /** {@inheritDoc} */
82      public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {
83  
84          // compute bodies separation vectors and squared norm
85          final PVCoordinates bodyPV   = body.getPVCoordinates(s.getDate(), s.getFrame());
86          final Vector3D satToBody     = bodyPV.getPosition().subtract(s.getPVCoordinates().getPosition());
87          final double r2Sat           = satToBody.getNormSq();
88  
89          // compute relative acceleration
90          final double gm = parameters[0];
91          final double a = gm / r2Sat;
92          return new Vector3D(a, satToBody.normalize()).add(bodyPV.getAcceleration());
93  
94      }
95  
96      /** {@inheritDoc} */
97      public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
98                                                                           final T[] parameters) {
99  
100         // compute bodies separation vectors and squared norm
101         final FieldPVCoordinates<T> bodyPV = body.getPVCoordinates(s.getDate(), s.getFrame());
102         final FieldVector3D<T> satToBody   = bodyPV.getPosition().subtract(s.getPVCoordinates().getPosition());
103         final T                r2Sat       = satToBody.getNormSq();
104 
105         // compute relative acceleration
106         final T gm = parameters[0];
107         final T a  = gm.divide(r2Sat);
108         return new FieldVector3D<>(a, satToBody.normalize()).add(bodyPV.getAcceleration());
109 
110     }
111 
112     /** {@inheritDoc} */
113     public Stream<EventDetector> getEventsDetectors() {
114         return Stream.empty();
115     }
116 
117     @Override
118     /** {@inheritDoc} */
119     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventsDetectors(final Field<T> field) {
120         return Stream.empty();
121     }
122 
123     /** {@inheritDoc} */
124     public List<ParameterDriver> getParametersDrivers() {
125         return Collections.singletonList(gmDriver);
126     }
127 
128 }