JacobianPropagatorConverter.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.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitExceptionWrapper;
  22. import org.orekit.errors.PropagationException;
  23. import org.orekit.propagation.SpacecraftState;
  24. import org.orekit.propagation.numerical.JacobiansMapper;
  25. import org.orekit.propagation.numerical.NumericalPropagator;
  26. import org.orekit.propagation.numerical.PartialDerivativesEquations;
  27. import org.orekit.propagation.sampling.OrekitStepHandler;
  28. import org.orekit.propagation.sampling.OrekitStepInterpolator;
  29. import org.orekit.time.AbsoluteDate;
  30. import org.orekit.utils.PVCoordinates;

  31. /** Propagator converter using the real jacobian.
  32.  * @author Pascal Parraud
  33.  * @since 6.0
  34.  */
  35. public class JacobianPropagatorConverter extends AbstractPropagatorConverter {

  36.     /** Numerical propagator builder. */
  37.     private final NumericalPropagatorBuilder builder;

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

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

  53.     /** {@inheritDoc} */
  54.     protected MultivariateMatrixFunction getObjectiveFunctionJacobian() {
  55.         return new ObjectiveFunctionJacobian();
  56.     }

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

  59.         /** {@inheritDoc} */
  60.         public double[] value(final double[] arg)
  61.             throws IllegalArgumentException, OrekitExceptionWrapper {
  62.             try {
  63.                 final double[] eval = new double[getTargetSize()];

  64.                 final NumericalPropagator prop = builder.buildPropagator(getDate(), arg);

  65.                 int k = 0;
  66.                 for (SpacecraftState state : getSample()) {
  67.                     final PVCoordinates pv = prop.getPVCoordinates(state.getDate(), getFrame());
  68.                     eval[k++] = pv.getPosition().getX();
  69.                     eval[k++] = pv.getPosition().getY();
  70.                     eval[k++] = pv.getPosition().getZ();
  71.                     if (!isOnlyPosition()) {
  72.                         eval[k++] = pv.getVelocity().getX();
  73.                         eval[k++] = pv.getVelocity().getY();
  74.                         eval[k++] = pv.getVelocity().getZ();
  75.                     }
  76.                 }

  77.                 return eval;

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

  82.     }

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

  85.         /** {@inheritDoc} */
  86.         public double[][] value(final double[] arg)
  87.             throws IllegalArgumentException, OrekitExceptionWrapper {
  88.             try {
  89.                 final double[][] jacob = new double[getTargetSize()][arg.length];

  90.                 final NumericalPropagator prop  = builder.buildPropagator(getDate(), arg);
  91.                 final int stateSize = isOnlyPosition() ? 3 : 6;
  92.                 final int paramSize = getFreeParameters().size();
  93.                 final PartialDerivativesEquations pde = new PartialDerivativesEquations("pde", prop);
  94.                 pde.selectParameters(getFreeParameters().toArray(new String[0]));
  95.                 prop.setInitialState(pde.setInitialJacobians(prop.getInitialState(), stateSize, paramSize));
  96.                 final JacobiansMapper mapper  = pde.getMapper();
  97.                 final JacobianHandler handler = new JacobianHandler(mapper);
  98.                 prop.setMasterMode(handler);

  99.                 int i = 0;
  100.                 for (SpacecraftState state : getSample()) {
  101.                     prop.propagate(state.getDate());
  102.                     final double[][] dYdY0 = handler.getdYdY0();
  103.                     final double[][] dYdP  = handler.getdYdP();
  104.                     for (int k = 0; k < stateSize; k++, i++) {
  105.                         System.arraycopy(dYdY0[k], 0, jacob[i], 0, stateSize);
  106.                         System.arraycopy(dYdP[k], 0, jacob[i], stateSize, paramSize);
  107.                     }
  108.                 }

  109.                 return jacob;

  110.             } catch (OrekitException ex) {
  111.                 ex.printStackTrace(System.err);
  112.                 throw new OrekitExceptionWrapper(ex);
  113.             }
  114.         }

  115.     }


  116.     /** Internal class for jacobians handling. */
  117.     private static class JacobianHandler implements OrekitStepHandler {

  118.         /** Jacobians mapper. */
  119.         private transient JacobiansMapper mapper;

  120.         /** Jacobian with respect to state. */
  121.         private final double[][] dYdY0;

  122.         /** Jacobian with respect to parameters. */
  123.         private final double[][] dYdP;

  124.         /** Simple constructor.
  125.          * @param mapper Jacobians mapper
  126.          */
  127.         public JacobianHandler(final JacobiansMapper mapper) {
  128.             this.mapper = mapper;
  129.             this.dYdY0  = new double[mapper.getStateDimension()][mapper.getStateDimension()];
  130.             this.dYdP   = new double[mapper.getStateDimension()][mapper.getParameters()];
  131.         }

  132.         /** Get the jacobian with respect to state.
  133.          * @return jacobian with respect to state
  134.          */
  135.         public double[][] getdYdY0() {
  136.             return dYdY0;
  137.         }

  138.         /** Get the jacobian with respect to parameters.
  139.          * @return jacobian with respect to parameters
  140.          */
  141.         public double[][] getdYdP() {
  142.             return dYdP;
  143.         }

  144.         /** {@inheritDoc} */
  145.         public void init(final SpacecraftState s0, final AbsoluteDate t) {
  146.         }

  147.         /** {@inheritDoc} */
  148.         public void handleStep(final OrekitStepInterpolator interpolator, final boolean isLast)
  149.             throws PropagationException {
  150.             try {
  151.                 // we want the Jacobians at the end of last step
  152.                 if (isLast) {
  153.                     interpolator.setInterpolatedDate(interpolator.getCurrentDate());
  154.                     final SpacecraftState state = interpolator.getInterpolatedState();
  155.                     mapper.getStateJacobian(state, dYdY0);
  156.                     mapper.getParametersJacobian(state, dYdP);
  157.                 }
  158.             } catch (PropagationException pe) {
  159.                 throw pe;
  160.             } catch (OrekitException oe) {
  161.                 throw new PropagationException(oe);
  162.             }
  163.         }

  164.     }

  165. }