Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ising一维C-规划_C - Fatal编程技术网

Ising一维C-规划

Ising一维C-规划,c,C,我试图模拟伊辛模型1-D。该模型由一个自旋链(100个自旋)组成,如果系统(幺正)能量下降或小于一个随机数,则使用Mont Carlo-Metropolis接受自旋翻转。 在正确的程序中,能量和磁化强度都变为零,我们得到的结果是高斯分布(能量图或磁化强度的蒙特卡罗步数) 我做了一些工作,但我认为我的随机生成器对此不正确,我不知道如何/在何处实现边界条件:链的最后一个旋转是第一个旋转。 我需要帮助来完成它。欢迎任何帮助。非常感谢。 我正在粘贴我的C程序: #include

我试图模拟伊辛模型1-D。该模型由一个自旋链(100个自旋)组成,如果系统(幺正)能量下降或小于一个随机数,则使用Mont Carlo-Metropolis接受自旋翻转。 在正确的程序中,能量和磁化强度都变为零,我们得到的结果是高斯分布(能量图或磁化强度的蒙特卡罗步数)

我做了一些工作,但我认为我的随机生成器对此不正确,我不知道如何/在何处实现边界条件:链的最后一个旋转是第一个旋转。 我需要帮助来完成它。欢迎任何帮助。非常感谢。 我正在粘贴我的C程序:

            #include <stdio.h>
            #include <stdlib.h>
            #include <math.h>
            #include <time.h>     //necessary for function time()
            #define LENGTH 100    //size of the chain of spins
            #define TEMP   2      // Temperature in units of J
            #define WARM   200  // Termalização
            #define MCS    20000 //Monte Carlo Steps



            void start( int spin[])
            {
                /* starts with all the spins 1 */
                int i;
                 for (i = 0 ; i < 100; i++)
                 {
                 spin[i] = 1;
                 }
            }
            double energy( int spin[]) //of the change function J=1
            {
                int i;
                double energyX=0;// because the begining Energy = -J*sum (until 100) =-100,

                      for (i = 0;i<100;i++)
                        energyX=energyX-spin[i]*spin[i+1];
                return(energyX);
            }
            int randnum(){
                 int num;
                 srand(time(NULL));



                 /* srand(time(NULL)) objectives to initiate the random number generator
                    with the value of the function time(NULL). This is calculated as being the
                    total of seconds passed since january first of 1970 until the present date.
                    So, this way, for each execution the value of the "seed" will be different.

                 */
                 srand(time(NULL));

                 //picking one spin randomly zero to 100
                        num=rand() % 100;
                 printf("num = %d ", num);
                return num;
            }
            void montcarlo( int spin[])
            {
            int i,j,num;
            double prob;
            double energyA, energyB; // A -> old energy and B -> the new energy
            int rnum1,rnum2;
            prob=exp(-(energyB-energyA)/TEMP);

              energyA = 0;
              energyB = 0;

                 for (i = 0;i<100;i++)
                    {
                         for (j = 0;j<100;j++)
                         {

                        energyA=energy(spin);

                        rnum1=randnum();
                        rnum2=randnum(); // i think they will give me different numbers

                        spin[rnum1] = -spin[rnum1]; //flip of the randomly selected spin

                        energyB = energyB-spin[j]*spin[j+1];


                        if ((energyB-energyA<0)||((energyB-energyA>0)&&(rnum2>prob))){ // using rnum2 not to be correlated if i used rnum1
                         spin[rnum1]=spin[rnum1];} // keep the flip

                         else if((energyB-energyA>0)&&(rnum2<prob))
                            spin[rnum1]=-spin[rnum1]; // unflip

                         }
                    }

            }
            int Mag_Moment( int spin[] ) // isso é momento magnetico
            {
                int i;
                int mag;


                      for (i = 0 ; i < 100; i++)
                      {
                      mag = mag + spin[i];

                      }


                    return(mag);
            }


            int main()
            {
              // starting the spin's chain
              int spin[100];//the vector goes til LENGHT=100
              int i,num,j;
              int itime;
              double mag_moment;

                start(spin);

            double energy_chain=0;
                   energy_chain=energy(spin); // that will give me -100 in the begining
                         printf("energy_chain starts with %f", energy_chain);// initially it gives -100
             /*Warming it makes the spins not so ordered*/

                 for (i = 1 ; i <= WARM; i++)
                 {
                     itime = i;
                     montcarlo(spin);
                 }
            printf("Configurtion after warming %d \n", itime);
                     for (j = 0 ; j < LENGTH; j++)
                     {
                     printf("%d",spin[j]);
                     }

                     printf("\n");

                     energy_chain=energy(spin); // new energy after the warming

              /*openning a file to save the values of energy and magnet moment of the chain*/

                FILE *fp; // declaring the file for the energy
                FILE *fp2;// declaring the file for the mag moment
                fp=fopen("energy_chain.txt","w");
                fp2=fopen("mag_moment.txt","w");

                int pures;// net value of i
                int a;



            /* using Monte Carlo metropolis for the whole chain */
                 for (i = (WARM + 1) ; i <= MCS; i++)
                 {
                     itime=i;//saving the i step for the final printf.
                     pures = i-(WARM+1);

                montcarlo(spin);

                energy_chain = energy_chain + energy(spin);// the spin chain is moodified by void montcarlo
                mag_moment = mag_moment + Mag_Moment(spin);

             a=pures%10000;// here i select a value to save in a txt file for 10000 steps to produce graphs

            if (a==0){
                        fprintf(fp,"%.12f\n",energy_chain); // %.12f just to give a great precision
                        fprintf(fp2,"%.12f\n",mag_moment);
                      }


                 }
                  fclose(fp); // closing the files
                  fclose(fp2);

            /* Finishing -- Printing */
               printf("energy_chain = %.12f\n", energy_chain);
               printf("mag_moment = %.12f \n", mag_moment);
               printf("Temperature = %d,\n Size of the system = 100 \n", TEMP);
               printf("Warm steps = %d, Montcarlo steps = %d \n", WARM , MCS);

               printf("Configuration in time %d \n", itime);
                     for (j = 0 ; j < 100; j++)
                     {
                     printf("%d",spin[j]);
                     }
                     printf("\n");


              return 0;
            }
