Field-based version of Euler method for solving ODE.

This commit is contained in:
Luc Maisonobe 2015-11-15 10:39:26 +01:00
parent 7ec2a6342b
commit ea0dabfd1b
2 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.ode.nonstiff;
import org.apache.commons.math3.Field;
import org.apache.commons.math3.RealFieldElement;
/**
* This class implements a simple Euler integrator for Ordinary
* Differential Equations.
*
* <p>The Euler algorithm is the simplest one that can be used to
* integrate ordinary differential equations. It is a simple inversion
* of the forward difference expression :
* <code>f'=(f(t+h)-f(t))/h</code> which leads to
* <code>f(t+h)=f(t)+hf'</code>. The interpolation scheme used for
* dense output is the linear scheme already used for integration.</p>
*
* <p>This algorithm looks cheap because it needs only one function
* evaluation per step. However, as it uses linear estimates, it needs
* very small steps to achieve high accuracy, and small steps lead to
* numerical errors and instabilities.</p>
*
* <p>This algorithm is almost never used and has been included in
* this package only as a comparison reference for more useful
* integrators.</p>
*
* @see MidpointFieldIntegrator
* @see ClassicalRungeKuttaFieldIntegrator
* @see GillFieldIntegrator
* @see ThreeEighthesFieldIntegrator
* @see LutherFieldIntegrator
* @param <T> the type of the field elements
* @since 3.6
*/
public class EulerFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {
/** Time steps Butcher array. */
private static final double[] STATIC_C = {
};
/** Internal weights Butcher array. */
private static final double[][] STATIC_A = {
};
/** Propagation weights Butcher array. */
private static final double[] STATIC_B = {
1.0
};
/** Simple constructor.
* Build an Euler integrator with the given step.
* @param field field to which the time and state vector elements belong
* @param step integration step
*/
public EulerFieldIntegrator(final Field<T> field, final T step) {
super(field, "Euler", STATIC_C, STATIC_A, STATIC_B, new EulerFieldStepInterpolator<T>(), step);
}
}

View File

@ -0,0 +1,103 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.ode.nonstiff;
import org.apache.commons.math3.RealFieldElement;
import org.apache.commons.math3.ode.FieldEquationsMapper;
import org.apache.commons.math3.ode.FieldODEStateAndDerivative;
import org.apache.commons.math3.util.MathArrays;
/**
* This class implements a linear interpolator for step.
*
* <p>This interpolator computes dense output inside the last
* step computed. The interpolation equation is consistent with the
* integration scheme :
* <ul>
* <li>Using reference point at step start:<br>
* y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>) + &theta; h y'
* </li>
* <li>Using reference point at step end:<br>
* y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h) - (1-&theta;) h y'
* </li>
* </ul>
* </p>
*
* where &theta; belongs to [0 ; 1] and where y' is the evaluation of
* the derivatives already computed during the step.</p>
*
* @see EulerFieldIntegrator
* @param <T> the type of the field elements
* @since 3.6
*/
class EulerFieldStepInterpolator<T extends RealFieldElement<T>>
extends RungeKuttaFieldStepInterpolator<T> {
/** Simple constructor.
* This constructor builds an instance that is not usable yet, the
* {@link
* org.apache.commons.math3.ode.sampling.AbstractStepInterpolator#reinitialize}
* method should be called before using the instance in order to
* initialize the internal arrays. This constructor is used only
* in order to delay the initialization in some cases. The {@link
* RungeKuttaIntegrator} class uses the prototyping design pattern
* to create the step interpolators by cloning an uninitialized model
* and later initializing the copy.
*/
EulerFieldStepInterpolator() {
}
/** Copy constructor.
* @param interpolator interpolator to copy from. The copy is a deep
* copy: its arrays are separated from the original arrays of the
* instance
*/
EulerFieldStepInterpolator(final EulerFieldStepInterpolator<T> interpolator) {
super(interpolator);
}
/** {@inheritDoc} */
@Override
protected EulerFieldStepInterpolator<T> doCopy() {
return new EulerFieldStepInterpolator<T>(this);
}
/** {@inheritDoc} */
@Override
protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
final T time, final T theta,
final T oneMinusThetaH) {
final T[] interpolatedState = MathArrays.buildArray(theta.getField(), previousState.length);
if ((previousState != null) && (theta.getReal() <= 0.5)) {
for (int i = 0; i < previousState.length; ++i) {
interpolatedState[i] = previousState[i].add(theta.multiply(h).multiply(yDotK[0][i]));
}
} else {
for (int i = 0; i < previousState.length; ++i) {
interpolatedState[i] = currentState[i].subtract(oneMinusThetaH.multiply(yDotK[0][i]));
}
}
return new FieldODEStateAndDerivative<T>(time,
interpolatedState,
yDotK[0]);
}
}