24小时热门版块排行榜    

查看: 1001  |  回复: 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)

我们都爱小木虫

相关版块跳转 我要订阅楼主 咔咔咔咔 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考博] 【2027博士申请】纳米药物递送方向 20+3 13586093586 2026-08-03 5/250 2026-08-08 21:54 by 13586093586
[基金申请] 关于豆爷回答的JTJC与%2F数量 +5 yang182083 2026-08-06 7/350 2026-08-08 18:29 by zhanghaozhu
[基金申请] 2026国自然放榜时间 +3 布布和一二 2026-08-08 3/150 2026-08-08 18:15 by Lightingo
[硕博家园] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 qTvzQBRAHjCi 2026-08-07 4/200 2026-08-08 17:19 by oEVWOejN9taj
[基金申请] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +3 qTvzQBRAHjCi 2026-08-07 5/250 2026-08-08 17:02 by oEVWOejN9taj
[论文投稿] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 qTvzQBRAHjCi 2026-08-07 4/200 2026-08-08 16:47 by oEVWOejN9taj
[基金申请] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 Vi50GxzrFcSG 2026-08-07 5/250 2026-08-08 16:27 by oEVWOejN9taj
[考博] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +3 Vi50GxzrFcSG 2026-08-07 4/200 2026-08-08 16:27 by oEVWOejN9taj
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 Vi50GxzrFcSG 2026-08-07 5/250 2026-08-08 15:59 by oEVWOejN9taj
[硕博家园] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +4 HEQlVqMTIA7d 2026-08-07 6/300 2026-08-08 14:47 by oEVWOejN9taj
[博后之家] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +3 HEQlVqMTIA7d 2026-08-07 6/300 2026-08-08 14:42 by oEVWOejN9taj
[教师之家] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +4 HEQlVqMTIA7d 2026-08-07 5/250 2026-08-08 14:27 by oEVWOejN9taj
[论文投稿] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +3 KXLV3nuBVBY7 2026-08-07 6/300 2026-08-08 14:01 by oEVWOejN9taj
[考博] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +3 HEQlVqMTIA7d 2026-08-07 3/150 2026-08-08 03:47 by 6vVgjDL4CnGu
[基金申请] 娱乐 +6 Tide man 2026-08-03 6/300 2026-08-07 22:40 by 铁帽子农民
[基金申请] 听说今天filecode变了 +24 布布和一二 2026-08-06 47/2350 2026-08-07 16:02 by zhiyanjiang
[基金申请] filecode与中标关系的预测 +3 布布和一二 2026-08-07 3/150 2026-08-07 15:09 by gltch
[基金申请] 大家散了吧,后缀研究没有意义,别浪费时间了,过好目前的每一天,不要焦虑 +5 Tide man 2026-08-06 7/350 2026-08-07 13:11 by 医学老男孩
[基金申请] 8月时间戳变的,举个手。玩一下,释放压力 +9 archvillain 2026-08-04 11/550 2026-08-05 20:06 by wlwhappy
[基金申请] 有没有H口的?有收到消息的吗? +3 超级海虾 2026-08-04 3/150 2026-08-04 17:26 by 学教育滴
信息提示
请填处理意见