Sunteți pe pagina 1din 20

University of Exeter

ECMM713: Modelling Applications and Case Studies

Numerical Advection Schemes in Two Dimensions


Author: Hugo Winter Supervisor: Prof. John Thuburn

April 26, 2011

1 INTRODUCTION

Introduction

Computational models lie at the heart of forecasting in the 21st century. The process of forecasting is much more than numerical weather prediction alone, yet without it the whole system would collapse. With these models it has become possible to predict on wide ranging time-scales, from the weather outside tomorrow to the level of greenhouse gases in the atmosphere in hundreds of years time. Such important applications have necessitated the development of more and more complicated and ingenious schemes to try and model the dynamics of the climate system. It is this development that has progressed numerical weather prediction forwards which makes this a signicant and interesting area to research. The science of uid dynamics revolves around the idea of uid particles being transported in time and space. This can occur in many types of media from water to air and is known as advection. This process is very important when understanding the atmosphere and as such necessary when attempting to predict the weather (Holton 2004). Without a good understanding of the advective processes at work it would be near impossible to give a prediction on the weather a day in advance, let alone a week in advance. Due to the increase in computing power in recent years it has become possible to create complicated algorithms which model this advection process. As such, many dierent numerical advection algorithms have sprung up giving much scope for model choice (Rood 1987). Many of these models have originated in dierent branches of science, some have come from plasma physics others from oceanography, but most can be used to model the advective processes in the atmosphere. With such choice on oer it therefore becomes paramount to choose the correct algorithm for the situation. Factors such as computing cost and accuracy needed have to be taken into account. Problems with the stability of certain schemes also contribute to their utility. There is little point in running a global climate model for several years if the results have blown up after a week. On the other hand, an accurate and stable scheme that takes ve days to predict the weather tomorrow is also of little use. Many of the so-called classical advection schemes work well on standard grids with constant velocity of the ow, but may not be able to model more complicated features of the ow. On the other hand more complicated schemes that take into account these factors may simply be too expensive for all but a large supercomputer (Staniforth and C ote 1991). The aim of this report is to look at the two-dimensional (2D) advection equation. Two-dimensional elds produce interesting problems for advec1

2 THE ADVECTION EQUATION

tion schemes. Stability properties change compared to the one-dimensional equation and there is the possibility of advecting at dierent speeds in different directions. Certain schemes can be directly extended from the onedimensional (1D) scheme, whereas others might require each step to be split in two orthogonal directions (LeVeque 2002).

2
2.1

The advection equation


Deriving the advection equation

In a xed volume of uid which does not contain any sources or sinks, the rate of change of a constituent is equal to the amount transported in and out of the xed volume (Rood 1987). This gives the constituent continuity equation DC C + u C Dt t Another important quantity is the mixing ratio. It is dened below C

When the two equations above are combined, we can rewrite the continuity equation as, ( ) D = + u = + u Dt t t However, now the right hand side of the continuity equation is just the mass continuity equation for the uid which is equal to zero so nally the advection equation can be written + u = 0 t For the purpose of this report we are going to work with the two-dimensional advection equation. If we take this case and assume the velocity of the ow in the x and y directions to be positive and constant the above equation can be rewritten +u +v =0 t x y 2

(1)

2.2 Taylor Series

2 THE ADVECTION EQUATION

For this equation an analytical solution is known. Having an analytical solution allows us to see how well our transport scheme is doing in comparison. If a scheme has good properties it can then be extended to more complicated cases. To create our dierent schemes for solving the advection equation it is rst important to dene a Taylor Series.

2.2

Taylor Series

The Taylor series expansion provides a method with which the advection equation can be solved computationally. The point at (i + 1, j ) can be expressed as a Taylor Series di,j x2 d2 i,j + + O(x3 ) dx 2 dx2

i+1,j = i,j + x

(2)

This Taylor series has been expanded around the point (i, j ) although it can be used to expand around any point. The point at (i 1, j ) can also be written as a Taylor Series expanded about the point (i, j ) di,j x2 d2 i,j + O(x3 ) dx 2 dx2

i1,j = i,j x

(3)

