void vmc_walker<comp>::update(qmc_type* vmc_obj) { double old_wavefunction_value; // make a copy of the old configuration *(this->state_tmp)=*(this->state); old_wavefunction_value=this->wavefunction_value; vmc_obj->moveEngine->moveGaussian(*(this->state)); vmc_obj->geo->all_particles_pbc(*(this->state)); // update the wavefunction this->wavefunction_value=vmc_obj->wave->log_evaluate(this->state); ++(vmc_obj->n_metropolis); this->accept=metropolis(2*(this->wavefunction_value - old_wavefunction_value),vmc_obj->rand); if ( this->accept ) { ++(vmc_obj->success_metropolis); } else { this->wavefunction_value=old_wavefunction_value; *(this->state)=*(this->state_tmp); }; }
int main(int argc, char **argv) { double temp; int iterations, initial; lat_t lat; // Read in command line arguments if(argc != 4) { fprintf(stderr, "Usage: %s temperature iterations initial\n", argv[0]); fprintf(stderr, "initial=-1 => all spin down\n"); fprintf(stderr, "initial=1 => all spin up\n"); fprintf(stderr, "initial=0 => spin values randomized\n"); return -1; } temp = atof(argv[1]); iterations = atoi(argv[2]); initial = atoi(argv[3]); // Set up a random lattice srand(time(NULL)); if(initial < 0) { lat_fill(&lat, -1); } else if(initial > 0) { lat_fill(&lat, 1); } else { lat_rand(&lat); } // Perform Metropolis algoritm for given number of iterations metropolis(&lat, 1.0/BOLTZMANN/temp, MAGNETIC_EXCHANGE, callback, &iterations); return 0; }
int main(int argc, char **argv) { double temp; int i; int iterations, ensemble_size, initial; lat_t lat; callback_data_t cdata; // Read in command line arguments if(argc != 5) { fprintf(stderr, "Usage: %s temperature iterations ensemblesize initial\n", argv[0]); fprintf(stderr, "initial=-1 => all spin down\n"); fprintf(stderr, "initial=1 => all spin up\n"); fprintf(stderr, "initial=0 => spin values randomized\n"); return -1; } temp = atof(argv[1]); iterations = atoi(argv[2]); ensemble_size = atoi(argv[3]); initial = atoi(argv[4]); // Initialize random number generator srand(time(NULL)); // For each lattice in the ensemble, add up the magnetization corresponding // to each iteration number (e.g. iteration 1 has average magnetization 0.345, // iteration 2 has magnetization 0.234) cdata.iterations = iterations; cdata.magnetizations = (double*)malloc(sizeof(double)*iterations); for(i = 0; i < iterations; i += 1) { cdata.magnetizations[i] = 0; } for(i = 0; i < ensemble_size; i += 1) { // Set up lattice if(initial < 0) { lat_fill(&lat, -1); } else if(initial > 0) { lat_fill(&lat, 1); } else { lat_rand(&lat); } // Sum up per-iteration magnetizations for this lattice metropolis(&lat, 1.0/BOLTZMANN/temp, MAGNETIC_EXCHANGE, callback, &cdata); } // Show the per-iteration average magnetizations for(i = 0; i < iterations; i += 1) { printf("%10d %12.5f\n", i, cdata.magnetizations[i]/ensemble_size); } return 0; }
int main(int argc, char * argv[]) { int world_size; int error; int n; int T_todo; int i,j,k,l; int dim; int type; int my_rank; double ratio; int *spin_out, *spin_in; int dest, recv; int swap; double r; int flips; int swap_attempts, swap_success; double *Es ,*Ts, *E_out, *T_out; double *Rs, *R_out; double e_recv, e_mine; double T_max, T_min, T_steps, T_curr, t_recv; spintype *s; MPI_Status status_info; double coupl[3] = {-1,-1,-1}; coupling = coupl; /* Get in Command line Args */ if (argc != 8) { printf("Usage: a.out N dim type T_Min T_step T_max Flips\n"); exit(EXIT_FAILURE); } n = atoi(argv[1]); dim = atoi(argv[2]); type = atoi(argv[3]); T_min = atof(argv[4]); T_steps = atof(argv[5]); T_max = atof(argv[6]); flips = atoi(argv[7]); DEBUGLINE printf("Passed: %d %d %d %lf %lf %lf\n", n, dim, type, T_min, T_steps, T_max); /* Allocate S */ s = malloc(sizeof(spintype)*pow(n,dim)); spin_out = malloc(sizeof(int)* pow(n,dim)); spin_in = malloc(sizeof(int) * pow(n,dim)); if (s == NULL || spin_out == NULL || spin_in == NULL) { printf("Didn't get the memory for S :( \n"); exit(EXIT_FAILURE); } /* Setup */ switch (type) { case 1: setupSqrSystem(s,n, dim); break; case 2: setupTriSystem(s,n, dim); break; default: setupTriSystem(s,n, dim); } initSpins(s, n, dim); MPI_Init(&argc, &argv); //Find out how big the world is swap_attempts =0; swap_success =0; MPI_Comm_size(MPI_COMM_WORLD, &world_size); T_todo = (int)ceil(((T_max - T_min)/T_steps)/world_size); DEBUGLINE printf("EACH HAS %d to do\n", T_todo); if (world_size == 1) { printf("Only got 1 processor... Bailing out\n"); //exit(EXIT_FAILURE); } MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); if(my_rank ==0) printf("#Got %d processors \n", world_size); Es = malloc(sizeof(double)*T_todo); Ts = malloc(sizeof(double)*T_todo); Rs = malloc(sizeof(double)*T_todo); if (!Es || !Ts) { printf("Didn't get enough memory"); exit(EXIT_FAILURE); } if (my_rank ==0) { T_out = malloc(sizeof(double) *(world_size*T_todo+GOOD_MEASURE)); for(j=0; j<world_size;j++) { for (i = 0; i < T_todo; i++) { T_out[i+(j*T_todo)] = T_min + (world_size*i +j)* T_steps; // printf("#%d = %lf\n",i+j*T_todo, T_out[i+j*T_todo]); } } } // DEBUGLINE printf("Starting scatter\n", my_rank, j,T_curr); MPI_Scatter(T_out, T_todo, MPI_DOUBLE, Ts, T_todo, MPI_DOUBLE, 0, MPI_COMM_WORLD); // DEBUGLINE printf("%d, Finished Scatter\n has %d to do", my_rank ,T_todo); // for(i=0; i < T_todo; i++) // printf("%d: %lf\n", my_rank, Ts[i]); // //DEBUGLINE printf("%d entering main loop\n", my_rank); for (l = 0; l < T_todo; l++) { T_curr = Ts[l]; // printf("#%d: Running Metroprolis at %lf\n", my_rank, T_curr); swap_success=0; swap_attempts=0; for ( j =0; j < SWAPS; j ++) { //DEBUGLINE printf("#%d: Running Metroprolis run %d at %lf\n", my_rank, j,T_curr); metropolis(s, n, dim, flips, T_curr, &ratio, 0); for (k = 0; k < 2; k++) { /* Prepare spins for transport */ for (i =0; i <pow(n,dim); i++) spin_out[i] = s[i].s; dest = my_rank-1; recv = my_rank +1; e_mine = energy_calc(s,n,dim,0.0); if ((my_rank+1) %2 == k) { if(dest >=0 ) { swap_attempts++; // DEBUGLINE printf("%d: has energy %lf, Partner: %d\n", my_rank, e_mine,dest); MPI_Recv(&t_recv, 1, MPI_DOUBLE, dest, TEMP, MPI_COMM_WORLD, &status_info); MPI_Recv(&e_recv, 1, MPI_DOUBLE, dest, ENERGY, MPI_COMM_WORLD, &status_info); // DEBUGLINE printf("%d: ....Parnter answered\n", my_rank); r = rand(); r = (double)r/RAND_MAX; if (r < exp((1/(kb*T_curr) - 1/(kb*t_recv))*(e_mine - e_recv))) { swap_success++; swap = 1; MPI_Ssend(&swap, 1, MPI_INT, dest, SWAP, MPI_COMM_WORLD); // DEBUGLINE printf("%d: sending to %d\n", my_rank, dest); MPI_Ssend(spin_out, pow(n,dim), MPI_INT, dest, HIGH_T, MPI_COMM_WORLD); // DEBUGLINE printf("%d: Sent\n", my_rank); MPI_Recv(spin_in, pow(n,dim), MPI_INT, dest, LOW_T, MPI_COMM_WORLD, &status_info); } else { swap = 0; //DEBUGLINE printf("%d is at %lf has just rejected %d at %lf\n", my_rank, T_curr, dest, t_recv); MPI_Ssend(&swap, 1, MPI_INT, dest, SWAP, MPI_COMM_WORLD); } } } else { swap=0; if (recv <world_size) { swap_attempts++; MPI_Ssend(&T_curr, 1, MPI_DOUBLE, recv, TEMP, MPI_COMM_WORLD); // DEBUGLINE printf("%d: has energy %lf, Partner %d\n", my_rank, e_mine,recv); MPI_Ssend(&e_mine, 1, MPI_DOUBLE, recv, ENERGY, MPI_COMM_WORLD); // DEBUGLINE printf("%d: ....Parnter answered\n", my_rank); // DEBUGLINE("%d: Waiting for Swap confirmation......\n", my_rank); MPI_Recv(&swap, 1, MPI_INT, recv, SWAP, MPI_COMM_WORLD, &status_info); // DEBUGLINE printf("%d: ....Swap details recieved\n", my_rank); if(swap == 1) { swap_success++; // DEBUGLINE printf("%d: Waiting for data from %d\n", my_rank, recv); MPI_Recv(spin_in, pow(n,dim), MPI_INT, recv, HIGH_T, MPI_COMM_WORLD, &status_info); // DEBUGLINE printf("%d: Swapping\n", my_rank); // DEBUGLINE printf("%d: Recieved\n", my_rank); MPI_Ssend(spin_out, pow(n,dim), MPI_INT, recv, LOW_T, MPI_COMM_WORLD); } } } /* Put new spins into our system */ if (swap==1) { for (i =0; i < pow(n,dim); i++) s[i].s = spin_in[i]; } } metropolis(s, n, dim, flips, T_curr, &ratio, 0); } metropolis(s, n, dim, flips*10, T_curr, &ratio, 0); Es[l] = energy_calc(s, n, dim, 0.0); Rs[l] = (double) swap_success; //Rs[l] = (double) (my_rank==1 || my_rank==2)? (swap_success/2.0):swap_success; Rs[l] = (double) Rs[l]/swap_attempts; if (Rs[l] ==1 ) { printf("#%d: has swapped every time at %lf\n", my_rank, T_curr); } // if (Es[l] == 0) // printf("%d: Zero energy\n", my_rank); Ts[l] = T_curr; } //if (my_rank ==0) { k = world_size*T_todo; E_out = malloc(sizeof(double) *(world_size*T_todo+GOOD_MEASURE)); R_out = malloc(sizeof(double) *(world_size*T_todo+GOOD_MEASURE)); //} DEBUGLINE printf("#GATHERING Es\n"); MPI_Gather(Es, T_todo, MPI_DOUBLE, E_out, T_todo, MPI_DOUBLE, 0, MPI_COMM_WORLD); DEBUGLINE printf("#GATHERING Ts\n"); MPI_Gather(Ts, T_todo, MPI_DOUBLE, T_out, T_todo, MPI_DOUBLE, 0, MPI_COMM_WORLD); MPI_Gather(Rs, T_todo, MPI_DOUBLE, R_out, T_todo, MPI_DOUBLE, 0, MPI_COMM_WORLD); if (my_rank == 0) { for(i =0; i < T_todo*world_size; i++) printf("%lf\t%lf\t%lf\n", T_out[i], E_out[i], R_out[i]); } printf("#%d: My ratio was: %lf\n", my_rank, (double)swap_success/swap_attempts); MPI_Finalize(); return(0); }
void experiment(void) { long i, j, k; double Temp, Tstep, b, h, e, e2; double h_max = 0.0; newlattice(); Tstep = (Tmax - Tmin) / (samples - 1); if (!coldstart) { Temp = Tmax; Tstep = - Tstep; } else Temp = Tmin; // zero the averaging arrays for (i = 0; i < samples; i++) { energy_list[i] = 0.0; energy2_list[i] = 0.0; magnetiz_list[i] = 0.0; } // iterate over samples, runs, and Monte-Carlo steps for (i = 0; i < samples; i++) { //printf("\tTemp = %f\n",Temp); Temp_list[i] = Temp; beta = 1.0 / Temp; for (k = 0; k < equlibriate; k++) metropolis(); for (j = 0; j < runcount; j++) { for (k = 0; k < runsteps; k++) metropolis(); stats(); energy_list[i] += energy; energy2_list[i] += energy2; magnetiz_list[i] += abs_double(magnetiz); } Temp += Tstep; } // complete averaging and calculate heat capacities for (i = 0; i < samples; i++) { b = 1.0 / Temp_list[i]; e = energy_list[i] / runcount; e2 = energy2_list[i] / runcount; magnetiz_list[i] /= runcount; energy_list[i] = e; energy2_list[i] = e2; h = Boltzmann * b * b * (e2 - e * e); heatcapacity_list[i] = h; // screening for the critical point if (h > h_max) { h_max = h; sample_critical = i; } } }