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.migration.ode;
24  
25  import java.lang.reflect.Constructor;
26  import java.lang.reflect.InvocationTargetException;
27  import java.util.Random;
28  
29  import org.hipparchus.exception.MathIllegalArgumentException;
30  import org.hipparchus.exception.MathIllegalStateException;
31  import org.hipparchus.ode.EquationsMapper;
32  import org.hipparchus.ode.ExpandableODE;
33  import org.hipparchus.ode.ODEIntegrator;
34  import org.hipparchus.ode.ODEState;
35  import org.hipparchus.ode.ODEStateAndDerivative;
36  import org.hipparchus.ode.TestProblem3;
37  import org.hipparchus.ode.nonstiff.DormandPrince54Integrator;
38  import org.hipparchus.ode.nonstiff.DormandPrince853Integrator;
39  import org.hipparchus.ode.sampling.DummyStepInterpolator;
40  import org.hipparchus.ode.sampling.ODEStateInterpolator;
41  import org.hipparchus.util.FastMath;
42  import org.junit.After;
43  import org.junit.Assert;
44  import org.junit.Before;
45  import org.junit.Test;
46  
47  @Deprecated
48  public class ContinuousOutputModelTest {
49  
50      TestProblem3 pb;
51      ODEIntegrator integ;
52  
53      @Test
54      public void testBoundaries() throws MathIllegalArgumentException, MathIllegalStateException {
55          integ.addStepHandler(new ContinuousOutputModel());
56          integ.integrate(pb, pb.getInitialState(), pb.getFinalTime());
57          ContinuousOutputModel cm = (ContinuousOutputModel) integ.getStepHandlers().iterator().next();
58          cm.setInterpolatedTime(2.0 * pb.getInitialTime() - pb.getFinalTime());
59          cm.setInterpolatedTime(2.0 * pb.getFinalTime() - pb.getInitialTime());
60          cm.setInterpolatedTime(0.5 * (pb.getFinalTime() + pb.getInitialTime()));
61      }
62  
63      @Test
64      public void testRandomAccess() throws MathIllegalArgumentException, MathIllegalStateException {
65  
66          ContinuousOutputModel cm = new ContinuousOutputModel();
67          integ.addStepHandler(cm);
68          integ.integrate(pb, pb.getInitialState(), pb.getFinalTime());
69  
70          Random random = new Random(347588535632l);
71          double maxError    = 0.0;
72          double maxErrorDot = 0.0;
73          for (int i = 0; i < 1000; ++i) {
74              double r = random.nextDouble();
75              double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
76              cm.setInterpolatedTime(time);
77              double[] interpolatedY    = cm.getInterpolatedState();
78              double[] interpolatedYDot = cm.getInterpolatedDerivatives();
79              double[] theoreticalY     = pb.computeTheoreticalState(time);
80              double[] theoreticalYDot  = pb.doComputeDerivatives(time, theoreticalY);
81              double dx = interpolatedY[0] - theoreticalY[0];
82              double dy = interpolatedY[1] - theoreticalY[1];
83              double error = dx * dx + dy * dy;
84              maxError = FastMath.max(maxError, error);
85              double dxDot = interpolatedYDot[0] - theoreticalYDot[0];
86              double dyDot = interpolatedYDot[1] - theoreticalYDot[1];
87              double errorDot = dxDot * dxDot + dyDot * dyDot;
88              maxErrorDot = FastMath.max(maxErrorDot, errorDot);
89          }
90  
91          Assert.assertEquals(0.0, maxError,    1.0e-9);
92          Assert.assertEquals(0.0, maxErrorDot, 4.0e-7);
93  
94      }
95  
96      @Test
97      public void testModelsMerging() throws MathIllegalArgumentException, MathIllegalStateException {
98  
99          // theoretical solution: y[0] = cos(t), y[1] = sin(t)
100         FirstOrderDifferentialEquations problem =
101                         new FirstOrderDifferentialEquations() {
102             @Override
103             public void computeDerivatives(double t, double[] y, double[] dot) {
104                 dot[0] = -y[1];
105                 dot[1] =  y[0];
106             }
107             @Override
108             public int getDimension() {
109                 return 2;
110             }
111         };
112 
113         // integrate backward from &pi; to 0;
114         ContinuousOutputModel cm1 = new ContinuousOutputModel();
115         ODEIntegrator integ1 = new DormandPrince853Integrator(0, 1.0, 1.0e-8, 1.0e-8);
116         integ1.addStepHandler(cm1);
117         integ1.integrate(new ExpandableODE(problem),
118                          new ODEState(FastMath.PI, new double[] { -1.0, 0.0 }),
119                          0);
120 
121         // integrate backward from 2&pi; to &pi;
122         ContinuousOutputModel cm2 = new ContinuousOutputModel();
123         ODEIntegrator integ2 = new DormandPrince853Integrator(0, 0.1, 1.0e-12, 1.0e-12);
124         integ2.addStepHandler(cm2);
125         integ2.integrate(new ExpandableODE(problem),
126                          new ODEState(2.0 * FastMath.PI, new double[] { 1.0, 0.0 }),
127                          FastMath.PI);
128 
129         // merge the two half circles
130         ContinuousOutputModel cm = new ContinuousOutputModel();
131         cm.append(cm2);
132         cm.append(new ContinuousOutputModel());
133         cm.append(cm1);
134 
135         // check circle
136         Assert.assertEquals(2.0 * FastMath.PI, cm.getInitialTime(), 1.0e-12);
137         Assert.assertEquals(0, cm.getFinalTime(), 1.0e-12);
138         Assert.assertEquals(cm.getFinalTime(), cm.getInterpolatedTime(), 1.0e-12);
139         for (double t = 0; t < 2.0 * FastMath.PI; t += 0.1) {
140             cm.setInterpolatedTime(t);
141             double[] y = cm.getInterpolatedState();
142             Assert.assertEquals(FastMath.cos(t), y[0], 1.0e-7);
143             Assert.assertEquals(FastMath.sin(t), y[1], 1.0e-7);
144         }
145 
146     }
147 
148     @Test
149     public void testErrorConditions() throws MathIllegalArgumentException, MathIllegalStateException {
150 
151         ContinuousOutputModel cm = new ContinuousOutputModel();
152         cm.handleStep(buildInterpolator(0, new double[] { 0.0, 1.0, -2.0 }, 1));
153 
154         // dimension mismatch
155         Assert.assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0 }, 2.0));
156 
157         // hole between time ranges
158         Assert.assertTrue(checkAppendError(cm, 10.0, new double[] { 0.0, 1.0, -2.0 }, 20.0));
159 
160         // propagation direction mismatch
161         Assert.assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 0.0));
162 
163         // no errors
164         Assert.assertFalse(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 2.0));
165 
166     }
167 
168     private boolean checkAppendError(ContinuousOutputModel cm,
169                                      double t0, double[] y0, double t1)
170                                                      throws MathIllegalArgumentException, MathIllegalStateException {
171         try {
172             ContinuousOutputModel otherCm = new ContinuousOutputModel();
173             otherCm.handleStep(buildInterpolator(t0, y0, t1));
174             cm.append(otherCm);
175         } catch(MathIllegalArgumentException iae) {
176             return true; // there was an allowable error
177         }
178         return false; // no allowable error
179     }
180 
181     private ODEStateInterpolator buildInterpolator(double t0, double[] y0, double t1) {
182         EquationsMapper mapper = null;
183         try {
184             Constructor<EquationsMapper> ctr;
185             ctr = EquationsMapper.class.getDeclaredConstructor(EquationsMapper.class, Integer.TYPE);
186             ctr.setAccessible(true);
187             mapper = ctr.newInstance(null, y0.length);
188         } catch (NoSuchMethodException | SecurityException | InstantiationException |
189                  IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
190             Assert.fail(e.getLocalizedMessage());
191         }
192         return new DummyStepInterpolator(t1 >= t0,
193                                          new ODEStateAndDerivative(t0, y0,  new double[y0.length]),
194                                          new ODEStateAndDerivative(t1, y0,  new double[y0.length]),
195                                          new ODEStateAndDerivative(t0, y0,  new double[y0.length]),
196                                          new ODEStateAndDerivative(t1, y0,  new double[y0.length]),
197                                          mapper);
198     }
199 
200     public void checkValue(double value, double reference) {
201         Assert.assertTrue(FastMath.abs(value - reference) < 1.0e-10);
202     }
203 
204     @Before
205     public void setUp() {
206         pb = new TestProblem3(0.9);
207         double minStep = 0;
208         double maxStep = pb.getFinalTime() - pb.getInitialTime();
209         integ = new DormandPrince54Integrator(minStep, maxStep, 1.0e-8, 1.0e-8);
210     }
211 
212     @After
213     public void tearDown() {
214         pb    = null;
215         integ = null;
216     }
217 
218 }