-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtriple_class.py
2599 lines (2114 loc) · 122 KB
/
triple_class.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
from interactions import *
from tidal_friction_constant import *
import sys
import time
from amuse.units import units, constants
from amuse.datamodel import Particles
from amuse.io import write_set_to_file
from amuse.units import quantities
from scipy.stats import maxwell
from scipy import optimize
# from math import isnan
import numpy as np
# from tres_options import REPORT_USER_WARNINGS, \
# GET_GYRATION_RADIUS_FROM_STELLAR_CODE, \
# GET_AMC_FROM_STELLAR_CODE, \
# REPORT_TRIPLE_EVOLUTION, \
# REPORT_DEBUG, \
# MAKE_PLOTS, \
# REPORT_DT, \
# kozai_type_factor, \
# maximum_time_step_factor, \
# minimum_time_step
from TRES_options import *
from TRES_setup import setup_secular_code, setup_stellar_code
from TRES_plotting import plot_data_container
class Triple_Class:
#-------
#setup stellar system
def __init__(self, stars, bins, correct_params,
stellar_code, secular_code, relative_inclination,
tend, tinit, number, maximum_radius_change_factor,
stop_at_mass_transfer, stop_at_init_mass_transfer, stop_at_outer_mass_transfer,
stop_at_stable_mass_transfer, stop_at_eccentric_stable_mass_transfer,
stop_at_unstable_mass_transfer, stop_at_eccentric_unstable_mass_transfer, which_common_envelope,
stop_at_no_CHE, include_CHE,
stop_at_merger, stop_at_disintegrated, stop_at_inner_collision, stop_at_outer_collision,
stop_at_dynamical_instability, stop_at_semisecular_regime,
stop_at_SN, SN_kick_distr, impulse_kick_for_black_holes,fallback_kick_for_black_holes,
stop_at_CPU_time, max_CPU_time,
file_name, file_type, dir_plots):
self.correct_params = correct_params
if correct_params == False:
self.triple = Particles(1)
return
self.tend = tend
self.tinit = tinit
self.previous_time = 0.0|units.yr
self.previous_dt = 0.0|units.yr
self.instantaneous_evolution = False
if tinit > 0|units.Myr:
self.instantaneous_evolution = True # no secular
self.maximum_radius_change_factor = maximum_radius_change_factor
self.fixed_timestep = -1|units.Myr
self.file_name = file_name
self.file_type = file_type
self.which_common_envelope = which_common_envelope
self.include_CHE = include_CHE
if REPORT_USER_WARNINGS and self.include_CHE:
print("Note: For CHE evolution to be included, it also needs to be switched on manually in SeBa.")
print("\t This can be done by setting include_CHE=True in SeBa's sstar/starclass/constants.C.")
print("\t AMUSE developer mode is required to access SeBa files.")
self.SN_kick_distr = SN_kick_distr
self.impulse_kick_for_black_holes = impulse_kick_for_black_holes
self.fallback_kick_for_black_holes = fallback_kick_for_black_holes
self.max_CPU_time = max_CPU_time
self.triple = bins[1]
self.triple.time = 0.0|units.yr
self.triple.relative_inclination = relative_inclination
self.triple.is_star = False #maybe not necessary?
self.triple.dynamical_instability = False
self.triple.number = number
self.triple.error_flag_secular = 0
self.triple.CPU_time = 0.0
self.triple.child2.delta_e_in = 0.0
self.triple.child2.max_delta_e_in = 0.0
self.dynamical_instability_at_initialisation = False
self.semisecular_regime_at_initialisation = False
self.mass_transfer_at_initialisation = False
self.CHE_at_initialisation = False
self.set_stopping_conditions(stop_at_mass_transfer, stop_at_init_mass_transfer,stop_at_outer_mass_transfer,
stop_at_stable_mass_transfer, stop_at_eccentric_stable_mass_transfer,
stop_at_unstable_mass_transfer, stop_at_eccentric_unstable_mass_transfer, stop_at_no_CHE,
stop_at_merger, stop_at_disintegrated, stop_at_inner_collision, stop_at_outer_collision,
stop_at_dynamical_instability, stop_at_semisecular_regime, stop_at_SN, stop_at_CPU_time)
self.initialize_stellar(stellar_code, stars)
self.initialize_secular(secular_code, stop_at_semisecular_regime,
stop_at_dynamical_instability)
self.check_RLOF()
if self.has_tertiary_donor() and (self.stop_at_outer_mass_transfer or self.stop_at_mass_transfer or self.stop_at_init_mass_transfer):
self.mass_transfer_at_initialisation = True
self.triple.bin_type = bin_type['rlof']
return
self.check_OLOF()
if self.stop_at_no_CHE and (not self.check_CHE()):
self.CHE_at_initialization = False
return
if (self.has_donor() or self.has_OLOF_donor()) and (self.stop_at_mass_transfer or self.stop_at_init_mass_transfer):
self.mass_transfer_at_initialisation = True
#assuming object is triple as is triple constructor
if self.is_binary(self.triple.child1):
bin = self.triple.child1
else:
bin = self.triple.child2
if bin.child1.is_OLOF_donor or bin.child2.is_OLOF_donor:
bin.bin_type = bin_type['olof']
return
elif bin.child1.is_donor and bin.child2.is_donor:
bin.bin_type = bin_type['contact']
return
else:
bin.bin_type = bin_type['rlof']
return
return
self.triple.kozai_type = self.get_kozai_type()
self.update_stellar_parameters()
self.update_time_derivative_of_radius()
self.update_previous_stellar_parameters()
def initialize_stellar(self, stellar_code, stars):
self.stellar_code = setup_stellar_code(stellar_code, stars)
self.channel_from_stellar = stellar_code.particles.new_channel_to(stars)
self.channel_to_stellar = stars.new_channel_to(stellar_code.particles)
self.copy_from_stellar()
self.initial_angular_frequency()
def initialize_secular(self, secular_code,
stop_at_semisecular_regime,
stop_at_dynamical_instability):
triple_set = self.triple.as_set()
self.secular_code = setup_secular_code(self.triple, secular_code, stop_at_semisecular_regime)
self.channel_from_secular = self.secular_code.triples.new_channel_to(triple_set)
self.channel_to_secular = triple_set.new_channel_to(self.secular_code.triples)
self.channel_to_secular.copy()
self.secular_code.check_for_dynamical_stability()
if stop_at_dynamical_instability == True and self.secular_code.triples[0].dynamical_instability == True:
self.dynamical_instability_at_initialisation = True
self.triple.dynamical_instability = True
self.set_bintype_to_dynamical_instability()
return
self.secular_code.check_for_semisecular_regime()
if stop_at_semisecular_regime == True and self.secular_code.triples[0].semisecular_regime == True:
self.semisecular_regime_at_initialisation = True
self.triple.semisecular_regime = True
return
def set_stopping_conditions(self, stop_at_mass_transfer,stop_at_init_mass_transfer,stop_at_outer_mass_transfer,
stop_at_stable_mass_transfer, stop_at_eccentric_stable_mass_transfer,
stop_at_unstable_mass_transfer, stop_at_eccentric_unstable_mass_transfer, stop_at_no_CHE,
stop_at_merger, stop_at_disintegrated, stop_at_inner_collision, stop_at_outer_collision,
stop_at_dynamical_instability, stop_at_semisecular_regime, stop_at_SN, stop_at_CPU_time):
if stop_at_disintegrated == False:
sys.exit('stop_at_disintegrated = False not possible yet. After the disintegration of the triple, further evolution can be done with stellar code directly. ')
if stop_at_outer_mass_transfer == False:
sys.exit('stop_at_outer_mass_transfer = False not possible yet. Methodology is as of yet non-existent.' )
if stop_at_outer_collision == False:
sys.exit('stop_at_outer_collision = False not possible. Non-hierarchical triples can not be simulated using the secular equations as used in TRES. Further evolution should be done by other means, e.g. one of the N-body codes implemented in AMUSE.' )
if stop_at_dynamical_instability == False:
sys.exit('stop_at_dynamical_instability = False not possible. Unstable triples can not be simulated using the secular equations as used in TRES. Further evolution should be done by other means, e.g. one of the N-body codes implemented in AMUSE.')
self.stop_at_mass_transfer = stop_at_mass_transfer
self.stop_at_init_mass_transfer = stop_at_init_mass_transfer
self.stop_at_outer_mass_transfer = stop_at_outer_mass_transfer
self.stop_at_stable_mass_transfer = stop_at_stable_mass_transfer
self.stop_at_eccentric_stable_mass_transfer = stop_at_eccentric_stable_mass_transfer
self.stop_at_unstable_mass_transfer = stop_at_unstable_mass_transfer
self.stop_at_eccentric_unstable_mass_transfer = stop_at_eccentric_unstable_mass_transfer
self.stop_at_no_CHE = stop_at_no_CHE
self.stop_at_merger = stop_at_merger
self.stop_at_disintegrated = stop_at_disintegrated
self.stop_at_inner_collision = stop_at_inner_collision
self.stop_at_outer_collision = stop_at_outer_collision
self.stop_at_dynamical_instability = stop_at_dynamical_instability
self.stop_at_semisecular_regime = stop_at_semisecular_regime
self.stop_at_SN = stop_at_SN
self.stop_at_CPU_time = stop_at_CPU_time
#-------
def copy_from_stellar(self):
# self.channel_from_stellar.copy()
self.channel_from_stellar.copy_attributes(["age", "mass", "core_mass", "radius", "core_radius", "convective_envelope_radius", "convective_envelope_mass", "stellar_type", "luminosity", "wind_mass_loss_rate", "temperature"])
if GET_GYRATION_RADIUS_FROM_STELLAR_CODE:
self.channel_from_stellar.copy_attributes(["gyration_radius"])
if GET_AMC_FROM_STELLAR_CODE:
self.channel_from_stellar.copy_attributes(["apsidal_motion_constant"])
#-------
def initial_angular_frequency(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
self.previous_time = self.triple.time
if stellar_system.is_star:
if stellar_system.stellar_type in stellar_types_planetary_objects:
stellar_system.spin_angular_frequency = 0.125 * break_up_angular_frequency(stellar_system)
else:
stellar_system.spin_angular_frequency = lang_spin_angular_frequency(stellar_system)
if self.include_CHE: #sets initial spin to corotation
stellar_system.spin_angular_frequency = corotating_spin_angular_frequency_binary(stellar_system.parent.semimajor_axis, self.get_mass(stellar_system.parent.child1), self.get_mass(stellar_system.parent.child2))
stellar_system.rotation_period = (2*np.pi/stellar_system.spin_angular_frequency)
else:
self.initial_angular_frequency(stellar_system.child1)
self.initial_angular_frequency(stellar_system.child2)
if self.include_CHE: #only needed when including CHE
self.channel_to_stellar.copy_attributes(['rotation_period'])
#-------
#-------
def refresh_memory(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
self.triple.time = self.previous_time
if stellar_system.is_star:
stellar_system.mass = stellar_system.previous_mass
stellar_system.radius = stellar_system.previous_radius
stellar_system.stellar_type = stellar_system.previous_stellar_type
stellar_system.moment_of_inertia_of_star = stellar_system.previous_moment_of_inertia_of_star
stellar_system.time_derivative_of_radius = stellar_system.previous_time_derivative_of_radius
stellar_system.spin_angular_frequency = stellar_system.previous_spin_angular_frequency
else:
self.refresh_memory(stellar_system.child1)
self.refresh_memory(stellar_system.child2)
stellar_system.mass = stellar_system.previous_mass
stellar_system.bin_type = stellar_system.previous_bin_type
if self.is_triple(stellar_system):
stellar_system.kozai_type = stellar_system.previous_kozai_type
def update_previous_stellar_parameters(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
self.previous_time = self.triple.time
if stellar_system.is_star:
stellar_system.previous_mass = self.get_mass(stellar_system)
stellar_system.previous_radius = stellar_system.radius
stellar_system.previous_stellar_type = stellar_system.stellar_type
stellar_system.previous_moment_of_inertia_of_star = stellar_system.moment_of_inertia_of_star
stellar_system.previous_spin_angular_frequency = stellar_system.spin_angular_frequency
if self.triple.time == quantities.zero: #initialization
stellar_system.previous_time_derivative_of_radius = 0.0 | units.RSun/units.yr
else:
stellar_system.previous_time_derivative_of_radius = stellar_system.time_derivative_of_radius
else:
self.update_previous_stellar_parameters(stellar_system.child1)
self.update_previous_stellar_parameters(stellar_system.child2)
stellar_system.previous_mass = self.get_mass(stellar_system)
stellar_system.previous_bin_type = stellar_system.bin_type
if self.is_triple(stellar_system):
stellar_system.previous_kozai_type = stellar_system.kozai_type
#-------
#-------
def update_time_derivative_of_radius(self, stellar_system = None):
#update time_derivative_of_radius for effect of wind on spin
#radius change due to stellar evolution, not mass transfer
if stellar_system == None:
stellar_system = self.triple
time_step = self.triple.time - self.previous_time
if self.triple.time == quantities.zero:
#initialization
self.triple.child2.child1.time_derivative_of_radius = 0.0 | units.RSun/units.yr
self.triple.child2.child2.time_derivative_of_radius = 0.0 | units.RSun/units.yr
self.triple.child1.time_derivative_of_radius = 0.0 | units.RSun/units.yr
else:
if stellar_system.is_star:
stellar_system.time_derivative_of_radius = (stellar_system.radius - stellar_system.previous_radius)/time_step
else:
self.update_time_derivative_of_radius(stellar_system.child1)
self.update_time_derivative_of_radius(stellar_system.child2)
#-------
#-------
def update_time_derivative_of_moment_of_inertia(self, stellar_system = None):
#update time_derivative_of_radius for effect of changing Jspin
if stellar_system == None:
stellar_system = self.triple
time_step = self.triple.time - self.previous_time
if self.triple.time == quantities.zero:
#initialization
self.triple.child2.child1.time_derivative_of_moment_of_inertia = 0.0 | units.MSun*units.RSun**2/units.yr
self.triple.child2.child2.time_derivative_of_moment_of_inertia = 0.0 | units.MSun*units.RSun**2/units.yr
self.triple.child1.time_derivative_of_moment_of_inertia = 0.0 | units.RSun/units.yr
else:
if stellar_system.is_star:
stellar_system.time_derivative_of_moment_of_inertia = (stellar_system.moment_of_inertia_of_star - stellar_system.previous_moment_of_inertia_of_star)/time_step
else:
self.update_time_derivative_of_moment_of_inertia(stellar_system.child1)
self.update_time_derivative_of_moment_of_inertia(stellar_system.child2)
#-------
#-------
def update_stellar_parameters(self, stellar_system = None):
# for the convective envelope mass:
# the prescription of Hurley, Pols & Tout 2000 is implemented in SeBa, however note that the prescription in BSE is different
# for the convective envelope radius:
# the prescription of Hurley, Tout & Pols 2002 is implemented in SeBa, however note that the prescription in BSE is different
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
if not GET_GYRATION_RADIUS_FROM_STELLAR_CODE:
stellar_system.gyration_radius = set_gyration_radius(stellar_system.stellar_type, stellar_system.mass)
if not GET_AMC_FROM_STELLAR_CODE:
stellar_system.apsidal_motion_constant = self.apsidal_motion_constant(stellar_system)
if stellar_system.core_radius > stellar_system.radius:
#can happen very late on the agb before WD formation
stellar_system.core_radius = stellar_system.radius
stellar_system.moment_of_inertia_of_star = self.moment_of_inertia(stellar_system)
if stellar_system.convective_envelope_radius < 0|units.RSun:
sys.exit('convective_envelope_radius < 0')
if stellar_system.convective_envelope_radius == 0|units.RSun:
stellar_system.convective_envelope_mass = 1.e-10 |units.MSun
stellar_system.convective_envelope_radius = 1.e-10 |units.RSun
#When the GW inspiral time is shorter than the inner orbit, the numerical solver crashes
#This is only possible for BH & NS as other stars would fill their RL earlier
#To avoid this articially increase stellar radii of BH/NS in secular code
#Does not affect any other processes
if stellar_system.stellar_type in stellar_types_SN_remnants:
stellar_system.radius = stellar_system.radius*10
else:
self.update_stellar_parameters(stellar_system.child1)
self.update_stellar_parameters(stellar_system.child2)
if self.is_triple(stellar_system):
stellar_system.kozai_type = self.get_kozai_type()
#-------
#-------
# useful functions general
#whether or not a stellar system consists of just two stars
def is_binary(self, stellar_system=None):
if stellar_system == None:
stellar_system = self.triple
if not stellar_system.is_star and stellar_system.child1.is_star and stellar_system.child2.is_star:
return True
else:
return False
def is_triple(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if not stellar_system.is_star:
if stellar_system.child1.is_star and self.is_binary(stellar_system.child2):
return True
elif stellar_system.child2.is_star and self.is_binary(stellar_system.child1):
return True
return False
def has_donor(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
if stellar_system.is_donor:
return True
else:
if self.has_donor(stellar_system.child1) or self.has_donor(stellar_system.child2):
return True
return False
def has_OLOF_donor(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
if stellar_system.is_OLOF_donor:
return True
else:
if self.has_OLOF_donor(stellar_system.child1) or self.has_OLOF_donor(stellar_system.child2):
return True
return False
def has_contact_system(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return False
elif self.is_binary(stellar_system):
if stellar_system.child1.is_donor and stellar_system.child2.is_donor:
return True
else:
if self.has_contact_system(stellar_system.child1):
return True
if self.has_contact_system(stellar_system.child2):
return True
return False
# if a merger is currently taking place, not if a merger has happened in the past
def has_merger(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return False
else:
if self.has_merger(stellar_system.child1):
return True
if self.has_merger(stellar_system.child2):
return True
if stellar_system.bin_type == bin_type['merger']:
return True
return False
# if a disruption is currently taking place, not if a disruption has happened in the past
def has_disintegrated(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return False
else:
if self.has_disintegrated(stellar_system.child1):
return True
if self.has_disintegrated(stellar_system.child2):
return True
if stellar_system.bin_type == bin_type['disintegrated']:
return True
return False
# if a dynamical instability is currently taking place, not if an instability has happened in the past
def has_dynamical_instability(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return False
else:
if self.has_dynamical_instability(stellar_system.child1):
return True
if self.has_dynamical_instability(stellar_system.child2):
return True
if stellar_system.bin_type == bin_type['dyn_inst']:
return True
return False
def set_bintype_to_dynamical_instability(self, stellar_system = None):
if self.triple.dynamical_instability:
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star == False:
stellar_system.bin_type = bin_type['dyn_inst']
self.set_bintype_to_dynamical_instability(stellar_system.child1)
self.set_bintype_to_dynamical_instability(stellar_system.child2)
#doesn't work well, as it uses bin_types that are set later -> use has_tertiary_donor
# if a mass transfer in the outer binary of the triple is currently taking place, not if a mass transfer has happened in the past
# def has_outer_mass_transfer(self, stellar_system = None):
# if stellar_system == None:
# stellar_system = self.triple
#
# if stellar_system.is_star:
# return False
# elif self.is_binary(stellar_system):
# return False
# else:
# if self.has_outer_mass_transfer(stellar_system.child1):
# return True
# if self.has_outer_mass_transfer(stellar_system.child2):
# return True
# if stellar_system.bin_type != bin_type['unknown'] and stellar_system.bin_type != bin_type['detached']:
# return True
#
# return False
# if a mass transfer in the outer binary of the triple is currently taking place, not if a mass transfer has happened in the past
def has_tertiary_donor(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return False
elif self.is_binary(stellar_system):
return False
else:
if self.has_tertiary_donor(stellar_system.child1):
return True
if self.has_tertiary_donor(stellar_system.child2):
return True
if stellar_system.child1.is_star and stellar_system.child1.is_donor and not stellar_system.child2.is_star:
return True
if stellar_system.child2.is_star and stellar_system.child2.is_donor and not stellar_system.child1.is_star:
return True
return False
def contains_SN_remnant(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
if stellar_system.stellar_type in stellar_types_SN_remnants:
return True
else:
if self.contains_SN_remnant(stellar_system.child1) or self.contains_SN_remnant(stellar_system.child2):
return True
return False
def has_stellar_type_changed_into_SN_remnant(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
if stellar_system.stellar_type != stellar_system.previous_stellar_type and stellar_system.stellar_type in stellar_types_SN_remnants:
return True
else:
if self.has_stellar_type_changed_into_SN_remnant(stellar_system.child1) or self.has_stellar_type_changed_into_SN_remnant(stellar_system.child2):
return True
return False
def has_stellar_type_changed(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
if stellar_system.stellar_type != stellar_system.previous_stellar_type:
return True
else:
if self.has_stellar_type_changed(stellar_system.child1) or self.has_stellar_type_changed(stellar_system.child2):
return True
return False
def has_kozai_type_changed(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if self.is_triple(stellar_system):
if stellar_system.kozai_type != stellar_system.previous_kozai_type:
return True
else: #binaries and single stars do not have a kozai timescale
return False
#obsolete?
# def is_system_stable(self, stellar_system = None):
# if stellar_system == None:
# stellar_system = self.triple
#
# if stellar_system.is_star:
# return True
# elif self.is_binary(stellar_system):
# return stellar_system.is_mt_stable
# else:
# if stellar_system.is_mt_stable and self.is_system_stable(stellar_system.child1) and self.is_system_stable(stellar_system.child2):
# return True
#
# return False
def get_mass(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return stellar_system.mass
else:
M1 = self.get_mass(stellar_system.child1)
M2 = self.get_mass(stellar_system.child2)
return M1 + M2
def get_size(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return stellar_system.radius
else:
return stellar_system.semimajor_axis
#-------
#-------
# useful functions general
def orbital_period(self, bs):
if not bs.is_star:
Porb = 2*np.pi * np.sqrt(bs.semimajor_axis**3/constants.G / self.get_mass(bs))
return Porb
else:
sys.exit('orbital_period: single star does not have a period')
def orbital_angular_momentum(self, bs):
if not bs.is_star:
M = self.get_mass(bs.child1)
m = self.get_mass(bs.child2)
a = bs.semimajor_axis
e = bs.eccentricity
J = M*m * np.sqrt(constants.G*a*(1-e**2)/(M+m))
if REPORT_BINARY_EVOLUTION:
print('Jorb:', M, m, a, e, J)
return J
else:
sys.exit('orbital_angular_momentum: single star does not have an orbit')
def spin_angular_momentum(self, ss):
if ss.is_star:
return ss.moment_of_inertia_of_star * ss.spin_angular_frequency
else:
sys.exit('spin_angular_momentum: structure stellar system unknown')
def apsidal_motion_constant(self, star):
if star.stellar_type in [13]|units.stellar_type: #ns
#based on Brooke & Olle 1955, for n=1 polytrope
return 0.260
elif star.stellar_type in [13, 14]|units.stellar_type: #bh
# Hamers et al. 2013
return 0.
elif star.stellar_type in [1,7,10,11,12]|units.stellar_type:#ms, he-ms, wd
#based on Brooke & Olle 1955, for n=3 polytrope
return 0.0144
elif star.stellar_type in [0,2,3,4,5,6,8,9,17]|units.stellar_type:#low-mass ms, hg, gb, cheb, agb, he-g, pre-ms
#based on Brooke & Olle 1955, for n=3 polytrope
# return 0.143
#based on Claret & Gimenez 1992, 96, 225 the value should be smaller, try:
return 0.05
elif star.stellar_type in [18]|units.stellar_type:#planet
#based on Brooke & Olle 1955, for n=3 polytrope
return 0.0144
elif star.stellar_type in [19]|units.stellar_type:#bd
#based on Brooke & Olle 1955, for n=3 polytrope
return 0.0144
else:
print(star.stellar_type)
sys.exit('apsidal motion constant: stellar_type unknown')
def moment_of_inertia(self, star):
if star.is_star:
if GET_GYRATION_RADIUS_FROM_STELLAR_CODE:
I = star.gyration_radius**2 * (star.mass)*star.radius**2
else:
k2 = 0.1
k3 = 0.21
if star.stellar_type in stellar_types_remnants:
I = k3*(star.mass)*star.radius**2
else:
I = k2*(star.mass - star.core_mass)*star.radius**2 + k3*star.core_mass*star.core_radius**2
return I
else:
sys.exit('moment_of_inertia: structure stellar system unknown')
def octupole_parameter(self):
if self.is_triple():
if self.triple.child1.is_star:
star = self.triple.child1
bin = self.triple.child2
else:
star = self.triple.child2
bin = self.triple.child1
return (self.get_mass(bin.child1)-self.get_mass(bin.child2))/self.get_mass(bin) * bin.semimajor_axis/self.triple.semimajor_axis * self.triple.eccentricity/(1-self.triple.eccentricity**2)
else:
print('Octupole parameter needs triple system')
return np.nan
def kozai_timescale(self):
if self.is_triple():
alpha_kozai = 1.
if self.triple.child1.is_star:
star = self.triple.child1
bin = self.triple.child2
else:
star = self.triple.child2
bin = self.triple.child1
P_in = self.orbital_period(bin) #period inner binary
P_out = self.orbital_period(self.triple)#period outer binary
return alpha_kozai * P_out**2 / P_in * (self.get_mass(self.triple) / self.get_mass(star)) * (1-self.triple.eccentricity**2)**1.5
else:
print('Kozai timescale needs triple system')
return np.nan
def get_kozai_type(self):
if self.is_triple():
if self.secular_code.parameters.ignore_tertiary == True:
return False
t_kozai = self.kozai_timescale()
if t_kozai >kozai_type_factor * self.tend:
return False
t_ev = self.get_min_stellar_evolution_timescale_of_system()
if t_kozai < kozai_type_factor * t_ev:
return True
else:
return False
else:
sys.exit('Kozai type needs triple system')
def get_min_stellar_evolution_timescale_of_system(self, stellar_system = None):
if stellar_system == None:
stellar_system = self.triple
if stellar_system.is_star:
return stellar_evolution_timescale(stellar_system)
else:
t1 = self.get_min_stellar_evolution_timescale_of_system(stellar_system.child1)
t2 = self.get_min_stellar_evolution_timescale_of_system(stellar_system.child2)
return max(t1, t2)
def check_RLOF(self):
if self.triple.is_star:
return
elif self.is_binary():
Rl1 = roche_radius(self, self.child1)
Rl2 = roche_radius(self, self.child2)
if REPORT_TRIPLE_EVOLUTION:
print('Roche lobe radii:', Rl1, Rl2)
print('Stellar radii:', self.triple.child1.radius, self.triple.child2.radius)
self.triple.child1.is_donor = False
self.triple.child2.is_donor = False
if self.triple.child1.radius >= Rl1 - (1.0 * small_numerical_error|units.RSun):
self.triple.child1.is_donor = True
if self.triple.child2.radius >= Rl2 - (1.0 * small_numerical_error|units.RSun):
self.triple.child2.is_donor = True
elif self.is_triple() and self.secular_code.parameters.ignore_tertiary == True:
# for disrupted binary
if self.triple.child1.is_star:
bin = self.triple.child2
else:
bin = self.triple.child1
Rl1 = roche_radius(bin, bin.child1, self)
Rl2 = roche_radius(bin, bin.child2, self)
if REPORT_TRIPLE_EVOLUTION:
print('Roche lobe radii:', Rl1, Rl2)
print('Stellar radii:', bin.child1.radius, bin.child2.radius)
bin.child1.is_donor = False
bin.child2.is_donor = False
if bin.child1.radius >= Rl1 - (1.0 * small_numerical_error|units.RSun):
bin.child1.is_donor = True
if bin.child2.radius >= Rl2 - (1.0 * small_numerical_error|units.RSun):
bin.child2.is_donor = True
elif self.is_triple():
if self.triple.child1.is_star:
star = self.triple.child1
bin = self.triple.child2
else:
star = self.triple.child2
bin = self.triple.child1
#assumping secular code always returns inner binary first
Rl1, Rl2, Rl3 = self.secular_code.give_roche_radii(self.triple)
if REPORT_TRIPLE_EVOLUTION:
print('Roche lobe radii:', Rl1, Rl2, Rl3)
print('Stellar radii:', bin.child1.radius, bin.child2.radius, star.radius)
print('binary Roche lobe radii:', roche_radius(bin, bin.child1, self), roche_radius(bin, bin.child2, self), roche_radius(self.triple, star, self))
print('eccentric binary Roche lobe radii:', roche_radius(bin, bin.child1, self)* (1-bin.eccentricity), roche_radius(bin, bin.child2, self)* (1-bin.eccentricity), roche_radius(self.triple, star, self)*(1-self.triple.eccentricity))
print('Masses:', bin.child1.mass, bin.child2.mass, star.mass)
print('Semi:', bin.semimajor_axis, self.triple.semimajor_axis)
print('Ecc:', bin.eccentricity, self.triple.eccentricity)
print('Stellar type:', bin.child1.stellar_type, bin.child2.stellar_type, star.stellar_type)
print('Spin:', bin.child1.spin_angular_frequency, bin.child2.spin_angular_frequency, star.spin_angular_frequency)
bin.child1.is_donor = False
bin.child2.is_donor = False
star.is_donor = False
if bin.child1.radius >= Rl1 - (1.0 * small_numerical_error|units.RSun):
bin.child1.is_donor = True
if bin.child2.radius >= Rl2 - (1.0 * small_numerical_error|units.RSun):
bin.child2.is_donor = True
if star.radius >= Rl3 - (1.0 * small_numerical_error|units.RSun):
star.is_donor = True
else:
sys.exit('check_RLOF: structure stellar system unknown')
def check_OLOF(self):
if self.triple.is_star:
return
elif self.is_binary():
Rl2_1 = L2_radius(self, self.child1)
Rl2_2 = L2_radius(self, self.child2)
if REPORT_TRIPLE_EVOLUTION:
print('L2 lobe radii:', Rl2_1, Rl2_2)
print('Stellar radii:', self.triple.child1.radius, self.triple.child2.radius)
self.triple.child1.is_OLOF_donor = False
self.triple.child2.is_OLOF_donor = False
if self.triple.child1.radius >= Rl2_1 - (1.0 * small_numerical_error|units.RSun):
self.triple.child1.is_OLOF_donor = True
if self.triple.child2.radius >= Rl2_2 - (1.0 * small_numerical_error|units.RSun):
self.triple.child2.is_OLOF_donor = True
elif self.is_triple():
# for disrupted binary
if self.triple.child1.is_star:
star = self.triple.child1
bin = self.triple.child2
else:
star = self.triple.child2
bin = self.triple.child1
Rl2_1 = L2_radius(bin, bin.child1, self)
Rl2_2 = L2_radius(bin, bin.child2, self)
bin.child1.is_OLOF_donor = False
bin.child2.is_OLOF_donor = False
if bin.child1.radius >= Rl2_1 - (1.0 * small_numerical_error|units.RSun):
bin.child1.is_OLOF_donor = True
if bin.child2.radius >= Rl2_2 - (1.0 * small_numerical_error|units.RSun):
bin.child2.is_OLOF_donor = True
if REPORT_TRIPLE_EVOLUTION:
print('L2 lobe radii:', Rl2_1, Rl2_2)
print('Stellar radii:', bin.child1.radius, bin.child2.radius)
print('Masses:', bin.child1.mass, bin.child2.mass, star.mass)
print('Semi:', bin.semimajor_axis, self.triple.semimajor_axis)
print('Ecc:', bin.eccentricity, self.triple.eccentricity)
print('Stellar type:', bin.child1.stellar_type, bin.child2.stellar_type, star.stellar_type)
print('Spin:', bin.child1.spin_angular_frequency, bin.child2.spin_angular_frequency, star.spin_angular_frequency)
else:
print('check_OLOF: structure stellar system unknown')
sys.exit('check_OLOF: structure stellar system unknown')
def check_CHE(self): #future option: potentially use: che_flag in SeBa
#returns true when one or both of the inner binary components are chemically homogeneously evolving
#if !include_CHE, then return False
#problematic for quadruples - what if one binary is CHE, and other is not
metallicity = self.stellar_code.parameters.metallicity
if self.triple.is_star:
return False
elif self.is_binary():
bin = self.triple
if self.include_CHE and ((bin.child1.spin_angular_frequency >= criticial_angular_frequency_CHE(bin.child1.mass, metallicity) and bin.child1.stellar_type <= 1|units.stellar_type) or
(bin.child2.spin_angular_frequency >= criticial_angular_frequency_CHE(bin.child2.mass, metallicity) and bin.child2.stellar_type <= 1|units.stellar_type)):
return True
else:
return False
elif self.is_triple():
if self.triple.child1.is_star:
bin = self.triple.child2
else:
bin = self.triple.child1
if self.include_CHE and ((bin.child1.spin_angular_frequency >= criticial_angular_frequency_CHE(bin.child1.mass, metallicity) and bin.child1.stellar_type <= 1|units.stellar_type) or
(bin.child2.spin_angular_frequency >= criticial_angular_frequency_CHE(bin.child2.mass, metallicity) and bin.child2.stellar_type <= 1|units.stellar_type)):
return True
else:
return False
else:
print('check_CHE: structure stellar system unknown')
sys.exit('check_CHE: structure stellar system unknown')
#
# def determine_partial_timestep_stable_mass_transfer(self, stellar_system = None):
# if stellar_system == None:
# stellar_system = self.triple
#
# if stellar_system.is_star:
# return np.inf |units.Myr
# else:
# dt1 = self.determine_partial_timestep_stable_mass_transfer(stellar_system.child1)
# dt2 = self.determine_partial_timestep_stable_mass_transfer(stellar_system.child2)
# dt = stellar_system.part_dt_mt
# return min(dt, min(dt1, dt2))
#-------
#-------
# useful functions for printing
def print_star(self, star):
if star.is_star:
print('star:')
print(star.age, )
print(star.stellar_type, )
print(star.mass, )
print(star.radius, )
print(star.core_mass, )
print(star.core_radius,)
print(star.convective_envelope_mass,)
print(star.convective_envelope_radius,)
print(star.luminosity,)
print(star.temperature,)
print(star.wind_mass_loss_rate,)
print(star.spin_angular_frequency,)
print(star.is_donor)
print('\t')
else:
sys.exit('print_star needs a star')
def print_binary(self, binary):
if not binary.is_star:
print(self.get_mass(binary), )
print(binary.semimajor_axis,)
print(binary.eccentricity, )
print(binary.argument_of_pericenter, )