Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

get certain elements of a mesh, then refine mesh there

Please login with a confirmed email address before reporting spam

Hi,
i'm quite new to Comsol and i'm not sure if the following is possible to do with the software combined with Matlab:

There is a simple 2D square geometry with a mesh on it.
Now i want to get a list of certain elements of that mesh: The list should contain all triangles that are located on a circle on that 2D plane defined by an equation like (x-1)^2+(y-1)^2=0.5.
Then i need to refine the mesh around the mesh elements on the list.

I thought it might be possible to determine the triangles by checking if a triangle has nodes that are inside AND outside of the circle (or if one or two nodes exactly on it). This could be done by inserting the coordinates in (x-1)^2+(y-1)^2<=0.5 and checking it the equation holds. The problem is that i don't know how to start or if this approach is possible at all.

Thanks for any suggestions :)

2 Replies Last Post Jan 29, 2011, 5:11 a.m. EST

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Jan 28, 2011, 3:48 a.m. EST
In the matlab interface you can get the the node coordinates as

fem.mesh.p

The node numbers of the individual elements are stored in

fem.mesh.t

If x0,y0,r describes your circle,

vec1 = (fem.mesh.p(1,fem.mesh.t(1,:)) - x0).^2 + (fem.mesh.p(2,fem.mesh.t(1,:)) - y0).^2 < r^2;

, will give you a boolean vector for which nodes (1st local node in the elements) are inside your circle. You can construct

vec2 = (fem.mesh.p(1,fem.mesh.t(2,:)) - x0).^2 + (fem.mesh.p(2,fem.mesh.t(2,:)) - y0).^2 < r^2;
vec3 = (fem.mesh.p(1,fem.mesh.t(3,:)) - x0).^2 + (fem.mesh.p(2,fem.mesh.t(3,:)) - y0).^2 < r^2;

Corresponding to the 2nd and 3rd local node in the elements. I think

idx = find(and(vec1+vec2 + vec3 ~= 0, vec1+vec2 + vec3 ~= 3));

will give you the elements that have nodes both inside and outside the circle (as you suggested). You can then proceed to use the meshrefine command with this idx variable.
In the matlab interface you can get the the node coordinates as fem.mesh.p The node numbers of the individual elements are stored in fem.mesh.t If x0,y0,r describes your circle, vec1 = (fem.mesh.p(1,fem.mesh.t(1,:)) - x0).^2 + (fem.mesh.p(2,fem.mesh.t(1,:)) - y0).^2 < r^2; , will give you a boolean vector for which nodes (1st local node in the elements) are inside your circle. You can construct vec2 = (fem.mesh.p(1,fem.mesh.t(2,:)) - x0).^2 + (fem.mesh.p(2,fem.mesh.t(2,:)) - y0).^2 < r^2; vec3 = (fem.mesh.p(1,fem.mesh.t(3,:)) - x0).^2 + (fem.mesh.p(2,fem.mesh.t(3,:)) - y0).^2 < r^2; Corresponding to the 2nd and 3rd local node in the elements. I think idx = find(and(vec1+vec2 + vec3 ~= 0, vec1+vec2 + vec3 ~= 3)); will give you the elements that have nodes both inside and outside the circle (as you suggested). You can then proceed to use the meshrefine command with this idx variable.

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Jan 29, 2011, 5:11 a.m. EST
Thank you so much for this elegant solution, this totally works for me :)
I use fem.mesh = meshrefine(fem,'tri',idx); to refine the mesh then.
Thank you so much for this elegant solution, this totally works for me :) I use fem.mesh = meshrefine(fem,'tri',idx); to refine the mesh then.

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.