FiniteDifferencePropagatorConverter.java

  1. /* Copyright 2002-2022 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.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.propagation.Propagator;
  26. import org.orekit.propagation.SpacecraftState;
  27. import org.orekit.utils.PVCoordinates;

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

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

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

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

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

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

  56.         /** {@inheritDoc} */
  57.         public double[] value(final double[] arg)
  58.             throws IllegalArgumentException, OrekitException {
  59.             final Propagator propagator = builder.buildPropagator(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.                 if (Double.isNaN(pv.getMomentum().getNorm())) {
  65.                     propagator.getPVCoordinates(state.getDate(), getFrame());
  66.                 }
  67.                 eval[k++] = pv.getPosition().getX();
  68.                 eval[k++] = pv.getPosition().getY();
  69.                 eval[k++] = pv.getPosition().getZ();
  70.                 if (!isOnlyPosition()) {
  71.                     eval[k++] = pv.getVelocity().getX();
  72.                     eval[k++] = pv.getVelocity().getY();
  73.                     eval[k++] = pv.getVelocity().getZ();
  74.                 }
  75.             }

  76.             return eval;

  77.         }
  78.     }

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

  81.         /** {@inheritDoc} */
  82.         public Pair<RealVector, RealMatrix> value(final RealVector point)
  83.             throws IllegalArgumentException, OrekitException {

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

  86.             final double[][] jacob = new double[getTargetSize()][arg.length];
  87.             final double[] eval = f.value(arg);
  88.             final double[] arg1 = new double[arg.length];
  89.             for (int j = 0; j < arg.length; j++) {
  90.                 System.arraycopy(arg, 0, arg1, 0, arg.length);
  91.                 arg1[j] += 1;
  92.                 final double[] eval1 = f.value(arg1);
  93.                 for (int t = 0; t < eval.length; t++) {
  94.                     jacob[t][j] = eval1[t] - eval[t];
  95.                 }
  96.             }

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

  99.         }

  100.     }

  101. }