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.sampling;
24
25 import org.hipparchus.exception.MathIllegalStateException;
26 import org.hipparchus.ode.EquationsMapper;
27 import org.hipparchus.ode.ODEStateAndDerivative;
28 import org.hipparchus.util.FastMath;
29
30 /** This abstract class represents an interpolator over the last step
31 * during an ODE integration.
32 *
33 * <p>The various ODE integrators provide objects extending this class
34 * to the step handlers. The handlers can use these objects to
35 * retrieve the state vector at intermediate times between the
36 * previous and the current grid points (dense output).</p>
37 *
38 * @see org.hipparchus.ode.ODEIntegrator
39 * @see ODEStepHandler
40 */
41
42 public abstract class AbstractODEStateInterpolator
43 implements ODEStateInterpolator {
44
45 /** Serializable UID. */
46 private static final long serialVersionUID = 20160328L;
47
48 /** Global previous state. */
49 private final ODEStateAndDerivative globalPreviousState;
50
51 /** Global current state. */
52 private final ODEStateAndDerivative globalCurrentState;
53
54 /** Soft previous state. */
55 private final ODEStateAndDerivative softPreviousState;
56
57 /** Soft current state. */
58 private final ODEStateAndDerivative softCurrentState;
59
60 /** integration direction. */
61 private final boolean forward;
62
63 /** Mapper for ODE equations primary and secondary components. */
64 private final EquationsMapper mapper;
65
66 /** Simple constructor.
67 * @param isForward integration direction indicator
68 * @param globalPreviousState start of the global step
69 * @param globalCurrentState end of the global step
70 * @param softPreviousState start of the restricted step
71 * @param softCurrentState end of the restricted step
72 * @param equationsMapper mapper for ODE equations primary and secondary components
73 */
74 protected AbstractODEStateInterpolator(final boolean isForward,
75 final ODEStateAndDerivative globalPreviousState,
76 final ODEStateAndDerivative globalCurrentState,
77 final ODEStateAndDerivative softPreviousState,
78 final ODEStateAndDerivative softCurrentState,
79 final EquationsMapper equationsMapper) {
80 this.forward = isForward;
81 this.globalPreviousState = globalPreviousState;
82 this.globalCurrentState = globalCurrentState;
83 this.softPreviousState = softPreviousState;
84 this.softCurrentState = softCurrentState;
85 this.mapper = equationsMapper;
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 public AbstractODEStateInterpolator restrictStep(final ODEStateAndDerivative previousState,
91 final ODEStateAndDerivative currentState) {
92 return create(forward, globalPreviousState, globalCurrentState, previousState, currentState, mapper);
93 }
94
95 /** Create a new instance.
96 * @param newForward integration direction indicator
97 * @param newGlobalPreviousState start of the global step
98 * @param newGlobalCurrentState end of the global step
99 * @param newSoftPreviousState start of the restricted step
100 * @param newSoftCurrentState end of the restricted step
101 * @param newMapper equations mapper for the all equations
102 * @return a new instance
103 */
104 protected abstract AbstractODEStateInterpolator create(boolean newForward,
105 ODEStateAndDerivative newGlobalPreviousState,
106 ODEStateAndDerivative newGlobalCurrentState,
107 ODEStateAndDerivative newSoftPreviousState,
108 ODEStateAndDerivative newSoftCurrentState,
109 EquationsMapper newMapper);
110
111 /**
112 * Get the previous global grid point state.
113 * @return previous global grid point state
114 */
115 public ODEStateAndDerivative getGlobalPreviousState() {
116 return globalPreviousState;
117 }
118
119 /**
120 * Get the current global grid point state.
121 * @return current global grid point state
122 */
123 public ODEStateAndDerivative getGlobalCurrentState() {
124 return globalCurrentState;
125 }
126
127 /** {@inheritDoc} */
128 @Override
129 public ODEStateAndDerivative getPreviousState() {
130 return softPreviousState;
131 }
132
133 /** {@inheritDoc} */
134 @Override
135 public boolean isPreviousStateInterpolated() {
136 return softPreviousState != globalPreviousState;
137 }
138
139 /** {@inheritDoc} */
140 @Override
141 public ODEStateAndDerivative getCurrentState() {
142 return softCurrentState;
143 }
144
145 /** {@inheritDoc} */
146 @Override
147 public boolean isCurrentStateInterpolated() {
148 return softCurrentState != globalCurrentState;
149 }
150
151 /** {@inheritDoc} */
152 @Override
153 public ODEStateAndDerivative getInterpolatedState(final double time) {
154 if (FastMath.abs(globalCurrentState.getTime() - globalPreviousState.getTime()) <=
155 FastMath.ulp(globalCurrentState.getTime())) {
156 return globalCurrentState;
157 }
158 final double thetaH = time - globalPreviousState.getTime();
159 final double oneMinusThetaH = globalCurrentState.getTime() - time;
160 final double theta = thetaH / (globalCurrentState.getTime() - globalPreviousState.getTime());
161 return computeInterpolatedStateAndDerivatives(mapper, time, theta, thetaH, oneMinusThetaH);
162 }
163
164 /** {@inheritDoc} */
165 @Override
166 public boolean isForward() {
167 return forward;
168 }
169
170 /** Get the mapper for ODE equations primary and secondary components.
171 * @return mapper for ODE equations primary and secondary components
172 */
173 protected EquationsMapper getMapper() {
174 return mapper;
175 }
176
177 /** Compute the state and derivatives at the interpolated time.
178 * This is the main processing method that should be implemented by
179 * the derived classes to perform the interpolation.
180 * @param equationsMapper mapper for ODE equations primary and secondary components
181 * @param time interpolation time
182 * @param theta normalized interpolation abscissa within the step
183 * (theta is zero at the previous time step and one at the current time step)
184 * @param thetaH time gap between the previous time and the interpolated time
185 * @param oneMinusThetaH time gap between the interpolated time and
186 * the current time
187 * @return interpolated state and derivatives
188 * @exception MathIllegalStateException if the number of functions evaluations is exceeded
189 */
190 protected abstract ODEStateAndDerivative computeInterpolatedStateAndDerivatives(EquationsMapper equationsMapper,
191 double time, double theta,
192 double thetaH, double oneMinusThetaH)
193 throws MathIllegalStateException;
194
195 }