Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

correct_perturbers.C

Go to the documentation of this file.
00001 
00002        //=======================================================//    _\|/_
00003       //  __  _____           ___                    ___       //      /|\ ~
00004      //  /      |      ^     |   \  |         ^     |   \     //          _\|/_
00005     //   \__    |     / \    |___/  |        / \    |___/    //            /|\ ~
00006    //       \   |    /___\   |  \   |       /___\   |   \   // _\|/_
00007   //     ___/   |   /     \  |   \  |____  /     \  |___/  //   /|\ ~
00008  //                                                       //            _\|/_
00009 //=======================================================//              /|\ ~
00010 
00011 // Remove escaping or destroyed nodes from all perturber lists.
00012 //
00013 // Externally visible functions:
00014 //
00015 //      void correct_perturber_lists
00016 //      void expand_perturber_lists
00017 
00018 #include "hdyn.h"
00019 
00020 int counter = 0;
00021 
00022 local void check_format_and_print(hdyn* bi)
00023 {
00024     if (counter%5 == 0) {
00025         cerr << endl << "  ";
00026     }
00027     cerr << "  " << bi->format_label();
00028     counter++;
00029 }
00030 
00031 local void check_and_correct_perturbers(hdyn* bi, hdyn ** ilist, int n,
00032                                         hdyn * cm)
00033 {
00034     if (bi->get_perturber_list() == NULL) {
00035         cerr << endl << "warning: check_and_correct_perturbers: "
00036              << bi->format_label()
00037              << " has valid_perturbers but no perturber_list" << endl;
00038         return;
00039     }
00040 
00041     hdynptr* plist = bi->get_perturber_list();          // convenient shorthand
00042 
00043     bool first = true;
00044     int null_count = 0;
00045 
00046     for (int i = 0; i < bi->get_n_perturbers(); i++) {
00047         hdyn* p = plist[i];
00048         for (int j = 0; j < n; j++) {
00049             if (ilist[j] == p) {
00050 
00051                 // Replace first plist member found to be on ilist by cm
00052                 // (which may be NULL).  Set all others NULL.
00053 
00054                 if (first) {
00055                     if (bi->get_kira_diag()->report_correct_perturber_list)
00056                         check_format_and_print(bi);
00057                     plist[i] = cm;
00058                 } else
00059                     plist[i] = NULL;
00060 
00061                 first = false;
00062                 if (plist[i] == NULL) null_count++;
00063 
00064                 break;          
00065             }
00066         }
00067     }
00068 
00069     // Do a second pass through the list to remove NULLs.
00070 
00071     if (null_count > 0) {
00072         int offset = 0;
00073         for (int i = 0; i < bi->get_n_perturbers(); i++) {
00074             if (offset > 0) plist[i-offset] = plist[i];
00075             if (plist[i] == NULL) offset++;
00076         }
00077         bi->set_n_perturbers(bi->get_n_perturbers()-null_count);
00078     }
00079 }
00080 
00081 // correct_perturber_lists:  Remove all nodes on the list from any perturber
00082 //                           list that may contain them, replacing the first
00083 //                           by cm, if non-NULL.
00084 
00085 void correct_perturber_lists(hdyn * b, hdyn ** list, int n,
00086                              hdyn * cm) // default = NULL
00087 {
00088     if (n <= 0) return;
00089 
00090     if (b->get_kira_diag()->report_correct_perturber_list) {
00091         cerr << endl << "correct_perturber_lists at time "
00092              << b->get_system_time() << ": ";
00093         PRL(ALLOW_LOW_LEVEL_PERTURBERS);
00094 
00095         cerr << "removing nodes:";
00096         for (int i = 0; i < n; i++) cerr << "  " << list[i]->format_label();
00097         cerr << ",  cm = ";
00098         if (cm)
00099             cerr << cm->format_label();
00100         else
00101             cerr << "NULL";
00102         cerr << flush;
00103 
00104         counter = 0;
00105     }
00106 
00107     if (ALLOW_LOW_LEVEL_PERTURBERS) {
00108 
00109         for_all_nodes(hdyn, b, bi)
00110             if (bi->get_valid_perturbers())
00111                 check_and_correct_perturbers(bi, list, n, cm);
00112 
00113     } else {
00114 
00115         for_all_daughters(hdyn, b, bi)
00116             if (bi->get_valid_perturbers())
00117                 check_and_correct_perturbers(bi, list, n, cm);
00118     }
00119 
00120     if (b->get_kira_diag()->report_correct_perturber_list) {
00121         if (counter == 0) cerr << "  (no corrections)";
00122         cerr << endl;
00123     }
00124 
00125     // cerr << "Leaving correct_perturber_lists." << endl << flush;
00126 }
00127 
00128 local int expand_perturber_list(hdyn* bi, hdyn* bb, bool verbose)
00129 {
00130     if (bi->get_perturber_list() == NULL) {
00131         cerr << endl << "warning: expand_perturber_list: "
00132              << bi->format_label()
00133              << " has valid_perturbers but no perturber_list" << endl;
00134         return 0;
00135     }
00136 
00137     hdynptr* plist = bi->get_perturber_list();          // convenient shorthand
00138     int np = bi->get_n_perturbers();
00139 
00140     for (int i = 0; i < np; i++) {
00141         hdyn* p = plist[i];
00142         if (plist[i] == bb) {
00143 
00144             if (verbose) check_format_and_print(bi);
00145 
00146             // Should only be expanding unperturbed binaries.  Flag
00147             // expansion of perturbed binaries.
00148 
00149             if (!bb->get_oldest_daughter()->get_kepler()) {
00150                 cerr << "warning: expand_perturber_list for node "
00151                      << bi->format_label() << ": ";
00152                 cerr << bb->format_label() << " is perturbed" << endl;
00153             }
00154 
00155             if (np >= MAX_PERTURBERS - 1) {
00156 
00157                 // List will overflow if expanded.  Make it invalid.
00158 
00159                 bi->set_valid_perturbers(false);
00160 
00161             } else {
00162 
00163                 // Remove bb from the list...
00164 
00165                 for (int j = i+1; j < np; j++)
00166                     plist[j-1] = plist[j];
00167                 np--;
00168 
00169                 // ...then add its components to the end.
00170 
00171                 for_all_leaves(hdyn, bb, bbb)
00172                     plist[np++] = bbb;
00173 
00174                 bi->set_n_perturbers(np);
00175             }
00176 
00177             return 1;
00178         }
00179     }
00180     return 0;
00181 }
00182 
00183 // expand_perturber_lists:  Expand the specified node into components on all
00184 //                          perturber lists that contain it.
00185 
00186 void expand_perturber_lists(hdyn* b, hdyn* bb,  // bb is CM node
00187                             bool verbose)       // default = false
00188 {
00189     if (bb->is_leaf()) return;
00190 
00191     if (verbose) {
00192         cerr << endl
00193              << "expand_perturber_lists at time "
00194              << b->get_system_time() << " to resolve "
00195              << bb->format_label() << ":";
00196         counter = 0;
00197     }
00198 
00199     int np = 0;
00200     if (ALLOW_LOW_LEVEL_PERTURBERS) {
00201         for_all_nodes(hdyn, b, bi)
00202             if (bi != bb && bi->get_valid_perturbers())
00203                 np += expand_perturber_list(bi, bb, verbose);
00204     } else {
00205         for_all_daughters(hdyn, b, bi)
00206             if (bi != bb && bi->get_valid_perturbers())
00207                 np += expand_perturber_list(bi, bb, verbose);
00208     }
00209 
00210     if (verbose) {
00211         if (counter == 0) cerr << "  (no changes)";
00212         cerr << endl;
00213     }
00214 }

Generated at Sun Feb 24 09:56:57 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001