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