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.math.ode;
19
20 import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
21 import org.apache.commons.math.ode.events.EventHandler;
22
23 /**
24 * This class is used as the base class of the problems that are
25 * integrated during the junit tests for the ODE integrators.
26 */
27 public abstract class TestProblemAbstract
28 implements FirstOrderDifferentialEquations {
29
30 /** Serializable version identifier. */
31 private static final long serialVersionUID = -8521928974502839379L;
32
33 /** Dimension of the problem. */
34 protected int n;
35
36 /** Number of functions calls. */
37 protected int calls;
38
39 /** Initial time */
40 protected double t0;
41
42 /** Initial state */
43 protected double[] y0;
44
45 /** Final time */
46 protected double t1;
47
48 /** Error scale */
49 protected double[] errorScale;
50
51 /**
52 * Simple constructor.
53 */
54 protected TestProblemAbstract() {
55 n = 0;
56 calls = 0;
57 t0 = 0;
58 y0 = null;
59 t1 = 0;
60 errorScale = null;
61 }
62
63 /**
64 * Copy constructor.
65 * @param problem problem to copy
66 */
67 protected TestProblemAbstract(TestProblemAbstract problem) {
68 n = problem.n;
69 calls = problem.calls;
70 t0 = problem.t0;
71 if (problem.y0 == null) {
72 y0 = null;
73 } else {
74 y0 = problem.y0.clone();
75 }
76 if (problem.errorScale == null) {
77 errorScale = null;
78 } else {
79 errorScale = problem.errorScale.clone();
80 }
81 t1 = problem.t1;
82 }
83
84 /**
85 * Copy operation.
86 * @return a copy of the instance
87 */
88 public abstract TestProblemAbstract copy();
89
90 /**
91 * Set the initial conditions
92 * @param t0 initial time
93 * @param y0 initial state vector
94 */
95 protected void setInitialConditions(double t0, double[] y0) {
96 calls = 0;
97 n = y0.length;
98 this.t0 = t0;
99 this.y0 = y0.clone();
100 }
101
102 /**
103 * Set the final conditions.
104 * @param t1 final time
105 */
106 protected void setFinalConditions(double t1) {
107 this.t1 = t1;
108 }
109
110 /**
111 * Set the error scale
112 * @param errorScale error scale
113 */
114 protected void setErrorScale(double[] errorScale) {
115 this.errorScale = errorScale.clone();
116 }
117
118 public int getDimension() {
119 return n;
120 }
121
122 /**
123 * Get the initial time.
124 * @return initial time
125 */
126 public double getInitialTime() {
127 return t0;
128 }
129
130 /**
131 * Get the initial state vector.
132 * @return initial state vector
133 */
134 public double[] getInitialState() {
135 return y0;
136 }
137
138 /**
139 * Get the final time.
140 * @return final time
141 */
142 public double getFinalTime() {
143 return t1;
144 }
145
146 /**
147 * Get the error scale.
148 * @return error scale
149 */
150 public double[] getErrorScale() {
151 return errorScale;
152 }
153
154 /**
155 * Get the events handlers.
156 * @return events handlers */
157 public EventHandler[] getEventsHandlers() {
158 return new EventHandler[0];
159 }
160
161 /**
162 * Get the number of calls.
163 * @return nuber of calls
164 */
165 public int getCalls() {
166 return calls;
167 }
168
169 public void computeDerivatives(double t, double[] y, double[] yDot) {
170 ++calls;
171 doComputeDerivatives(t, y, yDot);
172 }
173
174 abstract public void doComputeDerivatives(double t, double[] y, double[] yDot);
175
176 /**
177 * Compute the theoretical state at the specified time.
178 * @param t time at which the state is required
179 * @return state vector at time t
180 */
181 abstract public double[] computeTheoreticalState(double t);
182
183 }