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...
Posted 2012年4月17日 GMT-4 16:40 Interfacing, Geometry, Mesh, Results & Visualization Version 4.2a, Version 5.1 4 Replies
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
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
Attachments:
4 Replies Last Post 2016年2月12日 GMT-5 10:36