FieldExpandableODE.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;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.apache.commons.math4.legacy.core.RealFieldElement;
  21. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  22. import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
  23. import org.apache.commons.math4.legacy.core.MathArrays;


  24. /**
  25.  * This class represents a combined set of first order differential equations,
  26.  * with at least a primary set of equations expandable by some sets of secondary
  27.  * equations.
  28.  * <p>
  29.  * One typical use case is the computation of the Jacobian matrix for some ODE.
  30.  * In this case, the primary set of equations corresponds to the raw ODE, and we
  31.  * add to this set another bunch of secondary equations which represent the Jacobian
  32.  * matrix of the primary set.
  33.  * </p>
  34.  * <p>
  35.  * We want the integrator to use <em>only</em> the primary set to estimate the
  36.  * errors and hence the step sizes. It should <em>not</em> use the secondary
  37.  * equations in this computation. The {@link FirstOrderFieldIntegrator integrator} will
  38.  * be able to know where the primary set ends and so where the secondary sets begin.
  39.  * </p>
  40.  *
  41.  * @see FirstOrderFieldDifferentialEquations
  42.  * @see FieldSecondaryEquations
  43.  *
  44.  * @param <T> the type of the field elements
  45.  * @since 3.6
  46.  */

  47. public class FieldExpandableODE<T extends RealFieldElement<T>> {

  48.     /** Primary differential equation. */
  49.     private final FirstOrderFieldDifferentialEquations<T> primary;

  50.     /** Components of the expandable ODE. */
  51.     private List<FieldSecondaryEquations<T>> components;

  52.     /** Mapper for all equations. */
  53.     private FieldEquationsMapper<T> mapper;

  54.     /** Build an expandable set from its primary ODE set.
  55.      * @param primary the primary set of differential equations to be integrated.
  56.      */
  57.     public FieldExpandableODE(final FirstOrderFieldDifferentialEquations<T> primary) {
  58.         this.primary    = primary;
  59.         this.components = new ArrayList<>();
  60.         this.mapper     = new FieldEquationsMapper<>(null, primary.getDimension());
  61.     }

  62.     /** Get the mapper for the set of equations.
  63.      * @return mapper for the set of equations
  64.      */
  65.     public FieldEquationsMapper<T> getMapper() {
  66.         return mapper;
  67.     }

  68.     /** Add a set of secondary equations to be integrated along with the primary set.
  69.      * @param secondary secondary equations set
  70.      * @return index of the secondary equation in the expanded state, to be used
  71.      * as the parameter to {@link FieldODEState#getSecondaryState(int)} and
  72.      * {@link FieldODEStateAndDerivative#getSecondaryDerivative(int)} (beware index
  73.      * 0 corresponds to main state, additional states start at 1)
  74.      */
  75.     public int addSecondaryEquations(final FieldSecondaryEquations<T> secondary) {

  76.         components.add(secondary);
  77.         mapper = new FieldEquationsMapper<>(mapper, secondary.getDimension());

  78.         return components.size();
  79.     }

  80.     /** Initialize equations at the start of an ODE integration.
  81.      * @param t0 value of the independent <I>time</I> variable at integration start
  82.      * @param y0 array containing the value of the state vector at integration start
  83.      * @param finalTime target time for the integration
  84.      * @exception MaxCountExceededException if the number of functions evaluations is exceeded
  85.      * @exception DimensionMismatchException if arrays dimensions do not match equations settings
  86.      */
  87.     public void init(final T t0, final T[] y0, final T finalTime) {

  88.         // initialize primary equations
  89.         int index = 0;
  90.         final T[] primary0 = mapper.extractEquationData(index, y0);
  91.         primary.init(t0, primary0, finalTime);

  92.         // initialize secondary equations
  93.         while (++index < mapper.getNumberOfEquations()) {
  94.             final T[] secondary0 = mapper.extractEquationData(index, y0);
  95.             components.get(index - 1).init(t0, primary0, secondary0, finalTime);
  96.         }
  97.     }

  98.     /** Get the current time derivative of the complete state vector.
  99.      * @param t current value of the independent <I>time</I> variable
  100.      * @param y array containing the current value of the complete state vector
  101.      * @return time derivative of the complete state vector
  102.      * @exception MaxCountExceededException if the number of functions evaluations is exceeded
  103.      * @exception DimensionMismatchException if arrays dimensions do not match equations settings
  104.      */
  105.     public T[] computeDerivatives(final T t, final T[] y)
  106.         throws MaxCountExceededException, DimensionMismatchException {

  107.         final T[] yDot = MathArrays.buildArray(t.getField(), mapper.getTotalDimension());

  108.         // compute derivatives of the primary equations
  109.         int index = 0;
  110.         final T[] primaryState    = mapper.extractEquationData(index, y);
  111.         final T[] primaryStateDot = primary.computeDerivatives(t, primaryState);
  112.         mapper.insertEquationData(index, primaryStateDot, yDot);

  113.         // Add contribution for secondary equations
  114.         while (++index < mapper.getNumberOfEquations()) {
  115.             final T[] componentState    = mapper.extractEquationData(index, y);
  116.             final T[] componentStateDot = components.get(index - 1).computeDerivatives(t, primaryState, primaryStateDot,
  117.                                                                                        componentState);
  118.             mapper.insertEquationData(index, componentStateDot, yDot);
  119.         }

  120.         return yDot;
  121.     }
  122. }