You are on page 1of 11

Modelling 2

Chen Zhi Shen


Hui Huang Law
Dexter Gauge
December 8, 2017

1 Introduction
Numerical methods have been √ used even before computers existed. They were used by the ancient Greeks
to calculate things such as 2. Today they are used to find approximate but accurate solutions to many
difficult problems. In this report, numerical methods will be used to find solutions to three problems
which are a heat flow system, a trampoline and a fluid flow over cylinders.
A general approach to modelling such systems would be through Laplace’s equation or Poisson’s
equation. Although Laplace’s equation underlies these systems, the boundary conditions specified in
each situations makes each solution unique.
Iterative methods are used to progressively reduce the error in the overall plot to a set tolerance. A
stencil takes the average of the values around a grid point.
When the data set is large, iterative methods on its own would be slow. In this modelling project,
successive over relaxation and multigrid methods were used to speed up convergence.

2 Algorithm and implementation


1. Input values are specified by the user. ( If the answer is diverged, please change the over-Relaxation
parameter to 1 or lower it from the default value.)
2. Input values are checked for validity. (e.g. Grid resolution, tolerances, position of circles)
3. Grid resolution is adjusted to allow for multigrid method. Grid resolution in the x and y direction
is doubled after each multigrid iteration.
4. Default input values are used if user doesn’t specify them. (e.g. Tolerances, grid spacing) Physical
constants like grid length, grid width and radius of circle or cylinder are set.
5. Pre-allocation of old and new arrays.
6. Coordinates at specific points are identified. (Position of cylinder or circles.)
7. Values on the various boundaries are set according to the specified problem.
8. Calculate the updated values of all points in the domain, using finite difference method.
9. Values of elements found in step 6 are adjusted according the given conditions. This is done in the
new array.
10. The convergence is estimated by finding the difference between the old and new array.
11. New array is copied to the old array for next iteration. Step 8 to step 11 is repeated until convergence
is fulfilled.
12. If we achieve tolerated convergence, return to step 7. Otherwise, proceed to next step.
13. Elements in old array is transferred into a larger intermediate array 4 times its size.
14. Each element in the old array is duplicated 4 times to fill up the empty spaces in the intermediate
array.
15. Intermediate array is then transferred to the old array.
16. Step 3 to step 15 is repeated until the grid resolution reaches the user specified resolution.
17. The final solution goes through 100 Jacobi iterations to eliminate discontinuities.
18. Plot the function of the converged distribution and the converged plot vs iteration count.

1
3 User interface and user input
The graphical user interface is started by running the ‘Guide.m’ file. After selecting the desired question,
an input dialog appears. The user is able to specify the changes in the input variables. If the user chooses
not do change anything, default values are used.
Input values by the user are checked if they lie within the given parameters of the question. For
example, if a non-numerical number is inserted, an error dialog will appear to remind the user of his
mistake. User inputs that do not lie within the boundaries of the grid are also rejected. If the user
chooses to close the dialog boxes before entering anything, errors do not appear as they are caught.

4 Methods tried
4.1 Vectorized Jacobi Method
The Jacobi method iterative equation is:

k+1
(∆y 2 (Ti+1,j
k k
+ Ti−1,j ) + ∆x2 (Ti,j+1
k k
+ Ti,j−1 ))
Ti,j = 2 2
(1)
2(∆x + ∆y )

The requirement for such an equation to be vectorized is that all the values at RHS must be non changing
in the process of an iteration. This is true as the time levels of the terms at RHS are the same. Vector-
ization eliminates the requirement of for loop as given in the example code, because for loops are very
slow in Matlab. The speed difference is shown in table 1.

Method Time(s) Iterations


For loop Jacobi Method 38.3059 78715
Vectorized Jacobi Method 71.0318 78715

Table 1: Iterations and time taken for different methods

4.2 Matrix Coefficient


