現在Cを使用して陽解法を作成したのですが、そのプログラムをOpenGLを使用しx[imax+1]とc[imax+1]でグラフを描写するプログラムを作成したい。
C
1#include<stdio.h> 2#include<math.h> 3#include<time.h> 4#define imax 500 5#define dx 0.1 6 7int main(void){ 8 int i,n,nmax; 9 double x[imax+1], c[imax+1], cnew[imax+1]; 10 double time, pe, dt, cl, cc, cr,s; 11 FILE *f1; 12 clock_t start,end; 13 printf("pe="); 14 scanf("%lf",&pe); 15 printf("dt="); 16 scanf("%lf",&dt); 17 printf("nmax="); 18 scanf("%d",&nmax); 19 20 cl = dt*(1.0/dx+1.0/(pe*dx*dx)); 21 cc = 1.0-dt*(1.0/dx+2.0/(pe*dx*dx)); 22 cr = dt/(pe*dx*dx); 23 time = 0.0; 24 for(i=0; i<=imax; ++i){ 25 x[i] = -5.0+(double)i*dx; 26 if (fabs(x[i])< 0.5) c[i] = 1.0; 27 else if(fabs(x[i])==0.5) c[i] = 0.5; 28 else if(fabs(x[i])> 0.5) c[i] = 0.0;} 29 start = clock(); 30 for(n=0; n<=nmax; ++n){ 31 32 s=0.0; 33 for(i=1; i<=imax; ++i){ 34 s=s+(c[i-1]+c[i])*dx/2.0;} 35 printf("n=%d,time=%lf,s=%lf\n",n,time,s); 36 37 f1= fopen("ink.csv","w"); 38 fprintf(f1,"pe=%lf,n=%d,time=%lf\n",pe,n,time); 39 for(i=0; i<=imax; ++i){ 40 fprintf(f1,"%d,%lf,%lf\n",i,x[i],c[i]);} 41 fclose(f1); 42 for(i=1; i<=imax-1; ++i){ 43 cnew[i] = cl*c[i-1]+cc*c[i]+cr*c[i+1];} 44 cnew[0] = 0.0; 45 cnew[imax] = 0.0; 46 for(i=0; i<=imax; ++i){ 47 c[i] = cnew[i];} 48 time = time+dt;} 49 50 end = clock(); 51 printf("CPU time = %lf [s]\n",(double)(end-start)/CLOCKS_PER_SEC); 52 53 return 0;} 54
このプログラムに自分で過去に使用したプログラムをもとに書いたのが
C
1#include<GLUT/glut.h> 2#include<stdio.h> 3#include<unistd.h> 4#include<math.h> 5#include<time.h> 6#define imax 500 7#define dx 0.1 8#define pi 3.141592 9#define r 0.1 10#define n 60 11#define w 0.0 12#define h 0.9 13#define v0 0.0 14#define g -9.8 15#define dy 1.0/60.0 16#define k 0.9 17int main(void){ 18 int i,n,nmax; 19 double x[imax+1], c[imax+1], cnew[imax+1]; 20 double time, pe, dt, cl, cc, cr,s; 21 float xc=w,yc=h,a=0.0,v=v0,t=0.0; 22 FILE *f1; 23 clock_t start,end; 24 25 void display(){ 26 int v; 27 float b,x,y; 28 b=2*pi/(float)n; 29 glClear(GL_COLOR_BUFFER_BIT); 30 glBegin(GL_POLYGON); 31 glColor3d(0,1,1); 32 for (v=1; v<=n; ++v){ 33 x= xc+r*cos(a+(i-1)*b); 34 y= yc+r*sin(a+(i-1)*b); 35 glVertex2d(x,y);} 36 glEnd(); 37 glutSwapBuffers();} 38 39 void keyboard(unsigned char key,int x, int y){ 40 if(key == 'a') 41 {glutIdleFunc(idle);} 42 if(key == 's') 43 {glutIdleFunc(0);} 44 if(key == 'r') 45 {glutIdleFunc(0); 46 a=0.0; 47 yc=h; 48 v=v0; 49 display();}} 50 51 printf("pe="); 52 scanf("%lf",&pe); 53 printf("dt="); 54 scanf("%lf",&dt); 55 printf("nmax="); 56 scanf("%d",&nmax); 57 58 cl = dt*(1.0/dx+1.0/(pe*dx*dx)); 59 cc = 1.0-dt*(1.0/dx+2.0/(pe*dx*dx)); 60 cr = dt/(pe*dx*dx); 61 time = 0.0; 62 for(i=0; i<=imax; ++i){ 63 x[i] = -5.0+(double)i*dx; 64 if (fabs(x[i])< 0.5) c[i] = 1.0; 65 else if(fabs(x[i])==0.5) c[i] = 0.5; 66 else if(fabs(x[i])> 0.5) c[i] = 0.0;} 67 start = clock(); 68 for(n=0; n<=nmax; ++n){ 69 70 s=0.0; 71 for(i=1; i<=imax; ++i){ 72 s=s+(c[i-1]+c[i])*dx/2.0;} 73 printf("n=%d,time=%lf,s=%lf\n",n,time,s); 74 75 f1= fopen("ink.csv","w"); 76 fprintf(f1,"pe=%lf,n=%d,time=%lf\n",pe,n,time); 77 for(i=0; i<=imax; ++i){ 78 fprintf(f1,"%d,%lf,%lf\n",i,x[i],c[i]);} 79 glutInit(&argc,argv); 80 glutInitDisplayMode(GLUT_DOUBLE); 81 glutInitWindowSize(500,500); 82 glutCreateWindow("M"); 83 glutDisplayFunc(display); 84 glutKeyboardFunc(keyboard); 85 glutMainLoop(); 86 fclose(f1); 87 for(i=1; i<=imax-1; ++i){ 88 cnew[i] = cl*c[i-1]+cc*c[i]+cr*c[i+1];} 89 cnew[0] = 0.0; 90 cnew[imax] = 0.0; 91 for(i=0; i<=imax; ++i){ 92 c[i] = cnew[i];} 93 time = time+dt;} 94 95 end = clock(); 96 printf("CPU time = %lf [s]\n",(double)(end-start)/CLOCKS_PER_SEC); 97 98 return 0;} 99
です。
やりたいことはpe,dtに値を代入をしてから画面を表示させグラフを描くプログラムを書きたいです。
あなたの回答
tips
プレビュー