Plotting CFD data contour in matlab
26 views (last 30 days)
Show older comments
Muhammad Atif
on 17 Aug 2021
Edited: Nikhil Shinde
on 19 Aug 2022
Hi,
I have imported CFD data into matlab and I can see x,y,z and U column in my data and I seperate them in a column matrix. I want to plot the contour of this data. I am cluless here. I have tried many approaches. To my understanding about plotting a contour:
step1: plot x and y according to grid size(n) required and draw a meshgrid. [X,Y]=meshgrid(x,y). this will generate X and Y of n by n.
Step 2: constuct "V" of n by n matrix of velocity by rearranging the data.
Step 3: Plot contour using pcolor(x,y,V) or contour(x,y,V)
However I am stuck on step2 how to convert V into n by n order. I used reshape() function which is giving error (Index in position 1 is invalid. Array indices must be positive integers or logical values.
U_reshaped= reshape(U(zeros(200^2,1), X(:), Y(:)), [200 200]); % I want 200 by 200 however size of my U is only 33000 by 1. I have seen this line used by one code which is working fine in his code in this format. but when I am trying on my data it is giving me this error.
Is there any easy way to visualise data in Matlab or any help with this command. Thanks
4 Comments
Nikhil Shinde
on 19 Aug 2022
Edited: Nikhil Shinde
on 19 Aug 2022
Please take a look at Delaunay and Trisurf functions in matlab. Extract your vertices data in a matrix. Following is the code that I used in my posprocessing assignment, You can tailor it according to your needs
vel_files = dir('*.csv'); %Only velocity data
velnum = length(vel_files);
vel_data = cell(1, velnum);
for k = 1:velnum-1
filename = vel_files(k).name;
vel_data{k} = readmatrix(filename);
end
Tvert = readtable('vertices.csv'); %vertices data
x = Tvert.Var1;
y = Tvert.Var2;
vertices= cat(2,x ,y);
DT = delaunay(vertices);% Delaunay Triangulation
VELALL=cell2mat(vel_data); trisurf(DT,vertices(:,1),vertices(:,2),VELALL(:,141),'EdgeColor','none'); axis tight; daspect([1 1 1]); shading interp; colormap jet; view(2);title('Ux t=100s')
Accepted Answer
Star Strider
on 18 Aug 2021
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/714947/snapshot_vel-0001.txt', 'VariableNamingRule','preserve')
[Uy,ix1] = unique(T1.('y-coordinate'))
Unfortunately, the data do not appear to be gridded (here, regular intervals in ‘ix1’), so it will be necessary to interpolate. There are two likely options, griddata and scatteredInterpolant. This approach uses griddata:
N = 50; % Controls Grid Resolution
xv = linspace(min(T1.('x-coordinate')), max((T1.('x-coordinate'))), N);
yv = linspace(min(T1.('y-coordinate')), max((T1.('y-coordinate'))), N);
[Xc,Yc] = ndgrid(xv, yv);
Xv = griddata(T1.('x-coordinate'), T1.('y-coordinate'), T1.('x-velocity'), Xc, Yc);
figure
contourf(Xc, Yc,Xv)
figure
surfc(Xc, Yc, Xv)
This illustrates the general approach.
If you want to include a third dimension for the contour, it will be necessary to use the isosurface function. I leave those details to you.
.
2 Comments
Star Strider
on 19 Aug 2021
I adapted your code to mine:
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/714947/snapshot_vel-0001.txt', 'VariableNamingRule','preserve')
X=T1{:,2};
Y=T1{:,3};
U=T1{:,5};
V=T1{:,6};
W=T1{:,7};
X1=min(X):0.1:max(X);
Y1=min(Y):0.1:max(Y);
[xmesh, ymesh] = meshgrid(X1,Y1);
[x1 y1 u1]=griddata(X,Y,U,xmesh,ymesh);
[x1 y1 v1]=griddata(X,Y,V,xmesh,ymesh);
[x1 y1 w1]=griddata(X,Y,W,xmesh,ymesh);
res_vel=sqrt(u1.^2+v1.^2+w1.^2)
figure
[mc,hcf] = contourf(x1,y1,res_vel, 150);
Lvls = hcf.LevelList
colormap(turbo)
shading interp
colorbar
figure
[mc,hcf] = contourf(x1,y1,res_vel, [25:0.1:28]);
Lvls = hcf.LevelList
colormap(turbo)
shading interp
colorbar
This bears no strong resemblance to the .png file, and I am not certain that is even possible.
Adding the ‘150’ to the first contourf argument tells it to create 150 contours, so with this, it added some detail.
Restricting the contours from 25 to 28 with increments of 0.1 creates the second contourf plot. This is likely as good as it is possible to get.
I encourage you to experiment with the options the contourf function offers to get something that more closely approximates the result you want.
.
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!