MeshRefinementFunction according to the documentation is an option for DiscretizeRegion. Is there an analogue for ToElementMesh?

The following code

f = Function[{vertices, area}, If[Mean[vertices] > 1, area > 0.1, area > 0.01]]; 
ToElementMesh[Interval[{0, 2}], MeshRefinementFunction -> f]

gives the error

ToElementMesh::mrff: The MeshRefinementFunction Function[{vertices, area}, If[Mean[vertices]>1, area > 0.1, area > 0.01]] is not valid and will be ignored. The function does not return either True or False.

so I have to use an ugly workaround

mymesh = DiscretizeRegion[Interval[{0, 2}], MeshRefinementFunction -> f]; 
  • $\begingroup$ I found a similar question here, unanswered: community.wolfram.com/groups/-/m/t/1928814 $\endgroup$ Jun 24, 2020 at 15:16
  • 1
    $\begingroup$ The error message says it all. Try to apply your function f to an actual list of vertex positions vertices and a number area. Then you will see that the If statement does not evaluate because Mean[vertices] is a vector and not a number. $\endgroup$ Jun 24, 2020 at 15:26
  • $\begingroup$ @HenrikSchumacher I am afraid this is a too smart observation for me.:( How should I fix the function f to work with ToElementMesh? $\endgroup$ Jun 24, 2020 at 15:46
  • 1
    $\begingroup$ That depends on what you try to express by Mean[vertices] > 1... Maybe you meant to write f = Function[{vertices, area}, If[Thread[Mean[vertices] > 1], area > 0.1, area > 0.01]]? > might thread over vectors in other languages, but it does not in Mathematica: Things like {0,0,0}>1 do not produce True or False, and thus If[{0,0,0}>1, [...], [...]] stays just unevaluated. So no True nor False either. $\endgroup$ Jun 24, 2020 at 15:50
  • $\begingroup$ @HenrikSchumacher I just wanted a fine mesh for 0<x<1 and a rough mesh for x>1. $\endgroup$ Jun 24, 2020 at 15:52

1 Answer 1



The real observation is that MeshRefinementFunction does not work for 1D with ToElementMesh. Yes, that's unfortunately the case but you can easily use

f = Function[{vertices, area}, 
   If[Mean[vertices] > 1, area > 0.1, area > 0.01]];
mr = DiscretizeRegion[Interval[{0, 2}], MeshRefinementFunction -> f];

I'll add a note to the documentation until this is implemented.

Old Answer

The Reference Page of ToElementMesh has examples. Please have a look there.

  • 1
    $\begingroup$ Thank you. This was exactly that I was reading for the last several hours, together with Element Mesh Generation tutorial. Can not get it to work in 1D case, alas. $\endgroup$ Jun 24, 2020 at 15:59
  • 1
    $\begingroup$ @YashaGindikin, see update. $\endgroup$
    – user21
    Jun 24, 2020 at 16:39
  • $\begingroup$ BTW, calling mr in a context like this ParallelTable[NDEigenvalues[...,x\in mr]] does not work. Like the mr is not distributed across the parallel kernels. However, explicit construction like ParallelTable[NDEigenvalues[...,x\in ToElementMesh[DiscretizeRegion[...]]]] does work, fortunately. $\endgroup$ Jun 24, 2020 at 18:08

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .