24小时热门版块排行榜    

查看: 1122  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[教师之家] 焦虑 +8 水冰月月野兔 2026-03-13 10/500 2026-03-18 09:21 by 咪呜喵呜
[考研] 化学工程321分求调剂 +12 大米饭! 2026-03-15 15/750 2026-03-18 08:39 by 星空星月
[考研] 304求调剂 +9 小熊joy 2026-03-14 9/450 2026-03-18 04:33 by anny19840123
[考研] 一志愿天津大学化学工艺专业(081702)315分求调剂 +7 yangfz 2026-03-17 7/350 2026-03-17 23:57 by 星空星月
[考研] 328求调剂,英语六级551,有科研经历 +3 生物工程调剂 2026-03-16 8/400 2026-03-17 19:03 by Wangjingyue
[考研] 材料专硕274一志愿陕西师范大学求调剂 +5 薛云鹏 2026-03-13 5/250 2026-03-17 10:15 by Sammy2
[考研] 考研调剂 +3 淇ya_~ 2026-03-17 5/250 2026-03-17 09:25 by Winj1e
[考研] 326求调剂 +3 mlpqaz03 2026-03-15 3/150 2026-03-16 07:33 by Iveryant
[考研] 327求调剂 +6 拾光任染 2026-03-15 11/550 2026-03-15 22:47 by 拾光任染
[考研] 求老师收留调剂 +4 jiang姜66 2026-03-14 5/250 2026-03-15 20:11 by Winj1e
[考研] 070305求调剂 +3 mlpqaz03 2026-03-14 4/200 2026-03-15 11:04 by peike
[考研] 0703,333分求调剂 一志愿郑州大学-物理化学 +3 李魔女斗篷 2026-03-11 3/150 2026-03-13 22:24 by JourneyLucky
[考研] 材料工程调剂 +9 咪咪空空 2026-03-12 9/450 2026-03-13 22:05 by 星空星月
[考研] 求调剂 +5 一定有学上- 2026-03-12 5/250 2026-03-13 18:31 by ms629
[考研] 求调剂 +7 18880831720 2026-03-11 7/350 2026-03-13 16:10 by JourneyLucky
[考研] 307求调剂 +5 超级伊昂大王 2026-03-12 5/250 2026-03-13 15:56 by 棒棒球手
[考研] 295求调剂 +3 小匕仔汁 2026-03-12 3/150 2026-03-13 15:17 by vgtyfty
[论文投稿] 投稿问题 5+4 星光灿烂xt 2026-03-12 6/300 2026-03-13 14:17 by god_tian
[考研] 0817化学工程与技术考研312分调剂 +3 T123 tt 2026-03-12 3/150 2026-03-13 10:49 by houyaoxu
[考研] 290求调剂 +3 ADT 2026-03-13 3/150 2026-03-13 10:19 by peike
信息提示
请填处理意见