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