NumericalPropagatorBuilder.java

  1. /* Copyright 2002-2013 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.Collection;
  20. import java.util.List;

  21. import org.apache.commons.math3.exception.util.LocalizedFormats;
  22. import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
  23. import org.orekit.attitudes.Attitude;
  24. import org.orekit.attitudes.AttitudeProvider;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.forces.ForceModel;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.orbits.CartesianOrbit;
  29. import org.orekit.orbits.Orbit;
  30. import org.orekit.orbits.OrbitType;
  31. import org.orekit.propagation.Propagator;
  32. import org.orekit.propagation.SpacecraftState;
  33. import org.orekit.propagation.numerical.NumericalPropagator;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.utils.PVCoordinates;

  36. /** Builder for numerical propagator.
  37.  * @author Pascal Parraud
  38.  * @since 6.0
  39.  */
  40. public class NumericalPropagatorBuilder implements PropagatorBuilder {

  41.     /** Central attraction coefficient (m<sup>3</sup>/s<sup>2</sup>). */
  42.     private final double mu;

  43.     /** Frame in which the orbit is propagated. */
  44.     private final Frame frame;

  45.     /** First order integrator builder for propagation. */
  46.     private final FirstOrderIntegratorBuilder builder;

  47.     /** Force models used during the extrapolation of the orbit. */
  48.     private final List<ForceModel> forceModels;

  49.     /** Current mass for initial state (kg). */
  50.     private double mass;

  51.     /** Attitude provider. */
  52.     private AttitudeProvider attProvider;

  53.     /** List of the free parameters names. */
  54.     private Collection<String> freeParameters;

  55.     /** Build a new instance.
  56.      * @param mu central attraction coefficient (m<sup>3</sup>/s<sup>2</sup>)
  57.      * @param frame the frame in which the orbit is propagated
  58.      * (<em>must</em> be a {@link Frame#isPseudoInertial pseudo-inertial frame})
  59.      * @param builder first order integrator builder
  60.      */
  61.     public NumericalPropagatorBuilder(final double mu,
  62.                                       final Frame frame,
  63.                                       final FirstOrderIntegratorBuilder builder) {
  64.         this.mu          = mu;
  65.         this.frame       = frame;
  66.         this.builder     = builder;
  67.         this.forceModels = new ArrayList<ForceModel>();
  68.         this.mass        = Propagator.DEFAULT_MASS;
  69.         this.attProvider = Propagator.DEFAULT_LAW;
  70.     }

  71.     /** Set the attitude provider.
  72.      * @param attitudeProvider attitude provider
  73.      */
  74.     public void setAttitudeProvider(final AttitudeProvider attitudeProvider) {
  75.         this.attProvider = attitudeProvider;
  76.     }

  77.     /** Set the initial mass.
  78.      * @param mass the mass (kg)
  79.      */
  80.     public void setMass(final double mass) {
  81.         this.mass = mass;
  82.     }

  83.     /** Add a force model to the global perturbation model.
  84.      * <p>If this method is not called at all, the integrated orbit will follow
  85.      * a keplerian evolution only.</p>
  86.      * @param model perturbing {@link ForceModel} to add
  87.      */
  88.     public void addForceModel(final ForceModel model) {
  89.         forceModels.add(model);
  90.     }

  91.     /** {@inheritDoc} */
  92.     public NumericalPropagator buildPropagator(final AbsoluteDate date, final double[] parameters)
  93.         throws OrekitException {

  94.         if (parameters.length != (freeParameters.size() + 6)) {
  95.             throw OrekitException.createIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH);
  96.         }

  97.         final Orbit orb = new CartesianOrbit(new PVCoordinates(new Vector3D(parameters[0],
  98.                                                                             parameters[1],
  99.                                                                             parameters[2]),
  100.                                                                new Vector3D(parameters[3],
  101.                                                                             parameters[4],
  102.                                                                             parameters[5])),
  103.                                               frame, date, mu);

  104.         final Attitude attitude = attProvider.getAttitude(orb, date, frame);

  105.         final SpacecraftState state = new SpacecraftState(orb, attitude, mass);

  106.         for (int i = 6; i < parameters.length; i++) {
  107.             for (String free : freeParameters) {
  108.                 for (String available : getParametersNames()) {
  109.                     if (free.equals(available)) {
  110.                         setParameter(free, parameters[i]);
  111.                     }
  112.                 }
  113.             }
  114.         }

  115.         final NumericalPropagator propagator = new NumericalPropagator(builder.buildIntegrator(orb));
  116.         propagator.setOrbitType(OrbitType.CARTESIAN);
  117.         propagator.setAttitudeProvider(attProvider);
  118.         for (ForceModel model : forceModels) {
  119.             propagator.addForceModel(model);
  120.         }
  121.         propagator.resetInitialState(state);

  122.         return propagator;
  123.     }

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

  128.     /** {@inheritDoc} */
  129.     public void setFreeParameters(final Collection<String> parameters)
  130.         throws IllegalArgumentException {
  131.         freeParameters = new ArrayList<String>();
  132.         for (String name : parameters) {
  133.             if (!isSupported(name)) {
  134.                 throw OrekitException.createIllegalArgumentException(LocalizedFormats.UNKNOWN_PARAMETER, name);
  135.             }
  136.         }
  137.         freeParameters.addAll(parameters);
  138.     }

  139.     /** {@inheritDoc} */
  140.     public Collection<String> getParametersNames() {
  141.         final Collection<String> parametersNames = new ArrayList<String>();
  142.         for (ForceModel model : forceModels) {
  143.             parametersNames.addAll(model.getParametersNames());
  144.         }
  145.         return parametersNames;
  146.     }

  147.     /** {@inheritDoc} */
  148.     public boolean isSupported(final String name) {
  149.         for (ForceModel model : forceModels) {
  150.             if (model.isSupported(name)) {
  151.                 return true;
  152.             }
  153.         }
  154.         return false;
  155.     }

  156.     /** {@inheritDoc} */
  157.     public double getParameter(final String name)
  158.         throws IllegalArgumentException {
  159.         for (ForceModel model : forceModels) {
  160.             if (model.isSupported(name)) {
  161.                 return model.getParameter(name);
  162.             }
  163.         }
  164.         throw new IllegalArgumentException(name);
  165.     }

  166.     /** {@inheritDoc} */
  167.     public void setParameter(final String name, final double value)
  168.         throws IllegalArgumentException {
  169.         for (ForceModel model : forceModels) {
  170.             if (model.isSupported(name)) {
  171.                 model.setParameter(name, value);
  172.                 break;
  173.             }
  174.         }
  175.     }

  176. }