Using a (nx × ny) × (nx × ny) matrix, E to contain the coefficients required to multiply a 1 ×(nx ×
ny) array, T k containing T1k to Tnx×ny
k
to get a 1 × (nx × ny) new array, T k+1 . T k+1 is then substituted
k
back into T and the process repeats until the answer converges to the required tolerance. The original
equation is :
k+1
(∆y 2 (Ti+1,j
k k
+ Ti−1,j ) + ∆x2 (Ti,j+1
k k
+ Ti,j−1 ))
Ti,j = 2 2
(2)
2(∆x + ∆y )
Or separately:
k+1 (∆y 2 (T3,2
k k
+ T1,2 ) + ∆x2 (T2,3
k k
+ T2,1 ))
T2,2 = (3)
2(∆x2 + ∆y 2 )

k+1 (∆y 2 (T3,3


k k
+ T1,3 ) + ∆x2 (T2,4
k k
+ T2,2 ))
T2,3 = (4)
2(∆x2 + ∆y 2 )
∆y 2 ∆x2
and so on. Substituting 2(∆x2 +∆y 2 ) as A and 2(∆x2 +∆y 2 ) as B, the matrix should look like

Figure 1: Matrix concept

2
This method converts T k and T k+1 into linear indexing array, the constant coefficients into a matrix.
The matrix, E has :
∆y 2
1. Constants 2(∆x2 +∆y 2 ) at diagonals -ny+1 and ny-1

∆x2
2. Constants 2(∆x2 +∆y 2 ) at diagonals -1 and 1

3. Constants 1 at diagonal elements where the boundary condition of T=100 and T=0 applies.

The new equation is: ET k = T k+1 . The matrix E is made in Matlab using diag function and is shown
in Figure 2. ˜ ˜

Figure 2: Matrix concept implemented in Matlab on a 80 × 40 grid

This method is extremely slow as a 3200x3200 matrix is very big, it has to do a lot of unnecessary
calculation as most of the matrix are zeros. It only ran 6252 times in 47.81 seconds. Even with Matlab
function sparse matrix, which ignores the zeros, it is still slower than normal Jacobi method, hence it is
not employed in the code.

4.3 Gauss-Seidel Method and Succesive over-Relaxation Method


The Gauss-Seidel method (GS) uses a slightly different equation to the Jacobi method to solve finite
difference equations [1]. The equation used is :
h ∆y 2 (T k k+1 k+1 i
k+1 i+1,j + Ti−1,j ) + ∆x2 (Ti,j+1
k
+ Ti,j−1 )
Ti,j = 2 2
(5)
2(∆x + ∆y )

Notice the RHS of the equation uses values from time values of k+1 which is different from the explicit
solution method. This means that this method uses updated solution in the same iteration. In the same
iteration, the solution calculated in the previous data block will be used to calculate the present solution
which is not the case in the Jacobi method where in the same iteration, all values used to calculate the
present solution are from the previous iteration. This can be visualized easier with figure 3 where the
k+1
values used to calculate Ti,j are from surrounding 4 values.

(a) (b)

Figure 3: (a) Jacobi method visualized (b) Gauss Seidel method visualized

3
Assuming the data is calculated top row to bottom row from left to right, GS uses solution calculated on
top and at the left in the current iteration and solution calculated at bottom and right from the previous
iteration. Now that the Gauss-Seidel equation is understood, the Successive over-Relaxation equation
(SOR) uses a slightly different equation than the Gauss-Seidel method, it has another coefficient, α,
known as relaxation parameter as shown.
h ∆y 2 (T k k+1 k+1
k+1 k i+1,j + Ti−1,j ) + ∆x2 (Ti,j+1
k
+ Ti,j−1 ) k
i
Ti,j = Ti,j +α − Ti,j (6)
2(∆x2 + ∆y 2 )

Note that the Gauss-Seidel equation is actually just Successive over-Relaxation but with α=1. This
equation can be intepreted as the solution for a point in the current iteration is the solution of that point
from the previous iteration adding a ‘correction’, the difference between the GS solution and the solution
from the previous iteration, which is multiplied by α. The main advantage of SOR and GS compared to
the Jacobi method is the data signal travels faster across the grid in one iteration so answers converge
faster. This is because SOR and GS uses solution in the present iteration which is closer to the end
answer, the solution calculated will too be closer to the end answer. Theoretically, relaxation parameters
takes value only between 0 and 2 without divergence of solution, relaxation parameters that are less than
1 are under-relaxed and reducing speed of convergence.[2] In SOR, as alpha increases, the correction
becomes larger hence the solution converges to the end answer with less iterations until a optimal alpha
value where more iterations are needed after that optimal alpha. If alpha is too large, the solution will
actually diverge from the end answer. To demonstrate this, a code is written with reference to a Matlab
code [4] to show a plot of iteration counts vs alpha by solving the default thermodynamic question with
alpha values from 1.985 to 1.993 by 0.001 steps as shown in figure 4.

6700

6600

6500
Iteration count

6400

6300

6200

6100
X: 1.991
6000 Y: 6053

1.985 1.986 1.987 1.988 1.989 1.99 1.991 1.992 1.993 1.994
Alpha

Figure 4: Plot of iteration count vs alpha

It can be seen that when α=1.991, the iteration required to converge to the end answer is 6053 which
is the lowest among the three relaxation methods. Generally, SOR is faster than GS, as the α accelerates
the calculation. The general shape of the answer between two of them are similar but there are major
differences at the boundary of circles in SOR due to α value. The difference in default question 1 is shown
in Figure 5. Note that the discontinuity is not eliminated fully in GS but minimized.

4
(a) (b)

Figure 5: (a) SOR answer (b) GS answer

There are disadvantages of SOR and GS compared to Jacobi method:

1. It uses for loops which limits its potential as for loops in Matlab are slow.
2. It produces discontinuities at the boundary of circles.
3. The relaxation parameter, α differs with different grid resolution which might cause adverse effect on
the speed causing it to be slower than Jacobi method in some grid resolution. If the grid resolution
is not equal in the x direction and y direction, α drastically differs and even with values below 2, the
solution might diverge which led to an attempt to calculate the optimal Successive over-Relaxation
parameter.

4.3.1 Attempt to obtain optimal Succesive over-Relaxation parameter


Poisson’s and Laplace equation can be written as finite difference equations which can be written in a
matrix form , Aφk =φk+1 similar to the Matrix coefficient method described above. In order to have
an optimal α, the˜ Matrix
˜ A must have property A and consistent ordering. [3] Given that is true, the
optimal α is given by the equation:
2
αopt = 1 (7)
1 + (1 − µ2 ) 2
where µ is the maximum eigenvalue of the matrix A. This approach was tested with the default 1
dimensional code given as an example in Matlab. The iteration count of every α is recorded experimentally
and plotted (blue line) and compared with the theoretical optimal α value (red line) in different grid sizes
as shown in figure 6.

5
1800 3500

1600
3000
1400
Theoretical Optimal Alpha Theoretical Optimal Alpha
2500
Iteration count

Iteration count
1200

1000 2000

800
1500
600
1000
400

200 500
1.88 1.9 1.92 1.94 1.96 1.98 2 1.91 1.92 1.93 1.94 1.95 1.96 1.97 1.98 1.99
Alpha Alpha

(a) (b)

Figure 6: (a) Comparation for grid size 100 (b) Comparation for grid size 200

The results agree well with the theoretical value as the alpha required for the minimum iteration is
close to the theoretical optimal alpha. However, this method is not employed in the 2D problems as the
calculation of eigenvalues of (320 × 160) × (320 × 160) matrix is too big and not allowed in Matlab. The
method employed to determine α is through trial and error with multiple combination of grid resolutions x
and y and using a conditional statement to choose alpha depending on the combination of grid resolution.
This method is not perfect, so if the answer output is a diverged answer, please change α in the user
input to 1 as Gauss Seidel is definitely stable even though slower or lower α from the default value.

4.3.2 Red Black Ordering Successive Over-Relaxation


