24小时热门版块排行榜    

Znn3bq.jpeg
北京石油化工学院2026年研究生招生接收调剂公告
查看: 1126  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿0703化学招61最终排名62化学求调剂 +7 招61排名62 2026-04-07 7/350 2026-04-07 21:21 by 83503孙老师
[考研] 316求调剂 +3 15318418673 2026-04-07 3/150 2026-04-07 20:05 by WTUChen
[考研] 求调剂 +11 张zic 2026-04-05 12/600 2026-04-07 17:20 by 橘咂!
[考研] 化工求调剂! +24 RichLi_ 2026-04-06 24/1200 2026-04-07 17:17 by 蓝云思雨
[考研] 307分材料专业求调剂 +9 Hll胡 2026-04-05 9/450 2026-04-07 16:41 by daiee
[考研] 学硕化学工程与技术,一志愿中国海洋大学320+求调剂 +9 披星河 2026-04-02 9/450 2026-04-07 12:53 by 尽舜尧1
[考研] 08600生物与医药-327 +9 18755400796 2026-04-05 9/450 2026-04-06 22:35 by 52305043001
[考研] 297分083200求助 +9 aekx 2026-04-05 9/450 2026-04-06 20:57 by flysky1234
[考研] 考研调剂 +3 Wwwwwww哇 2026-04-06 3/150 2026-04-06 20:55 by lbsjt
[考研] 求调剂 +4 wos666 2026-04-03 5/250 2026-04-06 15:22 by wos666
[考研] 一志愿河北工业大学材料工程,初试344求专硕调剂 +6 15933906766 2026-04-05 6/300 2026-04-06 13:21 by 无际的草原
[考研] 材料0856 英一数二 323 求调剂 +14 袁sy 2026-04-01 14/700 2026-04-05 18:18 by cql1109
[考研] 272求调剂 +4 电气李 2026-04-05 4/200 2026-04-05 10:41 by lbsjt
[考研] 085600调剂 +4 1amJJ 2026-04-02 4/200 2026-04-04 21:53 by hemengdong
[考研] 306求调剂 +3 hyb上名工 2026-04-02 3/150 2026-04-04 18:12 by 热情沙漠
[考研] 295求调剂 +3 尚偌呀 2026-04-03 4/200 2026-04-03 21:23 by zhq0425
[考研] 兽医调剂 +3 wh119216 2026-04-02 3/150 2026-04-03 19:34 by zrongyan
[考研] 320求调剂 +5 振—TZ 2026-04-02 5/250 2026-04-03 14:42 by fxue1114
[考研] 数一英一285求调剂 +7 AZMK 2026-04-03 9/450 2026-04-03 13:03 by ms629
[考研] 一志愿华南师范大学-22408计算机-292分-求华南师范大学调剂 +4 爱读书的小鳄鱼 2026-04-02 4/200 2026-04-02 18:35 by 求调剂zz
信息提示
请填处理意见