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 java.lang.reflect.Array;
26  
27  import org.hipparchus.CalculusFieldElement;
28  import org.hipparchus.Field;
29  import org.hipparchus.ode.events.FieldODEEventDetector;
30  import org.hipparchus.util.MathArrays;
31  
32  /**
33   * This class is used as the base class of the problems that are
34   * integrated during the junit tests for the ODE integrators.
35   * @param <T> the type of the field elements
36   */
37  public abstract class TestFieldProblemAbstract<T extends CalculusFieldElement<T>>
38      implements FieldOrdinaryDifferentialEquation<T> {
39  
40      /** Number of functions calls. */
41      private int calls;
42  
43      /** Initial state */
44      private final FieldODEState<T> s0;
45  
46      /** Final time */
47      private final T t1;
48  
49      /** Error scale */
50      private final T[] errorScale;
51  
52      /**
53       * Simple constructor.
54       * @param t0 initial time
55       * @param y0 initial state
56       * @param t1 final time
57       * @param errorScale error scale
58       */
59      protected TestFieldProblemAbstract(T t0, T[] y0, T t1, T[] errorScale) {
60          calls      = 0;
61          s0         = new FieldODEState<T>(t0, y0);
62          this.t1    = t1;
63          this.errorScale = errorScale.clone();
64      }
65  
66      /** get the filed to which elements belong.
67       * @return field to which elements belong
68       */
69      public Field<T> getField() {
70          return s0.getTime().getField();
71      }
72  
73      /** Get the problem dimension.
74       * @return problem dimension
75       */
76      public int getDimension() {
77          return s0.getPrimaryStateDimension();
78      }
79  
80      /**
81       * Get the initial time.
82       * @return initial time
83       */
84      public T getInitialTime() {
85          return s0.getTime();
86      }
87  
88     /**
89       * Get the initial state.
90       * @return initial state
91       */
92      public FieldODEState<T> getInitialState() {
93          return s0;
94      }
95  
96      /**
97       * Get the final time.
98       * @return final time
99       */
100     public T getFinalTime() {
101         return t1;
102     }
103 
104     /**
105      * Get the error scale.
106      * @return error scale
107      */
108     public T[] getErrorScale() {
109         return errorScale;
110     }
111 
112     /**
113      * Get the event detectors.
114      * @param maxCheck maximum checking interval, must be strictly positive
115      * @param threshold convergence threshold (s)
116      * @param maxIter maximum number of iterations in the event time search
117      * @return events detectors   */
118     public FieldODEEventDetector<T>[] getEventDetectors(final double maxCheck, final T threshold, final int maxIter) {
119         @SuppressWarnings("unchecked")
120         final FieldODEEventDetector<T>[] empty =
121                         (FieldODEEventDetector<T>[]) Array.newInstance(FieldODEEventDetector.class, 0);
122         return empty;
123     }
124 
125     /**
126      * Get the theoretical events times.
127      * @return theoretical events times
128      */
129     public T[] getTheoreticalEventsTimes() {
130         return MathArrays.buildArray(s0.getTime().getField(), 0);
131     }
132 
133     /**
134      * Get the number of calls.
135      * @return nuber of calls
136      */
137     public int getCalls() {
138         return calls;
139     }
140 
141     /** {@inheritDoc} */
142     public void init(T t0, T[] y0, T t) {
143     }
144 
145     /** {@inheritDoc} */
146     public T[] computeDerivatives(T t, T[] y) {
147         ++calls;
148         return doComputeDerivatives(t, y);
149     }
150 
151     abstract public T[] doComputeDerivatives(T t, T[] y);
152 
153     /**
154      * Compute the theoretical state at the specified time.
155      * @param t time at which the state is required
156      * @return state vector at time t
157      */
158     abstract public T[] computeTheoreticalState(T t);
159 
160     /** Convert a double.
161      * @param d double to convert
162      * @return converted double
163      */
164     protected static <T extends CalculusFieldElement<T>> T convert(Field<T> field, double d) {
165         return field.getZero().add(d);
166     }
167 
168     /** Convert a one dimension array.
169      * @param elements array elements
170      * @return converted array
171      */
172     protected static <T extends CalculusFieldElement<T>> T[] convert(Field<T> field, double ... elements) {
173         T[] array = MathArrays.buildArray(field, elements.length);
174         for (int i = 0; i < elements.length; ++i) {
175             array[i] = convert(field, elements[i]);
176         }
177         return array;
178     }
179 
180 }