The O(xn ) term at the end of the expression is called the truncation error of our nite dierence approximation (Tu et al. 2008). Ideally we would like the truncation error to be of as small order as possible. However, it is important to balance out the added accuracy with the extra complexity that the additional terms provide. Two fundamental schemes are the forward dierence scheme (FTBS) and the centred dierence scheme (CTCS). Both of these schemes have benets and drawbacks in terms of errors and stability. These schemes are cheap in terms of computational cost, however FTBS is diusive and CTCS is dispersive (discussed in section 2.3) and both produce large truncation errors. Using the Taylor expansion (2) (expanded with respect to y and t also) it is possible to rewrite the advection equation (1) to obtain the forward dierence scheme
+1 m m m m m m i,j i,j i+1,j i,j i,j +1 i,j = u v + O(x, y, t) t x y

Which can be rearranged to give 3

2.3 Errors and Stability

2 THE ADVECTION EQUATION

+1 m m m m m = m i,j U (i+1,j i,j ) V (i,j +1 i,j ) + O (x, y, t) i,j

(4)

Where U and V are Courant numbers which are dened as x , t y t

U =u

V =v

From the denition of the scheme in equation (4), it can be seen that the scheme has rst order error. The scheme only requires the points upwind of (i, j ) to make the step and does not require information from any previous time steps. By taking equation (1) minus equation (2) we can then obtain the centred dierence scheme
+1 1 m m m m m m i,j i,j i+1,j i1,j i,j +1 i,j 1 = u v + O(x2 , y 2 , t2 ) 2t 2x 2y

Which can be rewritten as


+1 1 m m m 2 2 2 m = m U (m i+1,j i1,j ) V (i,j +1 i,j 1 ) + O (x , y , t ) (5) i,j i,j

Where the Courant numbers are dened in the same way as above. This scheme has an error of the second order which is an improvement on the forward dierence scheme. To obtain this extra accuracy, points either side of (i, j ) and values from the previous time step are also required. When using an advection scheme we want the errors to be as small as possible. However, for nite dierence methods the validity of the model is determined more by its stability, a process closely linked to the Courant number dened above.

2.3

Errors and Stability

When modelling an advective process it is important to know the type of errors that might occur. A perfect numerical advection scheme would transport a uid particle in space, returning it to exactly the same place that it started. However when truncating the Taylor Series errors are introduced into the system. Roughly these errors split into two dierent types, dispersion and diusion (Rood 1987). Dispersion errors occur when a scheme introduces small-scale waves, as dierent Fourier components propagate at 4

3 ADVECTING IN TWO DIMENSIONS

dierent phase speeds. These type of errors are most commonly seen on centred dierence schemes. Forward dierencing suers from diusion errors, which are problematic when dealing with shock fronts. Any rectangular distributions will be smoothed and eventually all types of distribution will be dissipated across the eld. Many of the classical numerical methods (such as the ones discussed in this report) are designed to reduce one of these two types of error. Modern schemes attempt to use a combination of methods when solutions become too dispersive or to diusive (Van Leer 1974). It is also vital for a numerical advection scheme to be stable. An unstable scheme is of little use, if the solution is not bounded as time goes to innity we cannot have condence that it will produce accurate results for any useful length of time. The Courant number dened earlier usually needs to be bounded to within a certain set of values for a scheme to be stable. Using the example of FTBS again, in one dimension it turns out that for the scheme to be stable the Courant number U needs to be bounded 0 < U < 1. In two dimensions the calculations are slightly more complicated. The Courant numbers U and V may not be the same and the grid spacing could be dierent in the x and y directions. Even if the grid spacing is the same, not all 1D advection schemes can be simply extended into two dimensions. To investigate the stability we shall use Von Neumann stability analysis. For this type of analysis to work the scheme has to be linear and the coecients must be constant. These conditions are satised if the advecting velocities u and v are set to be constants.

3
3.1

Advecting in two dimensions


Direct extension

In one dimension, for the centred dierence scheme to be stable it turns out that the Courant number is bounded by the value 1 from above. In two dimensions it is possible to extend this scheme directly from the onedimensional case, although the values for the Courant number for which the scheme is stable will change. As mentioned above we shall use Von Neumann stability and this requires us to seek solutions of the form
m m i,j = A exp {i(knx + lj y )}

(6)

Where k and l are the wave numbers in the x and y directions respectively.

3.1 Direct extension

3 ADVECTING IN TWO DIMENSIONS

