# Components and Data Structures

## Introduction

NDSolve is broken up into several basic steps. For advanced usage, it can sometimes be advantageous to access components to carry out each of these steps separately.

• Equation processing and method selection
• Method initialization
• Numerical solution
• Solution processing

NDSolve performs each of these steps internally, hiding the details from a casual user.

Here are the low-level functions that are used to break up these steps.

• NDSolve`ProcessEquations
• NDSolve`Iterate
• NDSolve`ProcessSolutions

NDSolve`ProcessEquations classifies the differential system into initial value problem, boundary value problem, differential-algebraic problem, partial differential problem, etc. It also chooses appropriate default integration methods and constructs the main NDSolve`StateData data structure.

NDSolve`Iterate advances the numerical solution. The first invocation (there can be several) initializes the numerical integration methods.

NDSolve`ProcessSolutions converts numerical data into an InterpolatingFunction to represent each solution.

Note that NDSolve`ProcessEquations can take a significant portion of the overall time to solve a differential system. In such cases, it can be useful to perform this step only once and use NDSolve`Reinitialize to repeatedly solve for different options or initial conditions.

## Example

Process equations and set up data structures for solving the differential system.
 In[1]:=
 Out[1]=
Initialize the method "ExplicitRungeKutta" and integrate the system up to time 10. The return value of NDSolve`Iterate is Null in order to avoid extra references, which would lead to undesirable copying.
 In[2]:=
Convert each set of solution data into an InterpolatingFunction.
 In[3]:=
 Out[3]=
Representing the solution as an InterpolatingFunction allows continuous output even for points that are not part of the numerical solution grid.
 In[4]:=
 Out[4]=

## ProcessEquations

The first stage of any solution using NDSolve is processing the equations specified into a form that can be efficiently accessed by the actual integration algorithms. This stage minimally involves determining the differential order of each variable, making substitutions needed to get a first-order system, solving for the time derivatives of the functions in terms of the functions, and forming the result into a "NumericalFunction" object. If you want to save the time of repeating this process for the same set of equations, or if you want more control over the numerical integration process, the processing stage can be executed separately with NDSolve`ProcessEquations.

 NDSolve`ProcessEquations[{eqn1,eqn2,…},{u1,u2,…},t] process the differential equations Null for the functions Null into a normal form; return a list of Null objects containing the solution and data associated with each solution for the time derivatives of the functions in terms of the functions; t may be specified in a list with a range of values as in NDSolve NDSolve`ProcessEquations[{eqn1,eqn2,…},{u1,u2,…},{x1,x1min,x1max},{x2,x2min,x2max},…] process the partial differential equations Null for the functions Null into a normal form; return a list of NDSolve`StateData objects containing the solution and data associated with each solution for the time derivatives of the functions in terms of the functions; if xj is the temporal variable, it need not be specified with the boundaries xj min,xj max

Processing equations for NDSolve.

This creates a list of two NDSolve`StateData objects because there are two possible solutions for the in terms of .
 In[5]:=
 Out[5]=

## Reinitialize

It is not uncommon that the solution to a more sophisticated problem involves solving the same differential equation repeatedly, but with different initial conditions. In some cases, processing equations may be as time consuming as numerically integrating the differential equations. In these situations, it is a significant advantage to be able to simply give new initial values.

 NDSolve`Reinitialize[state,conditions] assuming the equations and variables are the same as the ones used to create the NDSolve`StateData object state, form a list of new NDSolve`StateData objects, one for each of the possible solutions for the initial values of the functions of the equations conditions

Reusing processed equations.

This creates an NDSolve`StateData object for the harmonic oscillator.
 In[6]:=
 Out[6]=
This creates three new NDSolve`StateData objects, each with a different initial condition.
 In[7]:=
 Out[7]=

Using NDSolve`Reinitialize may save computation time when you need to solve the same differential equation for many different initial conditions, as you might in a shooting method for boundary value problems.

A subset of NDSolve options can be specified as options to NDSolve`Reinitialize.

This creates a new NDSolve`StateData object, specifying a starting step size.
 In[8]:=
 Out[8]=

## Iterating Solutions

One important use of NDSolve`StateData objects is to have more control of the integration. For some problems, it is appropriate to check the solution and start over or change parameters, depending on certain conditions.

 NDSolve`Iterate[state,Null] compute the solution of the differential equation in an NDSolve`StateData object that has been assigned as the value of the variable state from the current time up to time t

