電子のシートをシミュレートすることになっている 2D Ising モデルに取り組んでいます。私はこれを Python で動作させましたが、アップスケールしすぎると実行が遅すぎるように見えました。私はC++を試して翻訳しようとつまずきましたが、これが私が持っているものです。完全に機能するわけではありませんが、実行していくつかのエネルギーと磁化レベルを吐き出すだけで十分です。しかし、initEnergy() 関数の 23 行目で「expected unqualified-id before '{' token'」というエラーが表示されて困っています。構文に関する限り、見逃したものはありますか?ご協力ありがとうございました。
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#define T 2
#define n 10
#define iterations 10000
#define dataInterval 100
#define repeat 4
using namespace std;
using std::vector;
int sweep = n*n; //number of trials that constitutes one sweep of the system
int trials = sweep * iterations; //number of trial changes
vector<vector<int> > state;
int initEnergy(int state);{
int energy = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i == n-1){
if (j != n-1){
//if you're on the right side and not in the corner
energy = energy + state[i][j]*state[i][j+1];
}
}
//if you're on the bottom edge and not in the corner
else if (j == n-1){
energy = energy + state[i][j]*state[i+1][j];
}
else{
energy = energy + state[i][j]*state[i][j+1]*state[i+1][j]; //add the energy of dipole and right and bottom partners
}
}
}
return -energy
}
int initMag(state);{
int mag = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
mag = mag + state[i][j];
}
}
return mag
}
int dEnergy(i, j, n, state);{
if (i != 0)
left = state[i-1][j];
else
left = 0;
if (i != n-1)
right = state[i+1][j];
else
right = 0;
if (j != 0)
top = state[i][j-1];
else
top = 0;
if (j != n-1)
bottom = state[i][j+1];
else
bottom = 0;
return 2*state[i][j]*(left+right+top+bottom);
}
int main()
{
srand(time(0));
state.resize(n);
for (int i = 0; i < n; i++){
state[i].resize(n);
}
vector<vector<int>> E;
E.resize(repeat);
for (int i = 0; i < repeat; i++){
E[i].resize((iterations-5000)/dataInterval+1)
}
vector<vector<int>> M;
M.resize(repeat);
for (int i = 0; i < repeat; i++){
M[i].resize((iterations-5000)/dataInterval+1)
}
for (int setup = 1; setup <= repeat; setup++){
//1. Establish an initial microstate.
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (rand()%1 > 0.5)
state[i][j] = 1;
else
state[i][j];
}
}
//2. Make a random trial change in the microstate.
Estate = initEnergy(state)
Mstate = initMag(state)
for (int m = 1; m <= trials; m++){
int i = rand()%n;
int j = rand()%n;
//3. Compute dE, the change in the energy of the system due to the trial change.
int dE = dEnergy(i, j, n, state);
//4. If dE is less than or equal to zero, accept the new microstate and go to step 8.
if ((dE <= 0) || (rand() < exp(-dE/T))){
state[i][j] = -state[i][j];
Estate = Estate + dE;
Mstate = Mstate + 2 * state[i][j];
}
//5. If dE is positive, compute the quantity w = e^(-b*dE).
//6. Generate a random number r in the unit interval.
//7. If r <= w, accept the new microstate; otherwise retain the previous microstate.
//8. Determine the value of the desired physical quantities.
if (m%(dataInterval*sweep) == 0){
print("Finished sweep " + str(m/sweep) + " of iteration " + str(setup));
if (m >= sweep*5000){
E[repeat-1][m/(dataInterval*sweep)-5000] = Estate;
M[repeat-1][m/(dataInterval*sweep)-5000] = Mstate;
}
}
}
cout << E + "\n" + M + "\n";
}
}