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

Accessing the local index of a dof from the local index of the cell

+1 vote

Dear all,

Considering a DG0 space, I have been surprised to observe that the following code:

mesh = RectangleMesh(-1, -1, 1, 1, 5, 5)
V = FunctionSpace(mesh, 'DG', 0)
dofmap = V.dofmap()
...
for cell_no in range(mesh.num_cells()):
  print dofmap.cell_dofs(cell_no), dofmap.dofs()[cell_no] 
  # first: print the global index of the dof associated to the cell of local index cell_no
  # second: print the global index of the dof associated to the dof of local index cell_no

prints 2 different results if more than 1 processor is used.

In other words, the local index of a dof differs from the local index of the cell containing that dof... (even though there is a 1 on 1 relationship)

What would then be the way to determine one from the other?
I'm convinced that with the solution to this problem, I will be able to solve my previous question

Any help would be appreciated!

asked Nov 20, 2014 by V_L FEniCS User (4,440 points)

1 Answer

+2 votes
 
Best answer

dofmap.dofs()[cell_no] gives the global dofs, whereas dofmap.cell_dofs(cell_no) gives the local dofs. Both should be the same, except that for process one the values are dofmap.ownership_range()[0] higher. The docstring of dofmap.dofs() seems to be wrong, something you could report as a bug on bitbucket.

answered Nov 21, 2014 by mikael-mortensen FEniCS Expert (29,340 points)
selected Nov 22, 2014 by V_L

Thank you for answer!

With all due respect however, I don't think that's correct.

To illustrate this, if I change the code above a little bit:

for cell_no in range(mesh.num_cells()):
  print "rank = ", rank, ", local cell = ", cell_no, ", ", \
        dofmap.cell_dofs(cell_no), " vs ", dofmap.dofs()[cell_no]
  # first: print the rank of the process
  # second: print the local cell index cell_no
  # third: print the global index of the dof associated to the cell of local index cell_no
  # forth: print the global index of the dof associated to the dof of local index cell_no

here is the output I get using 4 processors:

rank =  3 , local cell =  0 ,  [49]  vs  37
rank =  1 , local cell =  0 ,  [24]  vs  12
rank =  2 , local cell =  0 ,  [36]  vs  25
rank =  0 , local cell =  0 ,  [11]  vs  0
rank =  3 , local cell =  1 ,  [48]  vs  38
rank =  0 , local cell =  1 ,  [10]  vs  1
rank =  2 , local cell =  1 ,  [35]  vs  26
rank =  2 , local cell =  2 ,  [34]  vs  27
rank =  0 , local cell =  2 ,  [9]  vs  2
rank =  3 , local cell =  2 ,  [47]  vs  39
rank =  3 , local cell =  3 ,  [46]  vs  40
rank =  1 , local cell =  1 ,  [23]  vs  13
rank =  0 , local cell =  3 ,  [8]  vs  3
rank =  0 , local cell =  4 ,  [7]  vs  4
rank =  2 , local cell =  3 ,  [33]  vs  28
rank =  2 , local cell =  4 ,  [32]  vs  29
rank =  3 , local cell =  4 ,  [45]  vs  41
rank =  1 , local cell =  2 ,  [22]  vs  14
rank =  0 , local cell =  5 ,  [6]  vs  5
rank =  3 , local cell =  5 ,  [44]  vs  42
rank =  2 , local cell =  5 ,  [31]  vs  30
rank =  0 , local cell =  6 ,  [5]  vs  6
rank =  3 , local cell =  6 ,  [43]  vs  43
rank =  2 , local cell =  6 ,  [30]  vs  31
rank =  1 , local cell =  3 ,  [21]  vs  15
rank =  2 , local cell =  7 ,  [29]  vs  32
rank =  3 , local cell =  7 ,  [42]  vs  44
rank =  0 , local cell =  7 ,  [4]  vs  7
rank =  1 , local cell =  4 ,  [20]  vs  16
rank =  2 , local cell =  8 ,  [28]  vs  33
rank =  3 , local cell =  8 ,  [41]  vs  45
rank =  0 , local cell =  8 ,  [3]  vs  8
rank =  0 , local cell =  9 ,  [2]  vs  9
rank =  3 , local cell =  9 ,  [40]  vs  46
rank =  2 , local cell =  9 ,  [27]  vs  34
rank =  0 , local cell =  10 ,  [1]  vs  10
rank =  1 , local cell =  5 ,  [19]  vs  17
rank =  3 , local cell =  10 ,  [39]  vs  47
rank =  2 , local cell =  10 ,  [26]  vs  35
rank =  0 , local cell =  11 ,  [0]  vs  11
rank =  2 , local cell =  11 ,  [25]  vs  36
rank =  3 , local cell =  11 ,  [38]  vs  48
rank =  1 , local cell =  6 ,  [18]  vs  18
rank =  3 , local cell =  12 ,  [37]  vs  49
rank =  1 , local cell =  7 ,  [17]  vs  19
rank =  1 , local cell =  8 ,  [16]  vs  20
rank =  1 , local cell =  9 ,  [15]  vs  21
rank =  1 , local cell =  10 ,  [14]  vs  22
rank =  1 , local cell =  11 ,  [13]  vs  23
rank =  1 , local cell =  12 ,  [12]  vs  24

To me it seems that both dofmap.cell_dofs(cell_no) and dofmap.dofs()[cell_no] give global dofs.

More specifically, I still think that dofmap.cell_dofs(cell) returns the vectors of the global indices of the dofs associated to the cell cell_no while dofmap.dofs()[cell_no] returns the global index of the dof associated to the dof of local index cell_no.

I have no idea why but it seems that the local index of the dof can be obtained from the local index of the cell using (if the number of processors is strictly greater than 1):

 local_dof = mesh.num_cells() - cell_no - 1 # local index of the dof associated to the local index of the cell cell_no

and using:

local_dof = cell_no

otherwise.

In that case, dofmap.cell_dofs(cell_no) and dofmap.dofs()[local_dof] return the same thing.

This trick doesn't seem very satisfying however as I have no idea why it works...

I cannot reproduce your example. My answer is correct for development version of dolfin.

I'm using 1.4

it's all good if it's already been fixed in the development version!

thanks a lot for your answer!!

...