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  
21  import org.hipparchus.ode.EquationsMapper;
22  import org.hipparchus.ode.ExpandableODE;
23  import org.hipparchus.ode.ODEStateAndDerivative;
24  
25  public abstract class RungeKuttaStateInterpolatorAbstractTest extends ODEStateInterpolatorAbstractTest {
26  
27      protected abstract RungeKuttaStateInterpolator
28          createInterpolator(boolean forward, double[][] yDotK,
29                             ODEStateAndDerivative globalPreviousState,
30                             ODEStateAndDerivative globalCurrentState,
31                             ODEStateAndDerivative softPreviousState,
32                             ODEStateAndDerivative softCurrentState,
33                             EquationsMapper mapper);
34  
35      protected abstract ButcherArrayProvider createButcherArrayProvider();
36  
37      protected RungeKuttaStateInterpolator setUpInterpolator(final ReferenceODE eqn,
38                                                              final double t0, final double[] y0,
39                                                              final double t1) {
40  
41          // get the Butcher arrays from the field integrator
42          ButcherArrayProvider provider = createButcherArrayProvider();
43          double[][] a = provider.getA();
44          double[]   b = provider.getB();
45          double[]   c = provider.getC();
46  
47          // store initial state
48          EquationsMapper mapper = new ExpandableODE(eqn).getMapper();
49          double[][] yDotK = new double[b.length][];
50          yDotK[0] = eqn.computeDerivatives(t0, y0);
51          ODEStateAndDerivative s0 = mapper.mapStateAndDerivative(t0, y0, yDotK[0]);
52  
53          // perform one integration step, in order to get consistent derivatives
54          double h = t1 - t0;
55          for (int k = 0; k < a.length; ++k) {
56              double[] y = y0.clone();
57              for (int i = 0; i < y0.length; ++i) {
58                  for (int s = 0; s <= k; ++s) {
59                      y[i] += h * a[k][s] * yDotK[s][i];
60                  }
61              }
62              yDotK[k + 1] = eqn.computeDerivatives(t0 + h * c[k], y);
63          }
64  
65          // store state at step end
66          double[] y = y0.clone();
67          for (int i = 0; i < y0.length; ++i) {
68              for (int s = 0; s < b.length; ++s) {
69                  y[i] += h * b[s] * yDotK[s][i];
70              }
71          }
72          ODEStateAndDerivative s1 = mapper.mapStateAndDerivative(t1, y, eqn.computeDerivatives(t1, y));
73  
74          return createInterpolator(t1 > t0, yDotK, s0, s1, s0, s1, mapper);
75  
76      }
77  
78  }