AbstractPropagatorBuilder.java

  1. /* Copyright 2002-2019 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 java.util.List;

  19. import org.hipparchus.exception.LocalizedCoreFormats;
  20. import org.hipparchus.util.FastMath;
  21. import org.orekit.errors.OrekitIllegalArgumentException;
  22. import org.orekit.forces.gravity.NewtonianAttraction;
  23. import org.orekit.frames.Frame;
  24. import org.orekit.orbits.Orbit;
  25. import org.orekit.orbits.OrbitType;
  26. import org.orekit.orbits.PositionAngle;
  27. import org.orekit.time.AbsoluteDate;
  28. import org.orekit.utils.ParameterDriver;
  29. import org.orekit.utils.ParameterDriversList;
  30. import org.orekit.utils.ParameterDriversList.DelegatingDriver;
  31. import org.orekit.utils.ParameterObserver;

  32. /** Base class for propagator builders.
  33.  * @author Pascal Parraud
  34.  * @since 7.1
  35.  */
  36. public abstract class AbstractPropagatorBuilder implements PropagatorBuilder {

  37.     /** Central attraction scaling factor.
  38.      * <p>
  39.      * We use a power of 2 to avoid numeric noise introduction
  40.      * in the multiplications/divisions sequences.
  41.      * </p>
  42.      */
  43.     private static final double MU_SCALE = FastMath.scalb(1.0, 32);

  44.     /** Date of the initial orbit. */
  45.     private AbsoluteDate initialOrbitDate;

  46.     /** Frame in which the orbit is propagated. */
  47.     private final Frame frame;

  48.     /** Central attraction coefficient (m³/s²). */
  49.     private double mu;

  50.     /** Drivers for orbital parameters. */
  51.     private final ParameterDriversList orbitalDrivers;

  52.     /** List of the supported parameters. */
  53.     private ParameterDriversList propagationDrivers;

  54.     /** Orbit type to use. */
  55.     private final OrbitType orbitType;

  56.     /** Position angle type to use. */
  57.     private final PositionAngle positionAngle;

  58.     /** Position scale to use for the orbital drivers. */
  59.     private final double positionScale;

  60.     /** Build a new instance.
  61.      * <p>
  62.      * The template orbit is used as a model to {@link
  63.      * #createInitialOrbit() create initial orbit}. It defines the
  64.      * inertial frame, the central attraction coefficient, the orbit type, and is also
  65.      * used together with the {@code positionScale} to convert from the {@link
  66.      * ParameterDriver#setNormalizedValue(double) normalized} parameters used by the
  67.      * callers of this builder to the real orbital parameters.
  68.      * </p>
  69.      * <p>
  70.      * By default, all the {@link #getOrbitalParametersDrivers() orbital parameters drivers}
  71.      * are selected, which means that if the builder is used for orbit determination or
  72.      * propagator conversion, all orbital parameters will be estimated. If only a subset
  73.      * of the orbital parameters must be estimated, caller must retrieve the orbital
  74.      * parameters by calling {@link #getOrbitalParametersDrivers()} and then call
  75.      * {@link ParameterDriver#setSelected(boolean) setSelected(false)}.
  76.      * </p>
  77.      * @param templateOrbit reference orbit from which real orbits will be built
  78.      * @param positionAngle position angle type to use
  79.      * @param positionScale scaling factor used for orbital parameters normalization
  80.      * (typically set to the expected standard deviation of the position)
  81.      * @param addDriverForCentralAttraction if true, a {@link ParameterDriver} should
  82.      * be set up for central attraction coefficient
  83.           * @since 8.0
  84.      */
  85.     protected AbstractPropagatorBuilder(final Orbit templateOrbit, final PositionAngle positionAngle,
  86.                                         final double positionScale, final boolean addDriverForCentralAttraction) {

  87.         this.initialOrbitDate    = templateOrbit.getDate();
  88.         this.frame               = templateOrbit.getFrame();
  89.         this.mu                  = templateOrbit.getMu();
  90.         this.propagationDrivers  = new ParameterDriversList();
  91.         this.orbitType           = templateOrbit.getType();
  92.         this.positionAngle       = positionAngle;
  93.         this.positionScale       = positionScale;
  94.         this.orbitalDrivers      = orbitType.getDrivers(positionScale, templateOrbit, positionAngle);
  95.         for (final DelegatingDriver driver : orbitalDrivers.getDrivers()) {
  96.             driver.setSelected(true);
  97.         }

  98.         if (addDriverForCentralAttraction) {
  99.             final ParameterDriver muDriver = new ParameterDriver(NewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT,
  100.                                                                  mu, MU_SCALE, 0, Double.POSITIVE_INFINITY);
  101.             muDriver.addObserver(new ParameterObserver() {
  102.                 /** {@inheridDoc} */
  103.                 @Override
  104.                 public void valueChanged(final double previousValue, final ParameterDriver driver) {
  105.                     AbstractPropagatorBuilder.this.mu = driver.getValue();
  106.                 }
  107.             });
  108.             propagationDrivers.add(muDriver);
  109.         }

  110.     }

  111.     /** {@inheritDoc} */
  112.     public OrbitType getOrbitType() {
  113.         return orbitType;
  114.     }

  115.     /** {@inheritDoc} */
  116.     public PositionAngle getPositionAngle() {
  117.         return positionAngle;
  118.     }

  119.     /** {@inheritDoc} */
  120.     public AbsoluteDate getInitialOrbitDate() {
  121.         return initialOrbitDate;
  122.     }

  123.     /** {@inheritDoc} */
  124.     public Frame getFrame() {
  125.         return frame;
  126.     }

  127.     /** {@inheritDoc} */
  128.     public ParameterDriversList getOrbitalParametersDrivers() {
  129.         return orbitalDrivers;
  130.     }

  131.     /** {@inheritDoc} */
  132.     public ParameterDriversList getPropagationParametersDrivers() {
  133.         return propagationDrivers;
  134.     }

  135.     /** Get the position scale.
  136.      * @return the position scale used to scale the orbital drivers
  137.      */
  138.     public double getPositionScale() {
  139.         return positionScale;
  140.     }

  141.     /** Get the central attraction coefficient (µ - m³/s²) value.
  142.      * @return the central attraction coefficient (µ - m³/s²) value
  143.      * @since 9.2
  144.      */
  145.     public double getMu() {
  146.         return mu;
  147.     }

  148.     /** Get the number of selected parameters.
  149.      * @return number of selected parameters
  150.      */
  151.     private int getNbSelected() {

  152.         int count = 0;

  153.         // count orbital parameters
  154.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  155.             if (driver.isSelected()) {
  156.                 ++count;
  157.             }
  158.         }

  159.         // count propagation parameters
  160.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  161.             if (driver.isSelected()) {
  162.                 ++count;
  163.             }
  164.         }

  165.         return count;

  166.     }

  167.     /** {@inheritDoc} */
  168.     public double[] getSelectedNormalizedParameters() {

  169.         // allocate array
  170.         final double[] selected = new double[getNbSelected()];

  171.         // fill data
  172.         int index = 0;
  173.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  174.             if (driver.isSelected()) {
  175.                 selected[index++] = driver.getNormalizedValue();
  176.             }
  177.         }
  178.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  179.             if (driver.isSelected()) {
  180.                 selected[index++] = driver.getNormalizedValue();
  181.             }
  182.         }

  183.         return selected;

  184.     }

  185.     /** Build an initial orbit using the current selected parameters.
  186.      * <p>
  187.      * This method is a stripped down version of {@link #buildPropagator(double[])}
  188.      * that only builds the initial orbit and not the full propagator.
  189.      * </p>
  190.      * @return an initial orbit
  191.      * @since 8.0
  192.      */
  193.     protected Orbit createInitialOrbit() {
  194.         final double[] unNormalized = new double[orbitalDrivers.getNbParams()];
  195.         for (int i = 0; i < unNormalized.length; ++i) {
  196.             unNormalized[i] = orbitalDrivers.getDrivers().get(i).getValue();
  197.         }
  198.         return getOrbitType().mapArrayToOrbit(unNormalized, null, positionAngle, initialOrbitDate, mu, frame);
  199.     }

  200.     /** Set the selected parameters.
  201.      * @param normalizedParameters normalized values for the selected parameters
  202.      */
  203.     protected void setParameters(final double[] normalizedParameters) {


  204.         if (normalizedParameters.length != getNbSelected()) {
  205.             throw new OrekitIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  206.                                                      normalizedParameters.length,
  207.                                                      getNbSelected());
  208.         }

  209.         int index = 0;

  210.         // manage orbital parameters
  211.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  212.             if (driver.isSelected()) {
  213.                 driver.setNormalizedValue(normalizedParameters[index++]);
  214.             }
  215.         }

  216.         // manage propagation parameters
  217.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  218.             if (driver.isSelected()) {
  219.                 driver.setNormalizedValue(normalizedParameters[index++]);
  220.             }
  221.         }

  222.     }

  223.     /** Add a supported parameter.
  224.      * @param driver driver for the parameter
  225.      */
  226.     protected void addSupportedParameter(final ParameterDriver driver) {
  227.         propagationDrivers.add(driver);
  228.         propagationDrivers.sort();
  229.     }

  230.     /** Reset the orbit in the propagator builder.
  231.      * @param newOrbit New orbit to set in the propagator builder
  232.      */
  233.     public void resetOrbit(final Orbit newOrbit) {

  234.         // Map the new orbit in an array of double
  235.         final double[] orbitArray = new double[6];
  236.         orbitType.mapOrbitToArray(newOrbit, getPositionAngle(), orbitArray, null);

  237.         // Update all the orbital drivers, selected or unselected
  238.         // Reset values and reference values
  239.         final List<DelegatingDriver> orbitalDriversList = getOrbitalParametersDrivers().getDrivers();
  240.         int i = 0;
  241.         for (DelegatingDriver driver : orbitalDriversList) {
  242.             driver.setReferenceValue(orbitArray[i]);
  243.             driver.setValue(orbitArray[i++]);
  244.         }

  245.         // Change the initial orbit date in the builder
  246.         this.initialOrbitDate = newOrbit.getDate();
  247.     }
  248. }