Clear[coord, metric,inversemetric, affine, Riemann, Ricci, Rscalar, Einstein, 
      Tmatter,Tmattermix, DcovTmatter,r,θ,ϕ,t,B,A,m];

n=4;
coord = {t,r,θ,ϕ};

metric=(({{-a[t]^2, 0, 0, 0}, {0, a[t]^2, 0, 0}, {0, 0, a[t]^2, 0}, {0, 0, 0, a[t]^2}}));

inversemetric=Inverse[metric]//FullSimplify;
inversemetric//MatrixForm;  mixmetric = metric.inversemetric;

Tmattermix= (({{-ρ[t], 0, 0, 0}, {0, P[t], 0, 0}, {0, 0, P[t], 0}, {0, 0, 0, P[t]}})) ;

Tmatter = Tmattermix.metric//FullSimplify;

affine=Simplify@Table[(1/2)*Sum[(inversemetric[[i,s]])*
               ( D[metric[[s,j]],coord[[k]] ]
               + D[metric[[s,k]],coord[[j]] ]
               - D[metric[[j,k]],coord[[s]] ] ),{s,1,n}], {i,1,n},{j,1,n},{k,1,n}];
listaffine:=Table[If[UnsameQ[affine[[i,j,k]],0],
                     {ToString[ Γ[i-1,j-1,k-1]],affine[[i,j,k] ]}],{i,1,n},{j,1,n},{k,1,j}]
TableForm[Partition[DeleteCases[Flatten[listaffine],Null],2],TableSpacing->{2,2}]

Riemann = Simplify@Table[ D[affine[[i,j,l]],coord[[k]] ] - D[affine[[i,j,k]],coord[[l]] ]
                        + Sum[affine[[s,j,l]] affine[[i,s,k]] - affine[[s,j,k]] affine[[i,s,l]], {s,1,n}]
                         ,{i,1,n},{j,1,n},{k,1,n},{l,1,n} ];

listriemann := Table[ If[ UnsameQ[Riemann[[i,j,k,l]],0],
                         {ToString[R[i-1,j-1,k-1,l-1]],Riemann[[i,j,k,l]]}] ,
                          {i,1,n},{j,1,n},{k,1,n},{l,1,k-1}]
TableForm[Partition[DeleteCases[Flatten[listriemann],Null],2],TableSpacing->{2,2}]

Ricci:=Ricci=Simplify@Table[Sum[Riemann[[i,j,i,l]],{i,1,n}],{j,1,n},{l,1,n}];
Ricci//MatrixForm//FullSimplify;
listricci:=Table[If[UnsameQ[Ricci[[j,l]],0],{ToString[R[j-1,l-1]],Ricci[[j,l]]}],{j,1,n},{l,1,j}]
TableForm[Partition[DeleteCases[Flatten[listricci],Null],2],TableSpacing->{2,2}]

Rscalar=Simplify[Sum[inversemetric[[i,j]]*Ricci[[i,j]],{i,1,n},{j,1,n}] ]

Einstein:=Einstein=Simplify[Ricci-(1/2)Rscalar*metric]

listeinstein:=Table[If[UnsameQ[Einstein[[j,l]],0],
                   {ToString[G[j,l]],Einstein[[j,l]]}],{j,1,n},{l,1,j}]
TableForm[Partition[DeleteCases[Flatten[listeinstein],Null],2],TableSpacing->{2,2}]

MixEinstein=Einstein.inversemetric//FullSimplify;
listMixeinstein:=Table[If[UnsameQ[MixEinstein[[j,l]],0],
                          {ToString[Gmix[j,l]],MixEinstein[[j,l]]}],{j,1,n},{l,1,j}]
TableForm[Partition[DeleteCases[Flatten[listMixeinstein],Null],2],TableSpacing->{2,2}]