-
Notifications
You must be signed in to change notification settings - Fork 4
/
channel.cc
1713 lines (1358 loc) · 64.3 KB
/
channel.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#include "network.h"
#include "channel.h"
#include <cmath>
using namespace std;
/* Create a PORE that does not contain any nodes or connections
* and spans 0 unit cells.*/
PORE::PORE(){
nodes = vector<DIJKSTRA_NODE> ();
connections = vector<CONN> ();
unitCells = vector<DELTA_POS> ();
ucNodes = vector< vector<int> > ();
dimensionality = 0;
basis[0][0]=0;basis[0][1]=0;basis[0][2]=0;
basis[1][0]=0;basis[1][1]=0;basis[1][2]=0;
basis[2][0]=0;basis[2][1]=0;basis[2][2]=0;
}
/* Returns true iff the CHANNEL unit can be depicted within one unit cell*/
bool CHANNEL::isUnicellular(){
return unitCells.size() == 1;
}
/* Constructs a pore from the provided nodes in the DIJKSTRA_NETWORK.
* Reconstructs the pore by trying to minimize the number of unit cells
* required to show a single pore unit.
*/
PORE::PORE(vector<int> nodeIDs, DIJKSTRA_NETWORK *dnet, int dim, int basisVecs[3][3]){
idMappings = map<int,int> ();
reverseIDMappings = map<int,int> ();
nodes = vector<DIJKSTRA_NODE> ();
connections = vector<CONN> ();
// Reindex the nodes to facilitate storage
for(unsigned int i = 0; i < nodeIDs.size(); i++){
idMappings.insert(pair<int,int>(nodeIDs.at(i),i));
reverseIDMappings.insert(pair<int,int>(i,nodeIDs.at(i)));
}
// Iterate over all nodes in list
for(unsigned int i = 0; i < nodeIDs.size(); i++){
DIJKSTRA_NODE oldNode = dnet->nodes.at(nodeIDs.at(i));
DIJKSTRA_NODE newNode = DIJKSTRA_NODE(i, oldNode.x, oldNode.y, oldNode.z, oldNode.max_radius);
// Include connection only if end node is in pore
// Reindex connection start and end ids
for(unsigned int j = 0; j < oldNode.connections.size(); j++){
CONN oldConn = oldNode.connections.at(j);
map<int,int>::iterator id1Iter = idMappings.find(oldConn.from);
map<int,int>::iterator id2Iter = idMappings.find(oldConn.to);
if(id2Iter != idMappings.end()){
CONN newConn = CONN(id1Iter->second,id2Iter->second,oldConn.length,oldConn.max_radius,oldConn.deltaPos);
newNode.connections.push_back(newConn);
connections.push_back(newConn);
}
}
nodes.push_back(newNode);
}
basis[0][0] = basisVecs[0][0]; basis[0][1] = basisVecs[0][1]; basis[0][2] = basisVecs[0][2];
basis[1][0] = basisVecs[1][0]; basis[1][1] = basisVecs[1][1]; basis[1][2] = basisVecs[1][2];
basis[2][0] = basisVecs[2][0]; basis[2][1] = basisVecs[2][1]; basis[2][2] = basisVecs[2][2];
dimensionality = dim;
// Store unit cell vectors
v_a = dnet->v_a;
v_b = dnet->v_b;
v_c = dnet->v_c;
reconstruct(); // Reconstruct pore
}
/* Provides a vector with IDs of Voronoi nodes correcponding to the current pore */
vector <int> PORE::nodeIds(){
vector <int> list;
for(unsigned int i=0; i<reverseIDMappings.size(); i++)
{
list.push_back(reverseIDMappings.find(i)->second);
};
return list;
}
/** Nodes within the provided DIJKSTRA_NETWORK can be classified as either accessible or inaccessible,
* where sets of accessible nodes constitute a CHANNEL and inaccessible POCKET. This function identifies
* the POREs that exist for the provided particle diameter and stores them using the provided pointer
* to a vector of POREs. CHANNEL and PORE can be distinguished by dimentionality.
* In addition, the pointer to the vector of bools is used to a store bool
* for each VORONOI_NODE, where infoStorage[i] is true if node #i is accessible. */
/* WARNING: this function assumes that at some point before VORONOI_NETWORK was pruned
* with prune() function. This is to set "active" flag on nodes larger than probe radius */
void PORE::findChannelsAndPockets(DIJKSTRA_NETWORK *dnet, vector<bool> *infoStorage,
vector<PORE> *pores)
{
//Define three access types
const int ACCESSIBLE = 1;
const int INACCESSIBLE = 0;
const int UNKNOWN = -1;
vector<DELTA_POS> directions = vector<DELTA_POS> ();
// Initialize the status of each node as UNKNOWN
vector<int> accessStatuses = vector<int> (dnet->nodes.size(), UNKNOWN);
unsigned int nodeIndex = 0;
// Get and Print basic info
int noAccDijkNodes = 0; // number of active Dijkstra nodes (ones with radius > 0)
int noAccDijkNodesAssigned = 0; // number of active Dijkstra nodes already assigned to pores.
for(unsigned int i = 0; i < dnet->nodes.size(); i++)
{
if(dnet->nodes.at(i).active == true) noAccDijkNodes++;
//debug line
//cout << "id,active " << i << " " << dnet->nodes.at(i).active << endl;
};
cout << "\nFinding channels and pockets in Dijkstra network of " << dnet->nodes.size() << " node(s). " << noAccDijkNodes << " are expected to compose pores." << endl;
// Iterate over all nodes
while(nodeIndex != accessStatuses.size()){
//Skip node if access status already determined
if(accessStatuses.at(nodeIndex) != UNKNOWN){
nodeIndex++;
continue;
}
// Start out with 0-dimensional pore w/o any basis vectors
int dim = 0;
int basis [3][3] = {{0,0,0},
{0,0,0},
{0,0,0}};
// Place starting node on stack with (0,0,0) displacement
int accessStatus = UNKNOWN;
DELTA_POS displacement = DELTA_POS(0,0,0);
map<int,DELTA_POS> visitedNodeDisplacement;
vector<pair<int,DELTA_POS> > stack;
stack.push_back(pair<int,DELTA_POS> (nodeIndex,displacement));
visitedNodeDisplacement.insert(pair<int,DELTA_POS>(nodeIndex, displacement));
while(stack.size() != 0){
// Remove top-most node
pair<int,DELTA_POS> nodeInfo = stack.back();
DIJKSTRA_NODE currentNode = dnet->nodes.at(nodeInfo.first);
stack.pop_back();
// Follow all edges leading to adjoining nodes
vector<CONN>::iterator connIter = currentNode.connections.begin();
while(connIter != currentNode.connections.end()){
int to = connIter->to;
DELTA_POS newDisplacement = nodeInfo.second + connIter->deltaPos;
int localAccessStatus = accessStatuses.at(to);
if(localAccessStatus == UNKNOWN){
map<int,DELTA_POS>::iterator visitedNode = visitedNodeDisplacement.find(to);
if(visitedNode != visitedNodeDisplacement.end()){
if(visitedNode->second.equals(newDisplacement)){
// Circling back to previous node
// Do nothing
}
else{
// Nodes are ACCESSIBLE b/c same node visited in different unit cell
accessStatus = ACCESSIBLE;
DELTA_POS direction = newDisplacement - visitedNode->second;
/******* Start of Michael's code ******/
if(dim == 0){
basis[0][0] = direction.x;
basis[1][0] = direction.y;
basis[2][0] = direction.z;
dim++;
}
else if (dim == 1){
double v;
if (basis[0][0] != 0) v = 1.0*direction.x/basis[0][0];
else if (basis[1][0] != 0) v = 1.0*direction.y/basis[1][0];
else if (basis[2][0] != 0) v = 1.0*direction.z/basis[2][0];
else {
cerr << "Error: Pore basis vector is zero vector. Exiting..." << "\n";
exit(1);
}
if(!(basis[0][0]*v == direction.x && basis[1][0]*v == direction.y &&
basis[2][0]*v == direction.z)){
basis[0][1] = direction.x;
basis[1][1] = direction.y;
basis[2][1] = direction.z;
dim++;
}
}
else if (dim == 2){
basis[0][2] = direction.x;
basis[1][2] = direction.y;
basis[2][2] = direction.z;
if(abs(calcDeterminant(basis)) < 1e-8){
basis[0][2] = basis[1][2] = basis[2][2] = 0;
}
else
dim++;
}
/****** End of Michael's code ******/
}
}
else{
// Node being visited for the first time
visitedNodeDisplacement.insert(pair<int,DELTA_POS>(to, newDisplacement));
stack.push_back(pair<int,DELTA_POS> (to, newDisplacement));
}
}
else{
// The status of a region should be determined
// with all of its neighbors. Otherwise, all possible nodes
// were not explored.
cerr << "Error: Illogical result when attempting to identify channels/pockets." << "\n";
cerr << "Please contact the source code provider with your program input. " << "\n";
cerr << "Exiting ..." << "\n";
exit(1);
}
connIter++;
}
}
// All connected nodes are inaccessible if their status is still UNKNOWN
if((stack.size() == 0) && (accessStatus == UNKNOWN))
accessStatus = INACCESSIBLE;
// Record the access status for each node in the cycle and create
// a list of the node ids for channel identification
map<int,DELTA_POS>::iterator resultIter = visitedNodeDisplacement.begin();
vector<int> listOfIDs = vector<int> ();
while(resultIter != visitedNodeDisplacement.end()){
int nodeID = resultIter->first;
if(accessStatuses.at(nodeID) != UNKNOWN){
// Each node should only have its accessibility determined once
cerr << "Error: Accessibility of node was determined more than once." << "\n";
cerr << "Please contact the source code provider with your program input. " << "\n";
cerr << "Exiting ..." << "\n";
exit(1);
}
else{
// Store node's status
accessStatuses.at(nodeID) = accessStatus;
listOfIDs.push_back(nodeID);
}
resultIter++;
}
nodeIndex++;
// Create CHANNEL from ACCESSIBLE nodes
if(accessStatus == ACCESSIBLE){
pores->push_back(PORE(listOfIDs, dnet, dim, basis));
// store pore statistics (number of nodes included in pore)
noAccDijkNodesAssigned += listOfIDs.size();
//output << dim << " ";
}else
{ // OTHERWISE create POCKET from INACCESSIBLE nodes (dim set to 0 indicates pocket)
// additionally pockets have to be built from active nodes (this is to ensure their
// size is larger than mix_radius)
if(dnet->nodes[listOfIDs[0]].active == true)
{
pores->push_back(PORE(listOfIDs, dnet, dim, basis));
// store pore statistics (number of nodes included in pore)
noAccDijkNodesAssigned += listOfIDs.size();
};
};
//more debug
bool flg;
for(unsigned int i=0;i<listOfIDs.size();i++)
{
if(i==0) flg = dnet->nodes[listOfIDs[0]].active;
else
{
if(flg !=dnet->nodes[listOfIDs[i]].active) cout << "pocket error" << endl;
};
};
//end of debug
}
// Store information in provided vector
infoStorage -> resize(accessStatuses.size());
for(unsigned int i = 0; i < accessStatuses.size(); i++){
infoStorage -> at(i) = (accessStatuses.at(i) == ACCESSIBLE);
}
// Print summary
int nchannel=0,npocket=0;
for(unsigned int i = 0; i<pores->size(); i++)
{
if(pores->at(i).dimensionality>0) nchannel++; else npocket++;
};
cout << "Analyzed and assigned " << nodeIndex << " nodes.";
cout << "\nIdentified " << nchannel << " channels and " << npocket << " pockets.\n";
cout << noAccDijkNodesAssigned << " nodes assigned to pores. " << endl;
}
/* Voronoi nodes within the provided VORONOI_NETWORK can be classified as either
* accessible or inaccessible based on a give probe diameter. This is done by constructing
* DIJKSTRA_NETWORK and calling the other findChannelsAndPockets function */
void PORE::findChannelsAndPockets(VORONOI_NETWORK *vornet, double minRadius,
vector<bool> *infoStorage, vector<PORE> *pores)
{
//Remove edges that don't allow the provided particle diameter to
//pass freely
//VORONOI_NETWORK newNetwork;
//pruneVoronoiNetwork(vornet, &newNetwork, minRadius);
VORONOI_NETWORK newNetwork = vornet->prune(minRadius);
//Build graph data structure
DIJKSTRA_NETWORK dnet;
DIJKSTRA_NETWORK::buildDijkstraNetwork(&newNetwork, &dnet);
findChannelsAndPockets(&dnet, infoStorage, pores);
}
/** Calculates center of mass and internal void radii (distance between the center of mass and its nearest atom)
* This function attempts pore reconstruction in cases where pores cross the cell boundaries.
*/
pair <XYZ, double> PORE::getCenterOfMass(){
/*
Pore variables
std::vector<DELTA_POS> unitCells; // List of displacements of unitcells in single PORE unit
std::vector< std::vector<int> > ucNodes; // List of nodes contained in each unit cell
XYZ v_a, v_b, v_c; // Unit cell vectors
*/
vector<XYZ> reconstructedNodeCoords;
XYZ centerOfMass (0,0,0);
double dist; // distance from the center of mass to the nearest node
XYZ tempPt;
for(int unsigned i=0; i<unitCells.size(); i++)
{
for(int unsigned j=0; j<ucNodes[i].size(); j++)
{
int nd = ucNodes[i].at(j); // current node
XYZ deltapos(double(unitCells[i].x), double(unitCells[i].y), double(unitCells[i].z));
XYZ node(nodes[nd].x, nodes[nd].y, nodes[nd].z);
node = node + v_a.scale(deltapos.x) + v_b.scale(deltapos.y) + v_c.scale(deltapos.z) ;
reconstructedNodeCoords.push_back(node);
centerOfMass = centerOfMass + node;
};
};
centerOfMass = centerOfMass.scale(1.0/double(nodes.size()));
for(int unsigned i=0; i<reconstructedNodeCoords.size(); i++)
{
if(i==0)
{
dist = centerOfMass.euclid_dist(reconstructedNodeCoords[i]);
}
else
{
if(centerOfMass.euclid_dist(reconstructedNodeCoords[i]) < dist) dist = centerOfMass.euclid_dist(reconstructedNodeCoords[i]);
};
};
if(dimensionality>0) cout << "Center of Mass calculation: PORE dimensionality>0; results may be buggy due to PORE reconstruction handling\n";
/* DEBUGGING: Printing out coordinates
for(int unsigned i=0; i<reconstructedNodeCoords.size(); i++)
{
cout << "C " << reconstructedNodeCoords[i].x << " " << reconstructedNodeCoords[i].y << " " << reconstructedNodeCoords[i].z << "\n";
};
*/
pair<XYZ, double> p(centerOfMass, dist);
return p;
} // ends getCenterOfMass() function
/** This function attempts pore reconstruction in cases where pores cross the cell boundaries.
* It returns a vector of vectors with pore XYZ coordinates
* */
vector< pair <int,XYZ> > PORE::getReconstructedPore(){
/*
* Pore variables
* std::vector<DELTA_POS> unitCells; // List of displacements of unitcells in single PORE unit
* std::vector< std::vector<int> > ucNodes; // List of nodes contained in each unit cell
* XYZ v_a, v_b, v_c; // Unit cell vectors
*
* */
vector< pair <int,XYZ> > reconstructedNodeCoords;
for(int unsigned i=0; i<unitCells.size(); i++)
{
for(int unsigned j=0; j<ucNodes[i].size(); j++)
{
int nd = ucNodes[i].at(j); // current node
XYZ deltapos(double(unitCells[i].x), double(unitCells[i].y), double(unitCells[i].z));
XYZ node(nodes[nd].x, nodes[nd].y, nodes[nd].z);
node = node + v_a.scale(deltapos.x) + v_b.scale(deltapos.y) + v_c.scale(deltapos.z) ;
pair <int,XYZ> p (nd,node);
reconstructedNodeCoords.push_back(p);
};
};
/* DEBUGGING: Check if reconstructured nodes become duplicate atoms
cout << "DEBUG: Pore/molecule size after reconstruction (XYZ coords) " << reconstructedNodeCoords.size() << endl;
*/
if(dimensionality>0) cout << "Calling PORE::getReconstructedPore for a pore with dim>0, it was not intended. DO NOT TRUST\n";
/* DEBUGGING: Printing out coordinates
* for(int unsigned i=0; i<reconstructedNodeCoords.size(); i++)
* {
* cout << "C " << reconstructedNodeCoords[i].x << " " << reconstructedNodeCoords[i].y << " " << reconstructedNodeCoords[i].z << "\n";
* };
* */
return reconstructedNodeCoords;
} // ends getReconstructredPore() function
/** This function attempts pore reconstruction in cases where pores cross the cell boundaries.
* In case of pores/molecules that cross boundy, multiple copies are saved each corresponding to one instance in periodic box.
* It returns a vector of vectors with pore XYZ coordinates and Ids
* */
vector< vector< pair <int,XYZ> > > PORE::getReconstructredPoresWithCrossBoundryCopies(){
/*
* Pore variables
* std::vector<DELTA_POS> unitCells; // List of displacements of unitcells in single PORE unit
* std::vector< std::vector<int> > ucNodes; // List of nodes contained in each unit cell
* XYZ v_a, v_b, v_c; // Unit cell vectors
*
* */
vector< pair <int,XYZ> > reconstructedNodeCoords; /* holds the reconstructure pore/molecule */
for(int unsigned i=0; i<unitCells.size(); i++)
{
for(int unsigned j=0; j<ucNodes[i].size(); j++)
{
int nd = ucNodes[i].at(j); // current node
XYZ deltapos(double(unitCells[i].x), double(unitCells[i].y), double(unitCells[i].z));
XYZ node(nodes[nd].x, nodes[nd].y, nodes[nd].z);
node = node + v_a.scale(deltapos.x) + v_b.scale(deltapos.y) + v_c.scale(deltapos.z) ;
pair <int,XYZ> p (nd,node);
reconstructedNodeCoords.push_back(p);
};
};
vector< vector< pair <int,XYZ> > > reconstructedCopies; // This will store copies of all reconstructure pores/molecules
if(dimensionality>0) cout << "Calling PORE::getReconstructredPoresWithCrossBoundryCopies() for a pore with dim>0, it was not intended. DO NOT TRUST\n";
for(int unsigned i=0; i<unitCells.size(); i++)
{
reconstructedCopies.push_back(reconstructedNodeCoords);
for(int unsigned j=0; j<reconstructedNodeCoords.size(); j++)
{
XYZ deltapos(double(unitCells[i].x), double(unitCells[i].y), double(unitCells[i].z));
reconstructedCopies[i].at(j).second = reconstructedCopies[i].at(j).second - v_a.scale(deltapos.x) - v_b.scale(deltapos.y) - v_c.scale(deltapos.z) ;
};
};
return reconstructedCopies;
} // ends getReconstructredPoresWithCrossBoundryCopies() function
/* Prints information about the pore to the provided output stream, including nuber of nodes, the largest included sphere,
positions and radii of all nodes */
void PORE::printPoreSummary(ostream &out, ATOM_NETWORK *atmNet){
vector <double> poresummary;
getSimplifiedPocketInfo(atmNet, &poresummary);
out << nodes.size() << " " << poresummary[0] << " " << poresummary[1] << " " << poresummary[2] << " " << poresummary[3] << " " << poresummary[4] << "\n";
// out << nodes.size() << " " << getIncludedSphereDiameter() << "\n";
for(unsigned int i = 0; i< nodes.size(); i++){
// nodes.at(i).print();
Point pt = atmNet->xyz_to_abc(nodes.at(i).x, nodes.at(i).y, nodes.at(i).z);
pt = atmNet->shiftABCInUC(pt);
out << pt[0] << " " << pt[1] << " " << pt[2];
out << " " << nodes.at(i).max_radius << "\n";
};
}
/* Prints information about the CHANNEL to the provided output stream, including
* the number of nodes, unitcells, and the nodes located in each unit cell. Additional
* node information is outputted if requested.*/
void CHANNEL::print(ostream &out, bool dispNodeInfo){
out << "Channel info:" << "\n";
out << " # Nodes: " << nodes.size() << "\n";
if(dispNodeInfo){
out << " Original Node IDs: ";
for(unsigned int i = 0; i < nodes.size(); i++){
out << reverseIDMappings.find(i)->second << " ";
}
out << "\n";
out << " New Node IDs: ";
for(unsigned int i = 0; i < nodes.size(); i++){
out << i << " ";
}
out << "\n";
out << " New Node info: " << "\n";
for(unsigned int i = 0; i< nodes.size(); i++){
nodes.at(i).print();
}
}
out << " # Unit cells:" << unitCells.size() << "\n";
for(unsigned int i = 0; i < unitCells.size(); i++){
DELTA_POS position = unitCells.at(i);
vector<int> ucNode = ucNodes.at(i);
out << " Unit cell #: " << i << "\n"
<< " Displacement: " << position.x << " " << position.y << " " << position.z << "\n"
<< " New Node ids: ";
for(unsigned int j = 0; j < ucNode.size(); j++){
out << ucNode.at(j) << " ";
}
out << "\n";
}
}
bool(*fn_pt)(DELTA_POS,DELTA_POS) = deltaPosLessThan;
/* Reset the visited positions and set the current position to the origin. */
ReconstructorComparator::ReconstructorComparator(){
positions = set<DELTA_POS, bool(*)(DELTA_POS,DELTA_POS)> (deltaPosLessThan);
currentPos = DELTA_POS(0,0,0);
}
/* Set the current position and store the old position. */
void ReconstructorComparator::setPosition(DELTA_POS p){
positions.insert(p);
currentPos = p;
}
bool ReconstructorComparator::compare(pair<int,DELTA_POS> p1, pair<int,DELTA_POS> p2) {
bool p1Current = p1.second.equals(currentPos);
bool p2Current = p2.second.equals(currentPos);
if(p1Current && p2Current)
return false;
else if (p1Current)
return false;
else if (p2Current)
return true;
else{
bool foundP1 = (positions.find(p1.second) != positions.end());
bool foundP2 = (positions.find(p2.second) != positions.end());
if(foundP1)
return false;
else
return foundP2;
}
}
ReconstructorComparator comparer; // Object used to reconstruct CHANNEL
bool compareNodes(pair<int,DELTA_POS> p1, pair<int,DELTA_POS> p2){
return comparer.compare(p1,p2);
}
/* Reconstructs the PORE by propagating paths until all nodes have been accessed.
* Stores each node in the unit cell in which it is encountered.
* Attempts to reduce the number of unit cells required for reconstruction by
* favoring nodes in already-accessed unit cells over nodes in new unit cells. */
void PORE::reconstruct(){
vector<bool> haveVisited = vector<bool>(nodes.size(),false);
vector<DELTA_POS> displacements = vector<DELTA_POS>(nodes.size());
unsigned int visitCount = 0;
//Search through the PORE, emphasizing nodes located in the current unit
//cell or in previously visited unit cells. Record the unit cell within which
//each node is encountered
comparer = ReconstructorComparator();
HEAP< pair<int,DELTA_POS> > stack (compareNodes);
stack.insert(pair<int,DELTA_POS> (0, DELTA_POS(0,0,0))); // Pick the first node as the starting point
DELTA_POS curPos = DELTA_POS(0,0,0);
// Continue reconstruction until all nodes have been visited
while(visitCount < nodes.size()){
if(stack.size() == 0){
cerr << "Error: Stack empties prior to pore reconstruction completion." << "\n"
<< "Please contact the source code provided with this message." << "\n"
<< "Exiting..." << "\n"
<< "Nnodes = " << nodes.size() << " visitCount= " << visitCount << "\n";
exit(1);
}
pair<int,DELTA_POS> best = stack.pop();
if(!haveVisited.at(best.first)){
visitCount++;
haveVisited.at(best.first) = true;
displacements.at(best.first) = best.second;
// If changed unit cell, need to restructure heap
if(!best.second.equals(curPos)){
comparer.setPosition(best.second);
stack.reHeapify();
curPos = best.second;
}
// Add all the nodes from the current node to the stack that
// have not yet been visited
DIJKSTRA_NODE curNode = nodes.at(best.first);
for(unsigned int j = 0; j < curNode.connections.size(); j++){
CONN curConn = curNode.connections.at(j);
if(!haveVisited.at(curConn.to)){
DELTA_POS newPos = curPos + curConn.deltaPos;
stack.insert(pair<int,DELTA_POS>(curConn.to,newPos));
}
}
}
}
map<DELTA_POS, vector<int> , bool(*)(DELTA_POS,DELTA_POS)> results (fn_pt);
//Store the list of nodes and unit cells in a usable format
for(unsigned int i = 0; i < nodes.size(); i++){
map<DELTA_POS, vector<int> >::iterator iter = results.find(displacements.at(i));
if(iter == results.end()){
vector<int> newList; newList.push_back(i);
results.insert(pair<DELTA_POS, vector<int> > (displacements.at(i),newList));
}
else
iter->second.push_back(i);
}
// Copy the unit cells visited and their corresponding node ids to intrinsic
// data structures
map<DELTA_POS, vector<int> >::iterator rIter = results.begin();
while(rIter != results.end()){
unitCells.push_back(rIter->first);
ucNodes.push_back(rIter->second);
rIter++;
}
}
/** Write the commands necessary to draw the CHANNEL in ZeoVis
* to the provided output stream. */
void CHANNEL::writeToVMD(int n, fstream &output){
if(!output.is_open()){
cerr << "Error: File stream needed to print channel information was not open." << "\n"
<< "Exiting ..." << "\n";
exit(1);
}
else{
output << "set channels(" << n << ") {" << "\n"
<< "{color $channelColors(" << n << ")}" << "\n";
// Draw the components located in each unit cell
for(unsigned int i = 0; i < unitCells.size(); i++){
vector<int> nodeIDs = ucNodes.at(i);
DELTA_POS disp = unitCells.at(i);
// Iterate over all nodes in the unit cell
for(unsigned int j = 0; j < nodeIDs.size(); j++){
DIJKSTRA_NODE curNode = nodes.at(nodeIDs.at(j));
// Find node coordinates
double xCoord = curNode.x + v_a.x*disp.x + v_b.x*disp.y + v_c.x*disp.z;
double yCoord = curNode.y + v_a.y*disp.x + v_b.y*disp.y + v_c.y*disp.z;
double zCoord = curNode.z + v_a.z*disp.x + v_b.z*disp.y + v_c.z*disp.z;
// Command used to draw node
output << "{sphere {" << xCoord << " " << yCoord << " " << zCoord
<< "} radius $nodeRadii(" << nodeIDs.at(j) <<") resolution $sphere_resolution}"
<< "\n";
// Iterate over all connections stemming from the current node
for(unsigned int k = 0; k < curNode.connections.size(); k++){
CONN curConn = curNode.connections.at(k);
DIJKSTRA_NODE otherNode = nodes.at(curConn.to);
int dx = disp.x + curConn.deltaPos.x;
int dy = disp.y + curConn.deltaPos.y;
int dz = disp.z + curConn.deltaPos.z;
// Find end of edge coordinates
double newX = otherNode.x + v_a.x*dx + v_b.x*dy + v_c.x*dz;
double newY = otherNode.y + v_a.y*dx + v_b.y*dy + v_c.y*dz;
double newZ = otherNode.z + v_a.z*dx + v_b.z*dy + v_c.z*dz;
// Command used to draw edge
output << "{line {" << xCoord << " " << yCoord << " " << zCoord << "} {"
<< newX << " " << newY << " " << newZ<< "}}" << "\n";
}
}
}
output << "}" << "\n";
}
}
/** Write the commands necessary to draw the CHANNEL in ZeoVis
* to the provided output stream. Includes a type because features and segments
* are drawn using the same command.*/
void CHANNEL::writeToVMD(string type, int n, fstream &output){
if(!output.is_open()){
cerr << "Error: File stream needed to print" << type << " information was not open." << "\n"
<< "Exiting ..." << "\n";
exit(1);
}
else{
output << "set " << type << "s(" << n << ") {" << "\n"
<< "{color $" << type << "Colors(" << n << ")}" << "\n";
// Draw the components located in each unit cell
for(unsigned int i = 0; i < unitCells.size(); i++){
vector<int> nodeIDs = ucNodes.at(i);
DELTA_POS disp = unitCells.at(i);
// Iterate over all nodes in the unit cell
for(unsigned int j = 0; j < nodeIDs.size(); j++){
DIJKSTRA_NODE curNode = nodes.at(nodeIDs.at(j));
// Find node coordinates
double xCoord = curNode.x + v_a.x*disp.x + v_b.x*disp.y + v_c.x*disp.z;
double yCoord = curNode.y + v_a.y*disp.x + v_b.y*disp.y + v_c.y*disp.z;
double zCoord = curNode.z + v_a.z*disp.x + v_b.z*disp.y + v_c.z*disp.z;
// Command used to draw node
output << "{sphere {" << xCoord << " " << yCoord << " " << zCoord
<< "} radius $nodeRadii(" << nodeIDs.at(j) <<") resolution $sphere_resolution}" << "\n";
// Iterate over all connections stemming from the current node
for(unsigned int k = 0; k < curNode.connections.size(); k++){
CONN curConn = curNode.connections.at(k);
DIJKSTRA_NODE otherNode = nodes.at(curConn.to);
int dx = disp.x + curConn.deltaPos.x;
int dy = disp.y + curConn.deltaPos.y;
int dz = disp.z + curConn.deltaPos.z;
// Find end of edge coordinates
double newX = otherNode.x + v_a.x*dx + v_b.x*dy + v_c.x*dz;
double newY = otherNode.y + v_a.y*dx + v_b.y*dy + v_c.y*dz;
double newZ = otherNode.z + v_a.z*dx + v_b.z*dy + v_c.z*dz;
// Command used to draw edge
output << "{line {" << xCoord << " " << yCoord << " " << zCoord << "} {"
<< newX << " " << newY << " " << newZ<< "}}" << "\n";
}
}
}
output << "}" << "\n";
}
}
/* Create a channel from a pore */
CHANNEL::CHANNEL(PORE *p){
nodes = p->nodes;
connections = p->connections;
unitCells = p->unitCells;
ucNodes = p->ucNodes;
dimensionality = p->dimensionality;
basis[0][0]=p->basis[0][0];basis[0][1]=p->basis[0][1];basis[0][2]=p->basis[0][2];
basis[1][0]=p->basis[1][0];basis[1][1]=p->basis[1][1];basis[1][2]=p->basis[1][2];
basis[2][0]=p->basis[2][0];basis[2][1]=p->basis[2][1];basis[2][2]=p->basis[2][2];
}
/** Nodes within the provided DIJKSTRA_NETWORK can be classified as either
* accessible or inaccessible, where sets of accessible nodes constitute a
* CHANNEL. As a result, this function identifies the CHANNELs that exist for
* the provided particle diameter and stores them using the provided pointer
* to a vector of channels. In addition, the pointer to the vector of bools is
* used to a store bool for each VORONOI_NODE, where infoStorage[i] is true
* iff node #i is accessible. */
/* WARNING: this function assumes that at some point before VORONOI_NETWORK was pruned
* with prune() function. This is to set "active" flag on nodes larger than probe radius */
void CHANNEL::findChannels(DIJKSTRA_NETWORK *dnet, vector<bool> *infoStorage,
vector<CHANNEL> *channels)
{
vector <PORE> pores;
findChannelsAndPockets(dnet, infoStorage, &pores);
for(unsigned int i = 0; i<pores.size(); i++)
{
if(pores[i].dimensionality>0) channels->push_back(CHANNEL(&pores[i]));
};
pores.clear();
}
/* below is an old version before PORE class was introducedd */
/*
void CHANNEL::findChannels(DIJKSTRA_NETWORK *dnet, vector<bool> *infoStorage,
vector<CHANNEL> *channels)
{
//Define three access types
const int ACCESSIBLE = 1;
const int INACCESSIBLE = 0;
const int UNKNOWN = -1;
vector<DELTA_POS> directions = vector<DELTA_POS> ();
// Initialize the status of each node as UNKNOWN
vector<int> accessStatuses = vector<int> (dnet->nodes.size(), UNKNOWN);
unsigned int nodeIndex = 0;
// Iterate over all nodes
while(nodeIndex != accessStatuses.size()){
//Skip node if access status already determined
if(accessStatuses.at(nodeIndex) != UNKNOWN){
nodeIndex++;
continue;
}
// Start out with 0-dimensional channel w/o any basis vectors
int dim = 0;
int basis [3][3] = {{0,0,0},
{0,0,0},
{0,0,0}};
// Place starting node on stack with (0,0,0) displacement
int accessStatus = UNKNOWN;
DELTA_POS displacement = DELTA_POS(0,0,0);
map<int,DELTA_POS> visitedNodeDisplacement;
vector<pair<int,DELTA_POS> > stack;
stack.push_back(pair<int,DELTA_POS> (nodeIndex,displacement));
visitedNodeDisplacement.insert(pair<int,DELTA_POS>(nodeIndex, displacement));
while(stack.size() != 0){
// Remove top-most node
pair<int,DELTA_POS> nodeInfo = stack.back();
DIJKSTRA_NODE currentNode = dnet->nodes.at(nodeInfo.first);
stack.pop_back();
// Follow all edges leading to adjoining nodes
vector<CONN>::iterator connIter = currentNode.connections.begin();
while(connIter != currentNode.connections.end()){
int to = connIter->to;
DELTA_POS newDisplacement = nodeInfo.second + connIter->deltaPos;
int localAccessStatus = accessStatuses.at(to);
if(localAccessStatus == UNKNOWN){
map<int,DELTA_POS>::iterator visitedNode = visitedNodeDisplacement.find(to);
if(visitedNode != visitedNodeDisplacement.end()){
if(visitedNode->second.equals(newDisplacement)){
// Circling back to previous node
// Do nothing
}
else{
// Nodes are ACCESSIBLE b/c same node visited in different unit cell
accessStatus = ACCESSIBLE;
DELTA_POS direction = newDisplacement - visitedNode->second;
// ******* Start of Michael's code ******
if(dim == 0){
basis[0][0] = direction.x;
basis[1][0] = direction.y;
basis[2][0] = direction.z;
dim++;
}
else if (dim == 1){
double v;
if (basis[0][0] != 0) v = 1.0*direction.x/basis[0][0];
else if (basis[1][0] != 0) v = 1.0*direction.y/basis[1][0];
else if (basis[2][0] != 0) v = 1.0*direction.z/basis[2][0];
else {
cerr << "Error: Channel basis vector is zero vector. Exiting..." << "\n";
exit(1);
}
if(!(basis[0][0]*v == direction.x && basis[1][0]*v == direction.y &&
basis[2][0]*v == direction.z)){
basis[0][1] = direction.x;
basis[1][1] = direction.y;
basis[2][1] = direction.z;
dim++;
}
}
else if (dim == 2){
basis[0][2] = direction.x;
basis[1][2] = direction.y;
basis[2][2] = direction.z;
if(abs(calcDeterminant(basis)) < 1e-8){
basis[0][2] = basis[1][2] = basis[2][2] = 0;
}
else
dim++;
}
// ****** End of Michael's code ******
}
}
else{
// Node being visited for the first time
visitedNodeDisplacement.insert(pair<int,DELTA_POS>(to, newDisplacement));
stack.push_back(pair<int,DELTA_POS> (to, newDisplacement));
}
}
else{
// The status of a region should be determined
// with all of its neighbors. Otherwise, all possible nodes
// were not explored.
cerr << "Error: Illogical result when attempting to identify channels." << "\n";
cerr << "Please contact the source code provider with your program input. " << "\n";
cerr << "Exiting ..." << "\n";
exit(1);
}
connIter++;
}
}
// All connected nodes are inaccessible if their status is still UNKNOWN
if((stack.size() == 0) && (accessStatus == UNKNOWN))
accessStatus = INACCESSIBLE;
// Record the access status for each node in the cycle and create
// a list of the node ids for channel identification
map<int,DELTA_POS>::iterator resultIter = visitedNodeDisplacement.begin();
vector<int> listOfIDs = vector<int> ();
while(resultIter != visitedNodeDisplacement.end()){
int nodeID = resultIter->first;
if(accessStatuses.at(nodeID) != UNKNOWN){
// Each node should only have its accessibility determined once
cerr << "Error: Accessibility of node was determined more than once." << "\n";
cerr << "Please contact the source code provider with your program input. " << "\n";
cerr << "Exiting ..." << "\n";
exit(1);
}
else{
// Store node's status
accessStatuses.at(nodeID) = accessStatus;
listOfIDs.push_back(nodeID);
}
resultIter++;
}
nodeIndex++;
// Create CHANNEL from ACCESSIBLE nodes
if(accessStatus == ACCESSIBLE){
channels->push_back(CHANNEL(listOfIDs, dnet, dim, basis));
//output << dim << " ";
}
}
// Store information in provided vector
infoStorage -> resize(accessStatuses.size());
for(unsigned int i = 0; i < accessStatuses.size(); i++){
infoStorage -> at(i) = (accessStatuses.at(i) == ACCESSIBLE);
}
}
*/
/* Voronoi nodes within the provided VORONOI_NETWORK can be classified as either
* accessible or inaccessible, where sets of accessible nodes constitute a
* CHANNEL. As a result, this function identifies the CHANNELs that exist for
* the provided particle diameter and stores them using the provided pointer
* to a vector of channels. In addition, the pointer to the vector of bools is
* used to a store bool for each VORONOI_NODE, where infoStorage[i] is true
* iff node #i is accessible. */