24小时热门版块排行榜    

查看: 1168  |  回复: 0

zyj8119

木虫 (著名写手)

[交流] 【转帖】wolff算法的C代码

CODE:
/* Wolff single-cluster algorithm for the two dimensional nearest neighbor
   Ising model                          By Jian-Sheng Wang, February 1995.
   Compile with  

       cc -O Wolff.c -lm

   -O for optimization, and -lm to link the math library.               
*/

#include
#include
#include
                              /* macro definitions */
#define  L  16                /* lattice linear size */
#define  N  (L*L)             /* total number of spins */
#define  Z  4                 /* coordination number = 2*d */
#define  MCTOT 500            /* total Monte Carlo steps */
#define  MCDIS 200            /* steps discarded in the beginning */
                              /* global variables */
int s[N];                     /* spin +1 or -1 */
double  p = 1.0;              /* percolation probability */

         /* funcition prototypes */
void neighbor(int i, int nn[ ]);
void flip(int i, int s0);
void monte_carlo_steps(int n);
void energy(double *);

/*  The main program, running the Monte Carlo loop, collecting data  */

void main()
{
   int i, mc;
   double e = 0;

   p = 1 - exp( - 2/2.269);

   for (i = 0; i < N; ++i)     /* initialize, all spin up */
      s = 1;

   for(mc = 0; mc < MCTOT; ++ mc) {
      monte_carlo_steps(5);
      if( mc >= MCDIS)
         energy(&e);
   }
   printf(" =  %f\n", e/(MCTOT-MCDIS)/N);
}

/* This function monte_carlo_steps performs n cluster flips, eqivalent to
few Monte Carlo steps in standard single-spin-flip algorithms.
It picks a seed site at random and calls the flip function to generate
one cluster.  N is total number of spin.  They are macro definitions. */

void monte_carlo_steps(int n)
{
   int i, k;

   for(k = 0; k < n; ++k) {
      i = drand48() * (double) N;
      flip(i, s);
   }
}

/*  Perform a Wolff single cluster flip. s[], p, and Z are passed globally.
The first argument i of flip function is the site to be flipped, the
second argument is the spin of the cluster before flipping. */

void flip(int i, int s0)
{
   int j, nn[Z];

   s = - s0;                    /* flip the spin immediately */
   neighbor(i, nn);                /* find nearest neighbor of i */
   for(j = 0; j < Z; ++j)          /* flip the neighbor if ...  */
      if(s0 == s[nn[j]] && drand48() < p)
         flip(nn[j], s0);
}

/* Neighbor returns in the array nn[ ] the neighbor sites of i.  The sites
are labelled sequentially, starting from 0.  It works for any hypercubic
lattice.  Z (=2*D) is the coordination number, passed as a macro defintion.
L is linear size, also passed as a macro definition. */

void neighbor(int i, int nn[ ])
{
   int j, r, p, q;

   r = i;
   p = 1 - L;
   q = 1;

   for(j = 0; j < Z; j += 2) {
      nn[j] = (r + 1) % L == 0 ? i + p : i + q;
      nn[j+1]     = r % L == 0 ? i - p : i - q;
      r = r/L;
      p *= L;
      q *= L;
   }
}

/* This function calculate the energy of the configuration s[],
   in fact, it is the negative of the energy.  */

void energy(double *e)
{
   int i, j, ie = 0;
   int nn[Z];
   
   for(i = 0; i < N; ++i) {
      neighbor(i, nn);             /* find the neighbor of center site */
      for(j = 0; j < Z; j += 2)    /* look at positive direction only */
         ie += s*s[nn[j]];      /* add nearest neighbor interaction */
   }
   *e += ie;                       /* accumulant energy */
}

其中drand48() 为0-1之间均匀分布的随机数

回复此楼
好好学习,天天向上。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 zyj8119 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[博后之家] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +5 gy1nBQXYQJqL 2026-08-29 8/400 2026-08-30 12:03 by l0VvVHGBGRLv
[考博] 找导师 +5 yuanjiabao 2026-08-29 6/300 2026-08-30 11:52 by zhouyanli11
[论文投稿] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +5 gy1nBQXYQJqL 2026-08-29 7/350 2026-08-30 11:51 by l0VvVHGBGRLv
[考研] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +6 ASdOkHsho7FD 2026-08-28 9/450 2026-08-30 11:30 by l0VvVHGBGRLv
[基金申请] 麻烦专家们看看评委们的意见(F口面上) +6 gdd2018 2026-08-28 11/550 2026-08-30 08:58 by 超级无敌华子
[基金申请] 我就是申请一个面上项目而已,这评审意见是按照杰青的条件评的吧? +6 gouxfjh 2026-08-28 11/550 2026-08-30 07:57 by gouxfjh
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +3 G6APbkg8SA6w 2026-08-29 4/200 2026-08-30 07:28 by ZPa0EcMwuECS
[硕博家园] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +4 gy1nBQXYQJqL 2026-08-29 5/250 2026-08-30 06:55 by ZPa0EcMwuECS
[考博] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +6 ASdOkHsho7FD 2026-08-28 10/500 2026-08-30 06:11 by ZPa0EcMwuECS
[教师之家] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +5 ASdOkHsho7FD 2026-08-28 8/400 2026-08-30 05:48 by ZPa0EcMwuECS
[考研] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +7 ASdOkHsho7FD 2026-08-28 11/550 2026-08-30 05:40 by ZPa0EcMwuECS
[考研] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +4 G6APbkg8SA6w 2026-08-29 5/250 2026-08-30 02:40 by ZPa0EcMwuECS
[考博] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 gy1nBQXYQJqL 2026-08-29 4/200 2026-08-30 01:34 by ZPa0EcMwuECS
[基金申请] 国自然面上复盘~欢迎讨论 (金币+15) +15 晴天加油 2026-08-26 16/800 2026-08-29 18:28 by symmetry
[基金申请] 有没有仍没收到信息的 +5 德尚中行 2026-08-27 6/300 2026-08-29 18:10 by manplx
[基金申请] 基金不中,共勉 +11 eulota 2026-08-26 11/550 2026-08-28 14:22 by 火星超人xi
[基金申请] 面上合作单位盖章 +5 ssyjh 2026-08-27 5/250 2026-08-27 20:50 by gdfollow
[基金申请] 为什么 国际(地区)合作与交流项目 没有放榜? 10+3 majunge000 2026-08-26 11/550 2026-08-27 08:42 by 北京莱茵编辑
[基金申请] 国合可查了 +3 paperzjh 2026-08-26 3/150 2026-08-26 10:41 by LemmonTr
[基金申请] 如果此刻你正在为国基感到焦虑,不妨来听听这首《基金之外》 +8 scalable 2026-08-24 8/400 2026-08-25 12:52 by jnhyjjm
信息提示
请填处理意见