00001
00020
00021
00022
00023
00026
00027
00028
00029
00030
00031
00032
00033 #include "dyn.h"
00034
00035 #ifndef TOOLBOX
00036
00037
00038
00039
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
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;
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
00104
00105
00106 main(int argc, char ** argv)
00107 {
00108 char *comment;
00109 dyn * b;
00110 bool c_flag = FALSE;
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