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  /** Body attraction force model computed as absolute acceleration towards a body.
38   * <p>
39   * This force model represents the same physical principles as {@link NewtonianAttraction},
40   * but has several major differences:
41   * </p>
42   * <ul>
43   *   <li>the attracting body can be <em>away</em> from the integration frame center,</li>
44   *   <li>several instances of this force model can be added when several bodies are involved,</li>
45   *   <li>this force model is <em>never</em> automatically added by the numerical propagator</li>
46   * </ul>
47   * <p>
48   * The possibility for the attracting body to be away from the frame center allows to use this force
49   * model when integrating for example an interplanetary trajectory propagated in an Earth centered
50   * frame (in which case an instance of {@link org.orekit.forces.inertia.InertialForces} must also be
51   * added to take into account the coupling effect of relative frames motion).
52   * </p>
53   * <p>
54   * The possibility to add several instances allows to use this in interplanetary trajectories or
55   * in trajectories about Lagrangian points
56   * </p>
57   * <p>
58   * The fact this force model is <em>never</em> automatically added by the numerical propagator differs
59   * from {@link NewtonianAttraction} as {@link NewtonianAttraction} may be added automatically when
60   * propagating a trajectory represented as an {@link org.orekit.orbits.Orbit}, which must always refer
61   * to a central body, if user did not add the {@link NewtonianAttraction} or set the central attraction
62   * coefficient by himself.
63   * </p>
64   * @see org.orekit.forces.inertia.InertialForces
65   * @author Luc Maisonobe
66   * @author Julio Hernanz
67   */
68  public class SingleBodyAbsoluteAttraction extends AbstractForceModel {
69  
70      /** Suffix for parameter name for attraction coefficient enabling Jacobian processing. */
71      public static final String ATTRACTION_COEFFICIENT_SUFFIX = " attraction coefficient";
72  
73      /** Central attraction scaling factor.
74       * <p>
75       * We use a power of 2 to avoid numeric noise introduction
76       * in the multiplications/divisions sequences.
77       * </p>
78       */
79      private static final double MU_SCALE = FastMath.scalb(1.0, 32);
80  
81      /** The body to consider. */
82      private final CelestialBody body;
83  
84      /** Driver for gravitational parameter. */
85      private final ParameterDriver gmParameterDriver;
86  
87      /** Simple constructor.
88       * @param body the body to consider
89       * (ex: {@link CelestialBodies#getSun()} or
90       * {@link CelestialBodies#getMoon()})
91       */
92      public SingleBodyAbsoluteAttraction(final CelestialBody body) {
93          gmParameterDriver = new ParameterDriver(body.getName() + ATTRACTION_COEFFICIENT_SUFFIX,
94                                                  body.getGM(), MU_SCALE,
95                                                  0.0, Double.POSITIVE_INFINITY);
96  
97          this.body = body;
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public boolean dependsOnPositionOnly() {
103         return true;
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {
109 
110         // compute bodies separation vectors and squared norm
111         final Vector3D bodyPosition = body.getPVCoordinates(s.getDate(), s.getFrame()).getPosition();
112         final Vector3D satToBody     = bodyPosition.subtract(s.getPVCoordinates().getPosition());
113         final double r2Sat           = satToBody.getNormSq();
114 
115         // compute absolute acceleration
116         return new Vector3D(parameters[0] / (r2Sat * FastMath.sqrt(r2Sat)), satToBody);
117 
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
123                                                                          final T[] parameters) {
124          // compute bodies separation vectors and squared norm
125         final FieldVector3D<T> centralToBody = new FieldVector3D<>(s.getA().getField(),
126                                                                    body.getPVCoordinates(s.getDate().toAbsoluteDate(), s.getFrame()).getPosition());
127         final FieldVector3D<T> satToBody     = centralToBody.subtract(s.getPVCoordinates().getPosition());
128         final T                r2Sat         = satToBody.getNormSq();
129 
130         // compute absolute acceleration
131         return new FieldVector3D<>(parameters[0].divide(r2Sat.multiply(r2Sat.sqrt())), satToBody);
132 
133     }
134 
135     /** {@inheritDoc} */
136     public Stream<EventDetector> getEventsDetectors() {
137         return Stream.empty();
138     }
139 
140     @Override
141     /** {@inheritDoc} */
142     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventsDetectors(final Field<T> field) {
143         return Stream.empty();
144     }
145 
146     /** {@inheritDoc} */
147     public List<ParameterDriver> getParametersDrivers() {
148         return Collections.singletonList(gmParameterDriver);
149     }
150 
151 }