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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 #include "hdyn.h"
00119 #include "hdyn_inline.C"
00120
00121 bool has_binary_perturbers(hdyn* b)
00122 {
00123
00124
00125 hdyn *pnode = b->find_perturber_node();
00126
00127 if (pnode == NULL || !pnode->get_valid_perturbers()) {
00128
00129
00130
00131
00132
00133 return true;
00134 }
00135
00136
00137
00138
00139
00140
00141 for (int k = 0; k < pnode->get_n_perturbers(); k++) {
00142 hdyn * p = pnode->get_perturber_list()[k];
00143 if (p->is_parent() || p->is_low_level_node()) return true;
00144 }
00145
00146 return false;
00147 }
00148
00149
00150
00151
00152
00153 static real k3[SLOW_BINS+1] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00154
00155 local void set_k3()
00156 {
00157 int k2 = 1;
00158 for (int k = 0; k < SLOW_BINS; k++) {
00159 k3[k] = pow(k2, 1.0/3);
00160 k2 *= 2;
00161 }
00162 }
00163
00164 local int get_slowdown_factor(hdyn* bi, real P = 0)
00165 {
00166
00167
00168 if (*k3 == 0) set_k3();
00169
00170
00171
00172
00173 hdyn* pnode = bi->find_perturber_node();
00174
00175 if (pnode == NULL || !pnode->get_valid_perturbers()) return -1;
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 if (P <= 0)
00187 P = get_period(bi, bi->get_younger_sister());
00188
00189 if (P <= 0 || P == VERY_LARGE_NUMBER) return -1;
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 int kappa = bi->get_max_slow_factor();
00201
00202 hdyn *par = bi->get_parent();
00203 real scale = binary_scale(par);
00204
00205
00206
00207 hdyn *top = bi->get_top_level_node();
00208 vector tpos = top->get_pred_pos();
00209 vector tvel = top->get_pred_vel();
00210 vector tacc = top->get_acc();
00211
00212
00213
00214 for (int i = 0; i < pnode->get_n_perturbers(); i++) {
00215
00216 hdyn *bj = pnode->get_perturber_list()[i];
00217
00218
00219
00220
00221
00222
00223 real rpert3 = crit_separation_cubed(par,
00224 bj->get_mass(),
00225 scale,
00226 bi->get_max_slow_perturbation());
00227
00228 real rpert = pow(rpert3, 1.0/3);
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 vector dpos = 0, dvel = 0, dacc = 0;
00240
00241 hdyn *jtop = bj->get_top_level_node();
00242
00243 if (jtop != top) {
00244 dpos = tpos - jtop->get_pred_pos();
00245 dvel = tvel - jtop->get_pred_vel();
00246 dacc = tacc - jtop->get_acc();
00247 } else {
00248 hdyn *sis = par->get_binary_sister();
00249 dpos = par->get_pos() - sis->get_pos();
00250 dvel = par->get_vel() - sis->get_vel();
00251 dacc = par->get_acc() - sis->get_acc();
00252 }
00253
00254 real dr = abs(dpos);
00255 real vr = dvel * dpos / dr;
00256 real ar = dacc * dpos / dr;
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 int k = 0, k2 = 1;
00267 while (k2 <= kappa) {
00268
00269 if (time_to_radius(dr - rpert * k3[k], vr, ar) < k2 * P)
00270 break;
00271
00272 k++;
00273 k2 *= 2;
00274 }
00275
00276 kappa = min(kappa, k2/2);
00277 if (kappa < 2) break;
00278 }
00279
00280
00281
00282 return kappa;
00283 }
00284
00285
00286
00287
00288
00289 void hdyn::startup_slow_motion()
00290 {
00291 int k = get_slowdown_factor(this);
00292
00293 if (k > 1) {
00294
00295 create_slow(k);
00296
00297
00298
00299
00300
00301
00302
00303 if (diag->slow) {
00304 cerr << endl
00305 << "starting slow motion for "
00306 << get_parent()->format_label()
00307 << " at time " << get_time()
00308 << "; kappa = " << get_kappa()
00309 << endl;
00310 cerr << " perturbation = " << sqrt(perturbation_squared)
00311 << endl
00312 << " timestep = " << timestep
00313 << " parent timestep = " << get_parent()->timestep
00314 << endl;
00315 if (get_parent()->get_nn())
00316 cerr << " nn of parent = "
00317 << get_parent()->get_nn()->format_label() << endl;
00318 }
00319
00320
00321
00322 hdyn *par = get_parent();
00323 if (par->is_top_level_node()) {
00324
00325
00326
00327
00328 hdyn *pnode = find_perturber_node();
00329
00330 if (!pnode || !pnode->get_valid_perturbers()) {
00331
00332
00333
00334
00335 cerr << "startup_slow_motion: "
00336 << "no valid perturber node for new slow binary "
00337 << get_parent()->format_label() << endl;
00338
00339 } else {
00340
00341 if (diag->slow)
00342 cerr << " perturber node is " << pnode->format_label()
00343 << " n_perturbers = " << pnode->get_n_perturbers()
00344 << endl;
00345
00346 for (int j = 0; j < pnode->get_n_perturbers(); j++) {
00347
00348 hdyn *pert_top = pnode->get_perturber_list()[j]
00349 ->get_top_level_node();
00350
00351
00352
00353
00354
00355 if (pert_top != par) {
00356
00357 slow_perturbed *s = pert_top->find_slow_perturbed(par);
00358 if (!s)
00359 s = pert_top->add_slow_perturbed(par,
00360 diag->slow_perturbed);
00361
00362 if (diag->slow && diag->slow_level > 0)
00363 cerr << " updated slow_perturbed list"
00364 << " of top-level node "
00365 << pert_top->format_label() << endl;
00366 }
00367 }
00368 }
00369 }
00370 }
00371 }
00372
00373 static char* term_reason[4] = {"forced",
00374 "invalid plist",
00375 "binary perturber",
00376 "perturbed"};
00377
00378 void hdyn::extend_or_end_slow_motion(real P)
00379
00380 {
00381 int k = -2, old_k = get_kappa();
00382
00383 if (!slow->get_stop())
00384 k = get_slowdown_factor(this, P);
00385
00386 if (k > 1) {
00387
00388
00389
00390 if (k != old_k) {
00391
00392
00393
00394 extend_slow(k);
00395
00396
00397
00398 hdyn *par = get_parent();
00399 if (par->is_top_level_node()) {
00400
00401 hdyn *pnode = find_perturber_node();
00402 if (pnode && pnode->get_valid_perturbers()) {
00403
00404 for (int j = 0; j < pnode->get_n_perturbers(); j++) {
00405
00406 hdyn *pert_top = pnode->get_perturber_list()[j]
00407 ->get_top_level_node();
00408
00409 if (pert_top != par) {
00410
00411 slow_perturbed *s = pert_top
00412 ->find_slow_perturbed(par);
00413 if (!s)
00414 s = pert_top->add_slow_perturbed(par,
00415 diag->slow_perturbed);
00416
00417 s->set_kappa(k);
00418
00419 if (diag->slow && diag->slow_level > 0)
00420 cerr << " updated slow_perturbed list of"
00421 << " top-level node "
00422 << pert_top->format_label() << endl;
00423 }
00424 }
00425 }
00426 }
00427
00428 } else
00429
00430
00431
00432
00433 slow->set_t_apo(get_time());
00434
00435 if (diag->slow && (diag->slow_level > 0 || k != old_k))
00436 cerr << endl
00437 << "extending slow motion for "
00438 << get_parent()->format_label()
00439 << " at time " << get_time()
00440 << "; new kappa = " << get_kappa()
00441 << endl;
00442
00443 } else {
00444
00445
00446
00447
00448 hdyn *par = get_parent();
00449
00450 if (diag->slow)
00451 cerr << endl
00452 << "ending slow motion for "
00453 << par->format_label()
00454 << " at time " << get_time()
00455 << " (" << term_reason[k+2] << ")"
00456 << endl;
00457
00458 if (par->is_top_level_node()) {
00459
00460 hdyn *pnode = find_perturber_node();
00461
00462 if (pnode && pnode->get_valid_perturbers())
00463 for (int j = 0; j < pnode->n_perturbers; j++) {
00464 hdyn *pert_top = pnode->perturber_list[j]
00465 ->get_top_level_node();
00466
00467 if (pert_top != par) {
00468 #if 0
00469 cerr << "correcting slow_perturbed list of "
00470 << pert_top->format_label() << endl;
00471 #endif
00472 pert_top->remove_slow_perturbed(par,
00473 diag->slow_perturbed);
00474 }
00475 }
00476 }
00477
00478 delete_slow();
00479 }
00480 }
00481
00482 void clear_perturbers_slow_perturbed(hdyn * b)
00483 {
00484
00485
00486
00487 if (b->get_valid_perturbers())
00488 for (int j = 0; j < b->get_n_perturbers(); j++) {
00489 hdyn * pert_top = b->get_perturber_list()[j]
00490 ->get_top_level_node();
00491 #if 0
00492 cerr << "removing " << b->format_label();
00493 cerr << " from slow_perturbed list of "
00494 << pert_top->format_label()
00495 << endl;
00496 #endif
00497 pert_top->remove_slow_perturbed(b, b->get_kira_diag()
00498 ->slow_perturbed);
00499 }
00500 }
00501
00502 void list_slow_perturbed(hdyn *b)
00503 {
00504
00505
00506 bool found = false;
00507
00508 for_all_nodes(hdyn, b, bb) {
00509 slow_perturbed *s = bb->get_sp();
00510 if (s) {
00511 found = true;
00512 bb->dump_slow_perturbed(" ");
00513 }
00514 }
00515
00516 if (!found)
00517 cerr << " no slow_perturbed data found for "
00518 << b->format_label() << endl;
00519 }
00520
00521 void check_slow_consistency(hdyn *b)
00522 {
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 hdyn *root = b->get_root();
00543
00544 for_all_nodes(hdyn, b, bi) {
00545 if (bi != root) {
00546
00547 slow_perturbed *s = bi->get_sp();
00548
00549
00550
00551 if (s && !bi->is_top_level_node())
00552 cerr << "check_slow_consistency: low-level node "
00553 << bi->format_label() << " has a slow_perturbed pointer"
00554 << endl;
00555
00556
00557
00558 while (s) {
00559 slow_perturbed *s1 = s->get_next();
00560 while (s1) {
00561 if (s1->get_node() == s->get_node()) {
00562 cerr << "check_slow_consistency: node "
00563 << s->get_node()->format_label()
00564 << " (" << s->get_node()
00565 << ")" << endl;
00566 cerr << " "
00567 << "duplicated on slow_perturbed list of "
00568 << bi->format_label() << endl;
00569 }
00570 s1 = s1->get_next();
00571 }
00572 s = s->get_next();
00573 }
00574
00575
00576
00577 if (bi->is_top_level_node()) {
00578
00579 s = bi->get_sp();
00580
00581 while (s) {
00582
00583 hdyn *pert_node = (hdyn*)s->get_node();
00584
00585 if (!is_valid_slow(pert_node)
00586 || !pert_node->is_top_level_node()) {
00587
00588 cerr << "check_slow_consistency: node "
00589 << pert_node->format_label()
00590 << " (" << pert_node
00591 << ")" << endl;
00592 cerr << " "
00593 << "on slow_perturbed list of "
00594 << bi->format_label() << endl
00595 << " "
00596 << "is ";
00597 if (!pert_node->is_valid())
00598 cerr << "invalid";
00599 else if (pert_node->is_top_level_node())
00600 cerr << "not a slow binary CM";
00601 else
00602 cerr << "not a top-level binary";
00603 cerr << " at time " << b->get_system_time() << endl;
00604
00605 } else {
00606
00607
00608
00609
00610
00611
00612
00613 hdyn *pnode = pert_node->get_oldest_daughter()
00614 ->find_perturber_node();
00615 if (!pnode) {
00616
00617 cerr << "check_slow_consistency: no perturber node"
00618 << " for " << pert_node->format_label()
00619 << " (" << pert_node << ")"
00620 << endl;
00621 cerr << " "
00622 << "on slow_perturbed list of "
00623 << bi->format_label()
00624 << endl;
00625 cerr << " "
00626 << "at time " << b->get_system_time()
00627 << endl;
00628
00629 } else {
00630
00631
00632
00633
00634
00635
00636
00637 bool found = false;
00638 for (int k = 0;
00639 k < pnode->get_n_perturbers(); k++) {
00640 hdyn* pk_top = pnode->get_perturber_list()[k]
00641 ->get_top_level_node();
00642 if (pk_top == bi) {
00643 found = true;
00644 break;
00645 }
00646 }
00647
00648 if (!found) {
00649
00650 cerr << "check_slow_consistency: node "
00651 << pert_node->format_label()
00652 << " (" << pert_node
00653 << ") on slow_perturbed list"
00654 << endl;
00655 cerr << " "
00656 << "of " << bi->format_label()
00657 << " is not perturbed by it"
00658 << endl;
00659
00660 pert_node->print_perturber_list();
00661
00662 } else {
00663
00664
00665
00666 real pk = pert_node->get_oldest_daughter()
00667 ->get_kappa();
00668
00669 if (pk != s->get_kappa()) {
00670 cerr << "check_slow_consistency: node "
00671 << pert_node->format_label()
00672 << " on slow_perturbed list of ";
00673 cerr << bi->format_label()
00674 << " has kappa = " << pk
00675 << " != " << s->get_kappa()
00676 << endl;
00677 }
00678 }
00679 }
00680 }
00681
00682 s = s->get_next();
00683
00684 }
00685 }
00686 }
00687 }
00688
00689
00690
00691 for_all_leaves(hdyn, b, bi) {
00692 if (bi->is_low_level_node()
00693 && bi->get_elder_sister() == NULL
00694 && bi->get_slow()) {
00695
00696 hdyn *pnode = bi->find_perturber_node();
00697 hdyn *par = bi->get_parent();
00698
00699 if (!pnode || !pnode->get_valid_perturbers()) {
00700
00701 cerr << "check_slow_consistency: "
00702 << "no valid perturber node for slow binary "
00703 << par->format_label() << endl;
00704
00705 } else {
00706
00707 if (par->is_top_level_node()) {
00708
00709 for (int k = 0; k < pnode->get_n_perturbers(); k++) {
00710
00711 hdyn *pert_top = pnode->get_perturber_list()[k]
00712 ->get_top_level_node();
00713
00714 if (pert_top != par) {
00715
00716
00717
00718
00719 bool found = false;
00720 slow_perturbed *s = pert_top->get_sp();
00721
00722 while (s) {
00723 if (s->get_node() == par) found = true;
00724 s = s->get_next();
00725 }
00726 if (!found) {
00727
00728 cerr << "check_slow_consistency: "
00729 << "no reference to slow binary CM "
00730 << par->format_label() << endl;
00731 cerr << " "
00732 << "on slow_perturbed list of perturber "
00733 << pert_top->format_label()
00734 << endl;
00735 cerr << " "
00736 << "at time " << b->get_system_time()
00737 << endl;
00738
00739 bi->print_pert();
00740 par->print_perturber_list();
00741
00742 pert_top->print_slow_perturbed();
00743 }
00744 }
00745 }
00746 }
00747 }
00748 }
00749 }
00750 }