moved stuff into function to remove a for loop

This commit is contained in:
Afonso Franco 2023-10-17 11:49:54 +01:00
parent 763c84739f
commit 1b0a9ab380
Signed by: afonso
SSH key fingerprint: SHA256:JiuxZNdA5bRWXPMUJChI0AQ75yC+cXY4xM0IaVwEVys
5 changed files with 524 additions and 539 deletions

View file

@ -4,3 +4,4 @@ TabWidth: 4
UseTab: Always UseTab: Always
BreakBeforeBraces: Attach BreakBeforeBraces: Attach
ColumnLimit: 100 ColumnLimit: 100
AlignReturnType: Always

View file

@ -6,7 +6,7 @@ ts := $(shell /usr/bin/date "+%d-%m__%H_%M_%S")
.DEFAULT_GOAL = MD .DEFAULT_GOAL = MD
MD: $(SRC)/MD.cpp MD: $(SRC)/MD.cpp
$(CC) $(CFLAGS) $(SRC)MD.cpp -lm -march=native -mavx -O2 -ftree-vectorize -funroll-loops -pg -g -o ./out/MD $(CC) $(CFLAGS) $(SRC)MD.cpp -lm -march=native -O2 -ftree-vectorize -funroll-loops -pg -g -o ./out/MD
MDorig: $(SRC)/MD-original.cpp MDorig: $(SRC)/MD-original.cpp
$(CC) $(CFLAGS) $(SRC)MD-original.cpp -lm -O2 -pg -o ./out/MD-original $(CC) $(CFLAGS) $(SRC)MD-original.cpp -lm -O2 -pg -o ./out/MD-original

BIN
out/MD

Binary file not shown.

View file

