-
Notifications
You must be signed in to change notification settings - Fork 3
/
AddBinary.py
1404 lines (1139 loc) · 42.6 KB
/
AddBinary.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
"""
David Fleming
Note: All functions in the module only interact with the binary system itself. For functions that work with
the disk or binary + disk, consult binaryUtils.py
Has all functions and utilities required to initialize/analyze binary star system
Initial input:
-Snapshot with central star of mass M (in Msol) located at (0,0,0)
in tipsy format (works with pynbody simArray data structure)
-Period (days), eccentricity of binary system
What This does:
Converts central star into 2 stars 1,2 under following conditions:
m1 + m2 = M (divide mass into 2 stars)
Center of Mass of stars remains at (0,0,0)
Used to initialize velocites of binary system given Keplerian relations
Also has additional functions to initialize and analyze binary system
#!!! Note: v_unit_vel will always be 29.785598165 km/s when m_unit = Msol and r_unit = 1 AU in kpc!!!
"""
#Constants and includes
import numpy as np
import math
from scipy import optimize
import sys
sys.path.append('/astro/users/dflemin3/Desktop/ICgen')
import isaac
import pynbody
SimArray = pynbody.array.SimArray
# Units/Constants
Msol = 1.98855e33 # g/Solar mass
BigG = 6.67259e-8 # in cgs
G = SimArray(BigG,'cm**3 g**-1 s**-2')
YEARSEC = 3.15569e7 # seconds per year
DAYSEC = 86400 # seconds per day
AUCM = 1.49597571e13 # cm/au
RAD2DEG = 180.0 / np.pi
SMALL = 1.0e-10 # less than this is zero enough
# ICgen-Specific constants (Shouldn't need to use these!)
VEL_UNIT = 29.785598165 # 29.785598165 km/s
POS_UNIT = 4.84813680873e-9 # in kpc == 1 au
# Function prototypes
# Binary Star Initilization Functions
def pToA(period=1, M=1):
"""
Converts period (in days) into semimajor axis (in au) given Kepler law
Parameters
----------
Period : float
[days]
M : float
COM mass [Msol]
Returns
-------
Semimajor axis a (au)
"""
conv = (DAYSEC * DAYSEC * Msol) / (AUCM * AUCM * AUCM)
a = conv * period * period * BigG * M / (4.0 * np.pi * np.pi)
return pow(a, 1.0 / 3.0)
# end function
def aToP(a=1, M=1):
"""
Given a semimajor axis (au), convert into period (in days) given Kepler law
Parameters
----------
a : float
semimajor axis [au]
M : float
mass of system [Msol]
Returns
-------
Period : float
[days]
"""
conv = (AUCM * AUCM * AUCM) / (DAYSEC * DAYSEC * Msol)
P = 4.0 * conv * np.pi * np.pi * a * a * a / (BigG * M)
return np.sqrt(P)
# end function
##########################################################################
# #
# Functions for naively initializing binary stars at perihelion with arg peri = 0, LoAN = 0, inc = 0 #
# #
##########################################################################
def calcPositions(M=1, a=1, e=0, p=0.5):
"""
Given total mass of system M, semimajor axis a, and percentage of total mass contained in primary star p,
calculate positions of binary components keep COM at origin
(ex: If 1Msol system and MPri = Msec = 0.5 -> M =1 -> p = 0.5)
Assume stars start a perihelion
Parameters
----------
M: float
Total mass of system (Msol)
a: float
Semimajor axis (au)
e: float
eccentricity
p: float
% of total mass contained in primary (m1)
Returns
-------
x1, x2: float
Semimajor axes of binary stars assuming that they start at perihelion.
"""
# Compute masses
m1 = p * M
m2 = (M - m1)
# Calculate each star's semi-major axis a1,a2
a1 = (m2 / M) * a
a2 = (m1 / M) * a
# Assume both stars star at perihelion (true anomaly = 0)
x1 = a1 * (1 - e * e) / (1 + e)
x2 = -1 * a2 * (1 - e * e) / (1 + e)
return x1, x2
# end function
def calcV(m1=0.5, m2=0.5, a=1, e=0):
"""
Given total mass M, postions of stars at perihelion x1, x2, and eccentricity e, calculate the velocities of the stars
assuming that they are located at the perihelion and rotate in same direction as disk (CCW)
Parameters
----------
m1, m2: floats
masses of primary and secondary (Msol)
x1, x2: floats
are semimajor axes of primary and secondary (au)
e: float
eccentricity
Returns
-------
v1, v2: floats
velocities of m1, m2 in km/s oriented for CCW rotation (in xy plane)
"""
# Correct units and conversion factors, sqrt of positive numbers
M = m1 + m2
econv = (Msol) / (AUCM * 100 * 100 * 1000 * 1000)
# Elliptical Orbit: initialize CCW velocity given orbit starts at
# perihelion
eps = (1 + e) / (1 - e)
mu = (m1 * m2) / M
vp = math.sqrt(econv * (BigG * M * eps) / (a))
v1 = (mu / m1) * vp # positive y direction for primary
v2 = (-mu / m2) * vp # negative y direction for secondary
return v1, v2
# end function
def calcCriticalRadius(a=1, e=0, m1=0.5, m2=0.5):
"""
Calculates the approximate bounds for where we would expect a planet to form
given the conditions of a circumbinary disk around a short-period binary system.
Calculates based on best fit from Holman&Wiegert+1999 (outer/P-type region)
Assumes m1 ~ m2 and NOT m1 >> m2 or m2 >> m1
Parameters
----------
a: float
Semimajor axis a of the binary system (au)
e: float
Eccentricity e of binary system
m1, m2: floats
Masses of binary components m1, m2 (Msol)
Returns
-------
ac, pmac: floats
Lower bounds for circumbinary planet's distace from binary COM and error terms of the following form:
ac, pmac (error bounds are symmetric) both in au
"""
# Compute mass ratio
mu = m2 / (m1 + m2)
# Compute critical following Holman&Wiegert+1999 with symmetric error for
# the outer region
ac = 1.60 + (5.10 * e) + (-2.22 * e * e) + (4.12 * mu) + \
(-4.27 * e * mu) + (-5.09 * mu * mu) + (4.61 * e * e * mu * mu)
ac *= a
pmac = 0.04 + (0.05 * e) - (0.11 * e * e) + (0.09 * mu) - \
(.17 * e * mu) - (0.11 * mu * mu) + (0.36 * e * e * mu * mu)
pmac *= a
return float(ac), float(pmac)
# end function
##########################################################################
# #
# Functions for computing Keplerian Orbital elements from Cartesian coordinates. Most useful for #
# computing for binaries, but general enough to compute for gas particles or whatever. #
# #
##########################################################################
def calcOrbitalElements(x1, x2, v1, v2, m1, m2):
"""
Given as pynbody SimArrays the cental mass(es), the coodinate(s) and velocity(ies) of a CCW orbiting object,
return the following orbital elements: eccentricity, semimajor axis, inclination, longitude of ascending node,
argument of periapsis, and true anomaly. This function is designed to work for a binary star system but is
general enough to also work for a ~massless gas particle orbiting around a central quasi-Keplerian mass.
Parameters
----------
All input assumed to be in simulation units and are converted to desired units internally.
x1,x2: position arrays in AU (x2 = 0 for gas particle case)
v1,v2: velocity arrays in km/s (v2 = 0 for gas particle case)
m1,m2: Central masses in Msol
Returns
-------
e: float
Eccentricity (unitless)
a: float
Semimajor Axis in Au
i: float
Inclination in degrees
Omega: float
Longitude of Ascending node in degrees
w: float
Argument of Periapsis in degrees
nu: float
True Anomaly in degrees
"""
# Compute elements. All unit conversion/processing done in sub functions
e = calcEcc(x1, x2, v1, v2, m1, m2)
a = calcSemi(x1, x2, v1, v2, m1, m2)
i = calcInc(x1, x2, v1, v2)
Omega = calcLongOfAscNode(x1, x2, v1, v2)
w = calcArgPeri(x1, x2, v1, v2, m1, m2)
nu = calcTrueAnomaly(x1, x2, v1, v2, m1, m2)
return e, a, i, Omega, w, nu
# end function
def calcEcc(x1, x2, v1, v2, m1, m2, flag=True):
"""
Given as pynbody arrays the masses of the binary system, arrays of the positions and velocities, compute
its orbital eccentricity.
Calculates e using following: e = sqrt(1 + (2*e*h^2)/(mu^2)
for h = r x v, mu = G*(m1+m2), e = (v^2)/2 - (mu/|r|)
Parameters
----------
All inputs expected to be pynbody simArrays!!!
x1, x2: SimArrays
Position arrays of primary and secondary x1, x2 (in AU)
v1, v2: SimArrays
Velocity arrays of primary and secondary v1, v2 (in km/s)
m1, m2: SimArrays
Masses of primary and secondary m1, m2 (in Msol)
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
e: float
Scalar eccentricity of binary system.
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
#m1 = np.asarray(isaac.strip_units(m1)) * Msol
#m2 = np.asarray(isaac.strip_units(m2)) * Msol
length, ax = computeLenAx(x1)
# Relative position vector in cgs
r = (x1 - x2)
magR = SimArray(np.linalg.norm(r, axis=ax),'cm')
# Compute standard gravitational parameter in cgs
mu = (G * (m1 + m2)).in_units('cm**3 s**-2')
# Compute relative velocity vector in cgs with appropriate scale
v = (v1 - v2)
magV = SimArray(np.linalg.norm(v, axis=ax),'cm s**-1')
# Compute specific orbital energy
eps = ((magV * magV / 2.0) - (mu / magR)).in_units('cm**2 s**-2')
# Compute specific angular momentum vector
h = SimArray(np.cross(r, v, axis=ax),'cm**2 s**-1')
magH = SimArray(np.linalg.norm(h, axis=ax),'cm**2 s**-1')
# Compute, return eccentricity
return np.sqrt(1.0 + ((2.0 * eps * magH * magH) / (mu * mu)))
# end function
def calcSemi(x1, x2, v1, v2, m1, m2, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the binary's semimajor axis.
Calculates a using the following: a = -mu/(2*e)
where mu = G*(m1+m2) and e = (v^2)/2 - (mu/|r|)
Parameters
----------
(as pynbody SimArrays!)
x1,x2: SimArrays
Primary and secondary position arrays [AU]
v1,v2: SimArrays
Primary and secondary velocity arrays [km/s]
m1,m2: SimArrays
Primary and secondary masses [Msol]
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
a: float
semimajor axis of binary orbit in AU
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
# Remove units since input is pynbody SimArray
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
#m1 = np.asarray(isaac.strip_units(m1)) * Msol
#m2 = np.asarray(isaac.strip_units(m2)) * Msol
length, ax = computeLenAx(x1)
# Relative position vector in cgs
r = (x1 - x2)
magR = SimArray(np.linalg.norm(r, axis=ax),'cm')
# Compute standard gravitational parameter in cgs
mu = (G * (m1 + m2))
# Compute relative velocity vector in cgs with appropriate scale
v = (v1 - v2)
magV = SimArray(np.linalg.norm(v, axis=ax),'cm s**-1')
# Compute specific orbital energy
eps = (magV * magV / 2.0) - (mu / magR)
# Compute, return semimajor axis in AU (convert from cgs->AU)
return (-mu / (2.0 * eps)).in_units('au') #/ (AUCM)
# end function
def calcInc(x1=1, x2=0, v1=1, v2=0, flag=True):
"""
Given pynbody arrays for positions and velocities of primary and secondaries bodies and masses in
a binary orbit, calculate's the orbit's inclination. (Given: Orbit starts in xy plane)
i = arccos(h_z/|h|)
Parameters
----------
as pynbody SimArrays [preferred units]
x1,x2: SimArrays
Primary and secondary position arrays [AU]
v1, v2: SimArrays
Primary and secondary velocity arrays [km/s]
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
i: float
Inclination in degrees
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
# Strip units from all inputs
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
# Compute length of array we're dealing with
length, ax = computeLenAx(x1)
# Relative position vector in cgs
r = (x1 - x2)
# Compute relative velocity vector in cgs with appropriate scale
v = (v1 - v2)
# Compute specific angular momentum vector
h = np.cross(r, v, axis=ax)
magH = SimArray(np.linalg.norm(h, axis=ax),'cm**2 s**-1')
if(length > 1):
h_z = h[:, 2]
else:
h_z = h[0, 2]
# Orbit is CCW (h_z < 0) so take fabs to have i >= 0
h_z = np.fabs(h_z)
# Compute i, convert to degrees
i = np.arccos(h_z / magH)
return i * RAD2DEG # return in degrees
# end function
def calcLongOfAscNode(x1=1, x2=0, v1=1, v2=0, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the binary's longitude of the ascending node Omega.
Usage note: Intended for binary system, but pass x2 = v2 = 0 to use with any
location in the disk.
Calculates Omega using the following: Omega = arccos(n_x/|n|) n_y > 0
Omega = 2*pi - arccos(n_x/|n|) n_y < 0
where n = (0,0,1) x h for h = r x v
Parameters
----------
Assumed as pynbody SimArrays in simulation units (AU, scaled velocity, etc)
x1,x2: SimArrays
Primary and secondary position arrays [AU]
v1,v2: SimArrays
Primary and secondary velocity arrays [km/s]
Returns
-------
Omega: float
longitude of the ascending node in degrees
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
# Strip units from all inputs and convert to cgs
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
# Define unit vectors pointing along z, x and y axes respectively
# Also ensure function can handle any number of values
length, ax = computeLenAx(x1)
k = np.zeros((length, 3))
i = np.zeros((length, 3))
j = np.zeros((length, 3))
if(length > 1):
j[:, 1] = 1
i[:, 0] = 1
k[:, 2] = 1
else:
j[0, 1] = 1
i[0, 0] = 1
k[0, 2] = 1
# Relative position vector in cgs
r = (x1 - x2)
# Compute relative velocity vector in cgs with appropriate scale
v = (v1 - v2)
# Compute specific angular momentum vector
h = np.cross(r, v, axis=ax)
# Compute vector pointing to ascending node
n = np.cross(k, h, axis=ax)
magN = SimArray(np.linalg.norm(n, axis=ax),'cm**2 s**-1')
# Ensure no divide by zero errors?
#magN[magN < SMALL] = 1.0
# Compute LoAN
inc = calcInc(x1, x2, v1, v2)/RAD2DEG
Omega = np.arccos(dotProduct(i, n) / magN)
# If inclination is ~0, define LoAN as 0
Omega[inc < SMALL] = 0.0
# Fix phase due to arccos return range
Omega[dotProduct(n, j) < 0] = 2.0 * np.pi - Omega[dotProduct(n, j) < 0]
# Convert to degrees, return
return Omega * RAD2DEG
# end function
def calcEccVector(x1=1, x2=0, v1=1, v2=0, m1=1, m2=1, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the eccentricity vector in the reduced two body system.
Usage note: Intended for binary system, but pass x2 = v2 = 0 to use with any
location in the disk.
Parameters
----------
x1,x2: SimArrays
Primary and secondary position arrays [length]
v1, v2: SimArrays
Primary and secondary velocity arrays [velocity]
m1,m2: SimArrays
Primary and secondary masses [mass]
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
Ecc: array
Eccentricity vector in cgs
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
# Remove units in case input is pynbody SimArray
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
#m1 = np.asarray(isaac.strip_units(m1)) * Msol
#m2 = np.asarray(isaac.strip_units(m2)) * Msol
# Determine length of arrays
length, ax = computeLenAx(x1)
# Relative position vector in cgs
r = (x1 - x2)
magR = SimArray(np.linalg.norm(r, axis=ax).reshape(len(r),1),'cm')
# Compute standard gravitational parameter in cgs
mu = (G * (m1 + m2)).reshape(len(r),1)
# Compute relative velocity vector in cgs with appropriate scale
v = (v1 - v2)
# Compute specific angular momentum vector
h = SimArray(np.cross(r, v, axis=ax),'cm**2 s**-1')
# Compute, return eccentricity vector
return (SimArray(np.cross(v, h, axis=ax),'cm**3 s**-2') / mu) - (r / magR)
# end function
def calcArgPeri(x1=1, x2=0, v1=1, v2=0, m1=1, m2=1, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the eccentricity vector in the reduced two body system.
Usage note: Intended for binary system, but pass x2 = v2 = 0 to use with any
location in the disk.
Parameters
----------
Assumed as pynbody SimArrays [preferred units]
x1,x2 : SimArrays
Primary and secondary position arrays [AU]
v1,v2 : SimArrays
Primary and secondary velocity arrays [km/s]
m1, m2 : SimArrays
Primary and secondary masses [Msol]
Flag : bool
Whether or not to internally convert to cgs units
Returns
-------
w: float
Argument of pericenter in degrees
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
# Remove units since input is pynbody SimArray
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
#m1 = np.asarray(isaac.strip_units(m1)) * Msol
#m2 = np.asarray(isaac.strip_units(m2)) * Msol
# Compute eccentricity vector
e = calcEccVector(x1, x2, v1, v2, m1, m2, flag=False)
magE = SimArray(np.linalg.norm(e, axis=1),'1')
length, ax = computeLenAx(x1)
# Define unit vector pointing along z axis
k = np.zeros((length, 3))
if length > 1:
k[:, 2] = 1.0
else:
k[0, 2] = 1.0
# Define specific angular momentum vector
# Relative position vector in cgs
r = (x1 - x2)
# Compute relative velocity vector in cgs with appropriate scale
v = (v1 - v2)
# Compute specific angular momentum vector
h = SimArray(np.cross(r, v, axis=ax),'cm**2 s**-1')
# Compute vector pointing to ascending node
n = np.cross(k, h, axis=ax)
magN = SimArray(np.linalg.norm(n, axis=ax),'cm**3 s**-1')
# Ensure no divide by zero errors?
magN[magN < SMALL] = 1.0
# Compute argument of periapsis
inc = calcInc(x1, x2, v1, v2)/RAD2DEG
arg = dotProduct(n, e) / (magN * magE)
# Bounds check arg
w = np.arccos(arg)
#w[arg < -1.0] = np.pi
#w[arg > 1.0] = 0.0
w[dotProduct(e, k) < 0] = 2.0 * np.pi - w[dotProduct(e, k) < 0.0]
w[inc < SMALL] = 0.0 # For orbit in a plane
#Return in degrees
return w * RAD2DEG
# end function
def calcTrueAnomaly(x1=1, x2=0, v1=1, v2=0, m1=1, m2=1, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the true anomaly in the reduced two body system.
Usage note: Intended for binary system, but pass x2 = v2 = 0 to use with any
location in the disk.
Parameters
----------
Assumed as pynbody SimArrays [preferred units]
x1,x2: SimArrays
Primary and secondary position arrays [AU]
v1,v2: SimArrays
Primary and secondary velocity arrays [km/s]
m1, m2: SimArrays
Primary and secondary masses [Msol]
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
nu: float
True anomaly in degrees
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
# Remove units since input is pynbody SimArray
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
#m1 = np.asarray(isaac.strip_units(m1)) * Msol
#m2 = np.asarray(isaac.strip_units(m2)) * Msol
# Compute length, correct axis
length, ax = computeLenAx(x1)
# Compute eccentricity vector
e = calcEccVector(x1, x2, v1, v2, m1, m2, flag=False)
magE = SimArray(np.linalg.norm(e, axis=ax),'1')
# Compute radius vector
r = (x1 - x2)
v = (v1 - v2)
magR = SimArray(np.linalg.norm(r, axis=ax),'cm')
# Compute true anomaly making sure I can handle single numbers or arrays
nu = np.arccos(dotProduct(e, r) / (magE * magR))
if isinstance(nu, np.float64):
if dotProduct(r, v) < 0.0:
nu = 2.0 * np.pi - nu
else:
nu[dotProduct(r, v) < 0.0] = 2.0 * np.pi - nu[dotProduct(r, v) < 0.0]
# Convert to degrees, return
return nu * RAD2DEG
# end function
def calcEccentricAnomaly(x1=1, x2=0, v1=1, v2=0, m1=1, m2=1, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the eccentric anomaly in the reduced two body system.
Usage note: Intended for binary system, but pass x2 = v2 = 0 to use with any
location in the disk.
Parameters
----------
Assumed as pynbody SimArrays [preferred units]
x1,x2: SimArrays
Primary and secondary position arrays [AU]
v1,v2: SimArrays
Primary and secondary velocity arrays [km/s]
m1, m2: SimArrays
Primary and secondary masses [Msol]
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
E: float
Eccentric anomaly in degrees
"""
#This if/else is stupid and I should change it eventually--dflemin3
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
# Remove units since input is pynbody SimArray
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
#m1 = np.asarray(isaac.strip_units(m1)) * Msol
#m2 = np.asarray(isaac.strip_units(m2)) * Msol
e = calcEcc(x1, x2, v1, v2, m1, m2, flag=False)
nu = calcTrueAnomaly(x1, x2, v1, v2, m1, m2, flag=False)
else:
e = calcEcc(x1, x2, v1, v2, m1, m2, flag=False)
nu = calcTrueAnomaly(x1, x2, v1, v2, m1, m2, flag=False)
# Calc E
nu = nu * (np.pi / 180.0) # convert to radians for numpy functions
E = np.arccos((e + np.cos(nu)) / (1.0 + e * np.cos(nu)))
# Make sure this can handle single numbers or arrays
if isinstance(E, np.float64):
if nu > np.pi and nu < 2.0 * np.pi:
E = 2.0 * np.pi - E
else:
E[np.logical_and(nu > np.pi, nu < 2.0 * np.pi)] = 2.0 * np.pi - E
# Return E in degrees
return E * RAD2DEG
def calcMeanAnomaly(x1=1, x2=0, v1=1, v2=0, m1=1, m2=1, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the Mean anomaly in the reduced two body system.
Usage note: Intended for binary system, but pass x2 = v2 = 0 to use with any
location in the disk.
Parameters
----------
Assumed as pynbody SimArrays [preferred units]
x1,x2: SimArrays
Primary and secondary position arrays [AU]
v1,v2: SimArrays
Primary and secondary velocity arrays [km/s]
m1, m2: SimArrays
Primary and secondary masses [Msol]
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
M: float
Mean anomaly in degrees
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
# Remove units since input is pynbody SimArray
#x1 = np.asarray(isaac.strip_units(x1)) * AUCM
#x2 = np.asarray(isaac.strip_units(x2)) * AUCM
#v1 = np.asarray(isaac.strip_units(v1)) * 1000 * 100 * VEL_UNIT
#v2 = np.asarray(isaac.strip_units(v2)) * 1000 * 100 * VEL_UNIT
#m1 = np.asarray(isaac.strip_units(m1)) * Msol
#m2 = np.asarray(isaac.strip_units(m2)) * Msol
e = calcEcc(x1, x2, v1, v2, m1, m2, flag=False)
E = calcEccentricAnomaly(x1, x2, v1, v2, m1, m2, flag=False)
else:
e = calcEcc(x1, x2, v1, v2, m1, m2, flag=False)
E = calcEccentricAnomaly(x1, x2, v1, v2, m1, m2, flag=False)
# Calculate Mean Anomaly
E = E * (np.pi / 180.0) # Conver E to radians for numpy
M = E - e * np.sin(E)
# Return M in degrees
return (M * RAD2DEG)
# end function
def trueToMean(nu, e):
"""
Given the true anomaly nu in degrees and the eccentricity e, compute the mean anomaly M in degrees.
Parameters
----------
nu: float
True anomaly (degrees)
e: float
eccentricity
Returns
-------
M: float
mean anomaly (degrees)
"""
# Compute eccentric anomaly E
nu = nu * (np.pi / 180.0) # convert to radians for numpy functions
E = np.arccos((e + np.cos(nu)) / (1.0 + e * np.cos(nu)))
# Make sure this can handle single numbers or arrays
if isinstance(E, np.float64):
if nu > np.pi and nu < 2.0 * np.pi:
E = 2.0 * np.pi - E
else:
E[np.logical_and(nu > np.pi, nu < 2.0 * np.pi)] = 2.0 * np.pi - E
# Compute, return M
M = E - e * np.sin(E)
return (M * RAD2DEG)
#end function
def calcMeanMotion(x1, x2, v1, v2, m1, m2, flag=True):
"""
Given pynbody arrays for positions and velocity of primary and secondary bodies
and masses, calculates the Mean motion in the reduced two body system.
Usage note: Intended for binary system, but pass x2 = v2 = 0 to use with any
location in the disk.
Parameters
----------
Assumed as pynbody SimArrays [preferred units]
x1,x2: SimArrays
Primary and secondary position arrays [AU]
v1,v2: SimArrays
Primary and secondary velocity arrays [km/s]
m1, m2: SimArrays
Primary and secondary masses [Msol]
Flag: bool
Whether or not to internally convert to cgs units
Returns
-------
n: SimArray
Mean motion in 1/s
"""
if flag:
#Ensure units are in cgs
x1 = x1.in_units('cm')
x2 = x2.in_units('cm')
v1 = v1.in_units('cm s**-1')
v2 = v2.in_units('cm s**-1')
m1 = m1.in_units('g')
m2 = m2.in_units('g')
#Put binary in reduced mass frame, compute, return
a = SimArray(calcSemi(x1,x2,v1,v2,m1,m2,flag=False),'au').in_units('cm')
mu = G * (m1 + m2)
return np.sqrt(mu/np.power(a,3))
#end function
##########################################################################
# #
# Functions for computing Cartesian coordinates from Keplerian orbital elements. #
# #
##########################################################################
def keplerToCartesian(
a,
e,
i,
Omega,
w,
M,
m1,
m2,
angleFlag=True,
scaleFlag=True):
"""
Given the Keplerian orbital elements, compute the cartesian coordinates of the object orbiting
in the reduced mass frame. Note: Requires all angles in degrees unless noted.
Note: A little redudant that I compute M when I typically already know the true anomaly nu, but most
other schemes know M initially instead of nu so I'll keep it for compatibility's sake.
Parameters
----------
a: float
Semimajor axis (AU)
e: float
Eccentricity
i: float
inclination (degrees)
Omega: float
Longitude of Ascending Node (degrees)
w: float
Argument of Pericenter (degrees)
M: float
Mean Anomaly (degrees)
m1, m2: float
Masses of central object(s) (Msol)