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

Accessing the coordinates of a degree of freedom

+5 votes

Hello!

I have the list of the global dof indices by doing the following*:

V = FunctionSpace(mesh, 'DG', 1)
dofs_0 = V.dofmap().dofs()

Now, I would like to access the coordinates of a given degree of freedom k in dofs_0. Is there a simple way to do that?

I saw that we can access the coordinates of all dofs by using thetabulate_all_coordinates() method of GenericDofMap but when I try this:

print V.dofmap().tabulate_all_coordinates()

I get the following error:

TypeError: GenericDofMap_tabulate_all_coordinates expected 2
arguments, got 1

which really confuses me since the documentation does not mention any argument for this method...

I also saw the dof_to_vertex_map method but I do not know if that would do it since it is said that it "only works for FunctionSpace with dofs exclusively on vertices. "

Any suggestions?

Thanks a lot in advance!
Vincent



*nicely provided by MiroK on this forum: see http://fenicsproject.org/qa/3865/updating-a-function-that-is-in-mixedfunctionspace if interested

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

1 Answer

+11 votes
 
Best answer

Hi, tabulate_all_coordinates method requires Mesh object as an argument, see here or here. Where in the documentation did you see otherwise? Anyways, to get the coordinate consider

from dolfin import *

mesh = UnitSquareMesh(1, 1)
gdim = mesh.geometry().dim()
V = FunctionSpace(mesh, 'DG', 1)
dofmap = V.dofmap()

dofs = dofmap.dofs()
# Get coordinates as len(dofs) x gdim array
dofs_x = dofmap.tabulate_all_coordinates(mesh).reshape((-1, gdim))

for dof, dof_x in zip(dofs, dofs_x):
    print dof, ':', dof_x

The dof_to_vertex_map solution won't work with DG spaces for all the dofs are
associated with cells. Consider

# See how many dofs in element are associated with vertices 
print dofmap.num_entity_dofs(0)
# All the dofs are associated with cell
print dofmap.num_entity_dofs(2), V.dim()/mesh.num_cells()  
answered Jun 21, 2014 by MiroK FEniCS Expert (80,920 points)
selected Jun 23, 2014 by V_L

Thanks again MiroK!
Interestingly, the solution above returns an error if V = M.sub(0) (where M = MixedFunctionSpace([V]*n) ) but it's okay, I found a way around it

As to where in the documentation I found tabulate_all_coordinates() does not require any arguments, here it is:
http://fenicsproject.org/documentation/dolfin/1.4.0/python/programmers-reference/cpp/fem/GenericDofMap.html#dolfin.cpp.fem.GenericDofMap.tabulate_all_coordinates

Anyway, thank you so much for the help!

Just a note for others searching: the API has changed slightly now in master due to this commit: https://bitbucket.org/fenics-project/dolfin/commits/ad26cbc501438db4c75fed43f24a129e1f39d623

With current master (post 1.6 release) you must now ask the function space for the coordinates, not the dofmap.

dofs_x = V.tabulate_dof_coordinates().reshape((-1, gdim))

Hi, running your code generates the error:

AttributeError: 'GenericDofMap' object has no attribute 'dofs'

Ok it's because I have Fenics 1.2.0 I need to update.

...