tb0013.pp 5.3 KB

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