-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunNH.cpp
More file actions
192 lines (188 loc) · 6.86 KB
/
Copy pathrunNH.cpp
File metadata and controls
192 lines (188 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// Author: Francesco Arceri
// Date: 10-03-2021
//
// Include C++ header files
#include "include/SP2D.h"
#include "include/FileIO.h"
#include "include/Simulator.h"
#include "include/defs.h"
#include <vector>
#include <tuple>
#include <string>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <functional>
#include <utility>
#include <thrust/host_vector.h>
#include <experimental/filesystem>
using namespace std;
int main(int argc, char **argv) {
// variables
bool readAndMakeNewDir = false, readAndSaveSameDir = false, runDynamics = false;
// readAndMakeNewDir reads the input dir and makes/saves a new output dir (cool or heat packing)
// readAndSaveSameDir reads the input dir and saves in the same input dir (thermalize packing)
// runDynamics works with readAndSaveSameDir and saves all the dynamics (run and save dynamics)
bool readState = true, saveFinal = true, logSave = false, linSave = false, alltoall = false, fixedbc = false;
long numParticles = atol(argv[6]), nDim = atol(argv[7]), maxStep = atof(argv[4]);
long checkPointFreq = int(maxStep / 10), linFreq = int(checkPointFreq / 10), saveEnergyFreq = int(linFreq / 10);
long initialStep = atof(argv[5]), step = 0, firstDecade = 0, multiple = 1, saveFreq = 1, updateCount = 0;
double LJcut = 4, cutoff = 0.5, cutDistance, waveQ, timeStep = atof(argv[2]), timeUnit, sigma;
double ec = 2, ew = ec, Tinject = atof(argv[3]), mass = 10, damping = 1;
std::string outDir, energyFile, currentDir, inDir = argv[1], potType = argv[8], dirSample, whichDynamics = "nh2/";
dirSample = whichDynamics + "T" + argv[3] + "/";
if(nDim == 3) {
LJcut = 2.5;
}
// initialize sp object
SP2D sp(numParticles, nDim);
if(fixedbc == true) {
sp.setGeometryType(simControlStruct::geometryEnum::squareWall);
sp.setWallEnergyScale(ew);
}
sp.setEnergyCostant(ec);
if(potType == "lj") {
sp.setPotentialType(simControlStruct::potentialEnum::lennardJones);
cout << "Setting Lennard-Jones potential" << endl;
sp.setLJcutoff(LJcut);
} else if(potType == "wca") {
sp.setPotentialType(simControlStruct::potentialEnum::WCA);
cout << "Setting WCA potential" << endl;
} else {
cout << "Setting Harmonic potential" << endl;
}
if(alltoall == true) {
sp.setNeighborType(simControlStruct::neighborEnum::allToAll);
}
ioSPFile ioSP(&sp);
// set input and output
if (readAndSaveSameDir == true) {//keep running the same dynamics
readState = true;
inDir = inDir + dirSample;
outDir = inDir;
if(runDynamics == true) {
if(logSave == true) {
outDir = outDir + "dynamics-log/";
} else {
outDir = outDir + "dynamics/";
}
if(std::experimental::filesystem::exists(outDir) == true) {
//if(initialStep != 0) {
inDir = outDir;
//}
} else {
std::experimental::filesystem::create_directory(outDir);
}
}
} else {//start a new dyanmics
if(readAndMakeNewDir == true) {
readState = true;
outDir = inDir + "../../" + dirSample;
} else {
if(std::experimental::filesystem::exists(inDir + whichDynamics) == false) {
std::experimental::filesystem::create_directory(inDir + whichDynamics);
}
outDir = inDir + dirSample;
}
std::experimental::filesystem::create_directory(outDir);
}
ioSP.readParticlePackingFromDirectory(inDir, numParticles, nDim);
if(readState == true) {
ioSP.readParticleState(inDir, numParticles, nDim);
ioSP.readNoseHooverParams(inDir, mass, damping);
}
// output file
energyFile = outDir + "energy.dat";
ioSP.openEnergyFile(energyFile);
// initialization
sigma = sp.getMeanParticleSigma();
timeUnit = sigma;//epsilon and mass are 1 sqrt(m sigma^2 / epsilon)
timeStep = sp.setTimeStep(timeStep * timeUnit);
cout << "Units - time: " << timeUnit << " space: " << sigma << endl;
cout << "Tinject: " << Tinject << " time step: " << timeStep << endl;
// initialize simulation
sp.initSoftParticleNoseHoover(Tinject, mass, damping, readState);
cutDistance = sp.setDisplacementCutoff(cutoff);
sp.calcParticleNeighbors(cutDistance);
sp.calcParticleForceEnergy();
sp.resetUpdateCount();
sp.setInitialPositions();
waveQ = sp.getSoftWaveNumber();
// record simulation time
float elapsed_time_ms = 0;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
// run integrator
while(step != maxStep) {
sp.softParticleNoseHooverLoop();
if(step % saveEnergyFreq == 0) {
ioSP.saveSimpleEnergy(step+initialStep, timeStep, numParticles);
if(step % checkPointFreq == 0) {
cout << "NVE: current step: " << step;
cout << " E/N: " << sp.getParticleEnergy() / numParticles;
cout << " T: " << sp.getParticleTemperature();
cout << " ISF: " << sp.getParticleISF(waveQ);
updateCount = sp.getUpdateCount();
if(step != 0 && updateCount > 0) {
cout << " number of updates: " << updateCount << " frequency " << checkPointFreq / updateCount << endl;
} else {
cout << " no updates" << endl;
}
sp.resetUpdateCount();
if(saveFinal == true) {
ioSP.saveParticlePacking(outDir);
//ioSP.saveParticleNeighbors(outDir);
ioSP.saveNoseHooverParams(outDir);
if(nDim == 3) {
ioSP.saveDumpPacking(outDir, numParticles, nDim, step);
}
}
}
}
if(logSave == true) {
if(step > (multiple * checkPointFreq)) {
saveFreq = 1;
multiple += 1;
}
if((step - (multiple-1) * checkPointFreq) > saveFreq*10) {
saveFreq *= 10;
}
if(((step - (multiple-1) * checkPointFreq) % saveFreq) == 0) {
currentDir = outDir + "/t" + std::to_string(initialStep + step) + "/";
std::experimental::filesystem::create_directory(currentDir);
ioSP.saveParticleState(currentDir);
ioSP.saveNoseHooverParams(currentDir);
//ioSP.saveParticleNeighbors(currentDir);
}
}
if(linSave == true) {
if((step % linFreq) == 0) {
currentDir = outDir + "/t" + std::to_string(initialStep + step) + "/";
std::experimental::filesystem::create_directory(currentDir);
ioSP.saveParticleState(currentDir);
ioSP.saveNoseHooverParams(currentDir);
//ioSP.saveParticleNeighbors(currentDir);
}
}
step += 1;
}
// instrument code to measure end time
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsed_time_ms, start, stop);
printf("Time to calculate results on GPU: %f ms.\n", elapsed_time_ms); // exec. time
// save final configuration
if(saveFinal == true) {
ioSP.saveParticlePacking(outDir);
//ioSP.saveParticleNeighbors(outDir);
ioSP.saveNoseHooverParams(outDir);
if(nDim == 3) {
ioSP.saveDumpPacking(outDir, numParticles, nDim, step);
}
}
ioSP.closeEnergyFile();
return 0;
}