00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "hdyn.h"
00027 #include <star/dstar_to_kira.h>
00028 #include <star/single_star.h>
00029
00030
00031
00032
00033
00034 local hdyn* apply_tidal_dissipation(hdyn* bi, hdyn* bj, kepler k)
00035 {
00036
00037
00038
00039
00040
00041 print_encounter_elements(bi, bj, "Tidal encounter");
00042
00043
00044
00045
00046
00047 cerr << endl << "**** tidal dissipation suppressed ****"
00048 << endl << endl;
00049 return NULL;
00050
00051
00052
00053
00054
00055 real t_peri = k.return_to_periastron();
00056 if (t_peri > bi->get_time())
00057 warning("apply_tidal_dissipation: stars not past periastron");
00058
00059 real m_tot = bi->get_mass() + bj->get_mass();
00060 vector r_rel = k.get_rel_pos();
00061 vector v_rel = k.get_rel_vel();
00062
00063 real de_diss = 0;
00064 for_all_daughters(hdyn, bi->get_parent(), bb) {
00065 if (bb->get_radius() > 0) {
00066 real dde_diss = 0;
00067 real rr = bb->get_radius()/k.get_separation();
00068 real eta_pt = sqrt(bb->get_mass() / m_tot) * pow(rr, -1.5);
00069 stellar_type type = bb->get_starbase()->get_element_type();
00070 dde_diss = tf2_energy_diss(eta_pt, type)
00071 + pow(rr, 2) * tf3_energy_diss(eta_pt, type);
00072
00073 cerr << endl;
00074 PRC(type); PRC(bb->get_radius()), PRL(k.get_separation());
00075 PRC(rr); PRC(eta_pt); PRL(dde_diss);
00076
00077 de_diss += pow(bb->get_binary_sister()->get_mass(), 2)
00078 * pow(rr, 6) * dde_diss / bb->get_radius();
00079 }
00080 }
00081
00082 real m_red = bi->get_mass()*bj->get_mass() / m_tot;
00083 real kinetic = 0.5*m_red*square(v_rel);
00084 real potential = -m_red*m_tot/abs(r_rel);
00085
00086 cerr << endl << "at periastron: U = " << potential
00087 << " K = " << kinetic
00088 << " total = " << potential + kinetic << endl;
00089 cerr << "de_diss = " << de_diss << endl;
00090
00091
00092
00093
00094 real new_E = k.get_energy() - de_diss;
00095 real J = k.get_angular_momentum();
00096
00097 real new_sma = -0.5*m_tot/new_E;
00098
00099
00100 real new_ecc_2 = 1 - J*J/(m_tot*new_sma);
00101
00102 if (new_ecc_2 < 0) {
00103 cerr << "immediate merger with J^2 > Ma" << endl;
00104 return bj;
00105 }
00106
00107 cerr << "old orbit parameters: sma = " << k.get_semi_major_axis()
00108 << " ecc = " << k.get_eccentricity()
00109 << " periastron = " << k.get_periastron() << endl;
00110
00111 real new_ecc = sqrt(new_ecc_2);
00112 k.set_semi_major_axis(abs(new_sma));
00113 k.set_eccentricity(new_ecc);
00114 k.initialize_from_shape_and_phase();
00115
00116 cerr << "expected orbit parameters: sma = " << abs(new_sma)
00117 << " ecc = " << new_ecc << endl;
00118 cerr << "new orbit parameters: sma = " << k.get_semi_major_axis()
00119 << " ecc = " << k.get_eccentricity()
00120 << " periastron = " << k.get_periastron() << endl;
00121
00122 real old_phi = bi->get_mass()*bi->get_pot()
00123 + bj->get_mass()*bj->get_pot()
00124 - 2*potential;
00125
00126 k.transform_to_time(bi->get_time());
00127
00128 r_rel = k.get_rel_pos();
00129 v_rel = k.get_rel_vel();
00130
00131 vector r_com = (bi->get_mass()*bi->get_pos()
00132 + bj->get_mass()*bj->get_pos()) / m_tot;
00133 vector v_com = (bi->get_mass()*bi->get_vel()
00134 + bj->get_mass()*bj->get_vel()) / m_tot;
00135
00136
00137
00138 bi->set_pos(r_com - bj->get_mass()*r_rel/m_tot);
00139 bj->set_pos(r_com + bi->get_mass()*r_rel/m_tot);
00140 bi->set_vel(v_com - bj->get_mass()*v_rel/m_tot);
00141 bj->set_vel(v_com + bi->get_mass()*v_rel/m_tot);
00142
00143
00144
00145 hdynptr list[2] = {bi, bj};
00146 bool _false = false;
00147 calculate_acc_and_jerk_for_list(bi->get_root(), list, 2,
00148 bi->get_time(),
00149 _false, _false, _false, _false);
00150
00151 bi->get_kira_counters()->de_total -= de_diss;
00152 bi->get_kira_counters()->de_tidal_diss -= de_diss;
00153
00154 potential = -m_red*m_tot/abs(k.get_separation());
00155 real new_phi = bi->get_mass()*bi->get_pot()
00156 + bj->get_mass()*bj->get_pot()
00157 - 2*potential;
00158
00159 PRC(old_phi); PRC(new_phi); PRL(potential);
00160 PRL(new_phi-old_phi);
00161
00162 return NULL;
00163 }
00164
00165
00166
00167
00168 local void print_perioapo_clustron(hdyn* bi) {
00169
00170 hdyn *bj = bi->get_root();
00171
00172 real d2cd_2 = -1;
00173 real d2cd_1 = -1;
00174 real d2cd = -1;
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 #if 0
00189 cerr << "\nIn print_close_encounter with bi = " << bi->format_label()
00190 << " at time " << bi->get_system_time() << endl;
00191 cerr << " "; print_coll(bi,2);
00192 cerr << " "; print_nn(bi,2);
00193 cerr << " (bj = " << bj->format_label();
00194 cerr << ": coll = "; print_coll(bj);
00195 cerr << ", nn = "; print_nn(bj); cerr << ")" << endl;
00196
00197 #endif
00198
00199
00200
00201 d2cd_2 = getrq(bi->get_log_story(), "d2cd_1");
00202 d2cd_1 = getrq(bi->get_log_story(), "d2cd");
00203 d2cd = square(bi->get_pos() - bj->get_pos());
00204
00205
00206
00207 if (d2cd_2 > 0) {
00208
00209 if (d2cd_2 > d2cd_1 && d2cd >= d2cd_1) {
00210
00211 int pcp_cntr = 0;
00212 real E = get_total_energy(bi, bj);
00213
00214 if (E > 0) {
00215
00216
00217
00218 print_encounter_elements(bi, bj, "Perioclustron", false);
00219
00220 }
00221 else {
00222 if (find_qmatch(bi->get_log_story(), "pcp_cntr"))
00223 pcp_cntr = getiq(bi->get_log_story(), "pcp_cntr");
00224
00225 pcp_cntr++;
00226
00227 if (pcp_cntr == 1)
00228 print_encounter_elements(bi, bj, "First periclustron", false);
00229 else
00230 print_encounter_elements(bi, bj, "Periclustron", false);
00231 }
00232
00233
00234
00235
00236 putiq(bi->get_log_story(), "pcp_cntr", pcp_cntr);
00237 putrq(bi->get_log_story(), "pcp_time", bi->get_time());
00238
00239 }
00240 else if (d2cd_2 < d2cd_1 && d2cd <= d2cd_1) {
00241
00242 int pca_cntr = 0;
00243 real E = get_total_energy(bi, bj);
00244
00245 if (E > 0) {
00246
00247
00248
00249 print_encounter_elements(bi, bj, "Apoclustron", false);
00250
00251 }
00252 else {
00253 if (find_qmatch(bi->get_log_story(), "pca_cntr"))
00254 pca_cntr = getiq(bi->get_log_story(), "pca_cntr");
00255
00256 pca_cntr++;
00257
00258 if (pca_cntr == 1)
00259 print_encounter_elements(bi, bj, "First apoclustron", false);
00260 else
00261 print_encounter_elements(bi, bj, "Apoclustron", false);
00262 }
00263
00264
00265
00266
00267 putiq(bi->get_log_story(), "pca_cntr", pca_cntr);
00268 putrq(bi->get_log_story(), "pca_time", bi->get_time());
00269 }
00270 else {
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 }
00283
00284 }
00285
00286 putrq(bi->get_log_story(), "d2cd_1", d2cd_1);
00287 putrq(bi->get_log_story(), "d2cd", d2cd);
00288
00289
00290 #if 0
00291 cerr << "\nAt end of print_close_encounter at time "
00292 << bi->get_system_time() << endl;
00293 cerr << "bi = " << bi->format_label();
00294 #endif
00295
00296 }
00297
00298 hdyn* hdyn::check_periapo_node() {
00299
00300
00301
00302
00303 print_perioapo_clustron(this);
00304 }
00305
00306
00307 hdyn* hdyn::check_merge_node()
00308 {
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 if (use_dstar) {
00321 if (is_low_level_node()) {
00322 if (binary_is_merged(this)) {
00323 cerr << "check_merge_node: merging a binary with logs:"
00324 << endl;
00325 print_log_story(cerr);
00326 cerr << "and\n";
00327 get_binary_sister()->print_log_story(cerr);
00328 return get_binary_sister();
00329 }
00330 }
00331 }
00332
00333
00334
00335
00336
00337
00338 if (stellar_capture_criterion_sq <= 0) return NULL;
00339
00340 hdyn *coll = get_coll();
00341
00342 if (is_leaf() && coll != NULL && coll->is_leaf() && (kep == NULL)) {
00343
00344
00345
00346 if (!coll->is_valid())
00347 warning("check_merge_node: invalid coll pointer");
00348
00349 real sum_of_radii_sq = pow(get_sum_of_radii(this, coll), 2);
00350
00351
00352
00353
00354 if (get_d_coll_sq() <= (stellar_capture_criterion_sq-1)
00355 * sum_of_radii_sq) {
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 real sep_sq = get_d_coll_sq();
00391 kepler k;
00392
00393 if (get_parent() == coll->get_parent()) {
00394 coll->synchronize_node();
00395 initialize_kepler_from_dyn_pair(k, this, coll, false);
00396 sep_sq = min(sep_sq, k.get_periastron() * k.get_periastron()
00397 - sum_of_radii_sq);
00398 }
00399
00400
00401
00402 if (sep_sq <= (stellar_merger_criterion_sq-1)
00403 * sum_of_radii_sq)
00404 return coll;
00405
00406
00407
00408 if (get_parent() != coll->get_parent()) return NULL;
00409
00410 if (find_qmatch(get_log_story(), "tidal_dissipation")) {
00411 rmq(get_log_story(), "tidal_dissipation");
00412 return apply_tidal_dissipation(this, coll, k);
00413 }
00414
00415 real t_peri = k.get_time_of_periastron_passage();
00416 if (time < t_peri && time + timestep >= t_peri)
00417 putiq(get_log_story(), "tidal_dissipation", 1);
00418
00419 }
00420 }
00421
00422 return NULL;
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433 local void correct_nn_pointers(hdyn * b, hdyn ** list, int n, hdyn * cm)
00434 {
00435 for_all_nodes(hdyn, b, bb) {
00436 for (int i = 0; i < n; i++) {
00437 if (bb->get_nn() == list[i]) bb->set_nn(cm);
00438 if (bb->get_coll() == list[i]) bb->set_coll(cm);
00439 }
00440 }
00441 }
00442
00443 void hdyn::merge_logs_after_collision(hdyn *bi, hdyn* bj) {
00444
00445
00446 putrq(get_log_story(), "last_merger_time", bi->get_time());
00447
00448 if(find_qmatch(bi->get_log_story(), "black_hole"))
00449 putiq(get_log_story(), "black_hole",
00450 getiq(bi->get_log_story(), "black_hole"));
00451 else if(find_qmatch(bj->get_log_story(), "black_hole"))
00452 putiq(get_log_story(), "black_hole",
00453 getiq(bj->get_log_story(), "black_hole"));
00454 }
00455
00456
00457 #define CONSTANT_DENSITY 1 // Should be a parameter...
00458 #define MASS_LOSS 0.0
00459 #define KICK_VELOCITY 0.0
00460
00461 hdyn* hdyn::merge_nodes(hdyn * bcoll,
00462 int full_dump)
00463 {
00464
00465
00466
00467
00468
00469
00470 if (diag->tree) {
00471 if (diag->tree_level > 0) {
00472 cerr << endl << "merge_nodes: merging leaves ";
00473 pretty_print_node(cerr);
00474 cerr << " and ";
00475 bcoll->pretty_print_node(cerr);
00476
00477 int p = cerr.precision(HIGH_PRECISION);
00478 cerr << " at time " << system_time << endl;
00479 cerr.precision(p);
00480
00481 if (diag->tree_level > 1) {
00482 cerr << endl;
00483 pp3(this, cerr);
00484 pp3(bcoll, cerr);
00485 if (diag->tree_level > 2) {
00486 cerr << endl;
00487 put_node(cerr, *this, options->print_xreal);
00488 put_node(cerr, *bcoll, options->print_xreal);
00489 }
00490 cerr << endl;
00491 }
00492 }
00493 }
00494
00495 bool is_pert = (get_kepler() == NULL);
00496 real sum_of_radii_sq = pow(get_sum_of_radii(this, bcoll), 2);
00497
00498
00499
00500
00501
00502
00503
00504
00505 cerr << "merge_nodes: calling synchronize_tree..." << flush;
00506 PRL(cpu_time());
00507 kira_synchronize_tree(get_root());
00508 cerr << "back" << endl << flush;
00509 PRL(cpu_time());
00510
00511
00512
00513
00514
00515
00516
00517 if (get_kepler()) {
00518 rmkepler();
00519 cerr << "deleted kepler for " << format_label() << endl;
00520 } else if (get_binary_sister() != bcoll) {
00521 if (bcoll->get_kepler()) bcoll->rmkepler();
00522 cerr << "deleted kepler for " << bcoll->format_label() << endl;
00523 }
00524
00525 if (is_top_level_node() || get_binary_sister() != bcoll) {
00526
00527
00528
00529
00530
00531
00532 cerr << "creating CM node" << endl;
00533
00534 hdyn* ancestor = common_ancestor(this, bcoll);
00535
00536 if (ancestor->is_root()) {
00537
00538
00539
00540
00541
00542
00543 bool decombine = !is_top_level_node()
00544 && !bcoll->is_top_level_node();
00545
00546 cerr << "merge_nodes: call combine_top_level_nodes" << endl;
00547
00548
00549
00550
00551
00552
00553 combine_top_level_nodes(get_top_level_node(),
00554 bcoll->get_top_level_node(),
00555 full_dump);
00556
00557
00558
00559 if (get_binary_sister() != bcoll) {
00560
00561 if (full_dump)
00562 put_node(cout, *(get_top_level_node()), false, 3);
00563
00564 move_node(bcoll, this);
00565
00566 if (full_dump)
00567 put_node(cout, *(get_top_level_node()), false, 2);
00568 }
00569
00570 if (decombine) {
00571
00572
00573
00574 split_top_level_node(get_top_level_node(), full_dump);
00575
00576 }
00577
00578 } else {
00579
00580
00581
00582
00583
00584 if (full_dump)
00585 put_node(cout, *(get_top_level_node()), false, 3);
00586
00587 move_node(bcoll, this);
00588
00589 if (full_dump)
00590 put_node(cout, *(get_top_level_node()), false, 2);
00591 }
00592 }
00593
00594
00595
00596
00597
00598
00599 if (full_dump)
00600 put_node(cout, *(get_top_level_node()), false, 3);
00601
00602
00603
00604 cerr << "calculating energies..." << endl << flush;
00605 real epot0, ekin0, etot0;
00606
00607
00608
00609
00610
00611
00612
00613 calculate_energies_with_external(get_root(), epot0, ekin0, etot0);
00614 PRC(epot0); PRC(ekin0); PRL(etot0);
00615
00616 hdyn* cm = get_parent();
00617
00618 real epot_int = -mass * bcoll->mass / abs(pos - bcoll->pos);
00619 real ekin_int = 0.5 * (mass * square(vel)
00620 + bcoll->mass * square(bcoll->vel));
00621 real etot_int = epot_int + ekin_int;
00622
00623 PRC(epot_int); PRC(ekin_int); PRL(etot_int);
00624 PRL(cpu_time());
00625
00626
00627
00628 label_merger_node(cm);
00629
00630
00631
00632 cm->set_radius(radius + bcoll->radius);
00633 PRL(cm->format_label());
00634
00635
00636
00637 real dm;
00638 vector dv;
00639
00640 if (!use_sstar) {
00641
00642
00643
00644
00645 dm = -MASS_LOSS*cm->mass;
00646
00647 real costh = randinter(-1, 1);
00648 real sinth = sqrt(1 - costh*costh);
00649 if (randinter(0, 1) < 0.5) sinth = -sinth;
00650 real phi = TWO_PI*randinter(0, 1);
00651 dv = KICK_VELOCITY * sqrt(cm->mass/cm->radius)
00652 * vector(sinth*cos(phi),
00653 sinth*sin(phi),
00654 costh);
00655
00656 print_encounter_elements(this, bcoll, "Collision");
00657
00658 if (CONSTANT_DENSITY) {
00659
00660
00661
00662
00663
00664
00665 real rho1 = 0, rho2 = 0;
00666 if (radius > 0) rho1 = mass/pow(radius, 3);
00667 if (bcoll->radius > 0) rho2 = bcoll->mass/pow(bcoll->radius, 3);
00668
00669 if (rho1+rho2 > 0)
00670 cm->set_radius(pow(2*(1-MASS_LOSS)*cm->mass
00671 / (rho1 + rho2), 1.0/3));
00672 else
00673 cm->set_radius(0);
00674
00675 } else
00676 cm->set_radius((1-MASS_LOSS)*cm->get_radius());
00677
00678 cerr << "non-stellar merger: new radius = "
00679 << cm->get_radius() << endl;
00680
00681 } else {
00682
00683
00684
00685
00686 hdyn * primary = this;
00687 hdyn * secondary = bcoll;
00688
00689 cerr << "merge_nodes: merging two STARS"<<endl;
00690
00691
00692 ((star*)(primary->sbase))->dump(cerr, false);
00693 ((star*)(secondary->sbase))->dump(cerr, false);
00694
00695
00696
00697
00698
00699
00700
00701 if (!merge_with_primary(dynamic_cast(star*, primary->sbase),
00702 dynamic_cast(star*, secondary->sbase))) {
00703 primary = bcoll;
00704 secondary = this;
00705 }
00706
00707 print_encounter_elements(primary, secondary, "Collision");
00708
00709 ((star*)(primary->sbase))
00710 ->merge_elements(((star*)(secondary->sbase)));
00711
00712 cerr << "Merger product: "<< endl;
00713 cerr << format_label() << " (";
00714
00715 put_state(make_star_state(primary), cerr);
00716 cerr << "; M=" << get_total_mass(primary) << " [Msun], "
00717 << " R=" << get_effective_radius(primary) << " [Rsun]). " << endl;
00718 ((star*)(primary->sbase))->dump(cerr, false);
00719
00720 real old_mass = cm->mass;
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736 cm->sbase = primary->sbase;
00737 cm->sbase->set_node(cm);
00738 primary->sbase = NULL;
00739
00740 real new_mass = get_total_mass(cm);
00741 dm = new_mass - old_mass;
00742 dv = anomalous_velocity(cm);
00743 }
00744
00745 cerr << "new node name = " << cm->format_label() << endl;
00746 PRL(cpu_time());
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769 correct_leaf_for_change_of_mass(cm, dm);
00770 if (cm->mass < 0) {
00771 cerr << "check and merge, negative mass ";
00772 PRL(cm->mass);
00773 }
00774
00775 cm->set_coll(NULL);
00776
00777
00778
00779 if (square(dv) > 0)
00780 correct_leaf_for_change_of_vector(cm, dv, &hdyn::get_vel,
00781 &hdyn::inc_vel);
00782
00783 cm->set_oldest_daughter(NULL);
00784 cm->remove_perturber_list();
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 real epot, ekin, etot;
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829 calculate_energies_with_external(get_root(), epot, ekin, etot);
00830 PRC(epot); PRC(ekin); PRL(etot);
00831
00832 real de_total = etot - etot0;
00833 vector vcm = hdyn_something_relative_to_root(cm, &hdyn::get_vel);
00834 real de_kick = 0.5 * cm->mass * (vcm*vcm - square(vcm-dv));
00835 real de_int = -etot_int;
00836
00837 PRC(de_total); PRC(de_int); PRL(de_kick);
00838 PRL(cpu_time());
00839
00840
00841
00842 kc->dm_massloss -= dm;
00843
00844 kc->de_total += de_total;
00845 kc->de_merge += de_int;
00846 kc->de_massloss += de_total - de_int - de_kick;
00847 kc->de_kick += de_kick;
00848
00849 hdyn* root = cm->get_root();
00850 xreal time = cm->time;
00851
00852
00853
00854
00855
00856
00857
00858
00859 if (cm->mass <= 0) {
00860
00861
00862
00863
00864 remove_node_and_correct_upto_ancestor(cm->get_parent(), cm);
00865 }
00866
00867 predict_loworder_all(root, time);
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882 hdynptr del[2];
00883 del[0] = this;
00884 del[1] = bcoll;
00885
00886 if (RESOLVE_UNPERTURBED_PERTURBERS || is_pert)
00887 correct_perturber_lists(get_root(), del, 2, cm);
00888
00889 correct_nn_pointers(get_root(), del, 2, cm);
00890
00891
00892
00893 if (cm->on_perturbed_list()) {
00894 cerr << "Removing " << cm->format_label()
00895 << " from perturbed binary list " << endl;
00896 cm->remove_from_perturbed_list();
00897 } else
00898 cerr << cm->format_label() << " not on perturbed binary list (OK)"
00899 << endl;
00900
00901 cerr << endl;
00902
00903
00904
00905
00906
00907
00908 if (full_dump) {
00909 hdyn *top = cm->get_top_level_node();
00910 predict_loworder_all(top, cm->get_system_time());
00911 put_node(cout, *top, false, 2);
00912 }
00913
00914 return cm;
00915 }