CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   CFX (https://www.cfd-online.com/Forums/cfx/)
-   -   Matlab contour plots (https://www.cfd-online.com/Forums/cfx/23521-matlab-contour-plots.html)

Chris January 12, 2007 09:11

Matlab contour plots
 
Hi everyone;-)

I have a problem getting matlab to plot the velocity contour which I can see in CFX POST. I tried exporting x,y,z and the velocity vector (.csv) at a giving plane but I can't seem to get the same contour plot in matlab (when using the "contour" command).

Does anyone know how to do this?

Thanks in advance

Regards Chris

Robin January 12, 2007 11:44

Re: Matlab contour plots
 
Check that you are using the same range, number of contours and a similar color map. Did you plot Hybrid or Conservative values and export the same?

-Robin

Chris January 12, 2007 12:46

Re: Matlab contour plots
 
Hi Robin,

Thanks for your answers but my problem is more Matlab related. I exported hybrid values but my problem is when I plot the values in matlab using the "contour" command I get a totally scattered image... Nothing that even resembes the contour found in POST Viewer!

Do you know some commands in matlab that are useful when using it as a visulization tool?

thanks anyways;-)


Robin January 12, 2007 14:25

Re: Matlab contour plots
 
Sorry, been too long since I used Matlab.

Chris January 12, 2007 15:27

Re: Matlab contour plots
 
OK no prob.

thanks anyways....:)

Oli January 15, 2007 05:03

Re: Matlab contour plots
 
Hi,

I'm a relatively experienced MATLAB user; contour or contourf will work fine, as long as you are inputting the correct data in the arrays.

Can you given an example of your MATLAB code, perhaps with a picture of the results you are getting?

Oli

Chris January 15, 2007 09:58

Re: Matlab contour plots
 
Hi Oli

I get this error:

??? Attempted to access x(:,0); index must be a positive integer or logical.

Error in ==> specgraph.contourgroup.schema>LdoDirtyAction at 256 refresh(h);

??? Error occurred while evaluating listener callback.

when I use this code in Matlab:

clear all; close all; clc;

% Importing data from CFX

filename={'..\CSV\proeve\VelocityPlane1_60lMin379e 3Elements'} extension='.csv'; for i=1:length(filename)

file=char(strcat(filename(i),extension));

csv=importdata(file)

Data=csv.data

Header=csv.colheaders

Text=csv.textdata

[rows, cols]=size(Data);

%function call for plot format

index=Data(:,1);

x=Data(:,2);

y=Data(:,3);

z=Data(:,4);

u=Data(:,5);

% [y,index]=sort(y); % x=x(index); % z=z(index); % u=u(index);

D=[y z u];

contourf(y,z,u)

end

The csv file comes directly from CFX and the data is imported correctly. The plane I want to make the contour in is the y,z-plane and jeg would like to see the velocity(u) as the contours...

Hope you can help me!

Oli January 16, 2007 10:35

Re: Matlab contour plots
 
Chris,

The specific error message you get is because you are trying to reference column 0 in the x array, which does not exist.

However, I think there are a few problems with your code, although I think it would be easier if I just posted some code I got to work. I don't have MATLAB available right now, but I'll have a look over it tonight.

Can I just ask so that I can clarify: are you outputting data from a plane in CFX? If so, doesn't the csv file just have 4 columns (x, y, z and U)? Your code suggests you have 5 columns.

Will post again soon,

Oli


chris January 16, 2007 16:34

Re: Matlab contour plots
 
Hi Oli

The five columns are due to a column containing the node number! It's like an index number (1,2,3,4,5...) - the .csv data is exported as follows:

Node Number(:,1) X(:,2) Y(:,3) Z(:,4) Velocity(:,5)

Hope this helps..

I am not sure that I understand the part you wrote about reference to column 0 in the x array, but I will try looking at it.. Regards Chris


Oli January 17, 2007 03:30

Re: Matlab contour plots
 
Chris,

I underestimated the amount of work on at the moment, so I can't run any code myself. However, I can give you a few tips and direct you to some resources you might find useful. Please note that I don't have a copy of MATLAB on me so this is from memory!

1) First of all, the error message:

??? Attempted to access x(:,0); index must be a positive integer or logical.

...occurs because 0 is not a positive integer or logical number. All arrays in MATLAB start at number 1, so x(:,1) would be fine, as long as x is dimensioned that way.

2) The reading of the csv file can be done much less laboriously using the function 'csvread'. Either look in the help, or type 'help csvread' at the command line. Also, the curly brackets around filename are unnecessary: try filename='thisisthefile.csv' then data=csvread(filename) or even data=csvread('thisisthefile.csv'). To add directory information, use square brackets: directory='C:\Work\etc\' then filename=[directory 'thisisthefile.csv'].

3) The reason why contourf doesn't work is that you are only inputting one-dimensional arrays into it. Again, look in the help file or type 'help contour'. Basically, x and y need to define a *structured* (by that I mean 'monotonic') grid, much like the one supplied by the function 'meshgrid' (look it up). Unfortunately, the CFX data is not in this format as it is unstructured, so you need to impose this data upon a structured grid produced by meshgrid using something like the function 'griddata'.

4) Finally, you don't need any 'for' loops for this function. Actually, you very rarely need 'for' loops in MATLAB, and should try to avoid them wherever possible. Your code should just be:

a. START: clear all, close all etc. etc. b. READ THE CSV FILE: use 'csvread' c. DEFINE THE CONTOUR GRID: use 'meshgrid' d. SUPERIMPOSE YOUR DATA ONTO THE CONTOUR GRID: use 'griddata' e. PLOT THE DATA: use 'contourf' with the x and y values from meshgrid, and the variable values from griddata. f. END

