Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

compute_mean_cod.C

Go to the documentation of this file.
00001 
00020 //-----------------------------------------------------------------------------
00021 //   version 1:  Nov 1994   Piet Hut
00022 //   version 2:  Jul 1996   Steve McMillan & Jun Makino
00023 //.............................................................................
00026 //.............................................................................
00027 //   non-local function: 
00028 //      compute_mean_cod
00029 //.............................................................................
00030 //   see also: density.C
00031 //-----------------------------------------------------------------------------
00032 
00033 #include "dyn.h"
00034 
00035 #ifndef TOOLBOX
00036 
00037 //-----------------------------------------------------------------------------
00038 //  compute_mean_cod -- Returns the position and velocity of the density
00039 //                      center of an N-body system.
00040 //-----------------------------------------------------------------------------
00041 
00042 #define MAX_COUNT 5
00043 
00044 void compute_mean_cod(dyn *b, vector& pos, vector& vel)
00045 {
00046     real total_weight = 0;
00047     bool print_message = true;
00048     int count = 0;
00049 
00050     pos = 0;
00051     vel = 0;
00052     
00053 //  for_all_leaves(dyn, b, d)
00054     for_all_daughters(dyn, b, d) {
00055 
00056         real dens_time = getrq(d->get_dyn_story(), "density_time");
00057 
00058         if (print_message
00059             && !twiddles(dens_time, (real) b->get_system_time(), 1.e-9)) {
00060             warning("compute_mean_cod: using out-of-date densities.");
00061             PRL(d->format_label());
00062             int p = cerr.precision(HIGH_PRECISION);
00063             PRL(b->get_system_time());
00064             PRL(dens_time);
00065             cerr.precision(p);
00066             if (++count > MAX_COUNT) print_message = false;
00067         }
00068 
00069         real this_density = getrq(d->get_dyn_story(), "density");
00070 
00071         if (this_density > 0) {
00072             real dens2 = this_density * this_density;       // weight factor
00073             total_weight += dens2;
00074             pos += dens2 * d->get_pos();
00075             vel += dens2 * d->get_vel();
00076         } else if (this_density <= -VERY_LARGE_NUMBER) {
00077             warning("compute_mean_cod: density not set.");
00078             PRL(d->format_label());
00079             if (++count > MAX_COUNT) print_message = false;
00080         }
00081     }   
00082 
00083     if (total_weight > 0) {
00084         pos /= total_weight;
00085         vel /= total_weight;
00086     }
00087 
00088     putsq(b->get_dyn_story(), "density_center_type", "mean");
00089     putrq(b->get_dyn_story(), "density_center_time", b->get_system_time());
00090     putvq(b->get_dyn_story(), "density_center_pos", pos);
00091     putvq(b->get_dyn_story(), "density_center_vel", vel);
00092 }
00093 
00094 void compute_mean_cod(dyn *b)
00095 {
00096     vector pos, vel;
00097     compute_mean_cod(b, pos, vel);
00098 }
00099 
00100 #else
00101 
00102 //-----------------------------------------------------------------------------
00103 //  main  --  driver to use compute_mean_cod() as a tool
00104 //-----------------------------------------------------------------------------
00105 
00106 main(int argc, char ** argv)
00107 {
00108     char  *comment;
00109     dyn * b;
00110     bool  c_flag = FALSE;       // if TRUE: a comment given on command line
00111 
00112     check_help();
00113 
00114     extern char *poptarg;
00115     int c;
00116     char* param_string = "c:";
00117 
00118     while ((c = pgetopt(argc, argv, param_string)) != -1)
00119         switch(c) {
00120 
00121             case 'c': c_flag = TRUE;
00122                       comment = poptarg;
00123                       break;
00124             case '?': params_to_usage(cerr, argv[0], param_string);
00125                       get_help();
00126                       exit(1);
00127         }            
00128 
00129     if ((b = get_dyn(cin)) == NULL)
00130        err_exit("compute_mean_cod: No N-body system on standard input");
00131 
00132     while (b) {
00133 
00134         if (c_flag == TRUE)
00135             b->log_comment(comment);
00136         b->log_history(argc, argv);
00137 
00138         compute_mean_cod(b);
00139 
00140         put_dyn(cout, *b);
00141         rmtree(b);
00142         b = get_dyn(cin);
00143     }
00144 }
00145 
00146 #endif
00147 
00148 
00149 // endof: compute_mean_cod.C

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