FiniteDifferencePropagatorConverter.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.propagation.conversion;

  18. import org.hipparchus.analysis.MultivariateVectorFunction;
  19. import org.hipparchus.linear.MatrixUtils;
  20. import org.hipparchus.linear.RealMatrix;
  21. import org.hipparchus.linear.RealVector;
  22. import org.hipparchus.optim.nonlinear.vector.leastsquares.MultivariateJacobianFunction;
  23. import org.hipparchus.util.Pair;
  24. import org.orekit.errors.OrekitException;
  25. import org.orekit.errors.OrekitExceptionWrapper;
  26. import org.orekit.propagation.Propagator;
  27. import org.orekit.propagation.SpacecraftState;
  28. import org.orekit.utils.PVCoordinates;

  29. /** Propagator converter using finite differences to compute the Jacobian.
  30.  * @author Pascal Parraud
  31.  * @since 6.0
  32.  */
  33. public class FiniteDifferencePropagatorConverter extends AbstractPropagatorConverter {

  34.     /** Propagator builder. */
  35.     private final PropagatorBuilder builder;

  36.     /** Simple constructor.
  37.      * @param factory builder for adapted propagator
  38.      * @param threshold absolute threshold for optimization algorithm
  39.      * @param maxIterations maximum number of iterations for fitting
  40.      */
  41.     public FiniteDifferencePropagatorConverter(final PropagatorBuilder factory,
  42.                                                final double threshold,
  43.                                                final int maxIterations) {
  44.         super(factory, threshold, maxIterations);
  45.         this.builder = factory;
  46.     }

  47.     /** {@inheritDoc} */
  48.     protected MultivariateVectorFunction getObjectiveFunction() {
  49.         return new ObjectiveFunction();
  50.     }

  51.     /** {@inheritDoc} */
  52.     protected MultivariateJacobianFunction getModel() {
  53.         return new ObjectiveFunctionJacobian();
  54.     }

  55.     /** Internal class for computing position/velocity at sample points. */
  56.     private class ObjectiveFunction implements MultivariateVectorFunction {

  57.         /** {@inheritDoc} */
  58.         public double[] value(final double[] arg)
  59.             throws IllegalArgumentException, OrekitExceptionWrapper {
  60.             try {
  61.                 final Propagator propagator = builder.buildPropagator(arg);
  62.                 final double[] eval = new double[getTargetSize()];
  63.                 int k = 0;
  64.                 for (SpacecraftState state : getSample()) {
  65.                     final PVCoordinates pv = propagator.getPVCoordinates(state.getDate(), getFrame());
  66.                     if (Double.isNaN(pv.getMomentum().getNorm())) {
  67.                         propagator.getPVCoordinates(state.getDate(), getFrame());
  68.                     }
  69.                     eval[k++] = pv.getPosition().getX();
  70.                     eval[k++] = pv.getPosition().getY();
  71.                     eval[k++] = pv.getPosition().getZ();
  72.                     if (!isOnlyPosition()) {
  73.                         eval[k++] = pv.getVelocity().getX();
  74.                         eval[k++] = pv.getVelocity().getY();
  75.                         eval[k++] = pv.getVelocity().getZ();
  76.                     }
  77.                 }

  78.                 return eval;

  79.             } catch (OrekitException ex) {
  80.                 throw new OrekitExceptionWrapper(ex);
  81.             }
  82.         }
  83.     }

  84.     /** Internal class for computing position/velocity Jacobian at sample points. */
  85.     private class ObjectiveFunctionJacobian implements MultivariateJacobianFunction {

  86.         /** {@inheritDoc} */
  87.         public Pair<RealVector, RealMatrix> value(final RealVector point)
  88.             throws IllegalArgumentException, OrekitExceptionWrapper {

  89.             final double[] arg = point.toArray();
  90.             final MultivariateVectorFunction f = new ObjectiveFunction();

  91.             final double[][] jacob = new double[getTargetSize()][arg.length];
  92.             final double[] eval = f.value(arg);
  93.             final double[] arg1 = new double[arg.length];
  94.             for (int j = 0; j < arg.length; j++) {
  95.                 System.arraycopy(arg, 0, arg1, 0, arg.length);
  96.                 arg1[j] += 1;
  97.                 final double[] eval1 = f.value(arg1);
  98.                 for (int t = 0; t < eval.length; t++) {
  99.                     jacob[t][j] = eval1[t] - eval[t];
  100.                 }
  101.             }

  102.             return new Pair<RealVector, RealMatrix>(MatrixUtils.createRealVector(eval),
  103.                                                     MatrixUtils.createRealMatrix(jacob));

  104.         }

  105.     }

  106. }