L1TransformProvider.java

  1. /* Copyright 2002-2025 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.frames;

  18. import org.hipparchus.Field;
  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
  21. import org.hipparchus.analysis.UnivariateFunction;
  22. import org.hipparchus.analysis.solvers.AllowedSolution;
  23. import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
  24. import org.hipparchus.analysis.solvers.FieldBracketingNthOrderBrentSolver;
  25. import org.hipparchus.analysis.solvers.UnivariateSolverUtils;
  26. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  27. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  28. import org.hipparchus.geometry.euclidean.threed.Rotation;
  29. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  30. import org.hipparchus.util.FastMath;
  31. import org.orekit.bodies.CelestialBody;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.time.FieldAbsoluteDate;
  34. import org.orekit.utils.FieldPVCoordinates;
  35. import org.orekit.utils.PVCoordinates;

  36. /** L1 Transform provider for a frame on the L1 Lagrange point of two celestial bodies.
  37.  *
  38.  * @author Luc Maisonobe
  39.  * @author Julio Hernanz
  40.  */
  41. public class L1TransformProvider implements TransformProvider {

  42.     /** Relative accuracy on position for solver. */
  43.     private static final double RELATIVE_ACCURACY = 1e-14;

  44.     /** Absolute accuracy on position for solver (1mm). */
  45.     private static final double ABSOLUTE_ACCURACY = 1e-3;

  46.     /** Function value ccuracy for solver (set to 0 so we rely only on position for convergence). */
  47.     private static final double FUNCTION_ACCURACY = 0;

  48.     /** Maximal order for solver. */
  49.     private static final int MAX_ORDER = 5;

  50.     /** Maximal number of evaluations for solver. */
  51.     private static final int MAX_EVALUATIONS = 1000;

  52.     /** Frame for results. Always defined as primaryBody's inertially oriented frame.*/
  53.     private final Frame frame;

  54.     /** Celestial body with bigger mass, m1.*/
  55.     private final CelestialBody primaryBody;

  56.     /** Celestial body with smaller mass, m2.*/
  57.     private final CelestialBody secondaryBody;

  58.     /** Simple constructor.
  59.      * @param primaryBody Primary body.
  60.      * @param secondaryBody Secondary body.
  61.      */
  62.     public L1TransformProvider(final CelestialBody primaryBody, final CelestialBody secondaryBody) {
  63.         this.primaryBody   = primaryBody;
  64.         this.secondaryBody = secondaryBody;
  65.         this.frame         = primaryBody.getInertiallyOrientedFrame();
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public Transform getTransform(final AbsoluteDate date) {
  70.         final PVCoordinates pv21        = secondaryBody.getPVCoordinates(date, frame);
  71.         final Vector3D      translation = getL1(pv21.getPosition()).negate();
  72.         final Rotation      rotation    = new Rotation(pv21.getPosition(), pv21.getVelocity(),
  73.                                                        Vector3D.PLUS_I, Vector3D.PLUS_J);
  74.         return new Transform(date, new Transform(date, translation), new Transform(date, rotation));
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public StaticTransform getStaticTransform(final AbsoluteDate date) {
  79.         final PVCoordinates pv21        = secondaryBody.getPVCoordinates(date, frame);
  80.         final Vector3D      translation = getL1(pv21.getPosition()).negate();
  81.         final Rotation      rotation    = new Rotation(pv21.getPosition(), pv21.getVelocity(),
  82.                 Vector3D.PLUS_I, Vector3D.PLUS_J);
  83.         return StaticTransform.compose(
  84.                 date,
  85.                 StaticTransform.of(date, translation),
  86.                 StaticTransform.of(date, rotation));
  87.     }

  88.     /** {@inheritDoc} */
  89.     @Override
  90.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
  91.         final FieldPVCoordinates<T> pv21        = secondaryBody.getPVCoordinates(date, frame);
  92.         final FieldVector3D<T>      translation = getL1(pv21.getPosition()).negate();
  93.         final Field<T>              field       = pv21.getPosition().getX().getField();
  94.         final FieldRotation<T>      rotation    = new FieldRotation<>(pv21.getPosition(), pv21.getVelocity(),
  95.                                                                       FieldVector3D.getPlusI(field),
  96.                                                                       FieldVector3D.getPlusJ(field));
  97.         return new FieldTransform<>(date,
  98.                                     new FieldTransform<>(date, translation),
  99.                                     new FieldTransform<>(date, rotation));
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public <T extends CalculusFieldElement<T>> FieldStaticTransform<T> getStaticTransform(final FieldAbsoluteDate<T> date) {
  104.         final FieldPVCoordinates<T> pv21        = secondaryBody.getPVCoordinates(date, frame);
  105.         final FieldVector3D<T>      translation = getL1(pv21.getPosition()).negate();
  106.         final FieldRotation<T>      rotation    = new FieldRotation<>(pv21.getPosition(), pv21.getVelocity(),
  107.                 FieldVector3D.getPlusI(date.getField()), FieldVector3D.getPlusJ(date.getField()));
  108.         return FieldStaticTransform.compose(
  109.                 date,
  110.                 FieldStaticTransform.of(date, translation),
  111.                 FieldStaticTransform.of(date, rotation));
  112.     }

  113.     /** Compute the coordinates of the L1 point.
  114.      * @param primaryToSecondary relative position of secondary body with respect to primary body
  115.      * @return coordinates of the L1 point given in frame: primaryBody.getInertiallyOrientedFrame()
  116.      */
  117.     private Vector3D getL1(final Vector3D primaryToSecondary) {

  118.         // mass ratio
  119.         final double massRatio = secondaryBody.getGM() / primaryBody.getGM();

  120.         // Approximate position of L1 point, valid when m2 << m1
  121.         final double bigR  = primaryToSecondary.getNorm();
  122.         final double baseR = bigR * (1 - FastMath.cbrt(massRatio / 3));

  123.         // Accurate position of L1 point, by solving the L1 equilibrium equation
  124.         final UnivariateFunction l1Equation = r -> {
  125.             final double bigrminusR  = bigR - r;
  126.             final double lhs         = 1.0 / ( r * r );
  127.             final double rhs1        = massRatio / (bigrminusR * bigrminusR);
  128.             final double rhs2        = 1.0 / (bigR * bigR);
  129.             final double rhs3        = (1 + massRatio) * bigrminusR * rhs2 / bigR;
  130.             return lhs - (rhs1 + rhs2 - rhs3);
  131.         };
  132.         final double[] searchInterval = UnivariateSolverUtils.bracket(l1Equation,
  133.                                                                       baseR, 0, bigR,
  134.                                                                       0.01 * bigR, 1, MAX_EVALUATIONS);
  135.         final BracketingNthOrderBrentSolver solver =
  136.                         new BracketingNthOrderBrentSolver(RELATIVE_ACCURACY,
  137.                                                           ABSOLUTE_ACCURACY,
  138.                                                           FUNCTION_ACCURACY,
  139.                                                           MAX_ORDER);
  140.         final double r = solver.solve(MAX_EVALUATIONS, l1Equation,
  141.                                       searchInterval[0], searchInterval[1],
  142.                                       AllowedSolution.ANY_SIDE);

  143.         // L1 point is built
  144.         return new Vector3D(r / bigR, primaryToSecondary);

  145.     }

  146.     /** Compute the coordinates of the L1 point.
  147.      * @param <T> type of the field elements
  148.      * @param primaryToSecondary relative position of secondary body with respect to primary body
  149.      * @return coordinates of the L1 point given in frame: primaryBody.getInertiallyOrientedFrame()
  150.      */
  151.     private <T extends CalculusFieldElement<T>> FieldVector3D<T>
  152.         getL1(final FieldVector3D<T> primaryToSecondary) {

  153.         // mass ratio
  154.         final double massRatio = secondaryBody.getGM() / primaryBody.getGM();

  155.         // Approximate position of L1 point, valid when m2 << m1
  156.         final T bigR  = primaryToSecondary.getNorm();
  157.         final T baseR = bigR.multiply(1 - FastMath.cbrt(massRatio / 3));

  158.         // Accurate position of L1 point, by solving the L1 equilibrium equation
  159.         final CalculusFieldUnivariateFunction<T> l1Equation = r -> {
  160.             final T bigrminusR = bigR.subtract(r);
  161.             final T lhs        = r.multiply(r).reciprocal();
  162.             final T rhs1       = bigrminusR.multiply(bigrminusR).reciprocal().multiply(massRatio);
  163.             final T rhs2       = bigR.multiply(bigR).reciprocal();
  164.             final T rhs3       = bigrminusR.multiply(rhs2).multiply(1 + massRatio).divide(bigR);
  165.             return lhs.subtract(rhs1.add(rhs2).add(rhs3));
  166.         };
  167.         final T zero             = primaryToSecondary.getX().getField().getZero();
  168.         final T[] searchInterval = UnivariateSolverUtils.bracket(l1Equation,
  169.                                                                  baseR, zero, bigR.multiply(2),
  170.                                                                  bigR.multiply(0.01), zero.newInstance(1),
  171.                                                                  MAX_EVALUATIONS);


  172.         final T relativeAccuracy = zero.newInstance(RELATIVE_ACCURACY);
  173.         final T absoluteAccuracy = zero.newInstance(ABSOLUTE_ACCURACY);
  174.         final T functionAccuracy = zero.newInstance(FUNCTION_ACCURACY);

  175.         final FieldBracketingNthOrderBrentSolver<T> solver =
  176.                         new FieldBracketingNthOrderBrentSolver<>(relativeAccuracy,
  177.                                                                  absoluteAccuracy,
  178.                                                                  functionAccuracy,
  179.                                                                  MAX_ORDER);
  180.         final T r = solver.solve(MAX_EVALUATIONS, l1Equation,
  181.                                  searchInterval[0], searchInterval[1],
  182.                                  AllowedSolution.ANY_SIDE);

  183.         // L1 point is built
  184.         return new FieldVector3D<>(r.divide(bigR), primaryToSecondary);

  185.     }

  186. }