This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Mesh Refine - MeshFunction

+2 votes

Hi,
I have a question about mesh refinement under Dolfin.
If I create a mesh and a MeshFunction associated to it, like for example a

FacetFunction facet(mesh);

which store some information about the facet (e.g. label of the facet)
and later I do something like

new_mesh = refine(mesh)

is there any way in which I can add the same information to the new facet
of the new_mesh ?

asked Jun 6, 2013 by gedeone FEniCS User (1,110 points)
retagged Jun 7, 2013 by chris_richardson

1 Answer

+3 votes
 
Best answer

Yes, you can use the function adapt for this. The Python syntax is:

f = FacetFunction(mesh)
refined_mesh = refine(mesh)
refined_f = adapt(f, refined_mesh)
answered Jun 6, 2013 by logg FEniCS Expert (11,790 points)
selected Jun 11, 2013 by Jan Blechta
And under C++ it would be the same?
Yes, but with C++ syntax.
I'm sorry to bother you, but under the c++ interface I can find only a

const MeshFunction<std::size_t>& adapt(const MeshFunction<std::size_t>& mesh_function, boost::shared_ptr<const Mesh> adapted_mesh)

and nothing similar to

const MeshFunction<std::size_t>& adapt(const MeshFunction<std::size_t>& mesh_function, Mesh& adapted_mesh)
Ops sorry I realize that I was doing two stupid things together:
Using a FacetFunction instead of a MeshFunction
Initializing in a wrong way the shared pointer
Now the code compile with something like

          dolfin::Mesh new_mesh;
          new_mesh = dolfin::refine(mesh);
          new_mesh.init(D - 1, D);
          dolfin::MeshFunction<uint> facet_r(new_mesh, D-1);
          boost::shared_ptr< dolfin::Mesh > mesh_par(&mesh);
          new_mesh.set_parent(mesh_par);
          boost::shared_ptr< dolfin::Mesh > mesh_p(&new_mesh);
          facet_r = dolfin::adapt(facet, mesh_p);

But it gives a run-time error:

*** Error:   Unable to adapt mesh function.
*** Reason:  Unable to extract information about parent mesh entites.
*** Where:   This error was encountered inside adapt.cpp.
*** Process: 0

I'm sorry but I can't find any solution which seems to work:

      dolfin::Mesh mesh;
      dolfin::MeshEditor editor;
      editor.open(mesh, D, D);
      //create the mesh ...

      mesh.init(mesh.topology().dim()-1);
      dolfin::MeshFunction<uint> facet(mesh,D-1);
      // create the MeshFunction

      dolfin::Mesh new_mesh;
      new_mesh = dolfin::adapt(mesh);
      new_mesh.init(D - 1, D);
      dolfin::MeshFunction<uint> facet_refined(new_mesh, D-1);
      boost::shared_ptr< dolfin::Mesh > mesh_p(new dolfin::Mesh);
      *mesh_p = new_mesh;
      facet_refined = dolfin::adapt(facet, mesh_p);

This code gives me the following run-time error:

*** Error:   Unable to adapt mesh function.
*** Reason:  Unable to extract information about parent mesh entites.
*** Where:   This error was encountered inside adapt.cpp.

I try to solve it adding any combination of the following statements:

boost::shared_ptr< dolfin::Mesh > mesh_par(new dolfin::Mesh);
*mesh_par = mesh;
new_mesh.set_parent(mesh_par);
boost::shared_ptr< dolfin::Mesh > mesh_p(new dolfin::Mesh);
*mesh_p = new_mesh;
boost::shared_ptr< dolfin::MeshFunction< uint> > parent_facet = new_mesh.data().create_mesh_function("parent_facet",D-1);
*parent_facet = facet;

I'm yet getting the same error:

*** Error:   Unable to adapt mesh function.
*** Reason:  Unable to extract information about parent mesh entites.
*** Where:   This error was encountered inside adapt.cpp.

Looking at the adapt.cpp file, it seems that the problem is related with parent, where parent is:

 // Extract parent map from data of refined mesh
boost::shared_ptr<MeshFunction<std::size_t> > parent;
  if (mesh_function.dim() == dim)
    parent = adapted_mesh->data().mesh_function("parent_cell");
  else if (mesh_function.dim() == (dim - 1))
    parent = adapted_mesh->data().mesh_function("parent_facet");
  else
    dolfin_not_implemented();

I don't understand if the MeshFunction "parent_cell" and "parent_facet" should have been automatically generated by the adapt function or if I have to initialize them in some way.

Thanks

Marco

Hi,

I think that in the file UniformMeshRefinement.cpp the MeshFunction "parent_cell" and "parent_facet" are not initialized, while it is done in the LocalMeshRefinement.cpp

I don't understand if it is a bug related to UniformMeshRefinement or if in this case the two MeshFunction are obvious and I should be able to add them by hand.

thanks

Marco

...