Can anyone please help me with this ?

68 visualizzazioni (ultimi 30 giorni)
Mohamed
Mohamed il 2 Ott 2022
Commentato: Sepehr il 9 Apr 2024 alle 21:41
im trying to compute the unsteady solutions for the stream function and scalar vorticity for a 2d flow around an infinite cylinder at RE = 60.
This is the code i've been using:
function omega = flow_around_cylinder_unsteady
Re=60;
%%%%% define the grid %%%%%
n=101; m=202; % number of grid points
N=n-1; M=m-2; % number of grid intervals: 2 ghost points, theta=-h,2*pi
h=2*pi/M; % grid spacing based on theta variable
xi=(0:N)*h; theta=(-1:M)*h; % xi and theta variables on the grid
%%%%% time-stepping parameters %%%%%
t_start=0; t_end=0.5; %vortex street starts at around t=1000
tspan=[t_start t_end];
%%%%% Initialize vorticity field %%%%%
omega=zeros(n,m);
%%%%% Construct the matrix A for psi equation %%%%%
boundary_index_bottom = 1:n;
boundary_index_left = 1:n:1+(m-1)*n;
boundary_index_top = 1+(m-1)*n:m*n;
boundary_index_right = n:n:m*n;
diagonals1 = [4*ones(n*m,1), -ones(n*m,4)];
A=spdiags(diagonals1,[0 -1 1 -n n], n*m, n*m);
I=speye(m*n);
A(boundary_index_left,:)=I(boundary_index_left,:);
A(boundary_index_right,:)=I(boundary_index_right,:);
diagonals2 = [ones(n*m,1), -ones(n*m,2)];
Aux = spdiags(diagonals2,[0 n -n],n*m,n*m);
A(boundary_index_top,:)=Aux(boundary_index_top,:);
A(boundary_index_bottom,:)=Aux(boundary_index_bottom,:);
%%%%% Find the LU decomposition %%%%%
[L,U]=lu(A); clear A;
%%%%% Compute any time-independent constants %%%%%
%%%%% advance solution using ode23 %%%%%
options=odeset('RelTol', 1.e-03);
omega=omega(2:n-1,2:m-1); % strip boundary values for ode23
omega=omega(:); % make a column vector
[t,omega]=ode23(@(t,omega)omega_eq(omega,L,U,theta,xi,h,Re,n,m), tspan, omega, options);
%%%%% expand omega to include boundaries %%%%%
temp=zeros(n,m);
temp(2:n-1,2:m-1)=reshape(omega(end,:),n-2,m-2);
omega=temp; clear temp;
%%%%% compute stream function (needed for omega boundary values) %%%%%
omega_tilde = zeros(n,m);
omega_tilde(1,:)=0;
omega_tilde(n,:)=exp(xi(n))*sin(theta);
omega_tilde(:,1)=0;
omega_tilde(:,m)=0;
for i=2:n-1
for j=2:m-1
omega_tilde(i,j) = h^2*exp(2*xi(i))*omega(i,j);
end
end
omega_tilde = omega_tilde(:);
psi = reshape(U\(L\omega_tilde),n,m);
%%%%% set omega boundary conditions %%%%%
omega(1,:)= (psi(3,:) - 8*psi(2,:)) * 1/(2*h^2);
omega(n,:)=0;
omega(:,1)=omega(:,m-1);
omega(:,m)=omega(:,2);
%%%%% plot scalar vorticity field %%%%%
plot_Re60(omega);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function d_omega_dt=omega_eq(omega,L,U,theta,xi,h,Re,n,m);
%%%%% expand omega to include boundary points %%%%%
temp=zeros(n,m);
index1=2:n-1; index2=2:m-1;
temp(index1,index2)=reshape(omega,n-2,m-2);
omega=temp; clear temp;
%%%%% compute stream function %%%%%
omega_tilde = zeros(n,m);
omega_tilde(1,:)=0;
omega_tilde(n,:)=exp(xi(n))*sin(theta);
omega_tilde(:,1)=0;
omega_tilde(:,m)=0;
for i=2:n-1
for j=2:m-1
omega_tilde(i,j) = h^2*exp(2*xi(i))*omega(i,j);
end
end
omega_tilde = omega_tilde(:);
psi = reshape(U\(L\omega_tilde),n,m);
%%%%% compute derivatives of omega %%%%%
omega(1,:)= (psi(3,:) - 8*psi(2,:)) * 1/(2*h^2);
omega(n,:)=0;
omega(:,1)=omega(:,m-1);
omega(:,m)=omega(:,2);
d_omega_dt=zeros(n-2,m-2);
for i=2:n-1
for j=2:m-1
d_omega_dt(i-1,j-1)=...
2*exp(-2*xi(i))* 1/(h^2*Re)*...
(omega(i+1,j)+omega(i-1,j)+...
omega(i,j+1)+omega(i,j-1)-4*omega(i,j))...
+exp(-2*xi(i))/(4*h^2)*...
((psi(i+1,j)-psi(i-1,j))*(omega(i,j+1)-omega(i,j-1))-...
(psi(i,j+1)-psi(i,j-1))*(omega(i+1,j)-omega(i-1,j)));
end
end
d_omega_dt = d_omega_dt(:);
end
The code to call the function is:
omega = flow_around_cylinder_unsteady;
is there any way to fix it or improve upon it ?
  5 Commenti
