deterministic mesh creation

Issue #18 new
Thomas Hisch created an issue

I would expect the meshes, created in the following script, to always be identical and therefore contain the same number of cells

import dolfin
import mshr

resolution = 20

c = mshr.Sphere(dolfin.Point(0, 0, 0), 0.9)
cm1 = mshr.generate_mesh(c, resolution)
cm2 = mshr.generate_mesh(c, resolution)

print('#cells1 %s' % cm1.num_cells())
print('#cells2 %s' % cm2.num_cells())

Is it possible to set the seed used in mshr somehow?

Comments (6)

  1. Benjamin Dam Kehlet

    The reason that the generated meshes differ is CGAL's mesh generator which uses random rays for generating points on the surface of the domain in the initial phase of the surface remeshing). I agree that the randomness is unfortunate, but I'm a bit reluctant to fixing the seed as the default choice, but I'll see if we can make this option available through the parameter system.

  2. Thomas Hisch reporter

    I tried to hardcode the seed in MeshGenerator.cpp but I still don't get the same number of cells in cm1 and cm2. Here is my patch

    --- a/src/MeshGenerator.cpp
    +++ b/src/MeshGenerator.cpp
    @@ -20,6 +20,8 @@
    
     #include <mshr/CSGCGALMeshGenerator2D.h>
     #include <mshr/CSGCGALMeshGenerator3D.h>
     #include <mshr/TetgenMeshGenerator3D.h>
    +#include <CGAL/Random.h>
    +
    
     #include <dolfin/log/log.h>
     #include <dolfin/mesh/BoundaryMesh.h>
    @@ -37,6 +39,7 @@ void generate(dolfin::Mesh& mesh,
       if (geometry.dim() == 2)
       {
         CSGCGALMeshGenerator2D generator;
    +    CGAL::default_random = CGAL::Random(0); 
         generator.parameters["mesh_resolution"] = resolution;
         generator.generate(geometry, mesh);
       }
    @@ -45,6 +48,7 @@ void generate(dolfin::Mesh& mesh,
         if (backend == "cgal")
         {
           CSGCGALMeshGenerator3D generator;
    +      CGAL::default_random = CGAL::Random(0); 
           generator.parameters["mesh_resolution"] = resolution;
           generator.generate(geometry, mesh);
         }
    
  3. Log in to comment