-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjamPacking.cpp
More file actions
191 lines (187 loc) · 7.62 KB
/
Copy pathjamPacking.cpp
File metadata and controls
191 lines (187 loc) · 7.62 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
//
// Author: Francesco Arceri
// Date: 10-25-2021
//
// Include C++ header files
#include "include/SP2D.h"
#include "include/FileIO.h"
#include "include/Simulator.h"
#include "include/FIRE.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) {
// variables
bool read = false, readState = false, increasedStep1 = false, increasedStep2 = false;
long numParticles = 65536, nDim = 2;
long iteration = 0, maxIterations = 1e06, minStep = 20, numStep = 0, overJamCount = 0;
long maxStep = 1e04, step = 0, maxSearchStep = 1500, searchStep = 0, repetition;
long printIter = int(maxIterations / 10), printFreq = int(maxStep / 10), updateFreq = 1e03;
double polydispersity = 0.20, previousPhi, currentPhi, deltaPhi = 1e-02, scaleFactor, newTimeStep;
double cutDistance, cutoff = 2, forceTollerance0 = 1e-10, pressureTollerance = 1e-08, phi1 = 0.4, phi2 = 0.84;
double forceCheck, previousForceCheck, energyCheck, energyTollerance = 1e-20, forceTollerance;
double FIREStep = 2e-03, dt = 5e-03, phi0 = 0.2, phiTh = 0.88, pressure, maxDelta;
double ec = 240, Tinject = 1e-03, sigma, damping, inertiaOverDamping = 10, timeStep, timeUnit;
bool jamCheck = 0, overJamCheck = 0, underJamCheck = 0;
std::string currentDir, outDir = argv[1], inDir;
// fire paramaters: a_start, f_dec, f_inc, f_a, dt, dt_max, a
std::vector<double> particleFIREparams = {0.2, 0.5, 1.1, 0.99, FIREStep, 10*FIREStep, 0.2};
thrust::host_vector<double> particlePos(numParticles * nDim);
thrust::host_vector<double> particleRad(numParticles);
// initialize sp object
SP2D sp(numParticles, nDim);
ioSPFile ioSP(&sp);
std::experimental::filesystem::create_directory(outDir);
// read initial configuration
if(read == true) {
inDir = argv[2];
inDir = outDir + inDir;
ioSP.readParticlePackingFromDirectory(inDir, numParticles, nDim);
particlePos = sp.getParticlePositions();
particleRad = sp.getParticleRadii();
if(readState == true) {
ioSP.readParticleState(inDir, numParticles, nDim);
}
} else {
sp.setPolyRandomParticles(phi0, polydispersity);
}
sp.setEnergyCostant(ec);
currentPhi = sp.getParticlePhi();
cout << "Current phi: " << currentPhi << endl;
previousPhi = currentPhi;
while (searchStep < maxSearchStep) {
sp.initFIRE(particleFIREparams, minStep, numStep, numParticles);
sp.setParticleMassFIRE();
cutDistance = sp.setDisplacementCutoff(cutoff);
sp.calcParticleNeighborList(cutDistance);
sp.calcParticleForceEnergy();
iteration = 0;
forceCheck = sp.getParticleMaxUnbalancedForce();
energyCheck = sp.getParticleEnergy();
repetition = 0;
previousForceCheck = 0;
newTimeStep = FIREStep;
forceTollerance = forceTollerance0 / sp.getMeanParticleSigma();
while((forceCheck > forceTollerance) && (iteration != maxIterations)) {
sp.particleFIRELoop();
forceCheck = sp.getParticleMaxUnbalancedForce();
energyCheck = sp.getParticleEnergy() / numParticles;
if(iteration % printIter == 0 && iteration != 0) {
cout << "FIRE: iteration: " << iteration;
cout << " maxUnbalancedForce: " << setprecision(precision) << forceCheck;
cout << " energy: " << energyCheck << "\n" << endl;
if(previousForceCheck <= forceCheck) {
repetition += 1;
}
if(repetition > 3) {
newTimeStep /= 2;
sp.setTimeStepFIRE(newTimeStep);
cout << "Dividing the time step in half" << endl;
repetition = 0;
}
previousForceCheck = forceCheck;
}
maxDelta = sp.getParticleMaxDisplacement();
if(3*maxDelta > cutoff) {
sp.calcParticleNeighborList(cutDistance);
sp.resetLastPositions();
}
iteration += 1;
}
pressure = sp.getParticlePressure();
cout << "FIRE: iteration: " << iteration;
cout << " maxUnbalancedForce: " << setprecision(precision) << forceCheck;
cout << " energy: " << energyCheck << endl;
// save minimized configuration
std::string currentDir = outDir + std::to_string(sp.getParticlePhi()) + "/";
std::experimental::filesystem::create_directory(currentDir);
ioSP.saveParticlePacking(currentDir);
// check configuration after energy minimization
//jamCheck = (energyCheck < 2.0 * energyTollerance && energyCheck > energyTollerance);
//overJamCheck = (energyCheck > 2.0 * energyTollerance);
//underJamCheck = (energyCheck < energyTollerance);
jamCheck = (forceCheck < 2.0 * forceTollerance && forceCheck > forceTollerance);
overJamCheck = (forceCheck > 2.0 * forceTollerance);
underJamCheck = (forceCheck < forceTollerance);
if(jamCheck) {
cout << "Compression step: " << searchStep;
cout << " Found jammed configuration, pressure: " << setprecision(precision) << pressure;
cout << " packing fraction: " << currentPhi << endl;
} else {
// compute scaleFactor with binary search
if(underJamCheck) {
scaleFactor = sqrt((currentPhi + deltaPhi) / currentPhi);
cout << "Compression step: " << searchStep;
cout << " Found underjammed configuration, scaleFactor: " << scaleFactor;
// save most recent underjammed configuration
previousPhi = currentPhi;
particlePos = sp.getParticlePositions();
particleRad = sp.getParticleRadii();
} else if(overJamCheck) {
deltaPhi *= 0.5;
scaleFactor = sqrt((previousPhi + deltaPhi) / previousPhi);
cout << "Compression step: " << searchStep;
cout << " Found overjammed configuration, scaleFactor: " << scaleFactor;
// copy back most recent underjammed configuration and compress half much
sp.setParticlePositions(particlePos);
sp.setParticleRadii(particleRad);
if(currentPhi > phi2) {
overJamCount += 1;
}
if(overJamCount > 10) {
// stop the search after finding 10 overjammed configurations
searchStep = maxSearchStep;
}
}
if(currentPhi > phi1) {
// run dynamics
sigma = sp.getMeanParticleSigma();
damping = sqrt(inertiaOverDamping) / sigma;
timeUnit = 1 / damping;
timeStep = sp.setTimeStep(dt * timeUnit);
cout << "\nRun dynamics, time step: " << timeStep << endl;
step = 0;
// thermalize packing after each energy minimization
sp.initSoftParticleLangevin(Tinject, damping, true); // readState = false
while(step != maxStep) {
sp.softParticleLangevinLoop();
if(step % printFreq == 0) {
cout << "NVT: current step: " << step;
cout << " U/N: " << sp.getParticleEnergy() / numParticles;
cout << " T: " << sp.getParticleTemperature() << endl;
}
maxDelta = sp.getParticleMaxDisplacement();
if(3*maxDelta > cutoff) {
sp.calcParticleNeighborList(cutDistance);
sp.resetLastPositions();
}
step += 1;
}
}
sp.scaleParticles(scaleFactor);
currentPhi = sp.getParticlePhi();
cout << "\nNew packing fraction: " << currentPhi << endl;
searchStep += 1;
// update parameters as the packing gets closer to jamming
if(currentPhi > phi1 && increasedStep1 == false) {
deltaPhi /= 5;
energyTollerance = 1e-18;
increasedStep1 = true;
}
if(currentPhi > phi2 && increasedStep2 == false) {
deltaPhi /= 5;
energyTollerance = 1e-16;
increasedStep2 = true;
}
}
}
return 0;
}