ProfileThrustPropulsionModel.java

  1. /* Copyright 2022-2025 Luc Maisonobe
  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.forces.maneuvers.propulsion;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.stream.Stream;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.Field;
  24. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  25. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  26. import org.orekit.forces.maneuvers.Control3DVectorCostType;
  27. import org.orekit.propagation.FieldSpacecraftState;
  28. import org.orekit.propagation.SpacecraftState;
  29. import org.orekit.propagation.events.DateDetector;
  30. import org.orekit.propagation.events.EventDetector;
  31. import org.orekit.propagation.events.FieldDateDetector;
  32. import org.orekit.propagation.events.FieldEventDetector;
  33. import org.orekit.time.AbsoluteDate;
  34. import org.orekit.time.TimeStamped;
  35. import org.orekit.utils.ParameterDriver;
  36. import org.orekit.utils.TimeSpanMap;

  37. /** Thrust propulsion model based on segmented profile.
  38.  * @author Luc Maisonobe
  39.  * @since 12.0
  40.  */
  41. public class ProfileThrustPropulsionModel implements ThrustPropulsionModel {

  42.     /** Thrust profile. */
  43.     private final TimeSpanMap<ThrustVectorProvider> profile;

  44.     /** Specific impulse. */
  45.     private final double isp;

  46.     /** Name of the maneuver. */
  47.     private final String name;

  48.     /** Type of norm linking thrust vector to mass flow rate. */
  49.     private final Control3DVectorCostType control3DVectorCostType;

  50.     /** Constructor with default cost type.
  51.      * @param profile thrust profile (N)
  52.      * @param isp specific impulse (s)
  53.      * @param name name of the maneuver
  54.      * @since 13.0
  55.      */
  56.     public ProfileThrustPropulsionModel(final TimeSpanMap<ThrustVectorProvider> profile, final double isp,
  57.                                         final String name) {
  58.         this(profile, isp, Control3DVectorCostType.TWO_NORM, name);
  59.     }

  60.     /** Generic constructor.
  61.      * @param profile thrust profile (N)
  62.      * @param isp specific impulse (s)
  63.      * @param control3DVectorCostType control vector's cost type
  64.      * @param name name of the maneuver
  65.      * @since 13.0
  66.      */
  67.     public ProfileThrustPropulsionModel(final TimeSpanMap<ThrustVectorProvider> profile, final double isp,
  68.                                         final Control3DVectorCostType control3DVectorCostType, final String name) {
  69.         this.name    = name;
  70.         this.isp     = isp;
  71.         this.profile = profile;
  72.         this.control3DVectorCostType = control3DVectorCostType;
  73.     }

  74.     /** Build with customized profile.
  75.      * @param profile thrust profile (N)
  76.      * @param isp specific impulse (s)
  77.      * @param name name of the maneuver
  78.      * @param control3DVectorCostType control vector's cost type
  79.      * @param <T> segment type
  80.      * @return propulsion model
  81.      * @since 13.0
  82.      */
  83.     public static <T extends ThrustVectorProvider> ProfileThrustPropulsionModel of(final TimeSpanMap<T> profile,
  84.                                                                                    final double isp,
  85.                                                                                    final Control3DVectorCostType control3DVectorCostType,
  86.                                                                                    final String name) {
  87.         return new ProfileThrustPropulsionModel(buildSegments(profile), isp, control3DVectorCostType, name);
  88.     }

  89.     /**
  90.      * Method building a map of generic segments from customized ones.
  91.      * @param timeSpanMap input profile
  92.      * @param <T> segment type
  93.      * @return generic profile
  94.      * @since 13.0
  95.      */
  96.     private static <T extends ThrustVectorProvider> TimeSpanMap<ThrustVectorProvider> buildSegments(final TimeSpanMap<T> timeSpanMap) {
  97.         TimeSpanMap.Span<T> span = timeSpanMap.getFirstSpan();
  98.         final TimeSpanMap<ThrustVectorProvider> segments = new TimeSpanMap<>(null);
  99.         while (span != null) {
  100.             segments.addValidBetween(span.getData(), span.getStart(), span.getEnd());
  101.             span = span.next();
  102.         }
  103.         return segments;
  104.     }

  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public String getName() {
  108.         return name;
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public Control3DVectorCostType getControl3DVectorCostType() {
  113.         return control3DVectorCostType;
  114.     }

  115.     /**
  116.      * Getter for active provider at input date.
  117.      * @param date date
  118.      * @return active segment
  119.      * @since 13.0
  120.      */
  121.     public ThrustVectorProvider getActiveProvider(final AbsoluteDate date) {
  122.         return profile.get(date);
  123.     }

  124.     /** {@inheritDoc} */
  125.     @Override
  126.     public Vector3D getThrustVector(final SpacecraftState s) {
  127.         return getThrustVector(s, getParameters());
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public double getFlowRate(final SpacecraftState s) {
  132.         return getFlowRate(s, getParameters());
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     public double getFlowRate(final SpacecraftState s, final double[] parameters) {
  137.         return -control3DVectorCostType.evaluate(getThrustVector(s, parameters)) / ThrustPropulsionModel.getExhaustVelocity(isp);
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public Vector3D getThrustVector(final SpacecraftState s, final double[] parameters) {
  142.         final ThrustVectorProvider active = getActiveProvider(s.getDate());
  143.         return active == null ? Vector3D.ZERO : active.getThrustVector(s.getDate(), s.getMass());
  144.     }

  145.     /** {@inheritDoc} */
  146.     public <T extends CalculusFieldElement<T>> FieldVector3D<T> getThrustVector(final FieldSpacecraftState<T> s,
  147.                                                                                 final T[] parameters) {
  148.         final ThrustVectorProvider active = getActiveProvider(s.getDate().toAbsoluteDate());
  149.         return active == null ? FieldVector3D.getZero(s.getDate().getField()) :
  150.                 active.getThrustVector(s.getDate(), s.getMass());
  151.     }

  152.     /** {@inheritDoc} */
  153.     public <T extends CalculusFieldElement<T>> T getFlowRate(final FieldSpacecraftState<T> s, final T[] parameters) {
  154.         return control3DVectorCostType.evaluate(getThrustVector(s, parameters)).divide(-ThrustPropulsionModel.getExhaustVelocity(isp));
  155.     }

  156.     /** {@inheritDoc}.
  157.      * <p>
  158.      * The single detector returned triggers {@link org.hipparchus.ode.events.Action#RESET_DERIVATIVES} events
  159.      * at every {@link ThrustVectorProvider} boundaries.
  160.      * </p>
  161.      */
  162.     @Override
  163.     public Stream<EventDetector> getEventDetectors() {

  164.         final List<AbsoluteDate> transitionDates = new ArrayList<>();
  165.         for (TimeSpanMap.Transition<ThrustVectorProvider> transition = profile.getFirstTransition();
  166.              transition != null;
  167.              transition = transition.next()) {
  168.             transitionDates.add(transition.getDate());
  169.         }
  170.         final DateDetector detector = getDateDetector(transitionDates.toArray(new TimeStamped[0]));
  171.         return Stream.of(detector);
  172.     }

  173.     /** {@inheritDoc}.
  174.      * <p>
  175.      * The single detector returned triggers {@link org.hipparchus.ode.events.Action#RESET_DERIVATIVES} events
  176.      * at every {@link ThrustVectorProvider} boundaries.
  177.      * </p>
  178.      */
  179.     @Override
  180.     public <T extends CalculusFieldElement<T>> Stream<FieldEventDetector<T>> getFieldEventDetectors(final Field<T> field) {
  181.         final List<AbsoluteDate> transitionDates = new ArrayList<>();
  182.         for (TimeSpanMap.Transition<ThrustVectorProvider> transition = profile.getFirstTransition();
  183.              transition != null;
  184.              transition = transition.next()) {
  185.             transitionDates.add(transition.getDate());
  186.         }
  187.         final FieldDateDetector<T> detector = getFieldDateDetector(field, transitionDates.toArray(new AbsoluteDate[0]));
  188.         return Stream.of(detector);
  189.     }

  190.     /** {@inheritDoc} */
  191.     @Override
  192.     public List<ParameterDriver> getParametersDrivers() {
  193.         return Collections.emptyList();
  194.     }
  195. }