The Red Black Ordering (RBO) is a variation of SOR. Imagine the data matrix as a checkerboard with
squares containing the data in red and black alternating colour. First SOR is used to update the datas
in the black boxes using datas from their 4 surrounding red boxes, then the datas in the red boxes are
updated using datas from their 4 surrounding black boxes. Note that to calculate the black boxes, all
of the data used are from the previous iteration. However, to calculate the red boxes, all datas used are
from the intermediately updated black boxes hence giving a more accurate answer. This method removes
the requirement of for loops because during the calculation of the black boxes all values used are non
changing (from previous iteration) while for calculation of red boxes, all values are also non changing
(from current intermediate iteration). This means that the equation used will be slightly different, the
equation used to calculate the black boxes will be :
h ∆y 2 (T k k
+ Ti−1,j ) + ∆x2 (Ti,j+1
k k
+ Ti,j−1 )
k+ 21
i
k i+1,j k
Ti,j = Ti,j +α − Ti,j (8)
2(∆x2 + ∆y 2 )

The equation used to calculate the red boxes will be:


1 1 1 1
h ∆y 2 (T k+ 2 + T k+ 2 ) + ∆x2 (T k+ 2 + T k+ 2 ) i
k k i+1,j i−1,j i,j+1 i,j−1 k
Ti,j = Ti,j +α − Ti,j (9)
2(∆x2 + ∆y 2 )

The way this method is implemented into Mat-


lab is explained figuratively in Figure 7. The white
border in Figure 7 represents the boundary condi-
tion, a and b is calculated first and then updated
as the intermediate value, then the values in c and
d are calculated with the 4 surrounding intermedi-
ate values. For ease of reference, the actual Matlab
equation used is as shown in figure 8.
Figure 7: Checkerboard matrix

6
Figure 8: Matlab Red Black Ordering equation

Surprisingly, RBO minimizes the discontinuities in SOR too, just like GS, even with an optimal alpha
value. Nonetheless, the discontinuity will be corrected at the last step with 100 iterations of Jacobi
method. This method is the fastest hence it is employed in the code.

4.4 Multigrid Method


The preallocated matrix is a sparse matrix where only the initial boundary conditions have values. The
finite difference method used utilizes the average of 4 values around each grid point. It will take many
iterations to populate the sparse matrix because majority of values in the matrix are zeros from the start.
A better approach would be to resize the finest grid by halving the number of grid points in both
axis directions. This would result in 14 of the number of grid points in the matrix. The matrix can be
populated quicker with less iterations and the calculated values can be transferred back into the finer
grid. The gaps in between values in each direction are filled in two separate steps. The horizontal gaps
are filled before the vertical gaps. This process can be done multiple times to refine the calculated val-
ues before finally reaching the specified grid resolution. For example, a user specified grid resolution of
161×321 can be resized to 81×161, 41×81 and 21×41 using the multigrid method. Interestingly, the
duplication method produced significantly better results than the simple linear interpolation method for
all three questions as shown in Table 2.

Method Used Iterations Time taken (s)


Jacobi Method and simple linear 79747 21.29
interpolation method (multigrid)
Jacobi Method and 100089 28.45
duplication method (multigrid)
Red Black Ordering (SOR) 7318 6.27
interpolation method (multigrid)
Red Black Ordering (SOR) 7296 6.10
duplication method (multigrid)

Table 2: Time and iterations needed for different methods

Two methods were tested to fill in the intermediate values. This could be done by simple linear
interpolation or simply duplicating a single value four times as it is transferred into a finer grid. The
code is displayed in figure 9.

(a) Simple linear interpolation method (b) Duplication Method

Figure 9: Multigrid methods

7
5 Differences in implementation between problems
5.1 Thermodynamics
In this problem, the boundary conditions are given by the temperature of the plates at the top and bottom
corners. The rest of the edges of the rectangular grid have similar temperature as the next innermost
value.The position of the array elements that lie in the circle are found using the ‘find’ function along
with the general formula of ((x − xcentreof circle )2 + (y − ycentreof circle )2 ) ≤ radius. The mean of the
temperature values within the circle is calculated and applied throughout the circle to simulate the effects
of the superconducting fluid.

5.2 Fluid Dynamics


