summaryrefslogtreecommitdiff
path: root/throw.py
blob: 5c4bdfc157b75e03fec8ecdfd0e00baed8c28279 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

from time import time as current_time
import sys
import numpy
from scipy.interpolate import interp1d
from scipy.integrate import odeint
from scipy.optimize import fmin
from matplotlib import pyplot

global time0
time0 = current_time()

tau = 2*numpy.pi
def taylor_sin(a):
    x = a % tau
    r = (x
        -x**3/6
        +x**5/120
        -x**7/5040
        +x**9/362880
        -x**11/39916800
        +x**13/6227020800
        -x**15/1307674368000
        +x**17/355687428096000
        -x**19/121645100408832000
        +x**21/51090942171709440000
        -x**23/25852016738884976640000
        +x**25/15511210043330985984000000
        -x**27/10888869450418352160768000000
        #+x**29/8841761993739701954543616000000
        #-x**31/8222838654177922817725562880000000
        #+x**33/8683317618811886495518194401280000000
        #-x**35/10333147966386144929666651337523200000000
        #+x**37/13763753091226345046315979581580902400000000
        #-x**39/20397882081197443358640281739902897356800000000
    )
    return r
def taylor_cos(a):
    x = a % tau
    r = (1
        -x**2/2
        +x**4/24
        -x**6/720
        +x**8/40320
        -x**10/3628800
        +x**12/479001600
        -x**14/87178291200
        +x**16/20922789888000
        -x**18/6402373705728000
        +x**20/2432902008176640000
        -x**22/1124000727777607680000
        +x**24/620448401733239439360000
        -x**26/403291461126605635584000000
        #+x**28/304888344611713860501504000000
        #-x**30/265252859812191058636308480000000
        #+x**32/263130836933693530167218012160000000
        #-x**34/295232799039604140847618609643520000000
        #+x**36/371993326789901217467999448150835200000000
        #-x**38/523022617466601111760007224100074291200000000
    )
    return r

### trig approximations
cos = taylor_cos
sin = taylor_sin

### radii:
global r0
r0 = 1. #meters -- the length of cable in the first full-circle rotation
r_anchor = .2 #meters -- the radius of the anchor point
r_winch  = .2 #meters -- the max radius of the roll of cable
length = 1000 #meters -- the length of the cable
phi_max = 2 * (length - r0) / r_winch #rad
r = lambda phi: r0 + r_winch*(1 - phi/(2*phi_max))*phi # cable let out
r_sat = lambda phi: numpy.sqrt(r(phi)**2 + r_anchor) # radius of satellite

### gravitational acceleration:
geopotential = 3.987 * 10**14 #cubic meters per second^2
earth_radius = 6.3675 * 10**6 #meters
balloon_height = 40*10**3
height = lambda phi: balloon_height + r(phi) * cos(phi)
g = lambda phi: geopotential / (earth_radius + height(phi))**2

### quadratic friction:
density = .003 #kg/m^3
kappa = .25 # for a sphere
radius = .1 #meters -- radius of the satellite cage
Area = numpy.pi * radius **2 # for a sphere
quad_co = kappa * density * Area
# fric_quad = quad_co * (r * phi1 * vcm_co)**2

### cable data:
global dia, l, max_tension
#diameter in m: [l in kg/m, breaking point in kN]
cables = {.006: [.023,   41.2], .008: [.039,   65.7],
          .009: [.042,   82.4], .010: [.059,  105.9],
          .011: [.063,  133.4], .012: [.095,  161.9],
          .014: [.128,  215.8], .016: [.160,  269.8],
          .018: [.208,  343.3], .020: [.255,  407.1],
          .022: [.305,  490.5], .024: [.358,  569.0],
          .026: [.410,  647.4], .028: [.465,  725.9],
          .030: [.520,  799.5], .032: [.570,  868.1],
          .034: [.625,  941.2], .036: [.680, 1020.2],
          .038: [.740, 1098.7], .040: [.840, 1245.8],
          .042: [.930, 1373.4], .044: [1.02, 1491.1],
          .046: [1.11, 1618.6], .048: [1.21, 1755.9],
          .050: [1.31, 1893.3], .052: [1.41, 2020.8],
          .056: [1.63, 2315.1], .060: [1.75, 2472.0],
          .064: [2.00, 2766.3], .068: [2.26, 3099.9],
          .072: [2.54, 3413.8], .080: [3.13, 4139.7],
          .088: [3.79, 4934.3], .096: [4.51, 5768.1]}
