16
Physics 420/580: Computational Physics Final Exam Tuesday, December 16, 2008 in class 14:00–15:20, in lab 15:30–17:00 Student’s Name: 7 points multiple choice questions 1–7 23 points long answer questions 8–13 30 points programming questions 14–18 60 points 1

Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

Embed Size (px)

Citation preview

Page 1: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

Physics 420/580: Computational Physics

Final Exam

Tuesday, December 16, 2008in class 14:00–15:20, in lab 15:30–17:00

Student’s Name:

7 points multiple choice questions 1–723 points long answer questions 8–13

30 points programming questions 14–18

60 points

1

Page 2: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

Multiple Choice Questions (7 points)

Answer by circling one of (a), (b), (c), etc. Please be clear about which oneyou have selected.

1. A very accurate Runga-Kutta integrator (high order and small timestep) is used to simulate a simple pendulum released from rest at an-gle θ = 30◦. Two instances of the program are run, tracing out thependulum trajectory from time t = 0 to t = 20. Program A runs tocompletion without interruption. Program B is stopped at t = 10, atwhich point it dumps its current configuration (3 digits of precision) tothe file snapshot.xml.

$ cat snapshot.xml

<pendulum>

<angle units="degrees"> 15.3 </angle>

<angular_velocity units="degrees_per_second">

-0.0138

</angular_velocity>

</pendulum>

Program B is started up again with initial conditions read in fromsnapshot.xml and allowed to run from t = 10 to t = 20. How do thetrajectories of A and B compare?

(a) The two trajectories diverge beyond t = 10

(b) Trajectory B experiences a slight hiccup at t = 10 but quicklyreconverges on trajectory A

(c) There is no difference whatsoever between the two trajectories

2. We want to write a function ave(float x, float y) that averagestwo numbers and is guaranteed not to overflow. Which of the followingis the correct body of the function?

(a) return (x + y)/2;

(b) return x/2 + y/2;

(c) return x + (y - x)/2;

2

Page 3: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

3. A two-dimensional space is discretized using a uniform square meshof grid size ∆x: i.e., φ(i · ∆x, j · ∆x) = φi,j. What is an appropriatefinite-difference approximation to the Laplacian ∇2φ?

(a) 1(∆x)2

[φi+1,j + φi−1,j + φi,j+1 + φi,j−1 − 2φi,j

](b) 1

(∆x)2

[φi+1,j − φi−1,j + φi,j+1 − φi,j−1 + 4φi,j

](c) 1

(∆x)2

[φi+1,j + φi−1,j + φi,j+1 + φi,j−1 − 4φi,j

]4. The diagram on the left shows two planar trajectories, r1(t) = (x1(t), y1(t))

and r2(t) = (x2(t), y2(t)). The diagram on the right shows the distancebetween them, d(t) = ‖r2(t) − r1(t)‖, plotted as a function of time.(Note the log scale.) Estimate the Lyaponov exponent. You may wantto use the fact that ln 10

.= 2.3.

x t

0.01

0.1

1 2 3 4 5

y d

(a) λ.= −0.025

(b) λ.= −1.33

(c) λ.= 1.15

(d) λ.= 25

3

Page 4: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

5. An Ising-like model with a magnetically-ordered, low-temperature phaseis simulated on lattices of increasingly larger size. Measurements of thesecond and fourth moments of the magnetization can be used to con-struct the Binder ratio U = 1 − 〈m4〉/3〈m2〉2. Below, a plot of Uversus temperature T is given for system sizes L1 < L2 < L3 < L4. AsL→∞, U(T ) approaches a step function (dotted line).

T

2/3

U

Tc

x = tL4/3

2/3

U

1/3

0

1/3

Suppose that the data collapses (roughly) onto a single curve whenplotted versus the composite coordinate tL4/3, where t = (T −Tc)/Tc is

4

Page 5: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

the reduced temperature. What is the value of the correlation lengthexponent? (The size of the magnetic domains goes as ξ ∼ t−ν .)

(a) ν = 1/3

(b) ν = 2/3

(c) ν = 3/4

(d) ν = 4/3

For the next two questions, answer by writing the corresponding letter in thespaces provided. Each of (a), (b), and (c) should appear once.

6. The execution time of a molecular dynamics simulation of a Lennard-Jones gas is plotted as a function of the number of particles N . Identifywhich curve belongs to which algorithm.

N

(a)

(b)

(c)

Wall Clock

( ) Standard algorithm running on one computer

( ) Parallel algorithm running on five computers

( ) Barnes-Hutt algorithm

5

Page 6: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

7. A drunken C++ programmer wants to sum the numbers 1 to 9 (whichshould give 1 + 2 + · · · + 8 + 9 = 45). His three attempts yield thefollowing code snippets. Try to determine the behaviour of each.

(a) int j = 0;

