-
Notifications
You must be signed in to change notification settings - Fork 0
/
example3.c
435 lines (361 loc) · 11.9 KB
/
example3.c
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
// Analysis example of electromagnetic field instantaneous value.
#include "emf_multilayer.h"
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <png.h>
typedef struct image_data{
char dir_name[64]; // directory name to output image
int scale; // number for enlarge the output image
int m; // sampling number
double rang; // range of sampling
double rs[3]; // coordinate of each cross section
int ts; // time step per cycle
int type; // type setting for field analysis functions
double complex *ve,*vh; // electromagnetic field data
double me[3],mh[3]; // maximum amplitude of each field component
}IMD;
void make_directory(char *dir_name);
void eh_field_x(IMD *id,Mlpw *ml);
void eh_field_y(IMD *id,Mlpw *ml);
void eh_field_z(IMD *id,Mlpw *ml);
void output_field(char *pl,IMD *id,Mlpw *ml);
// color table
png_byte ct1[9][3]={{0x00,0x00,0x90},{0x00,0x0f,0xff},{0x00,0x90,0xff},{0x0f,0xff,0xee},
{0xff,0xff,0xff},{0xff,0xee,0x00},{0xff,0x70,0x00},{0xee,0x00,0x00},{0x7f,0x00,0x00}};
/*
png_byte ct1[9][3]={{0x00,0x00,0x90},{0x00,0x0f,0xff},{0x00,0x90,0xff},{0x0f,0xff,0xee},
{0x90,0xff,0x70},{0xff,0xee,0x00},{0xff,0x70,0x00},{0xee,0x00,0x00},{0x7f,0x00,0x00}};
*/
int main(int argc,char *argv[])
{
if(argc!=4 && argc!=5){
printf("Usage: %s incident_plane_wave_datafile_name multilayer_datafile_name output_directory_name [incident_angle](optional)\n",argv[0]);
exit(0);
}
IMD id;
Mlpw ml;
read_data_mlpw(argv[1],argv[2],&ml); // read data and memory allocation
if(argc==5) ml.pw.theta=atof(argv[4]); // set incident angle theta
print_data_mlpw(&ml); // print data
//print_data_mlpw_mksa(&ml); // print data in MKSA system of units
setup_mlpw(&ml); // calculation of coefficients
if(strlen(argv[3])<64) strcpy(id.dir_name,argv[3]); // directory name to output image
else {
printf("directory name is too long. it can be up to 64 characters. exit...\n");
exit(1);
}
id.scale=1; // number for enlarge the output image
id.m=200; // sampling number
id.rang=2.0; // range of sampling
id.ts=40; // time step per cycle
id.rs[0]=0.0; // x-coordinate for y-z plane
id.rs[1]=0.0; // y-coordinate for x-z plane
id.rs[2]=0.0; // z-coordinate for x-y plane
id.type=0; // type setting for field analysis functions
make_directory(id.dir_name);
id.ve=(double complex *)m_alloc2(id.m*id.m*3,sizeof(double complex),"example3.c, id.ve");
id.vh=(double complex *)m_alloc2(id.m*id.m*3,sizeof(double complex),"example3.c, id.vh");
// x=id.rs[0] plane
eh_field_x(&id,&ml);
output_field("yz",&id,&ml);
// y=id.rs[1] plane
eh_field_y(&id,&ml);
output_field("xz",&id,&ml);
// z=id.rs[2] plane
eh_field_z(&id,&ml);
output_field("xy",&id,&ml);
free(id.ve);
free(id.vh);
printf("Done.\n");
mfree_mlpw(&ml);
return 0;
}
void make_directory(char *dir_name)
{
int ret;
ret=mkdir(dir_name,S_IRWXU|S_IRWXG);
if(ret!=0 && errno!=EEXIST){
printf("failed to make directory. Exit..");
exit(1);
}
}
void eh_field_x(IMD *id,Mlpw *ml)
{
double complex e[3],h[3];
double x[3],dr;
int i,j,d;
dr=id->rang*2.0/(double)(id->m-1);
for(i=0;i<3;i++){
id->me[i]=0.0;
id->mh[i]=0.0;
}
// x=id->rs[0] plane
x[0]=id->rs[0];
#pragma omp parallel for schedule(dynamic) firstprivate(x) private(j,d,e,h)
for(i=0;i<id->m;i++){
x[2]=id->rang-(double)i*dr;
for(j=0;j<id->m;j++){
x[1]=-id->rang+(double)j*dr;
EH_t_mlpw(e,h,x,id->type,ml);
#pragma omp critical
for(d=0;d<3;d++){
if(cabs(e[d])>id->me[d]) id->me[d]=cabs(e[d]);
if(cabs(h[d])>id->mh[d]) id->mh[d]=cabs(h[d]);
}
for(d=0;d<3;d++){
id->ve[i*id->m*3+j*3+d]=e[d];
id->vh[i*id->m*3+j*3+d]=h[d];
}
}
}
}
void eh_field_y(IMD *id,Mlpw *ml)
{
double complex e[3],h[3];
double x[3],dr;
int i,j,d;
dr=id->rang*2.0/(double)(id->m-1);
for(i=0;i<3;i++){
id->me[i]=0.0;
id->mh[i]=0.0;
}
// y=id->rs[1] plane
x[1]=id->rs[1];
#pragma omp parallel for schedule(dynamic) firstprivate(x) private(j,d,e,h)
for(i=0;i<id->m;i++){
x[2]=id->rang-(double)i*dr;
for(j=0;j<id->m;j++){
x[0]=-id->rang+(double)j*dr;
EH_t_mlpw(e,h,x,id->type,ml);
#pragma omp critical
for(d=0;d<3;d++){
if(cabs(e[d])>id->me[d]) id->me[d]=cabs(e[d]);
if(cabs(h[d])>id->mh[d]) id->mh[d]=cabs(h[d]);
}
for(d=0;d<3;d++){
id->ve[i*id->m*3+j*3+d]=e[d];
id->vh[i*id->m*3+j*3+d]=h[d];
}
}
}
}
void eh_field_z(IMD *id,Mlpw *ml)
{
double complex e[3],h[3];
double x[3],dr;
int i,j,d;
dr=id->rang*2.0/(double)(id->m-1);
for(i=0;i<3;i++){
id->me[i]=0.0;
id->mh[i]=0.0;
}
// z=id->rs[2] plane
x[2]=id->rs[2];
#pragma omp parallel for schedule(dynamic) firstprivate(x) private(j,d,e,h)
for(i=0;i<id->m;i++){
x[1]=id->rang-(double)i*dr;
for(j=0;j<id->m;j++){
x[0]=-id->rang+(double)j*dr;
EH_t_mlpw(e,h,x,id->type,ml);
#pragma omp critical
for(d=0;d<3;d++){
if(cabs(e[d])>id->me[d]) id->me[d]=cabs(e[d]);
if(cabs(h[d])>id->mh[d]) id->mh[d]=cabs(h[d]);
}
for(d=0;d<3;d++){
id->ve[i*id->m*3+j*3+d]=e[d];
id->vh[i*id->m*3+j*3+d]=h[d];
}
}
}
}
void output_field(char *pl,IMD *id,Mlpw *ml)
{
void output_png(int nt,double complex cet,char *pl,IMD *id);
void output_color_bar(IMD *id);
FILE *fp;
char fn[128];
double dt;
int n;
dt=ml->pw.lambda0/(double)id->ts;
#pragma omp parallel for schedule(dynamic)
for(n=0;n<id->ts;n++){
output_png(n,cexp(-I*ml->pw.k0*dt*(double)n),pl,id);
}
// print info
sprintf(fn,"%s/%s_info.txt",id->dir_name,pl);
fp=fopen(fn,"wt");
if(fp==NULL){
printf("Failed to open the %s file. Exit...\n",fn);
exit(1);
}
fprintf(fp,"the range of color bar\n");
fprintf(fp,"Ex is %8e to %8e\n",-id->me[0],id->me[0]);
fprintf(fp,"Ey is %8e to %8e\n",-id->me[1],id->me[1]);
fprintf(fp,"Ez is %8e to %8e\n",-id->me[2],id->me[2]);
fprintf(fp,"Hx is %8e to %8e\n",-id->mh[0],id->mh[0]);
fprintf(fp,"Hy is %8e to %8e\n",-id->mh[1],id->mh[1]);
fprintf(fp,"Hz is %8e to %8e\n",-id->mh[2],id->mh[2]);
fclose(fp);
// output color bar image
output_color_bar(id);
}
void output_png(int nt,double complex cet,char *pl,IMD *id)
{
int color_rgb(double x,png_byte *r,png_byte *g,png_byte *b); // -1 <= x <= 1
FILE *fep[3],*fhp[3];
char fname[256],sf[3]={'x','y','z'};
int j,i,sj,si,d,m,scale;
png_uint_32 width,height;
png_structp png_e[3],png_h[3];
png_infop info_e[3],info_h[3];
png_bytepp pd_e[3],pd_h[3];
png_byte r,g,b;
m=id->m;
scale=id->scale;
width =m*(scale+1);
height=m*(scale+1);
for(d=0;d<3;d++){
png_e[d] =png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_e[d]=png_create_info_struct(png_e[d]);
sprintf(fname,"%s/%s_E%c_%03d.png",id->dir_name,pl,sf[d],nt);
fep[d]=fopen(fname,"wb");
if(fep[d]==NULL){
printf("Failed to open the %s file. Exit...\n",fname);
exit(1);
}
png_h[d] =png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_h[d]=png_create_info_struct(png_h[d]);
sprintf(fname,"%s/%s_H%c_%03d.png",id->dir_name,pl,sf[d],nt);
fhp[d]=fopen(fname,"wb");
if(fhp[d]==NULL){
printf("Failed to open the %s file. Exit...\n",fname);
exit(1);
}
png_init_io(png_e[d],fep[d]);
png_set_IHDR(png_e[d],info_e[d],width,height,8,PNG_COLOR_TYPE_RGB,PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
pd_e[d]=(png_bytepp)png_malloc(png_e[d],sizeof(png_bytep)*height);
png_set_rows(png_e[d],info_e[d],pd_e[d]);
png_init_io(png_h[d],fhp[d]);
png_set_IHDR(png_h[d],info_h[d],width,height,8,PNG_COLOR_TYPE_RGB,PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
pd_h[d]=(png_bytepp)png_malloc(png_h[d],sizeof(png_bytep)*height);
png_set_rows(png_h[d],info_h[d],pd_h[d]);
for(j=0;j<height;j++){
pd_e[d][j]=(png_bytep)png_malloc(png_e[d],sizeof(png_byte)*width*3);
pd_h[d][j]=(png_bytep)png_malloc(png_h[d],sizeof(png_byte)*width*3);
}
}
for(i=0;i<m;i++){
for(j=0;j<m;j++){
for(d=0;d<3;d++){
color_rgb(creal(cet*id->ve[i*m*3+j*3+d])/id->me[d],&r,&g,&b);
for(si=0;si<=scale;si++){
for(sj=0;sj<=scale;sj++){
pd_e[d][i*(scale+1)+si][(j*(scale+1)+sj)*3+0]=r;
pd_e[d][i*(scale+1)+si][(j*(scale+1)+sj)*3+1]=g;
pd_e[d][i*(scale+1)+si][(j*(scale+1)+sj)*3+2]=b;
}
}
color_rgb(creal(cet*id->vh[i*m*3+j*3+d])/id->mh[d],&r,&g,&b);
for(si=0;si<=scale;si++){
for(sj=0;sj<=scale;sj++){
pd_h[d][i*(scale+1)+si][(j*(scale+1)+sj)*3+0]=r;
pd_h[d][i*(scale+1)+si][(j*(scale+1)+sj)*3+1]=g;
pd_h[d][i*(scale+1)+si][(j*(scale+1)+sj)*3+2]=b;
}
}
}
}
}
for(d=0;d<3;d++){
png_write_png(png_e[d],info_e[d],PNG_TRANSFORM_IDENTITY,NULL);
png_write_png(png_h[d],info_h[d],PNG_TRANSFORM_IDENTITY,NULL);
for(j=0;j<height;j++){
png_free(png_e[d],pd_e[d][j]);
png_free(png_h[d],pd_h[d][j]);
}
png_free(png_e[d],pd_e[d]);
png_free(png_h[d],pd_h[d]);
fclose(fep[d]);
fclose(fhp[d]);
}
}
void output_color_bar(IMD *id)
{
int color_rgb(double x,png_byte *r,png_byte *g,png_byte *b); // -1 <= x <= 1
FILE *fp;
char fname[128];
int j,i;
png_uint_32 width,height;
png_structp png;
png_infop info;
png_bytepp pdata;
png_byte r,g,b;
sprintf(fname,"%s/color_bar.png",id->dir_name);
height=id->m*(id->scale+1);
width=height/16;
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info= png_create_info_struct(png);
fp=fopen(fname,"wb");
if(fp==NULL){
printf("Failed to open the %s file. Exit...\n",fname);
exit(1);
}
png_init_io(png, fp);
png_set_IHDR(png,info,width,height,8,PNG_COLOR_TYPE_RGB,PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
pdata=(png_bytepp)png_malloc(png, sizeof(png_bytep)*height);
png_set_rows(png,info,pdata);
for(j=0;j<height;j++){
pdata[j]=(png_bytep)png_malloc(png,sizeof(png_byte)*width*3);
}
for(i=0;i<height;i++){
color_rgb(1.0-(2.0/(double)height)*(double)i,&r,&g,&b);
for(j=0;j<width;j++){
pdata[i][j*3+0]=r;
pdata[i][j*3+1]=g;
pdata[i][j*3+2]=b;
}
}
png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
for(j=0;j<height;j++){
png_free(png,pdata[j]);
}
png_free(png,pdata);
fclose(fp);
}
int color_rgb(double x,png_byte *r,png_byte *g,png_byte *b) // -1 <= x <= 1
{
double i_nc,dr,dg,db;
unsigned int i,n,nc,nd;
if(x<-1.0 || x>1.0){
*r=0x00; *g=0x00; *b=0x00;
return -1;
}
n=(unsigned int)floor(pow(2,23)*(x+1.0));
nc=(unsigned int)pow(2,21);
i_nc=1.0/(double)nc;
if(n<nc*1) i=1;
else if(n<nc*2) i=2;
else if(n<nc*3) i=3;
else if(n<nc*4) i=4;
else if(n<nc*5) i=5;
else if(n<nc*6) i=6;
else if(n<nc*7) i=7;
else if(n<nc*8) i=8;
else {
*r=ct1[8][0]; *g=ct1[8][1]; *b=ct1[8][2];
return 0;
}
nd=n-nc*(i-1);
dr=(double)(ct1[i][0]-ct1[i-1][0])*i_nc;
dg=(double)(ct1[i][1]-ct1[i-1][1])*i_nc;
db=(double)(ct1[i][2]-ct1[i-1][2])*i_nc;
*r=(png_byte)floor((double)ct1[i-1][0]+dr*(double)nd);
*g=(png_byte)floor((double)ct1[i-1][1]+dg*(double)nd);
*b=(png_byte)floor((double)ct1[i-1][2]+db*(double)nd);
return 0;
}