bug0016.pp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. uses
  2. crt;
  3. const
  4. { ... parameters }
  5. w = 10; { max. 10 }
  6. h = 10; { max. 10 }
  7. type
  8. tp = array[0..w,0..h] of double;
  9. var
  10. temp : tp;
  11. phi : tp;
  12. Bi : tp;
  13. boundary : array[0..w,0..h] of double;
  14. function start_temp(i,j : longint) : double;
  15. begin
  16. start_temp:=(boundary[i,0]*(h-j)+boundary[i,h]*j+boundary[0,j]*(w-i)+boundary[w,j]*i)/(w+h);
  17. end;
  18. procedure init;
  19. var
  20. i,j : longint;
  21. begin
  22. for i:=0 to w do
  23. for j:=0 to h do
  24. temp[i,j]:=start_temp(i,j);
  25. end;
  26. procedure draw;
  27. var
  28. i,j : longint;
  29. begin
  30. for i:=0 to w do
  31. for j:=0 to h do
  32. begin
  33. textcolor(white);
  34. gotoxy(i*7+1,j*2+1);
  35. writeln(temp[i,j]:6:0);
  36. textcolor(darkgray);
  37. gotoxy(i*7+1,j*2+2);
  38. writeln(phi[i,j]:6:3);
  39. end;
  40. end;
  41. procedure calc_phi;
  42. var
  43. i,j : longint;
  44. begin
  45. for i:=0 to w do
  46. for j:=0 to h do
  47. begin
  48. if (i=0) and (j=0) then
  49. begin
  50. phi[i,j]:=Bi[i,j]*boundary[i,j]+0.5*temp[i,j+1]+0.5*temp[i+1,j]-(1+Bi[i,j])*temp[i,j];
  51. end
  52. else if (i=0) and (j=h) then
  53. begin
  54. phi[i,j]:=Bi[i,j]*boundary[i,j]+0.5*temp[i,j-1]+0.5*temp[i+1,j]-(1+Bi[i,j])*temp[i,j];
  55. end
  56. else if (i=w) and (j=0) then
  57. begin
  58. phi[i,j]:=Bi[i,j]*boundary[i,j]+0.5*temp[i,j+1]+0.5*temp[i-1,j]-(1+Bi[i,j])*temp[i,j];
  59. end
  60. else if (i=w) and (j=h) then
  61. begin
  62. phi[i,j]:=Bi[i,j]*boundary[i,j]+0.5*temp[i,j-1]+0.5*temp[i-1,j]-(1+Bi[i,j])*temp[i,j];
  63. end
  64. else if i=0 then
  65. begin
  66. phi[i,j]:=Bi[i,j]*boundary[i,j]+temp[i+1,j]+0.5*temp[i,j-1]+0.5*temp[i,j+1]-(2+Bi[i,j])*temp[i,j];
  67. end
  68. else if i=w then
  69. begin
  70. phi[i,j]:=Bi[i,j]*boundary[i,j]+temp[i-1,j]+0.5*temp[i,j-1]+0.5*temp[i,j+1]-(2+Bi[i,j])*temp[i,j];
  71. end
  72. else if j=0 then
  73. begin
  74. phi[i,j]:=Bi[i,j]*boundary[i,j]+temp[i,j+1]+0.5*temp[i-1,j]+0.5*temp[i+1,j]-(2+Bi[i,j])*temp[i,j];
  75. end
  76. else if j=h then
  77. begin
  78. phi[i,j]:=Bi[i,j]*boundary[i,j]+temp[i,j-1]+0.5*temp[i-1,j]+0.5*temp[i+1,j]-(2+Bi[i,j])*temp[i,j];
  79. end
  80. else
  81. phi[i,j]:=temp[i,j-1]+temp[i-1,j]-4*temp[i,j]+temp[i+1,j]+temp[i,j+1];
  82. end;
  83. end;
  84. procedure adapt(i,j : longint);
  85. begin
  86. if (i=0) and (j=0) then
  87. begin
  88. temp[i,j]:=(Bi[i,j]*boundary[i,j]+0.5*temp[i,j+1]+0.5*temp[i+1,j])/(1+Bi[i,j]);
  89. end
  90. else if (i=0) and (j=h) then
  91. begin
  92. temp[i,j]:=(Bi[i,j]*boundary[i,j]+0.5*temp[i,j-1]+0.5*temp[i+1,j])/(1+Bi[i,j]);
  93. end
  94. else if (i=w) and (j=0) then
  95. begin
  96. temp[i,j]:=(Bi[i,j]*boundary[i,j]+0.5*temp[i,j+1]+0.5*temp[i-1,j])/(1+Bi[i,j]);
  97. end
  98. else if (i=w) and (j=h) then
  99. begin
  100. temp[i,j]:=(Bi[i,j]*boundary[i,j]+0.5*temp[i,j-1]+0.5*temp[i-1,j])/(1+Bi[i,j]);
  101. end
  102. else if i=0 then
  103. begin
  104. temp[i,j]:=(Bi[i,j]*boundary[i,j]+temp[i+1,j]+0.5*temp[i,j-1]+0.5*temp[i,j+1])/(2+Bi[i,j]);
  105. end
  106. else if i=w then
  107. begin
  108. temp[i,j]:=(Bi[i,j]*boundary[i,j]+temp[i-1,j]+0.5*temp[i,j-1]+0.5*temp[i,j+1])/(2+Bi[i,j]);
  109. end
  110. else if j=0 then
  111. begin
  112. temp[i,j]:=(Bi[i,j]*boundary[i,j]+temp[i,j+1]+0.5*temp[i-1,j]+0.5*temp[i+1,j])/(2+Bi[i,j]);
  113. end
  114. else if j=h then
  115. begin
  116. temp[i,j]:=(Bi[i,j]*boundary[i,j]+temp[i,j-1]+0.5*temp[i-1,j]+0.5*temp[i+1,j])/(2+Bi[i,j]);
  117. end
  118. else
  119. temp[i,j]:=(temp[i,j-1]+temp[i-1,j]+temp[i+1,j]+temp[i,j+1])/4;
  120. end;
  121. var
  122. iter,i,j,mi,mj : longint;
  123. habs,sigma_phi : double;
  124. begin
  125. clrscr;
  126. iter:=0;
  127. { setup boundary conditions }
  128. for i:=0 to w do
  129. for j:=0 to h do
  130. begin
  131. if (i=0) or (i=w) then
  132. bi[i,j]:=100
  133. else
  134. bi[i,j]:=100;
  135. if (j=0) then
  136. boundary[i,j]:=1000
  137. else
  138. boundary[i,j]:=300;
  139. end;
  140. init;
  141. draw;
  142. repeat
  143. calc_phi;
  144. mi:=0;
  145. mj:=0;
  146. sigma_phi:=0;
  147. inc(iter);
  148. habs:=abs(phi[mi,mj]);
  149. for i:=0 to w do
  150. for j:=0 to h do
  151. begin
  152. if abs(phi[i,j])>habs then
  153. begin
  154. mi:=i;
  155. mj:=j;
  156. habs:=abs(phi[mi,mj]);
  157. end;
  158. { calculate error }
  159. sigma_phi:=sigma_phi+abs(phi[i,j]);
  160. end;
  161. adapt(mi,mj);
  162. gotoxy(1,23);
  163. textcolor(white);
  164. writeln(iter,' iterations, sigma_phi=',sigma_phi);
  165. until keypressed or (sigma_phi<0.5);
  166. draw;
  167. gotoxy(1,23);
  168. textcolor(white);
  169. writeln(iter,' iterations, sigma_phi=',sigma_phi);
  170. writeln('press a key');
  171. if readkey=#0 then
  172. readkey;
  173. end.