/
system.cpp
2597 lines (2138 loc) · 75.6 KB
/
system.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
#include "system.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdio.h>
#include "voro++.hh"
#include "string.h"
#include <chrono>
#include <pybind11/stl.h>
using namespace voro;
//-----------------------------------------------------
// Constructor, Destructor and Access functions
//-----------------------------------------------------
System::System(){
nop = 0;
ghost_nop = 0;
real_nop = 0;
triclinic = 0;
usecells = 0;
filter = 0;
maxclusterid = -1;
alpha = 1;
voronoiused = 0;
solidq = 6;
criteria = 0;
comparecriteria = 0;
neighbordistance = 0;
//set box with zeros
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
box[i][j] = 0.0;
}
}
}
System::~System(){
}
//-----------------------------------------------------
// Simulation box related methods
//-----------------------------------------------------
void System::assign_triclinic_params(vector<vector<double>> drot, vector<vector<double>> drotinv){
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
rot[i][j] = drot[i][j];
rotinv[i][j] = drotinv[i][j];
}
}
triclinic = 1;
}
vector<vector<double>> System::get_triclinic_params(){
vector<vector<double>> drot;
vector<double> dummydrot;
for(int i=0; i<3; i++){
dummydrot.clear();
for(int j=0; j<3; j++){
dummydrot.emplace_back(rot[i][j]);
}
drot.emplace_back(dummydrot);
}
return drot;
}
void System::sbox(vector<vector <double>> boxd) {
//this method will be redone to get a 3x3 box
//always. They will be then translated to the
//corresponding other boxes
double isum;
for(int i=0; i<3; i++){
isum = 0;
for(int j=0; j<3; j++){
box[i][j] = boxd[i][j];
isum += boxd[i][j]*boxd[i][j];
}
boxdims[i][0] = 0;
boxdims[i][1] = sqrt(isum);
}
boxx = boxdims[0][1] - boxdims[0][0];
boxy = boxdims[1][1] - boxdims[1][0];
boxz = boxdims[2][1] - boxdims[2][0];
}
vector<vector<double>> System::gbox(){
vector<vector<double>> qres;
vector<double> qd;
for(int i=0;i<3;i++){
qd.clear();
for(int j=0;j<3;j++){
qd.emplace_back(box[i][j]);
}
qres.emplace_back(qd);
}
return qres;
}
//-----------------------------------------------------
// Atom related methods
//-----------------------------------------------------
//this function allows for handling custom formats of atoms and so on
void System::set_atoms( vector<Atom> atomitos){
atoms.clear();
nop = atomitos.size();
atoms.reserve(nop);
atoms.assign(atomitos.begin(), atomitos.end());
//now assign ghost and real atoms
int tg = 0;
int tl = 0;
for(int i=0; i<nop; i++){
if(atoms[i].ghost==0){
tl++;
}
else{
tg++;
}
}
ghost_nop = tg;
real_nop = tl;
//cout<<"Assigned real "<<tl<<" ghost "<<tg<<endl;
//cout<<nop<<endl;
}
//this function allows for handling custom formats of atoms and so on
vector<Atom> System::get_atoms( ){
//here, we have to filter ghost atoms
vector<Atom> retatoms;
for(int i=0; i<real_nop; i++){
retatoms.emplace_back(atoms[i]);
}
return retatoms;
}
void System::add_atoms(vector<Atom> atomitos){
//check for ghost atoms
int tg = 0;
int tl = 0;
for(int i=0; i<atomitos.size(); i++){
if(atomitos[i].ghost==0){
tl++;
}
else{
tg++;
}
}
//if there is are no ghosts in system and list, just add and forget
if(ghost_nop==0){
if(tg==0){
for (int i=0; i<atomitos.size(); i++){
atoms.emplace_back(atomitos[i]);
}
}
}
else if (ghost_nop>0){
//now the atoms need to be reordered
vector<Atom> real_atoms;
vector<Atom> ghost_atoms;
for(int i=0; i<nop; i++){
if(atoms[i].ghost==0){
real_atoms.emplace_back(atoms[i]);
}
else{
ghost_atoms.emplace_back(atoms[i]);
}
}
//now also add new atoms to the list
for (int i=0; i<atomitos.size(); i++){
if(atomitos[i].ghost==0){
real_atoms.emplace_back(atomitos[i]);
}
else{
ghost_atoms.emplace_back(atomitos[i]);
}
}
//now put them all in the big list
atoms.clear();
for(int i=0; i<real_atoms.size(); i++){
atoms.emplace_back(real_atoms[i]);
}
for(int i=0; i<ghost_atoms.size(); i++){
atoms.emplace_back(ghost_atoms[i]);
}
//we can clear now
real_atoms.clear();
ghost_atoms.clear();
}
//update
ghost_nop = ghost_nop + tg;
real_nop = real_nop + tl;
nop = nop + atomitos.size();
}
vector<Atom> System::get_all_atoms( ){
//here, we have to filter ghost atoms
vector<Atom> retatoms;
for(int i=0; i<nop; i++){
retatoms.emplace_back(atoms[i]);
}
return retatoms;
}
Atom System::gatom(int i) { return atoms[i]; }
void System::satom(Atom atom1) {
int idd = atom1.loc;
atoms[idd] = atom1;
}
//----------------------------------------------------
// Neighbor methods
//----------------------------------------------------
double System::get_abs_distance(int ti ,int tj,double &diffx ,double &diffy,double &diffz){
double abs, ax, ay, az;
diffx = atoms[tj].posx - atoms[ti].posx;
diffy = atoms[tj].posy - atoms[ti].posy;
diffz = atoms[tj].posz - atoms[ti].posz;
if (triclinic == 1){
//convert to the triclinic system
ax = rotinv[0][0]*diffx + rotinv[0][1]*diffy + rotinv[0][2]*diffz;
ay = rotinv[1][0]*diffx + rotinv[1][1]*diffy + rotinv[1][2]*diffz;
az = rotinv[2][0]*diffx + rotinv[2][1]*diffy + rotinv[2][2]*diffz;
//scale to match the triclinic box size
diffx = ax*boxx;
diffy = ay*boxy;
diffz = az*boxz;
//now check pbc
//nearest image
if (diffx> boxx/2.0) {diffx-=boxx;};
if (diffx<-boxx/2.0) {diffx+=boxx;};
if (diffy> boxy/2.0) {diffy-=boxy;};
if (diffy<-boxy/2.0) {diffy+=boxy;};
if (diffz> boxz/2.0) {diffz-=boxz;};
if (diffz<-boxz/2.0) {diffz+=boxz;};
//now divide by box vals - scale down the size
diffx = diffx/boxx;
diffy = diffy/boxy;
diffz = diffz/boxz;
//now transform back to normal system
ax = rot[0][0]*diffx + rot[0][1]*diffy + rot[0][2]*diffz;
ay = rot[1][0]*diffx + rot[1][1]*diffy + rot[1][2]*diffz;
az = rot[2][0]*diffx + rot[2][1]*diffy + rot[2][2]*diffz;
//now assign to diffs and calculate distnace
diffx = ax;
diffy = ay;
diffz = az;
//finally distance
abs = sqrt(diffx*diffx + diffy*diffy + diffz*diffz);
}
else{
//nearest image
if (diffx> boxx/2.0) {diffx-=boxx;};
if (diffx<-boxx/2.0) {diffx+=boxx;};
if (diffy> boxy/2.0) {diffy-=boxy;};
if (diffy<-boxy/2.0) {diffy+=boxy;};
if (diffz> boxz/2.0) {diffz-=boxz;};
if (diffz<-boxz/2.0) {diffz+=boxz;};
abs = sqrt(diffx*diffx + diffy*diffy + diffz*diffz);
}
return abs;
}
//function for binding
double System::get_abs_distance(Atom atom1 , Atom atom2 ){
double abs, ax, ay, az;
double diffx = atom1.posx - atom2.posx;
double diffy = atom1.posy - atom2.posy;
double diffz = atom1.posz - atom2.posz;
if (triclinic == 1){
//convert to the triclinic system
ax = rotinv[0][0]*diffx + rotinv[0][1]*diffy + rotinv[0][2]*diffz;
ay = rotinv[1][0]*diffx + rotinv[1][1]*diffy + rotinv[1][2]*diffz;
az = rotinv[2][0]*diffx + rotinv[2][1]*diffy + rotinv[2][2]*diffz;
//scale to match the triclinic box size
diffx = ax*boxx;
diffy = ay*boxy;
diffz = az*boxz;
//now check pbc
//nearest image
if (diffx> boxx/2.0) {diffx-=boxx;};
if (diffx<-boxx/2.0) {diffx+=boxx;};
if (diffy> boxy/2.0) {diffy-=boxy;};
if (diffy<-boxy/2.0) {diffy+=boxy;};
if (diffz> boxz/2.0) {diffz-=boxz;};
if (diffz<-boxz/2.0) {diffz+=boxz;};
//now divide by box vals - scale down the size
diffx = diffx/boxx;
diffy = diffy/boxy;
diffz = diffz/boxz;
//now transform back to normal system
ax = rot[0][0]*diffx + rot[0][1]*diffy + rot[0][2]*diffz;
ay = rot[1][0]*diffx + rot[1][1]*diffy + rot[1][2]*diffz;
az = rot[2][0]*diffx + rot[2][1]*diffy + rot[2][2]*diffz;
//now assign to diffs and calculate distnace
diffx = ax;
diffy = ay;
diffz = az;
//finally distance
abs = sqrt(diffx*diffx + diffy*diffy + diffz*diffz);
}
else{
//nearest image
if (diffx> boxx/2.0) {diffx-=boxx;};
if (diffx<-boxx/2.0) {diffx+=boxx;};
if (diffy> boxy/2.0) {diffy-=boxy;};
if (diffy<-boxy/2.0) {diffy+=boxy;};
if (diffz> boxz/2.0) {diffz-=boxz;};
if (diffz<-boxz/2.0) {diffz+=boxz;};
abs = sqrt(diffx*diffx + diffy*diffy + diffz*diffz);
}
return abs;
}
//function for binding
vector<double> System::get_distance_vector(Atom atom1 , Atom atom2 ){
double ax, ay, az;
double diffx = atom1.posx - atom2.posx;
double diffy = atom1.posy - atom2.posy;
double diffz = atom1.posz - atom2.posz;
if (triclinic == 1){
//convert to the triclinic system
ax = rotinv[0][0]*diffx + rotinv[0][1]*diffy + rotinv[0][2]*diffz;
ay = rotinv[1][0]*diffx + rotinv[1][1]*diffy + rotinv[1][2]*diffz;
az = rotinv[2][0]*diffx + rotinv[2][1]*diffy + rotinv[2][2]*diffz;
double dummy;
//scale to match the triclinic box size
diffx = ax*boxx;
diffy = ay*boxy;
diffz = az*boxz;
//now check pbc
//nearest image
if (diffx> boxx/2.0) {diffx-=boxx;};
if (diffx<-boxx/2.0) {diffx+=boxx;};
if (diffy> boxy/2.0) {diffy-=boxy;};
if (diffy<-boxy/2.0) {diffy+=boxy;};
if (diffz> boxz/2.0) {diffz-=boxz;};
if (diffz<-boxz/2.0) {diffz+=boxz;};
//now divide by box vals - scale down the size
diffx = diffx/boxx;
diffy = diffy/boxy;
diffz = diffz/boxz;
//now transform back to normal system
ax = rot[0][0]*diffx + rot[0][1]*diffy + rot[0][2]*diffz;
ay = rot[1][0]*diffx + rot[1][1]*diffy + rot[1][2]*diffz;
az = rot[2][0]*diffx + rot[2][1]*diffy + rot[2][2]*diffz;
//now assign to diffs and calculate distnace
diffx = ax;
diffy = ay;
diffz = az;
}
else{
//nearest image
if (diffx> boxx/2.0) {diffx-=boxx;};
if (diffx<-boxx/2.0) {diffx+=boxx;};
if (diffy> boxy/2.0) {diffy-=boxy;};
if (diffy<-boxy/2.0) {diffy+=boxy;};
if (diffz> boxz/2.0) {diffz-=boxz;};
if (diffz<-boxz/2.0) {diffz+=boxz;};
}
vector<double> abs;
abs.emplace_back(diffx);
abs.emplace_back(diffy);
abs.emplace_back(diffz);
return abs;
}
void System::reset_all_neighbors(){
for (int ti = 0;ti<nop;ti++){
atoms[ti].n_neighbors=0;
atoms[ti].temp_neighbors.clear();
atoms[ti].condition = 0;
for (int tn = 0;tn<MAXNUMBEROFNEIGHBORS;tn++){
atoms[ti].neighbors[tn] = NILVALUE;
atoms[ti].neighbordist[tn] = -1.0;
}
}
}
void System::reset_main_neighbors(){
for (int ti = 0;ti<nop;ti++){
atoms[ti].n_neighbors=0;
atoms[ti].condition = 0;
//atoms[ti].temp_neighbors.clear();
for (int tn = 0;tn<MAXNUMBEROFNEIGHBORS;tn++){
atoms[ti].neighbors[tn] = NILVALUE;
atoms[ti].neighbordist[tn] = -1.0;
}
}
}
vector<double> System::get_pairdistances(){
vector<double> res;
double d;
double diffx,diffy,diffz;
for (int ti=0; ti<nop; ti++){
for (int tj=ti; tj<nop; tj++){
if(ti==tj) { continue; }
d = get_abs_distance(ti,tj,diffx,diffy,diffz);
res.emplace_back(d);
}
}
return res;
}
//function to create cell lists
//snmall function that returns cell index when provided with cx, cy, cz
int System::cell_index(int cx, int cy, int cz){
return cx*ny*nz + cy*nz + cz;
}
//if number of particles are small, use brute force
//if box is triclinic, use brute force
void System::set_up_cells(){
int si,sj,sk, maincell, subcell;
vector<int> cc;
//find of all find the number of cells in each direction
nx = boxx/neighbordistance;
ny = boxy/neighbordistance;
nz = boxz/neighbordistance;
//now use this to find length of cell in each direction
double lx = boxx/nx;
double ly = boxy/ny;
double lz = boxz/nz;
//find the total number of cells
total_cells = nx*ny*nz;
//create a vector of cells
cells = new cell[total_cells];
//now run over and for each cell create its neighbor cells
//all neighbor cells are also added
for(int i=0; i<nx; i++){
for(int j=0; j<ny; j++){
for(int k=0; k<nz; k++){
maincell = cell_index(i, j, k);
for(int si=i-1; si<=i+1; si++){
for(int sj=j-1; sj<=j+1; sj++){
for(int sk=k-1; sk<=k+1; sk++){
cc = cell_periodic(si, sj, sk);
subcell = cell_index(cc[0], cc[1], cc[2]);
//add this to the list of neighbors
cells[maincell].neighbor_cells.emplace_back(subcell);
}
}
}
}
}
}
int cx, cy, cz;
double dx, dy, dz;
double ddx, ddy, ddz;
int ind;
//now loop over all atoms and assign cells
for(int ti=0; ti<nop; ti++){
//calculate c indices for the atom
dx = atoms[ti].posx;
dy = atoms[ti].posy;
dz = atoms[ti].posz;
//now apply boxdims
if( abs(dx-0) < 1E-6)
dx = 0;
if( abs(dy-0) < 1E-6)
dy = 0;
if( abs(dz-0) < 1E-6)
dz = 0;
if (dx < 0) dx+=boxx;
else if (dx >= boxx) dx-=boxx;
if (dy < 0) dy+=boxy;
else if (dy >= boxy) dy-=boxy;
if (dz < 0) dz+=boxz;
else if (dz >= boxz) dz-=boxz;
//now find c vals
cx = dx/lx;
cy = dy/ly;
cz = dz/lz;
//now get cell index
ind = cell_index(cx, cy, cz);
//got cell index
//now add the atom to the corresponding cells
cells[ind].members.emplace_back(ti);
}
//end of loop - all cells, the member atoms and neighboring cells are added
}
vector<double> System::remap_atom(vector<double> pos){
//remap atom position into the box
//now apply boxdims
double dx = pos[0];
double dy = pos[1];
double dz = pos[2];
double ax, ay, az;
if (triclinic == 1){
//convert to the triclinic system
ax = rotinv[0][0]*dx + rotinv[0][1]*dy + rotinv[0][2]*dz;
ay = rotinv[1][0]*dx + rotinv[1][1]*dy + rotinv[1][2]*dz;
az = rotinv[2][0]*dx + rotinv[2][1]*dy + rotinv[2][2]*dz;
//scale to match the triclinic box size
dx = ax*boxx;
dy = ay*boxy;
dz = az*boxz;
//now check pbc
//nearest image
if (dx> boxx/2.0) {dx-=boxx;};
if (dx<-boxx/2.0) {dx+=boxx;};
if (dy> boxy/2.0) {dy-=boxy;};
if (dy<-boxy/2.0) {dy+=boxy;};
if (dz> boxz/2.0) {dz-=boxz;};
if (dz<-boxz/2.0) {dz+=boxz;};
//now divide by box vals - scale down the size
dx = dx/boxx;
dy = dy/boxy;
dz = dz/boxz;
//now transform back to normal system
ax = rot[0][0]*dx + rot[0][1]*dy + rot[0][2]*dz;
ay = rot[1][0]*dx + rot[1][1]*dy + rot[1][2]*dz;
az = rot[2][0]*dx + rot[2][1]*dy + rot[2][2]*dz;
//now assign to diffs and calculate distnace
dx = ax;
dy = ay;
dz = az;
}
else{
if (dx < 0) dx+=boxx;
else if (dx >= boxx) dx-=boxx;
if (dy < 0) dy+=boxy;
else if (dy >= boxy) dy-=boxy;
if (dz < 0) dz+=boxz;
else if (dz >= boxz) dz-=boxz;
}
vector<double> rpos;
rpos.emplace_back(dx);
rpos.emplace_back(dy);
rpos.emplace_back(dz);
return rpos;
}
vector<int> System::cell_periodic(int i, int j, int k){
vector<int> ci;
//apply periodic conditions
if (i<0) i = i + nx;
else if (i>nx-1) i = i -nx;
ci.emplace_back(i);
if (j<0) j = j + ny;
else if (j>ny-1) j = j -ny;
ci.emplace_back(j);
if (k<0) k = k + nz;
else if (k>nz-1) k = k -nz;
ci.emplace_back(k);
return ci;
}
//get all neighbor info but using cell lists
void System::get_all_neighbors_cells(){
voronoiused = 0;
double d;
double diffx,diffy,diffz;
double r,theta,phi;
int ti, tj;
//first create cells
set_up_cells();
int maincell, subcell;
//now loop to find distance
for(int i=0; i<total_cells; i++){
//now go over the neighbor cells
//for each member in cell i
for(int mi=0; mi<cells[i].members.size(); mi++){
//now go through the neighbors
ti = cells[i].members[mi];
for(int j=0 ; j<cells[i].neighbor_cells.size(); j++){
//loop through members of j
subcell = cells[i].neighbor_cells[j];
for(int mj=0; mj<cells[subcell].members.size(); mj++){
//now we have mj -> members/compare with
tj = cells[subcell].members[mj];
//compare ti and tj and add
if (ti < tj){
d = get_abs_distance(ti,tj,diffx,diffy,diffz);
if (d < neighbordistance){
if ((filter == 1) && (atoms[ti].type != atoms[tj].type)){
continue;
}
else if ((filter == 2) && (atoms[ti].type == atoms[tj].type)){
continue;
}
//process_neighbor(ti, tj);
atoms[ti].neighbors[atoms[ti].n_neighbors] = tj;
atoms[ti].neighbordist[atoms[ti].n_neighbors] = d;
//weight is set to 1.0, unless manually reset
atoms[ti].neighborweight[atoms[ti].n_neighbors] = 1.00;
atoms[ti].n_diffx[atoms[ti].n_neighbors] = diffx;
atoms[ti].n_diffy[atoms[ti].n_neighbors] = diffy;
atoms[ti].n_diffz[atoms[ti].n_neighbors] = diffz;
convert_to_spherical_coordinates(diffx, diffy, diffz, r, phi, theta);
atoms[ti].n_r[atoms[ti].n_neighbors] = r;
atoms[ti].n_phi[atoms[ti].n_neighbors] = phi;
atoms[ti].n_theta[atoms[ti].n_neighbors] = theta;
atoms[ti].n_neighbors += 1;
atoms[ti].cutoff = neighbordistance;
atoms[tj].neighbors[atoms[tj].n_neighbors] = ti;
atoms[tj].neighbordist[atoms[tj].n_neighbors] = d;
//weight is set to 1.0, unless manually reset
atoms[tj].neighborweight[atoms[tj].n_neighbors] = 1.00;
atoms[tj].n_diffx[atoms[tj].n_neighbors] = -diffx;
atoms[tj].n_diffy[atoms[tj].n_neighbors] = -diffy;
atoms[tj].n_diffz[atoms[tj].n_neighbors] = -diffz;
convert_to_spherical_coordinates(-diffx, -diffy, -diffz, r, phi, theta);
atoms[tj].n_r[atoms[tj].n_neighbors] = r;
atoms[tj].n_phi[atoms[tj].n_neighbors] = phi;
atoms[tj].n_theta[atoms[tj].n_neighbors] = theta;
atoms[tj].n_neighbors +=1;
atoms[tj].cutoff = neighbordistance;
}
}
}
}
}
}
}
void System::get_all_neighbors_normal(){
//reset voronoi flag
voronoiused = 0;
double d;
double diffx,diffy,diffz;
double r,theta,phi;
for (int ti=0; ti<nop; ti++){
for (int tj=ti; tj<nop; tj++){
if(ti==tj) { continue; }
d = get_abs_distance(ti,tj,diffx,diffy,diffz);
if (d < neighbordistance){
if ((filter == 1) && (atoms[ti].type != atoms[tj].type)){
continue;
}
else if ((filter == 2) && (atoms[ti].type == atoms[tj].type)){
continue;
}
//process_neighbor(ti, tj);
atoms[ti].neighbors[atoms[ti].n_neighbors] = tj;
atoms[ti].neighbordist[atoms[ti].n_neighbors] = d;
//weight is set to 1.0, unless manually reset
atoms[ti].neighborweight[atoms[ti].n_neighbors] = 1.00;
atoms[ti].n_diffx[atoms[ti].n_neighbors] = diffx;
atoms[ti].n_diffy[atoms[ti].n_neighbors] = diffy;
atoms[ti].n_diffz[atoms[ti].n_neighbors] = diffz;
convert_to_spherical_coordinates(diffx, diffy, diffz, r, phi, theta);
atoms[ti].n_r[atoms[ti].n_neighbors] = r;
atoms[ti].n_phi[atoms[ti].n_neighbors] = phi;
atoms[ti].n_theta[atoms[ti].n_neighbors] = theta;
atoms[ti].n_neighbors += 1;
atoms[ti].cutoff = neighbordistance;
atoms[tj].neighbors[atoms[tj].n_neighbors] = ti;
atoms[tj].neighbordist[atoms[tj].n_neighbors] = d;
//weight is set to 1.0, unless manually reset
atoms[tj].neighborweight[atoms[tj].n_neighbors] = 1.00;
atoms[tj].n_diffx[atoms[tj].n_neighbors] = -diffx;
atoms[tj].n_diffy[atoms[tj].n_neighbors] = -diffy;
atoms[tj].n_diffz[atoms[tj].n_neighbors] = -diffz;
convert_to_spherical_coordinates(-diffx, -diffy, -diffz, r, phi, theta);
atoms[tj].n_r[atoms[tj].n_neighbors] = r;
atoms[tj].n_phi[atoms[tj].n_neighbors] = phi;
atoms[tj].n_theta[atoms[tj].n_neighbors] = theta;
atoms[tj].n_neighbors +=1;
atoms[tj].cutoff = neighbordistance;
}
}
}
}
void System::process_neighbor(int ti, int tj){
/*
Calculate all info and add it to list
ti - loc of host atom
tj - loc of the neighbor
d - interatomic distance
*/
double d, diffx, diffy, diffz;
double r, phi, theta;
d = get_abs_distance(ti, tj, diffx,diffy,diffz);
atoms[ti].neighbors[atoms[ti].n_neighbors] = tj;
atoms[ti].neighbordist[atoms[ti].n_neighbors] = d;
//weight is set to 1.0, unless manually reset
atoms[ti].neighborweight[atoms[ti].n_neighbors] = 1.00;
atoms[ti].n_diffx[atoms[ti].n_neighbors] = diffx;
atoms[ti].n_diffy[atoms[ti].n_neighbors] = diffy;
atoms[ti].n_diffz[atoms[ti].n_neighbors] = diffz;
convert_to_spherical_coordinates(diffx, diffy, diffz, r, phi, theta);
atoms[ti].n_r[atoms[ti].n_neighbors] = r;
atoms[ti].n_phi[atoms[ti].n_neighbors] = phi;
atoms[ti].n_theta[atoms[ti].n_neighbors] = theta;
atoms[ti].n_neighbors += 1;
}
/*
To increase the speed of the other methods, we need some functions using cells and
otheriwse which adds atoms to the temp_neighbors list
*/
void System::get_temp_neighbors_brute(){
//reset voronoi flag
double d;
double diffx,diffy,diffz;
for (int ti=0; ti<nop; ti++){
for (int tj=ti; tj<nop; tj++){
if(ti==tj) { continue; }
d = get_abs_distance(ti,tj,diffx,diffy,diffz);
if (d <= neighbordistance){
datom x = {d, tj};
atoms[ti].temp_neighbors.emplace_back(x);
datom y = {d, ti};
atoms[tj].temp_neighbors.emplace_back(y);
}
}
}
}
/*
Cells should only be used when the system has a minimum size - in this case,
about 2000 atoms.
*/
void System::get_temp_neighbors_cells(){
//first create cells
set_up_cells();
int maincell, subcell;
int ti, tj;
double d;
double diffx,diffy,diffz;
//now loop to find distance
for(int i=0; i<total_cells; i++){
//now go over the neighbor cells
//for each member in cell i
for(int mi=0; mi<cells[i].members.size(); mi++){
//now go through the neighbors
ti = cells[i].members[mi];
for(int j=0 ; j<cells[i].neighbor_cells.size(); j++){
//loop through members of j
subcell = cells[i].neighbor_cells[j];
for(int mj=0; mj<cells[subcell].members.size(); mj++){
//now we have mj -> members/compare with
tj = cells[subcell].members[mj];
//compare ti and tj and add
if (ti < tj){
d = get_abs_distance(ti,tj,diffx,diffy,diffz);
if (d < neighbordistance){
datom x = {d, tj};
atoms[ti].temp_neighbors.emplace_back(x);
datom y = {d, ti};
atoms[tj].temp_neighbors.emplace_back(y);
}
}
}
}
}
}
}
int System::get_all_neighbors_bynumber(double prefactor, int nns, int assign){
/*
A new neighbor algorithm that finds a specified number of
neighbors for each atom. But ONLY TEMP neighbors
*/
//reset voronoi flag
voronoiused = 0;
double d, dcut;
double diffx,diffy,diffz;
double r,theta,phi;
int m, maxneighs, finished;
finished = 1;
vector<int> nids;
vector<double> dists, sorted_dists;
//double prefactor = 1.21;
double summ;
double boxvol;
//some guesswork here
//find the box volumes
if (triclinic==1){
double a1, a2, a3, b1, b2, b3, c1, c2, c3;
//rot is the cell vectors transposed
a1 = rot[0][0];
a2 = rot[1][0];
a3 = rot[2][0];
b1 = rot[0][1];
b2 = rot[1][1];
b3 = rot[2][1];
c1 = rot[0][2];
c2 = rot[1][2];
c3 = rot[2][2];
boxvol = c1*(a2*b3-a3*b2) - c2*(a1*b3-b1*a3) + c3*(a1*b2-a2*b1);
}
else{
boxvol = boxx*boxy*boxz;
}
//now find the volume per particle
double guessvol = boxvol/float(nop);
//guess the side of a cube that is occupied by an atom - this is a guess distance
double guessdist = cbrt(guessvol);
//now add some safe padding - this is the prefactor which we will read in
guessdist = prefactor*guessdist;
neighbordistance = guessdist;
if (usecells){
get_temp_neighbors_cells();
}
else{
get_temp_neighbors_brute();
}
for (int ti=0; ti<nop; ti++){
if (atoms[ti].temp_neighbors.size() < nns){
return 0;
}
sort(atoms[ti].temp_neighbors.begin(), atoms[ti].temp_neighbors.end(), by_dist());
if(assign == 1){
//assign the neighbors
for(int i=0; i<nns; i++){
int tj = atoms[ti].temp_neighbors[i].index;
process_neighbor(ti, tj);
}
}
finished = 1;
}
return finished;
}
void System::set_atom_cutoff(double factor){
/*
Reassign atom cutoff
*/
int nn;
double sum;
double avgdist;
for (int ti=0; ti<nop; ti++){
nn = atoms[ti].n_neighbors;
sum = 0;
for (int j=0; j<nn; j++){
sum += atoms[ti].neighbordist[j];
}
avgdist = sum/(double(nn));
atoms[ti].cutoff = factor*avgdist;
}
}
int System::get_neighbors_from_temp(int style){
/*
A new neighbor algorithm that finds a specified number of
neighbors for each atom.
*/
int finished = 1;
//reset neighbors
reset_main_neighbors();
if (style == 12){
for (int ti=0; ti<nop; ti++){
if (atoms[ti].temp_neighbors.size() > 11){
double ssum = 0;
for(int i=0 ; i<12; i++){
ssum += atoms[ti].temp_neighbors[i].dist;
}