Starting from:

$30

CS475 Project 1 Solved

The aim of this project is to get familiar with various vector and matrix operations, from dynamic memory allocation to the usage of programs in the library package of the course. For Fortran users memory handling and most matrix and vector operations are included in the ANSI standard of Fortran 90/95. Array handling in Python is also rather trivial. For C++ user however, there are several possible options. Two are listed here.

• For this exercise we recommend that you make your own functions for dynamic memory allocation of a vector and a matrix. You don’t need to write a class for this operations. Use then the library package lib.cpp with its header file lib.hpp for obtaining LU-decomposed matrices, solve linear equations etc.

• A very good and often recommended library for C++ handling of arrays is the library Armadillo, to be found at arma.sourceforge.net. We will discuss the usage of this library during the lab sessions and lectures. Armadillo has also an interface to Lapack functions for solving systems of linear equations. Alternatively you can use the Eigen library, which has much of the same functionality as Armadillo.

Your program, whether it is written in C++, Python, Fortran or other languages, should include dynamic memory handling of matrices and vectors.

The material needed for this project is covered by chapter 6 of the lecture notes, in particular section 6.4 and subsequent sections.

Many important differential equations in Science can be written as linear second-order differential equations

d2y + k2(x)y = f(x), dx2

where f is normally called the inhomogeneous term and k2 is a real function.

⃝c 1999-2020, "Computational Physics I FYS3150/FYS4150":"http://www.uio.no/studier/emner/matnat/fys/FYS3150/index- eng.html". Released under CC Attribution-NonCommercial 4.0
license
A classical equation from electromagnetism is Poisson’s equation. The electrostatic potential Φ is generated by a localized charge distribution ρ(r). In three dimensions it reads

∇2Φ = −4πρ(r).
With a spherically symmetric Φ and ρ(r) the equations simplifies to a one-
dimensional equation in r, namely
1 d 􏰁 2dΦ􏰂
r2 dr r dr = −4πρ(r), which can be rewritten via a substitution Φ(r) = φ(r)/r as
d2φ = −4πrρ(r). dr2

The inhomogeneous term f or source term is given by the charge distribution ρ multiplied by r and the constant −4π.

We will rewrite this equation by letting φ → u and r → x. The general one-dimensional Poisson equation reads then

−u′′(x) = f(x).

Project 1 a): In this project we will solve the one-dimensional Poisson equation with Dirichlet boundary conditions by rewriting it as a set of linear equations.

To be more explicit we will solve the equation
−u′′(x) = f(x), x ∈ (0,1), u(0) = u(1) = 0.

and we define the discretized approximation to u as vi with grid points xi = ih in the interval from x0 = 0 to xn+1 = 1. The step length or spacing is defined as h = 1/(n + 1). We have then the boundary conditions v0 = vn+1 = 0. We approximate the second derivative of u with

−vi+1 +vi−1 −2vi =fi fori=1,...,n, h2

where fi = f(xi). Show that you can rewrite this equation as a linear set of equations of the form
A v = b ̃ ,
where A is an n × n tridiagonal matrix which we rewrite as

2 −1 0 ... ... 0 −1 2 −1 0 ... ...
0 −1 2 −1 0 ... 
A= ... ... ... ... ...,  0 . . . − 1 2 − 1 
0 ... 0 −1 2
2
and ̃bi = h2fi.
In our case we will assume that the source term is f(x) = 100e−10x, and keep

the same interval and boundary conditions. Then the above differential equation has a closed-form solution given by u(x) = 1 − (1 − e−10)x − e−10x (convince yourself that this is correct by inserting the solution in the Poisson equation). We will compare our numerical solution with this result in the next exercise.

Project 1 b): We can rewrite our matrix A in terms of one-dimensional vectors a, b, c of length 1 : n. Our linear equation reads
. . .   v 1  . . .   v 2  ... ...

... ... = .... an−1 bn vn ̃bn

Note well that we do not include the endpoints since the boundary conditions are used resulting in a fixed value for vi. A tridiagonal matrix is a special form of banded matrix where all the elements are zero except for those on and immediately above and below the leading diagonal. Develop a general algorithm first which does not assume that we have a matrix with the same elements along the diagonal and the non-diagonal elements. The algorithm for solving this set of equations is rather simple and requires two steps only, a decomposition and forward substitution and finally a backward substitution.

