A..11 10_Diffusion_1D_Text.c

10_Diffusion_1D_Text.c

   1 #include<stdio.h>
   2 #include<glsc3d_3.h>
   3 
   4 #define N (200)
   5 
   6 int main()
   7 {
   8     double  x;
   9     double  L = 5;
  10     double  dx = L / N;
  11     double  t = 0.0;
  12     double  dt = 0.0001;
  13     double  u[N + 2];
  14     double  tmpu[N + 2];
  15     double  D = 1.0;
  16     int     INTV = 100;
  17     g_init("Window", 900, 320); //Pixel Size
  18     g_def_scale_2D(0,                   //ID
  19                    -L*0.5, L*0.5,       //xmin,xmax
  20                    -0.2, 1.2,     //ymin,ymax
  21                    20.0, 20.0,          //Window (Left, Top) Position
  22                    560, 280);           //Window Size (x,y)
  23     
  24     g_def_scale_2D(1,                   //ID
  25                    -L*0.5, L*0.5,       //xmin,xmax
  26                    -0.2, 1.2,     //ymin,ymax
  27                    620.0, 20.0,          //Window (Left, Top) Position
  28                    260, 280);           //Window Size (x,y)
  29 
  30     //Set initial calculation
  31     {
  32         //Step1
  33         for(int i = 1;i < N + 1; i ++)
  34         {
  35             x = i*dx - L*0.5;
  36             u[i] = exp(-(x*x));
  37         }
  38         //Step2
  39         u[0] = u[1];
  40         u[N + 1] = u[N];
  41     }
  42     //////////// Start time loop ////////////
  43     for (int i_time = 0; t < 10; i_time++) {
  44         t = i_time * dt;
  45         //////////// Draw Part ////////////
  46         if (i_time%INTV == 0) {
  47             g_cls();                    //Clear window
  48             //////////// Graph Part ////////////
  49             {
  50                 g_sel_scale(0);             //Select Virtual scale
  51                 g_line_color(0.0, 0.0, 0.0, 1.0);
  52                 g_boundary();               //Draw Boundary
  53                 
  54                 g_line_width(1.0);
  55                 g_line_color(0.0, 0.0, 0.0, 0.5);
  56                 g_move_2D(-L*0.5, 0.0); g_plot_2D(L*0.5, 0.0);  //X Axis
  57                 g_move_2D(0.0, -1.2); g_plot_2D(0.0, 1.2);      //Y Axis
  58                 
  59                 //Profile of u
  60                 g_line_width(2.0);
  61                 g_line_color(1.0, 0.0, 0.0, 1.0);
  62                 for (int i=0; i<N-1; i++) {
  63                     g_move_2D(i*dx-L*0.5, u[i]);
  64                     g_plot_2D((i+1)*dx-L*0.5, u[i+1]);
  65                 }
  66                 
  67                 g_text_size(18);
  68                 g_text_color(1.0, 0.0, 0.0, 1.0);
  69                 g_text_2D_virtual(L*0.5 - 0.4, 1.0, "u: -");
  70             }
  71             //////////// Charactor Part ////////////
  72             {
  73                 g_sel_scale(1);             //Select Virtual scale
  74                 g_text_size(16);
  75                 g_text_color(0, 0, 0, 1);
  76                 
  77                 double TextLeft = 620.0;
  78                 double TextTop = 40.0;
  79                 double TextWidth = 30.0;
  80                 int IncrimentTextWidth = 0;
  81                 
  82                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "t = %2.15f", t);
  83                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "i_time = %d", i_time);
  84                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "dt = %2.8f", dt);
  85                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "L = %2.4f", L);
  86                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "N = %d", N);
  87                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "dx = %2.4f", dx);
  88                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "D = %2.4f", D);
  89                 g_text_standard(TextLeft, TextTop + TextWidth*(IncrimentTextWidth ++), "D * dt/(dx*dx) = %2.4f (< 0.5)", D*dt/(dx*dx));
  90             }
  91             g_finish();                 //flush Draw buffer
  92             g_sleep(0.0);               //Sleep 0.0 sec
  93         }
  94         //////////// Calculation Part ////////////
  95         {
  96             //Step3
  97             for(int i = 1;i < N + 1; i ++)
  98             {
  99                 tmpu[i] = u[i] + D * (u[i - 1] -2 * u[i] + u[i + 1]) / (dx * dx) * dt;
 100             }
 101             for(int i = 1;i < N + 1; i ++)
 102             {
 103                 u[i] = tmpu[i];
 104             }
 105             //Step4
 106             u[0] = u[1];
 107             u[N + 1] = u[N];
 108         }
 109     }
 110     return 0;
 111 }



桂田 祐史