AbstractPropagatorBuilder.java

  1. /* Copyright 2002-2016 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.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.apache.commons.math3.exception.util.LocalizedFormats;
  22. import org.orekit.errors.OrekitIllegalArgumentException;
  23. import org.orekit.errors.OrekitMessages;
  24. import org.orekit.forces.gravity.NewtonianAttraction;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.orbits.Orbit;
  27. import org.orekit.orbits.OrbitType;
  28. import org.orekit.orbits.PositionAngle;
  29. import org.orekit.time.AbsoluteDate;

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

  35.     /** Frame in which the orbit is propagated. */
  36.     private final Frame frame;

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

  39.     /** List of the supported parameters names. */
  40.     private List<String> supportedParameters;

  41.     /** List of the free parameters names. */
  42.     private List<String> freeParameters;

  43.     /** Orbit type to use. */
  44.     private final OrbitType orbitType;

  45.     /** Position angle type to use. */
  46.     private final PositionAngle positionAngle;

  47.     /** Build a new instance.
  48.      * @param frame the frame in which the orbit is propagated
  49.      *        (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  50.      * @param mu central attraction coefficient (m³/s²)
  51.      * @param orbitType orbit type to use
  52.      * @param positionAngle position angle type to use
  53.      * @since 7.1
  54.      */
  55.     public AbstractPropagatorBuilder(final Frame frame, final double mu,
  56.                                      final OrbitType orbitType, final PositionAngle positionAngle) {
  57.         this.frame               = frame;
  58.         this.mu                  = mu;
  59.         this.supportedParameters = new ArrayList<String>();
  60.         this.freeParameters      = new ArrayList<String>();
  61.         this.orbitType           = orbitType;
  62.         this.positionAngle       = positionAngle;
  63.         addSupportedParameter(NewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT);
  64.     }

  65.     /** {@inheritDoc} */
  66.     public OrbitType getOrbitType() {
  67.         return orbitType;
  68.     }

  69.     /** {@inheritDoc} */
  70.     public PositionAngle getPositionAngle() {
  71.         return positionAngle;
  72.     }

  73.     /** {@inheritDoc} */
  74.     public Frame getFrame() {
  75.         return frame;
  76.     }

  77.     /** {@inheritDoc} */
  78.     public List<String> getSupportedParameters() {
  79.         return Collections.unmodifiableList(supportedParameters);
  80.     }

  81.     /** {@inheritDoc} */
  82.     public void setFreeParameters(final List<String> parameters)
  83.         throws OrekitIllegalArgumentException {
  84.         for (String name : parameters) {
  85.             if (!supportedParameters.contains(name)) {
  86.                 throw new OrekitIllegalArgumentException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  87.                                                          name, supportedAsString());
  88.             }
  89.         }
  90.         freeParameters = new ArrayList<String>(parameters);
  91.     }

  92.     /** {@inheritDoc} */
  93.     public List<String> getFreeParameters() {
  94.         return Collections.unmodifiableList(freeParameters);
  95.     }

  96.     /** {@inheritDoc}
  97.      * <p>
  98.      * The abstract base class only supports {@link
  99.      * NewtonianAttraction#CENTRAL_ATTRACTION_COEFFICIENT}, specialized propagator
  100.      * builders may support more parameters.
  101.      * </p>
  102.      */
  103.     public double getParameter(final String name)
  104.         throws OrekitIllegalArgumentException {
  105.         if (NewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT.equals(name)) {
  106.             return mu;
  107.         }
  108.         throw new OrekitIllegalArgumentException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  109.                                                  name, supportedAsString());
  110.     }

  111.     /** {@inheritDoc}
  112.      * <p>
  113.      * The abstract base class only supports {@link
  114.      * NewtonianAttraction#CENTRAL_ATTRACTION_COEFFICIENT}, specialized propagator
  115.      * builders may support more parameters.
  116.      * </p>
  117.      */
  118.     public void setParameter(final String name, final double value)
  119.         throws OrekitIllegalArgumentException {
  120.         if (NewtonianAttraction.CENTRAL_ATTRACTION_COEFFICIENT.equals(name)) {
  121.             this.mu = value;
  122.         } else {
  123.             throw new OrekitIllegalArgumentException(OrekitMessages.UNSUPPORTED_PARAMETER_NAME,
  124.                                                      name, supportedAsString());
  125.         }
  126.     }

  127.     /** Check the size of the parameters array.
  128.      * @param parameters to configure the propagator
  129.      * @exception OrekitIllegalArgumentException if the number of
  130.      * parameters is not 6 (for initial state) plus the number of free
  131.      * parameters
  132.      */
  133.     protected void checkParameters(final double[] parameters)
  134.         throws OrekitIllegalArgumentException {
  135.         if (parameters.length != (freeParameters.size() + 6)) {
  136.             throw new OrekitIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
  137.                                                      parameters.length, freeParameters.size() + 6);
  138.         }
  139.     }

  140.     /** Crate the orbit for the first 6 parameters.
  141.      * @param date date associated to the parameters to configure the initial state
  142.      * @param parameters set of position/velocity(/free) parameters to configure the propagator
  143.      * @return initial orbit
  144.      */
  145.     protected Orbit createInitialOrbit(final AbsoluteDate date, final double[] parameters) {
  146.         return getOrbitType().mapArrayToOrbit(parameters, positionAngle, date, mu, frame);
  147.     }

  148.     /** Add a supported parameter name.
  149.      * @param name name of a supported parameter
  150.      * @exception OrekitIllegalArgumentException if the name is already supported
  151.      */
  152.     protected void addSupportedParameter(final String name)
  153.         throws OrekitIllegalArgumentException {
  154.         if (supportedParameters.contains(name)) {
  155.             throw new OrekitIllegalArgumentException(OrekitMessages.DUPLICATED_PARAMETER_NAME, name);
  156.         }
  157.         supportedParameters.add(name);
  158.     }

  159.     /** Create a string with the list of supported parameters.
  160.      * @return string with the list of supported parameters
  161.      */
  162.     private String supportedAsString() {
  163.         final StringBuilder supported = new StringBuilder();
  164.         for (final String name : supportedParameters) {
  165.             if (supported.length() > 0) {
  166.                 supported.append(", ");
  167.             }
  168.             supported.append(name);
  169.         }
  170.         return supported.toString();
  171.     }

  172. }