writing mesh file and loading for further use

2 次查看(过去 30 天)
Hi..
I have a simple square geometry (1x1) which i created in matlab and meshed using meshgrid command with 20 points. I write the x and y coordinates of this geometry to file (consider it as mesh or grid file). Now when i load this file and run the usual code on it but i am unable to.
For simplicity i have considered a simple 2D lid-driven cavity code (taken from net). The script cavity.m is the actual working code shown below.
CODE:
cavity.m
clear; close all; clc;
n = 20;
xx = linspace(-1,1,n);
yy = xx;
[x,y] = meshgrid(xx,yy);
dx = xx(2)-xx(1);
dy = dx;
T = zeros(n,n);
T(1,1:n) = 0;
T(n,1:n) = 100;
T(1:n,1) = 0;
T(1:n,n) = 0;
dt = dx^2/4; error = 1; TOL = 1e-2; k = 0;
while (error > TOL )
k = k+1
Told = T;
for i = 2:n-1
for j = 2:n-1
T(i,j) = Told(i,j) + dt*((Told(i+1,j)-2*Told(i,j)+Told(i-1,j))/dx^2 ...
+ (Told(i,j+1)-2*Told(i,j)+Told(i,j-1))/dy^2);
end
end
error = max(max(abs(Told-T)));
pcolor(x,y,T)
contour(x,y,T,20)
pause(0.001)
end
end
Now i split this code in two part: cavity_part1.m and cavity_part2.m
The first code cavity_part1.m creates a mesh using the meshgrid command and the data is written to dat file (mesh.dat) as shown.
Part1: cavity_part1.m
clc;
n = 20;
xx = linspace(0,1,n);
yy = xx;
[x,y] = meshgrid(xx,yy);
Data = [x; y];
fileID = fopen(['mesh.dat'],'w');
fprintf(fileID,'%12.8f %12.8f \n',Data);
fclose(fileID);
end
In the second part cavity_part2.m, this mesh file is loaded and the usual cavity code is runned over it. There is a problem the way geometry is being loaded.
Part1: cavity_part2.m
load('mesh.dat');
dx = x(2)-x(1);
dy = dx;
TOL = 1e-2;
T = zeros(n,n);
T(1,1:n) = 0; %bottom
T(n,1:n) = 100; %top
T(1:n,1) = 0; %LEFT
T(1:n,n) = 0; %RIGHT
dt = dx^2/4;
error = 1;
k = 0;
while (error > TOL )
k = k+1
Told = T;
for i = 2:n-1
for j = 2:n-1
T(i,j) = Told(i,j) + dt*((Told(i+1,j)-2*Told(i,j)+Told(i-1,j))/dx^2 ...
+ (Told(i,j+1)-2*Told(i,j)+Told(i,j-1))/dy^2);
end
end
error = max(max(abs(Told-T)));
pcolor(x,y,T)
contour(x,y,T,20)
pause(0.001)
end
end
Lid-driven cavity is just example, if there is simpler example that something similar to this, it can be considered as example for understanding.
Thanks

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by