/
voronoi.cpp
242 lines (207 loc) · 8.6 KB
/
voronoi.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
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
237
238
239
240
241
242
#include "modsystem.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdio.h>
#include "string.h"
#include <chrono>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <pybind11/complex.h>
#include <pybind11/functional.h>
#include <pybind11/chrono.h>
#include <map>
#include <string>
#include <any>
#include "voro++.hh"
using namespace voro;
void get_all_neighbors_voronoi(py::dict& atoms,
const double neighbordistance,
const int triclinic,
const vector<vector<double>> rot,
const vector<vector<double>> rotinv,
const vector<double> box,
const double face_area_exponent)
{
double d;
double diffx,diffy,diffz;
double tempr,temptheta,tempphi;
vector<double> diffi, diffj, pos;
int tnx,tny,tnz, ti, tj, nverts;
double rx,ry,rz,tsum, fa, x, y, z, vol, weightsum;
vector<int> neigh,f_vert, vert_nos;
vector<double> facearea, v, faceperimeters;
voronoicell_neighbor c;
vector<vector<double>> positions = atoms[py::str("positions")].cast<vector<vector<double>>>();
//vector<bool> mask_1 = atoms[py::str("mask_1")].cast<vector<bool>>();
//vector<bool> mask_2 = atoms[py::str("mask_2")].cast<vector<bool>>();
vector<bool> ghost = atoms[py::str("ghost")].cast<vector<bool>>();
int nop = positions.size();
vector<vector<int>> neighbors(nop);
vector<vector<double>> neighbordist(nop);
vector<vector<double>> neighborweight(nop);
vector<vector<vector<double>>> diff(nop);
vector<vector<double>> r(nop);
vector<vector<double>> phi(nop);
vector<vector<double>> theta(nop);
vector<double> cutoff(nop);
//specific properties related to Voronoi
vector<double> volume(nop);
vector<vector<int>> face_vertices(nop);
vector<vector<double>> face_perimeters(nop);
vector<vector<double>> vertex_vectors(nop);
vector<vector<int>> vertex_numbers(nop);
vector<vector<vector<double>>> vertex_positions(nop);
vector<vector<bool>> vertex_unique(nop);
pre_container pcon(0.00, box[0], 0.00, box[1], 0.0, box[2], true, true, true);
for(int i=0; i<nop; i++){
pos = positions[i];
pos = remap_atom_into_box(pos, triclinic, rot, rotinv, box);
pcon.put(i, pos[0], pos[1], pos[2]);
}
pcon.guess_optimal(tnx, tny, tnz);
//container con(boxdims[0][0],boxdims[1][1],boxdims[1][0],boxdims[1][1],boxdims[2][0],boxdims[2][1],tnx,tny,tnz,true,true,true, nop);
container con(0.00, box[0], 0.00, box[1], 0.0, box[2], tnx, tny, tnz, true, true, true, nop);
pcon.setup(con);
c_loop_all cl(con);
if (cl.start()) do if(con.compute_cell(c,cl)) {
ti=cl.pid();
c.face_areas(facearea);
c.neighbors(neigh);
c.face_orders(f_vert);
c.face_vertices(vert_nos);
c.vertices(x,y,z,v);
c.face_perimeters(faceperimeters);
vol = c.volume();
tsum = 0;
vector <double> dummyweights;
vector <int> dummyneighs;
weightsum = 0.0;
for (int i=0; i<facearea.size(); i++){
weightsum += pow(facearea[i], face_area_exponent);
}
volume[ti] = vol;
vertex_vectors[ti] = v;
vertex_numbers[ti] = vert_nos;
cutoff[ti] = cbrt(3*vol/(4*3.141592653589793));
//clean up and add vertex positions
nverts = int(v.size())/3;
pos = positions[ti];
for(int si=0; si<nverts; si++){
vector<double> temp;
int li=0;
for(int vi=si*3; vi<(si*3+3); vi++){
//get distance here
temp.emplace_back(v[vi]+pos[li]);
li++;
}
vertex_positions[ti].emplace_back(temp);
vertex_unique[ti].emplace_back(!ghost[ti]);
}
for (int tj=0; tj<neigh.size(); tj++){
d = get_abs_distance(positions[ti], positions[neigh[tj]],
triclinic, rot, rotinv, box,
diffx, diffy, diffz);
neighbors[ti].emplace_back(neigh[tj]);
neighbordist[ti].emplace_back(d);
neighborweight[ti].emplace_back(pow(facearea[tj], face_area_exponent)/weightsum);
face_vertices[ti].emplace_back(f_vert[tj]);
face_perimeters[ti].emplace_back(faceperimeters[tj]);
diffi.clear();
diffi.emplace_back(diffx);
diffi.emplace_back(diffy);
diffi.emplace_back(diffz);
diff[ti].emplace_back(diffi);
convert_to_spherical_coordinates(diffx, diffy, diffz, tempr, tempphi, temptheta);
r[ti].emplace_back(tempr);
phi[ti].emplace_back(tempphi);
theta[ti].emplace_back(temptheta);
}
} while (cl.inc());
//calculation over lets assign
atoms[py::str("neighbors")] = neighbors;
atoms[py::str("neighbordist")] = neighbordist;
atoms[py::str("neighborweight")] = neighborweight;
atoms[py::str("diff")] = diff;
atoms[py::str("r")] = r;
atoms[py::str("theta")] = theta;
atoms[py::str("phi")] = phi;
atoms[py::str("cutoff")] = cutoff;
atoms[py::str("voronoi_volume")] = volume;
atoms[py::str("face_vertices")] = face_vertices;
atoms[py::str("face_perimeters")] = face_perimeters;
atoms[py::str("vertex_vectors")] = vertex_vectors;
atoms[py::str("vertex_numbers")] = vertex_numbers;
atoms[py::str("vertex_is_unique")] = vertex_unique;
atoms[py::str("vertex_positions")] = vertex_positions;
}
bool check_if_in_box(const vector<double>& pos,
const vector<double>& box){
if ((pos[0] < -0.01) || (pos[0] > box[0]+0.01)) return false;
else if ((pos[1] < -0.0001) || (pos[1] > box[1])) return false;
else if ((pos[2] < -0.0001) || (pos[2] > box[2])) return false;
else return true;
}
void clean_voronoi_vertices(py::dict& atoms,
py::dict& all_atoms,
const double neighbordistance,
const int triclinic,
const vector<vector<double>> rot,
const vector<vector<double>> rotinv,
const vector<double> box,
const double distance_cutoff){
vector<vector<vector<double>>> positions = atoms[py::str("vertex_positions")].cast<vector<vector<vector<double>>>>();
vector<vector<bool>> vertex_unique = atoms[py::str("vertex_is_unique")].cast<vector<vector<bool>>>();
vector<vector<int>> neighbors = atoms[py::str("neighbors")].cast<vector<vector<int>>>();
//vector<bool> ghost = atoms[py::str("ghost")].cast<vector<bool>>();
int nop = positions.size();
double d, diffx, diffy, diffz;
int nn;
for(int ti=0; ti<nop; ti++){
//if (ghost[ti]) continue;
for(int vi=0; vi<positions[ti].size(); vi++){
if (!vertex_unique[ti][vi]) continue;
if (!check_if_in_box(positions[ti][vi], box)){
vertex_unique[ti][vi] = false;
continue;
}
for(int vj=vi+1; vj<positions[ti].size(); vj++){
if (!vertex_unique[ti][vj]) continue;
d = get_abs_distance(positions[ti][vi], positions[ti][vj],
triclinic, rot, rotinv, box,
diffx, diffy, diffz);
if (d < distance_cutoff){
vertex_unique[ti][vj] = false;
}
}
for(int tj=0; tj<neighbors[ti].size(); tj++){
nn = neighbors[ti][tj];
if (ti==nn) continue;
//if (ghost[nn]) continue;
for(int vj=0; vj<positions[nn].size(); vj++){
if (!vertex_unique[nn][vj]) continue;
if (!check_if_in_box(positions[nn][vj], box)){
vertex_unique[nn][vj] = false;
continue;
}
d = get_abs_distance(positions[ti][vi], positions[nn][vj],
triclinic, rot, rotinv, box,
diffx, diffy, diffz);
if (d < distance_cutoff){
vertex_unique[nn][vj] = false;
}
}
}
}
}
vector<vector<double>> unique_positions;
for(int ti=0; ti<nop; ti++){
for(int tj=0; tj<vertex_unique[ti].size(); tj++){
if(vertex_unique[ti][tj]){
unique_positions.emplace_back(positions[ti][tj]);
}
}
}
all_atoms[py::str("vertex_positions_unique_skipcheck")] = unique_positions;
}