How can I refine a portion of my mesh?

I have a 2-D grid that needs to be very fine to capture the physics. The problem with using meshgrid or ndgrid is that it plots points outside of my boundary. In order to have a grid that is fine enough, the code either takes several hours to run or the computer runs out of memory. I have identified what points are outside of my geometry in the form of a mxn logical (1's for any point in the boundary, 0's for any point outside of the boundary). I am trying to refine my mesh with the geometry after finding the boundaries. The final mesh will be a non-uniform mesh with 'NaN' at points outside of the geometry and a small grid spacing in x and y-directions. Ideally, the grid would be very fine near the upper and lower walls (y-direction) and widen in the middle, and decrease in grid spacing in the x-direction as the x increases.
Any thoughts on how I can refine an existing grid at specified locations?

5 Comments

Can you post some code, image or data?
Here is the portion of my code that I am trying to change:
load flow.mat
load surface_flow.mat
varnames = flow.Properties.VariableNames;
% size of grid will be [3*n1-1,ny]
% Create spacing in x-direction
n1 = 200; n2 = n1*2+1; ny = 500;
x1 = linspace(min(flow.x),median(flow.x),n1);
x2 = linspace(median(flow.x),max(flow.x),n2);
xq = [x1,x2(2:end)]; % create grid for x-direction
yq = linspace(min(flow.y),max(flow.x),ny); % create grid for y-direction
[flowmat.x,flowmat.y] = meshgrid(xq,yq); % create 2-D grid
% Interpolate raw data onto grid
for i = 3:length(varnames)
flowmat.(varnames{i}) = griddata(flow.x, flow.y, flow.(varnames{i}),...
flowmat.x, flowmat.y);
end
% calculate variables
flowmat.DensityGrad = gradient(flowmat.Density); % denisty gradient
flowmat.U_velocity = flowmat.Momentum_x./flowmat.Density; % velocity in x
flowmat.V_velocity = flowmat.Momentum_y./flowmat.Density; % velocity in y
varnames = fieldnames(flowmat);
% find boundary of nozzle - everything that has a velocity <= 0 is outside
% of the nozzle
idx = (flowmat.U_velocity <= 0 | isnan(flowmat.U_velocity));
for i = 1:length(varnames)
flowmat.(varnames{i})(idx) = nan(1); % adjust variables
end
The data is imported from the SU2 (Stanford Unstructured Squared) CFD script. I am trying to do some post-processing in MATLAB. The data is in the form of a table - each column containing a 1xm vector of data.
Is it possible for me to use this data or it's for SpaceX projects?
load flow.mat
load surface_flow.mat
Where is the image?

Sign in to comment.

 Accepted Answer

The arguments to ndgrid (or meshgrid) do not need to be uniform, they can be anything that makes sense to those functions.
Example —
x = -3:0.1:3;
v = exp(-x.^2/3);
[X,Y] = ndgrid(v);
figure
plot(X, Y)
Experiment to get the result you want.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!