def main(): skull = Body.GravBody(5, 6.371 * 10**6, 8000.) solver = Solver.RK2(0.001) def stop_condition(skull): return skull.velocity > 0 sim1 = Simulation.TrajectorySim(stop_condition, solver, skull) x, y = sim1.get_results() skull2 = Body.GravBody(5, 6.371 * 10**6, 8000.) sim2 = Simulation.InverseTrajectorySim(stop_condition, solver, skull2) a, b = sim2.get_results() plt.plot(x, y) plt.plot(a, b) plt.title("Skull Tosses") plt.xlabel("Time [s]") plt.ylabel("Height [m]") plt.legend(["Uniform Gravity", "Inverse Square Gravity"]) plt.figure() varray = np.arange(0, 100, 0.5) harray = [] for v in varray: skull3 = Body.GravBody(5, 6.371 * 10**6, v) sim3 = Simulation.InverseTrajectorySim(stop_condition, solver, skull3) result = sim3.get_results() harray.append(skull3.position) plt.plot(varray, harray) plt.title("Maximum Height vs. Initial Velocity") plt.xlabel("Initial Velocity [m/s]") plt.ylabel("Maximum Height [m]")
def sim_1(self): Earth = Body.GravBody(1,self.rEarth,self.vEarth) Mars = Body.GravBody(1,self.rMars,self.vMars) Planets = [Earth,Mars] sim = Simulation.OrbitSim(self.stop_1,self.physics,Planets) sim.advance() time, bodies = sim.get_results() return bodies[-1][1].position.theta
def __init__(self,solver,stopCondition,physics,Ms,Mp,Rs,Rp,ap,e,omega,i): self.Rs=Rs self.Rp=Rp self.omega=omega self.i=i self.Ms=Ms self.Mp=Mp self.ap=ap self.e=e self.physics=physics self.desiredOrbitRadius=None self.numberOrbits=0 self.G=6.67408*10**-11 self.vp=math.sqrt(self.G*Ms**3*(1+e)/(ap*(Mp+Ms)**2*(1-e))) self.vs=-(Mp/Ms)*self.vp self.rp=ap-ap*e self.rs=-(Mp/Ms)*self.rp M=1.989*10**30 body1x=self.rp body1y=0 body1z=0 body1pos=Vector.Vector(x=body1x,y=body1y,z=body1z) body1pos.rotZ(omega) body1pos.rotX(i) body2x=self.rs body2y=0 body2z=0 body2pos=Vector.Vector(x=body2x,y=body2y,z=body2z) body2pos.rotZ(omega) body2pos.rotX(i) body1vx=0 body1vy=self.vp body1vz=0 body1vel=Vector.Vector(x=body1vx,y=body1vy,z=body1vz) body1vel.rotZ(omega) body1vel.rotX(i) body2vx=0 body2vy=self.vs body2vz=0 body2vel=Vector.Vector(x=body2vx,y=body2vy,z=body2vz) body2vel.rotZ(omega) body2vel.rotX(i) self.body1=Body.GravBody(body1vel,body1pos,Mp) self.body2=Body.GravBody(body2vel,body2pos,Ms) bodies=[self.body1,self.body2] super(ExoSim,self).__init__(solver,bodies,stopCondition,M)
def sim_3(self): r1 = vector.Vector(1., 0, 0) v1 = vector.Vector(0, 1.1 * math.pi, 0) r2 = vector.Vector(-1., 0, 0) v2 = vector.Vector(0, -1.1 * math.pi, 0) One = Body.GravBody(1., r1, v1) Two = Body.GravBody(1., r2, v2) Objects = [One, Two] sim = Simulation.OrbitSim(physics=self.physics, body=Objects) sim.advance(time=10.) time, bodies = sim.get_results() self.plorbits(bodies)
def angleError(self, phi): '''The error function used in a Search method for finding an angle that gives an equal max height and distance. Parameters ----- phi : float The angle whose error is being calculated. Returns ----- float The difference between the max height and the distance traveled for the given phi. ''' rock = Body.GravBody(Vector.Vector(r=100.,theta=0,phi=phi),Vector.Vector(x=0,y=0,z=0)) rockTrajectory = TrajectorySim(self.solver,rock,Physics.stopCondition,c=self.c) t,bodies = rockTrajectory.simulate() z=[b.position.z for b in bodies] # y=[b.position.y for b in bodies] x=[b.position.x for b in bodies] # print max(z) # print x[-1] # # plt.plot(x,z) dist=x[-1] return max(z)-dist
def vError(self, v): '''The error function used in a Search method for finding an initial velocity that gives a trajectory that reaches the desired height. Parameters ----- v : float The velocity whose error is being calculated. Returns ----- float The difference between the calculated height and the desired height. ''' step=0.01 skull = Body.GravBody(Vector.Vector(x=0,y=0,z=v),Vector.Vector(x=0,y=0,z=self.startHeight)) solver = Solver.RK2(step,Physics.UniformGravity.diffEq) skullFlight = TrajectorySim(solver,Vector.Vector(x=0,y=0,z=self.g),skull,Physics.stopCondition) t,bodies = skullFlight.simulate() hGuess = bodies[-1].position.z return hGuess-self.desiredHeight
def error_3(self,launch_time): vShip = vector.Vector(0,2*math.pi,0) rShip = vector.Vector(1,0,0) Ship = Body.GravBody(1,rShip,vShip) rMars = vector.Vector(1.5,0,0) vMars = vector.Vector(0,2*math.pi/math.sqrt(1.5),0) Mars = Body.GravBody(1,rMars,vMars) Objects = [Ship,Mars] sim = Simulation.OrbitSim(physics=self.physics,body=Objects) sim.advance(time=launch_time) sim.body[0].velocity.r = self.vL p = ((2.5/2.)**(3./2.))/2. sim.advance(time=p+launch_time) t2list,blist = sim.get_results() sep = sim.body[1].position - sim.body[0].position error = sep.r if error <= 0.01: self.plorbits(blist) return error
def __init__(self, Ms=None, Mp=None, ap=None, e=0, Rs=None, Rp=None, omega=None, i=None, apnd=True): '''Save Values''' self.apnd = apnd self.Rs = Rs self.Rp = Rp self.G = 4 * math.pi**2. self.period = np.sqrt(((ap**3.) * ((Ms + Mp)**2.)) / Ms**3.) '''Set up Vectors''' rpp = ap - e * ap rsp = -(Mp / Ms) * rpp vpp = math.sqrt( ((self.G * Ms**3.) / (ap * (Ms + Mp)**2.)) * ((1. + e) / (1. - e))) vsp = -(Mp / Ms) * vpp '''Rotate Vectors into Viewer frame''' rs = vector.Vector(rsp, 0., 0.) rs.rot_z(omega) rs.rot_x(i) rp = vector.Vector(rpp, 0., 0.) rp.rot_z(omega) rp.rot_x(i) vs = vector.Vector(0., vsp, 0.) vs.rot_z(omega) vs.rot_x(i) vp = vector.Vector(0., vpp, 0.) vp.rot_z(omega) vp.rot_x(i) '''Set Up Sim''' star = Body.GravBody(Ms, rs, vs) planet = Body.GravBody(Mp, rp, vp) self.body = [star, planet] solver = Solver.RK2(0.01) self.physics = Physics.NBody(solver, self.G) self.bodies = [cp.deepcopy(self.body)] self.t = [0]
def closed2BodyAutoBuilder(omega, i, mBig, mSmall, ap, e): """ """ G = 1 #6.67408*10**(-11) vp = np.sqrt(G * mBig**3 * (1 + e) / (ap * (mSmall + mBig)**2 * (1 - e))) vs = -(mSmall / mBig) * vp rp = ap - ap * e rs = -(mSmall / mBig) * rp body1x = rp body1y = 0 body1z = 0 body1pos = Vector.Vector(x=body1x, y=body1y, z=body1z) body1pos.rotZ(omega) body1pos.rotX(i) body2x = rs body2y = 0 body2z = 0 body2pos = Vector.Vector(x=body2x, y=body2y, z=body2z) body2pos.rotZ(omega) body2pos.rotX(i) body1vx = 0 body1vy = vp body1vz = 0 body1vel = Vector.Vector(x=body1vx, y=body1vy, z=body1vz) body1vel.rotZ(omega) body1vel.rotX(i) body2vx = 0 body2vy = vs body2vz = 0 body2vel = Vector.Vector(x=body2vx, y=body2vy, z=body2vz) body2vel.rotZ(omega) body2vel.rotX(i) body1 = Body.GravBody(body1vel, body1pos, mSmall) body2 = Body.GravBody(body2vel, body2pos, mBig) bodies = [body1, body2] return bodies
def findV(self,tolerance): '''Finds the velocity given a desired height within a given tolerance for a body on Earth. Parameters ----- tolerance : float How close you want to be to the theoretically ideal value. Returns ----- The estimated velocity. ''' desiredHeight=self.desiredHeight vGuess=self.vGuess hGuess=self.hGuess g = -9.81 step = 0.1 lastvGuess = vGuess up = False down = False while abs(hGuess-desiredHeight)>tolerance: skull = Body.GravBody(vGuess,self.startHeight) solver = Solver.RK2(step,Physics.UniformGravity.diffEq) skullFlight = TrajectorySim(solver,g,skull,Physics.stopCondition) t,vAndh = skullFlight.simulate() hGuess = vAndh[1][-1] if hGuess-desiredHeight < 0: up = True if down == False: lastvGuess = vGuess vGuess = vGuess*2 else: lvgHold = vGuess vGuess = vGuess + abs(lastvGuess-vGuess)*0.5 lastvGuess = lvgHold elif hGuess-desiredHeight > 0: down = True if up == False: lastvGuess = vGuess vGuess = vGuess/2. else: lvgHold = vGuess vGuess = vGuess - abs(vGuess-lastvGuess)*0.5 lastvGuess = lvgHold else: print('Error?') return vGuess
def __init__(self,solver,stopCondition,physics,M1,M2,a1,e): self.M1=M1 self.M2=M2 self.a1=a1 self.e=e self.physics=physics self.desiredOrbitRadius=None self.numberOrbits=0 self.G=6.67408*10**-11 self.v1=math.sqrt(self.G*M2**3*(1+e)/(a1*(M1+M2)**2*(1-e))) self.v2=-(M1/M2)*self.v1 self.r1=a1-a1*e self.r2=-(M1/M2)*self.r1 M=1.989*10**30 body1x=self.r1 body1y=0 body1z=0 body1pos=Vector.Vector(x=body1x,y=body1y,z=body1z) body2x=self.r2 body2y=0 body2z=0 body2pos=Vector.Vector(x=body2x,y=body2y,z=body2z) body1vx=0 body1vy=self.v1 body1vz=0 body1vel=Vector.Vector(x=body1vx,y=body1vy,z=body1vz) body2vx=0 body2vy=self.v2 body2vz=0 body2vel=Vector.Vector(x=body2vx,y=body2vy,z=body2vz) self.body1=Body.GravBody(body1vel,body1pos,M1) self.body2=Body.GravBody(body2vel,body2pos,M2) bodies=[self.body1,self.body2] super(BinarySim,self).__init__(solver,bodies,stopCondition,M)
def error_2(self,v0): vShip = vector.Vector(0,v0,0) rShip = vector.Vector(1,0,0) Ship = Body.GravBody(1,rShip,vShip) sim = Simulation.OrbitSim(self.stop_1,self.physics,Ship) sim.advance() time, Ships = sim.get_results() r = [[s.position.r for s in ship] for ship in Ships] max_r = max(r) error = max_r[0] - 1.5 #since r becomes another stupid list of lists return error
def __init__(self, M1=None, M2=None, a1=1, e=0, apnd=True): '''Build GravBodies''' self.G = 4 * math.pi**2. r1p = a1 - e * a1 r2p = -(M1 / M2) * r1p v1p = math.sqrt( ((self.G * M2**3.) / (a1 * (M1 + M2)**2.)) * ((1. + e) / (1. - e))) v2p = -(M1 / M2) * v1p r1 = vector.Vector(r1p, 0., 0.) r2 = vector.Vector(r2p, 0., 0.) v1 = vector.Vector(0., v1p, 0.) v2 = vector.Vector(0., v2p, 0.) body1 = Body.GravBody(M1, r1, v1) body2 = Body.GravBody(M2, r2, v2) '''Set up Sim''' self.body = [body1, body2] solver = Solver.RK2(0.01) self.physics = Physics.NBody(solver, self.G) self.bodies = [cp.deepcopy(self.body)] self.t = [0] self.apnd = apnd
def error(theta): htarg = 0 r = vector.Vector(0,0,0) v = vector.Vector(r=100,theta=theta,phi=(math.pi/2)) particle = Body.GravBody(5,r,v) solver =Solver.RK4(0.1) physics = Physics.UniformGravity(solver,c=0.1) sim = Simulation.TrajectorySim(stop_maker(htarg),physics,particle) a,b = sim.get_results() y = [p.position.y for p in b] E = particle.position.x - max(y) return E
def main(): skull = Body.GravBody(5, 5, 5) solver = Solver.RK2(0.001) def stop_condition(skull): return skull.velocity > 0 sim = Simulation.TrajectorySim(stop_condition, solver, skull) t, h = sim.get_results() plt.plot(t, h) plt.title("OOP Skull Toss") plt.xlabel("Time [s]") plt.ylabel("Height [m]")
def manualOrbitBuilder(xPos,yPos,zPos,xVel,yVel,zVel,mass): ''' ''' bodies=[] for i in range(len(xPos)): bodyPos = Vector.Vector(x=xPos[i],y=yPos[i],z=zPos[i]) bodyVel = Vector.Vector(x=xVel[i],y=yVel[i],z=zVel[i]) body = Body.GravBody(bodyVel,bodyPos,mass[i]) bodies.append(body) return bodies
def __init__(self): '''Build Grav Bodies''' ms = 9. sunr = vector.Vector(0.,0.,0.) sunv = vector.Vector(0.,-0.6*np.pi/ms,0.) self.sun = Body.GravBody(ms,sunr,sunv) me = 0.1 earthr = vector.Vector(1.,0.,0.) earthv = vector.Vector(0.,6.*np.pi,0.) self.earth = Body.GravBody(me,earthr,earthv) mr5 = 0.1 rigel5r = vector.Vector(5.,0.,0.) rigel5v = vector.Vector(0.,2.*np.pi*np.sqrt(me+ms)/np.sqrt(5.),0.) self.rigel5 = Body.GravBody(mr5,rigel5r,rigel5v) '''Build Sim''' planets = [self.sun,self.earth,self.rigel5] solver = Solver.RK4(0.01) physics = Physics.NBody(solver,G=4.*np.pi**2.) self.sim = Simulation.OrbitSim(physics=physics,body=planets,apnd=False)
def __init__(self, pixels_per_AU=None, screen_size=None): pg.sprite.Sprite.__init__(self) self.pixels_per_AU = pixels_per_AU self.screen_size = screen_size self.image = pg.image.load('asteroid.png').convert_alpha() self.rect = self.image.get_rect() '''Create Random GravBodies''' mass = 0.0 index = rand.randint(1, 4) if index == 1: position = vector.Vector(x=rand.uniform(-6, 6), y=5.99, z=0.0) velocity = vector.Vector(r=5 * np.pi, theta=(rand.uniform(180, 360) * np.pi / 180), phi=np.pi / 2.) if index == 2: position = vector.Vector(x=rand.uniform(-6, 6), y=-5.99, z=0.0) velocity = vector.Vector(r=5 * np.pi, theta=(rand.uniform(0, 180) * np.pi / 180), phi=np.pi / 2.) if index == 3: position = vector.Vector(x=-5.99, y=rand.uniform(-6, 6), z=0.0) velocity = vector.Vector(r=5 * np.pi, theta=(rand.uniform(270, 450) * np.pi / 180), phi=np.pi / 2.) if index == 4: position = vector.Vector(x=5.99, y=rand.uniform(-6, 6), z=0.0) velocity = vector.Vector(r=5 * np.pi, theta=(rand.uniform(90, 270) * np.pi / 180), phi=np.pi / 2.) self.body = Body.GravBody(mass, position, velocity) self.update()
def run(self): '''Orbits''' for v0 in self.v0_list: for dt in self.dt_list: r = vector.Vector(self.AU, 0, 0) #starting position in AU v = vector.Vector(0, v0, 0) earth = Body.GravBody(1, r, v) solver = Solver.RK4(dt) physics = Physics.CentralGravity(solver, G=4 * math.pi**2, M=1) # M is 1 solar mass sim = Simulation.OrbitSim(self.stop, physics, earth) time, bodies = sim.get_results() y = [b[0].position.y for b in bodies] x = [b[0].position.x for b in bodies] energy = physics.energy(bodies) errors = self.error(energy) if v0 == math.pi * 2: plt.figure(1) plt.plot(x, y, '.') plt.title("Circular Orbits of Earth Around Sun") plt.ylabel("Position in y [AU]") plt.xlabel("Position in x [AU]") plt.legend(["dt=0.003", "dt=0.002", "dt=0.001"]) plt.figure(2) plt.plot(time, errors) plt.title("Error in Energy vs Time") plt.ylabel("Percent Error") plt.xlabel("Time [Years]") plt.legend(["dt=0.003", "dt=0.002", "dt=0.001"]) else: plt.figure(3) plt.plot(x, y, '.') plt.title("Elliptical Orbits of Earth Around Sun") plt.ylabel("Position in y [AU]") plt.xlabel("Position in x [AU]") plt.legend(["dt=0.003", "dt=0.002", "dt=0.001"]) plt.figure(4) plt.plot(time, errors) plt.title("Error in Energy vs Time") plt.ylabel("Percent Error") plt.xlabel("Time [Years]") plt.legend(["dt=0.003", "dt=0.002", "dt=0.001"]) """Kepler""" r_list = [0.9, 1, 1.1] v_list = [2 * math.pi, 2 * 0.9 * math.pi, 2 * 1.1 * math.pi] for r in r_list: for v in v_list: r0 = vector.Vector(r, 0, 0) v0 = vector.Vector(0, v, 0) earth = Body.GravBody(1, r0, v0) solver = Solver.RK4(0.001) physics = Physics.CentralGravity(solver, G=4 * math.pi**2, M=1) # M is 1 solar mass sim = Simulation.OrbitSim(self.stop, physics, earth) time, bodies = sim.get_results() a = sim.semimajor_axis() p = sim.period() plt.figure(5) plt.plot(a, p, '.') plt.figure(5) a = np.arange(0, 2, 0.001) p = a**(3. / 2.) plt.plot(a, p) plt.title("Semi Major Axis vs Period") plt.ylabel("Orbital Period [Years]") plt.xlabel("Semi Major Axis [AU]")