001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.ode;
019
020import java.util.Collection;
021
022import org.apache.commons.math3.RealFieldElement;
023import org.apache.commons.math3.analysis.solvers.BracketedRealFieldUnivariateSolver;
024import org.apache.commons.math3.exception.MaxCountExceededException;
025import org.apache.commons.math3.exception.NoBracketingException;
026import org.apache.commons.math3.exception.NumberIsTooSmallException;
027import org.apache.commons.math3.ode.events.FieldEventHandler;
028import org.apache.commons.math3.ode.sampling.FieldStepHandler;
029
030/** This interface represents a first order integrator for
031 * differential equations.
032
033 * <p>The classes which are devoted to solve first order differential
034 * equations should implement this interface. The problems which can
035 * be handled should implement the {@link
036 * FirstOrderDifferentialEquations} interface.</p>
037 *
038 * @see FirstOrderFieldDifferentialEquations
039 * @param <T> the type of the field elements
040 * @since 3.6
041 */
042
043public interface FirstOrderFieldIntegrator<T extends RealFieldElement<T>> {
044
045    /** Get the name of the method.
046     * @return name of the method
047     */
048    String getName();
049
050    /** Add a step handler to this integrator.
051     * <p>The handler will be called by the integrator for each accepted
052     * step.</p>
053     * @param handler handler for the accepted steps
054     * @see #getStepHandlers()
055     * @see #clearStepHandlers()
056     */
057    void addStepHandler(FieldStepHandler<T> handler);
058
059    /** Get all the step handlers that have been added to the integrator.
060     * @return an unmodifiable collection of the added events handlers
061     * @see #addStepHandler(FieldStepHandler)
062     * @see #clearStepHandlers()
063     */
064    Collection<FieldStepHandler<T>> getStepHandlers();
065
066    /** Remove all the step handlers that have been added to the integrator.
067     * @see #addStepHandler(FieldStepHandler)
068     * @see #getStepHandlers()
069     */
070    void clearStepHandlers();
071
072    /** Add an event handler to the integrator.
073     * <p>
074     * The default solver is a 5<sup>th</sup> order {@link
075     * org.apache.commons.math3.analysis.solvers.FieldBracketingNthOrderBrentSolver}.
076     * </p>
077     * @param handler event handler
078     * @param maxCheckInterval maximal time interval between switching
079     * function checks (this interval prevents missing sign changes in
080     * case the integration steps becomes very large)
081     * @param convergence convergence threshold in the event time search
082     * @param maxIterationCount upper limit of the iteration count in
083     * the event time search events.
084     * @see #addEventHandler(FieldEventHandler, double, double, int,
085     * org.apache.commons.math3.analysis.solvers.BracketedRealFieldUnivariateSolver)
086     * @see #getEventHandlers()
087     * @see #clearEventHandlers()
088     */
089    void addEventHandler(FieldEventHandler<T>  handler, double maxCheckInterval,
090                         double convergence, int maxIterationCount);
091
092    /** Add an event handler to the integrator.
093     * @param handler event handler
094     * @param maxCheckInterval maximal time interval between switching
095     * function checks (this interval prevents missing sign changes in
096     * case the integration steps becomes very large)
097     * @param convergence convergence threshold in the event time search
098     * @param maxIterationCount upper limit of the iteration count in
099     * 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.math3.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
188}