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

How to synchronize the order of the dofs of a finite-element function with the order of mesh vertices

0 votes

This question has probably been asked before, but I would appreciate some guidance on how to solve it in the following concrete, simple setting:

Consider a regular, low-resolution mesh on the unit square and a CG-1 finite-element space based on it. To fix ideas, let us define the mesh and the finite-element space like so:

from dolfin import *

resolution_param = 3

mesh = UnitSquareMesh(resolution_param, resolution_param)
num_mesh_vertices = (resolution_param  + 1) ** 2
mesh_vertex_coords = mesh.coordinates()

V = FunctionSpace(mesh, 'CG', 1)

Consider also the following function:

f = Expression("x[0] - x[1]")
f_proj = project(f, V)
f_proj_arr = f_proj.vector().array()

THE QUESTION

The value f_proj_arr[j] is not the value of f at mesh_vertex_coords[j].

How can I construct a new array f_proj_new_arr out of f_proj_arr such that f_proj_new_arr[j] is the value of f at mesh_vertex_coords[j]?

asked Feb 3, 2016 by kdma-at-dtu FEniCS Novice (590 points)

Clarification: The value f_proj_arr[j] is not the value of f at mesh_vertex_coords[j], which can be seen by running the following block of code:

print
print "-" * 30
print "Output #1"
print "-" * 30
print

for (x, y) in mesh_vertex_coords:
    print x, y
    print ' ' * 30, f_proj(x, y)

print
print "-" * 30
print "Output #2"
print "-" * 30
print

for i in range(num_mesh_vertices):
    print mesh_vertex_coords[i]
    print ' ' * 30, f_proj_arr[i]

1 Answer

+1 vote
 
Best answer

Try the following:

f_proj_arr = f_proj.compute_vertex_values()

This will be numbered the same as your vertices

answered Feb 3, 2016 by Tormod Landet FEniCS User (5,130 points)
selected Feb 18, 2016 by kdma-at-dtu

Thanks, that worked. I'm accepting your answer.

...