24小时热门版块排行榜    

查看: 946  |  回复: 0

咔咔咔咔

新虫 (初入文坛)

[求助] 二维ising模型蒙特卡洛程序出错,求指点

#include
#include
#include
#include
#define L 150                        
#define preN   100000               
#define lateN 5100000               
#define deltaN (lateN-preN)         
double recordEnergy[deltaN];         
int recordMagnet[deltaN];            
int lattice[L][L];                  
int temp[L][L];                     
double energy;                       
int magnet;                          
double T;                           
struct CX                           
{
    double C;
    double X;
};

void InitialLattice();
void SaveInitial();
void GetInitial();
void CaculateFirstMagnet();
void CaculateFirstEnergy();
void GenerateNext();
struct CX Caculate_C_and_X();

int main()
{
int i;
    struct CX cx[200];
    FILE *ft,*fx,*fc;
ft=fopen("T.txt","w";
fx=fopen("X.txt","w";
fc=fopen("C.txt","w";
    srand(time(NULL));

T = 1.0;
for(i=0;T<4.0;i++)
{
  InitialLattice();
  cx=Caculate_C_and_X();
  fprintf(ft,"%f\n",T);
  fprintf(fx,"%f\n",cx.X);
  fprintf(fc,"%f\n",cx.C);
  printf("T=%f ",T);
  printf("  X=%f ",cx.X);
  printf("  C=%f\n",cx.C);
        T = T+0.05;
}

fclose(ft);
fclose(fx);
fclose(fc);
return 0;
}

void InitialLattice()                             
{                                                
    int i,j;                                    
    for(i=0;i     {
        for(j=0;j   {
   if((rand()%10)>5) lattice[j]=1;
   else              lattice[j]=1;     
        }
    }
}
void SaveInitial()                                 
{                                                  
int k,m;                                       
for(k=0;k {
  for(m=0;m    temp[k][m]=lattice[k][m];
}
}
void GetInitial()                                 
{                                                  
int k,m;                                       
for(k=0;k {
  for(m=0;m    lattice[k][m]=temp[k][m];

}
}
void CaculateFirstMagnet()                          
{
    int i,j;
    int iMagnet=0;
    for(i=0;i     {
        for(j=0;j    iMagnet += lattice[j];
    }
    magnet = iMagnet;
}
void CaculateFirstEnergy()                          
{

    double dEnergy = 0.0;
int up,down,right,left,px,py;
for(px=0;px {
  for(py=0;py   {
   up = (L+px-1)%L;
   down = (px+1)%L;
   right = (L+py-1)%L;
   left = (py+1)%L;
   dEnergy += 0.5*lattice[px][py]*(lattice[up][py]+lattice[down][py]+lattice[px][right]+lattice[px][left]);
  }
}

energy = dEnergy;
}
void GenerateNext()                                    
{
    int px,py;
    int dE,sum;
int up,down,right,left;
px = rand()%L;
    py = rand()%L;

up = (L+px-1)%L;
down = (px+1)%L;
right = (L+py-1)%L;
left = (py+1)%L;

sum = (lattice[px][py])*(lattice[up][py]+lattice[down][py]+lattice[px][right]+lattice[px][left]);
switch(sum)
{
case 4: dE= 8; break;
case 2: dE= 4; break;
case 0: dE= 0; break;
case -2: dE= -4; break;
case -4: dE= -8; break;
default: printf("---Erro!---\n"; break;
}

    if( dE>0 )                                             
    {
        if((rand()*1.0/RAND_MAX) <= exp((-1.0)*dE/T))
  {
   lattice[px][py] = -lattice[px][py];
   magnet += 2*lattice[px][py];                  
   energy += dE;                                 
  }

    }
    else
    {
  lattice[px][py] = -lattice[px][py];
  magnet += 2*lattice[px][py];                       
  energy += dE;                                      
    }

}
struct CX Caculate_C_and_X()                              
{
    struct CX cx;
    int i;
    double AvgEnergy=0.0;
    double AvgSquareEnergy=0.0;
    double AvgMagnet=0.0;
    double AvgSquareMagnet=0.0;

CaculateFirstMagnet();                                 
CaculateFirstEnergy();                                 

    for(i=0; i     {
        GenerateNext();                                      
        if(i>=preN)                                          
        {
            recordEnergy[i-preN] = energy;                  
            recordMagnet[i-preN] = magnet;                  
  }
}

    for(i=0;i     {
        AvgEnergy += (double)recordEnergy/deltaN;
        AvgSquareEnergy += (double)recordEnergy*recordEnergy/deltaN;
        AvgMagnet += (double)recordMagnet/deltaN;
        AvgSquareMagnet += (double)recordMagnet*recordMagnet/deltaN;
    }

    cx.C = (AvgSquareEnergy - AvgEnergy*AvgEnergy)/T/T;      
    cx.X = (AvgSquareMagnet - AvgMagnet*AvgMagnet)/T;
  
    return cx;
}



:\microsoft visual studio\myprojects\ising\ising.cpp(44) : error C2440: '=' : cannot convert from 'struct CX' to 'struct CX [200]'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
d:\microsoft visual studio\myprojects\ising\ising.cpp(46) : error C2228: left of '.X' must have class/struct/union type
d:\microsoft visual studio\myprojects\ising\ising.cpp(47) : error C2228: left of '.C' must have class/struct/union type
d:\microsoft visual studio\myprojects\ising\ising.cpp(49) : error C2228: left of '.X' must have class/struct/union type
d:\microsoft visual studio\myprojects\ising\ising.cpp(50) : error C2228: left of '.C' must have class/struct/union type
d:\microsoft visual studio\myprojects\ising\ising.cpp(67) : error C2440: '=' : cannot convert from 'const int' to 'int [150]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
d:\microsoft visual studio\myprojects\ising\ising.cpp(68) : error C2440: '=' : cannot convert from 'const int' to 'int [150]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
d:\microsoft visual studio\myprojects\ising\ising.cpp(98) : error C2297: '+=' : illegal, right operand has type 'int [150]'
d:\microsoft visual studio\myprojects\ising\ising.cpp(187) : error C2440: 'type cast' : cannot convert from 'double [5000000]' to 'double'
        There is no context in which this conversion is possible
d:\microsoft visual studio\myprojects\ising\ising.cpp(188) : error C2440: 'type cast' : cannot convert from 'double [5000000]' to 'double'
        There is no context in which this conversion is possible
d:\microsoft visual studio\myprojects\ising\ising.cpp(189) : error C2440: 'type cast' : cannot convert from 'int [5000000]' to 'double'
        There is no context in which this conversion is possible
d:\microsoft visual studio\myprojects\ising\ising.cpp(190) : error C2440: 'type cast' : cannot convert from 'int [5000000]' to 'double'
        There is no context in which this conversion is possible
执行 cl.exe 时出错.

ising.obj - 1 error(s), 0 warning(s)
怎么办
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 咔咔咔咔 的主题更新
信息提示
请填处理意见