필터 지우기
필터 지우기

How to stop time loop when steady state is reached?

조회 수: 3 (최근 30일)
Yanni
Yanni 2023년 4월 20일
편집: Yanni 2023년 4월 29일
I'm using unsteady case. so, it will reach a steady state at a certain time level. I fixed time at 'j'th column wise and it ran at maximum time level, but I want to stop the 'time loop' when steady state is reached. Even though I'm using a steady criteria i.e error >tolerance along dt>0 using 'while' loop, it didn't work and using dt>0, my code didn't stop.
clc; close all; clear all;
ymax=20; n=80; dy=ymax/n;
xmax=1; m=20; dx=xmax/m;
tmax=100; nt=500; dt=tmax/nt; t=0:dt:tmax;
%tol=1e-5; max_difference=1.0;
UOLD=zeros(n,nt); VOLD=zeros(n,nt);
TNEW=0; TOLD=ones(n,nt);
A=zeros([1,m]);
B=A;
C=A;
D=A;
T=TOLD;
tic
%while dt>0 && max_difference>tol
for j=1:m
for i=1:n
if j>i
C(i)=(dt*VOLD(i,j)/4*dy)-((dt)/(2*dy^2));
elseif i>j
A(i)=(-dt*VOLD(i,j)/4*dy)-((dt)/(2*dy^2));
elseif i==j
B(i)=1+(dt*UOLD(i,j)/2*dx)+((dt)/(dy^2));
end
end
end
for j=2:m
for i=1:n
D(i)=(-dt*UOLD(i,j)*((-TNEW+TOLD(i,j)-TNEW)/(2*dx)))+(((dt)/(2*dy^2))*(TOLD(i+1,j)-(2*TOLD(i,j))+TNEW))-(((dt*VOLD(i,j))/(4*dy))*(TNEW-TOLD(i+1,j)-TOLD(i,j)));
end
end
T(:,j)=TriDiag(A,B,C,D); %call tridiagonal
dt=0.2+dt;
Thanks for any advice.
  댓글 수: 3
Mathieu NOE
Mathieu NOE 2023년 4월 20일
ok
I am not sure to understand all the details of your code , but something disturbs me
if we consider that the metric (error) that defines how your computation converge to the supposed steady state values
difference = (abs(T(:,j)-T(:,j-1))./max(1,abs(T(:,j-1))));
then if I plot it for the 80 iterations , we see the difference increasing and not decreasing .... so I wonder if you have 100% confidence in your code
ploted with y log scale to make it clear :
Image Analyst
Image Analyst 2023년 4월 20일
Which loop, the one over i or the one over j, is the "time loop"?
I don't see any line like "error >tolerance". Where exactly is that line?

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2023년 4월 21일
hello
think I have understood
your difference computation must be indexed with j , so it must be inside the for loop
I used a break to exist the for loop when the difference is below tol
now, tol = 1e-5 will not be achieved with only 500 iteration
for the sake of the demo I put tol = 1e-2 and it will do only 103 iterations
plot of difference vs j with tol = 1e-5
plot of difference vs j with tol = 1e-2
code :
clc; close all; clear all;
ymax=20; n=80; dy=ymax/n;
xmax=1; m=20; dx=xmax/m;
tmax=100; nt=500; dt=tmax/nt; t=0:dt:tmax;
tol=1e-2;
UOLD=zeros(n,nt); VOLD=zeros(n,nt);
TNEW=0; TOLD=TNEW*ones(n,nt); TWALL=ones(1,length(t));
A=zeros([1,m]);
B=A;
C=A;
D=A;
T=TOLD;
difference(1) = 1;
tic
for j=1:nt
for i=1:n
if j>i
C(i)=(dt*VOLD(i,j)/4*dy)-((dt)/(2*dy^2));
elseif i>j
A(i)=(-dt*VOLD(i,j)/4*dy)-((dt)/(2*dy^2));
elseif i==j
B(i)=1+(dt*UOLD(i,j)/2*dx)+((dt)/(dy^2));
end
end
end
for j=2:nt
if j==1
for i=1:n
if i==1
D(i)=(-dt*UOLD(i,j)*((-TNEW+TOLD(i,j)-TNEW)/(2*dx)))+(((dt)/(2*dy^2))*(TOLD(i+1,j)-(2*TOLD(i,j))+TNEW))-(((dt*VOLD(i,j))/(4*dy))*(TNEW-TOLD(i+1,j)-TOLD(i,j)))-((dt/(4*dy))*VOLD(i,j))-((dt/2*dy^2)*TNEW);
elseif i==n
D(i)=(-dt*UOLD(i,j)*((-TNEW+TOLD(i,j)-TNEW)/(2*dx)))+(((dt)/(2*dy^2))*(TWALL(j)-(2*TOLD(i,j))+TOLD(i-1,j)))-(((dt*VOLD(i,j))/(4*dy))*(TOLD(i-1,j)-TWALL(j)+TOLD(i,j)))-(((-dt)/(4*dy))*VOLD(i,j))-((dt/2*dy^2)*TWALL);
else
D(i)=(-dt*UOLD(i,j)*((-TNEW+TOLD(i,j)-TNEW)/(2*dx)))+(((dt)/(2*dy^2))*(TOLD(i+1,j)-(2*TOLD(i,j))+TOLD(i-1,j)))-(((dt*VOLD(i,j))/(4*dy))*(TOLD(i-1,j)-TOLD(i+1,j)+TOLD(i,j)));
end
end
else
for i=1:n
if i==1
D(i)=(-dt*UOLD(i,j)*((-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)))+(((dt)/(2*dy^2))*(TOLD(i+1,j)-(2*TOLD(i,j))+TNEW))-(((dt*VOLD(i,j))/(4*dy))*(TNEW-TOLD(i+1,j)+TOLD(i,j)))-((dt/(4*dy))*VOLD(i,j))-((dt/2*dy^2)*TNEW);
elseif i==n
D(i)=(-dt*UOLD(i,j)*((-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)))+(((dt)/(2*dy^2))*(TWALL(j)-(2*TOLD(i,j))+TOLD(i-1,j)))-(((dt*VOLD(i,j))/(4*dy))*(TOLD(i-1,j)-TWALL(j)+TOLD(i,j)))-(((-dt)/(4*dy))*VOLD(i,j))-((dt/2*dy^2)*TWALL(j));
else
D(i)=(-dt*UOLD(i,j)*((-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)))+(((dt)/(2*dy^2))*(TOLD(i+1,j)-(2*TOLD(i,j))+TOLD(i-1,j)))-(((dt*VOLD(i,j))/(4*dy))*(TOLD(i-1,j)-TOLD(i+1,j)+TOLD(i,j)));
end
end
end
T(:,j)=TriDiag(A,B,C,D); %call tridiagonal
dt=0.2+dt;
TOLD=T;
difference(j) = max(abs(T(:,j)-T(:,j-1))./max(1,abs(T(:,j-1))));
if difference(j)<tol
break
end
end
  댓글 수: 4
Sam Chak
Sam Chak 2023년 4월 22일
@Gayathri, Can you mathematically define the desired steady-state tolerance in your case?
Yanni
Yanni 2023년 4월 23일
@Sam Chak yes. steady state tolerance limit is between 1e-4 to 1e-6.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by