3

我正在研究一个应该模拟一张电子的 2D Ising 模型。我有这个在 Python 中工作,但是一旦我试图将它升级太多,它似乎运行得太慢了。我一直在通过 C++ 来尝试翻译它,这就是我所拥有的。虽然功能不全,但我只需要它运行并吐出一些能量和磁化水平。但是,我在 initEnergy() 函数的第 23 行遇到了错误“在 '{' token' 之前的预期不合格 ID。就语法而言,我是否遗漏了什么,或者我犯了其他一些愚蠢的错误?感谢您提供的任何帮助。

#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";
    }
}
4

1 回答 1

1
int initEnergy(int state);{

那个分号在那里做什么?每个函数签名后都有一个。这是错误的,它们需要被删除。否则它被解释为一个函数原型,后面有一段代码,这是无效的。

int initEnergy(int state) {
    // code here
}

是正确的;

于 2012-11-10T00:35:53.367 回答