TimeSpanParametricAcceleration.java

  1. /* Copyright 2002-2022 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.forces.empirical;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.NavigableSet;
  22. import java.util.stream.Stream;

  23. import org.hipparchus.Field;
  24. import org.hipparchus.CalculusFieldElement;
  25. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.util.MathArrays;
  28. import org.orekit.attitudes.Attitude;
  29. import org.orekit.attitudes.AttitudeProvider;
  30. import org.orekit.attitudes.FieldAttitude;
  31. import org.orekit.forces.AbstractForceModel;
  32. import org.orekit.propagation.FieldSpacecraftState;
  33. import org.orekit.propagation.SpacecraftState;
  34. import org.orekit.propagation.events.EventDetector;
  35. import org.orekit.propagation.events.FieldEventDetector;
  36. import org.orekit.time.AbsoluteDate;
  37. import org.orekit.time.FieldAbsoluteDate;
  38. import org.orekit.utils.ParameterDriver;
  39. import org.orekit.utils.TimeSpanMap;
  40. import org.orekit.utils.TimeSpanMap.Span;
  41. import org.orekit.utils.TimeSpanMap.Transition;

  42. /** Time span parametric acceleration model.
  43.  *  <p>
  44.  *  This class is closely related to {@link org.orekit.forces.empirical.ParametricAcceleration ParametricAcceleration} class.<br>
  45.  *  The difference is that it has a {@link TimeSpanMap} of {@link AccelerationModel} objects as attribute
  46.  *  instead of a single {@link AccelerationModel} object. <br>
  47.  *  The idea behind this model is to allow the user to design a parametric acceleration model that can see its physical parameters
  48.  *  change with time, at dates chosen by the user. <br>
  49.  *  </p>
  50.  *  <p>
  51.  *  This is a behavior that can be sought in precise orbit determination.<br>
  52.  *  Indeed for this type of application, the empirical parameters must be revalued at
  53.  *  each new orbit.
  54.  *  </p>
  55.  *  <b>Usage</b>:<ul>
  56.  *  <li><u>Construction</u>: constructor takes an acceleration direction, an attitude mode (or an inertial flag) and
  57.  *  an AccelerationModel model.<br>
  58.  *  This last model will be your initial AccelerationModel model and it will be initially valid for the whole time line.<br>
  59.  *  The real validity of this first entry will be truncated as other AccelerationModel models are added.
  60.  *  <li><u>Time spans</u>: AccelerationModel models are added using methods {@link #addAccelerationModelValidAfter(AccelerationModel, AbsoluteDate)}
  61.  *   or {@link #addAccelerationModelValidBefore(AccelerationModel, AbsoluteDate)}.<br>
  62.  *   Recommendations are the same than the ones in {@link TimeSpanMap}, meaning: <ul>
  63.  *   <li>As an entry is added, it truncates the validity of the neighboring entries already present in the map;
  64.  *   <li><b>The transition dates should be entered only once</b>. Repeating a transition date will lead to unexpected result and is not supported;
  65.  *   <li>It is advised to order your AccelerationModel models chronologically when adding them to avoid any confusion.
  66.  *   </ul>
  67.  *   <li><u>Naming the parameter drivers</u>: It is strongly advised to give a custom name to the {@link ParameterDriver}(s)
  68.  *   of each AccelerationModel model that is added to the object. This will allow you keeping track of the evolution of your models.<br>
  69.  *   Different names are mandatory to differentiate the different drivers.<br>
  70.  *   Since there is no default name for acceleration model parameters, you must handle the driver names to consider
  71.  *   different names when adding a new acceleration model.
  72.  *   </ul>
  73.  * @author Bryan Cazabonne
  74.  * @since 10.3
  75.  */
  76. public class TimeSpanParametricAcceleration extends AbstractForceModel {

  77.     /** Prefix for dates before in the parameter drivers' name. */
  78.     public static final String DATE_BEFORE = " - Before ";

  79.     /** Prefix for dates after in the parameter drivers' name. */
  80.     public static final String DATE_AFTER = " - After ";

  81.     /** Direction of the acceleration in defining frame. */
  82.     private final Vector3D direction;

  83.     /** Flag for inertial acceleration direction. */
  84.     private final boolean isInertial;

  85.     /** The attitude to override, if set. */
  86.     private final AttitudeProvider attitudeOverride;

  87.     /** TimeSpanMap of AccelerationModel objects. */
  88.     private final TimeSpanMap<AccelerationModel> accelerationModelTimeSpanMap;

  89.     /** Simple constructor.
  90.      * @param direction acceleration direction in overridden spacecraft frame
  91.      * @param isInertial if true, direction is defined in the same inertial
  92.      * frame used for propagation (i.e. {@link SpacecraftState#getFrame()}),
  93.      * otherwise direction is defined in spacecraft frame (i.e. using the
  94.      * propagation {@link
  95.      * org.orekit.propagation.Propagator#setAttitudeProvider(AttitudeProvider)
  96.      * attitude law})
  97.      * @param accelerationModel acceleration model used to compute the contribution of the empirical acceleration
  98.      */
  99.     public TimeSpanParametricAcceleration(final Vector3D direction,
  100.                                           final boolean isInertial,
  101.                                           final AccelerationModel accelerationModel) {
  102.         this(direction, isInertial, null, accelerationModel);
  103.     }

  104.     /** Simple constructor.
  105.      * @param direction acceleration direction in overridden spacecraft frame
  106.      * frame used for propagation (i.e. {@link SpacecraftState#getFrame()}),
  107.      * otherwise direction is defined in spacecraft frame (i.e. using the
  108.      * propagation {@link
  109.      * org.orekit.propagation.Propagator#setAttitudeProvider(AttitudeProvider)
  110.      * attitude law})
  111.      * @param attitudeOverride provider for attitude used to compute acceleration
  112.      * @param accelerationModel acceleration model used to compute the contribution of the empirical acceleration
  113.      */
  114.     public TimeSpanParametricAcceleration(final Vector3D direction,
  115.                                           final AttitudeProvider attitudeOverride,
  116.                                           final AccelerationModel accelerationModel) {
  117.         this(direction, false, attitudeOverride, accelerationModel);
  118.     }

  119.     /** Simple constructor.
  120.      * @param direction acceleration direction in overridden spacecraft frame
  121.      * @param isInertial if true, direction is defined in the same inertial
  122.      * frame used for propagation (i.e. {@link SpacecraftState#getFrame()}),
  123.      * otherwise direction is defined in spacecraft frame (i.e. using the
  124.      * propagation {@link
  125.      * org.orekit.propagation.Propagator#setAttitudeProvider(AttitudeProvider)
  126.      * attitude law})
  127.      * @param attitudeOverride provider for attitude used to compute acceleration
  128.      * @param accelerationModel acceleration model used to compute the contribution of the empirical acceleration
  129.      */
  130.     private TimeSpanParametricAcceleration(final Vector3D direction,
  131.                                            final boolean isInertial,
  132.                                            final AttitudeProvider attitudeOverride,
  133.                                            final AccelerationModel accelerationModel) {
  134.         this.direction                    = direction;
  135.         this.isInertial                   = isInertial;
  136.         this.attitudeOverride             = attitudeOverride;
  137.         this.accelerationModelTimeSpanMap = new TimeSpanMap<>(accelerationModel);
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public void init(final SpacecraftState initialState, final AbsoluteDate target) {
  142.         accelerationModelTimeSpanMap.forEach(accelerationModel -> accelerationModel.init(initialState, target));
  143.     }

  144.     /** Add an AccelerationModel entry valid before a limit date.<br>
  145.      * <p>
  146.      * Using <code>addAccelerationModelValidBefore(entry, t)</code> will make <code>entry</code>
  147.      * valid in ]-∞, t[ (note the open bracket).
  148.      * <p>
  149.      * <b>WARNING</b>: Since there is no default name for acceleration model parameters,
  150.      * the user must handle itself the driver names to consider different names
  151.      * (i.e. different parameters) when adding a new acceleration model.
  152.      * @param accelerationModel AccelerationModel entry
  153.      * @param latestValidityDate date before which the entry is valid
  154.      * (must be different from <b>all</b> dates already used for transitions)
  155.      */
  156.     public void addAccelerationModelValidBefore(final AccelerationModel accelerationModel, final AbsoluteDate latestValidityDate) {
  157.         accelerationModelTimeSpanMap.addValidBefore(accelerationModel, latestValidityDate, false);
  158.     }

  159.     /** Add a AccelerationModel entry valid after a limit date.<br>
  160.      * <p>
  161.      * Using <code>addAccelerationModelValidAfter(entry, t)</code> will make <code>entry</code>
  162.      * valid in [t, +∞[ (note the closed bracket).
  163.      * <p>
  164.      * <b>WARNING</b>: Since there is no default name for acceleration model parameters,
  165.      * the user must handle itself the driver names to consider different names
  166.      * (i.e. different parameters) when adding a new acceleration model.
  167.      * @param accelerationModel AccelerationModel entry
  168.      * @param earliestValidityDate date after which the entry is valid
  169.      * (must be different from <b>all</b> dates already used for transitions)
  170.      */
  171.     public void addAccelerationModelValidAfter(final AccelerationModel accelerationModel, final AbsoluteDate earliestValidityDate) {
  172.         accelerationModelTimeSpanMap.addValidAfter(accelerationModel, earliestValidityDate, false);
  173.     }

  174.     /** Get the {@link AccelerationModel} model valid at a date.
  175.      * @param date the date of validity
  176.      * @return the AccelerationModel model valid at date
  177.      */
  178.     public AccelerationModel getAccelerationModel(final AbsoluteDate date) {
  179.         return accelerationModelTimeSpanMap.get(date);
  180.     }

  181.     /** Get the {@link AccelerationModel} {@link Span} containing a specified date.
  182.      * @param date date belonging to the desired time span
  183.      * @return the AccelerationModel time span containing the specified date
  184.      */
  185.     public Span<AccelerationModel> getAccelerationModelSpan(final AbsoluteDate date) {
  186.         return accelerationModelTimeSpanMap.getSpan(date);
  187.     }

  188.     /** Extract a range of the {@link AccelerationModel} map.
  189.      * <p>
  190.      * The object returned will be a new independent instance that will contain
  191.      * only the transitions that lie in the specified range.
  192.      * </p>
  193.      * See the {@link TimeSpanMap#extractRange TimeSpanMap.extractRange method} for more.
  194.      * @param start earliest date at which a transition is included in the range
  195.      * (may be set to {@link AbsoluteDate#PAST_INFINITY} to keep all early transitions)
  196.      * @param end latest date at which a transition is included in the r
  197.      * (may be set to {@link AbsoluteDate#FUTURE_INFINITY} to keep all late transitions)
  198.      * @return a new TimeSpanMap instance of AccelerationModel with all transitions restricted to the specified range
  199.      */
  200.     public TimeSpanMap<AccelerationModel> extractAccelerationModelRange(final AbsoluteDate start, final AbsoluteDate end) {
  201.         return accelerationModelTimeSpanMap.extractRange(start, end);
  202.     }

  203.     /** Get the {@link Transition}s of the acceleration model time span map.
  204.      * @return the {@link Transition}s for the acceleration model time span map
  205.      * @deprecated as of 11.1, replace by {@link #getFirstSpan()}
  206.      */
  207.     @Deprecated
  208.     public NavigableSet<Transition<AccelerationModel>> getTransitions() {
  209.         return accelerationModelTimeSpanMap.getTransitions();
  210.     }

  211.     /** Get the first {@link Span time span} of the acceleration model time span map.
  212.      * @return the first {@link Span time span} of the acceleration model time span map
  213.      * @since 11.1
  214.      */
  215.     public Span<AccelerationModel> getFirstSpan() {
  216.         return accelerationModelTimeSpanMap.getFirstSpan();
  217.     }

  218.     /** {@inheritDoc} */
  219.     @Override
  220.     public boolean dependsOnPositionOnly() {
  221.         return isInertial;
  222.     }

  223.     /** {@inheritDoc} */
  224.     @Override
  225.     public Vector3D acceleration(final SpacecraftState state,
  226.                                  final double[] parameters) {

  227.         // Date
  228.         final AbsoluteDate date = state.getDate();

  229.         // Compute inertial direction
  230.         final Vector3D inertialDirection;
  231.         if (isInertial) {
  232.             // the acceleration direction is already defined in the inertial frame
  233.             inertialDirection = direction;
  234.         } else {
  235.             final Attitude attitude;
  236.             if (attitudeOverride == null) {
  237.                 // the acceleration direction is defined in spacecraft frame as set by the propagator
  238.                 attitude = state.getAttitude();
  239.             } else {
  240.                 // the acceleration direction is defined in a dedicated frame
  241.                 attitude = attitudeOverride.getAttitude(state.getOrbit(), date, state.getFrame());
  242.             }
  243.             inertialDirection = attitude.getRotation().applyInverseTo(direction);
  244.         }

  245.         // Extract the proper parameters valid at date from the input array
  246.         final double[] extractedParameters = extractParameters(parameters, date);

  247.         // Compute and return the parametric acceleration
  248.         return new Vector3D(getAccelerationModel(date).signedAmplitude(state, extractedParameters), inertialDirection);

  249.     }

  250.     /** {@inheritDoc} */
  251.     @Override
  252.     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> state,
  253.                                                                          final T[] parameters) {

  254.         // Date
  255.         final FieldAbsoluteDate<T> date = state.getDate();

  256.         // Compute inertial direction
  257.         final FieldVector3D<T> inertialDirection;
  258.         if (isInertial) {
  259.             // the acceleration direction is already defined in the inertial frame
  260.             inertialDirection = new FieldVector3D<>(state.getDate().getField(), direction);
  261.         } else {
  262.             final FieldAttitude<T> attitude;
  263.             if (attitudeOverride == null) {
  264.                 // the acceleration direction is defined in spacecraft frame as set by the propagator
  265.                 attitude = state.getAttitude();
  266.             } else {
  267.                 // the acceleration direction is defined in a dedicated frame
  268.                 attitude = attitudeOverride.getAttitude(state.getOrbit(), date, state.getFrame());
  269.             }
  270.             inertialDirection = attitude.getRotation().applyInverseTo(direction);
  271.         }

  272.         // Extract the proper parameters valid at date from the input array
  273.         final T[] extractedParameters = extractParameters(parameters, date);

  274.         // Compute and return the parametric acceleration
  275.         return new FieldVector3D<>(getAccelerationModel(date.toAbsoluteDate()).signedAmplitude(state, extractedParameters), inertialDirection);

  276.     }

  277.     /** {@inheritDoc} */
  278.     @Override
  279.     public Stream<EventDetector> getEventsDetectors() {
  280.         return Stream.empty();
  281.     }

  282.     /** {@inheritDoc} */
  283.     @Override
  284.     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventsDetectors(final Field<T> field) {
  285.         return Stream.empty();
  286.     }

  287.     /** {@inheritDoc}
  288.      * <p>
  289.      * All the parameter drivers of all AccelerationModel models are returned in an array.
  290.      * Models are ordered chronologically.
  291.      * </p>
  292.      */
  293.     @Override
  294.     public List<ParameterDriver> getParametersDrivers() {

  295.         // Get all transitions from the TimeSpanMap
  296.         final List<ParameterDriver> listParameterDrivers = new ArrayList<>();

  297.         // Loop on the spans
  298.         for (Span<AccelerationModel> span = getFirstSpan(); span != null; span = span.next()) {
  299.             // Add all the parameter drivers of the time span
  300.             for (ParameterDriver driver : span.getData().getParametersDrivers()) {
  301.                 // Add the driver only if the name does not exist already
  302.                 if (!findByName(listParameterDrivers, driver.getName())) {
  303.                     listParameterDrivers.add(driver);
  304.                 }
  305.             }
  306.         }

  307.         // Return an array of parameter drivers with no duplicated name
  308.         return Collections.unmodifiableList(listParameterDrivers);

  309.     }

  310.     /** Extract the proper parameter drivers' values from the array in input of the
  311.      * {@link #acceleration(SpacecraftState, double[]) acceleration} method.
  312.      *  Parameters are filtered given an input date.
  313.      * @param parameters the input parameters array
  314.      * @param date the date
  315.      * @return the parameters given the date
  316.      */
  317.     public double[] extractParameters(final double[] parameters, final AbsoluteDate date) {

  318.         // Get the acceleration model parameter drivers of the date
  319.         final List<ParameterDriver> empiricalParameterDriver = getAccelerationModel(date).getParametersDrivers();

  320.         // Find out the indexes of the parameters in the whole array of parameters
  321.         final List<ParameterDriver> allParameters = getParametersDrivers();
  322.         final double[] outParameters = new double[empiricalParameterDriver.size()];
  323.         int index = 0;
  324.         for (int i = 0; i < allParameters.size(); i++) {
  325.             final String driverName = allParameters.get(i).getName();
  326.             for (ParameterDriver accDriver : empiricalParameterDriver) {
  327.                 if (accDriver.getName().equals(driverName)) {
  328.                     outParameters[index++] = parameters[i];
  329.                 }
  330.             }
  331.         }
  332.         return outParameters;
  333.     }

  334.     /** Extract the proper parameter drivers' values from the array in input of the
  335.      * {@link #acceleration(FieldSpacecraftState, CalculusFieldElement[]) acceleration} method.
  336.      *  Parameters are filtered given an input date.
  337.      * @param parameters the input parameters array
  338.      * @param date the date
  339.      * @param <T> extends CalculusFieldElement
  340.      * @return the parameters given the date
  341.      */
  342.     public <T extends CalculusFieldElement<T>> T[] extractParameters(final T[] parameters,
  343.                                                                  final FieldAbsoluteDate<T> date) {

  344.         // Get the acceleration parameter drivers of the date
  345.         final List<ParameterDriver> empiricalParameterDriver = getAccelerationModel(date.toAbsoluteDate()).getParametersDrivers();

  346.         // Find out the indexes of the parameters in the whole array of parameters
  347.         final List<ParameterDriver> allParameters = getParametersDrivers();
  348.         final T[] outParameters = MathArrays.buildArray(date.getField(), empiricalParameterDriver.size());
  349.         int index = 0;
  350.         for (int i = 0; i < allParameters.size(); i++) {
  351.             final String driverName = allParameters.get(i).getName();
  352.             for (ParameterDriver accDriver : empiricalParameterDriver) {
  353.                 if (accDriver.getName().equals(driverName)) {
  354.                     outParameters[index++] = parameters[i];
  355.                 }
  356.             }
  357.         }
  358.         return outParameters;
  359.     }

  360.     /** Find if a parameter driver with a given name already exists in a list of parameter drivers.
  361.      * @param driversList the list of parameter drivers
  362.      * @param name the parameter driver's name to filter with
  363.      * @return true if the name was found, false otherwise
  364.      */
  365.     private boolean findByName(final List<ParameterDriver> driversList, final String name) {
  366.         for (final ParameterDriver d : driversList) {
  367.             if (d.getName().equals(name)) {
  368.                 return true;
  369.             }
  370.         }
  371.         return false;
  372.     }

  373. }