03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Good and Bad Scientific Software<br />

Chapter 1<br />

This chapter will give you an idea what we consider good scientific software and what not. If<br />

you have never programmed be<strong>for</strong>e in your life you might wish to skip the entire chapter. This<br />

is o.k. because if you had no contact with the program sources of bad software you can learn<br />

programming with a pure mind.<br />

If you have some software knowledge, there might be still some details you will not understand<br />

right now but this is no reason to worry. If you do not understand it after reading this script<br />

then you can start worrying, or we as authors could. This chapter is only about getting a feeling<br />

what distinguishes good from bad software in science.<br />

As foundation of our discussion — and to not start the book with hello world — we consider an<br />

iterative method to solve system of linear equations Ax = b where A is a symmetric positivedefinite<br />

(SPD) matrix, x and b are vectors, and x is searched. The method is called ‘Conjugate<br />

Gradients’ (CG) and was introduced by Magnus R. Hestenes and Eduard Stiefel [?].<br />

The mathematical details do not matter here but the different styles of implementation. The<br />

algorithm can be written in the following <strong>for</strong>m: 1<br />

Algorithm 1: Conjugate Gradient Method Algorithm.<br />

Input: SPD matrix A, vector b, and left preconditioner L, termination criterion ε.<br />

Output: Vector x such Ax ≈ b.<br />

1 r = b − Ax<br />

2 while |r| ≥ ε do<br />

z = L−1 3<br />

r<br />

4 ρ = 〈r, z〉<br />

5 if First iteration then<br />

6 p = z<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

else<br />

p = z + ρ<br />

ρ ′ p<br />

q = Ap<br />

α = ρ/〈p, q〉<br />

x = x + αp<br />

r = r − αq<br />

ρ ′ = ρ<br />

1 This is not precisely the original notation but a slightly adapted version that introduces some extra variables<br />

to avoid redundant calculations.<br />

11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!