Before we proceed with the solution of the differential equation, you should now plan the organization of your data flow. Here you will find it convenient to define vectors that will contain the matrix elements, the solution to the problem and the function f(x) when discretized. You should also plan on to read input data, whether you do this from the command line or from a selected file. We recommend strongly that you use dynamical memory allocation. An example of a C++ program which reads from the command line various input parameters can be found here

Your first task is to set up the general algorithm (assuming different values for the matrix elements) for solving this set of linear equations. Find also the precise number of floating point operations needed to solve the above equations. For the general algorithm you need to specify the values of the array elements a, b and c by inserting their explicit values.

Then you should code the above algorithm and solve the problem for matrices of the size 10 × 10, 100 × 100 and 1000 × 1000. That means that you select n = 10, n = 100 and n = 1000 grid points.

Compare your results (make plots) with the closed-form solution for the different number of grid points in the interval x ∈ (0, 1). The different number of grid points corresponds to different step lengths h.

3
 A = 
c 2 b3 ...
 b 1  a 1
. . .

. . .
. . .
. . .
...
...
 an−2 bn−1 cn−1 . . . . . .
c 1 b 2 a2 ...
0
c3
 
...
 ̃b 1   ̃b 2  ...
Project 1 c): Use thereafter the fact that the matrix has identical matrix elements along the diagonal and identical (but different) values for the non- diagonal elements. Specialize your algorithm to the special case and find the number of floating point operations for this specific tri-diagonal matrix. Compare the CPU time with the general algorithm from the previous point for matrices up to n = 106 grid points.
Project 1 d): Compute the relative error in the setting up

􏰁 􏰀􏰀 v i − u i 􏰀􏰀 􏰂 εi=log10 􏰀 u 􏰀 ,

􏰀i􏰀
data set i =
1, . . . , n,by
as function of log10(h) for the function values ui and vi. For each step length extract the max value of the relative error. Try to increase n to n = 107. Make a table of the results and comment your results. You can use either the algorithm from b) or c).

Project 1 e): Compare your results with those from the LU decomposition codes for the matrix of sizes 10 × 10, 100 × 100 and 1000 × 1000. Here you should use the library functions provided on the webpage of the course. Alternatively, if you use armadillo as a library, you can use the similar function for LU decomposition. The armadillo function for the LU decomposition is called LU while the function for solving linear sets of equations is called solve. Use for example the unix function time when you run your codes and compare the time usage between LU decomposition and your tridiagonal solver. Alternatively, you can use the functions in C++, Fortran or Python that measure the time used.

Make a table of the results and comment the differences in execution time How many floating point operations does the LU decomposition use to solve the set of linear equations? Can you run the standard LU decomposition for a matrix of the size 105 × 105? Comment your results.

To compute the elapsed time in c++ you can use the following statements

...
#include "time.h" // you have to include the time.h header
int main()

{
// declarations of variables
...
clock_t start, finish; // declare start and final time
start = clock();
// your code is here, do something and then get final time
finish = clock();
( (finish - start)/CLOCKS_PER_SEC );

...
4
Similarly, in Fortran, this simple example shows how to compute the elapsed time.
PROGRAM time
REAL :: etime
REAL :: elapsed(2)
REAL :: total
INTEGER :: i, j

WRITE(*,*) ’Start’

DO i = 1, 5000000 j=j+1

ENDDO
! Declare the type of etime()
! For receiving user and system time
! For receiving total time

total = ETIME(elapsed)
WRITE(*,*) ’End: total=’, total, ’ user=’, elapsed(1), &

’ system=’, elapsed(2)
END PROGRAM time

Your results may depend on the granularity of the clock.

Introduction to numerical projects

Here follows a brief recipe and recommendation on how to write a report for each project.

Give a short description of the nature of the problem and the eventual numerical methods you have used.
Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.
Include the source code of your program. Comment your program properly.
If possible, try to find analytic solutions, or known limits in order to test

your program when developing the code.
Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.
Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.
Try to give an interpretation of you results in your answers to the problems. 5
Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you’ve made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.
Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don’t properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.

Format for electronic delivery of report and programs

The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008 or Python. The following prescription should be followed when preparing the report:

Use Canvas to hand in your projects, log in at https://www.uio.no/ english/services/it/education/canvas/ with your normal UiO user- name and password.
Upload only the report file! For the source code file(s) you have developed please provide us with your link to your github domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them. Alternatively, you can just upload the address to your GitHub or GitLab repository.
In your git repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.
In this and all later projects, you should include tests (for example unit tests) of your code(s).
Comments from us on your projects, approval or not, corrections to be made etc can be found under your Canvas domain and are only visible to you and the teachers of the course.

More products