Affine Connection and Curvature

The Friedmann Metric

Initialization

Needs["xAct`xCoba`"]

(*$PrePrint=ScreenDollarIndices;
$DefInfoQ=False;
$UndefInfoQ=False;*)

DefManifold[M,4,{λ,μ,ν,ρ,σ,α,β,γ,δ}];
dimM = DimOfManifold[M];dimM1 = dimM-1;

Here is the definition of the coordinate system, and the metric:
Simply define the list coords = {....} and the matrix gmatrix

coords = {t[],χ[],θ[],ϕ[]};
(*DefConstantSymbol[mass,PrintAs->"M"];*)
DefScalarFunction[ascale,PrintAs->"a"]; (*Use as e.g. ascale[t[],r[]] for a function of (t,r)*)
gmatrix = DiagonalMatrix[
{-1,((ascale[t[]])^2),((ascale[t[]])^2)((Sin[χ[]])^2),((ascale[t[]])^2)((Sin[χ[]])^2) ((Sin[θ[]])^2)}
];
DefChart[ch,M,{0,1,2,3},coords,ChartColor->Blue];
g=CTensor[gmatrix,{-ch,-ch}];
SetCMetric[g,ch,SignatureOfMetric-> {3,1,0}];
CD = CovDOfMetric[g];

Print[
"(Subscript[g, μν])= ",ComponentArray[g[{-μ,-ch},{-ν,-ch}]]//MatrixForm,"    ,       ",
"(g^(μν))= ",ComponentArray[g[{  μ,   ch},{   ν,  ch}]]//MatrixForm,"\n",
"g = ", Determinant[g,ch][]," = ",Det[ComponentArray[g[{-μ,-ch},{-ν,-ch}]]]//Simplify
]

Affine Connection

Print nonzero components:
(Subscript[(Γ^λ), μν])=(1/2)(g^(λσ))((∂_μ) (Subscript[g, σν])+(∂_ν) (Subscript[g, σμ])-(∂_σ) (Subscript[g, μν]))
The components of the Christoffel symbols are collected  by the ComponentArray[expr] function.

list =ComponentArray[Christoffel[CD,PDch][{α,ch},{-β,-ch},{-γ,-ch}]];
list = Table[
If[
UnsameQ[list[[i,j,k]],0],{Subscript[Superscript["Γ",i-1],j-1,k-1],list[[i,j,k]]}
],
{i,1,Length[list]},{j,1,Length[list]},{k,1,j}
];
Partition[DeleteCases[Flatten[list],Null],2]//TableForm

Curvature

Print nonzero components of Riemann: (Subscript[(R^μ), νρσ])
(Subscript[(R^λ), ρμν]) = (∂_μ) (Subscript[(Γ^λ), νρ])-(∂_ν) (Subscript[(Γ^λ), μρ]) + (Subscript[(Γ^λ), μσ]) (Subscript[(Γ^σ), νρ]) - (Subscript[(Γ^λ), νσ]) (Subscript[(Γ^σ), μρ])  (Carroll+Hartle’s convention)
xCoba has Wald’s convention, which for a Levi-Civita Connection gives the same reult after some index raising/lowering.

The components of the Riemann tensor are collected by the ComponentArray[expr] function.

list=ComponentArray[Riemann[CD][{α,ch},{-β,-ch},{-γ,-ch},{-δ,-ch}]];
list = Table[
If[
UnsameQ[list[[i,j,k,l]],0],{Subscript[Superscript["R",i-1],j-1,k-1,l-1]
,list[[i,j,k,l]]}
],
{i,1,Length[list]},{j,1,Length[list]},{k,1,Length[list]},{l,1,k-1}
];
Partition[DeleteCases[Flatten[list],Null],2]//TableForm

The Riemann tensor with all lower indices: (Subscript[R, μνρσ])

list=ComponentArray[Riemann[CD][{-α,-ch},{-β,-ch},{-γ,-ch},{-δ,-ch}]];
list = Table[
If[
UnsameQ[list[[i,j,k,l]],0],{Subscript["R",i-1,j-1,k-1,l-1]
,list[[i,j,k,l]]}
],
{i,1,Length[list]},{j,1,i-1},{k,1,Length[list]},{l,1,k-1}
];
Partition[DeleteCases[Flatten[list],Null],2]//TableForm

The Riemann tensor with all upper indices: (R^(μνρσ))

