FieldExpandableODE.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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.  *      https://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.hipparchus.ode;

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

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.hipparchus.exception.MathIllegalArgumentException;
  22. import org.hipparchus.exception.MathIllegalStateException;
  23. import org.hipparchus.util.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 FieldODEIntegrator integrator} will
  38.  * be able to know where the primary set ends and so where the secondary sets begin.
  39.  * </p>
  40.  *
  41.  * @see FieldOrdinaryDifferentialEquation
  42.  * @see FieldSecondaryODE
  43.  *
  44.  * @param <T> the type of the field elements
  45.  */

  46. public class FieldExpandableODE<T extends CalculusFieldElement<T>> {

  47.     /** Primary differential equation. */
  48.     private final FieldOrdinaryDifferentialEquation<T> primary;

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

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

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

  61.     /** Get the primary set of differential equations to be integrated.
  62.      * @return primary set of differential equations to be integrated
  63.      * @since 2.2
  64.      */
  65.     public FieldOrdinaryDifferentialEquation<T> getPrimary() {
  66.         return primary;
  67.     }

  68.     /** Get the mapper for the set of equations.
  69.      * @return mapper for the set of equations
  70.      */
  71.     public FieldEquationsMapper<T> getMapper() {
  72.         return mapper;
  73.     }

  74.     /** Add a set of secondary equations to be integrated along with the primary set.
  75.      * @param secondary secondary equations set
  76.      * @return index of the secondary equation in the expanded state, to be used
  77.      * as the parameter to {@link FieldODEState#getSecondaryState(int)} and
  78.      * {@link FieldODEStateAndDerivative#getSecondaryDerivative(int)} (beware index
  79.      * 0 corresponds to primary state, secondary states start at 1)
  80.      */
  81.     public int addSecondaryEquations(final FieldSecondaryODE<T> secondary) {

  82.         components.add(secondary);
  83.         mapper = new FieldEquationsMapper<>(mapper, secondary.getDimension());

  84.         return components.size();

  85.     }

  86.     /** Initialize equations at the start of an ODE integration.
  87.      * @param s0 state at integration start
  88.      * @param finalTime target time for the integration
  89.      * @exception MathIllegalStateException if the number of functions evaluations is exceeded
  90.      * @exception MathIllegalArgumentException if arrays dimensions do not match equations settings
  91.      */
  92.     public void init(final FieldODEState<T> s0, final T finalTime) {

  93.         final T t0 = s0.getTime();

  94.         // initialize primary equations
  95.         final T[] primary0 = s0.getPrimaryState();
  96.         primary.init(t0, primary0, finalTime);

  97.         // initialize secondary equations
  98.         for (int index = 1; index < mapper.getNumberOfEquations(); ++index) {
  99.             final T[] secondary0 = s0.getSecondaryState(index);
  100.             components.get(index - 1).init(t0, primary0, secondary0, finalTime);
  101.         }

  102.     }

  103.     /** Get the current time derivative of the complete state vector.
  104.      * @param t current value of the independent <I>time</I> variable
  105.      * @param y array containing the current value of the complete state vector
  106.      * @return time derivative of the complete state vector
  107.      * @exception MathIllegalStateException if the number of functions evaluations is exceeded
  108.      * @exception MathIllegalArgumentException if arrays dimensions do not match equations settings
  109.      */
  110.     public T[] computeDerivatives(final T t, final T[] y)
  111.         throws MathIllegalArgumentException, MathIllegalStateException {

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

  113.         // compute derivatives of the primary equations
  114.         final T[] primaryState    = mapper.extractEquationData(0, y);
  115.         final T[] primaryStateDot = primary.computeDerivatives(t, primaryState);

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

  123.         // we retrieve the primaryStateDot array after the secondary equations have
  124.         // been computed in case they change the main state derivatives; this happens
  125.         // for example in optimal control when the secondary equations handle co-state,
  126.         // which changes control, and the control changes the primary state
  127.         mapper.insertEquationData(0, primaryStateDot, yDot);

  128.         return yDot;

  129.     }

  130. }