/* sweep_lattice_disorder() * Performans Monte Carlo sweeps. Sweeps the lattice once by choosing a random position and * proposing a spin flip using the Meteropolis Algorithm. This is done for the lattice size. */ void Clock2::sweep_lattice_disorder(float beta, std::mt19937 &engine) { for (size_t i = 0; i < size; i++) { size_t pos = static_cast<size_t>(rand0(engine) * size); // Compute new angle int new_angle; do { new_angle = static_cast<int>(rand0(engine) * q); } while(new_angle == spin[pos]); // Compute energy change float delta_E = 0.0; for (size_t i = 0; i < n_neigh; i++) { size_t neigh_angle = spin[neigh[pos].neighbor[i]]; size_t old_angle = spin[pos]; size_t old_idx = (old_angle - neigh_angle + q) % q; size_t new_idx = (new_angle - neigh_angle + q) % q; delta_E += J[pos].J_arr[i] * (cos_val[old_idx] - cos_val[new_idx]); } // Accept / reject new spin if (rand0(engine) < exp(-beta * delta_E)) spin[pos] = new_angle; } }
int main(int argc, char const *argv[]) { int count; for(count = 0; count < 5; count++) printf("%hd\n", rand0()); return 0; }
/* set_spin() * Sets the angle index representing the spin */ void Clock2::set_spin() { std::random_device rd; std::mt19937 engine(rd()); for (size_t i = 0; i < size; i++) spin[i] = static_cast<int>(rand0(engine) * q); }
int main(void) { int count; for (count = 0; count < 5; count++) printf("%d\n", rand0()); return 0; }
/* boxmuller transform. Turns uniform random numbers into * normally distributed random numbers, with mean 0 and * standard deviation 1. */ float gauss0(long *seed) { float x1, x2, y1, r, fac; static float y2; static int flag = 0; if (flag) { flag = 0; return y2; } do { x1 = rand0(seed)*2 - 1; x2 = rand0(seed)*2 - 1; r = x1*x1 + x2*x2; } while (r >= 1); fac = sqrt((-2*log(r)/r)); y1 = x1*fac; y2 = x2*fac; flag = 1; return y1; }
std::vector<Node*> DispersalFunctions::populate(std::vector<Node*> thisGeneration, int generation) { std::vector<Node*> children; float x,y;//,mindistance; //int j_x; float distance; int numberOfChildren; // bool ismin; bool *paired = new bool[thisGeneration.size()]; std::vector<NodePair> pairs; float cutoff = 90; int numFemale = 0; for (unsigned int i = 0; i < thisGeneration.size(); i++) paired[i] = false; for (unsigned int i = 0; i < thisGeneration.size(); i++) { // mindistance =std::numeric_limits<float>::max(); //ismin = false; //j_x = 0; if( thisGeneration[i]->_male == false && paired[i] == false) { numFemale++; for (unsigned int j = 0; j < thisGeneration.size(); j++) { if(thisGeneration[j]->_male == true && paired[j] == false) { x= (thisGeneration[i])->lat -(thisGeneration[j])->lat; y= (thisGeneration[i])->lon - (thisGeneration[j])->lon; distance = x*x+y*y; if(distance < cutoff) { NodePair x(i,j,distance); x._tmpDistance = 1.0/distance; pairs.push_back(x); } } } /* if(ismin) { paired[i] = true; paired[j_x] = true; numberOfChildren = settings->getNOffspring(generation, _rand); for (int j = 0; j < numberOfChildren; j++) { Node *child = new Node(generation + 1, thisGeneration[i], thisGeneration[j_x]); (thisGeneration[i])->children.push_back(child); (thisGeneration[j_x])->children.push_back(child); children.push_back(child); } }*/ } } Random rand0(42); for(int j=1;j<pairs.size();j++) { pairs[j]._tmpDistance += pairs[j-1]._tmpDistance; } double sum = pairs[pairs.size()-1]._tmpDistance; for(int i=0;i< 10000 && numFemale > 0 && pairs.size() > 0;i++) { NodePair asdasd(0,0,0); asdasd._tmpDistance =rand0.nextFloat()*sum; std::vector<NodePair>::iterator tmp = std::lower_bound(pairs.begin(), pairs.end(),asdasd); paired[tmp->indexFemale] = true; paired[tmp->indexMale] = true; numFemale--; numberOfChildren = settings->getNOffspring(generation, _rand); for (int j = 0; j < numberOfChildren; j++) { Node *child = new Node(generation + 1, thisGeneration[tmp->indexFemale], thisGeneration[tmp->indexMale]); (thisGeneration[tmp->indexFemale])->children.push_back(child); (thisGeneration[tmp->indexMale])->children.push_back(child); children.push_back(child); } if(i%(int)(log(thisGeneration.size())*log(thisGeneration.size())) == 0) { //todo: std::vector<NodePair> newPairs; for(int j=0;j<pairs.size();j++) { if(paired[pairs[j].indexFemale] == false && paired[pairs[j].indexMale] == false) { if(!newPairs.empty()) pairs[j]._tmpDistance = 1.0/pairs[j]._distance + newPairs[newPairs.size()-1]._tmpDistance; else pairs[j]._tmpDistance = 1.0/pairs[j]._distance; newPairs.push_back(pairs[j]); } } pairs.clear(); pairs = newPairs; sum = pairs[pairs.size()-1]._tmpDistance; } } delete[] paired; return children; }