C/C++ Visualizing the 2D Ising Model with Monte Carlo Algorithm

AI Thread Summary
The discussion centers on solving the 2D Ising Model using a Monte Carlo algorithm with periodic boundary conditions, focusing on visualizing the system's state at each iteration. The user has implemented a C program that generates a matrix representing spin states and uses a pipe to call gnuplot for plotting. However, the plot only appears after the program completes, not during iterations. To resolve this, the user is advised to flush the output buffer using fflush after each fprintf command to ensure gnuplot updates the plot in real-time. After implementing this solution, the user encounters a new issue where the program hangs after the last iteration, accompanied by a glibc error indicating a corrupted double-linked list. Suggestions include ensuring gnuplot exits normally and adding print statements to diagnose where the program hangs.
maverick280857
Messages
1,774
Reaction score
5
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!
 
Technology news on Phys.org


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.
 


M Quack said:
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 ***
 


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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top