Now substituting equation (6) into equation (7) we obtain after some rearrangement A = i (U sin(k x) + V sin(ly )) [1 (U sin(k x) + V sin(ly ))]1/2 For stability we need that |A| 1. This holds if |U sin(k x) + V sin(ly )| 1 (7)

At this point we shall consider the simple case of a square grid x = y = q . This permits the velocities u and v to be dened in terms of a specic ow speed (fs ) u = fs cos(), v = fs sin() In this way equation (7) becomes fs t |cos()sin(kq ) + sin()sin(lq )| 1 q (9) (8)

For this to be satised for all waves in the 2D plane it serves to prove it for the most rigorous case, when sin(kq ) = sin(lq ) = 1. This reduces equation (10) to fs t |cos() + sin()| 1 q (10)

We wish to look at the limiting case for which the ow is moving diagonally. This implies that = and the value of | cos( ) + sin( ) | = 2. So for the 4 two-dimensional case where we are on a square grid box and u and v are advecting with the same ow speed, this implies that fs t 1 q 2 (11)

The explanation behind this can be seen better in gure 1. With CTCS a wave signal cannot propagate more than one grid interval without becoming unstable. When combined with equations (8) and (9) the conditions on U and V are U 0.5, 6 V 0.5 (12)

3.2 Directional splitting

3 ADVECTING IN TWO DIMENSIONS

0.707d

Figure 1: 2D wave propagation

3.2

Directional splitting

It is not possible to extend all numerical advection methods into two dimensions quite so easily. An interesting example is the Lax-Wendro method. In one dimension the method is dened as U m U2 m m 2 2 (i+1 m ) + ( 2m i1 i + i1 ) + O (x , t ) 2 2 i+1

+1 m = m i i

t where U is the 1D Courant number u . For this case the Courant number is x stable between 1 and 1. However if we just add on the additional terms for the y-direction the scheme becomes completely unstable. The explanation for this lies within the derivation of the Lax-Wendro method. The starting point for the derivation is the Taylor series of about t

(x, y, t t) = (x, y, t) t

(x, y, t) t2 2 (x, y, t) + + ... t 2 t2

(13)

Using equation (1) each of the derivatives with respect to t can be written in terms of x and y . For notational simplicity we shall use the convention that (x,y,t) can be rewritten t . As such we have that t t = ux vy tt = u2 xx v 2 yy + 2uvxy (14) (15)

Substituting in the formulae for the rst and second derivatives of secondorder centred dierences (Rood 1987)

3.2 Directional splitting

3 ADVECTING IN TWO DIMENSIONS

x = xx

i+1,j i1,j 2x i+1,j 2i,j + i1,j = x2

(16) (17)

we obtain the fully 2D Lax-Wendro scheme V m U m m (i+1,j m ( i1,j ) i,j +1 ) 2 2 i,j +1 U2 V2 m m m m (m (i,j +1 2m i+1,j 2i,j + i1,j ) i,j + i,j 1 ) 2 2 UV m m m + (i+1,j +1 i1,j +1 i+1,j 1 + m i1,j 1 ) 4

+1 m = m i,j i,j

As can be seen from the above equation there is a cross derivative term in the fully 2D Lax-Wendro scheme. However, if the 1D scheme is simply extended this term is completely omitted, hence the direct extension method becomes completely unstable. One method that can be used to remedy this problem is to use directional splitting. This method consists of breaking a nite-dierence formula into a series of steps (Durran 1999). In the 2D problem that we have been using, it corresponds to taking the x step rst followed by a y step. So the fully 2D Lax-Wendro equation can be rewritten, using dierential operator notation (see appendix I), as (ut)2 xx m i,j 2 (v t)2 m = (m yy (m i,j ) v t2y (i,j ) + i,j ) 2

+1 m (m = m i,j ut2x i,j + i,j ) +1 m i,j

(18) (19)

The main benet of this method is that fewer computations are required to solve the split system, thus reducing computational cost. It also relies on the only the 1D form of Lax-Wendro which is a scheme that is well understood. The stability restrictions on the Courant number are also much less severe than for fully 2D schemes. Here, each of the steps is bounded by the stability condition for the 1D Courant number (1 U 1). One major drawback is the necessity to choose orthogonal steps for this method to work. As such we are restricted in the types of grid that can be chosen for the 2D problem. The choice of steps is interesting and there are two main options here. Figure 2(a) shows splitting dened in equations (18) and (19). 8

