FiniteDifferencePropagatorConverter.java

  1. /* Copyright 2002-2013 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.apache.commons.math3.analysis.MultivariateMatrixFunction;
  19. import org.apache.commons.math3.analysis.MultivariateVectorFunction;
  20. import org.apache.commons.math3.util.FastMath;
  21. import org.apache.commons.math3.util.Precision;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitExceptionWrapper;
  24. import org.orekit.propagation.Propagator;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.utils.PVCoordinates;

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

  32.     /** Propagator builder. */
  33.     private final PropagatorBuilder builder;

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

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

  49.     /** {@inheritDoc} */
  50.     protected MultivariateMatrixFunction getObjectiveFunctionJacobian() {
  51.         return new ObjectiveFunctionJacobian();
  52.     }

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

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

  73.                 return eval;

  74.             } catch (OrekitException ex) {
  75.                 throw new OrekitExceptionWrapper(ex);
  76.             }
  77.         }
  78.     }

  79.     /** Internal class for computing position/velocity Jacobian at sample points. */
  80.     private class ObjectiveFunctionJacobian implements MultivariateMatrixFunction {

  81.         /** {@inheritDoc} */
  82.         public double[][] value(final double[] arg)
  83.             throws IllegalArgumentException, OrekitExceptionWrapper {

  84.             final MultivariateVectorFunction f = new ObjectiveFunction();

  85.             final double[][] jacob = new double[getTargetSize()][arg.length];
  86.             final double[] eval = f.value(arg);
  87.             final double[] arg1 = new double[arg.length];
  88.             double increment = 0;
  89.             for (int j = 0; j < arg.length; j++) {
  90.                 System.arraycopy(arg, 0, arg1, 0, arg.length);
  91.                 increment = FastMath.sqrt(Precision.EPSILON) * FastMath.abs(arg[j]);
  92.                 if (increment <= Precision.SAFE_MIN) {
  93.                     increment = FastMath.sqrt(Precision.EPSILON);
  94.                 }
  95.                 arg1[j] += increment;
  96.                 final double[] eval1 = f.value(arg1);
  97.                 for (int t = 0; t < eval.length; t++) {
  98.                     jacob[t][j] = (eval1[t] - eval[t]) / increment;
  99.                 }
  100.             }

  101.             return jacob;
  102.         }

  103.     }

  104. }