Iterating solutions to differential equations.

This creates an NDSolve`StateData object that contains the information needed to solve the equation for an oscillator with a varying coefficient using an explicit RungeKutta method.
 In[9]:=
 Out[9]=

Note that when you use NDSolve`ProcessEquations, you do not need to give the range of the variable explicitly because that information is not needed to set up the equations in a form ready to solve. (For PDEs, you do have to give the ranges of all spatial variables, however, since that information is essential for determining an appropriate discretization.)

This computes the solution out to time .
 In[10]:=

NDSolve`Iterate does not return a value because it modifies the NDSolve`StateData object assigned to the variable state. Thus, the command affects the value of the variable in a manner similar to setting parts of a list, as described in "Manipulating Lists by Their Indices". You can see that the value of state has changed, since it now displays the current time to which it is integrated.

The output form of state shows the range of times over which the solution has been integrated.
 In[11]:=
 Out[11]=

If you want to integrate further, you can call NDSolve`Iterate again, but with a larger value for time.

This computes the solution out to time .
 In[12]:=

You can specify a time that is earlier than the first current time, in which case the integration proceeds backward with respect to time.

This computes the solution from the initial condition backward to .
 In[13]:=

NDSolve`Iterate allows you to specify intermediate times at which to stop. This can be useful, for example, to avoid discontinuities. Typically, this strategy is more effective with so-called one-step methods, such as the explicit RungeKutta method used in this example. However, it generally works with the default NDSolve method as well.

This computes the solution out to , making sure that the solution does not have problems with the points of discontinuity in the coefficients at , , .
 In[14]:=

## Getting Solution Functions

Once you have integrated a system up to a certain time, typically you want to be able to look at the current solution values and generate an approximate function representing the solution computed so far. The command NDSolve`ProcessSolutions allows you to do both.

 NDSolve`ProcessSolutions[state] give the solutions that have been computed in state as a list of rules with InterpolatingFunction objects

Getting solutions as InterpolatingFunction objects.

This extracts the solution computed in the previous section as an InterpolatingFunction object.
 In[15]:=
 Out[15]=
This plots the solution.
 In[16]:=
 Out[16]=

Just as when using NDSolve directly, there will be a rule for each function you specified in the second argument to NDSolve`ProcessEquations. Only the specified components of the solutions are saved in such a way that an InterpolatingFunction object can be created.

 NDSolve`ProcessSolutions[state,dir] give the solutions that have been most recently computed in direction Null in Null as a list of rules with values for both the functions and their derivatives

Obtaining the current solution values.

This gives the current solution values and derivatives in the forward direction.
 In[17]:=
 Out[17]=

The choices you can give for the direction dir are "Forward" and "Backward", which refer to the integration forward and backward from the initial condition.

 "Forward" integration in the direction of increasing values of the temporal variable "Backward" integration in the direction of decreasing values of the temporal variable "Active" integration in the direction that is currently being integrated; typically, this value should only be called from method initialization that is used during an active integration

Integration direction specifications.

The output given by NDSolve`ProcessSolution is always given in terms of the dependent variables, either at a specific value of the independent variable, or interpolated over all of the saved values. This means that when a partial differential equation is being integrated, you will get results representing the dependent variables over the spatial variables.

This computes the solution to the heat equation from time to .
 In[18]:=
This gives the solution at .
 In[20]:=
 Out[20]=

The solution is given as an InterpolatingFunction object that interpolates over the spatial variable .

This gives the solution at .
 In[21]:=
 Out[21]=

When you process the current solution for partial differential equations, the spatial error estimate is checked. (It is not generally checked except when solutions are produced because doing so would be quite time consuming.) Since it is excessive, the NDSolve::eerr message is issued. The typical association of the word "backward" with the heat equation as implying instability gives a clue to what is wrong in this example.

Here is a plot of the solution at .
 In[22]:=
 Out[22]=

The plot of the solution shows that instability is indeed the problem.

Even though the heat equation example is simple enough to know that the solution backward in time is problematic, using NDSolve`Iterate and NDSolve`ProcessSolutions to monitor the solution of a PDE can be done to save computing a solution that turns out not to be as accurate as desired. Another simple form of monitoring follows.

Entering the following commands generates a sequence of plots showing the solution of a generalization of the sine-Gordon equation as it is being computed.
 In[23]:=
 Out[25]=

When you monitor a solution in this way, it is usually possible to interrupt the computation if you see that the solution found is sufficient. You can still use the NDSolve`StateData object to get the solutions that have been computed.

## NDSolve`StateData Properties

An NDSolve`StateData object contains a lot of information, but it is arranged in a manner that makes it easy to iterate solutions, and not in a manner that makes it easy to understand where the information is kept. However, sometimes you will want to get information from the state data object: for this reason several properties have been defined to make accessing the information easy.

One of the most important uses of the information from an NDSolve`StateData object is to set up integration methods. Examples are shown in "The NDSolve Method Plugin Framework".

 state@"VariableData" gives a list of lists all of the variables structured according to the solution data state@"VariableDimensions" gives lists of the dimensions of each of the variables state@"VariablePositions" give lists of the positions in the solution data for each of the variables state@"NumericalFunction" give the NumericalFunction object used to evaluate the derivatives of the solution vector with respect to the temporal variable t state@"ProcessExpression"[args,expr,dims] process the expression expr using the same variable transformations that NDSolve`ProcessEquations used to generate state to give a NumericalFunction object for numerically evaluating expr; args are the arguments for the numerical function and should either be All or a list of arguments that are dependent variables of the system; dims should be Automatic or an explicit list giving the expected dimensions of the numerical function result state@"SystemSize" give the effective number of first-order ordinary differential equations being solved state@"MaxSteps" give the maximum number of steps allowed for iterating the differential equations state@"WorkingPrecision" give the working precision used to solve the equations state@"Norm" give the scaled norm to use for gauging error

Properties for an NDSolve`StateData object state.

Here is an NDSolve`StateData object for the pendulum equation.
 In[26]:=
 Out[26]=
Get the system size.
 In[27]:=
 Out[27]=

The system size is 2 because there are two scalar ODEs.

Much of the available information depends on the current solution values. NDSolve saves different data for the forward and backward (from the initial condition time) integration directions. These can be accessed by the following directional properties.

 state@"SolutionData"[dir] give the current value of the solution data in the integration direction dir state@"TimeStep"[dir] give the time step size for the next step in the integration direction dir state@"TimeStepsUsed"[dir] give the number of time steps used to get to the current time in the integration direction dir state@"MethodData"[dir] give the method data object used in the integration direction dir

Directional properties for an NDSolve`StateData object state.

If the direction argument is omitted, the functions will return a list with the data for both directions (a list with a single element at the initial condition). Otherwise, the direction can be "Forward", "Backward", or "Active" as specified in the previous subsection.

The time step is not initialized until integration has been started unless it was explicitly specified with the StartingStepSize option.
 In[28]:=
 Out[28]=
Integrate to and get the time step in the forward direction.
 In[29]:=
 Out[30]=
The number of time steps used in the backward and forward directions.
 In[31]:=
 Out[31]=
Get the solution data for the pendulum example.
 In[32]:=
 Out[32]=

### Solution Data

The solution data consists of a list of the lists of values for each type of solution component. The solution data components correspond to different types of variables, such as time, dependent variables, and discrete variables. Any component that is not being used for a particular problem will be given as an empty list ({}) or None.

 NDSolve`SolutionDataComponent[sd,c] get the solution component c from solution data sd NDSolve`SetSolutionDataComponent[sd,c,v] set the solution component c to v in the solution data assigned to the symbol sd nf@"SolutionDataComponents" give the solution data components used as arguments for the NumericalFunction nf used in NDSolve

Getting and setting solution data components.

The components of solution data are given in the following table.

 short name full name component "T" "Time" current time "S" "Space" spatial discretization "X" "DependentVariables" current values of dependent variables other than discrete "X'" "TemporalDerivatives" the time derivatives of the dependent variables "D" "DiscreteVariables" the discrete variables that take on a continuous range of values "ID" "IndexedDiscreteVariables" the discrete variables that take on a finite set of values "P" "Parameters" the parameters other than ones for which senstivity is being computed "SP" "SensitivityParameters" the parameters for which sensitivity is being computed

Solution data components.

Here is an NDSolve`StateData object for a solution of the pendulum equation with a discrete variable that has been computed up to .
 In[33]:=
 Out[35]=
This shows the variables that are used in the problem.
 In[36]:=
 Out[36]=
This shows the variables that are used in the problem.
 In[36]:=
 Out[36]=

To extract components from both variable data and solution data, NDSolve`SolutionDataComponent can be used.

Show the dependent variables.
 In[37]:=
 Out[37]=
This gets the current solution data in the backward and forward directions.
 In[38]:=
 Out[38]=
This gets the values of the dependent variables component for the forward direction.
 In[39]:=
 Out[39]=
This gets the discrete variables for the backward direction.
 In[40]:=
 Out[40]=
This gets the indexed discrete variables for the backward direction. The result is an empty list because no indexed discrete variables are used in this computation.
 In[41]:=
 Out[41]=

Note that the solution data is effectively the raw data that NDSolve uses to compute the solution. In the example above, the dependent variables component is a list of length 2 because the second-order equation is automatically reduced to a first-order system. If you want to have a clear identification of the parts of the current solution, you can use NDSolve`ProcessSolutions.

Get solution data associated with the variables via rules.
 In[42]:=
 Out[42]=

### NumericalFunction

When doing time integration, the methods NDSolve uses work with a right-hand side function for ODEs such that or a residual function for DAEs such that , where is real and , , and other solution data components used are vectors. The actual variables represented by the vectors and may have more complicated structure, so NDSolve uses an intermediate NumericalFunction object that takes vector arguments and returns vectors based on any underlying function.

Get the NDSolve`StateData object for a system with vector and scalar variables.
 In[43]:=
 Out[43]=
Get the NumericalFunction that evaluates the derivatives.
 In[44]:=
 Out[44]=

The NumericalFunction that NDSolve constructs for evaluation only uses as arguments the solution data components that are actually used in the problem. You can find what components it actually uses by using the "SolutionDataComponents" property.

Find which solution data components are required for evaluation
 In[45]:=
 Out[45]=
Get the initial solution data and the evaluation arguments
 In[46]:=
 Out[46]=
 In[47]:=
 Out[47]=

Note that the indexed discrete variable was introduced as a "DiscontinuitySignature" variable by the automatic discontinuity handling for the Sign function. (See the solution shown below.)

Evaluating the function gives the time derivatives, since the system is an ODE.
 In[48]:=
 Out[48]=
 In[49]:=
 Out[49]=

To make it easier to evaluate with the solution data for a given problem, two convenience functions have been included that evaluate with the solution data.

 NDSolve`EvaluateWithSolutionData[nf,sd] evaluate the NumericalFunction nf with arguments from the solution data list sd NDSolve`EvaluateJacobianWithSolutionData[nf,sd,c] evaluate the Jacobian derivative with respect to all the variables in the the solution data component c

Evaluating with solution data.

Evaluate the function using the solution data list.
 In[50]:=
 Out[50]=

One of the things that NumericalFunction does is to handle derivatives automatically. By default, symbolic derivatives are used if they can be found, and otherwise finite differences are used.

Evaluate the Jacobian with respect to the dependent variables.
 In[51]:=
 Out[51]=

In this example, finite differences are used because of the vector arguments and Dot products.

Integrate and plot the solution for z and z'.
 In[52]:=
 Out[53]=

It is sometimes useful to set up NumericalFunction to evaluate some function of the NDSolve variables, which can be done using "ProcessExpression".

Many of the built-in methods do this during method initialization so that the function can be evaluated efficiently repeated times. For example, the "projection" method sets up a function to evaluate the deviation of the invariants from their initial values and uses the derivatives to implement the projection after each step.

Set up an NDSolve`StateData object for an oscillator.
 In[54]:=
Make a NumericalFunction for the energy integral.
 In[55]:=
 Out[55]=
Evaluate at the initial condition.
 In[56]:=
 Out[56]=
Make a plot of the energy monitored at regular intervals during the integration.
 In[57]:=
 Out[57]=
The energy deviation is actually small considering how quickly the solution is oscillating by .
 In[58]:=
 Out[58]=