Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.

Spatial Error Estimates

Overview

When NDSolve solves a PDE, unless you have specified the spatial grid for it to use, by giving it explicitly or by giving a equal values for the MinPoints and MaxPoints options, NDSolve needs to make a spatial error estimate.

Ideally, the spatial error estimates would be monitored over time and the spatial mesh updated according to the evolution of the solution. The problem of grid adaptivity is difficult enough for a specific type of PDE and certainly has not been solved in any general way. Furthermore, techniques such as local refinement can be problematic with the method of lines since changing the number of mesh points requires a complete restart of the ODE methods. There are moving mesh techniques that appear promising for this approach, but at this point, NDSolve uses a static grid. The grid to use is determined by an a-priori error estimate based on the initial condition. An a-posteriori check is done at the end of the temporal interval for reasonable consistency and a warning message is given if that fails. This can, of course, be fooled, but in practice it provides a reasonable compromise. The most common cause of failure is when initial conditions have little variation, so the estimates are essentially meaningless. In this case, you may need to choose some appropriate grid settings yourself.

A-priori Error Estimates

When NDSolve solves a PDE using the method of lines, a decision has to be made on an appropriate spatial grid. NDSolve does this using an error estimate based on the initial condition (thus, a-priori).

It is easiest to show how this works in the context of an example. For illustrative purposes, consider the sine-Gordon equation in 1-dimension with periodic boundary conditions.

This solves the sine-Gordon equation with a Gaussian initial condition.

In[105]:= 

Out[105]=

This gives the number of spatial and temporal points used, respectively.

In[106]:= 
In[107]:= 

Out[107]=

The temporal points are chosen adaptively by the ODE method based on local error control. NDSolve used 49 (50 including the periodic endpoint) spatial points. This choice will be illustrated through the steps that follow.

In the equation processing phase of NDSolve, one of the first things that happen is that equations with second (or higher) order temporal derivatives are replaced with systems with only first order temporal derivatives.

This is a first order system equivalent to the sine-Gordon equation above.

In[108]:= 

Out[108]=

The next stage is to solve for the temporal derivatives.

This is the solution for the temporal derivatives, with the right hand side of the equations in normal (ODE) form.

In[109]:= 

Out[109]=

Now the problem is to choose a uniform grid that will approximate the derivative to within local error tolerances as specified by AccuracyGoal and PrecisionGoal. For this illustration, use the default DifferenceOrder (4) and the default AccuracyGoal and PrecisionGoal (both 4 for PDEs). The methods used to integrate the system of ODEs that results from discretization base their own error estimates on the assumption of sufficiently accurate function values. The estimates here have the goal of finding a spatial grid for which (at least with the initial condition) the spatial error is somewhat balanced with the local temporal error.

This sets variables to reflect the default settings for DifferenceOrder, AccuracyGoal, and PrecisionGoal.

In[110]:= 

The error estimate is based on Richardson extrapolation. If you know that the error is  and you have two approximations  and  at different values,  and  of h, then you can, in effect, extrapolate to the limit h->0 to get an error estimate

so the error in  is estimated to be

Here  and  are vectors of different length and  is a function, so we need to choose an appropriate norm. If we choose  = 2  , then we can simply use a scaled norm on the components common to both vectors, which is all of  and every other point of  .This is a good choice because it does not require any interpolation between grids.

For a given interval on which we want to set up a uniform grid, we can define a function  , where  is the length of the interval such that the grid is  ,  , ...,  }, where  .

This defines functions that return the step size  and a list of grid points as a function of  for the sine-Gordon equation.

In[113]:= 

