Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:22:51+00:00 2026-06-14T04:22:51+00:00

I’m working on a 2D Ising model that’s supposed to simulate a sheet of

  • 0

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";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:22:52+00:00Added an answer on June 14, 2026 at 4:22 am
    int initEnergy(int state);{
    

    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.

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

    Is correct;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.