forked from lingtikong/latgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
1115 lines (979 loc) · 38.5 KB
/
driver.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 "driver.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <time.h>
#include <math.h>
#include "version.h"
#include "common.h"
/* -----------------------------------------------------------------------------
* constructor to initialize
* -------------------------------------------------------------------------- */
Driver::Driver()
{
nx = ny = nz = natom = ntype = 0;
alat = 0.;
latt = NULL;
atpos = NULL;
attyp = NULL;
random = NULL;
element = NULL;
typeID = numtype = NULL;
xmap = ymap = zmap = umap = NULL;
flag_orient = 1;
memory = new Memory();
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) latvec[i][j] = 0.;
ShowVersion();
MainMenu();
return;
}
/* -----------------------------------------------------------------------------
* To show the main menu
* flag: 0, general case (main menu)
* >0, multilayer, polycrystal, or interstitials
* -------------------------------------------------------------------------- */
int Driver::ShowMenu(const int flag)
{
int ltype = 1;
char str[MAXLINE];
// print out the menu
printf("\n"); for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
if (flag>0) printf("Please select the lattice type for lattice: %c\n", flag+'A'-1);
else printf("Please select the lattice type of your system:\n");
printf(" 1. FCC/Diamond; | 4. A3B;\n");
printf(" 2. BCC; | 5. A2B;\n");
printf(" 3. HCP/Graphene; | 6. AB & ABXn;\n");
for (int i = 0; i < 31; ++i) printf("-"); printf("+");
for (int i = 0; i < 38; ++i) printf("-"); printf("\n");
if (flag != 0){
printf(" 7. User defined; | 0. Exit.\n");
} else {
printf(" 7. User defined; | 8. Multi-layer.\n");
for (int i = 0; i < 31; ++i) printf("-"); printf("+");
for (int i = 0; i < 38; ++i) printf("-"); printf("\n");
#ifdef Poly
printf(" 9. Xtal with interstitials; | 10. Polycrystals;\n");
for (int i = 0; i < 31; ++i) printf("-"); printf("+");
for (int i = 0; i < 38; ++i) printf("-"); printf("\n");
#endif
printf(" 0. Exit.\n");
}
for (int i = 0; i < 14; ++i) printf("-----");
printf("\nYour choice [1]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0) ltype = atoi(strtok(str," \t\n\r\f"));
printf("Your selection : %d\n", ltype);
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
switch (ltype){
case 1: latt = new FCC(); break;
case 2: latt = new BCC(); break;
case 3: latt = new HCP(); break;
case 4: latt = new A3B(); break;
case 5: latt = new A2B(); break;
case 6: latt = new AB(); break;
case 7: latt = new USER(); break;
case 8:
if (flag == 0){
flag_orient = 0;
FormLayers();
break;
}
#ifdef Poly
case 9:
if (flag == 0){
Interstitial();
flag_orient = 0;
break;
}
case 10: if (flag == 0){
flag_orient = 0;
PolyCrystal();
break;
}
#endif
default: exit(1);}
if (flag_orient > 0 && latt->initialized){
// re-orient the lattice
printf("Would you like to re-orient the unit cell to comply with LAMMPS? (y/n)[n]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0){
char *ptr = strtok(str," \t\n\r\f");
if (strcmp(ptr,"y")==0 || strcmp(ptr,"Y")==0) latt->OrientLattice();
}
}
return 8-ltype;
}
/* -----------------------------------------------------------------------------
* Driver to show the main menu
* -------------------------------------------------------------------------- */
void Driver::MainMenu()
{
if ( ShowMenu(0) > 0 ){
if (latt->initialized == 0) exit(2);
memory->create(name, strlen(latt->name)+1, "driver->MainMenu:name");
strcpy(name, latt->name);
alat = latt->alat;
latt->display();
generate();
}
return;
}
/* -----------------------------------------------------------------------------
* deconstructor to free memory
* -------------------------------------------------------------------------- */
Driver::~Driver()
{
memory->destroy(atpos);
memory->destroy(attyp);
memory->destroy(xmap);
memory->destroy(ymap);
memory->destroy(zmap);
memory->destroy(umap);
memory->destroy(name);
type2num.clear();
if (latt != NULL) delete latt;
if (element != NULL) delete element;
memory->destroy(typeID);
memory->destroy(numtype);
if (random != NULL) delete random;
delete memory;
}
/* -----------------------------------------------------------------------------
* method to generate atomic configurations
* -------------------------------------------------------------------------- */
void Driver::generate()
{
char str[MAXLINE];
int leading_dir = 1;
printf("\n"); for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
printf("Please input the extensions in x, y, and z directions: ");
if (count_words(fgets(str,MAXLINE,stdin)) < 3) exit(2);
nx = latt->inumeric(strtok(str, " \t\n\r\f"));
ny = latt->inumeric(strtok(NULL, " \t\n\r\f"));
nz = latt->inumeric(strtok(NULL, " \t\n\r\f"));
nucell = latt->nucell;
natom = nx*ny*nz*nucell;
if (natom < 1) exit(3);
printf("Your system would be %d x %d x %d with %d atoms.\n",nx,ny,nz,natom);
printf("Please indicate which direction should go faster (1: x; other: z)[1]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0) leading_dir = latt->inumeric(strtok(str, " \t\n\r\f"));
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
memory->create(atpos, natom, 3, "driver->generate:atpos");
memory->create(attyp, natom, "driver->generate:attyp");
memory->create(xmap, natom, "driver->generate:xmap");
memory->create(ymap, natom, "driver->generate:ymap");
memory->create(zmap, natom, "driver->generate:zmap");
memory->create(umap, natom, "driver->generate:umap");
int iatom = 0;
if ( leading_dir == 1){
for (int u = 0; u < latt->nucell; ++u)
for (int k = 0; k < nz; ++k)
for (int j = 0; j < ny; ++j)
for (int i = 0; i < nx; ++i){
xmap[iatom] = i;
ymap[iatom] = j;
zmap[iatom] = k;
umap[iatom] = u;
attyp[iatom] = latt->attyp[u];
atpos[iatom][0] = latt->atpos[u][0] + double(i);
atpos[iatom][1] = latt->atpos[u][1] + double(j);
atpos[iatom][2] = latt->atpos[u][2] + double(k);
++iatom;
}
} else {
for (int u = 0; u < latt->nucell; ++u)
for (int i = 0; i < nx; ++i)
for (int j = 0; j < ny; ++j)
for (int k = 0; k < nz; ++k){
xmap[iatom] = i;
ymap[iatom] = j;
zmap[iatom] = k;
umap[iatom] = u;
attyp[iatom] = latt->attyp[u];
atpos[iatom][0] = latt->atpos[u][0] + double(i);
atpos[iatom][1] = latt->atpos[u][1] + double(j);
atpos[iatom][2] = latt->atpos[u][2] + double(k);
++iatom;
}
}
// convert fractional coordinate to cartesian
double tmp[3];
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) latvec[i][j] = latt->latvec[i][j]*latt->alat;
for (int i = 0; i < natom; ++i){
for (int idim = 0; idim < 3; ++idim) tmp[idim] = atpos[i][idim];
atpos[i][0] = tmp[0]*latvec[0][0] + tmp[1]*latvec[1][0] + tmp[2]*latvec[2][0];
atpos[i][1] = tmp[0]*latvec[0][1] + tmp[1]*latvec[1][1] + tmp[2]*latvec[2][1];
atpos[i][2] = tmp[0]*latvec[0][2] + tmp[1]*latvec[1][2] + tmp[2]*latvec[2][2];
}
for (int i = 0; i < 3; ++i){
latvec[0][i] *= double(nx);
latvec[1][i] *= double(ny);
latvec[2][i] *= double(nz);
}
// find the total # of types and # of atoms for each type
typescan();
return;
}
/* -----------------------------------------------------------------------------
* method to find the total # of types and # of atoms for each type
* -------------------------------------------------------------------------- */
void Driver::typescan()
{
// allocate memory
int typmax = natom;
if (typeID != NULL) memory->destroy(typeID);
if (numtype!= NULL) memory->destroy(numtype);
memory->create(typeID, typmax, "driver->typescan:typeID");
memory->create(numtype, typmax, "driver->typescan:numtype");
for (int ip = 0; ip < typmax; ++ip) numtype[ip] = 0;
ntype = 0;
// now to identify the total number of types
for (int i = 0; i < natom; ++i){
int id = lookup(attyp[i]);
if (id < 0){
typeID[ntype] = attyp[i];
id = ntype++;
}
numtype[id]++;
}
return;
}
/* -----------------------------------------------------------------------------
* method to reset the atomic type ID
* -------------------------------------------------------------------------- */
void Driver::ResetTypeID()
{
char str[MAXLINE];
printf("\n"); for (int i = 0; i < 14; ++i) printf("=====");
printf("\nThere are %d atomic types in system, and their IDs are:\nIndex : ", ntype);
for (int ip = 0; ip < ntype; ++ip) printf(" %7d", ip+1); printf("\nTypeID: ");
for (int ip = 0; ip < ntype; ++ip) printf(" %7d", typeID[ip]); printf("\nNatTyp: ");
for (int ip = 0; ip < ntype; ++ip) printf(" %7d", numtype[ip]);
printf("\nPlease input the new type IDs in sequence, enter to skip: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0){
int newID[ntype], num=0;
char *ptr;
ptr = strtok(str, " \t\n\r\f");
while (ptr != NULL){
newID[num] = latt->inumeric(ptr);
if (++num >= ntype) break;
ptr = strtok(NULL, " \t\n\r\f");
}
if (num == ntype){
printf("\nThe new type IDs are:\nIndex : ");
for (int ip = 0; ip < ntype; ++ip) printf(" %7d", ip+1); printf("\nTypeID: ");
for (int ip = 0; ip < ntype; ++ip) printf(" %7d", newID[ip]);
printf("\nIs this what you want? (y/n)[y]: ");
int nw = count_words(fgets(str,MAXLINE,stdin));
char *flag = strtok(str, " \t\n\r\f");
if ( nw < 1 || ((strcmp(flag,"n") != 0) && (strcmp(flag,"N") != 0))){
for (int i = 0; i < natom; ++i){
int id = lookup(attyp[i]);
attyp[i] = newID[id];
}
typescan();
}
}
}
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
return;
}
/* -----------------------------------------------------------------------------
* method to map atomic type to real element
* -------------------------------------------------------------------------- */
void Driver::MapElement()
{
char str[MAXLINE];
printf("\n"); for (int i = 0; i < 14; ++i) printf("=====");
printf("\nThere are %d atomic types in system, and their IDs are:\nIndex : ", ntype);
for (int i = 0; i < ntype; ++i) printf(" %7d", i+1); printf("\nTypeID: ");
for (int i = 0; i < ntype; ++i) printf(" %7d", typeID[i]); printf("\nNatTyp: ");
for (int i = 0; i < ntype; ++i) printf(" %7d", numtype[i]);
printf("\nPlease input the element symbols in sequence, enter to skip: ");
if (count_words(fgets(str,MAXLINE,stdin)) >= ntype){
if (element == NULL) element = new ChemElements();
type2num.clear();
char *ptr = strtok(str, " \t\n\r\f");
for (int i = 0; i < ntype; ++i){
int ip = typeID[i];
type2num[ip] = element->Name2Num(ptr);
ptr = strtok(NULL, " \t\n\r\f");
}
printf("Element:");
for (int i = 0; i < ntype; ++i){
char ename[3];
int ip = typeID[i]; element->Num2Name(type2num[ip],ename);
printf(" %7s", ename);
} printf("\n");
}
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
return;
}
/* -----------------------------------------------------------------------------
* method to find the ID of the current atomic type
* -------------------------------------------------------------------------- */
int Driver::lookup(int ip)
{
for (int i = 0; i < ntype; ++i){if (ip == typeID[i]) return i;}
return -1;
}
/* -----------------------------------------------------------------------------
* method to write out atomic configuraton and mapping info
* -------------------------------------------------------------------------- */
void Driver::write(int format)
{
if (natom < 1) return;
FILE *fp;
char str[MAXLINE], *posfile, *mapfile, *lmpfile;
int flag_lmp_data = 1;
if (latvec[0][1]*latvec[0][1]+latvec[0][2]*latvec[0][2]+latvec[1][2]*latvec[1][2] > 1.e-6) flag_lmp_data = 0;
printf("\n"); for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
printf("Please input the filename of the output xyz file [atomcfg.xyz]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0){
int n = strlen(str) + 1;
posfile = new char[n];
strcpy(posfile, strtok(str," \t\n\r\f"));
} else {
posfile = new char[12];
strcpy(posfile, "atomcfg.xyz");
}
if (flag_lmp_data && format != 2){
printf("Please input the filename of the lammps atomic file [data.pos]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0){
int n = strlen(str) + 1;
lmpfile = new char[n];
strcpy(lmpfile, strtok(str," \t\n\r\f"));
} else {
lmpfile = new char[9];
strcpy(lmpfile, "data.pos");
}
}
if (format == 2){
printf("Please input the filename of the lammps atomic file [POSCAR]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0){
int n = strlen(str) + 1;
lmpfile = new char[n];
strcpy(lmpfile, strtok(str," \t\n\r\f"));
} else {
lmpfile = new char[7];
strcpy(lmpfile, "POSCAR");
}
}
if (xmap){
printf("Please input the filename of the output map file [map.in]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0){
int n = strlen(str) + 1;
mapfile = new char[n];
strcpy(mapfile, strtok(str," \t\n\r\f"));
} else {
mapfile = new char[7];
strcpy(mapfile, "map.in");
}
}
printf("\nThe atomic configuration will be written to: %s", posfile);
if (flag_lmp_data || format == 2) printf(" and %s\n", lmpfile); else printf("\n");
if (xmap) printf("The FFT map information will be written to file: %s\n", mapfile);
if (natom < 3){
printf("\nThe basis vectors of your system is:\n");
for (int i = 0; i < 3; ++i) printf(" %16.16e %16.16e %16.16e\n", latvec[i][0], latvec[i][1], latvec[i][2]);
}
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
// write the xyz position file
fp = fopen(posfile, "w");
fprintf(fp, "%d\n", natom);
fprintf(fp, "%s cell with dimension %dx%dx%d and a= %g\n", name, nx, ny, nz, alat);
int nr = 3;
if (natom < nr) nr = natom;
if (type2num.size() == ntype){
char ename[3];
for (int i = 0; i < nr; ++i){
int ip = attyp[i]; element->Num2Name(type2num[ip], ename);
fprintf(fp,"%2s %16.16e %16.16e %16.16e crystal_vector %d %16.16e %16.16e %16.16e\n", ename, atpos[i][0],
atpos[i][1], atpos[i][2], i+1, latvec[i][0], latvec[i][1], latvec[i][2]);
}
for (int i = nr; i < natom; ++i){
int ip = attyp[i]; element->Num2Name(type2num[ip], ename);
fprintf(fp,"%2s %16.16e %16.16e %16.16e\n", ename, atpos[i][0], atpos[i][1], atpos[i][2]);
}
} else {
for (int i = 0; i < nr; ++i){
fprintf(fp,"%d %16.16e %16.16e %16.16e crystal_vector %d %16.16e %16.16e %16.16e\n", attyp[i], atpos[i][0],
atpos[i][1], atpos[i][2], i+1, latvec[i][0], latvec[i][1], latvec[i][2]);
}
for (int i = nr; i < natom; ++i) fprintf(fp,"%d %16.16e %16.16e %16.16e\n", attyp[i], atpos[i][0], atpos[i][1], atpos[i][2]);
}
fclose(fp);
delete []posfile;
// write the lammps atomic style file
if (flag_lmp_data && format != 2){
fp = fopen(lmpfile,"w");
fprintf(fp, "# %s cell with dimension %d x %d x %d and a = %g\n", name, nx, ny, nz, alat);
fprintf(fp, "%10d atoms\n", natom);
fprintf(fp, "%10d atom types\n\n", ntype);
fprintf(fp, " 0. %20.14f xlo xhi\n", latvec[0][0]);
fprintf(fp, " 0. %20.14f ylo yhi\n", latvec[1][1]);
fprintf(fp, " 0. %20.14f zlo zhi\n", latvec[2][2]);
if ( latvec[1][0]*latvec[1][0] + latvec[2][0]*latvec[2][0] + latvec[2][1]*latvec[2][1] > 1.e-8 )
fprintf(fp, "%20.14f %20.14f %20.14f xy xz yz\n", latvec[1][0], latvec[2][0], latvec[2][1]);
// write atomic mass info (g/mol) if element mapping is done
if (type2num.size() == ntype){
fprintf(fp, "\nMasses\n\n");
for (std::map<int,int>::iterator it = type2num.begin(); it != type2num.end(); ++it){
int ip = (*it).first; int num = (*it).second;
fprintf(fp,"%d %g\n", ip, element->Num2Mass(num));
}
}
fprintf(fp, "\nAtoms\n\n");
if (format == 1){
for (int i = 0; i < natom; ++i)
fprintf(fp,"%d %d %20.14f %20.14f %20.14f\n", i+1, attyp[i], atpos[i][0], atpos[i][1], atpos[i][2]);
} else if (format == 3){
for (int i = 0; i < natom; ++i)
fprintf(fp,"%d %d 0. %20.14f %20.14f %20.14f\n", i+1, attyp[i], atpos[i][0], atpos[i][1], atpos[i][2]);
}
fclose(fp);
delete []lmpfile;
}
// write the poscar file
if (format == 2){
fp = fopen(lmpfile,"w");
fprintf(fp, "# %s cell with dimension %d x %d x %d and a = %g\n", name, nx, ny, nz, alat);
fprintf(fp, " 1.0 \n");
fprintf(fp, "%20.14f %20.14f %20.14f\n", latvec[0][0], latvec[0][1], latvec[0][2]);
fprintf(fp, "%20.14f %20.14f %20.14f\n", latvec[1][0], latvec[1][1], latvec[1][2]);
fprintf(fp, "%20.14f %20.14f %20.14f\n", latvec[2][0], latvec[2][1], latvec[2][2]);
// write atomic mass info (g/mol) if element mapping is done
char ename[3];
if (type2num.size() == ntype){
for (int ip = 0; ip < ntype; ++ip){
int id = typeID[ip];
element->Num2Name(type2num[id], ename);
fprintf(fp, " %s", ename);
}
fprintf(fp, "\n");
}
for (int ip = 0; ip < ntype; ++ip) fprintf(fp, " %d", numtype[ip]);
fprintf(fp, "\nCartesian\n");
for (int ip = 0; ip < ntype; ++ip){
for (int i = 0; i < natom; ++i){
int id = typeID[ip];
if (attyp[i] == id){
element->Num2Name(type2num[id], ename);
fprintf(fp,"%20.14f %20.14f %20.14f # %d %s\n", atpos[i][0], atpos[i][1], atpos[i][2], i+1, ename);
}
}
}
fclose(fp);
delete []lmpfile;
}
// write the map file, useful to fix_phonon only.
if (xmap){
fp = fopen(mapfile, "w");
fprintf(fp,"%d %d %d %d\n", nx, ny, nz, nucell);
fprintf(fp,"Map file for %dx%dx%d %s cell.\n",nx,ny,nz, name);
int ntm = nx * ny * nz * nucell; // Generally the same as natom
for (int i = 0; i < ntm; ++i)
fprintf(fp,"%d %d %d %d %d\n", xmap[i], ymap[i], zmap[i], umap[i], i+1);
fclose(fp);
delete []mapfile;
}
return;
}
/* -----------------------------------------------------------------------------
* method to modify the resultant model
* -------------------------------------------------------------------------- */
void Driver::modify()
{
if (natom < 1) return;
char str[MAXLINE];
int ncycle = 1;
while (ncycle){
int job=0;
// to display the menu for modification
printf("\n"); for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
printf("Please select the modification you want to do:\n");
printf(" 1. Create substitutional solid solution;\n");
printf(" 2. Reset atomic types;\n");
printf(" 3. Map atomic type to elements;\n");
if (ncycle == 1) printf(" 0. Nothing.\n");
else printf(" 0. Done.\n");
printf("Your choice [0]: ");
if (count_words(fgets(str,MAXLINE,stdin)) >0) job = latt->inumeric(strtok(str, " \t\n\r\f"));
printf("You selected: %d", job);
printf("\n"); for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
switch (job){
case 1: solidsol(); break;
case 2: ResetTypeID(); break;
case 3: MapElement(); break;
default: return;
}
ncycle++;
}
return;
}
/* -----------------------------------------------------------------------------
* private method to create substitutional solid solution
* -------------------------------------------------------------------------- */
void Driver::solidsol()
{
char str[MAXLINE];
int job = 0;
printf("\n"); for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
printf("Please select the region to create the solid solution:\n");
printf(" 1. Limit solid solution inside/outside a block region;\n");
printf(" 2. Limit solid solution inside/outside a spherical region;\n");
printf(" 3. Union of 1 & 2;\n");
printf(" 4. Intersection of 1 & 2;\n");
printf(" 5. All atoms in the box;\n");
printf(" 0. Return;\nYour choice [%d]: ", job);
if (count_words(fgets(str,MAXLINE,stdin)) > 0) job = latt->inumeric(strtok(str," \t\n\r\f"));
printf("You selected: %d\n", job);
if (job < 1 || job > 5){
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
return;
}
double block[6], cpos[3], radius;
int flag = 0, logand = 1, nsel, atsel[natom];
for (int id = 0; id < natom; ++id) atsel[id] = 1;
if (job==1 || job==3 || job==4) flag |= 1;
if (job==2 || job==3 || job==4) flag |= 2;
if (job==3) logand = 0;
if (flag & 1){ // block region needed
printf("\nThe basis vectors that define the current simulation box is:\n");
for (int idim = 0; idim < 3; ++idim) printf(" A%d: %lg %lg %lg\n", idim, latvec[idim][0], latvec[idim][1], latvec[idim][2]);
printf("Please input the bounds of the block region, in the format of `xlo xhi ylo yhi zlo zhi pos`,\n");
printf("where `pos` is either `i` or `o`, indicating inside or outside the block. If no limit in certain\n");
printf("direction, use `NULL`. For non-orthogonal box, this might not work well.\nPlease input them now: ");
if (count_words(fgets(str,MAXLINE,stdin)) >= 7){
char *ptr = strtok(str," \t\n\r\f");
for (int i = 0; i < 6; ++i){
if (strcmp(ptr, "NULL") == 0) block[i] = double(i%2)*latvec[i/2][i/2];
else block[i] = latt->numeric(ptr);
ptr = strtok(NULL, " \n\t\r\f");
}
int inside = 1;
if (strcmp(ptr,"o")==0 || strcmp(ptr,"O")==0){
inside = 0;
printf("\nAtoms outside block region bounded by [%g %g], [%g %g], [%g %g]\n",
block[0], block[1], block[2], block[3], block[4], block[5]);
} else {
printf("\nAtoms inside block region bounded by [%g %g], [%g %g], [%g %g]\n",
block[0], block[1], block[2], block[3], block[4], block[5]);
}
printf("will be used to create the substitutional solid solution.\n");
for (int i = 0; i < natom; ++i){
if (inside){
if (atpos[i][0] - block[0] < ZERO || atpos[i][0] - block[1] >-ZERO||
atpos[i][1] - block[2] < ZERO || atpos[i][1] - block[3] >-ZERO||
atpos[i][2] - block[4] < ZERO || atpos[i][2] - block[5] >-ZERO) atsel[i] = 0;
} else {
if (atpos[i][0] - block[0] >-ZERO && atpos[i][0] - block[1] < ZERO &&
atpos[i][1] - block[2] >-ZERO && atpos[i][1] - block[3] < ZERO &&
atpos[i][2] - block[4] >-ZERO && atpos[i][2] - block[5] < ZERO ) atsel[i] = 0;
}
}
} else {
printf("\nNo input read, operation terminated!\n");
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
}
}
nsel = 0;
for (int i = 0; i < natom; ++i) nsel += atsel[i];
if (flag & 2){ // spherical region needed
printf("\nThe basis vectors that define the current simulation box is:\n");
for (int idim = 0; idim < 3; ++idim) printf("A%d: %lg %lg %lg\n", idim, latvec[idim][0], latvec[idim][1], latvec[idim][2]);
printf("There are %d atoms in current selection. Please input the necessary parameters that define the\n", nsel);
printf("spherical region in the format of `x y z r pos`, where `pos` is either `i` or `o`, indicating\n");
printf("inside or outside the block. Please input them now: ");
if (count_words(fgets(str,MAXLINE,stdin)) >= 5){
char *ptr = strtok(str," \t\n\r\f");
for (int i = 0; i < 3; ++i){
cpos[i] = latt->numeric(ptr);
ptr = strtok(NULL, " \n\t\r\f");
}
radius = latt->numeric(ptr);
int inside = 1;
ptr = strtok(NULL, " \n\t\r\f");
if (strcmp(ptr,"o")==0 || strcmp(ptr,"O")==0){
inside = 0;
printf("\nAtoms outside sphere centered at [%g %g %g] with radius of %g\n", cpos[0], cpos[1], cpos[2], radius);
} else {
printf("\nAtoms inside sphere centered at [%g %g %g] with radius of %g\n", cpos[0], cpos[1], cpos[2], radius);
}
printf(" will be used to create the substitutional solid solution.\n");
radius *= radius;
for (int i = 0; i < natom; ++i){
double r2 = 0.;
for (int idim = 0; idim < 3; ++idim){
double dx = atpos[i][idim]-cpos[idim];
r2 += dx*dx;
}
if (logand){
if (inside==1 && r2>radius) atsel[i] = 0;
if (inside==0 && r2<radius) atsel[i] = 0;
} else {
if (inside==1 && r2<radius) atsel[i] = 1;
if (inside==0 && r2>radius) atsel[i] = 1;
}
}
}
}
nsel = 0;
int nsel_type[ntype];
for (int ip = 0; ip < ntype; ++ip) nsel_type[ip] = 0;
for (int i = 0; i < natom; ++i){
nsel += atsel[i];
int ip = lookup(attyp[i]);
nsel_type[ip] += atsel[i];
}
printf("\nTotal number of atoms in the system: %d\n", natom);
printf("Total number of atoms in selection : %d\n", nsel);
printf("Total number of atomimc types : %d\n", ntype);
printf("Atomic type number for each type :");
for (int ip = 0; ip < ntype; ++ip) printf(" %d", typeID[ip]);
printf("\nNumber of atoms for each type :");
for (int ip = 0; ip < ntype; ++ip) printf(" %d", numtype[ip]); printf("\n");
printf("\nNumber of atoms in selection for each type:");
for (int ip = 0; ip < ntype; ++ip) printf(" %d", nsel_type[ip]); printf("\n");
int ipsrc, idsrc=-1, numsub;
printf("\nPlease input the atomic type to be substituted: ");
while (count_words(fgets(str,MAXLINE,stdin)) < 1);
ipsrc = latt->inumeric(strtok(str, " \t\n\r\f"));
idsrc = lookup(ipsrc);
if (idsrc < 0){
printf("\nInput atomic type not found, operation terminated!\n");
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
return;
}
printf("Total # of atoms with type %d in selection is %d.\n", ipsrc, nsel_type[idsrc]);
if (nsel_type[idsrc] < 1){
printf("Not enough atoms to create substitutional solid solution.\n");
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
return;
}
double frac = -1.;
printf("Please input the fraction or total # of atoms to be replaced: ");
while (count_words(fgets(str,MAXLINE,stdin)) < 1);
frac = latt->numeric(strtok(str, " \t\n\r\f"));
if (frac <= 0. || int(frac) > nsel_type[idsrc]){
printf("Not enough atoms to create substitutional solid solution.\n");
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
return;
}
if (frac < 1.) numsub = MIN(int(frac*double(nsel_type[idsrc])+0.5), nsel_type[idsrc]);
else numsub = int(frac);
printf("There will be %d atoms with type %d to be replaced.\n", numsub, ipsrc);
if (numsub < 1) return;
int ipdes, iddes = 1;
do {
printf("Please input the atomic type to be assigned: ");
while (count_words(fgets(str,MAXLINE,stdin)) < 1);
ipdes = latt->inumeric(strtok(str, " \t\n\r\f"));
iddes = lookup(ipdes);
if (iddes >= 0){
iddes = -1;
printf("***Note: assigned type already exist, continue? (y/n)[y]: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0){
char *ptr = strtok(str, " \t\n\r\f");
if (strcmp(ptr,"n")==0 || strcmp(ptr,"N")==0) iddes = 1;
}
}
} while (iddes > 0);
// create random number generator if not created; current time as seed;
if (random == NULL){
time_t ctime;
time(&ctime);
random = new RanPark((int) ctime);
}
// now to generate the solid solution
int isub =0;
while (isub < numsub){
int i = int(random->uniform()*double(natom));
if (atsel[i] == 0) continue;
if (attyp[i] == ipsrc){
attyp[i] = ipdes;
isub++;
}
}
// reset type info
typescan();
// atomic type info after replacement
int newsel_type[ntype];
for (int i = 0; i < ntype; ++i) newsel_type[i] = 0;
nsel = 0;
for (int i = 0; i < natom; ++i){
nsel += atsel[i];
int ip = lookup(attyp[i]);
newsel_type[ip] += atsel[i];
}
printf("\nSystem info after creation of solid solution:\n");
printf(" Total number of atoms in the system: %d\n", natom);
printf(" Total number of atoms in selection : %d\n", nsel);
printf(" Total number of atomimc types : %d\n", ntype);
printf(" Atomic type number for each type :");
for (int i = 0; i < ntype; ++i) printf(" %d", typeID[i]);
printf("\n Number of atoms for each type :");
for (int i = 0; i < ntype; ++i) printf(" %d", numtype[i]); printf("\n");
printf("\n Number of atoms in selection for each type:");
for (int i = 0; i < ntype; ++i) printf(" %d", newsel_type[i]); printf("\n");
for (int i = 0; i < 14; ++i) printf("====="); printf("\n");
return;
}
/* -----------------------------------------------------------------------------
* Method to form multilayers
* -------------------------------------------------------------------------- */
void Driver::FormLayers()
{
int nlat = 1, idum;
int *mynx, *myny;
char str[MAXLINE];
printf("\n\n>>>>>>====== To form multilayers with multiple lattices ======<<<<<<\n");
printf("NOTE: The 3rd axis of these lattices must be perpendicular to the other 2!\n");
printf("\nPlease input the number of lattices in your multi-layer system: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0) nlat = atoi(strtok(str," \t\n\r\f"));
if (nlat < 1) return;
lattice *latts[nlat];
for (int i = 0; i < nlat; ++i){
if (! ShowMenu(i+1) ){nlat = i; break;}
latts[i] = latt;
latt = NULL;
}
for (int i = 0; i < nlat; ++i){
printf("\n>>>>>> Lattice info for lattice: %c - %s <<<<<", 'A'+i, latts[i]->name);
latts[i]->display();
if (latts[i]->perp_z == 0)
printf("\nWARNING: A3 is not perpendicular to A1 and A2, this lattice cannot be used to form layers!\n");
}
idum = 0;
printf("\nYou have defined %d lattices: ", nlat);
for (int i = 0; i < nlat; ++i) printf(" %c = %s;", 'A'+i, latts[i]->name); printf("\n");
memory->create(mynx, nlat, "mynx");
memory->create(myny, nlat, "myny");
for (int ilat = 0; ilat < nlat; ++ilat){
printf("Please input the lateral extensions (nx & ny) for lattice %c: ", 'A'+ilat);
while (1){
if ( count_words(fgets(str,MAXLINE,stdin)) == 2 ){
mynx[ilat] = latts[0]->inumeric(strtok(str, " \t\n\r\f"));
myny[ilat] = latts[0]->inumeric(strtok(NULL," \t\n\r\f"));
if (mynx[ilat] > 0 && myny[ilat] > 0) break;
}
}
}
nx = ny = nz = 0;
for (int j = 0; j < 3; ++j) latvec[0][j] = latvec[1][j] = 0.;
printf("\nThe surface vectors for each lattice will be:\n");
for (int i = 0; i < nlat; ++i){
printf(" %c: [", i+'A');
for (int j = 0; j < 3; ++j){
double xi = mynx[i]*latts[i]->latvec[0][j]*latts[i]->alat;
printf("%g ", xi);
latvec[0][j] += xi;
}
printf("] [");
for (int j = 0; j < 3; ++j){
double yi = myny[i]*latts[i]->latvec[1][j]*latts[i]->alat;
printf("%g ", yi);
latvec[1][j] += yi;
}
printf("]\n");
}
for (int j = 0; j < 3; ++j){
latvec[0][j] /= double(nlat);
latvec[1][j] /= double(nlat);
}
printf("Please input your desired surface vectors [%g %g %g, %g %g %g]: ",
latvec[0][0], latvec[0][1], latvec[0][2], latvec[1][0], latvec[1][1], latvec[1][2]);
if ( count_words(fgets(str,MAXLINE,stdin)) == 6 ){
char *ptr = strtok(str," \n\r\t\f");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j){ latvec[i][j] = latts[0]->numeric(ptr); ptr = strtok(NULL, " \n\r\t\f");}
}
double lx, ly, lx0=0., ly0=0.;
for (int j = 0; j < 3; ++j){
lx0 += latvec[0][j]*latvec[0][j];
ly0 += latvec[1][j]*latvec[1][j];
}
lx0 = sqrt(lx0); ly0 = sqrt(ly0);
for (int i = 0; i < nlat; ++i){
lx = ly = 0.;
for (int j = 0; j < 3; ++j){
double xi = mynx[i]*latts[i]->latvec[0][j]*latts[i]->alat;
double yi = myny[i]*latts[i]->latvec[1][j]*latts[i]->alat;
lx += xi*xi;
ly += yi*yi;
}
lx = sqrt(lx); ly = sqrt(ly);
printf("Lateral misfit for lattice %c is: [%lg %lg]\n", 'A'+i, (lx-lx0)/lx0, (ly-ly0)/ly0);
}
double H = 0.;
int zprev[nlat], ntprev[nlat], zflag = 0, iatom = 0;
for (int i = 0; i < nlat; ++i) zprev[i] = 0;
ntprev[0] = 0;
for (int i = 1; i < nlat; ++i) ntprev[i] = ntprev[i-1] + latts[i-1]->ntype;
char realized[MAXLINE]; strcpy(realized, "");
// prepare for map info if only one lattice is selected, suggesting layered structure
int fmap = 0;
if (nlat == 1) fmap = 1;
int istr = 0; // start layer ID
int first = 1, flag_no_interlayer = 0;
double Hlast = 0., Hfirst = 0., Hextra;
double shift[2];
shift[0] = shift[1] = 0.; // define shift of a lattice, in unit of basis vectors
printf("\nPlease input the layer sequences, for example, if you have two lattices: A and B,\n");
printf("and you want to have 4 layers A, 5 layers B and then 3 layers A, input A4 B5 A3.\n");
printf("If extra distance between different lattices is needed, just insert a number between\n");
printf("them, for example: A4 0.5 B5 0.4 A4 B5...; multiple numbers will add multiple distances.\n");
printf("If you want to form the 2nd A layers from its first layer in the unit cell, use\n");
printf("lower case 'a' instead of 'A'.\nOther options available:\n");
printf(" -s starting_layer : to define the starting layer of the next lattice; 0 is the 1st.\n");
printf(" -S xs ys : to define the relative shift of the next lattice.\n");
printf(" -z : to indicate not to add the next interlayer distance to the total height.\n");
printf("Now, input your sequences: ");
if (count_words(fgets(str,MAXLINE,stdin)) > 0) {
char *ptr; if (ptr = strchr(str,'#')) *ptr = '\0';
ptr = strtok(str," \n\r\t\f");
while (ptr != NULL){
zflag = 0; Hextra = 0.;
int ilat = ptr[0] - 'A';
if (ilat > nlat){ ilat = ptr[0] - 'a'; zflag = 1;}
if (ilat >=0 && ilat < nlat){
latt = latts[ilat];
strcat(realized," ");strcat(realized, ptr);
if (zflag) zprev[ilat] = 0;
zprev[ilat] += istr;
int nl_new = latt->inumeric(&ptr[1]);
int ntm_new = 0;
for (int i = 0; i < nl_new; ++i) ntm_new += latt->numlayer[(i+zprev[ilat])%latt->nlayer];
double Hbelow = latt->h[(latt->nlayer-1+zprev[ilat])%latt->nlayer];
if (first){
first = 0;
Hlast = 0.;
Hfirst = Hbelow;
H = -Hfirst;
}
if (nl_new > 0){
if (flag_no_interlayer) flag_no_interlayer = 0;
else H += MAX(Hlast,Hbelow);
}
ntm_new *= (mynx[ilat]*myny[ilat]);
natom += ntm_new;
memory->grow(atpos, natom, 3, "atpos");
memory->grow(attyp, natom, "attyp");
if (fmap){
memory->grow(xmap, natom, "xmap");
memory->grow(ymap, natom, "ymap");
memory->grow(zmap, natom, "zmap");
memory->grow(umap, natom, "umap");
nx = mynx[ilat];
ny = myny[ilat];
nucell = 0;
}
for (int k = 0; k < nl_new; ++k){
int il = (k+zprev[ilat])%latt->nlayer;
for (int ia = 0; ia < latt->nucell; ++ia){
if ( latt->layer[ia] == il ){
for (int i = 0; i < mynx[ilat]; ++i)
for (int j = 0; j < myny[ilat]; ++j){
atpos[iatom][0] = (double(i)+latt->atpos[ia][0]+shift[0])/double(mynx[ilat]);
atpos[iatom][1] = (double(j)+latt->atpos[ia][1]+shift[1])/double(myny[ilat]);
atpos[iatom][2] = H;
if (fmap){
xmap[iatom] = i;
ymap[iatom] = j;
zmap[iatom] = 0;
umap[iatom] = nucell;
}