For a given grid, the equation can be discretized using finite differences. This is easily done using NDSolve`FiniteDifferenceDerivative.

This defines a symbolic discretization of the right hand side of the sine-Gordon equation as a function of a grid. It returns a function of u and v, which gives the approximate values for  and  in a list. (Note that in principle this works for any grid, uniform or not, though in the following, only uniform grids will be used.)

In[116]:= 

For a given step size and grid, we can also discretize the initial conditions for u and v.

This defines a function that discretizes the initial conditions for u and v. The last grid point is dropped because, by periodic continuation, it is considered the same as the first.

In[117]:= 

The quantity of interest is the approximation of the right hand side for a particular value of  with this initial condition.

This defines a function that returns a vector consisting of the approximation of the right hand side of the equation for the initial condition for a given step size and grid. The vector is flattened to make subsequent analysis of it simpler.

In[118]:= 

Starting with a particular value of  , we can obtain the error estimate by generating the right hand side for  and  points.

This gives an example of the right hand side approximation vector for a grid with 10 points.

In[119]:= 

Out[119]=

This gives an example of the right hand side approximation vector for a grid with 20 points.

In[120]:= 

Out[120]=

As mentioned above, every other point on the grid with  points lies on the grid with  points. Thus, for simplicity, we can use a norm that only compares point common to both grids. Because the goal is to ultimately satisfy absolute and relative tolerance criteria, it is appropriate to use a scaled norm. In addition to taking into account the size of the right hand side for the scaling, it is also important to include the size of the corresponding components of u and v on the grid since error in the right hand side is ultimately included in u and v.

This defines a norm function for the difference in the approximation of the right hand side.

In[121]:= 

This applies the norm function to the two approximations found above.

In[122]:= 

Out[122]=

To get the error estimate form the distance, according to the Richardson extrapolation formula (3), this simply needs to be divided by  .

This computes the error estimate for n Equal 10. Since this is based on a scaled norm, the tolerance criteria are satisfied if the result is less than 1.

In[123]:= 

Out[123]=

This makes a function that combines the above functions to give an error estimate as a function of n.

In[124]:= 

The goal is to find the minimum value of n, such that the error estimate is less than or equal to 1 (since it is based on a scaled norm). In principle, it would be possible to use a root finding algorithm on this, but since n can only be an integer, this would be overkill and adjustments would have to be made to the stopping conditions. An easier solution is simply to use the simple Richardson extrapolation formula to predict what value of n would be appropriate and repeat the prediction process until the appropriate n is found.

The condition to satisfy is

and we have estimated that

so we can project that

or in terms of n, which is proportional to the reciprocal of h,

This computes the predicted optimal value of n based on the error estimate for n Equal 10 computed above.

In[125]:= 

Out[125]=

This computes the error estimate for the new value of n.

In[126]:= 

Out[126]=

Often the case that a prediction based on a very coarse grid will be inadequate. A coarse grid may completely miss some solution features that contribute to the error on a finer grid. Also, the error estimate is based on an asymptotic formula, so for coarse spacings, the estimate itself may not be very good, even when all the solution features are resolved to some extent.

In practice, it can be fairly expensive to compute these error estimates. Also, it is not necessary to find the very optimal n, but one that satisfies the error estimate. Remember, everything can change as the PDE evolves, so it is simply not worth a lot of extra effort to find an optimal spacing for just the initial time. A simple solution is to include an extra factor greater than 1 in the prediction formula for the new n. Even with an extra factor, it may still take a few iterations to get to an acceptable value. It does, however, typically make the process faster.

This defines a function that gives a predicted value for the number of grid points, which should satisfy the error estimate.

In[127]:= 

This iterates the predictions until a value is found that satisfies the error estimate.

In[128]:= 

Out[128]=

It is important to note that this process must have a limiting value since it may not be possible to satisfy the error tolerances, for example, with a discontinuous initial function. In NDSolve, the MaxSteps option provides the limit; for spatial discretization, this defaults to a total of 10000 across all spatial dimensions.

Pseudospectral derivatives cannot use this error estimate since they have an exponential rather than a polynomial convergence. An estimate can be made based on the formula used above in the limit p->Infinity. What this amounts to is considering the result on the finer grid to be exact and basing the error estimate on the difference since (1 -  ) approaches 1. A better approach is to use the fact that on a given grid with n points, the pseudospectral method is  . When comparing for two grids, it is appropriate to use the smaller n for p. This provides an imperfect, but adequate estimate for the purpose of determining grid size.

This modifies the error estimation function so that it will work with pseudospectral derivatives.

In[129]:= 

The prediction formula can be modified to use n instead of p in a similar way.

This modifies the function predicting an appropriate value of n to work with pseudospectral derivatives. This formulation does not try to pick an efficient FFT length.

In[130]:= 

When finalizing the choice of n for a pseudospectral method, an additional consideration is to choose a value that not only satisfies the tolerance conditions, but is also an efficient length for computing FFTs. In Mathematica, an efficient FFT does not require a power of two length since the Fourier command has a prime factor algorithm built in.

Typically, the difference order has a profound effect on the number of points required to satisfy the error estimate.

This makes a table of the number of points required to satisfy the a-priori error estimate as a function of the difference order.

In[131]:= 

Out[131]//TableForm=

A table of the number of points required as a function of difference order goes a long way towards explaining why the default setting for the method of lines is DifferenceOrder->4: the improvement from 2 to 4 is usually most dramatic and in the default tolerance range, fourth order differences do not tend to produce large roundoff errors, which can be the case with higher orders. Pseudospectral differences are often a good choice, particularly with periodic boundary conditions, but it is not a good choice for the default because they lead to full Jacobian matrices, which can be expensive to generate and solve if needed for stiff equations.

For non-periodic grids, the error estimate is done using only interior points. The reason is that the error coefficients for the derivatives near the boundary are different. This may miss features that are near the boundary, but the main idea is to keep the estimate simple and inexpensive since the evolution of the PDE may change everything anyway.

For multiple spatial dimensions, the determination is made one dimension at a time. Since better resolution in one dimension may change the requirements for another, the process is repeated in reverse order to improve the choice.

A-posteriori Error Estimates

When the solution of a PDE is computed with NDSolve, a final step is to do a spatial error estimate on the evolved solution and issue a warning message if this is excessively large.

These error estimates are done in a manner similar to the a-priori estimates described previously. The only real difference is that, instead of using grids with n and 2n points to estimate the error, grids with n/2 and n points are used. This is because, while there is no way to generate the values on a grid of 2n points without using interpolation, which would introduce its own errors, values are readily available on a grid of n/2 points simply by taking every other value. This is easily done in the Richardson extrapolation formula by using  Equal 2  , which gives

This defines a function (based on functions defined in the previous section) that can compute an error estimate on the solution of the sine-Gordon equation from solutions for u and v expressed as vectors. The function has been defined to be a function of the grid since this is applied to a grid already constructed. (Note, as defined here, this only works for grids of even length. It is not difficult to handle odd length, but it makes the function somewhat more complicated.)

In[132]:= 

This solves the sine-Gordon equation with a Gaussian initial condition.

In[133]:= 

Out[133]=

This is the grid used in the spatial direction which is the first set of coordinates used in the InterpolatingFunction. A grid with the last point dropped is used to obtain the values because of periodic continuation.

In[134]:= 

Out[134]=

This makes a function that gives the a-posteriori error estimate at a particular numerical value of t.

In[136]:= 

This makes a plot of the a-posteriori error estimate as a function of t.

In[137]:= 

The large amount of local variation seen in this function is typical. For that reason, NDSolve does not warn about excessive error unless this estimate gets above 10 (rather than the value of 1, which is used to choose the grid based on initial conditions.) The extra factor of 10 is further justified by the fact that the a-posteriori error estimate is less accurate than the a-priori one. Thus, when NDSolve issues a warning message based on the a-posteriori error estimate, it is usually because new solution features have appeared or because there is instability in the solution process.

This is an example with the same initial condition used in the examples above, but where NDSolve gives a warning message based on the a-posteriori error estimate.

In[138]:= 

Out[138]=

This shows a plot of the solution at t Equal 4. It is apparent that the warning message is appropriate because the oscillations near the peak are not physical.

In[139]:= 

When the NDSolve::eerr message does show up, it may be necessary for you to use options to control the grid selection process since it is likely that the default settings did not find an accurate solution.

Controlling the Spatial Grid Selection

The NDSolve implementation of the method of lines has several ways to control the selection of the spatial grid.

Tensor product grid options for the method of lines.

All of the options for tensor product grid discretization can be given as a list with length equal to the number of spatial dimensions, in which case the parameter for each spatial dimension is determined by the corresponding component of the list.

With the exception of pseudospectral methods on non-periodic problems, discretization is done with uniform grids, so when solving a problem on interval length  , there is a direct correspondence between the Points and StepSize options:

The StepSize options are effectively converted to the equivalent Points values. They are simply provided for convenience since sometimes it is more natural to relate problem specification to step size rather then the number of discretization points. When values other then Automatic are specified for both the Points and corresponding StepSize option, generally, the more stringent restriction is used.

In the previous section an example was shown where the solution was not resolved sufficiently because the solution steepened as it evolved. The examples below will show some different ways of modifying the grid parameters so that the near shock is better resolved.

One way to avoid the oscillations that showed up in the solution as the profile steepened is to make sure that we use sufficient points to resolve the profile at its steepest. In the one-hump solution of Burgers` equation,

