# C.S.R method in finite element matrix assembly

I have solved the 2D Poisson equation using finite element method with simplex triangular element in MATLAB.

First, I generated the triangular mesh using pdetool in Matlab and it gives me three matrices: p (position of each node), t (element with three vertices) and e. Then, I derived the local stiffness matrix like this,

for I=1:i_elems

conec(i_elem,1)=t(1,i_elem);
conec(i_elem,2)=t(2,i_elem);
conec(i_elem,3)=t(3,i_elem);

node_1=conec(i_elem,1);
node_2=conec(i_elem,2);
node_3=conec(i_elem,3);

NN_e(1,1)=(1/6) * elem_area(i_elem);
NN_e(1,2)=(1/12)* elem_area(i_elem);
NN_e(1,3)=(1/12)* elem_area(i_elem);
NN_e(2,1)=(1/12)* elem_area(i_elem);
NN_e(2,2)=(1/6) * elem_area(i_elem);
NN_e(2,3)=(1/12)* elem_area(i_elem);
NN_e(3,1)=(1/12)* elem_area(i_elem);
NN_e(3,2)=(1/12)* elem_area(i_elem);
NN_e(3,3)=(1/6) * elem_area(i_elem);

% and then I assembled that like this,

NN(node_1,node_1)=NN(node_1,node_1)+NN_e(1,1);
NN(node_1,node_2)=NN(node_1,node_2)+NN_e(1,2);
NN(node_1,node_3)=NN(node_1,node_3)+NN_e(1,3);
NN(node_2,node_1)=NN(node_2,node_1)+NN_e(2,1);
NN(node_2,node_2)=NN(node_2,node_2)+NN_e(2,2);
NN(node_2,node_3)=NN(node_2,node_3)+NN_e(2,3);
NN(node_3,node_1)=NN(node_3,node_1)+NN_e(3,1);
NN(node_3,node_2)=NN(node_3,node_2)+NN_e(3,2);
NN(node_3,node_3)=NN(node_3,node_3)+NN_e(3,3);

end


NN matrix will be a sparse matrix. I just want to use the c.s.r technique and make the assembling process more efficient but I don't know how to assemble the global stiffness matrix in this way. I would be grateful if someone could help me.

• What have you tried already? There is a large amount of material on CSR matrices online. Have you looked at libraries that implement it? Feb 27 at 0:25
• Many thanks for your reply. Actually I already know about c.s.r matrix,but the thing is I can not assemble the local stifness matrix by this way into global form. Before assembling I don't know the position of zero arrays and the exact number of them in each row. Mathematically I know every two node that have no connection together make a zero array in global stiffness matrix. I would be more grateful if you could help me with more detail.
– Resa
Feb 27 at 13:04
• The usual approach is a two-pass algorithm: You first go through your finite element mesh once and store the positions of those entries you will have to write to. From this, you then allocate memory for a CRS matrix. Then you go through the mesh again and actually do the assembly. Feb 27 at 20:50