AbstractPropagatorBuilder.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.propagation.conversion;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.exception.LocalizedCoreFormats;
  21. import org.hipparchus.util.FastMath;
  22. import org.orekit.attitudes.AttitudeProvider;
  23. import org.orekit.attitudes.InertialProvider;
  24. import org.orekit.errors.OrekitIllegalArgumentException;
  25. import org.orekit.forces.gravity.NewtonianAttraction;
  26. import org.orekit.frames.Frame;
  27. import org.orekit.orbits.Orbit;
  28. import org.orekit.orbits.OrbitType;
  29. import org.orekit.orbits.PositionAngle;
  30. import org.orekit.propagation.integration.AdditionalEquations;
  31. import org.orekit.time.AbsoluteDate;
  32. import org.orekit.utils.ParameterDriver;
  33. import org.orekit.utils.ParameterDriversList;
  34. import org.orekit.utils.ParameterDriversList.DelegatingDriver;
  35. import org.orekit.utils.ParameterObserver;

  36. /** Base class for propagator builders.
  37.  * @author Pascal Parraud
  38.  * @since 7.1
  39.  */
  40. public abstract class AbstractPropagatorBuilder implements PropagatorBuilder {

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

  48.     /** Date of the initial orbit. */
  49.     private AbsoluteDate initialOrbitDate;

  50.     /** Frame in which the orbit is propagated. */
  51.     private final Frame frame;

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

  54.     /** Drivers for orbital parameters. */
  55.     private final ParameterDriversList orbitalDrivers;

  56.     /** List of the supported parameters. */
  57.     private ParameterDriversList propagationDrivers;

  58.     /** Orbit type to use. */
  59.     private final OrbitType orbitType;

  60.     /** Position angle type to use. */
  61.     private final PositionAngle positionAngle;

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

  64.     /** Attitude provider for the propagator. */
  65.     private AttitudeProvider attitudeProvider;

  66.     /** Additional equations. */
  67.     private List<AdditionalEquations> additionalEquations;

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

  101.     /** Build a new instance.
  102.      * <p>
  103.      * The template orbit is used as a model to {@link
  104.      * #createInitialOrbit() create initial orbit}. It defines the
  105.      * inertial frame, the central attraction coefficient, the orbit type, and is also
  106.      * used together with the {@code positionScale} to convert from the {@link
  107.      * ParameterDriver#setNormalizedValue(double) normalized} parameters used by the
  108.      * callers of this builder to the real orbital parameters.
  109.      * </p>
  110.      * <p>
  111.      * By default, all the {@link #getOrbitalParametersDrivers() orbital parameters drivers}
  112.      * are selected, which means that if the builder is used for orbit determination or
  113.      * propagator conversion, all orbital parameters will be estimated. If only a subset
  114.      * of the orbital parameters must be estimated, caller must retrieve the orbital
  115.      * parameters by calling {@link #getOrbitalParametersDrivers()} and then call
  116.      * {@link ParameterDriver#setSelected(boolean) setSelected(false)}.
  117.      * </p>
  118.      * @param templateOrbit reference orbit from which real orbits will be built
  119.      * @param positionAngle position angle type to use
  120.      * @param positionScale scaling factor used for orbital parameters normalization
  121.      * (typically set to the expected standard deviation of the position)
  122.      * @param addDriverForCentralAttraction if true, a {@link ParameterDriver} should
  123.      * be set up for central attraction coefficient
  124.      * @param attitudeProvider for the propagator.
  125.      * @since 10.1
  126.      * @see #AbstractPropagatorBuilder(Orbit, PositionAngle, double, boolean)
  127.      */
  128.     protected AbstractPropagatorBuilder(final Orbit templateOrbit,
  129.                                         final PositionAngle positionAngle,
  130.                                         final double positionScale,
  131.                                         final boolean addDriverForCentralAttraction,
  132.                                         final AttitudeProvider attitudeProvider) {

  133.         this.initialOrbitDate    = templateOrbit.getDate();
  134.         this.frame               = templateOrbit.getFrame();
  135.         this.mu                  = templateOrbit.getMu();
  136.         this.propagationDrivers  = new ParameterDriversList();
  137.         this.orbitType           = templateOrbit.getType();
  138.         this.positionAngle       = positionAngle;
  139.         this.positionScale       = positionScale;
  140.         this.orbitalDrivers      = orbitType.getDrivers(positionScale, templateOrbit, positionAngle);
  141.         this.attitudeProvider = attitudeProvider;
  142.         for (final DelegatingDriver driver : orbitalDrivers.getDrivers()) {
  143.             driver.setSelected(true);
  144.         }

  145.         this.additionalEquations  = new ArrayList<AdditionalEquations>();

  146.         if (addDriverForCentralAttraction) {
  147.             final ParameterDriver muDriver = new ParameterDriver(NewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT,
  148.                                                                  mu, MU_SCALE, 0, Double.POSITIVE_INFINITY);
  149.             muDriver.addObserver(new ParameterObserver() {
  150.                 /** {@inheridDoc} */
  151.                 @Override
  152.                 public void valueChanged(final double previousValue, final ParameterDriver driver) {
  153.                     AbstractPropagatorBuilder.this.mu = driver.getValue();
  154.                 }
  155.             });
  156.             propagationDrivers.add(muDriver);
  157.         }

  158.     }

  159.     /** {@inheritDoc} */
  160.     public OrbitType getOrbitType() {
  161.         return orbitType;
  162.     }

  163.     /** {@inheritDoc} */
  164.     public PositionAngle getPositionAngle() {
  165.         return positionAngle;
  166.     }

  167.     /** {@inheritDoc} */
  168.     public AbsoluteDate getInitialOrbitDate() {
  169.         return initialOrbitDate;
  170.     }

  171.     /** {@inheritDoc} */
  172.     public Frame getFrame() {
  173.         return frame;
  174.     }

  175.     /** {@inheritDoc} */
  176.     public ParameterDriversList getOrbitalParametersDrivers() {
  177.         return orbitalDrivers;
  178.     }

  179.     /** {@inheritDoc} */
  180.     public ParameterDriversList getPropagationParametersDrivers() {
  181.         return propagationDrivers;
  182.     }

  183.     /**
  184.      * Get the attitude provider.
  185.      *
  186.      * @return the attitude provider
  187.      * @since 10.1
  188.      */
  189.     public AttitudeProvider getAttitudeProvider() {
  190.         return attitudeProvider;
  191.     }

  192.     /**
  193.      * Set the attitude provider.
  194.      *
  195.      * @param attitudeProvider attitude provider
  196.      * @since 10.1
  197.      */
  198.     public void setAttitudeProvider(final AttitudeProvider attitudeProvider) {
  199.         this.attitudeProvider = attitudeProvider;
  200.     }

  201.     /** Get the position scale.
  202.      * @return the position scale used to scale the orbital drivers
  203.      */
  204.     public double getPositionScale() {
  205.         return positionScale;
  206.     }

  207.     /** Get the central attraction coefficient (µ - m³/s²) value.
  208.      * @return the central attraction coefficient (µ - m³/s²) value
  209.      * @since 9.2
  210.      */
  211.     public double getMu() {
  212.         return mu;
  213.     }

  214.     /** Get the number of selected parameters.
  215.      * @return number of selected parameters
  216.      */
  217.     private int getNbSelected() {

  218.         int count = 0;

  219.         // count orbital parameters
  220.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  221.             if (driver.isSelected()) {
  222.                 ++count;
  223.             }
  224.         }

  225.         // count propagation parameters
  226.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  227.             if (driver.isSelected()) {
  228.                 ++count;
  229.             }
  230.         }

  231.         return count;

  232.     }

  233.     /** {@inheritDoc} */
  234.     public double[] getSelectedNormalizedParameters() {

  235.         // allocate array
  236.         final double[] selected = new double[getNbSelected()];

  237.         // fill data
  238.         int index = 0;
  239.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  240.             if (driver.isSelected()) {
  241.                 selected[index++] = driver.getNormalizedValue();
  242.             }
  243.         }
  244.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  245.             if (driver.isSelected()) {
  246.                 selected[index++] = driver.getNormalizedValue();
  247.             }
  248.         }

  249.         return selected;

  250.     }

  251.     /** Build an initial orbit using the current selected parameters.
  252.      * <p>
  253.      * This method is a stripped down version of {@link #buildPropagator(double[])}
  254.      * that only builds the initial orbit and not the full propagator.
  255.      * </p>
  256.      * @return an initial orbit
  257.      * @since 8.0
  258.      */
  259.     protected Orbit createInitialOrbit() {
  260.         final double[] unNormalized = new double[orbitalDrivers.getNbParams()];
  261.         for (int i = 0; i < unNormalized.length; ++i) {
  262.             unNormalized[i] = orbitalDrivers.getDrivers().get(i).getValue();
  263.         }
  264.         return getOrbitType().mapArrayToOrbit(unNormalized, null, positionAngle, initialOrbitDate, mu, frame);
  265.     }

  266.     /** Set the selected parameters.
  267.      * @param normalizedParameters normalized values for the selected parameters
  268.      */
  269.     protected void setParameters(final double[] normalizedParameters) {


  270.         if (normalizedParameters.length != getNbSelected()) {
  271.             throw new OrekitIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  272.                                                      normalizedParameters.length,
  273.                                                      getNbSelected());
  274.         }

  275.         int index = 0;

  276.         // manage orbital parameters
  277.         for (final ParameterDriver driver : orbitalDrivers.getDrivers()) {
  278.             if (driver.isSelected()) {
  279.                 driver.setNormalizedValue(normalizedParameters[index++]);
  280.             }
  281.         }

  282.         // manage propagation parameters
  283.         for (final ParameterDriver driver : propagationDrivers.getDrivers()) {
  284.             if (driver.isSelected()) {
  285.                 driver.setNormalizedValue(normalizedParameters[index++]);
  286.             }
  287.         }

  288.     }

  289.     /** Add a supported parameter.
  290.      * @param driver driver for the parameter
  291.      */
  292.     protected void addSupportedParameter(final ParameterDriver driver) {
  293.         propagationDrivers.add(driver);
  294.         propagationDrivers.sort();
  295.     }

  296.     /** Reset the orbit in the propagator builder.
  297.      * @param newOrbit New orbit to set in the propagator builder
  298.      */
  299.     public void resetOrbit(final Orbit newOrbit) {

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

  303.         // Update all the orbital drivers, selected or unselected
  304.         // Reset values and reference values
  305.         final List<DelegatingDriver> orbitalDriversList = getOrbitalParametersDrivers().getDrivers();
  306.         int i = 0;
  307.         for (DelegatingDriver driver : orbitalDriversList) {
  308.             driver.setReferenceValue(orbitArray[i]);
  309.             driver.setValue(orbitArray[i++]);
  310.         }

  311.         // Change the initial orbit date in the builder
  312.         this.initialOrbitDate = newOrbit.getDate();
  313.     }

  314.     /** Add a set of user-specified equations to be integrated along with the orbit propagation (author Shiva Iyer).
  315.      * @param additional additional equations
  316.      * @since 10.1
  317.      */
  318.     public void addAdditionalEquations(final AdditionalEquations additional) {

  319.         additionalEquations.add(additional);

  320.     }

  321.     /** Get the list of additional equations.
  322.      * @return the list of additional equations
  323.      * @since 10.1
  324.      */
  325.     protected List<AdditionalEquations> getAdditionalEquations() {
  326.         return additionalEquations;
  327.     }

  328. }