//Wang-Landau: update energy histogram void update_EnergyHistogram(double** ptrHistogram, Lattice lattice, double H_tot) { int L, j; L = lattice.area(); j = ( H_tot + L * 2 ) * 0.25; // bias by +L*2 (to get only positive indices) and then *0.25 is divide by 4 *ptrHistogram[j] += 1; }
int get_maxlikelyhood_coordinates(float* Matrix, std::vector<double>& coordinates, std::vector<double>& likelyhoods, Lattice lattice) { int D; if (lattice.get_dimensions() == 2) { D = lattice.area(); } else { D = lattice.volume(); } std::vector<double>::iterator max_likelyhood = std::max_element(likelyhoods.begin(),likelyhoods.end()); int distance = std::distance(likelyhoods.begin(), max_likelyhood); int S = distance * D; std::vector<double>::const_iterator it; int i = 0; for (it = (coordinates.begin() + S); it != (coordinates.begin() + S + D); ++it) { Matrix[i] = *it; ++i; } return distance; }
int get_alternativelikelyhood_coordinates(float* Matrix, std::vector<double>& coordinates, std::vector<double>& likelyhoods, Lattice lattice, int distance, double& H_tot, int K_iteration) { int D; if (lattice.get_dimensions() == 2) { D = lattice.area(); } else { D = lattice.volume(); } if (K_iteration == distance) { ++K_iteration; } H_tot = likelyhoods.at(K_iteration); int S = K_iteration * D; std::vector<double>::const_iterator it; int i = 0; for (it = (coordinates.begin() + S); it != (coordinates.begin() + S + D); ++it) { Matrix[i] = *it; ++i; } return K_iteration; }
//Wang-Landau: reset energy histogram void reset_EnergyHistogram(double* Histogram, Lattice lattice) { int L,j; L = lattice.area(); for (j=0; j < L - 1; j++) { Histogram[j] = 0; } }
//Wang-Landau : density of states map between array and pointer array void HistogramMap(double* Histogram, double** ptrHistogram, Lattice lattice) { int L = lattice.area(); for (int i = 2; i <= L-2; i++ ) { ptrHistogram[i] = &Histogram[i-1]; } ptrHistogram[0] = &Histogram[0]; ptrHistogram[L] = &Histogram[L-2]; }
int main(int argc, char ** argv) { std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); ////////////////////////////////////OpenOutput///////////////////////// std::ofstream output; /////////////////////////////////////////////////////////////////////// /////////////////////////////SetConstants///////////////////////////// Lattice lattice; lattice.set_dimensions(10, 10); double const J = 1; double const h = 0.; double label_coeff = FLT_MIN; //std::cout<<"Label Coefficient: "<<std::setprecision(16)<<label_coeff<<std::endl; int K = 100; int const mcycle = 500000; //100000 cycles double Z; /////////////////////////////////////////////////////////////////////// float* M = allocateMatrix_2D(lattice); float** ptrM = allocateptrMatrix_2D(lattice); matrixmap_2D(M, ptrM, lattice); std::vector<double> coordinates, likelyhoods, postlikelyhoods, list_X; list_X.reserve(K); RandomLib::Random r = initialise_random_number(); /////////////////////////Algorithm////////////////////////////////// ising_NestedSampling_Algorithm_2D(ptrM, M, coordinates, likelyhoods, postlikelyhoods, lattice, r, J, h, label_coeff, K, mcycle); std::vector<double>::const_iterator itp; for (itp = postlikelyhoods.begin(); itp != postlikelyhoods.end(); itp++) { std::cout<< *itp << " "; } std::cout<<""<<std::endl; //create list_X double X = 1; double const Kd = (double) K; for(itp = postlikelyhoods.begin(); itp != postlikelyhoods.end(); ++itp) { X = X * exp(-2/Kd);//(Kd-1)/Kd; //Kd/(Kd+1); list_X.insert(list_X.end(), X); } list_X.insert(list_X.begin(), 1); list_X.insert(list_X.end(), 0); /* double L = 0; int i = 0; for(itp = postlikelyhoods.begin(); itp != postlikelyhoods.end(); ++itp) { L += 0.5*( list_X.at(i) - list_X.at(i+2)); ++i; } std::cout<<"L: "<<L<<std::endl; */ //end list_X int S = postlikelyhoods.size(); double renorm = 2/list_X.at(S); output.open("../output/NestedSampling/NS_test_16.dat"); output<<"# 2D Nested-Sampling by Stefano Martiniani"<<std::endl; output<<"# kT \t A"<<std::endl; double A = 0; double b, c; for (c = 0.01; c <= 10; c += 0.01) { //Free energy integrate(postlikelyhoods, list_X, lattice, Z, c, renorm); A = -(1/c) * log(Z)/lattice.area(); b = 1/c; output<<b<<"\t"<<A<<std::endl; } output.close(); output.open("../output/NestedSampling/NS_U_16.dat"); output<<"# 2D Nested-Sampling by Stefano Martiniani"<<std::endl; output<<"# kT \t U"<<std::endl; //internal energy double U = 0; double E, F, arg, w; int i = 0; /* double G = 0; for(itp = postlikelyhoods.begin(); itp != postlikelyhoods.end(); ++itp) { G += 0.5*( list_X.at(i) - list_X.at(i+2)); ++i; } */ for (c = 0.01; c <= 10; c += 0.01) { E = 0; i = 0; for(itp = postlikelyhoods.begin(); itp != postlikelyhoods.end(); ++itp) { F = *itp; w = ( 0.5 * ( list_X.at(i) - list_X.at(i+2)) ) * renorm; //last two terms are for normalisation arg = -c*F; E = E + w*F*exp(arg) ; ++i; } integrate(postlikelyhoods, list_X, lattice, Z, c, renorm); U = (1/Z) * E / lattice.area(); b = 1/c; output<<b<<"\t"<<U<<std::endl; } //std::cout<<"Partition function: "<<Z<<std::endl; output.close(); output.open("../output/NestedSampling/NS_dos_16.dat"); output<<"# 2D Nested-Sampling by Stefano Martiniani"<<std::endl; output<<"# lnX \t lnL"<<std::endl; i = 1; for(itp = postlikelyhoods.begin(); itp != postlikelyhoods.end(); ++itp) { output<<log(renorm*list_X.at(i))<<"\t"<<-(*itp)<<std::endl; ++i; } output.close(); random_health_check(r); std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> time_span = std::chrono::duration_cast< std::chrono::duration<double> > (end - start); std::cout<<"It took "<< time_span.count() << "seconds for the whole program to run"<<std::endl; delete[] M; delete[] ptrM; coordinates.clear(); likelyhoods.clear(); postlikelyhoods.clear(); list_X.clear(); return 0; }