@ -63,28 +63,27 @@ char atype[10];
// Function prototypes // Function prototypes
// initialize positions on simple cubic lattice, also calls function to // initialize positions on simple cubic lattice, also calls function to
// initialize velocities // initialize velocities
void initialize (); void initialize();
// update positions and velocities using Velocity Verlet algorithm // update positions and velocities using Velocity Verlet algorithm
// print particle coordinates to file for rendering via VMD or other animation // print particle coordinates to file for rendering via VMD or other animation
// software return 'instantaneous pressure' // software return 'instantaneous pressure'
double VelocityVerlet (double dt, int iter, FILE *fp); double VelocityVerlet(double dt, int iter, FILE *fp);
// Compute Force using F = -dV/dr // Compute Force using F = -dV/dr
// solve F = ma for use in Velocity Verlet // solve F = ma for use in Velocity Verlet
void computeAccelerations (); void computeAccelerations();
// Numerical Recipes function for generation gaussian distribution // Numerical Recipes function for generation gaussian distribution
double gaussdist (); double gaussdist();
// Initialize velocities according to user-supplied initial Temperature // Initialize velocities according to user-supplied initial Temperature
// (Tinit) // (Tinit)
void initializeVelocities (); void initializeVelocities();
// Compute total potential energy from particle coordinates // Compute total potential energy from particle coordinates
double Potential (); double Potential();
// Compute mean squared velocity from particle velocities // Compute mean squared velocity from particle velocities
double MeanSquaredVelocity (); double MeanSquaredVelocity();
// Compute total kinetic energy from particle mass and velocities // Compute total kinetic energy from particle mass and velocities
double Kinetic (); double Kinetic();
int int main() {
main () {
// variable delcarations // variable delcarations
int i; int i;
@ -94,25 +93,21 @@ main () {
char trash[10000], prefix[1000], tfn[1000], ofn[1000], afn[1000]; char trash[10000], prefix[1000], tfn[1000], ofn[1000], afn[1000];
FILE *infp, *tfp, *ofp, *afp; FILE *infp, *tfp, *ofp, *afp;
printf ( printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
"\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf(" WELCOME TO WILLY P CHEM MD!\n");
printf (" WELCOME TO WILLY P CHEM MD!\n"); printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf ( printf("\n ENTER A TITLE FOR YOUR CALCULATION!\n");
" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); scanf("%s", prefix);
printf ("\n ENTER A TITLE FOR YOUR CALCULATION!\n"); strcpy(tfn, prefix);
scanf ("%s", prefix); strcat(tfn, "_traj.xyz");
strcpy (tfn, prefix); strcpy(ofn, prefix);
strcat (tfn, "_traj.xyz"); strcat(ofn, "_output.txt");
strcpy (ofn, prefix); strcpy(afn, prefix);
strcat (ofn, "_output.txt"); strcat(afn, "_average.txt");
strcpy (afn, prefix);
strcat (afn, "_average.txt");
printf ( printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
"\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf(" TITLE ENTERED AS '%s'\n", prefix);
printf (" TITLE ENTERED AS '%s'\n", prefix); printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf (
" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
/* Table of values for Argon relating natural units to SI units: /* Table of values for Argon relating natural units to SI units:
* These are derived from Lennard-Jones parameters from the article * These are derived from Lennard-Jones parameters from the article
@ -133,34 +128,31 @@ main () {
// Edit these factors to be computed in terms of basic properties in // Edit these factors to be computed in terms of basic properties in
// natural units of the gas being simulated // natural units of the gas being simulated
printf ( printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
"\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf(" WHICH NOBLE GAS WOULD YOU LIKE TO SIMULATE? (DEFAULT IS ARGON)\n");
printf ( printf("\n FOR HELIUM, TYPE 'He' THEN PRESS 'return' TO CONTINUE\n");
" WHICH NOBLE GAS WOULD YOU LIKE TO SIMULATE? (DEFAULT IS ARGON)\n"); printf(" FOR NEON, TYPE 'Ne' THEN PRESS 'return' TO CONTINUE\n");
printf ("\n FOR HELIUM, TYPE 'He' THEN PRESS 'return' TO CONTINUE\n"); printf(" FOR ARGON, TYPE 'Ar' THEN PRESS 'return' TO CONTINUE\n");
printf (" FOR NEON, TYPE 'Ne' THEN PRESS 'return' TO CONTINUE\n"); printf(" FOR KRYPTON, TYPE 'Kr' THEN PRESS 'return' TO CONTINUE\n");
printf (" FOR ARGON, TYPE 'Ar' THEN PRESS 'return' TO CONTINUE\n"); printf(" FOR XENON, TYPE 'Xe' THEN PRESS 'return' TO CONTINUE\n");
printf (" FOR KRYPTON, TYPE 'Kr' THEN PRESS 'return' TO CONTINUE\n"); printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf (" FOR XENON, TYPE 'Xe' THEN PRESS 'return' TO CONTINUE\n"); scanf("%s", atype);
printf (
" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
scanf ("%s", atype);
if (strcmp (atype, "He") == 0) { if (strcmp(atype, "He") == 0) {
VolFac = 1.8399744000000005e-29; VolFac = 1.8399744000000005e-29;
PressFac = 8152287.336171632; PressFac = 8152287.336171632;
TempFac = 10.864459551225972; TempFac = 10.864459551225972;
timefac = 1.7572698825166272e-12; timefac = 1.7572698825166272e-12;
} else if (strcmp (atype, "Ne") == 0) { } else if (strcmp(atype, "Ne") == 0) {
VolFac = 2.0570823999999997e-29; VolFac = 2.0570823999999997e-29;
PressFac = 27223022.27659913; PressFac = 27223022.27659913;
TempFac = 40.560648991243625; TempFac = 40.560648991243625;
timefac = 2.1192341945685407e-12; timefac = 2.1192341945685407e-12;
} else if (strcmp (atype, "Ar") == 0) { } else if (strcmp(atype, "Ar") == 0) {
VolFac = 3.7949992920124995e-29; VolFac = 3.7949992920124995e-29;
PressFac = 51695201.06691862; PressFac = 51695201.06691862;
@ -168,14 +160,14 @@ main () {
timefac = 2.09618e-12; timefac = 2.09618e-12;
// strcpy(atype,"Ar"); // strcpy(atype,"Ar");
} else if (strcmp (atype, "Kr") == 0) { } else if (strcmp(atype, "Kr") == 0) {
VolFac = 4.5882712000000004e-29; VolFac = 4.5882712000000004e-29;
PressFac = 59935428.40275003; PressFac = 59935428.40275003;
TempFac = 199.1817584391428; TempFac = 199.1817584391428;
timefac = 8.051563913585078e-13; timefac = 8.051563913585078e-13;
} else if (strcmp (atype, "Xe") == 0) { } else if (strcmp(atype, "Xe") == 0) {
VolFac = 5.4872e-29; VolFac = 5.4872e-29;
PressFac = 70527773.72794868; PressFac = 70527773.72794868;
@ -188,37 +180,33 @@ main () {
PressFac = 51695201.06691862; PressFac = 51695201.06691862;
TempFac = 142.0950000000000; TempFac = 142.0950000000000;
timefac = 2.09618e-12; timefac = 2.09618e-12;
strcpy (atype, "Ar"); strcpy(atype, "Ar");
} }
printf ( printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
"\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf("\n YOU ARE SIMULATING %s GAS! \n", atype);
printf ("\n YOU ARE SIMULATING %s GAS! \n", atype); printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf (
"\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf ( printf("\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
"\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf("\n YOU WILL NOW ENTER A FEW SIMULATION PARAMETERS\n");
printf ("\n YOU WILL NOW ENTER A FEW SIMULATION PARAMETERS\n"); printf(" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf ( printf("\n\n ENTER THE INTIAL TEMPERATURE OF YOUR GAS IN KELVIN\n");
" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); scanf("%lf", &Tinit);
printf ("\n\n ENTER THE INTIAL TEMPERATURE OF YOUR GAS IN KELVIN\n");
scanf ("%lf", &Tinit);
// Make sure temperature is a positive number! // Make sure temperature is a positive number!
if (Tinit < 0.) { if (Tinit < 0.) {
printf ("\n !!!!! ABSOLUTE TEMPERATURE MUST BE A POSITIVE NUMBER! " printf("\n !!!!! ABSOLUTE TEMPERATURE MUST BE A POSITIVE NUMBER! "
"PLEASE TRY AGAIN WITH A POSITIVE TEMPERATURE!!!\n"); "PLEASE TRY AGAIN WITH A POSITIVE TEMPERATURE!!!\n");
exit (0); exit(0);
} }
// Convert initial temperature from kelvin to natural units // Convert initial temperature from kelvin to natural units
Tinit /= TempFac; Tinit /= TempFac;
printf ("\n\n ENTER THE NUMBER DENSITY IN moles/m^3\n"); printf("\n\n ENTER THE NUMBER DENSITY IN moles/m^3\n");
printf (" FOR REFERENCE, NUMBER DENSITY OF AN IDEAL GAS AT STP IS ABOUT " printf(" FOR REFERENCE, NUMBER DENSITY OF AN IDEAL GAS AT STP IS ABOUT "
"40 moles/m^3\n"); "40 moles/m^3\n");
printf (" NUMBER DENSITY OF LIQUID ARGON AT 1 ATM AND 87 K IS ABOUT " printf(" NUMBER DENSITY OF LIQUID ARGON AT 1 ATM AND 87 K IS ABOUT "
"35000 moles/m^3\n"); "35000 moles/m^3\n");
scanf ("%lf", &rho); scanf("%lf", &rho);
N = 10 * 216; N = 10 * 216;
Vol = N / (rho * NA); Vol = N / (rho * NA);
@ -228,10 +216,10 @@ main () {
// Limiting N to MAXPART for practical reasons // Limiting N to MAXPART for practical reasons
if (N >= MAXPART) { if (N >= MAXPART) {
printf ("\n\n\n MAXIMUM NUMBER OF PARTICLES IS %i\n\n PLEASE ADJUST " printf("\n\n\n MAXIMUM NUMBER OF PARTICLES IS %i\n\n PLEASE ADJUST "
"YOUR INPUT FILE ACCORDINGLY \n\n", "YOUR INPUT FILE ACCORDINGLY \n\n",
MAXPART); MAXPART);
exit (0); exit(0);
} }
// Check to see if the volume makes sense - is it too small? // Check to see if the volume makes sense - is it too small?
// Remember VDW radius of the particles is 1 natural unit of length // Remember VDW radius of the particles is 1 natural unit of length
@ -240,29 +228,29 @@ main () {
// radius // radius
if (Vol < N) { if (Vol < N) {
printf ("\n\n\n YOUR DENSITY IS VERY HIGH!\n\n"); printf("\n\n\n YOUR DENSITY IS VERY HIGH!\n\n");
printf (" THE NUMBER OF PARTICLES IS %i AND THE AVAILABLE VOLUME IS " printf(" THE NUMBER OF PARTICLES IS %i AND THE AVAILABLE VOLUME IS "
"%f NATURAL UNITS\n", "%f NATURAL UNITS\n",
N, Vol); N, Vol);
printf (" SIMULATIONS WITH DENSITY GREATER THAN 1 PARTCICLE/(1 " printf(" SIMULATIONS WITH DENSITY GREATER THAN 1 PARTCICLE/(1 "
"Natural Unit of Volume) MAY DIVERGE\n"); "Natural Unit of Volume) MAY DIVERGE\n");
printf (" PLEASE ADJUST YOUR INPUT FILE ACCORDINGLY AND RETRY\n\n"); printf(" PLEASE ADJUST YOUR INPUT FILE ACCORDINGLY AND RETRY\n\n");
exit (0); exit(0);
} }
// Vol = L*L*L; // Vol = L*L*L;
// Length of the box in natural units: // Length of the box in natural units:
L = pow (Vol, (1. / 3)); L = pow(Vol, (1. / 3));
// Files that we can write different quantities to // Files that we can write different quantities to
tfp = fopen (tfn, "w"); // The MD trajectory, coordinates of every tfp = fopen(tfn, "w"); // The MD trajectory, coordinates of every
// particle at each timestep // particle at each timestep
ofp = fopen ( ofp = fopen(
ofn, ofn,
"w"); // Output of other quantities (T, P, gc, etc) at every timestep "w"); // Output of other quantities (T, P, gc, etc) at every timestep
afp = fopen (afn, "w"); // Average T, P, gc, etc from the simulation afp = fopen(afn, "w"); // Average T, P, gc, etc from the simulation
int NumTime; int NumTime;
if (strcmp (atype, "He") == 0) { if (strcmp(atype, "He") == 0) {
// dt in natural units of time s.t. in SI it is 5 f.s. for all other // dt in natural units of time s.t. in SI it is 5 f.s. for all other
// gasses // gasses
@ -280,59 +268,58 @@ main () {
// Put all the atoms in simple crystal lattice and give them random // Put all the atoms in simple crystal lattice and give them random
// velocities that corresponds to the initial temperature we have // velocities that corresponds to the initial temperature we have
// specified // specified
initialize (); initialize();
// Based on their positions, calculate the ininial intermolecular forces // Based on their positions, calculate the ininial intermolecular forces
// The accellerations of each particle will be defined from the forces and // The accellerations of each particle will be defined from the forces and
// their mass, and this will allow us to update their positions via // their mass, and this will allow us to update their positions via
// Newton's law // Newton's law
computeAccelerations (); computeAccelerations();
// Print number of particles to the trajectory file // Print number of particles to the trajectory file
fprintf (tfp, "%i\n", N); fprintf(tfp, "%i\n", N);
// We want to calculate the average Temperature and Pressure for the // We want to calculate the average Temperature and Pressure for the
// simulation The variables need to be set to zero initially // simulation The variables need to be set to zero initially
Pavg = 0; Pavg = 0;
Tavg = 0; Tavg = 0;
int tenp = floor (NumTime / 10); int tenp = floor(NumTime / 10);
fprintf ( fprintf(ofp,
ofp,
" time (s) T(t) (K) P(t) (Pa) " " time (s) T(t) (K) P(t) (Pa) "
"Kinetic En. (n.u.) Potential En. (n.u.) Total En. (n.u.)\n"); "Kinetic En. (n.u.) Potential En. (n.u.) Total En. (n.u.)\n");
printf (" PERCENTAGE OF CALCULATION COMPLETE:\n ["); printf(" PERCENTAGE OF CALCULATION COMPLETE:\n [");
for (i = 0; i < NumTime + 1; i++) { for (i = 0; i < NumTime + 1; i++) {
// This just prints updates on progress of the calculation for the // This just prints updates on progress of the calculation for the
// users convenience // users convenience
if (i == tenp) if (i == tenp)
printf (" 10 |"); printf(" 10 |");
else if (i == 2 * tenp) else if (i == 2 * tenp)
printf (" 20 |"); printf(" 20 |");
else if (i == 3 * tenp) else if (i == 3 * tenp)
printf (" 30 |"); printf(" 30 |");
else if (i == 4 * tenp) else if (i == 4 * tenp)
printf (" 40 |"); printf(" 40 |");
else if (i == 5 * tenp) else if (i == 5 * tenp)
printf (" 50 |"); printf(" 50 |");
else if (i == 6 * tenp) else if (i == 6 * tenp)
printf (" 60 |"); printf(" 60 |");
else if (i == 7 * tenp) else if (i == 7 * tenp)
printf (" 70 |"); printf(" 70 |");
else if (i == 8 * tenp) else if (i == 8 * tenp)
printf (" 80 |"); printf(" 80 |");
else if (i == 9 * tenp) else if (i == 9 * tenp)
printf (" 90 |"); printf(" 90 |");
else if (i == 10 * tenp) else if (i == 10 * tenp)
printf (" 100 ]\n"); printf(" 100 ]\n");
fflush (stdout); fflush(stdout);
// This updates the positions and velocities using Newton's Laws // This updates the positions and velocities using Newton's Laws
// Also computes the Pressure as the sum of momentum changes from wall // Also computes the Pressure as the sum of momentum changes from wall
// collisions / timestep which is a Kinetic Theory of gasses concept of // collisions / timestep which is a Kinetic Theory of gasses concept of
// Pressure // Pressure
Press = VelocityVerlet (dt, i + 1, tfp); Press = VelocityVerlet(dt, i + 1, tfp);
Press *= PressFac; Press *= PressFac;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@ -341,9 +328,9 @@ main () {
// Potential, and Kinetic Energy // Potential, and Kinetic Energy
// We would also like to use the IGL to try to see if we can extract // We would also like to use the IGL to try to see if we can extract
// the gas constant // the gas constant
mvs = MeanSquaredVelocity (); mvs = MeanSquaredVelocity();
KE = Kinetic (); KE = Kinetic();
PE = Potential (); PE = Potential();
// Temperature from Kinetic Theory // Temperature from Kinetic Theory
Temp = m * mvs / (3 * kB) * TempFac; Temp = m * mvs / (3 * kB) * TempFac;
@ -358,7 +345,7 @@ main () {
Tavg += Temp; Tavg += Temp;
Pavg += Press; Pavg += Press;
fprintf (ofp, " %8.4e %20.12f %20.12f %20.12f %20.12f %20.12f \n", fprintf(ofp, " %8.4e %20.12f %20.12f %20.12f %20.12f %20.12f \n",
i * dt * timefac, Temp, Press, KE, PE, KE + PE); i * dt * timefac, Temp, Press, KE, PE, KE + PE);
} }
@ -368,52 +355,49 @@ main () {
Tavg /= NumTime; Tavg /= NumTime;
Z = Pavg * (Vol * VolFac) / (N * kBSI * Tavg); Z = Pavg * (Vol * VolFac) / (N * kBSI * Tavg);
gc = NA * Pavg * (Vol * VolFac) / (N * Tavg); gc = NA * Pavg * (Vol * VolFac) / (N * Tavg);
fprintf (afp, fprintf(afp, " Total Time (s) T (K) P (Pa) PV/nT "
" Total Time (s) T (K) P (Pa) PV/nT "
"(J/(mol K)) Z V (m^3) N\n"); "(J/(mol K)) Z V (m^3) N\n");
fprintf ( fprintf(afp,
afp,
" -------------- ----------- --------------- " " -------------- ----------- --------------- "
"-------------- --------------- ------------ -----------\n"); "-------------- --------------- ------------ -----------\n");
fprintf (afp, fprintf(afp,
" %8.4e %15.5f %15.5f %10.5f %10.5f " " %8.4e %15.5f %15.5f %10.5f %10.5f "
"%10.5e %i\n", "%10.5e %i\n",
i * dt * timefac, Tavg, Pavg, gc, Z, Vol * VolFac, N); i * dt * timefac, Tavg, Pavg, gc, Z, Vol * VolFac, N);
printf ("\n TO ANIMATE YOUR SIMULATION, OPEN THE FILE \n '%s' WITH VMD " printf("\n TO ANIMATE YOUR SIMULATION, OPEN THE FILE \n '%s' WITH VMD "
"AFTER THE SIMULATION COMPLETES\n", "AFTER THE SIMULATION COMPLETES\n",
tfn); tfn);
printf ("\n TO ANALYZE INSTANTANEOUS DATA ABOUT YOUR MOLECULE, OPEN THE " printf("\n TO ANALYZE INSTANTANEOUS DATA ABOUT YOUR MOLECULE, OPEN THE "
"FILE \n '%s' WITH YOUR FAVORITE TEXT EDITOR OR IMPORT THE DATA " "FILE \n '%s' WITH YOUR FAVORITE TEXT EDITOR OR IMPORT THE DATA "
"INTO EXCEL\n", "INTO EXCEL\n",
ofn); ofn);
printf ("\n THE FOLLOWING THERMODYNAMIC AVERAGES WILL BE COMPUTED AND " printf("\n THE FOLLOWING THERMODYNAMIC AVERAGES WILL BE COMPUTED AND "
"WRITTEN TO THE FILE \n '%s':\n", "WRITTEN TO THE FILE \n '%s':\n",
afn); afn);
printf ("\n AVERAGE TEMPERATURE (K): %15.5f\n", Tavg); printf("\n AVERAGE TEMPERATURE (K): %15.5f\n", Tavg);
printf ("\n AVERAGE PRESSURE (Pa): %15.5f\n", Pavg); printf("\n AVERAGE PRESSURE (Pa): %15.5f\n", Pavg);
printf ("\n PV/nT (J * mol^-1 K^-1): %15.5f\n", gc); printf("\n PV/nT (J * mol^-1 K^-1): %15.5f\n", gc);
printf ("\n PERCENT ERROR of pV/nT AND GAS CONSTANT: %15.5f\n", printf("\n PERCENT ERROR of pV/nT AND GAS CONSTANT: %15.5f\n",
100 * fabs (gc - 8.3144598) / 8.3144598); 100 * fabs(gc - 8.3144598) / 8.3144598);
printf ("\n THE COMPRESSIBILITY (unitless): %15.5f \n", Z); printf("\n THE COMPRESSIBILITY (unitless): %15.5f \n", Z);
printf ("\n TOTAL VOLUME (m^3): %10.5e \n", printf("\n TOTAL VOLUME (m^3): %10.5e \n",
Vol * VolFac); Vol * VolFac);
printf ("\n NUMBER OF PARTICLES (unitless): %i \n", N); printf("\n NUMBER OF PARTICLES (unitless): %i \n", N);
fclose (tfp); fclose(tfp);
fclose (ofp); fclose(ofp);
fclose (afp); fclose(afp);
return 0; return 0;
} }
void void initialize() {
initialize () {
int n, p, i, j, k; int n, p, i, j, k;
double pos; double pos;
// Number of atoms in each direction // Number of atoms in each direction
n = int (ceil (pow (N, 1.0 / 3))); n = int(ceil(pow(N, 1.0 / 3)));
// spacing between atoms along a given direction // spacing between atoms along a given direction
pos = L / n; pos = L / n;
@ -436,7 +420,7 @@ initialize () {
} }
// Call function to initialize velocities // Call function to initialize velocities
initializeVelocities (); initializeVelocities();
/*********************************************** /***********************************************
* Uncomment if you want to see what the initial positions and velocities * Uncomment if you want to see what the initial positions and velocities
@ -452,8 +436,7 @@ initialize () {
} }
// Function to calculate the averaged velocity squared // Function to calculate the averaged velocity squared
double double MeanSquaredVelocity() {
MeanSquaredVelocity () {
double vx2 = 0; double vx2 = 0;
double vy2 = 0; double vy2 = 0;
@ -473,8 +456,7 @@ MeanSquaredVelocity () {
} }
// Function to calculate the kinetic energy of the system // Function to calculate the kinetic energy of the system
double double Kinetic() { // Write Function here!
Kinetic () { // Write Function here!
double v2, kin; double v2, kin;
@ -494,8 +476,7 @@ Kinetic () { // Write Function here!
} }
// Function to calculate the potential energy of the system // Function to calculate the potential energy of the system
double double Potential() {
Potential () {
double quot, r2, rnorm, term1, term2, Pot; double quot, r2, rnorm, term1, term2, Pot;
int i, j, k; int i, j, k;
@ -508,10 +489,10 @@ Potential () {
for (k = 0; k < 3; k++) { for (k = 0; k < 3; k++) {
r2 += (r[i][k] - r[j][k]) * (r[i][k] - r[j][k]); r2 += (r[i][k] - r[j][k]) * (r[i][k] - r[j][k]);
} }
rnorm = sqrt (r2); rnorm = sqrt(r2);
quot = sigma / rnorm; quot = sigma / rnorm;
term1 = pow (quot, 12.); term1 = pow(quot, 12.);
term2 = pow (quot, 6.); term2 = pow(quot, 6.);
Pot += 4 * epsilon * (term1 - term2); Pot += 4 * epsilon * (term1 - term2);
} }
@ -524,8 +505,7 @@ Potential () {
// Uses the derivative of the Lennard-Jones potential to calculate // Uses the derivative of the Lennard-Jones potential to calculate
// the forces on each atom. Then uses a = F/m to calculate the // the forces on each atom. Then uses a = F/m to calculate the
// accelleration of each atom. // accelleration of each atom.
void void computeAccelerations() {
computeAccelerations () {
int i, j, k; int i, j, k;
double f, rSqd; double f, rSqd;
double rij[3]; // position of i relative to j double rij[3]; // position of i relative to j
@ -549,7 +529,7 @@ computeAccelerations () {
// From derivative of Lennard-Jones with sigma and epsilon set // From derivative of Lennard-Jones with sigma and epsilon set
// equal to 1 in natural units! // equal to 1 in natural units!
f = 24 * (2 * pow (rSqd, -7) - pow (rSqd, -4)); f = 24 * (2 * pow(rSqd, -7) - pow(rSqd, -4));
for (k = 0; k < 3; k++) { for (k = 0; k < 3; k++) {
// from F = ma, where m = 1 in natural units! // from F = ma, where m = 1 in natural units!
a[i][k] += rij[k] * f; a[i][k] += rij[k] * f;
@ -560,8 +540,7 @@ computeAccelerations () {
} }
// returns sum of dv/dt*m/A (aka Pressure) from elastic collisions with walls // returns sum of dv/dt*m/A (aka Pressure) from elastic collisions with walls
double double VelocityVerlet(double dt, int iter, double *PE, FILE *fp) {
VelocityVerlet (double dt, int iter, double *PE, FILE *fp) {
int i, j, k; int i, j, k;
double psum = 0.; double psum = 0.;
@ -580,7 +559,7 @@ VelocityVerlet (double dt, int iter, double *PE, FILE *fp) {
// printf(" %i %6.4e %6.4e %6.4e\n",i,r[i][0],r[i][1],r[i][2]); // printf(" %i %6.4e %6.4e %6.4e\n",i,r[i][0],r[i][1],r[i][2]);
} }
// Update accellerations from updated positions // Update accellerations from updated positions
computeAccelerations (); computeAccelerations();
// Update velocity with updated acceleration // Update velocity with updated acceleration
for (i = 0; i < N; i++) { for (i = 0; i < N; i++) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
@ -593,13 +572,13 @@ VelocityVerlet (double dt, int iter, double *PE, FILE *fp) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
if (r[i][j] < 0.) { if (r[i][j] < 0.) {
v[i][j] *= -1.; //- elastic walls v[i][j] *= -1.; //- elastic walls
psum += 2 * m * fabs (v[i][j]) psum += 2 * m * fabs(v[i][j]) /
/ dt; // contribution to pressure from "left" walls dt; // contribution to pressure from "left" walls
} }
if (r[i][j] >= L) { if (r[i][j] >= L) {
v[i][j] *= -1.; //- elastic walls v[i][j] *= -1.; //- elastic walls
psum += 2 * m * fabs (v[i][j]) psum += 2 * m * fabs(v[i][j]) /
/ dt; // contribution to pressure from "right" walls dt; // contribution to pressure from "right" walls
} }
} }
} }
@ -617,8 +596,7 @@ VelocityVerlet (double dt, int iter, double *PE, FILE *fp) {
return psum / (6 * L * L); return psum / (6 * L * L);
} }
void void initializeVelocities() {
initializeVelocities () {
int i, j; int i, j;
@ -626,13 +604,13 @@ initializeVelocities () {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
// Pull a number from a Gaussian Distribution // Pull a number from a Gaussian Distribution
v[i][j] = gaussdist (); v[i][j] = gaussdist();
} }
} }
// Vcm = sum_i^N m*v_i/ sum_i^N M // Vcm = sum_i^N m*v_i/ sum_i^N M
// Compute center-of-mas velocity according to the formula above // Compute center-of-mas velocity according to the formula above
double vCM[3] = { 0, 0, 0 }; double vCM[3] = {0, 0, 0};
for (i = 0; i < N; i++) { for (i = 0; i < N; i++) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
@ -666,7 +644,7 @@ initializeVelocities () {
} }
} }
lambda = sqrt (3 * (N - 1) * Tinit / vSqdSum); lambda = sqrt(3 * (N - 1) * Tinit / vSqdSum);
for (i = 0; i < N; i++) { for (i = 0; i < N; i++) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
@ -677,19 +655,18 @@ initializeVelocities () {
} }
// Numerical recipes Gaussian distribution number generator // Numerical recipes Gaussian distribution number generator
double double gaussdist() {
gaussdist () {
static bool available = false; static bool available = false;
static double gset; static double gset;
double fac, rsq, v1, v2; double fac, rsq, v1, v2;
if (!available) { if (!available) {
do { do {
v1 = 2.0 * rand () / double (RAND_MAX) - 1.0; v1 = 2.0 * rand() / double(RAND_MAX) - 1.0;
v2 = 2.0 * rand () / double (RAND_MAX) - 1.0; v2 = 2.0 * rand() / double(RAND_MAX) - 1.0;
rsq = v1 * v1 + v2 * v2; rsq = v1 * v1 + v2 * v2;
} while (rsq >= 1.0 || rsq == 0.0); } while (rsq >= 1.0 || rsq == 0.0);
fac = sqrt (-2.0 * log (rsq) / rsq); fac = sqrt(-2.0 * log(rsq) / rsq);
gset = v1 * fac; gset = v1 * fac;
available = true; available = true;

View file

@ -25,6 +25,7 @@
*/ */
#include <emmintrin.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -475,22 +476,26 @@ double Kinetic() { // Write Function here!
return kin; return kin;
} }
// void void transposeMatrix(double mat[MAXPART][3], double matT[3][MAXPART]) {
// transposeMatrix (double mat[MAXPART][3], double matT[3][MAXPART]) { for (int i = 0; i < 3; i++) {
// for (int i = 0; i < 3; i++) { for (int j = 0; j < MAXPART; j++) {
// for (int j = 0; j < MAXPART; j++) { matT[i][j] = mat[j][i];
// matT[i][j] = mat[j][i]; }
// } }
// } }
// } void transposeMatrix2(double matT[MAXPART][3], double mat[3][MAXPART]) {
for (int i = 0; i < MAXPART; i++) {
for (int j = 0; j < 3; j++) {
matT[i][j] = mat[j][i];
}
}
}
double PotentialAndAcceleration() { double PotentialAndAcceleration(double dt) {
memset(a, 0, sizeof(a)); memset(a, 0, sizeof(a));
double Pot = 0.; double Pot = 0.;
// double rT[3][MAXPART];
// transposeMatrix (r, rT);
for (int i = 0; i < N - 1; i++) { for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) { for (int j = i + 1; j < N; j++) {
double quot, rnorm, term1, term2; double quot, rnorm, term1, term2;
// CALCULATE POTENTIAL ENERGY // CALCULATE POTENTIAL ENERGY
@ -504,23 +509,24 @@ double PotentialAndAcceleration() {
// ACCEL // ACCEL
posItoJ[k] = distTmp; posItoJ[k] = distTmp;
} }
quot = sigma * sigma / dist;
term2 = quot * quot * quot;
Pot += epsilon_8 * term2 * (term2 - 1);
// From derivative of Lennard-Jones with sigma and epsilon // From derivative of Lennard-Jones with sigma and epsilon
// set equal to 1 in natural units! // set equal to 1 in natural units!
double distSqd = dist * dist; double distSqd = dist * dist * dist;
double rSqd_inv4 = 1.0 / (distSqd * distSqd); double rSqd_inv7 = distSqd * distSqd * dist;
double rSqd_inv7 = rSqd_inv4 / (distSqd * dist); double f = (48. - (24. * distSqd)) / rSqd_inv7;
double f = 48.0 * rSqd_inv7 - 24 * rSqd_inv4;
// from F = ma, where m = 1 in natural units! // from F = ma, where m = 1 in natural units!
for (int k = 0; k < 3; k++) { for (int k = 0; k < 3; k++) {
double tmp = posItoJ[k] * f; double tmp = posItoJ[k] * f;
a[i][k] += tmp; a[i][k] += tmp;
a[j][k] -= tmp; a[j][k] -= tmp;
} }
quot = sigma * sigma / dist;
term2 = quot * quot * quot;
Pot += epsilon_8 * term2 * (term2 - 1.);
}
for (int j = 0; j < 3; j++) {
v[i][j] += 0.5 * a[i][j] * dt;
} }
} }
return Pot; return Pot;
@ -603,21 +609,22 @@ double VelocityVerlet(double dt, int iter, double *PE, FILE *fp) {
// printf(" Updated Positions!\n"); // printf(" Updated Positions!\n");
for (i = 0; i < N; i++) { for (i = 0; i < N; i++) {
for (j = 0; j < 3; j++) { for (j = 0; j < 3; j++) {
r[i][j] += v[i][j] * dt + 0.5 * a[i][j] * dt * dt; double tmp = 0.5 * a[i][j] * dt;
r[i][j] += v[i][j] * dt + tmp * dt;
v[i][j] += 0.5 * a[i][j] * dt; v[i][j] += tmp;
} }
// printf(" %i %6.4e %6.4e %6.4e\n",i,r[i][0],r[i][1],r[i][2]); // printf(" %i %6.4e %6.4e %6.4e\n",i,r[i][0],r[i][1],r[i][2]);
} }
// Update accellerations from updated positions // Update accellerations from updated positions
// computeAccelerations (); // computeAccelerations ();
*PE = PotentialAndAcceleration(); *PE = PotentialAndAcceleration(dt);
// Update velocity with updated acceleration // Update velocity with updated acceleration
for (i = 0; i < N; i++) { // for (i = 0; i < N; i++) {
for (j = 0; j < 3; j++) { // for (j = 0; j < 3; j++) {
v[i][j] += 0.5 * a[i][j] * dt; // v[i][j] += 0.5 * a[i][j] * dt;
} // }
} //}
// Elastic walls // Elastic walls
for (i = 0; i < N; i++) { for (i = 0; i < N; i++) {