Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 6.84 KB | None | 0 0
  1. function Ass6
  2.  
  3.   final_time = 5.0;
  4.   num_points_i = 100;
  5.   num_points_j = 100;
  6.   num_cells_i = num_points_i - 1;
  7.   num_cells_j = num_points_j - 1;
  8.   CFL = 0.75;
  9.   R_min = 1.0;
  10.   R_max = 12.0;
  11.   h = 1;
  12.   ux  = 5;
  13.   uy  = 0.0;
  14.   g = 9.81;
  15.   rho = 1000;
  16.   U_free_stream = zeros(1,1,3);
  17.   U_free_stream(1,1,1) = h;
  18.   U_free_stream(1,1,2) = h*ux;
  19.   U_free_stream(1,1,3) = h*uy;
  20.  
  21.   %Initialize Geometry
  22.   delta_R     = (R_max-R_min)/(num_points_j-1);
  23.   delta_theta = 2.0*pi/(num_points_i-1);
  24.   x     = zeros(num_points_i,num_points_j);
  25.   y     = zeros(num_points_i,num_points_j);
  26.   U     = zeros(num_points_i-1,num_points_j-1, 3);
  27.   areas = zeros(num_points_i-1,num_points_j-1);
  28.  
  29.   for i = 1:num_points_i
  30.     theta = (i-1)*delta_theta;
  31.     sin_theta = sin(theta);
  32.     cos_theta = cos(theta);
  33.     for j = 1:num_points_j
  34.       R = R_min + (j-1)*delta_R;
  35.       x(i,j) = R*cos_theta;
  36.       y(i,j) = R*sin_theta;
  37.     end
  38.   end
  39.  
  40.   %Initialize Cell-Centred Data
  41.   for i = 1:num_cells_i
  42.     for j = 1:num_cells_j
  43.         U(i,j,:) = U_free_stream;
  44.         areas(i,j) = compute_cell_area(x(i  , j  ),y(i  , j ), ...
  45.                                        x(i+1, j  ),y(i+1, j ), ...
  46.                                        x(i+1, j+1),y(i+1, j+1), ...
  47.                                        x(i  , j+1),y(i  , j+1));
  48.     end
  49.   end
  50.  
  51.   %Initialize Time
  52.   time = 0;
  53.   iteration = 0;
  54.  
  55.   %Time march
  56.   while time < final_time
  57.     %Initialize
  58.     dt = 1.0e6;
  59.     dUdt = zeros(size(U));
  60.     %Compute i-direction flux contributions to dUdt
  61.     for j = 1:num_cells_j
  62.       for i = 1:num_cells_i
  63.         [nx ny edge_length] = unit_normal(x(i,j+1),y(i,j+1),x(i,j),y(i,j));
  64.         if i == 1
  65.           Ul = U(num_cells_i,j,:);
  66.         else
  67.           Ul = U(i-1,j,:);
  68.         end
  69.         Ul_rot = rotate(Ul,nx,ny);
  70.         Ur = U(i,j,:);
  71.         Ur_rot = rotate(Ur,nx,ny);
  72.         [flux_rot fastest_wave] = Local_Lax_Friedrichs(Ul_rot,Ur_rot,g);
  73.         flux = rotate(flux_rot,nx,-ny);
  74.         if i == 1
  75.           dUdt(num_cells_i,j,:) = dUdt(num_cells_i,j,:) - flux*edge_length/areas(num_cells_i,j);
  76.         else
  77.           dUdt(i-1,j,:) = dUdt(i-1,j,:) - flux*edge_length/areas(i-1,j);
  78.         end
  79.         dUdt(i,j,:) = dUdt(i,j,:) + flux*edge_length/areas(i,j);
  80.         cell_width = areas(i,j)/edge_length;
  81.         dt = min(dt, CFL*cell_width/fastest_wave);
  82.       end
  83.     end
  84.    
  85.     %Compute j-direction Flux Contributions to dUdt
  86.     for j = 1:num_cells_j+1
  87.       for i = 1:num_cells_i
  88.         [nx ny edge_length] = unit_normal(x(i,j),y(i,j),x(i+1,j),y(i+1,j));
  89.         if j <= num_cells_j
  90.           Ur = U(i,j,:);
  91.         else
  92.           Ur = U_free_stream;
  93.         end
  94.         Ur_rot = rotate(Ur,nx,ny);
  95.         if j == 1
  96.           Ul_rot = reflect(Ur_rot);
  97.         else
  98.           Ul = U(i,j-1,:);
  99.           Ul_rot = rotate(Ul,nx,ny);
  100.         end
  101.         [flux_rot fastest_wave] = Local_Lax_Friedrichs(Ul_rot,Ur_rot,g);
  102.         flux = rotate(flux_rot,nx,-ny);
  103.         if j > 1
  104.           dUdt(i,j-1,:) = dUdt(i,j-1,:) - flux*edge_length/areas(i,j-1);
  105.         end
  106.         if j <= num_cells_j
  107.           dUdt(i,j,:) = dUdt(i,j,:) + flux*edge_length/areas(i,j);
  108.           cell_width = areas(i,j)/edge_length;
  109.           dt = min(dt, CFL*cell_width/fastest_wave);
  110.         end
  111.       end
  112.     end
  113.  
  114.     %Update
  115.     fit = 0;
  116.     fii = 0;
  117.     fjt = 0;
  118.     fij = 0;
  119.     ft = ((fit)^2 + (fjt)^2)^(0.5)
  120.     U = U + dt*dUdt;
  121.     iteration = iteration + 1;
  122.     time = time + dt;
  123.     for i = 1:num_cells_i
  124.       %Unit Normals and Edge Length for Range of i and j = 1
  125.       [nx ny edge_length] = unit_normal(x(i,1),y(i,1),x(i+1,1),y(i+1,1));
  126.       %Force Calculations
  127.       fii = -0.5*nx*rho*g*edge_length*(U(i,1,1))^2;
  128.       fij = -0.5*ny*rho*g*edge_length*(U(i,1,1))^2;
  129.       %Adjusting Total Forces
  130.       fit = fit+fii
  131.       fjt = fjt+fij
  132.     end
  133.     %interpolate_and_plot1(x,y,h_field(U),1);
  134.     %interpolate_and_plot2(x,y,ux_field(U),2);
  135.     %Output Update Every Time Step
  136.     disp(sprintf('timestep %i, time = %0.5e, dt = %0.5e, norm(dUdt[1]) = %f', ...
  137.                  iteration, time, dt, norm(dUdt(:,:,1))));
  138.   end %While Time March
  139. end
  140.  
  141. %Local Lax-Friedrichs Flux function
  142. function [Flux fastest_wave] = Local_Lax_Friedrichs(Ul_rot,Ur_rot,g)
  143.   fastest_wave = max([max_lambda_x(Ul_rot,g) max_lambda_x(Ur_rot,g)]);
  144.   Fl = Flux_x(Ul_rot,g);
  145.   Fr = Flux_x(Ur_rot,g);
  146.   Flux = 0.5*(Fl+Fr) - 0.5*fastest_wave*(Ur_rot-Ul_rot);
  147. end
  148.  
  149. %P.D.E. Specific Functions
  150. function Flux_x = Flux_x(U,g)
  151.   uy = U(3)/U(1);
  152.   ux = U(2)/U(1);
  153.   Flux_x = zeros(1,1,3);
  154.   Flux_x(1,1,1) = U(2);
  155.   Flux_x(1,1,2) = U(1)*ux^2+0.5*g*U(1)^2;
  156.   Flux_x(1,1,3) = U(1)*ux*uy;
  157. end
  158.  
  159. function Urot = rotate(U,nx,ny)
  160.   Urot = U;
  161.   Urot(2) = U(2)*nx+U(3)*ny;
  162.   Urot(3) = -U(2)*ny+U(3)*nx;
  163. end
  164.  
  165. function Ureflected = reflect(U)
  166.   Ureflected = U;
  167.   Ureflected(2) = -1.0*Ureflected(2);
  168. end
  169.  
  170. function l = max_lambda_x(U,g)
  171.   h = U(1);
  172.   ux = U(2)/U(1);
  173.   l1 = abs(ux-(g*h)^(0.5));
  174.   l2 = abs(ux+(g*h)^(0.5));
  175.   l = max([l1,l2]);
  176. end
  177.  
  178. %Geometry Functions
  179. function l = length(x1,y1,x2,y2)
  180.   l = ((x2-x1)^2+(y2-y1)^2)^(0.5);
  181. end
  182.  
  183. function [nx ny edge_length] = unit_normal(x1,y1,x2,y2)
  184.   edge_length = length(x1,y1,x2,y2);
  185.   nx =  (y2-y1)/edge_length;
  186.   ny = -(x2-x1)/edge_length;
  187. end
  188.  
  189. function A = compute_cell_area(x1,y1,x2,y2,x3,y3,x4,y4)
  190.   A = 0.5*abs(x1*y2-x2*y1+x2*y3-x3*y2+x3*y4-x4*y3+x4*y1-x1*y4);
  191. end
  192.  
  193. %Output-Related Functions
  194. function h = h_field(U)
  195.   h = U(:,:,1);
  196. end
  197.  
  198. function ux = ux_field(U)
  199.   ux = U(:,:,2)./U(:,:,1);
  200. end
  201.  
  202. function uy = uy_field(U)
  203.   uy = U(:,:,3)./U(:,:,1);
  204. end
  205.  
  206. function interpolated = interpolate(x,y,W)
  207.   %This Function Does a Crude Interpolation of Data From Cell
  208.     %Centres to Nodes for Plotting.
  209.   [num_i,num_j] = size(x);
  210.   interpolated = zeros(size(x));
  211.   %Interior Nodes are All the Same
  212.   for i = 2:num_i-1
  213.     for j = 2:num_j-1
  214.       interpolated(i,j) = 0.25*( W(i,j) + W(i-1,j) + W(i,j-1) + W(i-1,j-1));
  215.     end
  216.   end
  217.   %j_min and j_max
  218.   for i = 2:num_i-1
  219.       interpolated(i,1)     = 0.5*( W(i,1) + W(i-1,1));
  220.       interpolated(i,num_j) = 0.5*( W(i,num_j-1) + W(i-1,num_j-1));
  221.   end
  222.   %i_min and i_max
  223.   for j = 2:num_j-1
  224.       interpolated(1,j)     = 0.5*( W(1,j) + W(1,j-1));
  225.       interpolated(num_i,j) = 0.5*( W(num_i-1,j) + W(num_i-1,j-1));
  226.   end
  227.   %Four Corners
  228.   interpolated(1,1) = W(1,1);
  229.   interpolated(1,num_j) = W(1,num_j-1);
  230.   interpolated(num_i,1) = W(num_i-1,1);
  231.   interpolated(num_i,num_j) = W(num_i-1,num_j-1);
  232.  
  233. end
  234.  
  235. function interpolate_and_plot1(x,y,W,fig_num)
  236.   W_interp = interpolate(x,y,W);
  237.   figure(fig_num)
  238.   contourf(x,y,W_interp);
  239.   axis('square');
  240.   colorbar;
  241.   drawnow;
  242. end
  243.  
  244. function interpolate_and_plot2(x,y,W,fig_num)
  245.   W_interp = interpolate(x,y,W);
  246.   figure(fig_num)
  247.   contourf(x,y,W_interp);
  248.   axis('square');
  249.   colorbar;
  250.   drawnow;
  251.  
  252. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement