I’m working on a 2D Ising model that’s supposed to simulate a sheet of electrons. I had this working in Python, but it seemed to be running too slow once I tried to upscale it too much. I’ve been stumbling through C++ to try and translate it, and this is what I have. Although not fully functional, I just need it to run and spit out a few energy and magnetization levels. However, I’m stuck with the error “expected unqualified-id before ‘{‘ token’ at line 23 at the initEnergy() function. Is there something I missed as far as syntax goes, or have I made some other dumb mistake? Thank you for any help provided.
#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";
}
}
What’s that semi-colon doing there? You have one after every function signature. That is wrong, they need to be removed. Otherwise it is interpreted as a function prototype with a block of code afterward, which is invalid.
Is correct;