AbstractPropagatorBuilder.java

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

  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.orbitalDrivers      = orbitType.getDrivers(positionScale, templateOrbit, positionAngle);
  94.         for (final DelegatingDriver driver : orbitalDrivers.getDrivers()) {
  95.             driver.setSelected(true);
  96.         }

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

  109.     }

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

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

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

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

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

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

  134.     /** Get the number of selected parameters.
  135.      * @return number of selected parameters
  136.      */
  137.     private int getNbSelected() {

  138.         int count = 0;

  139.         // count orbital parameters
  140.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  141.             if (driver.isSelected()) {
  142.                 ++count;
  143.             }
  144.         }

  145.         // count propagation parameters
  146.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  147.             if (driver.isSelected()) {
  148.                 ++count;
  149.             }
  150.         }

  151.         return count;

  152.     }

  153.     /** {@inheritDoc} */
  154.     public double[] getSelectedNormalizedParameters() {

  155.         // allocate array
  156.         final double[] selected = new double[getNbSelected()];

  157.         // fill data
  158.         int index = 0;
  159.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  160.             if (driver.isSelected()) {
  161.                 selected[index++] = driver.getNormalizedValue();
  162.             }
  163.         }
  164.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  165.             if (driver.isSelected()) {
  166.                 selected[index++] = driver.getNormalizedValue();
  167.             }
  168.         }

  169.         return selected;

  170.     }

  171.     /** Build an initial orbit using the current selected parameters.
  172.      * <p>
  173.      * This method is a stripped down version of {@link #buildPropagator(double[])}
  174.      * that only builds the initial orbit and not the full propagator.
  175.      * </p>
  176.      * @return an initial orbit
  177.      * @since 8.0
  178.      */
  179.     protected Orbit createInitialOrbit() {
  180.         final double[] unNormalized = new double[orbitalDrivers.getNbParams()];
  181.         for (int i = 0; i < unNormalized.length; ++i) {
  182.             unNormalized[i] = orbitalDrivers.getDrivers().get(i).getValue();
  183.         }
  184.         return getOrbitType().mapArrayToOrbit(unNormalized, null, positionAngle, initialOrbitDate, mu, frame);
  185.     }

  186.     /** Set the selected parameters.
  187.      * @param normalizedParameters normalized values for the selected parameters
  188.      * @exception OrekitException if some parameter cannot be set to the specified value
  189.      * @exception OrekitIllegalArgumentException if the number of parameters is not the
  190.      * number of selected parameters (adding orbits and models parameters)
  191.      */
  192.     protected void setParameters(final double[] normalizedParameters)
  193.         throws OrekitException, OrekitIllegalArgumentException {


  194.         if (normalizedParameters.length != getNbSelected()) {
  195.             throw new OrekitIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  196.                                                      normalizedParameters.length,
  197.                                                      getNbSelected());
  198.         }

  199.         int index = 0;

  200.         // manage orbital parameters
  201.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  202.             if (driver.isSelected()) {
  203.                 driver.setNormalizedValue(normalizedParameters[index++]);
  204.             }
  205.         }

  206.         // manage propagation parameters
  207.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  208.             if (driver.isSelected()) {
  209.                 driver.setNormalizedValue(normalizedParameters[index++]);
  210.             }
  211.         }

  212.     }

  213.     /** Add a supported parameter.
  214.      * @param driver driver for the parameter
  215.      * @exception OrekitException if the name is already supported
  216.      */
  217.     protected void addSupportedParameter(final ParameterDriver driver)
  218.         throws OrekitException {
  219.         propagationDrivers.add(driver);
  220.         propagationDrivers.sort();
  221.     }

  222. }