Convert mesh node, element, face, displacement, strain and stress to numpy arrays to export as csv files

Can you tell me how to export fenics data to CSV file in the following format?

csv files data columns
nodes.csv node_id, x, y, z
element.csv element_id, node_list
face.csv face_id, element_list, node_list
properties.csv node_id, disp_x, disp_y, disp_z, strain_xx, strain_yy, strain_zz, strain_xy, strain_yz, strain_zx, stress_xx, stress_yy, stress_zz, stress_xy, stress_yz, stress_zx

For node,

dim = V.dim()
N = mesh.geometry().dim()
coordinates = V.tabulate_dof_coordinates().reshape(dim,N)

I can use meshio library to get node coordinates and cell data, but there will index mismatch between the node coordinates and properties data.

Thank you

As having data on mesh nodes is equivalent of having a CG 1 FunctionSpace, only certain functions are nicely visualized on nodes.
You can Get nodal values in the same order as mesh nodes by using compute_vertex_values

Thanks a lot for the reply.

But I am getting only a 1D array instead of a 3D array (ux, uy, uz). Can you how can I get 3D array (N X 3 array) of the displacement vector?

Do it per component

for i in range(3):
    ui=u.sub(i,deepcopy=True)
    values_i = ui.compute_vertex_values()

Thanks a lot. It worked.
I guess for strain and stress, the code will

strain_list = []
stress_list = []
for i in range(9):
    strain_list.append(strain.sub(i,deepcopy=True).compute_vertex_values())
    stress_list.append(stress.sub(i,deepcopy=True).compute_vertex_values())

As stress and strain is based on taking the derivative of CG functions, They should really be represented as DG functions, and is not very suitable for vertex (CG-1) representation.

Why do you use csv and not XDMF and write_checkpoint, as it can visualize DG functions?

I want to export this data to my fortran code and I can’t see any good documentation on XDMF library for fortran.

Will the above code (that I mentioned), lists the data in the same order as node data
i.e.,
stress at node_id[0] == stress_list[:,0]

It will match the points in the mesh geometry.

However, I am warning you that exporting a discontinuous function as a continuous function is a slippery slope if you want to use it as input for another computation. I cases Where the strain is actually discontinuous between cells, the nodal value is not uniquely defined.

Then can you suggest best way to export this data?

As this is very dependent of your Fortran code, and how it can handle such data, there isn’t much I can do.
You can Get the components of each stress and strain by projecting them to the appropriate DG space, and Get their coordinates by using tabulate dof coordinates.
The first question is what function space do you use for computation of deformation in your code.

V = VectorFunctionSpace(mesh, ‘CG’, 1)

Then each component of your stress/strain tensor is a piecewise constant per element, and should be treated as such (“DG”-0 function space)

So you mean that I should assign

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

T = TensorFunctionSpace(mesh, 'DG', 0)

Yes, I do. Then you have to figure out how to load data per cell to fortran.

Thanks for the info.

Can you also tell me how to export the element data and which node (node id) it is connected to?

As the data is connected to cells not nodes, you Need to make a cell mapping (should match the mesh topology in the case of DG 0), i.e. Cell 0 had value 0, cell 1 has value 1 etc, as long as you run it in serial. It gets more complicated If you run it in parallel

From your suggestions, I feel like it would be better to save as xdmf file and then use python xdmf library to convert it into csv files. Am I right?

Well, i dont know any XDMF -> CSV converters, so i cant help you much there.