while err > tol
ptemp = p;
thtemp = th;
for i = 2:Nx
for j = 2:Nz
Thpnm = thn(i,j);
hpn = Hn(i,j);
if (ptemp(i,j) > 0 || thtemp(i,j) >= 1)
PE = ((Thpnm*hpn - h(i,j)*th(i,j))/dt - (U*(h(i,j)*th(i,j) - h(i-1,j)*th(i-1,j)))/(2*dx) + (p(i+1,j)*(htemp3(i,j) ...
+ htemp3(i+1,j))*(PHx(i,j)/4 + PHx(i+1,j)/4) + p(i-1,j)*(htemp3(i,j) + htemp3(i-1,j))*(PHx(i,j)/4 ...
+ PHx(i-1,j)/4))/(12*dx^2*nu) + (p(i,j+1)*(htemp3(i,j) + htemp3(i,j+1))*(PHz(i,j)/4 + PHz(i,j+1)/4);
p(i,j) = wp*PE + (1 - wp)*ptemp(i,j);
if (p(i,j) >= 0)
th(i,j) = 1;
else
p(i,j) = 0;
end
end
if (p(i,j) <= 0 || th(i,j) < 1)
THETA = -(((htemp3(i,j) + htemp3(i+1,j))*(p(i,j) - p(i+1,j))*(PHx(i,j)/4 + PHx(i+1,j)/4) + (htemp3(i,j) + htemp3(i-1,j))*(p(i,j)...
- p(i-1,j))*(PHx(i,j)/4 + PHx(i-1,j)/4))/(12*dx^2*nu) + ((htemp3(i,j) + htemp3(i,j+1))*(p(i,j) ...
- p(i,j+1))*(PHz(i,j)/4 + PHz(i,j+1)/4) + (htemp3(i,j) + htemp3(i,j-1))*(p(i,j) - p(i,j-1))*(PHz(i,j)/4;
th(i,j) = wth*THETA + (1 - wth)*thtemp(i,j);
if (th(i,j) < 1)
p(i,j) = 0;
else
th(i,j) = 1;
end
end
end
end
pmax = max(max(p));
pmaxtemp = max(max(ptemp));
chpres = (pmax - pmaxtemp)/pmax;
thmax = max(max(p));
thmaxtemp = max(max(ptemp));
thchpres = (thmax - thmaxtemp)/thmax;
err = abs(chpres) + abs(thchpres);
if k > 50000
break
end
k = k + 1;
end