TimeSpanDragForce.java

  1. /* Copyright 2002-2024 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.drag;

  18. import java.lang.reflect.Array;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.stream.Stream;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.Field;
  24. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  25. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  26. import org.hipparchus.ode.events.Action;
  27. import org.hipparchus.util.MathArrays;
  28. import org.orekit.annotation.DefaultDataContext;
  29. import org.orekit.frames.Frame;
  30. import org.orekit.models.earth.atmosphere.Atmosphere;
  31. import org.orekit.propagation.FieldSpacecraftState;
  32. import org.orekit.propagation.SpacecraftState;
  33. import org.orekit.propagation.events.DateDetector;
  34. import org.orekit.propagation.events.EventDetector;
  35. import org.orekit.propagation.events.FieldDateDetector;
  36. import org.orekit.propagation.events.FieldEventDetector;
  37. import org.orekit.time.AbsoluteDate;
  38. import org.orekit.time.FieldAbsoluteDate;
  39. import org.orekit.time.FieldTimeStamped;
  40. import org.orekit.time.TimeScale;
  41. import org.orekit.time.TimeScalesFactory;
  42. import org.orekit.utils.ParameterDriver;
  43. import org.orekit.utils.TimeSpanMap;
  44. import org.orekit.utils.TimeSpanMap.Span;
  45. import org.orekit.utils.TimeSpanMap.Transition;


  46. /** Time span atmospheric drag force model.
  47.  *  <p>
  48.  *  This class is closely related to {@link org.orekit.forces.drag.DragForce DragForce} class.<br>
  49.  *  The difference is that it has a {@link TimeSpanMap} of {@link DragSensitive} objects as attribute
  50.  *  instead of a single {@link DragSensitive} object. <br>
  51.  *  The idea behind this model is to allow the user to design a drag force model that can see its physical parameters
  52.  *  (drag coefficient and lift ratio) change with time, at dates chosen by the user. <br>
  53.  *  </p>
  54.  *  <p>
  55.  *  This is a behavior that can be sought in operational orbit determination.<br>
  56.  *  Indeed the solar activity has a strong influence on the local atmospheric density, and thus on the drag force effect.<br>
  57.  *  Solar activity is a physical phenomenon that is difficult to model and predict. <br>
  58.  *  The errors induced by this incomplete modeling can be estimated through the drag coefficients.<br>
  59.  *  Being able to define and estimate drag coefficients depending on user-chosen dates in a piecewise fashion allows for
  60.  *  a better  modeling of solar activity uncertainties.
  61.  *  </p>
  62.  *  <p>
  63.  *  A typical operational use case is to have a daily solar activity with three-hourly magnetic indexes provided by an
  64.  *  international organization (NOAA for example).<br>
  65.  *  Given this input, a user can define a piecewise drag force model with daily or three-hourly drag coefficients.<br>
  66.  *  Each timed coefficient will absorb a part of the uncertainties in the solar activity and will allow for a more accurate
  67.  *  orbit determination
  68.  *  </p>
  69.  *  <b>Usage</b>:<ul>
  70.  *  <li><u>Construction</u>: constructor takes an atmospheric model and a DragSensitive model.<br>
  71.  *  This last model will be your initial DragSensitive model and it will be initially valid for the whole time line.<br>
  72.  *  The real validity of this first entry will be truncated as other DragSensitive models are added.
  73.  *  <li><u>Time spans</u>: DragSensitive models are added using methods {@link #addDragSensitiveValidAfter(DragSensitive, AbsoluteDate)}
  74.  *   or {@link #addDragSensitiveValidBefore(DragSensitive, AbsoluteDate)}.<br>
  75.  *   Recommendations are the same than the ones in {@link TimeSpanMap}, meaning: <ul>
  76.  *   <li>As an entry is added, it truncates the validity of the neighboring entries already present in the map;
  77.  *   <li><b>The transition dates should be entered only once</b>. Repeating a transition date will lead to unexpected result and is not supported;
  78.  *   <li>It is advised to order your DragSensitive models chronologically when adding them to avoid any confusion.
  79.  *   </ul>
  80.  *   <li><u>Naming the parameter drivers</u>: It is strongly advised to give a custom name to the {@link ParameterDriver}(s)
  81.  *   of each DragSensitive model that is added to the object. This will allow you keeping track of the evolution of your models.<br>
  82.  *   Different names are mandatory to differentiate the different drivers.<br>
  83.  *   If you do not specify a name, a default name will be chosen. Example for the drag coefficient:<ul>
  84.  *   <li>Initial DragSensitive model: the driver's default name is "{@link DragSensitive#DRAG_COEFFICIENT}";
  85.  *   <li>Using {@link #addDragSensitiveValidAfter(DragSensitive, AbsoluteDate)}: the driver's default name is
  86.  *   "{@link DragSensitive#DRAG_COEFFICIENT} + {@link #DATE_AFTER} + date.toString()"
  87.  *   <li>Using {@link #addDragSensitiveValidBefore(DragSensitive, AbsoluteDate)}: the driver's default name is
  88.  *   "{@link DragSensitive#DRAG_COEFFICIENT} + {@link #DATE_BEFORE} + date.toString()"
  89.  *   </ul>
  90.  *   </ul>
  91.  *  <b>Example following previous recommendations</b>:<ul>
  92.  *  <li>Given:
  93.  *  <ul>
  94.  *  <li><code>atmosphere</code>: an {@link Atmosphere atmospheric model};
  95.  *  <li><code>isotropicDrag0, 1 and 2</code>: three {@link org.orekit.forces.drag.IsotropicDrag IsotropicDrag} models;
  96.  *  <li><code>date</code>: an {@link AbsoluteDate}.
  97.  *  </ul>
  98.  *  <li>Name the drivers:<br>
  99.  *  <code>isotropicDrag0.getDragParametersDrivers()[0].setName = "Cd0";</code><br>
  100.  *  <code>isotropicDrag1.getDragParametersDrivers()[0].setName = "Cd1";</code><br>
  101.  *  <code>isotropicDrag2.getDragParametersDrivers()[0].setName = "Cd2";</code><br>
  102.  *  <li>Initialize the model: <br>
  103.  *  <code>TimeSpanDragForce force = new TimeSpanDragForce(atmosphere, isotropicDrag0);</code>
  104.  *  <li>Set the second and third model one Julian day apart each:<br>
  105.  *  <code>force.addDragSensitiveValidAfter(isotropicDrag1, date.shiftedBy(Constants.JULIAN_DAY));</code><br>
  106.  *  <code>force.addDragSensitiveValidAfter(isotropicDrag2, date.shiftedBy(2 * Constants.JULIAN_DAY));</code><br>
  107.  *  <li>With this, your model will have the following properties:
  108.  *  <ul>
  109.  *  <li>t in ]-∞, date + 1 day [ / Cd = Cd0
  110.  *  <li>t in [date + 1 day, date + 2days [ / Cd = Cd1
  111.  *  <li>t in [date + 2 days, +∞ [ / Cd = Cd2
  112.  *  </ul>
  113.  *  </ul>
  114.  *  <p>
  115.  *  <b>Warning</b>:<br> The TimeSpanDragForce model is versatile and you could end up with non-physical modeling.<br>
  116.  *  For example you could add 2 {@link org.orekit.forces.drag.IsotropicDrag IsotropicDrag} models with different areas,
  117.  *  or one {@link org.orekit.forces.drag.IsotropicDrag IsotropicDrag} model and then one
  118.  *  {@link org.orekit.forces.BoxAndSolarArraySpacecraft BoxAndSolarArraySpacecraft} model.<br>
  119.  *  It is up to you to ensure that your models are consistent with each other, Orekit will not perform any check for that.
  120.  *  </p>
  121.  * @author Maxime Journot
  122.  * @since 10.2
  123.  * @deprecated as of 12.1
  124.  */
  125. @Deprecated
  126. public class TimeSpanDragForce extends AbstractDragForceModel {
  127.     // TODO move to tests

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

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

  132.     /** TimeSpanMap of DragSensitive objects. */
  133.     private final TimeSpanMap<DragSensitive> dragSensitiveTimeSpanMap;

  134.     /** Time scale used for the default names of the drag parameter drivers. */
  135.     private final TimeScale timeScale;

  136.     /** Constructor with default UTC time scale for the default names of the drag parameter drivers.
  137.      * @param atmosphere atmospheric model
  138.      * @param spacecraft Time scale used for the default names of the drag parameter drivers
  139.      */
  140.     @DefaultDataContext
  141.     public TimeSpanDragForce(final Atmosphere atmosphere,
  142.                              final DragSensitive spacecraft) {
  143.         super(atmosphere);
  144.         this.dragSensitiveTimeSpanMap = new TimeSpanMap<>(spacecraft);
  145.         this.timeScale = TimeScalesFactory.getUTC();
  146.     }

  147.     /** Constructor.
  148.      * @param atmosphere atmospheric model
  149.      * @param spacecraft the initial object physical and geometric information
  150.      * @param timeScale Time scale used for the default names of the drag parameter drivers
  151.      */
  152.     public TimeSpanDragForce(final Atmosphere atmosphere,
  153.                              final DragSensitive spacecraft,
  154.                              final TimeScale timeScale) {
  155.         super(atmosphere);
  156.         this.dragSensitiveTimeSpanMap = new TimeSpanMap<>(spacecraft);
  157.         this.timeScale = timeScale;
  158.     }

  159.     /** Add a DragSensitive entry valid before a limit date.<br>
  160.      * Using <code>addDragSensitiveValidBefore(entry, t)</code> will make <code>entry</code>
  161.      * valid in ]-∞, t[ (note the open bracket).
  162.      * @param dragSensitive DragSensitive entry
  163.      * @param latestValidityDate date before which the entry is valid
  164.      * (must be different from <b>all</b> dates already used for transitions)
  165.      */
  166.     public void addDragSensitiveValidBefore(final DragSensitive dragSensitive, final AbsoluteDate latestValidityDate) {
  167.         dragSensitiveTimeSpanMap.addValidBefore(changeDragParameterDriversNames(dragSensitive,
  168.                                                                                 latestValidityDate,
  169.                                                                                 DATE_BEFORE),
  170.                                                 latestValidityDate, false);
  171.     }

  172.     /** Add a DragSensitive entry valid after a limit date.<br>
  173.      * Using <code>addDragSensitiveValidAfter(entry, t)</code> will make <code>entry</code>
  174.      * valid in [t, +∞[ (note the closed bracket).
  175.      * @param dragSensitive DragSensitive entry
  176.      * @param earliestValidityDate date after which the entry is valid
  177.      * (must be different from <b>all</b> dates already used for transitions)
  178.      */
  179.     public void addDragSensitiveValidAfter(final DragSensitive dragSensitive, final AbsoluteDate earliestValidityDate) {
  180.         dragSensitiveTimeSpanMap.addValidAfter(changeDragParameterDriversNames(dragSensitive,
  181.                                                                                earliestValidityDate,
  182.                                                                                DATE_AFTER),
  183.                                                earliestValidityDate, false);
  184.     }

  185.     /** Get the {@link DragSensitive} model valid at a date.
  186.      * @param date the date of validity
  187.      * @return the DragSensitive model valid at date
  188.      */
  189.     public DragSensitive getDragSensitive(final AbsoluteDate date) {
  190.         return dragSensitiveTimeSpanMap.get(date);
  191.     }

  192.     /** Get the {@link DragSensitive} {@link Span} containing a specified date.
  193.      * @param date date belonging to the desired time span
  194.      * @return the DragSensitive time span containing the specified date
  195.      */
  196.     public Span<DragSensitive> getDragSensitiveSpan(final AbsoluteDate date) {
  197.         return dragSensitiveTimeSpanMap.getSpan(date);
  198.     }

  199.     /** Extract a range of the {@link DragSensitive} map.
  200.      * <p>
  201.      * The object returned will be a new independent instance that will contain
  202.      * only the transitions that lie in the specified range.
  203.      * </p>
  204.      * See the {@link TimeSpanMap#extractRange TimeSpanMap.extractRange method} for more.
  205.      * @param start earliest date at which a transition is included in the range
  206.      * (may be set to {@link AbsoluteDate#PAST_INFINITY} to keep all early transitions)
  207.      * @param end latest date at which a transition is included in the r
  208.      * (may be set to {@link AbsoluteDate#FUTURE_INFINITY} to keep all late transitions)
  209.      * @return a new TimeSpanMap instance of DragSensitive with all transitions restricted to the specified range
  210.      */
  211.     public TimeSpanMap<DragSensitive> extractDragSensitiveRange(final AbsoluteDate start, final AbsoluteDate end) {
  212.         return dragSensitiveTimeSpanMap.extractRange(start, end);
  213.     }

  214.     /** Get the first {@link Span time span} of the drag sensitive time span map.
  215.      * @return the first {@link Span time span} of the drag sensitive time span map
  216.      * @since 11.1
  217.      */
  218.     public Span<DragSensitive> getFirstSpan() {
  219.         return dragSensitiveTimeSpanMap.getFirstSpan();
  220.     }

  221.     /** {@inheritDoc} */
  222.     @Override
  223.     public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {

  224.         // Local atmospheric density
  225.         final AbsoluteDate date     = s.getDate();
  226.         final Frame        frame    = s.getFrame();
  227.         final Vector3D     position = s.getPosition();
  228.         final double rho    = getAtmosphere().getDensity(date, position, frame);

  229.         // Spacecraft relative velocity with respect to the atmosphere
  230.         final Vector3D vAtm = getAtmosphere().getVelocity(date, position, frame);
  231.         final Vector3D relativeVelocity = vAtm.subtract(s.getPVCoordinates().getVelocity());

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

  234.         // Compute and return drag acceleration
  235.         return getDragSensitive(date).dragAcceleration(s, rho, relativeVelocity, extractedParameters);

  236.     }

  237.     /** {@inheritDoc} */
  238.     @Override
  239.     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
  240.                                                                              final T[] parameters) {
  241.         // Density and its derivatives
  242.         final T rho = getFieldDensity(s);

  243.         // Spacecraft relative velocity with respect to the atmosphere
  244.         final FieldAbsoluteDate<T> date     = s.getDate();
  245.         final Frame                frame    = s.getFrame();
  246.         final FieldVector3D<T>     position = s.getPosition();
  247.         final FieldVector3D<T> vAtm = getAtmosphere().getVelocity(date, position, frame);
  248.         final FieldVector3D<T> relativeVelocity = vAtm.subtract(s.getPVCoordinates().getVelocity());

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

  251.         // Compute and return drag acceleration
  252.         return getDragSensitive(date.toAbsoluteDate()).dragAcceleration(s, rho, relativeVelocity, extractedParameters);
  253.     }

  254.     /**{@inheritDoc}
  255.      * <p>
  256.      * A date detector is used to cleanly stop the propagator and reset
  257.      * the state derivatives at transition dates.
  258.      * </p>
  259.      */
  260.     @Override
  261.     public Stream<EventDetector> getEventDetectors() {

  262.         // Get the transitions' dates from the TimeSpanMap
  263.         final AbsoluteDate[] transitionDates = getTransitionDates();

  264.         // Initialize the date detector
  265.         final DateDetector datesDetector = new DateDetector(transitionDates[0]).
  266.                         withMaxCheck(60.).
  267.                         withHandler((state, detector, increasing) -> Action.RESET_DERIVATIVES);
  268.         // Add all transitions' dates to the date detector
  269.         for (int i = 1; i < transitionDates.length; i++) {
  270.             datesDetector.addEventDate(transitionDates[i]);
  271.         }

  272.         // Return the detector
  273.         return Stream.of(datesDetector);
  274.     }

  275.     /** {@inheritDoc}
  276.      * <p>
  277.      * A date detector is used to cleanly stop the propagator and reset
  278.      * the state derivatives at transition dates.
  279.      * </p>
  280.      */
  281.     @Override
  282.     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventDetectors(final Field<T> field) {

  283.         // Get the transitions' dates from the TimeSpanMap
  284.         final AbsoluteDate[] transitionDates = getTransitionDates();

  285.         // Initialize the date detector
  286.         @SuppressWarnings("unchecked")
  287.         final FieldDateDetector<T> datesDetector =
  288.                         new FieldDateDetector<>(field, (FieldTimeStamped<T>[]) Array.newInstance(FieldTimeStamped.class, 0)).
  289.                         withMaxCheck(60.0).
  290.                         withHandler((FieldSpacecraftState<T> state, FieldEventDetector<T> detector, boolean increasing) ->
  291.                                     Action.RESET_DERIVATIVES);
  292.         // Add all transitions' dates to the date detector
  293.         for (int i = 0; i < transitionDates.length; i++) {
  294.             datesDetector.addEventDate(new FieldAbsoluteDate<>(field, transitionDates[i]));
  295.         }

  296.         // Return the detector
  297.         return Stream.of(datesDetector);
  298.     }

  299.     /** {@inheritDoc}
  300.      * <p>
  301.      * All the parameter drivers of all DragSensitive models are returned in an array.
  302.      * Models are ordered chronologically.
  303.      * </p>
  304.      */
  305.     @Override
  306.     public List<ParameterDriver> getParametersDrivers() {

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

  309.         // Loop on the spans
  310.         for (Span<DragSensitive> span = getFirstSpan(); span != null; span = span.next()) {
  311.             // Add all the parameter drivers of the span
  312.             for (ParameterDriver driver : span.getData().getDragParametersDrivers()) {
  313.                 // Add the driver only if the name does not exist already
  314.                 if (!findByName(listParameterDrivers, driver.getName())) {
  315.                     listParameterDrivers.add(driver);
  316.                 }
  317.             }
  318.         }

  319.         // Return an array of parameter drivers with no duplicated name
  320.         return listParameterDrivers;

  321.     }

  322.     /** Extract the proper parameter drivers' values from the array in input of the
  323.      * {@link #acceleration(SpacecraftState, double[]) acceleration} method.
  324.      *  Parameters are filtered given an input date.
  325.      * @param parameters the input parameters array
  326.      * @param date the date
  327.      * @return the parameters given the date
  328.      */
  329.     public double[] extractParameters(final double[] parameters, final AbsoluteDate date) {

  330.         // Get the drag parameter drivers of the date
  331.         final List<ParameterDriver> dragParameterDriver = getDragSensitive(date).getDragParametersDrivers();

  332.         // Find out the indexes of the parameters in the whole array of parameters
  333.         final List<ParameterDriver> allParameters = getParametersDrivers();
  334.         final double[] outParameters = new double[dragParameterDriver.size()];
  335.         int index = 0;
  336.         for (int i = 0; i < allParameters.size(); i++) {
  337.             final String driverName = allParameters.get(i).getName();
  338.             for (ParameterDriver dragDriver : dragParameterDriver) {
  339.                 if (dragDriver.getName().equals(driverName)) {
  340.                     outParameters[index++] = parameters[i];
  341.                 }
  342.             }
  343.         }
  344.         return outParameters;
  345.     }

  346.     /** Extract the proper parameter drivers' values from the array in input of the
  347.      * {@link #acceleration(FieldSpacecraftState, CalculusFieldElement[]) acceleration} method.
  348.      *  Parameters are filtered given an input date.
  349.      * @param parameters the input parameters array
  350.      * @param date the date
  351.      * @param <T> extends CalculusFieldElement
  352.      * @return the parameters given the date
  353.      */
  354.     public <T extends CalculusFieldElement<T>> T[] extractParameters(final T[] parameters,
  355.                                                                  final FieldAbsoluteDate<T> date) {

  356.         // Get the drag parameter drivers of the date
  357.         final List<ParameterDriver> dragPD = getDragSensitive(date.toAbsoluteDate()).getDragParametersDrivers();

  358.         // Find out the indexes of the parameters in the whole array of parameters
  359.         final List<ParameterDriver> allParameters = getParametersDrivers();
  360.         final T[] outParameters = MathArrays.buildArray(date.getField(), dragPD.size());
  361.         int index = 0;
  362.         for (int i = 0; i < allParameters.size(); i++) {
  363.             final String driverName = allParameters.get(i).getName();
  364.             for (ParameterDriver dragDriver : dragPD) {
  365.                 if (dragDriver.getName().equals(driverName)) {
  366.                     outParameters[index++] = parameters[i];
  367.                 }
  368.             }
  369.         }
  370.         return outParameters;
  371.     }

  372.     /** Find if a parameter driver with a given name already exists in a list of parameter drivers.
  373.      * @param driversList the list of parameter drivers
  374.      * @param name the parameter driver's name to filter with
  375.      * @return true if the name was found, false otherwise
  376.      */
  377.     private boolean findByName(final List<ParameterDriver> driversList, final String name) {
  378.         for (final ParameterDriver d : driversList) {
  379.             if (d.getName().equals(name)) {
  380.                 return true;
  381.             }
  382.         }
  383.         return false;
  384.     }

  385.     /** Get the dates of the transitions for the drag sensitive models {@link TimeSpanMap}.
  386.      * @return dates of the transitions for the drag sensitive models {@link TimeSpanMap}
  387.      */
  388.     private AbsoluteDate[] getTransitionDates() {

  389.         // Get all transitions
  390.         final List<AbsoluteDate> listDates = new ArrayList<>();

  391.         // Extract all the transitions' dates
  392.         for (Transition<DragSensitive> transition = getFirstSpan().getEndTransition(); transition != null; transition = transition.next()) {
  393.             listDates.add(transition.getDate());
  394.         }
  395.         // Return the array of transition dates
  396.         return listDates.toArray(new AbsoluteDate[0]);
  397.     }

  398.     /** Change the parameter drivers names of a {@link DragSensitive} model, if needed.
  399.      * <p>
  400.      * This is done to avoid that several parameter drivers have the same name.<br>
  401.      * It is done only if the user hasn't modify the DragSensitive parameter drivers default names.
  402.      * </p>
  403.      * @param dragSensitive the DragSensitive model
  404.      * @param date the date used in the parameter driver's name
  405.      * @param datePrefix the date prefix used in the parameter driver's name
  406.      * @return the DragSensitive with its drivers' names changed
  407.      */
  408.     private DragSensitive changeDragParameterDriversNames(final DragSensitive dragSensitive,
  409.                                                           final AbsoluteDate date,
  410.                                                           final String datePrefix) {
  411.         // Loop on the parameter drivers of the DragSensitive model
  412.         for (ParameterDriver driver: dragSensitive.getDragParametersDrivers()) {
  413.             final String driverName = driver.getName();

  414.             // If the name is the default name for DragSensitive parameter drivers
  415.             // Modify the name to add the prefix and the date
  416.             if (driverName.equals(DragSensitive.GLOBAL_DRAG_FACTOR) ||
  417.                 driverName.equals(DragSensitive.DRAG_COEFFICIENT) ||
  418.                 driverName.equals(DragSensitive.LIFT_RATIO)) {
  419.                 driver.setName(driverName + datePrefix + date.toString(timeScale));
  420.             }
  421.         }
  422.         return dragSensitive;
  423.     }

  424. }