J2OnlyPerturbation.java

  1. /* Copyright 2022-2025 Romain Serra
  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. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.FastMath;
  22. import org.orekit.forces.ForceModel;
  23. import org.orekit.forces.gravity.potential.UnnormalizedSphericalHarmonicsProvider;
  24. import org.orekit.frames.FieldStaticTransform;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.frames.StaticTransform;
  27. import org.orekit.propagation.FieldSpacecraftState;
  28. import org.orekit.propagation.SpacecraftState;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.time.FieldAbsoluteDate;
  31. import org.orekit.time.TimeScalarFunction;
  32. import org.orekit.utils.ParameterDriver;

  33. import java.util.Collections;
  34. import java.util.List;

  35. /** J2-only force model.
  36.  * This class models the oblateness part alone of the central body's potential (degree 2 and order 0),
  37.  * whilst avoiding the computational overhead of generic NxM spherical harmonics.
  38.  *
  39.  * <p>
  40.  * This J2 coefficient has same magnitude and opposite sign than the so-called unnormalized C20 coefficient.
  41.  * </p>
  42.  *
  43.  * <p>
  44.  * This class should not be used in combination of {@link HolmesFeatherstoneAttractionModel},
  45.  * otherwise the J2 term would be taken into account twice.
  46.  * </p>
  47.  *
  48.  * @author Romain Serra
  49.  */
  50. public class J2OnlyPerturbation implements ForceModel {

  51.     /** Central body's gravitational constant. */
  52.     private final double mu;

  53.     /** Central body's equatorial radius. */
  54.     private final double rEq;

  55.     /** Central body's J2 coefficient as a function of time. */
  56.     private final TimeScalarFunction j2OverTime;

  57.     /** Frame where J2 applies. */
  58.     private final Frame frame;

  59.     /** Constructor with {@link TimeScalarFunction}.
  60.      * It is the user's responsibility to make sure the Field and double versions are consistent with each other.
  61.      * @param mu central body's gravitational constant
  62.      * @param rEq central body's equatorial radius
  63.      * @param j2OverTime J2 coefficient as a function of time.
  64.      * @param frame frame where J2 applies
  65.      */
  66.     public J2OnlyPerturbation(final double mu, final double rEq, final TimeScalarFunction j2OverTime,
  67.                               final Frame frame) {
  68.         this.mu = mu;
  69.         this.rEq = rEq;
  70.         this.j2OverTime = j2OverTime;
  71.         this.frame = frame;
  72.     }

  73.     /** Constructor with constant J2.
  74.      * @param mu central body gravitational constant
  75.      * @param rEq central body's equatorial radius
  76.      * @param constantJ2 constant J2 coefficient
  77.      * @param frame frame where J2 applies
  78.      */
  79.     public J2OnlyPerturbation(final double mu, final double rEq, final double constantJ2, final Frame frame) {
  80.         this.mu = mu;
  81.         this.rEq = rEq;
  82.         this.frame = frame;
  83.         this.j2OverTime = new TimeScalarFunction() {
  84.             @Override
  85.             public double value(final AbsoluteDate date) {
  86.                 return constantJ2;
  87.             }

  88.             @Override
  89.             public <T extends CalculusFieldElement<T>> T value(final FieldAbsoluteDate<T> date) {
  90.                 return date.getField().getZero().newInstance(constantJ2);
  91.             }
  92.         };
  93.     }

  94.     /** Constructor with spherical harmonics provider.
  95.      * @param harmonicsProvider spherical harmonics provider of unnormalized coefficients
  96.      * @param frame frame where J2 applies
  97.      */
  98.     public J2OnlyPerturbation(final UnnormalizedSphericalHarmonicsProvider harmonicsProvider, final Frame frame) {
  99.         this.mu = harmonicsProvider.getMu();
  100.         this.rEq = harmonicsProvider.getAe();
  101.         this.frame = frame;
  102.         this.j2OverTime = new TimeScalarFunction() {
  103.             @Override
  104.             public double value(final AbsoluteDate date) {
  105.                 return -harmonicsProvider.getUnnormalizedC20(date);
  106.             }

  107.             @Override
  108.             public <T extends CalculusFieldElement<T>> T value(final FieldAbsoluteDate<T> date) {
  109.                 return date.getField().getZero().newInstance(value(date.toAbsoluteDate()));
  110.             }
  111.         };
  112.     }

  113.     /** Getter for mu.
  114.      * @return mu
  115.      */
  116.     public double getMu() {
  117.         return mu;
  118.     }

  119.     /** Getter for equatorial radius.
  120.      * @return equatorial radius
  121.      */
  122.     public double getrEq() {
  123.         return rEq;
  124.     }

  125.     /** Getter for frame.
  126.      * @return frame
  127.      */
  128.     public Frame getFrame() {
  129.         return frame;
  130.     }

  131.     /** Return J2 at requested date.
  132.      * @param date epoch at which J2 coefficient should be retrieved
  133.      * @return J2 coefficient
  134.      */
  135.     public double getJ2(final AbsoluteDate date) {
  136.         return j2OverTime.value(date);
  137.     }

  138.     /** Return J2 at requested date (Field version).
  139.      * @param <T> field
  140.      * @param date epoch at which J2 coefficient should be retrieved
  141.      * @return J2 coefficient
  142.      */
  143.     public <T extends CalculusFieldElement<T>> T getJ2(final FieldAbsoluteDate<T> date) {
  144.         return j2OverTime.value(date);
  145.     }

  146.     /** {@inheritDoc} */
  147.     @Override
  148.     public boolean dependsOnPositionOnly() {
  149.         return true;
  150.     }

  151.     /** {@inheritDoc} */
  152.     @Override
  153.     public Vector3D acceleration(final SpacecraftState state, final double[] parameters) {
  154.         final AbsoluteDate date = state.getDate();
  155.         final StaticTransform fromPropagationToJ2Frame = state.getFrame().getStaticTransformTo(frame, date);
  156.         final Vector3D positionInJ2Frame = fromPropagationToJ2Frame.transformPosition(state.getPosition());
  157.         final double j2 = j2OverTime.value(date);
  158.         final Vector3D accelerationInJ2Frame = computeAccelerationInJ2Frame(positionInJ2Frame, mu, rEq, j2);
  159.         final StaticTransform fromJ2FrameToPropagationOne = fromPropagationToJ2Frame.getStaticInverse();
  160.         return fromJ2FrameToPropagationOne.transformVector(accelerationInJ2Frame);
  161.     }

  162.     /**
  163.      * Compute acceleration in J2 frame.
  164.      * @param positionInJ2Frame position in J2 frame@
  165.      * @param mu gravitational parameter
  166.      * @param rEq equatorial radius
  167.      * @param j2 J2 coefficient
  168.      * @return acceleration in J2 frame
  169.      */
  170.     public static Vector3D computeAccelerationInJ2Frame(final Vector3D positionInJ2Frame, final double mu,
  171.                                                         final double rEq, final double j2) {
  172.         final double squaredRadius = positionInJ2Frame.getNormSq();
  173.         final double squaredZ = positionInJ2Frame.getZ() * positionInJ2Frame.getZ();
  174.         final double ratioTimesFive = 5. * squaredZ / squaredRadius;
  175.         final double ratioTimesFiveMinusOne = ratioTimesFive - 1.;
  176.         final double componentX = positionInJ2Frame.getX() * ratioTimesFiveMinusOne;
  177.         final double componentY = positionInJ2Frame.getY() * ratioTimesFiveMinusOne;
  178.         final double componentZ = positionInJ2Frame.getZ() * (ratioTimesFive - 3);
  179.         final double squaredRadiiRatio = rEq * rEq / squaredRadius;
  180.         final double cubedRadius = squaredRadius * FastMath.sqrt(squaredRadius);
  181.         final double factor = 3 * j2 * mu * squaredRadiiRatio / (2 * cubedRadius);
  182.         return new Vector3D(componentX, componentY, componentZ).scalarMultiply(factor);
  183.     }

  184.     /** {@inheritDoc} */
  185.     @Override
  186.     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> state,
  187.                                                                              final T[] parameters) {
  188.         final FieldAbsoluteDate<T> date = state.getDate();
  189.         final FieldStaticTransform<T> fromPropagationToJ2Frame = state.getFrame().getStaticTransformTo(frame, date);
  190.         final FieldVector3D<T> positionInJ2Frame = fromPropagationToJ2Frame.transformPosition(state.getPosition());
  191.         final FieldVector3D<T> accelerationInJ2Frame = computeAccelerationInJ2Frame(positionInJ2Frame, mu, rEq,
  192.                 j2OverTime.value(date));
  193.         final FieldStaticTransform<T> fromJ2FrameToPropagation = fromPropagationToJ2Frame.getStaticInverse();
  194.         return fromJ2FrameToPropagation.transformVector(accelerationInJ2Frame);
  195.     }

  196.     /**
  197.      * Compute acceleration in J2 frame. Field version.
  198.      * @param positionInJ2Frame position in J2 frame@
  199.      * @param mu gravitational parameter
  200.      * @param rEq equatorial radius
  201.      * @param j2 J2 coefficient
  202.      * @param <T> field type
  203.      * @return acceleration in J2 frame
  204.      */
  205.     public static <T extends CalculusFieldElement<T>> FieldVector3D<T> computeAccelerationInJ2Frame(final FieldVector3D<T> positionInJ2Frame,
  206.                                                                                                     final double mu, final double rEq, final T j2) {
  207.         final T squaredRadius = positionInJ2Frame.getNormSq();
  208.         final T squaredZ = positionInJ2Frame.getZ().square();
  209.         final T ratioTimesFive = squaredZ.multiply(5.).divide(squaredRadius);
  210.         final T ratioTimesFiveMinusOne = ratioTimesFive.subtract(1.);
  211.         final T componentX = positionInJ2Frame.getX().multiply(ratioTimesFiveMinusOne);
  212.         final T componentY = positionInJ2Frame.getY().multiply(ratioTimesFiveMinusOne);
  213.         final T componentZ = positionInJ2Frame.getZ().multiply(ratioTimesFive.subtract(3.));
  214.         final T squaredRadiiRatio = squaredRadius.reciprocal().multiply(rEq * rEq);
  215.         final T cubedRadius = squaredRadius.multiply(FastMath.sqrt(squaredRadius));
  216.         final T factor = j2.multiply(mu).multiply(3.).multiply(squaredRadiiRatio).divide(cubedRadius.multiply(2));
  217.         return new FieldVector3D<>(componentX, componentY, componentZ).scalarMultiply(factor);
  218.     }

  219.     /** {@inheritDoc} */
  220.     @Override
  221.     public List<ParameterDriver> getParametersDrivers() {
  222.         return Collections.emptyList();
  223.     }
  224. }