-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmast_sey.cpp
1910 lines (1817 loc) · 64.7 KB
/
mast_sey.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 <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <random>
#include <ctime>
#include <chrono>
#include <string.h>
#define PI 3.14159265359
#define HA2EV 27.21138602
#define EV2HA 1/27.21138602
#define ANG2BOHR 1/0.5291772109
#define BOHR2ANG 0.5291772109
using namespace std;
string code_version = "4.1 (04-05-2022)";
vector<string> elems = {"XX","H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Nh","Fl","Mc","Lv","Ts","Og"};
random_device rd;
mt19937 mt(rd());
streambuf* orig_buf = cout.rdbuf();
vector<double> ie_arr, q_arr, de_arr;
vector<vector<array<double,2> > > inel_arr, elas_arr, ene_elf, jdos_arr;
vector<array<double,2> > elas_alloy_arr, dos_arr;
int dircos_algo = 0;
int spher_sec = 0;
char dirname[100];
vector <int> atnum;
vector <double> atcomp;
double mass, rho, ef, wf, u0, vol;
double ini_angle = 0.0;
double eb = 0.;
double erange = 1000.*EV2HA;
double ebeg = 0.0;
int egrid = 500, icsintgrid = 1000, qintgrid = 100, qdepgrid = 100, qdepomgrid = 1000;
bool lin_prep = false;
int mc_elec = 1000;
int qdep = 1;
string qdepname = "SPA";
string elfeps;
bool elsepa = false;
int es_nuc = 3, es_el = 1, es_ex = 1, es_muffin = 0, es_mcpol = 0;
bool emfp_only = false;
bool fermi_only = false;
bool prep = false;
bool preprange = false;
bool save_sumr = false;
bool save_qdep = false;
int sq_egrid, sq_qgrid;
double sq_qmax;
bool save_coords = false;
bool distrib = false;
bool noout = false;
bool classical_ang = false;
bool use_dos = false;
bool feg_dos = false;
bool notir = false;
bool test = false;
double cc = 137.;
string getTime();
void getInput(int argc, char** argv);
void printVersion(char** argv);
void readMaterialFile(string filename="material.in");;
void readEpsFile(string filename="eps.in");
vector<double> read1colFile(string filename="energies.in");
vector<array<double,2> > read2colFile(string filename="dos.in");
vector<vector<array<double,2> > > readScatteringFile(string filename);
void checkSumRules(bool save_sumr);
void printInput();
vector<double> range(double a, double b, int n);
vector<double> logrange(double a, double b, int n);
vector<double> getDeArr();
vector<array<double,2> > elas(double ie, int at=0, double comp=1.0);
vector<array<double,2> > inel(double ie);
vector<array<double,2> > int_elastic_ang(const vector<double> &xarr, const vector<double> &yarr, bool cumint=false);
vector<array<double,2> > int_inelastic_ene(double (*f)(double,double,int), double x0, double x1, int ndiv, double args, bool cumint=false);
vector<array<double,2> > int_inelastic_ang(double (*f)(double,double,int), double x0, double x1, int ndiv, double ie, double de, bool cumint=false);
vector<array<double,2> > cumintVect(const vector<array<double,2>> &xyarr);
array<double,3> f_rotdircos(array<double,3> uvw, double ang0, double ang1);
vector<array<double,2> > linterp2dline(double x0,double y0, const vector<double> &xarr, const vector<vector<array<double,2> > > &ytuparr);
double linterp(double x, const vector<array<double,2> > &xyarr, bool xfind=false);
double linterp2d(double x0,double y0, const vector<double> &xarr, const vector<vector<array<double,2> > > &ytuparr, bool total=false, bool xfind=false);
double linterp3d(double x0, double y0, double z0, const vector<double> &xarr, const vector<vector<vector<array<double,2> > > > &ytuparr, bool total, bool xfind);
double fzero(double (*f)(double,double,double), double x0, double x1, double ww, double qq, double tol=1e-6);
double jdos(double e, double de, double r);
double sspa_elf(double w, double q);
double spa_dispers(double w0, double w, double q);
double spa_elf(double w, double q);
double elfq(double q, double om, int dq=1);
double qIntFun(double om, double ie, int dummy);
void prepareJDOS(const vector<array<double,2> > &dos);
void saveVector(vector<double> arr, string filename);
void saveVector(vector<array<double,2> > arr, string filename);
void saveVector(vector<array<double,3> > arr, string filename);
void saveVector(vector<vector<double> > arr, string filename, int ncols=2);
void saveVector(vector<vector<array<double,2> > > arr, string filename);
void saveVector(vector<vector<vector<array<double,2> > > > arr, string filename);
void saveMFP(string filename);
void saveQdep(int n_ene, int n_q, double q_max);
void saveCoordVector(vector<vector<array<double,3> > > arr, vector<int> second, string filename);
void printVector(vector<double> &arr);
void printVector(vector<array<double,2> > &arr);
void printVector(vector<vector<array<double,2> > > &arr);
void marker(string num);
void print(string text);
void print(int num);
void print(double num);
void print(string text, int num);
void print(string text, double num);
void printProgress();
int printStars(int progr, int is, int size);
double rand01();
string checkName (string name);
class Electron
{
public:
double e,de,imfp,emfp,tmfp,s_ef,s_u0;
int secondary;
double angles[2];
double defl[2];
bool sc_type_el;
array<int,2> sc_type_elinel{0,0};
array<double,3> xyz{0.,0.,0.};
array<double,3> uvw{0.,0.,1.};
vector<array<double,3> > coord;
bool inside, dead;
Electron(double ie, double x=0.0, double y=0.0, double z=0.0, double u=0.0, double v=0.0, double w=1.0, int sec=0)
{
e = ie;
de = 0.0;
xyz[0] = x;
xyz[1] = y;
xyz[2] = z;
uvw[0] = u;
uvw[1] = v;
uvw[2] = w;
if (save_coords)
{
coord.push_back(xyz);
}
defl[0] = 0.0;
defl[1] = 0.0;
imfp = IMFP();
emfp = EMFP();
tmfp = emfp+imfp;
inside = true;
dead = false;
secondary = sec;
}
void travel_s()
{
double rn = random01();
double s = -(1./tmfp)*log(rn);
xyz[0]=xyz[0]+uvw[0]*s;
xyz[1]=xyz[1]+uvw[1]*s;
xyz[2]=xyz[2]+uvw[2]*s;
if (save_coords)
{
coord.push_back(xyz);
}
}
void dircos2ang()
{
angles[0]=acos(uvw[2]);
angles[1]=atan2(uvw[1],uvw[0]);
}
void determ_scatter()
{
double rn = random01();
if (rn < emfp/tmfp)
{
sc_type_el = true;
sc_type_elinel[0]++;
}
else {
sc_type_el = false;
sc_type_elinel[1]++;
}
}
bool scatter()
{
double rn = random01();
double rn2 = random01();
double rn3 = random01();
double rn4 = random01();
defl[1] = rn*2.*PI;
if (sc_type_el)
{
double tot_elast_int = linterp2d(e,-1,ie_arr,elas_arr,true);
defl[0] = linterp2d(e,rn2*tot_elast_int,ie_arr,elas_arr,false,true);
return false;
} else
{
double detot_inel_int = linterp2d(e,-1,ie_arr,inel_arr,true);
de = linterp2d(e,rn3*detot_inel_int,ie_arr,inel_arr,false,true);
if (classical_ang)
{
defl[0] = asin(sqrt(de/e));
}
else
{
vector<array<double,2> > int_inelas_ang = int_inelastic_ang(&elfq,0.0,PI/2.,100,e,de,true);
defl[0] = linterp(rn*int_inelas_ang[int_inelas_ang.size()-1][1],int_inelas_ang,true);
}
e = e-de;
died();
if (! dead)
{
imfp = IMFP();
emfp = EMFP();
tmfp = emfp+imfp;
}
if (use_dos)
{
if (feg_dos)
{
s_ef = fzero(&jdos,0.,ef,de,rn4);
} else {
double s_ef_int = linterp2d(de,-1,de_arr,jdos_arr,true);
s_ef = linterp2d(de,rn4*s_ef_int,de_arr,jdos_arr,false,true);
}
} else {
s_ef = ef;
}
return true;
}
}
double EMFP()
{
double dcs = linterp2d(e,-1,ie_arr,elas_arr,true);
return dcs/vol;
}
double IMFP()
{
return linterp2d(e,-1,ie_arr,inel_arr,true);
}
void escaped()
{
double rn = random01();
double t;
dircos2ang();
if (xyz[2]<0.0)
{
double beta = PI-angles[0];
if (e*cos(beta)*cos(beta) > u0)
{
t = 4.*sqrt(1.-u0/(e*cos(beta)*cos(beta)))/pow((1.+sqrt(1.-u0/(e*cos(beta)*cos(beta)))),2);
}
else
{
t = 0.;
}
if (rn < t)
{
inside = false;
xyz[0] = xyz[0]+sin(beta)*cos(angles[1])*xyz[2]/cos(beta);
xyz[1] = xyz[1]+sin(beta)*sin(angles[1])*xyz[2]/cos(beta);
xyz[2] = 0.0;
angles[0] = PI-asin(sin(beta)*sqrt(e/(e-u0)));
e = e-u0;
if (save_coords)
{
coord[coord.size()-1] = xyz;
coord.push_back({xyz[0]+1000.*sin(angles[0])*cos(angles[1]),xyz[1]+1000.*sin(angles[0])*sin(angles[1]),xyz[2]+1000.*cos(angles[0])});
}
}
else
{
if (notir)
{
dead = true;
} else {
uvw[2] = -1.*uvw[2];
xyz[2] = -1.*xyz[2];
if (save_coords)
{
coord[coord.size()-1] = {xyz[0],xyz[1],-1.*xyz[2]};
}
}
}
}
}
void died()
{
if (e<u0) { dead = true; }
}
double random01()
{
uniform_real_distribution<double> dist(0.0, 1.0);
return dist(mt);
}
};
int main(int argc, char** argv)
{
printVersion(argv);
readMaterialFile();
getInput(argc,argv);
readEpsFile();
if(noout) {
cout.rdbuf(NULL);
}
printInput();
if (prep)
{
readEpsFile();
if (lin_prep)
{
ie_arr = range(ebeg,erange,egrid);
}
else
{
ie_arr = logrange(ebeg,erange,egrid);
}
if (save_qdep)
{
saveQdep(sq_egrid,sq_qgrid,sq_qmax);
print("\n#\nSaved ELF(q,e) to file elf_qdep.out.");
exit(1);
}
checkSumRules(save_sumr);
print("# Starting mc_sey in \"prepare\" mode to get scattering properties");
printProgress();
int progress = 0;
for (size_t i = 0; i < ie_arr.size(); i++)
{
progress = printStars(progress,i,ie_arr.size());
elas_arr.push_back(elas(ie_arr[i],atnum[0],atcomp[0]));
for (size_t ia = 1; ia < atnum.size(); ia++)
{
elas_alloy_arr = elas(ie_arr[i],atnum[ia],atcomp[ia]);
for (int k = 0; k < 606; k++)
{
elas_arr[i][k][1] = elas_arr[i][k][1]+elas_alloy_arr[k][1];
}
}
elas_alloy_arr.clear();
if(!emfp_only)
{
inel_arr.push_back(inel(ie_arr[i]));
}
}
if (preprange)
{
string er_str = to_string(ebeg*HA2EV)+"_"+to_string(erange*HA2EV);
saveVector(ie_arr, "energies_"+er_str+".in");
saveVector(elas_arr, "elastic_"+er_str+".in");
if(!emfp_only)
{
saveVector(inel_arr, "inelastic_"+er_str+".in");
}
saveMFP("mfp_"+er_str+".plot");
cout << "\nFinished preparing input files, results in \"elastic_"+er_str+".in\", \"inelastic_"+er_str+".in\" and \"energies_"+er_str+".in\"" << endl;
} else
{
saveVector(ie_arr, "energies.in");
saveVector(elas_arr, "elastic.in");
if(!emfp_only)
{
saveVector(inel_arr, "inelastic.in");
}
saveMFP("mfp.plot");
cout << "\nFinished preparing input files, results in \"elastic.in\", \"inelastic.in\" and \"energies.in\"" << endl;
}
system("rm *dat elsepa.in");
return 0;
}
else
{
erange = erange+u0;
ie_arr = read1colFile();
if (erange>=ie_arr[ie_arr.size()-1])
{
cerr << "Incident energy higher than range generated in preparation step." << endl;
exit(1);
} else if (erange<=ie_arr[0])
{
cerr << "Incident energy lower than range generated in preparation step." << endl;
exit(1);
}
elas_arr = readScatteringFile("elastic.in");
inel_arr = readScatteringFile("inelastic.in");
if (use_dos && !feg_dos)
{
ifstream dosfile("jdos.in");
dos_arr = read2colFile("dos.in");
de_arr = getDeArr();
if (!dosfile)
{
print("# Could not find jdos.in - preparing...");
prepareJDOS(dos_arr);
print("# Prepared Joint DOS and saved to jdos.in\n#");
}
jdos_arr = readScatteringFile("jdos.in");
print("# Secondaries generated from joint DOS provided by user in jdos.in");
} else if (feg_dos){
print("# Secondaries generated from joint DOS of a Free Electron Gas");
}
if (spher_sec==1)
{
print("# Secondaries generated with spherical symmetry");
} else if (spher_sec==2) {
print("# Secondaries generated randomly");
}
if (classical_ang)
{
print("# Inelastic scattering angle approximated: asin(sqrt(de/e))");
print("#");
} else {
print("#");
}
print("# Input files read");
print("# Starting mc_sey in \"Simulation\" mode to get SEY\n#");
vector<Electron > elec_arr;
ini_angle = asin(sin(ini_angle)*sqrt((erange-u0)/erange));
int i, progress;
double s_ene;
array<double,3> s_uvw{0.0,0.0,0.0};
array<double,3> s_xyz{0.0,0.0,0.0};
i = -1;
progress = 0;
auto t1 = std::chrono::high_resolution_clock::now();
for (int n_e = 0; n_e < mc_elec; n_e++)
{
if (n_e==9)
{
auto t2 = std::chrono::high_resolution_clock::now();
auto ttt = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
cout << "# Estimated time of execution: " << setprecision(2) << (ttt*mc_elec/10./1000000.)/60. << " min\n#" << setprecision(17) << endl;
printProgress();
}
progress = printStars(progress,n_e,mc_elec);
elec_arr.push_back(Electron(erange,0.0,0.0,0.0,sin(ini_angle),0.0,cos(ini_angle),0));
while (i < (int)elec_arr.size()-1)
{
i++;
while (elec_arr[i].inside && ! elec_arr[i].dead)
{
elec_arr[i].travel_s();
elec_arr[i].escaped();
if (elec_arr[i].inside && ! elec_arr[i].dead)
{
elec_arr[i].determ_scatter();
if (elec_arr[i].scatter())
{
if (elec_arr[i].de-eb>u0 && eb>0.001)
{
s_ene = elec_arr[i].de-eb;
s_xyz[0] = elec_arr[i].xyz[0];
s_xyz[1] = elec_arr[i].xyz[1];
s_xyz[2] = elec_arr[i].xyz[2];
if (spher_sec==1) {
s_uvw = { sin(acos(2.*rand01()-1.))*cos(2.*rand01()*PI),
sin(acos(2.*rand01()-1.))*sin(2.*rand01()*PI),
cos(acos(2.*rand01()-1.)) };
} else if (spher_sec==2) {
s_uvw = { sin(rand01()*PI)*cos(2.*rand01()*PI),
sin(rand01()*PI)*sin(2.*rand01()*PI),
cos(rand01()*PI) };
} else {
s_uvw = f_rotdircos(elec_arr[i].uvw,asin(cos(elec_arr[i].defl[0])),elec_arr[i].defl[1]+PI);
}
elec_arr.push_back(Electron(s_ene,s_xyz[0],s_xyz[1],s_xyz[2],s_uvw[0],s_uvw[1],s_uvw[2],elec_arr[i].secondary+1));
}
else if (elec_arr[i].de+elec_arr[i].s_ef>u0)
{
s_ene = elec_arr[i].de+elec_arr[i].s_ef;
s_xyz[0] = elec_arr[i].xyz[0];
s_xyz[1] = elec_arr[i].xyz[1];
s_xyz[2] = elec_arr[i].xyz[2];
if (spher_sec==1) {
s_uvw = { sin(acos(2.*rand01()-1.))*cos(2.*rand01()*PI),
sin(acos(2.*rand01()-1.))*sin(2.*rand01()*PI),
cos(acos(2.*rand01()-1.)) };
} else if (spher_sec==2) {
s_uvw = { sin(rand01()*PI)*cos(2.*rand01()*PI),
sin(rand01()*PI)*sin(2.*rand01()*PI),
cos(rand01()*PI) };
} else {
s_uvw = f_rotdircos(elec_arr[i].uvw,asin(cos(elec_arr[i].defl[0])),elec_arr[i].defl[1]+PI);
}
elec_arr.push_back(Electron(s_ene,s_xyz[0],s_xyz[1],s_xyz[2],s_uvw[0],s_uvw[1],s_uvw[2],elec_arr[i].secondary+1));
}
}
elec_arr[i].uvw = f_rotdircos(elec_arr[i].uvw,elec_arr[i].defl[0],elec_arr[i].defl[1]);
}
}
}
}
vector<vector<array<double,3> > > coord_vec;
vector<int> secondary_ind;
vector<vector<double> > ene_distrib;
int em = 0, tem = 0, bsc = 0, nem = 0, d_prim = 0, e_bsc = 0;
for (size_t ei = 0; ei < elec_arr.size()-1; ei++)
{
if (save_coords)
{
coord_vec.push_back(elec_arr[ei].coord);
secondary_ind.push_back(elec_arr[ei].secondary);
}
if (! elec_arr[ei].inside)
{
em++;
if (elec_arr[ei].e < 50.*EV2HA)
{
tem++;
if (distrib)
{
ene_distrib.push_back({elec_arr[ei].e*HA2EV,elec_arr[ei].angles[0],elec_arr[ei].angles[1],elec_arr[ei].xyz[0],elec_arr[ei].xyz[1],(double)elec_arr[ei].secondary});
}
} else {
bsc++;
}
if (elec_arr[ei].secondary == 0 && elec_arr[ei].e > (erange-u0)-0.0001)
{
e_bsc++;
}
if (elec_arr[ei].secondary == 0 && elec_arr[ei].e <= (erange-u0)-0.0001)
{
d_prim++;
}
} else {
nem++;
}
}
if (save_coords) { saveCoordVector(coord_vec,secondary_ind,checkName("mc_coords.plot")); }
if (distrib)
{
saveVector(ene_distrib,checkName("mc_distrib.plot"),6);
}
print("\n#");
cout << fixed << setprecision(4) << setfill(' ');
cout << "# Energy[eV] SEY TrueSEY Bcksc DifPrim eBcksc" << endl;
if (noout) { cout.rdbuf(orig_buf); }
cout << setw(12) << (erange-u0)*HA2EV;
cout << setw(8) << (double)em/(double)mc_elec;
cout << setw(8) << (double)tem/(double)mc_elec;
cout << setw(8) << (double)bsc/(double)mc_elec;
cout << setw(8) << (double)d_prim/(double)mc_elec;
cout << setw(8) << (double)e_bsc/(double)mc_elec << endl;
return 0;
}
return 0;
}
string getTime()
{
time_t now = time(0);
tm *ltm = localtime(&now);
string dt = to_string(ltm->tm_mday)+"."+to_string(1+ltm->tm_mon)+"."+to_string(-100+ltm->tm_year)+" at "+to_string(ltm->tm_hour)+":"+to_string(ltm->tm_min)+":"+to_string(ltm->tm_sec);
return dt;
}
void getInput(int argc, char** argv)
{
if (argc == 1)
{
cerr << "No arguments specified, use \"-h\" flag for options.\nAt least the \"prepare\" keyword or \"-e\" flag is needed." << endl;
exit(1);
}
if (strcmp(argv[1], "prepare") == 0) { prep = true; }
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-e") == 0 && prep)
{
if (argc > i+3)
{
if (argv[i+1][0] != '-' && argv[i+2][0] != '-' && argv[i+3][0] != '-')
{
preprange = true;
ebeg = stod(argv[i+1])*EV2HA;
erange = stod(argv[i+2])*EV2HA;
egrid = stoi(argv[i+3]);
if (ebeg <= ef+1e-4)
{
cout << "# WARNING: Initial energy too low, setting to E_fermi = " << ef*HA2EV << "\n#" << endl;
ebeg = ef+1e-4;
}
} else if (argv[i+1][0] != '-' && argv[i+2][0] != '-' && argv[i+3][0] == '-')
{
ebeg = ef+1e-4;
erange = stod(argv[i+1])*EV2HA;
egrid = stoi(argv[i+2]);
if (erange <= ef+1e-4) {
cerr << "Energy range lower than E_fermi, stopping." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 2 or 3 arguments." << endl;
exit(1);
}
} else if (argc == i+3)
{
if (argv[i+1][0] != '-' && argv[i+2][0] != '-')
{
ebeg = ef+1e-4;
erange = stod(argv[i+1])*EV2HA;
egrid = stoi(argv[i+2]);
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 2 or 3 arguments." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 2 or 3 arguments." << endl;
exit(1);
}
} else if (strcmp(argv[i], "-e") == 0) {
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
erange = stod(argv[i+1])*EV2HA;
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-lin") == 0) { lin_prep = true; }
if (strcmp(argv[i], "-coord") == 0) { save_coords = true; }
if (strcmp(argv[i], "-distr") == 0) { distrib = true; }
if (strcmp(argv[i], "-sumr") == 0) { save_sumr = true; }
if (strcmp(argv[i], "-noout") == 0) { noout = true; }
if (strcmp(argv[i], "-noang") == 0) { classical_ang = true; }
if (strcmp(argv[i], "-sphsec") == 0) { spher_sec = 1; }
if (strcmp(argv[i], "-rndsec") == 0) { spher_sec = 2; }
if (strcmp(argv[i], "-notir") == 0) { notir = true; }
if (strcmp(argv[i], "-test") == 0) { test = true; }
if (strcmp(argv[i], "-emfp") == 0) { emfp_only = true; }
if (strcmp(argv[i], "SOLID") == 0) { es_muffin = 1; }
if (strcmp(argv[i], "LDA") == 0) { es_mcpol = 2; }
if (strcmp(argv[i], "-dircos") == 0)
{
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
dircos_algo = stoi(argv[i+1]);
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-dos") == 0)
{
use_dos = true;
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
if (strcmp(argv[i+1], "FEG") == 0)
{
feg_dos = true;
}
}
}
}
if (strcmp(argv[i], "-i") == 0)
{
if (argc > i+2)
{
if (argv[i+1][0] != '-' && argv[i+2][0] != '-')
{
icsintgrid = stoi(argv[i+1]);
qintgrid = stoi(argv[i+2]);
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 2 arguments." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 2 arguments." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-q") == 0)
{
if (argc > i+2)
{
if (argv[i+1][0] != '-' && argv[i+2][0] != '-')
{
qdepgrid = stoi(argv[i+1]);
qdepomgrid = stoi(argv[i+2]);
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 2 arguments." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 2 arguments." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-m") == 0)
{
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
mc_elec = stoi(argv[i+1]);
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
if (mc_elec<1000)
{
cerr << mc_elec << " MC e-'s is too few, results would make no sense." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-core") == 0)
{
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
eb = stod(argv[i+1])*EV2HA;
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-pa") == 0)
{
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
ini_angle = stod(argv[i+1])*PI/180.;
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-saveq") == 0)
{
save_qdep = true;
if (argc > i+3)
{
if (argv[i+1][0] != '-' && argv[i+2][0] != '-' && argv[i+3][0] != '-')
{
sq_egrid = stoi(argv[i+1]);
sq_qgrid = stoi(argv[i+2]);
sq_qmax = stod(argv[i+3]);
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 3 arguments." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 3 arguments." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-qdep") == 0)
{
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
qdepname=argv[i+1];
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
if (strcmp(argv[i+1], "CUSTOM") == 0)
{
qdep = 0;
} else if (strcmp(argv[i+1], "DFT") == 0)
{
qdep = 0;
} else if (strcmp(argv[i+1], "SPA") == 0)
{
qdep = 1;
} else if (strcmp(argv[i+1], "SSPA") == 0)
{
qdep = 2;
} else if (strcmp(argv[i+1], "FPA") == 0)
{
qdep = 3;
} else
{
cout << "# WARNING: option " << argv[i+1] << " for qdep not known, using SPA." << endl;
qdep = 1;
}
}
if (strcmp(argv[i], "-elastic") == 0)
{
elsepa = true;
if (argc > i+3)
{
if (argv[i+1][0] != '-' && argv[i+2][0] != '-' && argv[i+3][0] != '-')
{
if (strcmp(argv[i+1], "P") == 0)
{
es_nuc = 1;
} else if (strcmp(argv[i+1], "U") == 0)
{
es_nuc = 2;
} else if (strcmp(argv[i+1], "F") == 0)
{
es_nuc = 3;
} else if (strcmp(argv[i+1], "UU") == 0)
{
es_nuc = 4;
} else if (strcmp(argv[i+1], "radial") == 0)
{
elsepa = false;
} else {
cout << "# WARNING: option " << argv[i+1] << " for elsepa nucl. not known, using F." << endl;
}
if (strcmp(argv[i+2], "TFM") == 0)
{
es_el = 1;
} else if (strcmp(argv[i+2], "TFD") == 0)
{
es_el = 2;
} else if (strcmp(argv[i+2], "DHFS") == 0)
{
es_el = 3;
} else if (strcmp(argv[i+2], "DF") == 0)
{
es_el = 4;
} else {
cout << "# WARNING: option " << argv[i+2] << " for elsepa elec. not known, using TFM." << endl;
}
if (strcmp(argv[i+3], "NO") == 0)
{
es_ex = 0;
} else if (strcmp(argv[i+3], "FM") == 0)
{
es_ex = 1;
} else if (strcmp(argv[i+3], "TF") == 0)
{
es_ex = 2;
} else if (strcmp(argv[i+3], "RT") == 0)
{
es_ex = 3;
} else {
cout << "# WARNING: option " << argv[i+3] << " for exch. not known, using FM." << endl;
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 3 arguments." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 3 arguments." << endl;
exit(1);
}
}
if (strcmp(argv[i], "-radial") == 0)
{
if (argc > i+1)
{
if (argv[i+1][0] != '-')
{
if (strcmp(argv[i+1], "DHFS") == 0)
{
es_el = 1;
} else if (strcmp(argv[i+1], "TFM") == 0)
{
es_el = 2;
} else {
cout << "# WARNING: option " << argv[i+1] << " for radial not known, using DHFS." << endl;
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
} else {
cerr << "Too few arguments for " << argv[i] << ", needs 1 argument." << endl;
exit(1);
}
}
}
}
void printVersion(char** argv)
{
if (strcmp(argv[1], "-v") == 0)
{
cout << "MAterials Simulation Toolkit for Secondary Electron Emission (MAST-SEY)" << endl;
cout << "Cite as: Comput. Mater. Sci. 193 (2021), 110281 (https://doi.org/10.1016/j.commatsci.2021.110281)" << endl;
cerr << "(c) 2021 Maciej P. Polak ([email protected]) & Dane Morgan\n" << endl;
cout << "Code version "<< code_version << endl;
exit(0);
}
else if (strcmp(argv[1], "-h") == 0)
{
cout << "MAterials Simulation Toolkit for Secondary Electron Emission (MAST-SEY)" << endl;
cout << "Cite as: Comput. Mater. Sci. 193 (2021), 110281 (https://doi.org/10.1016/j.commatsci.2021.110281)" << endl;
cout << "(c) 2021 Maciej P. Polak ([email protected]) & Dane Morgan\n" << endl;
cout << "\nInput options:\n" << endl;
cout << "\"prepare\" as first argument will run input preparation from \"eps/elf.in\" and \"material.in\"" << endl;
cout << "otherwise, the \"simulate\" version will be executed" << endl;
cout << "\n\"prepare\" options:" << endl;
cout << "-e [iniE(eV,optional) range(eV) grid] energy range and grid (def: 1000 500)" << endl;
cout << "-lin generate the energy grid on a linear scale (default is logarithmic)" << endl;
cout << "-i [ICS q-int] grids for ICS and q integration (def: 1000 100)" << endl;
cout << "-qdep [SPA/SSPA/CUSTOM] specify type of q-dependence of ELF (def: SPA)" << endl;
cout << "-sumr output sum rules for plotting" << endl;
cout << "-saveq [E_grid q_grid q_max] save q-dependence for plotting" << endl;
cout << "-elastic [nuclear electron exchange (SOLID LDA opt.)] models to use in elastic scattering (def: F TFM FM)" << endl;
cout << " nuclear: [P]oint/[U]niform/[F]ermi" << endl;
cout << " electron: [TFM]Thomas–Fermi–Moliere/[TFD]Thomas-Fermi-Dirac/[DHFS]Dirac–Hartree–Fock–Slater/[DF]Dirac-Fock" << endl;
cout << " exchange: [NO]/[FM]Furness–McCarthy/[TF]Thomas-Fermi/[RT]Riley–Truhlar" << endl;
cout << " (optional): [SOLID] muffin-tin model potential" << endl;
cout << " (optional): [LDA] LDA correlation–polarization potential model" << endl;
cout << "\n\"simulate\" options:" << endl;
cout << "-e [incident_energy(eV)] energy of incident energy" << endl;
cout << "-m [number_of_e-] number of incident electrons (def: 1000)" << endl;
cout << "-core [energy(eV)] allow secondaries to come from bound states" << endl;
cout << "-dos [FEG (optional)] generate secondaries from joint DOS from prepared \"jdos.in\"" << endl;
cout << " or from parabolic free electron gas approximation (FEG)" << endl;
cout << "-pa [angle(deg)] angle of incident electrons with respect to surface normal" << endl;
cout << "-coord save travel paths of e-" << endl;
cout << "-distr save distribution of secondaries" << endl;
cout << "-noang use classical approach to inelastic angle scattering" << endl;
cout << "-noout supress all output" << endl;
cout << "\n-v display version of the code" << endl;
cout << "-h this message\n" << endl;
cout << "\nPlease be careful when giving input arguments, there is no extensive input checks" << endl;
cout << "Example executions:" << endl;
cout << "./mast_sey prepare -e 700 1000 -i 200 100 -elastic F TFD TF" << endl;
cout << "./mast_sey -e 350 -m 10000" << endl;
exit(0);
}
}
void readMaterialFile(string filename)
{
ifstream infile(filename);
if(!infile)
{
cerr << "MAterials Simulation Toolkit for Secondary Electron Emission (MAST-SEY)" << endl;
cerr << "Cite as: Comput. Mater. Sci. 193 (2021), 110281 (https://doi.org/10.1016/j.commatsci.2021.110281)" << endl;
cerr << "(c) 2021 Maciej P. Polak ([email protected]) & Dane Morgan\n" << endl;
cerr << "Cannot open obligatory file " << filename << endl;
cerr << "The file should have, line by line, the following information:" << endl;
cerr << "Atomic Number, Unit Cell Volume [A^3], Fermi energy [eV], Work Function [eV]\n" << endl;
exit(1);