No for loops required!!

I recommend these sources for more information:

1) The MATLAB help file is usually very good - I use it often. Search for the page that has a list of functions on and make that one of your favourites.

2) Failing that, try the online help: http://www.mathworks.com/support/pro...tml?product=ML

3) Failing that, there is a very active message board: http://www.mathworks.com/matlabcentral/

MATLAB is a really great language for visualisation - stick with it, even if it has a rather steep learning curve to being with. You wont regret it!

Oli

Chris January 22, 2007 04:43

Re: Matlab contour plots
 
Hi Oli,

Thanks for your detailed answer â€" really nice to get something concrete to go on…ïŠ

I have been working all weekend tying to get a decent contour plot but I still have some problems though…

The problem is that the contour isn't displayed nicely (it is jagged on the boundaries) and takes quit a while for matlab to calculate.

I think the problem is that the matrices produced by meshgrid are not equally spaced. This means that a lot of interpolated points from "meshgrid" are coinciding and resulting in a jagged outer edge.

I tried fixing this by interpolating the interpolated values from "meshgrid" onto an equally spaced matrix using "linspace" and "interp2" unfortunately I get an error message saying I should use "meshgrid" to create 'X' and 'Y' matrices in the "interp2" function â€" but I did this all ready….

This is the code I am using:

clear all; close all; clc;

% Importing data from CFX

% Hybrid values

Hybrid=importdata('../CSV/ContourTest/T_Turbulent_Hybrid.csv');

index_Hybrid=Hybrid(:,1);

xH=Hybrid(:,2);

yH=Hybrid(:,3);

zH=Hybrid(:,4);

uH=Hybrid(:,5);

[YIH ZIH] = meshgrid(yH,zH); UIH = griddata(yH,zH,uH,YIH,ZIH,'cubic');

yiH=linspace(min(yH),max(yH),100); %equally spaced vectors ziH=linspace(min(zH),max(zH),50);

[YiH ZiH]=meshgrid(yiH,ziH); UiH=interp2(YIH,ZIH,UIH,YiH,ZiH); % This doesn't work!! Any ideas?

figure contourf(YiH,ZiH,UiH,'LineStyle','none') axis equal hold on scatter(YIH(:),ZIH(:),30,UIH(:),'filled','MarkerEd geColor','k') % to see interpolated points hold off

Error message Error using ==> interp2 X and Y must be monotonic vectors or matrices produced by MESHGRID.

Do you have any idea why I can't use the "interp2" function here?

Thanks in advance

Regards Chris

afikr July 31, 2017 14:23

2 Attachment(s)
Hello guys,

Have you guys managed to find a solution to this problem.


I am stuck on the same problem.


What I did:

I extracted data from a sliced plane.

Then I changed it to a radial theta coordinate.

Next, I created a finer structured grid using meshgrid function,

Use griddata function to interpolate.

I have tried all types of interpolation , linear, nearest, cubic and natural. But my contour does not look like it shoud. (See image attached).

any help is much appreciated. Thanks

Bodo1993 October 26, 2020 13:25

Hi, I use the file 'surfaces' to extract 2D surfaces of the phase fraction from OpenFOAM transient results. I postprocess the data using MATLAB to plot contours of the phase fraction and make videos. I am wondering if I have to use the interpolation function 'griddata' to do so. The problem with 'griddata' is that it takes time to do the interpolation, e.g. a simulation with 100 time steps may take more than an hour to generate the video. Any suggestions?

ghorrocks October 26, 2020 17:04

My suggestion is you try an appropriate forum. Your question is on OpenFOAM and matlab, and this is the CFX forum.

Bodo1993 October 26, 2020 17:15

Dear ghorrocks,
Thank you for the suggestion. Kindly, the problem in principle is the same.
My question was more for MATLAB than the CFD tool. I will move it to the proper forum.

afikr October 28, 2020 04:16

Quote:

Originally Posted by Bodo1993 (Post 786022)
Hi, I use the file 'surfaces' to extract 2D surfaces of the phase fraction from OpenFOAM transient results. I postprocess the data using MATLAB to plot contours of the phase fraction and make videos. I am wondering if I have to use the interpolation function 'griddata' to do so. The problem with 'griddata' is that it takes time to do the interpolation, e.g. a simulation with 100 time steps may take more than an hour to generate the video. Any suggestions?

How many grid points on that plane are we talking about? I think griddata works fine if you have appropriate grid size.

Bodo1993 October 28, 2020 10:55

Quote:

Originally Posted by afikr (Post 786175)
How many grid points on that plane are we talking about? I think griddata works fine if you have appropriate grid size.

Thanks for the reply. I realized that the grid size that I use in MATLAB for meshgrid is orders higher than the one I use in the CFD software. I am wondering if I should use the same grid size in MATLAB as the CFD software or is it advisable to have a finer one to reduce the interpolation errors?

afikr October 29, 2020 05:32

Quote:

Originally Posted by Bodo1993 (Post 786221)
Thanks for the reply. I realized that the grid size that I use in MATLAB for meshgrid is orders higher than the one I use in the CFD software. I am wondering if I should use the same grid size in MATLAB as the CFD software or is it advisable to have a finer one to reduce the interpolation errors?

I think you don't need to have a finer grid size since your only wanted to create a video from the surface contour plots. Interpolation error is relevant if you intend to calculate some average values across the surface of interest. So I think you can experiment with several grid sizes and compare the CFD result with the one interpolated in Matlab. See if the interpolated result in Matlab manage to capture important details that have been resolved by the CFD solver.


All times are GMT -4. The time now is 20:10.