This problem concerns the calculation of stream functions of incompressible and steady flow in a channel
obstructed by cylinders. The important features of this problems are :
1. The inflow uses the formula ψ(y) = U∞ y, where y is 0 at the bottom left corner of the grid. This
means the boundary conditions are given by that the stream function at the bottom left corner
increases linearly to the top left corner of the grid with stream function value of 2.
2. The bottom edge’s stream function values are all 0.
3. The top edge’s stream function values are all 2.
4. The values at the right edge of the channel will be the same as the next inner values. This is because
it is enforced that there is no vertical velocity component at the right edge so δψ
δx =0.

5. The linearly indexed coordinates of the circle are obtained with self-written circle function(circle).
The value used in the cylinder follows the value of inflow velocity multiply the y coordinate of the
circle, the value will be assigned as a constant throughout the cylinder. This means that different
cylinder will have different constant value throughout.

5.3 Structural Dynamics


This problem concerns the calculation of displacement of a trampoline with a small rip when subjected
to the weight of a gymnast. The important features for this problem is :
1. The rip is conveniently modelled to be coincident with a mesh boundary. Since stress does not
travel across the rips, both the displacement adjacent to the rips should not affect each other and
each will take the value of the next inner displacement. The both ends of the rip will have the
same displacement which is the average of its original displacement. The behaviour of the rip is
explained figuratively as shown in figure 10 with the same naming used in the Matlab code.

Figure 10: Behaviour of rip in concept

The bolded line at the center is the rip coincident with the mesh boundary, the rip runs from
yripstart to yripend, with adjacent displacements xleft and xright. Notice that the displacements

8
adjacent to the rip are the same as the next inner displacement e.g. a=a, e=e. The values in i and
j is the mean value of the displacement in i and j, the same goes to k and l.
2. The way the displacements are calculated are slightly different compared to Question 1 and 2.
There is an extra forcing term which is used only when calculating displacements in the position of
the circular position of the gymnast’s feet, while outside the circular feet the equation used has no
forcing term. The equation used in the circle is:
h ∆y 2 (T k k
+ Ti−1,j ) + ∆x2 (Ti,j+1
k k
+ Ti,j−1 ) ∆x2 ∆y 2 mg i
k+ 12 k i+1,j k
Ti,j = Ti,j +α − Ti,j − (10)
2(∆x2 + ∆y 2 ) 2(∆x2 + ∆y 2 ) Ak
1 1 1 1
h ∆y 2 (T k+ 2 + T k+ 2 ) + ∆x2 (T k+ 2 + T k+ 2 ) ∆x2 ∆y 2 mg i
k+1 k i+1,j i−1,j i,j+1 i,j−1 k
Ti,j = Ti,j +α − Ti,j − (11)
2(∆x2 + ∆y 2 ) 2(∆x2 + ∆y 2 ) Ak

3. Question 3 the circle function that has another extra output (circ) which is the logical matrix
of the circle where it has the value of 0 outside the circular feet and value of 1 inside. "circ"
is used to include the forcing term only when calculating inside the circle. It is implemented by
∆x2 ∆y 2 mg ∆x2 ∆y 2 mg
multiplying with the forcing term, 2(∆x 2 +∆y 2 ) Ak , to become 2(∆x2 +∆y 2 ) Ak *circ so inside the circle,

the equation will have an extra term to minus compared to outside the circle.

6 Results

Figure 11: Surface plot and convergence plot against iteration count for thermodynamics

Figure 12: Surface plot and convergence plot against iteration count for fluid dynamics

9
Figure 13: Surface plot and convergence plot against iteration count for structural dynamics

Question Is error reset Method Used Iterations Time taken (s)


each multigrid
1 Yes Red Black Ordering (SOR) 6888 3.52
duplication method (multigrid)
2 Yes Red Black Ordering (SOR) 1312 0.669
duplication method (multigrid)
3 Yes Red Black Ordering (SOR) 1919 1.23
duplication method (multigrid)

Table 3: Time and iterations needed for different methods

7 Discussion
1. Different methods yield quite different solutions even though the general shape is similar. In the
default question 1, the circle temperature differs with different methods as shown in table 3.

Method Circle temperature (◦ C)


Successive over-Relaxation 63.47
Gauss Seidel 63.47
Red Black Ordering SOR 52.78
Jacobi Method 62.95

Table 4: Circle temperature variation with different method

