1   /* Copyright 2010-2011 Centre National d'Études Spatiales
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  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.Field;
24  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
25  import org.hipparchus.geometry.euclidean.threed.Vector3D;
26  import org.hipparchus.util.FastMath;
27  import org.orekit.forces.ForceModel;
28  import org.orekit.propagation.FieldSpacecraftState;
29  import org.orekit.propagation.SpacecraftState;
30  import org.orekit.propagation.numerical.FieldTimeDerivativesEquations;
31  import org.orekit.propagation.numerical.TimeDerivativesEquations;
32  import org.orekit.time.AbsoluteDate;
33  import org.orekit.time.FieldAbsoluteDate;
34  import org.orekit.utils.ParameterDriver;
35  
36  /** Force model for Newtonian central body attraction.
37   * @author Luc Maisonobe
38   */
39  public class NewtonianAttraction implements ForceModel {
40  
41      /** Name of the single parameter of this model: the central attraction coefficient. */
42      public static final String CENTRAL_ATTRACTION_COEFFICIENT = "central attraction coefficient";
43  
44      /** Central attraction scaling factor.
45       * <p>
46       * We use a power of 2 to avoid numeric noise introduction
47       * in the multiplications/divisions sequences.
48       * </p>
49       */
50      private static final double MU_SCALE = FastMath.scalb(1.0, 32);
51  
52      /** Driver for gravitational parameter. */
53      private final ParameterDriver gmParameterDriver;
54  
55     /** Simple constructor.
56       * @param mu central attraction coefficient (m^3/s^2)
57       */
58      public NewtonianAttraction(final double mu) {
59          gmParameterDriver = new ParameterDriver(NewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT,
60                                                  mu, MU_SCALE,
61                                                  0.0, Double.POSITIVE_INFINITY);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public boolean dependsOnPositionOnly() {
67          return true;
68      }
69  
70      /** Get the central attraction coefficient μ.
71       * @param date date at which the mu value wants to be known
72       * @return mu central attraction coefficient (m³/s²)
73       */
74      public double getMu(final AbsoluteDate date) {
75          return gmParameterDriver.getValue(date);
76      }
77  
78      /** Get the central attraction coefficient μ.
79       * @param <T> the type of the field element
80       * @param field field to which the state belongs
81       * @param date date at which the mu value wants to be known
82       * @return mu central attraction coefficient (m³/s²)
83       */
84      public <T extends CalculusFieldElement<T>> T getMu(final Field<T> field, final FieldAbsoluteDate<T> date) {
85          final T zero = field.getZero();
86          return zero.newInstance(gmParameterDriver.getValue(date.toAbsoluteDate()));
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public void addContribution(final SpacecraftState s, final TimeDerivativesEquations adder) {
92          adder.addKeplerContribution(getMu(s.getDate()));
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public <T extends CalculusFieldElement<T>> void addContribution(final FieldSpacecraftState<T> s,
98                                                                  final FieldTimeDerivativesEquations<T> adder) {
99          final Field<T> field = s.getDate().getField();
100         adder.addKeplerContribution(getMu(field, s.getDate()));
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {
106         final double mu = parameters[0];
107         final double r2 = s.getPosition().getNormSq();
108         return new Vector3D(-mu / (FastMath.sqrt(r2) * r2), s.getPosition());
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
114                                                                          final T[] parameters) {
115         final T mu = parameters[0];
116         final T r2 = s.getPosition().getNormSq();
117         return new FieldVector3D<>(r2.sqrt().multiply(r2).reciprocal().multiply(mu).negate(), s.getPosition());
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public List<ParameterDriver> getParametersDrivers() {
123         return Collections.singletonList(gmParameterDriver);
124     }
125 
126 }
127