It's all in the Graphics!

It would be interesting to display random walks one after another as they are generated. In order to do so the random_walk.c code must interact with gnuplot. The concept to use is therefore a Unix code-to-code communication pipe. This requires to look a bit more at C syntax.

The idea is to establish a communication channel from one program to another. In other words, to do an equivalent of the standard stdout piping into stdin between codes, but while the codes are executing and not involving a stdin and stdout pipe! The C language and Unix offers two functions to establish and close such open pipes, popen() and pclose(). To facilitate the tasks of the programmer the C language uses a syntax to read/write through a Unix pipe analogous to writing and reading from a disk file. This has the great advantage that you can use fprintf() into a pseudo file stream to communicate through the pipe.

To establish a pipe requires to establish a file handler, a file pointer, which will corresponded to the channel for the pipe, open the pipe, read from or write to the pipe and close the pipe once done.

1) FILE *fp establishes a file pointer.

2) fp = popen( ``gnuplot'', ``w'' ) starts gnuplot (or any other application), opens a pipe to this application and allows this pipe to be accessed via the file pointer fp for writing.

3) fprintf( fp, ``command'' ) sends (writes) information (command) to gnuplot.

4) fflush( fp ) empties the buffer associated with the file pointer fp. This makes sure that gnuplot receives all the information in the command that have been transmitted through the pipe.

5) pclose( fp ) closes the communication pipe to gnuplot. Note that you must make gnuplot quit through the pipe.

These provide the logical steps for starting gnuplot, opening a pipe to it, transmitting commands to it, and closing the pipe. Since the calculations can happen extremely fast and the piping/graphing not so fast, you may want to slow down the procedure by making the code sleep a few moments in between the actions taken by gnuplot. sleep(time) allows to insert a time seconds delay between the commands forwarded to the pipe.

The code gnuplot_pipe_demo.c illustrates the procedure for very simple data set. It picks up a file and plots it. The code moving_circles.c generates the data for this demo. Note how the frames are separated in the data.

To display random walks in sequence using gnuplot requires a bit more thinking. The following steps need to be taken: read the data through a pipe via a redirection from random_walk.c. Store the data into a temporary file under /tmp. Open a pipe to gnuplot and instruct it to plot the data points that were stored in the file. Receive new data in the redirected input and replace the file under /tmp with this new data. Instruct gnuplot to graph this new random walk. Continue until the end of the externally piped stream is reached.

Storing the file on the /tmp instead of the user directory is good practice. On all computers /tmp is public scratched disk space usable by any user on the system. After you create the data file on /tmp you should make sure the code removes that data file.

A signal must be transmitted to the code to indicate the end of each random walk. This is easy to do with a # followed by a comment. # followed by comments are ignored by gnuplot in plotting graphs. Therefore the file can be plotted by normal means if the data is stored in this format. But this constitutes a easy marker to recognize in between the frames to produce a sequence of frames.

The code gnuplot_pipe.c implements the procedure described above. To use it, type

random_walk -w 5 | gnuplot_pipe

This will create 5 random walks that will be displayed in sequence.

Exercise: Explore the various options in gnuplot_pipe.c. In particular, use the options -x xmin xmax and -y ymin ymax to fix the x- and y-ranges for a movie like display.

Michel Vallieres 2014-04-01