list=ComponentArray[Riemann[CD][{α,ch},{β,ch},{γ,ch},{δ,ch}]];
list = Table[
If[
UnsameQ[list[[i,j,k,l]],0],{Superscript[Superscript[Superscript[Superscript["R",i-1],j-1],k-1],l-1]
,list[[i,j,k,l]]}
],
{i,1,Length[list]},{j,1,i-1},{k,1,Length[list]},{l,1,k-1}
];
Partition[DeleteCases[Flatten[list],Null],2]//TableForm

The Ricci tensor: (Subscript[R, μν])

list=ComponentArray[Ricci[CD][{-α,-ch},{-β,-ch}]];
list = Table[
If[
UnsameQ[list[[i,j]],0],{Subscript["R",i-1,j-1]
,list[[i,j]]}
],
{i,1,Length[list]},{j,1,i}
];
Partition[DeleteCases[Flatten[list],Null],2]//TableForm

Ricci scalar:

Print["R = ",RicciScalar[CD][]]

(R^2) scalar

Print["(R^2) = ",Kretschmann[CD][]]

Einstein tensor: (Subscript[G, μν])

list=ComponentArray[Einstein[CD][{-α,-ch},{-β,-ch}]];
list = Table[
If[
UnsameQ[list[[i,j]],0],{Subscript["G",i-1,j-1]
,list[[i,j]]}
],
{i,1,Length[list]},{j,1,i}
];
Partition[DeleteCases[Flatten[list],Null],2]//TableForm

Weyl tensor:

list=ComponentArray[Weyl[CD][{-α,-ch},{-β,-ch},{-γ,-ch},{-δ,-ch}]];
list = Table[
If[
UnsameQ[list[[i,j,k,l]],0],{Subscript["C",i-1,j-1,k-1,l-1]
,list[[i,j,k,l]]}
],
{i,1,Length[list]},{j,1,i-1},{k,1,Length[list]},{l,1,k-1}
];
Partition[DeleteCases[Flatten[list],Null],2]//TableForm

Geodesic Equations

DefTensor[u[μ],M];

The geodesic equations are:
((du^μ)/(dτ))+(Subscript[(Γ^μ), ⋁ρ])(u^ν)(u^ρ)=0
geqs is a list with the second term geqs[μ+1]= (Subscript[(Γ^μ), ⋁ρ])(u^ν)(u^ρ) for each μ=0,..,d-1.
(careful, in the expressions below, when e.g.  (u^(3^2))=((((u^3)))^2), the squared terms don’t appear nicely)

geqs=ComponentArray[Christoffel[CD,PDch][{μ,ch},{-ν,-ch},{-ρ,-ch}] u[{ν,ch}]u[{ρ,ch}]]//ContractBasis//Simplify;
For[i=1,i<= Length[geqs],i++,Print["(d/(dτ))",u[{i-1,ch}],"+(",geqs[[i]],")=0"]]

DefScalarFunction[dt  ,PrintAs->"(OverDot[t])"];DefScalarFunction[dχ  ,PrintAs->"(OverDot[χ])"];
DefScalarFunction[dθ  ,PrintAs->"(OverDot[θ])"];DefScalarFunction[dϕ  ,PrintAs->"(OverDot[ϕ])"];
DefScalarFunction[ddt,PrintAs->"(Overscript[t, ¨])"];DefScalarFunction[ddχ,PrintAs->"(Overscript[χ, ¨])"];
DefScalarFunction[ddθ,PrintAs->"(Overscript[θ, ¨])"];DefScalarFunction[ddϕ,PrintAs->"(Overscript[ϕ, ¨])"];
u   = CTensor[{dt[]   ,dχ[]  ,dθ[]  ,dϕ[]   },{ch}];
du = CTensor[{ddt[],ddχ[],ddθ[],ddϕ[]},{ch}];
{u,du}

geqs=ComponentArray[Christoffel[CD,PDch][{μ,ch},{-ν,-ch},{-ρ,-ch}] u[{ν,ch}]u[{ρ,ch}]]//ContractBasis//Simplify;
For[i=1,i<= Length[geqs],i++,Print[du[{i-1,ch}],"+(",geqs[[i]],")=0"]]

Acknowledgements

This notebook has been programmed by Konstantinos Anagnostopoulos, Physics Department, National Technical University of Athens, Greece, while he was an instructor of the 4th year undergraduate course "General Relativity and Cosmology". It was created for fun, but it may turn out to be useful to everyone studying the General Theory of Relativity for the first time.
Email: konstant@mail.ntua.gr
Web:    http://physics.ntua.gr/konstant 
It is offered under a GPL/CC BY 4.0 license (in that order, depending on whether they apply on the programming part or the text part of the notebook).