View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.ode.sampling;
24  
25  import org.hipparchus.CalculusFieldElement;
26  import org.hipparchus.exception.MathIllegalStateException;
27  import org.hipparchus.ode.FieldEquationsMapper;
28  import org.hipparchus.ode.FieldODEStateAndDerivative;
29  import org.hipparchus.util.FastMath;
30  
31  /** This abstract class represents an interpolator over the last step
32   * during an ODE integration.
33   *
34   * <p>The various ODE integrators provide objects extending this class
35   * to the step handlers. The handlers can use these objects to
36   * retrieve the state vector at intermediate times between the
37   * previous and the current grid points (dense output).</p>
38   *
39   * @see org.hipparchus.ode.FieldODEIntegrator
40   * @see FieldODEStepHandler
41   *
42   * @param <T> the type of the field elements
43   */
44  
45  public abstract class AbstractFieldODEStateInterpolator<T extends CalculusFieldElement<T>>
46      implements FieldODEStateInterpolator<T> {
47  
48      /** Global previous state. */
49      private final FieldODEStateAndDerivative<T> globalPreviousState;
50  
51      /** Global current state. */
52      private final FieldODEStateAndDerivative<T> globalCurrentState;
53  
54      /** Soft previous state. */
55      private final FieldODEStateAndDerivative<T> softPreviousState;
56  
57      /** Soft current state. */
58      private final FieldODEStateAndDerivative<T> softCurrentState;
59  
60      /** integration direction. */
61      private final boolean forward;
62  
63      /** Mapper for ODE equations primary and secondary components. */
64      private FieldEquationsMapper<T> mapper;
65  
66      /** Simple constructor.
67       * @param isForward integration direction indicator
68       * @param globalPreviousState start of the global step
69       * @param globalCurrentState end of the global step
70       * @param softPreviousState start of the restricted step
71       * @param softCurrentState end of the restricted step
72       * @param equationsMapper mapper for ODE equations primary and secondary components
73       */
74      protected AbstractFieldODEStateInterpolator(final boolean isForward,
75                                                  final FieldODEStateAndDerivative<T> globalPreviousState,
76                                                  final FieldODEStateAndDerivative<T> globalCurrentState,
77                                                  final FieldODEStateAndDerivative<T> softPreviousState,
78                                                  final FieldODEStateAndDerivative<T> softCurrentState,
79                                                  final FieldEquationsMapper<T> equationsMapper) {
80          this.forward             = isForward;
81          this.globalPreviousState = globalPreviousState;
82          this.globalCurrentState  = globalCurrentState;
83          this.softPreviousState   = softPreviousState;
84          this.softCurrentState    = softCurrentState;
85          this.mapper              = equationsMapper;
86      }
87  
88      /** Create a new restricted version of the instance.
89       * <p>
90       * The instance is not changed at all.
91       * </p>
92       * @param previousState start of the restricted step
93       * @param currentState end of the restricted step
94       * @return restricted version of the instance
95       * @see #getPreviousState()
96       * @see #getCurrentState()
97       */
98      public AbstractFieldODEStateInterpolator<T> restrictStep(final FieldODEStateAndDerivative<T> previousState,
99                                                               final FieldODEStateAndDerivative<T> currentState) {
100         return create(forward, globalPreviousState, globalCurrentState, previousState, currentState, mapper);
101     }
102 
103     /** Create a new instance.
104      * @param newForward integration direction indicator
105      * @param newGlobalPreviousState start of the global step
106      * @param newGlobalCurrentState end of the global step
107      * @param newSoftPreviousState start of the restricted step
108      * @param newSoftCurrentState end of the restricted step
109      * @param newMapper equations mapper for the all equations
110      * @return a new instance
111      */
112     protected abstract AbstractFieldODEStateInterpolator<T> create(boolean newForward,
113                                                                    FieldODEStateAndDerivative<T> newGlobalPreviousState,
114                                                                    FieldODEStateAndDerivative<T> newGlobalCurrentState,
115                                                                    FieldODEStateAndDerivative<T> newSoftPreviousState,
116                                                                    FieldODEStateAndDerivative<T> newSoftCurrentState,
117                                                                    FieldEquationsMapper<T> newMapper);
118 
119     /**
120      * Get the previous global grid point state.
121      * @return previous global grid point state
122      */
123     public FieldODEStateAndDerivative<T> getGlobalPreviousState() {
124         return globalPreviousState;
125     }
126 
127     /**
128      * Get the current global grid point state.
129      * @return current global grid point state
130      */
131     public FieldODEStateAndDerivative<T> getGlobalCurrentState() {
132         return globalCurrentState;
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     public FieldODEStateAndDerivative<T> getPreviousState() {
138         return softPreviousState;
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public boolean isPreviousStateInterpolated() {
144         return softPreviousState != globalPreviousState;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public FieldODEStateAndDerivative<T> getCurrentState() {
150         return softCurrentState;
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public boolean isCurrentStateInterpolated() {
156         return softCurrentState != globalCurrentState;
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public FieldODEStateAndDerivative<T> getInterpolatedState(final T time) {
162         if (FastMath.abs(globalCurrentState.getTime().subtract(globalPreviousState.getTime()).getReal()) <=
163             FastMath.ulp(globalCurrentState.getTime().getReal())) {
164             return globalCurrentState;
165         }
166         final T thetaH         = time.subtract(globalPreviousState.getTime());
167         final T oneMinusThetaH = globalCurrentState.getTime().subtract(time);
168         final T theta          = thetaH.divide(globalCurrentState.getTime().subtract(globalPreviousState.getTime()));
169         return computeInterpolatedStateAndDerivatives(mapper, time, theta, thetaH, oneMinusThetaH);
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public boolean isForward() {
175         return forward;
176     }
177 
178     /** Get the mapper for ODE equations primary and secondary components.
179      * @return mapper for ODE equations primary and secondary components
180      */
181     protected FieldEquationsMapper<T> getMapper() {
182         return mapper;
183     }
184 
185     /** Compute the state and derivatives at the interpolated time.
186      * This is the main processing method that should be implemented by
187      * the derived classes to perform the interpolation.
188      * @param equationsMapper mapper for ODE equations primary and secondary components
189      * @param time interpolation time
190      * @param theta normalized interpolation abscissa within the step
191      * (theta is zero at the previous time step and one at the current time step)
192      * @param thetaH time gap between the previous time and the interpolated time
193      * @param oneMinusThetaH time gap between the interpolated time and
194      * the current time
195      * @return interpolated state and derivatives
196      * @exception MathIllegalStateException if the number of functions evaluations is exceeded
197      */
198     protected abstract FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(FieldEquationsMapper<T> equationsMapper,
199                                                                                             T time, T theta,
200                                                                                             T thetaH, T oneMinusThetaH)
201         throws MathIllegalStateException;
202 
203 }