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.ode;
24  
25  import org.hipparchus.Field;
26  import org.hipparchus.CalculusFieldElement;
27  import org.hipparchus.util.MathArrays;
28  
29  /**
30   * This class is used in the junit tests for the ODE integrators.
31  
32   * <p>This specific problem is the following differential equation :
33   * <pre>
34   *    y1'' = -y1/r^3  y1 (0) = 1-e  y1' (0) = 0
35   *    y2'' = -y2/r^3  y2 (0) = 0    y2' (0) =sqrt((1+e)/(1-e))
36   *    r = sqrt (y1^2 + y2^2), e = 0.9
37   * </pre>
38   * This is a two-body problem in the plane which can be solved by
39   * Kepler's equation
40   * <pre>
41   *   y1 (t) = ...
42   * </pre>
43   * </p>
44  
45   * @param <T> the type of the field elements
46   */
47  public class TestFieldProblem3<T extends CalculusFieldElement<T>>
48  extends TestFieldProblemAbstract<T> {
49  
50      /** Eccentricity */
51      T e;
52  
53      /**
54       * Simple constructor.
55       * @param e eccentricity
56       */
57      public TestFieldProblem3(T e) {
58          super(convert(e.getField(), 0.0),
59                createY0(e),
60                convert(e.getField(), 20.0),
61                convert(e.getField(), 1.0, 1.0, 1.0, 1.0));
62          this.e = e;
63      }
64  
65      private static <T extends CalculusFieldElement<T>> T[] createY0(final T e) {
66          T[] y0 = MathArrays.buildArray(e.getField(), 4);
67          y0[0] = e.subtract(1).negate();
68          y0[1] = e.getField().getZero();
69          y0[2] = e.getField().getZero();
70          y0[3] = e.add(1).divide(y0[0]).sqrt();
71          return y0;
72      }
73  
74      /**
75       * Simple constructor.
76       * @param field field to which elements belong
77       */
78      public TestFieldProblem3(Field<T> field) {
79          this(field.getZero().add(0.1));
80      }
81  
82      @Override
83      public T[] doComputeDerivatives(T t, T[] y) {
84  
85          final T[] yDot = MathArrays.buildArray(getField(), getDimension());
86  
87          // current radius
88          T r2 = y[0].multiply(y[0]).add(y[1].multiply(y[1]));
89          T invR3 = r2.multiply(r2.sqrt()).reciprocal();
90  
91          // compute the derivatives
92          yDot[0] = y[2];
93          yDot[1] = y[3];
94          yDot[2] = invR3.negate().multiply(y[0]);
95          yDot[3] = invR3.negate().multiply(y[1]);
96  
97          return yDot;
98  
99      }
100 
101     @Override
102     public T[] computeTheoreticalState(T t) {
103 
104         final T[] y = MathArrays.buildArray(getField(), getDimension());
105 
106         // solve Kepler's equation
107         T E = t;
108         T d = convert(t.getField(), 0);
109         T corr = convert(t.getField(), 999.0);
110         for (int i = 0; (i < 50) && (corr.norm() > 1.0e-12); ++i) {
111             T f2  = e.multiply(E.sin());
112             T f0  = d.subtract(f2);
113             T f1  = e.multiply(E.cos()).subtract(1).negate();
114             T f12 = f1.add(f1);
115             corr  = f0.multiply(f12).divide(f1.multiply(f12).subtract(f0.multiply(f2)));
116             d = d.subtract(corr);
117             E = t.add(d);
118         }
119 
120         T cosE = E.cos();
121         T sinE = E.sin();
122 
123         y[0] = cosE.subtract(e);
124         y[1] = e.multiply(e).subtract(1).negate().sqrt().multiply(sinE);
125         y[2] = sinE.divide(e.multiply(cosE).subtract(1));
126         y[3] = e.multiply(e).subtract(1).negate().sqrt().multiply(cosE).divide(e.multiply(cosE).subtract(1).negate());
127 
128         return y;
129 
130     }
131 
132 }