-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunActive.cpp
More file actions
236 lines (232 loc) · 8.64 KB
/
Copy pathrunActive.cpp
File metadata and controls
236 lines (232 loc) · 8.64 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// 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 <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) {
// read input and make new directory denoted by T: everything false
// read and save same directory denoted by T: readAndSaveSameDir = true
// read directory denoted by T and save in new directory denoted by T: readAndMakeNewDir = true
// read directory denoted by T and save in "dynamics" dirctory: readAndSaveSameDir = true and runDynamics = true
// read NH directory denoted by T for all previous options: readNH = true
// save in "active" directory for all the previous options: activeDir = true
// read input and save in "dynamics" directory: justRun = true
bool readNH = false, activeDir = true, justRun = false;
bool readAndMakeNewDir = false, readAndSaveSameDir = false, runDynamics = false;
// variables
bool initAngles = false, readState = true, saveFinal = true, logSave = false, linSave = true;
long numParticles = atol(argv[9]), nDim = atol(argv[10]), maxStep = atof(argv[6]);
long checkPointFreq = int(maxStep / 10), linFreq = int(checkPointFreq / 10), saveEnergyFreq = int(linFreq / 10);
long initialStep = atof(argv[7]), step = 0, firstDecade = 0, multiple = 1, saveFreq = 1, updateCount = 0;
double ec = atof(argv[12]), LJcut = 4, cutDistance, cutoff = 0.5, sigma, damping, waveQ, width;
double forceUnit, timeUnit, timeStep = atof(argv[2]), inertiaOverDamping = atof(argv[8]);
double Tinject = atof(argv[3]), tp = atof(argv[4]), driving = atof(argv[5]);
std::string outDir, energyFile, currentDir, potType = argv[11], inDir = argv[1], dirSample, whichDynamics = "active";
//thrust::host_vector<double> boxSize(nDim);
if(nDim == 3) {
LJcut = 2.5;
}
// initialize sp object
SP2D sp(numParticles, nDim);
sp.setParticleType(simControlStruct::particleEnum::active);
if(readNH == true) {
whichDynamics = "nh";
}
sp.setEnergyCostant(ec);
if(potType == "lj") {
sp.setPotentialType(simControlStruct::potentialEnum::lennardJones);
whichDynamics = whichDynamics + argv[12] + "/";
sp.setLJcutoff(LJcut);
} else if(potType == "wca") {
sp.setPotentialType(simControlStruct::potentialEnum::WCA);
whichDynamics = "active-wca/";
} else {
whichDynamics = "active/";
cout << "Setting default harmonic potential" << endl;
sp.setWallType(simControlStruct::wallEnum::harmonic);
}
if(activeDir == true) {
readNH = false;
whichDynamics = "tp";
dirSample = whichDynamics + argv[4] + "-f0" + argv[5] + "/";
} else {
if(readNH == true) {
dirSample = whichDynamics + "T" + argv[3] + "/";
} else {
dirSample = whichDynamics + "tp" + argv[4] + "-f0" + argv[5] + "/";
}
}
ioSPFile ioSP(&sp);
// set input and output
if(justRun == true) {
outDir = inDir + "dynamics/";
if(std::experimental::filesystem::exists(outDir) == false) {
std::experimental::filesystem::create_directory(outDir);
}
if(readAndSaveSameDir == true) {
inDir = outDir;
}
} else {
if (readAndSaveSameDir == true) {//keep running the same dynamics
readState = true;
inDir = inDir + dirSample;
outDir = inDir;
if(runDynamics == true) {
if(readNH == true) {
inDir = inDir + "damping" + argv[8] + "/";
outDir = outDir + "damping" + argv[8] + "/tp" + argv[4] + "-f0" + argv[5] + "/";
}
if(logSave == true) {
inDir = outDir;
outDir = outDir + "dynamics-log/";
}
if(linSave == true) {
inDir = outDir;
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;
if(activeDir == true) {
outDir = inDir + "../" + dirSample;
} else {
outDir = inDir + "../../" + dirSample;
}
} else {
initAngles = true; // initializing from NVT
if(activeDir == true) {
if(std::experimental::filesystem::exists(inDir + dirSample) == false) {
std::experimental::filesystem::create_directory(inDir + dirSample);
}
} else {
if(std::experimental::filesystem::exists(inDir + whichDynamics) == false) {
std::experimental::filesystem::create_directory(inDir + whichDynamics);
}
}
outDir = inDir + dirSample;
if(readNH == true) {
inDir = outDir;
}
}
std::experimental::filesystem::create_directory(outDir);
}
}
cout << "inDir: " << inDir << endl << "outDir: " << outDir << endl;
ioSP.readParticlePackingFromDirectory(inDir, numParticles, nDim);
if(readState == true) {
ioSP.readParticleState(inDir, numParticles, nDim, initAngles);
}
if(initAngles == true) sp.initializeParticleAngles();
// output file
energyFile = outDir + "energy.dat";
ioSP.openEnergyFile(energyFile);
// initialization
sigma = 2 * sp.getMeanParticleSigma();
damping = sqrt(inertiaOverDamping) / sigma;
timeUnit = sigma / sqrt(ec);
forceUnit = ec / sigma;
timeStep = sp.setTimeStep(timeStep * timeUnit);
cout << "Units - time: " << timeUnit << " space: " << sigma << " force: " << forceUnit << " time step: " << timeStep << endl;
cout << "Thermostat - damping: " << damping << " Tinject: " << Tinject << " noise magnitude: " << sqrt(2*damping*Tinject) << endl;
damping /= timeUnit;
driving = driving*forceUnit;
tp *= timeUnit;
sp.setSelfPropulsionParams(driving, tp);
ioSP.saveLangevinParams(outDir, damping);
// initialize simulation
sp.initSoftParticleLangevin(Tinject, damping, readState);
cutDistance = sp.setDisplacementCutoff(cutoff);
sp.calcParticleNeighbors(cutDistance);
sp.calcParticleForceEnergy();
sp.resetUpdateCount();
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.softParticleLangevinLoop();
if(step % saveEnergyFreq == 0) {
ioSP.saveEnergy(step+initialStep, timeStep, numParticles);
if(step % checkPointFreq == 0) {
cout << "Active: current step: " << step + initialStep;
cout << " E/N: " << sp.getParticleEnergy() / numParticles;
cout << " W/N: " << sp.getParticleWork() / 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);
}
}
}
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.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.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.closeEnergyFile();
return 0;
}