it can be shown [W76] that the width of the shock profile is proportional to Nu as Nu->0. More than 95% of the change is included in a layer of width 10 Nu. Thus, if we pick a maximum step size of half the profile width, there will always be a point somewhere in the steep part of the profile, and there is a hope of resolving it without significant oscillation.

This computes the solution to Burgers' equation, such that there are sufficient points to resolve the shock profile.

In[140]:= 

Out[141]=

Note that resolving the profile alone is by no means sufficient to meet the default tolerances of NDSolve, which requires an accuracy of  . However, once we have sufficient point to resolve the basic profile, it is not unreasonable to project based on the a-posteriori error estimate shown in the NDSolve::eerr message (with an extra 10% since, after all, it is just a projection).

This computes the solution to Burgers' equation with the maximum step size chosen so that it should be small enough to meet the default error tolerances based on a projection from the error of the previous calculation.

In[142]:= 

Out[143]=

To compare solutions like this, it is useful to look at a plot of the solution only at the spatial grid points. Because the grid points are stored as a part of the InterpolatingFunction, it is fairly simple to define a function that does this.

This defines a function that plots a solution only at the spatial grid points at a time t.

In[144]:= 

This makes a plot comparing the three solutions found at t = 4.

In[145]:= 

In this example, the left hand side of the domain really does not need so many points. The points need to be clustered where the steep profile evolves, so it might make sense to consider explicitly specifying a grid that has more points where the profile appears.