dia = .026
cable_choices = numpy.array(cables.keys())
nearest = lambda dia: cable_choices[numpy.abs(dia - cable_choices).argmin()]
l, max_tension = cables[nearest(dia)]

### masses:
global m, Mass
m = 39.7 #kilograms -- mass of the satellite cage (overestimate)
Mass = 670 #kilograms -- mass of /everything/
M = lambda phi: Mass - m - l*r(phi)
r_cm = lambda phi: (m*r(phi) + .5*l*r(phi)**2) / (m + l*r(phi) + M(phi))
v_cm_co = lambda phi: 1 - r_cm(phi)/r(phi)

tan = lambda phi: r_anchor / r(phi) # tan(theta)
def throw(phi, time):
    """The diff eqs for the windup."""
    phi, phi1 = phi
    prime_vars = [
      # phi' = phi'
        phi1,
      # phi'' =
        ( tan(phi) - r_winch/r(phi) ) * phi1**2
         + g(phi) * ( sin(phi) - cos(phi)*tan(phi) ) / r(phi)
         - quad_co * r(phi) *(v_cm_co(phi) * phi1 )**2 / m
      ]
    return prime_vars

#good final times: 20 185 1874
def solve_fun(func, init_vars, space=(0, 1874, 10**5)):
    time0 = current_time()
    time = numpy.linspace(*space)
    soln = odeint(func, init_vars, time)
    print "took %s seconds to solve" % (current_time() - time0)
    return time, soln
    
def interpret(soln, max_tension=max_tension):
    phi  = soln[:, 0]
    phi1 = soln[:, 1]
    v_tan = v_cm_co(phi) * phi1 * r(phi) / 1000 #kilometers/second
    cos_t = lambda phi: cos(numpy.arctan(tan(phi))) # cos(theta)
    tension = (m/cos_t(phi))*(phi1**2 * r(phi) - g(tau/4))/1000 #kiloNewtons
    cable_moment = l*((r(phi) - r_cm(phi))**3 - r_cm(phi)**3)/3 #kg m^2
    moment = m*(r(phi) - r_cm(phi))**2 + cable_moment - M(phi)*r_cm(phi)**2
    momentum = moment*phi1**2 #kg m^2/s^2
    
    try:
        maxindex = numpy.argmax(tension >= max_tension)
        max_arg = numpy.argmax(v_tan[:maxindex])
    except ValueError: max_arg = numpy.argmax(v_tan)
    
    return phi, phi1, v_tan, cos_t, tension, moment, momentum, max_arg

def plot(time, soln, graphs=['all'], i=7):
    phi, phi1, v_tan, cos_t, tension, moment, momentum, max_arg = \
        interpret(soln)
    phi_max, phi1_max, v_tan_max, tension_max, moment_max, momentum_max = \
        map(lambda x: numpy.ndarray.__getitem__(x, max_arg),
            [phi, phi1, v_tan, tension, moment, momentum])
    
    def test(*args):
        b = 0
        for string in args:
            b += string in graphs or \
                ('all' in graphs and "!" + string not in graphs)
        return bool(b)
    
    i -= 1
    if test('gravity'):
        pyplot.figure(i)
        pyplot.plot(time, g(phi))
        pyplot.grid(True)
        pyplot.ylabel("gravitational acceleration (m/s^2)")
        pyplot.xlabel("time (seconds)")
    
    i -= 1
    if test('phi', 'r', 'radius'):
        pyplot.figure(i)
        pyplot.plot(time, phi / tau, label="phi (cycles)")
        pyplot.plot(time, r(phi), label="r (m)")
        pyplot.grid(True)
        pyplot.legend(loc="upper left")
        pyplot.title("phi and radius vs. time")
        pyplot.ylabel("phi (cycles) and radius (meters)")
        pyplot.xlabel("time (seconds)")
    
    i -= 1
    if test("phi'", "omega", "phi1"):
        pyplot.figure(i)
        pyplot.plot(time, phi1 / tau)
        pyplot.grid(True)
        pyplot.ylabel("phi' (Hertz)")
        pyplot.xlabel("time (seconds)")

    i -= 1
    if test('v_tan', 'tension'):
        pyplot.figure(i)
        rect = [.1, .1, .8, .8]
        
        ax1 = pyplot.axes(rect)
        ax1.yaxis.tick_left()
        ax1.xaxis.grid(True)
        pyplot.plot(phi / tau, v_tan, marker='+', markevery=len(phi)/50)
        pyplot.ylabel("+: tangential velocity (km/s)")
        pyplot.xlabel("phi (cycles)")
        pyplot.ylim(ymin=0)
        
        ax2 = pyplot.axes(rect, frameon=False, sharex=ax1)
        ax2.yaxis.tick_right()
        ax2.yaxis.set_label_position('right')
        pyplot.setp(ax2.get_xticklabels(), visible=False)
        pyplot.plot(phi / tau, tension, marker='x', markevery=len(phi)/50)
        pyplot.ylabel("x: Tension (kN)")
        pyplot.ylim(ymin=0)
        
        pyplot.axvline(x=phi_max/tau)
    
    i -= 1
    if test('moment'):
        pyplot.figure(i)
        pyplot.plot(phi / tau, moment)
        pyplot.grid(True)
        pyplot.ylabel("Moment of Inertia (kg m^2)")
        pyplot.xlabel("phi (cycles)")
    
    i -= 1
    if test('momentum'):
        pyplot.figure(i)
        pyplot.plot(phi / tau, momentum)
        pyplot.grid(True)
        pyplot.ylabel("Angular Momentum (kg m^2 s^-2)")
        pyplot.xlabel("phi (cycles)")
    
    #import pdb; pdb.set_trace()

def opt_fun(guesses, f, func, init_vars):
    """Solve, plot, and compare the diff eq."""
    global r0, m, Mass, l, dia, max_tension
    r0, m, Mass, dia = guesses
    l, max_tension = cables[nearest(dia)]
    
    time, soln = solve_fun(func, init_vars)
    
    phi, phi1, v_tan, cos_t, tension, moment, momentum, max_arg = \
        interpret(soln)
    phi_max, phi1_max, v_tan_max, tension_max, moment_max, momentum_max = \
        map(lambda x: numpy.ndarray.__getitem__(x, max_arg),
            [phi, phi1, v_tan, tension, moment, momentum])
    
    f.write("%s, " % r0)
    f.write("%s, " % m)
    f.write("%s, " % Mass)
    f.write("%s, " % phi_max)
    f.write("%s, " % phi1_max)
    f.write("%s, " % tension_max)
    f.write("%s\n" % v_tan_max)

    return 2/v_tan_max + Mass/600 + phi1.max() / tau + cable*50

v0 = numpy.sqrt(g(0) * r(0))
w0 = v0/r0
inits = [0, w0]

#outfile = 'C:\\Users\\dave\\Desktop\\out'
outfile = '../out'
def optimize():
    fil = file(outfile, 'w')
    fil.write('r0, sat. mass, Total Mass, cable diameter, '
            + 'phi, omega, Tension, Max Vel.\n')
    guess = (r0, m, Mass)
    #try: print opt_fun(guess, fil, throw, inits)
    try: print fmin(opt_fun, guess, args=(fil, throw, inits))
    finally: fil.close()

def solve():
    time, soln = solve_fun(throw, inits)
    plot(time, soln, ['all', '!gravity'])
    pyplot.show()

def compare():
    global m, Mass
    m = 39.7
    Mass = 650
    time, soln = solve_fun(throw, inits)
    plot(time, soln, ['tension'], 8)
    pyplot.title("Mass = %i" % Mass)
    
    m = 39.7
    Mass = 670
    time, soln = solve_fun(throw, inits)
    plot(time, soln, ['tension'], 7)
    pyplot.title("Mass = %i" % Mass)
    
    m = 39.7
    Mass = 690
    time, soln = solve_fun(throw, inits)
    plot(time, soln, ['tension'], 6)
    pyplot.title("Mass = %i" % Mass)
    
    m = 41.81320863
    Mass = 4172
    time, soln = solve_fun(throw, inits)
    plot(time, soln, ['tension'], 5) 
    pyplot.title("Mass = %i" % Mass)   
    
    pyplot.show()

if len(sys.argv) == 2:
    func_name = sys.argv[1]
    if func_name in dir():
        exec(func_name + '()')