TimeSpanDragForce.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.forces.drag;

  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.stream.Stream;

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


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

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

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

  127.     /** TimeSpanMap of DragSensitive objects. */
  128.     private final TimeSpanMap<DragSensitive> dragSensitiveTimeSpanMap;

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

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

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

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

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

  180.     /** Get the {@link DragSensitive} model valid at a date.
  181.      * @param date the date of validity
  182.      * @return the DragSensitive model valid at date
  183.      */
  184.     public DragSensitive getDragSensitive(final AbsoluteDate date) {
  185.         return dragSensitiveTimeSpanMap.get(date);
  186.     }

  187.     /** Get the {@link DragSensitive} {@link Span} containing a specified date.
  188.      * @param date date belonging to the desired time span
  189.      * @return the DragSensitive time span containing the specified date
  190.      */
  191.     public Span<DragSensitive> getDragSensitiveSpan(final AbsoluteDate date) {
  192.         return dragSensitiveTimeSpanMap.getSpan(date);
  193.     }

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

  209.     /** Get the first {@link Span time span} of the drag sensitive time span map.
  210.      * @return the first {@link Span time span} of the drag sensitive time span map
  211.      * @since 11.1
  212.      */
  213.     public Span<DragSensitive> getFirstSpan() {
  214.         return dragSensitiveTimeSpanMap.getFirstSpan();
  215.     }

  216.     /** {@inheritDoc} */
  217.     @Override
  218.     public Vector3D acceleration(final SpacecraftState s, final double[] parameters) {

  219.         // Local atmospheric density
  220.         final AbsoluteDate date     = s.getDate();
  221.         final Frame        frame    = s.getFrame();
  222.         final Vector3D     position = s.getPosition();
  223.         final double rho    = getAtmosphere().getDensity(date, position, frame);

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

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

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

  231.     }

  232.     /** {@inheritDoc} */
  233.     @Override
  234.     public <T extends CalculusFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
  235.                                                                              final T[] parameters) {
  236.         // Density and its derivatives
  237.         final T rho = getFieldDensity(s);

  238.         // Spacecraft relative velocity with respect to the atmosphere
  239.         final FieldAbsoluteDate<T> date     = s.getDate();
  240.         final Frame                frame    = s.getFrame();
  241.         final FieldVector3D<T>     position = s.getPosition();
  242.         final FieldVector3D<T> vAtm = getAtmosphere().getVelocity(date, position, frame);
  243.         final FieldVector3D<T> relativeVelocity = vAtm.subtract(s.getPVCoordinates().getVelocity());

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

  246.         // Compute and return drag acceleration
  247.         return getDragSensitive(date.toAbsoluteDate()).dragAcceleration(s, rho, relativeVelocity, extractedParameters);
  248.     }

  249.     /**{@inheritDoc}
  250.      * <p>
  251.      * A date detector is used to cleanly stop the propagator and reset
  252.      * the state derivatives at transition dates.
  253.      * </p>
  254.      */
  255.     @Override
  256.     public Stream<EventDetector> getEventDetectors() {

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

  259.         // create detector and return it in Stream
  260.         final DateDetector detector = getDateDetector(transitionDates);
  261.         return Stream.of(detector);
  262.     }

  263.     /** {@inheritDoc}
  264.      * <p>
  265.      * A date detector is used to cleanly stop the propagator and reset
  266.      * the state derivatives at transition dates.
  267.      * </p>
  268.      */
  269.     @Override
  270.     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventDetectors(final Field<T> field) {

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

  273.         // create detector and return it in Stream
  274.         final FieldDateDetector<T> detector = getFieldDateDetector(field, transitionDates);
  275.         return Stream.of(detector);
  276.     }

  277.     /** {@inheritDoc}
  278.      * <p>
  279.      * All the parameter drivers of all DragSensitive models are returned in an array.
  280.      * Models are ordered chronologically.
  281.      * </p>
  282.      */
  283.     @Override
  284.     public List<ParameterDriver> getParametersDrivers() {

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

  287.         // Loop on the spans
  288.         for (Span<DragSensitive> span = getFirstSpan(); span != null; span = span.next()) {
  289.             // Add all the parameter drivers of the span
  290.             for (ParameterDriver driver : span.getData().getDragParametersDrivers()) {
  291.                 // Add the driver only if the name does not exist already
  292.                 if (!findByName(listParameterDrivers, driver.getName())) {
  293.                     listParameterDrivers.add(driver);
  294.                 }
  295.             }
  296.         }

  297.         // Return an array of parameter drivers with no duplicated name
  298.         return listParameterDrivers;

  299.     }

  300.     /** Extract the proper parameter drivers' values from the array in input of the
  301.      * {@link #acceleration(SpacecraftState, double[]) acceleration} method.
  302.      *  Parameters are filtered given an input date.
  303.      * @param parameters the input parameters array
  304.      * @param date the date
  305.      * @return the parameters given the date
  306.      */
  307.     public double[] extractParameters(final double[] parameters, final AbsoluteDate date) {

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

  310.         // Find out the indexes of the parameters in the whole array of parameters
  311.         final List<ParameterDriver> allParameters = getParametersDrivers();
  312.         final double[] outParameters = new double[dragParameterDriver.size()];
  313.         int index = 0;
  314.         for (int i = 0; i < allParameters.size(); i++) {
  315.             final String driverName = allParameters.get(i).getName();
  316.             for (ParameterDriver dragDriver : dragParameterDriver) {
  317.                 if (dragDriver.getName().equals(driverName)) {
  318.                     outParameters[index++] = parameters[i];
  319.                 }
  320.             }
  321.         }
  322.         return outParameters;
  323.     }

  324.     /** Extract the proper parameter drivers' values from the array in input of the
  325.      * {@link #acceleration(FieldSpacecraftState, CalculusFieldElement[]) acceleration} method.
  326.      *  Parameters are filtered given an input date.
  327.      * @param parameters the input parameters array
  328.      * @param date the date
  329.      * @param <T> extends CalculusFieldElement
  330.      * @return the parameters given the date
  331.      */
  332.     public <T extends CalculusFieldElement<T>> T[] extractParameters(final T[] parameters,
  333.                                                                  final FieldAbsoluteDate<T> date) {

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

  336.         // Find out the indexes of the parameters in the whole array of parameters
  337.         final List<ParameterDriver> allParameters = getParametersDrivers();
  338.         final T[] outParameters = MathArrays.buildArray(date.getField(), dragPD.size());
  339.         int index = 0;
  340.         for (int i = 0; i < allParameters.size(); i++) {
  341.             final String driverName = allParameters.get(i).getName();
  342.             for (ParameterDriver dragDriver : dragPD) {
  343.                 if (dragDriver.getName().equals(driverName)) {
  344.                     outParameters[index++] = parameters[i];
  345.                 }
  346.             }
  347.         }
  348.         return outParameters;
  349.     }

  350.     /** Find if a parameter driver with a given name already exists in a list of parameter drivers.
  351.      * @param driversList the list of parameter drivers
  352.      * @param name the parameter driver's name to filter with
  353.      * @return true if the name was found, false otherwise
  354.      */
  355.     private boolean findByName(final List<ParameterDriver> driversList, final String name) {
  356.         return ParameterDriversProvider.findByName(driversList, name);
  357.     }

  358.     /** Get the dates of the transitions for the drag sensitive models {@link TimeSpanMap}.
  359.      * @return dates of the transitions for the drag sensitive models {@link TimeSpanMap}
  360.      */
  361.     private AbsoluteDate[] getTransitionDates() {

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

  364.         // Extract all the transitions' dates
  365.         for (Transition<DragSensitive> transition = getFirstSpan().getEndTransition(); transition != null; transition = transition.next()) {
  366.             listDates.add(transition.getDate());
  367.         }
  368.         // Return the array of transition dates
  369.         return listDates.toArray(new AbsoluteDate[0]);
  370.     }

  371.     /** Change the parameter drivers names of a {@link DragSensitive} model, if needed.
  372.      * <p>
  373.      * This is done to avoid that several parameter drivers have the same name.<br>
  374.      * It is done only if the user hasn't modify the DragSensitive parameter drivers default names.
  375.      * </p>
  376.      * @param dragSensitive the DragSensitive model
  377.      * @param date the date used in the parameter driver's name
  378.      * @param datePrefix the date prefix used in the parameter driver's name
  379.      * @return the DragSensitive with its drivers' names changed
  380.      */
  381.     private DragSensitive changeDragParameterDriversNames(final DragSensitive dragSensitive,
  382.                                                           final AbsoluteDate date,
  383.                                                           final String datePrefix) {
  384.         // Loop on the parameter drivers of the DragSensitive model
  385.         for (ParameterDriver driver: dragSensitive.getDragParametersDrivers()) {
  386.             final String driverName = driver.getName();

  387.             // If the name is the default name for DragSensitive parameter drivers
  388.             // Modify the name to add the prefix and the date
  389.             if (driverName.equals(DragSensitive.GLOBAL_DRAG_FACTOR) ||
  390.                 driverName.equals(DragSensitive.DRAG_COEFFICIENT) ||
  391.                 driverName.equals(DragSensitive.LIFT_RATIO)) {
  392.                 driver.setName(driverName + datePrefix + date.toString(timeScale));
  393.             }
  394.         }
  395.         return dragSensitive;
  396.     }

  397. }