-
Notifications
You must be signed in to change notification settings - Fork 0
/
radialDistFunc.cpp
170 lines (127 loc) · 4.07 KB
/
radialDistFunc.cpp
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
#include <iostream> // for io
#include <fstream> //for file
#include <cmath> //for time
#include <random> // for random
#include <ctime> //for measuring time?
#include <cstdlib> //for srand (random seed)?
#include <algorithm> // for count
#include <vector> //for well, vector
// Constants
const double umass = 1.660539040e-27;
const double argonmass = umass*39.948;
const int T = 120;
const double kB = 1.38064852e-23;
const double sigma = 3.4e-10; // interaction cut off
const double pi = atan(1)*4;
//Box
const int boxside = 6;
const int boxsize = 4*6*6*6; // also number of particles, should change name?
const double boxlength = 10.229*sigma; //from paper
const double gridsize = boxlength/boxside; // spatial separation in initialization
struct particle // all relevant particle properties goes here
{
double posx;
double posy;
double posz;
};
particle particleArray[boxsize];
// initializes the positions, separation is specified by gridsize above.
// each loop plaves 4 particles corresponding to face centered crystal packing
void initPositions()
{
int k = 0;
std::srand(std::time(0)); // user current time as random seed
for (int kz = 0; kz < boxside; kz++)
for (int ky = 0; ky < boxside; ky++)
for (int kx = 0; kx < boxside; kx++, k += 4)
{
double displace = static_cast <double> (rand()) / static_cast <double> (RAND_MAX);
displace *= gridsize;
//displace = 0; // uncomment to remove noise
particleArray[k].posx = kx*gridsize + displace;
particleArray[k].posy = ky*gridsize + displace;
particleArray[k].posz = kz*gridsize + displace;
particleArray[k+1].posx = (kx + 0.5)*gridsize + displace;
particleArray[k+1].posy = (ky + 0.5)*gridsize + displace;
particleArray[k+1].posz = kz*gridsize + displace;
particleArray[k+2].posx = (kx + 0.5)*gridsize + displace;
particleArray[k+2].posy = ky*gridsize + displace;
particleArray[k+2].posz = (kz + 0.5)*gridsize + displace;
particleArray[k+3].posx = kx*gridsize + displace;
particleArray[k+3].posy = (ky + 0.5)*gridsize + displace;
particleArray[k+3].posz = (kz + 0.5)*gridsize + displace;
}
}
double dist (int p1, int p2)
{
double x = fabs(particleArray[p1].posx - particleArray[p2].posx);
double y = fabs(particleArray[p1].posy - particleArray[p2].posy);
double z = fabs(particleArray[p1].posz - particleArray[p2].posz);
x -= static_cast<int> (x/boxlength + 0.5) * boxlength;
y -= static_cast<int> (y/boxlength + 0.5) * boxlength;
z -= static_cast<int> (z/boxlength + 0.5) * boxlength;
double r = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
return r;
}
const int bins = 1000;
double densArr[bins];
std::vector<double> distances;
void radialdistfunc() //counts the number of particles in "shells" around x_i
{
for(int k = 0; k < boxsize; k++)
{
for(int l = 0; l < boxsize; l++)
{
if(l != k){distances.push_back(dist(k,l));}
}
}
int hist[bins];
double shell1 = 0, shell2;
for(int k = 0; k < bins; k++)
{
shell2 = ((double) k + 1)/bins*boxlength;
hist[k] = std::count_if(distances.begin(), distances.end(), [&shell1, &shell2](double i){return shell1 < i && i < shell2;});
shell1 = shell2;
std::cout << hist[k] << '\n';
}
shell1 = 0, shell2 = 0;
double vol;
for (int k = 0; k < bins; k++)
{
shell2 = 4/3*pi*pow(((double) k + 1)/bins*boxlength,3);
vol = shell2 - shell1;
densArr[k] = hist[k]*argonmass/vol;
shell1 = shell2;
}
}
void printInitPositions()
{
std::ofstream filestream;
filestream.open("radialdistpos.txt");
for(int k = 0; k < boxsize; k++)
filestream << particleArray[k].posx << " " << particleArray[k].posy << " " << particleArray[k].posz << " \n";
filestream.close();
}
void printDensArr()
{
std::ofstream filestream;
filestream.open("radialdistfunc.txt");
for(int k = 0; k < bins; k++)
filestream << k << " " << densArr[k] << "\n";
filestream.close();
}
int main()
{
int k;
initPositions();
printInitPositions();
radialdistfunc();
printDensArr();
while(true)
{
std::cout << "Give bin index:\n";
std::cin >> k;
std::cout << "density in this bin is: \n" << distances[k] << "\n";
}
return 0;
}