3.3 Fully 2D schemes

3 ADVECTING IN TWO DIMENSIONS

(a) Splitting from equations (18) and (19)

(b) Strang splitting

Figure 2: Dierent types of directional splitting On the other hand, gure 2(b) shows Strang splitting which takes one step of the previous method and then reverses it for one step. It may be intuitive that the second method would give a more accurate solution, however the dierence is not always that great (LeVeque 2002). The splitting of the problem could also lead to some loss of accuracy and spurious error terms of O(t)2 . It is very dicult to know in advance whether satisfactory results will be obtained using directional splitting (Kuzmin and L ohner 2005). As such we have to be careful when using this method, implying that a more robust fully 2D scheme is required.

3.3

Fully 2D schemes

The methods above have aimed to take schemes and extend them using the 1D form of the advection equation. There are benets to this, the 1D advection equation is well known and many of the methods are relatively simple. On the other hand there will be some loss in accuracy since we are trying to use 1D schemes to approximate what is a fully 2D process. As such it would be benecial to have a fully 2D scheme to use, which should be the most accurate scheme that we can create. In the previous section we dened the 2D Lax-Wendro scheme to illustrate the cross-derivative term that appears. Implementing this directly should hopefully give the most accurate representation and reduce the errors to their minimum. The stability condition for the fully 2D method is dened as U 2/3 + V 2/3 1 9 (20)

4 SIMULATION STUDY

This condition is very restrictive, much more so than that for CTCS and the directional split Lax-Wendro method. It is possible to improve the stability if a dierent approximation to the mixed spatial derivative term in the fully 2D Lax-Wendro scheme is used. If u 0 and v 0 we must replace the cross-derivative term (Durran 1999) U V t2x 2y m i,j with the term U V tx y m i 1 ,j 1
2 2

(21)

(22)

The change in the term means that the fully 2D Lax-Wendro scheme is stable for 0 U 1 and 0 V 1. This adaptation makes the scheme more exible and as such is the best scheme to illustrate the fully 2D LaxWendro scheme during my simulation study.

Simulation study

Each of the methods which have been set out so far have their strengths and weaknesses. To test which of these methods is better I have set up a simulation study, which has been undertaken using the mathematical program Matlab. Three schemes shall be under scrutiny in this section, CTCS, LaxWendro with directional splitting and the fully 2D Lax-Wendro scheme. For the directional split I have used the methodology outlined in equations (18) and (19) since there is no dierence between the results obtained by this method and the Strang splitting. Intuition suggests that both Lax-Wendro schemes should be superior to CTCS, with the added robustness of the 2D Lax-Wendro scheme making it the best for all situations. The study will consist of dierent initial conditions being set up on a box of size 0 x 1, 0 y 1. Within this box it is possible to set up a grid mesh, for the purpose of this study I shall keep to a square grid mesh. Each scheme will then be used to transport the initial condition around the box and back to the original place that it started from. At this point it is possible to quantify the error in terms of the deviation from the initial condition. A perfect scheme would bring the initial condition back around to the same place after any number of revolutions. The 2-norm and innity norm are two ways to quantify the error and are dened as 10

4 SIMULATION STUDY

1 N N 2 = | num ex i,j | N i=1 j =1 i,j = Maxi,j | num ex i,j i,j |

)1/2 (23) (24)

where num is the numerical solution and ex i,j i,j is the exact solution. The 2-norm takes into account the errors from all over the plane whereas the innity norm is only concerned with the one point where the largest error occurred. To build an accurate picture of the accuracy of each method it is important to provide both types of error. One scheme may create large errors on the wave being advected but small errors across the rest of the plane which leads to a small 2-norm, however this scheme may not necessarily be any better. The simulation will also take into account two dierent types of initial condition. A curved wave such as that created by a Gaussian distribution will be the rst type tested and it would be expected that the errors for this initial condition would be relatively small. There are no sudden changes from one grid point to another which can strongly aect the error. Secondly advecting a square wave will test how well each of the schemes deal with shock fronts. These type of waves can be the most dicult schemes to advect accurately, especially for even order schemes such as the ones being tested since they are dispersive. It is important to note that during this study the ow will be advecting with a constant velocity in both directions. All the equations that have been derived up until this point are done so with this assumption in mind. The grid mesh will also be set to a 100x100 grid so that the Courant number can only be varied by increasing or decreasing the ow velocity or time step. Table 1: Error results for Gaussian wave U = V = 0.5 2 Error Error CTCS 0.0038 0.0214 DSLW 0.0042 0.0274 2DLW 0.0054 0.0343 The error results for the Gaussian wave are very interesting since they seem to suggest that the CTCS scheme is best when it comes to reducing the error. CTCS is a dispersive scheme with no diusion, but the diusive nature of the wave being advected helps to reduce the error. The error results for the 11

