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

Save only nodal values for quadratic Lagrange elements (p2)

+1 vote

Hello,

Is there a way to save the solution only at nodal values (vertices of triangular elements) instead than at all degrees of freedom? I would like to do that for Lagrange quadratic elements.

Now, after computing the solution a1, I do the following:

X=V.tabulate_dof_coordinates()
X.resize((V.dim(), 2))
x = X[:,0]
y = X[:,1]
a_values  = a1.vector().array()
np.savetxt("results/a_p2.csv", np.c_[x, y, a_values], delimiter=",")

Is there a way to save only nodal values?

asked May 18, 2017 by caterinabig FEniCS User (1,460 points)

2 Answers

+3 votes
 
Best answer

Also you can do something like this:

from dolfin import *

mesh = UnitSquareMesh(5,5)
V = FunctionSpace(mesh, "CG", 2)

# Extract dofs at mesh vertices
dofmap = V.dofmap()
dofs = dofmap.dofs(mesh, 0)

# Test
u = interpolate(Expression("x[0]*x[1]"), V)   # Create P2 function
print u.vector()[dofs]                        # Print only dofs at vertices
answered May 18, 2017 by hernan_mella FEniCS Expert (19,460 points)
selected May 19, 2017 by caterinabig

Hey, that's pretty nice!

Thank you hernan_mella!

+4 votes

Interpolate the P2 function onto the P1 FunctionSpace, and save the resulting NumPy array.

from fenics import *

mesh = UnitSquareMesh(2,2)
P1   = FunctionSpace(mesh, 'CG', 1)
P2   = FunctionSpace(mesh, 'CG', 2)

x = Function(P2)

y = interpolate(x, P1)

y_a = y.vector().array()
answered May 18, 2017 by pf4d FEniCS User (2,970 points)
edited May 19, 2017 by pf4d

You probably wanted to interpolate to P1, ;)

Thank you!!! Easy solution :)

...