In the last two post I introduce the classes formalTensor (which has been not that useful) and Tensor, the latter include a series of attributes.
Now is time to get started with GR 😛
I left this class just as Sergei defined, it looked weird to me at the beginning, but again I decipher it 😉 with the help of Reinteract, b.t.w., those who want to play around with Reinteract, it’s in Ubuntu repositories, so just type in your console,
$ sudo apt-get install reinteract
Ok. Continuing… here is the code,
class Metric(Tensor): '''Represents a metric. Note that coordinates now MUST be provided''' def __init__(self,coord,rank=(0,2),sh=(-1,-1),symbol='g'): self.coord=coord super(Metric,self).__init__(symbol,rank,sh,coords=coord) def invert(self): '''Find the inverse of the metric and store the result in a Metric object self.inverse''' '''Create a unit matrix of dimension dim''' temp = sympy.eye(self.dim) '''Assign the values of the metric to temp''' for key in self.components.keys(): id = tuple(np.abs(key)) temp[id] = self.components[key] '''invert the matrix with inv() from sympy''' inv = temp.inv() '''convert the matrix in a dictionary''' inverse = self._dictkeycopy(self.components) for i in range(self.dim): for j in range(self.dim): inverse[i,j] = inv[i,j] self.inverse = Metric(self.coord,rank=(2,0),sh=(1,1),symbol='g_inv') self.inverse.components = inverse
The first I notice, as a non-expert programmer (or non-programmer at all 😛 ) was the word Tensor inside the brackets of the Metric class. This means that Metric is a Tensor… just as a Tensor is an Object. Thus, all methods defined in the Tensor class are applicable to the Metric class.
Coordinate system needed
The __init__ method define the rank, shape and symbol of the metric, which are always the same. However, the set of coordinates must be given.
Example of implementation
Coordinated should be entered like a tuple, and the name of coordinates MUST be “declared” as sympy.Symbol
t = Symbol('t') r = Symbol('r') th = Symbol('theta') ph = Symbol('phi') g = Metric((t,r,th,ph))
Given the this data, the Metric class calls the Tensor class and create a covariant rank 2 tensor. And we should give the components. For example, the easiest Schwarzschild metric (the metric is easier than the name!!!),
g[0,0] = -(1 - 2*M/r) g[1,1] = 1/(1 - 2*M/r) g[2,2] = r**2 g[3,3] = r**2*(sin(th))**2
WAIT!!!! Don’t forget to declare the mass parameter, so, again…
M = Symbol('M') g[0,0] = -(1 - 2*M/r) g[1,1] = 1/(1 - 2*M/r) g[2,2] = r**2 g[3,3] = r**2*(sin(th))**2
… Sorry guys, it’s again too late! and is already Monday!
Ok, tomorrow we’ll continue with the inverse metric 😉