PV.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.estimation.measurements;

  18. import java.util.Arrays;

  19. import org.hipparchus.exception.LocalizedCoreFormats;
  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.FastMath;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.propagation.SpacecraftState;
  24. import org.orekit.time.AbsoluteDate;
  25. import org.orekit.utils.TimeStampedPVCoordinates;

  26. /** Class modeling a position-velocity measurement.
  27.  * <p>
  28.  * For position-only measurement see {@link Position}.
  29.  * </p>
  30.  * @see Position
  31.  * @author Luc Maisonobe
  32.  * @since 8.0
  33.  */
  34. public class PV extends AbstractMeasurement<PV> {

  35.     /** Identity matrix, for states derivatives. */
  36.     private static final double[][] IDENTITY = new double[][] {
  37.         {
  38.             1, 0, 0, 0, 0, 0
  39.         }, {
  40.             0, 1, 0, 0, 0, 0
  41.         }, {
  42.             0, 0, 1, 0, 0, 0
  43.         }, {
  44.             0, 0, 0, 1, 0, 0
  45.         }, {
  46.             0, 0, 0, 0, 1, 0
  47.         }, {
  48.             0, 0, 0, 0, 0, 1
  49.         }
  50.     };

  51.     /** Covariance matrix of the PV measurement (size 6x6). */
  52.     private final double[][] covarianceMatrix;

  53.     /** Constructor with two double for the standard deviations.
  54.      * <p>The first double is the position's standard deviation, common to the 3 position's components.
  55.      * The second double is the position's standard deviation, common to the 3 position's components.</p>
  56.      * <p>
  57.      * The measurement must be in the orbit propagation frame.
  58.      * </p>
  59.      * @param date date of the measurement
  60.      * @param position position
  61.      * @param velocity velocity
  62.      * @param sigmaPosition theoretical standard deviation on position components
  63.      * @param sigmaVelocity theoretical standard deviation on velocity components
  64.      * @param baseWeight base weight
  65.      * @param satellite satellite related to this measurement
  66.      * @since 9.3
  67.      */
  68.     public PV(final AbsoluteDate date, final Vector3D position, final Vector3D velocity,
  69.               final double sigmaPosition, final double sigmaVelocity, final double baseWeight,
  70.               final ObservableSatellite satellite) {
  71.         this(date, position, velocity,
  72.              new double[] {
  73.                  sigmaPosition,
  74.                  sigmaPosition,
  75.                  sigmaPosition,
  76.                  sigmaVelocity,
  77.                  sigmaVelocity,
  78.                  sigmaVelocity
  79.              }, baseWeight, satellite);
  80.     }

  81.     /** Constructor with two vectors for the standard deviations.
  82.      * <p>One 3-sized vectors for position standard deviations.
  83.      * One 3-sized vectors for velocity standard deviations.
  84.      * The 3-sized vectors are the square root of the diagonal elements of the covariance matrix.</p>
  85.      * <p>The measurement must be in the orbit propagation frame.</p>
  86.      * @param date date of the measurement
  87.      * @param position position
  88.      * @param velocity velocity
  89.      * @param sigmaPosition 3-sized vector of the standard deviations of the position
  90.      * @param sigmaVelocity 3-sized vector of the standard deviations of the velocity
  91.      * @param baseWeight base weight
  92.      * @param satellite satellite related to this measurement
  93.      * @since 9.3
  94.      */
  95.     public PV(final AbsoluteDate date, final Vector3D position, final Vector3D velocity,
  96.               final double[] sigmaPosition, final double[] sigmaVelocity,
  97.               final double baseWeight, final ObservableSatellite satellite) {
  98.         this(date, position, velocity,
  99.              buildPvCovarianceMatrix(sigmaPosition, sigmaVelocity),
  100.              baseWeight, satellite);
  101.     }

  102.     /** Constructor with one vector for the standard deviations.
  103.      * <p>The 6-sized vector is the square root of the diagonal elements of the covariance matrix.</p>
  104.      * <p>The measurement must be in the orbit propagation frame.</p>
  105.      * @param date date of the measurement
  106.      * @param position position
  107.      * @param velocity velocity
  108.      * @param sigmaPV 6-sized vector of the standard deviations
  109.      * @param baseWeight base weight
  110.      * @param satellite satellite related to this measurement
  111.      * @since 9.3
  112.      */
  113.     public PV(final AbsoluteDate date, final Vector3D position, final Vector3D velocity,
  114.               final double[] sigmaPV, final double baseWeight, final ObservableSatellite satellite) {
  115.         this(date, position, velocity, buildPvCovarianceMatrix(sigmaPV), baseWeight, satellite);
  116.     }

  117.     /**
  118.      * Constructor with 2 smaller covariance matrices.
  119.      * <p>One 3x3 covariance matrix for position and one 3x3 covariance matrix for velocity.
  120.      * The fact that the covariance matrices are symmetric and positive definite is not checked.</p>
  121.      * <p>The measurement must be in the orbit propagation frame.</p>
  122.      * @param date date of the measurement
  123.      * @param position position
  124.      * @param velocity velocity
  125.      * @param positionCovarianceMatrix 3x3 covariance matrix of the position
  126.      * @param velocityCovarianceMatrix 3x3 covariance matrix of the velocity
  127.      * @param baseWeight base weight
  128.      * @param satellite satellite related to this measurement
  129.      * @since 9.3
  130.      */
  131.     public PV(final AbsoluteDate date, final Vector3D position, final Vector3D velocity,
  132.               final double[][] positionCovarianceMatrix, final double[][] velocityCovarianceMatrix,
  133.               final double baseWeight, final ObservableSatellite satellite) {
  134.         this(date, position, velocity,
  135.              buildPvCovarianceMatrix(positionCovarianceMatrix, velocityCovarianceMatrix),
  136.              baseWeight, satellite);
  137.     }

  138.     /** Constructor with full covariance matrix and all inputs.
  139.      * <p>The fact that the covariance matrix is symmetric and positive definite is not checked.</p>
  140.      * <p>The measurement must be in the orbit propagation frame.</p>
  141.      * @param date date of the measurement
  142.      * @param position position
  143.      * @param velocity velocity
  144.      * @param covarianceMatrix 6x6 covariance matrix of the PV measurement
  145.      * @param baseWeight base weight
  146.      * @param satellite satellite related to this measurement
  147.      * @since 9.3
  148.      */
  149.     public PV(final AbsoluteDate date, final Vector3D position, final Vector3D velocity,
  150.               final double[][] covarianceMatrix, final double baseWeight, final ObservableSatellite satellite) {
  151.         super(date,
  152.               new double[] {
  153.                   position.getX(), position.getY(), position.getZ(),
  154.                   velocity.getX(), velocity.getY(), velocity.getZ()
  155.               }, extractSigmas(covarianceMatrix),
  156.               new double[] {
  157.                   baseWeight, baseWeight, baseWeight,
  158.                   baseWeight, baseWeight, baseWeight
  159.               }, Arrays.asList(satellite));
  160.         this.covarianceMatrix = covarianceMatrix.clone();
  161.     }

  162.     /** Get the position.
  163.      * @return position
  164.      */
  165.     public Vector3D getPosition() {
  166.         final double[] pv = getObservedValue();
  167.         return new Vector3D(pv[0], pv[1], pv[2]);
  168.     }

  169.     /** Get the velocity.
  170.      * @return velocity
  171.      */
  172.     public Vector3D getVelocity() {
  173.         final double[] pv = getObservedValue();
  174.         return new Vector3D(pv[3], pv[4], pv[5]);
  175.     }

  176.     /** Get the covariance matrix.
  177.      * @return the covariance matrix
  178.      */
  179.     public double[][] getCovarianceMatrix() {
  180.         return covarianceMatrix.clone();
  181.     }

  182.     /** Get the correlation coefficients matrix.
  183.      * <p>This is the 6x6 matrix M such that:
  184.      * <p>Mij = Pij/(σi.σj)
  185.      * <p>Where:
  186.      * <ul>
  187.      * <li>P is the covariance matrix
  188.      * <li>σi is the i-th standard deviation (σi² = Pii)
  189.      * </ul>
  190.      * @return the correlation coefficient matrix (6x6)
  191.      */
  192.     public double[][] getCorrelationCoefficientsMatrix() {

  193.         // Get the standard deviations
  194.         final double[] sigmas = getTheoreticalStandardDeviation();

  195.         // Initialize the correlation coefficients matric to the covariance matrix
  196.         final double[][] corrCoefMatrix = new double[sigmas.length][sigmas.length];

  197.         // Divide by the standard deviations
  198.         for (int i = 0; i < sigmas.length; i++) {
  199.             for (int j = 0; j < sigmas.length; j++) {
  200.                 corrCoefMatrix[i][j] = covarianceMatrix[i][j] / (sigmas[i] * sigmas[j]);
  201.             }
  202.         }
  203.         return corrCoefMatrix;
  204.     }

  205.     /** {@inheritDoc} */
  206.     @Override
  207.     protected EstimatedMeasurement<PV> theoreticalEvaluation(final int iteration, final int evaluation,
  208.                                                              final SpacecraftState[] states) {

  209.         // PV value
  210.         final TimeStampedPVCoordinates pv = states[0].getPVCoordinates();

  211.         // prepare the evaluation
  212.         final EstimatedMeasurement<PV> estimated =
  213.                         new EstimatedMeasurement<>(this, iteration, evaluation, states,
  214.                                                    new TimeStampedPVCoordinates[] {
  215.                                                        pv
  216.                                                    });

  217.         estimated.setEstimatedValue(new double[] {
  218.             pv.getPosition().getX(), pv.getPosition().getY(), pv.getPosition().getZ(),
  219.             pv.getVelocity().getX(), pv.getVelocity().getY(), pv.getVelocity().getZ()
  220.         });

  221.         // partial derivatives with respect to state
  222.         estimated.setStateDerivatives(0, IDENTITY);

  223.         return estimated;
  224.     }

  225.     /** Extract standard deviations from a 6x6 PV covariance matrix.
  226.      * Check the size of the PV covariance matrix first.
  227.      * @param pvCovarianceMatrix the 6x6 PV covariance matrix
  228.      * @return the standard deviations (6-sized vector), they are
  229.      * the square roots of the diagonal elements of the covariance matrix in input.
  230.      */
  231.     private static double[] extractSigmas(final double[][] pvCovarianceMatrix) {

  232.         // Check the size of the covariance matrix, should be 6x6
  233.         if (pvCovarianceMatrix.length != 6 || pvCovarianceMatrix[0].length != 6) {
  234.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH_2x2,
  235.                                       pvCovarianceMatrix.length, pvCovarianceMatrix[0],
  236.                                       6, 6);
  237.         }

  238.         // Extract the standard deviations (square roots of the diagonal elements)
  239.         final double[] sigmas = new double[6];
  240.         for (int i = 0; i < sigmas.length; i++) {
  241.             sigmas[i] = FastMath.sqrt(pvCovarianceMatrix[i][i]);
  242.         }
  243.         return sigmas;
  244.     }

  245.     /** Build a 6x6 PV covariance matrix from two 3x3 matrices (covariances in position and velocity).
  246.      * Check the size of the matrices first.
  247.      * @param positionCovarianceMatrix the 3x3 covariance matrix in position
  248.      * @param velocityCovarianceMatrix the 3x3 covariance matrix in velocity
  249.      * @return the 6x6 PV covariance matrix
  250.      */
  251.     private static double[][] buildPvCovarianceMatrix(final double[][] positionCovarianceMatrix,
  252.                                                       final double[][] velocityCovarianceMatrix) {
  253.         // Check the sizes of the matrices first
  254.         if (positionCovarianceMatrix.length != 3 || positionCovarianceMatrix[0].length != 3) {
  255.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH_2x2,
  256.                                       positionCovarianceMatrix.length, positionCovarianceMatrix[0],
  257.                                       3, 3);
  258.         }
  259.         if (velocityCovarianceMatrix.length != 3 || velocityCovarianceMatrix[0].length != 3) {
  260.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH_2x2,
  261.                                       velocityCovarianceMatrix.length, velocityCovarianceMatrix[0],
  262.                                       3, 3);
  263.         }

  264.         // Build the PV 6x6 covariance matrix
  265.         final double[][] pvCovarianceMatrix = new double[6][6];
  266.         for (int i = 0; i < 3; i++) {
  267.             for (int j = 0; j < 3; j++) {
  268.                 pvCovarianceMatrix[i][j]         = positionCovarianceMatrix[i][j];
  269.                 pvCovarianceMatrix[i + 3][j + 3] = velocityCovarianceMatrix[i][j];
  270.             }
  271.         }
  272.         return pvCovarianceMatrix;
  273.     }

  274.     /** Build a 6x6 PV covariance matrix from a 6-sized vector (position and velocity standard deviations).
  275.      * Check the size of the vector first.
  276.      * @param sigmaPV 6-sized vector with position standard deviations on the first 3 elements
  277.      * and velocity standard deviations on the last 3 elements
  278.      * @return the 6x6 PV covariance matrix
  279.      */
  280.     private static double[][] buildPvCovarianceMatrix(final double[] sigmaPV) {
  281.         // Check the size of the vector first
  282.         if (sigmaPV.length != 6) {
  283.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH, sigmaPV.length, 6);

  284.         }

  285.         // Build the PV 6x6 covariance matrix
  286.         final double[][] pvCovarianceMatrix = new double[6][6];
  287.         for (int i = 0; i < sigmaPV.length; i++) {
  288.             pvCovarianceMatrix[i][i] =  sigmaPV[i] * sigmaPV[i];
  289.         }
  290.         return pvCovarianceMatrix;
  291.     }

  292.     /** Build a 6x6 PV covariance matrix from two 3-sized vectors (position and velocity standard deviations).
  293.      * Check the sizes of the vectors first.
  294.      * @param sigmaPosition standard deviations of the position (3-size vector)
  295.      * @param sigmaVelocity standard deviations of the velocity (3-size vector)
  296.      * @return the 6x6 PV covariance matrix
  297.      */
  298.     private static double[][] buildPvCovarianceMatrix(final double[] sigmaPosition,
  299.                                                       final double[] sigmaVelocity) {

  300.         // Check the sizes of the vectors first
  301.         if (sigmaPosition.length != 3) {
  302.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH, sigmaPosition.length, 3);

  303.         }
  304.         if (sigmaVelocity.length != 3) {
  305.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH, sigmaVelocity.length, 3);

  306.         }

  307.         // Build the PV 6x6 covariance matrix
  308.         final double[][] pvCovarianceMatrix = new double[6][6];
  309.         for (int i = 0; i < sigmaPosition.length; i++) {
  310.             pvCovarianceMatrix[i][i]         =  sigmaPosition[i] * sigmaPosition[i];
  311.             pvCovarianceMatrix[i + 3][i + 3] =  sigmaVelocity[i] * sigmaVelocity[i];
  312.         }
  313.         return pvCovarianceMatrix;
  314.     }
  315. }