4 SIMULATION STUDY

1 0.9 0.8 0.7 0.6 y 0.5 0.4

1.5

1 0.9

1.2

0.8 0.8 0.7

0.5 y

0.6 0.5 0.4

0.6

0.4

0 0.3 0.2 0.5 0.1 0.1 0.2 0.3 0.4 0.5 x 0.6 0.7 0.8 0.9 1 0.1 0.1 0.2 0.3 0.4 0.5 x 0.6 0.7 0.8 0.9 1 0.3 0.2

0.2

0.2

(a) CTCS
1 0.9 0.8 0.8 0.7 0.6 y 0.5 0.4 0.2 0.3 0.2 0.1 0.1 0.2 0.3 0.4 0.5 x 0.6 0.7 0.8 0.9 1 0 0.6 y 1.2 1

(b) Directional split Lax-Wendro


1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.1 0.2 0.3 0.4 0.5 x 0.6 0.7 0.8 0.9 1 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0

0.4

0.2

(c) Fully 2D Lax-Wendro

(d) True Solution

Figure 3: Square wave advected once around plane U = V = 0.5 Lax-Wendro schemes are close to that of CTCS, but introduce slightly more error. The directional split Lax-Wendro scheme also appears to be reducing the error better than the fully 2D Lax-Wendro. This is a surprising result since it would be expected that a fully 2D scheme would work better when advecting in two dimensions. The results obtained in table 2 and gure 3 show what occurs for a square wave. The wave has again been advected once around the plane and with Courant numbers U = V = 0.5. The dispersive CTCS scheme creates large errors when transporting a square wave since the scheme is quickly dominated by shortwaves.The diusion of the Lax-Wendro method controls this dispersive eect. For this reason the Lax-Wendro method is far superior to the CTCS for carrying the square wave. It is again signicant that the directional split Lax-Wendro method produces less error than the fully 2D Lax-Wendro. This pattern suggests that the tests being performed with constant ow velocities do not create any spurious errors in the directional 12

4 SIMULATION STUDY

0.2 0.18 0.16 0.14 CTCS TSLW 2DLW

1.5

CTCS TSLW 2DLW

1 IninityError 0.5 0.06 0.04 0.02 0 0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 Courant Number 0.7 0.8 0.9 1 0.12 2Error 0.1 0.08

0.2

0.3

0.4

0.5 0.6 Courant Number

0.7

0.8

0.9

(a) 2-error

(b) -error

Figure 4: How errors vary with courant number split method. In 3(a), peaks and troughs between 0.5 and 0.5 in the eld away from the initial condition (gure 3(d)) show that CTCS is a more dispersive scheme. This result compares to peaks and troughs of just between 0.2 and 0.2 for the Lax-Wendro methods. Figures 3(b) and 3(c) suggest that the square wave is being advected in a similar manner by both LaxWendro schemes, but it is the rest of the eld that is having an eect on the error values. The fully 2D scheme seems to produce more and slightly stronger dispersive waves in its wake, whereas the dispersion created by the directional split Lax-Wendro is weaker. So far we have set the Courant number to a certain rate and looked at how the dierent schemes perform. A situation may occur where for certain Courant numbers, dierent schemes may be better. As such gure 4 looks at how the error varies with dierent Courant numbers. For this graph the square wave from gure 3 was chosen with U = V . The graphs show that the directional split Lax-Wendro method reduces the error for all Courant numbers compared to its fully two dimensional counterpart. The error of the CTCS is smaller for certain Courant numbers than the fully 2D Lax-Wendro scheme. Physically, the change in Courant number represents the size of the time-step being taken (when the grid spacing and velocities have been xed). The reduction in time step improves the CTCS greatly since it requires three-time levels. Conversely, as the time-step is increased, the undamped computational mode of CTCS generates instability (timesplitting). Figure 4 shows that stability becomes a problem when U and V are greater that 0.5 (equation (12)).

