DragForce.java

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

  18. import java.util.stream.Stream;

  19. import org.hipparchus.Field;
  20. import org.hipparchus.RealFieldElement;
  21. import org.hipparchus.analysis.differentiation.DSFactory;
  22. import org.hipparchus.analysis.differentiation.DerivativeStructure;
  23. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  24. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.forces.AbstractForceModel;
  27. import org.orekit.forces.drag.atmosphere.Atmosphere;
  28. import org.orekit.frames.Frame;
  29. import org.orekit.frames.Transform;
  30. import org.orekit.propagation.FieldSpacecraftState;
  31. import org.orekit.propagation.SpacecraftState;
  32. import org.orekit.propagation.events.EventDetector;
  33. import org.orekit.propagation.events.FieldEventDetector;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.time.FieldAbsoluteDate;
  36. import org.orekit.utils.FieldPVCoordinates;
  37. import org.orekit.utils.ParameterDriver;


  38. /** Atmospheric drag force model.
  39.  *
  40.  * The drag acceleration is computed as follows :
  41.  *
  42.  * γ = (1/2 * ρ * V² * S / Mass) * DragCoefVector
  43.  *
  44.  * With DragCoefVector = {C<sub>x</sub>, C<sub>y</sub>, C<sub>z</sub>} and S given by the user through the interface
  45.  * {@link DragSensitive}
  46.  *
  47.  * @author &Eacute;douard Delente
  48.  * @author Fabien Maussion
  49.  * @author V&eacute;ronique Pommier-Maurussane
  50.  * @author Pascal Parraud
  51.  */

  52. public class DragForce extends AbstractForceModel {

  53.     /** Atmospheric model. */
  54.     private final Atmosphere atmosphere;

  55.     /** Spacecraft. */
  56.     private final DragSensitive spacecraft;

  57.     /** Simple constructor.
  58.      * @param atmosphere atmospheric model
  59.      * @param spacecraft the object physical and geometrical information
  60.      */
  61.     public DragForce(final Atmosphere atmosphere, final DragSensitive spacecraft) {
  62.         this.atmosphere = atmosphere;
  63.         this.spacecraft = spacecraft;
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public boolean dependsOnPositionOnly() {
  68.         return false;
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public Vector3D acceleration(final SpacecraftState s, final double[] parameters)
  73.         throws OrekitException {

  74.         final AbsoluteDate date     = s.getDate();
  75.         final Frame        frame    = s.getFrame();
  76.         final Vector3D     position = s.getPVCoordinates().getPosition();

  77.         final double rho    = atmosphere.getDensity(date, position, frame);
  78.         final Vector3D vAtm = atmosphere.getVelocity(date, position, frame);
  79.         final Vector3D relativeVelocity = vAtm.subtract(s.getPVCoordinates().getVelocity());

  80.         return spacecraft.dragAcceleration(date, frame, position, s.getAttitude().getRotation(),
  81.                                            s.getMass(), rho, relativeVelocity, parameters);

  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public <T extends RealFieldElement<T>> FieldVector3D<T> acceleration(final FieldSpacecraftState<T> s,
  86.                                                                          final T[] parameters)
  87.         throws OrekitException {

  88.         final FieldAbsoluteDate<T> date     = s.getDate();
  89.         final Frame                frame    = s.getFrame();
  90.         final FieldVector3D<T>     position = s.getPVCoordinates().getPosition();

  91.         // Density and its derivatives
  92.         final T rho;

  93.         // Check for faster computation dedicated to derivatives with respect to state
  94.         // Using finite differences instead of automatic differentiation as it seems to be much
  95.         // faster for the drag's derivatives' computation
  96.         if (isStateDerivative(s)) {
  97.             rho = this.getDensityWrtStateUsingFiniteDifferences(date.toAbsoluteDate(), frame, position);
  98.         } else {
  99.             rho    = atmosphere.getDensity(date, position, frame);
  100.         }

  101.         // Spacecraft relative velocity with respect to the atmosphere
  102.         final FieldVector3D<T> vAtm = atmosphere.getVelocity(date, position, frame);
  103.         final FieldVector3D<T> relativeVelocity = vAtm.subtract(s.getPVCoordinates().getVelocity());

  104.         // Drag acceleration along with its derivatives
  105.         return spacecraft.dragAcceleration(date, frame, position, s.getAttitude().getRotation(),
  106.                                            s.getMass(), rho, relativeVelocity, parameters);

  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public Stream<EventDetector> getEventsDetectors() {
  111.         return Stream.empty();
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public <T extends RealFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventsDetectors(final Field<T> field) {
  116.         return Stream.empty();
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public ParameterDriver[] getParametersDrivers() {
  121.         return spacecraft.getDragParametersDrivers();
  122.     }

  123.     /** Check if a field state corresponds to derivatives with respect to state.
  124.      * @param state state to check
  125.      * @param <T> type of the field elements
  126.      * @return true if state corresponds to derivatives with respect to state
  127.      * @since 9.0
  128.      */
  129.     private <T extends RealFieldElement<T>> boolean isStateDerivative(final FieldSpacecraftState<T> state) {
  130.         try {
  131.             final DerivativeStructure dsMass = (DerivativeStructure) state.getMass();
  132.             final int o = dsMass.getOrder();
  133.             final int p = dsMass.getFreeParameters();

  134.             // To be in the desired case:
  135.             // Order must be 1 (first order derivatives only)
  136.             // Number of parameters must be 6 (PV), 7 (PV + drag coefficient) or 8 (PV + drag coefficient + lift ratio)
  137.             if (o != 1 || (p != 6 && p != 7 && p != 8)) {
  138.                 return false;
  139.             }

  140.             // Check that the first 6 parameters are position and velocity
  141.             @SuppressWarnings("unchecked")
  142.             final FieldPVCoordinates<DerivativeStructure> pv = (FieldPVCoordinates<DerivativeStructure>) state.getPVCoordinates();
  143.             return isVariable(pv.getPosition().getX(), 0) &&
  144.                    isVariable(pv.getPosition().getY(), 1) &&
  145.                    isVariable(pv.getPosition().getZ(), 2) &&
  146.                    isVariable(pv.getVelocity().getX(), 3) &&
  147.                    isVariable(pv.getVelocity().getY(), 4) &&
  148.                    isVariable(pv.getVelocity().getZ(), 5);
  149.         } catch (ClassCastException cce) {
  150.             return false;
  151.         }
  152.     }

  153.     /** Check if a derivative represents a specified variable.
  154.      * @param ds derivative to check
  155.      * @param index index of the variable
  156.      * @return true if the derivative represents a specified variable
  157.      * @since 9.0
  158.      */
  159.     private boolean isVariable(final DerivativeStructure ds, final int index) {
  160.         final double[] derivatives = ds.getAllDerivatives();
  161.         boolean check = true;
  162.         for (int i = 1; i < derivatives.length; ++i) {
  163.             check &= derivatives[i] == ((index + 1 == i) ? 1.0 : 0.0);
  164.         }
  165.         return check;
  166.     }

  167.     /** Compute density and its derivatives.
  168.      * Using finite differences for the derivatives.
  169.      * And doing the actual computation only for the derivatives with respect to position (others are set to 0.).
  170.      * <p>
  171.      * From a theoretical point of view, this method computes the same values
  172.      * as {@link Atmosphere#getDensity(FieldAbsoluteDate, FieldVector3D, Frame)} in the
  173.      * specific case of {@link DerivativeStructure} with respect to state, so
  174.      * it is less general. However, it is *much* faster in this important case.
  175.      * <p>
  176.      * <p>
  177.      * The derivatives should be computed with respect to position. The input
  178.      * parameters already take into account the free parameters (6, 7 or 8 depending
  179.      * on derivation with respect to drag coefficient and lift ratio being considered or not)
  180.      * and order (always 1). Free parameters at indices 0, 1 and 2 correspond to derivatives
  181.      * with respect to position. Free parameters at indices 3, 4 and 5 correspond
  182.      * to derivatives with respect to velocity (these derivatives will remain zero
  183.      * as the atmospheric density does not depend on velocity). Free parameter
  184.      * at indexes 6 and 7 (if present) corresponds to derivatives with respect to drag coefficient
  185.      * and/or lift ratio (one of these or both).
  186.      * This 2 last derivatives will remain zero as atmospheric density does not depend on them.
  187.      * </p>
  188.      * @param date current date
  189.      * @param frame inertial reference frame for state (both orbit and attitude)
  190.      * @param position position of spacecraft in inertial frame
  191.      * @param <T> type of the elements
  192.      * @return the density and its derivatives
  193.      * @exception OrekitException if derivatives cannot be computed
  194.      * @since 9.0
  195.      */
  196.     private <T extends RealFieldElement<T>> T getDensityWrtStateUsingFiniteDifferences(final AbsoluteDate date,
  197.                                                                                        final Frame frame,
  198.                                                                                        final FieldVector3D<T> position)
  199.         throws OrekitException {

  200.         // Retrieve derivation properties for parameter T
  201.         // It is implied here that T is a DerivativeStructure
  202.         // With order 1 and 6, 7 or 8 free parameters
  203.         // This is all checked before in method isStateDerivatives
  204.         final DSFactory factory = ((DerivativeStructure) position.getX()).getFactory();

  205.         // Build a DerivativeStructure using only derivatives with respect to position
  206.         final DSFactory factory3 = new DSFactory(3, 1);
  207.         final FieldVector3D<DerivativeStructure> position3 =
  208.                         new FieldVector3D<>(factory3.variable(0, position.getX().getReal()),
  209.                                             factory3.variable(1,  position.getY().getReal()),
  210.                                             factory3.variable(2,  position.getZ().getReal()));

  211.         // Get atmosphere properties in atmosphere own frame
  212.         final Frame      atmFrame  = atmosphere.getFrame();
  213.         final Transform  toBody    = frame.getTransformTo(atmFrame, date);
  214.         final FieldVector3D<DerivativeStructure> posBodyDS = toBody.transformPosition(position3);
  215.         final Vector3D   posBody   = posBodyDS.toVector3D();

  216.         // Estimate density model by finite differences and composition
  217.         // Using a delta of 1m
  218.         final double delta  = 1.0;
  219.         final double x      = posBody.getX();
  220.         final double y      = posBody.getY();
  221.         final double z      = posBody.getZ();
  222.         final double rho0   = atmosphere.getDensity(date, posBody, atmFrame);
  223.         final double dRhodX = (atmosphere.getDensity(date, new Vector3D(x + delta, y,         z),         atmFrame) - rho0) / delta;
  224.         final double dRhodY = (atmosphere.getDensity(date, new Vector3D(x,         y + delta, z),         atmFrame) - rho0) / delta;
  225.         final double dRhodZ = (atmosphere.getDensity(date, new Vector3D(x,         y,         z + delta), atmFrame) - rho0) / delta;
  226.         final double[] dXdQ = posBodyDS.getX().getAllDerivatives();
  227.         final double[] dYdQ = posBodyDS.getY().getAllDerivatives();
  228.         final double[] dZdQ = posBodyDS.getZ().getAllDerivatives();

  229.         // Density with derivatives:
  230.         // - The value and only the 3 first derivatives (those with respect to spacecraft position) are computed
  231.         // - Others are set to 0.
  232.         final int p = factory.getCompiler().getFreeParameters();
  233.         final double[] rhoAll = new double[p + 1];
  234.         rhoAll[0] = rho0;
  235.         for (int i = 1; i < 4; ++i) {
  236.             rhoAll[i] = dRhodX * dXdQ[i] + dRhodY * dYdQ[i] + dRhodZ * dZdQ[i];
  237.         }
  238.         @SuppressWarnings("unchecked")
  239.         final T rho = (T) (factory.build(rhoAll));

  240.         return rho;
  241.     }
  242. }