This solves Burgers' equation on a specified grid that has most of its points to the right of x = 1.

In[146]:= 

Out[148]=

This makes a plot of the values of the solution at the assigned spatial grid points.

In[149]:= 

Many of the same principles apply to multiple spatial dimensions. Burgers' equation in 2 dimensions with anisotropy provides a good example.

This solves a variant of Burgers' equation in 2 dimensions with different velocities in the x and y directions.

In[150]:= 

Out[151]=

This shows a surface plot of the leading edge of the solution a t = 2.

In[152]:= 

Similar to the 1 dimensional case, the leading edge steepens. Since the viscosity term (Nu) is larger, the steepening is not quite so extreme, and this default solution actually resolves the front reasonably well. Therefore it should be possible to project from the error estimate to meet the default tolerances. A simple scaling argument indicates that the profile width in the x direction will be narrower than in the y direction by a factor of  . Thus, it makes sense that the step sizes in the y direction can be larger than those in the x direction by this factor, or, correspondingly, that the minimum number of points can be a factor of 1/ less.

This solves the 2 dimensional variant of Burgers' equation with appropriate step size restrictions in the x and y direction projected from the a-posteriori error estimate of the previous computation which was done with 69 points in the x direction.

In[153]:= 

Out[154]=

This solution takes a substantial amount of time to compute, which is not surprising since the solution involves solving a system of more than 18000 ODEs. In many cases, particularly in more than 1 spatial dimension, the default tolerances may be unrealistic to achieve, so you may have to reduce them by using AccuracyGoal and/or PrecisionGoal appropriately. Sometimes, especially with the coarser grids that come with less stringent tolerances, the systems are not stiff and it is possible to use explicit methods, that avoid the numerical linear algebra which can be problematic, especially for higher dimensional problems. For this example, using Method->ExplicitRungeKutta gets the solution in about half the time.

