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