-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMD-simulation-temperature.cpp
More file actions
202 lines (178 loc) · 6.52 KB
/
MD-simulation-temperature.cpp
File metadata and controls
202 lines (178 loc) · 6.52 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
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const int N = 16; // Number of particles
double L = 4; // Declare & assign the linear size of the system
double runMax = 4000; // Declare & assign the maximum number of runs
double sigma = 1.0; // sigma (in reduced units)
//double a = sigma; // a = lattice constant
double delta_r = sigma / 50; // => moves particles randomly within +/- sigma/2
double r[N][2]; //r[N particles][x and y-coordinates]: Declare array of positions; 3d: [N][3]
double v[N][2]; //v[N particles][x and y-coordinates]: Declare array of velocities; 3d: [N][3]
double acc[N][2]; // Declare arrays of accelerations 3d: [N][3]
double vcm[2];
double Vmax = 1.0; // Declare & assign the maximum initial velocity
double dt = 0.005;
double force =0;
double Potential=0;
double Energy=0;
double Temperature=0;
double vSquared = 0;
void Initialize()
{
// Initialize positions
int n = int(ceil(pow(N, 1.0/2))); // n=square root(N)=number of atoms in each row/column
double a = L / int(ceil(pow(N, 1.0/2))); // a = lattice spacing
// See photo of calculations in Telegram
// CompPhys953_Gen or _Edu [3d: 1.0/3]
ofstream write("positions2.dat"); // Use ofstream (output file stream) to create a file
// named "positions.dat" to write data to.
// "cout" writes to screen. "write" writes to file.
int p = 0; // Counter: number of particles Initialized
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++) // 3d: for (int z = 0; z < n; z++)
{
if (p < N) // Continue placing particles
{
// r[particle p][x-coordinate]
r[p][0] = (x+0.5)*a + 2*(rand()/double(RAND_MAX)-0.5)*delta_r; // Displace x of
// particle p randomly within +/- sigma/2
write << r[p][0] << '\t';
// r[particle p][y-coordinate]
r[p][1] = (y+0.5)*a + 2*(rand()/double(RAND_MAX)-0.5)*delta_r; // Displace y of
// particle p randomly within +/- sigma/2
write << r[p][1] << '\t';
write << '\n';
}
++p; // Increment particle counter by 1
}
write.close();
// Initialize velocities
for (int i = 0; i < N; i++) // Initialize all velocities
for (int j = 0; j < 2; j++) // Vx and Vy 3d: [< 3]
v[i][j] = Vmax * (2*rand()/double(RAND_MAX)-1); // particle velocity is given values
// between -Vmax and +Vmax
}
template <typename T>
int sgn(T val) {
return (T(0) < val) - (val < T(0));
}
void Accelerate()
{
Potential = 0;
force = 0;
for (int i = 0; i < N; i++) // set accelerations to zero
for (int j = 0; j < 2; j++) // 3d: < 3
acc[i][j] = 0;
for (int i = 0; i < N-1; i++)
for (int j = i+1; j < N; j++) // Sum over all j>i pairs
{
double rij[2]; // 3d: [3] // position of i relative to j
double rSquared = 0;
for (int k = 0; k < 2; k++) { // 3d: [3]
rij[k] = r[i][k] - r[j][k];
if (fabs(rij[k]) > L / 2)
rij[k] -= sgn(rij[k]) * L;
rSquared += rij[k] * rij[k];
}
//Differentiate LJ -V(x) with respect to x to get the force
Potential += 4 * (pow(rSquared, -6) - pow(rSquared, -3));
force = 24 * (2 * pow(rSquared, -7) - pow(rSquared, -4));
for (int k = 0; k < 2; k++) // 3d: [3]
{
acc[i][k] += rij[k]*force;
acc[j][k] -= rij[k]*force;
}
}
}
double Central_Mass_Velocity() {
for (int i = 0; i < N; i++)
for (int j = 0; j < 2; j++)
vcm[j] += v[i][j] / N;
for (int i = 0; i < N; i++)
for (int j = 0; j < 2; j++)
v[i][j] -= vcm[j];
}
void pbc()
{
for(int i=0; i<N; i++)
{
if (r[i][0] < 0) r[i][0] += L;
if (r[i][0] > L) r[i][0] -= L;
if (r[i][1] < 0) r[i][1] += L;
if (r[i][1] > L) r[i][1] -= L;
}
}
void velocityVerlet(double dt) {
Accelerate();
for (int i = 0; i < N; i++)
for (int j = 0; j < 2; j++) { //[2d [2]]
r[i][j] += v[i][j] * dt + 0.5 * acc[i][j] * dt * dt;
v[i][j] += 0.5 * acc[i][j] * dt;
}
Accelerate();
for (int i = 0; i < N; i++)
for (int j = 0; j < 2; j++) //[2d [2]]
v[i][j] += 0.5 * acc[i][j] * dt;
pbc();
}
double instantaneousTemperature() {
vSquared = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < 2; j++) //[2d [2]]
vSquared += v[i][j] * v[i][j];
double T = vSquared / (2 * (N - 1));
return T;
}
//////////////////// MAIN ////////////////////////
int main(int argc, char *argv[]) {
srand (time(NULL));
Initialize();
//ofstream position1("position1.txt");
//ofstream position2("position2.txt");
//ofstream position3("position3.txt");
ofstream temperature("temperature.txt");
ofstream energy("energy.txt");
for (int i = 0; i < runMax; i++){
velocityVerlet(dt);
/////////////////////////////////////////////////////////////////////////////////
//if ((i >= 0) && (i % 10 == 0) && (i <= 20)) {
//for (int p = 0; p < N; p++) {
//for (int q = 0; q < 2; q++) {
//position1 << r[p][q] << '\t';
//}
//position1 << '\n';
//}
//}
//if ((i >= 40) && (i % 10 == 0) && (i <= 800)) {
//for (int p = 0; p < N; p++) {
//for (int q = 0; q < 2; q++) {
//position2 << r[p][q] << '\t';
//}
//position2 << '\n';
//}
//}
//if ((i >= 2600) && (i % 10 == 0) && (i <= 3200)) {
//for (int p = 0; p < N; p++) {
//for (int q = 0; q < 2; q++) {
//position3 << r[p][q] << '\t';
//}
//position3 << '\n';
//}
//}
////////////////////////////////////////////////////////////////////////////////////////////////////
Temperature = instantaneousTemperature();
temperature << i*dt << '\t' << Temperature << '\n';
// Energy
Energy=Potential + vSquared / 2;
energy << i*dt << '\t' << Energy << '\n';
//////////////////////////////////////////////////////////////////////
if ( (i % 10 == 0) && (i < runMax) ) // set central mass velocity equal to zero after each 10 time-steps
double Central_Mass_Velocity();
}
return 0;
}