-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnetworkstorage.cc
3166 lines (2913 loc) · 156 KB
/
networkstorage.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
/* Stores information about the basic data types that constitute a unit cell,
* such as atoms (ATOM) and networks of atoms (ATOM_NETWORK). Also stores
* information about the underlying Voronoi network constituents such as nodes
* (VOR_NODE), edges (VOR_EDGE), and the network itself (VORONOI_NETWORK).
*/
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#include <float.h>
#include "networkstorage.h"
#include "networkinfo.h"
#include "geometry.h"
#include "zeo_consts.h"
#include "rmsd.h"
#include "symmetry.h"
#include "string_additions.h"
#include "ray.h"
using namespace std;
ATOM_NETWORK::ATOM_NETWORK() {highAccuracyFlag = false; valid = false; allowAdjustCoordsAndCellFlag = false;} //the flag 'valid', from the framework builder, describes whether the unit cell makes sense - it has to be false initially, and becomes true if the matrix could be inverted; allowAdjustCoordsAndCellFlag is for the temporary Voronoi volume error fix
ATOM::ATOM() {
type = "";
label = "";
radius = 0;
charge = 0; //because this is not always provided, a default value of zero is set
keep = true;
}
ATOM::ATOM(XYZ xyz, string s, double r) {
x = xyz.x;
y = xyz.y;
z = xyz.z;
type = s;
label = s;
radius = r;
charge = 0;
keep = true;
}
ATOM::ATOM(XYZ xyz, string s, string l, double r) {
x = xyz.x;
y = xyz.y;
z = xyz.z;
type = s;
label = l;
radius = r;
charge = 0;
keep = true;
}
/** Print the information about this atom to the provided output stream. */
void ATOM::print(ostream &out){
out << " type:" << type << " x:" << x << " y:" << y << " z:" << z
<< " a:" << a_coord << " b:" << b_coord << " c:" << c_coord
<< " radius:" << radius << "\n";
}
XYZ ATOM::xyz() {
XYZ xyz(x,y,z);
return xyz;
}
void ATOM::set_xyz(XYZ new_xyz) {
x = new_xyz.x;
y = new_xyz.y;
z = new_xyz.z;
}
/* updates the fractional coords of all atoms in the cell (for example, if you modify the unit cell parameters and want to preserve the xyz coordinates of the atoms) */
void ATOM_NETWORK::update_atom_fractional_coords() {
for(int i=0; i<numAtoms; i++) {
XYZ xyz(atoms.at(i).x, atoms.at(i).y, atoms.at(i).z);
XYZ abc = trans_to_origuc(xyz_to_abc_returning_XYZ(xyz));
atoms.at(i).a_coord = abc.x;
atoms.at(i).b_coord = abc.y;
atoms.at(i).c_coord = abc.z;
}
}
/* randomly changes the atom coordinates and unit cell definition by a small degree */
void ATOM_NETWORK::randomlyAdjustCoordsAndCell() {
double shiftMagnitude = TOLERANCE;
printf("NOTICE: attempting random vector shift of all atom coordinates by %e (and unit cell parameters by up to this amount) to overcome Voronoi volume check failure (this option can be disabled by not using the -allowAdjustCoordsAndCell flag)\n", shiftMagnitude);
printf("NOTICE: original cell dimensions and angles: %e %e %e; %e %e %e\n", a, b, c, alpha, beta, gamma);
make(
(((rand()*1.0/RAND_MAX)-0.5)*2.0*shiftMagnitude)+a,
(((rand()*1.0/RAND_MAX)-0.5)*2.0*shiftMagnitude)+b,
(((rand()*1.0/RAND_MAX)-0.5)*2.0*shiftMagnitude)+c,
(((rand()*1.0/RAND_MAX)-0.5)*2.0*shiftMagnitude)+alpha,
(((rand()*1.0/RAND_MAX)-0.5)*2.0*shiftMagnitude)+beta,
(((rand()*1.0/RAND_MAX)-0.5)*2.0*shiftMagnitude)+gamma
);
printf("NOTICE: randomly adjusted cell dimensions and angles: %e %e %e; %e %e %e\n", a, b, c, alpha, beta, gamma);
for(int at=0; at<numAtoms; at++) {
//shift each Cartesian coord in some direction, by distance TOLERANCE
Point smallShift = genRandomVec().scale(shiftMagnitude);
Point resultOfShiftInABCNoPBC = xyz_to_abc(Point(
atoms.at(at).x+smallShift[0],
atoms.at(at).y+smallShift[1],
atoms.at(at).z+smallShift[2]
));
// Point resultOfShiftInABC = Point(trans_to_origuc(resultOfShiftInABCNoPBC[0]),trans_to_origuc(resultOfShiftInABCNoPBC[1]),trans_to_origuc(resultOfShiftInABCNoPBC[2]));
Point resultOfShiftInABC = resultOfShiftInABCNoPBC;
Point resultOfShiftInXYZ = abc_to_xyz(resultOfShiftInABC);
//now write the new coords to the atmnet
//printf("DEBUG: atom %d was at %e %e %e, now at %e %e %e\n", at, atoms.at(at).x, atoms.at(at).y, atoms.at(at).z, resultOfShiftInXYZ[0], resultOfShiftInXYZ[1], resultOfShiftInXYZ[2]);
atoms.at(at).x = resultOfShiftInXYZ[0];
atoms.at(at).y = resultOfShiftInXYZ[1];
atoms.at(at).z = resultOfShiftInXYZ[2];
atoms.at(at).a_coord = resultOfShiftInABC[0];
atoms.at(at).b_coord = resultOfShiftInABC[1];
atoms.at(at).c_coord = resultOfShiftInABC[2];
}
}
/* ATOM_NETWORK pseudo-constructor - assigns values and calls initialization method */
void ATOM_NETWORK::make(double a0, double b0, double c0, double alpha0, double beta0, double gamma0){
a = a0;
b = b0;
c = c0;
alpha = alpha0;
beta = beta0;
gamma = gamma0;
initialize();
}
/* an alternative way of making a cell - if we know the unit cell vectors */
void ATOM_NETWORK::make(XYZ va, XYZ vb, XYZ vc) {
v_a = va;
v_b = vb;
v_c = vc;
a = v_a.magnitude();
b = v_b.magnitude();
c = v_c.magnitude();
alpha = 360.0*v_b.angle_between(v_c)/(2.0*PI);
beta = 360.0*v_a.angle_between(v_c)/(2.0*PI);
gamma = 360.0*v_a.angle_between(v_b)/(2.0*PI);
initMatrices();
distanceCalculator = MIN_PER_DISTANCE(v_a.x, v_b.x, v_b.y, v_c.x, v_c.y, v_c.z);
}
/** Print the information about this network of atoms to the
* provided output stream, including the information about each
* atom in the network. */
void ATOM_NETWORK::print(ostream &out){
out << "Name: " << name << "\n"
<< "A: " << a <<" B: " << b << " C: " << c << "\n"
<< "Alpha: " << alpha <<" Beta: " << beta << " Gamma: " << gamma
<< "\n";
out << "v_a: "; v_a.print();
out << "v_b: "; v_b.print();
out << "v_c: "; v_c.print();
out << "Number of atoms: " << numAtoms << "\n";
for(int i = 0; i<numAtoms; i++)
atoms.at(i).print();
}
/** Copy the data contained in this ATOM_NETWORK to a new network using
the provided pointer.
*/
void ATOM_NETWORK::copy(ATOM_NETWORK *newNet){
newNet->a = a; newNet->b = b; newNet->c = c;
newNet->alpha = alpha; newNet->beta = beta; newNet->gamma = gamma;
newNet->v_a = v_a; newNet->v_b = v_b; newNet->v_c = v_c;
newNet->numAtoms = numAtoms; newNet->name = name;
newNet->atoms.clear(); newNet->atoms = atoms;
newNet->IDmapping.clear(); newNet->IDmapping = IDmapping;
newNet->vertices.clear(); newNet->vertices = vertices;
newNet->orphan_edge_starts.clear(); newNet->orphan_edge_starts = orphan_edge_starts;
newNet->orphan_edge_ends.clear(); newNet->orphan_edge_ends = orphan_edge_ends;
newNet->vertex_basic_indices.clear(); newNet->vertex_basic_indices = vertex_basic_indices;
newNet->vertex_symmetry_operators.clear(); newNet->vertex_symmetry_operators = vertex_symmetry_operators;
newNet->sym_ID = sym_ID;
newNet->sym_name = sym_name;
newNet->valid = valid;
newNet->initialize();
}
/** Calculate the unit cell vectors based on the provided values
of its side lengths and angles. v_a corresponds to the cartesian
x-axis.
*/
void ATOM_NETWORK::initialize(){
double tempd, talpha, tbeta, tgamma;
talpha = 2*PI/360.0*alpha;
tbeta = 2*PI/360.0*beta;
tgamma = 2*PI/360.0*gamma;
double cosb = cos(tbeta);
double cosg = cos(tgamma);
double sing = sin(tgamma);
tempd=(cos(talpha)-cosg*cosb)/sing;
v_a.x=a;
v_a.y=0.0;
v_a.z=0.0;
v_b.x=b*cosg;
if(fabs(v_b.x) < TOLERANCE)
v_b.x=0;
v_b.y=b*sing;
v_b.z=0.0;
v_c.x=c*cosb;
if(fabs(v_c.x) < TOLERANCE)
v_c.x = 0;
v_c.y=c*tempd;
if(fabs(v_c.y) < TOLERANCE)
v_c.y = 0;
v_c.z=c*sqrt(1.0-(cosb*cosb)-(tempd*tempd));
initMatrices();
distanceCalculator = MIN_PER_DISTANCE(v_a.x, v_b.x, v_b.y, v_c.x, v_c.y, v_c.z);
}
/* Store the initialized unit cell vectors in matrix form. */
void ATOM_NETWORK::initMatrices(){
ucVectors[0][0] = v_a.x; ucVectors[1][0] = v_a.y; ucVectors[2][0] = v_a.z;
ucVectors[0][1] = v_b.x; ucVectors[1][1] = v_b.y; ucVectors[2][1] = v_b.z;
ucVectors[0][2] = v_c.x; ucVectors[1][2] = v_c.y; ucVectors[2][2] = v_c.z;
valid = tryInvertMatrix(ucVectors,invUCVectors);
}
MIN_PER_DISTANCE ATOM_NETWORK::getDistCalc() const {
return distanceCalculator;
}
/** Determine the smallest supercell dimensions such that a sphere of a given
* diameter does not overlap with itself across the periodic boundary */
TRIPLET ATOM_NETWORK::getSmallestSupercell(double diam) {
//1) set lower bound based on lengths of cell axes
int na = 1+ (diam/a);
int nb = 1+ (diam/b);
int nc = 1+ (diam/c);
int fewest_cells = -1; //no supercell yet successful
TRIPLET smallest_supercell(-1,-1,-1);
//2) search all possible supercell sizes to find the smallest one satisfying
// the minimum image convention for this radius
TRIPLET lb(na, nb, nc);
vector<TRIPLET> supercells;
supercells.push_back(lb);
while(supercells.size()>0) {
//3) take the oldest candidate on the vector
TRIPLET s = supercells.at(0); //oldest supercell candidate, s
for(int i=0; i<supercells.size()-1; i++)
supercells.at(i) = supercells.at(i+1); //shift vector up
supercells.pop_back(); //delete last, which is now duplicated at last-1
int num_cells = s.x*s.y*s.z;
//4) is s a potential new smallest supercell?
if(num_cells<fewest_cells || fewest_cells<0) {
//5) does s satisfy the minimum image convention?
int status = check_sphere_overlap(s.x, s.y, s.z, diam, this); //For time being
if(status==-1) {
printf("WARNING: bad unit cell angles!\n");
return smallest_supercell;
} else if(status==1) { //acceptable!
fewest_cells = num_cells;
smallest_supercell = s;
//printf("smallest satisfactory supercell so far: (%d %d %d) =
//%d cells\n", s.a, s.b, s.c, num_cells);
} else { //unacceptable - try larger supercells in each direction
TRIPLET s2(s.x+1, s.y, s.z);
TRIPLET s3(s.x, s.y+1, s.z);
TRIPLET s4(s.x, s.y, s.z+1);
supercells.push_back(s2);
supercells.push_back(s3);
supercells.push_back(s4);
}
}
}
return smallest_supercell;
}
// abc_to_xyz and xyz_to_abc functions which need ATOM_NETWORK are moved from geometry
/** Convert coordinates relative to the unit cell (a,b,c) to absolute cartesian
coordinates (x,y,z). */
const Point ATOM_NETWORK::abc_to_xyz(double a, double b, double c) const {
//Point xyzCoords;
/*
xyzCoords.x = a*cell->v_a.x+b*cell->v_b.x+c*cell->v_c.x;
xyzCoords.y = a*cell->v_a.y+b*cell->v_b.y+c*cell->v_c.y;
xyzCoords.z = a*cell->v_a.z+b*cell->v_b.z+c*cell->v_c.z;
if(fabs(xyzCoords.x) < 0.00001) xyzCoords.x = 0;
if(fabs(xyzCoords.y) < 0.00001) xyzCoords.y = 0;
if(fabs(xyzCoords.z) < 0.00001) xyzCoords.z = 0;
*/
// Use only non-zero elements in computation
double xt = a*v_a.x+b*v_b.x+c*v_c.x;
double yt = b*v_b.y+c*v_c.y;
double zt = c*v_c.z;
return Point(xt, yt, zt);
}
const XYZ ATOM_NETWORK::abc_to_xyz_returning_XYZ(double a, double b, double c) const {
Point p = abc_to_xyz(a, b, c);
return XYZ(p[0], p[1], p[2]);
}
/** Convert coordinates relative to the unit cell (a,b,c) to absolute cartesian
coordinates (x,y,z). */
const Point ATOM_NETWORK::abc_to_xyz(Point abcPt) const {
return abc_to_xyz(abcPt[0], abcPt[1], abcPt[2]);
}
const Point ATOM_NETWORK::abc_to_xyz (const XYZ& temp) const {
return abc_to_xyz(temp.x,temp.y,temp.z);
}
const XYZ ATOM_NETWORK::abc_to_xyz_returning_XYZ(const XYZ& temp) const {
return abc_to_xyz_returning_XYZ(temp.x,temp.y,temp.z);
}
/** Convert coordinates relative to the cartesian axes to those
relative to the unit cell vectors. Returns an Point instance of the
form (a_coord, b_coord, c_coord). */
const Point ATOM_NETWORK::xyz_to_abc(double xi, double yi, double zi) const{
//Point abcCoords;
/*
abcCoords.x = x*cell->invUCVectors[0][0]+y*cell->invUCVectors[0][1]+z*cell->invUCVectors[0][2];
abcCoords.y = x*cell->invUCVectors[1][0]+y*cell->invUCVectors[1][1]+z*cell->invUCVectors[1][2];
abcCoords.z = x*cell->invUCVectors[2][0]+y*cell->invUCVectors[2][1]+z*cell->invUCVectors[2][2];
if(fabs(abcCoords.x) < 0.0001) abcCoords.x = 0;
if(fabs(abcCoords.y) < 0.0001) abcCoords.y = 0;
if(fabs(abcCoords.z) < 0.0001) abcCoords.z = 0;
*/
// Use only non-zero elements in computation
double xt = xi*invUCVectors[0][0]+yi*invUCVectors[0][1]+zi*invUCVectors[0][2];
double yt = yi*invUCVectors[1][1]+zi*invUCVectors[1][2];
double zt = zi*invUCVectors[2][2];
return Point(xt, yt, zt);
}
const XYZ ATOM_NETWORK::xyz_to_abc_returning_XYZ(double a, double b, double c) const {
Point p = xyz_to_abc(a, b, c);
return XYZ(p[0], p[1], p[2]);
}
/* calculates the unit edge length in a net */
double get_unit_edge_length(ATOM_NETWORK *c) {
int num_v = c->vertices.size();
double unit_edge_length = -1;
for(int i=0; i<num_v; i++) {
VERTEX v = c->vertices.at(i);
int num_e = v.edges.size();
XYZ v_xyz = c->abc_to_xyz_returning_XYZ(v.abc);
for(int j=0; j<num_e; j++) {
XYZ e_xyz = c->abc_to_xyz_returning_XYZ(v.edges.at(j));
double edge_length = (e_xyz-v_xyz).magnitude();
if(unit_edge_length<0) unit_edge_length = edge_length;
else if(fabs(unit_edge_length-edge_length)>DISTANCE_TOLERANCE) {
printf("ERROR: found a basic edge length of %.3f which is sufficiently different to the previous length of %.3f; at the moment, nets with more than one edge length are not handled\n", edge_length, unit_edge_length);
exit(EXIT_FAILURE);
}
}
}
return unit_edge_length;
}
/** Convert coordinates relative to the cartesian axes to those
relative to the unit cell vectors. Returns an Point instance of the
form (a_coord, b_coord, c_coord). */
const Point ATOM_NETWORK::xyz_to_abc (Point xyzPt) const {
return xyz_to_abc(xyzPt[0], xyzPt[1], xyzPt[2]);
}
const Point ATOM_NETWORK::xyz_to_abc(const XYZ& temp) const {
return xyz_to_abc(temp.x,temp.y,temp.z);
}
const XYZ ATOM_NETWORK::xyz_to_abc_returning_XYZ(const XYZ& temp) const {
return xyz_to_abc_returning_XYZ(temp.x,temp.y,temp.z);
}
/** Calculates the minimum distance between the two points whose coordinates are relative to the unit cell vectors. */
double ATOM_NETWORK::calcDistanceABC(double a1, double b1, double c1, double a2, double b2, double c2) const{
return getDistCalc().minimum_periodic_distance(a1, b1, c1, a2, b2, c2);
}
double ATOM_NETWORK::calcDistanceABC(XYZ a, XYZ b) {
return getDistCalc().minimum_periodic_distance(a.x, a.y, a.z, b.x, b.y, b.z);
}
/** Calculates the minimum distance between the point (x,y,z) and the other point whose coordinates
* are relative to the unit cell vectors. */
double ATOM_NETWORK::calcDistanceXYZABC(double x1, double y1, double z1, double a2, double b2, double c2) const {
Point abcCoord = xyz_to_abc(x1, y1, z1);
return calcDistanceABC(abcCoord[0], abcCoord[1], abcCoord[2], a2, b2, c2);
}
/** Calculates the minimum distance between the point (x,y,z) and the provided atom. */
double ATOM_NETWORK::calcDistance(double x, double y, double z, ATOM *atm) const {
return calcDistanceXYZABC(x, y, z, atm->a_coord, atm->b_coord, atm->c_coord);
}
/** Calculates the minimum distance between the two provided atoms. */
double ATOM_NETWORK::calcDistance(const ATOM& atm1, const ATOM& atm2) const {
return calcDistanceABC(atm1.a_coord, atm1.b_coord, atm1.c_coord, atm2.a_coord, atm2.b_coord, atm2.c_coord);
}
/** Calculates the minimum distance between the points (x1,y1,z1) and (x2,y2,z2). */
double ATOM_NETWORK::calcDistanceXYZ(double x1, double y1, double z1, double x2, double y2, double z2) const {
Point abcCoord = xyz_to_abc(x1, y1, z1);
//cout << abcCoord.vals[0] << abcCoord.vals[1] << abcCoord.vals[2] << endl;
//cout << x2 << y2 << z2 << endl;
//cout << calcDistanceXYZABC(x2, y2, z2, abcCoord[0], abcCoord[1], abcCoord[2]);
return calcDistanceXYZABC(x2, y2, z2, abcCoord[0], abcCoord[1], abcCoord[2]);
}
/** Rich edit: for static point (x1,y1,z1), returns the closest periodic image of point (x2,y2,z2).*/
const XYZ ATOM_NETWORK::getClosestPointInABC(double x1, double y1, double z1, double x2, double y2, double z2){
Point abcCoordStatic = xyz_to_abc(x1, y1, z1);
Point abcCoordMobile = xyz_to_abc(x2, y2, z2);
XYZ answer;
getDistCalc().closest_periodic_image(abcCoordStatic[0], abcCoordStatic[1], abcCoordStatic[2],
abcCoordMobile[0], abcCoordMobile[1], abcCoordMobile[2],
answer.x, answer.y, answer.z);
return answer;
}
/** Modify the provided (x,y,z) Point so that its coordinates reflect unit cell translations
* by the provided amounts along each unit cell axis. */
void ATOM_NETWORK::translatePoint(Point *origPoint, double da, double db, double dc){
(*origPoint)[0] = (*origPoint)[0] + da*v_a.x + db*v_b.x + dc*v_c.x;
(*origPoint)[1] = (*origPoint)[1] + da*v_a.y + db*v_b.y + dc*v_c.y;
(*origPoint)[2] = (*origPoint)[2] + da*v_a.z + db*v_b.z + dc*v_c.z;
}
/** Shifts the provided Point whose coordinates are relative to the unit cell vectors
* such that it lies within the unitcell. */
const Point ATOM_NETWORK::shiftABCInUC(Point abcCoords) {
return Point(trans_to_origuc(abcCoords[0]), trans_to_origuc(abcCoords[1]), trans_to_origuc(abcCoords[2]));
}
/** Shifts the provided Point whose coordinates are relative to the x,y,z vectors
* such that it lies within the unitcell */
const Point ATOM_NETWORK::shiftXYZInUC(Point xyzCoords) {
Point abcCoords = shiftABCInUC(xyz_to_abc(xyzCoords));
return abc_to_xyz(abcCoords[0], abcCoords[1], abcCoords[2]);
}
/** Shift the coordinates of the provided Point using the unit cell vectors until
* the Euclidean distance between the Point and (x,y,z) is minimal. Returns the resulting point.
*/
const Point ATOM_NETWORK::minimizePointDistance(Point origPoint, double dx, double dy, double dz){
Point abc_one = xyz_to_abc(origPoint);
Point abc_two = xyz_to_abc(dx, dy, dz);
double minDa = DBL_MAX, minDb = DBL_MAX, minDc = DBL_MAX, best_a = DBL_MAX, best_b = DBL_MAX, best_c = DBL_MAX;
getDistCalc().closest_periodic_image(abc_two[0], abc_two[1], abc_two[2], abc_one[0], abc_one[1], abc_one[2],
minDa, minDb, minDc, best_a, best_b, best_c);
return abc_to_xyz(best_a, best_b, best_c);
}
/* Identifies tetrahedra of the given atom type, calculates their tetrahedrality and returns as a vector of doubles */
vector<double> ATOM_NETWORK::find_tetrahedra(string element) {
vector<double> tetras;
double d_ij = -1, d_ik = -1, d_il = -1, d_jk = -1, d_jl = -1, d_kl = -1; //stores distances between each pair of atoms in a potential tetrahedral arrangement
double maxDist = 5, minDist = 0.1; //four atoms are considered to be in a tetrahedral arrangement if they are each within this distance range of each other - if this is true, we then calculate the index of tetrahedral distortion
for(int i=0; i<numAtoms; i++) {
if(atoms.at(i).type.compare(element)==0) {
for(int j=i+1; j<numAtoms; j++) {
if(atoms.at(j).type.compare(element)==0) {
d_ij = getDistCalc().minimum_periodic_distance(atoms.at(i).a_coord, atoms.at(i).b_coord, atoms.at(i).c_coord, atoms.at(j).a_coord, atoms.at(j).b_coord, atoms.at(j).c_coord);
if(d_ij>minDist && d_ij<maxDist) {
for(int k=j+1; k<numAtoms; k++) {
if(atoms.at(k).type.compare(element)==0) {
d_ik = getDistCalc().minimum_periodic_distance(atoms.at(i).a_coord, atoms.at(i).b_coord, atoms.at(i).c_coord, atoms.at(k).a_coord, atoms.at(k).b_coord, atoms.at(k).c_coord);
if(d_ik>minDist && d_ik<maxDist) {
d_jk = getDistCalc().minimum_periodic_distance(atoms.at(j).a_coord, atoms.at(j).b_coord, atoms.at(j).c_coord, atoms.at(k).a_coord, atoms.at(k).b_coord, atoms.at(k).c_coord);
if(d_jk>minDist && d_jk<maxDist) {
for(int l=k+1; l<numAtoms; l++) {
if(atoms.at(l).type.compare(element)==0) {
d_il = getDistCalc().minimum_periodic_distance(atoms.at(i).a_coord, atoms.at(i).b_coord, atoms.at(i).c_coord, atoms.at(l).a_coord, atoms.at(l).b_coord, atoms.at(l).c_coord);
if(d_il>minDist && d_il<maxDist) {
d_jl = getDistCalc().minimum_periodic_distance(atoms.at(j).a_coord, atoms.at(j).b_coord, atoms.at(j).c_coord, atoms.at(l).a_coord, atoms.at(l).b_coord, atoms.at(l).c_coord);
if(d_jl>minDist && d_jl<maxDist) {
d_kl = getDistCalc().minimum_periodic_distance(atoms.at(k).a_coord, atoms.at(k).b_coord, atoms.at(k).c_coord, atoms.at(l).a_coord, atoms.at(l).b_coord, atoms.at(l).c_coord);
if(d_kl>minDist && d_kl<maxDist) {
//we have found four atoms within the tolerance
double t = CalculateTetrahedrality4Atoms(atoms.at(i), atoms.at(j), atoms.at(k), atoms.at(l));
tetras.push_back(t);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
//sort before returning
sort(tetras.begin(), tetras.end());
return tetras;
}
/* Returns Tetrahedrality index for a tetrahedron defined by four atoms */
double ATOM_NETWORK::CalculateTetrahedrality4Atoms(const ATOM& atm1, const ATOM& atm2, const ATOM& atm3, const ATOM& atm4) const {
vector <double> edges; // tetrahedra edges' lengths
double d_mean=0.0; // mean value
double Tindex=0.0;
edges.push_back(calcDistance(atm1,atm2));
edges.push_back(calcDistance(atm1,atm3));
edges.push_back(calcDistance(atm1,atm4));
edges.push_back(calcDistance(atm2,atm3));
edges.push_back(calcDistance(atm2,atm4));
edges.push_back(calcDistance(atm3,atm4));
for(int i=0;i<6;i++) d_mean+=edges[i];
d_mean=d_mean/6.0;
for(int i=0;i<5;i++) {
for(int j=i+1;j<6;j++) {
Tindex+=((edges[i]-edges[j])*(edges[i]-edges[j]))/(15.0*d_mean*d_mean);
}
}
return Tindex;
}
/* Return chemical formula */
std::string ATOM_NETWORK::returnChemicalFormula(){
std::string formula;
for(int i=0; i<MAX_ATOMIC_NUMBER; i++) atomic_composition[i] = 0;
for(int i=0; i<numAtoms; i++) {
atomic_composition[lookupAtomicNumber(atoms.at(i).type)-1]+=1;
};
for(int i=0; i<numAtoms; i++) {
if(atomic_composition[lookupAtomicNumber(atoms.at(i).type)-1]>0)
{
std::ostringstream os ;
os << atomic_composition[lookupAtomicNumber(atoms.at(i).type)-1];
formula += atoms.at(i).type + os.str();
atomic_composition[lookupAtomicNumber(atoms.at(i).type)-1]=0;
};
};
return formula;
} // end returnChemicalFormula
/* Determine the smallest supercell dimensions such that a sphere of a given
* diameter does not overlap with itself across the periodic boundary */
/* Marked for deletion
TRIPLET getSmallestSupercell(double diam, ATOM_NETWORK *atmnet) {
//1) set lower bound based on lengths of cell axes
int na = 1+ (diam/atmnet->a);
int nb = 1+ (diam/atmnet->b);
int nc = 1+ (diam/atmnet->c);
int fewest_cells = -1; //no supercell yet successful
TRIPLET smallest_supercell(-1,-1,-1);
//2) search all possible supercell sizes to find the smallest one satisfying
// the minimum image convention for this radius
TRIPLET lb(na, nb, nc);
vector<TRIPLET> supercells;
supercells.push_back(lb);
while(supercells.size()>0) {
//3) take the oldest candidate on the vector
TRIPLET s = supercells.at(0); //oldest supercell candidate, s
for(int i=0; i<supercells.size()-1; i++)
supercells.at(i) = supercells.at(i+1); //shift vector up
supercells.pop_back(); //delete last, which is now duplicated at last-1
int num_cells = s.x*s.y*s.z;
//4) is s a potential new smallest supercell?
if(num_cells<fewest_cells || fewest_cells<0) {
//5) does s satisfy the minimum image convention?
int status = check_sphere_overlap(s.x, s.y, s.z, diam, atmnet);
if(status==-1) {
printf("WARNING: bad unit cell angles!\n");
return smallest_supercell;
} else if(status==1) { //acceptable!
fewest_cells = num_cells;
smallest_supercell = s;
//printf("smallest satisfactory supercell so far: (%d %d %d) =
//%d cells\n", s.a, s.b, s.c, num_cells);
} else { //unacceptable - try larger supercells in each direction
TRIPLET s2(s.x+1, s.y, s.z);
TRIPLET s3(s.x, s.y+1, s.z);
TRIPLET s4(s.x, s.y, s.z+1);
supercells.push_back(s2);
supercells.push_back(s3);
supercells.push_back(s4);
}
}
}
return smallest_supercell;
}
* End: Marked for deletion
*/
/// Default constructor
VOR_EDGE::VOR_EDGE(){}
VOR_EDGE::VOR_EDGE(int myFrom, int myTo, double rad,
int dx, int dy, int dz, double len):
from(myFrom), to(myTo), rad_moving_sphere(rad),
delta_uc_x(dx), delta_uc_y(dy), delta_uc_z(dz),
length(len) {}
/// Copy constructor
VOR_EDGE::VOR_EDGE(const VOR_EDGE& orig):
from(orig.from), to(orig.to), rad_moving_sphere(orig.rad_moving_sphere),
delta_uc_x(orig.delta_uc_x), delta_uc_y(orig.delta_uc_y),
delta_uc_z(orig.delta_uc_z) {}
//Default constructor for VOR_NODE
VOR_NODE::VOR_NODE(){}
VOR_NODE::VOR_NODE(double myX, double myY, double myZ,
double rad, vector<int> ids){
x = myX; y = myY; z = myZ;
rad_stat_sphere = rad;
atomIDs = ids;
}
//Copy constructor for VOR_NODE
/*VOR_NODE::VOR_NODE(const VOR_NODE& orig):
x(orig.x), y(orig.y), z(orig.z),
rad_stat_sphere(orig.rad_stat_sphere), atomIDs(orig.atomIDs) {}
*/
/// Voronoi network constructor
VORONOI_NETWORK::VORONOI_NETWORK (){}
VORONOI_NETWORK::VORONOI_NETWORK(const XYZ& inp_va, const XYZ& inp_vb, const XYZ& inp_vc,
const vector<VOR_NODE>& inp_nodes,
const vector<VOR_EDGE>& inp_edges):
edges(inp_edges), nodes(inp_nodes), v_a(inp_va), v_b(inp_vb), v_c(inp_vc){}
/// Copy constructor for VORONOI_NETWORK
VORONOI_NETWORK::VORONOI_NETWORK (const VORONOI_NETWORK& net):
v_a(net.v_a), v_b(net.v_b), v_c(net.v_c), edges(net.edges), nodes(net.nodes) {}
/** Copy the data contained in this VORONOI_NETWORK to a new network using
the provided pointer. Deprecated, use copy constructor.
*/
void VORONOI_NETWORK::copy(VORONOI_NETWORK *newNet){
newNet->v_a = v_a; newNet->v_b = v_b; newNet->v_c = v_c;
newNet->edges.clear(); newNet->edges = edges;
newNet->nodes.clear(); newNet->nodes = nodes;
}
/* Marked for deletion
void VORONOI_NETWORK::filterVornetEdges(vector<int> nodeIDs,
VORONOI_NETWORK *oldNet,
VORONOI_NETWORK *newNet)
{
vector<bool> includeNodes = vector<bool>(oldNet->nodes.size(), false);
for(unsigned int i = 0; i < nodeIDs.size(); i++){
includeNodes[nodeIDs[i]] = true;
}
vector<VOR_NODE> newNodes = vector<VOR_NODE>();
for(unsigned int i = 0; i < oldNet->nodes.size(); i++){
newNodes.push_back(oldNet->nodes[i]);
}
vector<VOR_EDGE> newEdges = vector<VOR_EDGE>();
for(unsigned int i = 0; i < oldNet->edges.size(); i++){
VOR_EDGE edge = oldNet->edges[i];
if(includeNodes[edge.from] && includeNodes[edge.to]){
newEdges.push_back(edge);
}
}
newNet->nodes = newNodes;
newNet->edges = newEdges;
newNet->v_a = oldNet->v_a;
newNet->v_b = oldNet->v_b;
newNet->v_c = oldNet->v_c;
}
* End: marked for deletion
*/
/* Removes all the edges between nodes that are not both contained in nodeIDs.
However, these nodes remain in the Voronoi network
*/
const VORONOI_NETWORK VORONOI_NETWORK::filterEdges(vector<int> nodeIDs)
{
vector<bool> includeNodes = vector<bool>(nodes.size(), false);
for(unsigned int i = 0; i < nodeIDs.size(); i++){
includeNodes[nodeIDs[i]] = true;
}
vector<VOR_NODE> newNodes = vector<VOR_NODE>();
for(unsigned int i = 0; i < nodes.size(); i++){
newNodes.push_back(nodes[i]);
}
vector<VOR_EDGE> newEdges = vector<VOR_EDGE>();
for(unsigned int i = 0; i < edges.size(); i++){
VOR_EDGE edge = edges[i];
if(includeNodes[edge.from] && includeNodes[edge.to]){
newEdges.push_back(edge);
}
}
return VORONOI_NETWORK(v_a, v_b, v_c, newNodes, newEdges);
}
VORONOI_NETWORK filterVoronoiNetwork(const VORONOI_NETWORK* vornet, double minRadius)
{
// Add all nodes whose radius is greater than the provided minimum to a list
map<int,int> idMappings;
vector<VOR_NODE> newNodes;
int i = 0;
int idCount = 0;
vector<VOR_NODE>::const_iterator nodeIter = vornet->nodes.begin();
for(;nodeIter != vornet->nodes.end(); i++, nodeIter++)
if(nodeIter->rad_stat_sphere > minRadius){
newNodes.push_back(*nodeIter);
idMappings.insert(pair<int,int> (i, idCount));
idCount++;
}
// Add all edges whose radius is greater than the provided minimum to a list
vector<VOR_EDGE> newEdges;
vector<VOR_EDGE>::const_iterator edgeIter = vornet->edges.begin();
for(;edgeIter != vornet->edges.end(); edgeIter++)
if((edgeIter->rad_moving_sphere > minRadius) &&
(idMappings.find(edgeIter->from) != idMappings.end()) &&
(idMappings.find(edgeIter->to) != idMappings.end())){
int from = idMappings.find(edgeIter->from)->second;
int to = idMappings.find(edgeIter->to)->second;
newEdges.push_back(
VOR_EDGE(from, to, edgeIter->rad_moving_sphere,
edgeIter->delta_uc_x, edgeIter->delta_uc_y,
edgeIter->delta_uc_z, edgeIter->length)
);
}
return VORONOI_NETWORK(vornet->v_a, vornet->v_b, vornet->v_c, newNodes, newEdges);
}
/** Copies all edges and nodes within the provided VORONOI_NETWORK
to a new network iff a sphere with the specified radius can pass.*/
/* Deprecated. Use the function above. */
void filterVoronoiNetwork(VORONOI_NETWORK *vornet, VORONOI_NETWORK *newVornet, double minRadius){
map<int,int> idMappings;
vector<VOR_NODE>::iterator nodeIter = vornet->nodes.begin();
vector<VOR_NODE> newNodes;
int i = 0;
int idCount = 0;
// Add all nodes whose radius is greater than the provided minimum to a list
while(nodeIter != vornet->nodes.end()){
if(nodeIter->rad_stat_sphere > minRadius){
newNodes.push_back(*nodeIter);
idMappings.insert(pair<int,int> (i, idCount));
idCount++;
}
i++;
nodeIter++;
}
// Copy nodes that met requirement
newVornet->nodes = newNodes;
vector<VOR_EDGE>::iterator edgeIter = vornet->edges.begin();
vector<VOR_EDGE> newEdges;
// Add all edges whose radius is greater than the provided minimum to a list
while(edgeIter != vornet->edges.end()){
if((edgeIter->rad_moving_sphere > minRadius) && (
idMappings.find(edgeIter->from) != idMappings.end()) && (
idMappings.find(edgeIter->to) != idMappings.end())){
VOR_EDGE newEdge;
newEdge.from = idMappings.find(edgeIter->from)->second;
newEdge.to = idMappings.find(edgeIter->to)->second;
newEdge.rad_moving_sphere = edgeIter->rad_moving_sphere;
newEdge.delta_uc_x = edgeIter->delta_uc_x;
newEdge.delta_uc_y = edgeIter->delta_uc_y;
newEdge.delta_uc_z = edgeIter->delta_uc_z;
newEdge.length = edgeIter->length;
newEdges.push_back(newEdge);
}
edgeIter++;
}
// Copy edges that met requirement
newVornet->edges = newEdges;
// Copy unitcell vectors to new network
newVornet->v_a = vornet->v_a;
newVornet->v_b = vornet->v_b;
newVornet->v_c = vornet->v_c;
}
/** Returns a copy of the VORNOI_NETWORK instance
but removes the edges that do not allow a sphere
with the provided radius to pass. */
const VORONOI_NETWORK VORONOI_NETWORK::prune(const double& minRadius)
{
vector<VOR_EDGE> newEdges;
// Add edges whose radius is greater than the input minimum to a list
for(vector<VOR_EDGE>::iterator edgeIter = edges.begin();
edgeIter != edges.end(); edgeIter++) {
if(edgeIter->rad_moving_sphere > minRadius){
//VOR_EDGE newEdge = *edgeIter;
// further check: keep edges only if they connect accessible nodes
if( nodes[edgeIter->from].rad_stat_sphere > minRadius && nodes[edgeIter->to].rad_stat_sphere > minRadius )
newEdges.push_back(*edgeIter);
}
};
vector<VOR_NODE> newNodes = nodes;
for(unsigned int i = 0; i < nodes.size(); i++)
{
if(nodes[i].rad_stat_sphere > minRadius) newNodes[i].active = true; else newNodes[i].active = false;
};
return VORONOI_NETWORK(v_a, v_b, v_c, newNodes, newEdges);
}
/** Stores a copy of the original VORNOI_NETWORK into the other provided
VORONOI_NETWORK but removes the edges that do not allow a sphere
with the provided radius to pass. */
/* Marked for deletion
void pruneVoronoiNetwork(VORONOI_NETWORK *vornet,
VORONOI_NETWORK *newVornet,
double minRadius){
newVornet->nodes = vornet->nodes;
vector<VOR_EDGE>::iterator edgeIter = vornet->edges.begin();
vector<VOR_EDGE> newEdges;
// Add all edges whose radius is greater than the provided minimum to a list
while(edgeIter != vornet->edges.end()){
if(edgeIter->rad_moving_sphere > minRadius){
VOR_EDGE newEdge;
newEdge.from = edgeIter->from;
newEdge.to = edgeIter->to;
newEdge.rad_moving_sphere = edgeIter->rad_moving_sphere;
newEdge.delta_uc_x = edgeIter->delta_uc_x;
newEdge.delta_uc_y = edgeIter->delta_uc_y;
newEdge.delta_uc_z = edgeIter->delta_uc_z;
newEdge.length = edgeIter->length;
newEdges.push_back(newEdge);
}
edgeIter++;
}
// Copy edges that met requirement
newVornet->edges = newEdges;
// Copy unitcell vectors to new network
newVornet->v_a = vornet->v_a;
newVornet->v_b = vornet->v_b;
newVornet->v_c = vornet->v_c;
}
* End: Marked for deletion
*/
/** Stores a copy of the original VORNOI_NETWORK into the other provided
* VORONOI_NETWORK but removes the edges that are connected to specified
* nodes (specified by ID list)
*/
void pruneVoronoiNetworkfromEdgeList(VORONOI_NETWORK *vornet,
VORONOI_NETWORK *newVornet,
vector <int> ids){
newVornet->nodes = vornet->nodes;
vector<VOR_EDGE>::iterator edgeIter = vornet->edges.begin();
vector<VOR_EDGE> newEdges;
// Add all edges whose are not connected to specified nodes
while(edgeIter != vornet->edges.end()){
int flag=0;
for(unsigned int i=0; i<ids.size(); i++){
// if edge connects to node of specified id, flag it
if(edgeIter->from==ids[i]||edgeIter->to==ids[i]) flag++;
};
if(flag==0){ // only keep unflagged edges
VOR_EDGE newEdge;
newEdge.from = edgeIter->from;
newEdge.to = edgeIter->to;
newEdge.rad_moving_sphere = edgeIter->rad_moving_sphere;
newEdge.delta_uc_x = edgeIter->delta_uc_x;
newEdge.delta_uc_y = edgeIter->delta_uc_y;
newEdge.delta_uc_z = edgeIter->delta_uc_z;
newEdge.length = edgeIter->length;
newEdges.push_back(newEdge);
}
edgeIter++;
}
newVornet->edges = newEdges;
newVornet->v_a = vornet->v_a;
newVornet->v_b = vornet->v_b;
newVornet->v_c = vornet->v_c;
}
/* Attempt to substitute every other Si atom with an Al atom. ATOM_NETWORK may
* only consist of Si and O atoms, where each Si atom must be bonded to exactly
* 4 oxygen atoms and each oxygen atom must be bonded to exactly 2 Si atoms.
* Returns true iff the substitution was successful and stores the number of
* substitutions using the provided reference. The provided boolean specifies
* whether the seeded silicon atom is substituted or not.
* Since only 2 configurations are possible if the structure is consistent,
* changing this parameter enables generation of all configurations. */
bool substituteAtoms(ATOM_NETWORK *origNet, ATOM_NETWORK *newNet,
bool substituteSeed, int *numSubstitutions, bool radial){
int numAtoms = origNet->numAtoms;
double max_bond_length = 1.95;
vector< vector<int> > bonds = vector< vector<int> >(numAtoms, vector<int>());
for(int i = 0; i < numAtoms; i++){
ATOM atom_one = origNet->atoms[i];
for(int j = i + 1; j < numAtoms; j++){
ATOM atom_two = origNet->atoms[j];
if(origNet->calcDistance(atom_one, atom_two) < max_bond_length){
if(atom_one.type.compare(atom_two.type) == 0){
cerr << "Atomic network substitution aborted because atoms of same type "
"are bonded to one another" << "\n" << "Occurred for type "
<< atom_one.type << " between atoms " << i << " and " << j << "\n";
return false;
}
bonds[i].push_back(j);
bonds[j].push_back(i);
}
}
}
for(int i = 0; i < numAtoms; i++){
if(origNet->atoms[i].type.compare("Si") == 0){
if(bonds[i].size() != 4){
cerr << "Atomic network substitution aborted because Si atom bonded to "
<< bonds[i].size() << " other atoms instead of 4" << "\n"
<< "Occurred for atom " << i << "\n";
return false;
}
}
else if(origNet->atoms[i].type.compare("O") == 0){
if(bonds[i].size() != 2){
cerr << "Atomic network substitution aborted because O atom bonded to "
<< bonds[i].size() << " other atoms instead of 2" << "\n"
<< "Occurred for atom " << i << "\n";
return false;
}
}
else{
cerr << "Atomic network substitution aborted because atom type other than "
"Si or O detected" << "\n" << "Occurred for atom " << i << "\n";
return false;
}
}
int firstSiID = -1;
for(int i = 0; i < numAtoms; i++){
if(origNet->atoms[i].type.compare("Si") == 0){
firstSiID = i;
break;
}
}
if(firstSiID == -1){
cerr << "Error: Atom substitution failed because structure does not "
"contain any Si atoms \n";
return false;
}
vector<bool> atomProc = vector<bool>(numAtoms, false); // Whether atom has been processed
vector<bool> atomSubs = vector<bool>(numAtoms, false); // Whether atom has been substituted
int numProc = 0; // Number of processed atoms
vector< pair<int, bool> > atomsToProcess; // Stack of atom/substituion info
vector<int> fromIDs; // Stack of where each atom/subst came from
pair<int, bool> seed(firstSiID, substituteSeed);
atomsToProcess.push_back(seed); // Seed with the first Si atom and not substituting it
fromIDs.push_back(-1); // Starting node
while(atomsToProcess.size() != 0){