-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.cpp
More file actions
303 lines (239 loc) · 9.79 KB
/
simulation.cpp
File metadata and controls
303 lines (239 loc) · 9.79 KB
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
#include "simulation.h"
simulation::simulation(ifstream &m_file) // constructor from the control file
{
int n_material, n_layer;
double wav_begin,wav_end,npoint,aoi;
string datafilename;
m_file>>wav_begin>>wav_end>>npoint;
this->wav_begin = wav_begin;
this->wav_end = wav_end;
this->npoint = npoint;
m_file>>aoi;
this->aoi = aoi;
for (int i=0;i<npoint;i++)
{
wav_vector.push_back(wav_begin+(wav_end-wav_begin)/(npoint-1)*i);
}
// Read material files
m_file>>n_material;
cout<<"number of materials is: "<<n_material<<endl;
for (int i=0;i<n_material;i++)
{
int variable;
m_file>>datafilename>>variable;
material* temp=new material(datafilename,variable); // class material constructer takes filename as an input
material_data.push_back(temp);
}
// Read layers info, the last layer is always semi-infinite substrate layer
m_file>>n_layer;
cout<<endl<<endl<<"number of layer is: "<<n_layer<<endl;
for (int i=0;i<n_layer-1;i++)
{
int idx;
double thickness;
int variable;
m_file>>idx>>thickness>>variable;
layer* temp = new layer(idx,thickness,variable); // idx is material indice, thickness is the only parameter for each layer
layer_data.push_back(temp);
cout<<"layer "<<i+1<< " has the thickness of "<<thickness<<" nm and use the material data in "
<<material_data[idx-1]->file<<endl;
}
int idx;
double thickness;
int variable;
m_file>>idx>>thickness>>variable;;
if (thickness!=-1) {cout<<"last layer is semi-infinite, put thickness as -1\n"<<endl;exit(1);}
layer* temp = new layer(idx,thickness,0);
layer_data.push_back(temp);
cout<<"layer "<<n_layer<< " is semi-infinite substrate and use the material data in "
<<material_data[idx-1]->file<<endl;
}
simulation::simulation(void){
}
void simulation::get_ref_trans(string filename, char s) // always have the last semi-infinite substrate layer
{
if (layer_data.size()<1) {cout<<"please add more than one layer"<<endl;return;}
complex <double> theta0 (aoi*M_PI/180.0,0.0);
complex <double> n0 (1.0,0.0);
complex <double> M[2][2];
complex <double> tmp[2][2];
complex <double> EYE (0,1);
complex <double> n1, n2;
complex <double> theta1, theta2;
complex <double> rs, ts;
complex <double> delta;
complex <double> ref, trans;
int nlayer = layer_data.size();
ofstream myfile (filename.c_str());
if (s=='s'){
for (int wav_idx=0;wav_idx<npoint;wav_idx++)
{
/* calculate the first layer matrix */
n1 = material_data[layer_data[0]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
rs = (n0*cos(theta0)-n1*cos(theta1))/(n0*cos(theta0)+n1*cos(theta1));
ts = (2.0*n0*cos(theta0))/(n0*cos(theta0)+n1*cos(theta1));
M[0][0] = 1.0/ts; M[0][1] = rs/ts;
M[1][0] = rs/ts; M[1][1] = 1.0/ts;
/* calculate the middle layers */
for (int layer_idx = 0;layer_idx<nlayer-1;layer_idx++)
{
n1 = material_data[layer_data[layer_idx]->idx]->get_nk(wav_vector[wav_idx]);
n2 = material_data[layer_data[layer_idx+1]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
theta2 = asin(n0*sin(theta0)/n2);
delta = 2.0*M_PI/n0*n1*cos(theta1)/wav_vector[wav_idx]*layer_data[layer_idx]->thickness;
rs = (n1*cos(theta1)-n2*cos(theta2))/(n1*cos(theta1)+n2*cos(theta2));
ts = (2.0*n1*cos(theta1))/(n1*cos(theta1)+n2*cos(theta2));
tmp[0][0] = M[0][0]*exp(-EYE*delta)/ts+M[0][1]*exp(EYE*delta)*rs/ts;
tmp[0][1] = M[0][0]*exp(-EYE*delta)*rs/ts+M[0][1]*exp(EYE*delta)/ts;
tmp[1][0] = M[1][0]*exp(-EYE*delta)/ts+M[1][1]*exp(EYE*delta)*rs/ts;
tmp[1][1] = M[1][0]*exp(-EYE*delta)*rs/ts+M[1][1]*exp(EYE*delta)/ts;
M[0][0] = tmp[0][0];
M[0][1] = tmp[0][1];
M[1][0] = tmp[1][0];
M[1][1] = tmp[1][1];
}
ref = M[1][0]/M[0][0];
trans = 1.0/M[0][0];
ref_vector_s.push_back(ref);
trans_vector_s.push_back(trans);
myfile<<wav_vector[wav_idx]<<"\t"<<norm(ref)<<"\t"<<norm(trans)<<endl;
}
}
else if (s=='p'){
for (int wav_idx=0;wav_idx<npoint;wav_idx++)
{
/* calculate the first layer matrix */
n1 = material_data[layer_data[0]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
rs = (n1*cos(theta0)-n0*cos(theta1))/(n1*cos(theta0)+n0*cos(theta1));
ts = (2.0*n0*cos(theta0))/(n1*cos(theta0)+n0*cos(theta1));
M[0][0] = 1.0/ts; M[0][1] = rs/ts;
M[1][0] = rs/ts; M[1][1] = 1.0/ts;
/* calculate the middle layers */
for (int layer_idx = 0;layer_idx<nlayer-1;layer_idx++)
{
n1 = material_data[layer_data[layer_idx]->idx]->get_nk(wav_vector[wav_idx]);
n2 = material_data[layer_data[layer_idx+1]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
theta2 = asin(n0*sin(theta0)/n2);
delta = 2.0*M_PI/n0*n1*cos(theta1)/wav_vector[wav_idx]*layer_data[layer_idx]->thickness;
rs = (n2*cos(theta1)-n1*cos(theta2))/(n2*cos(theta1)+n1*cos(theta2));
ts = (2.0*n1*cos(theta1))/(n2*cos(theta1)+n1*cos(theta2));
tmp[0][0] = M[0][0]*exp(-EYE*delta)/ts+M[0][1]*exp(EYE*delta)*rs/ts;
tmp[0][1] = M[0][0]*exp(-EYE*delta)*rs/ts+M[0][1]*exp(EYE*delta)/ts;
tmp[1][0] = M[1][0]*exp(-EYE*delta)/ts+M[1][1]*exp(EYE*delta)*rs/ts;
tmp[1][1] = M[1][0]*exp(-EYE*delta)*rs/ts+M[1][1]*exp(EYE*delta)/ts;
M[0][0] = tmp[0][0];
M[0][1] = tmp[0][1];
M[1][0] = tmp[1][0];
M[1][1] = tmp[1][1];
}
ref = M[1][0]/M[0][0];
trans = 1.0/M[0][0];
ref_vector_p.push_back(ref);
trans_vector_p.push_back(trans);
myfile<<wav_vector[wav_idx]<<"\t"<<norm(ref)<<"\t"<<norm(trans)<<endl;
}
}
else cout<<"Please select the correct polarization"<<endl;
myfile.close();
}
void simulation::get_ref_trans(char s) // always have the last semi-infinite substrate layer
{
if (layer_data.size()<1) {cout<<"please add more than one layer"<<endl;return;}
complex <double> theta0 (aoi*M_PI/180.0,0.0);
complex <double> n0 (1.0,0.0);
complex <double> M[2][2];
complex <double> tmp[2][2];
complex <double> EYE (0,1);
complex <double> n1, n2;
complex <double> theta1, theta2;
complex <double> rs, ts;
complex <double> delta;
complex <double> ref, trans;
int nlayer = layer_data.size();
if (s=='s'){
ref_vector_s.clear();
trans_vector_s.clear();
for (int wav_idx=0;wav_idx<npoint;wav_idx++)
{
/* calculate the first layer matrix */
n1 = material_data[layer_data[0]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
rs = (n0*cos(theta0)-n1*cos(theta1))/(n0*cos(theta0)+n1*cos(theta1));
ts = (2.0*n0*cos(theta0))/(n0*cos(theta0)+n1*cos(theta1));
M[0][0] = 1.0/ts; M[0][1] = rs/ts;
M[1][0] = rs/ts; M[1][1] = 1.0/ts;
/* calculate the middle layers */
for (int layer_idx = 0;layer_idx<nlayer-1;layer_idx++)
{
n1 = material_data[layer_data[layer_idx]->idx]->get_nk(wav_vector[wav_idx]);
n2 = material_data[layer_data[layer_idx+1]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
theta2 = asin(n0*sin(theta0)/n2);
delta = 2.0*M_PI/n0*n1*cos(theta1)/wav_vector[wav_idx]*layer_data[layer_idx]->thickness;
rs = (n1*cos(theta1)-n2*cos(theta2))/(n1*cos(theta1)+n2*cos(theta2));
ts = (2.0*n1*cos(theta1))/(n1*cos(theta1)+n2*cos(theta2));
tmp[0][0] = M[0][0]*exp(-EYE*delta)/ts+M[0][1]*exp(EYE*delta)*rs/ts;
tmp[0][1] = M[0][0]*exp(-EYE*delta)*rs/ts+M[0][1]*exp(EYE*delta)/ts;
tmp[1][0] = M[1][0]*exp(-EYE*delta)/ts+M[1][1]*exp(EYE*delta)*rs/ts;
tmp[1][1] = M[1][0]*exp(-EYE*delta)*rs/ts+M[1][1]*exp(EYE*delta)/ts;
M[0][0] = tmp[0][0];
M[0][1] = tmp[0][1];
M[1][0] = tmp[1][0];
M[1][1] = tmp[1][1];
}
ref = M[1][0]/M[0][0];
trans = 1.0/M[0][0];
ref_vector_s.push_back(ref);
trans_vector_s.push_back(trans);
}
}
else if (s=='p'){
ref_vector_p.clear();
trans_vector_p.clear();
for (int wav_idx=0;wav_idx<npoint;wav_idx++)
{
/* calculate the first layer matrix */
n1 = material_data[layer_data[0]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
rs = (n1*cos(theta0)-n0*cos(theta1))/(n1*cos(theta0)+n0*cos(theta1));
ts = (2.0*n0*cos(theta0))/(n1*cos(theta0)+n0*cos(theta1));
M[0][0] = 1.0/ts; M[0][1] = rs/ts;
M[1][0] = rs/ts; M[1][1] = 1.0/ts;
/* calculate the middle layers */
for (int layer_idx = 0;layer_idx<nlayer-1;layer_idx++)
{
n1 = material_data[layer_data[layer_idx]->idx]->get_nk(wav_vector[wav_idx]);
n2 = material_data[layer_data[layer_idx+1]->idx]->get_nk(wav_vector[wav_idx]);
theta1 = asin(n0*sin(theta0)/n1);
theta2 = asin(n0*sin(theta0)/n2);
delta = 2.0*M_PI/n0*n1*cos(theta1)/wav_vector[wav_idx]*layer_data[layer_idx]->thickness;
rs = (n2*cos(theta1)-n1*cos(theta2))/(n2*cos(theta1)+n1*cos(theta2));
ts = (2.0*n1*cos(theta1))/(n2*cos(theta1)+n1*cos(theta2));
tmp[0][0] = M[0][0]*exp(-EYE*delta)/ts+M[0][1]*exp(EYE*delta)*rs/ts;
tmp[0][1] = M[0][0]*exp(-EYE*delta)*rs/ts+M[0][1]*exp(EYE*delta)/ts;
tmp[1][0] = M[1][0]*exp(-EYE*delta)/ts+M[1][1]*exp(EYE*delta)*rs/ts;
tmp[1][1] = M[1][0]*exp(-EYE*delta)*rs/ts+M[1][1]*exp(EYE*delta)/ts;
M[0][0] = tmp[0][0];
M[0][1] = tmp[0][1];
M[1][0] = tmp[1][0];
M[1][1] = tmp[1][1];
}
ref = M[1][0]/M[0][0];
trans = 1.0/M[0][0];
ref_vector_p.push_back(ref);
trans_vector_p.push_back(trans);
}
}
else cout<<"Please select the correct polarization"<<endl;
}
void simulation::override_wav(int npoint,vector<double> wav_vector){
this->npoint = npoint;
this->wav_vector = wav_vector;
}
void simulation::override_aoi(double aoi){
this->aoi = aoi;
}