New Reply

Using gnuplot through a pipe in C/C++ to generate multiple plots (one each iteration)

 
Share Thread Thread Tools
Jun19-12, 07:23 AM   #1
 

Using gnuplot through a pipe in C/C++ to generate multiple plots (one each iteration)


Hi,

So I'm trying to solve the 2D Ising Model using a simple Monte Carlo algorithm, for small square lattices, imposing periodic boundary conditions. Before I compute any thermodynamic quantities though, I want to study the energetics of the system with only nearest-neighbour interactions.

I want to visualize the system at every iteration of the Monte Carlo loop. The idea is to store the state of the lattice in a matrix, with each site storing the value +1 for spin up, and -1 for spin down.

A computer program I've written in C with a friend generates a data file with the state of the matrix. I then use a pipe to call gnuplot from within this program, to plot the file.

However, I notice that the plot window appears only after the pipe is closed -- presumably it is then that gnuplot is actually executed the first time, with all the arguments sent through the pipe. This means the plot window does not show up at a specific time of your choice, and in fact shows up at the end of the program.

Now, I want to visualize the matrix at every iteration, so for that, I need to call gnuplot (through the same pipe pointer) in every iteration, so that the existing plot is updated. How can this be done?

If I just naively include a plot command (through fprintf), then the plot shows up for the first time after some 20 or 30 iterations (depending on the size of the lattice).

Thanks in advance!
 
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> 'Whodunnit' of Irish potato famine solved
>> The mammoth's lament: Study shows how cosmic impact sparked devastating climate change
>> Curiosity Mars rover drills second rock target
Jun19-12, 10:36 AM   #2
 
you may need to flush the pipeline. If the C output is buffered, it will only be written when the buffer is full, or when the file/pipe is closed. You can manually trigger the flushing using fflush(file handle). Otherwise there may be a flag to open the pipe in an unbuffered mode.
 
Jun19-12, 10:44 AM   #3
 
Quote by M Quack View Post
you may need to flush the pipeline. If the C output is buffered, it will only be written when the buffer is full, or when the file/pipe is closed. You can manually trigger the flushing using fflush(file handle). Otherwise there may be a flag to open the pipe in an unbuffered mode.
Thanks M Quack! Using fflush after every fprintf seems to have done the trick. However, I get the following error after the last iteration, and the program simply hangs (without exiting).

Code:
glibc detected *** ./ising: corrupted double-linked list: 0x000000000093e000 ***
 
Jun19-12, 03:46 PM   #4
 

Using gnuplot through a pipe in C/C++ to generate multiple plots (one each iteration)


No clue. Make sure Gnuplot exits normally (send exit command) before you kill the pipe?!? Otherwise throw in a hand full of print commands to see how far it goes and where it hangs.
 
New Reply

Tags
gnuplot, ising model
Thread Tools


Similar Threads for: Using gnuplot through a pipe in C/C++ to generate multiple plots (one each iteration)
Thread Forum Replies
Using multiple variables to generate a table or csv Math & Science Software 0
Multiple 2D plots in Mathematica Math & Science Software 1
Radial Plots and Smooth Density Plots Math & Science Software 0
Contour Plots in Gnuplot Math & Science Software 1
graphing implicit/3d/whatever (multiple plots) in maple/other software Calculus & Beyond Homework 0