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.

Livelink for Matlab and COMSOL v4.2a Meshing and Solution not synchronized...

Please login with a confirmed email address before reporting spam

I have not found any discussion on this topic yet and I am having difficulty finding anyone in COMSOL support in North America that can help with this problem today.


Basically, I simply want to take a solution from COMSOL, import it into MATLAB and plot the solution on the mesh within MATLAB.

For an example problem:

1) I solve the 1D Helmholtz equation on a line segment from x=0...1, with Dirichlet boundary conditions at both ends.
*** I use the PDE Coefficient form setting the source term to zero (c=1,d_a=1,f=0). On a side note, I have tried to use the Helmholtz Equ "classic PDE" and it doesn't work (setting f=0) as it claims the "mass matrix is zero" - which is true, however, unnecessary when solving a sourceless problem (COMSOL please fix this).
*** I have also solved this for 2D and 3D as well, the problem I am describing still exists, but is harder to track down, read on....
2) Either export the solution to comsolserver or save the model as an mph file.
3) In Matlab (started from COMSOL with LiveLink for Matlab), load in the 1d Helmholtz solution:
either use:

fem1 = mphload('filename')

or use

fem1 = ModelUtil.model('Model');

4) load the solution vector (eigenvector) using either:

soltranspose = fem1.sol('sol1').getU(1);

or

soltranspose2 = mphgetu(fem1,'solnum',1)

5) load in the mesh using:

mesh1 = mphxmeshinfo(fem1,'solname', 'sol1');

6) set some variables for the x and y coordinates of the mesh (if you have a 2D mesh), if you have a 1D mesh, then only use the x coordinate:

xxx = mesh1.nodes.coords(1,:);
yyy = mesh1.nodes.coords(2,:);

it is good at this point to look at the mesh to verify that it is the same as in COMSOL:

plot(xxx,yyy,'.')

It looks good!

7) **** It is interesting to note that the solution vector coming from mphgetu is a column vector, but the mesh vector is a row vector. For this reason, I take the transpose of the solution vector to make it the same dimensions as the mesh vector.

8) Now, try to plot the solution for the 1D case:

solu = soltranspose';
plot(solu)

It looks good, like the correct answer should look, a half-wavelength eigenmode (the lowest mode).

9) Now try to plot the solution for the 1D case at the positions of the mesh:

plot(xxx,solu)

PROBLEM! The positions of the mesh are NOT ALIGNED with the solution vector. Although they both have the same size, the ordering of the mesh vector is not aligned to the ordering of the solution vector.

This is obvious when looking at the 1D solution to the Helmholtz equation, but is harder to trace when looking at a 2D or 3D solution.




So, WHY would you want to do this? In other words, why would I like to have total control over a solution from COMSOL? Simple, what if I wanted to perform my own ortho-normalization calculation. In this case, I would need to know the area around each point in the solution so that I could properly weight the integration (quadrature). There are many other reasons why I need to have the solution vector properly coordinated with the mesh, but that is the most obvious reason.


Any thoughts? Is there a property to the "mphxmeshinfo" structure that gives me the correct order for the mesh vector?

Help me Obiwan....

-KLM




4 Replies Last Post 12 févr. 2016, 10:36 UTC−5

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 17 avr. 2012, 20:19 UTC−4
I've noticed similar thing too. To make sure everything is consistent, I export the solution together with mesh to file using COMSOL's export-data feature. I couldn't find better solution to the problem....
I've noticed similar thing too. To make sure everything is consistent, I export the solution together with mesh to file using COMSOL's export-data feature. I couldn't find better solution to the problem....

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 18 avr. 2012, 13:43 UTC−4
Good point. If I am willing to only export a model as a file, then exporting in this fashion works. However, I need to quickly compute something in COMSOL, send it to Matlab, then calculate something and do this over repeatedly. The application is that I am using COMSOL to compute training pairs for a neural network which will need at least 50,000 training vectors in order to get decent convergence. I was hoping to simply use comsolserver, which would bypass the need to write and read a file.

So, to re-cap, when using the Results->Export->Data1 options and selecting the variables for the coordinates of the mesh as well as the solution, then "exporting" the results to a file. In Matlab, reading in that file properly gives the coordinates and the solution vector synchronized to each other.

Trying to do the same through comsolserver still does not work.

Thanks Alexander for your suggestion!

-K
Good point. If I am willing to only export a model as a file, then exporting in this fashion works. However, I need to quickly compute something in COMSOL, send it to Matlab, then calculate something and do this over repeatedly. The application is that I am using COMSOL to compute training pairs for a neural network which will need at least 50,000 training vectors in order to get decent convergence. I was hoping to simply use comsolserver, which would bypass the need to write and read a file. So, to re-cap, when using the Results->Export->Data1 options and selecting the variables for the coordinates of the mesh as well as the solution, then "exporting" the results to a file. In Matlab, reading in that file properly gives the coordinates and the solution vector synchronized to each other. Trying to do the same through comsolserver still does not work. Thanks Alexander for your suggestion! -K

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 19 avr. 2012, 14:14 UTC−4
The trick is not to use mphgetu and mphxmeshinfo. The correct command is:

having exported the model via comsolserver already:

From within Matlab:

fem1 = ModelUtil.model('Model');

% set a handle for your solution:

solu = mpheval(fem1,'u');

% which gives a structure that has the solution and the mesh (not the extended mesh!)

soluvec = solu.d1;
xcoord = solu,p(1,:);
ycoord = solu.p(2,:);

plot3(xcoord,ycoord,soluvec,'.');

That's it!


Thanks to Shupratik Datta at COMSOL Palo Alto for his help.


-Kevin
The trick is not to use mphgetu and mphxmeshinfo. The correct command is: having exported the model via comsolserver already: From within Matlab: fem1 = ModelUtil.model('Model'); % set a handle for your solution: solu = mpheval(fem1,'u'); % which gives a structure that has the solution and the mesh (not the extended mesh!) soluvec = solu.d1; xcoord = solu,p(1,:); ycoord = solu.p(2,:); plot3(xcoord,ycoord,soluvec,'.'); That's it! Thanks to Shupratik Datta at COMSOL Palo Alto for his help. -Kevin

Please login with a confirmed email address before reporting spam

Posted: 9 years ago 12 févr. 2016, 10:36 UTC−5
Thank you so much Kevin.

I encountered the exact same problem as you in the 1D model, and this really helps.
Thank you so much Kevin. I encountered the exact same problem as you in the 1D model, and this really helps.

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.