Skip to main content

mesh - Is there a way to make MaxCellMeasure and MaxBoundaryCellMeasure comparable?



I'm trying to learn a little about meshes used for FEA and such.


It seems like a good strategy if you are creating a mesh for a region is to use a finer mesh near features than the mesh used over most of the region, because then you can get higher accuracy near these features without having to do a ton of unnecessary calculations.


So I've created this test structure:


Needs["NDSolve`FEM`"];
myrect = Rectangle[{0, 0}, {5, 8}];
mydisk = Disk[{2, 4}, 1];
rectcirc = RegionDifference[myrect, mydisk];

And I'm trying to make a comparison of the cell size and the number of mesh elements, to illustrate that with a relatively small increase in the number of (boundary) elements, you can get a much more accurate picture. However, I'm running into trouble comparing them.


For example, here's one mesh:



rmesh = NDSolve`FEM`ToElementMesh[rectcirc, 
MaxCellMeasure -> {"Length" -> .5}]
rmesh["Wireframe"]

enter image description here


So that just limits the length of the maximum side of a cell to .5, and creates 1061 elements.


If I instead do:


rmesh2 = NDSolve`FEM`ToElementMesh[rectcirc, 
MaxCellMeasure -> {"Length" -> .5}, "MaxBoundaryCellMeasure" -> .1]
rmesh2["Wireframe"]


enter image description here


Which has 2008 elements, and is obviously finer around the boundaries. The documentation for MaxBoundaryCellMeasure is actually a little unclear in my opinion, because the default syntax for MaxCellMeasure (that is, doing MaxCellMeasure->someVal) is to set the max area of a cell (I set it instead by the Length parameter), but it appears that the default of MaxBoundaryCellMeasure is to set the max length of the boundary cells, as far as I can tell, though it actually doesn't say.


Now, I want to compare this last mesh with one where all the cells are the size of the boundary ones in that last one, to basically show how many more cells you'd need to have all the cells be that size. So I do:


rmesh3 = NDSolve`FEM`ToElementMesh[rectcirc, 
MaxCellMeasure -> {"Length" -> .1}]
rmesh3["Wireframe"]

enter image description here


Which has ~27,000 points. However, this is obviously a lot denser than the previous one. You can see this even more if you zoom in (top one is rmesh2, bottom is rmesh3):



enter image description here


So it seems like rmesh3 is making the cells a lot smaller than I really asked for. What am I missing, why aren't rmesh3's cells roughly the same size as the boundary ones of rmesh2?


Edit: Okay, I think I know the reason now. Because rmesh3 has a smaller interior mesh upper limit, it constrains the boundary mesh size, so even though those boundary elements can be as big as the ones for rmesh2 (which is shown by measuring the Max of rmesh2 and rmesh3 as @user21 did), on average they aren't as big:


Mean@BoundaryEdgeLength@rmesh2
Mean@BoundaryEdgeLength@rmesh3
0.0867
0.0564

This makes sense. But what doesn't make sense to me is, if we define the MaxBoundaryCellMeasure for each before passing it to ToElementMesh, shouldn't they then have the same average boundary cell size (because, at the time of the creation of their boundary meshes, ToBoundaryMesh had no information that it was going to be passed to a ToElementMesh with one value of MaxCellMeasure or another)?


For example:



rmesh2 = NDSolve`FEM`ToElementMesh[
NDSolve`FEM`ToBoundaryMesh[rectcirc,
"MaxBoundaryCellMeasure" -> .1],
MaxCellMeasure -> {"Length" -> .5}];
rmesh2["Wireframe"]
rmesh3 = NDSolve`FEM`ToElementMesh[
NDSolve`FEM`ToBoundaryMesh[rectcirc,
"MaxBoundaryCellMeasure" -> .1],
MaxCellMeasure -> {"Length" -> .1}];
rmesh3["Wireframe"]


However, you can tell by looking at them and from this, that nothing has changed:


Mean@BoundaryEdgeLength@rmesh2
Mean@BoundaryEdgeLength@rmesh3
0.0867
0.0564

So that implies that ToElementMesh is reworking the boundary mesh somehow, right? That would be strange if that's the case...




Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...