GenerateMesh via geometryFromEdges not working when large number of edges

I am trying to write a code that will autonomously create a mesh surrounding the edges of geometric shapes created from MATLAB contour function. E.g.:
[X,Y,Z] = peaks;
v = [-1,-1];
contourf(X,Y,Z,v)
I have got a working code that will successfully achieve this with a number of edges up to 30. Shown below:
When the number of edges exceeds 30, I recieve the following error:
Error using pde.AnalyticGeometry (line 56)
Invalid geometry. To specify a function handle, use
the @ prefix.
Error in pde.EquationModel/geometryFromEdges (line
48)
gm = pde.AnalyticGeometry(gedges);
Error in generateMesh_from_contours (line 559)
geometryFromEdges(model,g);
I am unsure why this is the case. I build the geometry from the edges of the contour lines. The geometry description is built via the decsg function (https://uk.mathworks.com/help/pde/ug/decsg.html), which is comprised of 3 inputs: geometry description matrix, set formula, name-space matrix.
  • The geometry description matrix, gdm, is created from the coordinate data that can be extracted to describe the contour lines from the contour plot.
  • The set formula, sf, is created from an algorithm I wrote to iteratively deduct contour lines from the domain (a rectangle).
  • And the name-space matrix, ns, is just a collumn vector of all the contour line IDs.
Maybe the issue is to do with memory, when the sf or gdm is too large? One thought I had was to potentially create more than one model and concatenate the data after, but I can't see a way to do this.
Please find the code attached.
In the section named 'Surface function' you can toggle different the equations that describes surfaces with different amount of contour lines (denoted by N=..., e.g. N=30).
The bulk of the work relevant to building the geometry in order to generate the mesh is done in the 'Create geometry and build SF equation' section; bar creating the geometry description matrix, gdm, which is done at various points of the 'Internal values of contours' section.
Thank you for your support and advice in advance.

14 Comments

Hi Elliot,
Thanks a lot for a clear problem description and reproduction steps. Could you please let me know what release of MATLAB you are using? It appears that this problem could be release-dependent. Thank you!
Best Regards,
Konstantin
Hi Konstantin,
Thanks for getting back to me.
I am using R2019b.
Best,
Elliot
Hi Konstantin,
I don't mean to bump this post. I just wondered if you determind if this was a release-dependant issue or not?
Does the code run for more than 30 contour lines on your end?
Thanks again,
Elliot
Hi Elliot, do you possibly figure out what might be the issue?
Hi Katherine, no I never did figure out why the GenerateMesh function wouldn't work beyond 30 contour lines. I am still interested in what might be causing the problem so if you do find out, please do let me know. Cheers
@Elliot Bontoft Hi, sorry to get back to you this late. Please try remove the bracket in your sf function like so
sf = [sf,'+',cf_i(i-1,:)]; % just for demonstration
then your code should be fine. I have tested all your not working functions. They work well.
Hi @Katherine Zheng, thank you for getting back to me on this regardless of the time span. Great spot. I originally had the brackets included to enforce an order to the set formula (say for example you have layers of contours and need to ensure they are added or removed from the prior layer) - however I definitely overkilled it.
@Elliot Bontoft Good to know it helped! I just find it out accidently and I still need to study your code to figure out how the contours are generated. BTW, do you plan to extend this 2D to 3D?
@Katherine Zheng The contours lines are generated through the intrinsic function
[C,h] = contour(X,Y,Z,([1 1]));
that creates a model of the data provided (X,Y,Z) and then exports the contour lines as coordinate data (C). Alternatively you can use the function
[C] = contourc(X(1,:),Y(:,1),Z,[1 1]);
which is faster but does not enable you to plot the contours. They both describe the contours (C) as a 2xN matrix that houses all the contour's coordinate data. In the first row the start of each contour is flagged by '0' and in the second row it presents the number of coordinates that are used to describe the contour, i.e. '23' would mean the following 23 collumns of the matrix are to describe that contour line.
You can isolate the coordinate data of these contour lines so that they are not all stored in one matrix. The 'Isolating all contours to seperate data files via the contour matrix, C' part of my code is how i did this. Hope that helps.
I would like to, i dont think it would be too painful to extend to 3D. However, it really depends on if i get the opportunity to extend my research; so as it stands, no, I won't. Would be keen to hear how you get on with your plan to convert this to CAD files.
@Elliot Bontoft Thanks for your explaination! I will let you know if I make it 3D.
Hi @Elliot Bontoft, sorry to bother you but I keep having error at the mesh refinement step. I think it relates to the mesh properties parameters. Do you have any suggestions of how to modify them?
Hi @Katherine Zheng, this can happen when the contour data has coordinate points that are very close together, which when meshing forces the mesh to create extremely small elements. One method to help this is to remove one of the close together points, another is to refine the meshing parameters (i.e. smaller average size, larger element ratio and grow rate). However, if the points are very close together the second method may not be enough to work. Cheers
Hi @Elliot Bontoft, thank you so much for your suggestion. You are right smaller the average size helps but the nodes are exceeding 250000 which is too many. So I removed some close points in the contour and the mesh nodes decreased dramatically. Yeah!

Sign in to comment.

 Accepted Answer

remove the bracket in the sf function like so
sf = [sf,'+',cf_i(i-1,:)]; % just for demonstration

More Answers (0)

Community Treasure Hunt

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

Start Hunting!