int i = 3;

for (i = 1; i < 10; ++i)

j = j + i;

cout << j << endl;

(b) int i;

int j = 1;

for (i = 2; i < 10; ++i);

j += i;

cout << j << endl;

(c) int j = 0;

for (int i = 0; i < 9; ++i)

j = j + i;

cout << j+i << endl;

( ) Fails to compile

( ) Outputs 11

( ) Outputs 45

6

Page 7: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

Long Answer Questions (23 points)

8. (3 points) Suppose we are given a first-order ODE of the form

x′(t) = F (x(t), t)

x(0) = x0

The formal solution,

x(t) = x0 +

∫ t

0

dsF (x(s), s),

is discretized using a fixed time step (xi = x(ti) and ti = i · ∆t, say),which yields

xi+1 = xi +

∫ ti+t

ti

dsF (x(s), s) ≈ xi +1

2

(Fi + Fi+1

)∆t.

The ≈ symbol indicates where the integral is replaced by its trapezoid-rule value using the notation Fi = F (x(ti), ti). This approximationleads to so-called Picard integration of the ODE. Explain why thisscheme requires a self-consistent solution. How would you find it?

7

Page 8: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

9. (4 points) Write a function double sinc(double) that computes sin(x)/xand is well-behaved around x = 0. Don’t forget your #includes, andbe sure to return something. You may want to take advantage of none,some, or all of the following properties of the sine function.

sinx = x− x3

3!+x5

5!− x7

7!+ · · ·∫

dx sinx = const.− cosx

sinx =opposite

hypotenuse(right-angled triangle containing angle x)

#include <cmath>

using std::sin;

using std::fabs;

using std::pow;

#include <limits>

using std::numeric_limits;

double sinc(double x)

{

const double eps = pow(numeric_limits<double>::epsilon(),1.0/6);

if (fabs(x) > eps) return sin(x)/x;

const double x2 = x*x;

return 1.0 - x2/6.0 + x2*x2/120.0 - x2*x2*x2/5040;

}

8

Page 9: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

10. (3 points) A four-bit signed integer is represented internally using thetwo’s complement scheme. What is the largest representable positivenumber? What is the most negative number? How should we interpretthe following operation?

0011+1101

The complete range of numbers for 4-bit two’s complement is as follows.

0111 70110 60101 50100 40011 30010 20001 10000 01111 −11110 −21101 −31100 −41011 −51010 −61001 −71000 −8

The most positive number is 7. The most negative is −8. The operationgiven above should be interpreted as 3 + (−3) = 0.

111100011

+11010000

The leftmost two carry digits (11) do not signal an overflow.

9

Page 10: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

11. (3 points) Compute the period of the sequence (xn) defined below. Givetwo reasons why it would make a bad random number generator.

xn = (xn−1 + xn−2) mod 44

x1 = 1

x0 = 5

10

Page 11: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

12. (5 points) Consider the code given below.

#include <iostream>

using std::cout;

using std::endl;

#include <queue>

using std::queue;

#include <stack>

using std::stack;

class counter

{

public:

const int value;

counter() : value(1) {}

counter(int n) : value(n) {}

int num_leaves() const { return 2; }

counter leaf(int n) const { return counter(2*value + n); }

};

template <class T>

void traverse_tree1(const T &obj)

{

queue<T> Q;

Q.push(obj);

while (!Q.empty() and Q.size() < 9)

{

T branch = Q.front();

Q.pop();

cout << branch.value << endl;

for (size_t k = 0; k < branch.num_leaves(); ++k)

Q.push(branch.leaf(k));

}

}

template <class T>

void traverse_tree2(const T &obj)

{

11

Page 12: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

stack<T> S;

S.push(obj);

while (!S.empty() and S.size() < 9)

{

T branch = S.top();

S.pop();

cout << branch.value << endl;

for (size_t k = 0; k < branch.num_leaves(); ++k)

S.push(branch.leaf(k));

}

}

int main()

{

cout << "Search 1:" << endl;

traverse_tree1(counter());

cout << "Search 2:" << endl;

traverse_tree2(counter());

return 0;

}

Draw the tree of values defined by the class counter. Trace the pro-gram execution and write its output exactly as it would appear in theterminal (cout). (Hint: the two traversal algorithms differ only in theirdata structures. Remember that a stack is a last-in-last-out list and aqueue is a first-in-first-out list.)

12

Page 13: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

13. (5 points) Draw all 3-step self-avoiding walks on the square lattice. Byrotational symmetry, you can choose the first step to be north (reducingthe number of walks by a factor of 4). Assume that the head and tailinteract with energy V if they are adjacent. If the system is held attemperature T , write down the average squared head-tail separation〈r2〉 as a function of v = V/kBT . Then, compute the limits of infiniteattraction, infinite repulsion, and infinite temperature. If you cannotcompute them, tell me about them in words.

13

Page 14: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

Programming (30 points)

This section of the exam is to be completed in the computer lab. You arefree to organize your program in any way you choose so long as all the coderesides in the program file exam.cpp and can be compiled with the providedmakefile.

To start, download the exam.tar.gz archive from the class web site:

http://www.ualberta.ca/~kbeach/phys420_580.html.

Unpack the archive and append your own last name to the exam directory.

$ gunzip exam.tar.gz

$ tar -xf exam.tar

$ mv exam exam_beach

$ cd exam_beach

At the end of the exam, you should archive your work directory.

$ cd ..

$ tar -cf exam_beach.tar exam_beach

$ gunzip exam_beach.tar.gz

Email it to the instructor at [email protected].

14

Page 15: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

14. (5 points) A 20 × 20 array of positive integers is defined with the fol-lowing values.

74 80 47 33 22 26 40 44 34 40 48 39 42 50 33 42 22 32 66 74

70 61 52 23 14 19 22 31 39 40 45 32 36 31 21 17 17 23 36 70

65 59 46 29 31 15 15 29 28 45 38 45 41 26 24 21 13 37 48 64

56 56 40 25 21 10 12 9 26 36 35 19 28 12 7 27 34 41 34 53

45 40 41 34 26 20 27 22 21 29 37 39 23 10 20 18 17 29 43 59

60 48 44 42 36 25 31 14 29 29 37 27 20 19 16 26 14 42 59 53

56 66 60 32 37 32 28 19 24 42 30 30 28 22 26 30 29 28 62 67

62 74 45 29 23 20 25 34 45 51 47 30 45 30 26 28 21 50 48 59

81 71 55 41 44 41 41 35 50 47 36 43 33 31 32 40 38 65 50 70

70 57 46 35 30 43 29 41 33 38 48 55 48 43 43 23 26 44 70 67

87 61 53 57 42 39 37 49 57 60 45 49 44 36 44 40 26 33 50 99

93 65 56 48 39 28 47 42 43 41 41 53 41 52 43 26 30 34 49 74

72 62 61 42 26 29 38 40 33 42 51 46 42 42 37 30 45 34 66 59

64 61 64 40 26 22 31 45 48 41 31 45 42 43 21 35 27 52 66 78

52 55 57 37 31 36 34 22 39 36 34 32 34 28 15 14 22 47 41 72

49 48 47 41 36 11 17 14 27 37 36 38 22 22 18 13 17 43 61 50

64 62 57 41 19 25 16 8 18 24 25 32 18 19 19 14 27 40 53 49

47 53 45 46 26 8 25 28 31 28 29 31 31 8 7 19 26 26 36 60

66 62 36 41 24 28 21 38 43 49 37 31 41 30 16 10 25 21 47 55

70 60 50 22 15 18 24 37 46 47 53 41 41 39 38 35 28 33 58 68

Write code to find the greatest product of four adjacent numbers in ahorizontal, vertical, or diagonal line. (No fancy optimization methodsare required; this is an exhaustive search.)

15. (5 points) Repeat your calculation for the case of periodic boundaryconditions.

16. (5 points) Place a random thermal walker at position (0,0). Allow itto take nearest neighbour steps (up, down, right, left, using periodicboundary conditions) with probability P = min(1, e−∆E/T ). Here, ∆Eis the change in grid values in going from the old to the new location.Keeping in mind that generating a random number and computing anexponential are very expensive operations, write the accept/reject stepin the most efficient way. (Hint: lazy evaluation with or.)

17. (12 points) Perform 1 000 000 equilibration steps (with no measurementstaken) and 1 000 000 sampling steps in which you maintain a histogram

15

Page 16: Physics 420/580: Computational Physicskbeach/phys420_580_2011/... · Physics 420/580: Computational Physics Final Exam Tuesday ... Standard algorithm running on one ... 56 56 40 25

of the walker location. (Don’t worry about autocorrelation or binning:take a measurement after every step.) You’ll have to declare another20 × 20 array to keep this information. Measure the histogram forthe five temperatures T = 1, 10, 20, 40, 80 and dump the (unnormal-ized) histogram to files hist-T1.dat, hist-T10.dat, hist-T20.dat,hist-T40.dat, and hist-T80.dat in matrix form (i.e., a square grid ofvalues with 20 columns and 20 rows, analogous to the Mandelbrot out-put in Assignment 1). If you’ve done everything correctly, you shouldbe able to view the results with the provided gnuplot script.

$ make

$ ./exam

$ gnuplot view.gp

18. (3 points) The script view.gp shows you a false-colour density plot ofthe five histograms in order of decreasing temperature. (You have tohit the return key to cycle through them.) Explain the trend as T goesfrom 80 to 10. Also, explain the rather different result for T = 1.

16