24小时热门版块排行榜    

查看: 1187  |  回复: 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文章,我:8O5.5.1.O.54,科目齐全,可+急 +3 uKOSCocCb6Fm 2026-11-12 7/350 2026-11-15 03:32 by xSrQNSSjcgoX
[考研] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +3 uKOSCocCb6Fm 2026-11-12 5/250 2026-11-15 03:21 by xSrQNSSjcgoX
[考博] 27博士 +6 锦鲤送福至 2026-11-10 6/300 2026-11-14 21:23 by xupeii
[基金申请] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 XJXMuRjD7cK7 2026-11-13 3/150 2026-11-13 23:37 by 6PrO1XyZ5URF
[考研] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +5 C6CRn66RYYXS 2026-11-10 6/300 2026-11-13 11:04 by Equinoxhua
[硕博家园] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +5 C6CRn66RYYXS 2026-11-10 9/450 2026-11-13 11:02 by Equinoxhua
[考博] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 Z7qfCsdIiD1T 2026-11-10 5/250 2026-11-12 11:25 by HajghKOMynmz
[考研] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +5 Z7qfCsdIiD1T 2026-11-10 8/400 2026-11-12 11:22 by HajghKOMynmz
[教师之家] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +4 Z7qfCsdIiD1T 2026-11-10 9/450 2026-11-12 11:22 by HajghKOMynmz
[教师之家] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +4 Z7qfCsdIiD1T 2026-11-10 7/350 2026-11-12 11:22 by HajghKOMynmz
[硕博家园] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +5 Z7qfCsdIiD1T 2026-11-10 8/400 2026-11-12 11:20 by HajghKOMynmz
[教师之家] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +4 Z7qfCsdIiD1T 2026-11-10 5/250 2026-11-12 11:14 by HajghKOMynmz
[教师之家] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +5 czAiolzR0IJC 2026-11-10 7/350 2026-11-12 11:09 by HajghKOMynmz
[基金申请] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +4 czAiolzR0IJC 2026-11-10 8/400 2026-11-12 11:00 by HajghKOMynmz
[硕博家园] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +4 czAiolzR0IJC 2026-11-10 8/400 2026-11-12 10:49 by HajghKOMynmz
[考博] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +4 C6CRn66RYYXS 2026-11-10 6/300 2026-11-12 10:27 by HajghKOMynmz
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +6 FChKX8meu1ds 2026-11-09 11/550 2026-11-12 09:57 by HajghKOMynmz
[教师之家] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +7 FChKX8meu1ds 2026-11-09 12/600 2026-11-12 09:43 by HajghKOMynmz
[考研] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +8 cqv1zKnzd7hr 2026-11-09 12/600 2026-11-12 09:32 by HajghKOMynmz
[基金申请] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +6 zSbvVX9JFl9J 2026-11-09 9/450 2026-11-12 09:21 by HajghKOMynmz
信息提示
请填处理意见