#! /usr/bin/env python3 # def ising_2d_simulation ( m, n, iterations, thresh ): #*****************************************************************************80 # ## ising_2d_simulation() carries out a 2D Ising simulation. # # Discussion: # # Note that, when all the cells are updated in a single cycle, there is # a mathematically stable checkerboard solution, in which all the reds # and blacks flip color repeatedly. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 November 2022 # # Author: # # John Burkardt # # Input: # # integer M, N: the dimensions of the region. # # integer ITERATIONS: the number of iterations. # # real THRESH: the threshold value. # import numpy as np print ( '' ) print ( 'ising_2d_simulation():' ) print ( ' Monte Carlo simulation of a 2D Ising model.' ) # # Define the probability of flipping if you are in a neighborhood of # 1, 2, 3, 4, or 5 of the same sign. # # This should really be some kind of exponential dependent on "temperature". # prob = np.array ( [ 0.98, 0.85, 0.50, 0.15, 0.02 ] ) print ( '' ) print ( ' The row dimension M =', m ) print ( ' The column dimension N =', n ) print ( ' The number of iterations taken is ITERATIONS =', iterations ) print ( ' The threshhold THRESH =', thresh ) print ( '' ) print ( ' The transition probability table, based on the number of' ) print ( ' neighbors with the same charge:' ) print ( '' ) print ( prob ) # # Initialize C1. # c1 = ising_2d_initialize ( m, n, thresh ) # # Do the simulation. # c1 = transition ( m, n, iterations, prob, c1 ) return def ising_2d_agree ( m, n, c1 ): #*****************************************************************************80 # ## ising_2d_agree() returns the number of neighbors agreeing with each cell. # # Discussion: # # The count includes the cell itself, so it is between 1 and 5. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 November 2022 # # Author: # # John Burkardt # # Input: # # integer M, N, the number of cells in each spatial dimension. # # integer C1(M,N), an array of 1's and -1's. # # Output: # # integer C5(M,N), the number of neighbors that agree. # 1, 2, 3, 4, or 5. # import numpy as np c5 = c1 \ + np.roll ( c1, -1, axis = 0 ) \ + np.roll ( c1, 1, axis = 0 ) \ + np.roll ( c1, -1, axis = 1 ) \ + np.roll ( c1, 1, axis = 1 ) c5[0