-
Notifications
You must be signed in to change notification settings - Fork 8
/
xtalcomp.cpp
2195 lines (2000 loc) · 70.5 KB
/
xtalcomp.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
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
/**********************************************************************
XtalComp - Determine if two crystal descriptions represent the same
structure
Copyright (C) 2011 by David C. Lonie
This source code is released under the New BSD License, (the "License").
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***********************************************************************/
#include "xtalcomp.h"
#include "stablecomparison.h"
extern "C" {
#include "spglib.h"
}
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
// Save some keystrokes...
typedef XtalComp::DuplicateMap DuplicateMap;
#define RAD_TO_DEG 57.2957795131
#define DEG_TO_RAD 0.0174532925199
#define PRECISION 1e-8
#undef XTALCOMP_DEBUG
//#define XTALCOMP_DEBUG 1
#ifdef XTALCOMP_DEBUG
#define DEBUG_BREAK printf("\n");
#define DEBUG_DIV printf("\
--------------------------------------------------------------------------------\n")
#define DEBUG_ATOM(t,c) printf("%2d: | %9.5f %9.5f %9.5f |\n", t, (c).x(), (c).y(), (c).z())
#define DEBUG_ATOMCF(t,c,f) printf("%2d: | %9.5f %9.5f %9.5f | %9.5f %9.5f %9.5f |\n", \
t, (c).x(), (c).y(), (c).z(), \
(f).x(), (f).y(), (f).z())
#define DEBUG_VECTOR(v) printf("| %9.5f %9.5f %9.5f |\n", (v).x(), (v).y(), (v).z())
#define DEBUG_MATRIX(m) printf("| %9.5f %9.5f %9.5f |\n" \
"| %9.5f %9.5f %9.5f |\n" \
"| %9.5f %9.5f %9.5f |\n", \
(m)(0,0), (m)(0,1), (m)(0,2), \
(m)(1,0), (m)(1,1), (m)(1,2), \
(m)(2,0), (m)(2,1), (m)(2,2))
#define DEBUG_STRING_VECTOR(s,v) printf("%s: | %9.5f %9.5f %9.5f |\n", \
s, (v).x(), (v).y(), (v).z())
#define DEBUG_MATRIX4(m) printf("| %9.5f %9.5f %9.5f %9.5f |\n" \
"| %9.5f %9.5f %9.5f %9.5f |\n" \
"| %9.5f %9.5f %9.5f %9.5f |\n" \
"| %9.5f %9.5f %9.5f %9.5f |\n", \
(m)(0,0), (m)(0,1), (m)(0,2), (m)(0,3), \
(m)(1,0), (m)(1,1), (m)(1,2), (m)(1,3), \
(m)(2,0), (m)(2,1), (m)(2,2), (m)(2,3), \
(m)(3,0), (m)(3,1), (m)(3,2), (m)(3,3))
#define DEBUG_STRING(str) printf("%s\n", str)
#define DEBUG_STRING_INT(str, i) printf("%s %d\n", str, i)
#endif
// vecs += trans
inline void translateVectorsInPlace(std::vector<XcVector> *vecs,
const XcVector &trans)
{
for (std::vector<XcVector>::iterator it = vecs->begin(),
it_end = vecs->end(); it != it_end; ++it) {
*it += trans;
}
}
// Calculate a standardized angle between v1 and v2 -- if the
// vectors are mirrored (e.g. an enantiomorphic pair of crystals),
// the angles will vary in a non-obvious way:
//
// v1 y v2
// ^ ^ ^
// \ | /
// \ | /
// \ | /
// \|/
// -----|--------> x
//
// v1 and v2 are simply mirrored around the y axis. Define:
// a1 = fabs(angle(x, v1))
// a2 = fabs(angle(x, v2))
// b = fabs(angle(y, v1)) = fabs(angle(y, v2)
//
// In this case, a2 = a1 - 2b.
//
// So check if any of the angles are greater than 90. If so
// correct by calculating b = a-90, then correct with a -= 2b.
static inline double compAngle(const XcVector &v1,
const XcVector &v2)
{
const double angle = fabs( acos( v1.dot(v2) /
sqrt(v1.squaredNorm() *
v2.squaredNorm()))
* RAD_TO_DEG);
if (angle <= 90.0)
return angle;
else
return 180.0 - angle;
}
class XtalComp::ReducedXtal
{
private:
unsigned int m_numAtoms;
std::vector<unsigned int> m_types;
std::vector<XcVector> m_ccoords;
std::vector<XcVector> m_fcoords;
// Fractionation and cell matrices -- ready to use:
// ccoord = cmat * fcoord
// fcoord = fmat * ccoord
XcMatrix m_cmat;
XcMatrix m_fmat;
public:
ReducedXtal(const XcMatrix cellMatrix,
const std::vector<unsigned int> types,
const std::vector<XcVector> positions) :
m_numAtoms(types.size()),
m_types(types),
m_ccoords(),
m_fcoords(positions),
m_cmat(cellMatrix.transpose()),
m_fmat(m_cmat.inverse())
{
// Fill ccoords
m_ccoords.reserve(m_fcoords.size());
for (std::vector<XcVector>::const_iterator it = m_fcoords.begin(),
it_end = m_fcoords.end(); it != it_end; ++it) {
m_ccoords.push_back(m_cmat * (*it));
}
}
virtual ~ReducedXtal() {}
unsigned int numAtoms() const {return m_numAtoms;}
const std::vector<unsigned int> & types() const {return m_types;}
const std::vector<XcVector> & ccoords() const {return m_ccoords;}
const std::vector<XcVector> & fcoords() const {return m_fcoords;}
const XcMatrix & cmat() const {return m_cmat;}
const XcMatrix & fmat() const {return m_fmat;}
double volume() const {return fabs(m_cmat.determinant());}
XcVector v1() const {return m_cmat.col(0);}
XcVector v2() const {return m_cmat.col(1);}
XcVector v3() const {return m_cmat.col(2);}
// Defined at end of file:
// Niggli reduce, rotate to std orientation, wrap atoms to cell:
bool canonicalizeLattice();
bool isNiggliReduced() const;
XcMatrix getCellMatrixInStandardOrientation() const;
void frac2Cart(const XcVector &fcoord, XcVector *ccoord) const
{
*ccoord = m_cmat * fcoord;
}
void cart2Frac(const XcVector &ccoord, XcVector *fcoord) const
{
*fcoord = m_fmat * ccoord;
}
// Translate member coords by the fractional translation vector fracTrans
void translateAndExpandCoords(const XcVector & fracTrans,
const float cartLengthTol,
DuplicateMap *duplicateAtoms)
{
// Translate coords
for(std::vector<XcVector>::iterator it = m_fcoords.begin(),
it_end = m_fcoords.end(); it != it_end; ++it) {
*it += fracTrans;
}
// Expand / wrap fcoords
XtalComp::expandFractionalCoordinates(&m_types, &m_fcoords,
duplicateAtoms,
m_cmat, cartLengthTol);
// update ccoords:
m_ccoords.resize(m_fcoords.size());
m_numAtoms = m_fcoords.size();
for (size_t i = 0; i < m_numAtoms; ++i) {
frac2Cart(m_fcoords[i], &m_ccoords[i]);
}
}
};
bool XtalComp::compare(const XcMatrix &_cellMatrix1,
const std::vector<unsigned int> &_types1,
const std::vector<XcVector> &_positions1,
const XcMatrix &_cellMatrix2,
const std::vector<unsigned int> &_types2,
const std::vector<XcVector> &_positions2,
float transform[16],
const double cartTol,
const double angleTol,
const bool reduceXtalsToPrimitive)
{
// Make a non-const copy of these variables so we may edit them if
// reduceToPrimitive is true
XcMatrix cellMatrix1 = _cellMatrix1;
std::vector<unsigned int> types1 = _types1;
std::vector<XcVector> positions1 = _positions1;
XcMatrix cellMatrix2 = _cellMatrix2;
std::vector<unsigned int> types2 = _types2;
std::vector<XcVector> positions2 = _positions2;
// First, reduce the xtals to their primitive form if needed
if (reduceXtalsToPrimitive) {
// Perhaps we'll use these later
//unsigned int numAtoms1 = types1.size();
//unsigned int numAtoms2 = types2.size();
// We'll use the same cartesian tolerance for the spglib calls, too
unsigned int spg1 = reduceToPrimitive(positions1, types1,
cellMatrix1, cartTol);
unsigned int spg2 = reduceToPrimitive(positions2, types2,
cellMatrix2, cartTol);
// bool cellReduced1 = false, cellReduced2 = false;
//if (numAtoms1 != types1.size()) cellReduced1 = true;
//if (numAtoms2 != types2.size()) cellReduced2 = true;
if (spg1 < 1 || spg1 > 230) {
std::cerr << "An invalid spg was detected by spglib for cell 1\n";
return false;
}
if (spg2 < 1 || spg2 > 230) {
std::cerr << "An invalid spg was detected by spglib for cell 2\n";
return false;
}
}
// Next, check that types and positions are of the same size
if (types1.size() != positions1.size() ||
types2.size() != positions2.size() ){
fprintf(stderr, "XtalComp::compare was given a structure description with"
" differing numbers of types and positions:\n\ttypes1: %d "
"positions1: %d\n\ttypes2: %d positions2: %d\n", types1.size(),
positions1.size(),types2.size(), positions2.size());
return false;
}
// Next ensure that the two descriptions have the same number of atoms
if (types1.size() != types2.size()) {
return false;
}
// Check that compositions match
// Make copy of types, sort, and compare
std::vector<unsigned int> types1Comp (types1);
std::vector<unsigned int> types2Comp (types2);
std::sort(types1Comp.begin(), types1Comp.end());
std::sort(types2Comp.begin(), types2Comp.end());
// Compare
for (size_t i = 0; i < types1Comp.size(); ++i) {
if (types1Comp[i] != types2Comp[i]) {
return false;
}
}
// Build ReducedXtals
ReducedXtal x1 (cellMatrix1, types1, positions1);
ReducedXtal x2 (cellMatrix2, types2, positions2);
// Standardize the lattices
if (!x1.canonicalizeLattice() ||
!x2.canonicalizeLattice() ){
std::cerr << "XtalComp warning: Failed to canonicalize one of the "
"lattices. Returning false without finishing comparison.\n";
return false;
}
// Check params here. Do not just compare the matrices, as this may
// not catch certain enantiomorphs:
// Compare volumes. Tolerance is 1% of this->getVolume()
const double vol1 = fabs(cellMatrix1.determinant());
const double vol2 = fabs(cellMatrix2.determinant());
// Match volumes to within 1%
const double voltol = 0.01 * 0.5 * (vol1 + vol2);
if (fabs(vol1 - vol2) > voltol) return false;
// Normalize and compare lattice params
const double a1 = x1.v1().squaredNorm();
const double b1 = x1.v2().squaredNorm();
const double c1 = x1.v3().squaredNorm();
const double a2 = x2.v1().squaredNorm();
const double b2 = x2.v2().squaredNorm();
const double c2 = x2.v3().squaredNorm();
// Estimate scaled error, 4 * x * \Delta x
const double cart2Tol ( 4.0 * sqrt((a1 + b1 + c1 + a2 + b2 + c2)
* 0.166666666667) * cartTol);
if (fabs(a1 - a2) > cart2Tol) return false;
if (fabs(b1 - b2) > cart2Tol) return false;
if (fabs(c1 - c2) > cart2Tol) return false;
// Angles -- see comment above definition of compAngle for
// explanation
const double alpha1 = compAngle(x1.v2(), x1.v3());
const double beta1 = compAngle(x1.v1(), x1.v3());
const double gamma1 = compAngle(x1.v1(), x1.v2());
const double alpha2 = compAngle(x2.v2(), x2.v3());
const double beta2 = compAngle(x2.v1(), x2.v3());
const double gamma2 = compAngle(x2.v1(), x2.v2());
if (fabs(alpha1 - alpha2) > angleTol) return false;
if (fabs(beta1 - beta2) > angleTol) return false;
if (fabs(gamma1 - gamma2) > angleTol) return false;
// Run the XtalComp algorithm
XtalComp xc (&x1, &x2, cartTol, angleTol);
// iterate through comparisons
while (xc.hasMoreTransforms()) {
xc.applyNextTransform();
if (xc.compareCurrent()) {
if (transform != NULL) {
xc.getCurrentTransform(transform);
}
// Found a match!
return true;
}
}
// No match
return false;
}
XtalComp::~XtalComp()
{
// ReducedXtals are cleaned up in compare(...)
}
XtalComp::XtalComp(ReducedXtal *x1, ReducedXtal *x2,
const double cartTol, const double angleTol) :
m_rx1(x1),
m_rx2(x2),
m_lengthtol(cartTol),
m_angletol(angleTol),
m_lfAtomType(UINT_MAX),
m_lfAtomCount(UINT_MAX),
m_transformsIndex(0)
{
setLeastFrequentAtomInfo();
setReferenceBasis();
prepareRx1();
buildSuperLfCCoordList2();
#ifdef XTALCOMP_DEBUG
DEBUG_BREAK;
DEBUG_DIV;
printf("Number of atoms: %d and %d\n", x1->numAtoms(), x2->numAtoms());
printf("There are %d atoms of type %d\n", m_lfAtomCount, m_lfAtomType);
DEBUG_DIV;
DEBUG_STRING("Reference Xtal 1 cmat:");
DEBUG_MATRIX(m_rx1->cmat());
DEBUG_STRING("Reference Xtal 1 fmat:");
DEBUG_MATRIX(m_rx1->fmat());
DEBUG_STRING("Reference Xtal 1 atoms (cart|frac):");
for (int i = 0; i < m_rx1->ccoords().size(); ++i) {
DEBUG_ATOMCF(m_rx1->types()[i], m_rx1->ccoords()[i],
m_rx1->fcoords()[i]);
}
DEBUG_DIV;
DEBUG_STRING("Reference Xtal 2 cmat:");
DEBUG_MATRIX(m_rx2->cmat());
DEBUG_STRING("Reference Xtal 2 fmat:");
DEBUG_MATRIX(m_rx2->fmat());
DEBUG_STRING("Original Xtal 2 atoms (cart|frac):");
for (int i = 0; i < m_rx2->ccoords().size(); ++i) {
DEBUG_ATOMCF(m_rx2->types()[i], m_rx2->ccoords()[i],
m_rx2->fcoords()[i]);
}
DEBUG_DIV;
#endif
findCandidateTransforms();
#ifdef XTALCOMP_DEBUG
printf("Number of transforms: %d\n", m_transforms.size());
DEBUG_DIV;
#endif
}
void XtalComp::setLeastFrequentAtomInfo()
{
// Get list of unique types
const std::vector<unsigned int> &rx1_types = m_rx1->types();
const std::vector<unsigned int> &rx2_types = m_rx2->types();
// Copy
std::vector<unsigned int> uniqueTypes (rx1_types);
// Sort before unique'ing
sort(uniqueTypes.begin(), uniqueTypes.end());
// Remove consecutive duplicates
const std::vector<unsigned int>::const_iterator uniqueTypes_end =
unique_copy(rx1_types.begin(), rx1_types.end(), uniqueTypes.begin());
// Now the range [uniqueTypes.begin(), uniqueTypes.end()) is
// sorted and unique.
// determine least frequent atom type
for (std::vector<unsigned int>::iterator it = uniqueTypes.begin();
it != uniqueTypes_end; ++it) {
ptrdiff_t cur = count(rx1_types.begin(), rx1_types.end(), *it);
unsigned int ucur = static_cast<unsigned int>(cur);
if (ucur < m_lfAtomCount) {
m_lfAtomCount = ucur;
m_lfAtomType = *it;
}
}
}
void XtalComp::setReferenceBasis()
{
// Just use the cell vectors of rx1
m_refVec1 = m_rx1->cmat().col(0);
m_refVec2 = m_rx1->cmat().col(1);
m_refVec3 = m_rx1->cmat().col(2);
}
void XtalComp::prepareRx1()
{
// Find a translation vector that moves an lfAtom in rx1 to the
// origin (e.g., the negative of an lfAtom's coordinates)
assert (m_rx1->fcoords().size() == m_rx1->types().size());
size_t refTransIndex = 0;
std::vector<unsigned int>::const_iterator refTransTypeIterator
= m_rx1->types().begin();
// Assert that there is at least one atom of type lfAtomType
assert (find(m_rx1->types().begin(), m_rx1->types().end(), m_lfAtomType)
!= m_rx1->types().end());
while (*refTransTypeIterator != m_lfAtomType) {
++refTransIndex;
++refTransTypeIterator;
}
XcVector rx1_ftrans = - (m_rx1->fcoords()[refTransIndex]);
// Translate rx1 by the above vector. This places an lfAtom at the origin.
m_rx1->translateAndExpandCoords(rx1_ftrans, m_lengthtol, &m_duplicatedAtoms);
}
void XtalComp::getCurrentTransform(float ret[16])
{
// Fill ret with the 4x4 matrix of the current transform.
const XcVector &trans = m_transform.translation();
const XcMatrix &rot = m_transform.rotation();
ret[0*4+0] = rot(0,0);
ret[0*4+1] = rot(0,1);
ret[0*4+2] = rot(0,2);
ret[0*4+3] = trans(0);
ret[1*4+0] = rot(1,0);
ret[1*4+1] = rot(1,1);
ret[1*4+2] = rot(1,2);
ret[1*4+3] = trans(1);
ret[2*4+0] = rot(2,0);
ret[2*4+1] = rot(2,1);
ret[2*4+2] = rot(2,2);
ret[2*4+3] = trans(2);
ret[3*4+0] = 0.0;
ret[3*4+1] = 0.0;
ret[3*4+2] = 0.0;
ret[3*4+3] = 1.0;
}
void XtalComp::buildSuperLfCCoordList2()
{
// Find all lfAtoms in rx2 and build a supercell of them
const std::vector<unsigned int> &types = m_rx2->types();
const std::vector<XcVector> &ccoords = m_rx2->ccoords();
m_superLfCCoordList2.clear();
m_superLfCCoordList2.reserve(8 * m_lfAtomCount);
assert (ccoords.size() == types.size());
// Determine the length of the cell diagonal. If it is the same as
// any vector length, we need to build a 3x3x3
// supercell. Otherwise, a (faster) 2x2x2 will suffice.
const XcVector v1 (m_rx2->cmat().col(0)); // 1 0 0
const XcVector v2 (m_rx2->cmat().col(1)); // 0 1 0
const XcVector v3 (m_rx2->cmat().col(2)); // 0 0 1
const double v1SqNorm = v1.squaredNorm();
const double v2SqNorm = v2.squaredNorm();
const double v3SqNorm = v3.squaredNorm();
const double diagSqNorm = (v1+v2+v3).squaredNorm();
const double normTol = 1e-4;
bool diagonalSameLengthAsVector =
(fabs(diagSqNorm - v1SqNorm) < normTol ||
fabs(diagSqNorm - v2SqNorm) < normTol ||
fabs(diagSqNorm - v3SqNorm) < normTol );
// We also need to build 3x3x3 for hexagonal cells
bool cellIsHexagonal =
((fabs(v1SqNorm - v2SqNorm) < normTol &&
fabs(compAngle(v1, v2) - 60.0) < m_angletol) ||
(fabs(v1SqNorm - v3SqNorm) < normTol &&
fabs(compAngle(v1, v3) - 60.0) < m_angletol) ||
(fabs(v2SqNorm - v3SqNorm) < normTol &&
fabs(compAngle(v2, v3) - 60.0) < m_angletol));
#ifdef XTALCOMP_DEBUG
if (cellIsHexagonal)
DEBUG_STRING("Cell is hexagonal");
#endif
// 3x3x3 case:
if (diagonalSameLengthAsVector || cellIsHexagonal) {
const XcVector v4 (v1 + v2); // 1 1 0
const XcVector v5 (v4 + v2); // 1 2 0
const XcVector v6 (v1 + v3); // 1 0 1
const XcVector v7 (v6 + v3); // 1 0 2
const XcVector v8 (v4 + v3); // 1 1 1
const XcVector v9 (v8 + v3); // 1 1 2
const XcVector v10(v8 + v2); // 1 2 1
const XcVector v11(v10 + v3); // 1 2 2
const XcVector v12(v1 + v1); // 2 0 0
const XcVector v13(v12 + v2); // 2 1 0
const XcVector v14(v13 + v2); // 2 2 0
const XcVector v15(v12 + v3); // 2 0 1
const XcVector v16(v15 + v3); // 2 0 2
const XcVector v17(v13 + v3); // 2 1 1
const XcVector v18(v17 + v3); // 2 1 2
const XcVector v19(v17 + v2); // 2 2 1
const XcVector v20(v19 + v3); // 2 2 2
const XcVector v21(v2 + v3); // 0 1 1
const XcVector v22(v21 + v3); // 0 1 2
const XcVector v23(v2 + v2); // 0 2 0
const XcVector v24(v23 + v3); // 0 2 1
const XcVector v25(v24 + v3); // 0 2 2
const XcVector v26(v3 + v3); // 0 0 2
for (size_t i = 0; i < types.size(); ++i) {
if (types[i] == m_lfAtomType) {
const XcVector &tmpVec = ccoords[i];
// Add to cell
m_superLfCCoordList2.push_back(tmpVec);
// Replicate to supercell
m_superLfCCoordList2.push_back(tmpVec + v1 );
m_superLfCCoordList2.push_back(tmpVec + v2 );
m_superLfCCoordList2.push_back(tmpVec + v3 );
m_superLfCCoordList2.push_back(tmpVec + v4 );
m_superLfCCoordList2.push_back(tmpVec + v5 );
m_superLfCCoordList2.push_back(tmpVec + v6 );
m_superLfCCoordList2.push_back(tmpVec + v7 );
m_superLfCCoordList2.push_back(tmpVec + v8 );
m_superLfCCoordList2.push_back(tmpVec + v9 );
m_superLfCCoordList2.push_back(tmpVec + v10);
m_superLfCCoordList2.push_back(tmpVec + v11);
m_superLfCCoordList2.push_back(tmpVec + v12);
m_superLfCCoordList2.push_back(tmpVec + v13);
m_superLfCCoordList2.push_back(tmpVec + v14);
m_superLfCCoordList2.push_back(tmpVec + v15);
m_superLfCCoordList2.push_back(tmpVec + v16);
m_superLfCCoordList2.push_back(tmpVec + v17);
m_superLfCCoordList2.push_back(tmpVec + v18);
m_superLfCCoordList2.push_back(tmpVec + v19);
m_superLfCCoordList2.push_back(tmpVec + v20);
m_superLfCCoordList2.push_back(tmpVec + v21);
m_superLfCCoordList2.push_back(tmpVec + v22);
m_superLfCCoordList2.push_back(tmpVec + v23);
m_superLfCCoordList2.push_back(tmpVec + v24);
m_superLfCCoordList2.push_back(tmpVec + v25);
m_superLfCCoordList2.push_back(tmpVec + v26);
}
}
}
// 2x2x2 case:
else {
const XcVector v4 (v1 + v2); // 1 1 0
const XcVector v5 (v1 + v3); // 1 0 1
const XcVector v6 (v4 + v3); // 1 1 1
const XcVector v7 (v2 + v3); // 0 1 1
for (size_t i = 0; i < types.size(); ++i) {
if (types[i] == m_lfAtomType) {
const XcVector &tmpVec = ccoords[i];
// Add to cell
m_superLfCCoordList2.push_back(tmpVec);
// Replicate to supercell
m_superLfCCoordList2.push_back(tmpVec + v1 );
m_superLfCCoordList2.push_back(tmpVec + v2 );
m_superLfCCoordList2.push_back(tmpVec + v3 );
m_superLfCCoordList2.push_back(tmpVec + v4 );
m_superLfCCoordList2.push_back(tmpVec + v5 );
m_superLfCCoordList2.push_back(tmpVec + v6 );
m_superLfCCoordList2.push_back(tmpVec + v7 );
}
}
}
}
void XtalComp::findCandidateTransforms()
{
// Find all sets of 4 coordinates that correspond to the reference
// translation vectors
//
// a
// | c
// | /
// |/
// o------b
//
// o, a, b, and c are cartesian coordinates in
// m_superLfCCoordList. Define:
//
// t1 = a-o
// t2 = b-o
// t3 = c-o
//
// If:
//
// t1.squaredNorm() == m_refVec1.squaredNorm() &&
// t2.squaredNorm() == m_refVec2.squaredNorm() &&
// t3.squaredNorm() == m_refVec3.squaredNorm() &&
// angle(t1,t2) == angle(m_refVec1,refVec2) (== gamma) &&
// angle(t1,t3) == angle(m_refVec1,refVec3) (== beta) &&
// angle(t2,t3) == angle(m_refVec2,refVec3) (== alpha)
//
// Then we have a valid candidate reference frame. Store -o as the
// translation and calculate a rotation/reflection matrix that
// will transform t1, t2, t3 into the refVecs. Use the XcTransform
// class to store these operations for later retrieval.
// Set tolerance for vector checks:
const double tol = m_lengthtol;
const double squaredTol = tol*tol;
// Allocate room for all lfAtoms in the translations list, (this
// is the maximum)
m_transforms.clear();
m_transforms.reserve(m_lfAtomCount);
// Lists to store candidate tX vectors
std::vector<XcVector> t1_candidates;
std::vector<XcVector> t2_candidates;
std::vector<XcVector> t3_candidates;
// Search for candidate tX vectors:
//
// Cache some values:
const double v1Norm2 = m_refVec1.squaredNorm();
const double v2Norm2 = m_refVec2.squaredNorm();
const double v3Norm2 = m_refVec3.squaredNorm();
const double vAlpha = compAngle(m_refVec2, m_refVec3);
const double vBeta = compAngle(m_refVec1, m_refVec3);
const double vGamma = compAngle(m_refVec1, m_refVec2);
XcMatrix V;
V.fillCols(m_refVec1, m_refVec2, m_refVec3);
//
// iterate over all "o" atoms.
for (std::vector<XcVector>::const_iterator
atm1 = m_superLfCCoordList2.begin(),
super_end = m_superLfCCoordList2.end();
atm1 != super_end; ++atm1) {
//
// Reset candidate tX lists
t1_candidates.clear();
t2_candidates.clear();
t3_candidates.clear();
// Search for all a, b, c atoms
for (std::vector<XcVector>::const_iterator
atm2 = m_superLfCCoordList2.begin();
atm2 != super_end; ++atm2) {
//
// Get trial vector:
XcVector t ((*atm2) - (*atm1));
const double tNorm2 = t.squaredNorm();
//
// Compare against reference vectors. Check the norm of their
// differences to a tolerance.
if (fabs(tNorm2 - v1Norm2) < squaredTol) t1_candidates.push_back(t);
if (fabs(tNorm2 - v2Norm2) < squaredTol) t2_candidates.push_back(t);
if (fabs(tNorm2 - v3Norm2) < squaredTol) t3_candidates.push_back(t);
}
// Move to next candidate origin if any candidates are missing:
if (!t1_candidates.size() ||
!t2_candidates.size() ||
!t3_candidates.size()) {
continue;
}
#ifdef XTALCOMP_DEBUG
DEBUG_DIV;
DEBUG_STRING("Candidate transforms for offset:");
DEBUG_VECTOR(*atm1);
DEBUG_DIV;
DEBUG_STRING("Candidates for t1");
for (std::vector<XcVector>::const_iterator
it = t1_candidates.begin(),
it_end = t1_candidates.end();
it != it_end; ++it) {
DEBUG_VECTOR(*it);
}
DEBUG_STRING("Candidates for t2");
for (std::vector<XcVector>::const_iterator
it = t2_candidates.begin(),
it_end = t2_candidates.end();
it != it_end; ++it) {
DEBUG_VECTOR(*it);
}
DEBUG_STRING("Candidates for t3");
for (std::vector<XcVector>::const_iterator
it = t3_candidates.begin(),
it_end = t3_candidates.end();
it != it_end; ++it) {
DEBUG_VECTOR(*it);
}
#endif
// Search for transforms by comparing angles:
//
// Iterate over all t1 candidates
for (std::vector<XcVector>::const_iterator
t1 = t1_candidates.begin(),
t1_end = t1_candidates.end();
t1 != t1_end; ++t1) {
#ifdef XTALCOMP_DEBUG
DEBUG_STRING_VECTOR("Using t1", *t1);
#endif
// Iterate over all t2 candidates
for (std::vector<XcVector>::const_iterator
t2 = t2_candidates.begin(),
t2_end = t2_candidates.end();
t2 != t2_end; ++t2) {
#ifdef XTALCOMP_DEBUG
DEBUG_STRING_VECTOR("Using t2", *t2);
printf("Comparing angles: t1,t2= %f v1,v2= %f\n", compAngle(*t1,*t2), vGamma);
#endif
// Compare t1.t2 with v1.v2
if (fabs(compAngle(*t1,*t2) - vGamma) < m_angletol) {
// They match, so now search for a valid t3
for (std::vector<XcVector>::const_iterator
t3 = t3_candidates.begin(),
t3_end = t3_candidates.end();
t3 != t3_end; ++t3) {
#ifdef XTALCOMP_DEBUG
DEBUG_STRING_VECTOR("Using t3", *t3);
printf("Comparing angles: t1,t3= %f v1,v2= %f\n", compAngle(*t1,*t3), vBeta);
printf("Comparing angles: t2,t3= %f v1,v2= %f\n", compAngle(*t2,*t3), vAlpha);
#endif
// Compare t1.t3 with v1.v3 and t2.t3 with v2.v3
if (fabs(compAngle(*t1,*t3) - vBeta) < m_angletol &&
fabs(compAngle(*t2,*t3) - vAlpha) < m_angletol) {
// t1, t2, and t3 correspond to v1, v2, and v3
//
// Find rotation matrix that converts matrix T = (t1,
// t2, t3) into matrix V = (v1, v2, v3). In other
// words, let's find a matrix R such that:
//
// V = R T
//
// Thus, R = V T^-1
XcMatrix T;
T.fillCols(*t1, *t2, *t3);
const XcMatrix R (V * T.inverse());
// Build and store XcTransform:
XcTransform transform;
transform.setIdentity();
transform.rotate(R);
transform.translate(-(*atm1));
m_transforms.push_back(transform);
#ifdef XTALCOMP_DEBUG
DEBUG_STRING("Found transform:");
DEBUG_STRING("Translation:");
DEBUG_VECTOR(-(*atm1));
DEBUG_STRING("Rotation");
DEBUG_MATRIX(R);
DEBUG_STRING("transform: rot, trans");
DEBUG_MATRIX(transform.rotation());
DEBUG_VECTOR(transform.translation());
#endif
// Verify that this is a pure rot/ref matrix (allow a rather
// large fudge factor here -- the vectors may not match exactly)
//assert(fabs(fabs(R.determinant()) - 1.0) < .1);
// DL 20120119 Removed assertion: This is not worth dying over.
}
}
}
}
}
}
}
bool XtalComp::hasMoreTransforms() const
{
// Are there any more transforms?
if (m_transformsIndex < m_transforms.size()) {
return true;
}
#ifdef XTALCOMP_DEBUG
DEBUG_DIV;
DEBUG_STRING("No more transforms");
DEBUG_DIV;
#endif
return false;
}
void XtalComp::applyNextTransform()
{
#ifdef XTALCOMP_DEBUG
DEBUG_DIV;
printf("Applying transform %d of %d\n", m_transformsIndex+1, m_transforms.size());
#endif
// Get current transformation
assert (m_transformsIndex < m_transforms.size());
m_transform = m_transforms[m_transformsIndex];
#ifdef XTALCOMP_DEBUG
DEBUG_STRING("m_transform: rot, trans");
DEBUG_MATRIX(m_transform.rotation());
DEBUG_VECTOR(m_transform.translation());
#endif
buildTransformedXtal2();
// Update transform index
++m_transformsIndex;
#ifdef XTALCOMP_DEBUG
DEBUG_DIV;
DEBUG_STRING("Transform applied");
DEBUG_STRING("Transformed cmat:");
DEBUG_MATRIX(m_transformedCMat);
DEBUG_STRING("Transformed fmat:");
DEBUG_MATRIX(m_transformedFMat);
DEBUG_STRING("Transformed atoms (cart|frac):");
for (int i = 0; i < m_transformedTypes.size(); ++i) {
DEBUG_ATOMCF(m_transformedTypes.at(i), m_transformedCCoords.at(i),
m_transformedFCoords.at(i));
}
DEBUG_DIV;
#endif
}
void XtalComp::buildTransformedXtal2()
{
// Transform matrices
m_transformedCMat = m_transform.rotation() * m_rx2->cmat();
m_transformedFMat = m_transformedCMat.inverse();
// TODO isUnitary
//assert((m_transformedFMat * m_transformedCMat).isUnitary(1e-4));
// Reset transformed types to the original types
m_transformedTypes.resize(m_rx2->types().size());
copy(m_rx2->types().begin(), m_rx2->types().end(),
m_transformedTypes.begin());
// Set transformed coordinates:
//
// First we need to adjust the transformation matrix. The
// translation in m_transform is in cartesian units, but we want
// to transform the fractional coordinates, since we will need to
// expand them before converting to the final cartesian set. So we
// first make a copy of the current transform:
XcTransform fracTransform (m_transform);
// We will be multiplying fractional coords from the right, so
// append a frac -> cart conversion to the right hand side of the
// transform:
fracTransform.rotate(m_rx2->cmat());
// We then need to convert back into fractional coordinates after the translation
fracTransform.prerotate(m_transformedFMat);
#ifdef XTALCOMP_DEBUG
DEBUG_STRING("FracTransform: rot, trans");
DEBUG_MATRIX(fracTransform.rotation());
DEBUG_VECTOR(fracTransform.translation());
#endif
// Now to perform the transformations
m_transformedFCoords.clear();
m_transformedFCoords.reserve(m_rx2->fcoords().size());
for (std::vector<XcVector>::const_iterator
it = m_rx2->fcoords().begin(),
it_end = m_rx2->fcoords().end();
it != it_end; ++it) {
// Transform each fractional coordinate
m_transformedFCoords.push_back(fracTransform * (*it));
}
// Don't bother wrapping these into the new unit cell -- they will be
// translated in rx1's cell during the comparison.
// Convert to cartesian
m_transformedCCoords.clear();
m_transformedCCoords.reserve(m_transformedFCoords.size());
for (std::vector<XcVector>::iterator
it = m_transformedFCoords.begin(),
it_end = m_transformedFCoords.end();
it != it_end; ++it) {
m_transformedCCoords.push_back(m_transformedCMat * (*it));
}
}
void XtalComp::expandFractionalCoordinates(std::vector<unsigned int> *types,
std::vector<XcVector> *fcoords,
DuplicateMap *duplicateAtoms,
const XcMatrix &cmat,
const double tol)
{
// Wrap translated coordinates and expand translated coordinates
// (if an atom is very close to a cell boundary, place another
// atom at the opposite boundary for numerical stability)
//
// Definitions using fractional basis:
//
// 5------8
// /: /|
// / : / |
// / 2.../..7
// 3--/---6 /
// | / | /
// |/ |/
// 1------4
//
// Points:
// p1 = (0 0 0) | p5 = (1 1 0)
// p2 = (1 0 0) | p6 = (0 1 1)
// p3 = (0 1 0) | p7 = (1 0 1)
// p4 = (0 0 1) | p8 = (1 1 1)
//
// Vectors:
// v1 (a) = p2 - p1 = (1 0 0)
// v2 (b) = p3 - p1 = (0 1 0)
// v3 (c) = p4 - p1 = (0 0 1)
//
// Planes:
// #: normal point | points
// 1: v1 p1 | 1,3,4,6
// 2: v2 p1 | 1,2,4,7
// 3: v3 p1 | 1,2,3,5
// 4: v1 p8 | 2,5,7,8
// 5: v2 p8 | 3,5,6,8
// 6: v3 p8 | 4,6,7,8
//
// Edges:
// # | pts | planes ## | pts | planes
// 1 | 1,2 | 2,3 7 | 3,6 | 1,5
// 2 | 1,3 | 1,3 8 | 4,6 | 1,6
// 3 | 1,4 | 1,2 9 | 4,7 | 2,6
// 4 | 2,5 | 3,4 10 | 5,8 | 4,5
// 5 | 2,7 | 2,4 11 | 6,8 | 5,6
// 6 | 3,5 | 3,5 12 | 7.8 | 4,6
//
// Defining cell boundaries as planes, n ( r - r0 ) = 0,
// n = normal vector
// r0 = point in plane
// r = coordinate
//
// There are 6 planes, defined using v1, v2, v3 as possible n, and
// p1, p8 as possible r0:
//
// Plane 1:
// n = v1; r0 = p1
// Plane equ: v1 ( r - p1 ) = 0 = v1[0] * r[0]
// r[0] = 0
//
// Plane 2:
// n = v2; r0 = p1
// Plane equ: v2 ( r - p1 ) = 0 = v2[1] * r[1]
// r[1] = 0
//
// Plane 3:
// n = v3; r0 = p1
// Plane equ: v3 ( r - p1 ) = 0 = v3[2] * r[2]
// r[2] = 0
//
// Plane 4:
// n = v1; r0 = p8
// Plane equ: v1 ( r - p8 ) = 0 = v1[0] * r[0] - v1[1] * r0[0] or
// Plane equ: v1 ( r - p8 ) = 0 = v1[0] * r[0] - v1[0] * r0[0] ??
// r[0] - 1 = 0
//
// Plane 5:
// n = v2; r0 = p8
// Plane equ: v2 ( r - p8 ) = 0 = v2[1] * r[1] - v2[1] * r0[1]
// r[1] - 1 = 0
//
// Plane 6: