User Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
exercise:prog:spinglass [2020/08/13 08:28] – created chunchungexercise:prog:spinglass [2020/11/06 03:15] (current) – [System representation] chunchung
Line 1: Line 1:
 ======Programming: Monte-Carlo simulation of spin-glass model====== ======Programming: Monte-Carlo simulation of spin-glass model======
 +Write a complete Monte-Carlo simulation program for the [[https://en.wikipedia.org/wiki/Spin_glass#The_model_of_Sherrington_and_Kirkpatrick|Sherrington–Kirkpatrick model]] of spin glass.
 +
 +The model is defined by the Hamiltonian,
 +\[H=-\sum_{i<j}J_{i,j}s_i s_j,\]
 +where $s_i=\pm 1$ with $i=0,..,N-1$ are the $N$ spins of the system.
 +
 +=====System representation=====
 +Both structure ($h_i$,$J_{i,j}$) and state ($s_i$) data are stored in one dimensional arrays.
 +<code c++>
 +int sz = 64;
 +int * spin = new int[sz];
 +double * h = new double [sz];
 +double * j = new double [sz*(sz-1)/2];
 +</code>
 +Since the coupling involves two spins, it is instinctive to use a two-dimensional array for ''j''. However, in a more general view, it is clearer to serialize the iteration into one over links instead of two nodes. This also reduces redundancy.
 +
 +Since we are using %%C++%%, we will use the ''vector'' class in place of array pointers:
 +<code c++>
 +std::vector<int> spin;
 +std::vector<double> h;
 +std::vector<double> j;
 +</code>
 +For couplings, we use ''struct Link'' to keep track of the nodes involved in each bond.
 +<code c++>
 +struct Link {
 + int n0;
 + int n1;
 +};
 +std::vector<Link> lnks;
 +</code>
 +
 +=====Data storage=====
 +For portability of data, we use [[https://www.hdfgroup.org/solutions/hdf5/|HDF5]] library. Here, we try the C++ binding of the library.
 +<code c++>
 +#include <H5Cpp.h>
 +#include <vector>
 +
 +std::vector<double> h;
 +std::vector<double> j;
 +
 +void load_params(std::string filename)
 +{
 +        using namespace H5;
 +        H5File f(filename,H5F_ACC_RDONLY);
 +        // read h
 +        DataSet d = f.openDataSet("h");
 +        hsize_t sz;
 +        d.getSpace().getSimpleExtentDims(&sz,0);
 +        h.resize(sz);
 +        d.read(h.data(),PredType::NATIVE_DOUBLE);
 +        // read j
 +        d = f.openDataSet("j");
 +        d.getSpace().getSimpleExtentDims(&sz,0);
 +        j.resize(sz);
 +        d.read(j.data(),PredType::NATIVE_DOUBLE);
 +}
 +</code>
 +=====All-to-all network=====

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information