196 CHAPTER 11. MONTE CARLO METHODS IN STATISTICAL PHYSICS
ofstream o f i l e ;
/ / inli ne fu nc tion for periodic boundary condition s
in line int per iodi c ( int i , int limit , int add ) {
return ( i + l i m it +add ) % ( limit ) ;
}
/ / Function to read in data from screen
void read_input ( int & , int & , double & , double & , double&) ;
/ / Function to i n i t i a l i s e energy and magnetization
void i n i t i a l i z e ( int , double , int , double & , double&) ;
/ / The metropolis algorithm
void Metropolis ( int , long & , int , double & , double & , double ) ;
/ / p r int s to f i l e the r e s u l t s of the c alcula t io n s
void output ( int , int , double , double ) ;
int main ( int argc , char argv [ ] )
{
char outfilename ;
long idum ;
int spin_matrix , n_spins , mcs ;
double w[17 ] , average [5 ] , initial_ temp , final_temp , E , M, temp_step ;
/ / Read in output f i l e , abort i f there are too few command l ine
arguments
i f ( argc <= 1 ) {
cout < < < < argv [0] < <
< < endl ;
ex i t (1) ;
}
el se {
outfilename =argv [ 1 ] ;
}
o f i l e . open ( outfilename ) ;
/ / Read in i n i t i a l values such as s i ze of l a t t i c e , temp and
cyc les
read_input ( n_spins , mcs , initia l_te mp , final_temp , temp_step ) ;
spin_matrix = ( int ) matrix ( n_spins , n_spins , s izeof ( int ) ) ;
idum = 1; / / random s ta rt ing point
for ( double temp = in i t i al_t e m p ; temp <= final_temp ; temp+=
temp_step ) {
/ / i n i t i a l i s e energy and magnetization
E = M = 0 . ;
/ / setup array for poss ible energy changes
for ( int de = 8; de <= 8 ; de++) w[ de ] = 0 ;
for ( int de = 8; de <= 8 ; de+=4) w[ de +8] = exp( de / temp ) ;