ParameterDriversProvider.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.utils;

  18. import java.util.List;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.Field;
  21. import org.hipparchus.util.MathArrays;
  22. import org.orekit.errors.UnsupportedParameterException;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.time.FieldAbsoluteDate;
  25. import org.orekit.utils.TimeSpanMap.Span;

  26. /** Provider for {@link ParameterDriver parameters drivers}.
  27.  * @author Luc Maisonobe
  28.  * @author Melina Vanel
  29.  * @author Maxime Journot
  30.  * @since 11.2
  31.  */
  32. public interface ParameterDriversProvider {

  33.     /** Get the drivers for parameters.
  34.      * @return drivers for parameters
  35.      */
  36.     List<ParameterDriver> getParametersDrivers();

  37.     /** Get total number of spans for all the parameters driver.
  38.      * @return total number of span to be estimated
  39.      * @since 12.0
  40.      */
  41.     default int getNbParametersDriversValue() {
  42.         int totalSpan = 0;
  43.         final List<ParameterDriver> allParameters = getParametersDrivers();
  44.         for (ParameterDriver driver : allParameters) {
  45.             totalSpan += driver.getNbOfValues();
  46.         }
  47.         return totalSpan;
  48.     }

  49.     /** Get model parameters.
  50.      * @return model parameters, will throw an
  51.      * exception if one PDriver has several values driven. If
  52.      * it's the case (if at least 1 PDriver of the model has several values
  53.      * driven) the method {@link #getParameters(AbsoluteDate)} must be used.
  54.      * @since 12.0
  55.      */
  56.     default double[] getParameters() {

  57.         final List<ParameterDriver> drivers = getParametersDrivers();
  58.         final double[] parameters = new double[drivers.size()];
  59.         for (int i = 0; i < drivers.size(); ++i) {
  60.             parameters[i] = drivers.get(i).getValue();
  61.         }
  62.         return parameters;
  63.     }

  64.     /** Get model parameters.
  65.      * @param date date at which the parameters want to be known, can
  66.      * be new AbsoluteDate() if all the parameters have no validity period
  67.      * that is to say that they have only 1 estimated value over the all
  68.      * interval
  69.      * @return model parameters
  70.      * @since 12.0
  71.      */
  72.     default double[] getParameters(AbsoluteDate date) {

  73.         final List<ParameterDriver> drivers = getParametersDrivers();
  74.         final double[] parameters = new double[drivers.size()];
  75.         for (int i = 0; i < drivers.size(); ++i) {
  76.             parameters[i] = drivers.get(i).getValue(date);
  77.         }
  78.         return parameters;
  79.     }

  80.     /** Get model parameters, return a list a all span values
  81.      * of all parameters.
  82.      * @return model parameters
  83.      * @since 12.0
  84.      */
  85.     default double[] getParametersAllValues() {

  86.         final List<ParameterDriver> drivers = getParametersDrivers();
  87.         final int nbParametersValues = getNbParametersDriversValue();
  88.         final double[] parameters = new double[nbParametersValues];
  89.         int paramIndex = 0;
  90.         for (int i = 0; i < drivers.size(); ++i) {
  91.             for (Span<Double> span = drivers.get(i).getValueSpanMap().getFirstSpan(); span != null; span = span.next()) {
  92.                 parameters[paramIndex++] = span.getData();
  93.             }

  94.         }
  95.         return parameters;
  96.     }

  97.     /** Get model parameters.
  98.      * @param field field to which the elements belong
  99.      * @param <T> type of the elements
  100.      * @return model parameters
  101.      * @since 9.0
  102.      */
  103.     default <T extends CalculusFieldElement<T>> T[] getParametersAllValues(final Field<T> field) {
  104.         final List<ParameterDriver> drivers = getParametersDrivers();
  105.         final int nbParametersValues = getNbParametersDriversValue();
  106.         final T[] parameters = MathArrays.buildArray(field, nbParametersValues);
  107.         int paramIndex = 0;
  108.         for (int i = 0; i < drivers.size(); ++i) {
  109.             for (Span<Double> span = drivers.get(i).getValueSpanMap().getFirstSpan(); span != null; span = span.next()) {
  110.                 parameters[paramIndex++] = field.getZero().add(span.getData());
  111.             }
  112.         }
  113.         return parameters;
  114.     }

  115.     /** Get model parameters.
  116.      * @param field field to which the elements belong
  117.      * @param <T> type of the elements
  118.      * @return model parameters, will throw an
  119.      * exception if one PDriver of the has several values driven. If
  120.      * it's the case (if at least 1 PDriver of the model has several values
  121.      * driven) the method {@link #getParameters(Field, FieldAbsoluteDate)} must be used.
  122.      * @since 9.0
  123.      */
  124.     default <T extends CalculusFieldElement<T>> T[] getParameters(final Field<T> field) {
  125.         final List<ParameterDriver> drivers = getParametersDrivers();
  126.         final T[] parameters = MathArrays.buildArray(field, drivers.size());
  127.         for (int i = 0; i < drivers.size(); ++i) {
  128.             parameters[i] = field.getZero().add(drivers.get(i).getValue());
  129.         }
  130.         return parameters;
  131.     }

  132.     /** Get model parameters.
  133.      * @param field field to which the elements belong
  134.      * @param <T> type of the elements
  135.      * @param date field date at which the parameters want to be known, can
  136.      * be new AbsoluteDate() if all the parameters have no validity period.
  137.      * @return model parameters
  138.      * @since 9.0
  139.      */
  140.     default <T extends CalculusFieldElement<T>> T[] getParameters(final Field<T> field, final FieldAbsoluteDate<T> date) {
  141.         final List<ParameterDriver> drivers = getParametersDrivers();
  142.         final T[] parameters = MathArrays.buildArray(field, drivers.size());
  143.         for (int i = 0; i < drivers.size(); ++i) {
  144.             parameters[i] = field.getZero().add(drivers.get(i).getValue(date.toAbsoluteDate()));
  145.         }
  146.         return parameters;
  147.     }

  148.     /** Get parameter value from its name.
  149.      * @param name parameter name
  150.      * @return parameter value
  151.      * @since 8.0
  152.      */
  153.     default ParameterDriver getParameterDriver(final String name) {

  154.         for (final ParameterDriver driver : getParametersDrivers()) {
  155.             if (name.equals(driver.getName())) {
  156.                 // we have found a parameter with that name
  157.                 return driver;
  158.             }
  159.         }
  160.         throw new UnsupportedParameterException(name, getParametersDrivers());
  161.     }

  162.     /** Check if a parameter is supported.
  163.      * <p>Supported parameters are those listed by {@link #getParametersDrivers()}.</p>
  164.      * @param name parameter name to check
  165.      * @return true if the parameter is supported
  166.      * @see #getParametersDrivers()
  167.      * @since 8.0
  168.      */
  169.     default boolean isSupported(String name) {
  170.         for (final ParameterDriver driver : getParametersDrivers()) {
  171.             if (name.equals(driver.getName())) {
  172.                 // we have found a parameter with that name
  173.                 return true;
  174.             }
  175.         }
  176.         // the parameter is not supported
  177.         return false;
  178.     }
  179. }