``波動方程式の問題を解いていて、これを実行しようしたのですが、Segmentation fault (コアダンプ)と出てしまいました。どうすればよいでしょうか
コード
// a sample program for Numerical Analysis a @ Kanazawa University, 2021 // Subject 1, Exercise 1.1 // modified from sample_NAa-2021.c with popen("gnuplot"): movie style // by Masato Kimura // 6 April 2021 //【Example 2.1】 //Problem2.1 #include <stdio.h> #include <math.h> #include <unistd.h> //for usleep() // Numerical parameter (division number) #define n 300 #define sl 30000 // Problem parameters #define c 1.0 #define T 5.0 double r(double x, double y) { return sqrt(pow((x - 0.5),2) + pow((y - 0.5),2)); } //関数を1つ(y)増やした //fの関数 double f(double x, double y, double t) { //Ex2.1 //return 0.0; //Ex2.2 return sin(M_PI*x) * sin(M_PI*y); } //関数を一つ(y)増やした //u0の関数はExampleできまる double u0(double x, double y) { //Ex2.1 // if(r(x,y) <= 0.1) // { // return cos(5. * M_PI * r(x,y)) * cos(5. * M_PI * r(x,y)); // } // else // { // return 0.0; // } //Ex.2.2 return 0.0; } //関数を一つ(y)増やした //v0の関数 //u0の関数はExampleできまる double v0(double x, double y) { return 0.0; } // End of problem parameters //-------------------------------------------OK // Main program int main() { //2次元になるようにパラメータを増やす、j、*[n+1] int i, j, k, m; double uh_new[n+1][n+1], uh[n+1][n+1], uh_old[n+1][n+1], t, h, tau, mu; FILE *file, *gp; // file = fopen("subject1_exer1_exam11.dat", "w"); //popen for gnuplot //変更なし gp = popen("gnuplot", "w"); //set parameter for gnuplot //変更なし fprintf(gp, "set xrange [0:1]\n"); fprintf(gp, "set yrange [0:1]\n"); fprintf(gp, "set zrange [:0.1]\n"); fprintf(gp, "set nokey\n"); // Numerical parameters //-------------------------------------------OK h = 1.0/n; tau = 0.7*h; m = floor(T/tau); mu = (c*c*tau*tau)/(h*h); //-------------------------------------------OK // Initial values for (i=1; i<n; i++) { for(j=1; j<n; j++) { uh_old[i][j] = u0(i*h, j*h); // k = 0 uh[i][j] = mu*(u0((i+1)*h,j*h) + u0((i-1)*h,j*h) + u0(i*h,(j+1)*h) + u0(i*h,(j-1)*h))/2 + (1-2*mu)*u0(i*h,j*h) + (f(i*h,j*h,0.0) * (tau * tau))/2 + (tau * v0(i*h, j*h));//k=1 } } //-------------------------------------------OK // Boundary values for (j=1; j<n; j++) { uh_new[0][j] = uh_new[n][j] = 0.0; uh[0][j] = uh[n][j] = 0.0; uh_old[0][j] = uh_old[n][j] = 0.0; } for (i=1; i<n; i++) { uh_new[i][0] = uh_new[i][n] = 0.0; uh[i][0] = uh[i][n] = 0.0; uh_old[i][0] = uh_old[i][n] = 0.0; } //-------------------------------------------OK // Output of initial uh file = fopen("subject1_exer1_exam11.dat", "w"); //2次元へ for (i=0; i<=n; i++) { for (j=0; j<=n; j++) { fprintf(file, "%f %f %f\n", i*h, j*h, uh_old[i][j]); //k=0 } fprintf(file,"\n"); } fclose (file); fprintf(gp, "set terminal png\n"); //fprintf(gp, "set output '~/Desktop/sample_NAa-2022/test.png''\n"); fprintf(gp, "set output 'test%08d.png'\n", 1); fprintf(gp, "set title 't=%f'\n", 0.0); fprintf(gp,"splot 'subject1_exer1_exam11.dat' w l\n" ); fflush(gp); usleep(sl); //-------------------------------------------OK file = fopen("subject1_exer1_exam11.dat", "w"); //2次元へ for (i=0; i<=n; i++) { for (j=0; j<=n; j++) { fprintf(file, "%f %f %f\n", i*h, j*h, uh[i][j]); //k=1 } fprintf(file,"\n"); } fclose (file); fprintf(gp, "set title 't=%f'\n", tau); fprintf(gp, "set terminal png\n"); //fprintf(gp, "set output '~/Desktop/sample_NAa-2022/test.png''\n"); fprintf(gp, "set output 'test%08d.png'\n", 2); fprintf(gp,"splot 'subject1_exer1_exam11.dat' w l\n" ); fflush(gp); usleep(sl); //-------------------------------------------OK // Time evolution for (k=2; k<=m; k++) { t = k*tau; // Update of uh for (i=1; i<n; i++) { for (j=1; j<n; j++) { uh_new[i][j] = mu * (uh[i+1][j] + uh[i-1][j] + uh[i][j+1] + uh[i][j-1]) + (2 * (1-2*mu) * uh[i][j]) - uh_old[i][j] + (tau * tau) * f(i*h, j*h, t); } } //-------------------------------------------OK for (i=0; i<n; i++) { for (j=0; j<n; j++) { uh_old[i][j] = uh[i][j]; uh[i][j] = uh_new[i][j]; } } //-------------------------------------------OK // Output at t = k*tau file = fopen("subject1_exer1_exam11.dat", "w"); for (i=0; i<=n; i++) { for (j=0; j<=n; j++) { fprintf(file, "%f %f %f\n", i*h, j*h, uh[i][j]); } fprintf(file,"\n"); } //-------------------------------------------OK fclose (file); fprintf(gp, "set terminal png\n"); //fprintf(gp, "set output '~/Desktop/sample_NAa-2022/test.png''\n"); fprintf(gp, "set output 'test%08d.png'\n", k+1); fprintf(gp, "set title 't=%f'\n", t); fprintf(gp,"splot 'subject1_exer1_exam11.dat' w l\n" ); fflush(gp); usleep(sl); } pclose (gp); return 0; }
コンパイルした時に出るんでしょうか?
それとも実行した時に出るんでしょうか
すみません!実行でした!
実行形式にデバッグ用の情報を埋め込むオプション(例えば gcc の場合は "-g")を付けてコンパイルして、デバッガ上で実行すればエラーの発生箇所は判るでしょう。ちなみに、Ubuntu 22.04/gcc 11.2.0 + memory sanitizer/gnuplot 5.4.2 環境でコンパイル・実行してみるとエラーは発生しません。画像ファイルが 2000 個近く生成されるのには閉口しましたが。。。
質問文は修正できるので、そっちを修正しましょう
それです!!!その画像ファイル2000個が欲しいんです。
どのようにしたらいいんでしょうか
質問文修正いたしました!すみません
このプログラミングはcygwinでコンパイル実行いたしました
回答1件
あなたの回答
tips
プレビュー