How to use evaluate_basis_derivatives_all function?

Hello,

I am trying to compute the derivatives of the shape functions at every node in a mesh for a ‘CG1’ vector function space. Looking at previous questions (derivative at a point, Basis function evaluations) and the source code, I think the function ‘evaluate_basis_derivatives_all’, is what I need.

For a vector function space with CG1 tetrahedral elements I think there should be 12 dofs per element (4x3) with 4 basis functions and 12 derivative terms (dN/dx, dN/dy/,dN/dz per basis function). However, the function evaluate_basis_all returns an array of length 36 and the derivative function returns an array of length 108.

from fenics import *
import numpy as np
parameters["form_compiler"]["no-evaluate_basis_derivatives"] = False
mesh = BoxMesh(Point(0, 0, 0), Point(1, 1, 1),10,10,10)
V =  VectorFunctionSpace(mesh, 'CG', 1)
points = V.tabulate_dof_coordinates().reshape((V.dim(), -1))
element = V.element()

dmap = V.dofmap()
tree = mesh.bounding_box_tree()
Num_elem = mesh.num_cells()
Derivative_matrix = np.zeros((Num_elem,12))
for row, pt in enumerate(points):
    cell_id = tree.compute_first_entity_collision(Point(*pt))
    cell = Cell(mesh, cell_id)
    vertex_coordinates = cell.get_vertex_coordinates()
    cell_orientation = cell.orientation()

    Basis_func_values = element.evaluate_basis_all(pt, vertex_coordinates, cell_orientation)
    
    # reshaped derivatives - [dN/dx, dNdy, dNdz]
    Basis_func_derivatives = element.evaluate_basis_derivatives_all(1,pt, vertex_coordinates, cell_orientation).reshape(-1,3)    
    
    #Extract 12 derivatives from Basis_func_derivatives (dN1/dx,dN1/dy,dN1/dz,...,dN4/dz)
    #Map N1,....N4 to verticies of cell
    
    #Derivative_matrix[cell_id] = [mapped derivative data]

How can I extract the 12 terms (dN1/dx, dN1/dy,dN1/dz… dN4/dz) and how do I map these shape function to the correct vertex for the given cell?

Thanks a lot in advance!

2 Likes