View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.ode.nonstiff;
19  
20  import org.hipparchus.ode.EquationsMapper;
21  import org.hipparchus.ode.ODEStateAndDerivative;
22  import org.hipparchus.ode.sampling.AbstractODEStateInterpolator;
23  import org.hipparchus.util.FastMath;
24  
25  /**
26   * This class implements an interpolator for the Gragg-Bulirsch-Stoer
27   * integrator.
28   *
29   * <p>This interpolator compute dense output inside the last step
30   * produced by a Gragg-Bulirsch-Stoer integrator.</p>
31   *
32   * <p>
33   * This implementation is basically a reimplementation in Java of the
34   * <a
35   * href="http://www.unige.ch/math/folks/hairer/prog/nonstiff/odex.f">odex</a>
36   * fortran code by E. Hairer and G. Wanner. The redistribution policy
37   * for this code is available <a
38   * href="http://www.unige.ch/~hairer/prog/licence.txt">here</a>, for
39   * convenience, it is reproduced below.</p>
40   *
41   * <blockquote>
42   * <p>Copyright (c) 2004, Ernst Hairer</p>
43   *
44   * <p>Redistribution and use in source and binary forms, with or
45   * without modification, are permitted provided that the following
46   * conditions are met:</p>
47   * <ul>
48   *  <li>Redistributions of source code must retain the above copyright
49   *      notice, this list of conditions and the following disclaimer.</li>
50   *  <li>Redistributions in binary form must reproduce the above copyright
51   *      notice, this list of conditions and the following disclaimer in the
52   *      documentation and/or other materials provided with the distribution.</li>
53   * </ul>
54   *
55   * <p><strong>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
56   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
57   * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
58   * FOR A  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
59   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
60   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
61   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
62   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
63   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
64   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
65   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</strong></p>
66   * </blockquote>
67   *
68   * @see GraggBulirschStoerIntegrator
69   */
70  
71  class GraggBulirschStoerStateInterpolator
72      extends AbstractODEStateInterpolator {
73  
74      /** Serializable version identifier. */
75      private static final long serialVersionUID = 20160329L;
76  
77      /** Scaled derivatives at the middle of the step $\tau$.
78       * (element k is $h^{k} d^{k}y(\tau)/dt^{k}$ where h is step size...)
79       */
80      private final double[][] yMidDots;
81  
82      /** Interpolation polynomials. */
83      private final double[][] polynomials;
84  
85      /** Error coefficients for the interpolation. */
86      private final double[] errfac;
87  
88      /** Degree of the interpolation polynomials. */
89      private final int currentDegree;
90  
91      /** Simple constructor.
92       * @param forward integration direction indicator
93       * @param globalPreviousState start of the global step
94       * @param globalCurrentState end of the global step
95       * @param softPreviousState start of the restricted step
96       * @param softCurrentState end of the restricted step
97       * @param mapper equations mapper for the all equations
98       * @param yMidDots scaled derivatives at the middle of the step $\tau$
99       * (element k is $h^{k} d^{k}y(\tau)/dt^{k}$ where h is step size...)
100      * @param mu degree of the interpolation polynomial
101      */
102     GraggBulirschStoerStateInterpolator(final boolean forward,
103                                         final ODEStateAndDerivative globalPreviousState,
104                                         final ODEStateAndDerivative globalCurrentState,
105                                         final ODEStateAndDerivative softPreviousState,
106                                         final ODEStateAndDerivative softCurrentState,
107                                         final EquationsMapper mapper,
108                                         final double[][] yMidDots,
109                                         final int mu) {
110         super(forward,
111               globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
112               mapper);
113 
114         this.yMidDots      = yMidDots.clone();
115         this.currentDegree = mu + 4;
116         this.polynomials   = new double[currentDegree + 1][getCurrentState().getCompleteStateDimension()];
117 
118         // initialize the error factors array for interpolation
119         if (currentDegree <= 4) {
120             errfac = null;
121         } else {
122             errfac = new double[currentDegree - 4];
123             for (int i = 0; i < errfac.length; ++i) {
124                 final int ip5 = i + 5;
125                 errfac[i] = 1.0 / (ip5 * ip5);
126                 final double e = 0.5 * FastMath.sqrt (((double) (i + 1)) / ip5);
127                 for (int j = 0; j <= i; ++j) {
128                     errfac[i] *= e / (j + 1);
129                 }
130             }
131         }
132 
133         // compute the interpolation coefficients
134         computeCoefficients(mu);
135 
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     protected GraggBulirschStoerStateInterpolator create(final boolean newForward,
141                                                          final ODEStateAndDerivative newGlobalPreviousState,
142                                                          final ODEStateAndDerivative newGlobalCurrentState,
143                                                          final ODEStateAndDerivative newSoftPreviousState,
144                                                          final ODEStateAndDerivative newSoftCurrentState,
145                                                          final EquationsMapper newMapper) {
146         return new GraggBulirschStoerStateInterpolator(newForward,
147                                                        newGlobalPreviousState, newGlobalCurrentState,
148                                                        newSoftPreviousState, newSoftCurrentState,
149                                                        newMapper, yMidDots, currentDegree - 4);
150     }
151 
152     /** Compute the interpolation coefficients for dense output.
153      * @param mu degree of the interpolation polynomial
154      */
155     private void computeCoefficients(final int mu) {
156 
157         final double[] y0Dot = getGlobalPreviousState().getCompleteDerivative();
158         final double[] y1Dot = getGlobalCurrentState().getCompleteDerivative();
159         final double[] y1    = getGlobalCurrentState().getCompleteState();
160 
161         final double[] previousState = getGlobalPreviousState().getCompleteState();
162         final double h = getGlobalCurrentState().getTime() - getGlobalPreviousState().getTime();
163         for (int i = 0; i < previousState.length; ++i) {
164 
165             final double yp0   = h * y0Dot[i];
166             final double yp1   = h * y1Dot[i];
167             final double ydiff = y1[i] - previousState[i];
168             final double aspl  = ydiff - yp1;
169             final double bspl  = yp0 - ydiff;
170 
171             polynomials[0][i] = previousState[i];
172             polynomials[1][i] = ydiff;
173             polynomials[2][i] = aspl;
174             polynomials[3][i] = bspl;
175 
176             if (mu < 0) {
177                 return;
178             }
179 
180             // compute the remaining coefficients
181             final double ph0 = 0.5 * (previousState[i] + y1[i]) + 0.125 * (aspl + bspl);
182             polynomials[4][i] = 16 * (yMidDots[0][i] - ph0);
183 
184             if (mu > 0) {
185                 final double ph1 = ydiff + 0.25 * (aspl - bspl);
186                 polynomials[5][i] = 16 * (yMidDots[1][i] - ph1);
187 
188                 if (mu > 1) {
189                     final double ph2 = yp1 - yp0;
190                     polynomials[6][i] = 16 * (yMidDots[2][i] - ph2 + polynomials[4][i]);
191 
192                     if (mu > 2) {
193                         final double ph3 = 6 * (bspl - aspl);
194                         polynomials[7][i] = 16 * (yMidDots[3][i] - ph3 + 3 * polynomials[5][i]);
195 
196                         for (int j = 4; j <= mu; ++j) {
197                             final double fac1 = 0.5 * j * (j - 1);
198                             final double fac2 = 2 * fac1 * (j - 2) * (j - 3);
199                             polynomials[j+4][i] =
200                                             16 * (yMidDots[j][i] + fac1 * polynomials[j+2][i] - fac2 * polynomials[j][i]);
201                         }
202 
203                     }
204                 }
205             }
206         }
207 
208     }
209 
210     /** Estimate interpolation error.
211      * @param scale scaling array
212      * @return estimate of the interpolation error
213      */
214     public double estimateError(final double[] scale) {
215         double error = 0;
216         if (currentDegree >= 5) {
217             for (int i = 0; i < scale.length; ++i) {
218                 final double e = polynomials[currentDegree][i] / scale[i];
219                 error += e * e;
220             }
221             error = FastMath.sqrt(error / scale.length) * errfac[currentDegree - 5];
222         }
223         return error;
224     }
225 
226     /** {@inheritDoc} */
227     @Override
228     protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
229                                                                            final double time, final double theta,
230                                                                            final double thetaH, final double oneMinusThetaH) {
231 
232         final int dimension = mapper.getTotalDimension();
233 
234         final double h             = thetaH / theta;
235         final double oneMinusTheta = 1.0 - theta;
236         final double theta05       = theta - 0.5;
237         final double tOmT          = theta * oneMinusTheta;
238         final double t4            = tOmT * tOmT;
239         final double t4Dot         = 2 * tOmT * (1 - 2 * theta);
240         final double dot1          = 1.0 / h;
241         final double dot2          = theta * (2 - 3 * theta) / h;
242         final double dot3          = ((3 * theta - 4) * theta + 1) / h;
243 
244         final double[] interpolatedState       = new double[dimension];
245         final double[] interpolatedDerivatives = new double[dimension];
246         for (int i = 0; i < dimension; ++i) {
247 
248             final double p0 = polynomials[0][i];
249             final double p1 = polynomials[1][i];
250             final double p2 = polynomials[2][i];
251             final double p3 = polynomials[3][i];
252             interpolatedState[i] = p0 + theta * (p1 + oneMinusTheta * (p2 * theta + p3 * oneMinusTheta));
253             interpolatedDerivatives[i] = dot1 * p1 + dot2 * p2 + dot3 * p3;
254 
255             if (currentDegree > 3) {
256                 double cDot = 0;
257                 double c = polynomials[currentDegree][i];
258                 for (int j = currentDegree - 1; j > 3; --j) {
259                     final double d = 1.0 / (j - 3);
260                     cDot = d * (theta05 * cDot + c);
261                     c = polynomials[j][i] + c * d * theta05;
262                 }
263                 interpolatedState[i]       += t4 * c;
264                 interpolatedDerivatives[i] += (t4 * cDot + t4Dot * c) / h;
265             }
266 
267         }
268 
269         if (h == 0) {
270             // in this degenerated case, the previous computation leads to NaN for derivatives
271             // we fix this by using the derivatives at midpoint
272             System.arraycopy(yMidDots[1], 0, interpolatedDerivatives, 0, dimension);
273         }
274 
275         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);
276 
277     }
278 
279 }