View Javadoc
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  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.exception.MathIllegalArgumentException;
24  import org.hipparchus.exception.MathIllegalStateException;
25  import org.hipparchus.util.MathArrays;
26  
27  
28  /**
29   * This class represents a combined set of first order differential equations,
30   * with at least a primary set of equations expandable by some sets of secondary
31   * equations.
32   * <p>
33   * One typical use case is the computation of the Jacobian matrix for some ODE.
34   * In this case, the primary set of equations corresponds to the raw ODE, and we
35   * add to this set another bunch of secondary equations which represent the Jacobian
36   * matrix of the primary set.
37   * </p>
38   * <p>
39   * We want the integrator to use <em>only</em> the primary set to estimate the
40   * errors and hence the step sizes. It should <em>not</em> use the secondary
41   * equations in this computation. The {@link FieldODEIntegrator integrator} will
42   * be able to know where the primary set ends and so where the secondary sets begin.
43   * </p>
44   *
45   * @see FieldOrdinaryDifferentialEquation
46   * @see FieldSecondaryODE
47   *
48   * @param <T> the type of the field elements
49   */
50  
51  public class FieldExpandableODE<T extends CalculusFieldElement<T>> {
52  
53      /** Primary differential equation. */
54      private final FieldOrdinaryDifferentialEquation<T> primary;
55  
56      /** Components of the expandable ODE. */
57      private List<FieldSecondaryODE<T>> components;
58  
59      /** Mapper for all equations. */
60      private FieldEquationsMapper<T> mapper;
61  
62      /** Build an expandable set from its primary ODE set.
63       * @param primary the primary set of differential equations to be integrated.
64       */
65      public FieldExpandableODE(final FieldOrdinaryDifferentialEquation<T> primary) {
66          this.primary    = primary;
67          this.components = new ArrayList<>();
68          this.mapper     = new FieldEquationsMapper<>(null, primary.getDimension());
69      }
70  
71      /** Get the primary set of differential equations to be integrated.
72       * @return primary set of differential equations to be integrated
73       * @since 2.2
74       */
75      public FieldOrdinaryDifferentialEquation<T> getPrimary() {
76          return primary;
77      }
78  
79      /** Get the mapper for the set of equations.
80       * @return mapper for the set of equations
81       */
82      public FieldEquationsMapper<T> getMapper() {
83          return mapper;
84      }
85  
86      /** Add a set of secondary equations to be integrated along with the primary set.
87       * @param secondary secondary equations set
88       * @return index of the secondary equation in the expanded state, to be used
89       * as the parameter to {@link FieldODEState#getSecondaryState(int)} and
90       * {@link FieldODEStateAndDerivative#getSecondaryDerivative(int)} (beware index
91       * 0 corresponds to primary state, secondary states start at 1)
92       */
93      public int addSecondaryEquations(final FieldSecondaryODE<T> secondary) {
94  
95          components.add(secondary);
96          mapper = new FieldEquationsMapper<>(mapper, secondary.getDimension());
97  
98          return components.size();
99  
100     }
101 
102     /** Initialize equations at the start of an ODE integration.
103      * @param s0 state at integration start
104      * @param finalTime target time for the integration
105      * @exception MathIllegalStateException if the number of functions evaluations is exceeded
106      * @exception MathIllegalArgumentException if arrays dimensions do not match equations settings
107      */
108     public void init(final FieldODEState<T> s0, final T finalTime) {
109 
110         final T t0 = s0.getTime();
111 
112         // initialize primary equations
113         final T[] primary0 = s0.getPrimaryState();
114         primary.init(t0, primary0, finalTime);
115 
116         // initialize secondary equations
117         for (int index = 1; index < mapper.getNumberOfEquations(); ++index) {
118             final T[] secondary0 = s0.getSecondaryState(index);
119             components.get(index - 1).init(t0, primary0, secondary0, finalTime);
120         }
121 
122     }
123 
124     /** Get the current time derivative of the complete state vector.
125      * @param t current value of the independent <I>time</I> variable
126      * @param y array containing the current value of the complete state vector
127      * @return time derivative of the complete state vector
128      * @exception MathIllegalStateException if the number of functions evaluations is exceeded
129      * @exception MathIllegalArgumentException if arrays dimensions do not match equations settings
130      */
131     public T[] computeDerivatives(final T t, final T[] y)
132         throws MathIllegalArgumentException, MathIllegalStateException {
133 
134         final T[] yDot = MathArrays.buildArray(t.getField(), mapper.getTotalDimension());
135 
136         // compute derivatives of the primary equations
137         final T[] primaryState    = mapper.extractEquationData(0, y);
138         final T[] primaryStateDot = primary.computeDerivatives(t, primaryState);
139 
140         // Add contribution for secondary equations
141         for (int index = 1; index < mapper.getNumberOfEquations(); ++index) {
142             final T[] componentState    = mapper.extractEquationData(index, y);
143             final T[] componentStateDot = components.get(index - 1).computeDerivatives(t, primaryState, primaryStateDot,
144                                                                                        componentState);
145             mapper.insertEquationData(index, componentStateDot, yDot);
146         }
147 
148         // we retrieve the primaryStateDot array after the secondary equations have
149         // been computed in case they change the main state derivatives; this happens
150         // for example in optimal control when the secondary equations handle co-state,
151         // which changes control, and the control changes the primary state
152         mapper.insertEquationData(0, primaryStateDot, yDot);
153 
154         return yDot;
155 
156     }
157 
158 }