FieldLongitudeCrossingDetector.java

  1. /* Copyright 2023-2024 Alberto Ferrero
  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.  * Alberto Ferrero 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.propagation.events;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.util.FastMath;
  21. import org.hipparchus.util.MathUtils;
  22. import org.orekit.bodies.FieldGeodeticPoint;
  23. import org.orekit.bodies.OneAxisEllipsoid;
  24. import org.orekit.propagation.FieldSpacecraftState;
  25. import org.orekit.propagation.events.handlers.FieldContinueOnEvent;
  26. import org.orekit.propagation.events.handlers.FieldEventHandler;
  27. import org.orekit.propagation.events.handlers.FieldStopOnIncreasing;
  28. import org.orekit.time.FieldAbsoluteDate;

  29. /** Detector for geographic longitude crossing.
  30.  * <p>This detector identifies when a spacecraft crosses a fixed
  31.  * longitude with respect to a central body.</p>
  32.  * @author Alberto Ferrero
  33.  * @since 12.0
  34.  * @param <T> type of the field elements
  35.  */
  36. public class FieldLongitudeCrossingDetector <T extends CalculusFieldElement<T>>
  37.     extends FieldAbstractDetector<FieldLongitudeCrossingDetector<T>, T> {

  38.     /**
  39.     * Body on which the longitude is defined.
  40.     */
  41.     private OneAxisEllipsoid body;

  42.     /**
  43.     * Fixed longitude to be crossed.
  44.     */
  45.     private final double longitude;

  46.     /**
  47.     * Filtering detector.
  48.     */
  49.     private final FieldEventEnablingPredicateFilter<T> filtering;

  50.     /**
  51.     * Build a new detector.
  52.     * <p>The new instance uses default values for maximal checking interval
  53.     * ({@link #DEFAULT_MAXCHECK}) and convergence threshold ({@link
  54.     * #DEFAULT_THRESHOLD}).</p>
  55.     *
  56.     * @param field     the type of numbers to use.
  57.     * @param body      body on which the longitude is defined
  58.     * @param longitude longitude to be crossed
  59.     */
  60.     public FieldLongitudeCrossingDetector(final Field<T> field, final OneAxisEllipsoid body, final double longitude) {
  61.         this(FieldAdaptableInterval.of(DEFAULT_MAXCHECK),
  62.             field.getZero().newInstance(DEFAULT_THRESHOLD), DEFAULT_MAX_ITER, new FieldStopOnIncreasing<>(), body, longitude);
  63.     }

  64.     /**
  65.     * Build a detector.
  66.     *
  67.     * @param maxCheck  maximal checking interval (s)
  68.     * @param threshold convergence threshold (s)
  69.     * @param body      body on which the longitude is defined
  70.     * @param longitude longitude to be crossed
  71.     */
  72.     public FieldLongitudeCrossingDetector(final T maxCheck,
  73.                                           final T threshold,
  74.                                           final OneAxisEllipsoid body,
  75.                                           final double longitude) {
  76.         this(FieldAdaptableInterval.of(maxCheck.getReal()), threshold, DEFAULT_MAX_ITER, new FieldStopOnIncreasing<>(), body, longitude);
  77.     }

  78.     /**
  79.     * Protected constructor with full parameters.
  80.     * <p>
  81.     * This constructor is not public as users are expected to use the builder
  82.     * API with the various {@code withXxx()} methods to set up the instance
  83.     * in a readable manner without using a huge amount of parameters.
  84.     * </p>
  85.     *
  86.     * @param maxCheck  maximum checking interval
  87.     * @param threshold convergence threshold (s)
  88.     * @param maxIter   maximum number of iterations in the event time search
  89.     * @param handler   event handler to call at event occurrences
  90.     * @param body      body on which the longitude is defined
  91.     * @param longitude longitude to be crossed
  92.     */
  93.     protected FieldLongitudeCrossingDetector(
  94.         final FieldAdaptableInterval<T> maxCheck,
  95.         final T threshold,
  96.         final int maxIter,
  97.         final FieldEventHandler<T> handler,
  98.         final OneAxisEllipsoid body,
  99.         final double longitude) {

  100.         super(maxCheck, threshold, maxIter, handler);

  101.         this.body = body;
  102.         this.longitude = longitude;

  103.         // we filter out spurious longitude crossings occurring at the antimeridian
  104.         final FieldRawLongitudeCrossingDetector<T> raw = new FieldRawLongitudeCrossingDetector<>(maxCheck, threshold, maxIter,
  105.             new FieldContinueOnEvent<>());
  106.         final FieldEnablingPredicate<T> predicate =
  107.             (state, detector, g) -> FastMath.abs(g).getReal() < 0.5 * FastMath.PI;
  108.         this.filtering = new FieldEventEnablingPredicateFilter<T>(raw, predicate);

  109.     }

  110.     /**
  111.     * {@inheritDoc}
  112.     */
  113.     @Override
  114.     protected FieldLongitudeCrossingDetector<T> create(
  115.         final FieldAdaptableInterval<T> newMaxCheck,
  116.         final T newThreshold,
  117.         final int newMaxIter,
  118.         final FieldEventHandler<T> newHandler) {
  119.         return new FieldLongitudeCrossingDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler,
  120.             body, longitude);
  121.     }

  122.     /**
  123.     * Get the body on which the geographic zone is defined.
  124.     *
  125.     * @return body on which the geographic zone is defined
  126.     */
  127.     public OneAxisEllipsoid getBody() {
  128.         return body;
  129.     }

  130.     /**
  131.     * Get the fixed longitude to be crossed (radians).
  132.     *
  133.     * @return fixed longitude to be crossed (radians)
  134.     */
  135.     public double getLongitude() {
  136.         return longitude;
  137.     }

  138.     /**
  139.     * {@inheritDoc}
  140.     */
  141.     public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
  142.         filtering.init(s0, t);
  143.     }

  144.     /**
  145.     * Compute the value of the detection function.
  146.     * <p>
  147.     * The value is the longitude difference between the spacecraft and the fixed
  148.     * longitude to be crossed, with some sign tweaks to ensure continuity.
  149.     * These tweaks imply the {@code increasing} flag in events detection becomes
  150.     * irrelevant here! As an example, the longitude of a prograde spacecraft
  151.     * will always increase, but this g function will increase and decrease so it
  152.     * will cross the zero value once per orbit, in increasing and decreasing
  153.     * directions on alternate orbits. If eastwards and westwards crossing have to
  154.     * be distinguished, the velocity direction has to be checked instead of looking
  155.     * at the {@code increasing} flag.
  156.     * </p>
  157.     *
  158.     * @param s the current state information: date, kinematics, attitude
  159.     * @return longitude difference between the spacecraft and the fixed
  160.     * longitude, with some sign tweaks to ensure continuity
  161.     */
  162.     public T g(final FieldSpacecraftState<T> s) {
  163.         return filtering.g(s);
  164.     }

  165.     private class FieldRawLongitudeCrossingDetector <TT extends CalculusFieldElement<TT>>
  166.         extends FieldAbstractDetector<FieldRawLongitudeCrossingDetector<TT>, TT> {

  167.         /**
  168.         * Protected constructor with full parameters.
  169.         * <p>
  170.         * This constructor is not public as users are expected to use the builder
  171.         * API with the various {@code withXxx()} methods to set up the instance
  172.         * in a readable manner without using a huge amount of parameters.
  173.         * </p>
  174.         *
  175.         * @param maxCheck  maximum checking interval
  176.         * @param threshold convergence threshold (s)
  177.         * @param maxIter   maximum number of iterations in the event time search
  178.         * @param handler   event handler to call at event occurrences
  179.         */
  180.         protected FieldRawLongitudeCrossingDetector(
  181.             final FieldAdaptableInterval<TT> maxCheck,
  182.             final TT threshold,
  183.             final int maxIter,
  184.             final FieldEventHandler<TT> handler) {
  185.             super(maxCheck, threshold, maxIter, handler);
  186.         }

  187.         /**
  188.         * {@inheritDoc}
  189.         */
  190.         @Override
  191.         protected FieldRawLongitudeCrossingDetector<TT> create(
  192.             final FieldAdaptableInterval<TT> newMaxCheck,
  193.             final TT newThreshold,
  194.             final int newMaxIter,
  195.             final FieldEventHandler<TT> newHandler) {
  196.             return new FieldRawLongitudeCrossingDetector<>(newMaxCheck, newThreshold, newMaxIter, newHandler);
  197.         }

  198.         /**
  199.         * Compute the value of the detection function.
  200.         * <p>
  201.         * The value is the longitude difference between the spacecraft and the fixed
  202.         * longitude to be crossed, and it <em>does</em> change sign twice around
  203.         * the central body: once at expected longitude and once at antimeridian.
  204.         * The second sign change is a spurious one and is filtered out by the
  205.         * outer class.
  206.         * </p>
  207.         *
  208.         * @param s the current state information: date, kinematics, attitude
  209.         * @return longitude difference between the spacecraft and the fixed
  210.         * longitude
  211.         */
  212.         public TT g(final FieldSpacecraftState<TT> s) {

  213.             // convert state to geodetic coordinates
  214.             final FieldGeodeticPoint<TT> gp = body.transform(s.getPosition(),
  215.                 s.getFrame(), s.getDate());

  216.             // longitude difference
  217.             final TT zero = gp.getLongitude().getField().getZero();
  218.             return MathUtils.normalizeAngle(gp.getLongitude().subtract(longitude), zero);

  219.         }

  220.     }

  221. }