void SpringNetwork::Simulate(float dt) { // Get ready for conjugate gradient iterative solver step. // Initialize operands. if (!awake) return; CalcForces(); int n = X.count; // all our big vectors are of this size float3N dFdXmV(n); // temp to store result of matrix multiply float3N B(n); dV.Zero(); A.Identity(); // build up the big matrix we feed to solver A -= dFdV * dt + dFdX * (dt * dt); dFdXmV = dFdX * V; B = F * dt + dFdXmV * (dt * dt); ConjGradientFiltered(dV, A, B, S); V = V + dV; // V.element[0] = float3(0,0,0); // V.element[numX-1] = float3(0,0,0); X = X + V * dt; UpdateLimits(); awake = (dot(V, V) < cloth_sleepthreshold) ? awake - 1 : awake = cloth_sleepcount; }
void VelocityVerlet(float deltaT, float box, float rcut, float a, float c, float m, float n) { float deltaT2 = deltaT * deltaT; for (int h=0; h<num_replicas; h++) { for (int i = 0; i < N; i++){ // Update position position[h][i][0] += velocity[h][i][0]*deltaT + (1.0/2.0)*force[h][i][0]*deltaT2; position[h][i][1] += velocity[h][i][1]*deltaT + (1.0/2.0)*force[h][i][1]*deltaT2; position[h][i][2] += velocity[h][i][2]*deltaT + (1.0/2.0)*force[h][i][2]*deltaT2; // Update velocity at half step velocity[h][i][0] += force[h][i][0]*deltaT/2.0; velocity[h][i][1] += force[h][i][1]*deltaT/2.0; velocity[h][i][2] += force[h][i][2]*deltaT/2.0; } } // New forces based on new positions CalcForces(box, rcut, a, c, m, n); // Updates array of forces from F(t) to F(t + deltaT) // Update velocity at full step with new forces for (int h=0; h<num_replicas; h++) { for (int i = 0; i < N; i++){ velocity[h][i][0] += force[h][i][0]*deltaT/2.0; velocity[h][i][1] += force[h][i][1]*deltaT/2.0; velocity[h][i][2] += force[h][i][2]*deltaT/2.0; } } }
void CDM_FEA::Solve() //formulates and solves system! { RemoveDisconnected(); CalcDOF(); CalcBonds(); CalcStiffness(); //jmc: think it crashes here ApplyForces(); if (DOF != 0){ iparm[2] = -1; //sets to defualt system value... double ddum = 0; //Double dummy var int idum = 0; //Integer dummy var //msglvl = 0; //don't output info! phase = 13; // PARDISO(pt, &maxfct, &mnum, &mtype, &phase, &DOF, a, ia, ja, &idum, &nrhs, iparm, &msglvl, b, x, &error, dparm); // F77_FUNC(PARDISO)(pt, &maxfct, &mnum, &mtype, &phase, &DOF, a, ia, ja, &idum, &nrhs, iparm, &msglvl, b, x, &error, dparm); F77_FUNC(pardiso)(pt, &maxfct, &mnum, &mtype, &phase, &DOF, a, ia, ja, &idum, &nrhs, iparm, &msglvl, b, x, &error, dparm); //if (error != 0) std::cout << "Pardiso error! (" << error << ") - Phase 1\n"; if (error == -1) std::cout << "Pardiso error: Input inconsistent\n"; else if (error == -2) std::cout << "Pardiso error: Not enough memory\n"; else if (error == -3) std::cout << "Pardiso error: Reodering Problem\n"; else if (error == -4) std::cout << "Pardiso error: Zero pivot, numerical factorization or iterative refinement problem\n"; else if (error == -10) std::cout << "Pardiso error: No License file Pardiso.lic found\n"; else if (error == -11) std::cout << "Pardiso error: License is expired\n"; else if (error == -12) std::cout << "Pardiso error: Wrong username or hostname\n"; phase = -1; /* Release internal memory. */ // PARDISO(pt, &maxfct, &mnum, &mtype, &phase, &DOF, &ddum, ia, ja, &idum, &nrhs, iparm, &msglvl, &ddum, &ddum, &error, dparm); // F77_FUNC(PARDISO)(pt, &maxfct, &mnum, &mtype, &phase, &DOF, &ddum, ia, ja, &idum, &nrhs, iparm, &msglvl, &ddum, &ddum, &error, dparm); F77_FUNC(pardiso)(pt, &maxfct, &mnum, &mtype, &phase, &DOF, &ddum, ia, ja, &idum, &nrhs, iparm, &msglvl, &ddum, &ddum, &error, dparm); } //CalcMaxDisps(); FindMaxOverall(&Disp, x, MaxDisps); if (WantForces) CalcForces(); // OutputMatrices(); if (a != NULL) {delete [] a; a = NULL;} if (ia != NULL) {delete [] ia; ia = NULL;} if (ja != NULL) {delete [] ja; ja = NULL;} }
int main(int argc, char** argv){ // declare metal properties float m, a, n, c, density; // check for second and third arguments, and initialize metal properties accordingly if (argc < 2) { std::cout << "Program must be invoked with argument \'gold\' or \'silver\'\n\n"; return -1; // terminate with error } else if (strcmp(argv[1],"gold") == 0) { m = gold_m; a = gold_a; n = gold_n; c = gold_c; density = gold_density; setpoint_temperature[0] = gold_temperature; } else if (strcmp(argv[1],"silver") == 0) { m = silver_m; a = silver_a; n = silver_n; c = silver_c; density = silver_density; setpoint_temperature[0] = silver_temperature; } else { std::cout << "Argument must be \'gold\' or \'silver\'\n\n"; return -1; } // declaring variables int timestart = time (NULL), timend, steps = 500; // Number of time steps to take float timeEvolved, deltaT = 0.001; // Size of time step float b = pow(abs(N/density) , (1.0/3.0)); float rc = b/2; // open files for write FILE * fileData; fileData = fopen("Results.txt", "w"); if (fileData == NULL) { std::cout << "Unable to open Results.txt" << std::endl; exit(1); // terminate with error } FILE * fileBestConfig; fileBestConfig = fopen("BestConfig.txt", "w"); if (fileBestConfig == NULL) { std::cout << "Unable to open BestConfig.txt" << std::endl; exit(1); // terminate with error } FILE * fileResults; fileResults = fopen("Data.txt", "w"); if (fileResults == NULL) { std::cout << "Unable to open Data.txt" << std::endl; exit(1); // terminate with error } // Initialize positions, forces, energies, and temperatures setup_temperatures(); GetInitPositionsAndInitVelocities(); // Start cold to prevent huge forces (should pass SetpointTemperature) CalcForces(b, rc, a, c, m, n); update_energy_and_temperature(); // print results fprintf (fileResults, "NOTE: POTENTIAL ENERGY IS GIVEN BY THE SUTTON-CHEN POTENTIAL. KINETIC ENERGY IS GIVEN BY THE PARTICLES' VELOCITIES.\n"); fprintf (fileResults, "Box length = %f\n", b); fprintf (fileResults, "Starting Temperature = %f\n", current_temperature[0]); fprintf (fileData, "NOTE: POTENTIAL ENERGY IS GIVEN BY THE SUTTON-CHEN POTENTIAL. KINETIC ENERGY IS GIVEN BY THE PARTICLES' VELOCITIES.\n"); fprintf (fileData, "Time \t\tTotal Energy \tPotentialE(V) \tKineticE \tCurrent Temp \n"); // Print headers fprintf (fileData, "%f \t%f \t%f \t%f \t%f\n", 0.0, total_energy[0], potential_energy[0], kinetic_energy[0], current_temperature[0]); // Print starting values for (int i = 0; i < steps; i++){ // keep the 0th config for MD, use the 1st to last configs as replicas if (RAN3() < 0.5) { int p = RANDINT(1, num_replicas); int q = p; if (p == 1) q++; else if (p == num_replicas-1 || RAN3() < 0.5) q--; else q++; float dBeta = (1.0/setpoint_temperature[q]) - (1.0/setpoint_temperature[p]); float dEnergy = total_energy[q] - total_energy[p]; if (RAN3() < min(1.0, exp(dBeta*dEnergy))) { float temp; // swap configurations (positions, velocities, and forces) for (int y=0; y<N; y++) { for (int z=0; z<3; z++) { temp = position[p][y][z]; position[p][y][z] = position[q][y][z]; position[q][y][z] = temp; temp = velocity[p][y][z]; velocity[p][y][z] = velocity[q][y][z]; velocity[q][y][z] = temp; temp = force[p][y][z]; force[p][y][z] = force[q][y][z]; force[q][y][z] = temp; } } } } // Update position, force, velocity, energy, and temperatures VelocityVerlet(deltaT, b, rc, a, c, m, n); update_energy_and_temperature(); // Increase and control temperature, but wait until particles have spread equilibrated first if (i > 0.3*steps) BussiThermostat(deltaT); // save best configuration save_best_config(); timeEvolved = (i+1) * deltaT; fprintf (fileData, "%f \t%f \t%f \t%f \t%f\n", timeEvolved, total_energy[0], potential_energy[0], kinetic_energy[0], current_temperature[0]); } int BEST = get_best_global_config(); current_temperature[0] = (2 * kinetic_energy[0])/(3 * N - 3); fprintf (fileResults, "Final Temperature = %f\n", current_temperature[0]); fprintf (fileResults, "Potential Energy = %f\n", potential_energy[0]); fprintf (fileResults, "Kinetic Energy = %f\n", kinetic_energy[0]); fprintf (fileResults, "Binding Energy (V/N) = %f\n", potential_energy[0] / N); fprintf (fileResults, "Binding Energy ((V+K)/N) = %f\n", total_energy[0] / N); fprintf (fileResults, "Diffusitivity = %f\n\n", CalculateDiffusitivity(timeEvolved, b)); fprintf (fileResults, "Global Minimum Search Results (found by Replica Exchange MD):\n"); fprintf (fileResults, "Global Potential Energy Minimnum (V at best config) = %f\n", best_v[BEST]); fprintf (fileResults, "Kinetic Energy at Global Minimnum = %f\n", best_k[BEST]); fprintf (fileResults, "Total Energy at Global Minimnum = %f\n", best_v[BEST]+best_k[BEST]); fprintf (fileResults, "Binding Energy at Global Minimnum (V/N) = %f\n\n", best_v[BEST] / N); fprintf (fileResults, "Binding Energy at Global Minimnum ((V+K)/N) = %f\n\n", (best_v[BEST]+best_k[BEST]) / N); timend = time (NULL); fprintf (fileResults, "%i seconds to execute.", timend-timestart); fprintf (fileBestConfig, "Best Configuration:\n\n"); fprintf (fileBestConfig, "x\t\ty\t\tz\n"); for (int i = 0; i < N; i++) { fprintf (fileBestConfig, "%f \t%f \t%f\n", best_configuration[BEST][i][0], best_configuration[BEST][i][1], best_configuration[BEST][i][2]); } fclose (fileBestConfig); fclose (fileData); fclose (fileResults); return 0; }
int main(int argc, char** argv) { srand((unsigned int)time(NULL)); // Calculate the length of the box side double length = powf(N/density, 1.0/3.0); // Set boundaries for (int k = 0; k < 3; ++k) { L[k] = length; L2[k] = L[k]/2.0; if (rcut2 > 0) { assert(L[k] > sqrt(rcut2)); } } // Set masses for every particle for (int i = 0; i < N; ++i) { m[i] = mAr; } FILE* f_en; FILE* f_kin; FILE* f_poten; FILE* f_temp; FILE* f_xyz; FILE* f_velocity; FILE* f_init_coord; FILE* f_len; if (PRINT_TO_FILE) { f_init_coord = fopen("data/init_coord.xyz", "w"); f_xyz = fopen("data/coordinates.xyz", "w"); f_temp = fopen("data/temp.csv", "w"); f_velocity = fopen("data/velocity.csv", "w"); f_en = fopen("data/energy.csv", "w"); f_kin = fopen("data/kinetic.csv", "w"); f_poten = fopen("data/poten.csv", "w"); f_len = fopen("data/len.csv", "w"); } // Store length to file fprintf(f_len, "%f", length); /* Set Initial Coordinates */ double init[3]; // Quantity of atoms per line int quant = powf(N, 1.0/3.0); // Step of the mesh double step = L[0] / quant; double ic = (step/2.0) - L2[0]; for (int i = 0; i < 3; ++i) { init[i] = ic; } // Set initial coordinates for (int i = 0; i < N; ++i) { for (int k = 0; k < 3; ++k) { r[i][k] = init[k]; } init[0] += step; if ((i+1) % (quant*quant) == 0) { init[0] = ic; init[1] = ic; init[2] += step; } else if ((i+1) % quant == 0) { init[0] = ic; init[1] += step; } } // Set initial velocities for (int i = 0; i < N; ++i) { for (int k = 0; k < 3; ++k) { v[i][k] = 1 - (((float)rand()/(float)RAND_MAX)*2); } } if (PRINT_TO_FILE) { // Print initial coordinates to file fprintf(f_init_coord, "%d\n\n", N); for (int i = 0; i < N; ++i) { fprintf(f_init_coord, "%c ", (char) (97+(i%26))); for (int k = 0; k < 3; ++k) { fprintf(f_init_coord, "%f ", r[i][k]); } fprintf(f_init_coord, "\n"); } fclose(f_init_coord); } /////////////// // Main Part // /////////////// for (int i = 0; i < iterations; ++i) { nearest_image(); ClearForces(); CalcForces(); EqMotion(); CalcEnergy(); CalcTemp(); Thermostat(i); if (PRINT_TO_FILE) { // Store energy and temperature to file fprintf(f_en, "%f,%i\n", K + utot, i); fprintf(f_kin, "%f,%i\n", K, i); fprintf(f_poten, "%f,%i\n", utot, i); fprintf(f_temp, "%f,%i\n", Temp, i); // Store velocities to file if (i > iterations/2) { for (int j = 0; j < N; ++j) { double v2 = 0; for (int k = 0; k < 3; ++k) { v2 += (float)v[j][k] * (float)v[j][k]; } fprintf(f_velocity, "%f\n", sqrt(v2)); } } // Store coordinates to file if (i < iter_to_write) { fprintf(f_xyz, "%d\n\n", N); for (int i = 0; i < N; ++i) { fprintf(f_xyz, "%c ", (char) (97+(i%26))); for (int k = 0; k < 3; ++k) { fprintf(f_xyz, "%f ", rn[i][k]); } fprintf(f_xyz, "\n"); } } } // Print to console if (i % (iterations / 10) == 0) { printf("> iter: %d\n", i); printf("-> temp: %f\n", Temp); printf("-> utot: %f\n", utot); printf("-> K: %f\n", K); printf("\n"); } assert(K != INFINITY); } if (PRINT_TO_FILE) { fclose(f_velocity); fclose(f_xyz); fclose(f_en); fclose(f_poten); fclose(f_kin); fclose(f_temp); fclose(f_len); printf("Print to file: Done!\n"); } printf("Result: Done!\n"); return 0; }
int main(int argc, char** argv){ // declare metal properties float m, a, n, c, density, SetpointTemperature; // check for second argument, and initialize metal properties accordingly if (argc < 2) { std::cout << "Program must be invoked with argument \'gold\' or \'silver\'" << std::endl; return -1; // terminate with error } else if (strcmp(argv[1],"gold") == 0) { m = gold_m; a = gold_a; n = gold_n; c = gold_c; density = gold_density; SetpointTemperature = gold_temperature; } else if (strcmp(argv[1],"silver") == 0) { m = silver_m; a = silver_a; n = silver_n; c = silver_c; density = silver_density; SetpointTemperature = silver_temperature; } else { std::cout << "Argument must be \'gold\' or \'silver\'\n\n"; return -1; } // declaring variables int timestart = time (NULL), timend, steps = 10; // Number of time steps to take float KineticEnergy = 0, CurrentTemperature, TotalEnergy, timeEvolved, deltaT = 0.001; // Size of time step float b = pow(abs(N/density) , (1.0/3.0)); float rc = b/2; float BestV = 0; printf("Box length = %f.\n", b); FILE * fileVelocityVerlet; fileVelocityVerlet = fopen("Results.txt", "w"); if (fileVelocityVerlet == NULL) { std::cout << "Unable to open VelocityVerlet.txt" << std::endl; exit(1); // terminate with error } FILE * fileBestConfig; fileBestConfig = fopen("BestConfig.txt", "w"); if (fileBestConfig == NULL) { std::cout << "Unable to open BestConfig.txt" << std::endl; exit(1); // terminate with error } // Initialize positions and forces GetInitPositionsAndInitVelocities(SetpointTemperature); CalcForces(b, rc, a, c, m, n); // tau = // Calculate initial kinetic and total energy and temperature KineticEnergy = GetKineticEnergy(); TotalEnergy = KineticEnergy + Vfinal; CurrentTemperature = (2 * KineticEnergy)/(3 * N - 3); std::cout << "Starting Temperature = " << CurrentTemperature << std::endl; fprintf (fileVelocityVerlet, "Time, Total Energy, PotentialE(V), KineticE \n", TotalEnergy, Vfinal, KineticEnergy); // Print starting values fprintf (fileVelocityVerlet, "0, %f, %f, %f\n", TotalEnergy, Vfinal, KineticEnergy); // Print starting values for (int i = 0; i < steps; i++){ // Update position and velocity VelocityVerlet(deltaT, b, rc, a, c, m, n); // Get Kinetic Energy KineticEnergy = GetKineticEnergy(); // std::cout << Vfinal << std::endl; for (int v = 0; v < 1; v++) { std::cout << velocity[v][0] << ", " << velocity[v][1] << ", "<< velocity[v][2] << std::endl; } CurrentTemperature = (2 * KineticEnergy)/(3 * N - 3); // Calculate temperature TotalEnergy = KineticEnergy + Vfinal; std::cout<< "ct = " << CurrentTemperature << "spt = " << SetpointTemperature << std::endl; BussiThermostat(deltaT, CurrentTemperature, SetpointTemperature); // Control temperature timeEvolved = i * deltaT; //~ printf ("t, TE, V, KE: %f, %f, %f, %f\n", timeEvolved, TotalEnergy, Vfinal, KineticEnergy); fprintf (fileVelocityVerlet, "%f, %f, %f, %f\n", timeEvolved, TotalEnergy, Vfinal, KineticEnergy); if (Vfinal < BestV){ BestV = Vfinal; for (int v = 0; v < N; v++) { for (int w = 0; w < 3; w++) { BestConfiguration[v][w] = position[v][w]; } } } } CurrentTemperature = (2 * KineticEnergy)/(3 * N - 3); std::cout << "Final Temperature = " << CurrentTemperature << std::endl << "Kinetic Energy = " << KineticEnergy << std::endl << "Binding Energy = " << TotalEnergy / N << std::endl << "Diffusitivity = " << CalculateDiffusitivity(deltaT, steps) << std::endl; fprintf (fileBestConfig, "Best Configuration:\n"); for (int i = 0; i < N; i++) { fprintf (fileBestConfig, "%f, %f, %f\n", BestConfiguration[i][0], BestConfiguration[i][1], BestConfiguration[i][2]); } fclose (fileBestConfig); fclose (fileVelocityVerlet); timend = time (NULL); std::cout << (timend-timestart) << " seconds to execute." << std::endl; //terminate the program return 0; }