Any of the other grid options can be specified as a list giving the values for each dimension. When only a single value is given, it is used for all the spatial dimensions. The two exceptions to this are MaxPoints, where a single value is taken to be the total number of grid points in the outer product, and Coordinates, where a grid must be specified explicitly for each dimension.

In[155]:= 

Out[157]=

It is important to keep in mind that the a-posteriori spatial error estimates are simply estimates of the local error in computing spatial derivatives and may not reflect the actual accumulated spatial error for a given solution. One way to get an estimate on the actual spatial error is to compute the solution to very stringent tolerances in time for different spatial grids. To show how this works, consider again the simpler 1-dimensional Burgers` equation.

This computes a list of solutions using {33, 65, ..., 4097} spatial grid points to compute the solution to Burgers` equation for difference orders 2, 4, 6, and pseudospectral. The temporal accuracy and precision tolerances are set very high so that essentially all of the error comes from the spatial discretization. Note that by specifying {t, 4, 4} in NDSolve, only the solution at t = 4 is saved. Without this precaution, some of the solutions for the finer grids (which take many more time steps) could exhaust available memory. Even so, the list of solutions takes a substantial amount of time to compute.

In[158]:= 

Given two solutions, a comparison needs to be done between the two. To keep out any sources of error except for that in the solutions themselves, it is best to use the data that is interpolated to make the InterpolatingFunction. This can be done by using points common to the two solutions.

This defines a function to estimate error by comparing two different solutions at the points common to both. The arguments coarse and fine should be the solutions on the coarser and finer grids, respectively. This works for the solutions generated above with grid spacing varying by powers of 2.

In[160]:= 

To get an indication of the general trend in error (in cases of instability, solutions do not converge, so this does not assume that), you can compare the difference of successive pairs of solutions.

This defines a function that will plot a sequence of error estimates for the successive solutions found for a given difference order and uses it to make a logarithmic plot of the estimated error as a function of the number of grid points.

In[161]:= 
In[162]:= 
In[164]:= 

A logarithmic plot of the maximum spatial error in approximating the solution of Burgers` equation at t = 4 as a function of the number of grid points. Finite differences of order 2, 4, and 6 on a uniform grid are shown in red, green, blue, and magenta, respectively. Pseudospectral derivatives with uniform (periodic) spacing are shown in black.

The upper left part of the plot are the grids where the profile is not adequately resolved, so differences are simply of magnitude order 1 (it would be a lot worse if there was instability) However, once there are a sufficient number of points to resolve the profile without oscillation, convergence becomes quite rapid. Not surprisingly, the slope of the logarithmic line is -4, which corresponds to the difference order NDSolve uses by default. If your grid is fine enough to be in the asymptotically converging part, a simpler error estimate could be effected by using Richardson extrapolation as in the previous two sections, but on the overall solution rather than the local error. On the other hand, computing more values and viewing a plot gives a better indication of whether you are in the asymptotic regime or not.

It is fairly clear from the plot that the best solution computed is the pseudospectral one with 2049 points (the one with more points was not computed because its spatial accuracy far exceeds the temporal tolerances that were set). This solution can, in effect, be treated almost as an exact solution, at least up to error tolerances of  or so.

To get a perspective of how best to solve the problem, it is useful to do the following: for each solution found that was at least a reasonable approximation, recompute it with the temporal accuracy tolerance set to be comparable to the possible spatial accuracy of the solution and plot the resulting accuracy as a function of solution time. The following (somewhat complicated) commands do this.

This identifies the "best" solution that will be used, in effect, as an exact solution in the computations that follow. It is dropped from the list of solutions to compare it to since the comparison would be meaningless.

In[165]:= 

This defines a function that, given a difference order, do, and a solution, sol, computed with that difference order, recomputes it with local temporal tolerance slightly more stringent than the actual spatial accuracy achieved if that accuracy is sufficient. The function output is a list of {number of grid points, difference order, time to compute in seconds, actual error of the recomputed solution}.

In[167]:= 

This applies the function to each of the previously computed solutions. (With the appropriate difference order!)

In[168]:= 

Out[168]=

This removes the cases that were not recomputed and makes a logarithmic plot of accuracy as a function of computation time.

In[169]:= 

A logarithmic plot of the error in approximating the solution of Burgers` equation at t = 4 as a function of the computation time. Each point shown indicates the number of spatial grid points used to compute the solution. Finite differences of order 2, 4, and 6 on a uniform grid are shown in red, blue, and green, respectively. Pseudospectral derivatives with uniform (periodic) spacing are shown in black.