Mohamed
Mohamed il 3 Ott 2022
the code in the gray areas cannot be changed.
i keep getting this:
this is line 132:
is there any other way to fix this ?

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 3 Ott 2022
You have not shown enough code to be certain, but I suspect that your function plot_Re60() is defined as taking two inputs, the first of which is the array of omega values, and the second is expected to be a vector of time values. And that the problem is that at line 55 of flow_around_cylinder_unsteady, that you are not passing the time vector into plot_Re60
  21 Commenti
Hugo
Hugo il 18 Nov 2022
I am also struggling with this assignment. The code for plot_Re60 is the following:
function plot_Re60(omega,t_plot)
% Plot vorticity for Re = 60 from flow_past_circle_unsteady
Re=60;
% xi-theta grid
n=size(omega,1); m=size(omega,2);
N=n-1; M=m-2;
h=2*pi/M; %grid spacing
xi=(0:N)*h; theta=(-1:M)*h; %xi and theta variables on the grid
[XI, THETA] = meshgrid(xi,theta);
% x-y grid
nx=640; ny=480; %number of pixels in x and y
xmin=-1.5; xmax=10; ymax=((xmax-xmin)/2)*ny/nx; ymin=-ymax;
x=linspace(xmin,xmax,nx+1); y=linspace(ymin,ymax,ny+1);
[X,Y]=meshgrid(x,y);
%construct interpolation points
xi_i=0.5*log(X.^2+Y.^2);
theta_i=wrapTo2Pi(atan2(Y,X));
omega_xy=interp2(XI,THETA,omega',xi_i,theta_i);
%inside circle
omega_xy(xi_i<0)=0;
%set colormap for contours
levels=linspace(-1,1,1000);
v=[levels(1) levels(end)];
cmap=jet(length(levels));
cmap=flipud(cmap);
colormap(cmap);
%color contours
imagesc(x,y,omega_xy,v); hold on;
%draw black circle for cylinder
t=linspace(0,2*pi, 1000);
a=cos(t); b=sin(t);
fill(a,b,[0 0 0]);
%neaten plot
set(gca,'YDir','normal');
axis([xmin xmax ymin ymax]); set(gcf,'color','w'); axis equal; axis off;
text(xmin+0.75*(xmax-xmin),ymin+0.08*(ymax-ymin),...
['t = ', num2str(t_plot,'%3.1f')],'FontSize',22,'Color','k');
end
Walter Roberson
Walter Roberson il 29 Set 2023
The call to plot_Re60 (which cannot be changed) only passes in a single parameter, but it appears from @Hugo that the function expects two parameters to be passed, with the second one named t_plot inside the function.
The code posted by Hugo uses t_plot only once, right near the bottom, in the lines
text(xmin+0.75*(xmax-xmin),ymin+0.08*(ymax-ymin),...
['t = ', num2str(t_plot,'%3.1f')],'FontSize',22,'Color','k');
If only a single parameter was being passed to the function, then Yes, you would have a problem: you would get an error message about not enough input parameters.
The code posted at https://www.mathworks.com/matlabcentral/answers/1815940-can-anyone-please-help-me-with-this#comment_2393145 by @Mohamed shows very similar code, but does not use a variable named t_plot at that point in the code, and instead uses t . If the only change between Mohamed's code and Hugo's code was that for Mohamed, the parameter was named t instead of t_plot then there would be the point that in the code posted by Hugo,
t=linspace(0,2*pi, 1000);
and so if that statement existed in Mohamed's code but the text() call referred to t then there would not be any error about insufficient parameters -- since the missing parameter would have been overwritten by the assignment of the linspace result.
We deduce that the function code for Mohamed is not the same as the function code for Hugo.
If a time is needed by the plotting function, but that it is not permitted to change the line that calls the function, but it is permitted to change the function, then I can see two possibilities at the moment:
  1. You could alter the plotting function to not expect a second parameter, and instead to use evalin('caller') to read out the time from the calling function. This is not generally a good practice, but if the teacher refuses to fix the calling code then you have to hack; OR
  2. since you are permitted to modify the comment immediately above the call to the plotting function, you could insert the proper call there. And then you could code the plotting function to detect that nargin < 2 and just to return in that case. So the badly-coded call to the plotting function would have no effect on the output, and the proper call to the plotting function would do the work.

Accedi per commentare.


Hugo
Hugo il 19 Nov 2022
@Mohamed did you end up managing to finish the assignment with the correct code? So yes, could you please share the code with me?
Kind Regards,
Hugo
  9 Commenti
屹林
屹林 il 6 Mar 2024
Excuse me, has this problem been resolved? It has been bothering me for half a month now, can you tell me? Thank you.

Accedi per commentare.

Categorie

Scopri di più su Linear Algebra in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by