EstimatedMeasurement.java

  1. /* Copyright 2002-2024 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.IdentityHashMap;
  19. import java.util.Map;
  20. import java.util.stream.Stream;

  21. import org.orekit.errors.OrekitIllegalArgumentException;
  22. import org.orekit.errors.OrekitIllegalStateException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.propagation.SpacecraftState;
  25. import org.orekit.time.AbsoluteDate;
  26. import org.orekit.utils.ParameterDriver;
  27. import org.orekit.utils.TimeSpanMap;
  28. import org.orekit.utils.TimeStampedPVCoordinates;
  29. import org.orekit.utils.TimeSpanMap.Span;

  30. /** Class holding an estimated theoretical value associated to an {@link ObservedMeasurement observed measurement}.
  31.  * @param <T> the type of the measurement
  32.  * @author Luc Maisonobe
  33.  * @since 8.0
  34.  */
  35. public class EstimatedMeasurement<T extends ObservedMeasurement<T>> extends EstimatedMeasurementBase<T> {

  36.     /** Partial derivatives with respect to states. */
  37.     private double[][][] stateDerivatives;

  38.     /** Partial derivatives with respect to parameters. */
  39.     private final Map<ParameterDriver, TimeSpanMap<double[]>> parametersDerivatives;

  40.     /** Simple constructor.
  41.      * @param observedMeasurement associated observed measurement
  42.      * @param iteration iteration number
  43.      * @param count evaluations counter
  44.      * @param states states of the spacecrafts
  45.      * @param participants coordinates of the participants in signal travel order
  46.      * in inertial frame
  47.      */
  48.     public EstimatedMeasurement(final T observedMeasurement,
  49.                                 final int iteration, final int count,
  50.                                 final SpacecraftState[] states,
  51.                                 final TimeStampedPVCoordinates[] participants) {
  52.         super(observedMeasurement, iteration, count, states, participants);
  53.         this.stateDerivatives      = new double[states.length][][];
  54.         this.parametersDerivatives = new IdentityHashMap<ParameterDriver, TimeSpanMap<double[]>>();
  55.     }

  56.     /** Get state size.
  57.      * <p>
  58.      * Warning, the {@link #setStateDerivatives(int, double[][])}
  59.      * method must have been called before this method is called.
  60.      * </p>
  61.      * @return state size
  62.      * @since 10.1
  63.      */
  64.     public int getStateSize() {
  65.         return stateDerivatives[0][0].length;
  66.     }

  67.     /** Get the partial derivatives of the {@link #getEstimatedValue()
  68.      * simulated measurement} with respect to state Cartesian coordinates.
  69.      * @param index index of the state, according to the {@code states}
  70.      * passed at construction
  71.      * @return partial derivatives of the simulated value (array of size
  72.      * {@link ObservedMeasurement#getDimension() dimension} x 6)
  73.      */
  74.     public double[][] getStateDerivatives(final int index) {
  75.         final double[][] sd = new double[getObservedMeasurement().getDimension()][];
  76.         for (int i = 0; i < getObservedMeasurement().getDimension(); ++i) {
  77.             sd[i] = stateDerivatives[index][i].clone();
  78.         }
  79.         return sd;
  80.     }

  81.     /** Set the partial derivatives of the {@link #getEstimatedValue()
  82.      * simulated measurement} with respect to state Cartesian coordinates.
  83.      * @param index index of the state, according to the {@code states}
  84.      * passed at construction
  85.      * @param derivatives partial derivatives with respect to state
  86.      */
  87.     public void setStateDerivatives(final int index, final double[]... derivatives) {
  88.         this.stateDerivatives[index] = new double[getObservedMeasurement().getDimension()][];
  89.         for (int i = 0; i < getObservedMeasurement().getDimension(); ++i) {
  90.             this.stateDerivatives[index][i] = derivatives[i].clone();
  91.         }
  92.     }

  93.     /** Get all the drivers with set derivatives.
  94.      * @return all the drivers with set derivatives
  95.      * @since 9.0
  96.      */
  97.     public Stream<ParameterDriver> getDerivativesDrivers() {
  98.         return parametersDerivatives.entrySet().stream().map(entry -> entry.getKey());
  99.     }

  100.     /** Get the partial derivatives of the {@link #getEstimatedValue()
  101.      * simulated measurement} with respect to a parameter.
  102.      * @param driver name of the span of the driver for the parameter for which
  103.      * the derivative wants to be known.
  104.      * @return partial derivatives of the simulated value
  105.      * @exception OrekitIllegalArgumentException if parameter is unknown or
  106.      * OrekitIllegalStateException if this function is used on a PDriver having several
  107.      * values driven, in this case the method
  108.      * {@link #getParameterDerivatives(ParameterDriver, AbsoluteDate)} must be called
  109.      */
  110.     public double[] getParameterDerivatives(final ParameterDriver driver)
  111.         throws OrekitIllegalArgumentException {
  112.         if (driver.getNbOfValues() == 1) {
  113.             final TimeSpanMap<double[]> p = parametersDerivatives.get(driver);
  114.             if (p == null) {
  115.                 final StringBuilder builder = new StringBuilder();
  116.                 for (final Map.Entry<ParameterDriver, TimeSpanMap<double[]>> entry : parametersDerivatives.entrySet()) {
  117.                     if (builder.length() > 0) {
  118.                         builder.append(",  ");
  119.                     }
  120.                     builder.append(entry.getKey());
  121.                 }
  122.                 throw new OrekitIllegalArgumentException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  123.                                                          driver,
  124.                                                          builder.length() > 0 ? builder.toString() : " <none>");
  125.             }
  126.             return p.get(AbsoluteDate.ARBITRARY_EPOCH);
  127.         } else {
  128.             throw new OrekitIllegalStateException(OrekitMessages.PARAMETER_WITH_SEVERAL_ESTIMATED_VALUES, driver.getName(), "getParameterDerivatives(driver, date)");
  129.         }
  130.     }

  131.     /** Get the partial derivatives of the {@link #getEstimatedValue()
  132.      * simulated measurement} with respect to a parameter.
  133.      * @param driver name of the span of the driver for the parameter for which
  134.      * the derivative wants to be known.
  135.      * @param date date at which the parameter derivatives wants to be known
  136.      * @return partial derivatives of the simulated value
  137.      * @exception OrekitIllegalArgumentException if parameter is unknown
  138.      */
  139.     public double[] getParameterDerivatives(final ParameterDriver driver, final AbsoluteDate date)
  140.         throws OrekitIllegalArgumentException {
  141.         final TimeSpanMap<double[]> p = parametersDerivatives.get(driver);
  142.         if (p == null) {
  143.             final StringBuilder builder = new StringBuilder();
  144.             for (final Map.Entry<ParameterDriver, TimeSpanMap<double[]>> entry : parametersDerivatives.entrySet()) {
  145.                 if (builder.length() > 0) {
  146.                     builder.append(", ");
  147.                 }
  148.                 builder.append(entry.getKey());
  149.             }
  150.             throw new OrekitIllegalArgumentException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  151.                                                      driver,
  152.                                                      builder.length() > 0 ? builder.toString() : "<none>");
  153.         }
  154.         return p.get(date);
  155.     }

  156.     /** Set the partial derivatives of the {@link #getEstimatedValue()
  157.      * simulated measurement} with respect to parameter.
  158.      * @param driver name of the span of the driver for the parameter for which
  159.      * the derivative wants to be known.
  160.      * @param date date at which the parameterDerivative wants to be set
  161.      * @param parameterDerivatives partial derivatives with respect to parameter
  162.      */
  163.     public void setParameterDerivatives(final ParameterDriver driver, final AbsoluteDate date, final double... parameterDerivatives) {
  164.         if (!parametersDerivatives.containsKey(driver) || parametersDerivatives.get(driver) == null) {
  165.             final TimeSpanMap<double[]> derivativeSpanMap = new TimeSpanMap<double[]>(parameterDerivatives);
  166.             final TimeSpanMap<String> driverNameSpan = driver.getNamesSpanMap();
  167.             for (Span<String> span = driverNameSpan.getSpan(driverNameSpan.getFirstSpan().getEnd()); span != null; span = span.next()) {
  168.                 derivativeSpanMap.addValidAfter(parameterDerivatives, span.getStart(), false);
  169.             }
  170.             parametersDerivatives.put(driver, derivativeSpanMap);

  171.         } else {

  172.             AbsoluteDate dateToAddAfter = driver.getNamesSpanMap().getSpan(date).getStart();
  173.             if (dateToAddAfter.equals(AbsoluteDate.PAST_INFINITY)) {
  174.                 dateToAddAfter = driver.getNamesSpanMap().getSpan(date).getEnd();
  175.                 parametersDerivatives.get(driver).addValidBefore(parameterDerivatives, dateToAddAfter, false);
  176.             } else {
  177.                 parametersDerivatives.get(driver).addValidAfter(parameterDerivatives, dateToAddAfter, false);
  178.             }

  179.         }

  180.     }

  181.     /** Set the partial derivatives of the {@link #getEstimatedValue()
  182.      * simulated measurement} with respect to parameter.
  183.      * @param driver driver for the parameter
  184.      * @param parameterDerivativesMap partial derivatives with respect to parameter
  185.      */
  186.     public void setParameterDerivatives(final ParameterDriver driver, final TimeSpanMap<double[]> parameterDerivativesMap) {
  187.         parametersDerivatives.put(driver, parameterDerivativesMap);
  188.     }

  189. }