The resulting graph demonstrates quite forcefully that, when they work, as in this case, periodic pseudospectral approximations are incredibly efficient. Otherwise, up to a point, the higher the difference order, the better the approximation will generally be. These are all features of smooth problems, which, this particular instance of Burgers` equation is. However, the higher order solutions would generally be quite poor if we went towards the limit Nu->0.

One final point to note is that the above graph was computed using the Automatic method for the temporal direction. This uses LSODA which switches between a stiff and non-stiff method depending on how the solution evolves. For the coarser grids, strictly explicit methods are typically a bit faster, and, except for the pseudospectral case, the implicit BDF methods are faster for the finer grids. A variety of alternative ODE methods are available in NDSolve.

Error at the boundaries

The a-priori error estimates are computed in the interior of the computational region because the differences used there all have consistent error terms which can be used to effectively estimate the number of points to use. Including the boundaries into the estimates would complicate the process beyond what is justified for such and a-priori estimate. Typically, this approach is successful in keeping the error under reasonable control. However, there are a few cases which can lead to difficulties.

Occasionally it may occur that because the error terms are larger for the one-sided derivatives used at the boundary, NDSolve will detect an inconsistency between boundary and initial conditions which is an artifact of the discretization error.

This solves the one-dimensional heat equation with the left end held at constant temperature and the right end radiating into free space.

In[2]:= 

Out[2]=

The NDSolve:ibcinc message is issued, in this case, completely to the larger discretization error at the right boundary. For this particular example, the extra error is not a problem because it gets damped out due to the nature of the equation. However, it is possible to eliminate the message by using just a few more spatial points.

This computes the solution to the same equation as above, but using a minimum of 50 points in the x-direction.

In[8]:= 

Out[8]=

One other case where error problems at the boundary can affect the discretization unexpectedly is when periodic boundary conditions are given with a function which is not truly periodic, so that an unintended discontinuity is introduced into the computation.

This begins the computation of the solution to the sine-Gordon equation with a Gaussian initial condition and periodic boundary conditions. The NDSolve command is wrapped with TimeConstrained since solving the given problem can take a very long time and a large amount of system memory.

In[7]:= 

Out[8]=

The problem here is that the initial condition is effectively discontinuous when the periodic continuation is taken into account.

This shows a plot of the initial condition over the extent of three full periods.

In[20]:= 

Since there is always a large derivative error at the cusps, NDSolve is forced to use the maximum number of points in an attempt to satisfy the a-priori error bound. To make matters worse, the extreme change makes solving the resulting ODEs more difficult, leading to a very long solution time which uses a lot of memory.

If the discontinuity is really intended, you will typically want to specify a number of points or spacing for the spatial grid which will be sufficiently to handle the aspects of the discontinuity you are interested in. To model discontinuities with high accuracy will typically take specialized methods which are beyond the scope of the general methods which NDSolve provides.

On the other hand, if the discontinuity was unintended, say in this example by simply choosing a computational domain which was too small, it can usually be fixed easily enough by extending the domain or by adding in terms to smooth things between periods.

This solves the sine-Gordon problem on a computational domain large enough so that the discontinuity in the initial condition is negligible compared to the error allowed by the default tolerances.

In[5]:= 

Out[6]=


Any questions about topics on this page? Click here to get an individual response.Buy NowFree TrialMore Information



 © 2009 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: