-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator360.py
95 lines (75 loc) · 2.6 KB
/
simulator360.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
import rebound
import numpy as np
import math
import matplotlib.pyplot as plt
def creatThrust(x,y,z):
def thrust(reb_sim):
reb_sim.contents.particles[3].ax += x
reb_sim.contents.particles[3].ay += y
reb_sim.contents.particles[3].az += z
deltaV = (reb_sim.contents.particles[3].ax**2.0+reb_sim.contents.particles[3].ay**2.0+reb_sim.contents.particles[3].az**2.0)**0.5
reb_sim.contents.particles[3].m = reb_sim.contents.particles[3].m*math.exp(-deltaV/10) # Mass loss
return thrust
class SpaceSim(object):
def __init__(self):
self.sim = self._initSim()
def status(self):
self.sim.status()
def make_move(self, x,y,z,t):
rocket = self.sim.particles[3]
self.sim.additional_forces = accelerateDown
self.sim.integrate(self.sim.t + 1) # time + 1 hr
return self.get_reward()
def setXYZ(self,x,y,z):
THRUSTX=x
THRUSTY=y
THRUSTZ=z
def _initSim(self):
"""
Initialize rebound Simulation with units, integrator, and bodies.
Code assumes rocket is ps[3] and mars is [2].
"""
sim = rebound.Simulation()
sim.units = 'day', 'AU', 'Msun'
sim.integrator = "ias15"
# add planets yourself.
sim.add(id=2, m=1.)
sim.add(id=5, m=1e-6, a=1.)
sim.add(m=1e-6, a=2., Omega =3.0)
sim.add(m=1e-34, a=1.01)
sim.move_to_com()
return sim
def simulate(self,parameters):
A = np.matrix(parameters)
A=A.shape
times=np.linspace(0,A[0]-1,A[0])
for i , time in enumerate(times):
decision=parameters[i]
self.setXYZ(decision[0],decision[1],decision[2])
self.sim.additional_forces = creatThrust(decision[0],decision[1],decision[2])
self.sim.velocity_dependent = 1
self.sim.integrate(time)
return self.sim
def plot(self):
fig = rebound.OrbitPlot(self.sim)
def stopEngine(reb_sim):
reb_sim.contents.particles[3].ax += 0.0
reb_sim.contents.particles[3].ay += 0.0
reb_sim.contents.particles[3].az += 0.0
decision = [0,0,0,False]
decisions=[]
for i in range(361):
decisions.append([i]+decision)
def generateSolutions():
decision = [0.,0.,0.,False]
decisions=[]
THRUST=0.0005
for i in range(361):
decision[0]=np.random.uniform(-THRUST,THRUST)
decision[1]=np.random.uniform(-THRUST,THRUST)
decision[2]=np.random.uniform(-THRUST,THRUST)
decisions.append(decision)
x = SpaceSim()
k=x.simulate(decisions)
print k.particles[3].m
# generateSolutions()