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