Assignment 2: Code refactoring
For assignment 2 of the 2023 Winter PHY1610 course, we will look at a one-dimensional variant of Conway's Game of Life, as conceived by Jonathan K. Millen and published in BYTE magazine in December, 1978.
This system is a linear set of cells that are either "alive" or "dead", and time progresses in discrete steps.
The state of each cell at the next time step depends on its present state and that of its neighbours, in the following way:
- First, count the number of alive neighbours at most 2 cells away
- An alive cell stays alive if that count is 2 or 4, else it dies
- A dead cell becomes alive if that count is 2 or 3, else it stays dead.
Since the first two and the last two cells would not have enough neighbours to apply this rule, we will use cells on the other side as neighbours, i.e., we use 'periodic boundary conditions'.
The initial state of this system will be constructed with a given fraction of alive cells which are (nearly) equally spaced among dead cells.
You are given a code, gameof1d.cpp, that already computes the time evolution of this system, and for each time step, prints out a line with a representation of the state and fraction of alive cells.
For this assignment, start by studying the code, make sure you understand what's going on. Make sure you can compile and run it on the Teach cluster.
Your task is to reorganize ('refactor') this code to be more modular. We'll see in the next few lectures why this is a good idea, but generally, the aim is to have separate functionalities be implemented in separate functions.
As a first step in this modularization process:
- Extract the part of the code that first fills the cell array, put it in a separate function that then gets called from the main function, so that the program still produce the same output (check that).
- Also put the "Update cells" in a function.
- And do the same the "Output" sections (this one should reduce the amount of code duplication).
- Add functionality to the code so that num_cells, num_steps, and target_alive_fraction can be specified as command line arguments.
- Create a text file called compilerun.sh, which should contain the commands needed to compile and run the program.
You should submit two files to the course website by midnight (ET) January 26th, 2023:
- Your refactored version of gameof1d.cpp
- The file compilerun.sh
- 19 January 2023, 10:28 PM