ExpandableODE.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.exception.MathIllegalArgumentException;
  21. import org.hipparchus.exception.MathIllegalStateException;


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

  43. public class ExpandableODE {

  44.     /** Primary differential equation. */
  45.     private final OrdinaryDifferentialEquation primary;

  46.     /** Components of the expandable ODE. */
  47.     private List<SecondaryODE> components;

  48.     /** Mapper for all equations. */
  49.     private EquationsMapper mapper;

  50.     /** Build an expandable set from its primary ODE set.
  51.      * @param primary the primary set of differential equations to be integrated.
  52.      */
  53.     public ExpandableODE(final OrdinaryDifferentialEquation primary) {
  54.         this.primary    = primary;
  55.         this.components = new ArrayList<>();
  56.         this.mapper     = new EquationsMapper(null, primary.getDimension());
  57.     }

  58.     /** Get the primary set of differential equations to be integrated.
  59.      * @return primary set of differential equations to be integrated
  60.      */
  61.     public OrdinaryDifferentialEquation getPrimary() {
  62.         return primary;
  63.     }

  64.     /** Get the mapper for the set of equations.
  65.      * @return mapper for the set of equations
  66.      */
  67.     public EquationsMapper getMapper() {
  68.         return mapper;
  69.     }

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

  78.         components.add(secondary);
  79.         mapper = new EquationsMapper(mapper, secondary.getDimension());

  80.         return components.size();

  81.     }

  82.     /** Initialize equations at the start of an ODE integration.
  83.      * @param s0 state at integration start
  84.      * @param finalTime target time for the integration
  85.      * @exception MathIllegalStateException if the number of functions evaluations is exceeded
  86.      * @exception MathIllegalArgumentException if arrays dimensions do not match equations settings
  87.      */
  88.     public void init(final ODEState s0, final double finalTime) {

  89.         final double t0 = s0.getTime();

  90.         // initialize primary equations
  91.         final double[] primary0 = s0.getPrimaryState();
  92.         primary.init(t0, primary0, finalTime);

  93.         // initialize secondary equations
  94.         for (int index = 1; index < mapper.getNumberOfEquations(); ++index) {
  95.             final double[] secondary0 = s0.getSecondaryState(index);
  96.             components.get(index - 1).init(t0, primary0, secondary0, finalTime);
  97.         }

  98.     }

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

  108.         final double[] yDot = new double[mapper.getTotalDimension()];

  109.         // compute derivatives of the primary equations
  110.         final double[] primaryState    = mapper.extractEquationData(0, y);
  111.         final double[] primaryStateDot = primary.computeDerivatives(t, primaryState);

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

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

  124.         return yDot;

  125.     }

  126. }