1
0

Add get masses/positions/momenta methods and modify frogleap input/output

This commit is contained in:
Thibault Barnouin
2021-10-22 16:23:08 +02:00
parent e1431ce1b4
commit 3e40f412e6
3 changed files with 26 additions and 8 deletions

View File

@@ -24,12 +24,16 @@ def dp_dt(m_array, q_array):
dp_array[np.isnan(dp_array)] = 0. dp_array[np.isnan(dp_array)] = 0.
return dp_array return dp_array
def frogleap(duration, step, m_array, q_array, p_array, display=False): def frogleap(duration, step, dyn_syst, display=False):
""" """
Leapfrog integrator for first order partial differential equations. Leapfrog integrator for first order partial differential equations.
iteration : half-step drift -> full-step kick -> half-step drift iteration : half-step drift -> full-step kick -> half-step drift
""" """
N = np.ceil(duration/step).astype(int) N = np.ceil(duration/step).astype(int)
m_array = dyn_syst.get_masses()
q_array = dyn_syst.get_positions()
p_array = dyn_syst.get_momenta()
if display: if display:
d = DynamicUpdate() d = DynamicUpdate()
d.min_x, d.max_x = -1.5*np.abs(q_array).max(), +1.5*np.abs(q_array).max() d.min_x, d.max_x = -1.5*np.abs(q_array).max(), +1.5*np.abs(q_array).max()
@@ -43,13 +47,16 @@ def frogleap(duration, step, m_array, q_array, p_array, display=False):
q_array, p_array = q_array + step/2*p_array/m_array , p_array q_array, p_array = q_array + step/2*p_array/m_array , p_array
#print(p_array) #print(p_array)
# In center of mass frame
q_cm = np.sum(m_array.reshape((q_array.shape[0],1))*q_array, axis=0)/m_array.sum()
q_array -= q_cm
if display: if display:
# In center of mass frame
q_cm = np.array([0.,0.])#np.sum(m_array.reshape((q_array.shape[0],1))*q_array, axis=0)/m_array.sum()
# display progression # display progression
d.on_running(q_array[:,0], q_array[:,1]) d.on_running(q_array[:,0]-q_cm[0], q_array[:,1]-q_cm[1])
time.sleep(0.01) time.sleep(0.01)
return q_array, p_array for i, body in enumerate(dyn_syst.bodylist):
body.q = q_array[i]
body.p = p_array[i]
body.v = body.p/body.m
return dyn_syst

View File

@@ -25,6 +25,15 @@ class System:
def __init__(self, bodylist): def __init__(self, bodylist):
self.bodylist = bodylist self.bodylist = bodylist
def get_masses(self): #return the masses of each object
return np.array([body.m for body in self.bodylit])
def get_positions(self): #return the positions of the bodies
return np.array([body.q for body in self.bodylist])
def get_momenta(self): #return the momenta of the bodies
return np.array([body.p for body in self.bodylist])
def Mass(self): #return total system mass def Mass(self): #return total system mass
mass = 0 mass = 0
for body in self.bodylist: for body in self.bodylist:
@@ -51,6 +60,7 @@ class System:
L = L + np.cross(comq[i],body.p) L = L + np.cross(comq[i],body.p)
i = i+1 i = i+1
return L return L
def Eval(self,Lbodylist): #return total energy of bodies in bodylist def Eval(self,Lbodylist): #return total energy of bodies in bodylist
G = 1. #Gravitational constant (here normalized) G = 1. #Gravitational constant (here normalized)
T = 0 T = 0

View File

@@ -3,10 +3,11 @@
from sys import exit as sysexit from sys import exit as sysexit
import numpy as np import numpy as np
from lib.integrator import frogleap from lib.integrator import frogleap
import lib.objects
def main(): def main():
#initialisation #initialisation
m = np.array([1e10, 1, 1]) m = np.array([1e10, 1, 0])
x1 = np.array([0, 0, 0]) x1 = np.array([0, 0, 0])
x2 = np.array([1, 0, 0]) x2 = np.array([1, 0, 0])