Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

extracting information with mphinterp() - ndgrid vs meshgrid

Please login with a confirmed email address before reporting spam

Hi,

I could not easily find anything about this topic, so excuse if I missed something.

In page 288 of the LiveLinkForMATLABGuide, there's an example of how to extract information using mpheval() with coordinates. The example states to use:

x0 = [0,1e-2,2.5e-2,5e-2]; y0 = x0; z0 = [5e-3,1e-2,1.1e-2];

[x,y,z] = meshgrid(x0,y0,z0); xx = [x(:),y(:),z(:)]';

T = mphinterp(model,'T','coord',xx);

which gives a (3,N) vector for coordinates (first index marks x, y, or z). The quantity returned, T, has the same size (3,N). Suppose I now want to view the result of one slice (say the x-z plane, for y=2.5e-2). The question is, how can I reshape() T into a (3,Nx,Ny,Nz) tensor, where then I could select all the x-coordinates of T for Ny=2, i.e. in MATLAB:

T3D(1,:,2,:);

After lots of digging around, I found that meshgrid() is not the correct function for this, but one should use ndgrid() instead. Here's an example MATLAB script to show this. We expect the coordinates of an axis to be equal in its plane regardless of where we slice. In other words, the 2D matrix of the x or z coordinate of the x-z plane for arbitrary y, should be equal:

clc;

x0 = [11,12];

y0 = [21,22,23];

z0 = [31,32,33,34];

[x,y,z] = ndgrid(x0,y0,z0); %% works!

%[x,y,z] = meshgrid(x0,y0,z0); %% doesn't!

coords = [x(:),y(:),z(:)]';

xyz = reshape(coords,3,numel(x0),numel(y0),numel(z0));

disp('x-y (x along z)'); % x-y plane, x coordinates, along z

x1=squeeze(xyz(1,:,:,1));

x2=squeeze(xyz(1,:,:,2));

x3=squeeze(xyz(1,:,:,3));

x4=squeeze(xyz(1,:,:,4));

all(x1(:)==x2(:)) && all(x3(:)==x3(:)) && all(x3(:)==x4(:))

disp('x-y (y along z)'); % x-y plane, y coordinates, along z

x1=squeeze(xyz(2,:,:,1));

x2=squeeze(xyz(2,:,:,2));

x3=squeeze(xyz(2,:,:,3));

x4=squeeze(xyz(2,:,:,4));

all(x1(:)==x2(:)) && all(x3(:)==x3(:)) && all(x3(:)==x4(:))

disp('x-z (x along y)'); % x-z plane, x coordinates, along y

x1=squeeze(xyz(1,:,1,:));

x2=squeeze(xyz(1,:,2,:));

x3=squeeze(xyz(1,:,3,:));

all(x1(:)==x2(:)) && all(x3(:)==x3(:))

disp('x-z (z along y)'); % x-z plane, z coordinates, along y

x1=squeeze(xyz(3,:,1,:));

x2=squeeze(xyz(3,:,2,:));

x3=squeeze(xyz(3,:,3,:));

all(x1(:)==x2(:)) && all(x3(:)==x3(:))

disp('y-z (y along x)'); % y-z plane, y coordinates, along x

x1=squeeze(xyz(2,1,:,:));

x2=squeeze(xyz(2,2,:,:));

all(x1(:)==x2(:))

disp('y-z (z along x)'); % y-z plane, y coordinates, along x

x1=squeeze(xyz(3,1,:,:));

x2=squeeze(xyz(3,2,:,:));

all(x1(:)==x2(:))

Just to finalize, here's an example of how I extract the x-z plane of the By magnetic field component at the first and last y index, in MATLAB, in my code:

[Bx,By,Bz, unit] = mphinterp(model,{'mf.Bx','mf.By','mf.Bz'},'dataset','dset1','coord',coords/lu,'solnum',1); % solnum referes to initial time

By3D = reshape(By,NDx,NDy,NDz);

figure; imagesc(squeeze(By3D(:,1,:)));
figure; imagesc(squeeze(By3D(:,end,:)));

lu is the length unit used in COMSOL (I converted everything to meters). NDx,NDy,NDz are the vector lengths of the original x,y,z coordinate vectors. Hope this helps!


0 Replies Last Post Nov 22, 2019, 4:21 a.m. EST
COMSOL Moderator

Hello Roy Shiloh

Your Discussion has gone 30 days without a reply. If you still need help with COMSOL and have an on-subscription license, please visit our Support Center for help.

If you do not hold an on-subscription license, you may find an answer in another Discussion or in the Knowledge Base.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.