Am I finished importing an .xml mesh?

Hello all,

I’m having trouble determining if I’ve completed all the necessary steps for importing a .xml mesh into fenics.

I have a .xml mesh. I am importing it using this code:

from dolfin import *

mesh = Mesh('mesh.xml')

Simple enough. However, I started poking around and called the num_edges() and num_faces() methods which returned zero. This did not seem correct. I now know that I need to initialize entities of a certain dimension with the init() method. Here is me doing that.

# Printing values before calling init
print('Edges = %d'%mesh.num_edges()) # returns 0
print('Faces = %d'%mesh.num_faces())   # returns 0

mesh.init(1)
mesh.init(2)

# Printing values after calling init
print('Edges = %d'%mesh.num_edges()) # returns correct, non-zero value
print('Faces = %d'%mesh.num_faces())   # returns correct, non-zero value

My question is: am I through importing the .xml mesh after i’ve initialized those entities? Should it be ready to use in fenics at this point?

Thanks in advance for any feedback.

Anecdotally, I have never had a problem using a Mesh without manually calling init.

Out of curiosity, I took a look at the code on Bitbucket, and my understanding of it is that typical usage of FEniCS should not require manually calling init. From the comments of dolfin/mesh/Mesh.h:

  /// Note that for efficiency, only entities of dimension zero
  /// (vertices) and entities of the maximal dimension (cells) exist
  /// when creating a _Mesh_. Other entities must be explicitly created
  /// by calling init(). For example, all edges in a mesh may be
  /// created by a call to mesh.init(1). Similarly, connectivities
  /// such as all edges connected to a given vertex must also be
  /// explicitly created (in this case by a call to mesh.init(0, 1)).

However, calls to Mesh::init for intermediate-dimensional entities appear to be made on an as-needed basis by functions like Assembler::assemble_exterior_facets, and would not normally need to be invoked by the end-user. For example, from dolfin/fem/Assembler.cpp:

  // Compute facets and facet - cell connectivity if not already computed
  const std::size_t D = mesh.topology().dim();
  mesh.init(D - 1);
  mesh.init(D - 1, D);

From the implementation of Mesh::init, you can see that there is a check to avoid redundant initialization:

  // Skip if already computed
  if (_topology.size(dim) > 0)
    return _topology.size(dim);