forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
relativity.py
executable file
·176 lines (153 loc) · 3.66 KB
/
relativity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
import iam_sympy_example
"""
This example calculates the Ricci tensor from the metric and does this
on the example of Schwarzschild solution.
"""
from sympy import exp, Symbol, sin, Rational, Derivative, dsolve, Function, Matrix
def grad(f,X):
a=[]
for x in X:
a.append( f.diff(x) )
return a
def d(m,x):
return grad(m[0,0],x)
class MT(object):
def __init__(self,m):
self.gdd=m
self.guu=m.inv()
def __str__(self):
return "g_dd =\n" + str(self.gdd)
def dd(self,i,j):
return self.gdd[i,j]
def uu(self,i,j):
return self.guu[i,j]
class G(object):
def __init__(self,g,x):
self.g = g
self.x = x
def udd(self,i,k,l):
g=self.g
x=self.x
r=0
for m in [0,1,2,3]:
r+=g.uu(i,m)/2 * (g.dd(m,k).diff(x[l])+g.dd(m,l).diff(x[k]) \
- g.dd(k,l).diff(x[m]))
return r
class Riemann(object):
def __init__(self,G,x):
self.G = G
self.x = x
def uddd(self,rho,sigma,mu,nu):
G=self.G
x=self.x
r=G.udd(rho,nu,sigma).diff(x[mu])-G.udd(rho,mu,sigma).diff(x[nu])
for lam in [0,1,2,3]:
r+=G.udd(rho,mu,lam)*G.udd(lam,nu,sigma) \
-G.udd(rho,nu,lam)*G.udd(lam,mu,sigma)
return r
class Ricci(object):
def __init__(self,R,x):
self.R = R
self.x = x
self.g = R.G.g
def dd(self,mu,nu):
R=self.R
x=self.x
r=0
for lam in [0,1,2,3]:
r+=R.uddd(lam,mu,lam,nu)
return r
def ud(self,mu,nu):
r=0
for lam in [0,1,2,3]:
r+=self.g.uu(mu,lam)*self.dd(lam,nu)
return r.expand()
def curvature(Rmn):
return Rmn.ud(0,0)+Rmn.ud(1,1)+Rmn.ud(2,2)+Rmn.ud(3,3)
#class nu(Function):
# def getname(self):
# return r"\nu"
# return r"nu"
#class lam(Function):
# def getname(self):
# return r"\lambda"
# return r"lambda"
nu = Function("nu")
lam = Function("lam")
t=Symbol("t")
r=Symbol("r")
theta=Symbol(r"\theta")
phi=Symbol(r"\phi")
#general, spherically symmetric metric
gdd=Matrix((
(-exp(nu(r)),0,0,0),
(0, exp(lam(r)), 0, 0),
(0, 0, r**2, 0),
(0, 0, 0, r**2*sin(theta)**2)
))
#spherical - flat
#gdd=Matrix((
# (-1, 0, 0, 0),
# (0, 1, 0, 0),
# (0, 0, r**2, 0),
# (0, 0, 0, r**2*sin(theta)**2)
# ))
#polar - flat
#gdd=Matrix((
# (-1, 0, 0, 0),
# (0, 1, 0, 0),
# (0, 0, 1, 0),
# (0, 0, 0, r**2)
# ))
#polar - on the sphere, on the north pole
#gdd=Matrix((
# (-1, 0, 0, 0),
# (0, 1, 0, 0),
# (0, 0, r**2*sin(theta)**2, 0),
# (0, 0, 0, r**2)
# ))
g=MT(gdd)
X=(t,r,theta,phi)
Gamma=G(g,X)
Rmn=Ricci(Riemann(Gamma,X),X)
def main():
#print g
print "-"*40
print "Christoffel symbols:"
print Gamma.udd(0,1,0)
print Gamma.udd(0,0,1)
print
print Gamma.udd(1,0,0)
print Gamma.udd(1,1,1)
print Gamma.udd(1,2,2)
print Gamma.udd(1,3,3)
print
print Gamma.udd(2,2,1)
print Gamma.udd(2,1,2)
print Gamma.udd(2,3,3)
print
print Gamma.udd(3,2,3)
print Gamma.udd(3,3,2)
print Gamma.udd(3,1,3)
print Gamma.udd(3,3,1)
print "-"*40
print "Ricci tensor:"
print Rmn.dd(0,0)
e = Rmn.dd(1,1)
print e
print Rmn.dd(2,2)
print Rmn.dd(3,3)
#print
#print "scalar curvature:"
#print curvature(Rmn)
print "-"*40
print "solve the Einstein's equations:"
e = e.subs(nu(r), -lam(r))
l = dsolve(e, [lam(r)])
print lam(r)," = ",l
metric = gdd.subs(lam(r), l).subs(nu(r),-l)#.combine()
print "metric:"
print metric
if __name__ == "__main__":
main()