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 * http://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.apache.commons.math4.legacy.ode;
19
20 import java.lang.reflect.Array;
21
22 import org.apache.commons.math4.legacy.core.Field;
23 import org.apache.commons.math4.legacy.core.RealFieldElement;
24 import org.apache.commons.math4.legacy.ode.events.FieldEventHandler;
25 import org.apache.commons.math4.legacy.core.MathArrays;
26
27 /**
28 * This class is used as the base class of the problems that are
29 * integrated during the junit tests for the ODE integrators.
30 * @param <T> the type of the field elements
31 */
32 public abstract class TestFieldProblemAbstract<T extends RealFieldElement<T>>
33 implements FirstOrderFieldDifferentialEquations<T> {
34
35 /** Field to which elements belong. */
36 private Field<T> field;
37
38 /** Dimension of the problem. */
39 private int n;
40
41 /** Number of functions calls. */
42 private int calls;
43
44 /** Initial time */
45 private T t0;
46
47 /** Initial state */
48 private T[] y0;
49
50 /** Final time */
51 private T t1;
52
53 /** Error scale */
54 private T[] errorScale;
55
56 /**
57 * Simple constructor.
58 * @param field field to which elements belong
59 */
60 protected TestFieldProblemAbstract(Field<T> field) {
61 this.field = field;
62 n = 0;
63 calls = 0;
64 t0 = field.getZero();
65 y0 = null;
66 t1 = field.getZero();
67 errorScale = null;
68 }
69
70 /**
71 * Set the initial conditions
72 * @param t0 initial time
73 * @param y0 initial state vector
74 */
75 protected void setInitialConditions(T t0, T[] y0) {
76 calls = 0;
77 n = y0.length;
78 this.t0 = t0;
79 this.y0 = y0.clone();
80 }
81
82 /**
83 * Set the final conditions.
84 * @param t1 final time
85 */
86 protected void setFinalConditions(T t1) {
87 this.t1 = t1;
88 }
89
90 /**
91 * Set the error scale
92 * @param errorScale error scale
93 */
94 protected void setErrorScale(T[] errorScale) {
95 this.errorScale = errorScale.clone();
96 }
97
98 /** get the filed to which elements belong.
99 * @return field to which elements belong
100 */
101 public Field<T> getField() {
102 return field;
103 }
104
105 /** Get the problem dimension.
106 * @return problem dimension
107 */
108 @Override
109 public int getDimension() {
110 return n;
111 }
112
113 /**
114 * Get the initial state.
115 * @return initial state
116 */
117 public FieldODEState<T> getInitialState() {
118 return new FieldODEState<>(t0, y0);
119 }
120
121 /**
122 * Get the final time.
123 * @return final time
124 */
125 public T getFinalTime() {
126 return t1;
127 }
128
129 /**
130 * Get the error scale.
131 * @return error scale
132 */
133 public T[] getErrorScale() {
134 return errorScale;
135 }
136
137 /**
138 * Get the events handlers.
139 * @return events handlers */
140 public FieldEventHandler<T>[] getEventsHandlers() {
141 @SuppressWarnings("unchecked")
142 final FieldEventHandler<T>[] empty =
143 (FieldEventHandler<T>[]) Array.newInstance(FieldEventHandler.class, 0);
144 return empty;
145 }
146
147 /**
148 * Get the theoretical events times.
149 * @return theoretical events times
150 */
151 public T[] getTheoreticalEventsTimes() {
152 return MathArrays.buildArray(field, 0);
153 }
154
155 /**
156 * Get the number of calls.
157 * @return number of calls
158 */
159 public int getCalls() {
160 return calls;
161 }
162
163 /** {@inheritDoc} */
164 @Override
165 public void init(T t0, T[] y0, T t) {
166 }
167
168 /** {@inheritDoc} */
169 @Override
170 public T[] computeDerivatives(T t, T[] y) {
171 ++calls;
172 return doComputeDerivatives(t, y);
173 }
174
175 public abstract T[] doComputeDerivatives(T t, T[] y);
176
177 /**
178 * Compute the theoretical state at the specified time.
179 * @param t time at which the state is required
180 * @return state vector at time t
181 */
182 public abstract T[] computeTheoreticalState(T t);
183
184 /** Convert a double.
185 * @param d double to convert
186 * @return converted double
187 */
188 protected T convert(double d) {
189 return field.getZero().add(d);
190 }
191
192 /** Convert a one dimension array.
193 * @param elements array elements
194 * @return converted array
195 */
196 protected T[] convert(double ... elements) {
197 T[] array = MathArrays.buildArray(field, elements.length);
198 for (int i = 0; i < elements.length; ++i) {
199 array[i] = convert(elements[i]);
200 }
201 return array;
202 }
203 }