%SETUP_CONTROL - compute the viscosity derivative with respect to the velocity 
%
%   This script is used in control methods it builds the mass matrix of the system


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%we want the number of non zero elements in the connectivity matrix.
row=zeros(nel*9,1);
col=zeros(nel*9,1);
value=zeros(nel*9,1);
count=-nel+1;
for l=1:3
   for m=1:3 
      count=count+nel;
      col(count:count+nel-1)=index(:,l);
      row(count:count+nel-1)=index(:,m);
      value(count:count+nel-1)=1 ;
   end
end
Connect=sparse(row,col,value,nods,nods);
numnz=nnz(Connect);
clear row col value Connect

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



Zeros=spalloc(nods,nods,0);

Elint=zeros(3,3);
row=zeros(3,1);
col=zeros(3,1);

S=spalloc(nods,nods,numnz);
for n=1:nel
   row=[index(n,1) index(n,2) index(n,3)]';
   col=[index(n,1) index(n,2) index(n,3)]';
   Elint=area(n)*[2 1 1
      1 2 1
      1 1 2]/12;
   S(row,col)=S(row,col)+Elint;
   
end

S=[S Zeros
   Zeros S];
