object = system
Properties
This commit is contained in:
@@ -28,12 +28,15 @@ class Body:
|
|||||||
def __str__(self): # Called upon "str(body)"
|
def __str__(self): # Called upon "str(body)"
|
||||||
return r"Body of mass: {0:.2f} $M_\odot$".format(self.m)
|
return r"Body of mass: {0:.2f} $M_\odot$".format(self.m)
|
||||||
|
|
||||||
class System:
|
class System(Body):
|
||||||
|
|
||||||
def __init__(self, bodylist, blackstyle=True):
|
def __init__(self, bodylist, blackstyle=True):
|
||||||
self.blackstyle = blackstyle
|
self.blackstyle = blackstyle #for dark mode in plot
|
||||||
self.bodylist = np.array(bodylist)
|
self.bodylist = np.array(bodylist)
|
||||||
self.time = 0
|
self.time = 0 #lifetime of system
|
||||||
|
self.m = self.M
|
||||||
|
self.q = self.COM
|
||||||
|
self.v = self.COMV
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def get_masses(self): #return the masses of each object
|
def get_masses(self): #return the masses of each object
|
||||||
@@ -60,39 +63,43 @@ class System:
|
|||||||
pzdata = np.array([body.p[2] for body in self.bodylist])
|
pzdata = np.array([body.p[2] for body in self.bodylist])
|
||||||
return pxdata, pydata, pzdata
|
return pxdata, pydata, pzdata
|
||||||
|
|
||||||
def Mass(self): #return total system mass
|
@property
|
||||||
|
def M(self): #return total system mass
|
||||||
mass = 0
|
mass = 0
|
||||||
for body in self.bodylist:
|
for body in self.bodylist:
|
||||||
mass = mass + body.m
|
mass = mass + body.m
|
||||||
return mass
|
return mass
|
||||||
|
|
||||||
|
@property
|
||||||
def COM(self): #return center of mass in cartesian np_array
|
def COM(self): #return center of mass in cartesian np_array
|
||||||
coord = np.zeros(3)
|
coord = np.zeros(3)
|
||||||
for body in self.bodylist:
|
for body in self.bodylist:
|
||||||
coord = coord + body.m*body.q
|
coord = coord + body.m*body.q
|
||||||
coord = coord/self.Mass()
|
coord = coord/self.M
|
||||||
return coord
|
return coord
|
||||||
|
|
||||||
|
@property
|
||||||
def COMV(self): #return center of mass velocity in cartesian np_array
|
def COMV(self): #return center of mass velocity in cartesian np_array
|
||||||
coord = np.zeros(3)
|
coord = np.zeros(3)
|
||||||
for body in self.bodylist:
|
for body in self.bodylist:
|
||||||
coord = coord + body.p
|
coord = coord + body.p
|
||||||
coord = coord/self.Mass()
|
coord = coord/self.M
|
||||||
return coord
|
return coord
|
||||||
|
|
||||||
def COMShift(self): #Shift coordinates of bodies in system to COM frame and set COM at rest
|
def COMShift(self): #Shift coordinates of bodies in system to COM frame and set COM at rest
|
||||||
for body in self.bodylist:
|
for body in self.bodylist:
|
||||||
body.q = body.q - self.COM()
|
body.q = body.q - self.COM
|
||||||
body.p = body.p - self.COMV()
|
body.p = body.p - self.COMV
|
||||||
return 0
|
|
||||||
|
|
||||||
def Lval(self): #return angular momentum of bodies in system
|
@property
|
||||||
|
def L(self): #return angular momentum of bodies in system
|
||||||
L = np.zeros(3)
|
L = np.zeros(3)
|
||||||
for body in self.bodylist:
|
for body in self.bodylist:
|
||||||
L = L + np.cross(body.q,body.p)
|
L = L + np.cross(body.q,body.p)
|
||||||
return L
|
return L
|
||||||
|
|
||||||
def Eval(self): #return total energy of bodies in system
|
@property
|
||||||
|
def E(self): #return total energy of bodies in system
|
||||||
T = 0
|
T = 0
|
||||||
W = 0
|
W = 0
|
||||||
for body in self.bodylist:
|
for body in self.bodylist:
|
||||||
@@ -101,7 +108,8 @@ class System:
|
|||||||
if body != otherbody:
|
if body != otherbody:
|
||||||
rij = np.linalg.norm(body.q-otherbody.q)
|
rij = np.linalg.norm(body.q-otherbody.q)
|
||||||
W = W - G*body.m*otherbody.m/rij
|
W = W - G*body.m*otherbody.m/rij
|
||||||
return T + W
|
E = T + W
|
||||||
|
return E
|
||||||
|
|
||||||
def Drift(self, dt):
|
def Drift(self, dt):
|
||||||
for body in self.bodylist:
|
for body in self.bodylist:
|
||||||
@@ -140,8 +148,8 @@ class System:
|
|||||||
for j in range(N):
|
for j in range(N):
|
||||||
self.LP(dt)
|
self.LP(dt)
|
||||||
|
|
||||||
E[j] = self.Eval()
|
E[j] = self.E
|
||||||
L[j] = self.Lval()
|
L[j] = self.L
|
||||||
|
|
||||||
if display and j%5==0:
|
if display and j%5==0:
|
||||||
# display progression
|
# display progression
|
||||||
@@ -237,8 +245,8 @@ class System:
|
|||||||
for j in range(N):
|
for j in range(N):
|
||||||
self.HPC(dt)
|
self.HPC(dt)
|
||||||
|
|
||||||
E[j] = self.Eval()
|
E[j] = self.E
|
||||||
L[j] = self.Lval()
|
L[j] = self.L
|
||||||
|
|
||||||
if display and j%100==0:
|
if display and j%100==0:
|
||||||
# display progression
|
# display progression
|
||||||
@@ -261,3 +269,33 @@ class System:
|
|||||||
def __str__(self): # Called upon "str(system)"
|
def __str__(self): # Called upon "str(system)"
|
||||||
return str([str(body) for body in self.bodylist])
|
return str([str(body) for body in self.bodylist])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mu(self):
|
||||||
|
sum = 0
|
||||||
|
prod = 1
|
||||||
|
for body in self.bodylist:
|
||||||
|
prod = prod * body.m
|
||||||
|
mu = prod/self.M
|
||||||
|
return mu
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ex(self): #exentricity of system (if composed of 2 bodies)
|
||||||
|
if len(self.bodylist) != 2 :
|
||||||
|
return np.nan
|
||||||
|
else:
|
||||||
|
k = (2.*self.E*(np.linalg.norm(self.L)**2))/((G**2)*(self.M**2)*(self.mu**3)) + 1.
|
||||||
|
return k
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sma(self): #semi major axis of system (if composed of 2 bodies)
|
||||||
|
if len(self.bodylist) != 2 :
|
||||||
|
return np.nan
|
||||||
|
else:
|
||||||
|
sma = -G*self.M*self.mu/(2.*self.E)
|
||||||
|
return sma
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -107,11 +107,6 @@ def display_parameters(E,L,parameters,savename=""):
|
|||||||
if savename != "":
|
if savename != "":
|
||||||
savename += "_"
|
savename += "_"
|
||||||
duration, step, dyn_syst, integrator = parameters
|
duration, step, dyn_syst, integrator = parameters
|
||||||
if type(step) != list:
|
|
||||||
step = [step]
|
|
||||||
print(E.shape, L.shape)
|
|
||||||
if (len(E.shape) == 1) and (len(L.shape) == 2):
|
|
||||||
E, L = [E], [L]
|
|
||||||
bodies = ""
|
bodies = ""
|
||||||
for body in dyn_syst.bodylist:
|
for body in dyn_syst.bodylist:
|
||||||
bodies += str(body)+" ; "
|
bodies += str(body)+" ; "
|
||||||
|
|||||||
2
main.py
2
main.py
@@ -28,7 +28,7 @@ def main():
|
|||||||
duration, step = 100*yr/yr, np.array([1./(365.25*2.), 1./365.25])*yr/yr #integration time and step in years
|
duration, step = 100*yr/yr, np.array([1./(365.25*2.), 1./365.25])*yr/yr #integration time and step in years
|
||||||
integrator = "leapfrog"
|
integrator = "leapfrog"
|
||||||
n_bodies = 2
|
n_bodies = 2
|
||||||
display = False
|
display = True
|
||||||
savename = "{0:d}bodies_{1:s}".format(n_bodies, integrator)
|
savename = "{0:d}bodies_{1:s}".format(n_bodies, integrator)
|
||||||
|
|
||||||
#simulation start
|
#simulation start
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 129 KiB After Width: | Height: | Size: 54 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 116 KiB After Width: | Height: | Size: 109 KiB |
Reference in New Issue
Block a user