/
traj_calc.py
3147 lines (2580 loc) · 107 KB
/
traj_calc.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
=== TRAJ CALC ===
Re-entry trajectory calculator
"""
from __future__ import print_function
import numpy as np
# import aerocalc.std_atm as atm
#import astropy.constants as ac
import flow_calc_lib as fcl
import heat_flux_lib as hcl
from scipy import integrate
import cantera as ct
#import thermopy as tp
import nrlmsise_00_header as nrl_head
import nrlmsise_00 as nrlmsise
import j77sri as j77
import matplotlib.pyplot as plt
import scipy.interpolate as spint
import rotate_lib
__author__ = 'Nathan Donaldson'
__contributor__ = 'Hilbert van Pelt'
__email__ = 'nathandonaldson@outlook.com'
__status__ = 'Development'
__version__ = '0.60'
__license__ = 'MIT'
# Altitude derivatives for forward Euler solver and ODE solver initialisation
# Velocity
def dv_dh(g, p_dyn, beta, V, gamma):
dvdh = ((p_dyn / beta) - (g * np.sin(gamma))) / (V * np.sin(gamma))
return dvdh
# Flightpath angle
def dgamma_dh(gamma, g, V, R, h):
dgdh = -(np.cos(gamma) * (g * ((V**2) / (R + h)))) / ((V**2) * np.sin(gamma))
return dgdh
# Time
def dt_dh(gamma, V):
dtdh = -1 / (V * np.sin(gamma))
return dtdh
# Ground range
def dr_dh(R, gamma, h):
drdh = (R * np.cos(gamma)) / ((R + h) * np.sin(gamma))
return drdh
# Time derivatives for forward Euler solver and ODE solver initialisation
# Velocity
def dv_dt(g, p_dyn, beta, gamma):
dvdt = (-p_dyn / beta) + (g * np.sin(gamma))
# dvdt = g * ((-p_dyn / beta) + (np.sin(gamma)))
return dvdt
# Flightpath angle
def dgamma_dt(gamma, g, V, R, h, L_D_ratio, p_dyn, beta):
# dgdt = (((p_dyn / beta) * L_D_ratio) +
# ((np.cos(gamma)) * (g - ((V**2) / (R + h))))) / V
dgdt = ((g * np.cos(gamma)) / V) - ((p_dyn * L_D_ratio) / (V * beta)) - \
((V * np.cos(gamma)) / (h + R))
return dgdt
# Time
def dh_dt(gamma, V):
dhdt = -V * np.sin(gamma)
return dhdt
# Ground range
def dr_dt(R, gamma, h, V):
drdt = (R * V * np.cos(gamma)) / (R + h)
return drdt
# Gravitational acceleration variation with altitude
def grav_sphere(g_0, R, h):
g = g_0 * ((R / (R + h))**2)
return g
# Ballistic coefficient
def ballistic_coeff(Cd, m, A):
beta = m / (Cd * A)
return beta
def traj_3DOF_dh(h, y, params):
# Function to be called by ODE solver when altitude integration of governing
# equations is required
V = y[0]
gamma = y[1]
t = y[2]
r = y[3]
R = params[0]
g_0 = params[1]
beta = params[2]
rho = params[3]
C_L = params[4]
C_D = params[5]
g = g_0 * ((R / (R + h))**2)
#rho = atm.alt2density(h)
p_dyn = fcl.p_dyn(rho=rho, V=V)
dy = np.zeros(4)
# dvdh
dy[0] = dv_dh(g, p_dyn, beta, V, gamma)
# dgdh
dy[1] = dgamma_dh(gamma, g, V, R, h)
# dtdh
dy[2] = dt_dh(gamma, V)
# drdh
dy[3] = dr_dh(R, gamma, h)
return dy
def traj_3DOF_dt(t, y, params):
# Function to be called by ODE solver when time integration of governing
# equations is required
V = y[0]
gamma = y[1]
h = y[2]
r = y[3]
R = params[0]
#g_0 = params[1]
g = params[1]
beta = params[2]
rho = params[3]
C_L = params[4]
C_D = params[5]
L_D_ratio = C_L / C_D
#g = g_0 * ((R / (R + h))**2)
#g = grav_sphere(g_0, R, h)
#rho = atm.alt2density(h)
p_dyn = fcl.p_dyn(rho=rho, V=V)
dy = np.zeros(4)
# dvdt
dy[0] = dv_dt(g, p_dyn, beta, gamma)
# dgdt
dy[1] = dgamma_dt(gamma, g, V, R, h, L_D_ratio, p_dyn, beta)
# dhdt
dy[2] = dh_dt(gamma, V)
# drdt
dy[3] = dr_dt(R, gamma, h, V)
return dy
def traj_3DOF_ascent_dt(t, y, params):
# Function to be called by ODE solver when time integration of governing
# equations is required
### STATE
V = y[0]
gamma = y[1]
h = y[2]
r = y[3]
### PARAMETERS
g = params[1]
m = params[2]
alpha = params[3]
F_D = params[4]
F_L = params[5]
F_T = params[6]
dy = np.zeros(4)
# dvdt
dy[0] = -((g * np.sin(gamma)) / (r**2)) - \
(F_D / m) + ((F_T * np.cos(alpha)) / m)
# dgdt
dy[1] = -((g * np.cos(gamma)) / (V * (r**2))) + \
(F_L / (V * m)) + ((V * np.cos(gamma)) / r) + \
((F_T * np.sin(alpha)) / (V * m))
# dhdt
dy[2] = V * np.sin(gamma)
# drdt
dy[3] = (V * np.cos(gamma)) / r
return dy
def traj_3DOF_rotating_dt(t, y, params):
"""
Function to be called by ODE solver for simulations using a rotating
spherical planet assumption.
"""
### STATE
# r: Altitude
# Lambda: Latitude
# delta: Longitude
# V: Velocity
# gamma: Flight path angle
# chi: Bearing
r = y[0]
Lambda = y[1]
delta = y[2]
V = y[3]
gamma = y[4]
chi = y[5]
### PARAMETERS
# R: Planet radius
# g: Gravitational acceleration
# F_D: Drag force
# F_L: Lift force
# F_D: Side force
# F_T: Thrust force
# m: Spacecraft mass
# omega: Planetary rotation speed
# alpha: pitch (thrust) angle
# mu: yaw angle
R = params[0]
g = params[1]
F_D = params[2]
F_L = params[3]
F_S = params[4]
F_T = params[5]
m = params[6]
omega = params[7]
alpha = params[8]
mu = params[9]
# Reserve space for derivatives array
dy = np.zeros(6)
### DERIVATIVES
# Altitude, dr_dt
dy[0] = V * np.sin(gamma)
# Latitude, dLambda_dt
dy[1] = (V * np.cos(gamma) * np.sin(chi)) / r
# Longitude, dDelta_dt
dy[2] = (V * np.cos(gamma) * np.cos(chi)) / (r * np.cos(Lambda))
# Velocity, dV_dt
dy[3] = ((F_T * np.sin(alpha)) / m) + (-g * np.sin(gamma)) + \
(-F_D / m) + (((omega**2) * r * np.cos(Lambda)) * \
((np.cos(Lambda) * np.sin(gamma)) - \
(np.sin(Lambda) * np.cos(gamma) * np.sin(chi))))
# Flight path angle, dGamma_dt
dy[4] = (((V / r) - (g / V)) * np.cos(gamma)) + \
((F_L * np.cos(mu)) / (m * V)) + \
((F_T * np.sin(alpha)) / (m * V)) + \
((F_S * np.sin(mu)) / (m * V)) + \
(2 * omega * np.cos(chi) * np.cos(Lambda)) + \
((((omega**2) * r * np.cos(Lambda)) / V) * \
((np.cos(gamma) * np.cos(Lambda)) + \
(np.sin(gamma) * np.sin(chi) * np.sin(Lambda))))
# Bearing, dChi_dt
dy[5] = ((F_L * np.sin(mu)) / (m * V * np.cos(gamma))) + \
((F_S * np.cos(mu)) / (m * V * np.cos(gamma))) - \
((V / r) * np.cos(gamma) * np.cos(chi) * np.tan(Lambda)) + \
(2 * omega * ((np.tan(gamma) * np.sin(chi) * np.cos(Lambda)) - \
np.sin(Lambda))) - \
(((omega**2) * r * np.cos(chi) * np.cos(Lambda) * np.sin(Lambda)) / \
(V * np.cos(gamma)))
return dy
def traj_6DOF_dt(t, y, params):
dy = None
return dy
def orbit_xyz(t, y, params):
"""
Orbital dynamcis solver by Hilbert van Pelt, Australian Defense Force Academy
Contact: HIlbert.VanPelt@student.adfa.edu.au
"""
Fx = params[0] #force in the x direction
Fy = params[1] #force in the y direction
Fz = params[2] #force in the z direction
Ms = params[3] #mass spacecraft
mu = params[4] #gravitational parameter mian gravitational body
dy = np.zeros(6) #placeholder for derivatives
# Acceleration in X, Y, and Z directions (respectively)
dy[0] = Fx / Ms - (mu * y[3]) / ((y[3]**2 + y[4]**2 + y[5]**2)**(3.0 / 2.0))
dy[1] = Fy / Ms - (mu * y[4]) / ((y[3]**2 + y[4]**2 + y[5]**2)**(3.0 / 2.0))
dy[2] = Fz / Ms - (mu * y[5]) / ((y[3]**2 + y[4]**2 + y[5]**2)**(3.0 / 2.0))
# Position in X, Y and Z directions (respectively)
dy[3] = y[0]
dy[4] = y[1]
dy[5] = y[2]
return dy
# Kinetic energy
def calculateKineticEnergy(m, V):
return 0.5 * m * (V**2)
# Gravitational potential energy
def calculatePotentialEnergy(m, mu, alt, R_planet):
r = R_planet + alt
return -(m * (mu / r))
# Orbital energy
def calculateSpecificOrbitalEnergy(KE, PE, m, gamma):
return ((KE * np.sin(gamma)) + PE) / m
# Gravitational parameter
def calculateGravitationalParameter(m_spacecraft, m_planet):
return 6.67408E-11 * (m_spacecraft + m_planet)
#def calculateGravitationalParameter(h, R_planet, V):
# return (h + R_planet) * (V**2)
def truncate(t, i, l):
for index, item in enumerate(l):
t.__dict__[item] = np.delete(t.__dict__[item],
np.arange(i, len(t.__dict__[item])), axis=0)
if t.spacecraft.aero_coeffs_type == 'VARIABLE':
for item in ['Cd', 'Cl', 'Cs', 'ballistic_coeff']:
t.spacecraft.__dict__[item] = np.delete(t.spacecraft.__dict__[item],
np.arange(i, len(t.spacecraft.__dict__[item])))
sol_temp = t.sol
t.sol = np.zeros([i, 4])
t.sol = sol_temp[0:i, :]
return None
def assign(t, t2, i, l):
for index, item in enumerate(l):
t.__dict__[item][i] = t2.__dict__[item]
return None
def interpolate_event(t, h_interp, l):
final_list = []
for index, item in enumerate(l):
final_list.append(spint.griddata(t.h, t.__dict__[item],
h_interp, method='linear'))
return final_list
def interpolate_atmosphere(t, h_interp):
# Check for attempts to integrate atmosphere below ground level
if h_interp < 0:
h_interp = 0
# Interpolate atmospheic model
rho_interp = spint.griddata(t.atmosphere.h, t.atmosphere.rho,
h_interp, method='linear')
a_interp = spint.griddata(t.atmosphere.h, t.atmosphere.a,
h_interp, method='linear')
p_interp = spint.griddata(t.atmosphere.h, t.atmosphere.p,
h_interp, method='linear')
T_interp = spint.griddata(t.atmosphere.h, t.atmosphere.T,
h_interp, method='linear')
mu_interp = spint.griddata(t.atmosphere.h, t.atmosphere.mu,
h_interp, method='linear')
Cp_interp = spint.griddata(t.atmosphere.h, t.atmosphere.cp,
h_interp, method='linear')
Cv_interp = spint.griddata(t.atmosphere.h, t.atmosphere.cv,
h_interp, method='linear')
return [rho_interp, a_interp, p_interp, T_interp, mu_interp, Cp_interp,
Cv_interp]
def error_out_of_bounds(t, i):
# Check for atmospheric model interpolation errors
# (OUT_OF_BOUNDS error)
if np.isnan(t.solver_rho[i]) == True:
t.out_of_bounds_error = True
print('ERROR: ATMOSPHERIC INTERPOLATION OUT OF BOUNDS AT ' \
'INDEX %i, TRY EXPANDING ALTITUDE RANGE\n=== SIMULATION' \
'ABORTED ===' % i)
return None
def ground_strike(t, i):
if t.h[i] <= 0:
t.ground_strike = True
print('GROUND STRIKE EVENT (ALTITUDE = 0) DETECTED BETWEEN ' \
'INDEXES %i AND %i\n=== SIMULATION ABORTED ===' % (i-1, i))
return None
def latlon():
return None
def atmosphere_nrl_query(h, doy=172, year=0, sec=29000, g_lat=60, g_long=120,
lst=16, f107A=150, f107=150, ap=4, transport_model='Multi'):
"""
Atmosphere model using US Naval Research Laboratory Mass Spectrometer and
Incoherent Scatter Radar atmosphere model. Valid from 0km upwards. Used
primarily for satellite drag simulations.
Order of n:
n_He, n_O, n_N2, n_O2, n_Ar, n_H, n_N, n_total
Order of X:
X_He, X_O, X_N2, X_O2, X_Ar, X_H, X_N
Returns:
[rho, a, p, T, mu, mfp, n, X]
"""
# gas = ct.Solution('nasa_thermo_9.xml')
gas = ct.Solution('nasa_thermo_9.cti')
# gas = ct.Solution('airNASA9.cti')
gas.transport_model = transport_model
# Average molecular diameter of gas
d = 4E-10
nrl_output = nrl_head.nrlmsise_output()
nrl_input = nrl_head.nrlmsise_input()
flags = nrl_head.nrlmsise_flags()
aph = nrl_head.ap_array()
# Set magnetic values array
for index in range(7):
aph.a[index] = 100
# Output in metres (as opposed to centimetres)
flags.switches[0] = 1
# Set other flags to TRUE (see docstring of nrlmsise_00_header.py)
for index in range(1, 24):
flags.switches[index] = 1
nrl_input.doy = doy
nrl_input.year = year
nrl_input.sec = sec
nrl_input.alt = h / 1000
nrl_input.g_lat = g_lat
nrl_input.g_long = g_long
# nrl_input.lst = lst
nrl_input.lst = (sec / 3600.0) + (g_long / 15.0)
nrl_input.f107A = f107A
nrl_input.f107 = f107
nrl_input.ap = ap
#nrl_input.ap_a = self.aph
# Run NRLMSISE00 model
# nrlmsise.gtd7(nrl_input, flags, nrl_output)
nrlmsise.gtd7d(nrl_input, flags, nrl_output)
# Extract density and temperature
rho = nrl_output.d[5]
T = nrl_output.t[1]
# Query Cantera for gas state
gas.TD = T, rho
cp = gas.cp
cv = gas.cv
mu = gas.viscosity
# Perfect gas constant for air (J/kgK)
R = cp - cv
# Ratio of specific heats (J/kgK)
k = cp / cv
# Pressure and speed of sound
p = rho * T * R
a = fcl.speed_of_sound(k, R, T)
# Mean free path
mfp = fcl.mean_free_path(T, p, d)
# Number densities of gas components
n_He = nrl_output.d[0]
n_O = nrl_output.d[1]
n_N2 = nrl_output.d[2]
n_O2 = nrl_output.d[3]
n_Ar = nrl_output.d[4]
n_H = nrl_output.d[6]
n_N = nrl_output.d[7]
# n_AnomO = nrl_output.d[8]
n_total = np.sum([n_He, n_O, n_N2, n_O2, n_Ar, n_H, n_N])
# Generate class structure of number densities
n = np.array([n_He, n_O, n_N2, n_O2, n_Ar, n_H, n_N, n_total])
# Calculate mass fractions of gas components
X_He = n_He / n_total
X_O = n_O / n_total
X_N2 = n_N2 / n_total
X_O2 = n_O2 / n_total
X_Ar = n_Ar / n_total
X_H = n_H / n_total
X_N = n_N / n_total
X = np.array([X_He, X_O, X_N2, X_O2, X_Ar, X_H, X_N])
return [rho, a, p, T, mu, mfp, n, X, k, R]
#def thermal_balance(q_in, q_out, T_init, m, Cp):
# q_in_sum = np.cumsum(q_in)
# T = None
# q_net = None
# return [T, q_net]
class placeholder:
def __init__(self):
pass
#class aero_coeffs(self):
# # This function is called by the solver at every integration step and
# # provides aerodynamic coefficients. Constants or empirical realtions
# # may be specified here.
#
# self.Cd = Cd
# self.Cl = Cl
# self.Cs = Cs
#
# return [Cd, Cl, Cs]
class planet:
"""
Class for storage of planet variables. This class is designed for use
with simulations where the planet variables may be assumed constant
throughout.
Values for EARTH :
Mass = 5.972E24 kg
Mean radius = 6378136.0 m
g_0 = 9.80665
"""
def __init__(self, name, mass, R, g_0):
self.name = name
self.m = mass
self.R = R
self.g_0 = g_0
return None
class spacecraft:
"""
Class for storage of spacecraft variables. This class is designed for use
with simulations where the spacecraft variables may be assumed constant
throughout.
"""
def __init__(self, Cd, m, A, R_n, L, Cl=0, Cs=0):
self.aero_coeffs_type = 'CONSTANT'
self.A = A
self.Cd = Cd
self.Cl = Cl
self.Cs = Cs
self.R_n = R_n
self.m = m
self.L = L
self.ballistic_coeff = (self.m) / (self.Cd * self.A)
return None
class spacecraft_var:
"""
Class for storage of spacecraft variables. This class is designed for use
with simulations where the spacecraft aerodynamic coefficients are variable.
A function (aero_dat) must be supplied which returns drag, lift, and lateral
force coefficients as a function of Mach, Reynolds and Knudsen numbers.
(Note that arguments and returns for aero_dat are given in the required
order in the paragraph above.)
This class must be initialised with the same number of integration steps as
the trajectory class with which it is to be used.
"""
def __init__(self, aero_dat, Cd_init, Cl_init, Cs_init, m, A, R_n, L, steps):
self.aero_coeffs_type = 'VARIABLE'
# Store number of integration steps
# NB: This should be the same as the trajectory instance used to run
# calculations.
self.steps = np.int(steps)
# Store uder-defined function for recalculating aero coefficients
self.aero_dat = aero_dat
#self.Cd, self.Cl, self.Cs = self.aero_dat(self.Re, self.Ma, self.Kn)
# Store spacecraft constants
self.A = A
self.R_n = R_n
self.m = m
self.L = L
# Generate storage stuctures for aero coefficients
self.Cd = np.zeros(self.steps)
self.Cl = np.zeros(self.steps)
self.Cs = np.zeros(self.steps)
self.ballistic_coeff = np.zeros(self.steps)
# Assign initial values for aero coefficients
self.Cd_init = Cd_init
self.Cl_init = Cl_init
self.Cs_init = Cs_init
self.ballistic_coeff_init = ballistic_coeff(self.Cd_init, self.m, self.A)
self.Cd[0] = Cd_init
self.Cl[0] = Cl_init
self.Cs[0] = Cs_init
self.ballistic_coeff[0] = self.ballistic_coeff_init
return None
def update_aero(self, index, Re, Ma, Kn, p_inf, q_inf, rho_inf, gamma_var,
Cd_prev, Cl_prev):
self.Cd[index], self.Cl[index], self.Cs[index] = \
self.aero_dat(Re, Ma, Kn, p_inf, q_inf, rho_inf, gamma_var,
Cd_prev, Cl_prev)
self.ballistic_coeff[index] = ballistic_coeff(self.Cd[index], self.m, self.A)
return None
class launcher_var:
"""
Class for storage of launcher variables. This class is designed for use
with simulations where the spacecraft aerodynamic coefficients are variable.
A function (aero_dat) must be supplied which returns drag, lift, and lateral
force coefficients as a function of Mach, Reynolds and Knudsen numbers.
(Note that arguments and returns for aero_dat are given in the required
order in the paragraph above.)
This class must be initialised with the same number of integration steps as
the trajectory class with which it is to be used.
"""
def __init__(self, aero_dat, thrust_dat, control_dat, Cd_init, Cl_init,
Cs_init, m_init, A, R_n, L, steps, Isp):
self.aero_coeffs_type = 'VARIABLE'
# Store number of integration steps
# NB: This should be the same as the trajectory instance used to run
# calculations.
self.steps = np.int(steps)
# Store uder-defined function for recalculating aero coefficients,
# engine thrust, and thrust vector angle
self.aero_dat = aero_dat
self.thrust_dat = thrust_dat
self.control_dat = control_dat
# Store spacecraft constants
self.A = A
self.R_n = R_n
self.m_init = m_init
self.L = L
# Generate storage stuctures for aero coefficients, forces, and mass
self.Cd = np.zeros(self.steps)
self.Cl = np.zeros(self.steps)
self.Cs = np.zeros(self.steps)
self.ballistic_coeff = np.zeros(self.steps)
self.m = np.zeros(self.steps)
self.F_D = np.zeros(self.steps)
self.F_L = np.zeros(self.steps)
self.F_T = np.zeros(self.steps)
# Assign initial values for aero coefficients, forces, and mass
self.Cd_init = Cd_init
self.Cl_init = Cl_init
self.Cs_init = Cs_init
self.ballistic_coeff_init = ballistic_coeff(self.Cd_init, self.m, self.A)
self.Cd[0] = Cd_init
self.Cl[0] = Cl_init
self.Cs[0] = Cs_init
self.ballistic_coeff[0] = self.ballistic_coeff_init
self.m[0] = self.m_init
self.F_D[0] = fcl.aero_force(self.solver_rho[0], self.V[0], \
self.spacecraft.Cd[0], self.spacecraft.A)
self.F_L[0] = fcl.aero_force(self.solver_rho[0], self.V[0], \
self.spacecraft.Cl[0], self.spacecraft.A)
self.F_T[0] = self.spacecraft.thrust_dat(0)
return None
def update_aero(self, index, Re, Ma, Kn, p_inf, q_inf, rho_inf, gamma_var,
Cd_prev, Cl_prev):
self.Cd[index], self.Cl[index], self.Cs[index] = \
self.aero_dat(Re, Ma, Kn, p_inf, q_inf, rho_inf, gamma_var,
Cd_prev, Cl_prev)
self.ballistic_coeff[index] = ballistic_coeff(self.Cd[index], \
self.m, self.A)
return None
def update_thrust(self, index, t):
self.F_T[index] = self.thrust_dat(t)
return None
def update_mass(self, index):
self.m[index] -= self.F_T[index] / self.Isp
return None
class atmosphere_us76:
"""
Atmosphere model using US Standard Atmosphere 1976 model. Valid from 0km
to 86km altitude.
"""
def __init__(self, h):
# Cantera Solution object
self.gas = ct.Solution('air.xml')
# Discretised altitude steps
self.h = h
# Average molecular diameter of gas
self.d = 4E-10
self.steps = len(h)
self.rho = np.zeros(self.steps)
self.p = np.zeros(self.steps)
self.T = np.zeros(self.steps)
self.a = np.zeros(self.steps)
self.k = np.zeros(self.steps)
self.mu = np.zeros(self.steps)
for index, alt in enumerate(self.h):
self.rho[index] = atm.alt2density(alt, alt_units='m', density_units='kg/m**3')
self.p[index] = atm.alt2press(alt, press_units='pa', alt_units='m')
self.T[index] = atm.alt2temp(alt, alt_units='m', temp_units='K')
self.a[index] = atm.temp2speed_of_sound(self.T[index], temp_units='K', speed_units='m/s')
for index, alt in enumerate(self.h):
self.gas.TP = self.T[index], self.p[index]
self.k[index] = self.gas.cp / self.gas.cv
self.mu[index] = self.gas.viscosity
print('ATMOSPHERIC MODEL COMPUTED (US76)')
return None
class atmosphere_j77:
def __init__(self, h, T_thermosphere):
# Cantera Solution object
self.gas = ct.Solution('air.xml')
# Discretised altitude steps
self.h = h
# Average molecular diameter of gas
self.d = 4E-10
# Ratio of specific heats
self.steps = len(h)
self.rho = np.zeros(self.steps)
self.p = np.zeros(self.steps)
self.T = np.zeros(self.steps)
self.a = np.zeros(self.steps)
self.k = np.zeros(self.steps)
self.mu = np.zeros(self.steps)
# Call Jacchia77 model
data = j77.j77sri(np.max(h), T_thermosphere)
data_np = np.array(data)
h_int = spint.griddata
T_int = spint.griddata
mw_int = spint.griddata
n = spint.griddata
for index, alt in enumerate(self.h):
self.rho[index] = atm.alt2density(alt, alt_units='m', density_units='kg/m**3')
self.p[index] = atm.alt2press(alt, press_units='pa', alt_units='m')
self.T[index] = atm.alt2temp(alt, alt_units='m', temp_units='K')
self.a[index] = atm.temp2speed_of_sound(self.T[index], temp_units='K', speed_units='m/s')
for index, alt in enumerate(self.h):
self.gas.TP = self.T[index], self.p[index]
self.k[index] = self.gas.cp / self.gas.cv
self.mu[index] = self.gas.viscosity
return None
class atmosphere_nrl:
"""
Atmosphere model using US Naval Research Laboratory Mass Spectrometer and
Incoherent Scatter Radar atmosphere model. Valid from 0km upwards. Used
primarily for satellite drag simulations.
"""
def __init__(self, h, doy=172, year=0, sec=29000, g_lat=60, g_long=120,
lst=16, f107A=150, f107=150, ap=4, console_output=True,
transport_model='Mix'):
# Cantera Solution object
self.gas = ct.Solution('nasa_thermo_9.cti')
# self.gas = ct.Solution('nasa_thermo_9.xml')
# self.gas = ct.Solution('air.cti')
# self.gas = ct.Solution('airNASA9.cti')
# Set transport model to multi component (as opposed to mixture-averaged)
# Sacrifices speed for more accurate low temperature results
# self.gas.transport_model = 'Multi'
self.gas.transport_model = transport_model
# Discretised altitude steps
self.h = h
# Average molecular diameter of gas
self.d = 4E-10
self.steps = len(h)
self.output = [nrl_head.nrlmsise_output() for _ in range(self.steps)]
self.input = [nrl_head.nrlmsise_input() for _ in range(self.steps)]
self.flags = nrl_head.nrlmsise_flags()
self.aph = nrl_head.ap_array()
# Set magnetic values array
for index in range(7):
self.aph.a[index] = 100
# Output in metres (as opposed to centimetres)
self.flags.switches[0] = 1
# Set other flags to TRUE (see docstring of nrlmsise_00_header.py)
for index in range(1, 24):
self.flags.switches[index] = 1
for index in range(self.steps):
self.input[index].doy = doy
self.input[index].year = year
self.input[index].sec = sec
self.input[index].alt = self.h[index] / 1000
self.input[index].g_lat = g_lat
self.input[index].g_long = g_long
# self.input[index].lst = lst
self.input[index].lst = (sec / 3600.0) + (g_long / 15.0)
self.input[index].f107A = f107A
self.input[index].f107 = f107
self.input[index].ap = ap
#self.input[index].ap_a = self.aph
# Run NRLMSISE00 model
for index in range(self.steps):
# nrlmsise.gtd7(self.input[index], self.flags, self.output[index])
nrlmsise.gtd7d(self.input[index], self.flags, self.output[index])
# Pre-allocate memory
self.rho = np.zeros(self.steps)
self.T = np.zeros(self.steps)
self.T_exo = np.zeros(self.steps)
self.a = np.zeros(self.steps)
self.k = np.zeros(self.steps)
self.mu = np.zeros(self.steps)
self.cp = np.zeros(self.steps)
self.cv = np.zeros(self.steps)
self.n = np.zeros(self.steps)
self.n_He = np.zeros(self.steps)
self.n_O = np.zeros(self.steps)
self.n_N2 = np.zeros(self.steps)
self.n_O2 = np.zeros(self.steps)
self.n_Ar = np.zeros(self.steps)
self.n_H = np.zeros(self.steps)
self.n_N = np.zeros(self.steps)
self.n_AnomO = np.zeros(self.steps)
self.X = np.zeros([self.steps, 10])
# Extract density and temperature
for index in range(self.steps):
self.rho[index] = self.output[index].d[5]
self.T[index] = self.output[index].t[1]
self.T_exo[index] = self.output[index].t[0]
self.n_He[index] = self.output[index].d[0]
self.n_O[index] = self.output[index].d[1]
self.n_N2[index] = self.output[index].d[2]
self.n_O2[index] = self.output[index].d[3]
self.n_Ar[index] = self.output[index].d[4]
self.n_H[index] = self.output[index].d[6]
self.n_N[index] = self.output[index].d[7]
self.n_AnomO[index] = self.output[index].d[8]
# Sum only number densities of species modelled by Cantera 'air.xml' object
# self.n[index] = np.sum([self.n_He[index], self.n_O[index], \
# self.n_N2[index], self.n_O2[index], self.n_Ar[index], \
# self.n_H[index], self.n_N[index]])
# Sum only number densitites initially modelled in 11-species air model
# self.n[index] = np.sum([self.n_O[index], self.n_N2[index], \
# self.n_O2[index], self.n_N[index]])
# Sum full number density
self.n[index] = np.sum([self.n_He[index], self.n_O[index], \
self.n_N2[index], self.n_O2[index], self.n_Ar[index], \
self.n_H[index], self.n_N[index], self.n_AnomO[index]])
self.X_names = ['O', 'O2', 'N2', 'N', 'H', 'He', 'Ar']
self.X[index, 0] = self.n_O[index] / self.n[index]
self.X[index, 1] = self.n_O2[index] / self.n[index]
self.X[index, 2] = self.n_N2[index] / self.n[index]
self.X[index, 3] = self.n_N[index] / self.n[index]
self.X[index, 4] = self.n_H[index] / self.n[index]
self.X[index, 5] = self.n_He[index] / self.n[index]
self.X[index, 6] = self.n_Ar[index] / self.n[index]
# self.X[index, 7] = self.n_AnomO[index] / self.n[index]
# self.X[index, 0] = self.n_O[index] / self.n[index]
# self.X[index, 1] = self.n_O2[index] / self.n[index]
# self.X[index, 2] = self.n_N[index] / self.n[index]
# self.X[index, 6] = self.n_N2[index] / self.n[index]
# self.X[index, 7] = self.n_Ar[index] / self.n[index]
# Query Cantera for gas state
# for index, alt in enumerate(self.h):
# self.gas.TD = self.T[index], self.rho[index]
# self.cp[index] = self.gas.cp
# self.cv[index] = self.gas.cv
# self.mu[index] = self.gas.viscosity
for index, alt in enumerate(self.h):
self.gas.X = self.X[index, :]
self.gas.TD = self.T[index], self.rho[index]
self.cp[index] = self.gas.cp
self.cv[index] = self.gas.cv
self.mu[index] = self.gas.viscosity
# Perfect gas constant for air (J/kgK)
self.R = self.cp - self.cv
# Ratio of specific heats (J/kgK)
self.k = self.cp / self.cv
# Pressure and speed of sound
self.p = self.rho * self.T * self.R
self.a = fcl.speed_of_sound(self.k, self.R, self.T)
# Mean free path
self.mfp = fcl.mean_free_path(self.T, self.p)
self.l = ['p', 'a', 'k', 'R', 'cp', 'cv', 'mu', 'rho', 'T', 'T_exo',
'n_He', 'n_O', 'n_N2', 'n_O2', 'n_Ar', 'n_H', 'n_N', 'n', 'X',
'n_AnomO', 'd', 'h']
if console_output == True:
print('ATMOSPHERIC MODEL COMPUTED (NRLMSISE00)')
return None
class trajectory_ballistic:
"""
Ballistic trajectory calculator. No lifting forces are considered in this
model, only drag and gravity. The integration step therefore is altitude,
and all diffrenetial equations are formulated wrt. h (altitude).
"""
def __init__(self, vehicle, atmosphere, gamma_init, V_init, g_0, R):
# NB: vehicle should be an instance of the class 'spacecraft'
# Import atmospheric model
self.atmosphere = atmosphere #atmosphere(self.h)
# Copy altitude array for convenience
self.h = self.atmosphere.h
self.steps = self.atmosphere.steps
# Import spacecraft entering atmosphere
self.spacecraft = vehicle
# Set astronomical constants
self.R = R
self.g_0 = g_0
# Set initial values
self.gamma_init = gamma_init
self.V_init = V_init
self.h_init = self.h[0]
# Define integration points in h
#self.h = h #np.linspace(h_init, h_end, steps)
self.del_h = np.abs(self.h[1] - self.h[0])
# Calculate variance in gravitational acceleration using inverse
# square law
self.g = grav_sphere(self.g_0, self.R, self.h)
# Pre-allocate memory for iterative trajectory calculations
self.V = np.zeros(self.steps)
self.gamma = np.zeros(self.steps)
self.t = np.zeros(self.steps)
self.r = np.zeros(self.steps)
self.dvdh = np.zeros(self.steps)
self.dgdh = np.zeros(self.steps)
self.dtdh = np.zeros(self.steps)
self.drdh = np.zeros(self.steps)
self.p_dyn = np.zeros(self.steps)
self.Ma = np.zeros(self.steps)
return None
def initialise(self):
self.V[0] = self.V_init
self.gamma[0] = self.gamma_init
self.p_dyn[0] = fcl.p_dyn(rho=self.atmosphere.rho[0], V=self.V[0])
self.Ma[0] = self.V[0] / self.atmosphere.a[0]
self.dvdh[0] = dv_dh(self.g[0], self.p_dyn[0], \
self.spacecraft.ballistic_coeff, self.V[0], self.gamma[0])