Variations of Metric With xTras

Downloading and Installing xAct

Visit the page: http://www.xact.es
Follow installation instructions on http://www.xact.es/download.html
Linux: 
1. Download the tarball xAct_V.tgz (V is the version number)
2. sudo -i ; cd /usr/share/Mathematica/Applications/; tar xvfz ~/Downloads/xAct_V.tgz
Windows:
1. Download the zip file xAct_V.zip (V is the version number)
2. unzip its contents in C:\Program Files\Wolfram Research\Mathematica\<version>\AddOns\Applications\

Read the documentation:
http://www.xact.es/documentation.html

The documentation is also installed locally, after you load the package, run the command:
xTrasHelp[]


 most likely in: (look for the files xTras.pdf, MetricVariations.nb using your file searching tool)
Linux:         /usr/share/Mathematica/Applications/xAct/xTras/Documentation/English/
See:            /usr/share/Mathematica/Applications/xAct/xTras/Documentation/English/xTras.pdf
                     /usr/share/Mathematica/Applications/xAct/xTras/Documentation/English/Guides/xTras.nb
                     /usr/share/Mathematica/Applications/xAct/xTras/Documentation/English/Tutorials/MetricVariations.nb

Metric Variations

xTras automatically defines 'proper' variations with respect to the metric via VarD and VarL whenever you define a metric.
({{VarD[g[-a,-b],cd][L], returns ((δ L )/(δ (Subscript[g, ab]))) while integrating by parts with respect to the 
covariant derivative cd.}, {VarL[g[-a,-b],cd][L], returns (1/(Sqrt[-g]))((δ ((Sqrt[-g]) L) )/(δ (Subscript[g, ab]))) while integrating by parts with respect to the 
covariant derivative cd.}})

First start a session:

Needs["xAct`xTras`"]

DefManifold[M4,4,{λ,μ,ν,ρ,σ,α,β,γ,δ}];
DefMetric[-1,g[-μ,-ν],CD,PrintAs->"g",CurvatureRelations->True];

Get help:

xTrasHelp[]; (* opens a window with a notebook to browse for help *)

You can perform variations with respect to ((δg)^(μν)) or  (Subscript[δg, μν]): 
Variations are expected to be performed on scalar/scalar density expressions

Print[
"(δ/((δg)^(μν)))g= ",VarD[g[  μ,   ν],CD][Detg[]] ,"\n",
"(δ/(Subscript[δg, μν]))g= ",VarD[g[-μ,-ν],CD][Detg[]] ,"\n",
"(δ/((δg)^(μν)))(Sqrt[g])= ",VarD[g[  μ,   ν],CD][(Sqrt[-Detg[]])]
]

VarL simply takes care the (Sqrt[-g]) factors in integrals:

Print[
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))(Sqrt[-g])= ",VarL[g[  μ,   ν],CD][1] ,"\n",
"(1/(Sqrt[-g]))(δ/(Subscript[δg, μν]))(Sqrt[-g])= ",VarL[g[-μ,-ν],CD][1] 
]

Einstein-Hilbert action:

Print[
"(δ/((δg)^(μν))) R= ",VarD[g[  μ,   ν],CD][RicciScalarCD[]],"\n",
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) R)= ",(1/(Sqrt[-Detg[]]))VarD[g[  μ,   ν],CD][(Sqrt[-Detg[]])RicciScalarCD[]]//Simplification,"\n",
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) R)= ",VarL[g[  μ,   ν],CD][RicciScalarCD[]],"\n"
]

We can vary more complicated expressions:

Print[
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) (Subscript[R, αβ])(R^(αβ)))= ",VarL[g[  μ,   ν],CD][RicciCD[-α,-β]RicciCD[α,β]]//ContractMetric//ToCanonical//ScreenDollarIndices
]

FullSimplification[ ] does a better job: Takes Bianchi identities into account, as well as sorting derivatives.

Print[
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) (Subscript[R, αβ])(R^(αβ)))= ",VarL[g[  μ,   ν],CD][RicciCD[-α,-β]RicciCD[α,β]]//FullSimplification[]//ScreenDollarIndices
]

