L1TransformProvider.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.RealFieldElement;
  20. import org.hipparchus.analysis.RealFieldUnivariateFunction;
  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.     /** Serializable UID.*/
  53.     private static final long serialVersionUID = 20170824L;

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

  56.     /** Celestial body with bigger mass, m1.*/
  57.     private final CelestialBody primaryBody;

  58.     /** Celestial body with smaller mass, m2.*/
  59.     private final CelestialBody secondaryBody;

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

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

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public <T extends RealFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
  81.         final FieldPVCoordinates<T> pv21        = secondaryBody.getPVCoordinates(date, frame);
  82.         final FieldVector3D<T>      translation = getL1(pv21.getPosition()).negate();
  83.         final Field<T>              field       = pv21.getPosition().getX().getField();
  84.         final FieldRotation<T>      rotation    = new FieldRotation<>(pv21.getPosition(), pv21.getVelocity(),
  85.                                                                       FieldVector3D.getPlusI(field),
  86.                                                                       FieldVector3D.getPlusJ(field));
  87.         return new FieldTransform<>(date,
  88.                                     new FieldTransform<>(date, translation),
  89.                                     new FieldTransform<>(date, rotation));
  90.     }

  91.     /** Compute the coordinates of the L1 point.
  92.      * @param primaryToSecondary relative position of secondary body with respect to primary body
  93.      * @return coordinates of the L1 point given in frame: primaryBody.getInertiallyOrientedFrame()
  94.      */
  95.     private Vector3D getL1(final Vector3D primaryToSecondary) {

  96.         // mass ratio
  97.         final double massRatio = secondaryBody.getGM() / primaryBody.getGM();

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

  101.         // Accurate position of L1 point, by solving the L1 equilibrium equation
  102.         final UnivariateFunction l1Equation = r -> {
  103.             final double bigrminusR  = bigR - r;
  104.             final double lhs         = 1.0 / ( r * r );
  105.             final double rhs1        = massRatio / (bigrminusR * bigrminusR);
  106.             final double rhs2        = 1.0 / (bigR * bigR);
  107.             final double rhs3        = (1 + massRatio) * bigrminusR * rhs2 / bigR;
  108.             return lhs - (rhs1 + rhs2 - rhs3);
  109.         };
  110.         final double[] searchInterval = UnivariateSolverUtils.bracket(l1Equation,
  111.                                                                       baseR, 0, bigR,
  112.                                                                       0.01 * bigR, 1, MAX_EVALUATIONS);
  113.         final BracketingNthOrderBrentSolver solver =
  114.                         new BracketingNthOrderBrentSolver(RELATIVE_ACCURACY,
  115.                                                           ABSOLUTE_ACCURACY,
  116.                                                           FUNCTION_ACCURACY,
  117.                                                           MAX_ORDER);
  118.         final double r = solver.solve(MAX_EVALUATIONS, l1Equation,
  119.                                       searchInterval[0], searchInterval[1],
  120.                                       AllowedSolution.ANY_SIDE);

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

  123.     }

  124.     /** Compute the coordinates of the L1 point.
  125.      * @param <T> type of the field elements
  126.      * @param primaryToSecondary relative position of secondary body with respect to primary body
  127.      * @return coordinates of the L1 point given in frame: primaryBody.getInertiallyOrientedFrame()
  128.      */
  129.     private <T extends RealFieldElement<T>> FieldVector3D<T>
  130.         getL1(final FieldVector3D<T> primaryToSecondary) {

  131.         // mass ratio
  132.         final double massRatio = secondaryBody.getGM() / primaryBody.getGM();

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

  136.         // Accurate position of L1 point, by solving the L1 equilibrium equation
  137.         final RealFieldUnivariateFunction<T> l1Equation = r -> {
  138.             final T bigrminusR = bigR.subtract(r);
  139.             final T lhs        = r.multiply(r).reciprocal();
  140.             final T rhs1       = bigrminusR.multiply(bigrminusR).reciprocal().multiply(massRatio);
  141.             final T rhs2       = bigR.multiply(bigR).reciprocal();
  142.             final T rhs3       = bigrminusR.multiply(rhs2).multiply(1 + massRatio).divide(bigR);
  143.             return lhs.subtract(rhs1.add(rhs2).add(rhs3));
  144.         };
  145.         final T zero             = primaryToSecondary.getX().getField().getZero();
  146.         final T[] searchInterval = UnivariateSolverUtils.bracket(l1Equation,
  147.                                                                  baseR, zero, bigR.multiply(2),
  148.                                                                  bigR.multiply(0.01), zero.add(1),
  149.                                                                  MAX_EVALUATIONS);


  150.         final T relativeAccuracy = zero.add(RELATIVE_ACCURACY);
  151.         final T absoluteAccuracy = zero.add(ABSOLUTE_ACCURACY);
  152.         final T functionAccuracy = zero.add(FUNCTION_ACCURACY);

  153.         final FieldBracketingNthOrderBrentSolver<T> solver =
  154.                         new FieldBracketingNthOrderBrentSolver<>(relativeAccuracy,
  155.                                                                  absoluteAccuracy,
  156.                                                                  functionAccuracy,
  157.                                                                  MAX_ORDER);
  158.         final T r = solver.solve(MAX_EVALUATIONS, l1Equation,
  159.                                  searchInterval[0], searchInterval[1],
  160.                                  AllowedSolution.ANY_SIDE);

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

  163.     }

  164. }