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 サンプルプログラムソースコード #include <stdio.h> #include <stdlib.h> #include <math.h> #include <GLUT/glut.h> ライブラリ #define NN 10000 // size of array #define PI 3.14159265 #define echarge #define ep0 #define emass // charge of an elementary electron // Electric permittivity of free space // mass of a free electron 物理定数 注意)行末にセミコロン(;)は必要ない /*Declaration of variables related to electron dynamics */ int PARTICLE_NUM; double dt,time; int step,total_step; double side_x,side_y,side_z,sideh_x,sideh_y,sideh_z,Efield; double epsilon; static double cd[NN]; static double cd_draw[NN]; static double vl[NN]; static double fc[NN]; static double mass[NN]; static double kinenergy[NN]; 配列指定 /***** Declaration of variables related to OpenGL *****/ int stop_flg = 1; double eye_len=220; double trans[3] = {0.0, 0.0, 0.0}; double angle[3] = {0.0, 0.0, 0.0}; int mouse_l = 0; int mouse_m = 0; int mouse_r = 0; int mpos[2]; double m_matrix[16]; double i_matrix[16]; /********************************/ /** Declaration of the variables to open the file **/ FILE *fp_output; /*Initialization of electron dynamics)*/ void init_dynamics(void) { int i,j,k; int ix,iy,iz; double vl2_sum; 電子のダイナミクスに関する初期条件の設定 /* Device size (in units of nano mater)*/ side_x= ; side_y= ; side_z= ; 導体のサイズの指定(nm 単位) /*Half of the device size ( Don't change this part.)*/ sideh_x=side_x*0.5; sideh_y=side_y*0.5; sideh_z=side_z*0.5; 1 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 /*Number of electrons*/ PARTICLE_NUM= ; PARTICLE_NUM=計算に使う粒子数 /*time step */ dt= ; dt=時間の刻み幅 /*time initialization*/ step = 1; step=計算回数.すなわち時刻 time=step*dt /*total number of simulation steps*/ total_step = ; /*electric field in units of [V/m]*/ Efield= ; Efield=-Efield; Efield は導体にかける電界 /*file open command */ fp_output=fopen("output.dat","w"); データ保存用に output.dat という名前のフ ァイルを開く(作成する) /*mass of electrons */ for(i = 0; i < PARTICLE_NUM; i++) { mass[i]= ; } 164 行目を参照のこと 材料の有効質量はここで指定する /*dielectric constant*/ epsilon= ; 材料の誘電率はここで指定する /*initial positions of electrons in units of [m] */ for (i=0;i<PARTICLE_NUM;i++) { cd[i*3] = ((double)rand()/RAND_MAX-.0)*side_x*1.0e-9; → x cd[i*3+1] = ((double)rand()/RAND_MAX-.0)*side_y*1.0e-9; → y (初期配置座標) →z cd[i*3+2] = ((double)rand()/RAND_MAX-.0)*side_z*1.0e-9; } 0∼1 の乱数を発生させる組み込み関数(このまま使う) /*initial velocities of electrons in units of [m/s] */ for(i = 0; i < PARTICLE_NUM; i++) { → vx vl[i*3] = ; → vy (初期速度) vl[i*3+1]= ; vl[i*3+2]= ; → vz } 初期条件の設定に関するサブルーチンはここまで } /*electron dynamics */ void run_dynamics(void) { int i,j; double dis,ld,md,nd,sumvz; 実際に運動方程式を解く部分 (run_dynamics を total_step=10000 回繰り返す) time+=dt; /* update of forces acting on electrons*/ 2 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 double dist; for(i = 0; i < PARTICLE_NUM; i++) { fc[i*3]=0.0; fc[i*3+1]=0.0; fc[i*3+2]=0.0; for(j = 0; j < PARTICLE_NUM; j++) { if(j != i) { dist=sqrt( (cd[i*3]-cd[j*3])*(cd[i*3]-cd[j*3]) (cd[i*3+2]-cd[j*3+2])*(cd[i*3+2]-cd[j*3+2]) ); fc[i*3]+= 0.0 ; fc[i*3+1]+= 0.0 ; fc[i*3+2]+= 0.0 ; } } } 電子間クーロン反発力の計算 + /*update of positions */ for(i = 0; i < PARTICLE_NUM; i++) { cd[i*3] = ; cd[i*3+1] = ; cd[i*3+2] = ; } → fx → fy → fz (cd[i*3+1]-cd[j*3+1])*(cd[i*3+1]-cd[j*3+1]) + 式(10) /*update of velocities*/ for(i = 0; i < PARTICLE_NUM; i++) { vl[i*3]= ; vl[i*3+1]= ; vl[i*3+2]= ; } 式(9) /*average velocity in z-direction*/ sumvz=0; for(i = 0; i < PARTICLE_NUM; i++) { sumvz+=vl[i*3+2]; } sumvz=sumvz/PARTICLE_NUM; 全電子(100 個)の z 方向の平均速度(sumvz) を計算する 時刻と全粒子の平均速度を出力させる ・fprintf はファイルに保存 ・printf は画面に表示 /*file output*/ fprintf(fp_output," %e %e ¥n",time,sumvz); /*printf("average velocity= %e (m/s) ¥n",sumvz);*/ %e: 実数型で出力、%d: 整数型で出力 ¥n は改行を与える /* reflections at x,y boundaries and periodic b.c. at z boundaries */ /* for(i = 0; i < PARTICLE_NUM; i++) { if (cd[i*3+2]>side_z*1.0e-9) { cd[i*3+2]= ; 3 境界条件を与える部分 周期的境界条件(z 方向) 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 } if (cd[i*3+2]<0) { cd[i*3+2]= ; } if (cd[i*3]>side_x*1.0e-9) { vl[i*3]= ; cd[i*3]= ; } if (cd[i*3]<0) { vl[i*3]= ; cd[i*3]= ; } if (cd[i*3+1]>side_y*1.0e-9) { vl[i*3+1]= ; cd[i*3+1]= ; } if (cd[i*3+1]<0) { vl[i*3+1]= ; cd[i*3+1]= ; } }*/ 周期的境界条件(z 方向) 鏡面反射条件(x, y 方向) /* time increment*/ step++; if(step == total_step) { fclose(fp_output); exit(1); } glutPostRedisplay(); // file close. // exit the dynamics time roop electron dynamics に関するサブルーチンはここまで // OpenGL command } /*ここから以下はグラフィックスに関する部分*/ void draw_box(void) { glDisable(GL_LIGHTING); glColor3f(1.0,1.0,1.0); glBegin(GL_LINE_LOOP); glVertex3f( 0, 0,0); glVertex3f(side_x, 0,0); glVertex3f(side_x,side_y,0); glVertex3f(0, side_y,0); glEnd(); 4 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 glBegin(GL_LINE_LOOP); glVertex3f( 0, 0,side_z); glVertex3f(side_x, 0,side_z); glVertex3f(side_x,side_y,side_z); glVertex3f(0, side_y,side_z); glEnd(); glBegin(GL_LINES); glVertex3f( 0, 0, 0); glVertex3f( 0, 0,side_z); glVertex3f(side_x, 0, 0); glVertex3f(side_x, 0,side_z); glVertex3f( 0,side_y, 0); glVertex3f( 0,side_y,side_z); glVertex3f(side_x,side_y, 0); glVertex3f(side_x,side_y,side_z); glEnd(); glEnable(GL_LIGHTING); } void draw_dynamics(void) { int i; 粒子の動きを描画する部分 double color_level; GLfloat color[4]; glTranslated(-sideh_x,-sideh_y,-sideh_z); draw_box(); for(i = 0; i < PARTICLE_NUM; i ++){ cd_draw[i*3]=cd[i*3]*1.0e9; cd_draw[i*3+1]=cd[i*3+1]*1.0e9; cd_draw[i*3+2]=cd[i*3+2]*1.0e9; glPushMatrix(); glTranslated(cd_draw[i*3],cd_draw[i*3+1],cd_draw[i*3+2]); → Red color[0]=0; → Green color[1]=1; → Blue color[2]=0; color[3]=1.0; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,color); glutSolidSphere(0.2, 20, 10); glPopMatrix(); } 0.2:粒子の大きさを設定 } void mat_inv(double a[4][4]) { int i,j,k; double t, u, det; 5 粒子を緑色に光らせる部分 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 int n = 3; det = 1; for(k = 0; k < n; k++){ t = a[k][k]; det *= t; for(i = 0; i < n; i++) a[k][i] /= t; a[k][k] = 1 / t; for(j = 0; j < n; j++) if(j != k){ u = a[j][k]; for(i = 0; i < n; i++) if(i != k) a[j][i] -= a[k][i] * u; else a[j][i] = -u/t; } } } void init_gl(void) { GLfloat light_position[] = {1.0, 1.1, 1.2, 0.0}; glShadeModel(GL_SMOOTH); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glMatrixMode(GL_MODELVIEW); glGetDoublev(GL_MODELVIEW_MATRIX,m_matrix); glGetDoublev(GL_MODELVIEW_MATRIX,i_matrix); } void display(void) { int i,j; double d0,d1,d2,d3,d4,d5; GLfloat color[4]; glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glCullFace(GL_BACK); glLoadIdentity(); glPushMatrix(); gluLookAt(eye_len, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); glTranslated(trans[0], trans[1], trans[2]); glPushMatrix(); glLoadIdentity(); glRotatef( angle[0],1.0,0.0,0.0); glRotatef( angle[1],0.0,1.0,0.0); glRotatef( angle[2],0.0,0.0,1.0); glMultMatrixd(m_matrix); glGetDoublev(GL_MODELVIEW_MATRIX, m_matrix); glPopMatrix(); 6 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 for(i = 0; i < 16; i++) i_matrix[i] = m_matrix[i]; mat_inv((double(*)[4])i_matrix); glMultMatrixd(m_matrix); if(mouse_l == 1 || mouse_m == 1 || mouse_r == 1){ angle[0] = 0; angle[1] = 0; angle[2] = 0; } draw_dynamics(); glPopMatrix(); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); glutSwapBuffers(); } void reshape(int w, int h) { int i; glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30.0, (double)w / (double)h, 1.0, 800.0); glMatrixMode(GL_MODELVIEW); } void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { mpos[0] = x; mpos[1] = y; mouse_l = 1; } if (state == GLUT_UP) { mouse_l = 0; } break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) { mpos[0] = x; mpos[1] = y; mouse_m = 1; } if (state == GLUT_UP) { mouse_m = 0; } 7 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 break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { mpos[0] = x; mpos[1] = y; mouse_r = 1; } if (state == GLUT_UP) { mouse_r = 0; } break; default: break; } } void motion(int x, int y) { double d0; double len = 10; len = eye_len; if(mouse_l == 1 && mouse_m == 1){ trans[0] += (double)(y-mpos[1])*len/150; angle[0] = -(double)(x-mpos[0])*0.2; } else if(mouse_m == 1 || (mouse_l == 1 && mouse_r == 1)){ trans[1] += (double)(x-mpos[0])*len*.001; trans[2] -= (double)(y-mpos[1])*len*.001; } else if(mouse_r == 1){ trans[0] -= (double)(y-mpos[1])*len/150; angle[0] = (double)(x-mpos[0])*0.2; } else if(mouse_l == 1){ d0 = len/50; if(d0 > 1.0) d0 = 1.0; angle[1] = (double)(y-mpos[1])*d0; angle[2] = (double)(x-mpos[0])*d0; } if(mouse_l == 1 || mouse_m == 1 || mouse_r == 1){ mpos[0] = x; mpos[1] = y; glutPostRedisplay(); } } void keyboard(unsigned char key, int x, int y) { if( key == 'q' || key == 'Q') exit(0); if(key == 's') { if(stop_flg == 1) { stop_flg = 0; glutIdleFunc(run_dynamics); } else if(stop_flg == 0) { stop_flg = 1; glutIdleFunc(NULL); 8 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 } } } int main(int argc, char** argv) { init_dynamics(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize (1000, 1000); glutInitWindowPosition (400, 100); glutCreateWindow (argv[0]); init_gl(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } 9 メイン文
© Copyright 2024 ExpyDoc