-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcycle.cc
358 lines (320 loc) · 13.6 KB
/
cycle.cc
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Cycles for each voronoi node are computed
* Useful in material science where the voronoi nodes
* are taken as tetrahedra interstitial sites
* An octahedral interstitial site is computed as centroid of
* voronoi nodes present in a cycle of length 4.
* Author: Bharat Medasani
* Date: Jan 02, 2013
*/
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cassert>
#include "mindist.h"
#include "geometry.h"
#include "networkstorage.h"
#include "graphstorage.h"
#include "sphere_approx.h"
#include "network.h"
#include "cycle.h"
#include "voronoicell.h"
#include "network.h"
using namespace std;
typedef vector<VOR_NODE>::iterator VnIt;
typedef vector<VOR_NODE>::const_iterator VnCit;
typedef vector<DIJKSTRA_NODE>::iterator DnIt;
typedef vector<DIJKSTRA_NODE>::const_iterator DnCit;
typedef vector<CONN>::iterator CnIt;
CYCLE::CYCLE() {length = 0;}
/* Function to compute girth of voronoi network with integer weights
* Args:
* Input:
* inp_vor: Pointer to input voronoi network
* range_flag: Denotes wether the weights have value other than 1
* range: Max. value of weights. weights = {1,2,...,range}
* Output:
* Returns length of smallest cycle
*/
int girth(const VORONOI_NETWORK* inp_vor, bool weight_flag, int wegith_range)
{
return 0;
}
void edge_finder(const VORONOI_NETWORK* vornet, int node_index, vector<int>* edge_indices)
{
typedef vector<VOR_EDGE>::const_iterator VeCit;
int i = 0;
VeCit it = vornet->edges.begin();
for (; it != vornet->edges.end(); ++i, ++it){
if (it->from == node_index || it->to == node_index)
edge_indices->push_back(i);
}
return;
}
/* At present I am assuming the least possible GIRTH to be 4 in a voronoi node
* of regular crystals.
*/
static const int GIRTH = 4;
/* Function to compute a cycle of given length for each voronoi node
* Args:
* Input:
* dnet: Pointer to input Dijkstra network
* cycle_length: Length of cycle to be computed
* If 0, minimum length cycle is computed
* weight_flag: Denotes whether the weights have value other than 1
* Ignored at present. Weight 1 is always used.
* weight_range: Max. value of weights. weights = {1,2,...,range}
* Ignored at present. Range 1 is used.
* Output:
* Returns false if a cycle of given length cannot be found
*/
/* Algorithm (Shortest cycle):
* 1) For each Dijkstra node, identifiy all the edges from the node.
* 2) For each edge from the node remove the edge and compute the
* shortest path between the pair of nodes in the removed edge
* using the Dijkstra's algorithm.
*/
/*
bool compute_cycle(DIJKSTRA_NETWORK* dnet, vector<CYCLE> *cycles,
int cycle_length, bool weight_flag, int weight_range)
{
// First clear the active flag for each Dijkstra node
// Set the max_radius of each connection to 1. Used in enabling or
// disabling a connection without deleting the connection
// Adds an additional check
DnIt it = dnet->nodes.begin();
for (; it != dnet->nodes.end(); ++it) {
it->active = false;
CnIt cit = it->connections.begin();
for (; cit != it->connections.end(); ++cit) cit->max_radius = 1;
}
if (!cycle_length) { // Shortest length cycle
int girth = 0;
for (int i = 0; i < dnet->nodes.size(); ++i){
int short_cycle_length = 0; //Shortest cycle length for this node
DIJKSTRA_NODE origNode = dnet->nodes.at(i);
CnIt it = origNode.connections.begin();
int conn_ind = 0;
for (; it != origNode.connections.end(); ++it, ++conn_ind){
int path_length = 0;
int orgNodeId = it->from;
int endNodeId = it->to;
map<int, int> visitedNodeDisplacement;
vector<pair<int,int> > stack;
stack.push_back(pair<int, int> (i, 0));
visitedNodeDisplacement.insert(pair<int,int>(i,0));
while (stack.size() != 0) {
// Remove the top-most node in stack
pair<int,int> nodeInfo = stack.back();
DIJKSTRA_NODE currNode = dnet->nodes.at(nodeInfo.first);
stack.pop_back();
CnIt cit = currNode.connections.begin();
int conn_ind = 0;
for (; cit != currNode.connections.end(); ++cit, ++conn_ind){
//Disable the connection and the reverse connection
cit->max_radius = 0;
// Find the index of connection from to-node to from-node
CONN reverse_conn = *it;
reverse_conn.from = it->to;
reverse_conn.to = it->from;
CnIt rev_cit;
rev_cit = find(dnet->nodes[it->to].connections.begin(),
dnet->nodes[it->to].connections.end(),
reverse_conn);
assert (rev_cit != dnet->nodes[it->to].connections.end());
rev_cit->max_radius = 0;
//Find the shortest path between the nodes of connection
}
}
}
}
}
else{
cout << "Functionality with given cycle length not implemented" << endl;
return false;
}
return false;
}
*/
/* Function to compute a cycle of given length for each voronoi node
* Args:
* Input:
* vornet: Pointer to input voronoi network
* cycle_length: Length of cycle to be computed
* If 0, minimum length cycle is computed
* weight_flag: Denotes wether the weights have value other than 1
* weight_range: Max. value of weights. weights = {1,2,...,range}
* Output:
* Returns false if the cycle of given length cannot be found
*/
/*
bool compute_cycle(const VORONOI_NETWORK* vornet, vector<CYCLE> *cycles,
int cycle_length, bool weight_flag, int weight_range)
{
//Build graph data structure
DIJKSTRA_NETWORK dnet;
DIJKSTRA_NETWORK::buildDijkstraNetwork(vornet, &dnet);
return compute_cycle(&dnet, cycles, cycle_length, weight_flag, weight_range);
}
*/
/* Function to compute a cycle of given length for each voronoi node
* Args:
* Input:
* dnet: Pointer to input Dijkstra network
* cycle_length: Length of cycle to be computed
* If 0, minimum length cycle is computed
* weight_flag: Denotes whether the weights have value other than 1
* Ignored at present. Weight 1 is always used.
* weight_range: Max. value of weights. weights = {1,2,...,range}
* Ignored at present. Range 1 is used.
* Output:
* Returns false if a cycle of given length cannot be found
*/
bool compute_4cycle(DIJKSTRA_NETWORK* dnet, vector<CYCLE> *cycles,
bool weight_flag, int weight_range)
{
/* Algorithm (Shortest cycle):
* 1) For each Dijkstra node, identifiy all the edges from the node.
* 2) For each edge from the node remove the edge and compute the
* shortest path between the pair of nodes in the removed edge
* using the Dijkstra's algorithm.
*/
// First clear the active flag for each Dijkstra node
// Set the max_radius of each connection to 1. Used in enabling or
// disabling a connection without deleting the connection
// Adds an additional check
DnIt it = dnet->nodes.begin();
int count = 0;
for (; it != dnet->nodes.end(); ++it, ++count) {
it->active = false;
CnIt cit = it->connections.begin();
for (; cit != it->connections.end(); ++cit) cit->max_radius = 1;
}
cout << "Length of vornet: " << count << endl;
it = dnet->nodes.begin();
for (; it != dnet->nodes.end(); ++it) {
//cout << "x: " << it->x << " y: " << it->y << " z: " << it->z << endl;
;
}
for (int i = 0; i < dnet->nodes.size(); ++i){
DIJKSTRA_NODE org_node = dnet->nodes.at(i);
if (i == 0) cout << "org_node: "<< org_node.x << " " << org_node.y << " " << org_node.z << endl;
CnIt it = org_node.connections.begin();
int conn_ind = 0;
for (; it != org_node.connections.end(); ++it, ++conn_ind){
int path_length = 0;
int org_node_id = it->from;
int end_node_id = it->to;
if (i == 0) cout << "end_node_id: " << end_node_id << endl;
DIJKSTRA_NODE end_node = dnet->nodes.at(end_node_id);
if (end_node.active) continue;
CnIt oit = it + 1; //Check the other connections of begin node
int oconn_ind=conn_ind + 1;
for (; oit != org_node.connections.end(); ++oit, ++oconn_ind) {
int inter_node_id = oit->to;
if (inter_node_id==org_node_id || inter_node_id==end_node_id) continue;
if (i==0) cout << " inter_node_id: " << inter_node_id << endl;
DIJKSTRA_NODE inter_node = dnet->nodes.at(inter_node_id);
if (inter_node.active) continue;
for (CnIt iit = inter_node.connections.begin(); iit != inter_node.connections.end(); ++iit)
if (i==0) cout << " to: "<< iit->to << endl;
CnIt eit = end_node.connections.begin();
int econn_ind=0;
for (; eit != end_node.connections.end(); ++eit, ++econn_ind) {
int final_node_id = eit->to;
if (final_node_id==end_node_id || final_node_id==org_node_id) continue;
if (i==0) cout << " final_node_id: " << final_node_id << endl;
DIJKSTRA_NODE final_node = dnet->nodes.at(final_node_id);
if (final_node.active) continue;
CnIt iit = inter_node.connections.begin();
for (; iit != inter_node.connections.end(); ++iit) {
if (iit->to == final_node_id){ //Cycle found
CYCLE new_cycle;
new_cycle.length=4;
new_cycle.nodes.push_back(org_node);
new_cycle.nodes.push_back(end_node);
new_cycle.nodes.push_back(inter_node);
new_cycle.nodes.push_back(dnet->nodes.at(final_node_id));
cycles->push_back(new_cycle);
}
}
}
}
}
org_node.active = true;
}
cout << "No. of cycles: " << cycles->size() << endl;
if (!cycles->size()) return false;
return true;
}
/* Function to compute a cycle of given length for each voronoi node
* Args:
* Input:
* vornet: Pointer to input voronoi network
* cycle_length: Length of cycle to be computed
* If 0, minimum length cycle is computed
* weight_flag: Denotes wether the weights have value other than 1
* weight_range: Max. value of weights. weights = {1,2,...,range}
* Output:
* Returns false if the cycle of given length cannot be found
*/
bool compute_4cycle(VORONOI_NETWORK* vornet, vector<CYCLE> *cycles,
bool weight_flag, int weight_range)
{
//Build graph data structure
DIJKSTRA_NETWORK dnet;
DIJKSTRA_NETWORK::buildDijkstraNetwork(vornet, &dnet);
return compute_4cycle(&dnet, cycles, weight_flag, weight_range);
}
void centroid(const CYCLE* const cycle, XYZ* center, vector<int>* node_ids)
{
DnCit it = cycle->nodes.begin();
//cout << "Length of nodes in the cycle: " << cycle->nodes.size() << endl;
*center = XYZ(0,0,0);
node_ids->clear();
//cout << "Length of node_ids vector: " << node_ids->size() << endl;
for (; it != cycle->nodes.end(); ++it) {
XYZ new_pt (it->x, it->y, it->z);
*center = *center + new_pt;
node_ids->push_back(it->id);
}
//cout << "Length of node_ids vector: " << node_ids->size() << endl;
*center = center->scale(1.0/cycle->length);
}
/* Function to compute the voronoi face centers
* Arguments:
* atmnet (Input) : Unit cell containing atoms/ions in ATOM_NETWORK class
* face_centers: (Output), pointer to vector of XYZ objects holding the
* coordinates of centroids of voronoi faces
*/
void face_center(ATOM_NETWORK* atmnet, vector<XYZ>* face_centers)
{
VORONOI_NETWORK vornet;
vector<VOR_CELL> vcell;
vector<BASIC_VCELL> bvcell;
performVoronoiDecomp(true, atmnet, &vornet, &vcell, true, &bvcell);
vector<VOR_CELL>::iterator vc_it;
vector<VOR_FACE>::iterator vf_it;
int vor_face_count = 0;
int vor_cell_count = 0;
for (vc_it = vcell.begin(); vc_it != vcell.end(); ++vc_it){
for (vf_it = vc_it->faces.begin(); vf_it != vc_it->faces.end(); ++vf_it){
++vor_face_count;
vector<int>::iterator iit;
if (vf_it->nodeIDs.size() > 4) {
continue;
}
vector<Point>::iterator ip_it;
cout << "Orderd vertices in the face: " << endl;
for (ip_it = vf_it->orderedVertices.begin(); ip_it != vf_it->orderedVertices.end(); ++ip_it){;
//cout << "(";
//ip_it->print(cout);
//cout << ") " ;
}
//cout << endl;
}
}
cout << "VOR_FACE_COUNT " << vor_face_count << endl;
vcell.clear();
bvcell.clear();
}