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;
24
25 import org.hipparchus.util.FastMath;
26
27 /**
28 * This class is used in the junit tests for the ODE integrators.
29
30 * <p>This specific problem is the following differential equation :
31 * <pre>
32 * y1'' = -y1/r^3 y1 (0) = 1-e y1' (0) = 0
33 * y2'' = -y2/r^3 y2 (0) = 0 y2' (0) =sqrt((1+e)/(1-e))
34 * r = sqrt (y1^2 + y2^2), e = 0.9
35 * </pre>
36 * This is a two-body problem in the plane which can be solved by
37 * Kepler's equation
38 * <pre>
39 * y1 (t) = ...
40 * </pre>
41 * </p>
42
43 */
44 public class TestProblem3 extends TestProblemAbstract {
45
46 /** Eccentricity */
47 double e;
48
49 /**
50 * Simple constructor.
51 * @param e eccentricity
52 */
53 public TestProblem3(double e) {
54 super(0.0, new double[] { 1 - e, 0, 0, FastMath.sqrt((1+e)/(1-e)) }, 20.0,
55 new double[] { 1.0, 1.0, 1.0, 1.0 });
56 this.e = e;
57 }
58
59 /**
60 * Simple constructor.
61 */
62 public TestProblem3() {
63 this(0.1);
64 }
65
66 @Override
67 public double[] doComputeDerivatives(double t, double[] y) {
68
69 final double[] yDot = new double[getDimension()];
70
71 // current radius
72 double r2 = y[0] * y[0] + y[1] * y[1];
73 double invR3 = 1 / (r2 * FastMath.sqrt(r2));
74
75 // compute the derivatives
76 yDot[0] = y[2];
77 yDot[1] = y[3];
78 yDot[2] = -invR3 * y[0];
79 yDot[3] = -invR3 * y[1];
80
81 return yDot;
82
83 }
84
85 @Override
86 public double[] computeTheoreticalState(double t) {
87
88 // solve Kepler's equation
89 double E = t;
90 double d = 0;
91 double corr = 999.0;
92 for (int i = 0; (i < 50) && (FastMath.abs(corr) > 1.0e-12); ++i) {
93 double f2 = e * FastMath.sin(E);
94 double f0 = d - f2;
95 double f1 = 1 - e * FastMath.cos(E);
96 double f12 = f1 + f1;
97 corr = f0 * f12 / (f1 * f12 - f0 * f2);
98 d -= corr;
99 E = t + d;
100 }
101
102 double cosE = FastMath.cos(E);
103 double sinE = FastMath.sin(E);
104
105 double[] y = new double[getDimension()];
106 y[0] = cosE - e;
107 y[1] = FastMath.sqrt(1 - e * e) * sinE;
108 y[2] = -sinE / (1 - e * cosE);
109 y[3] = FastMath.sqrt(1 - e * e) * cosE / (1 - e * cosE);
110
111 return y;
112 }
113
114 }