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

How to vectorize vtx2dof and mesh definition

0 votes

Hi

I already have a code that produces a

  1. vector vtx2dof that maps vertex numbers to DOF numbers for a CG1 field.
  2. mesh using the mesh editor.

The problem with the current code is that it is not vectorized, i.e. it uses for loops. I would appreciate feedback on how to vectorize the code.

vtx2dof code. Something like "cells = dof.all_cell_dofs" would be ideal.

ntri = mesh.num_cells()
dof = FunctionSpace(mesh,'CG',1).dofmap()
cells = numpy.empty([ntri, 3], dtype = numpy.intc)
for i in range(ntri):
    cells[i, :] = dof.cell_dofs(i)
cells_ = cells.flatten()
I = cells_.argsort()
cells_ = cells_[I]
d = array([True]+(cells_[range(0,ntri*3-1)] != cells_[range(1,ntri*3)]).tolist())
vtx2dof = mesh.cells().flatten()[I][d]

mesh definition code (mesh defined in arrays n_x, n_y and n_enlist)

n_mesh = Mesh()
ed = MeshEditor()
ed.open(n_mesh, 2, 2)
ed.init_vertices(len(n_x))
for i in range(len(n_x)):
  ed.add_vertex(i, n_x[i], n_y[i])
ed.init_cells(len(n_enlist)/3)
for i in range(len(n_enlist)/3):
  ed.add_cell(i, n_enlist[i * 3], n_enlist[i * 3 + 1], n_enlist[i * 3 + 2])
ed.close()

Thanks a lot

asked Dec 2, 2013 by KristianE FEniCS Expert (12,900 points)

1 Answer

+1 vote
 
Best answer

The first can be accomplished by:

vtx2dof = vertex_to_dof_map(V)

The second could be accomplished by porting it to C++ using the compiled_extension_module.

answered Dec 2, 2013 by johanhake FEniCS Expert (22,480 points)
selected Apr 15, 2016 by KristianE
OK, can the same thing be done for cells
...