2. Modularize Ants
You're given a C++ code calledantsontable.cpp along with some other related files (click to download a zip file). On the teach cluster, you can find these files in the directory /scinet/course/phy1610/antsontable.
Brief description of the ants' code:
The code simulates ants walking on a table. The table is subdivided into squares. A number of ants is initially put on the table, divided as evenly as possible over the squares. Ants can move to a neighbouring square or stay put at every time step. How many of a square's ants move in each of the 9 different directions is random (using the C++ random library). To be precise, the number of ants in each square is randomly partitioned into 9 sets corresponding to the 9 possible moves (up-left, up, up-right, down-left, down, down-right, left, right, stay). Ants can fall of the edges of the table. The program prints the number of ants left on the table after every time step, as well as the minimum number of ants on a square and the maximum. It also write the number of ants on each square at every timestep to a file called ants.txt.
The addition files are:
- teachsetup: a script to load all necessary modules on the teach cluster (gcc, graphsviz, texlive, doxygen, gnuplot and python/3)
- README.md: which explains what the code is about and how to get it to work
- Makefile: to help compile the code, create documentation, run, and plot.
- Documentation.mk: a separate makefile to produce the documentation
- Plotting.mk: a separate makefile to produce plots from the output.
- plotsnapshots.gnuplot: a gnuplot script to produce plots of snapshots of the output
- plotsnapshots.py: a python script to produce plots of snapshots of the output.
You should:
- Compile and run the code and create its documentation and plots.
- Save the original code and its output, to check against.
You will then restructure the code in the following way:
- Replace the automatic 1d arrays with dynamically allocated arrays.
- Modularize the code. There should at least be separate modules for
- Initialization of the ants
- The random partitioning function
- The 'linear' function (even though it's debatable if that's worthwhile).
- The single time step (i.e., the code block following the loop body of the for(int timestep...) loop, but excluding that for statement)
- Output, i.e., reporting the number of ants to screen and writing to ants.txt
Please recall that a proper "modularization" implies separating your functions in several files (header with prototypes, preprocessor guards and necessary includes, and source files with the implementation of such functions.
- Adapt the existing Makefile to be able to compile this modular code.
Submit your code files (source and header files), your makefile and the README.md file by January 28, 2021 at 23:55 PM. The late policy can be found in the syllabus. Do NOT submit the output of your program (it's too large).
- 2 août 2023, 17:09