13

5 CONCLUSION

Table 2: Error results for square wave U = V = 0.5 2 Error Error CTCS 0.1977 1.3888 DSLW 0.1073 0.7911 2DLW 0.1170 0.8050

Conclusion

The aim of this project was to take the well known one-dimensional advection equation and extend it into two-dimensions. By comparing three dierent second order schemes, the dierent processes behind model extension have been discussed. Schemes such as CTCS can be extended straight from their 1D counterparts and remain stable, although under restricted stability conditions. The Lax-Wendro scheme on the other hand is an example of a scheme that becomes totally unstable if the 1D case is simply extended. To overcome this it is either necessary to split directionally or derive a fully twodimensional scheme. These schemes have the benet of less strict stability conditions which make them more useful. To test the theory, Matlab was used to construct simulations of an advecting uid. Analysis of the errors produced by each of the dierent schemes allows the accuracy of each of them to be quantied. For a Gaussian wave it turned out that CTCS was a very good scheme to use which performed better than both of the Lax-Wendro methods. On the other hand, for square waves the small amount of dissipation added by the Lax-Wendro scheme made it a better scheme to use. One interesting result was the fact that the directional split scheme worked better than the fully two-dimensional scheme. An extension to this study would be to see if this holds for nonconstant advecting velocities. I would expect that the fully 2D scheme would be better when advecting with these non-constant velocities or maybe with dierent grid shapes. The application of advection schemes within numerical weather prediction continues to be very important to modern weather forecasting. If one thing has become apparent over this project is that there is such a large amount of schemes available for the user. The schemes that I have studied here may not be the ones that are being used for modern applications, but they are still important. They highlight where numerical weather prediction has come from and the rapid growth suggests that it can improve many years into the future. 14

REFERENCES

REFERENCES

Appendix I
Finite-dierence operator notation is used to write formulae in compact form. Necessary combinations of x and y directions can be made from the following (Durran 1999)
m m i+1,j i1,j = 2x m m m i+1,j 2i,j i1,j = (x)2

2x m i,j
2 m x i,j

References
[1] Durran, D.R., Numerical Methods for Wave Equations in Geophysical Fluid Dynamics, Springer, 1999. [2] Holton, J.R., An introduction to dynamic meteorology, 4th ed., Elsevier, 2004. [3] Kuzmin, D., L ohner, R., Flux-corrected transport: principles, algorithms, and applications, Springer, 2005. [4] LeVeque, R.J., Finite volume methods for hyperbolic problems, Cambridge, 2002. [5] Rood, R.B., 1987, Numerical Advection Algorithms and Their Role in Atmospheric Transport and Chemistry Models, Review of Geophysics, 25, 71-100. [6] Staniforth, A. and C ote, J., 1991, Semi-Lagrangian Integration Schemes for Atmospheric Models - A Review, Monthly Weather Review, 119, 22062223. [7] Tu, J., Yeoh, G.H. and Liu., C., Computational Fluid Dynamics: A Practical Approach, Butterworth-Heinemann, 2008. [8] Van Leer, B., 1974, Towards the Ultimate Conservative Dierence Scheme. II. Monotonicity and Conservation Combined in a Second-Order Scheme, Journal of Computational Physics, 14, 361-370.

15

REFERENCES

REFERENCES

Appendix II
Matlab code used for simulation study in section 4. % Domain size (periodic) l = 1.0; % Number of grid points in domain n = 100; % Grid dx = l/n; dy = 1/n; [x,y] = meshgrid(linspace(dx,l,n),linspace(dy,l,n)); % Advecting velocity u = 1.0; v = 1.0; % Time step dt = input(Timestep > ); % Courant number cu = u*dt/dx cv = v*dt/dy % Number of steps to take in order to go % once around nstop = ceil(n/cu); for k = 1:n for j = 1:n xx = x(k,j); yy = y(k,j); % Gaussian wave initial condition f(k,j) = (1/(2*pi*0.4*0.4))*exp(-(1/2)* (((xx-0.5)^2)/(0.1)^2 + ((yy-0.5)^2)/(0.1)^2)); % Square wave initial condition if (xx+yy >= 0.4 & xx+yy < 0.8 & yy >= xx-0.3 & yy < 0.3+xx) f(k,j) = 1.0; else f(k,j) = 0.0; 16

REFERENCES

REFERENCES

end end end h = f; contourf(x,y,f) scheme = input( 1. CTCS \n 2. Lax-Wendroff \n 3. Lax-Wendroff with time splitting \n 4. Alternate split LW \n 5. Fully 2D LW \n Which scheme? ); % Is user ready?hh ready = input( Ready ? ); % Set up arrays of indices k, k-1 and k+1, % but allowing for wrap-around (periodic domain) k = 1:n; km = k - 1; km(1) = n; kp = k + 1; kp(n) = 1; p = 1:n; pm = p - 1; pm(1) = n; pp = p + 1; pp(n) = 1; % One step needed if using CTCS if scheme == 1 d(k,p) = f(k,p); end % Loop over time steps for istep = 1:nstop % CTCS if scheme == 1 g(k,p) = d(k,p) - cu * (f(kp,p) - f(km,p)) - cv * (f(k,pp) - f(k,pm)); d(k,p) = f(k,p); f(k,p) = g(k,p);

17

REFERENCES

REFERENCES

end % Lax-Wendroff scheme if scheme == 2 f(k,p) = f(k,p) - (cu/2)*(f(kp,p) - f(km,p)) + ((cu^2)/2)*(f(kp,p)-(2*f(k,p))+f(km,p)) -(cv/2)*(f(k,pp) - f(k,pm)) + ((cv^2)/2)*(f(k,pp)-2*f(k,p)+f(k,pm)); end % Splitting for Lax-Wendroff if scheme == 3 f(k,p) = f(k,p) - (cu/2)*(f(kp,p) - f(km,p)) + ((cu^2)/2)*(f(kp,p)-2*f(k,p)+f(km,p)); f(k,p) = f(k,p) - (cv/2)*(f(k,pp) - f(k,pm)) + ((cv^2)/2)*(f(k,pp)-2*f(k,p)+f(k,pm)); end % Strang split if scheme == 4 if mod(istep,2)==0 f(k,p) = f(k,p) - (cu/2)*(f(kp,p) - f(km,p)) + ((cu^2)/2)*(f(kp,p)-2*f(k,p)+f(km,p)); f(k,p) = f(k,p) - (cv/2)*(f(k,pp) - f(k,pm)) + ((cv^2)/2)*(f(k,pp)-2*f(k,p)+f(k,pm)); else f(k,p) = f(k,p) - (cv/2)*(f(k,pp) - f(k,pm)) + ((cv^2)/2)*(f(k,pp)-2*f(k,p)+f(k,pm)); f(k,p) = f(k,p) - (cu/2)*(f(kp,p) - f(km,p)) + ((cu^2)/2)*(f(kp,p)-2*f(k,p)+f(km,p)); end end % Fully 2D LW if scheme == 5 fiplusm(k,p) = (1/2)*(f(k,p)+f(kp,p)) (1/2)*cu*(f(kp,p)-f(k,p)); fjplusm(k,p) = (1/2)*(f(k,p)+f(k,pp)) -

18

REFERENCES

REFERENCES

(1/2)*cv*(f(k,pp)-f(k,p)); fiminusm(k,p) = (1/2)*(f(km,p)+f(k,p)) (1/2)*cu*(f(k,p)-f(km,p)); fjminusm(k,p) = (1/2)*(f(k,pm)+f(k,p)) (1/2)*cv*(f(k,p)-f(k,pm)); f(k,p) = f(k,p) - cu*(fiplusm(k,p)-fiminusm(k,p)) cv*(fjplusm(k,p)-fjminusm(k,p)) + cu*cv*(f(k,p)-f(k,pm)-f(km,p)+f(km,pm)); end % Plot f at this timestep contourf(x,y,f) pause(0.1) end err = abs(f - h); rss = sum(sum(err.^2)); rss3 = sum(sum(err.^3)); error2 = (rss/(n^2))^(1/2); error3 = (rss3/(n^2))^(1/3); errorinf = max(max(err)); errorstuff = [error2;error3;errorinf] contourf(x,y,f); xlabel(x) ylabel(y)

19

S-ar putea să vă placă și