NumericalGradientConverter.java

  1. /* Copyright 2002-2020 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.propagation.numerical;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.Field;
  21. import org.hipparchus.analysis.differentiation.Gradient;
  22. import org.hipparchus.analysis.differentiation.GradientField;
  23. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  24. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  25. import org.orekit.attitudes.AttitudeProvider;
  26. import org.orekit.attitudes.FieldAttitude;
  27. import org.orekit.forces.ForceModel;
  28. import org.orekit.orbits.FieldCartesianOrbit;
  29. import org.orekit.orbits.FieldOrbit;
  30. import org.orekit.propagation.FieldSpacecraftState;
  31. import org.orekit.propagation.SpacecraftState;
  32. import org.orekit.propagation.integration.AbstractGradientConverter;
  33. import org.orekit.utils.FieldAngularCoordinates;
  34. import org.orekit.utils.FieldPVCoordinates;
  35. import org.orekit.utils.ParameterDriver;
  36. import org.orekit.utils.TimeStampedFieldAngularCoordinates;
  37. import org.orekit.utils.TimeStampedFieldPVCoordinates;

  38. /** Converter for states and parameters arrays.
  39.  * @author Luc Maisonobe
  40.  * @since 10.2
  41.  */
  42. class NumericalGradientConverter extends AbstractGradientConverter {

  43.     /** Dimension of the state. */
  44.     private final int freeStateParameters;

  45.     /** States with various number of additional parameters for force models. */
  46.     private final List<FieldSpacecraftState<Gradient>> gStates;

  47.     /** Simple constructor.
  48.      * @param state regular state
  49.      * @param freeStateParameters number of free parameters, either 3 (position) or 6 (position-velocity)
  50.      * @param provider provider to use if attitude needs to be recomputed
  51.      */
  52.     NumericalGradientConverter(final SpacecraftState state, final int freeStateParameters,
  53.                                final AttitudeProvider provider) {

  54.         super(freeStateParameters);
  55.         this.freeStateParameters = freeStateParameters;

  56.         // Derivative field
  57.         final Field<Gradient> field =  GradientField.getField(freeStateParameters);

  58.         // position always has derivatives
  59.         final Vector3D pos = state.getPVCoordinates().getPosition();
  60.         final FieldVector3D<Gradient> posG = new FieldVector3D<>(Gradient.variable(freeStateParameters, 0, pos.getX()),
  61.                                                                  Gradient.variable(freeStateParameters, 1, pos.getY()),
  62.                                                                  Gradient.variable(freeStateParameters, 2, pos.getZ()));

  63.         // velocity may have derivatives or not
  64.         final Vector3D vel = state.getPVCoordinates().getVelocity();
  65.         final FieldVector3D<Gradient> velG;
  66.         if (freeStateParameters > 3) {
  67.             velG = new FieldVector3D<>(Gradient.variable(freeStateParameters, 3, vel.getX()),
  68.                                        Gradient.variable(freeStateParameters, 4, vel.getY()),
  69.                                        Gradient.variable(freeStateParameters, 5, vel.getZ()));
  70.         } else {
  71.             velG = new FieldVector3D<>(Gradient.constant(freeStateParameters, vel.getX()),
  72.                                        Gradient.constant(freeStateParameters, vel.getY()),
  73.                                        Gradient.constant(freeStateParameters, vel.getZ()));
  74.         }

  75.         // acceleration never has derivatives
  76.         final Vector3D acc = state.getPVCoordinates().getAcceleration();
  77.         final FieldVector3D<Gradient> accG = new FieldVector3D<>(Gradient.constant(freeStateParameters, acc.getX()),
  78.                                                                  Gradient.constant(freeStateParameters, acc.getY()),
  79.                                                                  Gradient.constant(freeStateParameters, acc.getZ()));

  80.         // mass never has derivatives
  81.         final Gradient gM = Gradient.constant(freeStateParameters, state.getMass());

  82.         final Gradient gMu = Gradient.constant(freeStateParameters, state.getMu());

  83.         final FieldOrbit<Gradient> gOrbit =
  84.                         new FieldCartesianOrbit<>(new TimeStampedFieldPVCoordinates<>(state.getDate(), posG, velG, accG),
  85.                                                   state.getFrame(), gMu);

  86.         final FieldAttitude<Gradient> gAttitude;
  87.         if (freeStateParameters > 3) {
  88.             // compute attitude partial derivatives with respect to position/velocity
  89.             gAttitude = provider.getAttitude(gOrbit, gOrbit.getDate(), gOrbit.getFrame());
  90.         } else {
  91.             // force model does not depend on attitude, don't bother recomputing it
  92.             gAttitude = new FieldAttitude<>(field, state.getAttitude());
  93.         }

  94.         // initialize the list with the state having 0 force model parameters
  95.         gStates = new ArrayList<>();
  96.         gStates.add(new FieldSpacecraftState<>(gOrbit, gAttitude, gM));

  97.     }

  98.     /** Get the state with the number of parameters consistent with force model.
  99.      * @param forceModel force model
  100.      * @return state with the number of parameters consistent with force model
  101.      */
  102.     public FieldSpacecraftState<Gradient> getState(final ForceModel forceModel) {

  103.         // count the required number of parameters
  104.         int nbParams = 0;
  105.         for (final ParameterDriver driver : forceModel.getParametersDrivers()) {
  106.             if (driver.isSelected()) {
  107.                 ++nbParams;
  108.             }
  109.         }

  110.         // fill in intermediate slots
  111.         while (gStates.size() < nbParams + 1) {
  112.             gStates.add(null);
  113.         }

  114.         if (gStates.get(nbParams) == null) {
  115.             // it is the first time we need this number of parameters
  116.             // we need to create the state
  117.             final int freeParameters = freeStateParameters + nbParams;
  118.             final FieldSpacecraftState<Gradient> s0 = gStates.get(0);

  119.             // orbit
  120.             final FieldPVCoordinates<Gradient> pv0 = s0.getPVCoordinates();
  121.             final FieldOrbit<Gradient> gOrbit =
  122.                             new FieldCartesianOrbit<>(new TimeStampedFieldPVCoordinates<>(s0.getDate().toAbsoluteDate(),
  123.                                                                                           extend(pv0.getPosition(),     freeParameters),
  124.                                                                                           extend(pv0.getVelocity(),     freeParameters),
  125.                                                                                           extend(pv0.getAcceleration(), freeParameters)),
  126.                                                       s0.getFrame(),
  127.                                                       extend(s0.getMu(), freeParameters));

  128.             // attitude
  129.             final FieldAngularCoordinates<Gradient> ac0 = s0.getAttitude().getOrientation();
  130.             final FieldAttitude<Gradient> gAttitude =
  131.                             new FieldAttitude<>(s0.getAttitude().getReferenceFrame(),
  132.                                                 new TimeStampedFieldAngularCoordinates<>(gOrbit.getDate(),
  133.                                                                                          extend(ac0.getRotation(), freeParameters),
  134.                                                                                          extend(ac0.getRotationRate(), freeParameters),
  135.                                                                                          extend(ac0.getRotationAcceleration(), freeParameters)));

  136.             // mass
  137.             final Gradient gM = extend(s0.getMass(), freeParameters);

  138.             gStates.set(nbParams, new FieldSpacecraftState<>(gOrbit, gAttitude, gM));

  139.         }

  140.         return gStates.get(nbParams);

  141.     }

  142.     /** Get the force model parameters.
  143.      * @param state state as returned by {@link #getState(ForceModel)}
  144.      * @param forceModel force model associated with the parameters
  145.      * @return force model parameters
  146.      */
  147.     public Gradient[] getParameters(final FieldSpacecraftState<Gradient> state,
  148.                                     final ForceModel forceModel) {
  149.         final int freeParameters = state.getMass().getFreeParameters();
  150.         final ParameterDriver[] drivers = forceModel.getParametersDrivers();
  151.         final Gradient[] parameters = new Gradient[drivers.length];
  152.         int index = freeStateParameters;
  153.         for (int i = 0; i < drivers.length; ++i) {
  154.             parameters[i] = drivers[i].isSelected() ?
  155.                             Gradient.variable(freeParameters, index++, drivers[i].getValue()) :
  156.                             Gradient.constant(freeParameters, drivers[i].getValue());
  157.         }
  158.         return parameters;
  159.     }

  160. }