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 org.hipparchus.ode.sampling.ODEStateInterpolator;
26  import org.hipparchus.ode.sampling.ODEStepHandler;
27  import org.hipparchus.util.FastMath;
28  
29  /**
30   * This class is used to handle steps for the test problems
31   * integrated during the junit tests for the ODE integrators.
32   */
33  public class TestProblemHandler implements ODEStepHandler {
34  
35      /** Associated problem. */
36      private TestProblemAbstract problem;
37  
38      /** Maximal errors encountered during the integration. */
39      private double maxValueError;
40      private double maxTimeError;
41  
42      /** Error at the end of the integration. */
43      private double lastError;
44  
45      /** Time at the end of integration. */
46      private double lastTime;
47  
48      /** ODE solver used. */
49      private ODEIntegrator integrator;
50  
51      /** Expected start for step. */
52      private double expectedStepStart;
53  
54      /**
55       * Simple constructor.
56       * @param problem problem for which steps should be handled
57       * @param integrator ODE solver used
58       */
59      public TestProblemHandler(TestProblemAbstract problem, ODEIntegrator integrator) {
60          this.problem = problem;
61          this.integrator = integrator;
62          maxValueError = 0;
63          maxTimeError  = 0;
64          lastError     = 0;
65          expectedStepStart = Double.NaN;
66      }
67  
68      public void init(ODEStateAndDerivative s0, double t) {
69          maxValueError = 0;
70          maxTimeError  = 0;
71          lastError     = 0;
72          expectedStepStart = Double.NaN;
73      }
74  
75      public void handleStep(ODEStateInterpolator interpolator) {
76  
77          double start = interpolator.getPreviousState().getTime();
78          if (FastMath.abs((start - problem.getInitialTime()) / integrator.getCurrentSignedStepsize()) > 0.001) {
79              // multistep integrators do not handle the first steps themselves
80              // so we have to make sure the integrator we look at has really started its work
81              if (!Double.isNaN(expectedStepStart)) {
82                  // the step should either start at the end of the integrator step
83                  // or at an event if the step is split into several substeps
84                  double stepError = FastMath.max(maxTimeError, FastMath.abs(start - expectedStepStart));
85                  for (double eventTime : problem.getTheoreticalEventsTimes()) {
86                      stepError = FastMath.min(stepError, FastMath.abs(start - eventTime));
87                  }
88                  maxTimeError = FastMath.max(maxTimeError, stepError);
89              }
90              expectedStepStart = interpolator.getCurrentState().getTime();
91          }
92  
93  
94          double pT = interpolator.getPreviousState().getTime();
95          double cT = interpolator.getCurrentState().getTime();
96          double[] errorScale = problem.getErrorScale();
97  
98          // walk through the step
99          for (int k = 0; k <= 20; ++k) {
100 
101             double time = pT + (k * (cT - pT)) / 20;
102             ODEStateAndDerivative interpolated = interpolator.getInterpolatedState(time);
103             double[] interpolatedY = interpolated.getPrimaryState();
104             double[] theoreticalY  = problem.computeTheoreticalState(interpolated.getTime());
105 
106             // update the errors
107             for (int i = 0; i < interpolatedY.length; ++i) {
108                 double error = errorScale[i] * FastMath.abs(interpolatedY[i] - theoreticalY[i]);
109                 maxValueError = FastMath.max(error, maxValueError);
110             }
111         }
112 
113     }
114 
115     public void finish(ODEStateAndDerivative finalState) {
116         double[] theoreticalY  = problem.computeTheoreticalState(finalState.getTime());
117         for (int i = 0; i < finalState.getCompleteState().length; ++i) {
118             double error = FastMath.abs(finalState.getCompleteState()[i] - theoreticalY[i]);
119             lastError = FastMath.max(error, lastError);
120         }
121         lastTime = finalState.getTime();
122     }
123 
124     /**
125      * Get the maximal value error encountered during integration.
126      * @return maximal value error
127      */
128     public double getMaximalValueError() {
129         return maxValueError;
130     }
131 
132     /**
133      * Get the maximal time error encountered during integration.
134      * @return maximal time error
135      */
136     public double getMaximalTimeError() {
137         return maxTimeError;
138     }
139 
140 
141     public int getCalls() {
142         return problem.getCalls();
143     }
144 
145     /**
146      * Get the error at the end of the integration.
147      * @return error at the end of the integration
148      */
149     public double getLastError() {
150         return lastError;
151     }
152 
153     /**
154      * Get the time at the end of the integration.
155      * @return time at the end of the integration.
156      */
157     public double getLastTime() {
158         return lastTime;
159     }
160 
161 }