-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblock.cpp
executable file
·284 lines (261 loc) · 6.57 KB
/
block.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
#include"block.h"
#include<cmath>
#include<iostream>
using namespace std;
EulerAngles::EulerAngles()
{
//do nothing
}
EulerAngles::EulerAngles(double theta0)
//fixed theta0, and free rotation around the same theta
{
double phi0=random()*2.*M_PI/(RAND_MAX+1.0);
double st0=sin(theta0);
sp=st0*sin(phi0);
cp=sqrt(1.-sp*sp);
ct=cos(theta0)/cp;
st=cos(phi0)*st0/cp;
//phi=asin(sp); //no need
//theta=acos(ct);
}
EulerAngles::EulerAngles(double theta0,double phi0)
{
//theta=theta0;
//phi=phi0;
cp=cos(phi0),sp=sin(phi0);
ct=cos(theta0),st=sin(theta0);
}
EulerAngles operator +(EulerAngles a, EulerAngles b)
//transform to the original coordinates, add the rotation
{
EulerAngles o;
o.sp=b.cp*b.ct*a.sp+a.cp*b.sp;
o.cp=sqrt(1-o.sp*o.sp);
o.ct=(a.cp*b.cp*a.ct*b.ct -a.ct*a.sp*b.sp-b.cp*a.st*b.st)/o.cp;
o.st=(a.cp*b.cp*a.st*b.ct -a.st*a.sp*b.sp+b.cp*a.ct*b.st)/o.cp;
//o.phi=asin(o.sp); // no need to convert it back, we only do this after an photon scattered out of the sample
//o.theta=acos(o.ct);
return(o);
}
void EulerAngles::addTo(EulerAngles b)
//transform to the original coordinates, add the rotation
//need to speed up this
{
sp0=b.cp*b.ct*sp+cp*b.sp;
cp0=sqrt(1-sp0*sp0);
ct0=(cp*b.cp*ct*b.ct -ct*sp*b.sp-b.cp*st*b.st)/cp0;
st=(cp*b.cp*st*b.ct -st*sp*b.sp+b.cp*ct*b.st)/cp0;
ct=ct0;
cp=cp0;
sp=sp0;
}
double EulerAngles::get_theta(){
return acos(ct);
}
double EulerAngles::get_phi(){
return asin(sp);
}
Coordinates::Coordinates(){
//do nothing
}
Coordinates::Coordinates(double x0,double y0,double z0)
{
x=x0;
y=y0;
z=z0;
}
Coordinates::Coordinates(EulerAngles a)
{
x=a.ct;//cos(a.theta);
y=a.st;//sin(a.theta);
z=a.cp;//cos(a.phi);
y*=z;
x*=z;
z=a.sp;//sin(a.phi);
}
Coordinates::Coordinates(double theta0,double phi0)
{
x=cos(theta0);
y=sin(theta0);
z=cos(phi0);
y*=z;
x*=z;
z=sin(phi0);
}
double Coordinates::normxy()
{
return(x*x+ y*y);
}
void Coordinates::addTo(Coordinates b)
{
x += b.x;
y += b.y;
z += b.z;
}
Coordinates operator +(Coordinates a,Coordinates b)
{
Coordinates o=a;
o.x += b.x;
o.y += b.y;
o.z += b.z;
return (o);
}
Coordinates operator *(double a,Coordinates b)
{
Coordinates o=b;
o.x = a*b.x;
o.y = a*b.y;
o.z = a*b.z;
return (o);
}
ostream & operator << ( ostream& os,Coordinates b)
{
os<<b.x<<' '<<b.y<<' '<<b.z;
return os;
}
ostream & operator << ( ostream& os,EulerAngles o)
{
os<<o.get_theta()<<' '<<o.get_phi();
return os;
}
photon::photon()
//defaut sample diameter 0.25"
{
muPE= 1.16 * 2.33; // in cm^-1, PhotoElectric
muC= 0.15 * 2.33; // in cm^-1, compton
muTotal=1.31 * 2.33; // in cm^-1, total
// compton_ratio=(int) ( RAND_MAX*muC/muTotal);
imuTotal=1./muTotal;
sampleL=-2.54*0.5*0.25;
R2= (1+1e-8)*sampleL* sampleL;
}
photon::photon(double diameter)
{
muPE= 1.16 * 2.33; // in cm^-1, PhotoElectric
muC= 0.15 * 2.33; // in cm^-1, compton
muTotal=1.31 * 2.33; // in cm^-1, total
// compton_ratio=(int) ( RAND_MAX*muC/muTotal);
imuTotal=1./muTotal;
sampleL= -2.54*0.5*diameter;
R2= (1+1e-8)*sampleL* sampleL;
}
int photon::init()
{
scattered=0;
r.y= - sampleL- log(random()/(RAND_MAX+1.0)+1e-16)*imuTotal;
if (r.y>=sampleL) return(1);
r.x=r.z=0.;
//r=Coordinates(0.,r0,0.);
//o=EulerAngles(0.,0.);
o.cp=o.ct=1.;
o.sp=o.st=0.;
return(0);
}
int photon::initRef(double phi0)
{
scattered=0;
r0= log((RAND_MAX+1.0)/(random()+(long int) 1))*imuTotal;
cp0=cos(phi0);
r.y=r0*cp0;
if (r.y>=sampleL) return(1);
sp0=sin(phi0);
r.z=r0*sp0;
r.x=0.;
//r=Coordinates(0.,r0*cos(phi0),r0*sin(phi0));
o.ct=1.;o.st=0.;
o.cp=cp0;o.sp=sp0;
//o=EulerAngles(0.,phi0);
return(0);
}
int photon::initPass()
{//Pass through
scattered=0;
r.z=0.;
r.x=sampleL;
r.y=0.;
o.cp=1.;o.sp=0.;
o.ct=1.;o.st=0.;
//o=EulerAngles(0.,phi0);
return(0);
}
int photon::initGixos()
{//Pass through
scattered=0;//corritical angle 0.06903 degree, at Si/Ga interface 0.4132 angstrom
r.z=-0.06*M_PI/180*sampleL;
r.x=sampleL;
r.y=0.;
o.cp=sqrt(1.-0.00105*0.00105);o.sp=-0.00105;
o.ct=1.;o.st=0.;
//o=EulerAngles(0.,phi0);
return(0);
}
int photon::propagatePass(double theta0)
// pass through
{
r.addTo(log((RAND_MAX+1.0)/(random()+(long int) 1))*imuTotal*Coordinates(o)); //propagate according to exponential decay
if (r.normxy() > R2) return(-1); // scattered out of solid sample
// if(random()>compton_ratio) return(0);// not compton scattering
scattered++;
o.addTo(EulerAngles(theta0));
return(1);
}
int photon::initRefBuried(double sphi0)
{//Reflectivity, buried interface
scattered=0;
o.sp=sphi0;
o.cp=sqrt(1.-sphi0*sphi0);
r.z=sampleL*sphi0/o.cp;
r.x=sampleL;
r.y=0.;
o.ct=1.;o.st=0.;
//o=EulerAngles(0.,phi0);
return(0);
}
int photon::propagateGixos(double theta0)
// scattering process for reflectivity geometry
{
r.addTo(log((RAND_MAX+1.0)/(random()+0.01))*imuTotal*Coordinates(o)); //propagate according to exponential decay
// if(!( fabs(o.sp)<fabs(o.sp) +1.)) cout<<"before\n";
if (r.z<0.) {//gixos is at total reflection
r.z = fabs(r.z);
o.sp = fabs(o.sp);
}
if(!( fabs(o.sp)<fabs(o.sp) +1.)) cout<<"after\n";
if (r.normxy() > R2 ) return(-1); // scattered out of solid sample
// if(random()>compton_ratio) return(0);// not compton scattering
scattered++;
o.addTo(EulerAngles(theta0));
return(1);
}
int photon::propagateRef(double theta0)
// scattering process for reflectivity geometry
{
r.addTo(log((RAND_MAX+1.0)/(random()+(long int) 1))*imuTotal*Coordinates(o)); //propagate according to exponential decay
if (r.normxy() > R2 || r.z<0) return(-1); // scattered out of solid sample
// if(random()>compton_ratio) return(0);// not compton scattering
scattered++;
o.addTo(EulerAngles(theta0));
return(1);
}
int photon::propagate(double theta0)
{
r0=random()/(RAND_MAX+1.0);
if (r0<=muPEdr) {// absorbed
return(0);
}
r0 -= muPEdr;
if (r0<=muCdr) {// Compton scattered
scattered++;
o.addTo(EulerAngles(theta0));
}
r.addTo(rstep*Coordinates(o));
if (r.normxy() > R2) return(-1);
return(1);
}
double E_to_l(double en0)
//Energy to wavelength, KeV to angstrom
{
double e=1.602176e-19, h=6.626069e-34, c=2.997925e8;
return h*c/e*1.e7/en0;
}
//