This is strange as it would mean some of them are not accurate approximation of the actual solution.
However, when the circle obstruction is removed, all the solution yielded from different methods
are the exact same. This means that unnatural features like the circle and rip which are not related
to Laplace or Poisson equation does affect the answer significantly for different methods and that
theres no problem with the methods themselves.
2. The result of different methods are as shown in table 4.

Method Time(s) Iterations


Successive over-Relaxation 2.4592 2876
Gauss Seidel 115.9171 90638
Red Black Ordering SOR 8.1385 6788
Jacobi Method 38.3058 78715

Table 5: Iterations and time taken for different methods

According to the table, SOR is faster than RBO. However, it introduces large discontinuities into
the solution so it is not employed in the code. It is surprising to see that the Jacobi method has
lower iterations than GS due to the fact that GS uses updated solutions in a current iteration, GS

10
should be faster. The large time difference between GS and Jacobi is simply because the Jacobi
method is removed of for loop.
3. Removal of for loops and replaced with vectorizing methods provided the most significant time
reduction.[5] This is followed closely by multigrid methods which reduced a large number of itera-
tions.
4. Array preallocation is important for reducing computation time. If it is not done, MATLAB has to
copy the old values to the new memory location, it has the effect of increasing peak memory usage.
5. The convergence for all three questions takes less than 1 second if the error was not reset after each
multigrid iteration. If the error was reset, the computation time will increase. Other methods like
red black ordering Successive Over Relaxation method are explored. The changes in the code to
reset the error is shown in figure 16.
6. If SOR is not used, the simple linear interpolation method for the multigrid works better. If SOR
is used, the duplication method for the multigrid is better.
dT
7. It is noticed for dT0
in figure 18, a larger dT0 value will allow the fraction to decrease faster than
the specified tolerance, ‘tol’. However a larger dT0 value would mean that the values in the array
are still far away from the answers. A balance would need to be found for convergence to occur
faster.
8. As grid resolution changes, the optimal value for the optimal over-relaxation parameter would
change. Using an unoptimized value would delay convergence and in certain cases cause an incorrect
surface or contour plot.
9. When ‘sub2ind’ was used to convert subscripts to a index value, the time taken for convergence is
much longer.
10. Functions were created to find the position of the cylinders or circles. This made the code more
presentable especially in the fluid dynamics question.
11. The approach to solving all three questions were quite similar with adjustments needed to specify
the boundary conditions and edges of the circles, cylinders and the rip.

8 Conclusion
When iterative computation is performed on large arrays, it is necessary to implement multigrid methods
or improved relaxation methods when performing Laplace or Poisson equations in large grid sizes.Vectorizing
the code is important but there is little incentive to do so if it involves few iterative use of it.
Successive Over Relaxation methods are one of the fastest relaxation methods but they do not work
well near obstructions in the grid and require an optimal alpha value for it to be fast. Large alpha values
will diverge solutions while small alpha values will reduce speed. Red Black Ordering SOR is even faster
and minimizes discontinuities.

References
[1] Utah State University, Numerical Solution of Laplace Equation. Accessed 20 November 2017.
<http://ocw.usu.edu/Civil_and_Environmental_Engineering/Numerical_Methods_in
_Civil_Engineering/LaplaceDirichletTemperature>
[2] Computational Fluid Dynamics online, Successive over-relaxation method - SOR – CFD-Wiki, the free
CFD reference. Accessed 20 November 2017. <https://www.cfd-online.com/Wiki/Successive_
over-relaxation_method_-_SOR>
[3] J. K. Reid (1966), A method for finding the optimum successive over-relaxation parameter. Accessed
20 November 2017.
<https://academic.oup.com/comjnl/article-pdf/9/2/200/1728031/9-2-200.pdf>
[4] Mathworks UK, Successive Over Relaxation (SOR) of Finite Difference Method solution to Laplace’s
Equation. Accessed 30 November 2017.
<https://uk.mathworks.com/matlabcentral/fileexchange/34260>
[5] Stephen J.Chapman. (2008) Matlab Programming for Engineers. 4th Edition. Thompson. Chapter 4.
166-169

11

You might also like