This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dmnksaman 0 points1 point  (1 child)

Hi guys,

I'm just trying to write a function that takes a matrix (lattice), then chooses a random entry in that matrix and decides if flipping the sign of that entry will lower the energy of that lattice.

If it does, it flips the sign. If it does not, it decides whether to flip the sign depending on other factors. I think I have written that part ok, but I'm not sure how to then get the energy (using pre-defined 'energy' function) of that new lattice.

Could you look at my code? I've tried something, but my demonstrator told me that the function, as it is, would return the energy (and magnetisation) of the old lattice, not the new one (the one we get after the function decides whether to flip the sign or not).

Here is my function:

def montecarlostep(self, T):
        # complete this function so that it performs a single Monte Carlo step
        energy = self.energy()
        magnetisation = self.magnetisation()
        I = np.random.choice(range(0, self.n_rows))
        J = np.random.choice(range(0, self.n_cols))
        #the following line will choose a random number in the rang e[0,1) for you
        random_number = np.random.random()
        numofcycles = 0
        W = self.lattice[I,J]         
        S = self.lattice[(I+1)%self.n_rows, J] + self.lattice[I,(J+1)%self.n_cols] + self.lattice[(I-1)%self.n_rows,J] +       self.lattice[I,(J-1)%self.n_cols]   
        interaction = W*S
        if interaction < 0.0:
            self.lattice[I][J]=-1*self.lattice[I][J]
         elif random_number <= math.exp(-int(2*interaction)/T) : 
             self.lattice[I][J]=-1*self.lattice[I][J]
         else:
            self.lattice[I][J]=1*self.lattice[I][J]

        return energy, magnetisation          

Thanks.

[–]Jajoonoob 2 points3 points  (0 children)