EulerFieldIntegrator.java

  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. package org.apache.commons.math4.legacy.ode.nonstiff;

  18. import org.apache.commons.math4.legacy.core.Field;
  19. import org.apache.commons.math4.legacy.core.RealFieldElement;
  20. import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
  21. import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
  22. import org.apache.commons.math4.legacy.core.MathArrays;

  23. /**
  24.  * This class implements a simple Euler integrator for Ordinary
  25.  * Differential Equations.
  26.  *
  27.  * <p>The Euler algorithm is the simplest one that can be used to
  28.  * integrate ordinary differential equations. It is a simple inversion
  29.  * of the forward difference expression :
  30.  * <code>f'=(f(t+h)-f(t))/h</code> which leads to
  31.  * <code>f(t+h)=f(t)+hf'</code>. The interpolation scheme used for
  32.  * dense output is the linear scheme already used for integration.</p>
  33.  *
  34.  * <p>This algorithm looks cheap because it needs only one function
  35.  * evaluation per step. However, as it uses linear estimates, it needs
  36.  * very small steps to achieve high accuracy, and small steps lead to
  37.  * numerical errors and instabilities.</p>
  38.  *
  39.  * <p>This algorithm is almost never used and has been included in
  40.  * this package only as a comparison reference for more useful
  41.  * integrators.</p>
  42.  *
  43.  * @see MidpointFieldIntegrator
  44.  * @see ClassicalRungeKuttaFieldIntegrator
  45.  * @see GillFieldIntegrator
  46.  * @see ThreeEighthesFieldIntegrator
  47.  * @see LutherFieldIntegrator
  48.  * @param <T> the type of the field elements
  49.  * @since 3.6
  50.  */

  51. public class EulerFieldIntegrator<T extends RealFieldElement<T>> extends RungeKuttaFieldIntegrator<T> {

  52.     /** Simple constructor.
  53.      * Build an Euler integrator with the given step.
  54.      * @param field field to which the time and state vector elements belong
  55.      * @param step integration step
  56.      */
  57.     public EulerFieldIntegrator(final Field<T> field, final T step) {
  58.         super(field, "Euler", step);
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public T[] getC() {
  63.         return MathArrays.buildArray(getField(), 0);
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public T[][] getA() {
  68.         return MathArrays.buildArray(getField(), 0, 0);
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public T[] getB() {
  73.         final T[] b = MathArrays.buildArray(getField(), 1);
  74.         b[0] = getField().getOne();
  75.         return b;
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     protected EulerFieldStepInterpolator<T>
  80.         createInterpolator(final boolean forward, T[][] yDotK,
  81.                            final FieldODEStateAndDerivative<T> globalPreviousState,
  82.                            final FieldODEStateAndDerivative<T> globalCurrentState,
  83.                            final FieldEquationsMapper<T> mapper) {
  84.         return new EulerFieldStepInterpolator<>(getField(), forward, yDotK,
  85.                                                  globalPreviousState, globalCurrentState,
  86.                                                  globalPreviousState, globalCurrentState,
  87.                                                  mapper);
  88.     }
  89. }