Untitled
unknown
c_cpp
a month ago
2.8 kB
1
Indexable
Never
#include<iostream> #include<fstream> #include<cstdlib> #include<cmath> #include<ctime> using namespace std; int main() { srand(time(NULL)); float rA = 1, rD = 2, alpha = 1; // rDi = rD * (alpha)^i float r[7]={}; r[1] = 1; for(int i=2;i<7;i++) r[i] = rD*pow(alpha,i-2); int N = 1000; int ln = 10; int L[ln+2][ln+2]={}; float P[7]={}; float Result[N][2]={}; float t=0; for(int count=1;count<=N;count++) { int E1[ln*ln][2]={}; int E2[ln*ln][2]={}; int E3[ln*ln][2]={}; int E4[ln*ln][2]={}; int E5[ln*ln][2]={}; int E6[ln*ln][2]={}; int E[ln*ln][2]={}; int n[7]={}; int des=0; int s; for(int i=2;i<=ln+1;i++) { for(int j=2;j<=ln+1;j++) { if(L[i][j]==0) { E1[n[1]][0]=i; E1[n[1]][1]=j; n[1]++; } else { E[des][0]=i; E[des][1]=j; s = L[i-1][j]+L[i+1][j]+L[i][j-1]+L[i][j+1]; switch(s) { case 0: E2[n[2]][0]=i; E2[n[2]][1]=j; n[2]++; break; case 1: E2[n[3]][0]=i; E2[n[3]][1]=j; n[3]++; break; case 2: E2[n[4]][0]=i; E2[n[4]][1]=j; n[4]++; break; case 3: E2[n[5]][0]=i; E2[n[5]][1]=j; n[5]++; break; case 4: E2[n[6]][0]=i; E2[n[6]][1]=j; n[6]++; break; } des++; } } } int len[6] = {0,n[2],n[3],n[4],n[5],n[6]}; float R=0; for(int k=1;k<=6;k++) R+=n[k]*r[k]; float theta = (n[2]+n[3]+n[4]+n[5]+n[6]); float ln_square = ln*ln; theta/=ln_square; Result[count][0] = t; Result[count][1] = theta; P[1]=(n[1]*r[1])/R; for(int k=1;k<=5;k++) P[k+1] = P[k] + (n[k+1]*r[k+1])/R; float rd = rand()%1001; rd/=1000; float rd2 = rand()%1001; rd2/=1000; int temp; if(rd<=P[1]) { temp = rd2*n[1]+1; if(temp>n[1]) temp = n[1]; int ei,ej; ei=E1[temp][0]; ej=E1[temp][1]; L[ei][ej]=1; if(ei==2) L[ln+2][ej]=1; else if(ei==ln+1) L[1][ej]=1; if(ej==2) L[ei][ln+2]=1; else if(ej==ln+1) L[ei][1]=1; } for(int p=1;p<=5;p++) { if((rd>P[p])&&(rd<=P[p+1])) { temp = rd2*n[p+1]+1; if(temp>n[p+1]) temp = n[p+1]; int loc=0; for(int sum=1;sum<=p-1;sum++) loc+=len[sum]; int ei,ej; ei=E[loc+temp][0]; ej=E[loc+temp][1]; L[ei][ej]=0; } } t+=1/R; } for(int i=0;i<N;i++) cout << Result[i][0] << " " << Result[i][1] << endl; ofstream outputFile("KMC.txt"); for(int i=0;i<N;i++) outputFile << Result[i][0] << " " << Result[i][1] << endl; outputFile.close(); }
Leave a Comment