#包括
#包括
#包括
#包括//功能时间()所需的
#定义长度100//旋转链的大小
#定义温度2//以J为单位的温度
#定义温暖200//Termalizaão
#定义MCS 20000//Monte Carlo步骤
无效开始(整数旋转[])
{
/*从所有旋转1开始*/
int i;
对于(i=0;i<100;i++)
{
自旋[i]=1;
}
}
变化函数J=1的双能量(int-spin[])/
{
int i;
double energyX=0;//因为起始能量=-J*sum(直到100)=-100,
对于(i=0;i旧能源和B->新能源
int rnum1,rnum2;
prob=exp(-(energyB energyA)/TEMP;
energyA=0;
能量b=0;

对于(i=0;i0)&&(rnum2您应该在程序中只调用一次
srand(time(NULL));
。每次在同一秒内调用它,您都会得到相同的随机数序列。因此,很可能对
randnum
的两次调用都会给出相同的数字


只需在
main
的开头添加
srand(time(NULL));
并将其删除。

您应该调用
srand(time(NULL))
在程序中只调用一次。每次在同一秒钟内调用此函数时,都会得到相同的随机数序列。因此,很可能对
randnum
的两次调用都会得到相同的数


只需在
main
的开头添加
srand(time(NULL));
并将其删除到其他地方。

我认为,我在这段代码中看到了许多错误。第一个错误是重新植入
srand()
每个已经被处理过的循环。许多循环超出了数组边界,例如:

  for (ii = 0;ii<100;ii++)
  {
    energyX = energyX - spin[ii]*spin[ii+1];
  }

用于计算自旋的初始概率是,
prob=exp(-(energyB energyA)/TEMP);
但两个能量值都没有初始化,也许这是有意的,但我认为最好只使用
rand()
Mag_矩()
函数从不初始化返回值,因此你得到的返回值是垃圾。你能告诉我你试图复制的算法吗?我只是好奇。

我想我在这段代码中看到了很多错误。第一个错误是重新植入
srand()
已寻址的每个循环。许多循环超出了数组边界,例如:

  for (ii = 0;ii<100;ii++)
  {
    energyX = energyX - spin[ii]*spin[ii+1];
  }

用于计算自旋的初始概率是,
prob=exp(-(energyB energyA)/TEMP);
但两个能量值都没有初始化,也许这是有意的,但我认为最好只使用
rand()
Mag_矩()
函数从不初始化返回值,因此你得到的返回值是垃圾。你能告诉我你试图复制的算法吗?我只是好奇。

好的,我试着只调用一次。好的,我试着只调用一次。你知道更好的数字随机生成器吗?你可以在维基百科:我做了一些修正,但这段代码并没有给我能量和磁矩都为零。可能是因为随机数或/和周期性边界条件。你知道更好的随机数生成器吗?你可以在维基百科上查看这个模拟:我做了一些修正,但这段代码没有给我能量和磁矩都为零。可能是因为随机数或周期性边界条件。