Print[
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) (Subscript[R, αβγδ])(R^(αβγδ)))= ",VarL[g[  μ,   ν],CD][KretschmannCD[]]//FullSimplification[]//ScreenDollarIndices
]

Scalar Fields

DefTensor[ϕ[],M4,PrintAs->"ϕ"];
DefConstantSymbol[{Λ,κ,c}];

Vary the free real scalar field Lagrangian to obtain the energy momentum tensor:

Print[
"(Subscript[T, μν])= -(2/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g])ℒ)= ",-2 VarL[g[  μ,   ν],CD][-(1/2)(CD[-μ]@ ϕ[])(CD[μ]@ ϕ[])- (1/2)((ϕ[])^2)]//Simplify//ScreenDollarIndices
]

Vary with respect to ϕ to obtain equations of motion:

Print[
"(δ/(δϕ))ℒ = ",VarD[ϕ[],PD][-(1/2)(PD[-μ]@ ϕ[])(PD[μ]@ ϕ[])- (1/2)((ϕ[])^2)]//FullSimplification[]//ScreenDollarIndices,"  (flat spacetime)\n",
"     = ",VarD[ϕ[],CD][-(1/2)(CD[-μ]@ ϕ[])(CD[μ]@ ϕ[])- (1/2)((ϕ[])^2)]//FullSimplification[]//ScreenDollarIndices,"\n",
"     = ",VarD[ϕ[],CD][+(1/2)ϕ[](CD[-μ]@CD[μ]@ ϕ[])- (1/2)((ϕ[])^2)]//FullSimplification[]//ScreenDollarIndices
]

Other terms, present in other theories:

Print[
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) (E^ϕ) R)= ",VarL[g[  μ,   ν],CD][(E^(ϕ[])) RicciScalarCD[]]//FullSimplification[]//ScreenDollarIndices
]

Print[
"(1/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) (Subscript[G, μν])(∇^μ)ϕ (∇^ν)ϕ)= ",VarL[g[  μ,   ν],CD][EinsteinCD[-μ,-ν] (CD[μ]@ϕ[]) (CD[ν]@ϕ[])]//FullSimplification[]//ScreenDollarIndices
]

Electromagnetic Field

DefTensor[A[-μ],M4];
DefTensor[F[-μ,-ν],M4,Antisymmetric[{1,2}]]

Construct  rules to compute F=dA

FtodA = MakeRule[{F[-μ,-ν],PD[-μ][A[-ν]]-PD[-ν][A[-μ]]},MetricOn-> All,ContractMetrics-> True,UseSymmetries-> True];
FtoDA = MakeRule[{F[-μ,-ν],CD[-μ][A[-ν]]-CD[-ν][A[-μ]]},MetricOn-> All,ContractMetrics-> True,UseSymmetries-> True];

The rules can be used for any indices, upstairs or downstairs:

{F[-μ,-ν] /. FtodA, F[-μ,-ν] /. FtoDA,  F[μ,-ν] /. FtoDA, F[μ,ν] /. FtoDA}

The stress energy tensor: vary with respect to (g^(μν)).

Print[
"(Subscript[T, μν])= -(2/(Sqrt[-g]))(δ/((δg)^(μν)))((Sqrt[-g]) (-(1/4)(F^(μν))(Subscript[F, μν])))= ",tmn = (VarL[g[  μ,   ν],CD][ (1/2)F[-μ,-ν] F[μ,ν]]//FullSimplification[]//ScreenDollarIndices),"\n",
"   = ",tmn/.FtodA//FullSimplification[]//ScreenDollarIndices,"\n",
"   = ",tmn/.FtoDA//FullSimplification[]//ScreenDollarIndices
]

Equations of motion:  vary with respect to (Subscript[A, μ])

Print[
"(δ/(Subscript[δA, μ]))(-(1/4)(Subscript[F, μν])(F^(μν)))= ",VarD[A[-μ],PD][-(1/4)F[-μ,-ν] F[μ,ν]/.FtodA]//FullSimplification[]//ScreenDollarIndices,"\n",
"             = ",VarD[A[-μ],CD][-(1/4)F[-μ,-ν] F[μ,ν]/.FtoDA]//FullSimplification[]//ScreenDollarIndices
]

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).