![]() | Physics and Astronomy |
Back to top
Computational Physics Week 1: Course workTime allocation: seven hours plus remaining in-lab time.Connectivity of a meshWe consider an N by N array of nodes each of which is linked to each of its four neighbours to form a mesh. If we arbitrarily take the top edge of the mesh as our base we may then travel to the opposite (bottom) edge via a path made up of neighbouring nodes: we say that the top and bottom edges are connected:
0-0-0-0-0 | | | | | 0-0-0-0-0 | | | | | 0-0-0-0-0 | | | | | 0-0-0-0-0 (Notice that there are no explicit bonds in this example: we just assume that any two adjacent nodes are automatically connected with each other.) If we remove a few nodes the top and bottom edges will eventually become disconnected:
0-0-0-0-0 0 0-0 0-0 0-0 | | | | | | | | | 0-0-0-0-0 0-0-0 0 0-0-0 0 0 0 0 | | | | | -> | | | -> | | | -> | | | 0-0-0-0-0 0 0-0-0 0 0-0-0 0 0-0-0 | | | | | | | | | | 0-0-0-0-0 0-0-0 0 0-0 0-0 Notice how in the third example the route doubles back on itself. In the third example we have removed one bond to disconnect the top and bottom edges. Also, we don't require that every top node be connected to every bottom node, just one.
RequirementsTo represent and solve this problem we need the following:
Pseudo-random numbersWe will want to remove nodes at 'random' so we will need to be able to generate 'random' numbers to decide which bond is to be broken. (We have briefly seen this before in the wins example program.)The include file stdlib.h defines the rand() function that returns a pseudo-random integer in the range 0 to RAND_MAX. We can therefore write the following rather poor number to generate a pseudo-random integer in the range 0 to M: /* Can you see the problem with this function? */ int randint(int m) { long l = rand() % m; return l; } Initialising the pseudo-randomiserAs it stands, rand() always prints out the same pseudo-random sequence every time you run the program. If you don't want this you can initiase it with the srand() function. The classic example is to use the current time as the initialiser:#include <time.h> int main() { /* Variable declarations */ srand(time(NULL)); /* Code */ }Notice that the time() function is declared in time.h and that srand() is called only once per program. The programDo this one stage at a time and test it as you go.
Working out which nodes are connected to the top edgeOnce we start talking about whether a node is connected to the top edge we have three possible states for a node:
The test functionYour test function will be called inside your main loop, after each disconnection. The steps are:
|