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 java.util.Collection;
21
22 import org.apache.commons.math4.legacy.core.RealFieldElement;
23 import org.apache.commons.math4.legacy.analysis.solvers.BracketedRealFieldUnivariateSolver;
24 import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
25 import org.apache.commons.math4.legacy.exception.NoBracketingException;
26 import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
27 import org.apache.commons.math4.legacy.ode.events.FieldEventHandler;
28 import org.apache.commons.math4.legacy.ode.sampling.FieldStepHandler;
29
30 /** This interface represents a first order integrator for
31 * differential equations.
32
33 * <p>The classes which are devoted to solve first order differential
34 * equations should implement this interface. The problems which can
35 * be handled should implement the {@link
36 * FirstOrderDifferentialEquations} interface.</p>
37 *
38 * @see FirstOrderFieldDifferentialEquations
39 * @param <T> the type of the field elements
40 * @since 3.6
41 */
42
43 public interface FirstOrderFieldIntegrator<T extends RealFieldElement<T>> {
44
45 /** Get the name of the method.
46 * @return name of the method
47 */
48 String getName();
49
50 /** Add a step handler to this integrator.
51 * <p>The handler will be called by the integrator for each accepted
52 * step.</p>
53 * @param handler handler for the accepted steps
54 * @see #getStepHandlers()
55 * @see #clearStepHandlers()
56 */
57 void addStepHandler(FieldStepHandler<T> handler);
58
59 /** Get all the step handlers that have been added to the integrator.
60 * @return an unmodifiable collection of the added events handlers
61 * @see #addStepHandler(FieldStepHandler)
62 * @see #clearStepHandlers()
63 */
64 Collection<FieldStepHandler<T>> getStepHandlers();
65
66 /** Remove all the step handlers that have been added to the integrator.
67 * @see #addStepHandler(FieldStepHandler)
68 * @see #getStepHandlers()
69 */
70 void clearStepHandlers();
71
72 /** Add an event handler to the integrator.
73 * <p>
74 * The default solver is a 5<sup>th</sup> order {@link
75 * org.apache.commons.math4.legacy.analysis.solvers.FieldBracketingNthOrderBrentSolver}.
76 * </p>
77 * @param handler event handler
78 * @param maxCheckInterval maximal time interval between switching
79 * function checks (this interval prevents missing sign changes in
80 * case the integration steps becomes very large)
81 * @param convergence convergence threshold in the event time search
82 * @param maxIterationCount upper limit of the iteration count in
83 * the event time search events.
84 * @see #addEventHandler(FieldEventHandler, double, double, int,
85 * org.apache.commons.math4.legacy.analysis.solvers.BracketedRealFieldUnivariateSolver)
86 * @see #getEventHandlers()
87 * @see #clearEventHandlers()
88 */
89 void addEventHandler(FieldEventHandler<T> handler, double maxCheckInterval,
90 double convergence, int maxIterationCount);
91
92 /** Add an event handler to the integrator.
93 * @param handler event handler
94 * @param maxCheckInterval maximal time interval between switching
95 * function checks (this interval prevents missing sign changes in
96 * case the integration steps becomes very large)
97 * @param convergence convergence threshold in the event time search
98 * @param maxIterationCount upper limit of the iteration count in
99 * the event time search events.
100 * @param solver solver to use to locate the event
101 * @see #addEventHandler(FieldEventHandler, double, double, int)
102 * @see #getEventHandlers()
103 * @see #clearEventHandlers()
104 */
105 void addEventHandler(FieldEventHandler<T> handler, double maxCheckInterval,
106 double convergence, int maxIterationCount,
107 BracketedRealFieldUnivariateSolver<T> solver);
108
109 /** Get all the event handlers that have been added to the integrator.
110 * @return an unmodifiable collection of the added events handlers
111 * @see #addEventHandler(FieldEventHandler, double, double, int)
112 * @see #clearEventHandlers()
113 */
114 Collection<FieldEventHandler<T>> getEventHandlers();
115
116 /** Remove all the event handlers that have been added to the integrator.
117 * @see #addEventHandler(FieldEventHandler, double, double, int)
118 * @see #getEventHandlers()
119 */
120 void clearEventHandlers();
121
122 /** Get the current value of the step start time t<sub>i</sub>.
123 * <p>This method can be called during integration (typically by
124 * the object implementing the {@link FirstOrderDifferentialEquations
125 * differential equations} problem) if the value of the current step that
126 * is attempted is needed.</p>
127 * <p>The result is undefined if the method is called outside of
128 * calls to <code>integrate</code>.</p>
129 * @return current value of the state at step start time t<sub>i</sub>
130 */
131 FieldODEStateAndDerivative<T> getCurrentStepStart();
132
133 /** Get the current signed value of the integration stepsize.
134 * <p>This method can be called during integration (typically by
135 * the object implementing the {@link FirstOrderDifferentialEquations
136 * differential equations} problem) if the signed value of the current stepsize
137 * that is tried is needed.</p>
138 * <p>The result is undefined if the method is called outside of
139 * calls to <code>integrate</code>.</p>
140 * @return current signed value of the stepsize
141 */
142 T getCurrentSignedStepsize();
143
144 /** Set the maximal number of differential equations function evaluations.
145 * <p>The purpose of this method is to avoid infinite loops which can occur
146 * for example when stringent error constraints are set or when lots of
147 * discrete events are triggered, thus leading to many rejected steps.</p>
148 * @param maxEvaluations maximal number of function evaluations (negative
149 * values are silently converted to maximal integer value, thus representing
150 * almost unlimited evaluations)
151 */
152 void setMaxEvaluations(int maxEvaluations);
153
154 /** Get the maximal number of functions evaluations.
155 * @return maximal number of functions evaluations
156 */
157 int getMaxEvaluations();
158
159 /** Get the number of evaluations of the differential equations function.
160 * <p>
161 * The number of evaluations corresponds to the last call to the
162 * <code>integrate</code> method. It is 0 if the method has not been called yet.
163 * </p>
164 * @return number of evaluations of the differential equations function
165 */
166 int getEvaluations();
167
168 /** Integrate the differential equations up to the given time.
169 * <p>This method solves an Initial Value Problem (IVP).</p>
170 * <p>Since this method stores some internal state variables made
171 * available in its public interface during integration ({@link
172 * #getCurrentSignedStepsize()}), it is <em>not</em> thread-safe.</p>
173 * @param equations differential equations to integrate
174 * @param initialState initial state (time, primary and secondary state vectors)
175 * @param finalTime target time for the integration
176 * (can be set to a value smaller than {@code t0} for backward integration)
177 * @return final state, its time will be the same as {@code finalTime} if
178 * integration reached its target, but may be different if some {@link
179 * org.apache.commons.math4.legacy.ode.events.FieldEventHandler} stops it at some point.
180 * @exception NumberIsTooSmallException if integration step is too small
181 * @exception MaxCountExceededException if the number of functions evaluations is exceeded
182 * @exception NoBracketingException if the location of an event cannot be bracketed
183 */
184 FieldODEStateAndDerivative<T> integrate(FieldExpandableODE<T> equations,
185 FieldODEState<T> initialState, T finalTime)
186 throws NumberIsTooSmallException, MaxCountExceededException, NoBracketingException;
187 }